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