app/testpmd: enable empty polls in burst stats
[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 /** Validate flow rule. */
1399 int
1400 port_flow_validate(portid_t port_id,
1401                    const struct rte_flow_attr *attr,
1402                    const struct rte_flow_item *pattern,
1403                    const struct rte_flow_action *actions)
1404 {
1405         struct rte_flow_error error;
1406
1407         /* Poisoning to make sure PMDs update it in case of error. */
1408         memset(&error, 0x11, sizeof(error));
1409         if (rte_flow_validate(port_id, attr, pattern, actions, &error))
1410                 return port_flow_complain(&error);
1411         printf("Flow rule validated\n");
1412         return 0;
1413 }
1414
1415 /** Update age action context by port_flow pointer. */
1416 void
1417 update_age_action_context(const struct rte_flow_action *actions,
1418                         struct port_flow *pf)
1419 {
1420         struct rte_flow_action_age *age = NULL;
1421
1422         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1423                 switch (actions->type) {
1424                 case RTE_FLOW_ACTION_TYPE_AGE:
1425                         age = (struct rte_flow_action_age *)
1426                                 (uintptr_t)actions->conf;
1427                         age->context = pf;
1428                         return;
1429                 default:
1430                         break;
1431                 }
1432         }
1433 }
1434
1435 /** Create flow rule. */
1436 int
1437 port_flow_create(portid_t port_id,
1438                  const struct rte_flow_attr *attr,
1439                  const struct rte_flow_item *pattern,
1440                  const struct rte_flow_action *actions)
1441 {
1442         struct rte_flow *flow;
1443         struct rte_port *port;
1444         struct port_flow *pf;
1445         uint32_t id = 0;
1446         struct rte_flow_error error;
1447
1448         port = &ports[port_id];
1449         if (port->flow_list) {
1450                 if (port->flow_list->id == UINT32_MAX) {
1451                         printf("Highest rule ID is already assigned, delete"
1452                                " it first");
1453                         return -ENOMEM;
1454                 }
1455                 id = port->flow_list->id + 1;
1456         }
1457         pf = port_flow_new(attr, pattern, actions, &error);
1458         if (!pf)
1459                 return port_flow_complain(&error);
1460         update_age_action_context(actions, pf);
1461         /* Poisoning to make sure PMDs update it in case of error. */
1462         memset(&error, 0x22, sizeof(error));
1463         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
1464         if (!flow) {
1465                 free(pf);
1466                 return port_flow_complain(&error);
1467         }
1468         pf->next = port->flow_list;
1469         pf->id = id;
1470         pf->flow = flow;
1471         port->flow_list = pf;
1472         printf("Flow rule #%u created\n", pf->id);
1473         return 0;
1474 }
1475
1476 /** Destroy a number of flow rules. */
1477 int
1478 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
1479 {
1480         struct rte_port *port;
1481         struct port_flow **tmp;
1482         uint32_t c = 0;
1483         int ret = 0;
1484
1485         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1486             port_id == (portid_t)RTE_PORT_ALL)
1487                 return -EINVAL;
1488         port = &ports[port_id];
1489         tmp = &port->flow_list;
1490         while (*tmp) {
1491                 uint32_t i;
1492
1493                 for (i = 0; i != n; ++i) {
1494                         struct rte_flow_error error;
1495                         struct port_flow *pf = *tmp;
1496
1497                         if (rule[i] != pf->id)
1498                                 continue;
1499                         /*
1500                          * Poisoning to make sure PMDs update it in case
1501                          * of error.
1502                          */
1503                         memset(&error, 0x33, sizeof(error));
1504                         if (rte_flow_destroy(port_id, pf->flow, &error)) {
1505                                 ret = port_flow_complain(&error);
1506                                 continue;
1507                         }
1508                         printf("Flow rule #%u destroyed\n", pf->id);
1509                         *tmp = pf->next;
1510                         free(pf);
1511                         break;
1512                 }
1513                 if (i == n)
1514                         tmp = &(*tmp)->next;
1515                 ++c;
1516         }
1517         return ret;
1518 }
1519
1520 /** Remove all flow rules. */
1521 int
1522 port_flow_flush(portid_t port_id)
1523 {
1524         struct rte_flow_error error;
1525         struct rte_port *port;
1526         int ret = 0;
1527
1528         /* Poisoning to make sure PMDs update it in case of error. */
1529         memset(&error, 0x44, sizeof(error));
1530         if (rte_flow_flush(port_id, &error)) {
1531                 ret = port_flow_complain(&error);
1532                 if (port_id_is_invalid(port_id, DISABLED_WARN) ||
1533                     port_id == (portid_t)RTE_PORT_ALL)
1534                         return ret;
1535         }
1536         port = &ports[port_id];
1537         while (port->flow_list) {
1538                 struct port_flow *pf = port->flow_list->next;
1539
1540                 free(port->flow_list);
1541                 port->flow_list = pf;
1542         }
1543         return ret;
1544 }
1545
1546 /** Dump all flow rules. */
1547 int
1548 port_flow_dump(portid_t port_id, const char *file_name)
1549 {
1550         int ret = 0;
1551         FILE *file = stdout;
1552         struct rte_flow_error error;
1553
1554         if (file_name && strlen(file_name)) {
1555                 file = fopen(file_name, "w");
1556                 if (!file) {
1557                         printf("Failed to create file %s: %s\n", file_name,
1558                                strerror(errno));
1559                         return -errno;
1560                 }
1561         }
1562         ret = rte_flow_dev_dump(port_id, file, &error);
1563         if (ret) {
1564                 port_flow_complain(&error);
1565                 printf("Failed to dump flow: %s\n", strerror(-ret));
1566         } else
1567                 printf("Flow dump finished\n");
1568         if (file_name && strlen(file_name))
1569                 fclose(file);
1570         return ret;
1571 }
1572
1573 /** Query a flow rule. */
1574 int
1575 port_flow_query(portid_t port_id, uint32_t rule,
1576                 const struct rte_flow_action *action)
1577 {
1578         struct rte_flow_error error;
1579         struct rte_port *port;
1580         struct port_flow *pf;
1581         const char *name;
1582         union {
1583                 struct rte_flow_query_count count;
1584         } query;
1585         int ret;
1586
1587         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1588             port_id == (portid_t)RTE_PORT_ALL)
1589                 return -EINVAL;
1590         port = &ports[port_id];
1591         for (pf = port->flow_list; pf; pf = pf->next)
1592                 if (pf->id == rule)
1593                         break;
1594         if (!pf) {
1595                 printf("Flow rule #%u not found\n", rule);
1596                 return -ENOENT;
1597         }
1598         ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
1599                             &name, sizeof(name),
1600                             (void *)(uintptr_t)action->type, &error);
1601         if (ret < 0)
1602                 return port_flow_complain(&error);
1603         switch (action->type) {
1604         case RTE_FLOW_ACTION_TYPE_COUNT:
1605                 break;
1606         default:
1607                 printf("Cannot query action type %d (%s)\n",
1608                         action->type, name);
1609                 return -ENOTSUP;
1610         }
1611         /* Poisoning to make sure PMDs update it in case of error. */
1612         memset(&error, 0x55, sizeof(error));
1613         memset(&query, 0, sizeof(query));
1614         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
1615                 return port_flow_complain(&error);
1616         switch (action->type) {
1617         case RTE_FLOW_ACTION_TYPE_COUNT:
1618                 printf("%s:\n"
1619                        " hits_set: %u\n"
1620                        " bytes_set: %u\n"
1621                        " hits: %" PRIu64 "\n"
1622                        " bytes: %" PRIu64 "\n",
1623                        name,
1624                        query.count.hits_set,
1625                        query.count.bytes_set,
1626                        query.count.hits,
1627                        query.count.bytes);
1628                 break;
1629         default:
1630                 printf("Cannot display result for action type %d (%s)\n",
1631                        action->type, name);
1632                 break;
1633         }
1634         return 0;
1635 }
1636
1637 /** List simply and destroy all aged flows. */
1638 void
1639 port_flow_aged(portid_t port_id, uint8_t destroy)
1640 {
1641         void **contexts;
1642         int nb_context, total = 0, idx;
1643         struct rte_flow_error error;
1644         struct port_flow *pf;
1645
1646         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1647             port_id == (portid_t)RTE_PORT_ALL)
1648                 return;
1649         total = rte_flow_get_aged_flows(port_id, NULL, 0, &error);
1650         printf("Port %u total aged flows: %d\n", port_id, total);
1651         if (total < 0) {
1652                 port_flow_complain(&error);
1653                 return;
1654         }
1655         if (total == 0)
1656                 return;
1657         contexts = malloc(sizeof(void *) * total);
1658         if (contexts == NULL) {
1659                 printf("Cannot allocate contexts for aged flow\n");
1660                 return;
1661         }
1662         printf("ID\tGroup\tPrio\tAttr\n");
1663         nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error);
1664         if (nb_context != total) {
1665                 printf("Port:%d get aged flows count(%d) != total(%d)\n",
1666                         port_id, nb_context, total);
1667                 free(contexts);
1668                 return;
1669         }
1670         for (idx = 0; idx < nb_context; idx++) {
1671                 pf = (struct port_flow *)contexts[idx];
1672                 if (!pf) {
1673                         printf("Error: get Null context in port %u\n", port_id);
1674                         continue;
1675                 }
1676                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t\n",
1677                        pf->id,
1678                        pf->rule.attr->group,
1679                        pf->rule.attr->priority,
1680                        pf->rule.attr->ingress ? 'i' : '-',
1681                        pf->rule.attr->egress ? 'e' : '-',
1682                        pf->rule.attr->transfer ? 't' : '-');
1683         }
1684         if (destroy) {
1685                 int ret;
1686                 uint32_t flow_id;
1687
1688                 total = 0;
1689                 printf("\n");
1690                 for (idx = 0; idx < nb_context; idx++) {
1691                         pf = (struct port_flow *)contexts[idx];
1692                         if (!pf)
1693                                 continue;
1694                         flow_id = pf->id;
1695                         ret = port_flow_destroy(port_id, 1, &flow_id);
1696                         if (!ret)
1697                                 total++;
1698                 }
1699                 printf("%d flows be destroyed\n", total);
1700         }
1701         free(contexts);
1702 }
1703
1704 /** List flow rules. */
1705 void
1706 port_flow_list(portid_t port_id, uint32_t n, const uint32_t group[n])
1707 {
1708         struct rte_port *port;
1709         struct port_flow *pf;
1710         struct port_flow *list = NULL;
1711         uint32_t i;
1712
1713         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1714             port_id == (portid_t)RTE_PORT_ALL)
1715                 return;
1716         port = &ports[port_id];
1717         if (!port->flow_list)
1718                 return;
1719         /* Sort flows by group, priority and ID. */
1720         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
1721                 struct port_flow **tmp;
1722                 const struct rte_flow_attr *curr = pf->rule.attr;
1723
1724                 if (n) {
1725                         /* Filter out unwanted groups. */
1726                         for (i = 0; i != n; ++i)
1727                                 if (curr->group == group[i])
1728                                         break;
1729                         if (i == n)
1730                                 continue;
1731                 }
1732                 for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) {
1733                         const struct rte_flow_attr *comp = (*tmp)->rule.attr;
1734
1735                         if (curr->group > comp->group ||
1736                             (curr->group == comp->group &&
1737                              curr->priority > comp->priority) ||
1738                             (curr->group == comp->group &&
1739                              curr->priority == comp->priority &&
1740                              pf->id > (*tmp)->id))
1741                                 continue;
1742                         break;
1743                 }
1744                 pf->tmp = *tmp;
1745                 *tmp = pf;
1746         }
1747         printf("ID\tGroup\tPrio\tAttr\tRule\n");
1748         for (pf = list; pf != NULL; pf = pf->tmp) {
1749                 const struct rte_flow_item *item = pf->rule.pattern;
1750                 const struct rte_flow_action *action = pf->rule.actions;
1751                 const char *name;
1752
1753                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t",
1754                        pf->id,
1755                        pf->rule.attr->group,
1756                        pf->rule.attr->priority,
1757                        pf->rule.attr->ingress ? 'i' : '-',
1758                        pf->rule.attr->egress ? 'e' : '-',
1759                        pf->rule.attr->transfer ? 't' : '-');
1760                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
1761                         if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
1762                                           &name, sizeof(name),
1763                                           (void *)(uintptr_t)item->type,
1764                                           NULL) <= 0)
1765                                 name = "[UNKNOWN]";
1766                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1767                                 printf("%s ", name);
1768                         ++item;
1769                 }
1770                 printf("=>");
1771                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
1772                         if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
1773                                           &name, sizeof(name),
1774                                           (void *)(uintptr_t)action->type,
1775                                           NULL) <= 0)
1776                                 name = "[UNKNOWN]";
1777                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
1778                                 printf(" %s", name);
1779                         ++action;
1780                 }
1781                 printf("\n");
1782         }
1783 }
1784
1785 /** Restrict ingress traffic to the defined flow rules. */
1786 int
1787 port_flow_isolate(portid_t port_id, int set)
1788 {
1789         struct rte_flow_error error;
1790
1791         /* Poisoning to make sure PMDs update it in case of error. */
1792         memset(&error, 0x66, sizeof(error));
1793         if (rte_flow_isolate(port_id, set, &error))
1794                 return port_flow_complain(&error);
1795         printf("Ingress traffic on port %u is %s to the defined flow rules\n",
1796                port_id,
1797                set ? "now restricted" : "not restricted anymore");
1798         return 0;
1799 }
1800
1801 /*
1802  * RX/TX ring descriptors display functions.
1803  */
1804 int
1805 rx_queue_id_is_invalid(queueid_t rxq_id)
1806 {
1807         if (rxq_id < nb_rxq)
1808                 return 0;
1809         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
1810         return 1;
1811 }
1812
1813 int
1814 tx_queue_id_is_invalid(queueid_t txq_id)
1815 {
1816         if (txq_id < nb_txq)
1817                 return 0;
1818         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
1819         return 1;
1820 }
1821
1822 static int
1823 rx_desc_id_is_invalid(uint16_t rxdesc_id)
1824 {
1825         if (rxdesc_id < nb_rxd)
1826                 return 0;
1827         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
1828                rxdesc_id, nb_rxd);
1829         return 1;
1830 }
1831
1832 static int
1833 tx_desc_id_is_invalid(uint16_t txdesc_id)
1834 {
1835         if (txdesc_id < nb_txd)
1836                 return 0;
1837         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
1838                txdesc_id, nb_txd);
1839         return 1;
1840 }
1841
1842 static const struct rte_memzone *
1843 ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id)
1844 {
1845         char mz_name[RTE_MEMZONE_NAMESIZE];
1846         const struct rte_memzone *mz;
1847
1848         snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s",
1849                         port_id, q_id, ring_name);
1850         mz = rte_memzone_lookup(mz_name);
1851         if (mz == NULL)
1852                 printf("%s ring memory zoneof (port %d, queue %d) not"
1853                        "found (zone name = %s\n",
1854                        ring_name, port_id, q_id, mz_name);
1855         return mz;
1856 }
1857
1858 union igb_ring_dword {
1859         uint64_t dword;
1860         struct {
1861 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1862                 uint32_t lo;
1863                 uint32_t hi;
1864 #else
1865                 uint32_t hi;
1866                 uint32_t lo;
1867 #endif
1868         } words;
1869 };
1870
1871 struct igb_ring_desc_32_bytes {
1872         union igb_ring_dword lo_dword;
1873         union igb_ring_dword hi_dword;
1874         union igb_ring_dword resv1;
1875         union igb_ring_dword resv2;
1876 };
1877
1878 struct igb_ring_desc_16_bytes {
1879         union igb_ring_dword lo_dword;
1880         union igb_ring_dword hi_dword;
1881 };
1882
1883 static void
1884 ring_rxd_display_dword(union igb_ring_dword dword)
1885 {
1886         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
1887                                         (unsigned)dword.words.hi);
1888 }
1889
1890 static void
1891 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
1892 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1893                            portid_t port_id,
1894 #else
1895                            __rte_unused portid_t port_id,
1896 #endif
1897                            uint16_t desc_id)
1898 {
1899         struct igb_ring_desc_16_bytes *ring =
1900                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1901 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1902         int ret;
1903         struct rte_eth_dev_info dev_info;
1904
1905         ret = eth_dev_info_get_print_err(port_id, &dev_info);
1906         if (ret != 0)
1907                 return;
1908
1909         if (strstr(dev_info.driver_name, "i40e") != NULL) {
1910                 /* 32 bytes RX descriptor, i40e only */
1911                 struct igb_ring_desc_32_bytes *ring =
1912                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
1913                 ring[desc_id].lo_dword.dword =
1914                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1915                 ring_rxd_display_dword(ring[desc_id].lo_dword);
1916                 ring[desc_id].hi_dword.dword =
1917                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1918                 ring_rxd_display_dword(ring[desc_id].hi_dword);
1919                 ring[desc_id].resv1.dword =
1920                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
1921                 ring_rxd_display_dword(ring[desc_id].resv1);
1922                 ring[desc_id].resv2.dword =
1923                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
1924                 ring_rxd_display_dword(ring[desc_id].resv2);
1925
1926                 return;
1927         }
1928 #endif
1929         /* 16 bytes RX descriptor */
1930         ring[desc_id].lo_dword.dword =
1931                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1932         ring_rxd_display_dword(ring[desc_id].lo_dword);
1933         ring[desc_id].hi_dword.dword =
1934                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1935         ring_rxd_display_dword(ring[desc_id].hi_dword);
1936 }
1937
1938 static void
1939 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
1940 {
1941         struct igb_ring_desc_16_bytes *ring;
1942         struct igb_ring_desc_16_bytes txd;
1943
1944         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1945         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1946         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1947         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
1948                         (unsigned)txd.lo_dword.words.lo,
1949                         (unsigned)txd.lo_dword.words.hi,
1950                         (unsigned)txd.hi_dword.words.lo,
1951                         (unsigned)txd.hi_dword.words.hi);
1952 }
1953
1954 void
1955 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
1956 {
1957         const struct rte_memzone *rx_mz;
1958
1959         if (port_id_is_invalid(port_id, ENABLED_WARN))
1960                 return;
1961         if (rx_queue_id_is_invalid(rxq_id))
1962                 return;
1963         if (rx_desc_id_is_invalid(rxd_id))
1964                 return;
1965         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
1966         if (rx_mz == NULL)
1967                 return;
1968         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
1969 }
1970
1971 void
1972 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
1973 {
1974         const struct rte_memzone *tx_mz;
1975
1976         if (port_id_is_invalid(port_id, ENABLED_WARN))
1977                 return;
1978         if (tx_queue_id_is_invalid(txq_id))
1979                 return;
1980         if (tx_desc_id_is_invalid(txd_id))
1981                 return;
1982         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
1983         if (tx_mz == NULL)
1984                 return;
1985         ring_tx_descriptor_display(tx_mz, txd_id);
1986 }
1987
1988 void
1989 fwd_lcores_config_display(void)
1990 {
1991         lcoreid_t lc_id;
1992
1993         printf("List of forwarding lcores:");
1994         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
1995                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
1996         printf("\n");
1997 }
1998 void
1999 rxtx_config_display(void)
2000 {
2001         portid_t pid;
2002         queueid_t qid;
2003
2004         printf("  %s packet forwarding%s packets/burst=%d\n",
2005                cur_fwd_eng->fwd_mode_name,
2006                retry_enabled == 0 ? "" : " with retry",
2007                nb_pkt_per_burst);
2008
2009         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
2010                 printf("  packet len=%u - nb packet segments=%d\n",
2011                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
2012
2013         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
2014                nb_fwd_lcores, nb_fwd_ports);
2015
2016         RTE_ETH_FOREACH_DEV(pid) {
2017                 struct rte_eth_rxconf *rx_conf = &ports[pid].rx_conf[0];
2018                 struct rte_eth_txconf *tx_conf = &ports[pid].tx_conf[0];
2019                 uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0];
2020                 uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0];
2021                 uint16_t nb_rx_desc_tmp;
2022                 uint16_t nb_tx_desc_tmp;
2023                 struct rte_eth_rxq_info rx_qinfo;
2024                 struct rte_eth_txq_info tx_qinfo;
2025                 int32_t rc;
2026
2027                 /* per port config */
2028                 printf("  port %d: RX queue number: %d Tx queue number: %d\n",
2029                                 (unsigned int)pid, nb_rxq, nb_txq);
2030
2031                 printf("    Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n",
2032                                 ports[pid].dev_conf.rxmode.offloads,
2033                                 ports[pid].dev_conf.txmode.offloads);
2034
2035                 /* per rx queue config only for first queue to be less verbose */
2036                 for (qid = 0; qid < 1; qid++) {
2037                         rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo);
2038                         if (rc)
2039                                 nb_rx_desc_tmp = nb_rx_desc[qid];
2040                         else
2041                                 nb_rx_desc_tmp = rx_qinfo.nb_desc;
2042
2043                         printf("    RX queue: %d\n", qid);
2044                         printf("      RX desc=%d - RX free threshold=%d\n",
2045                                 nb_rx_desc_tmp, rx_conf[qid].rx_free_thresh);
2046                         printf("      RX threshold registers: pthresh=%d hthresh=%d "
2047                                 " wthresh=%d\n",
2048                                 rx_conf[qid].rx_thresh.pthresh,
2049                                 rx_conf[qid].rx_thresh.hthresh,
2050                                 rx_conf[qid].rx_thresh.wthresh);
2051                         printf("      RX Offloads=0x%"PRIx64"\n",
2052                                 rx_conf[qid].offloads);
2053                 }
2054
2055                 /* per tx queue config only for first queue to be less verbose */
2056                 for (qid = 0; qid < 1; qid++) {
2057                         rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo);
2058                         if (rc)
2059                                 nb_tx_desc_tmp = nb_tx_desc[qid];
2060                         else
2061                                 nb_tx_desc_tmp = tx_qinfo.nb_desc;
2062
2063                         printf("    TX queue: %d\n", qid);
2064                         printf("      TX desc=%d - TX free threshold=%d\n",
2065                                 nb_tx_desc_tmp, tx_conf[qid].tx_free_thresh);
2066                         printf("      TX threshold registers: pthresh=%d hthresh=%d "
2067                                 " wthresh=%d\n",
2068                                 tx_conf[qid].tx_thresh.pthresh,
2069                                 tx_conf[qid].tx_thresh.hthresh,
2070                                 tx_conf[qid].tx_thresh.wthresh);
2071                         printf("      TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n",
2072                                 tx_conf[qid].offloads, tx_conf->tx_rs_thresh);
2073                 }
2074         }
2075 }
2076
2077 void
2078 port_rss_reta_info(portid_t port_id,
2079                    struct rte_eth_rss_reta_entry64 *reta_conf,
2080                    uint16_t nb_entries)
2081 {
2082         uint16_t i, idx, shift;
2083         int ret;
2084
2085         if (port_id_is_invalid(port_id, ENABLED_WARN))
2086                 return;
2087
2088         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
2089         if (ret != 0) {
2090                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
2091                 return;
2092         }
2093
2094         for (i = 0; i < nb_entries; i++) {
2095                 idx = i / RTE_RETA_GROUP_SIZE;
2096                 shift = i % RTE_RETA_GROUP_SIZE;
2097                 if (!(reta_conf[idx].mask & (1ULL << shift)))
2098                         continue;
2099                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
2100                                         i, reta_conf[idx].reta[shift]);
2101         }
2102 }
2103
2104 /*
2105  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
2106  * key of the port.
2107  */
2108 void
2109 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
2110 {
2111         struct rte_eth_rss_conf rss_conf = {0};
2112         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
2113         uint64_t rss_hf;
2114         uint8_t i;
2115         int diag;
2116         struct rte_eth_dev_info dev_info;
2117         uint8_t hash_key_size;
2118         int ret;
2119
2120         if (port_id_is_invalid(port_id, ENABLED_WARN))
2121                 return;
2122
2123         ret = eth_dev_info_get_print_err(port_id, &dev_info);
2124         if (ret != 0)
2125                 return;
2126
2127         if (dev_info.hash_key_size > 0 &&
2128                         dev_info.hash_key_size <= sizeof(rss_key))
2129                 hash_key_size = dev_info.hash_key_size;
2130         else {
2131                 printf("dev_info did not provide a valid hash key size\n");
2132                 return;
2133         }
2134
2135         /* Get RSS hash key if asked to display it */
2136         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
2137         rss_conf.rss_key_len = hash_key_size;
2138         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2139         if (diag != 0) {
2140                 switch (diag) {
2141                 case -ENODEV:
2142                         printf("port index %d invalid\n", port_id);
2143                         break;
2144                 case -ENOTSUP:
2145                         printf("operation not supported by device\n");
2146                         break;
2147                 default:
2148                         printf("operation failed - diag=%d\n", diag);
2149                         break;
2150                 }
2151                 return;
2152         }
2153         rss_hf = rss_conf.rss_hf;
2154         if (rss_hf == 0) {
2155                 printf("RSS disabled\n");
2156                 return;
2157         }
2158         printf("RSS functions:\n ");
2159         for (i = 0; rss_type_table[i].str; i++) {
2160                 if (rss_hf & rss_type_table[i].rss_type)
2161                         printf("%s ", rss_type_table[i].str);
2162         }
2163         printf("\n");
2164         if (!show_rss_key)
2165                 return;
2166         printf("RSS key:\n");
2167         for (i = 0; i < hash_key_size; i++)
2168                 printf("%02X", rss_key[i]);
2169         printf("\n");
2170 }
2171
2172 void
2173 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
2174                          uint hash_key_len)
2175 {
2176         struct rte_eth_rss_conf rss_conf;
2177         int diag;
2178         unsigned int i;
2179
2180         rss_conf.rss_key = NULL;
2181         rss_conf.rss_key_len = hash_key_len;
2182         rss_conf.rss_hf = 0;
2183         for (i = 0; rss_type_table[i].str; i++) {
2184                 if (!strcmp(rss_type_table[i].str, rss_type))
2185                         rss_conf.rss_hf = rss_type_table[i].rss_type;
2186         }
2187         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
2188         if (diag == 0) {
2189                 rss_conf.rss_key = hash_key;
2190                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
2191         }
2192         if (diag == 0)
2193                 return;
2194
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 }
2207
2208 /*
2209  * Setup forwarding configuration for each logical core.
2210  */
2211 static void
2212 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
2213 {
2214         streamid_t nb_fs_per_lcore;
2215         streamid_t nb_fs;
2216         streamid_t sm_id;
2217         lcoreid_t  nb_extra;
2218         lcoreid_t  nb_fc;
2219         lcoreid_t  nb_lc;
2220         lcoreid_t  lc_id;
2221
2222         nb_fs = cfg->nb_fwd_streams;
2223         nb_fc = cfg->nb_fwd_lcores;
2224         if (nb_fs <= nb_fc) {
2225                 nb_fs_per_lcore = 1;
2226                 nb_extra = 0;
2227         } else {
2228                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
2229                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
2230         }
2231
2232         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
2233         sm_id = 0;
2234         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
2235                 fwd_lcores[lc_id]->stream_idx = sm_id;
2236                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
2237                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
2238         }
2239
2240         /*
2241          * Assign extra remaining streams, if any.
2242          */
2243         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
2244         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
2245                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
2246                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
2247                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
2248         }
2249 }
2250
2251 static portid_t
2252 fwd_topology_tx_port_get(portid_t rxp)
2253 {
2254         static int warning_once = 1;
2255
2256         RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports);
2257
2258         switch (port_topology) {
2259         default:
2260         case PORT_TOPOLOGY_PAIRED:
2261                 if ((rxp & 0x1) == 0) {
2262                         if (rxp + 1 < cur_fwd_config.nb_fwd_ports)
2263                                 return rxp + 1;
2264                         if (warning_once) {
2265                                 printf("\nWarning! port-topology=paired"
2266                                        " and odd forward ports number,"
2267                                        " the last port will pair with"
2268                                        " itself.\n\n");
2269                                 warning_once = 0;
2270                         }
2271                         return rxp;
2272                 }
2273                 return rxp - 1;
2274         case PORT_TOPOLOGY_CHAINED:
2275                 return (rxp + 1) % cur_fwd_config.nb_fwd_ports;
2276         case PORT_TOPOLOGY_LOOP:
2277                 return rxp;
2278         }
2279 }
2280
2281 static void
2282 simple_fwd_config_setup(void)
2283 {
2284         portid_t i;
2285
2286         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
2287         cur_fwd_config.nb_fwd_streams =
2288                 (streamid_t) cur_fwd_config.nb_fwd_ports;
2289
2290         /* reinitialize forwarding streams */
2291         init_fwd_streams();
2292
2293         /*
2294          * In the simple forwarding test, the number of forwarding cores
2295          * must be lower or equal to the number of forwarding ports.
2296          */
2297         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2298         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
2299                 cur_fwd_config.nb_fwd_lcores =
2300                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
2301         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2302
2303         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2304                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
2305                 fwd_streams[i]->rx_queue  = 0;
2306                 fwd_streams[i]->tx_port   =
2307                                 fwd_ports_ids[fwd_topology_tx_port_get(i)];
2308                 fwd_streams[i]->tx_queue  = 0;
2309                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
2310                 fwd_streams[i]->retry_enabled = retry_enabled;
2311         }
2312 }
2313
2314 /**
2315  * For the RSS forwarding test all streams distributed over lcores. Each stream
2316  * being composed of a RX queue to poll on a RX port for input messages,
2317  * associated with a TX queue of a TX port where to send forwarded packets.
2318  */
2319 static void
2320 rss_fwd_config_setup(void)
2321 {
2322         portid_t   rxp;
2323         portid_t   txp;
2324         queueid_t  rxq;
2325         queueid_t  nb_q;
2326         streamid_t  sm_id;
2327
2328         nb_q = nb_rxq;
2329         if (nb_q > nb_txq)
2330                 nb_q = nb_txq;
2331         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2332         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2333         cur_fwd_config.nb_fwd_streams =
2334                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
2335
2336         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2337                 cur_fwd_config.nb_fwd_lcores =
2338                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2339
2340         /* reinitialize forwarding streams */
2341         init_fwd_streams();
2342
2343         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2344         rxp = 0; rxq = 0;
2345         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2346                 struct fwd_stream *fs;
2347
2348                 fs = fwd_streams[sm_id];
2349                 txp = fwd_topology_tx_port_get(rxp);
2350                 fs->rx_port = fwd_ports_ids[rxp];
2351                 fs->rx_queue = rxq;
2352                 fs->tx_port = fwd_ports_ids[txp];
2353                 fs->tx_queue = rxq;
2354                 fs->peer_addr = fs->tx_port;
2355                 fs->retry_enabled = retry_enabled;
2356                 rxp++;
2357                 if (rxp < nb_fwd_ports)
2358                         continue;
2359                 rxp = 0;
2360                 rxq++;
2361         }
2362 }
2363
2364 /**
2365  * For the DCB forwarding test, each core is assigned on each traffic class.
2366  *
2367  * Each core is assigned a multi-stream, each stream being composed of
2368  * a RX queue to poll on a RX port for input messages, associated with
2369  * a TX queue of a TX port where to send forwarded packets. All RX and
2370  * TX queues are mapping to the same traffic class.
2371  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
2372  * the same core
2373  */
2374 static void
2375 dcb_fwd_config_setup(void)
2376 {
2377         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
2378         portid_t txp, rxp = 0;
2379         queueid_t txq, rxq = 0;
2380         lcoreid_t  lc_id;
2381         uint16_t nb_rx_queue, nb_tx_queue;
2382         uint16_t i, j, k, sm_id = 0;
2383         uint8_t tc = 0;
2384
2385         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2386         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2387         cur_fwd_config.nb_fwd_streams =
2388                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2389
2390         /* reinitialize forwarding streams */
2391         init_fwd_streams();
2392         sm_id = 0;
2393         txp = 1;
2394         /* get the dcb info on the first RX and TX ports */
2395         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2396         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2397
2398         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2399                 fwd_lcores[lc_id]->stream_nb = 0;
2400                 fwd_lcores[lc_id]->stream_idx = sm_id;
2401                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
2402                         /* if the nb_queue is zero, means this tc is
2403                          * not enabled on the POOL
2404                          */
2405                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
2406                                 break;
2407                         k = fwd_lcores[lc_id]->stream_nb +
2408                                 fwd_lcores[lc_id]->stream_idx;
2409                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
2410                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
2411                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2412                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
2413                         for (j = 0; j < nb_rx_queue; j++) {
2414                                 struct fwd_stream *fs;
2415
2416                                 fs = fwd_streams[k + j];
2417                                 fs->rx_port = fwd_ports_ids[rxp];
2418                                 fs->rx_queue = rxq + j;
2419                                 fs->tx_port = fwd_ports_ids[txp];
2420                                 fs->tx_queue = txq + j % nb_tx_queue;
2421                                 fs->peer_addr = fs->tx_port;
2422                                 fs->retry_enabled = retry_enabled;
2423                         }
2424                         fwd_lcores[lc_id]->stream_nb +=
2425                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2426                 }
2427                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
2428
2429                 tc++;
2430                 if (tc < rxp_dcb_info.nb_tcs)
2431                         continue;
2432                 /* Restart from TC 0 on next RX port */
2433                 tc = 0;
2434                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
2435                         rxp = (portid_t)
2436                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
2437                 else
2438                         rxp++;
2439                 if (rxp >= nb_fwd_ports)
2440                         return;
2441                 /* get the dcb information on next RX and TX ports */
2442                 if ((rxp & 0x1) == 0)
2443                         txp = (portid_t) (rxp + 1);
2444                 else
2445                         txp = (portid_t) (rxp - 1);
2446                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2447                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2448         }
2449 }
2450
2451 static void
2452 icmp_echo_config_setup(void)
2453 {
2454         portid_t  rxp;
2455         queueid_t rxq;
2456         lcoreid_t lc_id;
2457         uint16_t  sm_id;
2458
2459         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
2460                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
2461                         (nb_txq * nb_fwd_ports);
2462         else
2463                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2464         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2465         cur_fwd_config.nb_fwd_streams =
2466                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2467         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2468                 cur_fwd_config.nb_fwd_lcores =
2469                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2470         if (verbose_level > 0) {
2471                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
2472                        __FUNCTION__,
2473                        cur_fwd_config.nb_fwd_lcores,
2474                        cur_fwd_config.nb_fwd_ports,
2475                        cur_fwd_config.nb_fwd_streams);
2476         }
2477
2478         /* reinitialize forwarding streams */
2479         init_fwd_streams();
2480         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2481         rxp = 0; rxq = 0;
2482         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2483                 if (verbose_level > 0)
2484                         printf("  core=%d: \n", lc_id);
2485                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2486                         struct fwd_stream *fs;
2487                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2488                         fs->rx_port = fwd_ports_ids[rxp];
2489                         fs->rx_queue = rxq;
2490                         fs->tx_port = fs->rx_port;
2491                         fs->tx_queue = rxq;
2492                         fs->peer_addr = fs->tx_port;
2493                         fs->retry_enabled = retry_enabled;
2494                         if (verbose_level > 0)
2495                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
2496                                        sm_id, fs->rx_port, fs->rx_queue,
2497                                        fs->tx_queue);
2498                         rxq = (queueid_t) (rxq + 1);
2499                         if (rxq == nb_rxq) {
2500                                 rxq = 0;
2501                                 rxp = (portid_t) (rxp + 1);
2502                         }
2503                 }
2504         }
2505 }
2506
2507 #if defined RTE_LIBRTE_PMD_SOFTNIC
2508 static void
2509 softnic_fwd_config_setup(void)
2510 {
2511         struct rte_port *port;
2512         portid_t pid, softnic_portid;
2513         queueid_t i;
2514         uint8_t softnic_enable = 0;
2515
2516         RTE_ETH_FOREACH_DEV(pid) {
2517                         port = &ports[pid];
2518                         const char *driver = port->dev_info.driver_name;
2519
2520                         if (strcmp(driver, "net_softnic") == 0) {
2521                                 softnic_portid = pid;
2522                                 softnic_enable = 1;
2523                                 break;
2524                         }
2525         }
2526
2527         if (softnic_enable == 0) {
2528                 printf("Softnic mode not configured(%s)!\n", __func__);
2529                 return;
2530         }
2531
2532         cur_fwd_config.nb_fwd_ports = 1;
2533         cur_fwd_config.nb_fwd_streams = (streamid_t) nb_rxq;
2534
2535         /* Re-initialize forwarding streams */
2536         init_fwd_streams();
2537
2538         /*
2539          * In the softnic forwarding test, the number of forwarding cores
2540          * is set to one and remaining are used for softnic packet processing.
2541          */
2542         cur_fwd_config.nb_fwd_lcores = 1;
2543         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2544
2545         for (i = 0; i < cur_fwd_config.nb_fwd_streams; i++) {
2546                 fwd_streams[i]->rx_port   = softnic_portid;
2547                 fwd_streams[i]->rx_queue  = i;
2548                 fwd_streams[i]->tx_port   = softnic_portid;
2549                 fwd_streams[i]->tx_queue  = i;
2550                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
2551                 fwd_streams[i]->retry_enabled = retry_enabled;
2552         }
2553 }
2554 #endif
2555
2556 void
2557 fwd_config_setup(void)
2558 {
2559         cur_fwd_config.fwd_eng = cur_fwd_eng;
2560         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
2561                 icmp_echo_config_setup();
2562                 return;
2563         }
2564
2565 #if defined RTE_LIBRTE_PMD_SOFTNIC
2566         if (strcmp(cur_fwd_eng->fwd_mode_name, "softnic") == 0) {
2567                 softnic_fwd_config_setup();
2568                 return;
2569         }
2570 #endif
2571
2572         if ((nb_rxq > 1) && (nb_txq > 1)){
2573                 if (dcb_config)
2574                         dcb_fwd_config_setup();
2575                 else
2576                         rss_fwd_config_setup();
2577         }
2578         else
2579                 simple_fwd_config_setup();
2580 }
2581
2582 static const char *
2583 mp_alloc_to_str(uint8_t mode)
2584 {
2585         switch (mode) {
2586         case MP_ALLOC_NATIVE:
2587                 return "native";
2588         case MP_ALLOC_ANON:
2589                 return "anon";
2590         case MP_ALLOC_XMEM:
2591                 return "xmem";
2592         case MP_ALLOC_XMEM_HUGE:
2593                 return "xmemhuge";
2594         case MP_ALLOC_XBUF:
2595                 return "xbuf";
2596         default:
2597                 return "invalid";
2598         }
2599 }
2600
2601 void
2602 pkt_fwd_config_display(struct fwd_config *cfg)
2603 {
2604         struct fwd_stream *fs;
2605         lcoreid_t  lc_id;
2606         streamid_t sm_id;
2607
2608         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
2609                 "NUMA support %s, MP allocation mode: %s\n",
2610                 cfg->fwd_eng->fwd_mode_name,
2611                 retry_enabled == 0 ? "" : " with retry",
2612                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
2613                 numa_support == 1 ? "enabled" : "disabled",
2614                 mp_alloc_to_str(mp_alloc_type));
2615
2616         if (retry_enabled)
2617                 printf("TX retry num: %u, delay between TX retries: %uus\n",
2618                         burst_tx_retry_num, burst_tx_delay_time);
2619         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
2620                 printf("Logical Core %u (socket %u) forwards packets on "
2621                        "%d streams:",
2622                        fwd_lcores_cpuids[lc_id],
2623                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
2624                        fwd_lcores[lc_id]->stream_nb);
2625                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2626                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2627                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
2628                                "P=%d/Q=%d (socket %u) ",
2629                                fs->rx_port, fs->rx_queue,
2630                                ports[fs->rx_port].socket_id,
2631                                fs->tx_port, fs->tx_queue,
2632                                ports[fs->tx_port].socket_id);
2633                         print_ethaddr("peer=",
2634                                       &peer_eth_addrs[fs->peer_addr]);
2635                 }
2636                 printf("\n");
2637         }
2638         printf("\n");
2639 }
2640
2641 void
2642 set_fwd_eth_peer(portid_t port_id, char *peer_addr)
2643 {
2644         struct rte_ether_addr new_peer_addr;
2645         if (!rte_eth_dev_is_valid_port(port_id)) {
2646                 printf("Error: Invalid port number %i\n", port_id);
2647                 return;
2648         }
2649         if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) {
2650                 printf("Error: Invalid ethernet address: %s\n", peer_addr);
2651                 return;
2652         }
2653         peer_eth_addrs[port_id] = new_peer_addr;
2654 }
2655
2656 int
2657 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
2658 {
2659         unsigned int i;
2660         unsigned int lcore_cpuid;
2661         int record_now;
2662
2663         record_now = 0;
2664  again:
2665         for (i = 0; i < nb_lc; i++) {
2666                 lcore_cpuid = lcorelist[i];
2667                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
2668                         printf("lcore %u not enabled\n", lcore_cpuid);
2669                         return -1;
2670                 }
2671                 if (lcore_cpuid == rte_get_master_lcore()) {
2672                         printf("lcore %u cannot be masked on for running "
2673                                "packet forwarding, which is the master lcore "
2674                                "and reserved for command line parsing only\n",
2675                                lcore_cpuid);
2676                         return -1;
2677                 }
2678                 if (record_now)
2679                         fwd_lcores_cpuids[i] = lcore_cpuid;
2680         }
2681         if (record_now == 0) {
2682                 record_now = 1;
2683                 goto again;
2684         }
2685         nb_cfg_lcores = (lcoreid_t) nb_lc;
2686         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
2687                 printf("previous number of forwarding cores %u - changed to "
2688                        "number of configured cores %u\n",
2689                        (unsigned int) nb_fwd_lcores, nb_lc);
2690                 nb_fwd_lcores = (lcoreid_t) nb_lc;
2691         }
2692
2693         return 0;
2694 }
2695
2696 int
2697 set_fwd_lcores_mask(uint64_t lcoremask)
2698 {
2699         unsigned int lcorelist[64];
2700         unsigned int nb_lc;
2701         unsigned int i;
2702
2703         if (lcoremask == 0) {
2704                 printf("Invalid NULL mask of cores\n");
2705                 return -1;
2706         }
2707         nb_lc = 0;
2708         for (i = 0; i < 64; i++) {
2709                 if (! ((uint64_t)(1ULL << i) & lcoremask))
2710                         continue;
2711                 lcorelist[nb_lc++] = i;
2712         }
2713         return set_fwd_lcores_list(lcorelist, nb_lc);
2714 }
2715
2716 void
2717 set_fwd_lcores_number(uint16_t nb_lc)
2718 {
2719         if (nb_lc > nb_cfg_lcores) {
2720                 printf("nb fwd cores %u > %u (max. number of configured "
2721                        "lcores) - ignored\n",
2722                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
2723                 return;
2724         }
2725         nb_fwd_lcores = (lcoreid_t) nb_lc;
2726         printf("Number of forwarding cores set to %u\n",
2727                (unsigned int) nb_fwd_lcores);
2728 }
2729
2730 void
2731 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
2732 {
2733         unsigned int i;
2734         portid_t port_id;
2735         int record_now;
2736
2737         record_now = 0;
2738  again:
2739         for (i = 0; i < nb_pt; i++) {
2740                 port_id = (portid_t) portlist[i];
2741                 if (port_id_is_invalid(port_id, ENABLED_WARN))
2742                         return;
2743                 if (record_now)
2744                         fwd_ports_ids[i] = port_id;
2745         }
2746         if (record_now == 0) {
2747                 record_now = 1;
2748                 goto again;
2749         }
2750         nb_cfg_ports = (portid_t) nb_pt;
2751         if (nb_fwd_ports != (portid_t) nb_pt) {
2752                 printf("previous number of forwarding ports %u - changed to "
2753                        "number of configured ports %u\n",
2754                        (unsigned int) nb_fwd_ports, nb_pt);
2755                 nb_fwd_ports = (portid_t) nb_pt;
2756         }
2757 }
2758
2759 /**
2760  * Parse the user input and obtain the list of forwarding ports
2761  *
2762  * @param[in] list
2763  *   String containing the user input. User can specify
2764  *   in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6.
2765  *   For example, if the user wants to use all the available
2766  *   4 ports in his system, then the input can be 0-3 or 0,1,2,3.
2767  *   If the user wants to use only the ports 1,2 then the input
2768  *   is 1,2.
2769  *   valid characters are '-' and ','
2770  * @param[out] values
2771  *   This array will be filled with a list of port IDs
2772  *   based on the user input
2773  *   Note that duplicate entries are discarded and only the first
2774  *   count entries in this array are port IDs and all the rest
2775  *   will contain default values
2776  * @param[in] maxsize
2777  *   This parameter denotes 2 things
2778  *   1) Number of elements in the values array
2779  *   2) Maximum value of each element in the values array
2780  * @return
2781  *   On success, returns total count of parsed port IDs
2782  *   On failure, returns 0
2783  */
2784 static unsigned int
2785 parse_port_list(const char *list, unsigned int *values, unsigned int maxsize)
2786 {
2787         unsigned int count = 0;
2788         char *end = NULL;
2789         int min, max;
2790         int value, i;
2791         unsigned int marked[maxsize];
2792
2793         if (list == NULL || values == NULL)
2794                 return 0;
2795
2796         for (i = 0; i < (int)maxsize; i++)
2797                 marked[i] = 0;
2798
2799         min = INT_MAX;
2800
2801         do {
2802                 /*Remove the blank spaces if any*/
2803                 while (isblank(*list))
2804                         list++;
2805                 if (*list == '\0')
2806                         break;
2807                 errno = 0;
2808                 value = strtol(list, &end, 10);
2809                 if (errno || end == NULL)
2810                         return 0;
2811                 if (value < 0 || value >= (int)maxsize)
2812                         return 0;
2813                 while (isblank(*end))
2814                         end++;
2815                 if (*end == '-' && min == INT_MAX) {
2816                         min = value;
2817                 } else if ((*end == ',') || (*end == '\0')) {
2818                         max = value;
2819                         if (min == INT_MAX)
2820                                 min = value;
2821                         for (i = min; i <= max; i++) {
2822                                 if (count < maxsize) {
2823                                         if (marked[i])
2824                                                 continue;
2825                                         values[count] = i;
2826                                         marked[i] = 1;
2827                                         count++;
2828                                 }
2829                         }
2830                         min = INT_MAX;
2831                 } else
2832                         return 0;
2833                 list = end + 1;
2834         } while (*end != '\0');
2835
2836         return count;
2837 }
2838
2839 void
2840 parse_fwd_portlist(const char *portlist)
2841 {
2842         unsigned int portcount;
2843         unsigned int portindex[RTE_MAX_ETHPORTS];
2844         unsigned int i, valid_port_count = 0;
2845
2846         portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS);
2847         if (!portcount)
2848                 rte_exit(EXIT_FAILURE, "Invalid fwd port list\n");
2849
2850         /*
2851          * Here we verify the validity of the ports
2852          * and thereby calculate the total number of
2853          * valid ports
2854          */
2855         for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) {
2856                 if (rte_eth_dev_is_valid_port(portindex[i])) {
2857                         portindex[valid_port_count] = portindex[i];
2858                         valid_port_count++;
2859                 }
2860         }
2861
2862         set_fwd_ports_list(portindex, valid_port_count);
2863 }
2864
2865 void
2866 set_fwd_ports_mask(uint64_t portmask)
2867 {
2868         unsigned int portlist[64];
2869         unsigned int nb_pt;
2870         unsigned int i;
2871
2872         if (portmask == 0) {
2873                 printf("Invalid NULL mask of ports\n");
2874                 return;
2875         }
2876         nb_pt = 0;
2877         RTE_ETH_FOREACH_DEV(i) {
2878                 if (! ((uint64_t)(1ULL << i) & portmask))
2879                         continue;
2880                 portlist[nb_pt++] = i;
2881         }
2882         set_fwd_ports_list(portlist, nb_pt);
2883 }
2884
2885 void
2886 set_fwd_ports_number(uint16_t nb_pt)
2887 {
2888         if (nb_pt > nb_cfg_ports) {
2889                 printf("nb fwd ports %u > %u (number of configured "
2890                        "ports) - ignored\n",
2891                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
2892                 return;
2893         }
2894         nb_fwd_ports = (portid_t) nb_pt;
2895         printf("Number of forwarding ports set to %u\n",
2896                (unsigned int) nb_fwd_ports);
2897 }
2898
2899 int
2900 port_is_forwarding(portid_t port_id)
2901 {
2902         unsigned int i;
2903
2904         if (port_id_is_invalid(port_id, ENABLED_WARN))
2905                 return -1;
2906
2907         for (i = 0; i < nb_fwd_ports; i++) {
2908                 if (fwd_ports_ids[i] == port_id)
2909                         return 1;
2910         }
2911
2912         return 0;
2913 }
2914
2915 void
2916 set_nb_pkt_per_burst(uint16_t nb)
2917 {
2918         if (nb > MAX_PKT_BURST) {
2919                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
2920                        " ignored\n",
2921                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
2922                 return;
2923         }
2924         nb_pkt_per_burst = nb;
2925         printf("Number of packets per burst set to %u\n",
2926                (unsigned int) nb_pkt_per_burst);
2927 }
2928
2929 static const char *
2930 tx_split_get_name(enum tx_pkt_split split)
2931 {
2932         uint32_t i;
2933
2934         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2935                 if (tx_split_name[i].split == split)
2936                         return tx_split_name[i].name;
2937         }
2938         return NULL;
2939 }
2940
2941 void
2942 set_tx_pkt_split(const char *name)
2943 {
2944         uint32_t i;
2945
2946         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2947                 if (strcmp(tx_split_name[i].name, name) == 0) {
2948                         tx_pkt_split = tx_split_name[i].split;
2949                         return;
2950                 }
2951         }
2952         printf("unknown value: \"%s\"\n", name);
2953 }
2954
2955 void
2956 show_tx_pkt_segments(void)
2957 {
2958         uint32_t i, n;
2959         const char *split;
2960
2961         n = tx_pkt_nb_segs;
2962         split = tx_split_get_name(tx_pkt_split);
2963
2964         printf("Number of segments: %u\n", n);
2965         printf("Segment sizes: ");
2966         for (i = 0; i != n - 1; i++)
2967                 printf("%hu,", tx_pkt_seg_lengths[i]);
2968         printf("%hu\n", tx_pkt_seg_lengths[i]);
2969         printf("Split packet: %s\n", split);
2970 }
2971
2972 void
2973 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
2974 {
2975         uint16_t tx_pkt_len;
2976         unsigned i;
2977
2978         if (nb_segs >= (unsigned) nb_txd) {
2979                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
2980                        nb_segs, (unsigned int) nb_txd);
2981                 return;
2982         }
2983
2984         /*
2985          * Check that each segment length is greater or equal than
2986          * the mbuf data sise.
2987          * Check also that the total packet length is greater or equal than the
2988          * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) +
2989          * 20 + 8).
2990          */
2991         tx_pkt_len = 0;
2992         for (i = 0; i < nb_segs; i++) {
2993                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
2994                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
2995                                i, seg_lengths[i], (unsigned) mbuf_data_size);
2996                         return;
2997                 }
2998                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
2999         }
3000         if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) {
3001                 printf("total packet length=%u < %d - give up\n",
3002                                 (unsigned) tx_pkt_len,
3003                                 (int)(sizeof(struct rte_ether_hdr) + 20 + 8));
3004                 return;
3005         }
3006
3007         for (i = 0; i < nb_segs; i++)
3008                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
3009
3010         tx_pkt_length  = tx_pkt_len;
3011         tx_pkt_nb_segs = (uint8_t) nb_segs;
3012 }
3013
3014 void
3015 setup_gro(const char *onoff, portid_t port_id)
3016 {
3017         if (!rte_eth_dev_is_valid_port(port_id)) {
3018                 printf("invalid port id %u\n", port_id);
3019                 return;
3020         }
3021         if (test_done == 0) {
3022                 printf("Before enable/disable GRO,"
3023                                 " please stop forwarding first\n");
3024                 return;
3025         }
3026         if (strcmp(onoff, "on") == 0) {
3027                 if (gro_ports[port_id].enable != 0) {
3028                         printf("Port %u has enabled GRO. Please"
3029                                         " disable GRO first\n", port_id);
3030                         return;
3031                 }
3032                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
3033                         gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
3034                         gro_ports[port_id].param.max_flow_num =
3035                                 GRO_DEFAULT_FLOW_NUM;
3036                         gro_ports[port_id].param.max_item_per_flow =
3037                                 GRO_DEFAULT_ITEM_NUM_PER_FLOW;
3038                 }
3039                 gro_ports[port_id].enable = 1;
3040         } else {
3041                 if (gro_ports[port_id].enable == 0) {
3042                         printf("Port %u has disabled GRO\n", port_id);
3043                         return;
3044                 }
3045                 gro_ports[port_id].enable = 0;
3046         }
3047 }
3048
3049 void
3050 setup_gro_flush_cycles(uint8_t cycles)
3051 {
3052         if (test_done == 0) {
3053                 printf("Before change flush interval for GRO,"
3054                                 " please stop forwarding first.\n");
3055                 return;
3056         }
3057
3058         if (cycles > GRO_MAX_FLUSH_CYCLES || cycles <
3059                         GRO_DEFAULT_FLUSH_CYCLES) {
3060                 printf("The flushing cycle be in the range"
3061                                 " of 1 to %u. Revert to the default"
3062                                 " value %u.\n",
3063                                 GRO_MAX_FLUSH_CYCLES,
3064                                 GRO_DEFAULT_FLUSH_CYCLES);
3065                 cycles = GRO_DEFAULT_FLUSH_CYCLES;
3066         }
3067
3068         gro_flush_cycles = cycles;
3069 }
3070
3071 void
3072 show_gro(portid_t port_id)
3073 {
3074         struct rte_gro_param *param;
3075         uint32_t max_pkts_num;
3076
3077         param = &gro_ports[port_id].param;
3078
3079         if (!rte_eth_dev_is_valid_port(port_id)) {
3080                 printf("Invalid port id %u.\n", port_id);
3081                 return;
3082         }
3083         if (gro_ports[port_id].enable) {
3084                 printf("GRO type: TCP/IPv4\n");
3085                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
3086                         max_pkts_num = param->max_flow_num *
3087                                 param->max_item_per_flow;
3088                 } else
3089                         max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES;
3090                 printf("Max number of packets to perform GRO: %u\n",
3091                                 max_pkts_num);
3092                 printf("Flushing cycles: %u\n", gro_flush_cycles);
3093         } else
3094                 printf("Port %u doesn't enable GRO.\n", port_id);
3095 }
3096
3097 void
3098 setup_gso(const char *mode, portid_t port_id)
3099 {
3100         if (!rte_eth_dev_is_valid_port(port_id)) {
3101                 printf("invalid port id %u\n", port_id);
3102                 return;
3103         }
3104         if (strcmp(mode, "on") == 0) {
3105                 if (test_done == 0) {
3106                         printf("before enabling GSO,"
3107                                         " please stop forwarding first\n");
3108                         return;
3109                 }
3110                 gso_ports[port_id].enable = 1;
3111         } else if (strcmp(mode, "off") == 0) {
3112                 if (test_done == 0) {
3113                         printf("before disabling GSO,"
3114                                         " please stop forwarding first\n");
3115                         return;
3116                 }
3117                 gso_ports[port_id].enable = 0;
3118         }
3119 }
3120
3121 char*
3122 list_pkt_forwarding_modes(void)
3123 {
3124         static char fwd_modes[128] = "";
3125         const char *separator = "|";
3126         struct fwd_engine *fwd_eng;
3127         unsigned i = 0;
3128
3129         if (strlen (fwd_modes) == 0) {
3130                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
3131                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
3132                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
3133                         strncat(fwd_modes, separator,
3134                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
3135                 }
3136                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
3137         }
3138
3139         return fwd_modes;
3140 }
3141
3142 char*
3143 list_pkt_forwarding_retry_modes(void)
3144 {
3145         static char fwd_modes[128] = "";
3146         const char *separator = "|";
3147         struct fwd_engine *fwd_eng;
3148         unsigned i = 0;
3149
3150         if (strlen(fwd_modes) == 0) {
3151                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
3152                         if (fwd_eng == &rx_only_engine)
3153                                 continue;
3154                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
3155                                         sizeof(fwd_modes) -
3156                                         strlen(fwd_modes) - 1);
3157                         strncat(fwd_modes, separator,
3158                                         sizeof(fwd_modes) -
3159                                         strlen(fwd_modes) - 1);
3160                 }
3161                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
3162         }
3163
3164         return fwd_modes;
3165 }
3166
3167 void
3168 set_pkt_forwarding_mode(const char *fwd_mode_name)
3169 {
3170         struct fwd_engine *fwd_eng;
3171         unsigned i;
3172
3173         i = 0;
3174         while ((fwd_eng = fwd_engines[i]) != NULL) {
3175                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
3176                         printf("Set %s packet forwarding mode%s\n",
3177                                fwd_mode_name,
3178                                retry_enabled == 0 ? "" : " with retry");
3179                         cur_fwd_eng = fwd_eng;
3180                         return;
3181                 }
3182                 i++;
3183         }
3184         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
3185 }
3186
3187 void
3188 add_rx_dump_callbacks(portid_t portid)
3189 {
3190         struct rte_eth_dev_info dev_info;
3191         uint16_t queue;
3192         int ret;
3193
3194         if (port_id_is_invalid(portid, ENABLED_WARN))
3195                 return;
3196
3197         ret = eth_dev_info_get_print_err(portid, &dev_info);
3198         if (ret != 0)
3199                 return;
3200
3201         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
3202                 if (!ports[portid].rx_dump_cb[queue])
3203                         ports[portid].rx_dump_cb[queue] =
3204                                 rte_eth_add_rx_callback(portid, queue,
3205                                         dump_rx_pkts, NULL);
3206 }
3207
3208 void
3209 add_tx_dump_callbacks(portid_t portid)
3210 {
3211         struct rte_eth_dev_info dev_info;
3212         uint16_t queue;
3213         int ret;
3214
3215         if (port_id_is_invalid(portid, ENABLED_WARN))
3216                 return;
3217
3218         ret = eth_dev_info_get_print_err(portid, &dev_info);
3219         if (ret != 0)
3220                 return;
3221
3222         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
3223                 if (!ports[portid].tx_dump_cb[queue])
3224                         ports[portid].tx_dump_cb[queue] =
3225                                 rte_eth_add_tx_callback(portid, queue,
3226                                                         dump_tx_pkts, NULL);
3227 }
3228
3229 void
3230 remove_rx_dump_callbacks(portid_t portid)
3231 {
3232         struct rte_eth_dev_info dev_info;
3233         uint16_t queue;
3234         int ret;
3235
3236         if (port_id_is_invalid(portid, ENABLED_WARN))
3237                 return;
3238
3239         ret = eth_dev_info_get_print_err(portid, &dev_info);
3240         if (ret != 0)
3241                 return;
3242
3243         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
3244                 if (ports[portid].rx_dump_cb[queue]) {
3245                         rte_eth_remove_rx_callback(portid, queue,
3246                                 ports[portid].rx_dump_cb[queue]);
3247                         ports[portid].rx_dump_cb[queue] = NULL;
3248                 }
3249 }
3250
3251 void
3252 remove_tx_dump_callbacks(portid_t portid)
3253 {
3254         struct rte_eth_dev_info dev_info;
3255         uint16_t queue;
3256         int ret;
3257
3258         if (port_id_is_invalid(portid, ENABLED_WARN))
3259                 return;
3260
3261         ret = eth_dev_info_get_print_err(portid, &dev_info);
3262         if (ret != 0)
3263                 return;
3264
3265         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
3266                 if (ports[portid].tx_dump_cb[queue]) {
3267                         rte_eth_remove_tx_callback(portid, queue,
3268                                 ports[portid].tx_dump_cb[queue]);
3269                         ports[portid].tx_dump_cb[queue] = NULL;
3270                 }
3271 }
3272
3273 void
3274 configure_rxtx_dump_callbacks(uint16_t verbose)
3275 {
3276         portid_t portid;
3277
3278 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3279                 TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n");
3280                 return;
3281 #endif
3282
3283         RTE_ETH_FOREACH_DEV(portid)
3284         {
3285                 if (verbose == 1 || verbose > 2)
3286                         add_rx_dump_callbacks(portid);
3287                 else
3288                         remove_rx_dump_callbacks(portid);
3289                 if (verbose >= 2)
3290                         add_tx_dump_callbacks(portid);
3291                 else
3292                         remove_tx_dump_callbacks(portid);
3293         }
3294 }
3295
3296 void
3297 set_verbose_level(uint16_t vb_level)
3298 {
3299         printf("Change verbose level from %u to %u\n",
3300                (unsigned int) verbose_level, (unsigned int) vb_level);
3301         verbose_level = vb_level;
3302         configure_rxtx_dump_callbacks(verbose_level);
3303 }
3304
3305 void
3306 vlan_extend_set(portid_t port_id, int on)
3307 {
3308         int diag;
3309         int vlan_offload;
3310         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3311
3312         if (port_id_is_invalid(port_id, ENABLED_WARN))
3313                 return;
3314
3315         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3316
3317         if (on) {
3318                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
3319                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
3320         } else {
3321                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
3322                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
3323         }
3324
3325         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3326         if (diag < 0)
3327                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
3328                "diag=%d\n", port_id, on, diag);
3329         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3330 }
3331
3332 void
3333 rx_vlan_strip_set(portid_t port_id, int on)
3334 {
3335         int diag;
3336         int vlan_offload;
3337         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3338
3339         if (port_id_is_invalid(port_id, ENABLED_WARN))
3340                 return;
3341
3342         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3343
3344         if (on) {
3345                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
3346                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
3347         } else {
3348                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
3349                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
3350         }
3351
3352         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3353         if (diag < 0)
3354                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
3355                "diag=%d\n", port_id, on, diag);
3356         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3357 }
3358
3359 void
3360 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
3361 {
3362         int diag;
3363
3364         if (port_id_is_invalid(port_id, ENABLED_WARN))
3365                 return;
3366
3367         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
3368         if (diag < 0)
3369                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
3370                "diag=%d\n", port_id, queue_id, on, diag);
3371 }
3372
3373 void
3374 rx_vlan_filter_set(portid_t port_id, int on)
3375 {
3376         int diag;
3377         int vlan_offload;
3378         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3379
3380         if (port_id_is_invalid(port_id, ENABLED_WARN))
3381                 return;
3382
3383         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3384
3385         if (on) {
3386                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
3387                 port_rx_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3388         } else {
3389                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
3390                 port_rx_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
3391         }
3392
3393         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3394         if (diag < 0)
3395                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
3396                "diag=%d\n", port_id, on, diag);
3397         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3398 }
3399
3400 void
3401 rx_vlan_qinq_strip_set(portid_t port_id, int on)
3402 {
3403         int diag;
3404         int vlan_offload;
3405         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
3406
3407         if (port_id_is_invalid(port_id, ENABLED_WARN))
3408                 return;
3409
3410         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
3411
3412         if (on) {
3413                 vlan_offload |= ETH_QINQ_STRIP_OFFLOAD;
3414                 port_rx_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
3415         } else {
3416                 vlan_offload &= ~ETH_QINQ_STRIP_OFFLOAD;
3417                 port_rx_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
3418         }
3419
3420         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
3421         if (diag < 0)
3422                 printf("%s(port_pi=%d, on=%d) failed "
3423                "diag=%d\n", __func__, port_id, on, diag);
3424         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
3425 }
3426
3427 int
3428 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
3429 {
3430         int diag;
3431
3432         if (port_id_is_invalid(port_id, ENABLED_WARN))
3433                 return 1;
3434         if (vlan_id_is_invalid(vlan_id))
3435                 return 1;
3436         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
3437         if (diag == 0)
3438                 return 0;
3439         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
3440                "diag=%d\n",
3441                port_id, vlan_id, on, diag);
3442         return -1;
3443 }
3444
3445 void
3446 rx_vlan_all_filter_set(portid_t port_id, int on)
3447 {
3448         uint16_t vlan_id;
3449
3450         if (port_id_is_invalid(port_id, ENABLED_WARN))
3451                 return;
3452         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
3453                 if (rx_vft_set(port_id, vlan_id, on))
3454                         break;
3455         }
3456 }
3457
3458 void
3459 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
3460 {
3461         int diag;
3462
3463         if (port_id_is_invalid(port_id, ENABLED_WARN))
3464                 return;
3465
3466         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
3467         if (diag == 0)
3468                 return;
3469
3470         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
3471                "diag=%d\n",
3472                port_id, vlan_type, tp_id, diag);
3473 }
3474
3475 void
3476 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
3477 {
3478         struct rte_eth_dev_info dev_info;
3479         int ret;
3480
3481         if (port_id_is_invalid(port_id, ENABLED_WARN))
3482                 return;
3483         if (vlan_id_is_invalid(vlan_id))
3484                 return;
3485
3486         if (ports[port_id].dev_conf.txmode.offloads &
3487             DEV_TX_OFFLOAD_QINQ_INSERT) {
3488                 printf("Error, as QinQ has been enabled.\n");
3489                 return;
3490         }
3491
3492         ret = eth_dev_info_get_print_err(port_id, &dev_info);
3493         if (ret != 0)
3494                 return;
3495
3496         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT) == 0) {
3497                 printf("Error: vlan insert is not supported by port %d\n",
3498                         port_id);
3499                 return;
3500         }
3501
3502         tx_vlan_reset(port_id);
3503         ports[port_id].dev_conf.txmode.offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
3504         ports[port_id].tx_vlan_id = vlan_id;
3505 }
3506
3507 void
3508 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
3509 {
3510         struct rte_eth_dev_info dev_info;
3511         int ret;
3512
3513         if (port_id_is_invalid(port_id, ENABLED_WARN))
3514                 return;
3515         if (vlan_id_is_invalid(vlan_id))
3516                 return;
3517         if (vlan_id_is_invalid(vlan_id_outer))
3518                 return;
3519
3520         ret = eth_dev_info_get_print_err(port_id, &dev_info);
3521         if (ret != 0)
3522                 return;
3523
3524         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_QINQ_INSERT) == 0) {
3525                 printf("Error: qinq insert not supported by port %d\n",
3526                         port_id);
3527                 return;
3528         }
3529
3530         tx_vlan_reset(port_id);
3531         ports[port_id].dev_conf.txmode.offloads |= (DEV_TX_OFFLOAD_VLAN_INSERT |
3532                                                     DEV_TX_OFFLOAD_QINQ_INSERT);
3533         ports[port_id].tx_vlan_id = vlan_id;
3534         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
3535 }
3536
3537 void
3538 tx_vlan_reset(portid_t port_id)
3539 {
3540         if (port_id_is_invalid(port_id, ENABLED_WARN))
3541                 return;
3542         ports[port_id].dev_conf.txmode.offloads &=
3543                                 ~(DEV_TX_OFFLOAD_VLAN_INSERT |
3544                                   DEV_TX_OFFLOAD_QINQ_INSERT);
3545         ports[port_id].tx_vlan_id = 0;
3546         ports[port_id].tx_vlan_id_outer = 0;
3547 }
3548
3549 void
3550 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
3551 {
3552         if (port_id_is_invalid(port_id, ENABLED_WARN))
3553                 return;
3554
3555         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
3556 }
3557
3558 void
3559 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
3560 {
3561         uint16_t i;
3562         uint8_t existing_mapping_found = 0;
3563
3564         if (port_id_is_invalid(port_id, ENABLED_WARN))
3565                 return;
3566
3567         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
3568                 return;
3569
3570         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3571                 printf("map_value not in required range 0..%d\n",
3572                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
3573                 return;
3574         }
3575
3576         if (!is_rx) { /*then tx*/
3577                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
3578                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
3579                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
3580                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
3581                                 existing_mapping_found = 1;
3582                                 break;
3583                         }
3584                 }
3585                 if (!existing_mapping_found) { /* A new additional mapping... */
3586                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
3587                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
3588                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
3589                         nb_tx_queue_stats_mappings++;
3590                 }
3591         }
3592         else { /*rx*/
3593                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
3594                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
3595                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
3596                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
3597                                 existing_mapping_found = 1;
3598                                 break;
3599                         }
3600                 }
3601                 if (!existing_mapping_found) { /* A new additional mapping... */
3602                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
3603                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
3604                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
3605                         nb_rx_queue_stats_mappings++;
3606                 }
3607         }
3608 }
3609
3610 void
3611 set_xstats_hide_zero(uint8_t on_off)
3612 {
3613         xstats_hide_zero = on_off;
3614 }
3615
3616 static inline void
3617 print_fdir_mask(struct rte_eth_fdir_masks *mask)
3618 {
3619         printf("\n    vlan_tci: 0x%04x", rte_be_to_cpu_16(mask->vlan_tci_mask));
3620
3621         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
3622                 printf(", mac_addr: 0x%02x, tunnel_type: 0x%01x,"
3623                         " tunnel_id: 0x%08x",
3624                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
3625                         rte_be_to_cpu_32(mask->tunnel_id_mask));
3626         else if (fdir_conf.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3627                 printf(", src_ipv4: 0x%08x, dst_ipv4: 0x%08x",
3628                         rte_be_to_cpu_32(mask->ipv4_mask.src_ip),
3629                         rte_be_to_cpu_32(mask->ipv4_mask.dst_ip));
3630
3631                 printf("\n    src_port: 0x%04x, dst_port: 0x%04x",
3632                         rte_be_to_cpu_16(mask->src_port_mask),
3633                         rte_be_to_cpu_16(mask->dst_port_mask));
3634
3635                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
3636                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[0]),
3637                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[1]),
3638                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[2]),
3639                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[3]));
3640
3641                 printf("\n    dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
3642                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[0]),
3643                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[1]),
3644                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[2]),
3645                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[3]));
3646         }
3647
3648         printf("\n");
3649 }
3650
3651 static inline void
3652 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
3653 {
3654         struct rte_eth_flex_payload_cfg *cfg;
3655         uint32_t i, j;
3656
3657         for (i = 0; i < flex_conf->nb_payloads; i++) {
3658                 cfg = &flex_conf->flex_set[i];
3659                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
3660                         printf("\n    RAW:  ");
3661                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
3662                         printf("\n    L2_PAYLOAD:  ");
3663                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
3664                         printf("\n    L3_PAYLOAD:  ");
3665                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
3666                         printf("\n    L4_PAYLOAD:  ");
3667                 else
3668                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
3669                 for (j = 0; j < num; j++)
3670                         printf("  %-5u", cfg->src_offset[j]);
3671         }
3672         printf("\n");
3673 }
3674
3675 static char *
3676 flowtype_to_str(uint16_t flow_type)
3677 {
3678         struct flow_type_info {
3679                 char str[32];
3680                 uint16_t ftype;
3681         };
3682
3683         uint8_t i;
3684         static struct flow_type_info flowtype_str_table[] = {
3685                 {"raw", RTE_ETH_FLOW_RAW},
3686                 {"ipv4", RTE_ETH_FLOW_IPV4},
3687                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
3688                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
3689                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
3690                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
3691                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
3692                 {"ipv6", RTE_ETH_FLOW_IPV6},
3693                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
3694                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
3695                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
3696                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
3697                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
3698                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
3699                 {"port", RTE_ETH_FLOW_PORT},
3700                 {"vxlan", RTE_ETH_FLOW_VXLAN},
3701                 {"geneve", RTE_ETH_FLOW_GENEVE},
3702                 {"nvgre", RTE_ETH_FLOW_NVGRE},
3703                 {"vxlan-gpe", RTE_ETH_FLOW_VXLAN_GPE},
3704         };
3705
3706         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
3707                 if (flowtype_str_table[i].ftype == flow_type)
3708                         return flowtype_str_table[i].str;
3709         }
3710
3711         return NULL;
3712 }
3713
3714 static inline void
3715 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
3716 {
3717         struct rte_eth_fdir_flex_mask *mask;
3718         uint32_t i, j;
3719         char *p;
3720
3721         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
3722                 mask = &flex_conf->flex_mask[i];
3723                 p = flowtype_to_str(mask->flow_type);
3724                 printf("\n    %s:\t", p ? p : "unknown");
3725                 for (j = 0; j < num; j++)
3726                         printf(" %02x", mask->mask[j]);
3727         }
3728         printf("\n");
3729 }
3730
3731 static inline void
3732 print_fdir_flow_type(uint32_t flow_types_mask)
3733 {
3734         int i;
3735         char *p;
3736
3737         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
3738                 if (!(flow_types_mask & (1 << i)))
3739                         continue;
3740                 p = flowtype_to_str(i);
3741                 if (p)
3742                         printf(" %s", p);
3743                 else
3744                         printf(" unknown");
3745         }
3746         printf("\n");
3747 }
3748
3749 void
3750 fdir_get_infos(portid_t port_id)
3751 {
3752         struct rte_eth_fdir_stats fdir_stat;
3753         struct rte_eth_fdir_info fdir_info;
3754         int ret;
3755
3756         static const char *fdir_stats_border = "########################";
3757
3758         if (port_id_is_invalid(port_id, ENABLED_WARN))
3759                 return;
3760         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
3761         if (ret < 0) {
3762                 printf("\n FDIR is not supported on port %-2d\n",
3763                         port_id);
3764                 return;
3765         }
3766
3767         memset(&fdir_info, 0, sizeof(fdir_info));
3768         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
3769                                RTE_ETH_FILTER_INFO, &fdir_info);
3770         memset(&fdir_stat, 0, sizeof(fdir_stat));
3771         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
3772                                RTE_ETH_FILTER_STATS, &fdir_stat);
3773         printf("\n  %s FDIR infos for port %-2d     %s\n",
3774                fdir_stats_border, port_id, fdir_stats_border);
3775         printf("  MODE: ");
3776         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
3777                 printf("  PERFECT\n");
3778         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
3779                 printf("  PERFECT-MAC-VLAN\n");
3780         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
3781                 printf("  PERFECT-TUNNEL\n");
3782         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
3783                 printf("  SIGNATURE\n");
3784         else
3785                 printf("  DISABLE\n");
3786         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
3787                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
3788                 printf("  SUPPORTED FLOW TYPE: ");
3789                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
3790         }
3791         printf("  FLEX PAYLOAD INFO:\n");
3792         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
3793                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
3794                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
3795                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
3796                 fdir_info.flex_payload_unit,
3797                 fdir_info.max_flex_payload_segment_num,
3798                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
3799         printf("  MASK: ");
3800         print_fdir_mask(&fdir_info.mask);
3801         if (fdir_info.flex_conf.nb_payloads > 0) {
3802                 printf("  FLEX PAYLOAD SRC OFFSET:");
3803                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
3804         }
3805         if (fdir_info.flex_conf.nb_flexmasks > 0) {
3806                 printf("  FLEX MASK CFG:");
3807                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
3808         }
3809         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
3810                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
3811         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
3812                fdir_info.guarant_spc, fdir_info.best_spc);
3813         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
3814                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
3815                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
3816                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
3817                fdir_stat.collision, fdir_stat.free,
3818                fdir_stat.maxhash, fdir_stat.maxlen,
3819                fdir_stat.add, fdir_stat.remove,
3820                fdir_stat.f_add, fdir_stat.f_remove);
3821         printf("  %s############################%s\n",
3822                fdir_stats_border, fdir_stats_border);
3823 }
3824
3825 void
3826 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
3827 {
3828         struct rte_port *port;
3829         struct rte_eth_fdir_flex_conf *flex_conf;
3830         int i, idx = 0;
3831
3832         port = &ports[port_id];
3833         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
3834         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
3835                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
3836                         idx = i;
3837                         break;
3838                 }
3839         }
3840         if (i >= RTE_ETH_FLOW_MAX) {
3841                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
3842                         idx = flex_conf->nb_flexmasks;
3843                         flex_conf->nb_flexmasks++;
3844                 } else {
3845                         printf("The flex mask table is full. Can not set flex"
3846                                 " mask for flow_type(%u).", cfg->flow_type);
3847                         return;
3848                 }
3849         }
3850         rte_memcpy(&flex_conf->flex_mask[idx],
3851                          cfg,
3852                          sizeof(struct rte_eth_fdir_flex_mask));
3853 }
3854
3855 void
3856 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
3857 {
3858         struct rte_port *port;
3859         struct rte_eth_fdir_flex_conf *flex_conf;
3860         int i, idx = 0;
3861
3862         port = &ports[port_id];
3863         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
3864         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
3865                 if (cfg->type == flex_conf->flex_set[i].type) {
3866                         idx = i;
3867                         break;
3868                 }
3869         }
3870         if (i >= RTE_ETH_PAYLOAD_MAX) {
3871                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
3872                         idx = flex_conf->nb_payloads;
3873                         flex_conf->nb_payloads++;
3874                 } else {
3875                         printf("The flex payload table is full. Can not set"
3876                                 " flex payload for type(%u).", cfg->type);
3877                         return;
3878                 }
3879         }
3880         rte_memcpy(&flex_conf->flex_set[idx],
3881                          cfg,
3882                          sizeof(struct rte_eth_flex_payload_cfg));
3883
3884 }
3885
3886 void
3887 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
3888 {
3889 #ifdef RTE_LIBRTE_IXGBE_PMD
3890         int diag;
3891
3892         if (is_rx)
3893                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
3894         else
3895                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
3896
3897         if (diag == 0)
3898                 return;
3899         printf("rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
3900                         is_rx ? "rx" : "tx", port_id, diag);
3901         return;
3902 #endif
3903         printf("VF %s setting not supported for port %d\n",
3904                         is_rx ? "Rx" : "Tx", port_id);
3905         RTE_SET_USED(vf);
3906         RTE_SET_USED(on);
3907 }
3908
3909 int
3910 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
3911 {
3912         int diag;
3913         struct rte_eth_link link;
3914         int ret;
3915
3916         if (port_id_is_invalid(port_id, ENABLED_WARN))
3917                 return 1;
3918         ret = eth_link_get_nowait_print_err(port_id, &link);
3919         if (ret < 0)
3920                 return 1;
3921         if (rate > link.link_speed) {
3922                 printf("Invalid rate value:%u bigger than link speed: %u\n",
3923                         rate, link.link_speed);
3924                 return 1;
3925         }
3926         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
3927         if (diag == 0)
3928                 return diag;
3929         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
3930                 port_id, diag);
3931         return diag;
3932 }
3933
3934 int
3935 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
3936 {
3937         int diag = -ENOTSUP;
3938
3939         RTE_SET_USED(vf);
3940         RTE_SET_USED(rate);
3941         RTE_SET_USED(q_msk);
3942
3943 #ifdef RTE_LIBRTE_IXGBE_PMD
3944         if (diag == -ENOTSUP)
3945                 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
3946                                                        q_msk);
3947 #endif
3948 #ifdef RTE_LIBRTE_BNXT_PMD
3949         if (diag == -ENOTSUP)
3950                 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
3951 #endif
3952         if (diag == 0)
3953                 return diag;
3954
3955         printf("set_vf_rate_limit for port_id=%d failed diag=%d\n",
3956                 port_id, diag);
3957         return diag;
3958 }
3959
3960 /*
3961  * Functions to manage the set of filtered Multicast MAC addresses.
3962  *
3963  * A pool of filtered multicast MAC addresses is associated with each port.
3964  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
3965  * The address of the pool and the number of valid multicast MAC addresses
3966  * recorded in the pool are stored in the fields "mc_addr_pool" and
3967  * "mc_addr_nb" of the "rte_port" data structure.
3968  *
3969  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
3970  * to be supplied a contiguous array of multicast MAC addresses.
3971  * To comply with this constraint, the set of multicast addresses recorded
3972  * into the pool are systematically compacted at the beginning of the pool.
3973  * Hence, when a multicast address is removed from the pool, all following
3974  * addresses, if any, are copied back to keep the set contiguous.
3975  */
3976 #define MCAST_POOL_INC 32
3977
3978 static int
3979 mcast_addr_pool_extend(struct rte_port *port)
3980 {
3981         struct rte_ether_addr *mc_pool;
3982         size_t mc_pool_size;
3983
3984         /*
3985          * If a free entry is available at the end of the pool, just
3986          * increment the number of recorded multicast addresses.
3987          */
3988         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
3989                 port->mc_addr_nb++;
3990                 return 0;
3991         }
3992
3993         /*
3994          * [re]allocate a pool with MCAST_POOL_INC more entries.
3995          * The previous test guarantees that port->mc_addr_nb is a multiple
3996          * of MCAST_POOL_INC.
3997          */
3998         mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb +
3999                                                     MCAST_POOL_INC);
4000         mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool,
4001                                                 mc_pool_size);
4002         if (mc_pool == NULL) {
4003                 printf("allocation of pool of %u multicast addresses failed\n",
4004                        port->mc_addr_nb + MCAST_POOL_INC);
4005                 return -ENOMEM;
4006         }
4007
4008         port->mc_addr_pool = mc_pool;
4009         port->mc_addr_nb++;
4010         return 0;
4011
4012 }
4013
4014 static void
4015 mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr)
4016 {
4017         if (mcast_addr_pool_extend(port) != 0)
4018                 return;
4019         rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]);
4020 }
4021
4022 static void
4023 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
4024 {
4025         port->mc_addr_nb--;
4026         if (addr_idx == port->mc_addr_nb) {
4027                 /* No need to recompact the set of multicast addressses. */
4028                 if (port->mc_addr_nb == 0) {
4029                         /* free the pool of multicast addresses. */
4030                         free(port->mc_addr_pool);
4031                         port->mc_addr_pool = NULL;
4032                 }
4033                 return;
4034         }
4035         memmove(&port->mc_addr_pool[addr_idx],
4036                 &port->mc_addr_pool[addr_idx + 1],
4037                 sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx));
4038 }
4039
4040 static int
4041 eth_port_multicast_addr_list_set(portid_t port_id)
4042 {
4043         struct rte_port *port;
4044         int diag;
4045
4046         port = &ports[port_id];
4047         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
4048                                             port->mc_addr_nb);
4049         if (diag < 0)
4050                 printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
4051                         port_id, port->mc_addr_nb, diag);
4052
4053         return diag;
4054 }
4055
4056 void
4057 mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr)
4058 {
4059         struct rte_port *port;
4060         uint32_t i;
4061
4062         if (port_id_is_invalid(port_id, ENABLED_WARN))
4063                 return;
4064
4065         port = &ports[port_id];
4066
4067         /*
4068          * Check that the added multicast MAC address is not already recorded
4069          * in the pool of multicast addresses.
4070          */
4071         for (i = 0; i < port->mc_addr_nb; i++) {
4072                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
4073                         printf("multicast address already filtered by port\n");
4074                         return;
4075                 }
4076         }
4077
4078         mcast_addr_pool_append(port, mc_addr);
4079         if (eth_port_multicast_addr_list_set(port_id) < 0)
4080                 /* Rollback on failure, remove the address from the pool */
4081                 mcast_addr_pool_remove(port, i);
4082 }
4083
4084 void
4085 mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr)
4086 {
4087         struct rte_port *port;
4088         uint32_t i;
4089
4090         if (port_id_is_invalid(port_id, ENABLED_WARN))
4091                 return;
4092
4093         port = &ports[port_id];
4094
4095         /*
4096          * Search the pool of multicast MAC addresses for the removed address.
4097          */
4098         for (i = 0; i < port->mc_addr_nb; i++) {
4099                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
4100                         break;
4101         }
4102         if (i == port->mc_addr_nb) {
4103                 printf("multicast address not filtered by port %d\n", port_id);
4104                 return;
4105         }
4106
4107         mcast_addr_pool_remove(port, i);
4108         if (eth_port_multicast_addr_list_set(port_id) < 0)
4109                 /* Rollback on failure, add the address back into the pool */
4110                 mcast_addr_pool_append(port, mc_addr);
4111 }
4112
4113 void
4114 port_dcb_info_display(portid_t port_id)
4115 {
4116         struct rte_eth_dcb_info dcb_info;
4117         uint16_t i;
4118         int ret;
4119         static const char *border = "================";
4120
4121         if (port_id_is_invalid(port_id, ENABLED_WARN))
4122                 return;
4123
4124         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
4125         if (ret) {
4126                 printf("\n Failed to get dcb infos on port %-2d\n",
4127                         port_id);
4128                 return;
4129         }
4130         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
4131         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
4132         printf("\n  TC :        ");
4133         for (i = 0; i < dcb_info.nb_tcs; i++)
4134                 printf("\t%4d", i);
4135         printf("\n  Priority :  ");
4136         for (i = 0; i < dcb_info.nb_tcs; i++)
4137                 printf("\t%4d", dcb_info.prio_tc[i]);
4138         printf("\n  BW percent :");
4139         for (i = 0; i < dcb_info.nb_tcs; i++)
4140                 printf("\t%4d%%", dcb_info.tc_bws[i]);
4141         printf("\n  RXQ base :  ");
4142         for (i = 0; i < dcb_info.nb_tcs; i++)
4143                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
4144         printf("\n  RXQ number :");
4145         for (i = 0; i < dcb_info.nb_tcs; i++)
4146                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
4147         printf("\n  TXQ base :  ");
4148         for (i = 0; i < dcb_info.nb_tcs; i++)
4149                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
4150         printf("\n  TXQ number :");
4151         for (i = 0; i < dcb_info.nb_tcs; i++)
4152                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
4153         printf("\n");
4154 }
4155
4156 uint8_t *
4157 open_file(const char *file_path, uint32_t *size)
4158 {
4159         int fd = open(file_path, O_RDONLY);
4160         off_t pkg_size;
4161         uint8_t *buf = NULL;
4162         int ret = 0;
4163         struct stat st_buf;
4164
4165         if (size)
4166                 *size = 0;
4167
4168         if (fd == -1) {
4169                 printf("%s: Failed to open %s\n", __func__, file_path);
4170                 return buf;
4171         }
4172
4173         if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) {
4174                 close(fd);
4175                 printf("%s: File operations failed\n", __func__);
4176                 return buf;
4177         }
4178
4179         pkg_size = st_buf.st_size;
4180         if (pkg_size < 0) {
4181                 close(fd);
4182                 printf("%s: File operations failed\n", __func__);
4183                 return buf;
4184         }
4185
4186         buf = (uint8_t *)malloc(pkg_size);
4187         if (!buf) {
4188                 close(fd);
4189                 printf("%s: Failed to malloc memory\n", __func__);
4190                 return buf;
4191         }
4192
4193         ret = read(fd, buf, pkg_size);
4194         if (ret < 0) {
4195                 close(fd);
4196                 printf("%s: File read operation failed\n", __func__);
4197                 close_file(buf);
4198                 return NULL;
4199         }
4200
4201         if (size)
4202                 *size = pkg_size;
4203
4204         close(fd);
4205
4206         return buf;
4207 }
4208
4209 int
4210 save_file(const char *file_path, uint8_t *buf, uint32_t size)
4211 {
4212         FILE *fh = fopen(file_path, "wb");
4213
4214         if (fh == NULL) {
4215                 printf("%s: Failed to open %s\n", __func__, file_path);
4216                 return -1;
4217         }
4218
4219         if (fwrite(buf, 1, size, fh) != size) {
4220                 fclose(fh);
4221                 printf("%s: File write operation failed\n", __func__);
4222                 return -1;
4223         }
4224
4225         fclose(fh);
4226
4227         return 0;
4228 }
4229
4230 int
4231 close_file(uint8_t *buf)
4232 {
4233         if (buf) {
4234                 free((void *)buf);
4235                 return 0;
4236         }
4237
4238         return -1;
4239 }
4240
4241 void
4242 port_queue_region_info_display(portid_t port_id, void *buf)
4243 {
4244 #ifdef RTE_LIBRTE_I40E_PMD
4245         uint16_t i, j;
4246         struct rte_pmd_i40e_queue_regions *info =
4247                 (struct rte_pmd_i40e_queue_regions *)buf;
4248         static const char *queue_region_info_stats_border = "-------";
4249
4250         if (!info->queue_region_number)
4251                 printf("there is no region has been set before");
4252
4253         printf("\n      %s All queue region info for port=%2d %s",
4254                         queue_region_info_stats_border, port_id,
4255                         queue_region_info_stats_border);
4256         printf("\n      queue_region_number: %-14u \n",
4257                         info->queue_region_number);
4258
4259         for (i = 0; i < info->queue_region_number; i++) {
4260                 printf("\n      region_id: %-14u queue_number: %-14u "
4261                         "queue_start_index: %-14u \n",
4262                         info->region[i].region_id,
4263                         info->region[i].queue_num,
4264                         info->region[i].queue_start_index);
4265
4266                 printf("  user_priority_num is  %-14u :",
4267                                         info->region[i].user_priority_num);
4268                 for (j = 0; j < info->region[i].user_priority_num; j++)
4269                         printf(" %-14u ", info->region[i].user_priority[j]);
4270
4271                 printf("\n      flowtype_num is  %-14u :",
4272                                 info->region[i].flowtype_num);
4273                 for (j = 0; j < info->region[i].flowtype_num; j++)
4274                         printf(" %-14u ", info->region[i].hw_flowtype[j]);
4275         }
4276 #else
4277         RTE_SET_USED(port_id);
4278         RTE_SET_USED(buf);
4279 #endif
4280
4281         printf("\n\n");
4282 }
4283
4284 void
4285 show_macs(portid_t port_id)
4286 {
4287         char buf[RTE_ETHER_ADDR_FMT_SIZE];
4288         struct rte_eth_dev_info dev_info;
4289         struct rte_ether_addr *addr;
4290         uint32_t i, num_macs = 0;
4291         struct rte_eth_dev *dev;
4292
4293         dev = &rte_eth_devices[port_id];
4294
4295         rte_eth_dev_info_get(port_id, &dev_info);
4296
4297         for (i = 0; i < dev_info.max_mac_addrs; i++) {
4298                 addr = &dev->data->mac_addrs[i];
4299
4300                 /* skip zero address */
4301                 if (rte_is_zero_ether_addr(addr))
4302                         continue;
4303
4304                 num_macs++;
4305         }
4306
4307         printf("Number of MAC address added: %d\n", num_macs);
4308
4309         for (i = 0; i < dev_info.max_mac_addrs; i++) {
4310                 addr = &dev->data->mac_addrs[i];
4311
4312                 /* skip zero address */
4313                 if (rte_is_zero_ether_addr(addr))
4314                         continue;
4315
4316                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
4317                 printf("  %s\n", buf);
4318         }
4319 }
4320
4321 void
4322 show_mcast_macs(portid_t port_id)
4323 {
4324         char buf[RTE_ETHER_ADDR_FMT_SIZE];
4325         struct rte_ether_addr *addr;
4326         struct rte_port *port;
4327         uint32_t i;
4328
4329         port = &ports[port_id];
4330
4331         printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb);
4332
4333         for (i = 0; i < port->mc_addr_nb; i++) {
4334                 addr = &port->mc_addr_pool[i];
4335
4336                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
4337                 printf("  %s\n", buf);
4338         }
4339 }