app/testpmd: support extended RSS offload types
[dpdk.git] / app / test-pmd / config.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #include <stdarg.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <inttypes.h>
12
13 #include <sys/queue.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_debug.h>
22 #include <rte_log.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_memzone.h>
26 #include <rte_launch.h>
27 #include <rte_eal.h>
28 #include <rte_per_lcore.h>
29 #include <rte_lcore.h>
30 #include <rte_atomic.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_mempool.h>
33 #include <rte_mbuf.h>
34 #include <rte_interrupts.h>
35 #include <rte_pci.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_string_fns.h>
39 #include <rte_cycles.h>
40 #include <rte_flow.h>
41 #include <rte_errno.h>
42 #ifdef RTE_LIBRTE_IXGBE_PMD
43 #include <rte_pmd_ixgbe.h>
44 #endif
45 #ifdef RTE_LIBRTE_I40E_PMD
46 #include <rte_pmd_i40e.h>
47 #endif
48 #ifdef RTE_LIBRTE_BNXT_PMD
49 #include <rte_pmd_bnxt.h>
50 #endif
51 #include <rte_gro.h>
52
53 #include "testpmd.h"
54
55 #define ETHDEV_FWVERS_LEN 32
56
57 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
58 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
59 #else
60 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
61 #endif
62
63 #define NS_PER_SEC 1E9
64
65 static char *flowtype_to_str(uint16_t flow_type);
66
67 static const struct {
68         enum tx_pkt_split split;
69         const char *name;
70 } tx_split_name[] = {
71         {
72                 .split = TX_PKT_SPLIT_OFF,
73                 .name = "off",
74         },
75         {
76                 .split = TX_PKT_SPLIT_ON,
77                 .name = "on",
78         },
79         {
80                 .split = TX_PKT_SPLIT_RND,
81                 .name = "rand",
82         },
83 };
84
85 const struct rss_type_info rss_type_table[] = {
86         { "all", ETH_RSS_ETH | ETH_RSS_VLAN | ETH_RSS_IP | ETH_RSS_TCP |
87                 ETH_RSS_UDP | ETH_RSS_SCTP | ETH_RSS_L2_PAYLOAD |
88                 ETH_RSS_L2TPV3 | ETH_RSS_ESP | ETH_RSS_AH | ETH_RSS_PFCP |
89                 ETH_RSS_GTPU},
90         { "none", 0 },
91         { "eth", ETH_RSS_ETH },
92         { "l2-src-only", ETH_RSS_L2_SRC_ONLY },
93         { "l2-dst-only", ETH_RSS_L2_DST_ONLY },
94         { "vlan", ETH_RSS_VLAN },
95         { "s-vlan", ETH_RSS_S_VLAN },
96         { "c-vlan", ETH_RSS_C_VLAN },
97         { "ipv4", ETH_RSS_IPV4 },
98         { "ipv4-frag", ETH_RSS_FRAG_IPV4 },
99         { "ipv4-tcp", ETH_RSS_NONFRAG_IPV4_TCP },
100         { "ipv4-udp", ETH_RSS_NONFRAG_IPV4_UDP },
101         { "ipv4-sctp", ETH_RSS_NONFRAG_IPV4_SCTP },
102         { "ipv4-other", ETH_RSS_NONFRAG_IPV4_OTHER },
103         { "ipv6", ETH_RSS_IPV6 },
104         { "ipv6-frag", ETH_RSS_FRAG_IPV6 },
105         { "ipv6-tcp", ETH_RSS_NONFRAG_IPV6_TCP },
106         { "ipv6-udp", ETH_RSS_NONFRAG_IPV6_UDP },
107         { "ipv6-sctp", ETH_RSS_NONFRAG_IPV6_SCTP },
108         { "ipv6-other", ETH_RSS_NONFRAG_IPV6_OTHER },
109         { "l2-payload", ETH_RSS_L2_PAYLOAD },
110         { "ipv6-ex", ETH_RSS_IPV6_EX },
111         { "ipv6-tcp-ex", ETH_RSS_IPV6_TCP_EX },
112         { "ipv6-udp-ex", ETH_RSS_IPV6_UDP_EX },
113         { "port", ETH_RSS_PORT },
114         { "vxlan", ETH_RSS_VXLAN },
115         { "geneve", ETH_RSS_GENEVE },
116         { "nvgre", ETH_RSS_NVGRE },
117         { "ip", ETH_RSS_IP },
118         { "udp", ETH_RSS_UDP },
119         { "tcp", ETH_RSS_TCP },
120         { "sctp", ETH_RSS_SCTP },
121         { "tunnel", ETH_RSS_TUNNEL },
122         { "l3-pre32", RTE_ETH_RSS_L3_PRE32 },
123         { "l3-pre40", RTE_ETH_RSS_L3_PRE40 },
124         { "l3-pre48", RTE_ETH_RSS_L3_PRE48 },
125         { "l3-pre56", RTE_ETH_RSS_L3_PRE56 },
126         { "l3-pre64", RTE_ETH_RSS_L3_PRE64 },
127         { "l3-pre96", RTE_ETH_RSS_L3_PRE96 },
128         { "l3-src-only", ETH_RSS_L3_SRC_ONLY },
129         { "l3-dst-only", ETH_RSS_L3_DST_ONLY },
130         { "l4-src-only", ETH_RSS_L4_SRC_ONLY },
131         { "l4-dst-only", ETH_RSS_L4_DST_ONLY },
132         { "esp", ETH_RSS_ESP },
133         { "ah", ETH_RSS_AH },
134         { "l2tpv3", ETH_RSS_L2TPV3 },
135         { "pfcp", ETH_RSS_PFCP },
136         { "pppoe", ETH_RSS_PPPOE },
137         { "gtpu", ETH_RSS_GTPU },
138         { NULL, 0 },
139 };
140
141 static void
142 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
143 {
144         char buf[RTE_ETHER_ADDR_FMT_SIZE];
145         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
146         printf("%s%s", name, buf);
147 }
148
149 void
150 nic_stats_display(portid_t port_id)
151 {
152         static uint64_t prev_pkts_rx[RTE_MAX_ETHPORTS];
153         static uint64_t prev_pkts_tx[RTE_MAX_ETHPORTS];
154         static uint64_t prev_bytes_rx[RTE_MAX_ETHPORTS];
155         static uint64_t prev_bytes_tx[RTE_MAX_ETHPORTS];
156         static uint64_t prev_ns[RTE_MAX_ETHPORTS];
157         struct timespec cur_time;
158         uint64_t diff_pkts_rx, diff_pkts_tx, diff_bytes_rx, diff_bytes_tx,
159                                                                 diff_ns;
160         uint64_t mpps_rx, mpps_tx, mbps_rx, mbps_tx;
161         struct rte_eth_stats stats;
162         struct rte_port *port = &ports[port_id];
163         uint8_t i;
164
165         static const char *nic_stats_border = "########################";
166
167         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
168                 print_valid_ports();
169                 return;
170         }
171         rte_eth_stats_get(port_id, &stats);
172         printf("\n  %s NIC statistics for port %-2d %s\n",
173                nic_stats_border, port_id, nic_stats_border);
174
175         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
176                 printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
177                        "%-"PRIu64"\n",
178                        stats.ipackets, stats.imissed, stats.ibytes);
179                 printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
180                 printf("  RX-nombuf:  %-10"PRIu64"\n",
181                        stats.rx_nombuf);
182                 printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
183                        "%-"PRIu64"\n",
184                        stats.opackets, stats.oerrors, stats.obytes);
185         }
186         else {
187                 printf("  RX-packets:              %10"PRIu64"    RX-errors: %10"PRIu64
188                        "    RX-bytes: %10"PRIu64"\n",
189                        stats.ipackets, stats.ierrors, stats.ibytes);
190                 printf("  RX-errors:  %10"PRIu64"\n", stats.ierrors);
191                 printf("  RX-nombuf:               %10"PRIu64"\n",
192                        stats.rx_nombuf);
193                 printf("  TX-packets:              %10"PRIu64"    TX-errors: %10"PRIu64
194                        "    TX-bytes: %10"PRIu64"\n",
195                        stats.opackets, stats.oerrors, stats.obytes);
196         }
197
198         if (port->rx_queue_stats_mapping_enabled) {
199                 printf("\n");
200                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
201                         printf("  Stats reg %2d RX-packets: %10"PRIu64
202                                "    RX-errors: %10"PRIu64
203                                "    RX-bytes: %10"PRIu64"\n",
204                                i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
205                 }
206         }
207         if (port->tx_queue_stats_mapping_enabled) {
208                 printf("\n");
209                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
210                         printf("  Stats reg %2d TX-packets: %10"PRIu64
211                                "                             TX-bytes: %10"PRIu64"\n",
212                                i, stats.q_opackets[i], stats.q_obytes[i]);
213                 }
214         }
215
216         diff_ns = 0;
217         if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
218                 uint64_t ns;
219
220                 ns = cur_time.tv_sec * NS_PER_SEC;
221                 ns += cur_time.tv_nsec;
222
223                 if (prev_ns[port_id] != 0)
224                         diff_ns = ns - prev_ns[port_id];
225                 prev_ns[port_id] = ns;
226         }
227
228         diff_pkts_rx = (stats.ipackets > prev_pkts_rx[port_id]) ?
229                 (stats.ipackets - prev_pkts_rx[port_id]) : 0;
230         diff_pkts_tx = (stats.opackets > prev_pkts_tx[port_id]) ?
231                 (stats.opackets - prev_pkts_tx[port_id]) : 0;
232         prev_pkts_rx[port_id] = stats.ipackets;
233         prev_pkts_tx[port_id] = stats.opackets;
234         mpps_rx = diff_ns > 0 ?
235                 (double)diff_pkts_rx / diff_ns * NS_PER_SEC : 0;
236         mpps_tx = diff_ns > 0 ?
237                 (double)diff_pkts_tx / diff_ns * NS_PER_SEC : 0;
238
239         diff_bytes_rx = (stats.ibytes > prev_bytes_rx[port_id]) ?
240                 (stats.ibytes - prev_bytes_rx[port_id]) : 0;
241         diff_bytes_tx = (stats.obytes > prev_bytes_tx[port_id]) ?
242                 (stats.obytes - prev_bytes_tx[port_id]) : 0;
243         prev_bytes_rx[port_id] = stats.ibytes;
244         prev_bytes_tx[port_id] = stats.obytes;
245         mbps_rx = diff_ns > 0 ?
246                 (double)diff_bytes_rx / diff_ns * NS_PER_SEC : 0;
247         mbps_tx = diff_ns > 0 ?
248                 (double)diff_bytes_tx / diff_ns * NS_PER_SEC : 0;
249
250         printf("\n  Throughput (since last show)\n");
251         printf("  Rx-pps: %12"PRIu64"          Rx-bps: %12"PRIu64"\n  Tx-pps: %12"
252                PRIu64"          Tx-bps: %12"PRIu64"\n", mpps_rx, mbps_rx * 8,
253                mpps_tx, mbps_tx * 8);
254
255         printf("  %s############################%s\n",
256                nic_stats_border, nic_stats_border);
257 }
258
259 void
260 nic_stats_clear(portid_t port_id)
261 {
262         int ret;
263
264         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
265                 print_valid_ports();
266                 return;
267         }
268
269         ret = rte_eth_stats_reset(port_id);
270         if (ret != 0) {
271                 printf("%s: Error: failed to reset stats (port %u): %s",
272                        __func__, port_id, strerror(-ret));
273                 return;
274         }
275
276         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
277         if (ret != 0) {
278                 if (ret < 0)
279                         ret = -ret;
280                 printf("%s: Error: failed to get stats (port %u): %s",
281                        __func__, port_id, strerror(ret));
282                 return;
283         }
284         printf("\n  NIC statistics for port %d cleared\n", port_id);
285 }
286
287 void
288 nic_xstats_display(portid_t port_id)
289 {
290         struct rte_eth_xstat *xstats;
291         int cnt_xstats, idx_xstat;
292         struct rte_eth_xstat_name *xstats_names;
293
294         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
295                 print_valid_ports();
296                 return;
297         }
298         printf("###### NIC extended statistics for port %-2d\n", port_id);
299         if (!rte_eth_dev_is_valid_port(port_id)) {
300                 printf("Error: Invalid port number %i\n", port_id);
301                 return;
302         }
303
304         /* Get count */
305         cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
306         if (cnt_xstats  < 0) {
307                 printf("Error: Cannot get count of xstats\n");
308                 return;
309         }
310
311         /* Get id-name lookup table */
312         xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
313         if (xstats_names == NULL) {
314                 printf("Cannot allocate memory for xstats lookup\n");
315                 return;
316         }
317         if (cnt_xstats != rte_eth_xstats_get_names(
318                         port_id, xstats_names, cnt_xstats)) {
319                 printf("Error: Cannot get xstats lookup\n");
320                 free(xstats_names);
321                 return;
322         }
323
324         /* Get stats themselves */
325         xstats = malloc(sizeof(struct rte_eth_xstat) * cnt_xstats);
326         if (xstats == NULL) {
327                 printf("Cannot allocate memory for xstats\n");
328                 free(xstats_names);
329                 return;
330         }
331         if (cnt_xstats != rte_eth_xstats_get(port_id, xstats, cnt_xstats)) {
332                 printf("Error: Unable to get xstats\n");
333                 free(xstats_names);
334                 free(xstats);
335                 return;
336         }
337
338         /* Display xstats */
339         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
340                 if (xstats_hide_zero && !xstats[idx_xstat].value)
341                         continue;
342                 printf("%s: %"PRIu64"\n",
343                         xstats_names[idx_xstat].name,
344                         xstats[idx_xstat].value);
345         }
346         free(xstats_names);
347         free(xstats);
348 }
349
350 void
351 nic_xstats_clear(portid_t port_id)
352 {
353         int ret;
354
355         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
356                 print_valid_ports();
357                 return;
358         }
359
360         ret = rte_eth_xstats_reset(port_id);
361         if (ret != 0) {
362                 printf("%s: Error: failed to reset xstats (port %u): %s",
363                        __func__, port_id, strerror(-ret));
364                 return;
365         }
366
367         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
368         if (ret != 0) {
369                 if (ret < 0)
370                         ret = -ret;
371                 printf("%s: Error: failed to get stats (port %u): %s",
372                        __func__, port_id, strerror(ret));
373                 return;
374         }
375 }
376
377 void
378 nic_stats_mapping_display(portid_t port_id)
379 {
380         struct rte_port *port = &ports[port_id];
381         uint16_t i;
382
383         static const char *nic_stats_mapping_border = "########################";
384
385         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
386                 print_valid_ports();
387                 return;
388         }
389
390         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
391                 printf("Port id %d - either does not support queue statistic mapping or"
392                        " no queue statistic mapping set\n", port_id);
393                 return;
394         }
395
396         printf("\n  %s NIC statistics mapping for port %-2d %s\n",
397                nic_stats_mapping_border, port_id, nic_stats_mapping_border);
398
399         if (port->rx_queue_stats_mapping_enabled) {
400                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
401                         if (rx_queue_stats_mappings[i].port_id == port_id) {
402                                 printf("  RX-queue %2d mapped to Stats Reg %2d\n",
403                                        rx_queue_stats_mappings[i].queue_id,
404                                        rx_queue_stats_mappings[i].stats_counter_id);
405                         }
406                 }
407                 printf("\n");
408         }
409
410
411         if (port->tx_queue_stats_mapping_enabled) {
412                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
413                         if (tx_queue_stats_mappings[i].port_id == port_id) {
414                                 printf("  TX-queue %2d mapped to Stats Reg %2d\n",
415                                        tx_queue_stats_mappings[i].queue_id,
416                                        tx_queue_stats_mappings[i].stats_counter_id);
417                         }
418                 }
419         }
420
421         printf("  %s####################################%s\n",
422                nic_stats_mapping_border, nic_stats_mapping_border);
423 }
424
425 void
426 rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
427 {
428         struct rte_eth_burst_mode mode;
429         struct rte_eth_rxq_info qinfo;
430         int32_t rc;
431         static const char *info_border = "*********************";
432
433         rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
434         if (rc != 0) {
435                 printf("Failed to retrieve information for port: %u, "
436                         "RX queue: %hu\nerror desc: %s(%d)\n",
437                         port_id, queue_id, strerror(-rc), rc);
438                 return;
439         }
440
441         printf("\n%s Infos for port %-2u, RX queue %-2u %s",
442                info_border, port_id, queue_id, info_border);
443
444         printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
445         printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
446         printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
447         printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
448         printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
449         printf("\nRX drop packets: %s",
450                 (qinfo.conf.rx_drop_en != 0) ? "on" : "off");
451         printf("\nRX deferred start: %s",
452                 (qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
453         printf("\nRX scattered packets: %s",
454                 (qinfo.scattered_rx != 0) ? "on" : "off");
455         printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
456
457         if (rte_eth_rx_burst_mode_get(port_id, queue_id, &mode) == 0)
458                 printf("\nBurst mode: %s%s",
459                        mode.info,
460                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
461                                 " (per queue)" : "");
462
463         printf("\n");
464 }
465
466 void
467 tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
468 {
469         struct rte_eth_burst_mode mode;
470         struct rte_eth_txq_info qinfo;
471         int32_t rc;
472         static const char *info_border = "*********************";
473
474         rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
475         if (rc != 0) {
476                 printf("Failed to retrieve information for port: %u, "
477                         "TX queue: %hu\nerror desc: %s(%d)\n",
478                         port_id, queue_id, strerror(-rc), rc);
479                 return;
480         }
481
482         printf("\n%s Infos for port %-2u, TX queue %-2u %s",
483                info_border, port_id, queue_id, info_border);
484
485         printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
486         printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
487         printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
488         printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
489         printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
490         printf("\nTX deferred start: %s",
491                 (qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
492         printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
493
494         if (rte_eth_tx_burst_mode_get(port_id, queue_id, &mode) == 0)
495                 printf("\nBurst mode: %s%s",
496                        mode.info,
497                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
498                                 " (per queue)" : "");
499
500         printf("\n");
501 }
502
503 static int bus_match_all(const struct rte_bus *bus, const void *data)
504 {
505         RTE_SET_USED(bus);
506         RTE_SET_USED(data);
507         return 0;
508 }
509
510 void
511 device_infos_display(const char *identifier)
512 {
513         static const char *info_border = "*********************";
514         struct rte_bus *start = NULL, *next;
515         struct rte_dev_iterator dev_iter;
516         char name[RTE_ETH_NAME_MAX_LEN];
517         struct rte_ether_addr mac_addr;
518         struct rte_device *dev;
519         struct rte_devargs da;
520         portid_t port_id;
521         char devstr[128];
522
523         memset(&da, 0, sizeof(da));
524         if (!identifier)
525                 goto skip_parse;
526
527         if (rte_devargs_parsef(&da, "%s", identifier)) {
528                 printf("cannot parse identifier\n");
529                 if (da.args)
530                         free(da.args);
531                 return;
532         }
533
534 skip_parse:
535         while ((next = rte_bus_find(start, bus_match_all, NULL)) != NULL) {
536
537                 start = next;
538                 if (identifier && da.bus != next)
539                         continue;
540
541                 /* Skip buses that don't have iterate method */
542                 if (!next->dev_iterate)
543                         continue;
544
545                 snprintf(devstr, sizeof(devstr), "bus=%s", next->name);
546                 RTE_DEV_FOREACH(dev, devstr, &dev_iter) {
547
548                         if (!dev->driver)
549                                 continue;
550                         /* Check for matching device if identifier is present */
551                         if (identifier &&
552                             strncmp(da.name, dev->name, strlen(dev->name)))
553                                 continue;
554                         printf("\n%s Infos for device %s %s\n",
555                                info_border, dev->name, info_border);
556                         printf("Bus name: %s", dev->bus->name);
557                         printf("\nDriver name: %s", dev->driver->name);
558                         printf("\nDevargs: %s",
559                                dev->devargs ? dev->devargs->args : "");
560                         printf("\nConnect to socket: %d", dev->numa_node);
561                         printf("\n");
562
563                         /* List ports with matching device name */
564                         RTE_ETH_FOREACH_DEV_OF(port_id, dev) {
565                                 printf("\n\tPort id: %-2d", port_id);
566                                 if (eth_macaddr_get_print_err(port_id,
567                                                               &mac_addr) == 0)
568                                         print_ethaddr("\n\tMAC address: ",
569                                                       &mac_addr);
570                                 rte_eth_dev_get_name_by_port(port_id, name);
571                                 printf("\n\tDevice name: %s", name);
572                                 printf("\n");
573                         }
574                 }
575         };
576 }
577
578 void
579 port_infos_display(portid_t port_id)
580 {
581         struct rte_port *port;
582         struct rte_ether_addr mac_addr;
583         struct rte_eth_link link;
584         struct rte_eth_dev_info dev_info;
585         int vlan_offload;
586         struct rte_mempool * mp;
587         static const char *info_border = "*********************";
588         uint16_t mtu;
589         char name[RTE_ETH_NAME_MAX_LEN];
590         int ret;
591         char fw_version[ETHDEV_FWVERS_LEN];
592
593         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
594                 print_valid_ports();
595                 return;
596         }
597         port = &ports[port_id];
598         ret = eth_link_get_nowait_print_err(port_id, &link);
599         if (ret < 0)
600                 return;
601
602         ret = eth_dev_info_get_print_err(port_id, &dev_info);
603         if (ret != 0)
604                 return;
605
606         printf("\n%s Infos for port %-2d %s\n",
607                info_border, port_id, info_border);
608         if (eth_macaddr_get_print_err(port_id, &mac_addr) == 0)
609                 print_ethaddr("MAC address: ", &mac_addr);
610         rte_eth_dev_get_name_by_port(port_id, name);
611         printf("\nDevice name: %s", name);
612         printf("\nDriver name: %s", dev_info.driver_name);
613
614         if (rte_eth_dev_fw_version_get(port_id, fw_version,
615                                                 ETHDEV_FWVERS_LEN) == 0)
616                 printf("\nFirmware-version: %s", fw_version);
617         else
618                 printf("\nFirmware-version: %s", "not available");
619
620         if (dev_info.device->devargs && dev_info.device->devargs->args)
621                 printf("\nDevargs: %s", dev_info.device->devargs->args);
622         printf("\nConnect to socket: %u", port->socket_id);
623
624         if (port_numa[port_id] != NUMA_NO_CONFIG) {
625                 mp = mbuf_pool_find(port_numa[port_id]);
626                 if (mp)
627                         printf("\nmemory allocation on the socket: %d",
628                                                         port_numa[port_id]);
629         } else
630                 printf("\nmemory allocation on the socket: %u",port->socket_id);
631
632         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
633         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
634         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
635                ("full-duplex") : ("half-duplex"));
636
637         if (!rte_eth_dev_get_mtu(port_id, &mtu))
638                 printf("MTU: %u\n", mtu);
639
640         printf("Promiscuous mode: %s\n",
641                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
642         printf("Allmulticast mode: %s\n",
643                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
644         printf("Maximum number of MAC addresses: %u\n",
645                (unsigned int)(port->dev_info.max_mac_addrs));
646         printf("Maximum number of MAC addresses of hash filtering: %u\n",
647                (unsigned int)(port->dev_info.max_hash_mac_addrs));
648
649         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
650         if (vlan_offload >= 0){
651                 printf("VLAN offload: \n");
652                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
653                         printf("  strip on, ");
654                 else
655                         printf("  strip off, ");
656
657                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
658                         printf("filter on, ");
659                 else
660                         printf("filter off, ");
661
662                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
663                         printf("extend on, ");
664                 else
665                         printf("extend off, ");
666
667                 if (vlan_offload & ETH_QINQ_STRIP_OFFLOAD)
668                         printf("qinq strip on\n");
669                 else
670                         printf("qinq strip off\n");
671         }
672
673         if (dev_info.hash_key_size > 0)
674                 printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
675         if (dev_info.reta_size > 0)
676                 printf("Redirection table size: %u\n", dev_info.reta_size);
677         if (!dev_info.flow_type_rss_offloads)
678                 printf("No RSS offload flow type is supported.\n");
679         else {
680                 uint16_t i;
681                 char *p;
682
683                 printf("Supported RSS offload flow types:\n");
684                 for (i = RTE_ETH_FLOW_UNKNOWN + 1;
685                      i < sizeof(dev_info.flow_type_rss_offloads) * CHAR_BIT; i++) {
686                         if (!(dev_info.flow_type_rss_offloads & (1ULL << i)))
687                                 continue;
688                         p = flowtype_to_str(i);
689                         if (p)
690                                 printf("  %s\n", p);
691                         else
692                                 printf("  user defined %d\n", i);
693                 }
694         }
695
696         printf("Minimum size of RX buffer: %u\n", dev_info.min_rx_bufsize);
697         printf("Maximum configurable length of RX packet: %u\n",
698                 dev_info.max_rx_pktlen);
699         printf("Maximum configurable size of LRO aggregated packet: %u\n",
700                 dev_info.max_lro_pkt_size);
701         if (dev_info.max_vfs)
702                 printf("Maximum number of VFs: %u\n", dev_info.max_vfs);
703         if (dev_info.max_vmdq_pools)
704                 printf("Maximum number of VMDq pools: %u\n",
705                         dev_info.max_vmdq_pools);
706
707         printf("Current number of RX queues: %u\n", dev_info.nb_rx_queues);
708         printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
709         printf("Max possible number of RXDs per queue: %hu\n",
710                 dev_info.rx_desc_lim.nb_max);
711         printf("Min possible number of RXDs per queue: %hu\n",
712                 dev_info.rx_desc_lim.nb_min);
713         printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
714
715         printf("Current number of TX queues: %u\n", dev_info.nb_tx_queues);
716         printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
717         printf("Max possible number of TXDs per queue: %hu\n",
718                 dev_info.tx_desc_lim.nb_max);
719         printf("Min possible number of TXDs per queue: %hu\n",
720                 dev_info.tx_desc_lim.nb_min);
721         printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
722         printf("Max segment number per packet: %hu\n",
723                 dev_info.tx_desc_lim.nb_seg_max);
724         printf("Max segment number per MTU/TSO: %hu\n",
725                 dev_info.tx_desc_lim.nb_mtu_seg_max);
726
727         /* Show switch info only if valid switch domain and port id is set */
728         if (dev_info.switch_info.domain_id !=
729                 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
730                 if (dev_info.switch_info.name)
731                         printf("Switch name: %s\n", dev_info.switch_info.name);
732
733                 printf("Switch domain Id: %u\n",
734                         dev_info.switch_info.domain_id);
735                 printf("Switch Port Id: %u\n",
736                         dev_info.switch_info.port_id);
737         }
738 }
739
740 void
741 port_summary_header_display(void)
742 {
743         uint16_t port_number;
744
745         port_number = rte_eth_dev_count_avail();
746         printf("Number of available ports: %i\n", port_number);
747         printf("%-4s %-17s %-12s %-14s %-8s %s\n", "Port", "MAC Address", "Name",
748                         "Driver", "Status", "Link");
749 }
750
751 void
752 port_summary_display(portid_t port_id)
753 {
754         struct rte_ether_addr mac_addr;
755         struct rte_eth_link link;
756         struct rte_eth_dev_info dev_info;
757         char name[RTE_ETH_NAME_MAX_LEN];
758         int ret;
759
760         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
761                 print_valid_ports();
762                 return;
763         }
764
765         ret = eth_link_get_nowait_print_err(port_id, &link);
766         if (ret < 0)
767                 return;
768
769         ret = eth_dev_info_get_print_err(port_id, &dev_info);
770         if (ret != 0)
771                 return;
772
773         rte_eth_dev_get_name_by_port(port_id, name);
774         ret = eth_macaddr_get_print_err(port_id, &mac_addr);
775         if (ret != 0)
776                 return;
777
778         printf("%-4d %02X:%02X:%02X:%02X:%02X:%02X %-12s %-14s %-8s %uMbps\n",
779                 port_id, mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
780                 mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
781                 mac_addr.addr_bytes[4], mac_addr.addr_bytes[5], name,
782                 dev_info.driver_name, (link.link_status) ? ("up") : ("down"),
783                 (unsigned int) link.link_speed);
784 }
785
786 void
787 port_offload_cap_display(portid_t port_id)
788 {
789         struct rte_eth_dev_info dev_info;
790         static const char *info_border = "************";
791         int ret;
792
793         if (port_id_is_invalid(port_id, ENABLED_WARN))
794                 return;
795
796         ret = eth_dev_info_get_print_err(port_id, &dev_info);
797         if (ret != 0)
798                 return;
799
800         printf("\n%s Port %d supported offload features: %s\n",
801                 info_border, port_id, info_border);
802
803         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) {
804                 printf("VLAN stripped:                 ");
805                 if (ports[port_id].dev_conf.rxmode.offloads &
806                     DEV_RX_OFFLOAD_VLAN_STRIP)
807                         printf("on\n");
808                 else
809                         printf("off\n");
810         }
811
812         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_QINQ_STRIP) {
813                 printf("Double VLANs stripped:         ");
814                 if (ports[port_id].dev_conf.rxmode.offloads &
815                     DEV_RX_OFFLOAD_QINQ_STRIP)
816                         printf("on\n");
817                 else
818                         printf("off\n");
819         }
820
821         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM) {
822                 printf("RX IPv4 checksum:              ");
823                 if (ports[port_id].dev_conf.rxmode.offloads &
824                     DEV_RX_OFFLOAD_IPV4_CKSUM)
825                         printf("on\n");
826                 else
827                         printf("off\n");
828         }
829
830         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM) {
831                 printf("RX UDP checksum:               ");
832                 if (ports[port_id].dev_conf.rxmode.offloads &
833                     DEV_RX_OFFLOAD_UDP_CKSUM)
834                         printf("on\n");
835                 else
836                         printf("off\n");
837         }
838
839         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM) {
840                 printf("RX TCP checksum:               ");
841                 if (ports[port_id].dev_conf.rxmode.offloads &
842                     DEV_RX_OFFLOAD_TCP_CKSUM)
843                         printf("on\n");
844                 else
845                         printf("off\n");
846         }
847
848         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SCTP_CKSUM) {
849                 printf("RX SCTP checksum:              ");
850                 if (ports[port_id].dev_conf.rxmode.offloads &
851                     DEV_RX_OFFLOAD_SCTP_CKSUM)
852                         printf("on\n");
853                 else
854                         printf("off\n");
855         }
856
857         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM) {
858                 printf("RX Outer IPv4 checksum:        ");
859                 if (ports[port_id].dev_conf.rxmode.offloads &
860                     DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM)
861                         printf("on\n");
862                 else
863                         printf("off\n");
864         }
865
866         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_OUTER_UDP_CKSUM) {
867                 printf("RX Outer UDP checksum:         ");
868                 if (ports[port_id].dev_conf.rxmode.offloads &
869                     DEV_RX_OFFLOAD_OUTER_UDP_CKSUM)
870                         printf("on\n");
871                 else
872                         printf("off\n");
873         }
874
875         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO) {
876                 printf("Large receive offload:         ");
877                 if (ports[port_id].dev_conf.rxmode.offloads &
878                     DEV_RX_OFFLOAD_TCP_LRO)
879                         printf("on\n");
880                 else
881                         printf("off\n");
882         }
883
884         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TIMESTAMP) {
885                 printf("HW timestamp:                  ");
886                 if (ports[port_id].dev_conf.rxmode.offloads &
887                     DEV_RX_OFFLOAD_TIMESTAMP)
888                         printf("on\n");
889                 else
890                         printf("off\n");
891         }
892
893         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_KEEP_CRC) {
894                 printf("Rx Keep CRC:                   ");
895                 if (ports[port_id].dev_conf.rxmode.offloads &
896                     DEV_RX_OFFLOAD_KEEP_CRC)
897                         printf("on\n");
898                 else
899                         printf("off\n");
900         }
901
902         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY) {
903                 printf("RX offload security:           ");
904                 if (ports[port_id].dev_conf.rxmode.offloads &
905                     DEV_RX_OFFLOAD_SECURITY)
906                         printf("on\n");
907                 else
908                         printf("off\n");
909         }
910
911         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT) {
912                 printf("VLAN insert:                   ");
913                 if (ports[port_id].dev_conf.txmode.offloads &
914                     DEV_TX_OFFLOAD_VLAN_INSERT)
915                         printf("on\n");
916                 else
917                         printf("off\n");
918         }
919
920         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_QINQ_INSERT) {
921                 printf("Double VLANs insert:           ");
922                 if (ports[port_id].dev_conf.txmode.offloads &
923                     DEV_TX_OFFLOAD_QINQ_INSERT)
924                         printf("on\n");
925                 else
926                         printf("off\n");
927         }
928
929         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM) {
930                 printf("TX IPv4 checksum:              ");
931                 if (ports[port_id].dev_conf.txmode.offloads &
932                     DEV_TX_OFFLOAD_IPV4_CKSUM)
933                         printf("on\n");
934                 else
935                         printf("off\n");
936         }
937
938         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM) {
939                 printf("TX UDP checksum:               ");
940                 if (ports[port_id].dev_conf.txmode.offloads &
941                     DEV_TX_OFFLOAD_UDP_CKSUM)
942                         printf("on\n");
943                 else
944                         printf("off\n");
945         }
946
947         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM) {
948                 printf("TX TCP checksum:               ");
949                 if (ports[port_id].dev_conf.txmode.offloads &
950                     DEV_TX_OFFLOAD_TCP_CKSUM)
951                         printf("on\n");
952                 else
953                         printf("off\n");
954         }
955
956         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SCTP_CKSUM) {
957                 printf("TX SCTP checksum:              ");
958                 if (ports[port_id].dev_conf.txmode.offloads &
959                     DEV_TX_OFFLOAD_SCTP_CKSUM)
960                         printf("on\n");
961                 else
962                         printf("off\n");
963         }
964
965         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) {
966                 printf("TX Outer IPv4 checksum:        ");
967                 if (ports[port_id].dev_conf.txmode.offloads &
968                     DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)
969                         printf("on\n");
970                 else
971                         printf("off\n");
972         }
973
974         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) {
975                 printf("TX TCP segmentation:           ");
976                 if (ports[port_id].dev_conf.txmode.offloads &
977                     DEV_TX_OFFLOAD_TCP_TSO)
978                         printf("on\n");
979                 else
980                         printf("off\n");
981         }
982
983         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_TSO) {
984                 printf("TX UDP segmentation:           ");
985                 if (ports[port_id].dev_conf.txmode.offloads &
986                     DEV_TX_OFFLOAD_UDP_TSO)
987                         printf("on\n");
988                 else
989                         printf("off\n");
990         }
991
992         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VXLAN_TNL_TSO) {
993                 printf("TSO for VXLAN tunnel packet:   ");
994                 if (ports[port_id].dev_conf.txmode.offloads &
995                     DEV_TX_OFFLOAD_VXLAN_TNL_TSO)
996                         printf("on\n");
997                 else
998                         printf("off\n");
999         }
1000
1001         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_GRE_TNL_TSO) {
1002                 printf("TSO for GRE tunnel packet:     ");
1003                 if (ports[port_id].dev_conf.txmode.offloads &
1004                     DEV_TX_OFFLOAD_GRE_TNL_TSO)
1005                         printf("on\n");
1006                 else
1007                         printf("off\n");
1008         }
1009
1010         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPIP_TNL_TSO) {
1011                 printf("TSO for IPIP tunnel packet:    ");
1012                 if (ports[port_id].dev_conf.txmode.offloads &
1013                     DEV_TX_OFFLOAD_IPIP_TNL_TSO)
1014                         printf("on\n");
1015                 else
1016                         printf("off\n");
1017         }
1018
1019         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_GENEVE_TNL_TSO) {
1020                 printf("TSO for GENEVE tunnel packet:  ");
1021                 if (ports[port_id].dev_conf.txmode.offloads &
1022                     DEV_TX_OFFLOAD_GENEVE_TNL_TSO)
1023                         printf("on\n");
1024                 else
1025                         printf("off\n");
1026         }
1027
1028         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IP_TNL_TSO) {
1029                 printf("IP tunnel TSO:  ");
1030                 if (ports[port_id].dev_conf.txmode.offloads &
1031                     DEV_TX_OFFLOAD_IP_TNL_TSO)
1032                         printf("on\n");
1033                 else
1034                         printf("off\n");
1035         }
1036
1037         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_TNL_TSO) {
1038                 printf("UDP tunnel TSO:  ");
1039                 if (ports[port_id].dev_conf.txmode.offloads &
1040                     DEV_TX_OFFLOAD_UDP_TNL_TSO)
1041                         printf("on\n");
1042                 else
1043                         printf("off\n");
1044         }
1045
1046         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) {
1047                 printf("TX Outer UDP checksum:         ");
1048                 if (ports[port_id].dev_conf.txmode.offloads &
1049                     DEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
1050                         printf("on\n");
1051                 else
1052                         printf("off\n");
1053         }
1054
1055 }
1056
1057 int
1058 port_id_is_invalid(portid_t port_id, enum print_warning warning)
1059 {
1060         uint16_t pid;
1061
1062         if (port_id == (portid_t)RTE_PORT_ALL)
1063                 return 0;
1064
1065         RTE_ETH_FOREACH_DEV(pid)
1066                 if (port_id == pid)
1067                         return 0;
1068
1069         if (warning == ENABLED_WARN)
1070                 printf("Invalid port %d\n", port_id);
1071
1072         return 1;
1073 }
1074
1075 void print_valid_ports(void)
1076 {
1077         portid_t pid;
1078
1079         printf("The valid ports array is [");
1080         RTE_ETH_FOREACH_DEV(pid) {
1081                 printf(" %d", pid);
1082         }
1083         printf(" ]\n");
1084 }
1085
1086 static int
1087 vlan_id_is_invalid(uint16_t vlan_id)
1088 {
1089         if (vlan_id < 4096)
1090                 return 0;
1091         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
1092         return 1;
1093 }
1094
1095 static int
1096 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
1097 {
1098         const struct rte_pci_device *pci_dev;
1099         const struct rte_bus *bus;
1100         uint64_t pci_len;
1101
1102         if (reg_off & 0x3) {
1103                 printf("Port register offset 0x%X not aligned on a 4-byte "
1104                        "boundary\n",
1105                        (unsigned)reg_off);
1106                 return 1;
1107         }
1108
1109         if (!ports[port_id].dev_info.device) {
1110                 printf("Invalid device\n");
1111                 return 0;
1112         }
1113
1114         bus = rte_bus_find_by_device(ports[port_id].dev_info.device);
1115         if (bus && !strcmp(bus->name, "pci")) {
1116                 pci_dev = RTE_DEV_TO_PCI(ports[port_id].dev_info.device);
1117         } else {
1118                 printf("Not a PCI device\n");
1119                 return 1;
1120         }
1121
1122         pci_len = pci_dev->mem_resource[0].len;
1123         if (reg_off >= pci_len) {
1124                 printf("Port %d: register offset %u (0x%X) out of port PCI "
1125                        "resource (length=%"PRIu64")\n",
1126                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
1127                 return 1;
1128         }
1129         return 0;
1130 }
1131
1132 static int
1133 reg_bit_pos_is_invalid(uint8_t bit_pos)
1134 {
1135         if (bit_pos <= 31)
1136                 return 0;
1137         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
1138         return 1;
1139 }
1140
1141 #define display_port_and_reg_off(port_id, reg_off) \
1142         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
1143
1144 static inline void
1145 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
1146 {
1147         display_port_and_reg_off(port_id, (unsigned)reg_off);
1148         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
1149 }
1150
1151 void
1152 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
1153 {
1154         uint32_t reg_v;
1155
1156
1157         if (port_id_is_invalid(port_id, ENABLED_WARN))
1158                 return;
1159         if (port_reg_off_is_invalid(port_id, reg_off))
1160                 return;
1161         if (reg_bit_pos_is_invalid(bit_x))
1162                 return;
1163         reg_v = port_id_pci_reg_read(port_id, reg_off);
1164         display_port_and_reg_off(port_id, (unsigned)reg_off);
1165         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
1166 }
1167
1168 void
1169 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
1170                            uint8_t bit1_pos, uint8_t bit2_pos)
1171 {
1172         uint32_t reg_v;
1173         uint8_t  l_bit;
1174         uint8_t  h_bit;
1175
1176         if (port_id_is_invalid(port_id, ENABLED_WARN))
1177                 return;
1178         if (port_reg_off_is_invalid(port_id, reg_off))
1179                 return;
1180         if (reg_bit_pos_is_invalid(bit1_pos))
1181                 return;
1182         if (reg_bit_pos_is_invalid(bit2_pos))
1183                 return;
1184         if (bit1_pos > bit2_pos)
1185                 l_bit = bit2_pos, h_bit = bit1_pos;
1186         else
1187                 l_bit = bit1_pos, h_bit = bit2_pos;
1188
1189         reg_v = port_id_pci_reg_read(port_id, reg_off);
1190         reg_v >>= l_bit;
1191         if (h_bit < 31)
1192                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
1193         display_port_and_reg_off(port_id, (unsigned)reg_off);
1194         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
1195                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
1196 }
1197
1198 void
1199 port_reg_display(portid_t port_id, uint32_t reg_off)
1200 {
1201         uint32_t reg_v;
1202
1203         if (port_id_is_invalid(port_id, ENABLED_WARN))
1204                 return;
1205         if (port_reg_off_is_invalid(port_id, reg_off))
1206                 return;
1207         reg_v = port_id_pci_reg_read(port_id, reg_off);
1208         display_port_reg_value(port_id, reg_off, reg_v);
1209 }
1210
1211 void
1212 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
1213                  uint8_t bit_v)
1214 {
1215         uint32_t reg_v;
1216
1217         if (port_id_is_invalid(port_id, ENABLED_WARN))
1218                 return;
1219         if (port_reg_off_is_invalid(port_id, reg_off))
1220                 return;
1221         if (reg_bit_pos_is_invalid(bit_pos))
1222                 return;
1223         if (bit_v > 1) {
1224                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
1225                 return;
1226         }
1227         reg_v = port_id_pci_reg_read(port_id, reg_off);
1228         if (bit_v == 0)
1229                 reg_v &= ~(1 << bit_pos);
1230         else
1231                 reg_v |= (1 << bit_pos);
1232         port_id_pci_reg_write(port_id, reg_off, reg_v);
1233         display_port_reg_value(port_id, reg_off, reg_v);
1234 }
1235
1236 void
1237 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
1238                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
1239 {
1240         uint32_t max_v;
1241         uint32_t reg_v;
1242         uint8_t  l_bit;
1243         uint8_t  h_bit;
1244
1245         if (port_id_is_invalid(port_id, ENABLED_WARN))
1246                 return;
1247         if (port_reg_off_is_invalid(port_id, reg_off))
1248                 return;
1249         if (reg_bit_pos_is_invalid(bit1_pos))
1250                 return;
1251         if (reg_bit_pos_is_invalid(bit2_pos))
1252                 return;
1253         if (bit1_pos > bit2_pos)
1254                 l_bit = bit2_pos, h_bit = bit1_pos;
1255         else
1256                 l_bit = bit1_pos, h_bit = bit2_pos;
1257
1258         if ((h_bit - l_bit) < 31)
1259                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
1260         else
1261                 max_v = 0xFFFFFFFF;
1262
1263         if (value > max_v) {
1264                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
1265                                 (unsigned)value, (unsigned)value,
1266                                 (unsigned)max_v, (unsigned)max_v);
1267                 return;
1268         }
1269         reg_v = port_id_pci_reg_read(port_id, reg_off);
1270         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
1271         reg_v |= (value << l_bit); /* Set changed bits */
1272         port_id_pci_reg_write(port_id, reg_off, reg_v);
1273         display_port_reg_value(port_id, reg_off, reg_v);
1274 }
1275
1276 void
1277 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
1278 {
1279         if (port_id_is_invalid(port_id, ENABLED_WARN))
1280                 return;
1281         if (port_reg_off_is_invalid(port_id, reg_off))
1282                 return;
1283         port_id_pci_reg_write(port_id, reg_off, reg_v);
1284         display_port_reg_value(port_id, reg_off, reg_v);
1285 }
1286
1287 void
1288 port_mtu_set(portid_t port_id, uint16_t mtu)
1289 {
1290         int diag;
1291         struct rte_port *rte_port = &ports[port_id];
1292         struct rte_eth_dev_info dev_info;
1293         uint16_t eth_overhead;
1294         int ret;
1295
1296         if (port_id_is_invalid(port_id, ENABLED_WARN))
1297                 return;
1298
1299         ret = eth_dev_info_get_print_err(port_id, &dev_info);
1300         if (ret != 0)
1301                 return;
1302
1303         if (mtu > dev_info.max_mtu || mtu < dev_info.min_mtu) {
1304                 printf("Set MTU failed. MTU:%u is not in valid range, min:%u - max:%u\n",
1305                         mtu, dev_info.min_mtu, dev_info.max_mtu);
1306                 return;
1307         }
1308         diag = rte_eth_dev_set_mtu(port_id, mtu);
1309         if (diag)
1310                 printf("Set MTU failed. diag=%d\n", diag);
1311         else if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1312                 /*
1313                  * Ether overhead in driver is equal to the difference of
1314                  * max_rx_pktlen and max_mtu in rte_eth_dev_info when the
1315                  * device supports jumbo frame.
1316                  */
1317                 eth_overhead = dev_info.max_rx_pktlen - dev_info.max_mtu;
1318                 if (mtu > RTE_ETHER_MAX_LEN - eth_overhead) {
1319                         rte_port->dev_conf.rxmode.offloads |=
1320                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1321                         rte_port->dev_conf.rxmode.max_rx_pkt_len =
1322                                                 mtu + eth_overhead;
1323                 } else
1324                         rte_port->dev_conf.rxmode.offloads &=
1325                                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1326         }
1327 }
1328
1329 /* Generic flow management functions. */
1330
1331 /** Generate a port_flow entry from attributes/pattern/actions. */
1332 static struct port_flow *
1333 port_flow_new(const struct rte_flow_attr *attr,
1334               const struct rte_flow_item *pattern,
1335               const struct rte_flow_action *actions,
1336               struct rte_flow_error *error)
1337 {
1338         const struct rte_flow_conv_rule rule = {
1339                 .attr_ro = attr,
1340                 .pattern_ro = pattern,
1341                 .actions_ro = actions,
1342         };
1343         struct port_flow *pf;
1344         int ret;
1345
1346         ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, error);
1347         if (ret < 0)
1348                 return NULL;
1349         pf = calloc(1, offsetof(struct port_flow, rule) + ret);
1350         if (!pf) {
1351                 rte_flow_error_set
1352                         (error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1353                          "calloc() failed");
1354                 return NULL;
1355         }
1356         if (rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &pf->rule, ret, &rule,
1357                           error) >= 0)
1358                 return pf;
1359         free(pf);
1360         return NULL;
1361 }
1362
1363 /** Print a message out of a flow error. */
1364 static int
1365 port_flow_complain(struct rte_flow_error *error)
1366 {
1367         static const char *const errstrlist[] = {
1368                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
1369                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
1370                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
1371                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
1372                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
1373                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
1374                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
1375                 [RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER] = "transfer field",
1376                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
1377                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
1378                 [RTE_FLOW_ERROR_TYPE_ITEM_SPEC] = "item specification",
1379                 [RTE_FLOW_ERROR_TYPE_ITEM_LAST] = "item specification range",
1380                 [RTE_FLOW_ERROR_TYPE_ITEM_MASK] = "item specification mask",
1381                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
1382                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
1383                 [RTE_FLOW_ERROR_TYPE_ACTION_CONF] = "action configuration",
1384                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
1385         };
1386         const char *errstr;
1387         char buf[32];
1388         int err = rte_errno;
1389
1390         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
1391             !errstrlist[error->type])
1392                 errstr = "unknown type";
1393         else
1394                 errstr = errstrlist[error->type];
1395         printf("%s(): Caught PMD error type %d (%s): %s%s: %s\n", __func__,
1396                error->type, errstr,
1397                error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
1398                                         error->cause), buf) : "",
1399                error->message ? error->message : "(no stated reason)",
1400                rte_strerror(err));
1401         return -err;
1402 }
1403
1404 static void
1405 rss_config_display(struct rte_flow_action_rss *rss_conf)
1406 {
1407         uint8_t i;
1408
1409         if (rss_conf == NULL) {
1410                 printf("Invalid rule\n");
1411                 return;
1412         }
1413
1414         printf("RSS:\n"
1415                " queues: ");
1416         if (rss_conf->queue_num == 0)
1417                 printf("none\n");
1418         for (i = 0; i < rss_conf->queue_num; i++)
1419                 printf("%d\n", rss_conf->queue[i]);
1420
1421         printf(" function: ");
1422         switch (rss_conf->func) {
1423         case RTE_ETH_HASH_FUNCTION_DEFAULT:
1424                 printf("default\n");
1425                 break;
1426         case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1427                 printf("toeplitz\n");
1428                 break;
1429         case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
1430                 printf("simple_xor\n");
1431                 break;
1432         case RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ:
1433                 printf("symmetric_toeplitz\n");
1434                 break;
1435         default:
1436                 printf("Unknown function\n");
1437                 return;
1438         }
1439
1440         printf(" types:\n");
1441         if (rss_conf->types == 0) {
1442                 printf("  none\n");
1443                 return;
1444         }
1445         for (i = 0; rss_type_table[i].str; i++) {
1446                 if ((rss_conf->types &
1447                     rss_type_table[i].rss_type) ==
1448                     rss_type_table[i].rss_type &&
1449                     rss_type_table[i].rss_type != 0)
1450                         printf("  %s\n", rss_type_table[i].str);
1451         }
1452 }
1453
1454 /** Validate flow rule. */
1455 int
1456 port_flow_validate(portid_t port_id,
1457                    const struct rte_flow_attr *attr,
1458                    const struct rte_flow_item *pattern,
1459                    const struct rte_flow_action *actions)
1460 {
1461         struct rte_flow_error error;
1462
1463         /* Poisoning to make sure PMDs update it in case of error. */
1464         memset(&error, 0x11, sizeof(error));
1465         if (rte_flow_validate(port_id, attr, pattern, actions, &error))
1466                 return port_flow_complain(&error);
1467         printf("Flow rule validated\n");
1468         return 0;
1469 }
1470
1471 /** Update age action context by port_flow pointer. */
1472 void
1473 update_age_action_context(const struct rte_flow_action *actions,
1474                         struct port_flow *pf)
1475 {
1476         struct rte_flow_action_age *age = NULL;
1477
1478         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1479                 switch (actions->type) {
1480                 case RTE_FLOW_ACTION_TYPE_AGE:
1481                         age = (struct rte_flow_action_age *)
1482                                 (uintptr_t)actions->conf;
1483                         age->context = pf;
1484                         return;
1485                 default:
1486                         break;
1487                 }
1488         }
1489 }
1490
1491 /** Create flow rule. */
1492 int
1493 port_flow_create(portid_t port_id,
1494                  const struct rte_flow_attr *attr,
1495                  const struct rte_flow_item *pattern,
1496                  const struct rte_flow_action *actions)
1497 {
1498         struct rte_flow *flow;
1499         struct rte_port *port;
1500         struct port_flow *pf;
1501         uint32_t id = 0;
1502         struct rte_flow_error error;
1503
1504         port = &ports[port_id];
1505         if (port->flow_list) {
1506                 if (port->flow_list->id == UINT32_MAX) {
1507                         printf("Highest rule ID is already assigned, delete"
1508                                " it first");
1509                         return -ENOMEM;
1510                 }
1511                 id = port->flow_list->id + 1;
1512         }
1513         pf = port_flow_new(attr, pattern, actions, &error);
1514         if (!pf)
1515                 return port_flow_complain(&error);
1516         update_age_action_context(actions, pf);
1517         /* Poisoning to make sure PMDs update it in case of error. */
1518         memset(&error, 0x22, sizeof(error));
1519         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
1520         if (!flow) {
1521                 free(pf);
1522                 return port_flow_complain(&error);
1523         }
1524         pf->next = port->flow_list;
1525         pf->id = id;
1526         pf->flow = flow;
1527         port->flow_list = pf;
1528         printf("Flow rule #%u created\n", pf->id);
1529         return 0;
1530 }
1531
1532 /** Destroy a number of flow rules. */
1533 int
1534 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
1535 {
1536         struct rte_port *port;
1537         struct port_flow **tmp;
1538         uint32_t c = 0;
1539         int ret = 0;
1540
1541         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1542             port_id == (portid_t)RTE_PORT_ALL)
1543                 return -EINVAL;
1544         port = &ports[port_id];
1545         tmp = &port->flow_list;
1546         while (*tmp) {
1547                 uint32_t i;
1548
1549                 for (i = 0; i != n; ++i) {
1550                         struct rte_flow_error error;
1551                         struct port_flow *pf = *tmp;
1552
1553                         if (rule[i] != pf->id)
1554                                 continue;
1555                         /*
1556                          * Poisoning to make sure PMDs update it in case
1557                          * of error.
1558                          */
1559                         memset(&error, 0x33, sizeof(error));
1560                         if (rte_flow_destroy(port_id, pf->flow, &error)) {
1561                                 ret = port_flow_complain(&error);
1562                                 continue;
1563                         }
1564                         printf("Flow rule #%u destroyed\n", pf->id);
1565                         *tmp = pf->next;
1566                         free(pf);
1567                         break;
1568                 }
1569                 if (i == n)
1570                         tmp = &(*tmp)->next;
1571                 ++c;
1572         }
1573         return ret;
1574 }
1575
1576 /** Remove all flow rules. */
1577 int
1578 port_flow_flush(portid_t port_id)
1579 {
1580         struct rte_flow_error error;
1581         struct rte_port *port;
1582         int ret = 0;
1583
1584         /* Poisoning to make sure PMDs update it in case of error. */
1585         memset(&error, 0x44, sizeof(error));
1586         if (rte_flow_flush(port_id, &error)) {
1587                 ret = port_flow_complain(&error);
1588                 if (port_id_is_invalid(port_id, DISABLED_WARN) ||
1589                     port_id == (portid_t)RTE_PORT_ALL)
1590                         return ret;
1591         }
1592         port = &ports[port_id];
1593         while (port->flow_list) {
1594                 struct port_flow *pf = port->flow_list->next;
1595
1596                 free(port->flow_list);
1597                 port->flow_list = pf;
1598         }
1599         return ret;
1600 }
1601
1602 /** Dump all flow rules. */
1603 int
1604 port_flow_dump(portid_t port_id, const char *file_name)
1605 {
1606         int ret = 0;
1607         FILE *file = stdout;
1608         struct rte_flow_error error;
1609
1610         if (file_name && strlen(file_name)) {
1611                 file = fopen(file_name, "w");
1612                 if (!file) {
1613                         printf("Failed to create file %s: %s\n", file_name,
1614                                strerror(errno));
1615                         return -errno;
1616                 }
1617         }
1618         ret = rte_flow_dev_dump(port_id, file, &error);
1619         if (ret) {
1620                 port_flow_complain(&error);
1621                 printf("Failed to dump flow: %s\n", strerror(-ret));
1622         } else
1623                 printf("Flow dump finished\n");
1624         if (file_name && strlen(file_name))
1625                 fclose(file);
1626         return ret;
1627 }
1628
1629 /** Query a flow rule. */
1630 int
1631 port_flow_query(portid_t port_id, uint32_t rule,
1632                 const struct rte_flow_action *action)
1633 {
1634         struct rte_flow_error error;
1635         struct rte_port *port;
1636         struct port_flow *pf;
1637         const char *name;
1638         union {
1639                 struct rte_flow_query_count count;
1640                 struct rte_flow_action_rss rss_conf;
1641         } query;
1642         int ret;
1643
1644         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1645             port_id == (portid_t)RTE_PORT_ALL)
1646                 return -EINVAL;
1647         port = &ports[port_id];
1648         for (pf = port->flow_list; pf; pf = pf->next)
1649                 if (pf->id == rule)
1650                         break;
1651         if (!pf) {
1652                 printf("Flow rule #%u not found\n", rule);
1653                 return -ENOENT;
1654         }
1655         ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
1656                             &name, sizeof(name),
1657                             (void *)(uintptr_t)action->type, &error);
1658         if (ret < 0)
1659                 return port_flow_complain(&error);
1660         switch (action->type) {
1661         case RTE_FLOW_ACTION_TYPE_COUNT:
1662         case RTE_FLOW_ACTION_TYPE_RSS:
1663                 break;
1664         default:
1665                 printf("Cannot query action type %d (%s)\n",
1666                         action->type, name);
1667                 return -ENOTSUP;
1668         }
1669         /* Poisoning to make sure PMDs update it in case of error. */
1670         memset(&error, 0x55, sizeof(error));
1671         memset(&query, 0, sizeof(query));
1672         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
1673                 return port_flow_complain(&error);
1674         switch (action->type) {
1675         case RTE_FLOW_ACTION_TYPE_COUNT:
1676                 printf("%s:\n"
1677                        " hits_set: %u\n"
1678                        " bytes_set: %u\n"
1679                        " hits: %" PRIu64 "\n"
1680                        " bytes: %" PRIu64 "\n",
1681                        name,
1682                        query.count.hits_set,
1683                        query.count.bytes_set,
1684                        query.count.hits,
1685                        query.count.bytes);
1686                 break;
1687         case RTE_FLOW_ACTION_TYPE_RSS:
1688                 rss_config_display(&query.rss_conf);
1689                 break;
1690         default:
1691                 printf("Cannot display result for action type %d (%s)\n",
1692                        action->type, name);
1693                 break;
1694         }
1695         return 0;
1696 }
1697
1698 /** List simply and destroy all aged flows. */
1699 void
1700 port_flow_aged(portid_t port_id, uint8_t destroy)
1701 {
1702         void **contexts;
1703         int nb_context, total = 0, idx;
1704         struct rte_flow_error error;
1705         struct port_flow *pf;
1706
1707         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1708             port_id == (portid_t)RTE_PORT_ALL)
1709                 return;
1710         total = rte_flow_get_aged_flows(port_id, NULL, 0, &error);
1711         printf("Port %u total aged flows: %d\n", port_id, total);
1712         if (total < 0) {
1713                 port_flow_complain(&error);
1714                 return;
1715         }
1716         if (total == 0)
1717                 return;
1718         contexts = malloc(sizeof(void *) * total);
1719         if (contexts == NULL) {
1720                 printf("Cannot allocate contexts for aged flow\n");
1721                 return;
1722         }
1723         printf("ID\tGroup\tPrio\tAttr\n");
1724         nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error);
1725         if (nb_context != total) {
1726                 printf("Port:%d get aged flows count(%d) != total(%d)\n",
1727                         port_id, nb_context, total);
1728                 free(contexts);
1729                 return;
1730         }
1731         for (idx = 0; idx < nb_context; idx++) {
1732                 pf = (struct port_flow *)contexts[idx];
1733                 if (!pf) {
1734                         printf("Error: get Null context in port %u\n", port_id);
1735                         continue;
1736                 }
1737                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t\n",
1738                        pf->id,
1739                        pf->rule.attr->group,
1740                        pf->rule.attr->priority,
1741                        pf->rule.attr->ingress ? 'i' : '-',
1742                        pf->rule.attr->egress ? 'e' : '-',
1743                        pf->rule.attr->transfer ? 't' : '-');
1744         }
1745         if (destroy) {
1746                 int ret;
1747                 uint32_t flow_id;
1748
1749                 total = 0;
1750                 printf("\n");
1751                 for (idx = 0; idx < nb_context; idx++) {
1752                         pf = (struct port_flow *)contexts[idx];
1753                         if (!pf)
1754                                 continue;
1755                         flow_id = pf->id;
1756                         ret = port_flow_destroy(port_id, 1, &flow_id);
1757                         if (!ret)
1758                                 total++;
1759                 }
1760                 printf("%d flows be destroyed\n", total);
1761         }
1762         free(contexts);
1763 }
1764
1765 /** List flow rules. */
1766 void
1767 port_flow_list(portid_t port_id, uint32_t n, const uint32_t group[n])
1768 {
1769         struct rte_port *port;
1770         struct port_flow *pf;
1771         struct port_flow *list = NULL;
1772         uint32_t i;
1773
1774         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1775             port_id == (portid_t)RTE_PORT_ALL)
1776                 return;
1777         port = &ports[port_id];
1778         if (!port->flow_list)
1779                 return;
1780         /* Sort flows by group, priority and ID. */
1781         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
1782                 struct port_flow **tmp;
1783                 const struct rte_flow_attr *curr = pf->rule.attr;
1784
1785                 if (n) {
1786                         /* Filter out unwanted groups. */
1787                         for (i = 0; i != n; ++i)
1788                                 if (curr->group == group[i])
1789                                         break;
1790                         if (i == n)
1791                                 continue;
1792                 }
1793                 for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) {
1794                         const struct rte_flow_attr *comp = (*tmp)->rule.attr;
1795
1796                         if (curr->group > comp->group ||
1797                             (curr->group == comp->group &&
1798                              curr->priority > comp->priority) ||
1799                             (curr->group == comp->group &&
1800                              curr->priority == comp->priority &&
1801                              pf->id > (*tmp)->id))
1802                                 continue;
1803                         break;
1804                 }
1805                 pf->tmp = *tmp;
1806                 *tmp = pf;
1807         }
1808         printf("ID\tGroup\tPrio\tAttr\tRule\n");
1809         for (pf = list; pf != NULL; pf = pf->tmp) {
1810                 const struct rte_flow_item *item = pf->rule.pattern;
1811                 const struct rte_flow_action *action = pf->rule.actions;
1812                 const char *name;
1813
1814                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t",
1815                        pf->id,
1816                        pf->rule.attr->group,
1817                        pf->rule.attr->priority,
1818                        pf->rule.attr->ingress ? 'i' : '-',
1819                        pf->rule.attr->egress ? 'e' : '-',
1820                        pf->rule.attr->transfer ? 't' : '-');
1821                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
1822                         if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
1823                                           &name, sizeof(name),
1824                                           (void *)(uintptr_t)item->type,
1825                                           NULL) <= 0)
1826                                 name = "[UNKNOWN]";
1827                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1828                                 printf("%s ", name);
1829                         ++item;
1830                 }
1831                 printf("=>");
1832                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
1833                         if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
1834                                           &name, sizeof(name),
1835                                           (void *)(uintptr_t)action->type,
1836                                           NULL) <= 0)
1837                                 name = "[UNKNOWN]";
1838                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
1839                                 printf(" %s", name);
1840                         ++action;
1841                 }
1842                 printf("\n");
1843         }
1844 }
1845
1846 /** Restrict ingress traffic to the defined flow rules. */
1847 int
1848 port_flow_isolate(portid_t port_id, int set)
1849 {
1850         struct rte_flow_error error;
1851
1852         /* Poisoning to make sure PMDs update it in case of error. */
1853         memset(&error, 0x66, sizeof(error));
1854         if (rte_flow_isolate(port_id, set, &error))
1855                 return port_flow_complain(&error);
1856         printf("Ingress traffic on port %u is %s to the defined flow rules\n",
1857                port_id,
1858                set ? "now restricted" : "not restricted anymore");
1859         return 0;
1860 }
1861
1862 /*
1863  * RX/TX ring descriptors display functions.
1864  */
1865 int
1866 rx_queue_id_is_invalid(queueid_t rxq_id)
1867 {
1868         if (rxq_id < nb_rxq)
1869                 return 0;
1870         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
1871         return 1;
1872 }
1873
1874 int
1875 tx_queue_id_is_invalid(queueid_t txq_id)
1876 {
1877         if (txq_id < nb_txq)
1878                 return 0;
1879         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
1880         return 1;
1881 }
1882
1883 static int
1884 rx_desc_id_is_invalid(uint16_t rxdesc_id)
1885 {
1886         if (rxdesc_id < nb_rxd)
1887                 return 0;
1888         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
1889                rxdesc_id, nb_rxd);
1890         return 1;
1891 }
1892
1893 static int
1894 tx_desc_id_is_invalid(uint16_t txdesc_id)
1895 {
1896         if (txdesc_id < nb_txd)
1897                 return 0;
1898         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
1899                txdesc_id, nb_txd);
1900         return 1;
1901 }
1902
1903 static const struct rte_memzone *
1904 ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id)
1905 {
1906         char mz_name[RTE_MEMZONE_NAMESIZE];
1907         const struct rte_memzone *mz;
1908
1909         snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s",
1910                         port_id, q_id, ring_name);
1911         mz = rte_memzone_lookup(mz_name);
1912         if (mz == NULL)
1913                 printf("%s ring memory zoneof (port %d, queue %d) not"
1914                        "found (zone name = %s\n",
1915                        ring_name, port_id, q_id, mz_name);
1916         return mz;
1917 }
1918
1919 union igb_ring_dword {
1920         uint64_t dword;
1921         struct {
1922 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1923                 uint32_t lo;
1924                 uint32_t hi;
1925 #else
1926                 uint32_t hi;
1927                 uint32_t lo;
1928 #endif
1929         } words;
1930 };
1931
1932 struct igb_ring_desc_32_bytes {
1933         union igb_ring_dword lo_dword;
1934         union igb_ring_dword hi_dword;
1935         union igb_ring_dword resv1;
1936         union igb_ring_dword resv2;
1937 };
1938
1939 struct igb_ring_desc_16_bytes {
1940         union igb_ring_dword lo_dword;
1941         union igb_ring_dword hi_dword;
1942 };
1943
1944 static void
1945 ring_rxd_display_dword(union igb_ring_dword dword)
1946 {
1947         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
1948                                         (unsigned)dword.words.hi);
1949 }
1950
1951 static void
1952 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
1953 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1954                            portid_t port_id,
1955 #else
1956                            __rte_unused portid_t port_id,
1957 #endif
1958                            uint16_t desc_id)
1959 {
1960         struct igb_ring_desc_16_bytes *ring =
1961                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1962 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1963         int ret;
1964         struct rte_eth_dev_info dev_info;
1965
1966         ret = eth_dev_info_get_print_err(port_id, &dev_info);
1967         if (ret != 0)
1968                 return;
1969
1970         if (strstr(dev_info.driver_name, "i40e") != NULL) {
1971                 /* 32 bytes RX descriptor, i40e only */
1972                 struct igb_ring_desc_32_bytes *ring =
1973                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
1974                 ring[desc_id].lo_dword.dword =
1975                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1976                 ring_rxd_display_dword(ring[desc_id].lo_dword);
1977                 ring[desc_id].hi_dword.dword =
1978                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1979                 ring_rxd_display_dword(ring[desc_id].hi_dword);
1980                 ring[desc_id].resv1.dword =
1981                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
1982                 ring_rxd_display_dword(ring[desc_id].resv1);
1983                 ring[desc_id].resv2.dword =
1984                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
1985                 ring_rxd_display_dword(ring[desc_id].resv2);
1986
1987                 return;
1988         }
1989 #endif
1990         /* 16 bytes RX descriptor */
1991         ring[desc_id].lo_dword.dword =
1992                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1993         ring_rxd_display_dword(ring[desc_id].lo_dword);
1994         ring[desc_id].hi_dword.dword =
1995                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1996         ring_rxd_display_dword(ring[desc_id].hi_dword);
1997 }
1998
1999 static void
2000 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
2001 {
2002         struct igb_ring_desc_16_bytes *ring;
2003         struct igb_ring_desc_16_bytes txd;
2004
2005         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
2006         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
2007         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
2008         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
2009                         (unsigned)txd.lo_dword.words.lo,
2010                         (unsigned)txd.lo_dword.words.hi,
2011                         (unsigned)txd.hi_dword.words.lo,
2012                         (unsigned)txd.hi_dword.words.hi);
2013 }
2014
2015 void
2016 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
2017 {
2018         const struct rte_memzone *rx_mz;
2019
2020         if (port_id_is_invalid(port_id, ENABLED_WARN))
2021                 return;
2022         if (rx_queue_id_is_invalid(rxq_id))
2023                 return;
2024         if (rx_desc_id_is_invalid(rxd_id))
2025                 return;
2026         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
2027         if (rx_mz == NULL)
2028                 return;
2029         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
2030 }
2031
2032 void
2033 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
2034 {
2035         const struct rte_memzone *tx_mz;
2036
2037         if (port_id_is_invalid(port_id, ENABLED_WARN))
2038                 return;
2039         if (tx_queue_id_is_invalid(txq_id))
2040                 return;
2041         if (tx_desc_id_is_invalid(txd_id))
2042                 return;
2043         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
2044         if (tx_mz == NULL)
2045                 return;
2046         ring_tx_descriptor_display(tx_mz, txd_id);
2047 }
2048
2049 void
2050 fwd_lcores_config_display(void)
2051 {
2052         lcoreid_t lc_id;
2053
2054         printf("List of forwarding lcores:");
2055         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
2056                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
2057         printf("\n");
2058 }
2059 void
2060 rxtx_config_display(void)
2061 {
2062         portid_t pid;
2063         queueid_t qid;
2064
2065         printf("  %s packet forwarding%s packets/burst=%d\n",
2066                cur_fwd_eng->fwd_mode_name,
2067                retry_enabled == 0 ? "" : " with retry",
2068                nb_pkt_per_burst);
2069
2070         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
2071                 printf("  packet len=%u - nb packet segments=%d\n",
2072                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
2073
2074         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
2075                nb_fwd_lcores, nb_fwd_ports);
2076
2077         RTE_ETH_FOREACH_DEV(pid) {
2078                 struct rte_eth_rxconf *rx_conf = &ports[pid].rx_conf[0];
2079                 struct rte_eth_txconf *tx_conf = &ports[pid].tx_conf[0];
2080                 uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0];
2081                 uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0];
2082                 uint16_t nb_rx_desc_tmp;
2083                 uint16_t nb_tx_desc_tmp;
2084                 struct rte_eth_rxq_info rx_qinfo;
2085                 struct rte_eth_txq_info tx_qinfo;
2086                 int32_t rc;
2087
2088                 /* per port config */
2089                 printf("  port %d: RX queue number: %d Tx queue number: %d\n",
2090                                 (unsigned int)pid, nb_rxq, nb_txq);
2091
2092                 printf("    Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n",
2093                                 ports[pid].dev_conf.rxmode.offloads,
2094                                 ports[pid].dev_conf.txmode.offloads);
2095
2096                 /* per rx queue config only for first queue to be less verbose */
2097                 for (qid = 0; qid < 1; qid++) {
2098                         rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo);
2099                         if (rc)
2100                                 nb_rx_desc_tmp = nb_rx_desc[qid];
2101                         else
2102                                 nb_rx_desc_tmp = rx_qinfo.nb_desc;
2103
2104                         printf("    RX queue: %d\n", qid);
2105                         printf("      RX desc=%d - RX free threshold=%d\n",
2106                                 nb_rx_desc_tmp, rx_conf[qid].rx_free_thresh);
2107                         printf("      RX threshold registers: pthresh=%d hthresh=%d "
2108                                 " wthresh=%d\n",
2109                                 rx_conf[qid].rx_thresh.pthresh,
2110                                 rx_conf[qid].rx_thresh.hthresh,
2111                                 rx_conf[qid].rx_thresh.wthresh);
2112                         printf("      RX Offloads=0x%"PRIx64"\n",
2113                                 rx_conf[qid].offloads);
2114                 }
2115
2116                 /* per tx queue config only for first queue to be less verbose */
2117                 for (qid = 0; qid < 1; qid++) {
2118                         rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo);
2119                         if (rc)
2120                                 nb_tx_desc_tmp = nb_tx_desc[qid];
2121                         else
2122                                 nb_tx_desc_tmp = tx_qinfo.nb_desc;
2123
2124                         printf("    TX queue: %d\n", qid);
2125                         printf("      TX desc=%d - TX free threshold=%d\n",
2126                                 nb_tx_desc_tmp, tx_conf[qid].tx_free_thresh);
2127                         printf("      TX threshold registers: pthresh=%d hthresh=%d "
2128                                 " wthresh=%d\n",
2129                                 tx_conf[qid].tx_thresh.pthresh,
2130                                 tx_conf[qid].tx_thresh.hthresh,
2131                                 tx_conf[qid].tx_thresh.wthresh);
2132                         printf("      TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n",
2133                                 tx_conf[qid].offloads, tx_conf->tx_rs_thresh);
2134                 }
2135         }
2136 }
2137
2138 void
2139 port_rss_reta_info(portid_t port_id,
2140                    struct rte_eth_rss_reta_entry64 *reta_conf,
2141                    uint16_t nb_entries)
2142 {
2143         uint16_t i, idx, shift;
2144         int ret;
2145
2146         if (port_id_is_invalid(port_id, ENABLED_WARN))
2147                 return;
2148
2149         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
2150         if (ret != 0) {
2151                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
2152                 return;
2153         }
2154
2155         for (i = 0; i < nb_entries; i++) {
2156                 idx = i / RTE_RETA_GROUP_SIZE;
2157                 shift = i % RTE_RETA_GROUP_SIZE;
2158                 if (!(reta_conf[idx].mask & (1ULL << shift)))
2159                         continue;
2160                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
2161                                         i, reta_conf[idx].reta[shift]);
2162         }
2163 }
2164
2165 /*
2166  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
2167  * key of the port.
2168  */
2169 void
2170 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
2171 {
2172         struct rte_eth_rss_conf rss_conf = {0};
2173         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
2174         uint64_t rss_hf;
2175         uint8_t i;
2176         int diag;
2177         struct rte_eth_dev_info dev_info;
2178         uint8_t hash_key_size;
2179         int ret;
2180
2181         if (port_id_is_invalid(port_id, ENABLED_WARN))
2182                 return;
2183
2184         ret = eth_dev_info_get_print_err(port_id, &dev_info);
2185         if (ret != 0)
2186                 return;
2187
2188         if (dev_info.hash_key_size > 0 &&
2189                         dev_info.hash_key_size <= sizeof(rss_key))
2190                 hash_key_size = dev_info.hash_key_size;
2191         else {
2192                 printf("dev_info did not provide a valid hash key size\n");
2193                 return;
2194         }
2195
2196         /* Get RSS hash key if asked to display it */
2197         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
2198         rss_conf.rss_key_len = hash_key_size;
2199         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2200         if (diag != 0) {
2201                 switch (diag) {
2202                 case -ENODEV:
2203                         printf("port index %d invalid\n", port_id);
2204                         break;
2205                 case -ENOTSUP:
2206                         printf("operation not supported by device\n");
2207                         break;
2208                 default:
2209                         printf("operation failed - diag=%d\n", diag);
2210                         break;
2211                 }
2212                 return;
2213         }
2214         rss_hf = rss_conf.rss_hf;
2215         if (rss_hf == 0) {
2216                 printf("RSS disabled\n");
2217                 return;
2218         }
2219         printf("RSS functions:\n ");
2220         for (i = 0; rss_type_table[i].str; i++) {
2221                 if (rss_hf & rss_type_table[i].rss_type)
2222                         printf("%s ", rss_type_table[i].str);
2223         }
2224         printf("\n");
2225         if (!show_rss_key)
2226                 return;
2227         printf("RSS key:\n");
2228         for (i = 0; i < hash_key_size; i++)
2229                 printf("%02X", rss_key[i]);
2230         printf("\n");
2231 }
2232
2233 void
2234 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
2235                          uint hash_key_len)
2236 {
2237         struct rte_eth_rss_conf rss_conf;
2238         int diag;
2239         unsigned int i;
2240
2241         rss_conf.rss_key = NULL;
2242         rss_conf.rss_key_len = hash_key_len;
2243         rss_conf.rss_hf = 0;
2244         for (i = 0; rss_type_table[i].str; i++) {
2245                 if (!strcmp(rss_type_table[i].str, rss_type))
2246                         rss_conf.rss_hf = rss_type_table[i].rss_type;
2247         }
2248         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2249         if (diag == 0) {
2250                 rss_conf.rss_key = hash_key;
2251                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
2252         }
2253         if (diag == 0)
2254                 return;
2255
2256         switch (diag) {
2257         case -ENODEV:
2258                 printf("port index %d invalid\n", port_id);
2259                 break;
2260         case -ENOTSUP:
2261                 printf("operation not supported by device\n");
2262                 break;
2263         default:
2264                 printf("operation failed - diag=%d\n", diag);
2265                 break;
2266         }
2267 }
2268
2269 /*
2270  * Setup forwarding configuration for each logical core.
2271  */
2272 static void
2273 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
2274 {
2275         streamid_t nb_fs_per_lcore;
2276         streamid_t nb_fs;
2277         streamid_t sm_id;
2278         lcoreid_t  nb_extra;
2279         lcoreid_t  nb_fc;
2280         lcoreid_t  nb_lc;
2281         lcoreid_t  lc_id;
2282
2283         nb_fs = cfg->nb_fwd_streams;
2284         nb_fc = cfg->nb_fwd_lcores;
2285         if (nb_fs <= nb_fc) {
2286                 nb_fs_per_lcore = 1;
2287                 nb_extra = 0;
2288         } else {
2289                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
2290                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
2291         }
2292
2293         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
2294         sm_id = 0;
2295         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
2296                 fwd_lcores[lc_id]->stream_idx = sm_id;
2297                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
2298                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
2299         }
2300
2301         /*
2302          * Assign extra remaining streams, if any.
2303          */
2304         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
2305         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
2306                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
2307                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
2308                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
2309         }
2310 }
2311
2312 static portid_t
2313 fwd_topology_tx_port_get(portid_t rxp)
2314 {
2315         static int warning_once = 1;
2316
2317         RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports);
2318
2319         switch (port_topology) {
2320         default:
2321         case PORT_TOPOLOGY_PAIRED:
2322                 if ((rxp & 0x1) == 0) {
2323                         if (rxp + 1 < cur_fwd_config.nb_fwd_ports)
2324                                 return rxp + 1;
2325                         if (warning_once) {
2326                                 printf("\nWarning! port-topology=paired"
2327                                        " and odd forward ports number,"
2328                                        " the last port will pair with"
2329                                        " itself.\n\n");
2330                                 warning_once = 0;
2331                         }
2332                         return rxp;
2333                 }
2334                 return rxp - 1;
2335         case PORT_TOPOLOGY_CHAINED:
2336                 return (rxp + 1) % cur_fwd_config.nb_fwd_ports;
2337         case PORT_TOPOLOGY_LOOP:
2338                 return rxp;
2339         }
2340 }
2341
2342 static void
2343 simple_fwd_config_setup(void)
2344 {
2345         portid_t i;
2346
2347         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
2348         cur_fwd_config.nb_fwd_streams =
2349                 (streamid_t) cur_fwd_config.nb_fwd_ports;
2350
2351         /* reinitialize forwarding streams */
2352         init_fwd_streams();
2353
2354         /*
2355          * In the simple forwarding test, the number of forwarding cores
2356          * must be lower or equal to the number of forwarding ports.
2357          */
2358         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2359         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
2360                 cur_fwd_config.nb_fwd_lcores =
2361                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
2362         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2363
2364         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2365                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
2366                 fwd_streams[i]->rx_queue  = 0;
2367                 fwd_streams[i]->tx_port   =
2368                                 fwd_ports_ids[fwd_topology_tx_port_get(i)];
2369                 fwd_streams[i]->tx_queue  = 0;
2370                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
2371                 fwd_streams[i]->retry_enabled = retry_enabled;
2372         }
2373 }
2374
2375 /**
2376  * For the RSS forwarding test all streams distributed over lcores. Each stream
2377  * being composed of a RX queue to poll on a RX port for input messages,
2378  * associated with a TX queue of a TX port where to send forwarded packets.
2379  */
2380 static void
2381 rss_fwd_config_setup(void)
2382 {
2383         portid_t   rxp;
2384         portid_t   txp;
2385         queueid_t  rxq;
2386         queueid_t  nb_q;
2387         streamid_t  sm_id;
2388
2389         nb_q = nb_rxq;
2390         if (nb_q > nb_txq)
2391                 nb_q = nb_txq;
2392         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2393         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2394         cur_fwd_config.nb_fwd_streams =
2395                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
2396
2397         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2398                 cur_fwd_config.nb_fwd_lcores =
2399                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2400
2401         /* reinitialize forwarding streams */
2402         init_fwd_streams();
2403
2404         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2405         rxp = 0; rxq = 0;
2406         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2407                 struct fwd_stream *fs;
2408
2409                 fs = fwd_streams[sm_id];
2410                 txp = fwd_topology_tx_port_get(rxp);
2411                 fs->rx_port = fwd_ports_ids[rxp];
2412                 fs->rx_queue = rxq;
2413                 fs->tx_port = fwd_ports_ids[txp];
2414                 fs->tx_queue = rxq;
2415                 fs->peer_addr = fs->tx_port;
2416                 fs->retry_enabled = retry_enabled;
2417                 rxp++;
2418                 if (rxp < nb_fwd_ports)
2419                         continue;
2420                 rxp = 0;
2421                 rxq++;
2422         }
2423 }
2424
2425 /**
2426  * For the DCB forwarding test, each core is assigned on each traffic class.
2427  *
2428  * Each core is assigned a multi-stream, each stream being composed of
2429  * a RX queue to poll on a RX port for input messages, associated with
2430  * a TX queue of a TX port where to send forwarded packets. All RX and
2431  * TX queues are mapping to the same traffic class.
2432  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
2433  * the same core
2434  */
2435 static void
2436 dcb_fwd_config_setup(void)
2437 {
2438         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
2439         portid_t txp, rxp = 0;
2440         queueid_t txq, rxq = 0;
2441         lcoreid_t  lc_id;
2442         uint16_t nb_rx_queue, nb_tx_queue;
2443         uint16_t i, j, k, sm_id = 0;
2444         uint8_t tc = 0;
2445
2446         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2447         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2448         cur_fwd_config.nb_fwd_streams =
2449                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2450
2451         /* reinitialize forwarding streams */
2452         init_fwd_streams();
2453         sm_id = 0;
2454         txp = 1;
2455         /* get the dcb info on the first RX and TX ports */
2456         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2457         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2458
2459         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2460                 fwd_lcores[lc_id]->stream_nb = 0;
2461                 fwd_lcores[lc_id]->stream_idx = sm_id;
2462                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
2463                         /* if the nb_queue is zero, means this tc is
2464                          * not enabled on the POOL
2465                          */
2466                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
2467                                 break;
2468                         k = fwd_lcores[lc_id]->stream_nb +
2469                                 fwd_lcores[lc_id]->stream_idx;
2470                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
2471                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
2472                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2473                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
2474                         for (j = 0; j < nb_rx_queue; j++) {
2475                                 struct fwd_stream *fs;
2476
2477                                 fs = fwd_streams[k + j];
2478                                 fs->rx_port = fwd_ports_ids[rxp];
2479                                 fs->rx_queue = rxq + j;
2480                                 fs->tx_port = fwd_ports_ids[txp];
2481                                 fs->tx_queue = txq + j % nb_tx_queue;
2482                                 fs->peer_addr = fs->tx_port;
2483                                 fs->retry_enabled = retry_enabled;
2484                         }
2485                         fwd_lcores[lc_id]->stream_nb +=
2486                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2487                 }
2488                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
2489
2490                 tc++;
2491                 if (tc < rxp_dcb_info.nb_tcs)
2492                         continue;
2493                 /* Restart from TC 0 on next RX port */
2494                 tc = 0;
2495                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
2496                         rxp = (portid_t)
2497                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
2498                 else
2499                         rxp++;
2500                 if (rxp >= nb_fwd_ports)
2501                         return;
2502                 /* get the dcb information on next RX and TX ports */
2503                 if ((rxp & 0x1) == 0)
2504                         txp = (portid_t) (rxp + 1);
2505                 else
2506                         txp = (portid_t) (rxp - 1);
2507                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2508                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2509         }
2510 }
2511
2512 static void
2513 icmp_echo_config_setup(void)
2514 {
2515         portid_t  rxp;
2516         queueid_t rxq;
2517         lcoreid_t lc_id;
2518         uint16_t  sm_id;
2519
2520         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
2521                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
2522                         (nb_txq * nb_fwd_ports);
2523         else
2524                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2525         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2526         cur_fwd_config.nb_fwd_streams =
2527                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2528         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2529                 cur_fwd_config.nb_fwd_lcores =
2530                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2531         if (verbose_level > 0) {
2532                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
2533                        __FUNCTION__,
2534                        cur_fwd_config.nb_fwd_lcores,
2535                        cur_fwd_config.nb_fwd_ports,
2536                        cur_fwd_config.nb_fwd_streams);
2537         }
2538
2539         /* reinitialize forwarding streams */
2540         init_fwd_streams();
2541         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2542         rxp = 0; rxq = 0;
2543         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2544                 if (verbose_level > 0)
2545                         printf("  core=%d: \n", lc_id);
2546                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2547                         struct fwd_stream *fs;
2548                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2549                         fs->rx_port = fwd_ports_ids[rxp];
2550                         fs->rx_queue = rxq;
2551                         fs->tx_port = fs->rx_port;
2552                         fs->tx_queue = rxq;
2553                         fs->peer_addr = fs->tx_port;
2554                         fs->retry_enabled = retry_enabled;
2555                         if (verbose_level > 0)
2556                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
2557                                        sm_id, fs->rx_port, fs->rx_queue,
2558                                        fs->tx_queue);
2559                         rxq = (queueid_t) (rxq + 1);
2560                         if (rxq == nb_rxq) {
2561                                 rxq = 0;
2562                                 rxp = (portid_t) (rxp + 1);
2563                         }
2564                 }
2565         }
2566 }
2567
2568 #if defined RTE_LIBRTE_PMD_SOFTNIC
2569 static void
2570 softnic_fwd_config_setup(void)
2571 {
2572         struct rte_port *port;
2573         portid_t pid, softnic_portid;
2574         queueid_t i;
2575         uint8_t softnic_enable = 0;
2576
2577         RTE_ETH_FOREACH_DEV(pid) {
2578                         port = &ports[pid];
2579                         const char *driver = port->dev_info.driver_name;
2580
2581                         if (strcmp(driver, "net_softnic") == 0) {
2582                                 softnic_portid = pid;
2583                                 softnic_enable = 1;
2584                                 break;
2585                         }
2586         }
2587
2588         if (softnic_enable == 0) {
2589                 printf("Softnic mode not configured(%s)!\n", __func__);
2590                 return;
2591         }
2592
2593         cur_fwd_config.nb_fwd_ports = 1;
2594         cur_fwd_config.nb_fwd_streams = (streamid_t) nb_rxq;
2595
2596         /* Re-initialize forwarding streams */
2597         init_fwd_streams();
2598
2599         /*
2600          * In the softnic forwarding test, the number of forwarding cores
2601          * is set to one and remaining are used for softnic packet processing.
2602          */
2603         cur_fwd_config.nb_fwd_lcores = 1;
2604         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2605
2606         for (i = 0; i < cur_fwd_config.nb_fwd_streams; i++) {
2607                 fwd_streams[i]->rx_port   = softnic_portid;
2608                 fwd_streams[i]->rx_queue  = i;
2609                 fwd_streams[i]->tx_port   = softnic_portid;
2610                 fwd_streams[i]->tx_queue  = i;
2611                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
2612                 fwd_streams[i]->retry_enabled = retry_enabled;
2613         }
2614 }
2615 #endif
2616
2617 void
2618 fwd_config_setup(void)
2619 {
2620         cur_fwd_config.fwd_eng = cur_fwd_eng;
2621         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
2622                 icmp_echo_config_setup();
2623                 return;
2624         }
2625
2626 #if defined RTE_LIBRTE_PMD_SOFTNIC
2627         if (strcmp(cur_fwd_eng->fwd_mode_name, "softnic") == 0) {
2628                 softnic_fwd_config_setup();
2629                 return;
2630         }
2631 #endif
2632
2633         if ((nb_rxq > 1) && (nb_txq > 1)){
2634                 if (dcb_config)
2635                         dcb_fwd_config_setup();
2636                 else
2637                         rss_fwd_config_setup();
2638         }
2639         else
2640                 simple_fwd_config_setup();
2641 }
2642
2643 static const char *
2644 mp_alloc_to_str(uint8_t mode)
2645 {
2646         switch (mode) {
2647         case MP_ALLOC_NATIVE:
2648                 return "native";
2649         case MP_ALLOC_ANON:
2650                 return "anon";
2651         case MP_ALLOC_XMEM:
2652                 return "xmem";
2653         case MP_ALLOC_XMEM_HUGE:
2654                 return "xmemhuge";
2655         case MP_ALLOC_XBUF:
2656                 return "xbuf";
2657         default:
2658                 return "invalid";
2659         }
2660 }
2661
2662 void
2663 pkt_fwd_config_display(struct fwd_config *cfg)
2664 {
2665         struct fwd_stream *fs;
2666         lcoreid_t  lc_id;
2667         streamid_t sm_id;
2668
2669         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
2670                 "NUMA support %s, MP allocation mode: %s\n",
2671                 cfg->fwd_eng->fwd_mode_name,
2672                 retry_enabled == 0 ? "" : " with retry",
2673                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
2674                 numa_support == 1 ? "enabled" : "disabled",
2675                 mp_alloc_to_str(mp_alloc_type));
2676
2677         if (retry_enabled)
2678                 printf("TX retry num: %u, delay between TX retries: %uus\n",
2679                         burst_tx_retry_num, burst_tx_delay_time);
2680         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
2681                 printf("Logical Core %u (socket %u) forwards packets on "
2682                        "%d streams:",
2683                        fwd_lcores_cpuids[lc_id],
2684                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
2685                        fwd_lcores[lc_id]->stream_nb);
2686                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2687                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2688                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
2689                                "P=%d/Q=%d (socket %u) ",
2690                                fs->rx_port, fs->rx_queue,
2691                                ports[fs->rx_port].socket_id,
2692                                fs->tx_port, fs->tx_queue,
2693                                ports[fs->tx_port].socket_id);
2694                         print_ethaddr("peer=",
2695                                       &peer_eth_addrs[fs->peer_addr]);
2696                 }
2697                 printf("\n");
2698         }
2699         printf("\n");
2700 }
2701
2702 void
2703 set_fwd_eth_peer(portid_t port_id, char *peer_addr)
2704 {
2705         struct rte_ether_addr new_peer_addr;
2706         if (!rte_eth_dev_is_valid_port(port_id)) {
2707                 printf("Error: Invalid port number %i\n", port_id);
2708                 return;
2709         }
2710         if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) {
2711                 printf("Error: Invalid ethernet address: %s\n", peer_addr);
2712                 return;
2713         }
2714         peer_eth_addrs[port_id] = new_peer_addr;
2715 }
2716
2717 int
2718 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
2719 {
2720         unsigned int i;
2721         unsigned int lcore_cpuid;
2722         int record_now;
2723
2724         record_now = 0;
2725  again:
2726         for (i = 0; i < nb_lc; i++) {
2727                 lcore_cpuid = lcorelist[i];
2728                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
2729                         printf("lcore %u not enabled\n", lcore_cpuid);
2730                         return -1;
2731                 }
2732                 if (lcore_cpuid == rte_get_master_lcore()) {
2733                         printf("lcore %u cannot be masked on for running "
2734                                "packet forwarding, which is the master lcore "
2735                                "and reserved for command line parsing only\n",
2736                                lcore_cpuid);
2737                         return -1;
2738                 }
2739                 if (record_now)
2740                         fwd_lcores_cpuids[i] = lcore_cpuid;
2741         }
2742         if (record_now == 0) {
2743                 record_now = 1;
2744                 goto again;
2745         }
2746         nb_cfg_lcores = (lcoreid_t) nb_lc;
2747         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
2748                 printf("previous number of forwarding cores %u - changed to "
2749                        "number of configured cores %u\n",
2750                        (unsigned int) nb_fwd_lcores, nb_lc);
2751                 nb_fwd_lcores = (lcoreid_t) nb_lc;
2752         }
2753
2754         return 0;
2755 }
2756
2757 int
2758 set_fwd_lcores_mask(uint64_t lcoremask)
2759 {
2760         unsigned int lcorelist[64];
2761         unsigned int nb_lc;
2762         unsigned int i;
2763
2764         if (lcoremask == 0) {
2765                 printf("Invalid NULL mask of cores\n");
2766                 return -1;
2767         }
2768         nb_lc = 0;
2769         for (i = 0; i < 64; i++) {
2770                 if (! ((uint64_t)(1ULL << i) & lcoremask))
2771                         continue;
2772                 lcorelist[nb_lc++] = i;
2773         }
2774         return set_fwd_lcores_list(lcorelist, nb_lc);
2775 }
2776
2777 void
2778 set_fwd_lcores_number(uint16_t nb_lc)
2779 {
2780         if (nb_lc > nb_cfg_lcores) {
2781                 printf("nb fwd cores %u > %u (max. number of configured "
2782                        "lcores) - ignored\n",
2783                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
2784                 return;
2785         }
2786         nb_fwd_lcores = (lcoreid_t) nb_lc;
2787         printf("Number of forwarding cores set to %u\n",
2788                (unsigned int) nb_fwd_lcores);
2789 }
2790
2791 void
2792 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
2793 {
2794         unsigned int i;
2795         portid_t port_id;
2796         int record_now;
2797
2798         record_now = 0;
2799  again:
2800         for (i = 0; i < nb_pt; i++) {
2801                 port_id = (portid_t) portlist[i];
2802                 if (port_id_is_invalid(port_id, ENABLED_WARN))
2803                         return;
2804                 if (record_now)
2805                         fwd_ports_ids[i] = port_id;
2806         }
2807         if (record_now == 0) {
2808                 record_now = 1;
2809                 goto again;
2810         }
2811         nb_cfg_ports = (portid_t) nb_pt;
2812         if (nb_fwd_ports != (portid_t) nb_pt) {
2813                 printf("previous number of forwarding ports %u - changed to "
2814                        "number of configured ports %u\n",
2815                        (unsigned int) nb_fwd_ports, nb_pt);
2816                 nb_fwd_ports = (portid_t) nb_pt;
2817         }
2818 }
2819
2820 /**
2821  * Parse the user input and obtain the list of forwarding ports
2822  *
2823  * @param[in] list
2824  *   String containing the user input. User can specify
2825  *   in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6.
2826  *   For example, if the user wants to use all the available
2827  *   4 ports in his system, then the input can be 0-3 or 0,1,2,3.
2828  *   If the user wants to use only the ports 1,2 then the input
2829  *   is 1,2.
2830  *   valid characters are '-' and ','
2831  * @param[out] values
2832  *   This array will be filled with a list of port IDs
2833  *   based on the user input
2834  *   Note that duplicate entries are discarded and only the first
2835  *   count entries in this array are port IDs and all the rest
2836  *   will contain default values
2837  * @param[in] maxsize
2838  *   This parameter denotes 2 things
2839  *   1) Number of elements in the values array
2840  *   2) Maximum value of each element in the values array
2841  * @return
2842  *   On success, returns total count of parsed port IDs
2843  *   On failure, returns 0
2844  */
2845 static unsigned int
2846 parse_port_list(const char *list, unsigned int *values, unsigned int maxsize)
2847 {
2848         unsigned int count = 0;
2849         char *end = NULL;
2850         int min, max;
2851         int value, i;
2852         unsigned int marked[maxsize];
2853
2854         if (list == NULL || values == NULL)
2855                 return 0;
2856
2857         for (i = 0; i < (int)maxsize; i++)
2858                 marked[i] = 0;
2859
2860         min = INT_MAX;
2861
2862         do {
2863                 /*Remove the blank spaces if any*/
2864                 while (isblank(*list))
2865                         list++;
2866                 if (*list == '\0')
2867                         break;
2868                 errno = 0;
2869                 value = strtol(list, &end, 10);
2870                 if (errno || end == NULL)
2871                         return 0;
2872                 if (value < 0 || value >= (int)maxsize)
2873                         return 0;
2874                 while (isblank(*end))
2875                         end++;
2876                 if (*end == '-' && min == INT_MAX) {
2877                         min = value;
2878                 } else if ((*end == ',') || (*end == '\0')) {
2879                         max = value;
2880                         if (min == INT_MAX)
2881                                 min = value;
2882                         for (i = min; i <= max; i++) {
2883                                 if (count < maxsize) {
2884                                         if (marked[i])
2885                                                 continue;
2886                                         values[count] = i;
2887                                         marked[i] = 1;
2888                                         count++;
2889                                 }
2890                         }
2891                         min = INT_MAX;
2892                 } else
2893                         return 0;
2894                 list = end + 1;
2895         } while (*end != '\0');
2896
2897         return count;
2898 }
2899
2900 void
2901 parse_fwd_portlist(const char *portlist)
2902 {
2903         unsigned int portcount;
2904         unsigned int portindex[RTE_MAX_ETHPORTS];
2905         unsigned int i, valid_port_count = 0;
2906
2907         portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS);
2908         if (!portcount)
2909                 rte_exit(EXIT_FAILURE, "Invalid fwd port list\n");
2910
2911         /*
2912          * Here we verify the validity of the ports
2913          * and thereby calculate the total number of
2914          * valid ports
2915          */
2916         for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) {
2917                 if (rte_eth_dev_is_valid_port(portindex[i])) {
2918                         portindex[valid_port_count] = portindex[i];
2919                         valid_port_count++;
2920                 }
2921         }
2922
2923         set_fwd_ports_list(portindex, valid_port_count);
2924 }
2925
2926 void
2927 set_fwd_ports_mask(uint64_t portmask)
2928 {
2929         unsigned int portlist[64];
2930         unsigned int nb_pt;
2931         unsigned int i;
2932
2933         if (portmask == 0) {
2934                 printf("Invalid NULL mask of ports\n");
2935                 return;
2936         }
2937         nb_pt = 0;
2938         RTE_ETH_FOREACH_DEV(i) {
2939                 if (! ((uint64_t)(1ULL << i) & portmask))
2940                         continue;
2941                 portlist[nb_pt++] = i;
2942         }
2943         set_fwd_ports_list(portlist, nb_pt);
2944 }
2945
2946 void
2947 set_fwd_ports_number(uint16_t nb_pt)
2948 {
2949         if (nb_pt > nb_cfg_ports) {
2950                 printf("nb fwd ports %u > %u (number of configured "
2951                        "ports) - ignored\n",
2952                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
2953                 return;
2954         }
2955         nb_fwd_ports = (portid_t) nb_pt;
2956         printf("Number of forwarding ports set to %u\n",
2957                (unsigned int) nb_fwd_ports);
2958 }
2959
2960 int
2961 port_is_forwarding(portid_t port_id)
2962 {
2963         unsigned int i;
2964
2965         if (port_id_is_invalid(port_id, ENABLED_WARN))
2966                 return -1;
2967
2968         for (i = 0; i < nb_fwd_ports; i++) {
2969                 if (fwd_ports_ids[i] == port_id)
2970                         return 1;
2971         }
2972
2973         return 0;
2974 }
2975
2976 void
2977 set_nb_pkt_per_burst(uint16_t nb)
2978 {
2979         if (nb > MAX_PKT_BURST) {
2980                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
2981                        " ignored\n",
2982                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
2983                 return;
2984         }
2985         nb_pkt_per_burst = nb;
2986         printf("Number of packets per burst set to %u\n",
2987                (unsigned int) nb_pkt_per_burst);
2988 }
2989
2990 static const char *
2991 tx_split_get_name(enum tx_pkt_split split)
2992 {
2993         uint32_t i;
2994
2995         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2996                 if (tx_split_name[i].split == split)
2997                         return tx_split_name[i].name;
2998         }
2999         return NULL;
3000 }
3001
3002 void
3003 set_tx_pkt_split(const char *name)
3004 {
3005         uint32_t i;
3006
3007         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
3008                 if (strcmp(tx_split_name[i].name, name) == 0) {
3009                         tx_pkt_split = tx_split_name[i].split;
3010                         return;
3011                 }
3012         }
3013         printf("unknown value: \"%s\"\n", name);
3014 }
3015
3016 void
3017 show_tx_pkt_segments(void)
3018 {
3019         uint32_t i, n;
3020         const char *split;
3021
3022         n = tx_pkt_nb_segs;
3023         split = tx_split_get_name(tx_pkt_split);
3024
3025         printf("Number of segments: %u\n", n);
3026         printf("Segment sizes: ");
3027         for (i = 0; i != n - 1; i++)
3028                 printf("%hu,", tx_pkt_seg_lengths[i]);
3029         printf("%hu\n", tx_pkt_seg_lengths[i]);
3030         printf("Split packet: %s\n", split);
3031 }
3032
3033 void
3034 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
3035 {
3036         uint16_t tx_pkt_len;
3037         unsigned i;
3038
3039         if (nb_segs >= (unsigned) nb_txd) {
3040                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
3041                        nb_segs, (unsigned int) nb_txd);
3042                 return;
3043         }
3044
3045         /*
3046          * Check that each segment length is greater or equal than
3047          * the mbuf data sise.
3048          * Check also that the total packet length is greater or equal than the
3049          * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) +
3050          * 20 + 8).
3051          */
3052         tx_pkt_len = 0;
3053         for (i = 0; i < nb_segs; i++) {
3054                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
3055                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
3056                                i, seg_lengths[i], (unsigned) mbuf_data_size);
3057                         return;
3058                 }
3059                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
3060         }
3061         if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) {
3062                 printf("total packet length=%u < %d - give up\n",
3063                                 (unsigned) tx_pkt_len,
3064                                 (int)(sizeof(struct rte_ether_hdr) + 20 + 8));
3065                 return;
3066         }
3067
3068         for (i = 0; i < nb_segs; i++)
3069                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
3070
3071         tx_pkt_length  = tx_pkt_len;
3072         tx_pkt_nb_segs = (uint8_t) nb_segs;
3073 }
3074
3075 void
3076 setup_gro(const char *onoff, portid_t port_id)
3077 {
3078         if (!rte_eth_dev_is_valid_port(port_id)) {
3079                 printf("invalid port id %u\n", port_id);
3080                 return;
3081         }
3082         if (test_done == 0) {
3083                 printf("Before enable/disable GRO,"
3084                                 " please stop forwarding first\n");
3085                 return;
3086         }
3087         if (strcmp(onoff, "on") == 0) {
3088                 if (gro_ports[port_id].enable != 0) {
3089                         printf("Port %u has enabled GRO. Please"
3090                                         " disable GRO first\n", port_id);
3091                         return;
3092                 }
3093                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
3094                         gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
3095                         gro_ports[port_id].param.max_flow_num =
3096                                 GRO_DEFAULT_FLOW_NUM;
3097                         gro_ports[port_id].param.max_item_per_flow =
3098                                 GRO_DEFAULT_ITEM_NUM_PER_FLOW;
3099                 }
3100                 gro_ports[port_id].enable = 1;
3101         } else {
3102                 if (gro_ports[port_id].enable == 0) {
3103                         printf("Port %u has disabled GRO\n", port_id);
3104                         return;
3105                 }
3106                 gro_ports[port_id].enable = 0;
3107         }
3108 }
3109
3110 void
3111 setup_gro_flush_cycles(uint8_t cycles)
3112 {
3113         if (test_done == 0) {
3114                 printf("Before change flush interval for GRO,"
3115                                 " please stop forwarding first.\n");
3116                 return;
3117         }
3118
3119         if (cycles > GRO_MAX_FLUSH_CYCLES || cycles <
3120                         GRO_DEFAULT_FLUSH_CYCLES) {
3121                 printf("The flushing cycle be in the range"
3122                                 " of 1 to %u. Revert to the default"
3123                                 " value %u.\n",
3124                                 GRO_MAX_FLUSH_CYCLES,
3125                                 GRO_DEFAULT_FLUSH_CYCLES);
3126                 cycles = GRO_DEFAULT_FLUSH_CYCLES;
3127         }
3128
3129         gro_flush_cycles = cycles;
3130 }
3131
3132 void
3133 show_gro(portid_t port_id)
3134 {
3135         struct rte_gro_param *param;
3136         uint32_t max_pkts_num;
3137
3138         param = &gro_ports[port_id].param;
3139
3140         if (!rte_eth_dev_is_valid_port(port_id)) {
3141                 printf("Invalid port id %u.\n", port_id);
3142                 return;
3143         }
3144         if (gro_ports[port_id].enable) {
3145                 printf("GRO type: TCP/IPv4\n");
3146                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
3147                         max_pkts_num = param->max_flow_num *
3148                                 param->max_item_per_flow;
3149                 } else
3150                         max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES;
3151                 printf("Max number of packets to perform GRO: %u\n",
3152                                 max_pkts_num);
3153                 printf("Flushing cycles: %u\n", gro_flush_cycles);
3154         } else
3155                 printf("Port %u doesn't enable GRO.\n", port_id);
3156 }
3157
3158 void
3159 setup_gso(const char *mode, portid_t port_id)
3160 {
3161         if (!rte_eth_dev_is_valid_port(port_id)) {
3162                 printf("invalid port id %u\n", port_id);
3163                 return;
3164         }
3165         if (strcmp(mode, "on") == 0) {
3166                 if (test_done == 0) {
3167                         printf("before enabling GSO,"
3168                                         " please stop forwarding first\n");
3169                         return;
3170                 }
3171                 gso_ports[port_id].enable = 1;
3172         } else if (strcmp(mode, "off") == 0) {
3173                 if (test_done == 0) {
3174                         printf("before disabling GSO,"
3175                                         " please stop forwarding first\n");
3176                         return;
3177                 }
3178                 gso_ports[port_id].enable = 0;
3179         }
3180 }
3181
3182 char*
3183 list_pkt_forwarding_modes(void)
3184 {
3185         static char fwd_modes[128] = "";
3186         const char *separator = "|";
3187         struct fwd_engine *fwd_eng;
3188         unsigned i = 0;
3189
3190         if (strlen (fwd_modes) == 0) {
3191                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
3192                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
3193                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
3194                         strncat(fwd_modes, separator,
3195                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
3196                 }
3197                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
3198         }
3199
3200         return fwd_modes;
3201 }
3202
3203 char*
3204 list_pkt_forwarding_retry_modes(void)
3205 {
3206         static char fwd_modes[128] = "";
3207         const char *separator = "|";
3208         struct fwd_engine *fwd_eng;
3209         unsigned i = 0;
3210
3211         if (strlen(fwd_modes) == 0) {
3212                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
3213                         if (fwd_eng == &rx_only_engine)
3214                                 continue;
3215                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
3216                                         sizeof(fwd_modes) -
3217                                         strlen(fwd_modes) - 1);
3218                         strncat(fwd_modes, separator,
3219                                         sizeof(fwd_modes) -
3220                                         strlen(fwd_modes) - 1);
3221                 }
3222                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
3223         }
3224
3225         return fwd_modes;
3226 }
3227
3228 void
3229 set_pkt_forwarding_mode(const char *fwd_mode_name)
3230 {
3231         struct fwd_engine *fwd_eng;
3232         unsigned i;
3233
3234         i = 0;
3235         while ((fwd_eng = fwd_engines[i]) != NULL) {
3236                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
3237                         printf("Set %s packet forwarding mode%s\n",
3238                                fwd_mode_name,
3239                                retry_enabled == 0 ? "" : " with retry");
3240                         cur_fwd_eng = fwd_eng;
3241                         return;
3242                 }
3243                 i++;
3244         }
3245         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
3246 }
3247
3248 void
3249 add_rx_dump_callbacks(portid_t portid)
3250 {
3251         struct rte_eth_dev_info dev_info;
3252         uint16_t queue;
3253         int ret;
3254
3255         if (port_id_is_invalid(portid, ENABLED_WARN))
3256                 return;
3257
3258         ret = eth_dev_info_get_print_err(portid, &dev_info);
3259         if (ret != 0)
3260                 return;
3261
3262         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
3263                 if (!ports[portid].rx_dump_cb[queue])
3264                         ports[portid].rx_dump_cb[queue] =
3265                                 rte_eth_add_rx_callback(portid, queue,
3266                                         dump_rx_pkts, NULL);
3267 }
3268
3269 void
3270 add_tx_dump_callbacks(portid_t portid)
3271 {
3272         struct rte_eth_dev_info dev_info;
3273         uint16_t queue;
3274         int ret;
3275
3276         if (port_id_is_invalid(portid, ENABLED_WARN))
3277                 return;
3278
3279         ret = eth_dev_info_get_print_err(portid, &dev_info);
3280         if (ret != 0)
3281                 return;
3282
3283         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
3284                 if (!ports[portid].tx_dump_cb[queue])
3285                         ports[portid].tx_dump_cb[queue] =
3286                                 rte_eth_add_tx_callback(portid, queue,
3287                                                         dump_tx_pkts, NULL);
3288 }
3289
3290 void
3291 remove_rx_dump_callbacks(portid_t portid)
3292 {
3293         struct rte_eth_dev_info dev_info;
3294         uint16_t queue;
3295         int ret;
3296
3297         if (port_id_is_invalid(portid, ENABLED_WARN))
3298                 return;
3299
3300         ret = eth_dev_info_get_print_err(portid, &dev_info);
3301         if (ret != 0)
3302                 return;
3303
3304         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
3305                 if (ports[portid].rx_dump_cb[queue]) {
3306                         rte_eth_remove_rx_callback(portid, queue,
3307                                 ports[portid].rx_dump_cb[queue]);
3308                         ports[portid].rx_dump_cb[queue] = NULL;
3309                 }
3310 }
3311
3312 void
3313 remove_tx_dump_callbacks(portid_t portid)
3314 {
3315         struct rte_eth_dev_info dev_info;
3316         uint16_t queue;
3317         int ret;
3318
3319         if (port_id_is_invalid(portid, ENABLED_WARN))
3320                 return;
3321
3322         ret = eth_dev_info_get_print_err(portid, &dev_info);
3323         if (ret != 0)
3324                 return;
3325
3326         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
3327                 if (ports[portid].tx_dump_cb[queue]) {
3328                         rte_eth_remove_tx_callback(portid, queue,
3329                                 ports[portid].tx_dump_cb[queue]);
3330                         ports[portid].tx_dump_cb[queue] = NULL;
3331                 }
3332 }
3333
3334 void
3335 configure_rxtx_dump_callbacks(uint16_t verbose)
3336 {
3337         portid_t portid;
3338
3339 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3340                 TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n");
3341                 return;
3342 #endif
3343
3344         RTE_ETH_FOREACH_DEV(portid)
3345         {
3346                 if (verbose == 1 || verbose > 2)
3347                         add_rx_dump_callbacks(portid);
3348                 else
3349                         remove_rx_dump_callbacks(portid);
3350                 if (verbose >= 2)
3351                         add_tx_dump_callbacks(portid);
3352                 else
3353                         remove_tx_dump_callbacks(portid);
3354         }
3355 }
3356
3357 void
3358 set_verbose_level(uint16_t vb_level)
3359 {
3360         printf("Change verbose level from %u to %u\n",
3361                (unsigned int) verbose_level, (unsigned int) vb_level);
3362         verbose_level = vb_level;
3363         configure_rxtx_dump_callbacks(verbose_level);
3364 }
3365
3366 void
3367 vlan_extend_set(portid_t port_id, int on)
3368 {
3369         int diag;
3370         int vlan_offload;
3371         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3372
3373         if (port_id_is_invalid(port_id, ENABLED_WARN))
3374                 return;
3375
3376         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3377
3378         if (on) {
3379                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
3380                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
3381         } else {
3382                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
3383                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
3384         }
3385
3386         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3387         if (diag < 0)
3388                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
3389                "diag=%d\n", port_id, on, diag);
3390         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3391 }
3392
3393 void
3394 rx_vlan_strip_set(portid_t port_id, int on)
3395 {
3396         int diag;
3397         int vlan_offload;
3398         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3399
3400         if (port_id_is_invalid(port_id, ENABLED_WARN))
3401                 return;
3402
3403         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3404
3405         if (on) {
3406                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
3407                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
3408         } else {
3409                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
3410                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
3411         }
3412
3413         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3414         if (diag < 0)
3415                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
3416                "diag=%d\n", port_id, on, diag);
3417         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3418 }
3419
3420 void
3421 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
3422 {
3423         int diag;
3424
3425         if (port_id_is_invalid(port_id, ENABLED_WARN))
3426                 return;
3427
3428         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
3429         if (diag < 0)
3430                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
3431                "diag=%d\n", port_id, queue_id, on, diag);
3432 }
3433
3434 void
3435 rx_vlan_filter_set(portid_t port_id, int on)
3436 {
3437         int diag;
3438         int vlan_offload;
3439         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3440
3441         if (port_id_is_invalid(port_id, ENABLED_WARN))
3442                 return;
3443
3444         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3445
3446         if (on) {
3447                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
3448                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3449         } else {
3450                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
3451                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
3452         }
3453
3454         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3455         if (diag < 0)
3456                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
3457                "diag=%d\n", port_id, on, diag);
3458         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3459 }
3460
3461 void
3462 rx_vlan_qinq_strip_set(portid_t port_id, int on)
3463 {
3464         int diag;
3465         int vlan_offload;
3466         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3467
3468         if (port_id_is_invalid(port_id, ENABLED_WARN))
3469                 return;
3470
3471         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3472
3473         if (on) {
3474                 vlan_offload |= ETH_QINQ_STRIP_OFFLOAD;
3475                 port_rx_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
3476         } else {
3477                 vlan_offload &= ~ETH_QINQ_STRIP_OFFLOAD;
3478                 port_rx_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
3479         }
3480
3481         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3482         if (diag < 0)
3483                 printf("%s(port_pi=%d, on=%d) failed "
3484                "diag=%d\n", __func__, port_id, on, diag);
3485         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3486 }
3487
3488 int
3489 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
3490 {
3491         int diag;
3492
3493         if (port_id_is_invalid(port_id, ENABLED_WARN))
3494                 return 1;
3495         if (vlan_id_is_invalid(vlan_id))
3496                 return 1;
3497         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
3498         if (diag == 0)
3499                 return 0;
3500         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
3501                "diag=%d\n",
3502                port_id, vlan_id, on, diag);
3503         return -1;
3504 }
3505
3506 void
3507 rx_vlan_all_filter_set(portid_t port_id, int on)
3508 {
3509         uint16_t vlan_id;
3510
3511         if (port_id_is_invalid(port_id, ENABLED_WARN))
3512                 return;
3513         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
3514                 if (rx_vft_set(port_id, vlan_id, on))
3515                         break;
3516         }
3517 }
3518
3519 void
3520 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
3521 {
3522         int diag;
3523
3524         if (port_id_is_invalid(port_id, ENABLED_WARN))
3525                 return;
3526
3527         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
3528         if (diag == 0)
3529                 return;
3530
3531         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
3532                "diag=%d\n",
3533                port_id, vlan_type, tp_id, diag);
3534 }
3535
3536 void
3537 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
3538 {
3539         struct rte_eth_dev_info dev_info;
3540         int ret;
3541
3542         if (port_id_is_invalid(port_id, ENABLED_WARN))
3543                 return;
3544         if (vlan_id_is_invalid(vlan_id))
3545                 return;
3546
3547         if (ports[port_id].dev_conf.txmode.offloads &
3548             DEV_TX_OFFLOAD_QINQ_INSERT) {
3549                 printf("Error, as QinQ has been enabled.\n");
3550                 return;
3551         }
3552
3553         ret = eth_dev_info_get_print_err(port_id, &dev_info);
3554         if (ret != 0)
3555                 return;
3556
3557         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT) == 0) {
3558                 printf("Error: vlan insert is not supported by port %d\n",
3559                         port_id);
3560                 return;
3561         }
3562
3563         tx_vlan_reset(port_id);
3564         ports[port_id].dev_conf.txmode.offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
3565         ports[port_id].tx_vlan_id = vlan_id;
3566 }
3567
3568 void
3569 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
3570 {
3571         struct rte_eth_dev_info dev_info;
3572         int ret;
3573
3574         if (port_id_is_invalid(port_id, ENABLED_WARN))
3575                 return;
3576         if (vlan_id_is_invalid(vlan_id))
3577                 return;
3578         if (vlan_id_is_invalid(vlan_id_outer))
3579                 return;
3580
3581         ret = eth_dev_info_get_print_err(port_id, &dev_info);
3582         if (ret != 0)
3583                 return;
3584
3585         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_QINQ_INSERT) == 0) {
3586                 printf("Error: qinq insert not supported by port %d\n",
3587                         port_id);
3588                 return;
3589         }
3590
3591         tx_vlan_reset(port_id);
3592         ports[port_id].dev_conf.txmode.offloads |= (DEV_TX_OFFLOAD_VLAN_INSERT |
3593                                                     DEV_TX_OFFLOAD_QINQ_INSERT);
3594         ports[port_id].tx_vlan_id = vlan_id;
3595         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
3596 }
3597
3598 void
3599 tx_vlan_reset(portid_t port_id)
3600 {
3601         if (port_id_is_invalid(port_id, ENABLED_WARN))
3602                 return;
3603         ports[port_id].dev_conf.txmode.offloads &=
3604                                 ~(DEV_TX_OFFLOAD_VLAN_INSERT |
3605                                   DEV_TX_OFFLOAD_QINQ_INSERT);
3606         ports[port_id].tx_vlan_id = 0;
3607         ports[port_id].tx_vlan_id_outer = 0;
3608 }
3609
3610 void
3611 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
3612 {
3613         if (port_id_is_invalid(port_id, ENABLED_WARN))
3614                 return;
3615
3616         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
3617 }
3618
3619 void
3620 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
3621 {
3622         uint16_t i;
3623         uint8_t existing_mapping_found = 0;
3624
3625         if (port_id_is_invalid(port_id, ENABLED_WARN))
3626                 return;
3627
3628         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
3629                 return;
3630
3631         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3632                 printf("map_value not in required range 0..%d\n",
3633                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
3634                 return;
3635         }
3636
3637         if (!is_rx) { /*then tx*/
3638                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
3639                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
3640                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
3641                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
3642                                 existing_mapping_found = 1;
3643                                 break;
3644                         }
3645                 }
3646                 if (!existing_mapping_found) { /* A new additional mapping... */
3647                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
3648                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
3649                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
3650                         nb_tx_queue_stats_mappings++;
3651                 }
3652         }
3653         else { /*rx*/
3654                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
3655                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
3656                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
3657                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
3658                                 existing_mapping_found = 1;
3659                                 break;
3660                         }
3661                 }
3662                 if (!existing_mapping_found) { /* A new additional mapping... */
3663                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
3664                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
3665                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
3666                         nb_rx_queue_stats_mappings++;
3667                 }
3668         }
3669 }
3670
3671 void
3672 set_xstats_hide_zero(uint8_t on_off)
3673 {
3674         xstats_hide_zero = on_off;
3675 }
3676
3677 static inline void
3678 print_fdir_mask(struct rte_eth_fdir_masks *mask)
3679 {
3680         printf("\n    vlan_tci: 0x%04x", rte_be_to_cpu_16(mask->vlan_tci_mask));
3681
3682         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
3683                 printf(", mac_addr: 0x%02x, tunnel_type: 0x%01x,"
3684                         " tunnel_id: 0x%08x",
3685                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
3686                         rte_be_to_cpu_32(mask->tunnel_id_mask));
3687         else if (fdir_conf.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3688                 printf(", src_ipv4: 0x%08x, dst_ipv4: 0x%08x",
3689                         rte_be_to_cpu_32(mask->ipv4_mask.src_ip),
3690                         rte_be_to_cpu_32(mask->ipv4_mask.dst_ip));
3691
3692                 printf("\n    src_port: 0x%04x, dst_port: 0x%04x",
3693                         rte_be_to_cpu_16(mask->src_port_mask),
3694                         rte_be_to_cpu_16(mask->dst_port_mask));
3695
3696                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
3697                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[0]),
3698                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[1]),
3699                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[2]),
3700                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[3]));
3701
3702                 printf("\n    dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
3703                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[0]),
3704                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[1]),
3705                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[2]),
3706                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[3]));
3707         }
3708
3709         printf("\n");
3710 }
3711
3712 static inline void
3713 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
3714 {
3715         struct rte_eth_flex_payload_cfg *cfg;
3716         uint32_t i, j;
3717
3718         for (i = 0; i < flex_conf->nb_payloads; i++) {
3719                 cfg = &flex_conf->flex_set[i];
3720                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
3721                         printf("\n    RAW:  ");
3722                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
3723                         printf("\n    L2_PAYLOAD:  ");
3724                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
3725                         printf("\n    L3_PAYLOAD:  ");
3726                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
3727                         printf("\n    L4_PAYLOAD:  ");
3728                 else
3729                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
3730                 for (j = 0; j < num; j++)
3731                         printf("  %-5u", cfg->src_offset[j]);
3732         }
3733         printf("\n");
3734 }
3735
3736 static char *
3737 flowtype_to_str(uint16_t flow_type)
3738 {
3739         struct flow_type_info {
3740                 char str[32];
3741                 uint16_t ftype;
3742         };
3743
3744         uint8_t i;
3745         static struct flow_type_info flowtype_str_table[] = {
3746                 {"raw", RTE_ETH_FLOW_RAW},
3747                 {"ipv4", RTE_ETH_FLOW_IPV4},
3748                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
3749                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
3750                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
3751                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
3752                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
3753                 {"ipv6", RTE_ETH_FLOW_IPV6},
3754                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
3755                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
3756                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
3757                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
3758                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
3759                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
3760                 {"port", RTE_ETH_FLOW_PORT},
3761                 {"vxlan", RTE_ETH_FLOW_VXLAN},
3762                 {"geneve", RTE_ETH_FLOW_GENEVE},
3763                 {"nvgre", RTE_ETH_FLOW_NVGRE},
3764                 {"vxlan-gpe", RTE_ETH_FLOW_VXLAN_GPE},
3765         };
3766
3767         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
3768                 if (flowtype_str_table[i].ftype == flow_type)
3769                         return flowtype_str_table[i].str;
3770         }
3771
3772         return NULL;
3773 }
3774
3775 static inline void
3776 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
3777 {
3778         struct rte_eth_fdir_flex_mask *mask;
3779         uint32_t i, j;
3780         char *p;
3781
3782         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
3783                 mask = &flex_conf->flex_mask[i];
3784                 p = flowtype_to_str(mask->flow_type);
3785                 printf("\n    %s:\t", p ? p : "unknown");
3786                 for (j = 0; j < num; j++)
3787                         printf(" %02x", mask->mask[j]);
3788         }
3789         printf("\n");
3790 }
3791
3792 static inline void
3793 print_fdir_flow_type(uint32_t flow_types_mask)
3794 {
3795         int i;
3796         char *p;
3797
3798         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
3799                 if (!(flow_types_mask & (1 << i)))
3800                         continue;
3801                 p = flowtype_to_str(i);
3802                 if (p)
3803                         printf(" %s", p);
3804                 else
3805                         printf(" unknown");
3806         }
3807         printf("\n");
3808 }
3809
3810 static int
3811 get_fdir_info(portid_t port_id, struct rte_eth_fdir_info *fdir_info,
3812                     struct rte_eth_fdir_stats *fdir_stat)
3813 {
3814         int ret;
3815
3816         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
3817         if (!ret) {
3818                 rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
3819                                RTE_ETH_FILTER_INFO, fdir_info);
3820                 rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
3821                                RTE_ETH_FILTER_STATS, fdir_stat);
3822                 return 0;
3823         }
3824
3825 #ifdef RTE_LIBRTE_I40E_PMD
3826         if (ret == -ENOTSUP) {
3827                 ret = rte_pmd_i40e_get_fdir_info(port_id, fdir_info);
3828                 if (!ret)
3829                         ret = rte_pmd_i40e_get_fdir_stats(port_id, fdir_stat);
3830         }
3831 #endif
3832 #ifdef RTE_LIBRTE_IXGBE_PMD
3833         if (ret == -ENOTSUP) {
3834                 ret = rte_pmd_ixgbe_get_fdir_info(port_id, fdir_info);
3835                 if (!ret)
3836                         ret = rte_pmd_ixgbe_get_fdir_stats(port_id, fdir_stat);
3837         }
3838 #endif
3839         switch (ret) {
3840         case 0:
3841                 break;
3842         case -ENOTSUP:
3843                 printf("\n FDIR is not supported on port %-2d\n",
3844                         port_id);
3845                 break;
3846         default:
3847                 printf("programming error: (%s)\n", strerror(-ret));
3848                 break;
3849         }
3850         return ret;
3851 }
3852
3853 void
3854 fdir_get_infos(portid_t port_id)
3855 {
3856         struct rte_eth_fdir_stats fdir_stat;
3857         struct rte_eth_fdir_info fdir_info;
3858
3859         static const char *fdir_stats_border = "########################";
3860
3861         if (port_id_is_invalid(port_id, ENABLED_WARN))
3862                 return;
3863
3864         memset(&fdir_info, 0, sizeof(fdir_info));
3865         memset(&fdir_stat, 0, sizeof(fdir_stat));
3866         if (get_fdir_info(port_id, &fdir_info, &fdir_stat))
3867                 return;
3868
3869         printf("\n  %s FDIR infos for port %-2d     %s\n",
3870                fdir_stats_border, port_id, fdir_stats_border);
3871         printf("  MODE: ");
3872         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
3873                 printf("  PERFECT\n");
3874         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
3875                 printf("  PERFECT-MAC-VLAN\n");
3876         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
3877                 printf("  PERFECT-TUNNEL\n");
3878         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
3879                 printf("  SIGNATURE\n");
3880         else
3881                 printf("  DISABLE\n");
3882         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
3883                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
3884                 printf("  SUPPORTED FLOW TYPE: ");
3885                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
3886         }
3887         printf("  FLEX PAYLOAD INFO:\n");
3888         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
3889                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
3890                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
3891                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
3892                 fdir_info.flex_payload_unit,
3893                 fdir_info.max_flex_payload_segment_num,
3894                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
3895         printf("  MASK: ");
3896         print_fdir_mask(&fdir_info.mask);
3897         if (fdir_info.flex_conf.nb_payloads > 0) {
3898                 printf("  FLEX PAYLOAD SRC OFFSET:");
3899                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
3900         }
3901         if (fdir_info.flex_conf.nb_flexmasks > 0) {
3902                 printf("  FLEX MASK CFG:");
3903                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
3904         }
3905         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
3906                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
3907         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
3908                fdir_info.guarant_spc, fdir_info.best_spc);
3909         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
3910                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
3911                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
3912                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
3913                fdir_stat.collision, fdir_stat.free,
3914                fdir_stat.maxhash, fdir_stat.maxlen,
3915                fdir_stat.add, fdir_stat.remove,
3916                fdir_stat.f_add, fdir_stat.f_remove);
3917         printf("  %s############################%s\n",
3918                fdir_stats_border, fdir_stats_border);
3919 }
3920
3921 void
3922 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
3923 {
3924         struct rte_port *port;
3925         struct rte_eth_fdir_flex_conf *flex_conf;
3926         int i, idx = 0;
3927
3928         port = &ports[port_id];
3929         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
3930         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
3931                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
3932                         idx = i;
3933                         break;
3934                 }
3935         }
3936         if (i >= RTE_ETH_FLOW_MAX) {
3937                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
3938                         idx = flex_conf->nb_flexmasks;
3939                         flex_conf->nb_flexmasks++;
3940                 } else {
3941                         printf("The flex mask table is full. Can not set flex"
3942                                 " mask for flow_type(%u).", cfg->flow_type);
3943                         return;
3944                 }
3945         }
3946         rte_memcpy(&flex_conf->flex_mask[idx],
3947                          cfg,
3948                          sizeof(struct rte_eth_fdir_flex_mask));
3949 }
3950
3951 void
3952 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
3953 {
3954         struct rte_port *port;
3955         struct rte_eth_fdir_flex_conf *flex_conf;
3956         int i, idx = 0;
3957
3958         port = &ports[port_id];
3959         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
3960         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
3961                 if (cfg->type == flex_conf->flex_set[i].type) {
3962                         idx = i;
3963                         break;
3964                 }
3965         }
3966         if (i >= RTE_ETH_PAYLOAD_MAX) {
3967                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
3968                         idx = flex_conf->nb_payloads;
3969                         flex_conf->nb_payloads++;
3970                 } else {
3971                         printf("The flex payload table is full. Can not set"
3972                                 " flex payload for type(%u).", cfg->type);
3973                         return;
3974                 }
3975         }
3976         rte_memcpy(&flex_conf->flex_set[idx],
3977                          cfg,
3978                          sizeof(struct rte_eth_flex_payload_cfg));
3979
3980 }
3981
3982 void
3983 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
3984 {
3985 #ifdef RTE_LIBRTE_IXGBE_PMD
3986         int diag;
3987
3988         if (is_rx)
3989                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
3990         else
3991                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
3992
3993         if (diag == 0)
3994                 return;
3995         printf("rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
3996                         is_rx ? "rx" : "tx", port_id, diag);
3997         return;
3998 #endif
3999         printf("VF %s setting not supported for port %d\n",
4000                         is_rx ? "Rx" : "Tx", port_id);
4001         RTE_SET_USED(vf);
4002         RTE_SET_USED(on);
4003 }
4004
4005 int
4006 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
4007 {
4008         int diag;
4009         struct rte_eth_link link;
4010         int ret;
4011
4012         if (port_id_is_invalid(port_id, ENABLED_WARN))
4013                 return 1;
4014         ret = eth_link_get_nowait_print_err(port_id, &link);
4015         if (ret < 0)
4016                 return 1;
4017         if (rate > link.link_speed) {
4018                 printf("Invalid rate value:%u bigger than link speed: %u\n",
4019                         rate, link.link_speed);
4020                 return 1;
4021         }
4022         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
4023         if (diag == 0)
4024                 return diag;
4025         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
4026                 port_id, diag);
4027         return diag;
4028 }
4029
4030 int
4031 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
4032 {
4033         int diag = -ENOTSUP;
4034
4035         RTE_SET_USED(vf);
4036         RTE_SET_USED(rate);
4037         RTE_SET_USED(q_msk);
4038
4039 #ifdef RTE_LIBRTE_IXGBE_PMD
4040         if (diag == -ENOTSUP)
4041                 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
4042                                                        q_msk);
4043 #endif
4044 #ifdef RTE_LIBRTE_BNXT_PMD
4045         if (diag == -ENOTSUP)
4046                 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
4047 #endif
4048         if (diag == 0)
4049                 return diag;
4050
4051         printf("set_vf_rate_limit for port_id=%d failed diag=%d\n",
4052                 port_id, diag);
4053         return diag;
4054 }
4055
4056 /*
4057  * Functions to manage the set of filtered Multicast MAC addresses.
4058  *
4059  * A pool of filtered multicast MAC addresses is associated with each port.
4060  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
4061  * The address of the pool and the number of valid multicast MAC addresses
4062  * recorded in the pool are stored in the fields "mc_addr_pool" and
4063  * "mc_addr_nb" of the "rte_port" data structure.
4064  *
4065  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
4066  * to be supplied a contiguous array of multicast MAC addresses.
4067  * To comply with this constraint, the set of multicast addresses recorded
4068  * into the pool are systematically compacted at the beginning of the pool.
4069  * Hence, when a multicast address is removed from the pool, all following
4070  * addresses, if any, are copied back to keep the set contiguous.
4071  */
4072 #define MCAST_POOL_INC 32
4073
4074 static int
4075 mcast_addr_pool_extend(struct rte_port *port)
4076 {
4077         struct rte_ether_addr *mc_pool;
4078         size_t mc_pool_size;
4079
4080         /*
4081          * If a free entry is available at the end of the pool, just
4082          * increment the number of recorded multicast addresses.
4083          */
4084         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
4085                 port->mc_addr_nb++;
4086                 return 0;
4087         }
4088
4089         /*
4090          * [re]allocate a pool with MCAST_POOL_INC more entries.
4091          * The previous test guarantees that port->mc_addr_nb is a multiple
4092          * of MCAST_POOL_INC.
4093          */
4094         mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb +
4095                                                     MCAST_POOL_INC);
4096         mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool,
4097                                                 mc_pool_size);
4098         if (mc_pool == NULL) {
4099                 printf("allocation of pool of %u multicast addresses failed\n",
4100                        port->mc_addr_nb + MCAST_POOL_INC);
4101                 return -ENOMEM;
4102         }
4103
4104         port->mc_addr_pool = mc_pool;
4105         port->mc_addr_nb++;
4106         return 0;
4107
4108 }
4109
4110 static void
4111 mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr)
4112 {
4113         if (mcast_addr_pool_extend(port) != 0)
4114                 return;
4115         rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]);
4116 }
4117
4118 static void
4119 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
4120 {
4121         port->mc_addr_nb--;
4122         if (addr_idx == port->mc_addr_nb) {
4123                 /* No need to recompact the set of multicast addressses. */
4124                 if (port->mc_addr_nb == 0) {
4125                         /* free the pool of multicast addresses. */
4126                         free(port->mc_addr_pool);
4127                         port->mc_addr_pool = NULL;
4128                 }
4129                 return;
4130         }
4131         memmove(&port->mc_addr_pool[addr_idx],
4132                 &port->mc_addr_pool[addr_idx + 1],
4133                 sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx));
4134 }
4135
4136 static int
4137 eth_port_multicast_addr_list_set(portid_t port_id)
4138 {
4139         struct rte_port *port;
4140         int diag;
4141
4142         port = &ports[port_id];
4143         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
4144                                             port->mc_addr_nb);
4145         if (diag < 0)
4146                 printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
4147                         port_id, port->mc_addr_nb, diag);
4148
4149         return diag;
4150 }
4151
4152 void
4153 mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr)
4154 {
4155         struct rte_port *port;
4156         uint32_t i;
4157
4158         if (port_id_is_invalid(port_id, ENABLED_WARN))
4159                 return;
4160
4161         port = &ports[port_id];
4162
4163         /*
4164          * Check that the added multicast MAC address is not already recorded
4165          * in the pool of multicast addresses.
4166          */
4167         for (i = 0; i < port->mc_addr_nb; i++) {
4168                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
4169                         printf("multicast address already filtered by port\n");
4170                         return;
4171                 }
4172         }
4173
4174         mcast_addr_pool_append(port, mc_addr);
4175         if (eth_port_multicast_addr_list_set(port_id) < 0)
4176                 /* Rollback on failure, remove the address from the pool */
4177                 mcast_addr_pool_remove(port, i);
4178 }
4179
4180 void
4181 mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr)
4182 {
4183         struct rte_port *port;
4184         uint32_t i;
4185
4186         if (port_id_is_invalid(port_id, ENABLED_WARN))
4187                 return;
4188
4189         port = &ports[port_id];
4190
4191         /*
4192          * Search the pool of multicast MAC addresses for the removed address.
4193          */
4194         for (i = 0; i < port->mc_addr_nb; i++) {
4195                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
4196                         break;
4197         }
4198         if (i == port->mc_addr_nb) {
4199                 printf("multicast address not filtered by port %d\n", port_id);
4200                 return;
4201         }
4202
4203         mcast_addr_pool_remove(port, i);
4204         if (eth_port_multicast_addr_list_set(port_id) < 0)
4205                 /* Rollback on failure, add the address back into the pool */
4206                 mcast_addr_pool_append(port, mc_addr);
4207 }
4208
4209 void
4210 port_dcb_info_display(portid_t port_id)
4211 {
4212         struct rte_eth_dcb_info dcb_info;
4213         uint16_t i;
4214         int ret;
4215         static const char *border = "================";
4216
4217         if (port_id_is_invalid(port_id, ENABLED_WARN))
4218                 return;
4219
4220         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
4221         if (ret) {
4222                 printf("\n Failed to get dcb infos on port %-2d\n",
4223                         port_id);
4224                 return;
4225         }
4226         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
4227         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
4228         printf("\n  TC :        ");
4229         for (i = 0; i < dcb_info.nb_tcs; i++)
4230                 printf("\t%4d", i);
4231         printf("\n  Priority :  ");
4232         for (i = 0; i < dcb_info.nb_tcs; i++)
4233                 printf("\t%4d", dcb_info.prio_tc[i]);
4234         printf("\n  BW percent :");
4235         for (i = 0; i < dcb_info.nb_tcs; i++)
4236                 printf("\t%4d%%", dcb_info.tc_bws[i]);
4237         printf("\n  RXQ base :  ");
4238         for (i = 0; i < dcb_info.nb_tcs; i++)
4239                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
4240         printf("\n  RXQ number :");
4241         for (i = 0; i < dcb_info.nb_tcs; i++)
4242                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
4243         printf("\n  TXQ base :  ");
4244         for (i = 0; i < dcb_info.nb_tcs; i++)
4245                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
4246         printf("\n  TXQ number :");
4247         for (i = 0; i < dcb_info.nb_tcs; i++)
4248                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
4249         printf("\n");
4250 }
4251
4252 uint8_t *
4253 open_file(const char *file_path, uint32_t *size)
4254 {
4255         int fd = open(file_path, O_RDONLY);
4256         off_t pkg_size;
4257         uint8_t *buf = NULL;
4258         int ret = 0;
4259         struct stat st_buf;
4260
4261         if (size)
4262                 *size = 0;
4263
4264         if (fd == -1) {
4265                 printf("%s: Failed to open %s\n", __func__, file_path);
4266                 return buf;
4267         }
4268
4269         if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) {
4270                 close(fd);
4271                 printf("%s: File operations failed\n", __func__);
4272                 return buf;
4273         }
4274
4275         pkg_size = st_buf.st_size;
4276         if (pkg_size < 0) {
4277                 close(fd);
4278                 printf("%s: File operations failed\n", __func__);
4279                 return buf;
4280         }
4281
4282         buf = (uint8_t *)malloc(pkg_size);
4283         if (!buf) {
4284                 close(fd);
4285                 printf("%s: Failed to malloc memory\n", __func__);
4286                 return buf;
4287         }
4288
4289         ret = read(fd, buf, pkg_size);
4290         if (ret < 0) {
4291                 close(fd);
4292                 printf("%s: File read operation failed\n", __func__);
4293                 close_file(buf);
4294                 return NULL;
4295         }
4296
4297         if (size)
4298                 *size = pkg_size;
4299
4300         close(fd);
4301
4302         return buf;
4303 }
4304
4305 int
4306 save_file(const char *file_path, uint8_t *buf, uint32_t size)
4307 {
4308         FILE *fh = fopen(file_path, "wb");
4309
4310         if (fh == NULL) {
4311                 printf("%s: Failed to open %s\n", __func__, file_path);
4312                 return -1;
4313         }
4314
4315         if (fwrite(buf, 1, size, fh) != size) {
4316                 fclose(fh);
4317                 printf("%s: File write operation failed\n", __func__);
4318                 return -1;
4319         }
4320
4321         fclose(fh);
4322
4323         return 0;
4324 }
4325
4326 int
4327 close_file(uint8_t *buf)
4328 {
4329         if (buf) {
4330                 free((void *)buf);
4331                 return 0;
4332         }
4333
4334         return -1;
4335 }
4336
4337 void
4338 port_queue_region_info_display(portid_t port_id, void *buf)
4339 {
4340 #ifdef RTE_LIBRTE_I40E_PMD
4341         uint16_t i, j;
4342         struct rte_pmd_i40e_queue_regions *info =
4343                 (struct rte_pmd_i40e_queue_regions *)buf;
4344         static const char *queue_region_info_stats_border = "-------";
4345
4346         if (!info->queue_region_number)
4347                 printf("there is no region has been set before");
4348
4349         printf("\n      %s All queue region info for port=%2d %s",
4350                         queue_region_info_stats_border, port_id,
4351                         queue_region_info_stats_border);
4352         printf("\n      queue_region_number: %-14u \n",
4353                         info->queue_region_number);
4354
4355         for (i = 0; i < info->queue_region_number; i++) {
4356                 printf("\n      region_id: %-14u queue_number: %-14u "
4357                         "queue_start_index: %-14u \n",
4358                         info->region[i].region_id,
4359                         info->region[i].queue_num,
4360                         info->region[i].queue_start_index);
4361
4362                 printf("  user_priority_num is  %-14u :",
4363                                         info->region[i].user_priority_num);
4364                 for (j = 0; j < info->region[i].user_priority_num; j++)
4365                         printf(" %-14u ", info->region[i].user_priority[j]);
4366
4367                 printf("\n      flowtype_num is  %-14u :",
4368                                 info->region[i].flowtype_num);
4369                 for (j = 0; j < info->region[i].flowtype_num; j++)
4370                         printf(" %-14u ", info->region[i].hw_flowtype[j]);
4371         }
4372 #else
4373         RTE_SET_USED(port_id);
4374         RTE_SET_USED(buf);
4375 #endif
4376
4377         printf("\n\n");
4378 }
4379
4380 void
4381 show_macs(portid_t port_id)
4382 {
4383         char buf[RTE_ETHER_ADDR_FMT_SIZE];
4384         struct rte_eth_dev_info dev_info;
4385         struct rte_ether_addr *addr;
4386         uint32_t i, num_macs = 0;
4387         struct rte_eth_dev *dev;
4388
4389         dev = &rte_eth_devices[port_id];
4390
4391         rte_eth_dev_info_get(port_id, &dev_info);
4392
4393         for (i = 0; i < dev_info.max_mac_addrs; i++) {
4394                 addr = &dev->data->mac_addrs[i];
4395
4396                 /* skip zero address */
4397                 if (rte_is_zero_ether_addr(addr))
4398                         continue;
4399
4400                 num_macs++;
4401         }
4402
4403         printf("Number of MAC address added: %d\n", num_macs);
4404
4405         for (i = 0; i < dev_info.max_mac_addrs; i++) {
4406                 addr = &dev->data->mac_addrs[i];
4407
4408                 /* skip zero address */
4409                 if (rte_is_zero_ether_addr(addr))
4410                         continue;
4411
4412                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
4413                 printf("  %s\n", buf);
4414         }
4415 }
4416
4417 void
4418 show_mcast_macs(portid_t port_id)
4419 {
4420         char buf[RTE_ETHER_ADDR_FMT_SIZE];
4421         struct rte_ether_addr *addr;
4422         struct rte_port *port;
4423         uint32_t i;
4424
4425         port = &ports[port_id];
4426
4427         printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb);
4428
4429         for (i = 0; i < port->mc_addr_nb; i++) {
4430                 addr = &port->mc_addr_pool[i];
4431
4432                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
4433                 printf("  %s\n", buf);
4434         }
4435 }