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