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