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