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