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