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