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