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