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