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