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