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