app/testpmd: add retry option
[dpdk.git] / app / test-pmd / config.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*   BSD LICENSE
34  *
35  *   Copyright 2013-2014 6WIND S.A.
36  *
37  *   Redistribution and use in source and binary forms, with or without
38  *   modification, are permitted provided that the following conditions
39  *   are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in
45  *       the documentation and/or other materials provided with the
46  *       distribution.
47  *     * Neither the name of 6WIND S.A. nor the names of its
48  *       contributors may be used to endorse or promote products derived
49  *       from this software without specific prior written permission.
50  *
51  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63
64 #include <stdarg.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <string.h>
68 #include <stdarg.h>
69 #include <stdint.h>
70 #include <inttypes.h>
71
72 #include <sys/queue.h>
73
74 #include <rte_common.h>
75 #include <rte_byteorder.h>
76 #include <rte_debug.h>
77 #include <rte_log.h>
78 #include <rte_memory.h>
79 #include <rte_memcpy.h>
80 #include <rte_memzone.h>
81 #include <rte_launch.h>
82 #include <rte_eal.h>
83 #include <rte_per_lcore.h>
84 #include <rte_lcore.h>
85 #include <rte_atomic.h>
86 #include <rte_branch_prediction.h>
87 #include <rte_ring.h>
88 #include <rte_mempool.h>
89 #include <rte_mbuf.h>
90 #include <rte_interrupts.h>
91 #include <rte_pci.h>
92 #include <rte_ether.h>
93 #include <rte_ethdev.h>
94 #include <rte_string_fns.h>
95
96 #include "testpmd.h"
97
98 static char *flowtype_to_str(uint16_t flow_type);
99
100 static const struct {
101         enum tx_pkt_split split;
102         const char *name;
103 } tx_split_name[] = {
104         {
105                 .split = TX_PKT_SPLIT_OFF,
106                 .name = "off",
107         },
108         {
109                 .split = TX_PKT_SPLIT_ON,
110                 .name = "on",
111         },
112         {
113                 .split = TX_PKT_SPLIT_RND,
114                 .name = "rand",
115         },
116 };
117
118 struct rss_type_info {
119         char str[32];
120         uint64_t rss_type;
121 };
122
123 static const struct rss_type_info rss_type_table[] = {
124         { "ipv4", ETH_RSS_IPV4 },
125         { "ipv4-frag", ETH_RSS_FRAG_IPV4 },
126         { "ipv4-tcp", ETH_RSS_NONFRAG_IPV4_TCP },
127         { "ipv4-udp", ETH_RSS_NONFRAG_IPV4_UDP },
128         { "ipv4-sctp", ETH_RSS_NONFRAG_IPV4_SCTP },
129         { "ipv4-other", ETH_RSS_NONFRAG_IPV4_OTHER },
130         { "ipv6", ETH_RSS_IPV6 },
131         { "ipv6-frag", ETH_RSS_FRAG_IPV6 },
132         { "ipv6-tcp", ETH_RSS_NONFRAG_IPV6_TCP },
133         { "ipv6-udp", ETH_RSS_NONFRAG_IPV6_UDP },
134         { "ipv6-sctp", ETH_RSS_NONFRAG_IPV6_SCTP },
135         { "ipv6-other", ETH_RSS_NONFRAG_IPV6_OTHER },
136         { "l2-payload", ETH_RSS_L2_PAYLOAD },
137         { "ipv6-ex", ETH_RSS_IPV6_EX },
138         { "ipv6-tcp-ex", ETH_RSS_IPV6_TCP_EX },
139         { "ipv6-udp-ex", ETH_RSS_IPV6_UDP_EX },
140 };
141
142 static void
143 print_ethaddr(const char *name, struct ether_addr *eth_addr)
144 {
145         char buf[ETHER_ADDR_FMT_SIZE];
146         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
147         printf("%s%s", name, buf);
148 }
149
150 void
151 nic_stats_display(portid_t port_id)
152 {
153         struct rte_eth_stats stats;
154         struct rte_port *port = &ports[port_id];
155         uint8_t i;
156         portid_t pid;
157
158         static const char *nic_stats_border = "########################";
159
160         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
161                 printf("Valid port range is [0");
162                 FOREACH_PORT(pid, ports)
163                         printf(", %d", pid);
164                 printf("]\n");
165                 return;
166         }
167         rte_eth_stats_get(port_id, &stats);
168         printf("\n  %s NIC statistics for port %-2d %s\n",
169                nic_stats_border, port_id, nic_stats_border);
170
171         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
172                 printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
173                        "%-"PRIu64"\n",
174                        stats.ipackets, stats.imissed, stats.ibytes);
175                 printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
176                 printf("  RX-nombuf:  %-10"PRIu64"\n",
177                        stats.rx_nombuf);
178                 printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
179                        "%-"PRIu64"\n",
180                        stats.opackets, stats.oerrors, stats.obytes);
181         }
182         else {
183                 printf("  RX-packets:              %10"PRIu64"    RX-errors: %10"PRIu64
184                        "    RX-bytes: %10"PRIu64"\n",
185                        stats.ipackets, stats.ierrors, stats.ibytes);
186                 printf("  RX-errors:  %10"PRIu64"\n", stats.ierrors);
187                 printf("  RX-nombuf:               %10"PRIu64"\n",
188                        stats.rx_nombuf);
189                 printf("  TX-packets:              %10"PRIu64"    TX-errors: %10"PRIu64
190                        "    TX-bytes: %10"PRIu64"\n",
191                        stats.opackets, stats.oerrors, stats.obytes);
192         }
193
194         if (port->rx_queue_stats_mapping_enabled) {
195                 printf("\n");
196                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
197                         printf("  Stats reg %2d RX-packets: %10"PRIu64
198                                "    RX-errors: %10"PRIu64
199                                "    RX-bytes: %10"PRIu64"\n",
200                                i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
201                 }
202         }
203         if (port->tx_queue_stats_mapping_enabled) {
204                 printf("\n");
205                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
206                         printf("  Stats reg %2d TX-packets: %10"PRIu64
207                                "                             TX-bytes: %10"PRIu64"\n",
208                                i, stats.q_opackets[i], stats.q_obytes[i]);
209                 }
210         }
211
212         printf("  %s############################%s\n",
213                nic_stats_border, nic_stats_border);
214 }
215
216 void
217 nic_stats_clear(portid_t port_id)
218 {
219         portid_t pid;
220
221         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
222                 printf("Valid port range is [0");
223                 FOREACH_PORT(pid, ports)
224                         printf(", %d", pid);
225                 printf("]\n");
226                 return;
227         }
228         rte_eth_stats_reset(port_id);
229         printf("\n  NIC statistics for port %d cleared\n", port_id);
230 }
231
232 void
233 nic_xstats_display(portid_t port_id)
234 {
235         struct rte_eth_xstats *xstats;
236         int len, ret, i;
237
238         printf("###### NIC extended statistics for port %-2d\n", port_id);
239
240         len = rte_eth_xstats_get(port_id, NULL, 0);
241         if (len < 0) {
242                 printf("Cannot get xstats count\n");
243                 return;
244         }
245         xstats = malloc(sizeof(xstats[0]) * len);
246         if (xstats == NULL) {
247                 printf("Cannot allocate memory for xstats\n");
248                 return;
249         }
250         ret = rte_eth_xstats_get(port_id, xstats, len);
251         if (ret < 0 || ret > len) {
252                 printf("Cannot get xstats\n");
253                 free(xstats);
254                 return;
255         }
256         for (i = 0; i < len; i++)
257                 printf("%s: %"PRIu64"\n", xstats[i].name, xstats[i].value);
258         free(xstats);
259 }
260
261 void
262 nic_xstats_clear(portid_t port_id)
263 {
264         rte_eth_xstats_reset(port_id);
265 }
266
267 void
268 nic_stats_mapping_display(portid_t port_id)
269 {
270         struct rte_port *port = &ports[port_id];
271         uint16_t i;
272         portid_t pid;
273
274         static const char *nic_stats_mapping_border = "########################";
275
276         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
277                 printf("Valid port range is [0");
278                 FOREACH_PORT(pid, ports)
279                         printf(", %d", pid);
280                 printf("]\n");
281                 return;
282         }
283
284         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
285                 printf("Port id %d - either does not support queue statistic mapping or"
286                        " no queue statistic mapping set\n", port_id);
287                 return;
288         }
289
290         printf("\n  %s NIC statistics mapping for port %-2d %s\n",
291                nic_stats_mapping_border, port_id, nic_stats_mapping_border);
292
293         if (port->rx_queue_stats_mapping_enabled) {
294                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
295                         if (rx_queue_stats_mappings[i].port_id == port_id) {
296                                 printf("  RX-queue %2d mapped to Stats Reg %2d\n",
297                                        rx_queue_stats_mappings[i].queue_id,
298                                        rx_queue_stats_mappings[i].stats_counter_id);
299                         }
300                 }
301                 printf("\n");
302         }
303
304
305         if (port->tx_queue_stats_mapping_enabled) {
306                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
307                         if (tx_queue_stats_mappings[i].port_id == port_id) {
308                                 printf("  TX-queue %2d mapped to Stats Reg %2d\n",
309                                        tx_queue_stats_mappings[i].queue_id,
310                                        tx_queue_stats_mappings[i].stats_counter_id);
311                         }
312                 }
313         }
314
315         printf("  %s####################################%s\n",
316                nic_stats_mapping_border, nic_stats_mapping_border);
317 }
318
319 void
320 rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
321 {
322         struct rte_eth_rxq_info qinfo;
323         int32_t rc;
324         static const char *info_border = "*********************";
325
326         rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
327         if (rc != 0) {
328                 printf("Failed to retrieve information for port: %hhu, "
329                         "RX queue: %hu\nerror desc: %s(%d)\n",
330                         port_id, queue_id, strerror(-rc), rc);
331                 return;
332         }
333
334         printf("\n%s Infos for port %-2u, RX queue %-2u %s",
335                info_border, port_id, queue_id, info_border);
336
337         printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
338         printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
339         printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
340         printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
341         printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
342         printf("\nRX drop packets: %s",
343                 (qinfo.conf.rx_drop_en != 0) ? "on" : "off");
344         printf("\nRX deferred start: %s",
345                 (qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
346         printf("\nRX scattered packets: %s",
347                 (qinfo.scattered_rx != 0) ? "on" : "off");
348         printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
349         printf("\n");
350 }
351
352 void
353 tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
354 {
355         struct rte_eth_txq_info qinfo;
356         int32_t rc;
357         static const char *info_border = "*********************";
358
359         rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
360         if (rc != 0) {
361                 printf("Failed to retrieve information for port: %hhu, "
362                         "TX queue: %hu\nerror desc: %s(%d)\n",
363                         port_id, queue_id, strerror(-rc), rc);
364                 return;
365         }
366
367         printf("\n%s Infos for port %-2u, TX queue %-2u %s",
368                info_border, port_id, queue_id, info_border);
369
370         printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
371         printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
372         printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
373         printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
374         printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
375         printf("\nTX flags: %#x", qinfo.conf.txq_flags);
376         printf("\nTX deferred start: %s",
377                 (qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
378         printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
379         printf("\n");
380 }
381
382 void
383 port_infos_display(portid_t port_id)
384 {
385         struct rte_port *port;
386         struct ether_addr mac_addr;
387         struct rte_eth_link link;
388         struct rte_eth_dev_info dev_info;
389         int vlan_offload;
390         struct rte_mempool * mp;
391         static const char *info_border = "*********************";
392         portid_t pid;
393
394         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
395                 printf("Valid port range is [0");
396                 FOREACH_PORT(pid, ports)
397                         printf(", %d", pid);
398                 printf("]\n");
399                 return;
400         }
401         port = &ports[port_id];
402         rte_eth_link_get_nowait(port_id, &link);
403         printf("\n%s Infos for port %-2d %s\n",
404                info_border, port_id, info_border);
405         rte_eth_macaddr_get(port_id, &mac_addr);
406         print_ethaddr("MAC address: ", &mac_addr);
407         printf("\nConnect to socket: %u", port->socket_id);
408
409         if (port_numa[port_id] != NUMA_NO_CONFIG) {
410                 mp = mbuf_pool_find(port_numa[port_id]);
411                 if (mp)
412                         printf("\nmemory allocation on the socket: %d",
413                                                         port_numa[port_id]);
414         } else
415                 printf("\nmemory allocation on the socket: %u",port->socket_id);
416
417         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
418         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
419         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
420                ("full-duplex") : ("half-duplex"));
421         printf("Promiscuous mode: %s\n",
422                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
423         printf("Allmulticast mode: %s\n",
424                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
425         printf("Maximum number of MAC addresses: %u\n",
426                (unsigned int)(port->dev_info.max_mac_addrs));
427         printf("Maximum number of MAC addresses of hash filtering: %u\n",
428                (unsigned int)(port->dev_info.max_hash_mac_addrs));
429
430         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
431         if (vlan_offload >= 0){
432                 printf("VLAN offload: \n");
433                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
434                         printf("  strip on \n");
435                 else
436                         printf("  strip off \n");
437
438                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
439                         printf("  filter on \n");
440                 else
441                         printf("  filter off \n");
442
443                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
444                         printf("  qinq(extend) on \n");
445                 else
446                         printf("  qinq(extend) off \n");
447         }
448
449         memset(&dev_info, 0, sizeof(dev_info));
450         rte_eth_dev_info_get(port_id, &dev_info);
451         if (dev_info.hash_key_size > 0)
452                 printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
453         if (dev_info.reta_size > 0)
454                 printf("Redirection table size: %u\n", dev_info.reta_size);
455         if (!dev_info.flow_type_rss_offloads)
456                 printf("No flow type is supported.\n");
457         else {
458                 uint16_t i;
459                 char *p;
460
461                 printf("Supported flow types:\n");
462                 for (i = RTE_ETH_FLOW_UNKNOWN + 1; i < RTE_ETH_FLOW_MAX;
463                                                                 i++) {
464                         if (!(dev_info.flow_type_rss_offloads & (1ULL << i)))
465                                 continue;
466                         p = flowtype_to_str(i);
467                         printf("  %s\n", (p ? p : "unknown"));
468                 }
469         }
470
471         printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
472         printf("Max possible number of RXDs per queue: %hu\n",
473                 dev_info.rx_desc_lim.nb_max);
474         printf("Min possible number of RXDs per queue: %hu\n",
475                 dev_info.rx_desc_lim.nb_min);
476         printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
477
478         printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
479         printf("Max possible number of TXDs per queue: %hu\n",
480                 dev_info.tx_desc_lim.nb_max);
481         printf("Min possible number of TXDs per queue: %hu\n",
482                 dev_info.tx_desc_lim.nb_min);
483         printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
484 }
485
486 int
487 port_id_is_invalid(portid_t port_id, enum print_warning warning)
488 {
489         if (port_id == (portid_t)RTE_PORT_ALL)
490                 return 0;
491
492         if (port_id < RTE_MAX_ETHPORTS && ports[port_id].enabled)
493                 return 0;
494
495         if (warning == ENABLED_WARN)
496                 printf("Invalid port %d\n", port_id);
497
498         return 1;
499 }
500
501 static int
502 vlan_id_is_invalid(uint16_t vlan_id)
503 {
504         if (vlan_id < 4096)
505                 return 0;
506         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
507         return 1;
508 }
509
510 static int
511 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
512 {
513         uint64_t pci_len;
514
515         if (reg_off & 0x3) {
516                 printf("Port register offset 0x%X not aligned on a 4-byte "
517                        "boundary\n",
518                        (unsigned)reg_off);
519                 return 1;
520         }
521         pci_len = ports[port_id].dev_info.pci_dev->mem_resource[0].len;
522         if (reg_off >= pci_len) {
523                 printf("Port %d: register offset %u (0x%X) out of port PCI "
524                        "resource (length=%"PRIu64")\n",
525                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
526                 return 1;
527         }
528         return 0;
529 }
530
531 static int
532 reg_bit_pos_is_invalid(uint8_t bit_pos)
533 {
534         if (bit_pos <= 31)
535                 return 0;
536         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
537         return 1;
538 }
539
540 #define display_port_and_reg_off(port_id, reg_off) \
541         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
542
543 static inline void
544 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
545 {
546         display_port_and_reg_off(port_id, (unsigned)reg_off);
547         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
548 }
549
550 void
551 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
552 {
553         uint32_t reg_v;
554
555
556         if (port_id_is_invalid(port_id, ENABLED_WARN))
557                 return;
558         if (port_reg_off_is_invalid(port_id, reg_off))
559                 return;
560         if (reg_bit_pos_is_invalid(bit_x))
561                 return;
562         reg_v = port_id_pci_reg_read(port_id, reg_off);
563         display_port_and_reg_off(port_id, (unsigned)reg_off);
564         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
565 }
566
567 void
568 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
569                            uint8_t bit1_pos, uint8_t bit2_pos)
570 {
571         uint32_t reg_v;
572         uint8_t  l_bit;
573         uint8_t  h_bit;
574
575         if (port_id_is_invalid(port_id, ENABLED_WARN))
576                 return;
577         if (port_reg_off_is_invalid(port_id, reg_off))
578                 return;
579         if (reg_bit_pos_is_invalid(bit1_pos))
580                 return;
581         if (reg_bit_pos_is_invalid(bit2_pos))
582                 return;
583         if (bit1_pos > bit2_pos)
584                 l_bit = bit2_pos, h_bit = bit1_pos;
585         else
586                 l_bit = bit1_pos, h_bit = bit2_pos;
587
588         reg_v = port_id_pci_reg_read(port_id, reg_off);
589         reg_v >>= l_bit;
590         if (h_bit < 31)
591                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
592         display_port_and_reg_off(port_id, (unsigned)reg_off);
593         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
594                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
595 }
596
597 void
598 port_reg_display(portid_t port_id, uint32_t reg_off)
599 {
600         uint32_t reg_v;
601
602         if (port_id_is_invalid(port_id, ENABLED_WARN))
603                 return;
604         if (port_reg_off_is_invalid(port_id, reg_off))
605                 return;
606         reg_v = port_id_pci_reg_read(port_id, reg_off);
607         display_port_reg_value(port_id, reg_off, reg_v);
608 }
609
610 void
611 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
612                  uint8_t bit_v)
613 {
614         uint32_t reg_v;
615
616         if (port_id_is_invalid(port_id, ENABLED_WARN))
617                 return;
618         if (port_reg_off_is_invalid(port_id, reg_off))
619                 return;
620         if (reg_bit_pos_is_invalid(bit_pos))
621                 return;
622         if (bit_v > 1) {
623                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
624                 return;
625         }
626         reg_v = port_id_pci_reg_read(port_id, reg_off);
627         if (bit_v == 0)
628                 reg_v &= ~(1 << bit_pos);
629         else
630                 reg_v |= (1 << bit_pos);
631         port_id_pci_reg_write(port_id, reg_off, reg_v);
632         display_port_reg_value(port_id, reg_off, reg_v);
633 }
634
635 void
636 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
637                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
638 {
639         uint32_t max_v;
640         uint32_t reg_v;
641         uint8_t  l_bit;
642         uint8_t  h_bit;
643
644         if (port_id_is_invalid(port_id, ENABLED_WARN))
645                 return;
646         if (port_reg_off_is_invalid(port_id, reg_off))
647                 return;
648         if (reg_bit_pos_is_invalid(bit1_pos))
649                 return;
650         if (reg_bit_pos_is_invalid(bit2_pos))
651                 return;
652         if (bit1_pos > bit2_pos)
653                 l_bit = bit2_pos, h_bit = bit1_pos;
654         else
655                 l_bit = bit1_pos, h_bit = bit2_pos;
656
657         if ((h_bit - l_bit) < 31)
658                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
659         else
660                 max_v = 0xFFFFFFFF;
661
662         if (value > max_v) {
663                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
664                                 (unsigned)value, (unsigned)value,
665                                 (unsigned)max_v, (unsigned)max_v);
666                 return;
667         }
668         reg_v = port_id_pci_reg_read(port_id, reg_off);
669         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
670         reg_v |= (value << l_bit); /* Set changed bits */
671         port_id_pci_reg_write(port_id, reg_off, reg_v);
672         display_port_reg_value(port_id, reg_off, reg_v);
673 }
674
675 void
676 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
677 {
678         if (port_id_is_invalid(port_id, ENABLED_WARN))
679                 return;
680         if (port_reg_off_is_invalid(port_id, reg_off))
681                 return;
682         port_id_pci_reg_write(port_id, reg_off, reg_v);
683         display_port_reg_value(port_id, reg_off, reg_v);
684 }
685
686 void
687 port_mtu_set(portid_t port_id, uint16_t mtu)
688 {
689         int diag;
690
691         if (port_id_is_invalid(port_id, ENABLED_WARN))
692                 return;
693         diag = rte_eth_dev_set_mtu(port_id, mtu);
694         if (diag == 0)
695                 return;
696         printf("Set MTU failed. diag=%d\n", diag);
697 }
698
699 /*
700  * RX/TX ring descriptors display functions.
701  */
702 int
703 rx_queue_id_is_invalid(queueid_t rxq_id)
704 {
705         if (rxq_id < nb_rxq)
706                 return 0;
707         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
708         return 1;
709 }
710
711 int
712 tx_queue_id_is_invalid(queueid_t txq_id)
713 {
714         if (txq_id < nb_txq)
715                 return 0;
716         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
717         return 1;
718 }
719
720 static int
721 rx_desc_id_is_invalid(uint16_t rxdesc_id)
722 {
723         if (rxdesc_id < nb_rxd)
724                 return 0;
725         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
726                rxdesc_id, nb_rxd);
727         return 1;
728 }
729
730 static int
731 tx_desc_id_is_invalid(uint16_t txdesc_id)
732 {
733         if (txdesc_id < nb_txd)
734                 return 0;
735         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
736                txdesc_id, nb_txd);
737         return 1;
738 }
739
740 static const struct rte_memzone *
741 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
742 {
743         char mz_name[RTE_MEMZONE_NAMESIZE];
744         const struct rte_memzone *mz;
745
746         snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
747                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
748         mz = rte_memzone_lookup(mz_name);
749         if (mz == NULL)
750                 printf("%s ring memory zoneof (port %d, queue %d) not"
751                        "found (zone name = %s\n",
752                        ring_name, port_id, q_id, mz_name);
753         return mz;
754 }
755
756 union igb_ring_dword {
757         uint64_t dword;
758         struct {
759 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
760                 uint32_t lo;
761                 uint32_t hi;
762 #else
763                 uint32_t hi;
764                 uint32_t lo;
765 #endif
766         } words;
767 };
768
769 struct igb_ring_desc_32_bytes {
770         union igb_ring_dword lo_dword;
771         union igb_ring_dword hi_dword;
772         union igb_ring_dword resv1;
773         union igb_ring_dword resv2;
774 };
775
776 struct igb_ring_desc_16_bytes {
777         union igb_ring_dword lo_dword;
778         union igb_ring_dword hi_dword;
779 };
780
781 static void
782 ring_rxd_display_dword(union igb_ring_dword dword)
783 {
784         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
785                                         (unsigned)dword.words.hi);
786 }
787
788 static void
789 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
790 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
791                            uint8_t port_id,
792 #else
793                            __rte_unused uint8_t port_id,
794 #endif
795                            uint16_t desc_id)
796 {
797         struct igb_ring_desc_16_bytes *ring =
798                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
799 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
800         struct rte_eth_dev_info dev_info;
801
802         memset(&dev_info, 0, sizeof(dev_info));
803         rte_eth_dev_info_get(port_id, &dev_info);
804         if (strstr(dev_info.driver_name, "i40e") != NULL) {
805                 /* 32 bytes RX descriptor, i40e only */
806                 struct igb_ring_desc_32_bytes *ring =
807                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
808                 ring[desc_id].lo_dword.dword =
809                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
810                 ring_rxd_display_dword(ring[desc_id].lo_dword);
811                 ring[desc_id].hi_dword.dword =
812                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
813                 ring_rxd_display_dword(ring[desc_id].hi_dword);
814                 ring[desc_id].resv1.dword =
815                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
816                 ring_rxd_display_dword(ring[desc_id].resv1);
817                 ring[desc_id].resv2.dword =
818                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
819                 ring_rxd_display_dword(ring[desc_id].resv2);
820
821                 return;
822         }
823 #endif
824         /* 16 bytes RX descriptor */
825         ring[desc_id].lo_dword.dword =
826                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
827         ring_rxd_display_dword(ring[desc_id].lo_dword);
828         ring[desc_id].hi_dword.dword =
829                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
830         ring_rxd_display_dword(ring[desc_id].hi_dword);
831 }
832
833 static void
834 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
835 {
836         struct igb_ring_desc_16_bytes *ring;
837         struct igb_ring_desc_16_bytes txd;
838
839         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
840         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
841         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
842         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
843                         (unsigned)txd.lo_dword.words.lo,
844                         (unsigned)txd.lo_dword.words.hi,
845                         (unsigned)txd.hi_dword.words.lo,
846                         (unsigned)txd.hi_dword.words.hi);
847 }
848
849 void
850 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
851 {
852         const struct rte_memzone *rx_mz;
853
854         if (port_id_is_invalid(port_id, ENABLED_WARN))
855                 return;
856         if (rx_queue_id_is_invalid(rxq_id))
857                 return;
858         if (rx_desc_id_is_invalid(rxd_id))
859                 return;
860         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
861         if (rx_mz == NULL)
862                 return;
863         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
864 }
865
866 void
867 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
868 {
869         const struct rte_memzone *tx_mz;
870
871         if (port_id_is_invalid(port_id, ENABLED_WARN))
872                 return;
873         if (tx_queue_id_is_invalid(txq_id))
874                 return;
875         if (tx_desc_id_is_invalid(txd_id))
876                 return;
877         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
878         if (tx_mz == NULL)
879                 return;
880         ring_tx_descriptor_display(tx_mz, txd_id);
881 }
882
883 void
884 fwd_lcores_config_display(void)
885 {
886         lcoreid_t lc_id;
887
888         printf("List of forwarding lcores:");
889         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
890                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
891         printf("\n");
892 }
893 void
894 rxtx_config_display(void)
895 {
896         printf("  %s packet forwarding%s - CRC stripping %s - "
897                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
898                retry_enabled == 0 ? "" : " with retry",
899                rx_mode.hw_strip_crc ? "enabled" : "disabled",
900                nb_pkt_per_burst);
901
902         if (cur_fwd_eng == &tx_only_engine)
903                 printf("  packet len=%u - nb packet segments=%d\n",
904                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
905
906         struct rte_eth_rxconf *rx_conf = &ports[0].rx_conf;
907         struct rte_eth_txconf *tx_conf = &ports[0].tx_conf;
908
909         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
910                nb_fwd_lcores, nb_fwd_ports);
911         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
912                nb_rxq, nb_rxd, rx_conf->rx_free_thresh);
913         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
914                rx_conf->rx_thresh.pthresh, rx_conf->rx_thresh.hthresh,
915                rx_conf->rx_thresh.wthresh);
916         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
917                nb_txq, nb_txd, tx_conf->tx_free_thresh);
918         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
919                tx_conf->tx_thresh.pthresh, tx_conf->tx_thresh.hthresh,
920                tx_conf->tx_thresh.wthresh);
921         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
922                tx_conf->tx_rs_thresh, tx_conf->txq_flags);
923 }
924
925 void
926 port_rss_reta_info(portid_t port_id,
927                    struct rte_eth_rss_reta_entry64 *reta_conf,
928                    uint16_t nb_entries)
929 {
930         uint16_t i, idx, shift;
931         int ret;
932
933         if (port_id_is_invalid(port_id, ENABLED_WARN))
934                 return;
935
936         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
937         if (ret != 0) {
938                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
939                 return;
940         }
941
942         for (i = 0; i < nb_entries; i++) {
943                 idx = i / RTE_RETA_GROUP_SIZE;
944                 shift = i % RTE_RETA_GROUP_SIZE;
945                 if (!(reta_conf[idx].mask & (1ULL << shift)))
946                         continue;
947                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
948                                         i, reta_conf[idx].reta[shift]);
949         }
950 }
951
952 /*
953  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
954  * key of the port.
955  */
956 void
957 port_rss_hash_conf_show(portid_t port_id, char rss_info[], int show_rss_key)
958 {
959         struct rte_eth_rss_conf rss_conf;
960         uint8_t rss_key[10 * 4] = "";
961         uint64_t rss_hf;
962         uint8_t i;
963         int diag;
964
965         if (port_id_is_invalid(port_id, ENABLED_WARN))
966                 return;
967
968         rss_conf.rss_hf = 0;
969         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
970                 if (!strcmp(rss_info, rss_type_table[i].str))
971                         rss_conf.rss_hf = rss_type_table[i].rss_type;
972         }
973
974         /* Get RSS hash key if asked to display it */
975         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
976         rss_conf.rss_key_len = sizeof(rss_key);
977         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
978         if (diag != 0) {
979                 switch (diag) {
980                 case -ENODEV:
981                         printf("port index %d invalid\n", port_id);
982                         break;
983                 case -ENOTSUP:
984                         printf("operation not supported by device\n");
985                         break;
986                 default:
987                         printf("operation failed - diag=%d\n", diag);
988                         break;
989                 }
990                 return;
991         }
992         rss_hf = rss_conf.rss_hf;
993         if (rss_hf == 0) {
994                 printf("RSS disabled\n");
995                 return;
996         }
997         printf("RSS functions:\n ");
998         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
999                 if (rss_hf & rss_type_table[i].rss_type)
1000                         printf("%s ", rss_type_table[i].str);
1001         }
1002         printf("\n");
1003         if (!show_rss_key)
1004                 return;
1005         printf("RSS key:\n");
1006         for (i = 0; i < sizeof(rss_key); i++)
1007                 printf("%02X", rss_key[i]);
1008         printf("\n");
1009 }
1010
1011 void
1012 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
1013                          uint hash_key_len)
1014 {
1015         struct rte_eth_rss_conf rss_conf;
1016         int diag;
1017         unsigned int i;
1018
1019         rss_conf.rss_key = NULL;
1020         rss_conf.rss_key_len = hash_key_len;
1021         rss_conf.rss_hf = 0;
1022         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1023                 if (!strcmp(rss_type_table[i].str, rss_type))
1024                         rss_conf.rss_hf = rss_type_table[i].rss_type;
1025         }
1026         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
1027         if (diag == 0) {
1028                 rss_conf.rss_key = hash_key;
1029                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
1030         }
1031         if (diag == 0)
1032                 return;
1033
1034         switch (diag) {
1035         case -ENODEV:
1036                 printf("port index %d invalid\n", port_id);
1037                 break;
1038         case -ENOTSUP:
1039                 printf("operation not supported by device\n");
1040                 break;
1041         default:
1042                 printf("operation failed - diag=%d\n", diag);
1043                 break;
1044         }
1045 }
1046
1047 /*
1048  * Setup forwarding configuration for each logical core.
1049  */
1050 static void
1051 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
1052 {
1053         streamid_t nb_fs_per_lcore;
1054         streamid_t nb_fs;
1055         streamid_t sm_id;
1056         lcoreid_t  nb_extra;
1057         lcoreid_t  nb_fc;
1058         lcoreid_t  nb_lc;
1059         lcoreid_t  lc_id;
1060
1061         nb_fs = cfg->nb_fwd_streams;
1062         nb_fc = cfg->nb_fwd_lcores;
1063         if (nb_fs <= nb_fc) {
1064                 nb_fs_per_lcore = 1;
1065                 nb_extra = 0;
1066         } else {
1067                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
1068                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
1069         }
1070
1071         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
1072         sm_id = 0;
1073         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
1074                 fwd_lcores[lc_id]->stream_idx = sm_id;
1075                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
1076                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1077         }
1078
1079         /*
1080          * Assign extra remaining streams, if any.
1081          */
1082         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
1083         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
1084                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
1085                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
1086                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1087         }
1088 }
1089
1090 static void
1091 simple_fwd_config_setup(void)
1092 {
1093         portid_t i;
1094         portid_t j;
1095         portid_t inc = 2;
1096
1097         if (port_topology == PORT_TOPOLOGY_CHAINED ||
1098             port_topology == PORT_TOPOLOGY_LOOP) {
1099                 inc = 1;
1100         } else if (nb_fwd_ports % 2) {
1101                 printf("\nWarning! Cannot handle an odd number of ports "
1102                        "with the current port topology. Configuration "
1103                        "must be changed to have an even number of ports, "
1104                        "or relaunch application with "
1105                        "--port-topology=chained\n\n");
1106         }
1107
1108         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
1109         cur_fwd_config.nb_fwd_streams =
1110                 (streamid_t) cur_fwd_config.nb_fwd_ports;
1111
1112         /* reinitialize forwarding streams */
1113         init_fwd_streams();
1114
1115         /*
1116          * In the simple forwarding test, the number of forwarding cores
1117          * must be lower or equal to the number of forwarding ports.
1118          */
1119         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1120         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
1121                 cur_fwd_config.nb_fwd_lcores =
1122                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
1123         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1124
1125         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
1126                 if (port_topology != PORT_TOPOLOGY_LOOP)
1127                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
1128                 else
1129                         j = i;
1130                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
1131                 fwd_streams[i]->rx_queue  = 0;
1132                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
1133                 fwd_streams[i]->tx_queue  = 0;
1134                 fwd_streams[i]->peer_addr = j;
1135                 fwd_streams[i]->retry_enabled = retry_enabled;
1136
1137                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
1138                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
1139                         fwd_streams[j]->rx_queue  = 0;
1140                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
1141                         fwd_streams[j]->tx_queue  = 0;
1142                         fwd_streams[j]->peer_addr = i;
1143                         fwd_streams[j]->retry_enabled = retry_enabled;
1144                 }
1145         }
1146 }
1147
1148 /**
1149  * For the RSS forwarding test, each core is assigned on every port a transmit
1150  * queue whose index is the index of the core itself. This approach limits the
1151  * maximumm number of processing cores of the RSS test to the maximum number of
1152  * TX queues supported by the devices.
1153  *
1154  * Each core is assigned a single stream, each stream being composed of
1155  * a RX queue to poll on a RX port for input messages, associated with
1156  * a TX queue of a TX port where to send forwarded packets.
1157  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1158  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1159  * following rules:
1160  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1161  *    - TxQl = RxQj
1162  */
1163 static void
1164 rss_fwd_config_setup(void)
1165 {
1166         portid_t   rxp;
1167         portid_t   txp;
1168         queueid_t  rxq;
1169         queueid_t  nb_q;
1170         lcoreid_t  lc_id;
1171
1172         nb_q = nb_rxq;
1173         if (nb_q > nb_txq)
1174                 nb_q = nb_txq;
1175         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1176         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1177         cur_fwd_config.nb_fwd_streams =
1178                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1179         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
1180                 cur_fwd_config.nb_fwd_streams =
1181                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
1182         else
1183                 cur_fwd_config.nb_fwd_lcores =
1184                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1185
1186         /* reinitialize forwarding streams */
1187         init_fwd_streams();
1188
1189         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1190         rxp = 0; rxq = 0;
1191         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1192                 struct fwd_stream *fs;
1193
1194                 fs = fwd_streams[lc_id];
1195
1196                 if ((rxp & 0x1) == 0)
1197                         txp = (portid_t) (rxp + 1);
1198                 else
1199                         txp = (portid_t) (rxp - 1);
1200                 /*
1201                  * if we are in loopback, simply send stuff out through the
1202                  * ingress port
1203                  */
1204                 if (port_topology == PORT_TOPOLOGY_LOOP)
1205                         txp = rxp;
1206
1207                 fs->rx_port = fwd_ports_ids[rxp];
1208                 fs->rx_queue = rxq;
1209                 fs->tx_port = fwd_ports_ids[txp];
1210                 fs->tx_queue = rxq;
1211                 fs->peer_addr = fs->tx_port;
1212                 fs->retry_enabled = retry_enabled;
1213                 rxq = (queueid_t) (rxq + 1);
1214                 if (rxq < nb_q)
1215                         continue;
1216                 /*
1217                  * rxq == nb_q
1218                  * Restart from RX queue 0 on next RX port
1219                  */
1220                 rxq = 0;
1221                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1222                         rxp = (portid_t)
1223                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1224                 else
1225                         rxp = (portid_t) (rxp + 1);
1226         }
1227 }
1228
1229 /**
1230  * For the DCB forwarding test, each core is assigned on each traffic class.
1231  *
1232  * Each core is assigned a multi-stream, each stream being composed of
1233  * a RX queue to poll on a RX port for input messages, associated with
1234  * a TX queue of a TX port where to send forwarded packets. All RX and
1235  * TX queues are mapping to the same traffic class.
1236  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
1237  * the same core
1238  */
1239 static void
1240 dcb_fwd_config_setup(void)
1241 {
1242         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
1243         portid_t txp, rxp = 0;
1244         queueid_t txq, rxq = 0;
1245         lcoreid_t  lc_id;
1246         uint16_t nb_rx_queue, nb_tx_queue;
1247         uint16_t i, j, k, sm_id = 0;
1248         uint8_t tc = 0;
1249
1250         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1251         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1252         cur_fwd_config.nb_fwd_streams =
1253                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1254
1255         /* reinitialize forwarding streams */
1256         init_fwd_streams();
1257         sm_id = 0;
1258         txp = 1;
1259         /* get the dcb info on the first RX and TX ports */
1260         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
1261         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
1262
1263         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1264                 fwd_lcores[lc_id]->stream_nb = 0;
1265                 fwd_lcores[lc_id]->stream_idx = sm_id;
1266                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
1267                         /* if the nb_queue is zero, means this tc is
1268                          * not enabled on the POOL
1269                          */
1270                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
1271                                 break;
1272                         k = fwd_lcores[lc_id]->stream_nb +
1273                                 fwd_lcores[lc_id]->stream_idx;
1274                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
1275                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
1276                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
1277                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
1278                         for (j = 0; j < nb_rx_queue; j++) {
1279                                 struct fwd_stream *fs;
1280
1281                                 fs = fwd_streams[k + j];
1282                                 fs->rx_port = fwd_ports_ids[rxp];
1283                                 fs->rx_queue = rxq + j;
1284                                 fs->tx_port = fwd_ports_ids[txp];
1285                                 fs->tx_queue = txq + j % nb_tx_queue;
1286                                 fs->peer_addr = fs->tx_port;
1287                                 fs->retry_enabled = retry_enabled;
1288                         }
1289                         fwd_lcores[lc_id]->stream_nb +=
1290                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
1291                 }
1292                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
1293
1294                 tc++;
1295                 if (tc < rxp_dcb_info.nb_tcs)
1296                         continue;
1297                 /* Restart from TC 0 on next RX port */
1298                 tc = 0;
1299                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1300                         rxp = (portid_t)
1301                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1302                 else
1303                         rxp++;
1304                 if (rxp >= nb_fwd_ports)
1305                         return;
1306                 /* get the dcb information on next RX and TX ports */
1307                 if ((rxp & 0x1) == 0)
1308                         txp = (portid_t) (rxp + 1);
1309                 else
1310                         txp = (portid_t) (rxp - 1);
1311                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
1312                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
1313         }
1314 }
1315
1316 static void
1317 icmp_echo_config_setup(void)
1318 {
1319         portid_t  rxp;
1320         queueid_t rxq;
1321         lcoreid_t lc_id;
1322         uint16_t  sm_id;
1323
1324         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
1325                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
1326                         (nb_txq * nb_fwd_ports);
1327         else
1328                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1329         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1330         cur_fwd_config.nb_fwd_streams =
1331                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1332         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1333                 cur_fwd_config.nb_fwd_lcores =
1334                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1335         if (verbose_level > 0) {
1336                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
1337                        __FUNCTION__,
1338                        cur_fwd_config.nb_fwd_lcores,
1339                        cur_fwd_config.nb_fwd_ports,
1340                        cur_fwd_config.nb_fwd_streams);
1341         }
1342
1343         /* reinitialize forwarding streams */
1344         init_fwd_streams();
1345         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1346         rxp = 0; rxq = 0;
1347         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1348                 if (verbose_level > 0)
1349                         printf("  core=%d: \n", lc_id);
1350                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1351                         struct fwd_stream *fs;
1352                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1353                         fs->rx_port = fwd_ports_ids[rxp];
1354                         fs->rx_queue = rxq;
1355                         fs->tx_port = fs->rx_port;
1356                         fs->tx_queue = rxq;
1357                         fs->peer_addr = fs->tx_port;
1358                         fs->retry_enabled = retry_enabled;
1359                         if (verbose_level > 0)
1360                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
1361                                        sm_id, fs->rx_port, fs->rx_queue,
1362                                        fs->tx_queue);
1363                         rxq = (queueid_t) (rxq + 1);
1364                         if (rxq == nb_rxq) {
1365                                 rxq = 0;
1366                                 rxp = (portid_t) (rxp + 1);
1367                         }
1368                 }
1369         }
1370 }
1371
1372 void
1373 fwd_config_setup(void)
1374 {
1375         cur_fwd_config.fwd_eng = cur_fwd_eng;
1376         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
1377                 icmp_echo_config_setup();
1378                 return;
1379         }
1380         if ((nb_rxq > 1) && (nb_txq > 1)){
1381                 if (dcb_config)
1382                         dcb_fwd_config_setup();
1383                 else
1384                         rss_fwd_config_setup();
1385         }
1386         else
1387                 simple_fwd_config_setup();
1388 }
1389
1390 void
1391 pkt_fwd_config_display(struct fwd_config *cfg)
1392 {
1393         struct fwd_stream *fs;
1394         lcoreid_t  lc_id;
1395         streamid_t sm_id;
1396
1397         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
1398                 "NUMA support %s, MP over anonymous pages %s\n",
1399                 cfg->fwd_eng->fwd_mode_name,
1400                 retry_enabled == 0 ? "" : " with retry",
1401                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
1402                 numa_support == 1 ? "enabled" : "disabled",
1403                 mp_anon != 0 ? "enabled" : "disabled");
1404
1405         if (retry_enabled)
1406                 printf("TX retry num: %u, delay between TX retries: %uus\n",
1407                         burst_tx_retry_num, burst_tx_delay_time);
1408         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
1409                 printf("Logical Core %u (socket %u) forwards packets on "
1410                        "%d streams:",
1411                        fwd_lcores_cpuids[lc_id],
1412                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
1413                        fwd_lcores[lc_id]->stream_nb);
1414                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1415                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1416                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
1417                                "P=%d/Q=%d (socket %u) ",
1418                                fs->rx_port, fs->rx_queue,
1419                                ports[fs->rx_port].socket_id,
1420                                fs->tx_port, fs->tx_queue,
1421                                ports[fs->tx_port].socket_id);
1422                         print_ethaddr("peer=",
1423                                       &peer_eth_addrs[fs->peer_addr]);
1424                 }
1425                 printf("\n");
1426         }
1427         printf("\n");
1428 }
1429
1430 int
1431 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
1432 {
1433         unsigned int i;
1434         unsigned int lcore_cpuid;
1435         int record_now;
1436
1437         record_now = 0;
1438  again:
1439         for (i = 0; i < nb_lc; i++) {
1440                 lcore_cpuid = lcorelist[i];
1441                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
1442                         printf("lcore %u not enabled\n", lcore_cpuid);
1443                         return -1;
1444                 }
1445                 if (lcore_cpuid == rte_get_master_lcore()) {
1446                         printf("lcore %u cannot be masked on for running "
1447                                "packet forwarding, which is the master lcore "
1448                                "and reserved for command line parsing only\n",
1449                                lcore_cpuid);
1450                         return -1;
1451                 }
1452                 if (record_now)
1453                         fwd_lcores_cpuids[i] = lcore_cpuid;
1454         }
1455         if (record_now == 0) {
1456                 record_now = 1;
1457                 goto again;
1458         }
1459         nb_cfg_lcores = (lcoreid_t) nb_lc;
1460         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
1461                 printf("previous number of forwarding cores %u - changed to "
1462                        "number of configured cores %u\n",
1463                        (unsigned int) nb_fwd_lcores, nb_lc);
1464                 nb_fwd_lcores = (lcoreid_t) nb_lc;
1465         }
1466
1467         return 0;
1468 }
1469
1470 int
1471 set_fwd_lcores_mask(uint64_t lcoremask)
1472 {
1473         unsigned int lcorelist[64];
1474         unsigned int nb_lc;
1475         unsigned int i;
1476
1477         if (lcoremask == 0) {
1478                 printf("Invalid NULL mask of cores\n");
1479                 return -1;
1480         }
1481         nb_lc = 0;
1482         for (i = 0; i < 64; i++) {
1483                 if (! ((uint64_t)(1ULL << i) & lcoremask))
1484                         continue;
1485                 lcorelist[nb_lc++] = i;
1486         }
1487         return set_fwd_lcores_list(lcorelist, nb_lc);
1488 }
1489
1490 void
1491 set_fwd_lcores_number(uint16_t nb_lc)
1492 {
1493         if (nb_lc > nb_cfg_lcores) {
1494                 printf("nb fwd cores %u > %u (max. number of configured "
1495                        "lcores) - ignored\n",
1496                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
1497                 return;
1498         }
1499         nb_fwd_lcores = (lcoreid_t) nb_lc;
1500         printf("Number of forwarding cores set to %u\n",
1501                (unsigned int) nb_fwd_lcores);
1502 }
1503
1504 void
1505 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
1506 {
1507         unsigned int i;
1508         portid_t port_id;
1509         int record_now;
1510
1511         record_now = 0;
1512  again:
1513         for (i = 0; i < nb_pt; i++) {
1514                 port_id = (portid_t) portlist[i];
1515                 if (port_id_is_invalid(port_id, ENABLED_WARN))
1516                         return;
1517                 if (record_now)
1518                         fwd_ports_ids[i] = port_id;
1519         }
1520         if (record_now == 0) {
1521                 record_now = 1;
1522                 goto again;
1523         }
1524         nb_cfg_ports = (portid_t) nb_pt;
1525         if (nb_fwd_ports != (portid_t) nb_pt) {
1526                 printf("previous number of forwarding ports %u - changed to "
1527                        "number of configured ports %u\n",
1528                        (unsigned int) nb_fwd_ports, nb_pt);
1529                 nb_fwd_ports = (portid_t) nb_pt;
1530         }
1531 }
1532
1533 void
1534 set_fwd_ports_mask(uint64_t portmask)
1535 {
1536         unsigned int portlist[64];
1537         unsigned int nb_pt;
1538         unsigned int i;
1539
1540         if (portmask == 0) {
1541                 printf("Invalid NULL mask of ports\n");
1542                 return;
1543         }
1544         nb_pt = 0;
1545         for (i = 0; i < (unsigned)RTE_MIN(64, RTE_MAX_ETHPORTS); i++) {
1546                 if (! ((uint64_t)(1ULL << i) & portmask))
1547                         continue;
1548                 portlist[nb_pt++] = i;
1549         }
1550         set_fwd_ports_list(portlist, nb_pt);
1551 }
1552
1553 void
1554 set_fwd_ports_number(uint16_t nb_pt)
1555 {
1556         if (nb_pt > nb_cfg_ports) {
1557                 printf("nb fwd ports %u > %u (number of configured "
1558                        "ports) - ignored\n",
1559                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
1560                 return;
1561         }
1562         nb_fwd_ports = (portid_t) nb_pt;
1563         printf("Number of forwarding ports set to %u\n",
1564                (unsigned int) nb_fwd_ports);
1565 }
1566
1567 int
1568 port_is_forwarding(portid_t port_id)
1569 {
1570         unsigned int i;
1571
1572         if (port_id_is_invalid(port_id, ENABLED_WARN))
1573                 return -1;
1574
1575         for (i = 0; i < nb_fwd_ports; i++) {
1576                 if (fwd_ports_ids[i] == port_id)
1577                         return 1;
1578         }
1579
1580         return 0;
1581 }
1582
1583 void
1584 set_nb_pkt_per_burst(uint16_t nb)
1585 {
1586         if (nb > MAX_PKT_BURST) {
1587                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
1588                        " ignored\n",
1589                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
1590                 return;
1591         }
1592         nb_pkt_per_burst = nb;
1593         printf("Number of packets per burst set to %u\n",
1594                (unsigned int) nb_pkt_per_burst);
1595 }
1596
1597 static const char *
1598 tx_split_get_name(enum tx_pkt_split split)
1599 {
1600         uint32_t i;
1601
1602         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
1603                 if (tx_split_name[i].split == split)
1604                         return tx_split_name[i].name;
1605         }
1606         return NULL;
1607 }
1608
1609 void
1610 set_tx_pkt_split(const char *name)
1611 {
1612         uint32_t i;
1613
1614         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
1615                 if (strcmp(tx_split_name[i].name, name) == 0) {
1616                         tx_pkt_split = tx_split_name[i].split;
1617                         return;
1618                 }
1619         }
1620         printf("unknown value: \"%s\"\n", name);
1621 }
1622
1623 void
1624 show_tx_pkt_segments(void)
1625 {
1626         uint32_t i, n;
1627         const char *split;
1628
1629         n = tx_pkt_nb_segs;
1630         split = tx_split_get_name(tx_pkt_split);
1631
1632         printf("Number of segments: %u\n", n);
1633         printf("Segment sizes: ");
1634         for (i = 0; i != n - 1; i++)
1635                 printf("%hu,", tx_pkt_seg_lengths[i]);
1636         printf("%hu\n", tx_pkt_seg_lengths[i]);
1637         printf("Split packet: %s\n", split);
1638 }
1639
1640 void
1641 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
1642 {
1643         uint16_t tx_pkt_len;
1644         unsigned i;
1645
1646         if (nb_segs >= (unsigned) nb_txd) {
1647                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
1648                        nb_segs, (unsigned int) nb_txd);
1649                 return;
1650         }
1651
1652         /*
1653          * Check that each segment length is greater or equal than
1654          * the mbuf data sise.
1655          * Check also that the total packet length is greater or equal than the
1656          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
1657          */
1658         tx_pkt_len = 0;
1659         for (i = 0; i < nb_segs; i++) {
1660                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
1661                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
1662                                i, seg_lengths[i], (unsigned) mbuf_data_size);
1663                         return;
1664                 }
1665                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
1666         }
1667         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
1668                 printf("total packet length=%u < %d - give up\n",
1669                                 (unsigned) tx_pkt_len,
1670                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
1671                 return;
1672         }
1673
1674         for (i = 0; i < nb_segs; i++)
1675                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
1676
1677         tx_pkt_length  = tx_pkt_len;
1678         tx_pkt_nb_segs = (uint8_t) nb_segs;
1679 }
1680
1681 char*
1682 list_pkt_forwarding_modes(void)
1683 {
1684         static char fwd_modes[128] = "";
1685         const char *separator = "|";
1686         struct fwd_engine *fwd_eng;
1687         unsigned i = 0;
1688
1689         if (strlen (fwd_modes) == 0) {
1690                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1691                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
1692                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
1693                         strncat(fwd_modes, separator,
1694                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
1695                 }
1696                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1697         }
1698
1699         return fwd_modes;
1700 }
1701
1702 char*
1703 list_pkt_forwarding_retry_modes(void)
1704 {
1705         static char fwd_modes[128] = "";
1706         const char *separator = "|";
1707         struct fwd_engine *fwd_eng;
1708         unsigned i = 0;
1709
1710         if (strlen(fwd_modes) == 0) {
1711                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1712                         if (fwd_eng == &rx_only_engine)
1713                                 continue;
1714                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
1715                                         sizeof(fwd_modes) -
1716                                         strlen(fwd_modes) - 1);
1717                         strncat(fwd_modes, separator,
1718                                         sizeof(fwd_modes) -
1719                                         strlen(fwd_modes) - 1);
1720                 }
1721                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1722         }
1723
1724         return fwd_modes;
1725 }
1726
1727 void
1728 set_pkt_forwarding_mode(const char *fwd_mode_name)
1729 {
1730         struct fwd_engine *fwd_eng;
1731         unsigned i;
1732
1733         i = 0;
1734         while ((fwd_eng = fwd_engines[i]) != NULL) {
1735                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
1736                         printf("Set %s packet forwarding mode%s\n",
1737                                fwd_mode_name,
1738                                retry_enabled == 0 ? "" : " with retry");
1739                         cur_fwd_eng = fwd_eng;
1740                         return;
1741                 }
1742                 i++;
1743         }
1744         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
1745 }
1746
1747 void
1748 set_verbose_level(uint16_t vb_level)
1749 {
1750         printf("Change verbose level from %u to %u\n",
1751                (unsigned int) verbose_level, (unsigned int) vb_level);
1752         verbose_level = vb_level;
1753 }
1754
1755 void
1756 vlan_extend_set(portid_t port_id, int on)
1757 {
1758         int diag;
1759         int vlan_offload;
1760
1761         if (port_id_is_invalid(port_id, ENABLED_WARN))
1762                 return;
1763
1764         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1765
1766         if (on)
1767                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
1768         else
1769                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
1770
1771         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1772         if (diag < 0)
1773                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
1774                "diag=%d\n", port_id, on, diag);
1775 }
1776
1777 void
1778 rx_vlan_strip_set(portid_t port_id, int on)
1779 {
1780         int diag;
1781         int vlan_offload;
1782
1783         if (port_id_is_invalid(port_id, ENABLED_WARN))
1784                 return;
1785
1786         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1787
1788         if (on)
1789                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
1790         else
1791                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
1792
1793         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1794         if (diag < 0)
1795                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
1796                "diag=%d\n", port_id, on, diag);
1797 }
1798
1799 void
1800 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
1801 {
1802         int diag;
1803
1804         if (port_id_is_invalid(port_id, ENABLED_WARN))
1805                 return;
1806
1807         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
1808         if (diag < 0)
1809                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
1810                "diag=%d\n", port_id, queue_id, on, diag);
1811 }
1812
1813 void
1814 rx_vlan_filter_set(portid_t port_id, int on)
1815 {
1816         int diag;
1817         int vlan_offload;
1818
1819         if (port_id_is_invalid(port_id, ENABLED_WARN))
1820                 return;
1821
1822         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1823
1824         if (on)
1825                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1826         else
1827                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
1828
1829         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1830         if (diag < 0)
1831                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
1832                "diag=%d\n", port_id, on, diag);
1833 }
1834
1835 int
1836 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
1837 {
1838         int diag;
1839
1840         if (port_id_is_invalid(port_id, ENABLED_WARN))
1841                 return 1;
1842         if (vlan_id_is_invalid(vlan_id))
1843                 return 1;
1844         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
1845         if (diag == 0)
1846                 return 0;
1847         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
1848                "diag=%d\n",
1849                port_id, vlan_id, on, diag);
1850         return -1;
1851 }
1852
1853 void
1854 rx_vlan_all_filter_set(portid_t port_id, int on)
1855 {
1856         uint16_t vlan_id;
1857
1858         if (port_id_is_invalid(port_id, ENABLED_WARN))
1859                 return;
1860         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
1861                 if (rx_vft_set(port_id, vlan_id, on))
1862                         break;
1863         }
1864 }
1865
1866 void
1867 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
1868 {
1869         int diag;
1870
1871         if (port_id_is_invalid(port_id, ENABLED_WARN))
1872                 return;
1873
1874         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
1875         if (diag == 0)
1876                 return;
1877
1878         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
1879                "diag=%d\n",
1880                port_id, vlan_type, tp_id, diag);
1881 }
1882
1883 void
1884 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
1885 {
1886         int vlan_offload;
1887         if (port_id_is_invalid(port_id, ENABLED_WARN))
1888                 return;
1889         if (vlan_id_is_invalid(vlan_id))
1890                 return;
1891
1892         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1893         if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD) {
1894                 printf("Error, as QinQ has been enabled.\n");
1895                 return;
1896         }
1897
1898         tx_vlan_reset(port_id);
1899         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1900         ports[port_id].tx_vlan_id = vlan_id;
1901 }
1902
1903 void
1904 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
1905 {
1906         int vlan_offload;
1907         if (port_id_is_invalid(port_id, ENABLED_WARN))
1908                 return;
1909         if (vlan_id_is_invalid(vlan_id))
1910                 return;
1911         if (vlan_id_is_invalid(vlan_id_outer))
1912                 return;
1913
1914         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1915         if (!(vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)) {
1916                 printf("Error, as QinQ hasn't been enabled.\n");
1917                 return;
1918         }
1919
1920         tx_vlan_reset(port_id);
1921         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_QINQ;
1922         ports[port_id].tx_vlan_id = vlan_id;
1923         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
1924 }
1925
1926 void
1927 tx_vlan_reset(portid_t port_id)
1928 {
1929         if (port_id_is_invalid(port_id, ENABLED_WARN))
1930                 return;
1931         ports[port_id].tx_ol_flags &= ~(TESTPMD_TX_OFFLOAD_INSERT_VLAN |
1932                                 TESTPMD_TX_OFFLOAD_INSERT_QINQ);
1933         ports[port_id].tx_vlan_id = 0;
1934         ports[port_id].tx_vlan_id_outer = 0;
1935 }
1936
1937 void
1938 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
1939 {
1940         if (port_id_is_invalid(port_id, ENABLED_WARN))
1941                 return;
1942
1943         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
1944 }
1945
1946 void
1947 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
1948 {
1949         uint16_t i;
1950         uint8_t existing_mapping_found = 0;
1951
1952         if (port_id_is_invalid(port_id, ENABLED_WARN))
1953                 return;
1954
1955         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
1956                 return;
1957
1958         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1959                 printf("map_value not in required range 0..%d\n",
1960                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1961                 return;
1962         }
1963
1964         if (!is_rx) { /*then tx*/
1965                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1966                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1967                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
1968                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
1969                                 existing_mapping_found = 1;
1970                                 break;
1971                         }
1972                 }
1973                 if (!existing_mapping_found) { /* A new additional mapping... */
1974                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
1975                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
1976                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
1977                         nb_tx_queue_stats_mappings++;
1978                 }
1979         }
1980         else { /*rx*/
1981                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1982                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1983                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
1984                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
1985                                 existing_mapping_found = 1;
1986                                 break;
1987                         }
1988                 }
1989                 if (!existing_mapping_found) { /* A new additional mapping... */
1990                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
1991                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
1992                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
1993                         nb_rx_queue_stats_mappings++;
1994                 }
1995         }
1996 }
1997
1998 static inline void
1999 print_fdir_mask(struct rte_eth_fdir_masks *mask)
2000 {
2001         printf("\n    vlan_tci: 0x%04x, ", mask->vlan_tci_mask);
2002
2003         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2004                 printf("mac_addr: 0x%02x", mask->mac_addr_byte_mask);
2005         else if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2006                 printf("mac_addr: 0x%02x, tunnel_type: 0x%01x, tunnel_id: 0x%08x",
2007                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
2008                         mask->tunnel_id_mask);
2009         else {
2010                 printf("src_ipv4: 0x%08x, dst_ipv4: 0x%08x,"
2011                         " src_port: 0x%04x, dst_port: 0x%04x",
2012                         mask->ipv4_mask.src_ip, mask->ipv4_mask.dst_ip,
2013                         mask->src_port_mask, mask->dst_port_mask);
2014
2015                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x,"
2016                         " dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2017                         mask->ipv6_mask.src_ip[0], mask->ipv6_mask.src_ip[1],
2018                         mask->ipv6_mask.src_ip[2], mask->ipv6_mask.src_ip[3],
2019                         mask->ipv6_mask.dst_ip[0], mask->ipv6_mask.dst_ip[1],
2020                         mask->ipv6_mask.dst_ip[2], mask->ipv6_mask.dst_ip[3]);
2021         }
2022
2023         printf("\n");
2024 }
2025
2026 static inline void
2027 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2028 {
2029         struct rte_eth_flex_payload_cfg *cfg;
2030         uint32_t i, j;
2031
2032         for (i = 0; i < flex_conf->nb_payloads; i++) {
2033                 cfg = &flex_conf->flex_set[i];
2034                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
2035                         printf("\n    RAW:  ");
2036                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
2037                         printf("\n    L2_PAYLOAD:  ");
2038                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
2039                         printf("\n    L3_PAYLOAD:  ");
2040                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
2041                         printf("\n    L4_PAYLOAD:  ");
2042                 else
2043                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
2044                 for (j = 0; j < num; j++)
2045                         printf("  %-5u", cfg->src_offset[j]);
2046         }
2047         printf("\n");
2048 }
2049
2050 static char *
2051 flowtype_to_str(uint16_t flow_type)
2052 {
2053         struct flow_type_info {
2054                 char str[32];
2055                 uint16_t ftype;
2056         };
2057
2058         uint8_t i;
2059         static struct flow_type_info flowtype_str_table[] = {
2060                 {"raw", RTE_ETH_FLOW_RAW},
2061                 {"ipv4", RTE_ETH_FLOW_IPV4},
2062                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
2063                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
2064                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
2065                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
2066                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
2067                 {"ipv6", RTE_ETH_FLOW_IPV6},
2068                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
2069                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
2070                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
2071                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
2072                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
2073                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
2074         };
2075
2076         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
2077                 if (flowtype_str_table[i].ftype == flow_type)
2078                         return flowtype_str_table[i].str;
2079         }
2080
2081         return NULL;
2082 }
2083
2084 static inline void
2085 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2086 {
2087         struct rte_eth_fdir_flex_mask *mask;
2088         uint32_t i, j;
2089         char *p;
2090
2091         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
2092                 mask = &flex_conf->flex_mask[i];
2093                 p = flowtype_to_str(mask->flow_type);
2094                 printf("\n    %s:\t", p ? p : "unknown");
2095                 for (j = 0; j < num; j++)
2096                         printf(" %02x", mask->mask[j]);
2097         }
2098         printf("\n");
2099 }
2100
2101 static inline void
2102 print_fdir_flow_type(uint32_t flow_types_mask)
2103 {
2104         int i;
2105         char *p;
2106
2107         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
2108                 if (!(flow_types_mask & (1 << i)))
2109                         continue;
2110                 p = flowtype_to_str(i);
2111                 if (p)
2112                         printf(" %s", p);
2113                 else
2114                         printf(" unknown");
2115         }
2116         printf("\n");
2117 }
2118
2119 void
2120 fdir_get_infos(portid_t port_id)
2121 {
2122         struct rte_eth_fdir_stats fdir_stat;
2123         struct rte_eth_fdir_info fdir_info;
2124         int ret;
2125
2126         static const char *fdir_stats_border = "########################";
2127
2128         if (port_id_is_invalid(port_id, ENABLED_WARN))
2129                 return;
2130         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
2131         if (ret < 0) {
2132                 printf("\n FDIR is not supported on port %-2d\n",
2133                         port_id);
2134                 return;
2135         }
2136
2137         memset(&fdir_info, 0, sizeof(fdir_info));
2138         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2139                                RTE_ETH_FILTER_INFO, &fdir_info);
2140         memset(&fdir_stat, 0, sizeof(fdir_stat));
2141         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2142                                RTE_ETH_FILTER_STATS, &fdir_stat);
2143         printf("\n  %s FDIR infos for port %-2d     %s\n",
2144                fdir_stats_border, port_id, fdir_stats_border);
2145         printf("  MODE: ");
2146         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
2147                 printf("  PERFECT\n");
2148         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2149                 printf("  PERFECT-MAC-VLAN\n");
2150         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2151                 printf("  PERFECT-TUNNEL\n");
2152         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
2153                 printf("  SIGNATURE\n");
2154         else
2155                 printf("  DISABLE\n");
2156         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
2157                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
2158                 printf("  SUPPORTED FLOW TYPE: ");
2159                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
2160         }
2161         printf("  FLEX PAYLOAD INFO:\n");
2162         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
2163                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
2164                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
2165                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
2166                 fdir_info.flex_payload_unit,
2167                 fdir_info.max_flex_payload_segment_num,
2168                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
2169         printf("  MASK: ");
2170         print_fdir_mask(&fdir_info.mask);
2171         if (fdir_info.flex_conf.nb_payloads > 0) {
2172                 printf("  FLEX PAYLOAD SRC OFFSET:");
2173                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2174         }
2175         if (fdir_info.flex_conf.nb_flexmasks > 0) {
2176                 printf("  FLEX MASK CFG:");
2177                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2178         }
2179         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
2180                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
2181         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
2182                fdir_info.guarant_spc, fdir_info.best_spc);
2183         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
2184                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
2185                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
2186                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
2187                fdir_stat.collision, fdir_stat.free,
2188                fdir_stat.maxhash, fdir_stat.maxlen,
2189                fdir_stat.add, fdir_stat.remove,
2190                fdir_stat.f_add, fdir_stat.f_remove);
2191         printf("  %s############################%s\n",
2192                fdir_stats_border, fdir_stats_border);
2193 }
2194
2195 void
2196 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
2197 {
2198         struct rte_port *port;
2199         struct rte_eth_fdir_flex_conf *flex_conf;
2200         int i, idx = 0;
2201
2202         port = &ports[port_id];
2203         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2204         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
2205                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
2206                         idx = i;
2207                         break;
2208                 }
2209         }
2210         if (i >= RTE_ETH_FLOW_MAX) {
2211                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
2212                         idx = flex_conf->nb_flexmasks;
2213                         flex_conf->nb_flexmasks++;
2214                 } else {
2215                         printf("The flex mask table is full. Can not set flex"
2216                                 " mask for flow_type(%u).", cfg->flow_type);
2217                         return;
2218                 }
2219         }
2220         (void)rte_memcpy(&flex_conf->flex_mask[idx],
2221                          cfg,
2222                          sizeof(struct rte_eth_fdir_flex_mask));
2223 }
2224
2225 void
2226 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
2227 {
2228         struct rte_port *port;
2229         struct rte_eth_fdir_flex_conf *flex_conf;
2230         int i, idx = 0;
2231
2232         port = &ports[port_id];
2233         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2234         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
2235                 if (cfg->type == flex_conf->flex_set[i].type) {
2236                         idx = i;
2237                         break;
2238                 }
2239         }
2240         if (i >= RTE_ETH_PAYLOAD_MAX) {
2241                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
2242                         idx = flex_conf->nb_payloads;
2243                         flex_conf->nb_payloads++;
2244                 } else {
2245                         printf("The flex payload table is full. Can not set"
2246                                 " flex payload for type(%u).", cfg->type);
2247                         return;
2248                 }
2249         }
2250         (void)rte_memcpy(&flex_conf->flex_set[idx],
2251                          cfg,
2252                          sizeof(struct rte_eth_flex_payload_cfg));
2253
2254 }
2255
2256 void
2257 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
2258 {
2259         int diag;
2260
2261         if (port_id_is_invalid(port_id, ENABLED_WARN))
2262                 return;
2263         if (is_rx)
2264                 diag = rte_eth_dev_set_vf_rx(port_id,vf,on);
2265         else
2266                 diag = rte_eth_dev_set_vf_tx(port_id,vf,on);
2267         if (diag == 0)
2268                 return;
2269         if(is_rx)
2270                 printf("rte_eth_dev_set_vf_rx for port_id=%d failed "
2271                         "diag=%d\n", port_id, diag);
2272         else
2273                 printf("rte_eth_dev_set_vf_tx for port_id=%d failed "
2274                         "diag=%d\n", port_id, diag);
2275
2276 }
2277
2278 void
2279 set_vf_rx_vlan(portid_t port_id, uint16_t vlan_id, uint64_t vf_mask, uint8_t on)
2280 {
2281         int diag;
2282
2283         if (port_id_is_invalid(port_id, ENABLED_WARN))
2284                 return;
2285         if (vlan_id_is_invalid(vlan_id))
2286                 return;
2287         diag = rte_eth_dev_set_vf_vlan_filter(port_id, vlan_id, vf_mask, on);
2288         if (diag == 0)
2289                 return;
2290         printf("rte_eth_dev_set_vf_vlan_filter for port_id=%d failed "
2291                "diag=%d\n", port_id, diag);
2292 }
2293
2294 int
2295 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
2296 {
2297         int diag;
2298         struct rte_eth_link link;
2299
2300         if (port_id_is_invalid(port_id, ENABLED_WARN))
2301                 return 1;
2302         rte_eth_link_get_nowait(port_id, &link);
2303         if (rate > link.link_speed) {
2304                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2305                         rate, link.link_speed);
2306                 return 1;
2307         }
2308         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
2309         if (diag == 0)
2310                 return diag;
2311         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
2312                 port_id, diag);
2313         return diag;
2314 }
2315
2316 int
2317 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
2318 {
2319         int diag;
2320         struct rte_eth_link link;
2321
2322         if (q_msk == 0)
2323                 return 0;
2324
2325         if (port_id_is_invalid(port_id, ENABLED_WARN))
2326                 return 1;
2327         rte_eth_link_get_nowait(port_id, &link);
2328         if (rate > link.link_speed) {
2329                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2330                         rate, link.link_speed);
2331                 return 1;
2332         }
2333         diag = rte_eth_set_vf_rate_limit(port_id, vf, rate, q_msk);
2334         if (diag == 0)
2335                 return diag;
2336         printf("rte_eth_set_vf_rate_limit for port_id=%d failed diag=%d\n",
2337                 port_id, diag);
2338         return diag;
2339 }
2340
2341 /*
2342  * Functions to manage the set of filtered Multicast MAC addresses.
2343  *
2344  * A pool of filtered multicast MAC addresses is associated with each port.
2345  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
2346  * The address of the pool and the number of valid multicast MAC addresses
2347  * recorded in the pool are stored in the fields "mc_addr_pool" and
2348  * "mc_addr_nb" of the "rte_port" data structure.
2349  *
2350  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
2351  * to be supplied a contiguous array of multicast MAC addresses.
2352  * To comply with this constraint, the set of multicast addresses recorded
2353  * into the pool are systematically compacted at the beginning of the pool.
2354  * Hence, when a multicast address is removed from the pool, all following
2355  * addresses, if any, are copied back to keep the set contiguous.
2356  */
2357 #define MCAST_POOL_INC 32
2358
2359 static int
2360 mcast_addr_pool_extend(struct rte_port *port)
2361 {
2362         struct ether_addr *mc_pool;
2363         size_t mc_pool_size;
2364
2365         /*
2366          * If a free entry is available at the end of the pool, just
2367          * increment the number of recorded multicast addresses.
2368          */
2369         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
2370                 port->mc_addr_nb++;
2371                 return 0;
2372         }
2373
2374         /*
2375          * [re]allocate a pool with MCAST_POOL_INC more entries.
2376          * The previous test guarantees that port->mc_addr_nb is a multiple
2377          * of MCAST_POOL_INC.
2378          */
2379         mc_pool_size = sizeof(struct ether_addr) * (port->mc_addr_nb +
2380                                                     MCAST_POOL_INC);
2381         mc_pool = (struct ether_addr *) realloc(port->mc_addr_pool,
2382                                                 mc_pool_size);
2383         if (mc_pool == NULL) {
2384                 printf("allocation of pool of %u multicast addresses failed\n",
2385                        port->mc_addr_nb + MCAST_POOL_INC);
2386                 return -ENOMEM;
2387         }
2388
2389         port->mc_addr_pool = mc_pool;
2390         port->mc_addr_nb++;
2391         return 0;
2392
2393 }
2394
2395 static void
2396 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
2397 {
2398         port->mc_addr_nb--;
2399         if (addr_idx == port->mc_addr_nb) {
2400                 /* No need to recompact the set of multicast addressses. */
2401                 if (port->mc_addr_nb == 0) {
2402                         /* free the pool of multicast addresses. */
2403                         free(port->mc_addr_pool);
2404                         port->mc_addr_pool = NULL;
2405                 }
2406                 return;
2407         }
2408         memmove(&port->mc_addr_pool[addr_idx],
2409                 &port->mc_addr_pool[addr_idx + 1],
2410                 sizeof(struct ether_addr) * (port->mc_addr_nb - addr_idx));
2411 }
2412
2413 static void
2414 eth_port_multicast_addr_list_set(uint8_t port_id)
2415 {
2416         struct rte_port *port;
2417         int diag;
2418
2419         port = &ports[port_id];
2420         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
2421                                             port->mc_addr_nb);
2422         if (diag == 0)
2423                 return;
2424         printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
2425                port->mc_addr_nb, port_id, -diag);
2426 }
2427
2428 void
2429 mcast_addr_add(uint8_t port_id, struct ether_addr *mc_addr)
2430 {
2431         struct rte_port *port;
2432         uint32_t i;
2433
2434         if (port_id_is_invalid(port_id, ENABLED_WARN))
2435                 return;
2436
2437         port = &ports[port_id];
2438
2439         /*
2440          * Check that the added multicast MAC address is not already recorded
2441          * in the pool of multicast addresses.
2442          */
2443         for (i = 0; i < port->mc_addr_nb; i++) {
2444                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
2445                         printf("multicast address already filtered by port\n");
2446                         return;
2447                 }
2448         }
2449
2450         if (mcast_addr_pool_extend(port) != 0)
2451                 return;
2452         ether_addr_copy(mc_addr, &port->mc_addr_pool[i]);
2453         eth_port_multicast_addr_list_set(port_id);
2454 }
2455
2456 void
2457 mcast_addr_remove(uint8_t port_id, struct ether_addr *mc_addr)
2458 {
2459         struct rte_port *port;
2460         uint32_t i;
2461
2462         if (port_id_is_invalid(port_id, ENABLED_WARN))
2463                 return;
2464
2465         port = &ports[port_id];
2466
2467         /*
2468          * Search the pool of multicast MAC addresses for the removed address.
2469          */
2470         for (i = 0; i < port->mc_addr_nb; i++) {
2471                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
2472                         break;
2473         }
2474         if (i == port->mc_addr_nb) {
2475                 printf("multicast address not filtered by port %d\n", port_id);
2476                 return;
2477         }
2478
2479         mcast_addr_pool_remove(port, i);
2480         eth_port_multicast_addr_list_set(port_id);
2481 }
2482
2483 void
2484 port_dcb_info_display(uint8_t port_id)
2485 {
2486         struct rte_eth_dcb_info dcb_info;
2487         uint16_t i;
2488         int ret;
2489         static const char *border = "================";
2490
2491         if (port_id_is_invalid(port_id, ENABLED_WARN))
2492                 return;
2493
2494         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
2495         if (ret) {
2496                 printf("\n Failed to get dcb infos on port %-2d\n",
2497                         port_id);
2498                 return;
2499         }
2500         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
2501         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
2502         printf("\n  TC :        ");
2503         for (i = 0; i < dcb_info.nb_tcs; i++)
2504                 printf("\t%4d", i);
2505         printf("\n  Priority :  ");
2506         for (i = 0; i < dcb_info.nb_tcs; i++)
2507                 printf("\t%4d", dcb_info.prio_tc[i]);
2508         printf("\n  BW percent :");
2509         for (i = 0; i < dcb_info.nb_tcs; i++)
2510                 printf("\t%4d%%", dcb_info.tc_bws[i]);
2511         printf("\n  RXQ base :  ");
2512         for (i = 0; i < dcb_info.nb_tcs; i++)
2513                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
2514         printf("\n  RXQ number :");
2515         for (i = 0; i < dcb_info.nb_tcs; i++)
2516                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
2517         printf("\n  TXQ base :  ");
2518         for (i = 0; i < dcb_info.nb_tcs; i++)
2519                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
2520         printf("\n  TXQ number :");
2521         for (i = 0; i < dcb_info.nb_tcs; i++)
2522                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
2523         printf("\n");
2524 }