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