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