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