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