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