app/testpmd: fix some indent
[dpdk.git] / app / test-pmd / config.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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_tailq.h>
83 #include <rte_eal.h>
84 #include <rte_per_lcore.h>
85 #include <rte_lcore.h>
86 #include <rte_atomic.h>
87 #include <rte_branch_prediction.h>
88 #include <rte_ring.h>
89 #include <rte_mempool.h>
90 #include <rte_mbuf.h>
91 #include <rte_interrupts.h>
92 #include <rte_pci.h>
93 #include <rte_ether.h>
94 #include <rte_ethdev.h>
95 #include <rte_string_fns.h>
96
97 #include "testpmd.h"
98
99 static char *flowtype_to_str(uint16_t flow_type);
100
101 static void
102 print_ethaddr(const char *name, struct ether_addr *eth_addr)
103 {
104         char buf[ETHER_ADDR_FMT_SIZE];
105         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
106         printf("%s%s", name, buf);
107 }
108
109 void
110 nic_stats_display(portid_t port_id)
111 {
112         struct rte_eth_stats stats;
113         struct rte_port *port = &ports[port_id];
114         uint8_t i;
115
116         static const char *nic_stats_border = "########################";
117
118         if (port_id >= nb_ports) {
119                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
120                 return;
121         }
122         rte_eth_stats_get(port_id, &stats);
123         printf("\n  %s NIC statistics for port %-2d %s\n",
124                nic_stats_border, port_id, nic_stats_border);
125
126         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
127                 printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
128                        "%-"PRIu64"\n",
129                        stats.ipackets, stats.imissed, stats.ibytes);
130                 printf("  RX-badcrc:  %-10"PRIu64" RX-badlen: %-10"PRIu64" RX-errors: "
131                        "%-"PRIu64"\n",
132                        stats.ibadcrc, stats.ibadlen, stats.ierrors);
133                 printf("  RX-nombuf:  %-10"PRIu64"\n",
134                        stats.rx_nombuf);
135                 printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
136                        "%-"PRIu64"\n",
137                        stats.opackets, stats.oerrors, stats.obytes);
138         }
139         else {
140                 printf("  RX-packets:              %10"PRIu64"    RX-errors: %10"PRIu64
141                        "    RX-bytes: %10"PRIu64"\n",
142                        stats.ipackets, stats.ierrors, stats.ibytes);
143                 printf("  RX-badcrc:               %10"PRIu64"    RX-badlen: %10"PRIu64
144                        "  RX-errors:  %10"PRIu64"\n",
145                        stats.ibadcrc, stats.ibadlen, stats.ierrors);
146                 printf("  RX-nombuf:               %10"PRIu64"\n",
147                        stats.rx_nombuf);
148                 printf("  TX-packets:              %10"PRIu64"    TX-errors: %10"PRIu64
149                        "    TX-bytes: %10"PRIu64"\n",
150                        stats.opackets, stats.oerrors, stats.obytes);
151         }
152
153         /* stats fdir */
154         if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
155                 printf("  Fdirmiss:   %-10"PRIu64" Fdirmatch: %-10"PRIu64"\n",
156                        stats.fdirmiss,
157                        stats.fdirmatch);
158
159         if (port->rx_queue_stats_mapping_enabled) {
160                 printf("\n");
161                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
162                         printf("  Stats reg %2d RX-packets: %10"PRIu64
163                                "    RX-errors: %10"PRIu64
164                                "    RX-bytes: %10"PRIu64"\n",
165                                i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
166                 }
167         }
168         if (port->tx_queue_stats_mapping_enabled) {
169                 printf("\n");
170                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
171                         printf("  Stats reg %2d TX-packets: %10"PRIu64
172                                "                             TX-bytes: %10"PRIu64"\n",
173                                i, stats.q_opackets[i], stats.q_obytes[i]);
174                 }
175         }
176
177         /* Display statistics of XON/XOFF pause frames, if any. */
178         if ((stats.tx_pause_xon  | stats.rx_pause_xon |
179              stats.tx_pause_xoff | stats.rx_pause_xoff) > 0) {
180                 printf("  RX-XOFF:    %-10"PRIu64" RX-XON:    %-10"PRIu64"\n",
181                        stats.rx_pause_xoff, stats.rx_pause_xon);
182                 printf("  TX-XOFF:    %-10"PRIu64" TX-XON:    %-10"PRIu64"\n",
183                        stats.tx_pause_xoff, stats.tx_pause_xon);
184         }
185         printf("  %s############################%s\n",
186                nic_stats_border, nic_stats_border);
187 }
188
189 void
190 nic_stats_clear(portid_t port_id)
191 {
192         if (port_id >= nb_ports) {
193                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
194                 return;
195         }
196         rte_eth_stats_reset(port_id);
197         printf("\n  NIC statistics for port %d cleared\n", port_id);
198 }
199
200 void
201 nic_xstats_display(portid_t port_id)
202 {
203         struct rte_eth_xstats *xstats;
204         int len, ret, i;
205
206         printf("###### NIC extended statistics for port %-2d\n", port_id);
207
208         len = rte_eth_xstats_get(port_id, NULL, 0);
209         if (len < 0) {
210                 printf("Cannot get xstats count\n");
211                 return;
212         }
213         xstats = malloc(sizeof(xstats[0]) * len);
214         if (xstats == NULL) {
215                 printf("Cannot allocate memory for xstats\n");
216                 return;
217         }
218         ret = rte_eth_xstats_get(port_id, xstats, len);
219         if (ret < 0 || ret > len) {
220                 printf("Cannot get xstats\n");
221                 free(xstats);
222                 return;
223         }
224         for (i = 0; i < len; i++)
225                 printf("%s: %"PRIu64"\n", xstats[i].name, xstats[i].value);
226         free(xstats);
227 }
228
229 void
230 nic_xstats_clear(portid_t port_id)
231 {
232         rte_eth_xstats_reset(port_id);
233 }
234
235 void
236 nic_stats_mapping_display(portid_t port_id)
237 {
238         struct rte_port *port = &ports[port_id];
239         uint16_t i;
240
241         static const char *nic_stats_mapping_border = "########################";
242
243         if (port_id >= nb_ports) {
244                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
245                 return;
246         }
247
248         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
249                 printf("Port id %d - either does not support queue statistic mapping or"
250                        " no queue statistic mapping set\n", port_id);
251                 return;
252         }
253
254         printf("\n  %s NIC statistics mapping for port %-2d %s\n",
255                nic_stats_mapping_border, port_id, nic_stats_mapping_border);
256
257         if (port->rx_queue_stats_mapping_enabled) {
258                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
259                         if (rx_queue_stats_mappings[i].port_id == port_id) {
260                                 printf("  RX-queue %2d mapped to Stats Reg %2d\n",
261                                        rx_queue_stats_mappings[i].queue_id,
262                                        rx_queue_stats_mappings[i].stats_counter_id);
263                         }
264                 }
265                 printf("\n");
266         }
267
268
269         if (port->tx_queue_stats_mapping_enabled) {
270                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
271                         if (tx_queue_stats_mappings[i].port_id == port_id) {
272                                 printf("  TX-queue %2d mapped to Stats Reg %2d\n",
273                                        tx_queue_stats_mappings[i].queue_id,
274                                        tx_queue_stats_mappings[i].stats_counter_id);
275                         }
276                 }
277         }
278
279         printf("  %s####################################%s\n",
280                nic_stats_mapping_border, nic_stats_mapping_border);
281 }
282
283 void
284 port_infos_display(portid_t port_id)
285 {
286         struct rte_port *port;
287         struct ether_addr mac_addr;
288         struct rte_eth_link link;
289         struct rte_eth_dev_info dev_info;
290         int vlan_offload;
291         struct rte_mempool * mp;
292         static const char *info_border = "*********************";
293
294         if (port_id >= nb_ports) {
295                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
296                 return;
297         }
298         port = &ports[port_id];
299         rte_eth_link_get_nowait(port_id, &link);
300         printf("\n%s Infos for port %-2d %s\n",
301                info_border, port_id, info_border);
302         rte_eth_macaddr_get(port_id, &mac_addr);
303         print_ethaddr("MAC address: ", &mac_addr);
304         printf("\nConnect to socket: %u", port->socket_id);
305
306         if (port_numa[port_id] != NUMA_NO_CONFIG) {
307                 mp = mbuf_pool_find(port_numa[port_id]);
308                 if (mp)
309                         printf("\nmemory allocation on the socket: %d",
310                                                         port_numa[port_id]);
311         } else
312                 printf("\nmemory allocation on the socket: %u",port->socket_id);
313
314         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
315         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
316         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
317                ("full-duplex") : ("half-duplex"));
318         printf("Promiscuous mode: %s\n",
319                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
320         printf("Allmulticast mode: %s\n",
321                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
322         printf("Maximum number of MAC addresses: %u\n",
323                (unsigned int)(port->dev_info.max_mac_addrs));
324         printf("Maximum number of MAC addresses of hash filtering: %u\n",
325                (unsigned int)(port->dev_info.max_hash_mac_addrs));
326
327         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
328         if (vlan_offload >= 0){
329                 printf("VLAN offload: \n");
330                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
331                         printf("  strip on \n");
332                 else
333                         printf("  strip off \n");
334
335                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
336                         printf("  filter on \n");
337                 else
338                         printf("  filter off \n");
339
340                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
341                         printf("  qinq(extend) on \n");
342                 else
343                         printf("  qinq(extend) off \n");
344         }
345
346         memset(&dev_info, 0, sizeof(dev_info));
347         rte_eth_dev_info_get(port_id, &dev_info);
348         if (dev_info.reta_size > 0)
349                 printf("Redirection table size: %u\n", dev_info.reta_size);
350         if (!dev_info.flow_type_rss_offloads)
351                 printf("No flow type is supported.\n");
352         else {
353                 uint16_t i;
354                 char *p;
355
356                 printf("Supported flow types:\n");
357                 for (i = RTE_ETH_FLOW_UNKNOWN + 1; i < RTE_ETH_FLOW_MAX;
358                                                                 i++) {
359                         if (!(dev_info.flow_type_rss_offloads & (1ULL << i)))
360                                 continue;
361                         p = flowtype_to_str(i);
362                         printf("  %s\n", (p ? p : "unknown"));
363                 }
364         }
365 }
366
367 int
368 port_id_is_invalid(portid_t port_id)
369 {
370         if (port_id < nb_ports)
371                 return 0;
372         printf("Invalid port %d (must be < nb_ports=%d)\n", port_id, nb_ports);
373         return 1;
374 }
375
376 static int
377 vlan_id_is_invalid(uint16_t vlan_id)
378 {
379         if (vlan_id < 4096)
380                 return 0;
381         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
382         return 1;
383 }
384
385 static int
386 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
387 {
388         uint64_t pci_len;
389
390         if (reg_off & 0x3) {
391                 printf("Port register offset 0x%X not aligned on a 4-byte "
392                        "boundary\n",
393                        (unsigned)reg_off);
394                 return 1;
395         }
396         pci_len = ports[port_id].dev_info.pci_dev->mem_resource[0].len;
397         if (reg_off >= pci_len) {
398                 printf("Port %d: register offset %u (0x%X) out of port PCI "
399                        "resource (length=%"PRIu64")\n",
400                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
401                 return 1;
402         }
403         return 0;
404 }
405
406 static int
407 reg_bit_pos_is_invalid(uint8_t bit_pos)
408 {
409         if (bit_pos <= 31)
410                 return 0;
411         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
412         return 1;
413 }
414
415 #define display_port_and_reg_off(port_id, reg_off) \
416         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
417
418 static inline void
419 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
420 {
421         display_port_and_reg_off(port_id, (unsigned)reg_off);
422         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
423 }
424
425 void
426 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
427 {
428         uint32_t reg_v;
429
430
431         if (port_id_is_invalid(port_id))
432                 return;
433         if (port_reg_off_is_invalid(port_id, reg_off))
434                 return;
435         if (reg_bit_pos_is_invalid(bit_x))
436                 return;
437         reg_v = port_id_pci_reg_read(port_id, reg_off);
438         display_port_and_reg_off(port_id, (unsigned)reg_off);
439         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
440 }
441
442 void
443 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
444                            uint8_t bit1_pos, uint8_t bit2_pos)
445 {
446         uint32_t reg_v;
447         uint8_t  l_bit;
448         uint8_t  h_bit;
449
450         if (port_id_is_invalid(port_id))
451                 return;
452         if (port_reg_off_is_invalid(port_id, reg_off))
453                 return;
454         if (reg_bit_pos_is_invalid(bit1_pos))
455                 return;
456         if (reg_bit_pos_is_invalid(bit2_pos))
457                 return;
458         if (bit1_pos > bit2_pos)
459                 l_bit = bit2_pos, h_bit = bit1_pos;
460         else
461                 l_bit = bit1_pos, h_bit = bit2_pos;
462
463         reg_v = port_id_pci_reg_read(port_id, reg_off);
464         reg_v >>= l_bit;
465         if (h_bit < 31)
466                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
467         display_port_and_reg_off(port_id, (unsigned)reg_off);
468         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
469                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
470 }
471
472 void
473 port_reg_display(portid_t port_id, uint32_t reg_off)
474 {
475         uint32_t reg_v;
476
477         if (port_id_is_invalid(port_id))
478                 return;
479         if (port_reg_off_is_invalid(port_id, reg_off))
480                 return;
481         reg_v = port_id_pci_reg_read(port_id, reg_off);
482         display_port_reg_value(port_id, reg_off, reg_v);
483 }
484
485 void
486 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
487                  uint8_t bit_v)
488 {
489         uint32_t reg_v;
490
491         if (port_id_is_invalid(port_id))
492                 return;
493         if (port_reg_off_is_invalid(port_id, reg_off))
494                 return;
495         if (reg_bit_pos_is_invalid(bit_pos))
496                 return;
497         if (bit_v > 1) {
498                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
499                 return;
500         }
501         reg_v = port_id_pci_reg_read(port_id, reg_off);
502         if (bit_v == 0)
503                 reg_v &= ~(1 << bit_pos);
504         else
505                 reg_v |= (1 << bit_pos);
506         port_id_pci_reg_write(port_id, reg_off, reg_v);
507         display_port_reg_value(port_id, reg_off, reg_v);
508 }
509
510 void
511 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
512                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
513 {
514         uint32_t max_v;
515         uint32_t reg_v;
516         uint8_t  l_bit;
517         uint8_t  h_bit;
518
519         if (port_id_is_invalid(port_id))
520                 return;
521         if (port_reg_off_is_invalid(port_id, reg_off))
522                 return;
523         if (reg_bit_pos_is_invalid(bit1_pos))
524                 return;
525         if (reg_bit_pos_is_invalid(bit2_pos))
526                 return;
527         if (bit1_pos > bit2_pos)
528                 l_bit = bit2_pos, h_bit = bit1_pos;
529         else
530                 l_bit = bit1_pos, h_bit = bit2_pos;
531
532         if ((h_bit - l_bit) < 31)
533                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
534         else
535                 max_v = 0xFFFFFFFF;
536
537         if (value > max_v) {
538                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
539                                 (unsigned)value, (unsigned)value,
540                                 (unsigned)max_v, (unsigned)max_v);
541                 return;
542         }
543         reg_v = port_id_pci_reg_read(port_id, reg_off);
544         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
545         reg_v |= (value << l_bit); /* Set changed bits */
546         port_id_pci_reg_write(port_id, reg_off, reg_v);
547         display_port_reg_value(port_id, reg_off, reg_v);
548 }
549
550 void
551 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
552 {
553         if (port_id_is_invalid(port_id))
554                 return;
555         if (port_reg_off_is_invalid(port_id, reg_off))
556                 return;
557         port_id_pci_reg_write(port_id, reg_off, reg_v);
558         display_port_reg_value(port_id, reg_off, reg_v);
559 }
560
561 void
562 port_mtu_set(portid_t port_id, uint16_t mtu)
563 {
564         int diag;
565
566         if (port_id_is_invalid(port_id))
567                 return;
568         diag = rte_eth_dev_set_mtu(port_id, mtu);
569         if (diag == 0)
570                 return;
571         printf("Set MTU failed. diag=%d\n", diag);
572 }
573
574 /*
575  * RX/TX ring descriptors display functions.
576  */
577 int
578 rx_queue_id_is_invalid(queueid_t rxq_id)
579 {
580         if (rxq_id < nb_rxq)
581                 return 0;
582         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
583         return 1;
584 }
585
586 int
587 tx_queue_id_is_invalid(queueid_t txq_id)
588 {
589         if (txq_id < nb_txq)
590                 return 0;
591         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
592         return 1;
593 }
594
595 static int
596 rx_desc_id_is_invalid(uint16_t rxdesc_id)
597 {
598         if (rxdesc_id < nb_rxd)
599                 return 0;
600         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
601                rxdesc_id, nb_rxd);
602         return 1;
603 }
604
605 static int
606 tx_desc_id_is_invalid(uint16_t txdesc_id)
607 {
608         if (txdesc_id < nb_txd)
609                 return 0;
610         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
611                txdesc_id, nb_txd);
612         return 1;
613 }
614
615 static const struct rte_memzone *
616 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
617 {
618         char mz_name[RTE_MEMZONE_NAMESIZE];
619         const struct rte_memzone *mz;
620
621         snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
622                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
623         mz = rte_memzone_lookup(mz_name);
624         if (mz == NULL)
625                 printf("%s ring memory zoneof (port %d, queue %d) not"
626                        "found (zone name = %s\n",
627                        ring_name, port_id, q_id, mz_name);
628         return (mz);
629 }
630
631 union igb_ring_dword {
632         uint64_t dword;
633         struct {
634 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
635                 uint32_t lo;
636                 uint32_t hi;
637 #else
638                 uint32_t hi;
639                 uint32_t lo;
640 #endif
641         } words;
642 };
643
644 struct igb_ring_desc_32_bytes {
645         union igb_ring_dword lo_dword;
646         union igb_ring_dword hi_dword;
647         union igb_ring_dword resv1;
648         union igb_ring_dword resv2;
649 };
650
651 struct igb_ring_desc_16_bytes {
652         union igb_ring_dword lo_dword;
653         union igb_ring_dword hi_dword;
654 };
655
656 static void
657 ring_rxd_display_dword(union igb_ring_dword dword)
658 {
659         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
660                                         (unsigned)dword.words.hi);
661 }
662
663 static void
664 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
665 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
666                            uint8_t port_id,
667 #else
668                            __rte_unused uint8_t port_id,
669 #endif
670                            uint16_t desc_id)
671 {
672         struct igb_ring_desc_16_bytes *ring =
673                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
674 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
675         struct rte_eth_dev_info dev_info;
676
677         memset(&dev_info, 0, sizeof(dev_info));
678         rte_eth_dev_info_get(port_id, &dev_info);
679         if (strstr(dev_info.driver_name, "i40e") != NULL) {
680                 /* 32 bytes RX descriptor, i40e only */
681                 struct igb_ring_desc_32_bytes *ring =
682                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
683                 ring[desc_id].lo_dword.dword =
684                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
685                 ring_rxd_display_dword(ring[desc_id].lo_dword);
686                 ring[desc_id].hi_dword.dword =
687                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
688                 ring_rxd_display_dword(ring[desc_id].hi_dword);
689                 ring[desc_id].resv1.dword =
690                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
691                 ring_rxd_display_dword(ring[desc_id].resv1);
692                 ring[desc_id].resv2.dword =
693                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
694                 ring_rxd_display_dword(ring[desc_id].resv2);
695
696                 return;
697         }
698 #endif
699         /* 16 bytes RX descriptor */
700         ring[desc_id].lo_dword.dword =
701                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
702         ring_rxd_display_dword(ring[desc_id].lo_dword);
703         ring[desc_id].hi_dword.dword =
704                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
705         ring_rxd_display_dword(ring[desc_id].hi_dword);
706 }
707
708 static void
709 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
710 {
711         struct igb_ring_desc_16_bytes *ring;
712         struct igb_ring_desc_16_bytes txd;
713
714         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
715         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
716         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
717         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
718                         (unsigned)txd.lo_dword.words.lo,
719                         (unsigned)txd.lo_dword.words.hi,
720                         (unsigned)txd.hi_dword.words.lo,
721                         (unsigned)txd.hi_dword.words.hi);
722 }
723
724 void
725 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
726 {
727         const struct rte_memzone *rx_mz;
728
729         if (port_id_is_invalid(port_id))
730                 return;
731         if (rx_queue_id_is_invalid(rxq_id))
732                 return;
733         if (rx_desc_id_is_invalid(rxd_id))
734                 return;
735         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
736         if (rx_mz == NULL)
737                 return;
738         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
739 }
740
741 void
742 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
743 {
744         const struct rte_memzone *tx_mz;
745
746         if (port_id_is_invalid(port_id))
747                 return;
748         if (tx_queue_id_is_invalid(txq_id))
749                 return;
750         if (tx_desc_id_is_invalid(txd_id))
751                 return;
752         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
753         if (tx_mz == NULL)
754                 return;
755         ring_tx_descriptor_display(tx_mz, txd_id);
756 }
757
758 void
759 fwd_lcores_config_display(void)
760 {
761         lcoreid_t lc_id;
762
763         printf("List of forwarding lcores:");
764         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
765                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
766         printf("\n");
767 }
768 void
769 rxtx_config_display(void)
770 {
771         printf("  %s packet forwarding - CRC stripping %s - "
772                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
773                rx_mode.hw_strip_crc ? "enabled" : "disabled",
774                nb_pkt_per_burst);
775
776         if (cur_fwd_eng == &tx_only_engine)
777                 printf("  packet len=%u - nb packet segments=%d\n",
778                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
779
780         struct rte_eth_rxconf *rx_conf = &ports[0].rx_conf;
781         struct rte_eth_txconf *tx_conf = &ports[0].tx_conf;
782
783         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
784                nb_fwd_lcores, nb_fwd_ports);
785         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
786                nb_rxq, nb_rxd, rx_conf->rx_free_thresh);
787         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
788                rx_conf->rx_thresh.pthresh, rx_conf->rx_thresh.hthresh,
789                rx_conf->rx_thresh.wthresh);
790         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
791                nb_txq, nb_txd, tx_conf->tx_free_thresh);
792         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
793                tx_conf->tx_thresh.pthresh, tx_conf->tx_thresh.hthresh,
794                tx_conf->tx_thresh.wthresh);
795         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
796                tx_conf->tx_rs_thresh, tx_conf->txq_flags);
797 }
798
799 void
800 port_rss_reta_info(portid_t port_id,
801                    struct rte_eth_rss_reta_entry64 *reta_conf,
802                    uint16_t nb_entries)
803 {
804         uint16_t i, idx, shift;
805         int ret;
806
807         if (port_id_is_invalid(port_id))
808                 return;
809
810         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
811         if (ret != 0) {
812                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
813                 return;
814         }
815
816         for (i = 0; i < nb_entries; i++) {
817                 idx = i / RTE_RETA_GROUP_SIZE;
818                 shift = i % RTE_RETA_GROUP_SIZE;
819                 if (!(reta_conf[idx].mask & (1ULL << shift)))
820                         continue;
821                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
822                                         i, reta_conf[idx].reta[shift]);
823         }
824 }
825
826 /*
827  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
828  * key of the port.
829  */
830 void
831 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
832 {
833         struct rss_type_info {
834                 char str[32];
835                 uint64_t rss_type;
836         };
837         static const struct rss_type_info rss_type_table[] = {
838                 {"ipv4", ETH_RSS_IPV4},
839                 {"ipv4-frag", ETH_RSS_FRAG_IPV4},
840                 {"ipv4-tcp", ETH_RSS_NONFRAG_IPV4_TCP},
841                 {"ipv4-udp", ETH_RSS_NONFRAG_IPV4_UDP},
842                 {"ipv4-sctp", ETH_RSS_NONFRAG_IPV4_SCTP},
843                 {"ipv4-other", ETH_RSS_NONFRAG_IPV4_OTHER},
844                 {"ipv6", ETH_RSS_IPV6},
845                 {"ipv6-frag", ETH_RSS_FRAG_IPV6},
846                 {"ipv6-tcp", ETH_RSS_NONFRAG_IPV6_TCP},
847                 {"ipv6-udp", ETH_RSS_NONFRAG_IPV6_UDP},
848                 {"ipv6-sctp", ETH_RSS_NONFRAG_IPV6_SCTP},
849                 {"ipv6-other", ETH_RSS_NONFRAG_IPV6_OTHER},
850                 {"l2-payload", ETH_RSS_L2_PAYLOAD},
851                 {"ipv6-ex", ETH_RSS_IPV6_EX},
852                 {"ipv6-tcp-ex", ETH_RSS_IPV6_TCP_EX},
853                 {"ipv6-udp-ex", ETH_RSS_IPV6_UDP_EX},
854         };
855
856         struct rte_eth_rss_conf rss_conf;
857         uint8_t rss_key[10 * 4];
858         uint64_t rss_hf;
859         uint8_t i;
860         int diag;
861
862         if (port_id_is_invalid(port_id))
863                 return;
864         /* Get RSS hash key if asked to display it */
865         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
866         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
867         if (diag != 0) {
868                 switch (diag) {
869                 case -ENODEV:
870                         printf("port index %d invalid\n", port_id);
871                         break;
872                 case -ENOTSUP:
873                         printf("operation not supported by device\n");
874                         break;
875                 default:
876                         printf("operation failed - diag=%d\n", diag);
877                         break;
878                 }
879                 return;
880         }
881         rss_hf = rss_conf.rss_hf;
882         if (rss_hf == 0) {
883                 printf("RSS disabled\n");
884                 return;
885         }
886         printf("RSS functions:\n ");
887         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
888                 if (rss_hf & rss_type_table[i].rss_type)
889                         printf("%s ", rss_type_table[i].str);
890         }
891         printf("\n");
892         if (!show_rss_key)
893                 return;
894         printf("RSS key:\n");
895         for (i = 0; i < sizeof(rss_key); i++)
896                 printf("%02X", rss_key[i]);
897         printf("\n");
898 }
899
900 void
901 port_rss_hash_key_update(portid_t port_id, uint8_t *hash_key)
902 {
903         struct rte_eth_rss_conf rss_conf;
904         int diag;
905
906         rss_conf.rss_key = NULL;
907         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
908         if (diag == 0) {
909                 rss_conf.rss_key = hash_key;
910                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
911         }
912         if (diag == 0)
913                 return;
914
915         switch (diag) {
916         case -ENODEV:
917                 printf("port index %d invalid\n", port_id);
918                 break;
919         case -ENOTSUP:
920                 printf("operation not supported by device\n");
921                 break;
922         default:
923                 printf("operation failed - diag=%d\n", diag);
924                 break;
925         }
926 }
927
928 /*
929  * Setup forwarding configuration for each logical core.
930  */
931 static void
932 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
933 {
934         streamid_t nb_fs_per_lcore;
935         streamid_t nb_fs;
936         streamid_t sm_id;
937         lcoreid_t  nb_extra;
938         lcoreid_t  nb_fc;
939         lcoreid_t  nb_lc;
940         lcoreid_t  lc_id;
941
942         nb_fs = cfg->nb_fwd_streams;
943         nb_fc = cfg->nb_fwd_lcores;
944         if (nb_fs <= nb_fc) {
945                 nb_fs_per_lcore = 1;
946                 nb_extra = 0;
947         } else {
948                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
949                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
950         }
951
952         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
953         sm_id = 0;
954         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
955                 fwd_lcores[lc_id]->stream_idx = sm_id;
956                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
957                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
958         }
959
960         /*
961          * Assign extra remaining streams, if any.
962          */
963         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
964         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
965                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
966                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
967                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
968         }
969 }
970
971 static void
972 simple_fwd_config_setup(void)
973 {
974         portid_t i;
975         portid_t j;
976         portid_t inc = 2;
977
978         if (port_topology == PORT_TOPOLOGY_CHAINED ||
979             port_topology == PORT_TOPOLOGY_LOOP) {
980                 inc = 1;
981         } else if (nb_fwd_ports % 2) {
982                 printf("\nWarning! Cannot handle an odd number of ports "
983                        "with the current port topology. Configuration "
984                        "must be changed to have an even number of ports, "
985                        "or relaunch application with "
986                        "--port-topology=chained\n\n");
987         }
988
989         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
990         cur_fwd_config.nb_fwd_streams =
991                 (streamid_t) cur_fwd_config.nb_fwd_ports;
992
993         /* reinitialize forwarding streams */
994         init_fwd_streams();
995
996         /*
997          * In the simple forwarding test, the number of forwarding cores
998          * must be lower or equal to the number of forwarding ports.
999          */
1000         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1001         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
1002                 cur_fwd_config.nb_fwd_lcores =
1003                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
1004         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1005
1006         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
1007                 if (port_topology != PORT_TOPOLOGY_LOOP)
1008                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
1009                 else
1010                         j = i;
1011                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
1012                 fwd_streams[i]->rx_queue  = 0;
1013                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
1014                 fwd_streams[i]->tx_queue  = 0;
1015                 fwd_streams[i]->peer_addr = j;
1016
1017                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
1018                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
1019                         fwd_streams[j]->rx_queue  = 0;
1020                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
1021                         fwd_streams[j]->tx_queue  = 0;
1022                         fwd_streams[j]->peer_addr = i;
1023                 }
1024         }
1025 }
1026
1027 /**
1028  * For the RSS forwarding test, each core is assigned on every port a transmit
1029  * queue whose index is the index of the core itself. This approach limits the
1030  * maximumm number of processing cores of the RSS test to the maximum number of
1031  * TX queues supported by the devices.
1032  *
1033  * Each core is assigned a single stream, each stream being composed of
1034  * a RX queue to poll on a RX port for input messages, associated with
1035  * a TX queue of a TX port where to send forwarded packets.
1036  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1037  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1038  * following rules:
1039  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1040  *    - TxQl = RxQj
1041  */
1042 static void
1043 rss_fwd_config_setup(void)
1044 {
1045         portid_t   rxp;
1046         portid_t   txp;
1047         queueid_t  rxq;
1048         queueid_t  nb_q;
1049         lcoreid_t  lc_id;
1050
1051         nb_q = nb_rxq;
1052         if (nb_q > nb_txq)
1053                 nb_q = nb_txq;
1054         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1055         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1056         cur_fwd_config.nb_fwd_streams =
1057                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1058         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
1059                 cur_fwd_config.nb_fwd_streams =
1060                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
1061         else
1062                 cur_fwd_config.nb_fwd_lcores =
1063                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1064
1065         /* reinitialize forwarding streams */
1066         init_fwd_streams();
1067
1068         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1069         rxp = 0; rxq = 0;
1070         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1071                 struct fwd_stream *fs;
1072
1073                 fs = fwd_streams[lc_id];
1074
1075                 if ((rxp & 0x1) == 0)
1076                         txp = (portid_t) (rxp + 1);
1077                 else
1078                         txp = (portid_t) (rxp - 1);
1079                 /*
1080                  * if we are in loopback, simply send stuff out through the
1081                  * ingress port
1082                  */
1083                 if (port_topology == PORT_TOPOLOGY_LOOP)
1084                         txp = rxp;
1085
1086                 fs->rx_port = fwd_ports_ids[rxp];
1087                 fs->rx_queue = rxq;
1088                 fs->tx_port = fwd_ports_ids[txp];
1089                 fs->tx_queue = rxq;
1090                 fs->peer_addr = fs->tx_port;
1091                 rxq = (queueid_t) (rxq + 1);
1092                 if (rxq < nb_q)
1093                         continue;
1094                 /*
1095                  * rxq == nb_q
1096                  * Restart from RX queue 0 on next RX port
1097                  */
1098                 rxq = 0;
1099                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1100                         rxp = (portid_t)
1101                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1102                 else
1103                         rxp = (portid_t) (rxp + 1);
1104         }
1105 }
1106
1107 /*
1108  * In DCB and VT on,the mapping of 128 receive queues to 128 transmit queues.
1109  */
1110 static void
1111 dcb_rxq_2_txq_mapping(queueid_t rxq, queueid_t *txq)
1112 {
1113         if(dcb_q_mapping == DCB_4_TCS_Q_MAPPING) {
1114
1115                 if (rxq < 32)
1116                         /* tc0: 0-31 */
1117                         *txq = rxq;
1118                 else if (rxq < 64) {
1119                         /* tc1: 64-95 */
1120                         *txq =  (uint16_t)(rxq + 32);
1121                 }
1122                 else {
1123                         /* tc2: 96-111;tc3:112-127 */
1124                         *txq =  (uint16_t)(rxq/2 + 64);
1125                 }
1126         }
1127         else {
1128                 if (rxq < 16)
1129                         /* tc0 mapping*/
1130                         *txq = rxq;
1131                 else if (rxq < 32) {
1132                         /* tc1 mapping*/
1133                          *txq = (uint16_t)(rxq + 16);
1134                 }
1135                 else if (rxq < 64) {
1136                         /*tc2,tc3 mapping */
1137                         *txq =  (uint16_t)(rxq + 32);
1138                 }
1139                 else {
1140                         /* tc4,tc5,tc6 and tc7 mapping */
1141                         *txq =  (uint16_t)(rxq/2 + 64);
1142                 }
1143         }
1144 }
1145
1146 /**
1147  * For the DCB forwarding test, each core is assigned on every port multi-transmit
1148  * queue.
1149  *
1150  * Each core is assigned a multi-stream, each stream being composed of
1151  * a RX queue to poll on a RX port for input messages, associated with
1152  * a TX queue of a TX port where to send forwarded packets.
1153  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1154  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1155  * following rules:
1156  * In VT mode,
1157  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1158  *    - TxQl = RxQj
1159  * In non-VT mode,
1160  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1161  *    There is a mapping of RxQj to TxQl to be required,and the mapping was implemented
1162  *    in dcb_rxq_2_txq_mapping function.
1163  */
1164 static void
1165 dcb_fwd_config_setup(void)
1166 {
1167         portid_t   rxp;
1168         portid_t   txp;
1169         queueid_t  rxq;
1170         queueid_t  nb_q;
1171         lcoreid_t  lc_id;
1172         uint16_t sm_id;
1173
1174         nb_q = nb_rxq;
1175
1176         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1177         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1178         cur_fwd_config.nb_fwd_streams =
1179                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1180
1181         /* reinitialize forwarding streams */
1182         init_fwd_streams();
1183
1184         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1185         rxp = 0; rxq = 0;
1186         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1187                 /* a fwd core can run multi-streams */
1188                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++)
1189                 {
1190                         struct fwd_stream *fs;
1191                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1192                         if ((rxp & 0x1) == 0)
1193                                 txp = (portid_t) (rxp + 1);
1194                         else
1195                                 txp = (portid_t) (rxp - 1);
1196                         fs->rx_port = fwd_ports_ids[rxp];
1197                         fs->rx_queue = rxq;
1198                         fs->tx_port = fwd_ports_ids[txp];
1199                         if (dcb_q_mapping == DCB_VT_Q_MAPPING)
1200                                 fs->tx_queue = rxq;
1201                         else
1202                                 dcb_rxq_2_txq_mapping(rxq, &fs->tx_queue);
1203                         fs->peer_addr = fs->tx_port;
1204                         rxq = (queueid_t) (rxq + 1);
1205                         if (rxq < nb_q)
1206                                 continue;
1207                         rxq = 0;
1208                         if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1209                                 rxp = (portid_t)
1210                                         (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1211                         else
1212                                 rxp = (portid_t) (rxp + 1);
1213                 }
1214         }
1215 }
1216
1217 static void
1218 icmp_echo_config_setup(void)
1219 {
1220         portid_t  rxp;
1221         queueid_t rxq;
1222         lcoreid_t lc_id;
1223         uint16_t  sm_id;
1224
1225         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
1226                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
1227                         (nb_txq * nb_fwd_ports);
1228         else
1229                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1230         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1231         cur_fwd_config.nb_fwd_streams =
1232                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1233         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1234                 cur_fwd_config.nb_fwd_lcores =
1235                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1236         if (verbose_level > 0) {
1237                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
1238                        __FUNCTION__,
1239                        cur_fwd_config.nb_fwd_lcores,
1240                        cur_fwd_config.nb_fwd_ports,
1241                        cur_fwd_config.nb_fwd_streams);
1242         }
1243
1244         /* reinitialize forwarding streams */
1245         init_fwd_streams();
1246         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1247         rxp = 0; rxq = 0;
1248         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1249                 if (verbose_level > 0)
1250                         printf("  core=%d: \n", lc_id);
1251                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1252                         struct fwd_stream *fs;
1253                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1254                         fs->rx_port = fwd_ports_ids[rxp];
1255                         fs->rx_queue = rxq;
1256                         fs->tx_port = fs->rx_port;
1257                         fs->tx_queue = lc_id;
1258                         fs->peer_addr = fs->tx_port;
1259                         if (verbose_level > 0)
1260                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
1261                                        sm_id, fs->rx_port, fs->rx_queue,
1262                                        fs->tx_queue);
1263                         rxq = (queueid_t) (rxq + 1);
1264                         if (rxq == nb_rxq) {
1265                                 rxq = 0;
1266                                 rxp = (portid_t) (rxp + 1);
1267                         }
1268                 }
1269         }
1270 }
1271
1272 void
1273 fwd_config_setup(void)
1274 {
1275         cur_fwd_config.fwd_eng = cur_fwd_eng;
1276         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
1277                 icmp_echo_config_setup();
1278                 return;
1279         }
1280         if ((nb_rxq > 1) && (nb_txq > 1)){
1281                 if (dcb_config)
1282                         dcb_fwd_config_setup();
1283                 else
1284                         rss_fwd_config_setup();
1285         }
1286         else
1287                 simple_fwd_config_setup();
1288 }
1289
1290 static void
1291 pkt_fwd_config_display(struct fwd_config *cfg)
1292 {
1293         struct fwd_stream *fs;
1294         lcoreid_t  lc_id;
1295         streamid_t sm_id;
1296
1297         printf("%s packet forwarding - ports=%d - cores=%d - streams=%d - "
1298                 "NUMA support %s, MP over anonymous pages %s\n",
1299                 cfg->fwd_eng->fwd_mode_name,
1300                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
1301                 numa_support == 1 ? "enabled" : "disabled",
1302                 mp_anon != 0 ? "enabled" : "disabled");
1303
1304         if (strcmp(cfg->fwd_eng->fwd_mode_name, "mac_retry") == 0)
1305                 printf("TX retry num: %u, delay between TX retries: %uus\n",
1306                         burst_tx_retry_num, burst_tx_delay_time);
1307         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
1308                 printf("Logical Core %u (socket %u) forwards packets on "
1309                        "%d streams:",
1310                        fwd_lcores_cpuids[lc_id],
1311                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
1312                        fwd_lcores[lc_id]->stream_nb);
1313                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1314                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1315                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
1316                                "P=%d/Q=%d (socket %u) ",
1317                                fs->rx_port, fs->rx_queue,
1318                                ports[fs->rx_port].socket_id,
1319                                fs->tx_port, fs->tx_queue,
1320                                ports[fs->tx_port].socket_id);
1321                         print_ethaddr("peer=",
1322                                       &peer_eth_addrs[fs->peer_addr]);
1323                 }
1324                 printf("\n");
1325         }
1326         printf("\n");
1327 }
1328
1329
1330 void
1331 fwd_config_display(void)
1332 {
1333         if((dcb_config) && (nb_fwd_lcores == 1)) {
1334                 printf("In DCB mode,the nb forwarding cores should be larger than 1\n");
1335                 return;
1336         }
1337         fwd_config_setup();
1338         pkt_fwd_config_display(&cur_fwd_config);
1339 }
1340
1341 int
1342 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
1343 {
1344         unsigned int i;
1345         unsigned int lcore_cpuid;
1346         int record_now;
1347
1348         record_now = 0;
1349  again:
1350         for (i = 0; i < nb_lc; i++) {
1351                 lcore_cpuid = lcorelist[i];
1352                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
1353                         printf("lcore %u not enabled\n", lcore_cpuid);
1354                         return -1;
1355                 }
1356                 if (lcore_cpuid == rte_get_master_lcore()) {
1357                         printf("lcore %u cannot be masked on for running "
1358                                "packet forwarding, which is the master lcore "
1359                                "and reserved for command line parsing only\n",
1360                                lcore_cpuid);
1361                         return -1;
1362                 }
1363                 if (record_now)
1364                         fwd_lcores_cpuids[i] = lcore_cpuid;
1365         }
1366         if (record_now == 0) {
1367                 record_now = 1;
1368                 goto again;
1369         }
1370         nb_cfg_lcores = (lcoreid_t) nb_lc;
1371         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
1372                 printf("previous number of forwarding cores %u - changed to "
1373                        "number of configured cores %u\n",
1374                        (unsigned int) nb_fwd_lcores, nb_lc);
1375                 nb_fwd_lcores = (lcoreid_t) nb_lc;
1376         }
1377
1378         return 0;
1379 }
1380
1381 int
1382 set_fwd_lcores_mask(uint64_t lcoremask)
1383 {
1384         unsigned int lcorelist[64];
1385         unsigned int nb_lc;
1386         unsigned int i;
1387
1388         if (lcoremask == 0) {
1389                 printf("Invalid NULL mask of cores\n");
1390                 return -1;
1391         }
1392         nb_lc = 0;
1393         for (i = 0; i < 64; i++) {
1394                 if (! ((uint64_t)(1ULL << i) & lcoremask))
1395                         continue;
1396                 lcorelist[nb_lc++] = i;
1397         }
1398         return set_fwd_lcores_list(lcorelist, nb_lc);
1399 }
1400
1401 void
1402 set_fwd_lcores_number(uint16_t nb_lc)
1403 {
1404         if (nb_lc > nb_cfg_lcores) {
1405                 printf("nb fwd cores %u > %u (max. number of configured "
1406                        "lcores) - ignored\n",
1407                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
1408                 return;
1409         }
1410         nb_fwd_lcores = (lcoreid_t) nb_lc;
1411         printf("Number of forwarding cores set to %u\n",
1412                (unsigned int) nb_fwd_lcores);
1413 }
1414
1415 void
1416 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
1417 {
1418         unsigned int i;
1419         portid_t port_id;
1420         int record_now;
1421
1422         record_now = 0;
1423  again:
1424         for (i = 0; i < nb_pt; i++) {
1425                 port_id = (portid_t) portlist[i];
1426                 if (port_id >= nb_ports) {
1427                         printf("Invalid port id %u >= %u\n",
1428                                (unsigned int) port_id,
1429                                (unsigned int) nb_ports);
1430                         return;
1431                 }
1432                 if (record_now)
1433                         fwd_ports_ids[i] = port_id;
1434         }
1435         if (record_now == 0) {
1436                 record_now = 1;
1437                 goto again;
1438         }
1439         nb_cfg_ports = (portid_t) nb_pt;
1440         if (nb_fwd_ports != (portid_t) nb_pt) {
1441                 printf("previous number of forwarding ports %u - changed to "
1442                        "number of configured ports %u\n",
1443                        (unsigned int) nb_fwd_ports, nb_pt);
1444                 nb_fwd_ports = (portid_t) nb_pt;
1445         }
1446 }
1447
1448 void
1449 set_fwd_ports_mask(uint64_t portmask)
1450 {
1451         unsigned int portlist[64];
1452         unsigned int nb_pt;
1453         unsigned int i;
1454
1455         if (portmask == 0) {
1456                 printf("Invalid NULL mask of ports\n");
1457                 return;
1458         }
1459         nb_pt = 0;
1460         for (i = 0; i < (unsigned)RTE_MIN(64, RTE_MAX_ETHPORTS); i++) {
1461                 if (! ((uint64_t)(1ULL << i) & portmask))
1462                         continue;
1463                 portlist[nb_pt++] = i;
1464         }
1465         set_fwd_ports_list(portlist, nb_pt);
1466 }
1467
1468 void
1469 set_fwd_ports_number(uint16_t nb_pt)
1470 {
1471         if (nb_pt > nb_cfg_ports) {
1472                 printf("nb fwd ports %u > %u (number of configured "
1473                        "ports) - ignored\n",
1474                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
1475                 return;
1476         }
1477         nb_fwd_ports = (portid_t) nb_pt;
1478         printf("Number of forwarding ports set to %u\n",
1479                (unsigned int) nb_fwd_ports);
1480 }
1481
1482 void
1483 set_nb_pkt_per_burst(uint16_t nb)
1484 {
1485         if (nb > MAX_PKT_BURST) {
1486                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
1487                        " ignored\n",
1488                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
1489                 return;
1490         }
1491         nb_pkt_per_burst = nb;
1492         printf("Number of packets per burst set to %u\n",
1493                (unsigned int) nb_pkt_per_burst);
1494 }
1495
1496 void
1497 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
1498 {
1499         uint16_t tx_pkt_len;
1500         unsigned i;
1501
1502         if (nb_segs >= (unsigned) nb_txd) {
1503                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
1504                        nb_segs, (unsigned int) nb_txd);
1505                 return;
1506         }
1507
1508         /*
1509          * Check that each segment length is greater or equal than
1510          * the mbuf data sise.
1511          * Check also that the total packet length is greater or equal than the
1512          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
1513          */
1514         tx_pkt_len = 0;
1515         for (i = 0; i < nb_segs; i++) {
1516                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
1517                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
1518                                i, seg_lengths[i], (unsigned) mbuf_data_size);
1519                         return;
1520                 }
1521                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
1522         }
1523         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
1524                 printf("total packet length=%u < %d - give up\n",
1525                                 (unsigned) tx_pkt_len,
1526                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
1527                 return;
1528         }
1529
1530         for (i = 0; i < nb_segs; i++)
1531                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
1532
1533         tx_pkt_length  = tx_pkt_len;
1534         tx_pkt_nb_segs = (uint8_t) nb_segs;
1535 }
1536
1537 char*
1538 list_pkt_forwarding_modes(void)
1539 {
1540         static char fwd_modes[128] = "";
1541         const char *separator = "|";
1542         struct fwd_engine *fwd_eng;
1543         unsigned i = 0;
1544
1545         if (strlen (fwd_modes) == 0) {
1546                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1547                         strcat(fwd_modes, fwd_eng->fwd_mode_name);
1548                         strcat(fwd_modes, separator);
1549                 }
1550                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1551         }
1552
1553         return fwd_modes;
1554 }
1555
1556 void
1557 set_pkt_forwarding_mode(const char *fwd_mode_name)
1558 {
1559         struct fwd_engine *fwd_eng;
1560         unsigned i;
1561
1562         i = 0;
1563         while ((fwd_eng = fwd_engines[i]) != NULL) {
1564                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
1565                         printf("Set %s packet forwarding mode\n",
1566                                fwd_mode_name);
1567                         cur_fwd_eng = fwd_eng;
1568                         return;
1569                 }
1570                 i++;
1571         }
1572         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
1573 }
1574
1575 void
1576 set_verbose_level(uint16_t vb_level)
1577 {
1578         printf("Change verbose level from %u to %u\n",
1579                (unsigned int) verbose_level, (unsigned int) vb_level);
1580         verbose_level = vb_level;
1581 }
1582
1583 void
1584 vlan_extend_set(portid_t port_id, int on)
1585 {
1586         int diag;
1587         int vlan_offload;
1588
1589         if (port_id_is_invalid(port_id))
1590                 return;
1591
1592         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1593
1594         if (on)
1595                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
1596         else
1597                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
1598
1599         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1600         if (diag < 0)
1601                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
1602                "diag=%d\n", port_id, on, diag);
1603 }
1604
1605 void
1606 rx_vlan_strip_set(portid_t port_id, int on)
1607 {
1608         int diag;
1609         int vlan_offload;
1610
1611         if (port_id_is_invalid(port_id))
1612                 return;
1613
1614         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1615
1616         if (on)
1617                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
1618         else
1619                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
1620
1621         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1622         if (diag < 0)
1623                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
1624                "diag=%d\n", port_id, on, diag);
1625 }
1626
1627 void
1628 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
1629 {
1630         int diag;
1631
1632         if (port_id_is_invalid(port_id))
1633                 return;
1634
1635         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
1636         if (diag < 0)
1637                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
1638                "diag=%d\n", port_id, queue_id, on, diag);
1639 }
1640
1641 void
1642 rx_vlan_filter_set(portid_t port_id, int on)
1643 {
1644         int diag;
1645         int vlan_offload;
1646
1647         if (port_id_is_invalid(port_id))
1648                 return;
1649
1650         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1651
1652         if (on)
1653                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1654         else
1655                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
1656
1657         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1658         if (diag < 0)
1659                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
1660                "diag=%d\n", port_id, on, diag);
1661 }
1662
1663 void
1664 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
1665 {
1666         int diag;
1667
1668         if (port_id_is_invalid(port_id))
1669                 return;
1670         if (vlan_id_is_invalid(vlan_id))
1671                 return;
1672         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
1673         if (diag == 0)
1674                 return;
1675         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
1676                "diag=%d\n",
1677                port_id, vlan_id, on, diag);
1678 }
1679
1680 void
1681 rx_vlan_all_filter_set(portid_t port_id, int on)
1682 {
1683         uint16_t vlan_id;
1684
1685         if (port_id_is_invalid(port_id))
1686                 return;
1687         for (vlan_id = 0; vlan_id < 4096; vlan_id++)
1688                 rx_vft_set(port_id, vlan_id, on);
1689 }
1690
1691 void
1692 vlan_tpid_set(portid_t port_id, uint16_t tp_id)
1693 {
1694         int diag;
1695         if (port_id_is_invalid(port_id))
1696                 return;
1697
1698         diag = rte_eth_dev_set_vlan_ether_type(port_id, tp_id);
1699         if (diag == 0)
1700                 return;
1701
1702         printf("tx_vlan_tpid_set(port_pi=%d, tpid=%d) failed "
1703                "diag=%d\n",
1704                port_id, tp_id, diag);
1705 }
1706
1707 void
1708 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
1709 {
1710         if (port_id_is_invalid(port_id))
1711                 return;
1712         if (vlan_id_is_invalid(vlan_id))
1713                 return;
1714         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1715         ports[port_id].tx_vlan_id = vlan_id;
1716 }
1717
1718 void
1719 tx_vlan_reset(portid_t port_id)
1720 {
1721         if (port_id_is_invalid(port_id))
1722                 return;
1723         ports[port_id].tx_ol_flags &= ~TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1724 }
1725
1726 void
1727 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
1728 {
1729         if (port_id_is_invalid(port_id))
1730                 return;
1731
1732         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
1733 }
1734
1735 void
1736 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
1737 {
1738         uint16_t i;
1739         uint8_t existing_mapping_found = 0;
1740
1741         if (port_id_is_invalid(port_id))
1742                 return;
1743
1744         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
1745                 return;
1746
1747         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1748                 printf("map_value not in required range 0..%d\n",
1749                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1750                 return;
1751         }
1752
1753         if (!is_rx) { /*then tx*/
1754                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1755                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1756                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
1757                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
1758                                 existing_mapping_found = 1;
1759                                 break;
1760                         }
1761                 }
1762                 if (!existing_mapping_found) { /* A new additional mapping... */
1763                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
1764                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
1765                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
1766                         nb_tx_queue_stats_mappings++;
1767                 }
1768         }
1769         else { /*rx*/
1770                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1771                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1772                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
1773                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
1774                                 existing_mapping_found = 1;
1775                                 break;
1776                         }
1777                 }
1778                 if (!existing_mapping_found) { /* A new additional mapping... */
1779                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
1780                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
1781                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
1782                         nb_rx_queue_stats_mappings++;
1783                 }
1784         }
1785 }
1786
1787 static inline void
1788 print_fdir_mask(struct rte_eth_fdir_masks *mask)
1789 {
1790         printf("\n    vlan_tci: 0x%04x, src_ipv4: 0x%08x, dst_ipv4: 0x%08x,"
1791                       " src_port: 0x%04x, dst_port: 0x%04x",
1792                 mask->vlan_tci_mask, mask->ipv4_mask.src_ip,
1793                 mask->ipv4_mask.dst_ip,
1794                 mask->src_port_mask, mask->dst_port_mask);
1795
1796         printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x,"
1797                      " dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
1798                 mask->ipv6_mask.src_ip[0], mask->ipv6_mask.src_ip[1],
1799                 mask->ipv6_mask.src_ip[2], mask->ipv6_mask.src_ip[3],
1800                 mask->ipv6_mask.dst_ip[0], mask->ipv6_mask.dst_ip[1],
1801                 mask->ipv6_mask.dst_ip[2], mask->ipv6_mask.dst_ip[3]);
1802         printf("\n");
1803 }
1804
1805 static inline void
1806 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
1807 {
1808         struct rte_eth_flex_payload_cfg *cfg;
1809         uint32_t i, j;
1810
1811         for (i = 0; i < flex_conf->nb_payloads; i++) {
1812                 cfg = &flex_conf->flex_set[i];
1813                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
1814                         printf("\n    RAW:  ");
1815                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
1816                         printf("\n    L2_PAYLOAD:  ");
1817                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
1818                         printf("\n    L3_PAYLOAD:  ");
1819                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
1820                         printf("\n    L4_PAYLOAD:  ");
1821                 else
1822                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
1823                 for (j = 0; j < num; j++)
1824                         printf("  %-5u", cfg->src_offset[j]);
1825         }
1826         printf("\n");
1827 }
1828
1829 static char *
1830 flowtype_to_str(uint16_t flow_type)
1831 {
1832         struct flow_type_info {
1833                 char str[32];
1834                 uint16_t ftype;
1835         };
1836
1837         uint8_t i;
1838         static struct flow_type_info flowtype_str_table[] = {
1839                 {"raw", RTE_ETH_FLOW_RAW},
1840                 {"ipv4", RTE_ETH_FLOW_IPV4},
1841                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
1842                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
1843                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
1844                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
1845                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
1846                 {"ipv6", RTE_ETH_FLOW_IPV6},
1847                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
1848                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
1849                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
1850                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
1851                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
1852                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
1853         };
1854
1855         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
1856                 if (flowtype_str_table[i].ftype == flow_type)
1857                         return flowtype_str_table[i].str;
1858         }
1859
1860         return NULL;
1861 }
1862
1863 static inline void
1864 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
1865 {
1866         struct rte_eth_fdir_flex_mask *mask;
1867         uint32_t i, j;
1868         char *p;
1869
1870         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
1871                 mask = &flex_conf->flex_mask[i];
1872                 p = flowtype_to_str(mask->flow_type);
1873                 printf("\n    %s:\t", p ? p : "unknown");
1874                 for (j = 0; j < num; j++)
1875                         printf(" %02x", mask->mask[j]);
1876         }
1877         printf("\n");
1878 }
1879
1880 static inline void
1881 print_fdir_flow_type(uint32_t flow_types_mask)
1882 {
1883         int i;
1884         char *p;
1885
1886         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
1887                 if (!(flow_types_mask & (1 << i)))
1888                         continue;
1889                 p = flowtype_to_str(i);
1890                 if (p)
1891                         printf(" %s", p);
1892                 else
1893                         printf(" unknown");
1894         }
1895         printf("\n");
1896 }
1897
1898 void
1899 fdir_get_infos(portid_t port_id)
1900 {
1901         struct rte_eth_fdir_stats fdir_stat;
1902         struct rte_eth_fdir_info fdir_info;
1903         int ret;
1904
1905         static const char *fdir_stats_border = "########################";
1906
1907         if (port_id_is_invalid(port_id))
1908                 return;
1909         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
1910         if (ret < 0) {
1911                 printf("\n FDIR is not supported on port %-2d\n",
1912                         port_id);
1913                 return;
1914         }
1915
1916         memset(&fdir_info, 0, sizeof(fdir_info));
1917         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
1918                                RTE_ETH_FILTER_INFO, &fdir_info);
1919         memset(&fdir_stat, 0, sizeof(fdir_stat));
1920         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
1921                                RTE_ETH_FILTER_STATS, &fdir_stat);
1922         printf("\n  %s FDIR infos for port %-2d     %s\n",
1923                fdir_stats_border, port_id, fdir_stats_border);
1924         printf("  MODE: ");
1925         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
1926                 printf("  PERFECT\n");
1927         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
1928                 printf("  SIGNATURE\n");
1929         else
1930                 printf("  DISABLE\n");
1931         printf("  SUPPORTED FLOW TYPE: ");
1932         print_fdir_flow_type(fdir_info.flow_types_mask[0]);
1933         printf("  FLEX PAYLOAD INFO:\n");
1934         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
1935                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
1936                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
1937                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
1938                 fdir_info.flex_payload_unit,
1939                 fdir_info.max_flex_payload_segment_num,
1940                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
1941         printf("  MASK: ");
1942         print_fdir_mask(&fdir_info.mask);
1943         if (fdir_info.flex_conf.nb_payloads > 0) {
1944                 printf("  FLEX PAYLOAD SRC OFFSET:");
1945                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
1946         }
1947         if (fdir_info.flex_conf.nb_flexmasks > 0) {
1948                 printf("  FLEX MASK CFG:");
1949                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
1950         }
1951         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
1952                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
1953         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
1954                fdir_info.guarant_spc, fdir_info.best_spc);
1955         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
1956                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
1957                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
1958                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
1959                fdir_stat.collision, fdir_stat.free,
1960                fdir_stat.maxhash, fdir_stat.maxlen,
1961                fdir_stat.add, fdir_stat.remove,
1962                fdir_stat.f_add, fdir_stat.f_remove);
1963         printf("  %s############################%s\n",
1964                fdir_stats_border, fdir_stats_border);
1965 }
1966
1967 void
1968 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
1969 {
1970         struct rte_port *port;
1971         struct rte_eth_fdir_flex_conf *flex_conf;
1972         int i, idx = 0;
1973
1974         port = &ports[port_id];
1975         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
1976         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
1977                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
1978                         idx = i;
1979                         break;
1980                 }
1981         }
1982         if (i >= RTE_ETH_FLOW_MAX) {
1983                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
1984                         idx = flex_conf->nb_flexmasks;
1985                         flex_conf->nb_flexmasks++;
1986                 } else {
1987                         printf("The flex mask table is full. Can not set flex"
1988                                 " mask for flow_type(%u).", cfg->flow_type);
1989                         return;
1990                 }
1991         }
1992         (void)rte_memcpy(&flex_conf->flex_mask[idx],
1993                          cfg,
1994                          sizeof(struct rte_eth_fdir_flex_mask));
1995 }
1996
1997 void
1998 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
1999 {
2000         struct rte_port *port;
2001         struct rte_eth_fdir_flex_conf *flex_conf;
2002         int i, idx = 0;
2003
2004         port = &ports[port_id];
2005         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2006         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
2007                 if (cfg->type == flex_conf->flex_set[i].type) {
2008                         idx = i;
2009                         break;
2010                 }
2011         }
2012         if (i >= RTE_ETH_PAYLOAD_MAX) {
2013                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
2014                         idx = flex_conf->nb_payloads;
2015                         flex_conf->nb_payloads++;
2016                 } else {
2017                         printf("The flex payload table is full. Can not set"
2018                                 " flex payload for type(%u).", cfg->type);
2019                         return;
2020                 }
2021         }
2022         (void)rte_memcpy(&flex_conf->flex_set[idx],
2023                          cfg,
2024                          sizeof(struct rte_eth_flex_payload_cfg));
2025
2026 }
2027
2028 void
2029 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
2030 {
2031         int diag;
2032
2033         if (port_id_is_invalid(port_id))
2034                 return;
2035         if (is_rx)
2036                 diag = rte_eth_dev_set_vf_rx(port_id,vf,on);
2037         else
2038                 diag = rte_eth_dev_set_vf_tx(port_id,vf,on);
2039         if (diag == 0)
2040                 return;
2041         if(is_rx)
2042                 printf("rte_eth_dev_set_vf_rx for port_id=%d failed "
2043                         "diag=%d\n", port_id, diag);
2044         else
2045                 printf("rte_eth_dev_set_vf_tx for port_id=%d failed "
2046                         "diag=%d\n", port_id, diag);
2047
2048 }
2049
2050 void
2051 set_vf_rx_vlan(portid_t port_id, uint16_t vlan_id, uint64_t vf_mask, uint8_t on)
2052 {
2053         int diag;
2054
2055         if (port_id_is_invalid(port_id))
2056                 return;
2057         if (vlan_id_is_invalid(vlan_id))
2058                 return;
2059         diag = rte_eth_dev_set_vf_vlan_filter(port_id, vlan_id, vf_mask, on);
2060         if (diag == 0)
2061                 return;
2062         printf("rte_eth_dev_set_vf_vlan_filter for port_id=%d failed "
2063                "diag=%d\n", port_id, diag);
2064 }
2065
2066 int
2067 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
2068 {
2069         int diag;
2070         struct rte_eth_link link;
2071
2072         if (port_id_is_invalid(port_id))
2073                 return 1;
2074         rte_eth_link_get_nowait(port_id, &link);
2075         if (rate > link.link_speed) {
2076                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2077                         rate, link.link_speed);
2078                 return 1;
2079         }
2080         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
2081         if (diag == 0)
2082                 return diag;
2083         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
2084                 port_id, diag);
2085         return diag;
2086 }
2087
2088 int
2089 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
2090 {
2091         int diag;
2092         struct rte_eth_link link;
2093
2094         if (q_msk == 0)
2095                 return 0;
2096
2097         if (port_id_is_invalid(port_id))
2098                 return 1;
2099         rte_eth_link_get_nowait(port_id, &link);
2100         if (rate > link.link_speed) {
2101                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2102                         rate, link.link_speed);
2103                 return 1;
2104         }
2105         diag = rte_eth_set_vf_rate_limit(port_id, vf, rate, q_msk);
2106         if (diag == 0)
2107                 return diag;
2108         printf("rte_eth_set_vf_rate_limit for port_id=%d failed diag=%d\n",
2109                 port_id, diag);
2110         return diag;
2111 }