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