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