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