6f1ead0c4e92c4b59a8dcdb3bb64cfb737fdc6a0
[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         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
1203                 cur_fwd_config.nb_fwd_streams =
1204                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
1205         else
1206                 cur_fwd_config.nb_fwd_lcores =
1207                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1208
1209         /* reinitialize forwarding streams */
1210         init_fwd_streams();
1211
1212         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1213         rxp = 0; rxq = 0;
1214         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1215                 struct fwd_stream *fs;
1216
1217                 fs = fwd_streams[lc_id];
1218
1219                 if ((rxp & 0x1) == 0)
1220                         txp = (portid_t) (rxp + 1);
1221                 else
1222                         txp = (portid_t) (rxp - 1);
1223                 /*
1224                  * if we are in loopback, simply send stuff out through the
1225                  * ingress port
1226                  */
1227                 if (port_topology == PORT_TOPOLOGY_LOOP)
1228                         txp = rxp;
1229
1230                 fs->rx_port = fwd_ports_ids[rxp];
1231                 fs->rx_queue = rxq;
1232                 fs->tx_port = fwd_ports_ids[txp];
1233                 fs->tx_queue = rxq;
1234                 fs->peer_addr = fs->tx_port;
1235                 fs->retry_enabled = retry_enabled;
1236                 rxq = (queueid_t) (rxq + 1);
1237                 if (rxq < nb_q)
1238                         continue;
1239                 /*
1240                  * rxq == nb_q
1241                  * Restart from RX queue 0 on next RX port
1242                  */
1243                 rxq = 0;
1244                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1245                         rxp = (portid_t)
1246                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1247                 else
1248                         rxp = (portid_t) (rxp + 1);
1249         }
1250 }
1251
1252 /**
1253  * For the DCB forwarding test, each core is assigned on each traffic class.
1254  *
1255  * Each core is assigned a multi-stream, each stream being composed of
1256  * a RX queue to poll on a RX port for input messages, associated with
1257  * a TX queue of a TX port where to send forwarded packets. All RX and
1258  * TX queues are mapping to the same traffic class.
1259  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
1260  * the same core
1261  */
1262 static void
1263 dcb_fwd_config_setup(void)
1264 {
1265         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
1266         portid_t txp, rxp = 0;
1267         queueid_t txq, rxq = 0;
1268         lcoreid_t  lc_id;
1269         uint16_t nb_rx_queue, nb_tx_queue;
1270         uint16_t i, j, k, sm_id = 0;
1271         uint8_t tc = 0;
1272
1273         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1274         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1275         cur_fwd_config.nb_fwd_streams =
1276                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1277
1278         /* reinitialize forwarding streams */
1279         init_fwd_streams();
1280         sm_id = 0;
1281         txp = 1;
1282         /* get the dcb info on the first RX and TX ports */
1283         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
1284         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
1285
1286         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1287                 fwd_lcores[lc_id]->stream_nb = 0;
1288                 fwd_lcores[lc_id]->stream_idx = sm_id;
1289                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
1290                         /* if the nb_queue is zero, means this tc is
1291                          * not enabled on the POOL
1292                          */
1293                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
1294                                 break;
1295                         k = fwd_lcores[lc_id]->stream_nb +
1296                                 fwd_lcores[lc_id]->stream_idx;
1297                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
1298                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
1299                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
1300                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
1301                         for (j = 0; j < nb_rx_queue; j++) {
1302                                 struct fwd_stream *fs;
1303
1304                                 fs = fwd_streams[k + j];
1305                                 fs->rx_port = fwd_ports_ids[rxp];
1306                                 fs->rx_queue = rxq + j;
1307                                 fs->tx_port = fwd_ports_ids[txp];
1308                                 fs->tx_queue = txq + j % nb_tx_queue;
1309                                 fs->peer_addr = fs->tx_port;
1310                                 fs->retry_enabled = retry_enabled;
1311                         }
1312                         fwd_lcores[lc_id]->stream_nb +=
1313                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
1314                 }
1315                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
1316
1317                 tc++;
1318                 if (tc < rxp_dcb_info.nb_tcs)
1319                         continue;
1320                 /* Restart from TC 0 on next RX port */
1321                 tc = 0;
1322                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1323                         rxp = (portid_t)
1324                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1325                 else
1326                         rxp++;
1327                 if (rxp >= nb_fwd_ports)
1328                         return;
1329                 /* get the dcb information on next RX and TX ports */
1330                 if ((rxp & 0x1) == 0)
1331                         txp = (portid_t) (rxp + 1);
1332                 else
1333                         txp = (portid_t) (rxp - 1);
1334                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
1335                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
1336         }
1337 }
1338
1339 static void
1340 icmp_echo_config_setup(void)
1341 {
1342         portid_t  rxp;
1343         queueid_t rxq;
1344         lcoreid_t lc_id;
1345         uint16_t  sm_id;
1346
1347         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
1348                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
1349                         (nb_txq * nb_fwd_ports);
1350         else
1351                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1352         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1353         cur_fwd_config.nb_fwd_streams =
1354                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1355         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1356                 cur_fwd_config.nb_fwd_lcores =
1357                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1358         if (verbose_level > 0) {
1359                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
1360                        __FUNCTION__,
1361                        cur_fwd_config.nb_fwd_lcores,
1362                        cur_fwd_config.nb_fwd_ports,
1363                        cur_fwd_config.nb_fwd_streams);
1364         }
1365
1366         /* reinitialize forwarding streams */
1367         init_fwd_streams();
1368         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1369         rxp = 0; rxq = 0;
1370         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1371                 if (verbose_level > 0)
1372                         printf("  core=%d: \n", lc_id);
1373                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1374                         struct fwd_stream *fs;
1375                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1376                         fs->rx_port = fwd_ports_ids[rxp];
1377                         fs->rx_queue = rxq;
1378                         fs->tx_port = fs->rx_port;
1379                         fs->tx_queue = rxq;
1380                         fs->peer_addr = fs->tx_port;
1381                         fs->retry_enabled = retry_enabled;
1382                         if (verbose_level > 0)
1383                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
1384                                        sm_id, fs->rx_port, fs->rx_queue,
1385                                        fs->tx_queue);
1386                         rxq = (queueid_t) (rxq + 1);
1387                         if (rxq == nb_rxq) {
1388                                 rxq = 0;
1389                                 rxp = (portid_t) (rxp + 1);
1390                         }
1391                 }
1392         }
1393 }
1394
1395 void
1396 fwd_config_setup(void)
1397 {
1398         cur_fwd_config.fwd_eng = cur_fwd_eng;
1399         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
1400                 icmp_echo_config_setup();
1401                 return;
1402         }
1403         if ((nb_rxq > 1) && (nb_txq > 1)){
1404                 if (dcb_config)
1405                         dcb_fwd_config_setup();
1406                 else
1407                         rss_fwd_config_setup();
1408         }
1409         else
1410                 simple_fwd_config_setup();
1411 }
1412
1413 void
1414 pkt_fwd_config_display(struct fwd_config *cfg)
1415 {
1416         struct fwd_stream *fs;
1417         lcoreid_t  lc_id;
1418         streamid_t sm_id;
1419
1420         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
1421                 "NUMA support %s, MP over anonymous pages %s\n",
1422                 cfg->fwd_eng->fwd_mode_name,
1423                 retry_enabled == 0 ? "" : " with retry",
1424                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
1425                 numa_support == 1 ? "enabled" : "disabled",
1426                 mp_anon != 0 ? "enabled" : "disabled");
1427
1428         if (retry_enabled)
1429                 printf("TX retry num: %u, delay between TX retries: %uus\n",
1430                         burst_tx_retry_num, burst_tx_delay_time);
1431         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
1432                 printf("Logical Core %u (socket %u) forwards packets on "
1433                        "%d streams:",
1434                        fwd_lcores_cpuids[lc_id],
1435                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
1436                        fwd_lcores[lc_id]->stream_nb);
1437                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1438                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1439                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
1440                                "P=%d/Q=%d (socket %u) ",
1441                                fs->rx_port, fs->rx_queue,
1442                                ports[fs->rx_port].socket_id,
1443                                fs->tx_port, fs->tx_queue,
1444                                ports[fs->tx_port].socket_id);
1445                         print_ethaddr("peer=",
1446                                       &peer_eth_addrs[fs->peer_addr]);
1447                 }
1448                 printf("\n");
1449         }
1450         printf("\n");
1451 }
1452
1453 int
1454 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
1455 {
1456         unsigned int i;
1457         unsigned int lcore_cpuid;
1458         int record_now;
1459
1460         record_now = 0;
1461  again:
1462         for (i = 0; i < nb_lc; i++) {
1463                 lcore_cpuid = lcorelist[i];
1464                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
1465                         printf("lcore %u not enabled\n", lcore_cpuid);
1466                         return -1;
1467                 }
1468                 if (lcore_cpuid == rte_get_master_lcore()) {
1469                         printf("lcore %u cannot be masked on for running "
1470                                "packet forwarding, which is the master lcore "
1471                                "and reserved for command line parsing only\n",
1472                                lcore_cpuid);
1473                         return -1;
1474                 }
1475                 if (record_now)
1476                         fwd_lcores_cpuids[i] = lcore_cpuid;
1477         }
1478         if (record_now == 0) {
1479                 record_now = 1;
1480                 goto again;
1481         }
1482         nb_cfg_lcores = (lcoreid_t) nb_lc;
1483         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
1484                 printf("previous number of forwarding cores %u - changed to "
1485                        "number of configured cores %u\n",
1486                        (unsigned int) nb_fwd_lcores, nb_lc);
1487                 nb_fwd_lcores = (lcoreid_t) nb_lc;
1488         }
1489
1490         return 0;
1491 }
1492
1493 int
1494 set_fwd_lcores_mask(uint64_t lcoremask)
1495 {
1496         unsigned int lcorelist[64];
1497         unsigned int nb_lc;
1498         unsigned int i;
1499
1500         if (lcoremask == 0) {
1501                 printf("Invalid NULL mask of cores\n");
1502                 return -1;
1503         }
1504         nb_lc = 0;
1505         for (i = 0; i < 64; i++) {
1506                 if (! ((uint64_t)(1ULL << i) & lcoremask))
1507                         continue;
1508                 lcorelist[nb_lc++] = i;
1509         }
1510         return set_fwd_lcores_list(lcorelist, nb_lc);
1511 }
1512
1513 void
1514 set_fwd_lcores_number(uint16_t nb_lc)
1515 {
1516         if (nb_lc > nb_cfg_lcores) {
1517                 printf("nb fwd cores %u > %u (max. number of configured "
1518                        "lcores) - ignored\n",
1519                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
1520                 return;
1521         }
1522         nb_fwd_lcores = (lcoreid_t) nb_lc;
1523         printf("Number of forwarding cores set to %u\n",
1524                (unsigned int) nb_fwd_lcores);
1525 }
1526
1527 void
1528 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
1529 {
1530         unsigned int i;
1531         portid_t port_id;
1532         int record_now;
1533
1534         record_now = 0;
1535  again:
1536         for (i = 0; i < nb_pt; i++) {
1537                 port_id = (portid_t) portlist[i];
1538                 if (port_id_is_invalid(port_id, ENABLED_WARN))
1539                         return;
1540                 if (record_now)
1541                         fwd_ports_ids[i] = port_id;
1542         }
1543         if (record_now == 0) {
1544                 record_now = 1;
1545                 goto again;
1546         }
1547         nb_cfg_ports = (portid_t) nb_pt;
1548         if (nb_fwd_ports != (portid_t) nb_pt) {
1549                 printf("previous number of forwarding ports %u - changed to "
1550                        "number of configured ports %u\n",
1551                        (unsigned int) nb_fwd_ports, nb_pt);
1552                 nb_fwd_ports = (portid_t) nb_pt;
1553         }
1554 }
1555
1556 void
1557 set_fwd_ports_mask(uint64_t portmask)
1558 {
1559         unsigned int portlist[64];
1560         unsigned int nb_pt;
1561         unsigned int i;
1562
1563         if (portmask == 0) {
1564                 printf("Invalid NULL mask of ports\n");
1565                 return;
1566         }
1567         nb_pt = 0;
1568         for (i = 0; i < (unsigned)RTE_MIN(64, RTE_MAX_ETHPORTS); i++) {
1569                 if (! ((uint64_t)(1ULL << i) & portmask))
1570                         continue;
1571                 portlist[nb_pt++] = i;
1572         }
1573         set_fwd_ports_list(portlist, nb_pt);
1574 }
1575
1576 void
1577 set_fwd_ports_number(uint16_t nb_pt)
1578 {
1579         if (nb_pt > nb_cfg_ports) {
1580                 printf("nb fwd ports %u > %u (number of configured "
1581                        "ports) - ignored\n",
1582                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
1583                 return;
1584         }
1585         nb_fwd_ports = (portid_t) nb_pt;
1586         printf("Number of forwarding ports set to %u\n",
1587                (unsigned int) nb_fwd_ports);
1588 }
1589
1590 int
1591 port_is_forwarding(portid_t port_id)
1592 {
1593         unsigned int i;
1594
1595         if (port_id_is_invalid(port_id, ENABLED_WARN))
1596                 return -1;
1597
1598         for (i = 0; i < nb_fwd_ports; i++) {
1599                 if (fwd_ports_ids[i] == port_id)
1600                         return 1;
1601         }
1602
1603         return 0;
1604 }
1605
1606 void
1607 set_nb_pkt_per_burst(uint16_t nb)
1608 {
1609         if (nb > MAX_PKT_BURST) {
1610                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
1611                        " ignored\n",
1612                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
1613                 return;
1614         }
1615         nb_pkt_per_burst = nb;
1616         printf("Number of packets per burst set to %u\n",
1617                (unsigned int) nb_pkt_per_burst);
1618 }
1619
1620 static const char *
1621 tx_split_get_name(enum tx_pkt_split split)
1622 {
1623         uint32_t i;
1624
1625         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
1626                 if (tx_split_name[i].split == split)
1627                         return tx_split_name[i].name;
1628         }
1629         return NULL;
1630 }
1631
1632 void
1633 set_tx_pkt_split(const char *name)
1634 {
1635         uint32_t i;
1636
1637         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
1638                 if (strcmp(tx_split_name[i].name, name) == 0) {
1639                         tx_pkt_split = tx_split_name[i].split;
1640                         return;
1641                 }
1642         }
1643         printf("unknown value: \"%s\"\n", name);
1644 }
1645
1646 void
1647 show_tx_pkt_segments(void)
1648 {
1649         uint32_t i, n;
1650         const char *split;
1651
1652         n = tx_pkt_nb_segs;
1653         split = tx_split_get_name(tx_pkt_split);
1654
1655         printf("Number of segments: %u\n", n);
1656         printf("Segment sizes: ");
1657         for (i = 0; i != n - 1; i++)
1658                 printf("%hu,", tx_pkt_seg_lengths[i]);
1659         printf("%hu\n", tx_pkt_seg_lengths[i]);
1660         printf("Split packet: %s\n", split);
1661 }
1662
1663 void
1664 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
1665 {
1666         uint16_t tx_pkt_len;
1667         unsigned i;
1668
1669         if (nb_segs >= (unsigned) nb_txd) {
1670                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
1671                        nb_segs, (unsigned int) nb_txd);
1672                 return;
1673         }
1674
1675         /*
1676          * Check that each segment length is greater or equal than
1677          * the mbuf data sise.
1678          * Check also that the total packet length is greater or equal than the
1679          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
1680          */
1681         tx_pkt_len = 0;
1682         for (i = 0; i < nb_segs; i++) {
1683                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
1684                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
1685                                i, seg_lengths[i], (unsigned) mbuf_data_size);
1686                         return;
1687                 }
1688                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
1689         }
1690         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
1691                 printf("total packet length=%u < %d - give up\n",
1692                                 (unsigned) tx_pkt_len,
1693                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
1694                 return;
1695         }
1696
1697         for (i = 0; i < nb_segs; i++)
1698                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
1699
1700         tx_pkt_length  = tx_pkt_len;
1701         tx_pkt_nb_segs = (uint8_t) nb_segs;
1702 }
1703
1704 char*
1705 list_pkt_forwarding_modes(void)
1706 {
1707         static char fwd_modes[128] = "";
1708         const char *separator = "|";
1709         struct fwd_engine *fwd_eng;
1710         unsigned i = 0;
1711
1712         if (strlen (fwd_modes) == 0) {
1713                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1714                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
1715                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
1716                         strncat(fwd_modes, separator,
1717                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
1718                 }
1719                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1720         }
1721
1722         return fwd_modes;
1723 }
1724
1725 char*
1726 list_pkt_forwarding_retry_modes(void)
1727 {
1728         static char fwd_modes[128] = "";
1729         const char *separator = "|";
1730         struct fwd_engine *fwd_eng;
1731         unsigned i = 0;
1732
1733         if (strlen(fwd_modes) == 0) {
1734                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1735                         if (fwd_eng == &rx_only_engine)
1736                                 continue;
1737                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
1738                                         sizeof(fwd_modes) -
1739                                         strlen(fwd_modes) - 1);
1740                         strncat(fwd_modes, separator,
1741                                         sizeof(fwd_modes) -
1742                                         strlen(fwd_modes) - 1);
1743                 }
1744                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1745         }
1746
1747         return fwd_modes;
1748 }
1749
1750 void
1751 set_pkt_forwarding_mode(const char *fwd_mode_name)
1752 {
1753         struct fwd_engine *fwd_eng;
1754         unsigned i;
1755
1756         i = 0;
1757         while ((fwd_eng = fwd_engines[i]) != NULL) {
1758                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
1759                         printf("Set %s packet forwarding mode%s\n",
1760                                fwd_mode_name,
1761                                retry_enabled == 0 ? "" : " with retry");
1762                         cur_fwd_eng = fwd_eng;
1763                         return;
1764                 }
1765                 i++;
1766         }
1767         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
1768 }
1769
1770 void
1771 set_verbose_level(uint16_t vb_level)
1772 {
1773         printf("Change verbose level from %u to %u\n",
1774                (unsigned int) verbose_level, (unsigned int) vb_level);
1775         verbose_level = vb_level;
1776 }
1777
1778 void
1779 vlan_extend_set(portid_t port_id, int on)
1780 {
1781         int diag;
1782         int vlan_offload;
1783
1784         if (port_id_is_invalid(port_id, ENABLED_WARN))
1785                 return;
1786
1787         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1788
1789         if (on)
1790                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
1791         else
1792                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
1793
1794         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1795         if (diag < 0)
1796                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
1797                "diag=%d\n", port_id, on, diag);
1798 }
1799
1800 void
1801 rx_vlan_strip_set(portid_t port_id, int on)
1802 {
1803         int diag;
1804         int vlan_offload;
1805
1806         if (port_id_is_invalid(port_id, ENABLED_WARN))
1807                 return;
1808
1809         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1810
1811         if (on)
1812                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
1813         else
1814                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
1815
1816         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1817         if (diag < 0)
1818                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
1819                "diag=%d\n", port_id, on, diag);
1820 }
1821
1822 void
1823 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
1824 {
1825         int diag;
1826
1827         if (port_id_is_invalid(port_id, ENABLED_WARN))
1828                 return;
1829
1830         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
1831         if (diag < 0)
1832                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
1833                "diag=%d\n", port_id, queue_id, on, diag);
1834 }
1835
1836 void
1837 rx_vlan_filter_set(portid_t port_id, int on)
1838 {
1839         int diag;
1840         int vlan_offload;
1841
1842         if (port_id_is_invalid(port_id, ENABLED_WARN))
1843                 return;
1844
1845         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1846
1847         if (on)
1848                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1849         else
1850                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
1851
1852         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1853         if (diag < 0)
1854                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
1855                "diag=%d\n", port_id, on, diag);
1856 }
1857
1858 int
1859 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
1860 {
1861         int diag;
1862
1863         if (port_id_is_invalid(port_id, ENABLED_WARN))
1864                 return 1;
1865         if (vlan_id_is_invalid(vlan_id))
1866                 return 1;
1867         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
1868         if (diag == 0)
1869                 return 0;
1870         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
1871                "diag=%d\n",
1872                port_id, vlan_id, on, diag);
1873         return -1;
1874 }
1875
1876 void
1877 rx_vlan_all_filter_set(portid_t port_id, int on)
1878 {
1879         uint16_t vlan_id;
1880
1881         if (port_id_is_invalid(port_id, ENABLED_WARN))
1882                 return;
1883         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
1884                 if (rx_vft_set(port_id, vlan_id, on))
1885                         break;
1886         }
1887 }
1888
1889 void
1890 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
1891 {
1892         int diag;
1893
1894         if (port_id_is_invalid(port_id, ENABLED_WARN))
1895                 return;
1896
1897         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
1898         if (diag == 0)
1899                 return;
1900
1901         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
1902                "diag=%d\n",
1903                port_id, vlan_type, tp_id, diag);
1904 }
1905
1906 void
1907 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
1908 {
1909         int vlan_offload;
1910         if (port_id_is_invalid(port_id, ENABLED_WARN))
1911                 return;
1912         if (vlan_id_is_invalid(vlan_id))
1913                 return;
1914
1915         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1916         if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD) {
1917                 printf("Error, as QinQ has been enabled.\n");
1918                 return;
1919         }
1920
1921         tx_vlan_reset(port_id);
1922         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1923         ports[port_id].tx_vlan_id = vlan_id;
1924 }
1925
1926 void
1927 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
1928 {
1929         int vlan_offload;
1930         if (port_id_is_invalid(port_id, ENABLED_WARN))
1931                 return;
1932         if (vlan_id_is_invalid(vlan_id))
1933                 return;
1934         if (vlan_id_is_invalid(vlan_id_outer))
1935                 return;
1936
1937         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1938         if (!(vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)) {
1939                 printf("Error, as QinQ hasn't been enabled.\n");
1940                 return;
1941         }
1942
1943         tx_vlan_reset(port_id);
1944         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_QINQ;
1945         ports[port_id].tx_vlan_id = vlan_id;
1946         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
1947 }
1948
1949 void
1950 tx_vlan_reset(portid_t port_id)
1951 {
1952         if (port_id_is_invalid(port_id, ENABLED_WARN))
1953                 return;
1954         ports[port_id].tx_ol_flags &= ~(TESTPMD_TX_OFFLOAD_INSERT_VLAN |
1955                                 TESTPMD_TX_OFFLOAD_INSERT_QINQ);
1956         ports[port_id].tx_vlan_id = 0;
1957         ports[port_id].tx_vlan_id_outer = 0;
1958 }
1959
1960 void
1961 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
1962 {
1963         if (port_id_is_invalid(port_id, ENABLED_WARN))
1964                 return;
1965
1966         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
1967 }
1968
1969 void
1970 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
1971 {
1972         uint16_t i;
1973         uint8_t existing_mapping_found = 0;
1974
1975         if (port_id_is_invalid(port_id, ENABLED_WARN))
1976                 return;
1977
1978         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
1979                 return;
1980
1981         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1982                 printf("map_value not in required range 0..%d\n",
1983                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1984                 return;
1985         }
1986
1987         if (!is_rx) { /*then tx*/
1988                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1989                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1990                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
1991                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
1992                                 existing_mapping_found = 1;
1993                                 break;
1994                         }
1995                 }
1996                 if (!existing_mapping_found) { /* A new additional mapping... */
1997                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
1998                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
1999                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
2000                         nb_tx_queue_stats_mappings++;
2001                 }
2002         }
2003         else { /*rx*/
2004                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
2005                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
2006                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
2007                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
2008                                 existing_mapping_found = 1;
2009                                 break;
2010                         }
2011                 }
2012                 if (!existing_mapping_found) { /* A new additional mapping... */
2013                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
2014                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
2015                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
2016                         nb_rx_queue_stats_mappings++;
2017                 }
2018         }
2019 }
2020
2021 static inline void
2022 print_fdir_mask(struct rte_eth_fdir_masks *mask)
2023 {
2024         printf("\n    vlan_tci: 0x%04x, ", mask->vlan_tci_mask);
2025
2026         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2027                 printf("mac_addr: 0x%02x", mask->mac_addr_byte_mask);
2028         else if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2029                 printf("mac_addr: 0x%02x, tunnel_type: 0x%01x, tunnel_id: 0x%08x",
2030                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
2031                         mask->tunnel_id_mask);
2032         else {
2033                 printf("src_ipv4: 0x%08x, dst_ipv4: 0x%08x,"
2034                         " src_port: 0x%04x, dst_port: 0x%04x",
2035                         mask->ipv4_mask.src_ip, mask->ipv4_mask.dst_ip,
2036                         mask->src_port_mask, mask->dst_port_mask);
2037
2038                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x,"
2039                         " dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2040                         mask->ipv6_mask.src_ip[0], mask->ipv6_mask.src_ip[1],
2041                         mask->ipv6_mask.src_ip[2], mask->ipv6_mask.src_ip[3],
2042                         mask->ipv6_mask.dst_ip[0], mask->ipv6_mask.dst_ip[1],
2043                         mask->ipv6_mask.dst_ip[2], mask->ipv6_mask.dst_ip[3]);
2044         }
2045
2046         printf("\n");
2047 }
2048
2049 static inline void
2050 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2051 {
2052         struct rte_eth_flex_payload_cfg *cfg;
2053         uint32_t i, j;
2054
2055         for (i = 0; i < flex_conf->nb_payloads; i++) {
2056                 cfg = &flex_conf->flex_set[i];
2057                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
2058                         printf("\n    RAW:  ");
2059                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
2060                         printf("\n    L2_PAYLOAD:  ");
2061                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
2062                         printf("\n    L3_PAYLOAD:  ");
2063                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
2064                         printf("\n    L4_PAYLOAD:  ");
2065                 else
2066                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
2067                 for (j = 0; j < num; j++)
2068                         printf("  %-5u", cfg->src_offset[j]);
2069         }
2070         printf("\n");
2071 }
2072
2073 static char *
2074 flowtype_to_str(uint16_t flow_type)
2075 {
2076         struct flow_type_info {
2077                 char str[32];
2078                 uint16_t ftype;
2079         };
2080
2081         uint8_t i;
2082         static struct flow_type_info flowtype_str_table[] = {
2083                 {"raw", RTE_ETH_FLOW_RAW},
2084                 {"ipv4", RTE_ETH_FLOW_IPV4},
2085                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
2086                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
2087                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
2088                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
2089                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
2090                 {"ipv6", RTE_ETH_FLOW_IPV6},
2091                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
2092                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
2093                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
2094                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
2095                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
2096                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
2097         };
2098
2099         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
2100                 if (flowtype_str_table[i].ftype == flow_type)
2101                         return flowtype_str_table[i].str;
2102         }
2103
2104         return NULL;
2105 }
2106
2107 static inline void
2108 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2109 {
2110         struct rte_eth_fdir_flex_mask *mask;
2111         uint32_t i, j;
2112         char *p;
2113
2114         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
2115                 mask = &flex_conf->flex_mask[i];
2116                 p = flowtype_to_str(mask->flow_type);
2117                 printf("\n    %s:\t", p ? p : "unknown");
2118                 for (j = 0; j < num; j++)
2119                         printf(" %02x", mask->mask[j]);
2120         }
2121         printf("\n");
2122 }
2123
2124 static inline void
2125 print_fdir_flow_type(uint32_t flow_types_mask)
2126 {
2127         int i;
2128         char *p;
2129
2130         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
2131                 if (!(flow_types_mask & (1 << i)))
2132                         continue;
2133                 p = flowtype_to_str(i);
2134                 if (p)
2135                         printf(" %s", p);
2136                 else
2137                         printf(" unknown");
2138         }
2139         printf("\n");
2140 }
2141
2142 void
2143 fdir_get_infos(portid_t port_id)
2144 {
2145         struct rte_eth_fdir_stats fdir_stat;
2146         struct rte_eth_fdir_info fdir_info;
2147         int ret;
2148
2149         static const char *fdir_stats_border = "########################";
2150
2151         if (port_id_is_invalid(port_id, ENABLED_WARN))
2152                 return;
2153         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
2154         if (ret < 0) {
2155                 printf("\n FDIR is not supported on port %-2d\n",
2156                         port_id);
2157                 return;
2158         }
2159
2160         memset(&fdir_info, 0, sizeof(fdir_info));
2161         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2162                                RTE_ETH_FILTER_INFO, &fdir_info);
2163         memset(&fdir_stat, 0, sizeof(fdir_stat));
2164         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2165                                RTE_ETH_FILTER_STATS, &fdir_stat);
2166         printf("\n  %s FDIR infos for port %-2d     %s\n",
2167                fdir_stats_border, port_id, fdir_stats_border);
2168         printf("  MODE: ");
2169         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
2170                 printf("  PERFECT\n");
2171         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2172                 printf("  PERFECT-MAC-VLAN\n");
2173         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2174                 printf("  PERFECT-TUNNEL\n");
2175         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
2176                 printf("  SIGNATURE\n");
2177         else
2178                 printf("  DISABLE\n");
2179         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
2180                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
2181                 printf("  SUPPORTED FLOW TYPE: ");
2182                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
2183         }
2184         printf("  FLEX PAYLOAD INFO:\n");
2185         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
2186                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
2187                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
2188                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
2189                 fdir_info.flex_payload_unit,
2190                 fdir_info.max_flex_payload_segment_num,
2191                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
2192         printf("  MASK: ");
2193         print_fdir_mask(&fdir_info.mask);
2194         if (fdir_info.flex_conf.nb_payloads > 0) {
2195                 printf("  FLEX PAYLOAD SRC OFFSET:");
2196                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2197         }
2198         if (fdir_info.flex_conf.nb_flexmasks > 0) {
2199                 printf("  FLEX MASK CFG:");
2200                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2201         }
2202         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
2203                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
2204         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
2205                fdir_info.guarant_spc, fdir_info.best_spc);
2206         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
2207                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
2208                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
2209                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
2210                fdir_stat.collision, fdir_stat.free,
2211                fdir_stat.maxhash, fdir_stat.maxlen,
2212                fdir_stat.add, fdir_stat.remove,
2213                fdir_stat.f_add, fdir_stat.f_remove);
2214         printf("  %s############################%s\n",
2215                fdir_stats_border, fdir_stats_border);
2216 }
2217
2218 void
2219 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
2220 {
2221         struct rte_port *port;
2222         struct rte_eth_fdir_flex_conf *flex_conf;
2223         int i, idx = 0;
2224
2225         port = &ports[port_id];
2226         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2227         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
2228                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
2229                         idx = i;
2230                         break;
2231                 }
2232         }
2233         if (i >= RTE_ETH_FLOW_MAX) {
2234                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
2235                         idx = flex_conf->nb_flexmasks;
2236                         flex_conf->nb_flexmasks++;
2237                 } else {
2238                         printf("The flex mask table is full. Can not set flex"
2239                                 " mask for flow_type(%u).", cfg->flow_type);
2240                         return;
2241                 }
2242         }
2243         (void)rte_memcpy(&flex_conf->flex_mask[idx],
2244                          cfg,
2245                          sizeof(struct rte_eth_fdir_flex_mask));
2246 }
2247
2248 void
2249 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
2250 {
2251         struct rte_port *port;
2252         struct rte_eth_fdir_flex_conf *flex_conf;
2253         int i, idx = 0;
2254
2255         port = &ports[port_id];
2256         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2257         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
2258                 if (cfg->type == flex_conf->flex_set[i].type) {
2259                         idx = i;
2260                         break;
2261                 }
2262         }
2263         if (i >= RTE_ETH_PAYLOAD_MAX) {
2264                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
2265                         idx = flex_conf->nb_payloads;
2266                         flex_conf->nb_payloads++;
2267                 } else {
2268                         printf("The flex payload table is full. Can not set"
2269                                 " flex payload for type(%u).", cfg->type);
2270                         return;
2271                 }
2272         }
2273         (void)rte_memcpy(&flex_conf->flex_set[idx],
2274                          cfg,
2275                          sizeof(struct rte_eth_flex_payload_cfg));
2276
2277 }
2278
2279 void
2280 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
2281 {
2282         int diag;
2283
2284         if (port_id_is_invalid(port_id, ENABLED_WARN))
2285                 return;
2286         if (is_rx)
2287                 diag = rte_eth_dev_set_vf_rx(port_id,vf,on);
2288         else
2289                 diag = rte_eth_dev_set_vf_tx(port_id,vf,on);
2290         if (diag == 0)
2291                 return;
2292         if(is_rx)
2293                 printf("rte_eth_dev_set_vf_rx for port_id=%d failed "
2294                         "diag=%d\n", port_id, diag);
2295         else
2296                 printf("rte_eth_dev_set_vf_tx for port_id=%d failed "
2297                         "diag=%d\n", port_id, diag);
2298
2299 }
2300
2301 void
2302 set_vf_rx_vlan(portid_t port_id, uint16_t vlan_id, uint64_t vf_mask, uint8_t on)
2303 {
2304         int diag;
2305
2306         if (port_id_is_invalid(port_id, ENABLED_WARN))
2307                 return;
2308         if (vlan_id_is_invalid(vlan_id))
2309                 return;
2310         diag = rte_eth_dev_set_vf_vlan_filter(port_id, vlan_id, vf_mask, on);
2311         if (diag == 0)
2312                 return;
2313         printf("rte_eth_dev_set_vf_vlan_filter for port_id=%d failed "
2314                "diag=%d\n", port_id, diag);
2315 }
2316
2317 int
2318 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
2319 {
2320         int diag;
2321         struct rte_eth_link link;
2322
2323         if (port_id_is_invalid(port_id, ENABLED_WARN))
2324                 return 1;
2325         rte_eth_link_get_nowait(port_id, &link);
2326         if (rate > link.link_speed) {
2327                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2328                         rate, link.link_speed);
2329                 return 1;
2330         }
2331         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
2332         if (diag == 0)
2333                 return diag;
2334         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
2335                 port_id, diag);
2336         return diag;
2337 }
2338
2339 int
2340 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
2341 {
2342         int diag;
2343         struct rte_eth_link link;
2344
2345         if (q_msk == 0)
2346                 return 0;
2347
2348         if (port_id_is_invalid(port_id, ENABLED_WARN))
2349                 return 1;
2350         rte_eth_link_get_nowait(port_id, &link);
2351         if (rate > link.link_speed) {
2352                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2353                         rate, link.link_speed);
2354                 return 1;
2355         }
2356         diag = rte_eth_set_vf_rate_limit(port_id, vf, rate, q_msk);
2357         if (diag == 0)
2358                 return diag;
2359         printf("rte_eth_set_vf_rate_limit for port_id=%d failed diag=%d\n",
2360                 port_id, diag);
2361         return diag;
2362 }
2363
2364 /*
2365  * Functions to manage the set of filtered Multicast MAC addresses.
2366  *
2367  * A pool of filtered multicast MAC addresses is associated with each port.
2368  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
2369  * The address of the pool and the number of valid multicast MAC addresses
2370  * recorded in the pool are stored in the fields "mc_addr_pool" and
2371  * "mc_addr_nb" of the "rte_port" data structure.
2372  *
2373  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
2374  * to be supplied a contiguous array of multicast MAC addresses.
2375  * To comply with this constraint, the set of multicast addresses recorded
2376  * into the pool are systematically compacted at the beginning of the pool.
2377  * Hence, when a multicast address is removed from the pool, all following
2378  * addresses, if any, are copied back to keep the set contiguous.
2379  */
2380 #define MCAST_POOL_INC 32
2381
2382 static int
2383 mcast_addr_pool_extend(struct rte_port *port)
2384 {
2385         struct ether_addr *mc_pool;
2386         size_t mc_pool_size;
2387
2388         /*
2389          * If a free entry is available at the end of the pool, just
2390          * increment the number of recorded multicast addresses.
2391          */
2392         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
2393                 port->mc_addr_nb++;
2394                 return 0;
2395         }
2396
2397         /*
2398          * [re]allocate a pool with MCAST_POOL_INC more entries.
2399          * The previous test guarantees that port->mc_addr_nb is a multiple
2400          * of MCAST_POOL_INC.
2401          */
2402         mc_pool_size = sizeof(struct ether_addr) * (port->mc_addr_nb +
2403                                                     MCAST_POOL_INC);
2404         mc_pool = (struct ether_addr *) realloc(port->mc_addr_pool,
2405                                                 mc_pool_size);
2406         if (mc_pool == NULL) {
2407                 printf("allocation of pool of %u multicast addresses failed\n",
2408                        port->mc_addr_nb + MCAST_POOL_INC);
2409                 return -ENOMEM;
2410         }
2411
2412         port->mc_addr_pool = mc_pool;
2413         port->mc_addr_nb++;
2414         return 0;
2415
2416 }
2417
2418 static void
2419 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
2420 {
2421         port->mc_addr_nb--;
2422         if (addr_idx == port->mc_addr_nb) {
2423                 /* No need to recompact the set of multicast addressses. */
2424                 if (port->mc_addr_nb == 0) {
2425                         /* free the pool of multicast addresses. */
2426                         free(port->mc_addr_pool);
2427                         port->mc_addr_pool = NULL;
2428                 }
2429                 return;
2430         }
2431         memmove(&port->mc_addr_pool[addr_idx],
2432                 &port->mc_addr_pool[addr_idx + 1],
2433                 sizeof(struct ether_addr) * (port->mc_addr_nb - addr_idx));
2434 }
2435
2436 static void
2437 eth_port_multicast_addr_list_set(uint8_t port_id)
2438 {
2439         struct rte_port *port;
2440         int diag;
2441
2442         port = &ports[port_id];
2443         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
2444                                             port->mc_addr_nb);
2445         if (diag == 0)
2446                 return;
2447         printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
2448                port->mc_addr_nb, port_id, -diag);
2449 }
2450
2451 void
2452 mcast_addr_add(uint8_t port_id, struct ether_addr *mc_addr)
2453 {
2454         struct rte_port *port;
2455         uint32_t i;
2456
2457         if (port_id_is_invalid(port_id, ENABLED_WARN))
2458                 return;
2459
2460         port = &ports[port_id];
2461
2462         /*
2463          * Check that the added multicast MAC address is not already recorded
2464          * in the pool of multicast addresses.
2465          */
2466         for (i = 0; i < port->mc_addr_nb; i++) {
2467                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
2468                         printf("multicast address already filtered by port\n");
2469                         return;
2470                 }
2471         }
2472
2473         if (mcast_addr_pool_extend(port) != 0)
2474                 return;
2475         ether_addr_copy(mc_addr, &port->mc_addr_pool[i]);
2476         eth_port_multicast_addr_list_set(port_id);
2477 }
2478
2479 void
2480 mcast_addr_remove(uint8_t port_id, struct ether_addr *mc_addr)
2481 {
2482         struct rte_port *port;
2483         uint32_t i;
2484
2485         if (port_id_is_invalid(port_id, ENABLED_WARN))
2486                 return;
2487
2488         port = &ports[port_id];
2489
2490         /*
2491          * Search the pool of multicast MAC addresses for the removed address.
2492          */
2493         for (i = 0; i < port->mc_addr_nb; i++) {
2494                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
2495                         break;
2496         }
2497         if (i == port->mc_addr_nb) {
2498                 printf("multicast address not filtered by port %d\n", port_id);
2499                 return;
2500         }
2501
2502         mcast_addr_pool_remove(port, i);
2503         eth_port_multicast_addr_list_set(port_id);
2504 }
2505
2506 void
2507 port_dcb_info_display(uint8_t port_id)
2508 {
2509         struct rte_eth_dcb_info dcb_info;
2510         uint16_t i;
2511         int ret;
2512         static const char *border = "================";
2513
2514         if (port_id_is_invalid(port_id, ENABLED_WARN))
2515                 return;
2516
2517         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
2518         if (ret) {
2519                 printf("\n Failed to get dcb infos on port %-2d\n",
2520                         port_id);
2521                 return;
2522         }
2523         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
2524         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
2525         printf("\n  TC :        ");
2526         for (i = 0; i < dcb_info.nb_tcs; i++)
2527                 printf("\t%4d", i);
2528         printf("\n  Priority :  ");
2529         for (i = 0; i < dcb_info.nb_tcs; i++)
2530                 printf("\t%4d", dcb_info.prio_tc[i]);
2531         printf("\n  BW percent :");
2532         for (i = 0; i < dcb_info.nb_tcs; i++)
2533                 printf("\t%4d%%", dcb_info.tc_bws[i]);
2534         printf("\n  RXQ base :  ");
2535         for (i = 0; i < dcb_info.nb_tcs; i++)
2536                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
2537         printf("\n  RXQ number :");
2538         for (i = 0; i < dcb_info.nb_tcs; i++)
2539                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
2540         printf("\n  TXQ base :  ");
2541         for (i = 0; i < dcb_info.nb_tcs; i++)
2542                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
2543         printf("\n  TXQ number :");
2544         for (i = 0; i < dcb_info.nb_tcs; i++)
2545                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
2546         printf("\n");
2547 }