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