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