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