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