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