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