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