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