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