6ea8630fca00a7f4e6dfb7c9e420864565905a6e
[dpdk.git] / app / test-pmd / config.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #include <stdarg.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <inttypes.h>
12
13 #include <sys/queue.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_debug.h>
22 #include <rte_log.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_memzone.h>
26 #include <rte_launch.h>
27 #include <rte_eal.h>
28 #include <rte_per_lcore.h>
29 #include <rte_lcore.h>
30 #include <rte_atomic.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_mempool.h>
33 #include <rte_mbuf.h>
34 #include <rte_interrupts.h>
35 #include <rte_pci.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_string_fns.h>
39 #include <rte_cycles.h>
40 #include <rte_flow.h>
41 #include <rte_mtr.h>
42 #include <rte_errno.h>
43 #ifdef RTE_NET_IXGBE
44 #include <rte_pmd_ixgbe.h>
45 #endif
46 #ifdef RTE_NET_I40E
47 #include <rte_pmd_i40e.h>
48 #endif
49 #ifdef RTE_NET_BNXT
50 #include <rte_pmd_bnxt.h>
51 #endif
52 #include <rte_gro.h>
53 #include <rte_hexdump.h>
54
55 #include "testpmd.h"
56 #include "cmdline_mtr.h"
57
58 #define ETHDEV_FWVERS_LEN 32
59
60 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
61 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
62 #else
63 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
64 #endif
65
66 #define NS_PER_SEC 1E9
67
68 static char *flowtype_to_str(uint16_t flow_type);
69
70 static const struct {
71         enum tx_pkt_split split;
72         const char *name;
73 } tx_split_name[] = {
74         {
75                 .split = TX_PKT_SPLIT_OFF,
76                 .name = "off",
77         },
78         {
79                 .split = TX_PKT_SPLIT_ON,
80                 .name = "on",
81         },
82         {
83                 .split = TX_PKT_SPLIT_RND,
84                 .name = "rand",
85         },
86 };
87
88 const struct rss_type_info rss_type_table[] = {
89         { "all", ETH_RSS_ETH | ETH_RSS_VLAN | ETH_RSS_IP | ETH_RSS_TCP |
90                 ETH_RSS_UDP | ETH_RSS_SCTP | ETH_RSS_L2_PAYLOAD |
91                 ETH_RSS_L2TPV3 | ETH_RSS_ESP | ETH_RSS_AH | ETH_RSS_PFCP |
92                 ETH_RSS_GTPU | ETH_RSS_ECPRI | ETH_RSS_MPLS},
93         { "none", 0 },
94         { "eth", ETH_RSS_ETH },
95         { "l2-src-only", ETH_RSS_L2_SRC_ONLY },
96         { "l2-dst-only", ETH_RSS_L2_DST_ONLY },
97         { "vlan", ETH_RSS_VLAN },
98         { "s-vlan", ETH_RSS_S_VLAN },
99         { "c-vlan", ETH_RSS_C_VLAN },
100         { "ipv4", ETH_RSS_IPV4 },
101         { "ipv4-frag", ETH_RSS_FRAG_IPV4 },
102         { "ipv4-tcp", ETH_RSS_NONFRAG_IPV4_TCP },
103         { "ipv4-udp", ETH_RSS_NONFRAG_IPV4_UDP },
104         { "ipv4-sctp", ETH_RSS_NONFRAG_IPV4_SCTP },
105         { "ipv4-other", ETH_RSS_NONFRAG_IPV4_OTHER },
106         { "ipv6", ETH_RSS_IPV6 },
107         { "ipv6-frag", ETH_RSS_FRAG_IPV6 },
108         { "ipv6-tcp", ETH_RSS_NONFRAG_IPV6_TCP },
109         { "ipv6-udp", ETH_RSS_NONFRAG_IPV6_UDP },
110         { "ipv6-sctp", ETH_RSS_NONFRAG_IPV6_SCTP },
111         { "ipv6-other", ETH_RSS_NONFRAG_IPV6_OTHER },
112         { "l2-payload", ETH_RSS_L2_PAYLOAD },
113         { "ipv6-ex", ETH_RSS_IPV6_EX },
114         { "ipv6-tcp-ex", ETH_RSS_IPV6_TCP_EX },
115         { "ipv6-udp-ex", ETH_RSS_IPV6_UDP_EX },
116         { "port", ETH_RSS_PORT },
117         { "vxlan", ETH_RSS_VXLAN },
118         { "geneve", ETH_RSS_GENEVE },
119         { "nvgre", ETH_RSS_NVGRE },
120         { "ip", ETH_RSS_IP },
121         { "udp", ETH_RSS_UDP },
122         { "tcp", ETH_RSS_TCP },
123         { "sctp", ETH_RSS_SCTP },
124         { "tunnel", ETH_RSS_TUNNEL },
125         { "l3-pre32", RTE_ETH_RSS_L3_PRE32 },
126         { "l3-pre40", RTE_ETH_RSS_L3_PRE40 },
127         { "l3-pre48", RTE_ETH_RSS_L3_PRE48 },
128         { "l3-pre56", RTE_ETH_RSS_L3_PRE56 },
129         { "l3-pre64", RTE_ETH_RSS_L3_PRE64 },
130         { "l3-pre96", RTE_ETH_RSS_L3_PRE96 },
131         { "l3-src-only", ETH_RSS_L3_SRC_ONLY },
132         { "l3-dst-only", ETH_RSS_L3_DST_ONLY },
133         { "l4-src-only", ETH_RSS_L4_SRC_ONLY },
134         { "l4-dst-only", ETH_RSS_L4_DST_ONLY },
135         { "esp", ETH_RSS_ESP },
136         { "ah", ETH_RSS_AH },
137         { "l2tpv3", ETH_RSS_L2TPV3 },
138         { "pfcp", ETH_RSS_PFCP },
139         { "pppoe", ETH_RSS_PPPOE },
140         { "gtpu", ETH_RSS_GTPU },
141         { "ecpri", ETH_RSS_ECPRI },
142         { "mpls", ETH_RSS_MPLS },
143         { "ipv4-chksum", ETH_RSS_IPV4_CHKSUM },
144         { "l4-chksum", ETH_RSS_L4_CHKSUM },
145         { NULL, 0 },
146 };
147
148 static const struct {
149         enum rte_eth_fec_mode mode;
150         const char *name;
151 } fec_mode_name[] = {
152         {
153                 .mode = RTE_ETH_FEC_NOFEC,
154                 .name = "off",
155         },
156         {
157                 .mode = RTE_ETH_FEC_AUTO,
158                 .name = "auto",
159         },
160         {
161                 .mode = RTE_ETH_FEC_BASER,
162                 .name = "baser",
163         },
164         {
165                 .mode = RTE_ETH_FEC_RS,
166                 .name = "rs",
167         },
168 };
169
170 static void
171 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
172 {
173         char buf[RTE_ETHER_ADDR_FMT_SIZE];
174         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
175         printf("%s%s", name, buf);
176 }
177
178 void
179 nic_stats_display(portid_t port_id)
180 {
181         static uint64_t prev_pkts_rx[RTE_MAX_ETHPORTS];
182         static uint64_t prev_pkts_tx[RTE_MAX_ETHPORTS];
183         static uint64_t prev_bytes_rx[RTE_MAX_ETHPORTS];
184         static uint64_t prev_bytes_tx[RTE_MAX_ETHPORTS];
185         static uint64_t prev_ns[RTE_MAX_ETHPORTS];
186         struct timespec cur_time;
187         uint64_t diff_pkts_rx, diff_pkts_tx, diff_bytes_rx, diff_bytes_tx,
188                                                                 diff_ns;
189         uint64_t mpps_rx, mpps_tx, mbps_rx, mbps_tx;
190         struct rte_eth_stats stats;
191
192         static const char *nic_stats_border = "########################";
193
194         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
195                 print_valid_ports();
196                 return;
197         }
198         rte_eth_stats_get(port_id, &stats);
199         printf("\n  %s NIC statistics for port %-2d %s\n",
200                nic_stats_border, port_id, nic_stats_border);
201
202         printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
203                "%-"PRIu64"\n", stats.ipackets, stats.imissed, stats.ibytes);
204         printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
205         printf("  RX-nombuf:  %-10"PRIu64"\n", stats.rx_nombuf);
206         printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
207                "%-"PRIu64"\n", stats.opackets, stats.oerrors, stats.obytes);
208
209         diff_ns = 0;
210         if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
211                 uint64_t ns;
212
213                 ns = cur_time.tv_sec * NS_PER_SEC;
214                 ns += cur_time.tv_nsec;
215
216                 if (prev_ns[port_id] != 0)
217                         diff_ns = ns - prev_ns[port_id];
218                 prev_ns[port_id] = ns;
219         }
220
221         diff_pkts_rx = (stats.ipackets > prev_pkts_rx[port_id]) ?
222                 (stats.ipackets - prev_pkts_rx[port_id]) : 0;
223         diff_pkts_tx = (stats.opackets > prev_pkts_tx[port_id]) ?
224                 (stats.opackets - prev_pkts_tx[port_id]) : 0;
225         prev_pkts_rx[port_id] = stats.ipackets;
226         prev_pkts_tx[port_id] = stats.opackets;
227         mpps_rx = diff_ns > 0 ?
228                 (double)diff_pkts_rx / diff_ns * NS_PER_SEC : 0;
229         mpps_tx = diff_ns > 0 ?
230                 (double)diff_pkts_tx / diff_ns * NS_PER_SEC : 0;
231
232         diff_bytes_rx = (stats.ibytes > prev_bytes_rx[port_id]) ?
233                 (stats.ibytes - prev_bytes_rx[port_id]) : 0;
234         diff_bytes_tx = (stats.obytes > prev_bytes_tx[port_id]) ?
235                 (stats.obytes - prev_bytes_tx[port_id]) : 0;
236         prev_bytes_rx[port_id] = stats.ibytes;
237         prev_bytes_tx[port_id] = stats.obytes;
238         mbps_rx = diff_ns > 0 ?
239                 (double)diff_bytes_rx / diff_ns * NS_PER_SEC : 0;
240         mbps_tx = diff_ns > 0 ?
241                 (double)diff_bytes_tx / diff_ns * NS_PER_SEC : 0;
242
243         printf("\n  Throughput (since last show)\n");
244         printf("  Rx-pps: %12"PRIu64"          Rx-bps: %12"PRIu64"\n  Tx-pps: %12"
245                PRIu64"          Tx-bps: %12"PRIu64"\n", mpps_rx, mbps_rx * 8,
246                mpps_tx, mbps_tx * 8);
247
248         printf("  %s############################%s\n",
249                nic_stats_border, nic_stats_border);
250 }
251
252 void
253 nic_stats_clear(portid_t port_id)
254 {
255         int ret;
256
257         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
258                 print_valid_ports();
259                 return;
260         }
261
262         ret = rte_eth_stats_reset(port_id);
263         if (ret != 0) {
264                 fprintf(stderr,
265                         "%s: Error: failed to reset stats (port %u): %s",
266                         __func__, port_id, strerror(-ret));
267                 return;
268         }
269
270         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
271         if (ret != 0) {
272                 if (ret < 0)
273                         ret = -ret;
274                 fprintf(stderr,
275                         "%s: Error: failed to get stats (port %u): %s",
276                         __func__, port_id, strerror(ret));
277                 return;
278         }
279         printf("\n  NIC statistics for port %d cleared\n", port_id);
280 }
281
282 void
283 nic_xstats_display(portid_t port_id)
284 {
285         struct rte_eth_xstat *xstats;
286         int cnt_xstats, idx_xstat;
287         struct rte_eth_xstat_name *xstats_names;
288
289         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
290                 print_valid_ports();
291                 return;
292         }
293         printf("###### NIC extended statistics for port %-2d\n", port_id);
294         if (!rte_eth_dev_is_valid_port(port_id)) {
295                 fprintf(stderr, "Error: Invalid port number %i\n", port_id);
296                 return;
297         }
298
299         /* Get count */
300         cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
301         if (cnt_xstats  < 0) {
302                 fprintf(stderr, "Error: Cannot get count of xstats\n");
303                 return;
304         }
305
306         /* Get id-name lookup table */
307         xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
308         if (xstats_names == NULL) {
309                 fprintf(stderr, "Cannot allocate memory for xstats lookup\n");
310                 return;
311         }
312         if (cnt_xstats != rte_eth_xstats_get_names(
313                         port_id, xstats_names, cnt_xstats)) {
314                 fprintf(stderr, "Error: Cannot get xstats lookup\n");
315                 free(xstats_names);
316                 return;
317         }
318
319         /* Get stats themselves */
320         xstats = malloc(sizeof(struct rte_eth_xstat) * cnt_xstats);
321         if (xstats == NULL) {
322                 fprintf(stderr, "Cannot allocate memory for xstats\n");
323                 free(xstats_names);
324                 return;
325         }
326         if (cnt_xstats != rte_eth_xstats_get(port_id, xstats, cnt_xstats)) {
327                 fprintf(stderr, "Error: Unable to get xstats\n");
328                 free(xstats_names);
329                 free(xstats);
330                 return;
331         }
332
333         /* Display xstats */
334         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
335                 if (xstats_hide_zero && !xstats[idx_xstat].value)
336                         continue;
337                 printf("%s: %"PRIu64"\n",
338                         xstats_names[idx_xstat].name,
339                         xstats[idx_xstat].value);
340         }
341         free(xstats_names);
342         free(xstats);
343 }
344
345 void
346 nic_xstats_clear(portid_t port_id)
347 {
348         int ret;
349
350         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
351                 print_valid_ports();
352                 return;
353         }
354
355         ret = rte_eth_xstats_reset(port_id);
356         if (ret != 0) {
357                 fprintf(stderr,
358                         "%s: Error: failed to reset xstats (port %u): %s\n",
359                         __func__, port_id, strerror(-ret));
360                 return;
361         }
362
363         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
364         if (ret != 0) {
365                 if (ret < 0)
366                         ret = -ret;
367                 fprintf(stderr, "%s: Error: failed to get stats (port %u): %s",
368                         __func__, port_id, strerror(ret));
369                 return;
370         }
371 }
372
373 static const char *
374 get_queue_state_name(uint8_t queue_state)
375 {
376         if (queue_state == RTE_ETH_QUEUE_STATE_STOPPED)
377                 return "stopped";
378         else if (queue_state == RTE_ETH_QUEUE_STATE_STARTED)
379                 return "started";
380         else if (queue_state == RTE_ETH_QUEUE_STATE_HAIRPIN)
381                 return "hairpin";
382         else
383                 return "unknown";
384 }
385
386 void
387 rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
388 {
389         struct rte_eth_burst_mode mode;
390         struct rte_eth_rxq_info qinfo;
391         int32_t rc;
392         static const char *info_border = "*********************";
393
394         rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
395         if (rc != 0) {
396                 fprintf(stderr,
397                         "Failed to retrieve information for port: %u, RX queue: %hu\nerror desc: %s(%d)\n",
398                         port_id, queue_id, strerror(-rc), rc);
399                 return;
400         }
401
402         printf("\n%s Infos for port %-2u, RX queue %-2u %s",
403                info_border, port_id, queue_id, info_border);
404
405         printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
406         printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
407         printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
408         printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
409         printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
410         printf("\nRX drop packets: %s",
411                 (qinfo.conf.rx_drop_en != 0) ? "on" : "off");
412         printf("\nRX deferred start: %s",
413                 (qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
414         printf("\nRX scattered packets: %s",
415                 (qinfo.scattered_rx != 0) ? "on" : "off");
416         printf("\nRx queue state: %s", get_queue_state_name(qinfo.queue_state));
417         if (qinfo.rx_buf_size != 0)
418                 printf("\nRX buffer size: %hu", qinfo.rx_buf_size);
419         printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
420
421         if (rte_eth_rx_burst_mode_get(port_id, queue_id, &mode) == 0)
422                 printf("\nBurst mode: %s%s",
423                        mode.info,
424                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
425                                 " (per queue)" : "");
426
427         printf("\n");
428 }
429
430 void
431 tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
432 {
433         struct rte_eth_burst_mode mode;
434         struct rte_eth_txq_info qinfo;
435         int32_t rc;
436         static const char *info_border = "*********************";
437
438         rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
439         if (rc != 0) {
440                 fprintf(stderr,
441                         "Failed to retrieve information for port: %u, TX queue: %hu\nerror desc: %s(%d)\n",
442                         port_id, queue_id, strerror(-rc), rc);
443                 return;
444         }
445
446         printf("\n%s Infos for port %-2u, TX queue %-2u %s",
447                info_border, port_id, queue_id, info_border);
448
449         printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
450         printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
451         printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
452         printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
453         printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
454         printf("\nTX deferred start: %s",
455                 (qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
456         printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
457         printf("\nTx queue state: %s", get_queue_state_name(qinfo.queue_state));
458
459         if (rte_eth_tx_burst_mode_get(port_id, queue_id, &mode) == 0)
460                 printf("\nBurst mode: %s%s",
461                        mode.info,
462                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
463                                 " (per queue)" : "");
464
465         printf("\n");
466 }
467
468 static int bus_match_all(const struct rte_bus *bus, const void *data)
469 {
470         RTE_SET_USED(bus);
471         RTE_SET_USED(data);
472         return 0;
473 }
474
475 static void
476 device_infos_display_speeds(uint32_t speed_capa)
477 {
478         printf("\n\tDevice speed capability:");
479         if (speed_capa == ETH_LINK_SPEED_AUTONEG)
480                 printf(" Autonegotiate (all speeds)");
481         if (speed_capa & ETH_LINK_SPEED_FIXED)
482                 printf(" Disable autonegotiate (fixed speed)  ");
483         if (speed_capa & ETH_LINK_SPEED_10M_HD)
484                 printf(" 10 Mbps half-duplex  ");
485         if (speed_capa & ETH_LINK_SPEED_10M)
486                 printf(" 10 Mbps full-duplex  ");
487         if (speed_capa & ETH_LINK_SPEED_100M_HD)
488                 printf(" 100 Mbps half-duplex  ");
489         if (speed_capa & ETH_LINK_SPEED_100M)
490                 printf(" 100 Mbps full-duplex  ");
491         if (speed_capa & ETH_LINK_SPEED_1G)
492                 printf(" 1 Gbps  ");
493         if (speed_capa & ETH_LINK_SPEED_2_5G)
494                 printf(" 2.5 Gbps  ");
495         if (speed_capa & ETH_LINK_SPEED_5G)
496                 printf(" 5 Gbps  ");
497         if (speed_capa & ETH_LINK_SPEED_10G)
498                 printf(" 10 Gbps  ");
499         if (speed_capa & ETH_LINK_SPEED_20G)
500                 printf(" 20 Gbps  ");
501         if (speed_capa & ETH_LINK_SPEED_25G)
502                 printf(" 25 Gbps  ");
503         if (speed_capa & ETH_LINK_SPEED_40G)
504                 printf(" 40 Gbps  ");
505         if (speed_capa & ETH_LINK_SPEED_50G)
506                 printf(" 50 Gbps  ");
507         if (speed_capa & ETH_LINK_SPEED_56G)
508                 printf(" 56 Gbps  ");
509         if (speed_capa & ETH_LINK_SPEED_100G)
510                 printf(" 100 Gbps  ");
511         if (speed_capa & ETH_LINK_SPEED_200G)
512                 printf(" 200 Gbps  ");
513 }
514
515 void
516 device_infos_display(const char *identifier)
517 {
518         static const char *info_border = "*********************";
519         struct rte_bus *start = NULL, *next;
520         struct rte_dev_iterator dev_iter;
521         char name[RTE_ETH_NAME_MAX_LEN];
522         struct rte_ether_addr mac_addr;
523         struct rte_device *dev;
524         struct rte_devargs da;
525         portid_t port_id;
526         struct rte_eth_dev_info dev_info;
527         char devstr[128];
528
529         memset(&da, 0, sizeof(da));
530         if (!identifier)
531                 goto skip_parse;
532
533         if (rte_devargs_parsef(&da, "%s", identifier)) {
534                 fprintf(stderr, "cannot parse identifier\n");
535                 return;
536         }
537
538 skip_parse:
539         while ((next = rte_bus_find(start, bus_match_all, NULL)) != NULL) {
540
541                 start = next;
542                 if (identifier && da.bus != next)
543                         continue;
544
545                 /* Skip buses that don't have iterate method */
546                 if (!next->dev_iterate)
547                         continue;
548
549                 snprintf(devstr, sizeof(devstr), "bus=%s", next->name);
550                 RTE_DEV_FOREACH(dev, devstr, &dev_iter) {
551
552                         if (!dev->driver)
553                                 continue;
554                         /* Check for matching device if identifier is present */
555                         if (identifier &&
556                             strncmp(da.name, dev->name, strlen(dev->name)))
557                                 continue;
558                         printf("\n%s Infos for device %s %s\n",
559                                info_border, dev->name, info_border);
560                         printf("Bus name: %s", dev->bus->name);
561                         printf("\nDriver name: %s", dev->driver->name);
562                         printf("\nDevargs: %s",
563                                dev->devargs ? dev->devargs->args : "");
564                         printf("\nConnect to socket: %d", dev->numa_node);
565                         printf("\n");
566
567                         /* List ports with matching device name */
568                         RTE_ETH_FOREACH_DEV_OF(port_id, dev) {
569                                 printf("\n\tPort id: %-2d", port_id);
570                                 if (eth_macaddr_get_print_err(port_id,
571                                                               &mac_addr) == 0)
572                                         print_ethaddr("\n\tMAC address: ",
573                                                       &mac_addr);
574                                 rte_eth_dev_get_name_by_port(port_id, name);
575                                 printf("\n\tDevice name: %s", name);
576                                 if (rte_eth_dev_info_get(port_id, &dev_info) == 0)
577                                         device_infos_display_speeds(dev_info.speed_capa);
578                                 printf("\n");
579                         }
580                 }
581         };
582         rte_devargs_reset(&da);
583 }
584
585 void
586 port_infos_display(portid_t port_id)
587 {
588         struct rte_port *port;
589         struct rte_ether_addr mac_addr;
590         struct rte_eth_link link;
591         struct rte_eth_dev_info dev_info;
592         int vlan_offload;
593         struct rte_mempool * mp;
594         static const char *info_border = "*********************";
595         uint16_t mtu;
596         char name[RTE_ETH_NAME_MAX_LEN];
597         int ret;
598         char fw_version[ETHDEV_FWVERS_LEN];
599
600         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
601                 print_valid_ports();
602                 return;
603         }
604         port = &ports[port_id];
605         ret = eth_link_get_nowait_print_err(port_id, &link);
606         if (ret < 0)
607                 return;
608
609         ret = eth_dev_info_get_print_err(port_id, &dev_info);
610         if (ret != 0)
611                 return;
612
613         printf("\n%s Infos for port %-2d %s\n",
614                info_border, port_id, info_border);
615         if (eth_macaddr_get_print_err(port_id, &mac_addr) == 0)
616                 print_ethaddr("MAC address: ", &mac_addr);
617         rte_eth_dev_get_name_by_port(port_id, name);
618         printf("\nDevice name: %s", name);
619         printf("\nDriver name: %s", dev_info.driver_name);
620
621         if (rte_eth_dev_fw_version_get(port_id, fw_version,
622                                                 ETHDEV_FWVERS_LEN) == 0)
623                 printf("\nFirmware-version: %s", fw_version);
624         else
625                 printf("\nFirmware-version: %s", "not available");
626
627         if (dev_info.device->devargs && dev_info.device->devargs->args)
628                 printf("\nDevargs: %s", dev_info.device->devargs->args);
629         printf("\nConnect to socket: %u", port->socket_id);
630
631         if (port_numa[port_id] != NUMA_NO_CONFIG) {
632                 mp = mbuf_pool_find(port_numa[port_id], 0);
633                 if (mp)
634                         printf("\nmemory allocation on the socket: %d",
635                                                         port_numa[port_id]);
636         } else
637                 printf("\nmemory allocation on the socket: %u",port->socket_id);
638
639         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
640         printf("Link speed: %s\n", rte_eth_link_speed_to_str(link.link_speed));
641         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
642                ("full-duplex") : ("half-duplex"));
643         printf("Autoneg status: %s\n", (link.link_autoneg == ETH_LINK_AUTONEG) ?
644                ("On") : ("Off"));
645
646         if (!rte_eth_dev_get_mtu(port_id, &mtu))
647                 printf("MTU: %u\n", mtu);
648
649         printf("Promiscuous mode: %s\n",
650                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
651         printf("Allmulticast mode: %s\n",
652                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
653         printf("Maximum number of MAC addresses: %u\n",
654                (unsigned int)(port->dev_info.max_mac_addrs));
655         printf("Maximum number of MAC addresses of hash filtering: %u\n",
656                (unsigned int)(port->dev_info.max_hash_mac_addrs));
657
658         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
659         if (vlan_offload >= 0){
660                 printf("VLAN offload: \n");
661                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
662                         printf("  strip on, ");
663                 else
664                         printf("  strip off, ");
665
666                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
667                         printf("filter on, ");
668                 else
669                         printf("filter off, ");
670
671                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
672                         printf("extend on, ");
673                 else
674                         printf("extend off, ");
675
676                 if (vlan_offload & ETH_QINQ_STRIP_OFFLOAD)
677                         printf("qinq strip on\n");
678                 else
679                         printf("qinq strip off\n");
680         }
681
682         if (dev_info.hash_key_size > 0)
683                 printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
684         if (dev_info.reta_size > 0)
685                 printf("Redirection table size: %u\n", dev_info.reta_size);
686         if (!dev_info.flow_type_rss_offloads)
687                 printf("No RSS offload flow type is supported.\n");
688         else {
689                 uint16_t i;
690                 char *p;
691
692                 printf("Supported RSS offload flow types:\n");
693                 for (i = RTE_ETH_FLOW_UNKNOWN + 1;
694                      i < sizeof(dev_info.flow_type_rss_offloads) * CHAR_BIT; i++) {
695                         if (!(dev_info.flow_type_rss_offloads & (1ULL << i)))
696                                 continue;
697                         p = flowtype_to_str(i);
698                         if (p)
699                                 printf("  %s\n", p);
700                         else
701                                 printf("  user defined %d\n", i);
702                 }
703         }
704
705         printf("Minimum size of RX buffer: %u\n", dev_info.min_rx_bufsize);
706         printf("Maximum configurable length of RX packet: %u\n",
707                 dev_info.max_rx_pktlen);
708         printf("Maximum configurable size of LRO aggregated packet: %u\n",
709                 dev_info.max_lro_pkt_size);
710         if (dev_info.max_vfs)
711                 printf("Maximum number of VFs: %u\n", dev_info.max_vfs);
712         if (dev_info.max_vmdq_pools)
713                 printf("Maximum number of VMDq pools: %u\n",
714                         dev_info.max_vmdq_pools);
715
716         printf("Current number of RX queues: %u\n", dev_info.nb_rx_queues);
717         printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
718         printf("Max possible number of RXDs per queue: %hu\n",
719                 dev_info.rx_desc_lim.nb_max);
720         printf("Min possible number of RXDs per queue: %hu\n",
721                 dev_info.rx_desc_lim.nb_min);
722         printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
723
724         printf("Current number of TX queues: %u\n", dev_info.nb_tx_queues);
725         printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
726         printf("Max possible number of TXDs per queue: %hu\n",
727                 dev_info.tx_desc_lim.nb_max);
728         printf("Min possible number of TXDs per queue: %hu\n",
729                 dev_info.tx_desc_lim.nb_min);
730         printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
731         printf("Max segment number per packet: %hu\n",
732                 dev_info.tx_desc_lim.nb_seg_max);
733         printf("Max segment number per MTU/TSO: %hu\n",
734                 dev_info.tx_desc_lim.nb_mtu_seg_max);
735
736         /* Show switch info only if valid switch domain and port id is set */
737         if (dev_info.switch_info.domain_id !=
738                 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
739                 if (dev_info.switch_info.name)
740                         printf("Switch name: %s\n", dev_info.switch_info.name);
741
742                 printf("Switch domain Id: %u\n",
743                         dev_info.switch_info.domain_id);
744                 printf("Switch Port Id: %u\n",
745                         dev_info.switch_info.port_id);
746         }
747 }
748
749 void
750 port_summary_header_display(void)
751 {
752         uint16_t port_number;
753
754         port_number = rte_eth_dev_count_avail();
755         printf("Number of available ports: %i\n", port_number);
756         printf("%-4s %-17s %-12s %-14s %-8s %s\n", "Port", "MAC Address", "Name",
757                         "Driver", "Status", "Link");
758 }
759
760 void
761 port_summary_display(portid_t port_id)
762 {
763         struct rte_ether_addr mac_addr;
764         struct rte_eth_link link;
765         struct rte_eth_dev_info dev_info;
766         char name[RTE_ETH_NAME_MAX_LEN];
767         int ret;
768
769         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
770                 print_valid_ports();
771                 return;
772         }
773
774         ret = eth_link_get_nowait_print_err(port_id, &link);
775         if (ret < 0)
776                 return;
777
778         ret = eth_dev_info_get_print_err(port_id, &dev_info);
779         if (ret != 0)
780                 return;
781
782         rte_eth_dev_get_name_by_port(port_id, name);
783         ret = eth_macaddr_get_print_err(port_id, &mac_addr);
784         if (ret != 0)
785                 return;
786
787         printf("%-4d " RTE_ETHER_ADDR_PRT_FMT " %-12s %-14s %-8s %s\n",
788                 port_id, RTE_ETHER_ADDR_BYTES(&mac_addr), name,
789                 dev_info.driver_name, (link.link_status) ? ("up") : ("down"),
790                 rte_eth_link_speed_to_str(link.link_speed));
791 }
792
793 void
794 port_eeprom_display(portid_t port_id)
795 {
796         struct rte_dev_eeprom_info einfo;
797         int ret;
798         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
799                 print_valid_ports();
800                 return;
801         }
802
803         int len_eeprom = rte_eth_dev_get_eeprom_length(port_id);
804         if (len_eeprom < 0) {
805                 switch (len_eeprom) {
806                 case -ENODEV:
807                         fprintf(stderr, "port index %d invalid\n", port_id);
808                         break;
809                 case -ENOTSUP:
810                         fprintf(stderr, "operation not supported by device\n");
811                         break;
812                 case -EIO:
813                         fprintf(stderr, "device is removed\n");
814                         break;
815                 default:
816                         fprintf(stderr, "Unable to get EEPROM: %d\n",
817                                 len_eeprom);
818                         break;
819                 }
820                 return;
821         }
822
823         char buf[len_eeprom];
824         einfo.offset = 0;
825         einfo.length = len_eeprom;
826         einfo.data = buf;
827
828         ret = rte_eth_dev_get_eeprom(port_id, &einfo);
829         if (ret != 0) {
830                 switch (ret) {
831                 case -ENODEV:
832                         fprintf(stderr, "port index %d invalid\n", port_id);
833                         break;
834                 case -ENOTSUP:
835                         fprintf(stderr, "operation not supported by device\n");
836                         break;
837                 case -EIO:
838                         fprintf(stderr, "device is removed\n");
839                         break;
840                 default:
841                         fprintf(stderr, "Unable to get EEPROM: %d\n", ret);
842                         break;
843                 }
844                 return;
845         }
846         rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
847         printf("Finish -- Port: %d EEPROM length: %d bytes\n", port_id, len_eeprom);
848 }
849
850 void
851 port_module_eeprom_display(portid_t port_id)
852 {
853         struct rte_eth_dev_module_info minfo;
854         struct rte_dev_eeprom_info einfo;
855         int ret;
856
857         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
858                 print_valid_ports();
859                 return;
860         }
861
862
863         ret = rte_eth_dev_get_module_info(port_id, &minfo);
864         if (ret != 0) {
865                 switch (ret) {
866                 case -ENODEV:
867                         fprintf(stderr, "port index %d invalid\n", port_id);
868                         break;
869                 case -ENOTSUP:
870                         fprintf(stderr, "operation not supported by device\n");
871                         break;
872                 case -EIO:
873                         fprintf(stderr, "device is removed\n");
874                         break;
875                 default:
876                         fprintf(stderr, "Unable to get module EEPROM: %d\n",
877                                 ret);
878                         break;
879                 }
880                 return;
881         }
882
883         char buf[minfo.eeprom_len];
884         einfo.offset = 0;
885         einfo.length = minfo.eeprom_len;
886         einfo.data = buf;
887
888         ret = rte_eth_dev_get_module_eeprom(port_id, &einfo);
889         if (ret != 0) {
890                 switch (ret) {
891                 case -ENODEV:
892                         fprintf(stderr, "port index %d invalid\n", port_id);
893                         break;
894                 case -ENOTSUP:
895                         fprintf(stderr, "operation not supported by device\n");
896                         break;
897                 case -EIO:
898                         fprintf(stderr, "device is removed\n");
899                         break;
900                 default:
901                         fprintf(stderr, "Unable to get module EEPROM: %d\n",
902                                 ret);
903                         break;
904                 }
905                 return;
906         }
907
908         rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
909         printf("Finish -- Port: %d MODULE EEPROM length: %d bytes\n", port_id, einfo.length);
910 }
911
912 int
913 port_id_is_invalid(portid_t port_id, enum print_warning warning)
914 {
915         uint16_t pid;
916
917         if (port_id == (portid_t)RTE_PORT_ALL)
918                 return 0;
919
920         RTE_ETH_FOREACH_DEV(pid)
921                 if (port_id == pid)
922                         return 0;
923
924         if (warning == ENABLED_WARN)
925                 fprintf(stderr, "Invalid port %d\n", port_id);
926
927         return 1;
928 }
929
930 void print_valid_ports(void)
931 {
932         portid_t pid;
933
934         printf("The valid ports array is [");
935         RTE_ETH_FOREACH_DEV(pid) {
936                 printf(" %d", pid);
937         }
938         printf(" ]\n");
939 }
940
941 static int
942 vlan_id_is_invalid(uint16_t vlan_id)
943 {
944         if (vlan_id < 4096)
945                 return 0;
946         fprintf(stderr, "Invalid vlan_id %d (must be < 4096)\n", vlan_id);
947         return 1;
948 }
949
950 static int
951 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
952 {
953         const struct rte_pci_device *pci_dev;
954         const struct rte_bus *bus;
955         uint64_t pci_len;
956
957         if (reg_off & 0x3) {
958                 fprintf(stderr,
959                         "Port register offset 0x%X not aligned on a 4-byte boundary\n",
960                         (unsigned int)reg_off);
961                 return 1;
962         }
963
964         if (!ports[port_id].dev_info.device) {
965                 fprintf(stderr, "Invalid device\n");
966                 return 0;
967         }
968
969         bus = rte_bus_find_by_device(ports[port_id].dev_info.device);
970         if (bus && !strcmp(bus->name, "pci")) {
971                 pci_dev = RTE_DEV_TO_PCI(ports[port_id].dev_info.device);
972         } else {
973                 fprintf(stderr, "Not a PCI device\n");
974                 return 1;
975         }
976
977         pci_len = pci_dev->mem_resource[0].len;
978         if (reg_off >= pci_len) {
979                 fprintf(stderr,
980                         "Port %d: register offset %u (0x%X) out of port PCI resource (length=%"PRIu64")\n",
981                         port_id, (unsigned int)reg_off, (unsigned int)reg_off,
982                         pci_len);
983                 return 1;
984         }
985         return 0;
986 }
987
988 static int
989 reg_bit_pos_is_invalid(uint8_t bit_pos)
990 {
991         if (bit_pos <= 31)
992                 return 0;
993         fprintf(stderr, "Invalid bit position %d (must be <= 31)\n", bit_pos);
994         return 1;
995 }
996
997 #define display_port_and_reg_off(port_id, reg_off) \
998         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
999
1000 static inline void
1001 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
1002 {
1003         display_port_and_reg_off(port_id, (unsigned)reg_off);
1004         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
1005 }
1006
1007 void
1008 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
1009 {
1010         uint32_t reg_v;
1011
1012
1013         if (port_id_is_invalid(port_id, ENABLED_WARN))
1014                 return;
1015         if (port_reg_off_is_invalid(port_id, reg_off))
1016                 return;
1017         if (reg_bit_pos_is_invalid(bit_x))
1018                 return;
1019         reg_v = port_id_pci_reg_read(port_id, reg_off);
1020         display_port_and_reg_off(port_id, (unsigned)reg_off);
1021         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
1022 }
1023
1024 void
1025 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
1026                            uint8_t bit1_pos, uint8_t bit2_pos)
1027 {
1028         uint32_t reg_v;
1029         uint8_t  l_bit;
1030         uint8_t  h_bit;
1031
1032         if (port_id_is_invalid(port_id, ENABLED_WARN))
1033                 return;
1034         if (port_reg_off_is_invalid(port_id, reg_off))
1035                 return;
1036         if (reg_bit_pos_is_invalid(bit1_pos))
1037                 return;
1038         if (reg_bit_pos_is_invalid(bit2_pos))
1039                 return;
1040         if (bit1_pos > bit2_pos)
1041                 l_bit = bit2_pos, h_bit = bit1_pos;
1042         else
1043                 l_bit = bit1_pos, h_bit = bit2_pos;
1044
1045         reg_v = port_id_pci_reg_read(port_id, reg_off);
1046         reg_v >>= l_bit;
1047         if (h_bit < 31)
1048                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
1049         display_port_and_reg_off(port_id, (unsigned)reg_off);
1050         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
1051                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
1052 }
1053
1054 void
1055 port_reg_display(portid_t port_id, uint32_t reg_off)
1056 {
1057         uint32_t reg_v;
1058
1059         if (port_id_is_invalid(port_id, ENABLED_WARN))
1060                 return;
1061         if (port_reg_off_is_invalid(port_id, reg_off))
1062                 return;
1063         reg_v = port_id_pci_reg_read(port_id, reg_off);
1064         display_port_reg_value(port_id, reg_off, reg_v);
1065 }
1066
1067 void
1068 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
1069                  uint8_t bit_v)
1070 {
1071         uint32_t reg_v;
1072
1073         if (port_id_is_invalid(port_id, ENABLED_WARN))
1074                 return;
1075         if (port_reg_off_is_invalid(port_id, reg_off))
1076                 return;
1077         if (reg_bit_pos_is_invalid(bit_pos))
1078                 return;
1079         if (bit_v > 1) {
1080                 fprintf(stderr, "Invalid bit value %d (must be 0 or 1)\n",
1081                         (int) bit_v);
1082                 return;
1083         }
1084         reg_v = port_id_pci_reg_read(port_id, reg_off);
1085         if (bit_v == 0)
1086                 reg_v &= ~(1 << bit_pos);
1087         else
1088                 reg_v |= (1 << bit_pos);
1089         port_id_pci_reg_write(port_id, reg_off, reg_v);
1090         display_port_reg_value(port_id, reg_off, reg_v);
1091 }
1092
1093 void
1094 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
1095                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
1096 {
1097         uint32_t max_v;
1098         uint32_t reg_v;
1099         uint8_t  l_bit;
1100         uint8_t  h_bit;
1101
1102         if (port_id_is_invalid(port_id, ENABLED_WARN))
1103                 return;
1104         if (port_reg_off_is_invalid(port_id, reg_off))
1105                 return;
1106         if (reg_bit_pos_is_invalid(bit1_pos))
1107                 return;
1108         if (reg_bit_pos_is_invalid(bit2_pos))
1109                 return;
1110         if (bit1_pos > bit2_pos)
1111                 l_bit = bit2_pos, h_bit = bit1_pos;
1112         else
1113                 l_bit = bit1_pos, h_bit = bit2_pos;
1114
1115         if ((h_bit - l_bit) < 31)
1116                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
1117         else
1118                 max_v = 0xFFFFFFFF;
1119
1120         if (value > max_v) {
1121                 fprintf(stderr, "Invalid value %u (0x%x) must be < %u (0x%x)\n",
1122                                 (unsigned)value, (unsigned)value,
1123                                 (unsigned)max_v, (unsigned)max_v);
1124                 return;
1125         }
1126         reg_v = port_id_pci_reg_read(port_id, reg_off);
1127         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
1128         reg_v |= (value << l_bit); /* Set changed bits */
1129         port_id_pci_reg_write(port_id, reg_off, reg_v);
1130         display_port_reg_value(port_id, reg_off, reg_v);
1131 }
1132
1133 void
1134 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
1135 {
1136         if (port_id_is_invalid(port_id, ENABLED_WARN))
1137                 return;
1138         if (port_reg_off_is_invalid(port_id, reg_off))
1139                 return;
1140         port_id_pci_reg_write(port_id, reg_off, reg_v);
1141         display_port_reg_value(port_id, reg_off, reg_v);
1142 }
1143
1144 void
1145 port_mtu_set(portid_t port_id, uint16_t mtu)
1146 {
1147         int diag;
1148         struct rte_port *rte_port = &ports[port_id];
1149         struct rte_eth_dev_info dev_info;
1150         uint16_t eth_overhead;
1151         int ret;
1152
1153         if (port_id_is_invalid(port_id, ENABLED_WARN))
1154                 return;
1155
1156         ret = eth_dev_info_get_print_err(port_id, &dev_info);
1157         if (ret != 0)
1158                 return;
1159
1160         if (mtu > dev_info.max_mtu || mtu < dev_info.min_mtu) {
1161                 fprintf(stderr,
1162                         "Set MTU failed. MTU:%u is not in valid range, min:%u - max:%u\n",
1163                         mtu, dev_info.min_mtu, dev_info.max_mtu);
1164                 return;
1165         }
1166         diag = rte_eth_dev_set_mtu(port_id, mtu);
1167         if (diag)
1168                 fprintf(stderr, "Set MTU failed. diag=%d\n", diag);
1169         else if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1170                 /*
1171                  * Ether overhead in driver is equal to the difference of
1172                  * max_rx_pktlen and max_mtu in rte_eth_dev_info when the
1173                  * device supports jumbo frame.
1174                  */
1175                 eth_overhead = dev_info.max_rx_pktlen - dev_info.max_mtu;
1176                 if (mtu > RTE_ETHER_MTU) {
1177                         rte_port->dev_conf.rxmode.offloads |=
1178                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1179                         rte_port->dev_conf.rxmode.max_rx_pkt_len =
1180                                                 mtu + eth_overhead;
1181                 } else
1182                         rte_port->dev_conf.rxmode.offloads &=
1183                                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1184         }
1185 }
1186
1187 /* Generic flow management functions. */
1188
1189 static struct port_flow_tunnel *
1190 port_flow_locate_tunnel_id(struct rte_port *port, uint32_t port_tunnel_id)
1191 {
1192         struct port_flow_tunnel *flow_tunnel;
1193
1194         LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
1195                 if (flow_tunnel->id == port_tunnel_id)
1196                         goto out;
1197         }
1198         flow_tunnel = NULL;
1199
1200 out:
1201         return flow_tunnel;
1202 }
1203
1204 const char *
1205 port_flow_tunnel_type(struct rte_flow_tunnel *tunnel)
1206 {
1207         const char *type;
1208         switch (tunnel->type) {
1209         default:
1210                 type = "unknown";
1211                 break;
1212         case RTE_FLOW_ITEM_TYPE_VXLAN:
1213                 type = "vxlan";
1214                 break;
1215         }
1216
1217         return type;
1218 }
1219
1220 struct port_flow_tunnel *
1221 port_flow_locate_tunnel(uint16_t port_id, struct rte_flow_tunnel *tun)
1222 {
1223         struct rte_port *port = &ports[port_id];
1224         struct port_flow_tunnel *flow_tunnel;
1225
1226         LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
1227                 if (!memcmp(&flow_tunnel->tunnel, tun, sizeof(*tun)))
1228                         goto out;
1229         }
1230         flow_tunnel = NULL;
1231
1232 out:
1233         return flow_tunnel;
1234 }
1235
1236 void port_flow_tunnel_list(portid_t port_id)
1237 {
1238         struct rte_port *port = &ports[port_id];
1239         struct port_flow_tunnel *flt;
1240
1241         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1242                 printf("port %u tunnel #%u type=%s",
1243                         port_id, flt->id, port_flow_tunnel_type(&flt->tunnel));
1244                 if (flt->tunnel.tun_id)
1245                         printf(" id=%" PRIu64, flt->tunnel.tun_id);
1246                 printf("\n");
1247         }
1248 }
1249
1250 void port_flow_tunnel_destroy(portid_t port_id, uint32_t tunnel_id)
1251 {
1252         struct rte_port *port = &ports[port_id];
1253         struct port_flow_tunnel *flt;
1254
1255         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1256                 if (flt->id == tunnel_id)
1257                         break;
1258         }
1259         if (flt) {
1260                 LIST_REMOVE(flt, chain);
1261                 free(flt);
1262                 printf("port %u: flow tunnel #%u destroyed\n",
1263                         port_id, tunnel_id);
1264         }
1265 }
1266
1267 void port_flow_tunnel_create(portid_t port_id, const struct tunnel_ops *ops)
1268 {
1269         struct rte_port *port = &ports[port_id];
1270         enum rte_flow_item_type type;
1271         struct port_flow_tunnel *flt;
1272
1273         if (!strcmp(ops->type, "vxlan"))
1274                 type = RTE_FLOW_ITEM_TYPE_VXLAN;
1275         else {
1276                 fprintf(stderr, "cannot offload \"%s\" tunnel type\n",
1277                         ops->type);
1278                 return;
1279         }
1280         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1281                 if (flt->tunnel.type == type)
1282                         break;
1283         }
1284         if (!flt) {
1285                 flt = calloc(1, sizeof(*flt));
1286                 if (!flt) {
1287                         fprintf(stderr, "failed to allocate port flt object\n");
1288                         return;
1289                 }
1290                 flt->tunnel.type = type;
1291                 flt->id = LIST_EMPTY(&port->flow_tunnel_list) ? 1 :
1292                                   LIST_FIRST(&port->flow_tunnel_list)->id + 1;
1293                 LIST_INSERT_HEAD(&port->flow_tunnel_list, flt, chain);
1294         }
1295         printf("port %d: flow tunnel #%u type %s\n",
1296                 port_id, flt->id, ops->type);
1297 }
1298
1299 /** Generate a port_flow entry from attributes/pattern/actions. */
1300 static struct port_flow *
1301 port_flow_new(const struct rte_flow_attr *attr,
1302               const struct rte_flow_item *pattern,
1303               const struct rte_flow_action *actions,
1304               struct rte_flow_error *error)
1305 {
1306         const struct rte_flow_conv_rule rule = {
1307                 .attr_ro = attr,
1308                 .pattern_ro = pattern,
1309                 .actions_ro = actions,
1310         };
1311         struct port_flow *pf;
1312         int ret;
1313
1314         ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, error);
1315         if (ret < 0)
1316                 return NULL;
1317         pf = calloc(1, offsetof(struct port_flow, rule) + ret);
1318         if (!pf) {
1319                 rte_flow_error_set
1320                         (error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1321                          "calloc() failed");
1322                 return NULL;
1323         }
1324         if (rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &pf->rule, ret, &rule,
1325                           error) >= 0)
1326                 return pf;
1327         free(pf);
1328         return NULL;
1329 }
1330
1331 /** Print a message out of a flow error. */
1332 static int
1333 port_flow_complain(struct rte_flow_error *error)
1334 {
1335         static const char *const errstrlist[] = {
1336                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
1337                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
1338                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
1339                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
1340                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
1341                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
1342                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
1343                 [RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER] = "transfer field",
1344                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
1345                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
1346                 [RTE_FLOW_ERROR_TYPE_ITEM_SPEC] = "item specification",
1347                 [RTE_FLOW_ERROR_TYPE_ITEM_LAST] = "item specification range",
1348                 [RTE_FLOW_ERROR_TYPE_ITEM_MASK] = "item specification mask",
1349                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
1350                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
1351                 [RTE_FLOW_ERROR_TYPE_ACTION_CONF] = "action configuration",
1352                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
1353         };
1354         const char *errstr;
1355         char buf[32];
1356         int err = rte_errno;
1357
1358         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
1359             !errstrlist[error->type])
1360                 errstr = "unknown type";
1361         else
1362                 errstr = errstrlist[error->type];
1363         fprintf(stderr, "%s(): Caught PMD error type %d (%s): %s%s: %s\n",
1364                 __func__, error->type, errstr,
1365                 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
1366                                          error->cause), buf) : "",
1367                 error->message ? error->message : "(no stated reason)",
1368                 rte_strerror(err));
1369         return -err;
1370 }
1371
1372 static void
1373 rss_config_display(struct rte_flow_action_rss *rss_conf)
1374 {
1375         uint8_t i;
1376
1377         if (rss_conf == NULL) {
1378                 fprintf(stderr, "Invalid rule\n");
1379                 return;
1380         }
1381
1382         printf("RSS:\n"
1383                " queues:");
1384         if (rss_conf->queue_num == 0)
1385                 printf(" none");
1386         for (i = 0; i < rss_conf->queue_num; i++)
1387                 printf(" %d", rss_conf->queue[i]);
1388         printf("\n");
1389
1390         printf(" function: ");
1391         switch (rss_conf->func) {
1392         case RTE_ETH_HASH_FUNCTION_DEFAULT:
1393                 printf("default\n");
1394                 break;
1395         case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1396                 printf("toeplitz\n");
1397                 break;
1398         case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
1399                 printf("simple_xor\n");
1400                 break;
1401         case RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ:
1402                 printf("symmetric_toeplitz\n");
1403                 break;
1404         default:
1405                 printf("Unknown function\n");
1406                 return;
1407         }
1408
1409         printf(" types:\n");
1410         if (rss_conf->types == 0) {
1411                 printf("  none\n");
1412                 return;
1413         }
1414         for (i = 0; rss_type_table[i].str; i++) {
1415                 if ((rss_conf->types &
1416                     rss_type_table[i].rss_type) ==
1417                     rss_type_table[i].rss_type &&
1418                     rss_type_table[i].rss_type != 0)
1419                         printf("  %s\n", rss_type_table[i].str);
1420         }
1421 }
1422
1423 static struct port_indirect_action *
1424 action_get_by_id(portid_t port_id, uint32_t id)
1425 {
1426         struct rte_port *port;
1427         struct port_indirect_action **ppia;
1428         struct port_indirect_action *pia = NULL;
1429
1430         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1431             port_id == (portid_t)RTE_PORT_ALL)
1432                 return NULL;
1433         port = &ports[port_id];
1434         ppia = &port->actions_list;
1435         while (*ppia) {
1436                 if ((*ppia)->id == id) {
1437                         pia = *ppia;
1438                         break;
1439                 }
1440                 ppia = &(*ppia)->next;
1441         }
1442         if (!pia)
1443                 fprintf(stderr,
1444                         "Failed to find indirect action #%u on port %u\n",
1445                         id, port_id);
1446         return pia;
1447 }
1448
1449 static int
1450 action_alloc(portid_t port_id, uint32_t id,
1451              struct port_indirect_action **action)
1452 {
1453         struct rte_port *port;
1454         struct port_indirect_action **ppia;
1455         struct port_indirect_action *pia = NULL;
1456
1457         *action = NULL;
1458         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1459             port_id == (portid_t)RTE_PORT_ALL)
1460                 return -EINVAL;
1461         port = &ports[port_id];
1462         if (id == UINT32_MAX) {
1463                 /* taking first available ID */
1464                 if (port->actions_list) {
1465                         if (port->actions_list->id == UINT32_MAX - 1) {
1466                                 fprintf(stderr,
1467                                         "Highest indirect action ID is already assigned, delete it first\n");
1468                                 return -ENOMEM;
1469                         }
1470                         id = port->actions_list->id + 1;
1471                 } else {
1472                         id = 0;
1473                 }
1474         }
1475         pia = calloc(1, sizeof(*pia));
1476         if (!pia) {
1477                 fprintf(stderr,
1478                         "Allocation of port %u indirect action failed\n",
1479                         port_id);
1480                 return -ENOMEM;
1481         }
1482         ppia = &port->actions_list;
1483         while (*ppia && (*ppia)->id > id)
1484                 ppia = &(*ppia)->next;
1485         if (*ppia && (*ppia)->id == id) {
1486                 fprintf(stderr,
1487                         "Indirect action #%u is already assigned, delete it first\n",
1488                         id);
1489                 free(pia);
1490                 return -EINVAL;
1491         }
1492         pia->next = *ppia;
1493         pia->id = id;
1494         *ppia = pia;
1495         *action = pia;
1496         return 0;
1497 }
1498
1499 /** Create indirect action */
1500 int
1501 port_action_handle_create(portid_t port_id, uint32_t id,
1502                           const struct rte_flow_indir_action_conf *conf,
1503                           const struct rte_flow_action *action)
1504 {
1505         struct port_indirect_action *pia;
1506         int ret;
1507         struct rte_flow_error error;
1508         struct rte_port *port;
1509
1510         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1511             port_id == (portid_t)RTE_PORT_ALL)
1512                 return -EINVAL;
1513
1514         ret = action_alloc(port_id, id, &pia);
1515         if (ret)
1516                 return ret;
1517
1518         port = &ports[port_id];
1519
1520         if (conf->transfer)
1521                 port_id = port->flow_transfer_proxy;
1522
1523         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1524             port_id == (portid_t)RTE_PORT_ALL)
1525                 return -EINVAL;
1526
1527         if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
1528                 struct rte_flow_action_age *age =
1529                         (struct rte_flow_action_age *)(uintptr_t)(action->conf);
1530
1531                 pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
1532                 age->context = &pia->age_type;
1533         } else if (action->type == RTE_FLOW_ACTION_TYPE_CONNTRACK) {
1534                 struct rte_flow_action_conntrack *ct =
1535                 (struct rte_flow_action_conntrack *)(uintptr_t)(action->conf);
1536
1537                 memcpy(ct, &conntrack_context, sizeof(*ct));
1538         }
1539         /* Poisoning to make sure PMDs update it in case of error. */
1540         memset(&error, 0x22, sizeof(error));
1541         pia->handle = rte_flow_action_handle_create(port_id, conf, action,
1542                                                     &error);
1543         if (!pia->handle) {
1544                 uint32_t destroy_id = pia->id;
1545                 port_action_handle_destroy(port_id, 1, &destroy_id);
1546                 return port_flow_complain(&error);
1547         }
1548         pia->type = action->type;
1549         pia->transfer = conf->transfer;
1550         printf("Indirect action #%u created\n", pia->id);
1551         return 0;
1552 }
1553
1554 /** Destroy indirect action */
1555 int
1556 port_action_handle_destroy(portid_t port_id,
1557                            uint32_t n,
1558                            const uint32_t *actions)
1559 {
1560         struct rte_port *port;
1561         struct port_indirect_action **tmp;
1562         uint32_t c = 0;
1563         int ret = 0;
1564
1565         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1566             port_id == (portid_t)RTE_PORT_ALL)
1567                 return -EINVAL;
1568         port = &ports[port_id];
1569         tmp = &port->actions_list;
1570         while (*tmp) {
1571                 uint32_t i;
1572
1573                 for (i = 0; i != n; ++i) {
1574                         struct rte_flow_error error;
1575                         struct port_indirect_action *pia = *tmp;
1576                         portid_t port_id_eff = port_id;
1577
1578                         if (actions[i] != pia->id)
1579                                 continue;
1580
1581                         if (pia->transfer)
1582                                 port_id_eff = port->flow_transfer_proxy;
1583
1584                         if (port_id_is_invalid(port_id_eff, ENABLED_WARN) ||
1585                             port_id_eff == (portid_t)RTE_PORT_ALL)
1586                                 return -EINVAL;
1587
1588                         /*
1589                          * Poisoning to make sure PMDs update it in case
1590                          * of error.
1591                          */
1592                         memset(&error, 0x33, sizeof(error));
1593
1594                         if (pia->handle && rte_flow_action_handle_destroy(
1595                                         port_id_eff, pia->handle, &error)) {
1596                                 ret = port_flow_complain(&error);
1597                                 continue;
1598                         }
1599                         *tmp = pia->next;
1600                         printf("Indirect action #%u destroyed\n", pia->id);
1601                         free(pia);
1602                         break;
1603                 }
1604                 if (i == n)
1605                         tmp = &(*tmp)->next;
1606                 ++c;
1607         }
1608         return ret;
1609 }
1610
1611
1612 /** Get indirect action by port + id */
1613 struct rte_flow_action_handle *
1614 port_action_handle_get_by_id(portid_t port_id, uint32_t id)
1615 {
1616
1617         struct port_indirect_action *pia = action_get_by_id(port_id, id);
1618
1619         return (pia) ? pia->handle : NULL;
1620 }
1621
1622 /** Update indirect action */
1623 int
1624 port_action_handle_update(portid_t port_id, uint32_t id,
1625                           const struct rte_flow_action *action)
1626 {
1627         struct rte_flow_error error;
1628         struct rte_flow_action_handle *action_handle;
1629         struct port_indirect_action *pia;
1630         struct rte_port *port;
1631         const void *update;
1632
1633         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1634             port_id == (portid_t)RTE_PORT_ALL)
1635                 return -EINVAL;
1636
1637         port = &ports[port_id];
1638
1639         action_handle = port_action_handle_get_by_id(port_id, id);
1640         if (!action_handle)
1641                 return -EINVAL;
1642         pia = action_get_by_id(port_id, id);
1643         if (!pia)
1644                 return -EINVAL;
1645         switch (pia->type) {
1646         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
1647                 update = action->conf;
1648                 break;
1649         default:
1650                 update = action;
1651                 break;
1652         }
1653
1654         if (pia->transfer)
1655                 port_id = port->flow_transfer_proxy;
1656
1657         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1658             port_id == (portid_t)RTE_PORT_ALL)
1659                 return -EINVAL;
1660
1661         if (rte_flow_action_handle_update(port_id, action_handle, update,
1662                                           &error)) {
1663                 return port_flow_complain(&error);
1664         }
1665         printf("Indirect action #%u updated\n", id);
1666         return 0;
1667 }
1668
1669 int
1670 port_action_handle_query(portid_t port_id, uint32_t id)
1671 {
1672         struct rte_flow_error error;
1673         struct port_indirect_action *pia;
1674         union {
1675                 struct rte_flow_query_count count;
1676                 struct rte_flow_query_age age;
1677                 struct rte_flow_action_conntrack ct;
1678         } query;
1679         portid_t port_id_eff = port_id;
1680         struct rte_port *port;
1681
1682         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1683             port_id == (portid_t)RTE_PORT_ALL)
1684                 return -EINVAL;
1685
1686         port = &ports[port_id];
1687
1688         pia = action_get_by_id(port_id, id);
1689         if (!pia)
1690                 return -EINVAL;
1691         switch (pia->type) {
1692         case RTE_FLOW_ACTION_TYPE_AGE:
1693         case RTE_FLOW_ACTION_TYPE_COUNT:
1694                 break;
1695         default:
1696                 fprintf(stderr,
1697                         "Indirect action %u (type: %d) on port %u doesn't support query\n",
1698                         id, pia->type, port_id);
1699                 return -ENOTSUP;
1700         }
1701
1702         if (pia->transfer)
1703                 port_id_eff = port->flow_transfer_proxy;
1704
1705         if (port_id_is_invalid(port_id_eff, ENABLED_WARN) ||
1706             port_id_eff == (portid_t)RTE_PORT_ALL)
1707                 return -EINVAL;
1708
1709         /* Poisoning to make sure PMDs update it in case of error. */
1710         memset(&error, 0x55, sizeof(error));
1711         memset(&query, 0, sizeof(query));
1712         if (rte_flow_action_handle_query(port_id_eff, pia->handle, &query,
1713                                          &error))
1714                 return port_flow_complain(&error);
1715         switch (pia->type) {
1716         case RTE_FLOW_ACTION_TYPE_AGE:
1717                 printf("Indirect AGE action:\n"
1718                        " aged: %u\n"
1719                        " sec_since_last_hit_valid: %u\n"
1720                        " sec_since_last_hit: %" PRIu32 "\n",
1721                        query.age.aged,
1722                        query.age.sec_since_last_hit_valid,
1723                        query.age.sec_since_last_hit);
1724                 break;
1725         case RTE_FLOW_ACTION_TYPE_COUNT:
1726                 printf("Indirect COUNT action:\n"
1727                        " hits_set: %u\n"
1728                        " bytes_set: %u\n"
1729                        " hits: %" PRIu64 "\n"
1730                        " bytes: %" PRIu64 "\n",
1731                        query.count.hits_set,
1732                        query.count.bytes_set,
1733                        query.count.hits,
1734                        query.count.bytes);
1735                 break;
1736         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
1737                 printf("Conntrack Context:\n"
1738                        "  Peer: %u, Flow dir: %s, Enable: %u\n"
1739                        "  Live: %u, SACK: %u, CACK: %u\n"
1740                        "  Packet dir: %s, Liberal: %u, State: %u\n"
1741                        "  Factor: %u, Retrans: %u, TCP flags: %u\n"
1742                        "  Last Seq: %u, Last ACK: %u\n"
1743                        "  Last Win: %u, Last End: %u\n",
1744                        query.ct.peer_port,
1745                        query.ct.is_original_dir ? "Original" : "Reply",
1746                        query.ct.enable, query.ct.live_connection,
1747                        query.ct.selective_ack, query.ct.challenge_ack_passed,
1748                        query.ct.last_direction ? "Original" : "Reply",
1749                        query.ct.liberal_mode, query.ct.state,
1750                        query.ct.max_ack_window, query.ct.retransmission_limit,
1751                        query.ct.last_index, query.ct.last_seq,
1752                        query.ct.last_ack, query.ct.last_window,
1753                        query.ct.last_end);
1754                 printf("  Original Dir:\n"
1755                        "    scale: %u, fin: %u, ack seen: %u\n"
1756                        " unacked data: %u\n    Sent end: %u,"
1757                        "    Reply end: %u, Max win: %u, Max ACK: %u\n",
1758                        query.ct.original_dir.scale,
1759                        query.ct.original_dir.close_initiated,
1760                        query.ct.original_dir.last_ack_seen,
1761                        query.ct.original_dir.data_unacked,
1762                        query.ct.original_dir.sent_end,
1763                        query.ct.original_dir.reply_end,
1764                        query.ct.original_dir.max_win,
1765                        query.ct.original_dir.max_ack);
1766                 printf("  Reply Dir:\n"
1767                        "    scale: %u, fin: %u, ack seen: %u\n"
1768                        " unacked data: %u\n    Sent end: %u,"
1769                        "    Reply end: %u, Max win: %u, Max ACK: %u\n",
1770                        query.ct.reply_dir.scale,
1771                        query.ct.reply_dir.close_initiated,
1772                        query.ct.reply_dir.last_ack_seen,
1773                        query.ct.reply_dir.data_unacked,
1774                        query.ct.reply_dir.sent_end,
1775                        query.ct.reply_dir.reply_end,
1776                        query.ct.reply_dir.max_win,
1777                        query.ct.reply_dir.max_ack);
1778                 break;
1779         default:
1780                 fprintf(stderr,
1781                         "Indirect action %u (type: %d) on port %u doesn't support query\n",
1782                         id, pia->type, port_id);
1783                 break;
1784         }
1785         return 0;
1786 }
1787
1788 static struct port_flow_tunnel *
1789 port_flow_tunnel_offload_cmd_prep(portid_t port_id,
1790                                   const struct rte_flow_item *pattern,
1791                                   const struct rte_flow_action *actions,
1792                                   const struct tunnel_ops *tunnel_ops)
1793 {
1794         int ret;
1795         struct rte_port *port;
1796         struct port_flow_tunnel *pft;
1797         struct rte_flow_error error;
1798
1799         port = &ports[port_id];
1800         pft = port_flow_locate_tunnel_id(port, tunnel_ops->id);
1801         if (!pft) {
1802                 fprintf(stderr, "failed to locate port flow tunnel #%u\n",
1803                         tunnel_ops->id);
1804                 return NULL;
1805         }
1806         if (tunnel_ops->actions) {
1807                 uint32_t num_actions;
1808                 const struct rte_flow_action *aptr;
1809
1810                 ret = rte_flow_tunnel_decap_set(port_id, &pft->tunnel,
1811                                                 &pft->pmd_actions,
1812                                                 &pft->num_pmd_actions,
1813                                                 &error);
1814                 if (ret) {
1815                         port_flow_complain(&error);
1816                         return NULL;
1817                 }
1818                 for (aptr = actions, num_actions = 1;
1819                      aptr->type != RTE_FLOW_ACTION_TYPE_END;
1820                      aptr++, num_actions++);
1821                 pft->actions = malloc(
1822                                 (num_actions +  pft->num_pmd_actions) *
1823                                 sizeof(actions[0]));
1824                 if (!pft->actions) {
1825                         rte_flow_tunnel_action_decap_release(
1826                                         port_id, pft->actions,
1827                                         pft->num_pmd_actions, &error);
1828                         return NULL;
1829                 }
1830                 rte_memcpy(pft->actions, pft->pmd_actions,
1831                            pft->num_pmd_actions * sizeof(actions[0]));
1832                 rte_memcpy(pft->actions + pft->num_pmd_actions, actions,
1833                            num_actions * sizeof(actions[0]));
1834         }
1835         if (tunnel_ops->items) {
1836                 uint32_t num_items;
1837                 const struct rte_flow_item *iptr;
1838
1839                 ret = rte_flow_tunnel_match(port_id, &pft->tunnel,
1840                                             &pft->pmd_items,
1841                                             &pft->num_pmd_items,
1842                                             &error);
1843                 if (ret) {
1844                         port_flow_complain(&error);
1845                         return NULL;
1846                 }
1847                 for (iptr = pattern, num_items = 1;
1848                      iptr->type != RTE_FLOW_ITEM_TYPE_END;
1849                      iptr++, num_items++);
1850                 pft->items = malloc((num_items + pft->num_pmd_items) *
1851                                     sizeof(pattern[0]));
1852                 if (!pft->items) {
1853                         rte_flow_tunnel_item_release(
1854                                         port_id, pft->pmd_items,
1855                                         pft->num_pmd_items, &error);
1856                         return NULL;
1857                 }
1858                 rte_memcpy(pft->items, pft->pmd_items,
1859                            pft->num_pmd_items * sizeof(pattern[0]));
1860                 rte_memcpy(pft->items + pft->num_pmd_items, pattern,
1861                            num_items * sizeof(pattern[0]));
1862         }
1863
1864         return pft;
1865 }
1866
1867 static void
1868 port_flow_tunnel_offload_cmd_release(portid_t port_id,
1869                                      const struct tunnel_ops *tunnel_ops,
1870                                      struct port_flow_tunnel *pft)
1871 {
1872         struct rte_flow_error error;
1873
1874         if (tunnel_ops->actions) {
1875                 free(pft->actions);
1876                 rte_flow_tunnel_action_decap_release(
1877                         port_id, pft->pmd_actions,
1878                         pft->num_pmd_actions, &error);
1879                 pft->actions = NULL;
1880                 pft->pmd_actions = NULL;
1881         }
1882         if (tunnel_ops->items) {
1883                 free(pft->items);
1884                 rte_flow_tunnel_item_release(port_id, pft->pmd_items,
1885                                              pft->num_pmd_items,
1886                                              &error);
1887                 pft->items = NULL;
1888                 pft->pmd_items = NULL;
1889         }
1890 }
1891
1892 /** Add port meter policy */
1893 int
1894 port_meter_policy_add(portid_t port_id, uint32_t policy_id,
1895                         const struct rte_flow_action *actions)
1896 {
1897         struct rte_mtr_error error;
1898         const struct rte_flow_action *act = actions;
1899         const struct rte_flow_action *start;
1900         struct rte_mtr_meter_policy_params policy;
1901         uint32_t i = 0, act_n;
1902         int ret;
1903
1904         for (i = 0; i < RTE_COLORS; i++) {
1905                 for (act_n = 0, start = act;
1906                         act->type != RTE_FLOW_ACTION_TYPE_END; act++)
1907                         act_n++;
1908                 if (act_n && act->type == RTE_FLOW_ACTION_TYPE_END)
1909                         policy.actions[i] = start;
1910                 else
1911                         policy.actions[i] = NULL;
1912                 act++;
1913         }
1914         ret = rte_mtr_meter_policy_add(port_id,
1915                         policy_id,
1916                         &policy, &error);
1917         if (ret)
1918                 print_mtr_err_msg(&error);
1919         return ret;
1920 }
1921
1922 /** Validate flow rule. */
1923 int
1924 port_flow_validate(portid_t port_id,
1925                    const struct rte_flow_attr *attr,
1926                    const struct rte_flow_item *pattern,
1927                    const struct rte_flow_action *actions,
1928                    const struct tunnel_ops *tunnel_ops)
1929 {
1930         struct rte_flow_error error;
1931         struct port_flow_tunnel *pft = NULL;
1932         struct rte_port *port;
1933
1934         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1935             port_id == (portid_t)RTE_PORT_ALL)
1936                 return -EINVAL;
1937
1938         port = &ports[port_id];
1939
1940         if (attr->transfer)
1941                 port_id = port->flow_transfer_proxy;
1942
1943         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1944             port_id == (portid_t)RTE_PORT_ALL)
1945                 return -EINVAL;
1946
1947         /* Poisoning to make sure PMDs update it in case of error. */
1948         memset(&error, 0x11, sizeof(error));
1949         if (tunnel_ops->enabled) {
1950                 pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
1951                                                         actions, tunnel_ops);
1952                 if (!pft)
1953                         return -ENOENT;
1954                 if (pft->items)
1955                         pattern = pft->items;
1956                 if (pft->actions)
1957                         actions = pft->actions;
1958         }
1959         if (rte_flow_validate(port_id, attr, pattern, actions, &error))
1960                 return port_flow_complain(&error);
1961         if (tunnel_ops->enabled)
1962                 port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
1963         printf("Flow rule validated\n");
1964         return 0;
1965 }
1966
1967 /** Return age action structure if exists, otherwise NULL. */
1968 static struct rte_flow_action_age *
1969 age_action_get(const struct rte_flow_action *actions)
1970 {
1971         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1972                 switch (actions->type) {
1973                 case RTE_FLOW_ACTION_TYPE_AGE:
1974                         return (struct rte_flow_action_age *)
1975                                 (uintptr_t)actions->conf;
1976                 default:
1977                         break;
1978                 }
1979         }
1980         return NULL;
1981 }
1982
1983 /** Create flow rule. */
1984 int
1985 port_flow_create(portid_t port_id,
1986                  const struct rte_flow_attr *attr,
1987                  const struct rte_flow_item *pattern,
1988                  const struct rte_flow_action *actions,
1989                  const struct tunnel_ops *tunnel_ops)
1990 {
1991         struct rte_flow *flow;
1992         struct rte_port *port;
1993         struct port_flow *pf;
1994         uint32_t id = 0;
1995         struct rte_flow_error error;
1996         struct port_flow_tunnel *pft = NULL;
1997         struct rte_flow_action_age *age = age_action_get(actions);
1998
1999         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2000             port_id == (portid_t)RTE_PORT_ALL)
2001                 return -EINVAL;
2002
2003         port = &ports[port_id];
2004
2005         if (attr->transfer)
2006                 port_id = port->flow_transfer_proxy;
2007
2008         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2009             port_id == (portid_t)RTE_PORT_ALL)
2010                 return -EINVAL;
2011
2012         if (port->flow_list) {
2013                 if (port->flow_list->id == UINT32_MAX) {
2014                         fprintf(stderr,
2015                                 "Highest rule ID is already assigned, delete it first");
2016                         return -ENOMEM;
2017                 }
2018                 id = port->flow_list->id + 1;
2019         }
2020         if (tunnel_ops->enabled) {
2021                 pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
2022                                                         actions, tunnel_ops);
2023                 if (!pft)
2024                         return -ENOENT;
2025                 if (pft->items)
2026                         pattern = pft->items;
2027                 if (pft->actions)
2028                         actions = pft->actions;
2029         }
2030         pf = port_flow_new(attr, pattern, actions, &error);
2031         if (!pf)
2032                 return port_flow_complain(&error);
2033         if (age) {
2034                 pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
2035                 age->context = &pf->age_type;
2036         }
2037         /* Poisoning to make sure PMDs update it in case of error. */
2038         memset(&error, 0x22, sizeof(error));
2039         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
2040         if (!flow) {
2041                 if (tunnel_ops->enabled)
2042                         port_flow_tunnel_offload_cmd_release(port_id,
2043                                                              tunnel_ops, pft);
2044                 free(pf);
2045                 return port_flow_complain(&error);
2046         }
2047         pf->next = port->flow_list;
2048         pf->id = id;
2049         pf->flow = flow;
2050         port->flow_list = pf;
2051         if (tunnel_ops->enabled)
2052                 port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
2053         printf("Flow rule #%u created\n", pf->id);
2054         return 0;
2055 }
2056
2057 /** Destroy a number of flow rules. */
2058 int
2059 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
2060 {
2061         struct rte_port *port;
2062         struct port_flow **tmp;
2063         uint32_t c = 0;
2064         int ret = 0;
2065
2066         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2067             port_id == (portid_t)RTE_PORT_ALL)
2068                 return -EINVAL;
2069         port = &ports[port_id];
2070         tmp = &port->flow_list;
2071         while (*tmp) {
2072                 uint32_t i;
2073
2074                 for (i = 0; i != n; ++i) {
2075                         portid_t port_id_eff = port_id;
2076                         struct rte_flow_error error;
2077                         struct port_flow *pf = *tmp;
2078
2079                         if (rule[i] != pf->id)
2080                                 continue;
2081                         /*
2082                          * Poisoning to make sure PMDs update it in case
2083                          * of error.
2084                          */
2085                         memset(&error, 0x33, sizeof(error));
2086
2087                         if (pf->rule.attr->transfer)
2088                                 port_id_eff = port->flow_transfer_proxy;
2089
2090                         if (port_id_is_invalid(port_id_eff, ENABLED_WARN) ||
2091                             port_id_eff == (portid_t)RTE_PORT_ALL)
2092                                 return -EINVAL;
2093
2094                         if (rte_flow_destroy(port_id_eff, pf->flow, &error)) {
2095                                 ret = port_flow_complain(&error);
2096                                 continue;
2097                         }
2098                         printf("Flow rule #%u destroyed\n", pf->id);
2099                         *tmp = pf->next;
2100                         free(pf);
2101                         break;
2102                 }
2103                 if (i == n)
2104                         tmp = &(*tmp)->next;
2105                 ++c;
2106         }
2107         return ret;
2108 }
2109
2110 /** Remove all flow rules. */
2111 int
2112 port_flow_flush(portid_t port_id)
2113 {
2114         struct rte_flow_error error;
2115         struct rte_port *port;
2116         int ret = 0;
2117
2118         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2119                 port_id == (portid_t)RTE_PORT_ALL)
2120                 return -EINVAL;
2121
2122         port = &ports[port_id];
2123
2124         if (port->flow_list == NULL)
2125                 return ret;
2126
2127         /* Poisoning to make sure PMDs update it in case of error. */
2128         memset(&error, 0x44, sizeof(error));
2129         if (rte_flow_flush(port_id, &error)) {
2130                 port_flow_complain(&error);
2131         }
2132
2133         while (port->flow_list) {
2134                 struct port_flow *pf = port->flow_list->next;
2135
2136                 free(port->flow_list);
2137                 port->flow_list = pf;
2138         }
2139         return ret;
2140 }
2141
2142 /** Dump flow rules. */
2143 int
2144 port_flow_dump(portid_t port_id, bool dump_all, uint32_t rule_id,
2145                 const char *file_name)
2146 {
2147         int ret = 0;
2148         FILE *file = stdout;
2149         struct rte_flow_error error;
2150         struct rte_port *port;
2151         struct port_flow *pflow;
2152         struct rte_flow *tmpFlow = NULL;
2153         bool found = false;
2154
2155         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2156                 port_id == (portid_t)RTE_PORT_ALL)
2157                 return -EINVAL;
2158
2159         if (!dump_all) {
2160                 port = &ports[port_id];
2161                 pflow = port->flow_list;
2162                 while (pflow) {
2163                         if (rule_id != pflow->id) {
2164                                 pflow = pflow->next;
2165                         } else {
2166                                 tmpFlow = pflow->flow;
2167                                 if (tmpFlow)
2168                                         found = true;
2169                                 break;
2170                         }
2171                 }
2172                 if (found == false) {
2173                         fprintf(stderr, "Failed to dump to flow %d\n", rule_id);
2174                         return -EINVAL;
2175                 }
2176         }
2177
2178         if (file_name && strlen(file_name)) {
2179                 file = fopen(file_name, "w");
2180                 if (!file) {
2181                         fprintf(stderr, "Failed to create file %s: %s\n",
2182                                 file_name, strerror(errno));
2183                         return -errno;
2184                 }
2185         }
2186
2187         if (!dump_all)
2188                 ret = rte_flow_dev_dump(port_id, tmpFlow, file, &error);
2189         else
2190                 ret = rte_flow_dev_dump(port_id, NULL, file, &error);
2191         if (ret) {
2192                 port_flow_complain(&error);
2193                 fprintf(stderr, "Failed to dump flow: %s\n", strerror(-ret));
2194         } else
2195                 printf("Flow dump finished\n");
2196         if (file_name && strlen(file_name))
2197                 fclose(file);
2198         return ret;
2199 }
2200
2201 /** Query a flow rule. */
2202 int
2203 port_flow_query(portid_t port_id, uint32_t rule,
2204                 const struct rte_flow_action *action)
2205 {
2206         struct rte_flow_error error;
2207         struct rte_port *port;
2208         struct port_flow *pf;
2209         const char *name;
2210         union {
2211                 struct rte_flow_query_count count;
2212                 struct rte_flow_action_rss rss_conf;
2213                 struct rte_flow_query_age age;
2214         } query;
2215         int ret;
2216
2217         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2218             port_id == (portid_t)RTE_PORT_ALL)
2219                 return -EINVAL;
2220         port = &ports[port_id];
2221         for (pf = port->flow_list; pf; pf = pf->next)
2222                 if (pf->id == rule)
2223                         break;
2224         if (!pf) {
2225                 fprintf(stderr, "Flow rule #%u not found\n", rule);
2226                 return -ENOENT;
2227         }
2228
2229         if (pf->rule.attr->transfer)
2230                 port_id = port->flow_transfer_proxy;
2231
2232         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2233             port_id == (portid_t)RTE_PORT_ALL)
2234                 return -EINVAL;
2235
2236         ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
2237                             &name, sizeof(name),
2238                             (void *)(uintptr_t)action->type, &error);
2239         if (ret < 0)
2240                 return port_flow_complain(&error);
2241         switch (action->type) {
2242         case RTE_FLOW_ACTION_TYPE_COUNT:
2243         case RTE_FLOW_ACTION_TYPE_RSS:
2244         case RTE_FLOW_ACTION_TYPE_AGE:
2245                 break;
2246         default:
2247                 fprintf(stderr, "Cannot query action type %d (%s)\n",
2248                         action->type, name);
2249                 return -ENOTSUP;
2250         }
2251         /* Poisoning to make sure PMDs update it in case of error. */
2252         memset(&error, 0x55, sizeof(error));
2253         memset(&query, 0, sizeof(query));
2254         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
2255                 return port_flow_complain(&error);
2256         switch (action->type) {
2257         case RTE_FLOW_ACTION_TYPE_COUNT:
2258                 printf("%s:\n"
2259                        " hits_set: %u\n"
2260                        " bytes_set: %u\n"
2261                        " hits: %" PRIu64 "\n"
2262                        " bytes: %" PRIu64 "\n",
2263                        name,
2264                        query.count.hits_set,
2265                        query.count.bytes_set,
2266                        query.count.hits,
2267                        query.count.bytes);
2268                 break;
2269         case RTE_FLOW_ACTION_TYPE_RSS:
2270                 rss_config_display(&query.rss_conf);
2271                 break;
2272         case RTE_FLOW_ACTION_TYPE_AGE:
2273                 printf("%s:\n"
2274                        " aged: %u\n"
2275                        " sec_since_last_hit_valid: %u\n"
2276                        " sec_since_last_hit: %" PRIu32 "\n",
2277                        name,
2278                        query.age.aged,
2279                        query.age.sec_since_last_hit_valid,
2280                        query.age.sec_since_last_hit);
2281                 break;
2282         default:
2283                 fprintf(stderr,
2284                         "Cannot display result for action type %d (%s)\n",
2285                         action->type, name);
2286                 break;
2287         }
2288         return 0;
2289 }
2290
2291 /** List simply and destroy all aged flows. */
2292 void
2293 port_flow_aged(portid_t port_id, uint8_t destroy)
2294 {
2295         void **contexts;
2296         int nb_context, total = 0, idx;
2297         struct rte_flow_error error;
2298         enum age_action_context_type *type;
2299         union {
2300                 struct port_flow *pf;
2301                 struct port_indirect_action *pia;
2302         } ctx;
2303
2304         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2305             port_id == (portid_t)RTE_PORT_ALL)
2306                 return;
2307         total = rte_flow_get_aged_flows(port_id, NULL, 0, &error);
2308         printf("Port %u total aged flows: %d\n", port_id, total);
2309         if (total < 0) {
2310                 port_flow_complain(&error);
2311                 return;
2312         }
2313         if (total == 0)
2314                 return;
2315         contexts = malloc(sizeof(void *) * total);
2316         if (contexts == NULL) {
2317                 fprintf(stderr, "Cannot allocate contexts for aged flow\n");
2318                 return;
2319         }
2320         printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type");
2321         nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error);
2322         if (nb_context != total) {
2323                 fprintf(stderr,
2324                         "Port:%d get aged flows count(%d) != total(%d)\n",
2325                         port_id, nb_context, total);
2326                 free(contexts);
2327                 return;
2328         }
2329         total = 0;
2330         for (idx = 0; idx < nb_context; idx++) {
2331                 if (!contexts[idx]) {
2332                         fprintf(stderr, "Error: get Null context in port %u\n",
2333                                 port_id);
2334                         continue;
2335                 }
2336                 type = (enum age_action_context_type *)contexts[idx];
2337                 switch (*type) {
2338                 case ACTION_AGE_CONTEXT_TYPE_FLOW:
2339                         ctx.pf = container_of(type, struct port_flow, age_type);
2340                         printf("%-20s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32
2341                                                                  "\t%c%c%c\t\n",
2342                                "Flow",
2343                                ctx.pf->id,
2344                                ctx.pf->rule.attr->group,
2345                                ctx.pf->rule.attr->priority,
2346                                ctx.pf->rule.attr->ingress ? 'i' : '-',
2347                                ctx.pf->rule.attr->egress ? 'e' : '-',
2348                                ctx.pf->rule.attr->transfer ? 't' : '-');
2349                         if (destroy && !port_flow_destroy(port_id, 1,
2350                                                           &ctx.pf->id))
2351                                 total++;
2352                         break;
2353                 case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION:
2354                         ctx.pia = container_of(type,
2355                                         struct port_indirect_action, age_type);
2356                         printf("%-20s\t%" PRIu32 "\n", "Indirect action",
2357                                ctx.pia->id);
2358                         break;
2359                 default:
2360                         fprintf(stderr, "Error: invalid context type %u\n",
2361                                 port_id);
2362                         break;
2363                 }
2364         }
2365         printf("\n%d flows destroyed\n", total);
2366         free(contexts);
2367 }
2368
2369 /** List flow rules. */
2370 void
2371 port_flow_list(portid_t port_id, uint32_t n, const uint32_t *group)
2372 {
2373         struct rte_port *port;
2374         struct port_flow *pf;
2375         struct port_flow *list = NULL;
2376         uint32_t i;
2377
2378         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2379             port_id == (portid_t)RTE_PORT_ALL)
2380                 return;
2381         port = &ports[port_id];
2382         if (!port->flow_list)
2383                 return;
2384         /* Sort flows by group, priority and ID. */
2385         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2386                 struct port_flow **tmp;
2387                 const struct rte_flow_attr *curr = pf->rule.attr;
2388
2389                 if (n) {
2390                         /* Filter out unwanted groups. */
2391                         for (i = 0; i != n; ++i)
2392                                 if (curr->group == group[i])
2393                                         break;
2394                         if (i == n)
2395                                 continue;
2396                 }
2397                 for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) {
2398                         const struct rte_flow_attr *comp = (*tmp)->rule.attr;
2399
2400                         if (curr->group > comp->group ||
2401                             (curr->group == comp->group &&
2402                              curr->priority > comp->priority) ||
2403                             (curr->group == comp->group &&
2404                              curr->priority == comp->priority &&
2405                              pf->id > (*tmp)->id))
2406                                 continue;
2407                         break;
2408                 }
2409                 pf->tmp = *tmp;
2410                 *tmp = pf;
2411         }
2412         printf("ID\tGroup\tPrio\tAttr\tRule\n");
2413         for (pf = list; pf != NULL; pf = pf->tmp) {
2414                 const struct rte_flow_item *item = pf->rule.pattern;
2415                 const struct rte_flow_action *action = pf->rule.actions;
2416                 const char *name;
2417
2418                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t",
2419                        pf->id,
2420                        pf->rule.attr->group,
2421                        pf->rule.attr->priority,
2422                        pf->rule.attr->ingress ? 'i' : '-',
2423                        pf->rule.attr->egress ? 'e' : '-',
2424                        pf->rule.attr->transfer ? 't' : '-');
2425                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
2426                         if ((uint32_t)item->type > INT_MAX)
2427                                 name = "PMD_INTERNAL";
2428                         else if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
2429                                           &name, sizeof(name),
2430                                           (void *)(uintptr_t)item->type,
2431                                           NULL) <= 0)
2432                                 name = "[UNKNOWN]";
2433                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
2434                                 printf("%s ", name);
2435                         ++item;
2436                 }
2437                 printf("=>");
2438                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
2439                         if ((uint32_t)action->type > INT_MAX)
2440                                 name = "PMD_INTERNAL";
2441                         else if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
2442                                           &name, sizeof(name),
2443                                           (void *)(uintptr_t)action->type,
2444                                           NULL) <= 0)
2445                                 name = "[UNKNOWN]";
2446                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
2447                                 printf(" %s", name);
2448                         ++action;
2449                 }
2450                 printf("\n");
2451         }
2452 }
2453
2454 /** Restrict ingress traffic to the defined flow rules. */
2455 int
2456 port_flow_isolate(portid_t port_id, int set)
2457 {
2458         struct rte_flow_error error;
2459
2460         /* Poisoning to make sure PMDs update it in case of error. */
2461         memset(&error, 0x66, sizeof(error));
2462         if (rte_flow_isolate(port_id, set, &error))
2463                 return port_flow_complain(&error);
2464         printf("Ingress traffic on port %u is %s to the defined flow rules\n",
2465                port_id,
2466                set ? "now restricted" : "not restricted anymore");
2467         return 0;
2468 }
2469
2470 /*
2471  * RX/TX ring descriptors display functions.
2472  */
2473 int
2474 rx_queue_id_is_invalid(queueid_t rxq_id)
2475 {
2476         if (rxq_id < nb_rxq)
2477                 return 0;
2478         fprintf(stderr, "Invalid RX queue %d (must be < nb_rxq=%d)\n",
2479                 rxq_id, nb_rxq);
2480         return 1;
2481 }
2482
2483 int
2484 tx_queue_id_is_invalid(queueid_t txq_id)
2485 {
2486         if (txq_id < nb_txq)
2487                 return 0;
2488         fprintf(stderr, "Invalid TX queue %d (must be < nb_txq=%d)\n",
2489                 txq_id, nb_txq);
2490         return 1;
2491 }
2492
2493 static int
2494 get_rx_ring_size(portid_t port_id, queueid_t rxq_id, uint16_t *ring_size)
2495 {
2496         struct rte_port *port = &ports[port_id];
2497         struct rte_eth_rxq_info rx_qinfo;
2498         int ret;
2499
2500         ret = rte_eth_rx_queue_info_get(port_id, rxq_id, &rx_qinfo);
2501         if (ret == 0) {
2502                 *ring_size = rx_qinfo.nb_desc;
2503                 return ret;
2504         }
2505
2506         if (ret != -ENOTSUP)
2507                 return ret;
2508         /*
2509          * If the rte_eth_rx_queue_info_get is not support for this PMD,
2510          * ring_size stored in testpmd will be used for validity verification.
2511          * When configure the rxq by rte_eth_rx_queue_setup with nb_rx_desc
2512          * being 0, it will use a default value provided by PMDs to setup this
2513          * rxq. If the default value is 0, it will use the
2514          * RTE_ETH_DEV_FALLBACK_RX_RINGSIZE to setup this rxq.
2515          */
2516         if (port->nb_rx_desc[rxq_id])
2517                 *ring_size = port->nb_rx_desc[rxq_id];
2518         else if (port->dev_info.default_rxportconf.ring_size)
2519                 *ring_size = port->dev_info.default_rxportconf.ring_size;
2520         else
2521                 *ring_size = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
2522         return 0;
2523 }
2524
2525 static int
2526 get_tx_ring_size(portid_t port_id, queueid_t txq_id, uint16_t *ring_size)
2527 {
2528         struct rte_port *port = &ports[port_id];
2529         struct rte_eth_txq_info tx_qinfo;
2530         int ret;
2531
2532         ret = rte_eth_tx_queue_info_get(port_id, txq_id, &tx_qinfo);
2533         if (ret == 0) {
2534                 *ring_size = tx_qinfo.nb_desc;
2535                 return ret;
2536         }
2537
2538         if (ret != -ENOTSUP)
2539                 return ret;
2540         /*
2541          * If the rte_eth_tx_queue_info_get is not support for this PMD,
2542          * ring_size stored in testpmd will be used for validity verification.
2543          * When configure the txq by rte_eth_tx_queue_setup with nb_tx_desc
2544          * being 0, it will use a default value provided by PMDs to setup this
2545          * txq. If the default value is 0, it will use the
2546          * RTE_ETH_DEV_FALLBACK_TX_RINGSIZE to setup this txq.
2547          */
2548         if (port->nb_tx_desc[txq_id])
2549                 *ring_size = port->nb_tx_desc[txq_id];
2550         else if (port->dev_info.default_txportconf.ring_size)
2551                 *ring_size = port->dev_info.default_txportconf.ring_size;
2552         else
2553                 *ring_size = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
2554         return 0;
2555 }
2556
2557 static int
2558 rx_desc_id_is_invalid(portid_t port_id, queueid_t rxq_id, uint16_t rxdesc_id)
2559 {
2560         uint16_t ring_size;
2561         int ret;
2562
2563         ret = get_rx_ring_size(port_id, rxq_id, &ring_size);
2564         if (ret)
2565                 return 1;
2566
2567         if (rxdesc_id < ring_size)
2568                 return 0;
2569
2570         fprintf(stderr, "Invalid RX descriptor %u (must be < ring_size=%u)\n",
2571                 rxdesc_id, ring_size);
2572         return 1;
2573 }
2574
2575 static int
2576 tx_desc_id_is_invalid(portid_t port_id, queueid_t txq_id, uint16_t txdesc_id)
2577 {
2578         uint16_t ring_size;
2579         int ret;
2580
2581         ret = get_tx_ring_size(port_id, txq_id, &ring_size);
2582         if (ret)
2583                 return 1;
2584
2585         if (txdesc_id < ring_size)
2586                 return 0;
2587
2588         fprintf(stderr, "Invalid TX descriptor %u (must be < ring_size=%u)\n",
2589                 txdesc_id, ring_size);
2590         return 1;
2591 }
2592
2593 static const struct rte_memzone *
2594 ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id)
2595 {
2596         char mz_name[RTE_MEMZONE_NAMESIZE];
2597         const struct rte_memzone *mz;
2598
2599         snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s",
2600                         port_id, q_id, ring_name);
2601         mz = rte_memzone_lookup(mz_name);
2602         if (mz == NULL)
2603                 fprintf(stderr,
2604                         "%s ring memory zoneof (port %d, queue %d) not found (zone name = %s\n",
2605                         ring_name, port_id, q_id, mz_name);
2606         return mz;
2607 }
2608
2609 union igb_ring_dword {
2610         uint64_t dword;
2611         struct {
2612 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
2613                 uint32_t lo;
2614                 uint32_t hi;
2615 #else
2616                 uint32_t hi;
2617                 uint32_t lo;
2618 #endif
2619         } words;
2620 };
2621
2622 struct igb_ring_desc_32_bytes {
2623         union igb_ring_dword lo_dword;
2624         union igb_ring_dword hi_dword;
2625         union igb_ring_dword resv1;
2626         union igb_ring_dword resv2;
2627 };
2628
2629 struct igb_ring_desc_16_bytes {
2630         union igb_ring_dword lo_dword;
2631         union igb_ring_dword hi_dword;
2632 };
2633
2634 static void
2635 ring_rxd_display_dword(union igb_ring_dword dword)
2636 {
2637         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
2638                                         (unsigned)dword.words.hi);
2639 }
2640
2641 static void
2642 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
2643 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
2644                            portid_t port_id,
2645 #else
2646                            __rte_unused portid_t port_id,
2647 #endif
2648                            uint16_t desc_id)
2649 {
2650         struct igb_ring_desc_16_bytes *ring =
2651                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
2652 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
2653         int ret;
2654         struct rte_eth_dev_info dev_info;
2655
2656         ret = eth_dev_info_get_print_err(port_id, &dev_info);
2657         if (ret != 0)
2658                 return;
2659
2660         if (strstr(dev_info.driver_name, "i40e") != NULL) {
2661                 /* 32 bytes RX descriptor, i40e only */
2662                 struct igb_ring_desc_32_bytes *ring =
2663                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
2664                 ring[desc_id].lo_dword.dword =
2665                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
2666                 ring_rxd_display_dword(ring[desc_id].lo_dword);
2667                 ring[desc_id].hi_dword.dword =
2668                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
2669                 ring_rxd_display_dword(ring[desc_id].hi_dword);
2670                 ring[desc_id].resv1.dword =
2671                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
2672                 ring_rxd_display_dword(ring[desc_id].resv1);
2673                 ring[desc_id].resv2.dword =
2674                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
2675                 ring_rxd_display_dword(ring[desc_id].resv2);
2676
2677                 return;
2678         }
2679 #endif
2680         /* 16 bytes RX descriptor */
2681         ring[desc_id].lo_dword.dword =
2682                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
2683         ring_rxd_display_dword(ring[desc_id].lo_dword);
2684         ring[desc_id].hi_dword.dword =
2685                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
2686         ring_rxd_display_dword(ring[desc_id].hi_dword);
2687 }
2688
2689 static void
2690 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
2691 {
2692         struct igb_ring_desc_16_bytes *ring;
2693         struct igb_ring_desc_16_bytes txd;
2694
2695         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
2696         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
2697         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
2698         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
2699                         (unsigned)txd.lo_dword.words.lo,
2700                         (unsigned)txd.lo_dword.words.hi,
2701                         (unsigned)txd.hi_dword.words.lo,
2702                         (unsigned)txd.hi_dword.words.hi);
2703 }
2704
2705 void
2706 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
2707 {
2708         const struct rte_memzone *rx_mz;
2709
2710         if (rx_desc_id_is_invalid(port_id, rxq_id, rxd_id))
2711                 return;
2712         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
2713         if (rx_mz == NULL)
2714                 return;
2715         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
2716 }
2717
2718 void
2719 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
2720 {
2721         const struct rte_memzone *tx_mz;
2722
2723         if (tx_desc_id_is_invalid(port_id, txq_id, txd_id))
2724                 return;
2725         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
2726         if (tx_mz == NULL)
2727                 return;
2728         ring_tx_descriptor_display(tx_mz, txd_id);
2729 }
2730
2731 void
2732 fwd_lcores_config_display(void)
2733 {
2734         lcoreid_t lc_id;
2735
2736         printf("List of forwarding lcores:");
2737         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
2738                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
2739         printf("\n");
2740 }
2741 void
2742 rxtx_config_display(void)
2743 {
2744         portid_t pid;
2745         queueid_t qid;
2746
2747         printf("  %s packet forwarding%s packets/burst=%d\n",
2748                cur_fwd_eng->fwd_mode_name,
2749                retry_enabled == 0 ? "" : " with retry",
2750                nb_pkt_per_burst);
2751
2752         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
2753                 printf("  packet len=%u - nb packet segments=%d\n",
2754                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
2755
2756         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
2757                nb_fwd_lcores, nb_fwd_ports);
2758
2759         RTE_ETH_FOREACH_DEV(pid) {
2760                 struct rte_eth_rxconf *rx_conf = &ports[pid].rx_conf[0];
2761                 struct rte_eth_txconf *tx_conf = &ports[pid].tx_conf[0];
2762                 uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0];
2763                 uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0];
2764                 struct rte_eth_rxq_info rx_qinfo;
2765                 struct rte_eth_txq_info tx_qinfo;
2766                 uint16_t rx_free_thresh_tmp;
2767                 uint16_t tx_free_thresh_tmp;
2768                 uint16_t tx_rs_thresh_tmp;
2769                 uint16_t nb_rx_desc_tmp;
2770                 uint16_t nb_tx_desc_tmp;
2771                 uint64_t offloads_tmp;
2772                 uint8_t pthresh_tmp;
2773                 uint8_t hthresh_tmp;
2774                 uint8_t wthresh_tmp;
2775                 int32_t rc;
2776
2777                 /* per port config */
2778                 printf("  port %d: RX queue number: %d Tx queue number: %d\n",
2779                                 (unsigned int)pid, nb_rxq, nb_txq);
2780
2781                 printf("    Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n",
2782                                 ports[pid].dev_conf.rxmode.offloads,
2783                                 ports[pid].dev_conf.txmode.offloads);
2784
2785                 /* per rx queue config only for first queue to be less verbose */
2786                 for (qid = 0; qid < 1; qid++) {
2787                         rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo);
2788                         if (rc) {
2789                                 nb_rx_desc_tmp = nb_rx_desc[qid];
2790                                 rx_free_thresh_tmp =
2791                                         rx_conf[qid].rx_free_thresh;
2792                                 pthresh_tmp = rx_conf[qid].rx_thresh.pthresh;
2793                                 hthresh_tmp = rx_conf[qid].rx_thresh.hthresh;
2794                                 wthresh_tmp = rx_conf[qid].rx_thresh.wthresh;
2795                                 offloads_tmp = rx_conf[qid].offloads;
2796                         } else {
2797                                 nb_rx_desc_tmp = rx_qinfo.nb_desc;
2798                                 rx_free_thresh_tmp =
2799                                                 rx_qinfo.conf.rx_free_thresh;
2800                                 pthresh_tmp = rx_qinfo.conf.rx_thresh.pthresh;
2801                                 hthresh_tmp = rx_qinfo.conf.rx_thresh.hthresh;
2802                                 wthresh_tmp = rx_qinfo.conf.rx_thresh.wthresh;
2803                                 offloads_tmp = rx_qinfo.conf.offloads;
2804                         }
2805
2806                         printf("    RX queue: %d\n", qid);
2807                         printf("      RX desc=%d - RX free threshold=%d\n",
2808                                 nb_rx_desc_tmp, rx_free_thresh_tmp);
2809                         printf("      RX threshold registers: pthresh=%d hthresh=%d "
2810                                 " wthresh=%d\n",
2811                                 pthresh_tmp, hthresh_tmp, wthresh_tmp);
2812                         printf("      RX Offloads=0x%"PRIx64"\n", offloads_tmp);
2813                 }
2814
2815                 /* per tx queue config only for first queue to be less verbose */
2816                 for (qid = 0; qid < 1; qid++) {
2817                         rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo);
2818                         if (rc) {
2819                                 nb_tx_desc_tmp = nb_tx_desc[qid];
2820                                 tx_free_thresh_tmp =
2821                                         tx_conf[qid].tx_free_thresh;
2822                                 pthresh_tmp = tx_conf[qid].tx_thresh.pthresh;
2823                                 hthresh_tmp = tx_conf[qid].tx_thresh.hthresh;
2824                                 wthresh_tmp = tx_conf[qid].tx_thresh.wthresh;
2825                                 offloads_tmp = tx_conf[qid].offloads;
2826                                 tx_rs_thresh_tmp = tx_conf[qid].tx_rs_thresh;
2827                         } else {
2828                                 nb_tx_desc_tmp = tx_qinfo.nb_desc;
2829                                 tx_free_thresh_tmp =
2830                                                 tx_qinfo.conf.tx_free_thresh;
2831                                 pthresh_tmp = tx_qinfo.conf.tx_thresh.pthresh;
2832                                 hthresh_tmp = tx_qinfo.conf.tx_thresh.hthresh;
2833                                 wthresh_tmp = tx_qinfo.conf.tx_thresh.wthresh;
2834                                 offloads_tmp = tx_qinfo.conf.offloads;
2835                                 tx_rs_thresh_tmp = tx_qinfo.conf.tx_rs_thresh;
2836                         }
2837
2838                         printf("    TX queue: %d\n", qid);
2839                         printf("      TX desc=%d - TX free threshold=%d\n",
2840                                 nb_tx_desc_tmp, tx_free_thresh_tmp);
2841                         printf("      TX threshold registers: pthresh=%d hthresh=%d "
2842                                 " wthresh=%d\n",
2843                                 pthresh_tmp, hthresh_tmp, wthresh_tmp);
2844                         printf("      TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n",
2845                                 offloads_tmp, tx_rs_thresh_tmp);
2846                 }
2847         }
2848 }
2849
2850 void
2851 port_rss_reta_info(portid_t port_id,
2852                    struct rte_eth_rss_reta_entry64 *reta_conf,
2853                    uint16_t nb_entries)
2854 {
2855         uint16_t i, idx, shift;
2856         int ret;
2857
2858         if (port_id_is_invalid(port_id, ENABLED_WARN))
2859                 return;
2860
2861         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
2862         if (ret != 0) {
2863                 fprintf(stderr,
2864                         "Failed to get RSS RETA info, return code = %d\n",
2865                         ret);
2866                 return;
2867         }
2868
2869         for (i = 0; i < nb_entries; i++) {
2870                 idx = i / RTE_RETA_GROUP_SIZE;
2871                 shift = i % RTE_RETA_GROUP_SIZE;
2872                 if (!(reta_conf[idx].mask & (1ULL << shift)))
2873                         continue;
2874                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
2875                                         i, reta_conf[idx].reta[shift]);
2876         }
2877 }
2878
2879 /*
2880  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
2881  * key of the port.
2882  */
2883 void
2884 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
2885 {
2886         struct rte_eth_rss_conf rss_conf = {0};
2887         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
2888         uint64_t rss_hf;
2889         uint8_t i;
2890         int diag;
2891         struct rte_eth_dev_info dev_info;
2892         uint8_t hash_key_size;
2893         int ret;
2894
2895         if (port_id_is_invalid(port_id, ENABLED_WARN))
2896                 return;
2897
2898         ret = eth_dev_info_get_print_err(port_id, &dev_info);
2899         if (ret != 0)
2900                 return;
2901
2902         if (dev_info.hash_key_size > 0 &&
2903                         dev_info.hash_key_size <= sizeof(rss_key))
2904                 hash_key_size = dev_info.hash_key_size;
2905         else {
2906                 fprintf(stderr,
2907                         "dev_info did not provide a valid hash key size\n");
2908                 return;
2909         }
2910
2911         /* Get RSS hash key if asked to display it */
2912         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
2913         rss_conf.rss_key_len = hash_key_size;
2914         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2915         if (diag != 0) {
2916                 switch (diag) {
2917                 case -ENODEV:
2918                         fprintf(stderr, "port index %d invalid\n", port_id);
2919                         break;
2920                 case -ENOTSUP:
2921                         fprintf(stderr, "operation not supported by device\n");
2922                         break;
2923                 default:
2924                         fprintf(stderr, "operation failed - diag=%d\n", diag);
2925                         break;
2926                 }
2927                 return;
2928         }
2929         rss_hf = rss_conf.rss_hf;
2930         if (rss_hf == 0) {
2931                 printf("RSS disabled\n");
2932                 return;
2933         }
2934         printf("RSS functions:\n ");
2935         for (i = 0; rss_type_table[i].str; i++) {
2936                 if (rss_hf & rss_type_table[i].rss_type)
2937                         printf("%s ", rss_type_table[i].str);
2938         }
2939         printf("\n");
2940         if (!show_rss_key)
2941                 return;
2942         printf("RSS key:\n");
2943         for (i = 0; i < hash_key_size; i++)
2944                 printf("%02X", rss_key[i]);
2945         printf("\n");
2946 }
2947
2948 void
2949 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
2950                          uint8_t hash_key_len)
2951 {
2952         struct rte_eth_rss_conf rss_conf;
2953         int diag;
2954         unsigned int i;
2955
2956         rss_conf.rss_key = NULL;
2957         rss_conf.rss_key_len = hash_key_len;
2958         rss_conf.rss_hf = 0;
2959         for (i = 0; rss_type_table[i].str; i++) {
2960                 if (!strcmp(rss_type_table[i].str, rss_type))
2961                         rss_conf.rss_hf = rss_type_table[i].rss_type;
2962         }
2963         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2964         if (diag == 0) {
2965                 rss_conf.rss_key = hash_key;
2966                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
2967         }
2968         if (diag == 0)
2969                 return;
2970
2971         switch (diag) {
2972         case -ENODEV:
2973                 fprintf(stderr, "port index %d invalid\n", port_id);
2974                 break;
2975         case -ENOTSUP:
2976                 fprintf(stderr, "operation not supported by device\n");
2977                 break;
2978         default:
2979                 fprintf(stderr, "operation failed - diag=%d\n", diag);
2980                 break;
2981         }
2982 }
2983
2984 /*
2985  * Setup forwarding configuration for each logical core.
2986  */
2987 static void
2988 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
2989 {
2990         streamid_t nb_fs_per_lcore;
2991         streamid_t nb_fs;
2992         streamid_t sm_id;
2993         lcoreid_t  nb_extra;
2994         lcoreid_t  nb_fc;
2995         lcoreid_t  nb_lc;
2996         lcoreid_t  lc_id;
2997
2998         nb_fs = cfg->nb_fwd_streams;
2999         nb_fc = cfg->nb_fwd_lcores;
3000         if (nb_fs <= nb_fc) {
3001                 nb_fs_per_lcore = 1;
3002                 nb_extra = 0;
3003         } else {
3004                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
3005                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
3006         }
3007
3008         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
3009         sm_id = 0;
3010         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
3011                 fwd_lcores[lc_id]->stream_idx = sm_id;
3012                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
3013                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
3014         }
3015
3016         /*
3017          * Assign extra remaining streams, if any.
3018          */
3019         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
3020         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
3021                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
3022                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
3023                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
3024         }
3025 }
3026
3027 static portid_t
3028 fwd_topology_tx_port_get(portid_t rxp)
3029 {
3030         static int warning_once = 1;
3031
3032         RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports);
3033
3034         switch (port_topology) {
3035         default:
3036         case PORT_TOPOLOGY_PAIRED:
3037                 if ((rxp & 0x1) == 0) {
3038                         if (rxp + 1 < cur_fwd_config.nb_fwd_ports)
3039                                 return rxp + 1;
3040                         if (warning_once) {
3041                                 fprintf(stderr,
3042                                         "\nWarning! port-topology=paired and odd forward ports number, the last port will pair with itself.\n\n");
3043                                 warning_once = 0;
3044                         }
3045                         return rxp;
3046                 }
3047                 return rxp - 1;
3048         case PORT_TOPOLOGY_CHAINED:
3049                 return (rxp + 1) % cur_fwd_config.nb_fwd_ports;
3050         case PORT_TOPOLOGY_LOOP:
3051                 return rxp;
3052         }
3053 }
3054
3055 static void
3056 simple_fwd_config_setup(void)
3057 {
3058         portid_t i;
3059
3060         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
3061         cur_fwd_config.nb_fwd_streams =
3062                 (streamid_t) cur_fwd_config.nb_fwd_ports;
3063
3064         /* reinitialize forwarding streams */
3065         init_fwd_streams();
3066
3067         /*
3068          * In the simple forwarding test, the number of forwarding cores
3069          * must be lower or equal to the number of forwarding ports.
3070          */
3071         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
3072         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
3073                 cur_fwd_config.nb_fwd_lcores =
3074                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
3075         setup_fwd_config_of_each_lcore(&cur_fwd_config);
3076
3077         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
3078                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
3079                 fwd_streams[i]->rx_queue  = 0;
3080                 fwd_streams[i]->tx_port   =
3081                                 fwd_ports_ids[fwd_topology_tx_port_get(i)];
3082                 fwd_streams[i]->tx_queue  = 0;
3083                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
3084                 fwd_streams[i]->retry_enabled = retry_enabled;
3085         }
3086 }
3087
3088 /**
3089  * For the RSS forwarding test all streams distributed over lcores. Each stream
3090  * being composed of a RX queue to poll on a RX port for input messages,
3091  * associated with a TX queue of a TX port where to send forwarded packets.
3092  */
3093 static void
3094 rss_fwd_config_setup(void)
3095 {
3096         portid_t   rxp;
3097         portid_t   txp;
3098         queueid_t  rxq;
3099         queueid_t  nb_q;
3100         streamid_t  sm_id;
3101         int start;
3102         int end;
3103
3104         nb_q = nb_rxq;
3105         if (nb_q > nb_txq)
3106                 nb_q = nb_txq;
3107         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
3108         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
3109         cur_fwd_config.nb_fwd_streams =
3110                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
3111
3112         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
3113                 cur_fwd_config.nb_fwd_lcores =
3114                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
3115
3116         /* reinitialize forwarding streams */
3117         init_fwd_streams();
3118
3119         setup_fwd_config_of_each_lcore(&cur_fwd_config);
3120
3121         if (proc_id > 0 && nb_q % num_procs != 0)
3122                 printf("Warning! queue numbers should be multiple of processes, or packet loss will happen.\n");
3123
3124         /**
3125          * In multi-process, All queues are allocated to different
3126          * processes based on num_procs and proc_id. For example:
3127          * if supports 4 queues(nb_q), 2 processes(num_procs),
3128          * the 0~1 queue for primary process.
3129          * the 2~3 queue for secondary process.
3130          */
3131         start = proc_id * nb_q / num_procs;
3132         end = start + nb_q / num_procs;
3133         rxp = 0;
3134         rxq = start;
3135         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
3136                 struct fwd_stream *fs;
3137
3138                 fs = fwd_streams[sm_id];
3139                 txp = fwd_topology_tx_port_get(rxp);
3140                 fs->rx_port = fwd_ports_ids[rxp];
3141                 fs->rx_queue = rxq;
3142                 fs->tx_port = fwd_ports_ids[txp];
3143                 fs->tx_queue = rxq;
3144                 fs->peer_addr = fs->tx_port;
3145                 fs->retry_enabled = retry_enabled;
3146                 rxp++;
3147                 if (rxp < nb_fwd_ports)
3148                         continue;
3149                 rxp = 0;
3150                 rxq++;
3151                 if (rxq >= end)
3152                         rxq = start;
3153         }
3154 }
3155
3156 static uint16_t
3157 get_fwd_port_total_tc_num(void)
3158 {
3159         struct rte_eth_dcb_info dcb_info;
3160         uint16_t total_tc_num = 0;
3161         unsigned int i;
3162
3163         for (i = 0; i < nb_fwd_ports; i++) {
3164                 (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[i], &dcb_info);
3165                 total_tc_num += dcb_info.nb_tcs;
3166         }
3167
3168         return total_tc_num;
3169 }
3170
3171 /**
3172  * For the DCB forwarding test, each core is assigned on each traffic class.
3173  *
3174  * Each core is assigned a multi-stream, each stream being composed of
3175  * a RX queue to poll on a RX port for input messages, associated with
3176  * a TX queue of a TX port where to send forwarded packets. All RX and
3177  * TX queues are mapping to the same traffic class.
3178  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
3179  * the same core
3180  */
3181 static void
3182 dcb_fwd_config_setup(void)
3183 {
3184         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
3185         portid_t txp, rxp = 0;
3186         queueid_t txq, rxq = 0;
3187         lcoreid_t  lc_id;
3188         uint16_t nb_rx_queue, nb_tx_queue;
3189         uint16_t i, j, k, sm_id = 0;
3190         uint16_t total_tc_num;
3191         struct rte_port *port;
3192         uint8_t tc = 0;
3193         portid_t pid;
3194         int ret;
3195
3196         /*
3197          * The fwd_config_setup() is called when the port is RTE_PORT_STARTED
3198          * or RTE_PORT_STOPPED.
3199          *
3200          * Re-configure ports to get updated mapping between tc and queue in
3201          * case the queue number of the port is changed. Skip for started ports
3202          * since modifying queue number and calling dev_configure need to stop
3203          * ports first.
3204          */
3205         for (pid = 0; pid < nb_fwd_ports; pid++) {
3206                 if (port_is_started(pid) == 1)
3207                         continue;
3208
3209                 port = &ports[pid];
3210                 ret = rte_eth_dev_configure(pid, nb_rxq, nb_txq,
3211                                             &port->dev_conf);
3212                 if (ret < 0) {
3213                         fprintf(stderr,
3214                                 "Failed to re-configure port %d, ret = %d.\n",
3215                                 pid, ret);
3216                         return;
3217                 }
3218         }
3219
3220         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
3221         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
3222         cur_fwd_config.nb_fwd_streams =
3223                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
3224         total_tc_num = get_fwd_port_total_tc_num();
3225         if (cur_fwd_config.nb_fwd_lcores > total_tc_num)
3226                 cur_fwd_config.nb_fwd_lcores = total_tc_num;
3227
3228         /* reinitialize forwarding streams */
3229         init_fwd_streams();
3230         sm_id = 0;
3231         txp = 1;
3232         /* get the dcb info on the first RX and TX ports */
3233         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
3234         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
3235
3236         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
3237                 fwd_lcores[lc_id]->stream_nb = 0;
3238                 fwd_lcores[lc_id]->stream_idx = sm_id;
3239                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
3240                         /* if the nb_queue is zero, means this tc is
3241                          * not enabled on the POOL
3242                          */
3243                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
3244                                 break;
3245                         k = fwd_lcores[lc_id]->stream_nb +
3246                                 fwd_lcores[lc_id]->stream_idx;
3247                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
3248                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
3249                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
3250                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
3251                         for (j = 0; j < nb_rx_queue; j++) {
3252                                 struct fwd_stream *fs;
3253
3254                                 fs = fwd_streams[k + j];
3255                                 fs->rx_port = fwd_ports_ids[rxp];
3256                                 fs->rx_queue = rxq + j;
3257                                 fs->tx_port = fwd_ports_ids[txp];
3258                                 fs->tx_queue = txq + j % nb_tx_queue;
3259                                 fs->peer_addr = fs->tx_port;
3260                                 fs->retry_enabled = retry_enabled;
3261                         }
3262                         fwd_lcores[lc_id]->stream_nb +=
3263                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
3264                 }
3265                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
3266
3267                 tc++;
3268                 if (tc < rxp_dcb_info.nb_tcs)
3269                         continue;
3270                 /* Restart from TC 0 on next RX port */
3271                 tc = 0;
3272                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
3273                         rxp = (portid_t)
3274                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
3275                 else
3276                         rxp++;
3277                 if (rxp >= nb_fwd_ports)
3278                         return;
3279                 /* get the dcb information on next RX and TX ports */
3280                 if ((rxp & 0x1) == 0)
3281                         txp = (portid_t) (rxp + 1);
3282                 else
3283                         txp = (portid_t) (rxp - 1);
3284                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
3285                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
3286         }
3287 }
3288
3289 static void
3290 icmp_echo_config_setup(void)
3291 {
3292         portid_t  rxp;
3293         queueid_t rxq;
3294         lcoreid_t lc_id;
3295         uint16_t  sm_id;
3296
3297         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
3298                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
3299                         (nb_txq * nb_fwd_ports);
3300         else
3301                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
3302         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
3303         cur_fwd_config.nb_fwd_streams =
3304                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
3305         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
3306                 cur_fwd_config.nb_fwd_lcores =
3307                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
3308         if (verbose_level > 0) {
3309                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
3310                        __FUNCTION__,
3311                        cur_fwd_config.nb_fwd_lcores,
3312                        cur_fwd_config.nb_fwd_ports,
3313                        cur_fwd_config.nb_fwd_streams);
3314         }
3315
3316         /* reinitialize forwarding streams */
3317         init_fwd_streams();
3318         setup_fwd_config_of_each_lcore(&cur_fwd_config);
3319         rxp = 0; rxq = 0;
3320         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
3321                 if (verbose_level > 0)
3322                         printf("  core=%d: \n", lc_id);
3323                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
3324                         struct fwd_stream *fs;
3325                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
3326                         fs->rx_port = fwd_ports_ids[rxp];
3327                         fs->rx_queue = rxq;
3328                         fs->tx_port = fs->rx_port;
3329                         fs->tx_queue = rxq;
3330                         fs->peer_addr = fs->tx_port;
3331                         fs->retry_enabled = retry_enabled;
3332                         if (verbose_level > 0)
3333                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
3334                                        sm_id, fs->rx_port, fs->rx_queue,
3335                                        fs->tx_queue);
3336                         rxq = (queueid_t) (rxq + 1);
3337                         if (rxq == nb_rxq) {
3338                                 rxq = 0;
3339                                 rxp = (portid_t) (rxp + 1);
3340                         }
3341                 }
3342         }
3343 }
3344
3345 void
3346 fwd_config_setup(void)
3347 {
3348         struct rte_port *port;
3349         portid_t pt_id;
3350         unsigned int i;
3351
3352         cur_fwd_config.fwd_eng = cur_fwd_eng;
3353         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
3354                 icmp_echo_config_setup();
3355                 return;
3356         }
3357
3358         if ((nb_rxq > 1) && (nb_txq > 1)){
3359                 if (dcb_config) {
3360                         for (i = 0; i < nb_fwd_ports; i++) {
3361                                 pt_id = fwd_ports_ids[i];
3362                                 port = &ports[pt_id];
3363                                 if (!port->dcb_flag) {
3364                                         fprintf(stderr,
3365                                                 "In DCB mode, all forwarding ports must be configured in this mode.\n");
3366                                         return;
3367                                 }
3368                         }
3369                         if (nb_fwd_lcores == 1) {
3370                                 fprintf(stderr,
3371                                         "In DCB mode,the nb forwarding cores should be larger than 1.\n");
3372                                 return;
3373                         }
3374
3375                         dcb_fwd_config_setup();
3376                 } else
3377                         rss_fwd_config_setup();
3378         }
3379         else
3380                 simple_fwd_config_setup();
3381 }
3382
3383 static const char *
3384 mp_alloc_to_str(uint8_t mode)
3385 {
3386         switch (mode) {
3387         case MP_ALLOC_NATIVE:
3388                 return "native";
3389         case MP_ALLOC_ANON:
3390                 return "anon";
3391         case MP_ALLOC_XMEM:
3392                 return "xmem";
3393         case MP_ALLOC_XMEM_HUGE:
3394                 return "xmemhuge";
3395         case MP_ALLOC_XBUF:
3396                 return "xbuf";
3397         default:
3398                 return "invalid";
3399         }
3400 }
3401
3402 void
3403 pkt_fwd_config_display(struct fwd_config *cfg)
3404 {
3405         struct fwd_stream *fs;
3406         lcoreid_t  lc_id;
3407         streamid_t sm_id;
3408
3409         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
3410                 "NUMA support %s, MP allocation mode: %s\n",
3411                 cfg->fwd_eng->fwd_mode_name,
3412                 retry_enabled == 0 ? "" : " with retry",
3413                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
3414                 numa_support == 1 ? "enabled" : "disabled",
3415                 mp_alloc_to_str(mp_alloc_type));
3416
3417         if (retry_enabled)
3418                 printf("TX retry num: %u, delay between TX retries: %uus\n",
3419                         burst_tx_retry_num, burst_tx_delay_time);
3420         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
3421                 printf("Logical Core %u (socket %u) forwards packets on "
3422                        "%d streams:",
3423                        fwd_lcores_cpuids[lc_id],
3424                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
3425                        fwd_lcores[lc_id]->stream_nb);
3426                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
3427                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
3428                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
3429                                "P=%d/Q=%d (socket %u) ",
3430                                fs->rx_port, fs->rx_queue,
3431                                ports[fs->rx_port].socket_id,
3432                                fs->tx_port, fs->tx_queue,
3433                                ports[fs->tx_port].socket_id);
3434                         print_ethaddr("peer=",
3435                                       &peer_eth_addrs[fs->peer_addr]);
3436                 }
3437                 printf("\n");
3438         }
3439         printf("\n");
3440 }
3441
3442 void
3443 set_fwd_eth_peer(portid_t port_id, char *peer_addr)
3444 {
3445         struct rte_ether_addr new_peer_addr;
3446         if (!rte_eth_dev_is_valid_port(port_id)) {
3447                 fprintf(stderr, "Error: Invalid port number %i\n", port_id);
3448                 return;
3449         }
3450         if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) {
3451                 fprintf(stderr, "Error: Invalid ethernet address: %s\n",
3452                         peer_addr);
3453                 return;
3454         }
3455         peer_eth_addrs[port_id] = new_peer_addr;
3456 }
3457
3458 int
3459 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
3460 {
3461         unsigned int i;
3462         unsigned int lcore_cpuid;
3463         int record_now;
3464
3465         record_now = 0;
3466  again:
3467         for (i = 0; i < nb_lc; i++) {
3468                 lcore_cpuid = lcorelist[i];
3469                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
3470                         fprintf(stderr, "lcore %u not enabled\n", lcore_cpuid);
3471                         return -1;
3472                 }
3473                 if (lcore_cpuid == rte_get_main_lcore()) {
3474                         fprintf(stderr,
3475                                 "lcore %u cannot be masked on for running packet forwarding, which is the main lcore and reserved for command line parsing only\n",
3476                                 lcore_cpuid);
3477                         return -1;
3478                 }
3479                 if (record_now)
3480                         fwd_lcores_cpuids[i] = lcore_cpuid;
3481         }
3482         if (record_now == 0) {
3483                 record_now = 1;
3484                 goto again;
3485         }
3486         nb_cfg_lcores = (lcoreid_t) nb_lc;
3487         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
3488                 printf("previous number of forwarding cores %u - changed to "
3489                        "number of configured cores %u\n",
3490                        (unsigned int) nb_fwd_lcores, nb_lc);
3491                 nb_fwd_lcores = (lcoreid_t) nb_lc;
3492         }
3493
3494         return 0;
3495 }
3496
3497 int
3498 set_fwd_lcores_mask(uint64_t lcoremask)
3499 {
3500         unsigned int lcorelist[64];
3501         unsigned int nb_lc;
3502         unsigned int i;
3503
3504         if (lcoremask == 0) {
3505                 fprintf(stderr, "Invalid NULL mask of cores\n");
3506                 return -1;
3507         }
3508         nb_lc = 0;
3509         for (i = 0; i < 64; i++) {
3510                 if (! ((uint64_t)(1ULL << i) & lcoremask))
3511                         continue;
3512                 lcorelist[nb_lc++] = i;
3513         }
3514         return set_fwd_lcores_list(lcorelist, nb_lc);
3515 }
3516
3517 void
3518 set_fwd_lcores_number(uint16_t nb_lc)
3519 {
3520         if (test_done == 0) {
3521                 fprintf(stderr, "Please stop forwarding first\n");
3522                 return;
3523         }
3524         if (nb_lc > nb_cfg_lcores) {
3525                 fprintf(stderr,
3526                         "nb fwd cores %u > %u (max. number of configured lcores) - ignored\n",
3527                         (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
3528                 return;
3529         }
3530         nb_fwd_lcores = (lcoreid_t) nb_lc;
3531         printf("Number of forwarding cores set to %u\n",
3532                (unsigned int) nb_fwd_lcores);
3533 }
3534
3535 void
3536 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
3537 {
3538         unsigned int i;
3539         portid_t port_id;
3540         int record_now;
3541
3542         record_now = 0;
3543  again:
3544         for (i = 0; i < nb_pt; i++) {
3545                 port_id = (portid_t) portlist[i];
3546                 if (port_id_is_invalid(port_id, ENABLED_WARN))
3547                         return;
3548                 if (record_now)
3549                         fwd_ports_ids[i] = port_id;
3550         }
3551         if (record_now == 0) {
3552                 record_now = 1;
3553                 goto again;
3554         }
3555         nb_cfg_ports = (portid_t) nb_pt;
3556         if (nb_fwd_ports != (portid_t) nb_pt) {
3557                 printf("previous number of forwarding ports %u - changed to "
3558                        "number of configured ports %u\n",
3559                        (unsigned int) nb_fwd_ports, nb_pt);
3560                 nb_fwd_ports = (portid_t) nb_pt;
3561         }
3562 }
3563
3564 /**
3565  * Parse the user input and obtain the list of forwarding ports
3566  *
3567  * @param[in] list
3568  *   String containing the user input. User can specify
3569  *   in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6.
3570  *   For example, if the user wants to use all the available
3571  *   4 ports in his system, then the input can be 0-3 or 0,1,2,3.
3572  *   If the user wants to use only the ports 1,2 then the input
3573  *   is 1,2.
3574  *   valid characters are '-' and ','
3575  * @param[out] values
3576  *   This array will be filled with a list of port IDs
3577  *   based on the user input
3578  *   Note that duplicate entries are discarded and only the first
3579  *   count entries in this array are port IDs and all the rest
3580  *   will contain default values
3581  * @param[in] maxsize
3582  *   This parameter denotes 2 things
3583  *   1) Number of elements in the values array
3584  *   2) Maximum value of each element in the values array
3585  * @return
3586  *   On success, returns total count of parsed port IDs
3587  *   On failure, returns 0
3588  */
3589 static unsigned int
3590 parse_port_list(const char *list, unsigned int *values, unsigned int maxsize)
3591 {
3592         unsigned int count = 0;
3593         char *end = NULL;
3594         int min, max;
3595         int value, i;
3596         unsigned int marked[maxsize];
3597
3598         if (list == NULL || values == NULL)
3599                 return 0;
3600
3601         for (i = 0; i < (int)maxsize; i++)
3602                 marked[i] = 0;
3603
3604         min = INT_MAX;
3605
3606         do {
3607                 /*Remove the blank spaces if any*/
3608                 while (isblank(*list))
3609                         list++;
3610                 if (*list == '\0')
3611                         break;
3612                 errno = 0;
3613                 value = strtol(list, &end, 10);
3614                 if (errno || end == NULL)
3615                         return 0;
3616                 if (value < 0 || value >= (int)maxsize)
3617                         return 0;
3618                 while (isblank(*end))
3619                         end++;
3620                 if (*end == '-' && min == INT_MAX) {
3621                         min = value;
3622                 } else if ((*end == ',') || (*end == '\0')) {
3623                         max = value;
3624                         if (min == INT_MAX)
3625                                 min = value;
3626                         for (i = min; i <= max; i++) {
3627                                 if (count < maxsize) {
3628                                         if (marked[i])
3629                                                 continue;
3630                                         values[count] = i;
3631                                         marked[i] = 1;
3632                                         count++;
3633                                 }
3634                         }
3635                         min = INT_MAX;
3636                 } else
3637                         return 0;
3638                 list = end + 1;
3639         } while (*end != '\0');
3640
3641         return count;
3642 }
3643
3644 void
3645 parse_fwd_portlist(const char *portlist)
3646 {
3647         unsigned int portcount;
3648         unsigned int portindex[RTE_MAX_ETHPORTS];
3649         unsigned int i, valid_port_count = 0;
3650
3651         portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS);
3652         if (!portcount)
3653                 rte_exit(EXIT_FAILURE, "Invalid fwd port list\n");
3654
3655         /*
3656          * Here we verify the validity of the ports
3657          * and thereby calculate the total number of
3658          * valid ports
3659          */
3660         for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) {
3661                 if (rte_eth_dev_is_valid_port(portindex[i])) {
3662                         portindex[valid_port_count] = portindex[i];
3663                         valid_port_count++;
3664                 }
3665         }
3666
3667         set_fwd_ports_list(portindex, valid_port_count);
3668 }
3669
3670 void
3671 set_fwd_ports_mask(uint64_t portmask)
3672 {
3673         unsigned int portlist[64];
3674         unsigned int nb_pt;
3675         unsigned int i;
3676
3677         if (portmask == 0) {
3678                 fprintf(stderr, "Invalid NULL mask of ports\n");
3679                 return;
3680         }
3681         nb_pt = 0;
3682         RTE_ETH_FOREACH_DEV(i) {
3683                 if (! ((uint64_t)(1ULL << i) & portmask))
3684                         continue;
3685                 portlist[nb_pt++] = i;
3686         }
3687         set_fwd_ports_list(portlist, nb_pt);
3688 }
3689
3690 void
3691 set_fwd_ports_number(uint16_t nb_pt)
3692 {
3693         if (nb_pt > nb_cfg_ports) {
3694                 fprintf(stderr,
3695                         "nb fwd ports %u > %u (number of configured ports) - ignored\n",
3696                         (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
3697                 return;
3698         }
3699         nb_fwd_ports = (portid_t) nb_pt;
3700         printf("Number of forwarding ports set to %u\n",
3701                (unsigned int) nb_fwd_ports);
3702 }
3703
3704 int
3705 port_is_forwarding(portid_t port_id)
3706 {
3707         unsigned int i;
3708
3709         if (port_id_is_invalid(port_id, ENABLED_WARN))
3710                 return -1;
3711
3712         for (i = 0; i < nb_fwd_ports; i++) {
3713                 if (fwd_ports_ids[i] == port_id)
3714                         return 1;
3715         }
3716
3717         return 0;
3718 }
3719
3720 void
3721 set_nb_pkt_per_burst(uint16_t nb)
3722 {
3723         if (nb > MAX_PKT_BURST) {
3724                 fprintf(stderr,
3725                         "nb pkt per burst: %u > %u (maximum packet per burst)  ignored\n",
3726                         (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
3727                 return;
3728         }
3729         nb_pkt_per_burst = nb;
3730         printf("Number of packets per burst set to %u\n",
3731                (unsigned int) nb_pkt_per_burst);
3732 }
3733
3734 static const char *
3735 tx_split_get_name(enum tx_pkt_split split)
3736 {
3737         uint32_t i;
3738
3739         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
3740                 if (tx_split_name[i].split == split)
3741                         return tx_split_name[i].name;
3742         }
3743         return NULL;
3744 }
3745
3746 void
3747 set_tx_pkt_split(const char *name)
3748 {
3749         uint32_t i;
3750
3751         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
3752                 if (strcmp(tx_split_name[i].name, name) == 0) {
3753                         tx_pkt_split = tx_split_name[i].split;
3754                         return;
3755                 }
3756         }
3757         fprintf(stderr, "unknown value: \"%s\"\n", name);
3758 }
3759
3760 int
3761 parse_fec_mode(const char *name, uint32_t *fec_capa)
3762 {
3763         uint8_t i;
3764
3765         for (i = 0; i < RTE_DIM(fec_mode_name); i++) {
3766                 if (strcmp(fec_mode_name[i].name, name) == 0) {
3767                         *fec_capa =
3768                                 RTE_ETH_FEC_MODE_TO_CAPA(fec_mode_name[i].mode);
3769                         return 0;
3770                 }
3771         }
3772         return -1;
3773 }
3774
3775 void
3776 show_fec_capability(unsigned int num, struct rte_eth_fec_capa *speed_fec_capa)
3777 {
3778         unsigned int i, j;
3779
3780         printf("FEC capabilities:\n");
3781
3782         for (i = 0; i < num; i++) {
3783                 printf("%s : ",
3784                         rte_eth_link_speed_to_str(speed_fec_capa[i].speed));
3785
3786                 for (j = 0; j < RTE_DIM(fec_mode_name); j++) {
3787                         if (RTE_ETH_FEC_MODE_TO_CAPA(j) &
3788                                                 speed_fec_capa[i].capa)
3789                                 printf("%s ", fec_mode_name[j].name);
3790                 }
3791                 printf("\n");
3792         }
3793 }
3794
3795 void
3796 show_rx_pkt_offsets(void)
3797 {
3798         uint32_t i, n;
3799
3800         n = rx_pkt_nb_offs;
3801         printf("Number of offsets: %u\n", n);
3802         if (n) {
3803                 printf("Segment offsets: ");
3804                 for (i = 0; i != n - 1; i++)
3805                         printf("%hu,", rx_pkt_seg_offsets[i]);
3806                 printf("%hu\n", rx_pkt_seg_lengths[i]);
3807         }
3808 }
3809
3810 void
3811 set_rx_pkt_offsets(unsigned int *seg_offsets, unsigned int nb_offs)
3812 {
3813         unsigned int i;
3814
3815         if (nb_offs >= MAX_SEGS_BUFFER_SPLIT) {
3816                 printf("nb segments per RX packets=%u >= "
3817                        "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_offs);
3818                 return;
3819         }
3820
3821         /*
3822          * No extra check here, the segment length will be checked by PMD
3823          * in the extended queue setup.
3824          */
3825         for (i = 0; i < nb_offs; i++) {
3826                 if (seg_offsets[i] >= UINT16_MAX) {
3827                         printf("offset[%u]=%u > UINT16_MAX - give up\n",
3828                                i, seg_offsets[i]);
3829                         return;
3830                 }
3831         }
3832
3833         for (i = 0; i < nb_offs; i++)
3834                 rx_pkt_seg_offsets[i] = (uint16_t) seg_offsets[i];
3835
3836         rx_pkt_nb_offs = (uint8_t) nb_offs;
3837 }
3838
3839 void
3840 show_rx_pkt_segments(void)
3841 {
3842         uint32_t i, n;
3843
3844         n = rx_pkt_nb_segs;
3845         printf("Number of segments: %u\n", n);
3846         if (n) {
3847                 printf("Segment sizes: ");
3848                 for (i = 0; i != n - 1; i++)
3849                         printf("%hu,", rx_pkt_seg_lengths[i]);
3850                 printf("%hu\n", rx_pkt_seg_lengths[i]);
3851         }
3852 }
3853
3854 void
3855 set_rx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
3856 {
3857         unsigned int i;
3858
3859         if (nb_segs >= MAX_SEGS_BUFFER_SPLIT) {
3860                 printf("nb segments per RX packets=%u >= "
3861                        "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs);
3862                 return;
3863         }
3864
3865         /*
3866          * No extra check here, the segment length will be checked by PMD
3867          * in the extended queue setup.
3868          */
3869         for (i = 0; i < nb_segs; i++) {
3870                 if (seg_lengths[i] >= UINT16_MAX) {
3871                         printf("length[%u]=%u > UINT16_MAX - give up\n",
3872                                i, seg_lengths[i]);
3873                         return;
3874                 }
3875         }
3876
3877         for (i = 0; i < nb_segs; i++)
3878                 rx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
3879
3880         rx_pkt_nb_segs = (uint8_t) nb_segs;
3881 }
3882
3883 void
3884 show_tx_pkt_segments(void)
3885 {
3886         uint32_t i, n;
3887         const char *split;
3888
3889         n = tx_pkt_nb_segs;
3890         split = tx_split_get_name(tx_pkt_split);
3891
3892         printf("Number of segments: %u\n", n);
3893         printf("Segment sizes: ");
3894         for (i = 0; i != n - 1; i++)
3895                 printf("%hu,", tx_pkt_seg_lengths[i]);
3896         printf("%hu\n", tx_pkt_seg_lengths[i]);
3897         printf("Split packet: %s\n", split);
3898 }
3899
3900 static bool
3901 nb_segs_is_invalid(unsigned int nb_segs)
3902 {
3903         uint16_t ring_size;
3904         uint16_t queue_id;
3905         uint16_t port_id;
3906         int ret;
3907
3908         RTE_ETH_FOREACH_DEV(port_id) {
3909                 for (queue_id = 0; queue_id < nb_txq; queue_id++) {
3910                         ret = get_tx_ring_size(port_id, queue_id, &ring_size);
3911                         if (ret) {
3912                                 /* Port may not be initialized yet, can't say
3913                                  * the port is invalid in this stage.
3914                                  */
3915                                 continue;
3916                         }
3917                         if (ring_size < nb_segs) {
3918                                 printf("nb segments per TX packets=%u >= TX "
3919                                        "queue(%u) ring_size=%u - txpkts ignored\n",
3920                                        nb_segs, queue_id, ring_size);
3921                                 return true;
3922                         }
3923                 }
3924         }
3925
3926         return false;
3927 }
3928
3929 void
3930 set_tx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
3931 {
3932         uint16_t tx_pkt_len;
3933         unsigned int i;
3934
3935         /*
3936          * For single segment settings failed check is ignored.
3937          * It is a very basic capability to send the single segment
3938          * packets, suppose it is always supported.
3939          */
3940         if (nb_segs > 1 && nb_segs_is_invalid(nb_segs)) {
3941                 fprintf(stderr,
3942                         "Tx segment size(%u) is not supported - txpkts ignored\n",
3943                         nb_segs);
3944                 return;
3945         }
3946
3947         if (nb_segs > RTE_MAX_SEGS_PER_PKT) {
3948                 fprintf(stderr,
3949                         "Tx segment size(%u) is bigger than max number of segment(%u)\n",
3950                         nb_segs, RTE_MAX_SEGS_PER_PKT);
3951                 return;
3952         }
3953
3954         /*
3955          * Check that each segment length is greater or equal than
3956          * the mbuf data size.
3957          * Check also that the total packet length is greater or equal than the
3958          * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) +
3959          * 20 + 8).
3960          */
3961         tx_pkt_len = 0;
3962         for (i = 0; i < nb_segs; i++) {
3963                 if (seg_lengths[i] > mbuf_data_size[0]) {
3964                         fprintf(stderr,
3965                                 "length[%u]=%u > mbuf_data_size=%u - give up\n",
3966                                 i, seg_lengths[i], mbuf_data_size[0]);
3967                         return;
3968                 }
3969                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
3970         }
3971         if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) {
3972                 fprintf(stderr, "total packet length=%u < %d - give up\n",
3973                                 (unsigned) tx_pkt_len,
3974                                 (int)(sizeof(struct rte_ether_hdr) + 20 + 8));
3975                 return;
3976         }
3977
3978         for (i = 0; i < nb_segs; i++)
3979                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
3980
3981         tx_pkt_length  = tx_pkt_len;
3982         tx_pkt_nb_segs = (uint8_t) nb_segs;
3983 }
3984
3985 void
3986 show_tx_pkt_times(void)
3987 {
3988         printf("Interburst gap: %u\n", tx_pkt_times_inter);
3989         printf("Intraburst gap: %u\n", tx_pkt_times_intra);
3990 }
3991
3992 void
3993 set_tx_pkt_times(unsigned int *tx_times)
3994 {
3995         tx_pkt_times_inter = tx_times[0];
3996         tx_pkt_times_intra = tx_times[1];
3997 }
3998
3999 void
4000 setup_gro(const char *onoff, portid_t port_id)
4001 {
4002         if (!rte_eth_dev_is_valid_port(port_id)) {
4003                 fprintf(stderr, "invalid port id %u\n", port_id);
4004                 return;
4005         }
4006         if (test_done == 0) {
4007                 fprintf(stderr,
4008                         "Before enable/disable GRO, please stop forwarding first\n");
4009                 return;
4010         }
4011         if (strcmp(onoff, "on") == 0) {
4012                 if (gro_ports[port_id].enable != 0) {
4013                         fprintf(stderr,
4014                                 "Port %u has enabled GRO. Please disable GRO first\n",
4015                                 port_id);
4016                         return;
4017                 }
4018                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
4019                         gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
4020                         gro_ports[port_id].param.max_flow_num =
4021                                 GRO_DEFAULT_FLOW_NUM;
4022                         gro_ports[port_id].param.max_item_per_flow =
4023                                 GRO_DEFAULT_ITEM_NUM_PER_FLOW;
4024                 }
4025                 gro_ports[port_id].enable = 1;
4026         } else {
4027                 if (gro_ports[port_id].enable == 0) {
4028                         fprintf(stderr, "Port %u has disabled GRO\n", port_id);
4029                         return;
4030                 }
4031                 gro_ports[port_id].enable = 0;
4032         }
4033 }
4034
4035 void
4036 setup_gro_flush_cycles(uint8_t cycles)
4037 {
4038         if (test_done == 0) {
4039                 fprintf(stderr,
4040                         "Before change flush interval for GRO, please stop forwarding first.\n");
4041                 return;
4042         }
4043
4044         if (cycles > GRO_MAX_FLUSH_CYCLES || cycles <
4045                         GRO_DEFAULT_FLUSH_CYCLES) {
4046                 fprintf(stderr,
4047                         "The flushing cycle be in the range of 1 to %u. Revert to the default value %u.\n",
4048                         GRO_MAX_FLUSH_CYCLES, GRO_DEFAULT_FLUSH_CYCLES);
4049                 cycles = GRO_DEFAULT_FLUSH_CYCLES;
4050         }
4051
4052         gro_flush_cycles = cycles;
4053 }
4054
4055 void
4056 show_gro(portid_t port_id)
4057 {
4058         struct rte_gro_param *param;
4059         uint32_t max_pkts_num;
4060
4061         param = &gro_ports[port_id].param;
4062
4063         if (!rte_eth_dev_is_valid_port(port_id)) {
4064                 fprintf(stderr, "Invalid port id %u.\n", port_id);
4065                 return;
4066         }
4067         if (gro_ports[port_id].enable) {
4068                 printf("GRO type: TCP/IPv4\n");
4069                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
4070                         max_pkts_num = param->max_flow_num *
4071                                 param->max_item_per_flow;
4072                 } else
4073                         max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES;
4074                 printf("Max number of packets to perform GRO: %u\n",
4075                                 max_pkts_num);
4076                 printf("Flushing cycles: %u\n", gro_flush_cycles);
4077         } else
4078                 printf("Port %u doesn't enable GRO.\n", port_id);
4079 }
4080
4081 void
4082 setup_gso(const char *mode, portid_t port_id)
4083 {
4084         if (!rte_eth_dev_is_valid_port(port_id)) {
4085                 fprintf(stderr, "invalid port id %u\n", port_id);
4086                 return;
4087         }
4088         if (strcmp(mode, "on") == 0) {
4089                 if (test_done == 0) {
4090                         fprintf(stderr,
4091                                 "before enabling GSO, please stop forwarding first\n");
4092                         return;
4093                 }
4094                 gso_ports[port_id].enable = 1;
4095         } else if (strcmp(mode, "off") == 0) {
4096                 if (test_done == 0) {
4097                         fprintf(stderr,
4098                                 "before disabling GSO, please stop forwarding first\n");
4099                         return;
4100                 }
4101                 gso_ports[port_id].enable = 0;
4102         }
4103 }
4104
4105 char*
4106 list_pkt_forwarding_modes(void)
4107 {
4108         static char fwd_modes[128] = "";
4109         const char *separator = "|";
4110         struct fwd_engine *fwd_eng;
4111         unsigned i = 0;
4112
4113         if (strlen (fwd_modes) == 0) {
4114                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
4115                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
4116                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
4117                         strncat(fwd_modes, separator,
4118                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
4119                 }
4120                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
4121         }
4122
4123         return fwd_modes;
4124 }
4125
4126 char*
4127 list_pkt_forwarding_retry_modes(void)
4128 {
4129         static char fwd_modes[128] = "";
4130         const char *separator = "|";
4131         struct fwd_engine *fwd_eng;
4132         unsigned i = 0;
4133
4134         if (strlen(fwd_modes) == 0) {
4135                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
4136                         if (fwd_eng == &rx_only_engine)
4137                                 continue;
4138                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
4139                                         sizeof(fwd_modes) -
4140                                         strlen(fwd_modes) - 1);
4141                         strncat(fwd_modes, separator,
4142                                         sizeof(fwd_modes) -
4143                                         strlen(fwd_modes) - 1);
4144                 }
4145                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
4146         }
4147
4148         return fwd_modes;
4149 }
4150
4151 void
4152 set_pkt_forwarding_mode(const char *fwd_mode_name)
4153 {
4154         struct fwd_engine *fwd_eng;
4155         unsigned i;
4156
4157         i = 0;
4158         while ((fwd_eng = fwd_engines[i]) != NULL) {
4159                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
4160                         printf("Set %s packet forwarding mode%s\n",
4161                                fwd_mode_name,
4162                                retry_enabled == 0 ? "" : " with retry");
4163                         cur_fwd_eng = fwd_eng;
4164                         return;
4165                 }
4166                 i++;
4167         }
4168         fprintf(stderr, "Invalid %s packet forwarding mode\n", fwd_mode_name);
4169 }
4170
4171 void
4172 add_rx_dump_callbacks(portid_t portid)
4173 {
4174         struct rte_eth_dev_info dev_info;
4175         uint16_t queue;
4176         int ret;
4177
4178         if (port_id_is_invalid(portid, ENABLED_WARN))
4179                 return;
4180
4181         ret = eth_dev_info_get_print_err(portid, &dev_info);
4182         if (ret != 0)
4183                 return;
4184
4185         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
4186                 if (!ports[portid].rx_dump_cb[queue])
4187                         ports[portid].rx_dump_cb[queue] =
4188                                 rte_eth_add_rx_callback(portid, queue,
4189                                         dump_rx_pkts, NULL);
4190 }
4191
4192 void
4193 add_tx_dump_callbacks(portid_t portid)
4194 {
4195         struct rte_eth_dev_info dev_info;
4196         uint16_t queue;
4197         int ret;
4198
4199         if (port_id_is_invalid(portid, ENABLED_WARN))
4200                 return;
4201
4202         ret = eth_dev_info_get_print_err(portid, &dev_info);
4203         if (ret != 0)
4204                 return;
4205
4206         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
4207                 if (!ports[portid].tx_dump_cb[queue])
4208                         ports[portid].tx_dump_cb[queue] =
4209                                 rte_eth_add_tx_callback(portid, queue,
4210                                                         dump_tx_pkts, NULL);
4211 }
4212
4213 void
4214 remove_rx_dump_callbacks(portid_t portid)
4215 {
4216         struct rte_eth_dev_info dev_info;
4217         uint16_t queue;
4218         int ret;
4219
4220         if (port_id_is_invalid(portid, ENABLED_WARN))
4221                 return;
4222
4223         ret = eth_dev_info_get_print_err(portid, &dev_info);
4224         if (ret != 0)
4225                 return;
4226
4227         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
4228                 if (ports[portid].rx_dump_cb[queue]) {
4229                         rte_eth_remove_rx_callback(portid, queue,
4230                                 ports[portid].rx_dump_cb[queue]);
4231                         ports[portid].rx_dump_cb[queue] = NULL;
4232                 }
4233 }
4234
4235 void
4236 remove_tx_dump_callbacks(portid_t portid)
4237 {
4238         struct rte_eth_dev_info dev_info;
4239         uint16_t queue;
4240         int ret;
4241
4242         if (port_id_is_invalid(portid, ENABLED_WARN))
4243                 return;
4244
4245         ret = eth_dev_info_get_print_err(portid, &dev_info);
4246         if (ret != 0)
4247                 return;
4248
4249         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
4250                 if (ports[portid].tx_dump_cb[queue]) {
4251                         rte_eth_remove_tx_callback(portid, queue,
4252                                 ports[portid].tx_dump_cb[queue]);
4253                         ports[portid].tx_dump_cb[queue] = NULL;
4254                 }
4255 }
4256
4257 void
4258 configure_rxtx_dump_callbacks(uint16_t verbose)
4259 {
4260         portid_t portid;
4261
4262 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4263                 TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n");
4264                 return;
4265 #endif
4266
4267         RTE_ETH_FOREACH_DEV(portid)
4268         {
4269                 if (verbose == 1 || verbose > 2)
4270                         add_rx_dump_callbacks(portid);
4271                 else
4272                         remove_rx_dump_callbacks(portid);
4273                 if (verbose >= 2)
4274                         add_tx_dump_callbacks(portid);
4275                 else
4276                         remove_tx_dump_callbacks(portid);
4277         }
4278 }
4279
4280 void
4281 set_verbose_level(uint16_t vb_level)
4282 {
4283         printf("Change verbose level from %u to %u\n",
4284                (unsigned int) verbose_level, (unsigned int) vb_level);
4285         verbose_level = vb_level;
4286         configure_rxtx_dump_callbacks(verbose_level);
4287 }
4288
4289 void
4290 vlan_extend_set(portid_t port_id, int on)
4291 {
4292         int diag;
4293         int vlan_offload;
4294         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
4295
4296         if (port_id_is_invalid(port_id, ENABLED_WARN))
4297                 return;
4298
4299         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
4300
4301         if (on) {
4302                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
4303                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
4304         } else {
4305                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
4306                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
4307         }
4308
4309         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
4310         if (diag < 0) {
4311                 fprintf(stderr,
4312                         "rx_vlan_extend_set(port_pi=%d, on=%d) failed diag=%d\n",
4313                         port_id, on, diag);
4314                 return;
4315         }
4316         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
4317 }
4318
4319 void
4320 rx_vlan_strip_set(portid_t port_id, int on)
4321 {
4322         int diag;
4323         int vlan_offload;
4324         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
4325
4326         if (port_id_is_invalid(port_id, ENABLED_WARN))
4327                 return;
4328
4329         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
4330
4331         if (on) {
4332                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
4333                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
4334         } else {
4335                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
4336                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
4337         }
4338
4339         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
4340         if (diag < 0) {
4341                 fprintf(stderr,
4342                         "%s(port_pi=%d, on=%d) failed diag=%d\n",
4343                         __func__, port_id, on, diag);
4344                 return;
4345         }
4346         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
4347 }
4348
4349 void
4350 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
4351 {
4352         int diag;
4353
4354         if (port_id_is_invalid(port_id, ENABLED_WARN))
4355                 return;
4356
4357         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
4358         if (diag < 0)
4359                 fprintf(stderr,
4360                         "%s(port_pi=%d, queue_id=%d, on=%d) failed diag=%d\n",
4361                         __func__, port_id, queue_id, on, diag);
4362 }
4363
4364 void
4365 rx_vlan_filter_set(portid_t port_id, int on)
4366 {
4367         int diag;
4368         int vlan_offload;
4369         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
4370
4371         if (port_id_is_invalid(port_id, ENABLED_WARN))
4372                 return;
4373
4374         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
4375
4376         if (on) {
4377                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
4378                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
4379         } else {
4380                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
4381                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
4382         }
4383
4384         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
4385         if (diag < 0) {
4386                 fprintf(stderr,
4387                         "%s(port_pi=%d, on=%d) failed diag=%d\n",
4388                         __func__, port_id, on, diag);
4389                 return;
4390         }
4391         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
4392 }
4393
4394 void
4395 rx_vlan_qinq_strip_set(portid_t port_id, int on)
4396 {
4397         int diag;
4398         int vlan_offload;
4399         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
4400
4401         if (port_id_is_invalid(port_id, ENABLED_WARN))
4402                 return;
4403
4404         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
4405
4406         if (on) {
4407                 vlan_offload |= ETH_QINQ_STRIP_OFFLOAD;
4408                 port_rx_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
4409         } else {
4410                 vlan_offload &= ~ETH_QINQ_STRIP_OFFLOAD;
4411                 port_rx_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
4412         }
4413
4414         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
4415         if (diag < 0) {
4416                 fprintf(stderr, "%s(port_pi=%d, on=%d) failed diag=%d\n",
4417                         __func__, port_id, on, diag);
4418                 return;
4419         }
4420         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
4421 }
4422
4423 int
4424 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
4425 {
4426         int diag;
4427
4428         if (port_id_is_invalid(port_id, ENABLED_WARN))
4429                 return 1;
4430         if (vlan_id_is_invalid(vlan_id))
4431                 return 1;
4432         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
4433         if (diag == 0)
4434                 return 0;
4435         fprintf(stderr,
4436                 "rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed diag=%d\n",
4437                 port_id, vlan_id, on, diag);
4438         return -1;
4439 }
4440
4441 void
4442 rx_vlan_all_filter_set(portid_t port_id, int on)
4443 {
4444         uint16_t vlan_id;
4445
4446         if (port_id_is_invalid(port_id, ENABLED_WARN))
4447                 return;
4448         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
4449                 if (rx_vft_set(port_id, vlan_id, on))
4450                         break;
4451         }
4452 }
4453
4454 void
4455 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
4456 {
4457         int diag;
4458
4459         if (port_id_is_invalid(port_id, ENABLED_WARN))
4460                 return;
4461
4462         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
4463         if (diag == 0)
4464                 return;
4465
4466         fprintf(stderr,
4467                 "tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed diag=%d\n",
4468                 port_id, vlan_type, tp_id, diag);
4469 }
4470
4471 void
4472 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
4473 {
4474         struct rte_eth_dev_info dev_info;
4475         int ret;
4476
4477         if (vlan_id_is_invalid(vlan_id))
4478                 return;
4479
4480         if (ports[port_id].dev_conf.txmode.offloads &
4481             DEV_TX_OFFLOAD_QINQ_INSERT) {
4482                 fprintf(stderr, "Error, as QinQ has been enabled.\n");
4483                 return;
4484         }
4485
4486         ret = eth_dev_info_get_print_err(port_id, &dev_info);
4487         if (ret != 0)
4488                 return;
4489
4490         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT) == 0) {
4491                 fprintf(stderr,
4492                         "Error: vlan insert is not supported by port %d\n",
4493                         port_id);
4494                 return;
4495         }
4496
4497         tx_vlan_reset(port_id);
4498         ports[port_id].dev_conf.txmode.offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
4499         ports[port_id].tx_vlan_id = vlan_id;
4500 }
4501
4502 void
4503 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
4504 {
4505         struct rte_eth_dev_info dev_info;
4506         int ret;
4507
4508         if (vlan_id_is_invalid(vlan_id))
4509                 return;
4510         if (vlan_id_is_invalid(vlan_id_outer))
4511                 return;
4512
4513         ret = eth_dev_info_get_print_err(port_id, &dev_info);
4514         if (ret != 0)
4515                 return;
4516
4517         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_QINQ_INSERT) == 0) {
4518                 fprintf(stderr,
4519                         "Error: qinq insert not supported by port %d\n",
4520                         port_id);
4521                 return;
4522         }
4523
4524         tx_vlan_reset(port_id);
4525         ports[port_id].dev_conf.txmode.offloads |= (DEV_TX_OFFLOAD_VLAN_INSERT |
4526                                                     DEV_TX_OFFLOAD_QINQ_INSERT);
4527         ports[port_id].tx_vlan_id = vlan_id;
4528         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
4529 }
4530
4531 void
4532 tx_vlan_reset(portid_t port_id)
4533 {
4534         ports[port_id].dev_conf.txmode.offloads &=
4535                                 ~(DEV_TX_OFFLOAD_VLAN_INSERT |
4536                                   DEV_TX_OFFLOAD_QINQ_INSERT);
4537         ports[port_id].tx_vlan_id = 0;
4538         ports[port_id].tx_vlan_id_outer = 0;
4539 }
4540
4541 void
4542 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
4543 {
4544         if (port_id_is_invalid(port_id, ENABLED_WARN))
4545                 return;
4546
4547         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
4548 }
4549
4550 void
4551 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
4552 {
4553         int ret;
4554
4555         if (port_id_is_invalid(port_id, ENABLED_WARN))
4556                 return;
4557
4558         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
4559                 return;
4560
4561         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4562                 fprintf(stderr, "map_value not in required range 0..%d\n",
4563                         RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
4564                 return;
4565         }
4566
4567         if (!is_rx) { /* tx */
4568                 ret = rte_eth_dev_set_tx_queue_stats_mapping(port_id, queue_id,
4569                                                              map_value);
4570                 if (ret) {
4571                         fprintf(stderr,
4572                                 "failed to set tx queue stats mapping.\n");
4573                         return;
4574                 }
4575         } else { /* rx */
4576                 ret = rte_eth_dev_set_rx_queue_stats_mapping(port_id, queue_id,
4577                                                              map_value);
4578                 if (ret) {
4579                         fprintf(stderr,
4580                                 "failed to set rx queue stats mapping.\n");
4581                         return;
4582                 }
4583         }
4584 }
4585
4586 void
4587 set_xstats_hide_zero(uint8_t on_off)
4588 {
4589         xstats_hide_zero = on_off;
4590 }
4591
4592 void
4593 set_record_core_cycles(uint8_t on_off)
4594 {
4595         record_core_cycles = on_off;
4596 }
4597
4598 void
4599 set_record_burst_stats(uint8_t on_off)
4600 {
4601         record_burst_stats = on_off;
4602 }
4603
4604 static char*
4605 flowtype_to_str(uint16_t flow_type)
4606 {
4607         struct flow_type_info {
4608                 char str[32];
4609                 uint16_t ftype;
4610         };
4611
4612         uint8_t i;
4613         static struct flow_type_info flowtype_str_table[] = {
4614                 {"raw", RTE_ETH_FLOW_RAW},
4615                 {"ipv4", RTE_ETH_FLOW_IPV4},
4616                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
4617                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
4618                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
4619                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
4620                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
4621                 {"ipv6", RTE_ETH_FLOW_IPV6},
4622                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
4623                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
4624                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
4625                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
4626                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
4627                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
4628                 {"port", RTE_ETH_FLOW_PORT},
4629                 {"vxlan", RTE_ETH_FLOW_VXLAN},
4630                 {"geneve", RTE_ETH_FLOW_GENEVE},
4631                 {"nvgre", RTE_ETH_FLOW_NVGRE},
4632                 {"vxlan-gpe", RTE_ETH_FLOW_VXLAN_GPE},
4633         };
4634
4635         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
4636                 if (flowtype_str_table[i].ftype == flow_type)
4637                         return flowtype_str_table[i].str;
4638         }
4639
4640         return NULL;
4641 }
4642
4643 #if defined(RTE_NET_I40E) || defined(RTE_NET_IXGBE)
4644
4645 static inline void
4646 print_fdir_mask(struct rte_eth_fdir_masks *mask)
4647 {
4648         printf("\n    vlan_tci: 0x%04x", rte_be_to_cpu_16(mask->vlan_tci_mask));
4649
4650         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
4651                 printf(", mac_addr: 0x%02x, tunnel_type: 0x%01x,"
4652                         " tunnel_id: 0x%08x",
4653                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
4654                         rte_be_to_cpu_32(mask->tunnel_id_mask));
4655         else if (fdir_conf.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
4656                 printf(", src_ipv4: 0x%08x, dst_ipv4: 0x%08x",
4657                         rte_be_to_cpu_32(mask->ipv4_mask.src_ip),
4658                         rte_be_to_cpu_32(mask->ipv4_mask.dst_ip));
4659
4660                 printf("\n    src_port: 0x%04x, dst_port: 0x%04x",
4661                         rte_be_to_cpu_16(mask->src_port_mask),
4662                         rte_be_to_cpu_16(mask->dst_port_mask));
4663
4664                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
4665                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[0]),
4666                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[1]),
4667                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[2]),
4668                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[3]));
4669
4670                 printf("\n    dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
4671                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[0]),
4672                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[1]),
4673                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[2]),
4674                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[3]));
4675         }
4676
4677         printf("\n");
4678 }
4679
4680 static inline void
4681 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
4682 {
4683         struct rte_eth_flex_payload_cfg *cfg;
4684         uint32_t i, j;
4685
4686         for (i = 0; i < flex_conf->nb_payloads; i++) {
4687                 cfg = &flex_conf->flex_set[i];
4688                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
4689                         printf("\n    RAW:  ");
4690                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
4691                         printf("\n    L2_PAYLOAD:  ");
4692                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
4693                         printf("\n    L3_PAYLOAD:  ");
4694                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
4695                         printf("\n    L4_PAYLOAD:  ");
4696                 else
4697                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
4698                 for (j = 0; j < num; j++)
4699                         printf("  %-5u", cfg->src_offset[j]);
4700         }
4701         printf("\n");
4702 }
4703
4704 static inline void
4705 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
4706 {
4707         struct rte_eth_fdir_flex_mask *mask;
4708         uint32_t i, j;
4709         char *p;
4710
4711         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
4712                 mask = &flex_conf->flex_mask[i];
4713                 p = flowtype_to_str(mask->flow_type);
4714                 printf("\n    %s:\t", p ? p : "unknown");
4715                 for (j = 0; j < num; j++)
4716                         printf(" %02x", mask->mask[j]);
4717         }
4718         printf("\n");
4719 }
4720
4721 static inline void
4722 print_fdir_flow_type(uint32_t flow_types_mask)
4723 {
4724         int i;
4725         char *p;
4726
4727         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
4728                 if (!(flow_types_mask & (1 << i)))
4729                         continue;
4730                 p = flowtype_to_str(i);
4731                 if (p)
4732                         printf(" %s", p);
4733                 else
4734                         printf(" unknown");
4735         }
4736         printf("\n");
4737 }
4738
4739 static int
4740 get_fdir_info(portid_t port_id, struct rte_eth_fdir_info *fdir_info,
4741                     struct rte_eth_fdir_stats *fdir_stat)
4742 {
4743         int ret = -ENOTSUP;
4744
4745 #ifdef RTE_NET_I40E
4746         if (ret == -ENOTSUP) {
4747                 ret = rte_pmd_i40e_get_fdir_info(port_id, fdir_info);
4748                 if (!ret)
4749                         ret = rte_pmd_i40e_get_fdir_stats(port_id, fdir_stat);
4750         }
4751 #endif
4752 #ifdef RTE_NET_IXGBE
4753         if (ret == -ENOTSUP) {
4754                 ret = rte_pmd_ixgbe_get_fdir_info(port_id, fdir_info);
4755                 if (!ret)
4756                         ret = rte_pmd_ixgbe_get_fdir_stats(port_id, fdir_stat);
4757         }
4758 #endif
4759         switch (ret) {
4760         case 0:
4761                 break;
4762         case -ENOTSUP:
4763                 fprintf(stderr, "\n FDIR is not supported on port %-2d\n",
4764                         port_id);
4765                 break;
4766         default:
4767                 fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
4768                 break;
4769         }
4770         return ret;
4771 }
4772
4773 void
4774 fdir_get_infos(portid_t port_id)
4775 {
4776         struct rte_eth_fdir_stats fdir_stat;
4777         struct rte_eth_fdir_info fdir_info;
4778
4779         static const char *fdir_stats_border = "########################";
4780
4781         if (port_id_is_invalid(port_id, ENABLED_WARN))
4782                 return;
4783
4784         memset(&fdir_info, 0, sizeof(fdir_info));
4785         memset(&fdir_stat, 0, sizeof(fdir_stat));
4786         if (get_fdir_info(port_id, &fdir_info, &fdir_stat))
4787                 return;
4788
4789         printf("\n  %s FDIR infos for port %-2d     %s\n",
4790                fdir_stats_border, port_id, fdir_stats_border);
4791         printf("  MODE: ");
4792         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
4793                 printf("  PERFECT\n");
4794         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
4795                 printf("  PERFECT-MAC-VLAN\n");
4796         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
4797                 printf("  PERFECT-TUNNEL\n");
4798         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
4799                 printf("  SIGNATURE\n");
4800         else
4801                 printf("  DISABLE\n");
4802         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
4803                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
4804                 printf("  SUPPORTED FLOW TYPE: ");
4805                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
4806         }
4807         printf("  FLEX PAYLOAD INFO:\n");
4808         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
4809                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
4810                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
4811                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
4812                 fdir_info.flex_payload_unit,
4813                 fdir_info.max_flex_payload_segment_num,
4814                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
4815         printf("  MASK: ");
4816         print_fdir_mask(&fdir_info.mask);
4817         if (fdir_info.flex_conf.nb_payloads > 0) {
4818                 printf("  FLEX PAYLOAD SRC OFFSET:");
4819                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
4820         }
4821         if (fdir_info.flex_conf.nb_flexmasks > 0) {
4822                 printf("  FLEX MASK CFG:");
4823                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
4824         }
4825         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
4826                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
4827         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
4828                fdir_info.guarant_spc, fdir_info.best_spc);
4829         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
4830                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
4831                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
4832                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
4833                fdir_stat.collision, fdir_stat.free,
4834                fdir_stat.maxhash, fdir_stat.maxlen,
4835                fdir_stat.add, fdir_stat.remove,
4836                fdir_stat.f_add, fdir_stat.f_remove);
4837         printf("  %s############################%s\n",
4838                fdir_stats_border, fdir_stats_border);
4839 }
4840
4841 #endif /* RTE_NET_I40E || RTE_NET_IXGBE */
4842
4843 void
4844 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
4845 {
4846         struct rte_port *port;
4847         struct rte_eth_fdir_flex_conf *flex_conf;
4848         int i, idx = 0;
4849
4850         port = &ports[port_id];
4851         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
4852         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
4853                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
4854                         idx = i;
4855                         break;
4856                 }
4857         }
4858         if (i >= RTE_ETH_FLOW_MAX) {
4859                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
4860                         idx = flex_conf->nb_flexmasks;
4861                         flex_conf->nb_flexmasks++;
4862                 } else {
4863                         fprintf(stderr,
4864                                 "The flex mask table is full. Can not set flex mask for flow_type(%u).",
4865                                 cfg->flow_type);
4866                         return;
4867                 }
4868         }
4869         rte_memcpy(&flex_conf->flex_mask[idx],
4870                          cfg,
4871                          sizeof(struct rte_eth_fdir_flex_mask));
4872 }
4873
4874 void
4875 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
4876 {
4877         struct rte_port *port;
4878         struct rte_eth_fdir_flex_conf *flex_conf;
4879         int i, idx = 0;
4880
4881         port = &ports[port_id];
4882         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
4883         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
4884                 if (cfg->type == flex_conf->flex_set[i].type) {
4885                         idx = i;
4886                         break;
4887                 }
4888         }
4889         if (i >= RTE_ETH_PAYLOAD_MAX) {
4890                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
4891                         idx = flex_conf->nb_payloads;
4892                         flex_conf->nb_payloads++;
4893                 } else {
4894                         fprintf(stderr,
4895                                 "The flex payload table is full. Can not set flex payload for type(%u).",
4896                                 cfg->type);
4897                         return;
4898                 }
4899         }
4900         rte_memcpy(&flex_conf->flex_set[idx],
4901                          cfg,
4902                          sizeof(struct rte_eth_flex_payload_cfg));
4903
4904 }
4905
4906 void
4907 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
4908 {
4909 #ifdef RTE_NET_IXGBE
4910         int diag;
4911
4912         if (is_rx)
4913                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
4914         else
4915                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
4916
4917         if (diag == 0)
4918                 return;
4919         fprintf(stderr,
4920                 "rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
4921                 is_rx ? "rx" : "tx", port_id, diag);
4922         return;
4923 #endif
4924         fprintf(stderr, "VF %s setting not supported for port %d\n",
4925                 is_rx ? "Rx" : "Tx", port_id);
4926         RTE_SET_USED(vf);
4927         RTE_SET_USED(on);
4928 }
4929
4930 int
4931 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
4932 {
4933         int diag;
4934         struct rte_eth_link link;
4935         int ret;
4936
4937         if (port_id_is_invalid(port_id, ENABLED_WARN))
4938                 return 1;
4939         ret = eth_link_get_nowait_print_err(port_id, &link);
4940         if (ret < 0)
4941                 return 1;
4942         if (link.link_speed != ETH_SPEED_NUM_UNKNOWN &&
4943             rate > link.link_speed) {
4944                 fprintf(stderr,
4945                         "Invalid rate value:%u bigger than link speed: %u\n",
4946                         rate, link.link_speed);
4947                 return 1;
4948         }
4949         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
4950         if (diag == 0)
4951                 return diag;
4952         fprintf(stderr,
4953                 "rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
4954                 port_id, diag);
4955         return diag;
4956 }
4957
4958 int
4959 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
4960 {
4961         int diag = -ENOTSUP;
4962
4963         RTE_SET_USED(vf);
4964         RTE_SET_USED(rate);
4965         RTE_SET_USED(q_msk);
4966
4967 #ifdef RTE_NET_IXGBE
4968         if (diag == -ENOTSUP)
4969                 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
4970                                                        q_msk);
4971 #endif
4972 #ifdef RTE_NET_BNXT
4973         if (diag == -ENOTSUP)
4974                 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
4975 #endif
4976         if (diag == 0)
4977                 return diag;
4978
4979         fprintf(stderr,
4980                 "%s for port_id=%d failed diag=%d\n",
4981                 __func__, port_id, diag);
4982         return diag;
4983 }
4984
4985 /*
4986  * Functions to manage the set of filtered Multicast MAC addresses.
4987  *
4988  * A pool of filtered multicast MAC addresses is associated with each port.
4989  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
4990  * The address of the pool and the number of valid multicast MAC addresses
4991  * recorded in the pool are stored in the fields "mc_addr_pool" and
4992  * "mc_addr_nb" of the "rte_port" data structure.
4993  *
4994  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
4995  * to be supplied a contiguous array of multicast MAC addresses.
4996  * To comply with this constraint, the set of multicast addresses recorded
4997  * into the pool are systematically compacted at the beginning of the pool.
4998  * Hence, when a multicast address is removed from the pool, all following
4999  * addresses, if any, are copied back to keep the set contiguous.
5000  */
5001 #define MCAST_POOL_INC 32
5002
5003 static int
5004 mcast_addr_pool_extend(struct rte_port *port)
5005 {
5006         struct rte_ether_addr *mc_pool;
5007         size_t mc_pool_size;
5008
5009         /*
5010          * If a free entry is available at the end of the pool, just
5011          * increment the number of recorded multicast addresses.
5012          */
5013         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
5014                 port->mc_addr_nb++;
5015                 return 0;
5016         }
5017
5018         /*
5019          * [re]allocate a pool with MCAST_POOL_INC more entries.
5020          * The previous test guarantees that port->mc_addr_nb is a multiple
5021          * of MCAST_POOL_INC.
5022          */
5023         mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb +
5024                                                     MCAST_POOL_INC);
5025         mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool,
5026                                                 mc_pool_size);
5027         if (mc_pool == NULL) {
5028                 fprintf(stderr,
5029                         "allocation of pool of %u multicast addresses failed\n",
5030                         port->mc_addr_nb + MCAST_POOL_INC);
5031                 return -ENOMEM;
5032         }
5033
5034         port->mc_addr_pool = mc_pool;
5035         port->mc_addr_nb++;
5036         return 0;
5037
5038 }
5039
5040 static void
5041 mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr)
5042 {
5043         if (mcast_addr_pool_extend(port) != 0)
5044                 return;
5045         rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]);
5046 }
5047
5048 static void
5049 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
5050 {
5051         port->mc_addr_nb--;
5052         if (addr_idx == port->mc_addr_nb) {
5053                 /* No need to recompact the set of multicast addressses. */
5054                 if (port->mc_addr_nb == 0) {
5055                         /* free the pool of multicast addresses. */
5056                         free(port->mc_addr_pool);
5057                         port->mc_addr_pool = NULL;
5058                 }
5059                 return;
5060         }
5061         memmove(&port->mc_addr_pool[addr_idx],
5062                 &port->mc_addr_pool[addr_idx + 1],
5063                 sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx));
5064 }
5065
5066 static int
5067 eth_port_multicast_addr_list_set(portid_t port_id)
5068 {
5069         struct rte_port *port;
5070         int diag;
5071
5072         port = &ports[port_id];
5073         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
5074                                             port->mc_addr_nb);
5075         if (diag < 0)
5076                 fprintf(stderr,
5077                         "rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
5078                         port_id, port->mc_addr_nb, diag);
5079
5080         return diag;
5081 }
5082
5083 void
5084 mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr)
5085 {
5086         struct rte_port *port;
5087         uint32_t i;
5088
5089         if (port_id_is_invalid(port_id, ENABLED_WARN))
5090                 return;
5091
5092         port = &ports[port_id];
5093
5094         /*
5095          * Check that the added multicast MAC address is not already recorded
5096          * in the pool of multicast addresses.
5097          */
5098         for (i = 0; i < port->mc_addr_nb; i++) {
5099                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
5100                         fprintf(stderr,
5101                                 "multicast address already filtered by port\n");
5102                         return;
5103                 }
5104         }
5105
5106         mcast_addr_pool_append(port, mc_addr);
5107         if (eth_port_multicast_addr_list_set(port_id) < 0)
5108                 /* Rollback on failure, remove the address from the pool */
5109                 mcast_addr_pool_remove(port, i);
5110 }
5111
5112 void
5113 mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr)
5114 {
5115         struct rte_port *port;
5116         uint32_t i;
5117
5118         if (port_id_is_invalid(port_id, ENABLED_WARN))
5119                 return;
5120
5121         port = &ports[port_id];
5122
5123         /*
5124          * Search the pool of multicast MAC addresses for the removed address.
5125          */
5126         for (i = 0; i < port->mc_addr_nb; i++) {
5127                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
5128                         break;
5129         }
5130         if (i == port->mc_addr_nb) {
5131                 fprintf(stderr, "multicast address not filtered by port %d\n",
5132                         port_id);
5133                 return;
5134         }
5135
5136         mcast_addr_pool_remove(port, i);
5137         if (eth_port_multicast_addr_list_set(port_id) < 0)
5138                 /* Rollback on failure, add the address back into the pool */
5139                 mcast_addr_pool_append(port, mc_addr);
5140 }
5141
5142 void
5143 port_dcb_info_display(portid_t port_id)
5144 {
5145         struct rte_eth_dcb_info dcb_info;
5146         uint16_t i;
5147         int ret;
5148         static const char *border = "================";
5149
5150         if (port_id_is_invalid(port_id, ENABLED_WARN))
5151                 return;
5152
5153         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
5154         if (ret) {
5155                 fprintf(stderr, "\n Failed to get dcb infos on port %-2d\n",
5156                         port_id);
5157                 return;
5158         }
5159         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
5160         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
5161         printf("\n  TC :        ");
5162         for (i = 0; i < dcb_info.nb_tcs; i++)
5163                 printf("\t%4d", i);
5164         printf("\n  Priority :  ");
5165         for (i = 0; i < dcb_info.nb_tcs; i++)
5166                 printf("\t%4d", dcb_info.prio_tc[i]);
5167         printf("\n  BW percent :");
5168         for (i = 0; i < dcb_info.nb_tcs; i++)
5169                 printf("\t%4d%%", dcb_info.tc_bws[i]);
5170         printf("\n  RXQ base :  ");
5171         for (i = 0; i < dcb_info.nb_tcs; i++)
5172                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
5173         printf("\n  RXQ number :");
5174         for (i = 0; i < dcb_info.nb_tcs; i++)
5175                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
5176         printf("\n  TXQ base :  ");
5177         for (i = 0; i < dcb_info.nb_tcs; i++)
5178                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
5179         printf("\n  TXQ number :");
5180         for (i = 0; i < dcb_info.nb_tcs; i++)
5181                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
5182         printf("\n");
5183 }
5184
5185 uint8_t *
5186 open_file(const char *file_path, uint32_t *size)
5187 {
5188         int fd = open(file_path, O_RDONLY);
5189         off_t pkg_size;
5190         uint8_t *buf = NULL;
5191         int ret = 0;
5192         struct stat st_buf;
5193
5194         if (size)
5195                 *size = 0;
5196
5197         if (fd == -1) {
5198                 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
5199                 return buf;
5200         }
5201
5202         if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) {
5203                 close(fd);
5204                 fprintf(stderr, "%s: File operations failed\n", __func__);
5205                 return buf;
5206         }
5207
5208         pkg_size = st_buf.st_size;
5209         if (pkg_size < 0) {
5210                 close(fd);
5211                 fprintf(stderr, "%s: File operations failed\n", __func__);
5212                 return buf;
5213         }
5214
5215         buf = (uint8_t *)malloc(pkg_size);
5216         if (!buf) {
5217                 close(fd);
5218                 fprintf(stderr, "%s: Failed to malloc memory\n", __func__);
5219                 return buf;
5220         }
5221
5222         ret = read(fd, buf, pkg_size);
5223         if (ret < 0) {
5224                 close(fd);
5225                 fprintf(stderr, "%s: File read operation failed\n", __func__);
5226                 close_file(buf);
5227                 return NULL;
5228         }
5229
5230         if (size)
5231                 *size = pkg_size;
5232
5233         close(fd);
5234
5235         return buf;
5236 }
5237
5238 int
5239 save_file(const char *file_path, uint8_t *buf, uint32_t size)
5240 {
5241         FILE *fh = fopen(file_path, "wb");
5242
5243         if (fh == NULL) {
5244                 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
5245                 return -1;
5246         }
5247
5248         if (fwrite(buf, 1, size, fh) != size) {
5249                 fclose(fh);
5250                 fprintf(stderr, "%s: File write operation failed\n", __func__);
5251                 return -1;
5252         }
5253
5254         fclose(fh);
5255
5256         return 0;
5257 }
5258
5259 int
5260 close_file(uint8_t *buf)
5261 {
5262         if (buf) {
5263                 free((void *)buf);
5264                 return 0;
5265         }
5266
5267         return -1;
5268 }
5269
5270 void
5271 port_queue_region_info_display(portid_t port_id, void *buf)
5272 {
5273 #ifdef RTE_NET_I40E
5274         uint16_t i, j;
5275         struct rte_pmd_i40e_queue_regions *info =
5276                 (struct rte_pmd_i40e_queue_regions *)buf;
5277         static const char *queue_region_info_stats_border = "-------";
5278
5279         if (!info->queue_region_number)
5280                 printf("there is no region has been set before");
5281
5282         printf("\n      %s All queue region info for port=%2d %s",
5283                         queue_region_info_stats_border, port_id,
5284                         queue_region_info_stats_border);
5285         printf("\n      queue_region_number: %-14u \n",
5286                         info->queue_region_number);
5287
5288         for (i = 0; i < info->queue_region_number; i++) {
5289                 printf("\n      region_id: %-14u queue_number: %-14u "
5290                         "queue_start_index: %-14u \n",
5291                         info->region[i].region_id,
5292                         info->region[i].queue_num,
5293                         info->region[i].queue_start_index);
5294
5295                 printf("  user_priority_num is  %-14u :",
5296                                         info->region[i].user_priority_num);
5297                 for (j = 0; j < info->region[i].user_priority_num; j++)
5298                         printf(" %-14u ", info->region[i].user_priority[j]);
5299
5300                 printf("\n      flowtype_num is  %-14u :",
5301                                 info->region[i].flowtype_num);
5302                 for (j = 0; j < info->region[i].flowtype_num; j++)
5303                         printf(" %-14u ", info->region[i].hw_flowtype[j]);
5304         }
5305 #else
5306         RTE_SET_USED(port_id);
5307         RTE_SET_USED(buf);
5308 #endif
5309
5310         printf("\n\n");
5311 }
5312
5313 void
5314 show_macs(portid_t port_id)
5315 {
5316         char buf[RTE_ETHER_ADDR_FMT_SIZE];
5317         struct rte_eth_dev_info dev_info;
5318         int32_t i, rc, num_macs = 0;
5319
5320         if (eth_dev_info_get_print_err(port_id, &dev_info))
5321                 return;
5322
5323         struct rte_ether_addr addr[dev_info.max_mac_addrs];
5324         rc = rte_eth_macaddrs_get(port_id, addr, dev_info.max_mac_addrs);
5325         if (rc < 0)
5326                 return;
5327
5328         for (i = 0; i < rc; i++) {
5329
5330                 /* skip zero address */
5331                 if (rte_is_zero_ether_addr(&addr[i]))
5332                         continue;
5333
5334                 num_macs++;
5335         }
5336
5337         printf("Number of MAC address added: %d\n", num_macs);
5338
5339         for (i = 0; i < rc; i++) {
5340
5341                 /* skip zero address */
5342                 if (rte_is_zero_ether_addr(&addr[i]))
5343                         continue;
5344
5345                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, &addr[i]);
5346                 printf("  %s\n", buf);
5347         }
5348 }
5349
5350 void
5351 show_mcast_macs(portid_t port_id)
5352 {
5353         char buf[RTE_ETHER_ADDR_FMT_SIZE];
5354         struct rte_ether_addr *addr;
5355         struct rte_port *port;
5356         uint32_t i;
5357
5358         port = &ports[port_id];
5359
5360         printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb);
5361
5362         for (i = 0; i < port->mc_addr_nb; i++) {
5363                 addr = &port->mc_addr_pool[i];
5364
5365                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
5366                 printf("  %s\n", buf);
5367         }
5368 }