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