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