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