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