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