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