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