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