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