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