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