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