4d873cdde77bd2f98c9545f3e9e4f862158c9d24
[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(MPLS, sizeof(struct rte_flow_item_mpls)),
972         MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
973 };
974
975 /** Compute storage space needed by item specification. */
976 static void
977 flow_item_spec_size(const struct rte_flow_item *item,
978                     size_t *size, size_t *pad)
979 {
980         if (!item->spec)
981                 goto empty;
982         switch (item->type) {
983                 union {
984                         const struct rte_flow_item_raw *raw;
985                 } spec;
986
987         case RTE_FLOW_ITEM_TYPE_RAW:
988                 spec.raw = item->spec;
989                 *size = offsetof(struct rte_flow_item_raw, pattern) +
990                         spec.raw->length * sizeof(*spec.raw->pattern);
991                 break;
992         default:
993 empty:
994                 *size = 0;
995                 break;
996         }
997         *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
998 }
999
1000 /** Generate flow_action[] entry. */
1001 #define MK_FLOW_ACTION(t, s) \
1002         [RTE_FLOW_ACTION_TYPE_ ## t] = { \
1003                 .name = # t, \
1004                 .size = s, \
1005         }
1006
1007 /** Information about known flow actions. */
1008 static const struct {
1009         const char *name;
1010         size_t size;
1011 } flow_action[] = {
1012         MK_FLOW_ACTION(END, 0),
1013         MK_FLOW_ACTION(VOID, 0),
1014         MK_FLOW_ACTION(PASSTHRU, 0),
1015         MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1016         MK_FLOW_ACTION(FLAG, 0),
1017         MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
1018         MK_FLOW_ACTION(DROP, 0),
1019         MK_FLOW_ACTION(COUNT, 0),
1020         MK_FLOW_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
1021         MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)), /* +queue[] */
1022         MK_FLOW_ACTION(PF, 0),
1023         MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1024 };
1025
1026 /** Compute storage space needed by action configuration. */
1027 static void
1028 flow_action_conf_size(const struct rte_flow_action *action,
1029                       size_t *size, size_t *pad)
1030 {
1031         if (!action->conf)
1032                 goto empty;
1033         switch (action->type) {
1034                 union {
1035                         const struct rte_flow_action_rss *rss;
1036                 } conf;
1037
1038         case RTE_FLOW_ACTION_TYPE_RSS:
1039                 conf.rss = action->conf;
1040                 *size = offsetof(struct rte_flow_action_rss, queue) +
1041                         conf.rss->num * sizeof(*conf.rss->queue);
1042                 break;
1043         default:
1044 empty:
1045                 *size = 0;
1046                 break;
1047         }
1048         *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
1049 }
1050
1051 /** Generate a port_flow entry from attributes/pattern/actions. */
1052 static struct port_flow *
1053 port_flow_new(const struct rte_flow_attr *attr,
1054               const struct rte_flow_item *pattern,
1055               const struct rte_flow_action *actions)
1056 {
1057         const struct rte_flow_item *item;
1058         const struct rte_flow_action *action;
1059         struct port_flow *pf = NULL;
1060         size_t tmp;
1061         size_t pad;
1062         size_t off1 = 0;
1063         size_t off2 = 0;
1064         int err = ENOTSUP;
1065
1066 store:
1067         item = pattern;
1068         if (pf)
1069                 pf->pattern = (void *)&pf->data[off1];
1070         do {
1071                 struct rte_flow_item *dst = NULL;
1072
1073                 if ((unsigned int)item->type >= RTE_DIM(flow_item) ||
1074                     !flow_item[item->type].name)
1075                         goto notsup;
1076                 if (pf)
1077                         dst = memcpy(pf->data + off1, item, sizeof(*item));
1078                 off1 += sizeof(*item);
1079                 flow_item_spec_size(item, &tmp, &pad);
1080                 if (item->spec) {
1081                         if (pf)
1082                                 dst->spec = memcpy(pf->data + off2,
1083                                                    item->spec, tmp);
1084                         off2 += tmp + pad;
1085                 }
1086                 if (item->last) {
1087                         if (pf)
1088                                 dst->last = memcpy(pf->data + off2,
1089                                                    item->last, tmp);
1090                         off2 += tmp + pad;
1091                 }
1092                 if (item->mask) {
1093                         if (pf)
1094                                 dst->mask = memcpy(pf->data + off2,
1095                                                    item->mask, tmp);
1096                         off2 += tmp + pad;
1097                 }
1098                 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
1099         } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
1100         off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
1101         action = actions;
1102         if (pf)
1103                 pf->actions = (void *)&pf->data[off1];
1104         do {
1105                 struct rte_flow_action *dst = NULL;
1106
1107                 if ((unsigned int)action->type >= RTE_DIM(flow_action) ||
1108                     !flow_action[action->type].name)
1109                         goto notsup;
1110                 if (pf)
1111                         dst = memcpy(pf->data + off1, action, sizeof(*action));
1112                 off1 += sizeof(*action);
1113                 flow_action_conf_size(action, &tmp, &pad);
1114                 if (action->conf) {
1115                         if (pf)
1116                                 dst->conf = memcpy(pf->data + off2,
1117                                                    action->conf, tmp);
1118                         off2 += tmp + pad;
1119                 }
1120                 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
1121         } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
1122         if (pf != NULL)
1123                 return pf;
1124         off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
1125         tmp = RTE_ALIGN_CEIL(offsetof(struct port_flow, data), sizeof(double));
1126         pf = calloc(1, tmp + off1 + off2);
1127         if (pf == NULL)
1128                 err = errno;
1129         else {
1130                 *pf = (const struct port_flow){
1131                         .size = tmp + off1 + off2,
1132                         .attr = *attr,
1133                 };
1134                 tmp -= offsetof(struct port_flow, data);
1135                 off2 = tmp + off1;
1136                 off1 = tmp;
1137                 goto store;
1138         }
1139 notsup:
1140         rte_errno = err;
1141         return NULL;
1142 }
1143
1144 /** Print a message out of a flow error. */
1145 static int
1146 port_flow_complain(struct rte_flow_error *error)
1147 {
1148         static const char *const errstrlist[] = {
1149                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
1150                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
1151                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
1152                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
1153                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
1154                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
1155                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
1156                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
1157                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
1158                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
1159                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
1160                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
1161         };
1162         const char *errstr;
1163         char buf[32];
1164         int err = rte_errno;
1165
1166         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
1167             !errstrlist[error->type])
1168                 errstr = "unknown type";
1169         else
1170                 errstr = errstrlist[error->type];
1171         printf("Caught error type %d (%s): %s%s\n",
1172                error->type, errstr,
1173                error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
1174                                         error->cause), buf) : "",
1175                error->message ? error->message : "(no stated reason)");
1176         return -err;
1177 }
1178
1179 /** Validate flow rule. */
1180 int
1181 port_flow_validate(portid_t port_id,
1182                    const struct rte_flow_attr *attr,
1183                    const struct rte_flow_item *pattern,
1184                    const struct rte_flow_action *actions)
1185 {
1186         struct rte_flow_error error;
1187
1188         /* Poisoning to make sure PMDs update it in case of error. */
1189         memset(&error, 0x11, sizeof(error));
1190         if (rte_flow_validate(port_id, attr, pattern, actions, &error))
1191                 return port_flow_complain(&error);
1192         printf("Flow rule validated\n");
1193         return 0;
1194 }
1195
1196 /** Create flow rule. */
1197 int
1198 port_flow_create(portid_t port_id,
1199                  const struct rte_flow_attr *attr,
1200                  const struct rte_flow_item *pattern,
1201                  const struct rte_flow_action *actions)
1202 {
1203         struct rte_flow *flow;
1204         struct rte_port *port;
1205         struct port_flow *pf;
1206         uint32_t id;
1207         struct rte_flow_error error;
1208
1209         /* Poisoning to make sure PMDs update it in case of error. */
1210         memset(&error, 0x22, sizeof(error));
1211         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
1212         if (!flow)
1213                 return port_flow_complain(&error);
1214         port = &ports[port_id];
1215         if (port->flow_list) {
1216                 if (port->flow_list->id == UINT32_MAX) {
1217                         printf("Highest rule ID is already assigned, delete"
1218                                " it first");
1219                         rte_flow_destroy(port_id, flow, NULL);
1220                         return -ENOMEM;
1221                 }
1222                 id = port->flow_list->id + 1;
1223         } else
1224                 id = 0;
1225         pf = port_flow_new(attr, pattern, actions);
1226         if (!pf) {
1227                 int err = rte_errno;
1228
1229                 printf("Cannot allocate flow: %s\n", rte_strerror(err));
1230                 rte_flow_destroy(port_id, flow, NULL);
1231                 return -err;
1232         }
1233         pf->next = port->flow_list;
1234         pf->id = id;
1235         pf->flow = flow;
1236         port->flow_list = pf;
1237         printf("Flow rule #%u created\n", pf->id);
1238         return 0;
1239 }
1240
1241 /** Destroy a number of flow rules. */
1242 int
1243 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
1244 {
1245         struct rte_port *port;
1246         struct port_flow **tmp;
1247         uint32_t c = 0;
1248         int ret = 0;
1249
1250         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1251             port_id == (portid_t)RTE_PORT_ALL)
1252                 return -EINVAL;
1253         port = &ports[port_id];
1254         tmp = &port->flow_list;
1255         while (*tmp) {
1256                 uint32_t i;
1257
1258                 for (i = 0; i != n; ++i) {
1259                         struct rte_flow_error error;
1260                         struct port_flow *pf = *tmp;
1261
1262                         if (rule[i] != pf->id)
1263                                 continue;
1264                         /*
1265                          * Poisoning to make sure PMDs update it in case
1266                          * of error.
1267                          */
1268                         memset(&error, 0x33, sizeof(error));
1269                         if (rte_flow_destroy(port_id, pf->flow, &error)) {
1270                                 ret = port_flow_complain(&error);
1271                                 continue;
1272                         }
1273                         printf("Flow rule #%u destroyed\n", pf->id);
1274                         *tmp = pf->next;
1275                         free(pf);
1276                         break;
1277                 }
1278                 if (i == n)
1279                         tmp = &(*tmp)->next;
1280                 ++c;
1281         }
1282         return ret;
1283 }
1284
1285 /** Remove all flow rules. */
1286 int
1287 port_flow_flush(portid_t port_id)
1288 {
1289         struct rte_flow_error error;
1290         struct rte_port *port;
1291         int ret = 0;
1292
1293         /* Poisoning to make sure PMDs update it in case of error. */
1294         memset(&error, 0x44, sizeof(error));
1295         if (rte_flow_flush(port_id, &error)) {
1296                 ret = port_flow_complain(&error);
1297                 if (port_id_is_invalid(port_id, DISABLED_WARN) ||
1298                     port_id == (portid_t)RTE_PORT_ALL)
1299                         return ret;
1300         }
1301         port = &ports[port_id];
1302         while (port->flow_list) {
1303                 struct port_flow *pf = port->flow_list->next;
1304
1305                 free(port->flow_list);
1306                 port->flow_list = pf;
1307         }
1308         return ret;
1309 }
1310
1311 /** Query a flow rule. */
1312 int
1313 port_flow_query(portid_t port_id, uint32_t rule,
1314                 enum rte_flow_action_type action)
1315 {
1316         struct rte_flow_error error;
1317         struct rte_port *port;
1318         struct port_flow *pf;
1319         const char *name;
1320         union {
1321                 struct rte_flow_query_count count;
1322         } query;
1323
1324         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1325             port_id == (portid_t)RTE_PORT_ALL)
1326                 return -EINVAL;
1327         port = &ports[port_id];
1328         for (pf = port->flow_list; pf; pf = pf->next)
1329                 if (pf->id == rule)
1330                         break;
1331         if (!pf) {
1332                 printf("Flow rule #%u not found\n", rule);
1333                 return -ENOENT;
1334         }
1335         if ((unsigned int)action >= RTE_DIM(flow_action) ||
1336             !flow_action[action].name)
1337                 name = "unknown";
1338         else
1339                 name = flow_action[action].name;
1340         switch (action) {
1341         case RTE_FLOW_ACTION_TYPE_COUNT:
1342                 break;
1343         default:
1344                 printf("Cannot query action type %d (%s)\n", action, name);
1345                 return -ENOTSUP;
1346         }
1347         /* Poisoning to make sure PMDs update it in case of error. */
1348         memset(&error, 0x55, sizeof(error));
1349         memset(&query, 0, sizeof(query));
1350         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
1351                 return port_flow_complain(&error);
1352         switch (action) {
1353         case RTE_FLOW_ACTION_TYPE_COUNT:
1354                 printf("%s:\n"
1355                        " hits_set: %u\n"
1356                        " bytes_set: %u\n"
1357                        " hits: %" PRIu64 "\n"
1358                        " bytes: %" PRIu64 "\n",
1359                        name,
1360                        query.count.hits_set,
1361                        query.count.bytes_set,
1362                        query.count.hits,
1363                        query.count.bytes);
1364                 break;
1365         default:
1366                 printf("Cannot display result for action type %d (%s)\n",
1367                        action, name);
1368                 break;
1369         }
1370         return 0;
1371 }
1372
1373 /** List flow rules. */
1374 void
1375 port_flow_list(portid_t port_id, uint32_t n, const uint32_t group[n])
1376 {
1377         struct rte_port *port;
1378         struct port_flow *pf;
1379         struct port_flow *list = NULL;
1380         uint32_t i;
1381
1382         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1383             port_id == (portid_t)RTE_PORT_ALL)
1384                 return;
1385         port = &ports[port_id];
1386         if (!port->flow_list)
1387                 return;
1388         /* Sort flows by group, priority and ID. */
1389         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
1390                 struct port_flow **tmp;
1391
1392                 if (n) {
1393                         /* Filter out unwanted groups. */
1394                         for (i = 0; i != n; ++i)
1395                                 if (pf->attr.group == group[i])
1396                                         break;
1397                         if (i == n)
1398                                 continue;
1399                 }
1400                 tmp = &list;
1401                 while (*tmp &&
1402                        (pf->attr.group > (*tmp)->attr.group ||
1403                         (pf->attr.group == (*tmp)->attr.group &&
1404                          pf->attr.priority > (*tmp)->attr.priority) ||
1405                         (pf->attr.group == (*tmp)->attr.group &&
1406                          pf->attr.priority == (*tmp)->attr.priority &&
1407                          pf->id > (*tmp)->id)))
1408                         tmp = &(*tmp)->tmp;
1409                 pf->tmp = *tmp;
1410                 *tmp = pf;
1411         }
1412         printf("ID\tGroup\tPrio\tAttr\tRule\n");
1413         for (pf = list; pf != NULL; pf = pf->tmp) {
1414                 const struct rte_flow_item *item = pf->pattern;
1415                 const struct rte_flow_action *action = pf->actions;
1416
1417                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c\t",
1418                        pf->id,
1419                        pf->attr.group,
1420                        pf->attr.priority,
1421                        pf->attr.ingress ? 'i' : '-',
1422                        pf->attr.egress ? 'e' : '-');
1423                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
1424                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1425                                 printf("%s ", flow_item[item->type].name);
1426                         ++item;
1427                 }
1428                 printf("=>");
1429                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
1430                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
1431                                 printf(" %s", flow_action[action->type].name);
1432                         ++action;
1433                 }
1434                 printf("\n");
1435         }
1436 }
1437
1438 /*
1439  * RX/TX ring descriptors display functions.
1440  */
1441 int
1442 rx_queue_id_is_invalid(queueid_t rxq_id)
1443 {
1444         if (rxq_id < nb_rxq)
1445                 return 0;
1446         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
1447         return 1;
1448 }
1449
1450 int
1451 tx_queue_id_is_invalid(queueid_t txq_id)
1452 {
1453         if (txq_id < nb_txq)
1454                 return 0;
1455         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
1456         return 1;
1457 }
1458
1459 static int
1460 rx_desc_id_is_invalid(uint16_t rxdesc_id)
1461 {
1462         if (rxdesc_id < nb_rxd)
1463                 return 0;
1464         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
1465                rxdesc_id, nb_rxd);
1466         return 1;
1467 }
1468
1469 static int
1470 tx_desc_id_is_invalid(uint16_t txdesc_id)
1471 {
1472         if (txdesc_id < nb_txd)
1473                 return 0;
1474         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
1475                txdesc_id, nb_txd);
1476         return 1;
1477 }
1478
1479 static const struct rte_memzone *
1480 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
1481 {
1482         char mz_name[RTE_MEMZONE_NAMESIZE];
1483         const struct rte_memzone *mz;
1484
1485         snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
1486                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
1487         mz = rte_memzone_lookup(mz_name);
1488         if (mz == NULL)
1489                 printf("%s ring memory zoneof (port %d, queue %d) not"
1490                        "found (zone name = %s\n",
1491                        ring_name, port_id, q_id, mz_name);
1492         return mz;
1493 }
1494
1495 union igb_ring_dword {
1496         uint64_t dword;
1497         struct {
1498 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1499                 uint32_t lo;
1500                 uint32_t hi;
1501 #else
1502                 uint32_t hi;
1503                 uint32_t lo;
1504 #endif
1505         } words;
1506 };
1507
1508 struct igb_ring_desc_32_bytes {
1509         union igb_ring_dword lo_dword;
1510         union igb_ring_dword hi_dword;
1511         union igb_ring_dword resv1;
1512         union igb_ring_dword resv2;
1513 };
1514
1515 struct igb_ring_desc_16_bytes {
1516         union igb_ring_dword lo_dword;
1517         union igb_ring_dword hi_dword;
1518 };
1519
1520 static void
1521 ring_rxd_display_dword(union igb_ring_dword dword)
1522 {
1523         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
1524                                         (unsigned)dword.words.hi);
1525 }
1526
1527 static void
1528 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
1529 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1530                            uint8_t port_id,
1531 #else
1532                            __rte_unused uint8_t port_id,
1533 #endif
1534                            uint16_t desc_id)
1535 {
1536         struct igb_ring_desc_16_bytes *ring =
1537                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1538 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1539         struct rte_eth_dev_info dev_info;
1540
1541         memset(&dev_info, 0, sizeof(dev_info));
1542         rte_eth_dev_info_get(port_id, &dev_info);
1543         if (strstr(dev_info.driver_name, "i40e") != NULL) {
1544                 /* 32 bytes RX descriptor, i40e only */
1545                 struct igb_ring_desc_32_bytes *ring =
1546                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
1547                 ring[desc_id].lo_dword.dword =
1548                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1549                 ring_rxd_display_dword(ring[desc_id].lo_dword);
1550                 ring[desc_id].hi_dword.dword =
1551                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1552                 ring_rxd_display_dword(ring[desc_id].hi_dword);
1553                 ring[desc_id].resv1.dword =
1554                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
1555                 ring_rxd_display_dword(ring[desc_id].resv1);
1556                 ring[desc_id].resv2.dword =
1557                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
1558                 ring_rxd_display_dword(ring[desc_id].resv2);
1559
1560                 return;
1561         }
1562 #endif
1563         /* 16 bytes RX descriptor */
1564         ring[desc_id].lo_dword.dword =
1565                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1566         ring_rxd_display_dword(ring[desc_id].lo_dword);
1567         ring[desc_id].hi_dword.dword =
1568                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1569         ring_rxd_display_dword(ring[desc_id].hi_dword);
1570 }
1571
1572 static void
1573 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
1574 {
1575         struct igb_ring_desc_16_bytes *ring;
1576         struct igb_ring_desc_16_bytes txd;
1577
1578         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1579         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1580         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1581         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
1582                         (unsigned)txd.lo_dword.words.lo,
1583                         (unsigned)txd.lo_dword.words.hi,
1584                         (unsigned)txd.hi_dword.words.lo,
1585                         (unsigned)txd.hi_dword.words.hi);
1586 }
1587
1588 void
1589 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
1590 {
1591         const struct rte_memzone *rx_mz;
1592
1593         if (port_id_is_invalid(port_id, ENABLED_WARN))
1594                 return;
1595         if (rx_queue_id_is_invalid(rxq_id))
1596                 return;
1597         if (rx_desc_id_is_invalid(rxd_id))
1598                 return;
1599         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
1600         if (rx_mz == NULL)
1601                 return;
1602         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
1603 }
1604
1605 void
1606 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
1607 {
1608         const struct rte_memzone *tx_mz;
1609
1610         if (port_id_is_invalid(port_id, ENABLED_WARN))
1611                 return;
1612         if (tx_queue_id_is_invalid(txq_id))
1613                 return;
1614         if (tx_desc_id_is_invalid(txd_id))
1615                 return;
1616         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
1617         if (tx_mz == NULL)
1618                 return;
1619         ring_tx_descriptor_display(tx_mz, txd_id);
1620 }
1621
1622 void
1623 fwd_lcores_config_display(void)
1624 {
1625         lcoreid_t lc_id;
1626
1627         printf("List of forwarding lcores:");
1628         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
1629                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
1630         printf("\n");
1631 }
1632 void
1633 rxtx_config_display(void)
1634 {
1635         printf("  %s packet forwarding%s - CRC stripping %s - "
1636                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
1637                retry_enabled == 0 ? "" : " with retry",
1638                rx_mode.hw_strip_crc ? "enabled" : "disabled",
1639                nb_pkt_per_burst);
1640
1641         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
1642                 printf("  packet len=%u - nb packet segments=%d\n",
1643                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
1644
1645         struct rte_eth_rxconf *rx_conf = &ports[0].rx_conf;
1646         struct rte_eth_txconf *tx_conf = &ports[0].tx_conf;
1647
1648         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
1649                nb_fwd_lcores, nb_fwd_ports);
1650         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
1651                nb_rxq, nb_rxd, rx_conf->rx_free_thresh);
1652         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
1653                rx_conf->rx_thresh.pthresh, rx_conf->rx_thresh.hthresh,
1654                rx_conf->rx_thresh.wthresh);
1655         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
1656                nb_txq, nb_txd, tx_conf->tx_free_thresh);
1657         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
1658                tx_conf->tx_thresh.pthresh, tx_conf->tx_thresh.hthresh,
1659                tx_conf->tx_thresh.wthresh);
1660         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
1661                tx_conf->tx_rs_thresh, tx_conf->txq_flags);
1662 }
1663
1664 void
1665 port_rss_reta_info(portid_t port_id,
1666                    struct rte_eth_rss_reta_entry64 *reta_conf,
1667                    uint16_t nb_entries)
1668 {
1669         uint16_t i, idx, shift;
1670         int ret;
1671
1672         if (port_id_is_invalid(port_id, ENABLED_WARN))
1673                 return;
1674
1675         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
1676         if (ret != 0) {
1677                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
1678                 return;
1679         }
1680
1681         for (i = 0; i < nb_entries; i++) {
1682                 idx = i / RTE_RETA_GROUP_SIZE;
1683                 shift = i % RTE_RETA_GROUP_SIZE;
1684                 if (!(reta_conf[idx].mask & (1ULL << shift)))
1685                         continue;
1686                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
1687                                         i, reta_conf[idx].reta[shift]);
1688         }
1689 }
1690
1691 /*
1692  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
1693  * key of the port.
1694  */
1695 void
1696 port_rss_hash_conf_show(portid_t port_id, char rss_info[], int show_rss_key)
1697 {
1698         struct rte_eth_rss_conf rss_conf;
1699         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
1700         uint64_t rss_hf;
1701         uint8_t i;
1702         int diag;
1703         struct rte_eth_dev_info dev_info;
1704         uint8_t hash_key_size;
1705
1706         if (port_id_is_invalid(port_id, ENABLED_WARN))
1707                 return;
1708
1709         memset(&dev_info, 0, sizeof(dev_info));
1710         rte_eth_dev_info_get(port_id, &dev_info);
1711         if (dev_info.hash_key_size > 0 &&
1712                         dev_info.hash_key_size <= sizeof(rss_key))
1713                 hash_key_size = dev_info.hash_key_size;
1714         else {
1715                 printf("dev_info did not provide a valid hash key size\n");
1716                 return;
1717         }
1718
1719         rss_conf.rss_hf = 0;
1720         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1721                 if (!strcmp(rss_info, rss_type_table[i].str))
1722                         rss_conf.rss_hf = rss_type_table[i].rss_type;
1723         }
1724
1725         /* Get RSS hash key if asked to display it */
1726         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
1727         rss_conf.rss_key_len = hash_key_size;
1728         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
1729         if (diag != 0) {
1730                 switch (diag) {
1731                 case -ENODEV:
1732                         printf("port index %d invalid\n", port_id);
1733                         break;
1734                 case -ENOTSUP:
1735                         printf("operation not supported by device\n");
1736                         break;
1737                 default:
1738                         printf("operation failed - diag=%d\n", diag);
1739                         break;
1740                 }
1741                 return;
1742         }
1743         rss_hf = rss_conf.rss_hf;
1744         if (rss_hf == 0) {
1745                 printf("RSS disabled\n");
1746                 return;
1747         }
1748         printf("RSS functions:\n ");
1749         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1750                 if (rss_hf & rss_type_table[i].rss_type)
1751                         printf("%s ", rss_type_table[i].str);
1752         }
1753         printf("\n");
1754         if (!show_rss_key)
1755                 return;
1756         printf("RSS key:\n");
1757         for (i = 0; i < hash_key_size; i++)
1758                 printf("%02X", rss_key[i]);
1759         printf("\n");
1760 }
1761
1762 void
1763 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
1764                          uint hash_key_len)
1765 {
1766         struct rte_eth_rss_conf rss_conf;
1767         int diag;
1768         unsigned int i;
1769
1770         rss_conf.rss_key = NULL;
1771         rss_conf.rss_key_len = hash_key_len;
1772         rss_conf.rss_hf = 0;
1773         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1774                 if (!strcmp(rss_type_table[i].str, rss_type))
1775                         rss_conf.rss_hf = rss_type_table[i].rss_type;
1776         }
1777         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
1778         if (diag == 0) {
1779                 rss_conf.rss_key = hash_key;
1780                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
1781         }
1782         if (diag == 0)
1783                 return;
1784
1785         switch (diag) {
1786         case -ENODEV:
1787                 printf("port index %d invalid\n", port_id);
1788                 break;
1789         case -ENOTSUP:
1790                 printf("operation not supported by device\n");
1791                 break;
1792         default:
1793                 printf("operation failed - diag=%d\n", diag);
1794                 break;
1795         }
1796 }
1797
1798 /*
1799  * Setup forwarding configuration for each logical core.
1800  */
1801 static void
1802 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
1803 {
1804         streamid_t nb_fs_per_lcore;
1805         streamid_t nb_fs;
1806         streamid_t sm_id;
1807         lcoreid_t  nb_extra;
1808         lcoreid_t  nb_fc;
1809         lcoreid_t  nb_lc;
1810         lcoreid_t  lc_id;
1811
1812         nb_fs = cfg->nb_fwd_streams;
1813         nb_fc = cfg->nb_fwd_lcores;
1814         if (nb_fs <= nb_fc) {
1815                 nb_fs_per_lcore = 1;
1816                 nb_extra = 0;
1817         } else {
1818                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
1819                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
1820         }
1821
1822         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
1823         sm_id = 0;
1824         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
1825                 fwd_lcores[lc_id]->stream_idx = sm_id;
1826                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
1827                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1828         }
1829
1830         /*
1831          * Assign extra remaining streams, if any.
1832          */
1833         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
1834         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
1835                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
1836                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
1837                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1838         }
1839 }
1840
1841 static void
1842 simple_fwd_config_setup(void)
1843 {
1844         portid_t i;
1845         portid_t j;
1846         portid_t inc = 2;
1847
1848         if (port_topology == PORT_TOPOLOGY_CHAINED ||
1849             port_topology == PORT_TOPOLOGY_LOOP) {
1850                 inc = 1;
1851         } else if (nb_fwd_ports % 2) {
1852                 printf("\nWarning! Cannot handle an odd number of ports "
1853                        "with the current port topology. Configuration "
1854                        "must be changed to have an even number of ports, "
1855                        "or relaunch application with "
1856                        "--port-topology=chained\n\n");
1857         }
1858
1859         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
1860         cur_fwd_config.nb_fwd_streams =
1861                 (streamid_t) cur_fwd_config.nb_fwd_ports;
1862
1863         /* reinitialize forwarding streams */
1864         init_fwd_streams();
1865
1866         /*
1867          * In the simple forwarding test, the number of forwarding cores
1868          * must be lower or equal to the number of forwarding ports.
1869          */
1870         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1871         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
1872                 cur_fwd_config.nb_fwd_lcores =
1873                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
1874         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1875
1876         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
1877                 if (port_topology != PORT_TOPOLOGY_LOOP)
1878                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
1879                 else
1880                         j = i;
1881                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
1882                 fwd_streams[i]->rx_queue  = 0;
1883                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
1884                 fwd_streams[i]->tx_queue  = 0;
1885                 fwd_streams[i]->peer_addr = j;
1886                 fwd_streams[i]->retry_enabled = retry_enabled;
1887
1888                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
1889                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
1890                         fwd_streams[j]->rx_queue  = 0;
1891                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
1892                         fwd_streams[j]->tx_queue  = 0;
1893                         fwd_streams[j]->peer_addr = i;
1894                         fwd_streams[j]->retry_enabled = retry_enabled;
1895                 }
1896         }
1897 }
1898
1899 /**
1900  * For the RSS forwarding test all streams distributed over lcores. Each stream
1901  * being composed of a RX queue to poll on a RX port for input messages,
1902  * associated with a TX queue of a TX port where to send forwarded packets.
1903  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1904  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1905  * following rules:
1906  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1907  *    - TxQl = RxQj
1908  */
1909 static void
1910 rss_fwd_config_setup(void)
1911 {
1912         portid_t   rxp;
1913         portid_t   txp;
1914         queueid_t  rxq;
1915         queueid_t  nb_q;
1916         streamid_t  sm_id;
1917
1918         nb_q = nb_rxq;
1919         if (nb_q > nb_txq)
1920                 nb_q = nb_txq;
1921         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1922         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1923         cur_fwd_config.nb_fwd_streams =
1924                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1925
1926         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1927                 cur_fwd_config.nb_fwd_lcores =
1928                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1929
1930         /* reinitialize forwarding streams */
1931         init_fwd_streams();
1932
1933         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1934         rxp = 0; rxq = 0;
1935         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1936                 struct fwd_stream *fs;
1937
1938                 fs = fwd_streams[sm_id];
1939
1940                 if ((rxp & 0x1) == 0)
1941                         txp = (portid_t) (rxp + 1);
1942                 else
1943                         txp = (portid_t) (rxp - 1);
1944                 /*
1945                  * if we are in loopback, simply send stuff out through the
1946                  * ingress port
1947                  */
1948                 if (port_topology == PORT_TOPOLOGY_LOOP)
1949                         txp = rxp;
1950
1951                 fs->rx_port = fwd_ports_ids[rxp];
1952                 fs->rx_queue = rxq;
1953                 fs->tx_port = fwd_ports_ids[txp];
1954                 fs->tx_queue = rxq;
1955                 fs->peer_addr = fs->tx_port;
1956                 fs->retry_enabled = retry_enabled;
1957                 rxq = (queueid_t) (rxq + 1);
1958                 if (rxq < nb_q)
1959                         continue;
1960                 /*
1961                  * rxq == nb_q
1962                  * Restart from RX queue 0 on next RX port
1963                  */
1964                 rxq = 0;
1965                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1966                         rxp = (portid_t)
1967                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1968                 else
1969                         rxp = (portid_t) (rxp + 1);
1970         }
1971 }
1972
1973 /**
1974  * For the DCB forwarding test, each core is assigned on each traffic class.
1975  *
1976  * Each core is assigned a multi-stream, each stream being composed of
1977  * a RX queue to poll on a RX port for input messages, associated with
1978  * a TX queue of a TX port where to send forwarded packets. All RX and
1979  * TX queues are mapping to the same traffic class.
1980  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
1981  * the same core
1982  */
1983 static void
1984 dcb_fwd_config_setup(void)
1985 {
1986         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
1987         portid_t txp, rxp = 0;
1988         queueid_t txq, rxq = 0;
1989         lcoreid_t  lc_id;
1990         uint16_t nb_rx_queue, nb_tx_queue;
1991         uint16_t i, j, k, sm_id = 0;
1992         uint8_t tc = 0;
1993
1994         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1995         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1996         cur_fwd_config.nb_fwd_streams =
1997                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1998
1999         /* reinitialize forwarding streams */
2000         init_fwd_streams();
2001         sm_id = 0;
2002         txp = 1;
2003         /* get the dcb info on the first RX and TX ports */
2004         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2005         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2006
2007         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2008                 fwd_lcores[lc_id]->stream_nb = 0;
2009                 fwd_lcores[lc_id]->stream_idx = sm_id;
2010                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
2011                         /* if the nb_queue is zero, means this tc is
2012                          * not enabled on the POOL
2013                          */
2014                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
2015                                 break;
2016                         k = fwd_lcores[lc_id]->stream_nb +
2017                                 fwd_lcores[lc_id]->stream_idx;
2018                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
2019                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
2020                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2021                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
2022                         for (j = 0; j < nb_rx_queue; j++) {
2023                                 struct fwd_stream *fs;
2024
2025                                 fs = fwd_streams[k + j];
2026                                 fs->rx_port = fwd_ports_ids[rxp];
2027                                 fs->rx_queue = rxq + j;
2028                                 fs->tx_port = fwd_ports_ids[txp];
2029                                 fs->tx_queue = txq + j % nb_tx_queue;
2030                                 fs->peer_addr = fs->tx_port;
2031                                 fs->retry_enabled = retry_enabled;
2032                         }
2033                         fwd_lcores[lc_id]->stream_nb +=
2034                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2035                 }
2036                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
2037
2038                 tc++;
2039                 if (tc < rxp_dcb_info.nb_tcs)
2040                         continue;
2041                 /* Restart from TC 0 on next RX port */
2042                 tc = 0;
2043                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
2044                         rxp = (portid_t)
2045                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
2046                 else
2047                         rxp++;
2048                 if (rxp >= nb_fwd_ports)
2049                         return;
2050                 /* get the dcb information on next RX and TX ports */
2051                 if ((rxp & 0x1) == 0)
2052                         txp = (portid_t) (rxp + 1);
2053                 else
2054                         txp = (portid_t) (rxp - 1);
2055                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2056                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2057         }
2058 }
2059
2060 static void
2061 icmp_echo_config_setup(void)
2062 {
2063         portid_t  rxp;
2064         queueid_t rxq;
2065         lcoreid_t lc_id;
2066         uint16_t  sm_id;
2067
2068         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
2069                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
2070                         (nb_txq * nb_fwd_ports);
2071         else
2072                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2073         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2074         cur_fwd_config.nb_fwd_streams =
2075                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2076         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2077                 cur_fwd_config.nb_fwd_lcores =
2078                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2079         if (verbose_level > 0) {
2080                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
2081                        __FUNCTION__,
2082                        cur_fwd_config.nb_fwd_lcores,
2083                        cur_fwd_config.nb_fwd_ports,
2084                        cur_fwd_config.nb_fwd_streams);
2085         }
2086
2087         /* reinitialize forwarding streams */
2088         init_fwd_streams();
2089         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2090         rxp = 0; rxq = 0;
2091         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2092                 if (verbose_level > 0)
2093                         printf("  core=%d: \n", lc_id);
2094                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2095                         struct fwd_stream *fs;
2096                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2097                         fs->rx_port = fwd_ports_ids[rxp];
2098                         fs->rx_queue = rxq;
2099                         fs->tx_port = fs->rx_port;
2100                         fs->tx_queue = rxq;
2101                         fs->peer_addr = fs->tx_port;
2102                         fs->retry_enabled = retry_enabled;
2103                         if (verbose_level > 0)
2104                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
2105                                        sm_id, fs->rx_port, fs->rx_queue,
2106                                        fs->tx_queue);
2107                         rxq = (queueid_t) (rxq + 1);
2108                         if (rxq == nb_rxq) {
2109                                 rxq = 0;
2110                                 rxp = (portid_t) (rxp + 1);
2111                         }
2112                 }
2113         }
2114 }
2115
2116 void
2117 fwd_config_setup(void)
2118 {
2119         cur_fwd_config.fwd_eng = cur_fwd_eng;
2120         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
2121                 icmp_echo_config_setup();
2122                 return;
2123         }
2124         if ((nb_rxq > 1) && (nb_txq > 1)){
2125                 if (dcb_config)
2126                         dcb_fwd_config_setup();
2127                 else
2128                         rss_fwd_config_setup();
2129         }
2130         else
2131                 simple_fwd_config_setup();
2132 }
2133
2134 void
2135 pkt_fwd_config_display(struct fwd_config *cfg)
2136 {
2137         struct fwd_stream *fs;
2138         lcoreid_t  lc_id;
2139         streamid_t sm_id;
2140
2141         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
2142                 "NUMA support %s, MP over anonymous pages %s\n",
2143                 cfg->fwd_eng->fwd_mode_name,
2144                 retry_enabled == 0 ? "" : " with retry",
2145                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
2146                 numa_support == 1 ? "enabled" : "disabled",
2147                 mp_anon != 0 ? "enabled" : "disabled");
2148
2149         if (retry_enabled)
2150                 printf("TX retry num: %u, delay between TX retries: %uus\n",
2151                         burst_tx_retry_num, burst_tx_delay_time);
2152         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
2153                 printf("Logical Core %u (socket %u) forwards packets on "
2154                        "%d streams:",
2155                        fwd_lcores_cpuids[lc_id],
2156                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
2157                        fwd_lcores[lc_id]->stream_nb);
2158                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2159                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2160                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
2161                                "P=%d/Q=%d (socket %u) ",
2162                                fs->rx_port, fs->rx_queue,
2163                                ports[fs->rx_port].socket_id,
2164                                fs->tx_port, fs->tx_queue,
2165                                ports[fs->tx_port].socket_id);
2166                         print_ethaddr("peer=",
2167                                       &peer_eth_addrs[fs->peer_addr]);
2168                 }
2169                 printf("\n");
2170         }
2171         printf("\n");
2172 }
2173
2174 int
2175 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
2176 {
2177         unsigned int i;
2178         unsigned int lcore_cpuid;
2179         int record_now;
2180
2181         record_now = 0;
2182  again:
2183         for (i = 0; i < nb_lc; i++) {
2184                 lcore_cpuid = lcorelist[i];
2185                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
2186                         printf("lcore %u not enabled\n", lcore_cpuid);
2187                         return -1;
2188                 }
2189                 if (lcore_cpuid == rte_get_master_lcore()) {
2190                         printf("lcore %u cannot be masked on for running "
2191                                "packet forwarding, which is the master lcore "
2192                                "and reserved for command line parsing only\n",
2193                                lcore_cpuid);
2194                         return -1;
2195                 }
2196                 if (record_now)
2197                         fwd_lcores_cpuids[i] = lcore_cpuid;
2198         }
2199         if (record_now == 0) {
2200                 record_now = 1;
2201                 goto again;
2202         }
2203         nb_cfg_lcores = (lcoreid_t) nb_lc;
2204         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
2205                 printf("previous number of forwarding cores %u - changed to "
2206                        "number of configured cores %u\n",
2207                        (unsigned int) nb_fwd_lcores, nb_lc);
2208                 nb_fwd_lcores = (lcoreid_t) nb_lc;
2209         }
2210
2211         return 0;
2212 }
2213
2214 int
2215 set_fwd_lcores_mask(uint64_t lcoremask)
2216 {
2217         unsigned int lcorelist[64];
2218         unsigned int nb_lc;
2219         unsigned int i;
2220
2221         if (lcoremask == 0) {
2222                 printf("Invalid NULL mask of cores\n");
2223                 return -1;
2224         }
2225         nb_lc = 0;
2226         for (i = 0; i < 64; i++) {
2227                 if (! ((uint64_t)(1ULL << i) & lcoremask))
2228                         continue;
2229                 lcorelist[nb_lc++] = i;
2230         }
2231         return set_fwd_lcores_list(lcorelist, nb_lc);
2232 }
2233
2234 void
2235 set_fwd_lcores_number(uint16_t nb_lc)
2236 {
2237         if (nb_lc > nb_cfg_lcores) {
2238                 printf("nb fwd cores %u > %u (max. number of configured "
2239                        "lcores) - ignored\n",
2240                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
2241                 return;
2242         }
2243         nb_fwd_lcores = (lcoreid_t) nb_lc;
2244         printf("Number of forwarding cores set to %u\n",
2245                (unsigned int) nb_fwd_lcores);
2246 }
2247
2248 void
2249 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
2250 {
2251         unsigned int i;
2252         portid_t port_id;
2253         int record_now;
2254
2255         record_now = 0;
2256  again:
2257         for (i = 0; i < nb_pt; i++) {
2258                 port_id = (portid_t) portlist[i];
2259                 if (port_id_is_invalid(port_id, ENABLED_WARN))
2260                         return;
2261                 if (record_now)
2262                         fwd_ports_ids[i] = port_id;
2263         }
2264         if (record_now == 0) {
2265                 record_now = 1;
2266                 goto again;
2267         }
2268         nb_cfg_ports = (portid_t) nb_pt;
2269         if (nb_fwd_ports != (portid_t) nb_pt) {
2270                 printf("previous number of forwarding ports %u - changed to "
2271                        "number of configured ports %u\n",
2272                        (unsigned int) nb_fwd_ports, nb_pt);
2273                 nb_fwd_ports = (portid_t) nb_pt;
2274         }
2275 }
2276
2277 void
2278 set_fwd_ports_mask(uint64_t portmask)
2279 {
2280         unsigned int portlist[64];
2281         unsigned int nb_pt;
2282         unsigned int i;
2283
2284         if (portmask == 0) {
2285                 printf("Invalid NULL mask of ports\n");
2286                 return;
2287         }
2288         nb_pt = 0;
2289         RTE_ETH_FOREACH_DEV(i) {
2290                 if (! ((uint64_t)(1ULL << i) & portmask))
2291                         continue;
2292                 portlist[nb_pt++] = i;
2293         }
2294         set_fwd_ports_list(portlist, nb_pt);
2295 }
2296
2297 void
2298 set_fwd_ports_number(uint16_t nb_pt)
2299 {
2300         if (nb_pt > nb_cfg_ports) {
2301                 printf("nb fwd ports %u > %u (number of configured "
2302                        "ports) - ignored\n",
2303                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
2304                 return;
2305         }
2306         nb_fwd_ports = (portid_t) nb_pt;
2307         printf("Number of forwarding ports set to %u\n",
2308                (unsigned int) nb_fwd_ports);
2309 }
2310
2311 int
2312 port_is_forwarding(portid_t port_id)
2313 {
2314         unsigned int i;
2315
2316         if (port_id_is_invalid(port_id, ENABLED_WARN))
2317                 return -1;
2318
2319         for (i = 0; i < nb_fwd_ports; i++) {
2320                 if (fwd_ports_ids[i] == port_id)
2321                         return 1;
2322         }
2323
2324         return 0;
2325 }
2326
2327 void
2328 set_nb_pkt_per_burst(uint16_t nb)
2329 {
2330         if (nb > MAX_PKT_BURST) {
2331                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
2332                        " ignored\n",
2333                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
2334                 return;
2335         }
2336         nb_pkt_per_burst = nb;
2337         printf("Number of packets per burst set to %u\n",
2338                (unsigned int) nb_pkt_per_burst);
2339 }
2340
2341 static const char *
2342 tx_split_get_name(enum tx_pkt_split split)
2343 {
2344         uint32_t i;
2345
2346         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2347                 if (tx_split_name[i].split == split)
2348                         return tx_split_name[i].name;
2349         }
2350         return NULL;
2351 }
2352
2353 void
2354 set_tx_pkt_split(const char *name)
2355 {
2356         uint32_t i;
2357
2358         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2359                 if (strcmp(tx_split_name[i].name, name) == 0) {
2360                         tx_pkt_split = tx_split_name[i].split;
2361                         return;
2362                 }
2363         }
2364         printf("unknown value: \"%s\"\n", name);
2365 }
2366
2367 void
2368 show_tx_pkt_segments(void)
2369 {
2370         uint32_t i, n;
2371         const char *split;
2372
2373         n = tx_pkt_nb_segs;
2374         split = tx_split_get_name(tx_pkt_split);
2375
2376         printf("Number of segments: %u\n", n);
2377         printf("Segment sizes: ");
2378         for (i = 0; i != n - 1; i++)
2379                 printf("%hu,", tx_pkt_seg_lengths[i]);
2380         printf("%hu\n", tx_pkt_seg_lengths[i]);
2381         printf("Split packet: %s\n", split);
2382 }
2383
2384 void
2385 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
2386 {
2387         uint16_t tx_pkt_len;
2388         unsigned i;
2389
2390         if (nb_segs >= (unsigned) nb_txd) {
2391                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
2392                        nb_segs, (unsigned int) nb_txd);
2393                 return;
2394         }
2395
2396         /*
2397          * Check that each segment length is greater or equal than
2398          * the mbuf data sise.
2399          * Check also that the total packet length is greater or equal than the
2400          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
2401          */
2402         tx_pkt_len = 0;
2403         for (i = 0; i < nb_segs; i++) {
2404                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
2405                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
2406                                i, seg_lengths[i], (unsigned) mbuf_data_size);
2407                         return;
2408                 }
2409                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
2410         }
2411         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
2412                 printf("total packet length=%u < %d - give up\n",
2413                                 (unsigned) tx_pkt_len,
2414                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
2415                 return;
2416         }
2417
2418         for (i = 0; i < nb_segs; i++)
2419                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
2420
2421         tx_pkt_length  = tx_pkt_len;
2422         tx_pkt_nb_segs = (uint8_t) nb_segs;
2423 }
2424
2425 char*
2426 list_pkt_forwarding_modes(void)
2427 {
2428         static char fwd_modes[128] = "";
2429         const char *separator = "|";
2430         struct fwd_engine *fwd_eng;
2431         unsigned i = 0;
2432
2433         if (strlen (fwd_modes) == 0) {
2434                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
2435                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
2436                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
2437                         strncat(fwd_modes, separator,
2438                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
2439                 }
2440                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
2441         }
2442
2443         return fwd_modes;
2444 }
2445
2446 char*
2447 list_pkt_forwarding_retry_modes(void)
2448 {
2449         static char fwd_modes[128] = "";
2450         const char *separator = "|";
2451         struct fwd_engine *fwd_eng;
2452         unsigned i = 0;
2453
2454         if (strlen(fwd_modes) == 0) {
2455                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
2456                         if (fwd_eng == &rx_only_engine)
2457                                 continue;
2458                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
2459                                         sizeof(fwd_modes) -
2460                                         strlen(fwd_modes) - 1);
2461                         strncat(fwd_modes, separator,
2462                                         sizeof(fwd_modes) -
2463                                         strlen(fwd_modes) - 1);
2464                 }
2465                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
2466         }
2467
2468         return fwd_modes;
2469 }
2470
2471 void
2472 set_pkt_forwarding_mode(const char *fwd_mode_name)
2473 {
2474         struct fwd_engine *fwd_eng;
2475         unsigned i;
2476
2477         i = 0;
2478         while ((fwd_eng = fwd_engines[i]) != NULL) {
2479                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
2480                         printf("Set %s packet forwarding mode%s\n",
2481                                fwd_mode_name,
2482                                retry_enabled == 0 ? "" : " with retry");
2483                         cur_fwd_eng = fwd_eng;
2484                         return;
2485                 }
2486                 i++;
2487         }
2488         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
2489 }
2490
2491 void
2492 set_verbose_level(uint16_t vb_level)
2493 {
2494         printf("Change verbose level from %u to %u\n",
2495                (unsigned int) verbose_level, (unsigned int) vb_level);
2496         verbose_level = vb_level;
2497 }
2498
2499 void
2500 vlan_extend_set(portid_t port_id, int on)
2501 {
2502         int diag;
2503         int vlan_offload;
2504
2505         if (port_id_is_invalid(port_id, ENABLED_WARN))
2506                 return;
2507
2508         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2509
2510         if (on)
2511                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
2512         else
2513                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
2514
2515         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2516         if (diag < 0)
2517                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
2518                "diag=%d\n", port_id, on, diag);
2519 }
2520
2521 void
2522 rx_vlan_strip_set(portid_t port_id, int on)
2523 {
2524         int diag;
2525         int vlan_offload;
2526
2527         if (port_id_is_invalid(port_id, ENABLED_WARN))
2528                 return;
2529
2530         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2531
2532         if (on)
2533                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
2534         else
2535                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
2536
2537         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2538         if (diag < 0)
2539                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
2540                "diag=%d\n", port_id, on, diag);
2541 }
2542
2543 void
2544 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
2545 {
2546         int diag;
2547
2548         if (port_id_is_invalid(port_id, ENABLED_WARN))
2549                 return;
2550
2551         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
2552         if (diag < 0)
2553                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
2554                "diag=%d\n", port_id, queue_id, on, diag);
2555 }
2556
2557 void
2558 rx_vlan_filter_set(portid_t port_id, int on)
2559 {
2560         int diag;
2561         int vlan_offload;
2562
2563         if (port_id_is_invalid(port_id, ENABLED_WARN))
2564                 return;
2565
2566         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2567
2568         if (on)
2569                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
2570         else
2571                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
2572
2573         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2574         if (diag < 0)
2575                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
2576                "diag=%d\n", port_id, on, diag);
2577 }
2578
2579 int
2580 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
2581 {
2582         int diag;
2583
2584         if (port_id_is_invalid(port_id, ENABLED_WARN))
2585                 return 1;
2586         if (vlan_id_is_invalid(vlan_id))
2587                 return 1;
2588         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
2589         if (diag == 0)
2590                 return 0;
2591         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
2592                "diag=%d\n",
2593                port_id, vlan_id, on, diag);
2594         return -1;
2595 }
2596
2597 void
2598 rx_vlan_all_filter_set(portid_t port_id, int on)
2599 {
2600         uint16_t vlan_id;
2601
2602         if (port_id_is_invalid(port_id, ENABLED_WARN))
2603                 return;
2604         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
2605                 if (rx_vft_set(port_id, vlan_id, on))
2606                         break;
2607         }
2608 }
2609
2610 void
2611 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
2612 {
2613         int diag;
2614
2615         if (port_id_is_invalid(port_id, ENABLED_WARN))
2616                 return;
2617
2618         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
2619         if (diag == 0)
2620                 return;
2621
2622         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
2623                "diag=%d\n",
2624                port_id, vlan_type, tp_id, diag);
2625 }
2626
2627 void
2628 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
2629 {
2630         int vlan_offload;
2631         if (port_id_is_invalid(port_id, ENABLED_WARN))
2632                 return;
2633         if (vlan_id_is_invalid(vlan_id))
2634                 return;
2635
2636         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2637         if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD) {
2638                 printf("Error, as QinQ has been enabled.\n");
2639                 return;
2640         }
2641
2642         tx_vlan_reset(port_id);
2643         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
2644         ports[port_id].tx_vlan_id = vlan_id;
2645 }
2646
2647 void
2648 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
2649 {
2650         int vlan_offload;
2651         if (port_id_is_invalid(port_id, ENABLED_WARN))
2652                 return;
2653         if (vlan_id_is_invalid(vlan_id))
2654                 return;
2655         if (vlan_id_is_invalid(vlan_id_outer))
2656                 return;
2657
2658         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2659         if (!(vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)) {
2660                 printf("Error, as QinQ hasn't been enabled.\n");
2661                 return;
2662         }
2663
2664         tx_vlan_reset(port_id);
2665         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_QINQ;
2666         ports[port_id].tx_vlan_id = vlan_id;
2667         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
2668 }
2669
2670 void
2671 tx_vlan_reset(portid_t port_id)
2672 {
2673         if (port_id_is_invalid(port_id, ENABLED_WARN))
2674                 return;
2675         ports[port_id].tx_ol_flags &= ~(TESTPMD_TX_OFFLOAD_INSERT_VLAN |
2676                                 TESTPMD_TX_OFFLOAD_INSERT_QINQ);
2677         ports[port_id].tx_vlan_id = 0;
2678         ports[port_id].tx_vlan_id_outer = 0;
2679 }
2680
2681 void
2682 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
2683 {
2684         if (port_id_is_invalid(port_id, ENABLED_WARN))
2685                 return;
2686
2687         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
2688 }
2689
2690 void
2691 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
2692 {
2693         uint16_t i;
2694         uint8_t existing_mapping_found = 0;
2695
2696         if (port_id_is_invalid(port_id, ENABLED_WARN))
2697                 return;
2698
2699         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
2700                 return;
2701
2702         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2703                 printf("map_value not in required range 0..%d\n",
2704                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
2705                 return;
2706         }
2707
2708         if (!is_rx) { /*then tx*/
2709                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
2710                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
2711                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
2712                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
2713                                 existing_mapping_found = 1;
2714                                 break;
2715                         }
2716                 }
2717                 if (!existing_mapping_found) { /* A new additional mapping... */
2718                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
2719                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
2720                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
2721                         nb_tx_queue_stats_mappings++;
2722                 }
2723         }
2724         else { /*rx*/
2725                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
2726                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
2727                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
2728                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
2729                                 existing_mapping_found = 1;
2730                                 break;
2731                         }
2732                 }
2733                 if (!existing_mapping_found) { /* A new additional mapping... */
2734                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
2735                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
2736                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
2737                         nb_rx_queue_stats_mappings++;
2738                 }
2739         }
2740 }
2741
2742 static inline void
2743 print_fdir_mask(struct rte_eth_fdir_masks *mask)
2744 {
2745         printf("\n    vlan_tci: 0x%04x", rte_be_to_cpu_16(mask->vlan_tci_mask));
2746
2747         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2748                 printf(", mac_addr: 0x%02x, tunnel_type: 0x%01x,"
2749                         " tunnel_id: 0x%08x",
2750                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
2751                         rte_be_to_cpu_32(mask->tunnel_id_mask));
2752         else if (fdir_conf.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2753                 printf(", src_ipv4: 0x%08x, dst_ipv4: 0x%08x",
2754                         rte_be_to_cpu_32(mask->ipv4_mask.src_ip),
2755                         rte_be_to_cpu_32(mask->ipv4_mask.dst_ip));
2756
2757                 printf("\n    src_port: 0x%04x, dst_port: 0x%04x",
2758                         rte_be_to_cpu_16(mask->src_port_mask),
2759                         rte_be_to_cpu_16(mask->dst_port_mask));
2760
2761                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2762                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[0]),
2763                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[1]),
2764                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[2]),
2765                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[3]));
2766
2767                 printf("\n    dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2768                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[0]),
2769                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[1]),
2770                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[2]),
2771                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[3]));
2772         }
2773
2774         printf("\n");
2775 }
2776
2777 static inline void
2778 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2779 {
2780         struct rte_eth_flex_payload_cfg *cfg;
2781         uint32_t i, j;
2782
2783         for (i = 0; i < flex_conf->nb_payloads; i++) {
2784                 cfg = &flex_conf->flex_set[i];
2785                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
2786                         printf("\n    RAW:  ");
2787                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
2788                         printf("\n    L2_PAYLOAD:  ");
2789                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
2790                         printf("\n    L3_PAYLOAD:  ");
2791                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
2792                         printf("\n    L4_PAYLOAD:  ");
2793                 else
2794                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
2795                 for (j = 0; j < num; j++)
2796                         printf("  %-5u", cfg->src_offset[j]);
2797         }
2798         printf("\n");
2799 }
2800
2801 static char *
2802 flowtype_to_str(uint16_t flow_type)
2803 {
2804         struct flow_type_info {
2805                 char str[32];
2806                 uint16_t ftype;
2807         };
2808
2809         uint8_t i;
2810         static struct flow_type_info flowtype_str_table[] = {
2811                 {"raw", RTE_ETH_FLOW_RAW},
2812                 {"ipv4", RTE_ETH_FLOW_IPV4},
2813                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
2814                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
2815                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
2816                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
2817                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
2818                 {"ipv6", RTE_ETH_FLOW_IPV6},
2819                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
2820                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
2821                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
2822                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
2823                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
2824                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
2825                 {"port", RTE_ETH_FLOW_PORT},
2826                 {"vxlan", RTE_ETH_FLOW_VXLAN},
2827                 {"geneve", RTE_ETH_FLOW_GENEVE},
2828                 {"nvgre", RTE_ETH_FLOW_NVGRE},
2829         };
2830
2831         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
2832                 if (flowtype_str_table[i].ftype == flow_type)
2833                         return flowtype_str_table[i].str;
2834         }
2835
2836         return NULL;
2837 }
2838
2839 static inline void
2840 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2841 {
2842         struct rte_eth_fdir_flex_mask *mask;
2843         uint32_t i, j;
2844         char *p;
2845
2846         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
2847                 mask = &flex_conf->flex_mask[i];
2848                 p = flowtype_to_str(mask->flow_type);
2849                 printf("\n    %s:\t", p ? p : "unknown");
2850                 for (j = 0; j < num; j++)
2851                         printf(" %02x", mask->mask[j]);
2852         }
2853         printf("\n");
2854 }
2855
2856 static inline void
2857 print_fdir_flow_type(uint32_t flow_types_mask)
2858 {
2859         int i;
2860         char *p;
2861
2862         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
2863                 if (!(flow_types_mask & (1 << i)))
2864                         continue;
2865                 p = flowtype_to_str(i);
2866                 if (p)
2867                         printf(" %s", p);
2868                 else
2869                         printf(" unknown");
2870         }
2871         printf("\n");
2872 }
2873
2874 void
2875 fdir_get_infos(portid_t port_id)
2876 {
2877         struct rte_eth_fdir_stats fdir_stat;
2878         struct rte_eth_fdir_info fdir_info;
2879         int ret;
2880
2881         static const char *fdir_stats_border = "########################";
2882
2883         if (port_id_is_invalid(port_id, ENABLED_WARN))
2884                 return;
2885         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
2886         if (ret < 0) {
2887                 printf("\n FDIR is not supported on port %-2d\n",
2888                         port_id);
2889                 return;
2890         }
2891
2892         memset(&fdir_info, 0, sizeof(fdir_info));
2893         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2894                                RTE_ETH_FILTER_INFO, &fdir_info);
2895         memset(&fdir_stat, 0, sizeof(fdir_stat));
2896         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2897                                RTE_ETH_FILTER_STATS, &fdir_stat);
2898         printf("\n  %s FDIR infos for port %-2d     %s\n",
2899                fdir_stats_border, port_id, fdir_stats_border);
2900         printf("  MODE: ");
2901         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
2902                 printf("  PERFECT\n");
2903         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2904                 printf("  PERFECT-MAC-VLAN\n");
2905         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2906                 printf("  PERFECT-TUNNEL\n");
2907         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
2908                 printf("  SIGNATURE\n");
2909         else
2910                 printf("  DISABLE\n");
2911         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
2912                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
2913                 printf("  SUPPORTED FLOW TYPE: ");
2914                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
2915         }
2916         printf("  FLEX PAYLOAD INFO:\n");
2917         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
2918                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
2919                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
2920                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
2921                 fdir_info.flex_payload_unit,
2922                 fdir_info.max_flex_payload_segment_num,
2923                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
2924         printf("  MASK: ");
2925         print_fdir_mask(&fdir_info.mask);
2926         if (fdir_info.flex_conf.nb_payloads > 0) {
2927                 printf("  FLEX PAYLOAD SRC OFFSET:");
2928                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2929         }
2930         if (fdir_info.flex_conf.nb_flexmasks > 0) {
2931                 printf("  FLEX MASK CFG:");
2932                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2933         }
2934         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
2935                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
2936         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
2937                fdir_info.guarant_spc, fdir_info.best_spc);
2938         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
2939                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
2940                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
2941                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
2942                fdir_stat.collision, fdir_stat.free,
2943                fdir_stat.maxhash, fdir_stat.maxlen,
2944                fdir_stat.add, fdir_stat.remove,
2945                fdir_stat.f_add, fdir_stat.f_remove);
2946         printf("  %s############################%s\n",
2947                fdir_stats_border, fdir_stats_border);
2948 }
2949
2950 void
2951 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
2952 {
2953         struct rte_port *port;
2954         struct rte_eth_fdir_flex_conf *flex_conf;
2955         int i, idx = 0;
2956
2957         port = &ports[port_id];
2958         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2959         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
2960                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
2961                         idx = i;
2962                         break;
2963                 }
2964         }
2965         if (i >= RTE_ETH_FLOW_MAX) {
2966                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
2967                         idx = flex_conf->nb_flexmasks;
2968                         flex_conf->nb_flexmasks++;
2969                 } else {
2970                         printf("The flex mask table is full. Can not set flex"
2971                                 " mask for flow_type(%u).", cfg->flow_type);
2972                         return;
2973                 }
2974         }
2975         (void)rte_memcpy(&flex_conf->flex_mask[idx],
2976                          cfg,
2977                          sizeof(struct rte_eth_fdir_flex_mask));
2978 }
2979
2980 void
2981 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
2982 {
2983         struct rte_port *port;
2984         struct rte_eth_fdir_flex_conf *flex_conf;
2985         int i, idx = 0;
2986
2987         port = &ports[port_id];
2988         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2989         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
2990                 if (cfg->type == flex_conf->flex_set[i].type) {
2991                         idx = i;
2992                         break;
2993                 }
2994         }
2995         if (i >= RTE_ETH_PAYLOAD_MAX) {
2996                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
2997                         idx = flex_conf->nb_payloads;
2998                         flex_conf->nb_payloads++;
2999                 } else {
3000                         printf("The flex payload table is full. Can not set"
3001                                 " flex payload for type(%u).", cfg->type);
3002                         return;
3003                 }
3004         }
3005         (void)rte_memcpy(&flex_conf->flex_set[idx],
3006                          cfg,
3007                          sizeof(struct rte_eth_flex_payload_cfg));
3008
3009 }
3010
3011 #ifdef RTE_LIBRTE_IXGBE_PMD
3012 void
3013 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
3014 {
3015         int diag;
3016
3017         if (is_rx)
3018                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
3019         else
3020                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
3021
3022         if (diag == 0)
3023                 return;
3024         if(is_rx)
3025                 printf("rte_pmd_ixgbe_set_vf_rx for port_id=%d failed "
3026                         "diag=%d\n", port_id, diag);
3027         else
3028                 printf("rte_pmd_ixgbe_set_vf_tx for port_id=%d failed "
3029                         "diag=%d\n", port_id, diag);
3030
3031 }
3032 #endif
3033
3034 int
3035 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
3036 {
3037         int diag;
3038         struct rte_eth_link link;
3039
3040         if (port_id_is_invalid(port_id, ENABLED_WARN))
3041                 return 1;
3042         rte_eth_link_get_nowait(port_id, &link);
3043         if (rate > link.link_speed) {
3044                 printf("Invalid rate value:%u bigger than link speed: %u\n",
3045                         rate, link.link_speed);
3046                 return 1;
3047         }
3048         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
3049         if (diag == 0)
3050                 return diag;
3051         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
3052                 port_id, diag);
3053         return diag;
3054 }
3055
3056 #ifdef RTE_LIBRTE_IXGBE_PMD
3057 int
3058 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
3059 {
3060         int diag;
3061
3062         diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate, q_msk);
3063         if (diag == 0)
3064                 return diag;
3065         printf("rte_pmd_ixgbe_set_vf_rate_limit for port_id=%d failed diag=%d\n",
3066                 port_id, diag);
3067         return diag;
3068 }
3069 #endif
3070
3071 /*
3072  * Functions to manage the set of filtered Multicast MAC addresses.
3073  *
3074  * A pool of filtered multicast MAC addresses is associated with each port.
3075  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
3076  * The address of the pool and the number of valid multicast MAC addresses
3077  * recorded in the pool are stored in the fields "mc_addr_pool" and
3078  * "mc_addr_nb" of the "rte_port" data structure.
3079  *
3080  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
3081  * to be supplied a contiguous array of multicast MAC addresses.
3082  * To comply with this constraint, the set of multicast addresses recorded
3083  * into the pool are systematically compacted at the beginning of the pool.
3084  * Hence, when a multicast address is removed from the pool, all following
3085  * addresses, if any, are copied back to keep the set contiguous.
3086  */
3087 #define MCAST_POOL_INC 32
3088
3089 static int
3090 mcast_addr_pool_extend(struct rte_port *port)
3091 {
3092         struct ether_addr *mc_pool;
3093         size_t mc_pool_size;
3094
3095         /*
3096          * If a free entry is available at the end of the pool, just
3097          * increment the number of recorded multicast addresses.
3098          */
3099         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
3100                 port->mc_addr_nb++;
3101                 return 0;
3102         }
3103
3104         /*
3105          * [re]allocate a pool with MCAST_POOL_INC more entries.
3106          * The previous test guarantees that port->mc_addr_nb is a multiple
3107          * of MCAST_POOL_INC.
3108          */
3109         mc_pool_size = sizeof(struct ether_addr) * (port->mc_addr_nb +
3110                                                     MCAST_POOL_INC);
3111         mc_pool = (struct ether_addr *) realloc(port->mc_addr_pool,
3112                                                 mc_pool_size);
3113         if (mc_pool == NULL) {
3114                 printf("allocation of pool of %u multicast addresses failed\n",
3115                        port->mc_addr_nb + MCAST_POOL_INC);
3116                 return -ENOMEM;
3117         }
3118
3119         port->mc_addr_pool = mc_pool;
3120         port->mc_addr_nb++;
3121         return 0;
3122
3123 }
3124
3125 static void
3126 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
3127 {
3128         port->mc_addr_nb--;
3129         if (addr_idx == port->mc_addr_nb) {
3130                 /* No need to recompact the set of multicast addressses. */
3131                 if (port->mc_addr_nb == 0) {
3132                         /* free the pool of multicast addresses. */
3133                         free(port->mc_addr_pool);
3134                         port->mc_addr_pool = NULL;
3135                 }
3136                 return;
3137         }
3138         memmove(&port->mc_addr_pool[addr_idx],
3139                 &port->mc_addr_pool[addr_idx + 1],
3140                 sizeof(struct ether_addr) * (port->mc_addr_nb - addr_idx));
3141 }
3142
3143 static void
3144 eth_port_multicast_addr_list_set(uint8_t port_id)
3145 {
3146         struct rte_port *port;
3147         int diag;
3148
3149         port = &ports[port_id];
3150         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
3151                                             port->mc_addr_nb);
3152         if (diag == 0)
3153                 return;
3154         printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
3155                port->mc_addr_nb, port_id, -diag);
3156 }
3157
3158 void
3159 mcast_addr_add(uint8_t port_id, struct ether_addr *mc_addr)
3160 {
3161         struct rte_port *port;
3162         uint32_t i;
3163
3164         if (port_id_is_invalid(port_id, ENABLED_WARN))
3165                 return;
3166
3167         port = &ports[port_id];
3168
3169         /*
3170          * Check that the added multicast MAC address is not already recorded
3171          * in the pool of multicast addresses.
3172          */
3173         for (i = 0; i < port->mc_addr_nb; i++) {
3174                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
3175                         printf("multicast address already filtered by port\n");
3176                         return;
3177                 }
3178         }
3179
3180         if (mcast_addr_pool_extend(port) != 0)
3181                 return;
3182         ether_addr_copy(mc_addr, &port->mc_addr_pool[i]);
3183         eth_port_multicast_addr_list_set(port_id);
3184 }
3185
3186 void
3187 mcast_addr_remove(uint8_t port_id, struct ether_addr *mc_addr)
3188 {
3189         struct rte_port *port;
3190         uint32_t i;
3191
3192         if (port_id_is_invalid(port_id, ENABLED_WARN))
3193                 return;
3194
3195         port = &ports[port_id];
3196
3197         /*
3198          * Search the pool of multicast MAC addresses for the removed address.
3199          */
3200         for (i = 0; i < port->mc_addr_nb; i++) {
3201                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
3202                         break;
3203         }
3204         if (i == port->mc_addr_nb) {
3205                 printf("multicast address not filtered by port %d\n", port_id);
3206                 return;
3207         }
3208
3209         mcast_addr_pool_remove(port, i);
3210         eth_port_multicast_addr_list_set(port_id);
3211 }
3212
3213 void
3214 port_dcb_info_display(uint8_t port_id)
3215 {
3216         struct rte_eth_dcb_info dcb_info;
3217         uint16_t i;
3218         int ret;
3219         static const char *border = "================";
3220
3221         if (port_id_is_invalid(port_id, ENABLED_WARN))
3222                 return;
3223
3224         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
3225         if (ret) {
3226                 printf("\n Failed to get dcb infos on port %-2d\n",
3227                         port_id);
3228                 return;
3229         }
3230         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
3231         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
3232         printf("\n  TC :        ");
3233         for (i = 0; i < dcb_info.nb_tcs; i++)
3234                 printf("\t%4d", i);
3235         printf("\n  Priority :  ");
3236         for (i = 0; i < dcb_info.nb_tcs; i++)
3237                 printf("\t%4d", dcb_info.prio_tc[i]);
3238         printf("\n  BW percent :");
3239         for (i = 0; i < dcb_info.nb_tcs; i++)
3240                 printf("\t%4d%%", dcb_info.tc_bws[i]);
3241         printf("\n  RXQ base :  ");
3242         for (i = 0; i < dcb_info.nb_tcs; i++)
3243                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
3244         printf("\n  RXQ number :");
3245         for (i = 0; i < dcb_info.nb_tcs; i++)
3246                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
3247         printf("\n  TXQ base :  ");
3248         for (i = 0; i < dcb_info.nb_tcs; i++)
3249                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
3250         printf("\n  TXQ number :");
3251         for (i = 0; i < dcb_info.nb_tcs; i++)
3252                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
3253         printf("\n");
3254 }
3255
3256 uint8_t *
3257 open_ddp_package_file(const char *file_path, uint32_t *size)
3258 {
3259         FILE *fh = fopen(file_path, "rb");
3260         uint32_t pkg_size;
3261         uint8_t *buf = NULL;
3262         int ret = 0;
3263
3264         if (size)
3265                 *size = 0;
3266
3267         if (fh == NULL) {
3268                 printf("%s: Failed to open %s\n", __func__, file_path);
3269                 return buf;
3270         }
3271
3272         ret = fseek(fh, 0, SEEK_END);
3273         if (ret < 0) {
3274                 fclose(fh);
3275                 printf("%s: File operations failed\n", __func__);
3276                 return buf;
3277         }
3278
3279         pkg_size = ftell(fh);
3280
3281         buf = (uint8_t *)malloc(pkg_size);
3282         if (!buf) {
3283                 fclose(fh);
3284                 printf("%s: Failed to malloc memory\n", __func__);
3285                 return buf;
3286         }
3287
3288         ret = fseek(fh, 0, SEEK_SET);
3289         if (ret < 0) {
3290                 fclose(fh);
3291                 printf("%s: File seek operation failed\n", __func__);
3292                 close_ddp_package_file(buf);
3293                 return NULL;
3294         }
3295
3296         ret = fread(buf, 1, pkg_size, fh);
3297         if (ret < 0) {
3298                 fclose(fh);
3299                 printf("%s: File read operation failed\n", __func__);
3300                 close_ddp_package_file(buf);
3301                 return NULL;
3302         }
3303
3304         if (size)
3305                 *size = pkg_size;
3306
3307         fclose(fh);
3308
3309         return buf;
3310 }
3311
3312 int
3313 close_ddp_package_file(uint8_t *buf)
3314 {
3315         if (buf) {
3316                 free((void *)buf);
3317                 return 0;
3318         }
3319
3320         return -1;
3321 }