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