71b34dd786f89b000fba4a9b2a39df0d916f8acc
[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                 uint32_t hi;
632                 uint32_t lo;
633         } words;
634 };
635
636 struct igb_ring_desc_32_bytes {
637         union igb_ring_dword lo_dword;
638         union igb_ring_dword hi_dword;
639         union igb_ring_dword resv1;
640         union igb_ring_dword resv2;
641 };
642
643 struct igb_ring_desc_16_bytes {
644         union igb_ring_dword lo_dword;
645         union igb_ring_dword hi_dword;
646 };
647
648 static void
649 ring_rxd_display_dword(union igb_ring_dword dword)
650 {
651         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
652                                         (unsigned)dword.words.hi);
653 }
654
655 static void
656 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
657 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
658                            uint8_t port_id,
659 #else
660                            __rte_unused uint8_t port_id,
661 #endif
662                            uint16_t desc_id)
663 {
664         struct igb_ring_desc_16_bytes *ring =
665                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
666 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
667         struct rte_eth_dev_info dev_info;
668
669         memset(&dev_info, 0, sizeof(dev_info));
670         rte_eth_dev_info_get(port_id, &dev_info);
671         if (strstr(dev_info.driver_name, "i40e") != NULL) {
672                 /* 32 bytes RX descriptor, i40e only */
673                 struct igb_ring_desc_32_bytes *ring =
674                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
675
676                 ring_rxd_display_dword(rte_le_to_cpu_64(
677                                 ring[desc_id].lo_dword));
678                 ring_rxd_display_dword(rte_le_to_cpu_64(
679                                 ring[desc_id].hi_dword));
680                 ring_rxd_display_dword(rte_le_to_cpu_64(
681                                 ring[desc_id].resv1));
682                 ring_rxd_display_dword(rte_le_to_cpu_64(
683                                 ring[desc_id].resv2));
684                 return;
685         }
686 #endif
687         /* 16 bytes RX descriptor */
688         ring_rxd_display_dword(rte_le_to_cpu_64(
689                         ring[desc_id].lo_dword));
690         ring_rxd_display_dword(rte_le_to_cpu_64(
691                         ring[desc_id].hi_dword));
692 }
693
694 static void
695 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
696 {
697         struct igb_ring_desc_16_bytes *ring;
698         struct igb_ring_desc_16_bytes txd;
699
700         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
701         txd.lo_dword = rte_le_to_cpu_64(ring[desc_id].lo_dword);
702         txd.hi_dword = rte_le_to_cpu_64(ring[desc_id].hi_dword);
703         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
704                         (unsigned)txd.lo_dword.words.lo,
705                         (unsigned)txd.lo_dword.words.hi,
706                         (unsigned)txd.hi_dword.words.lo,
707                         (unsigned)txd.hi_dword.words.hi);
708 }
709
710 void
711 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
712 {
713         const struct rte_memzone *rx_mz;
714
715         if (port_id_is_invalid(port_id))
716                 return;
717         if (rx_queue_id_is_invalid(rxq_id))
718                 return;
719         if (rx_desc_id_is_invalid(rxd_id))
720                 return;
721         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
722         if (rx_mz == NULL)
723                 return;
724         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
725 }
726
727 void
728 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
729 {
730         const struct rte_memzone *tx_mz;
731
732         if (port_id_is_invalid(port_id))
733                 return;
734         if (tx_queue_id_is_invalid(txq_id))
735                 return;
736         if (tx_desc_id_is_invalid(txd_id))
737                 return;
738         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
739         if (tx_mz == NULL)
740                 return;
741         ring_tx_descriptor_display(tx_mz, txd_id);
742 }
743
744 void
745 fwd_lcores_config_display(void)
746 {
747         lcoreid_t lc_id;
748
749         printf("List of forwarding lcores:");
750         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
751                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
752         printf("\n");
753 }
754 void
755 rxtx_config_display(void)
756 {
757         printf("  %s packet forwarding - CRC stripping %s - "
758                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
759                rx_mode.hw_strip_crc ? "enabled" : "disabled",
760                nb_pkt_per_burst);
761
762         if (cur_fwd_eng == &tx_only_engine)
763                 printf("  packet len=%u - nb packet segments=%d\n",
764                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
765
766         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
767                nb_fwd_lcores, nb_fwd_ports);
768         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
769                nb_rxq, nb_rxd, rx_free_thresh);
770         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
771                rx_thresh.pthresh, rx_thresh.hthresh, rx_thresh.wthresh);
772         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
773                nb_txq, nb_txd, tx_free_thresh);
774         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
775                tx_thresh.pthresh, tx_thresh.hthresh, tx_thresh.wthresh);
776         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
777                tx_rs_thresh, txq_flags);
778 }
779
780 void
781 port_rss_reta_info(portid_t port_id,
782                    struct rte_eth_rss_reta_entry64 *reta_conf,
783                    uint16_t nb_entries)
784 {
785         uint16_t i, idx, shift;
786         int ret;
787
788         if (port_id_is_invalid(port_id))
789                 return;
790
791         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
792         if (ret != 0) {
793                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
794                 return;
795         }
796
797         for (i = 0; i < nb_entries; i++) {
798                 idx = i / RTE_RETA_GROUP_SIZE;
799                 shift = i % RTE_RETA_GROUP_SIZE;
800                 if (!(reta_conf[idx].mask & (1ULL << shift)))
801                         continue;
802                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
803                                         i, reta_conf[idx].reta[shift]);
804         }
805 }
806
807 /*
808  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
809  * key of the port.
810  */
811 void
812 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
813 {
814         struct rte_eth_rss_conf rss_conf;
815         uint8_t rss_key[10 * 4];
816         uint16_t rss_hf;
817         uint8_t i;
818         int diag;
819
820         if (port_id_is_invalid(port_id))
821                 return;
822         /* Get RSS hash key if asked to display it */
823         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
824         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
825         if (diag != 0) {
826                 switch (diag) {
827                 case -ENODEV:
828                         printf("port index %d invalid\n", port_id);
829                         break;
830                 case -ENOTSUP:
831                         printf("operation not supported by device\n");
832                         break;
833                 default:
834                         printf("operation failed - diag=%d\n", diag);
835                         break;
836                 }
837                 return;
838         }
839         rss_hf = rss_conf.rss_hf;
840         if (rss_hf == 0) {
841                 printf("RSS disabled\n");
842                 return;
843         }
844         printf("RSS functions:\n ");
845         if (rss_hf & ETH_RSS_IPV4)
846                 printf("ip4");
847         if (rss_hf & ETH_RSS_IPV4_TCP)
848                 printf(" tcp4");
849         if (rss_hf & ETH_RSS_IPV4_UDP)
850                 printf(" udp4");
851         if (rss_hf & ETH_RSS_IPV6)
852                 printf(" ip6");
853         if (rss_hf & ETH_RSS_IPV6_EX)
854                 printf(" ip6-ex");
855         if (rss_hf & ETH_RSS_IPV6_TCP)
856                 printf(" tcp6");
857         if (rss_hf & ETH_RSS_IPV6_TCP_EX)
858                 printf(" tcp6-ex");
859         if (rss_hf & ETH_RSS_IPV6_UDP)
860                 printf(" udp6");
861         if (rss_hf & ETH_RSS_IPV6_UDP_EX)
862                 printf(" udp6-ex");
863         printf("\n");
864         if (!show_rss_key)
865                 return;
866         printf("RSS key:\n");
867         for (i = 0; i < sizeof(rss_key); i++)
868                 printf("%02X", rss_key[i]);
869         printf("\n");
870 }
871
872 void
873 port_rss_hash_key_update(portid_t port_id, uint8_t *hash_key)
874 {
875         struct rte_eth_rss_conf rss_conf;
876         int diag;
877
878         rss_conf.rss_key = NULL;
879         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
880         if (diag == 0) {
881                 rss_conf.rss_key = hash_key;
882                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
883         }
884         if (diag == 0)
885                 return;
886
887         switch (diag) {
888         case -ENODEV:
889                 printf("port index %d invalid\n", port_id);
890                 break;
891         case -ENOTSUP:
892                 printf("operation not supported by device\n");
893                 break;
894         default:
895                 printf("operation failed - diag=%d\n", diag);
896                 break;
897         }
898 }
899
900 /*
901  * Setup forwarding configuration for each logical core.
902  */
903 static void
904 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
905 {
906         streamid_t nb_fs_per_lcore;
907         streamid_t nb_fs;
908         streamid_t sm_id;
909         lcoreid_t  nb_extra;
910         lcoreid_t  nb_fc;
911         lcoreid_t  nb_lc;
912         lcoreid_t  lc_id;
913
914         nb_fs = cfg->nb_fwd_streams;
915         nb_fc = cfg->nb_fwd_lcores;
916         if (nb_fs <= nb_fc) {
917                 nb_fs_per_lcore = 1;
918                 nb_extra = 0;
919         } else {
920                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
921                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
922         }
923
924         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
925         sm_id = 0;
926         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
927                 fwd_lcores[lc_id]->stream_idx = sm_id;
928                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
929                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
930         }
931
932         /*
933          * Assign extra remaining streams, if any.
934          */
935         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
936         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
937                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
938                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
939                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
940         }
941 }
942
943 static void
944 simple_fwd_config_setup(void)
945 {
946         portid_t i;
947         portid_t j;
948         portid_t inc = 2;
949
950         if (port_topology == PORT_TOPOLOGY_CHAINED ||
951             port_topology == PORT_TOPOLOGY_LOOP) {
952                 inc = 1;
953         } else if (nb_fwd_ports % 2) {
954                 printf("\nWarning! Cannot handle an odd number of ports "
955                        "with the current port topology. Configuration "
956                        "must be changed to have an even number of ports, "
957                        "or relaunch application with "
958                        "--port-topology=chained\n\n");
959         }
960
961         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
962         cur_fwd_config.nb_fwd_streams =
963                 (streamid_t) cur_fwd_config.nb_fwd_ports;
964
965         /* reinitialize forwarding streams */
966         init_fwd_streams();
967
968         /*
969          * In the simple forwarding test, the number of forwarding cores
970          * must be lower or equal to the number of forwarding ports.
971          */
972         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
973         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
974                 cur_fwd_config.nb_fwd_lcores =
975                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
976         setup_fwd_config_of_each_lcore(&cur_fwd_config);
977
978         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
979                 if (port_topology != PORT_TOPOLOGY_LOOP)
980                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
981                 else
982                         j = i;
983                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
984                 fwd_streams[i]->rx_queue  = 0;
985                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
986                 fwd_streams[i]->tx_queue  = 0;
987                 fwd_streams[i]->peer_addr = j;
988
989                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
990                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
991                         fwd_streams[j]->rx_queue  = 0;
992                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
993                         fwd_streams[j]->tx_queue  = 0;
994                         fwd_streams[j]->peer_addr = i;
995                 }
996         }
997 }
998
999 /**
1000  * For the RSS forwarding test, each core is assigned on every port a transmit
1001  * queue whose index is the index of the core itself. This approach limits the
1002  * maximumm number of processing cores of the RSS test to the maximum number of
1003  * TX queues supported by the devices.
1004  *
1005  * Each core is assigned a single stream, each stream being composed of
1006  * a RX queue to poll on a RX port for input messages, associated with
1007  * a TX queue of a TX port where to send forwarded packets.
1008  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1009  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1010  * following rules:
1011  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1012  *    - TxQl = RxQj
1013  */
1014 static void
1015 rss_fwd_config_setup(void)
1016 {
1017         portid_t   rxp;
1018         portid_t   txp;
1019         queueid_t  rxq;
1020         queueid_t  nb_q;
1021         lcoreid_t  lc_id;
1022
1023         nb_q = nb_rxq;
1024         if (nb_q > nb_txq)
1025                 nb_q = nb_txq;
1026         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1027         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1028         cur_fwd_config.nb_fwd_streams =
1029                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1030         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
1031                 cur_fwd_config.nb_fwd_streams =
1032                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
1033         else
1034                 cur_fwd_config.nb_fwd_lcores =
1035                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1036
1037         /* reinitialize forwarding streams */
1038         init_fwd_streams();
1039
1040         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1041         rxp = 0; rxq = 0;
1042         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1043                 struct fwd_stream *fs;
1044
1045                 fs = fwd_streams[lc_id];
1046
1047                 if ((rxp & 0x1) == 0)
1048                         txp = (portid_t) (rxp + 1);
1049                 else
1050                         txp = (portid_t) (rxp - 1);
1051                 /*
1052                  * if we are in loopback, simply send stuff out through the
1053                  * ingress port
1054                  */
1055                 if (port_topology == PORT_TOPOLOGY_LOOP)
1056                         txp = rxp;
1057
1058                 fs->rx_port = fwd_ports_ids[rxp];
1059                 fs->rx_queue = rxq;
1060                 fs->tx_port = fwd_ports_ids[txp];
1061                 fs->tx_queue = rxq;
1062                 fs->peer_addr = fs->tx_port;
1063                 rxq = (queueid_t) (rxq + 1);
1064                 if (rxq < nb_q)
1065                         continue;
1066                 /*
1067                  * rxq == nb_q
1068                  * Restart from RX queue 0 on next RX port
1069                  */
1070                 rxq = 0;
1071                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1072                         rxp = (portid_t)
1073                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1074                 else
1075                         rxp = (portid_t) (rxp + 1);
1076         }
1077 }
1078
1079 /*
1080  * In DCB and VT on,the mapping of 128 receive queues to 128 transmit queues.
1081  */
1082 static void
1083 dcb_rxq_2_txq_mapping(queueid_t rxq, queueid_t *txq)
1084 {
1085         if(dcb_q_mapping == DCB_4_TCS_Q_MAPPING) {
1086
1087                 if (rxq < 32)
1088                         /* tc0: 0-31 */
1089                         *txq = rxq;
1090                 else if (rxq < 64) {
1091                         /* tc1: 64-95 */
1092                         *txq =  (uint16_t)(rxq + 32);
1093                 }
1094                 else {
1095                         /* tc2: 96-111;tc3:112-127 */
1096                         *txq =  (uint16_t)(rxq/2 + 64);
1097                 }
1098         }
1099         else {
1100                 if (rxq < 16)
1101                         /* tc0 mapping*/
1102                         *txq = rxq;
1103                 else if (rxq < 32) {
1104                         /* tc1 mapping*/
1105                          *txq = (uint16_t)(rxq + 16);
1106                 }
1107                 else if (rxq < 64) {
1108                         /*tc2,tc3 mapping */
1109                         *txq =  (uint16_t)(rxq + 32);
1110                 }
1111                 else {
1112                         /* tc4,tc5,tc6 and tc7 mapping */
1113                         *txq =  (uint16_t)(rxq/2 + 64);
1114                 }
1115         }
1116 }
1117
1118 /**
1119  * For the DCB forwarding test, each core is assigned on every port multi-transmit
1120  * queue.
1121  *
1122  * Each core is assigned a multi-stream, each stream being composed of
1123  * a RX queue to poll on a RX port for input messages, associated with
1124  * a TX queue of a TX port where to send forwarded packets.
1125  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1126  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1127  * following rules:
1128  * In VT mode,
1129  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1130  *    - TxQl = RxQj
1131  * In non-VT mode,
1132  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1133  *    There is a mapping of RxQj to TxQl to be required,and the mapping was implemented
1134  *    in dcb_rxq_2_txq_mapping function.
1135  */
1136 static void
1137 dcb_fwd_config_setup(void)
1138 {
1139         portid_t   rxp;
1140         portid_t   txp;
1141         queueid_t  rxq;
1142         queueid_t  nb_q;
1143         lcoreid_t  lc_id;
1144         uint16_t sm_id;
1145
1146         nb_q = nb_rxq;
1147
1148         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1149         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1150         cur_fwd_config.nb_fwd_streams =
1151                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1152
1153         /* reinitialize forwarding streams */
1154         init_fwd_streams();
1155
1156         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1157         rxp = 0; rxq = 0;
1158         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1159                 /* a fwd core can run multi-streams */
1160                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++)
1161                 {
1162                         struct fwd_stream *fs;
1163                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1164                         if ((rxp & 0x1) == 0)
1165                                 txp = (portid_t) (rxp + 1);
1166                         else
1167                                 txp = (portid_t) (rxp - 1);
1168                         fs->rx_port = fwd_ports_ids[rxp];
1169                         fs->rx_queue = rxq;
1170                         fs->tx_port = fwd_ports_ids[txp];
1171                         if (dcb_q_mapping == DCB_VT_Q_MAPPING)
1172                                 fs->tx_queue = rxq;
1173                         else
1174                                 dcb_rxq_2_txq_mapping(rxq, &fs->tx_queue);
1175                         fs->peer_addr = fs->tx_port;
1176                         rxq = (queueid_t) (rxq + 1);
1177                         if (rxq < nb_q)
1178                                 continue;
1179                         rxq = 0;
1180                         if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1181                                 rxp = (portid_t)
1182                                         (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1183                         else
1184                                 rxp = (portid_t) (rxp + 1);
1185                 }
1186         }
1187 }
1188
1189 static void
1190 icmp_echo_config_setup(void)
1191 {
1192         portid_t  rxp;
1193         queueid_t rxq;
1194         lcoreid_t lc_id;
1195         uint16_t  sm_id;
1196
1197         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
1198                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
1199                         (nb_txq * nb_fwd_ports);
1200         else
1201                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1202         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1203         cur_fwd_config.nb_fwd_streams =
1204                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
1205         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1206                 cur_fwd_config.nb_fwd_lcores =
1207                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1208         if (verbose_level > 0) {
1209                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
1210                        __FUNCTION__,
1211                        cur_fwd_config.nb_fwd_lcores,
1212                        cur_fwd_config.nb_fwd_ports,
1213                        cur_fwd_config.nb_fwd_streams);
1214         }
1215
1216         /* reinitialize forwarding streams */
1217         init_fwd_streams();
1218         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1219         rxp = 0; rxq = 0;
1220         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1221                 if (verbose_level > 0)
1222                         printf("  core=%d: \n", lc_id);
1223                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1224                         struct fwd_stream *fs;
1225                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1226                         fs->rx_port = fwd_ports_ids[rxp];
1227                         fs->rx_queue = rxq;
1228                         fs->tx_port = fs->rx_port;
1229                         fs->tx_queue = lc_id;
1230                         fs->peer_addr = fs->tx_port;
1231                         if (verbose_level > 0)
1232                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
1233                                        sm_id, fs->rx_port, fs->rx_queue,
1234                                        fs->tx_queue);
1235                         rxq = (queueid_t) (rxq + 1);
1236                         if (rxq == nb_rxq) {
1237                                 rxq = 0;
1238                                 rxp = (portid_t) (rxp + 1);
1239                         }
1240                 }
1241         }
1242 }
1243
1244 void
1245 fwd_config_setup(void)
1246 {
1247         cur_fwd_config.fwd_eng = cur_fwd_eng;
1248         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
1249                 icmp_echo_config_setup();
1250                 return;
1251         }
1252         if ((nb_rxq > 1) && (nb_txq > 1)){
1253                 if (dcb_config)
1254                         dcb_fwd_config_setup();
1255                 else
1256                         rss_fwd_config_setup();
1257         }
1258         else
1259                 simple_fwd_config_setup();
1260 }
1261
1262 static void
1263 pkt_fwd_config_display(struct fwd_config *cfg)
1264 {
1265         struct fwd_stream *fs;
1266         lcoreid_t  lc_id;
1267         streamid_t sm_id;
1268
1269         printf("%s packet forwarding - ports=%d - cores=%d - streams=%d - "
1270                 "NUMA support %s, MP over anonymous pages %s\n",
1271                 cfg->fwd_eng->fwd_mode_name,
1272                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
1273                 numa_support == 1 ? "enabled" : "disabled",
1274                 mp_anon != 0 ? "enabled" : "disabled");
1275
1276         if (strcmp(cfg->fwd_eng->fwd_mode_name, "mac_retry") == 0)
1277                 printf("TX retry num: %u, delay between TX retries: %uus\n",
1278                         burst_tx_retry_num, burst_tx_delay_time);
1279         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
1280                 printf("Logical Core %u (socket %u) forwards packets on "
1281                        "%d streams:",
1282                        fwd_lcores_cpuids[lc_id],
1283                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
1284                        fwd_lcores[lc_id]->stream_nb);
1285                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1286                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1287                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
1288                                "P=%d/Q=%d (socket %u) ",
1289                                fs->rx_port, fs->rx_queue,
1290                                ports[fs->rx_port].socket_id,
1291                                fs->tx_port, fs->tx_queue,
1292                                ports[fs->tx_port].socket_id);
1293                         print_ethaddr("peer=",
1294                                       &peer_eth_addrs[fs->peer_addr]);
1295                 }
1296                 printf("\n");
1297         }
1298         printf("\n");
1299 }
1300
1301
1302 void
1303 fwd_config_display(void)
1304 {
1305         if((dcb_config) && (nb_fwd_lcores == 1)) {
1306                 printf("In DCB mode,the nb forwarding cores should be larger than 1\n");
1307                 return;
1308         }
1309         fwd_config_setup();
1310         pkt_fwd_config_display(&cur_fwd_config);
1311 }
1312
1313 int
1314 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
1315 {
1316         unsigned int i;
1317         unsigned int lcore_cpuid;
1318         int record_now;
1319
1320         record_now = 0;
1321  again:
1322         for (i = 0; i < nb_lc; i++) {
1323                 lcore_cpuid = lcorelist[i];
1324                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
1325                         printf("lcore %u not enabled\n", lcore_cpuid);
1326                         return -1;
1327                 }
1328                 if (lcore_cpuid == rte_get_master_lcore()) {
1329                         printf("lcore %u cannot be masked on for running "
1330                                "packet forwarding, which is the master lcore "
1331                                "and reserved for command line parsing only\n",
1332                                lcore_cpuid);
1333                         return -1;
1334                 }
1335                 if (record_now)
1336                         fwd_lcores_cpuids[i] = lcore_cpuid;
1337         }
1338         if (record_now == 0) {
1339                 record_now = 1;
1340                 goto again;
1341         }
1342         nb_cfg_lcores = (lcoreid_t) nb_lc;
1343         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
1344                 printf("previous number of forwarding cores %u - changed to "
1345                        "number of configured cores %u\n",
1346                        (unsigned int) nb_fwd_lcores, nb_lc);
1347                 nb_fwd_lcores = (lcoreid_t) nb_lc;
1348         }
1349
1350         return 0;
1351 }
1352
1353 int
1354 set_fwd_lcores_mask(uint64_t lcoremask)
1355 {
1356         unsigned int lcorelist[64];
1357         unsigned int nb_lc;
1358         unsigned int i;
1359
1360         if (lcoremask == 0) {
1361                 printf("Invalid NULL mask of cores\n");
1362                 return -1;
1363         }
1364         nb_lc = 0;
1365         for (i = 0; i < 64; i++) {
1366                 if (! ((uint64_t)(1ULL << i) & lcoremask))
1367                         continue;
1368                 lcorelist[nb_lc++] = i;
1369         }
1370         return set_fwd_lcores_list(lcorelist, nb_lc);
1371 }
1372
1373 void
1374 set_fwd_lcores_number(uint16_t nb_lc)
1375 {
1376         if (nb_lc > nb_cfg_lcores) {
1377                 printf("nb fwd cores %u > %u (max. number of configured "
1378                        "lcores) - ignored\n",
1379                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
1380                 return;
1381         }
1382         nb_fwd_lcores = (lcoreid_t) nb_lc;
1383         printf("Number of forwarding cores set to %u\n",
1384                (unsigned int) nb_fwd_lcores);
1385 }
1386
1387 void
1388 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
1389 {
1390         unsigned int i;
1391         portid_t port_id;
1392         int record_now;
1393
1394         record_now = 0;
1395  again:
1396         for (i = 0; i < nb_pt; i++) {
1397                 port_id = (portid_t) portlist[i];
1398                 if (port_id >= nb_ports) {
1399                         printf("Invalid port id %u >= %u\n",
1400                                (unsigned int) port_id,
1401                                (unsigned int) nb_ports);
1402                         return;
1403                 }
1404                 if (record_now)
1405                         fwd_ports_ids[i] = port_id;
1406         }
1407         if (record_now == 0) {
1408                 record_now = 1;
1409                 goto again;
1410         }
1411         nb_cfg_ports = (portid_t) nb_pt;
1412         if (nb_fwd_ports != (portid_t) nb_pt) {
1413                 printf("previous number of forwarding ports %u - changed to "
1414                        "number of configured ports %u\n",
1415                        (unsigned int) nb_fwd_ports, nb_pt);
1416                 nb_fwd_ports = (portid_t) nb_pt;
1417         }
1418 }
1419
1420 void
1421 set_fwd_ports_mask(uint64_t portmask)
1422 {
1423         unsigned int portlist[64];
1424         unsigned int nb_pt;
1425         unsigned int i;
1426
1427         if (portmask == 0) {
1428                 printf("Invalid NULL mask of ports\n");
1429                 return;
1430         }
1431         nb_pt = 0;
1432         for (i = 0; i < 64; i++) {
1433                 if (! ((uint64_t)(1ULL << i) & portmask))
1434                         continue;
1435                 portlist[nb_pt++] = i;
1436         }
1437         set_fwd_ports_list(portlist, nb_pt);
1438 }
1439
1440 void
1441 set_fwd_ports_number(uint16_t nb_pt)
1442 {
1443         if (nb_pt > nb_cfg_ports) {
1444                 printf("nb fwd ports %u > %u (number of configured "
1445                        "ports) - ignored\n",
1446                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
1447                 return;
1448         }
1449         nb_fwd_ports = (portid_t) nb_pt;
1450         printf("Number of forwarding ports set to %u\n",
1451                (unsigned int) nb_fwd_ports);
1452 }
1453
1454 void
1455 set_nb_pkt_per_burst(uint16_t nb)
1456 {
1457         if (nb > MAX_PKT_BURST) {
1458                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
1459                        " ignored\n",
1460                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
1461                 return;
1462         }
1463         nb_pkt_per_burst = nb;
1464         printf("Number of packets per burst set to %u\n",
1465                (unsigned int) nb_pkt_per_burst);
1466 }
1467
1468 void
1469 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
1470 {
1471         uint16_t tx_pkt_len;
1472         unsigned i;
1473
1474         if (nb_segs >= (unsigned) nb_txd) {
1475                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
1476                        nb_segs, (unsigned int) nb_txd);
1477                 return;
1478         }
1479
1480         /*
1481          * Check that each segment length is greater or equal than
1482          * the mbuf data sise.
1483          * Check also that the total packet length is greater or equal than the
1484          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
1485          */
1486         tx_pkt_len = 0;
1487         for (i = 0; i < nb_segs; i++) {
1488                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
1489                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
1490                                i, seg_lengths[i], (unsigned) mbuf_data_size);
1491                         return;
1492                 }
1493                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
1494         }
1495         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
1496                 printf("total packet length=%u < %d - give up\n",
1497                                 (unsigned) tx_pkt_len,
1498                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
1499                 return;
1500         }
1501
1502         for (i = 0; i < nb_segs; i++)
1503                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
1504
1505         tx_pkt_length  = tx_pkt_len;
1506         tx_pkt_nb_segs = (uint8_t) nb_segs;
1507 }
1508
1509 char*
1510 list_pkt_forwarding_modes(void)
1511 {
1512         static char fwd_modes[128] = "";
1513         const char *separator = "|";
1514         struct fwd_engine *fwd_eng;
1515         unsigned i = 0;
1516
1517         if (strlen (fwd_modes) == 0) {
1518                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1519                         strcat(fwd_modes, fwd_eng->fwd_mode_name);
1520                         strcat(fwd_modes, separator);
1521                 }
1522                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1523         }
1524
1525         return fwd_modes;
1526 }
1527
1528 void
1529 set_pkt_forwarding_mode(const char *fwd_mode_name)
1530 {
1531         struct fwd_engine *fwd_eng;
1532         unsigned i;
1533
1534         i = 0;
1535         while ((fwd_eng = fwd_engines[i]) != NULL) {
1536                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
1537                         printf("Set %s packet forwarding mode\n",
1538                                fwd_mode_name);
1539                         cur_fwd_eng = fwd_eng;
1540                         return;
1541                 }
1542                 i++;
1543         }
1544         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
1545 }
1546
1547 void
1548 set_verbose_level(uint16_t vb_level)
1549 {
1550         printf("Change verbose level from %u to %u\n",
1551                (unsigned int) verbose_level, (unsigned int) vb_level);
1552         verbose_level = vb_level;
1553 }
1554
1555 void
1556 vlan_extend_set(portid_t port_id, int on)
1557 {
1558         int diag;
1559         int vlan_offload;
1560
1561         if (port_id_is_invalid(port_id))
1562                 return;
1563
1564         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1565
1566         if (on)
1567                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
1568         else
1569                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
1570
1571         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1572         if (diag < 0)
1573                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
1574                "diag=%d\n", port_id, on, diag);
1575 }
1576
1577 void
1578 rx_vlan_strip_set(portid_t port_id, int on)
1579 {
1580         int diag;
1581         int vlan_offload;
1582
1583         if (port_id_is_invalid(port_id))
1584                 return;
1585
1586         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1587
1588         if (on)
1589                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
1590         else
1591                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
1592
1593         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1594         if (diag < 0)
1595                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
1596                "diag=%d\n", port_id, on, diag);
1597 }
1598
1599 void
1600 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
1601 {
1602         int diag;
1603
1604         if (port_id_is_invalid(port_id))
1605                 return;
1606
1607         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
1608         if (diag < 0)
1609                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
1610                "diag=%d\n", port_id, queue_id, on, diag);
1611 }
1612
1613 void
1614 rx_vlan_filter_set(portid_t port_id, int on)
1615 {
1616         int diag;
1617         int vlan_offload;
1618
1619         if (port_id_is_invalid(port_id))
1620                 return;
1621
1622         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1623
1624         if (on)
1625                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1626         else
1627                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
1628
1629         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1630         if (diag < 0)
1631                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
1632                "diag=%d\n", port_id, on, diag);
1633 }
1634
1635 void
1636 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
1637 {
1638         int diag;
1639
1640         if (port_id_is_invalid(port_id))
1641                 return;
1642         if (vlan_id_is_invalid(vlan_id))
1643                 return;
1644         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
1645         if (diag == 0)
1646                 return;
1647         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
1648                "diag=%d\n",
1649                port_id, vlan_id, on, diag);
1650 }
1651
1652 void
1653 rx_vlan_all_filter_set(portid_t port_id, int on)
1654 {
1655         uint16_t vlan_id;
1656
1657         if (port_id_is_invalid(port_id))
1658                 return;
1659         for (vlan_id = 0; vlan_id < 4096; vlan_id++)
1660                 rx_vft_set(port_id, vlan_id, on);
1661 }
1662
1663 void
1664 vlan_tpid_set(portid_t port_id, uint16_t tp_id)
1665 {
1666         int diag;
1667         if (port_id_is_invalid(port_id))
1668                 return;
1669
1670         diag = rte_eth_dev_set_vlan_ether_type(port_id, tp_id);
1671         if (diag == 0)
1672                 return;
1673
1674         printf("tx_vlan_tpid_set(port_pi=%d, tpid=%d) failed "
1675                "diag=%d\n",
1676                port_id, tp_id, diag);
1677 }
1678
1679 void
1680 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
1681 {
1682         if (port_id_is_invalid(port_id))
1683                 return;
1684         if (vlan_id_is_invalid(vlan_id))
1685                 return;
1686         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1687         ports[port_id].tx_vlan_id = vlan_id;
1688 }
1689
1690 void
1691 tx_vlan_reset(portid_t port_id)
1692 {
1693         if (port_id_is_invalid(port_id))
1694                 return;
1695         ports[port_id].tx_ol_flags &= ~TESTPMD_TX_OFFLOAD_INSERT_VLAN;
1696 }
1697
1698 void
1699 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
1700 {
1701         if (port_id_is_invalid(port_id))
1702                 return;
1703
1704         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
1705 }
1706
1707 void
1708 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
1709 {
1710         uint16_t i;
1711         uint8_t existing_mapping_found = 0;
1712
1713         if (port_id_is_invalid(port_id))
1714                 return;
1715
1716         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
1717                 return;
1718
1719         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1720                 printf("map_value not in required range 0..%d\n",
1721                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1722                 return;
1723         }
1724
1725         if (!is_rx) { /*then tx*/
1726                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1727                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1728                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
1729                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
1730                                 existing_mapping_found = 1;
1731                                 break;
1732                         }
1733                 }
1734                 if (!existing_mapping_found) { /* A new additional mapping... */
1735                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
1736                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
1737                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
1738                         nb_tx_queue_stats_mappings++;
1739                 }
1740         }
1741         else { /*rx*/
1742                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1743                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1744                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
1745                                 rx_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                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
1752                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
1753                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
1754                         nb_rx_queue_stats_mappings++;
1755                 }
1756         }
1757 }
1758
1759 void
1760 fdir_add_signature_filter(portid_t port_id, uint8_t queue_id,
1761                           struct rte_fdir_filter *fdir_filter)
1762 {
1763         int diag;
1764
1765         if (port_id_is_invalid(port_id))
1766                 return;
1767
1768         diag = rte_eth_dev_fdir_add_signature_filter(port_id, fdir_filter,
1769                                                      queue_id);
1770         if (diag == 0)
1771                 return;
1772
1773         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1774                "diag=%d\n", port_id, diag);
1775 }
1776
1777 void
1778 fdir_update_signature_filter(portid_t port_id, uint8_t queue_id,
1779                              struct rte_fdir_filter *fdir_filter)
1780 {
1781         int diag;
1782
1783         if (port_id_is_invalid(port_id))
1784                 return;
1785
1786         diag = rte_eth_dev_fdir_update_signature_filter(port_id, fdir_filter,
1787                                                         queue_id);
1788         if (diag == 0)
1789                 return;
1790
1791         printf("rte_eth_dev_fdir_update_signature_filter for port_id=%d failed "
1792                "diag=%d\n", port_id, diag);
1793 }
1794
1795 void
1796 fdir_remove_signature_filter(portid_t port_id,
1797                              struct rte_fdir_filter *fdir_filter)
1798 {
1799         int diag;
1800
1801         if (port_id_is_invalid(port_id))
1802                 return;
1803
1804         diag = rte_eth_dev_fdir_remove_signature_filter(port_id, fdir_filter);
1805         if (diag == 0)
1806                 return;
1807
1808         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1809                "diag=%d\n", port_id, diag);
1810
1811 }
1812
1813 static inline void
1814 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf)
1815 {
1816         struct rte_eth_flex_payload_cfg *cfg;
1817         int i, j;
1818
1819         for (i = 0; i < flex_conf->nb_payloads; i++) {
1820                 cfg = &flex_conf->flex_set[i];
1821                 if (cfg->type == RTE_ETH_L2_PAYLOAD)
1822                         printf("\n    L2_PAYLOAD:  ");
1823                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
1824                         printf("\n    L3_PAYLOAD:  ");
1825                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
1826                         printf("\n    L4_PAYLOAD:  ");
1827                 else
1828                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
1829                 for (j = 0; j < RTE_ETH_FDIR_MAX_FLEXLEN; j++)
1830                         printf("  %-5u", cfg->src_offset[j]);
1831         }
1832         printf("\n");
1833 }
1834
1835 static inline void
1836 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf)
1837 {
1838         struct rte_eth_fdir_flex_mask *mask;
1839         int i, j;
1840
1841         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
1842                 mask = &flex_conf->flex_mask[i];
1843                 printf("\n    %s:\t", flowtype_str[mask->flow_type]);
1844                 for (j = 0; j < RTE_ETH_FDIR_MAX_FLEXLEN; j++)
1845                         printf(" %02x", mask->mask[j]);
1846         }
1847         printf("\n");
1848 }
1849
1850 static inline void
1851 print_fdir_flow_type(uint32_t flow_types_mask)
1852 {
1853         int i = 0;
1854
1855         for (i = RTE_ETH_FLOW_TYPE_UDPV4;
1856              i <= RTE_ETH_FLOW_TYPE_FRAG_IPV6;
1857              i++) {
1858                 if (flow_types_mask & (1 << i))
1859                         printf(" %s", flowtype_str[i]);
1860         }
1861         printf("\n");
1862 }
1863
1864 void
1865 fdir_get_infos(portid_t port_id)
1866 {
1867         struct rte_eth_fdir_stats fdir_stat;
1868         struct rte_eth_fdir_info fdir_info;
1869         int ret;
1870
1871         static const char *fdir_stats_border = "########################";
1872
1873         if (port_id_is_invalid(port_id))
1874                 return;
1875         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
1876         if (ret < 0) {
1877                 /* use the old fdir APIs to get info */
1878                 struct rte_eth_fdir fdir;
1879                 memset(&fdir, 0, sizeof(fdir));
1880                 ret = rte_eth_dev_fdir_get_infos(port_id, &fdir);
1881                 if (ret < 0) {
1882                         printf("\n getting fdir info fails on port %-2d\n",
1883                                 port_id);
1884                         return;
1885                 }
1886                 printf("\n  %s FDIR infos for port %-2d     %s\n",
1887                         fdir_stats_border, port_id, fdir_stats_border);
1888                 printf("  collision: %-10"PRIu64"  free:     %"PRIu64"\n"
1889                        "  maxhash:   %-10"PRIu64"  maxlen:   %"PRIu64"\n"
1890                        "  add:       %-10"PRIu64"  remove:   %"PRIu64"\n"
1891                        "  f_add:     %-10"PRIu64"  f_remove: %"PRIu64"\n",
1892                        (uint64_t)(fdir.collision), (uint64_t)(fdir.free),
1893                        (uint64_t)(fdir.maxhash), (uint64_t)(fdir.maxlen),
1894                        fdir.add, fdir.remove, fdir.f_add, fdir.f_remove);
1895                 printf("  %s############################%s\n",
1896                        fdir_stats_border, fdir_stats_border);
1897                 return;
1898         }
1899
1900         memset(&fdir_info, 0, sizeof(fdir_info));
1901         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
1902                                RTE_ETH_FILTER_INFO, &fdir_info);
1903         memset(&fdir_stat, 0, sizeof(fdir_stat));
1904         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
1905                                RTE_ETH_FILTER_STATS, &fdir_stat);
1906         printf("\n  %s FDIR infos for port %-2d     %s\n",
1907                fdir_stats_border, port_id, fdir_stats_border);
1908         printf("  MODE: ");
1909         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
1910                         printf("  PERFECT\n");
1911         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
1912                         printf("  SIGNATURE\n");
1913         else
1914                         printf("  DISABLE\n");
1915         printf("  SUPPORTED FLOW TYPE: ");
1916         print_fdir_flow_type(fdir_info.flow_types_mask[0]);
1917         printf("  FLEX PAYLOAD INFO:\n");
1918         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
1919                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
1920                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
1921                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
1922                 fdir_info.flex_payload_unit,
1923                 fdir_info.max_flex_payload_segment_num,
1924                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
1925         if (fdir_info.flex_conf.nb_payloads > 0) {
1926                 printf("  FLEX PAYLOAD SRC OFFSET:");
1927                 print_fdir_flex_payload(&fdir_info.flex_conf);
1928         }
1929         if (fdir_info.flex_conf.nb_flexmasks > 0) {
1930                 printf("  FLEX MASK CFG:");
1931                 print_fdir_flex_mask(&fdir_info.flex_conf);
1932         }
1933         printf("  guarant_count: %-10"PRIu32"  best_count:    %-10"PRIu32"\n",
1934                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
1935         printf("  guarant_space: %-10"PRIu32"  best_space:    %-10"PRIu32"\n",
1936                fdir_info.guarant_spc, fdir_info.guarant_spc);
1937         printf("  %s############################%s\n",
1938                fdir_stats_border, fdir_stats_border);
1939 }
1940
1941 void
1942 fdir_add_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1943                         uint8_t drop, struct rte_fdir_filter *fdir_filter)
1944 {
1945         int diag;
1946
1947         if (port_id_is_invalid(port_id))
1948                 return;
1949
1950         diag = rte_eth_dev_fdir_add_perfect_filter(port_id, fdir_filter,
1951                                                    soft_id, queue_id, drop);
1952         if (diag == 0)
1953                 return;
1954
1955         printf("rte_eth_dev_fdir_add_perfect_filter for port_id=%d failed "
1956                "diag=%d\n", port_id, diag);
1957 }
1958
1959 void
1960 fdir_update_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1961                            uint8_t drop, struct rte_fdir_filter *fdir_filter)
1962 {
1963         int diag;
1964
1965         if (port_id_is_invalid(port_id))
1966                 return;
1967
1968         diag = rte_eth_dev_fdir_update_perfect_filter(port_id, fdir_filter,
1969                                                       soft_id, queue_id, drop);
1970         if (diag == 0)
1971                 return;
1972
1973         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1974                "diag=%d\n", port_id, diag);
1975 }
1976
1977 void
1978 fdir_remove_perfect_filter(portid_t port_id, uint16_t soft_id,
1979                            struct rte_fdir_filter *fdir_filter)
1980 {
1981         int diag;
1982
1983         if (port_id_is_invalid(port_id))
1984                 return;
1985
1986         diag = rte_eth_dev_fdir_remove_perfect_filter(port_id, fdir_filter,
1987                                                       soft_id);
1988         if (diag == 0)
1989                 return;
1990
1991         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1992                "diag=%d\n", port_id, diag);
1993 }
1994
1995 void
1996 fdir_set_masks(portid_t port_id, struct rte_fdir_masks *fdir_masks)
1997 {
1998         int diag;
1999
2000         if (port_id_is_invalid(port_id))
2001                 return;
2002
2003         diag = rte_eth_dev_fdir_set_masks(port_id, fdir_masks);
2004         if (diag == 0)
2005                 return;
2006
2007         printf("rte_eth_dev_set_masks_filter for port_id=%d failed "
2008                "diag=%d\n", port_id, diag);
2009 }
2010
2011 void
2012 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
2013 {
2014         struct rte_port *port;
2015         struct rte_eth_fdir_flex_conf *flex_conf;
2016         int i, idx = 0;
2017
2018         port = &ports[port_id];
2019         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2020         for (i = 0; i < RTE_ETH_FLOW_TYPE_MAX; i++) {
2021                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
2022                         idx = i;
2023                         break;
2024                 }
2025         }
2026         if (i >= RTE_ETH_FLOW_TYPE_MAX) {
2027                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
2028                         idx = flex_conf->nb_flexmasks;
2029                         flex_conf->nb_flexmasks++;
2030                 } else {
2031                         printf("The flex mask table is full. Can not set flex"
2032                                 " mask for flow_type(%u).", cfg->flow_type);
2033                         return;
2034                 }
2035         }
2036         (void)rte_memcpy(&flex_conf->flex_mask[idx],
2037                          cfg,
2038                          sizeof(struct rte_eth_fdir_flex_mask));
2039 }
2040
2041 void
2042 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
2043 {
2044         struct rte_port *port;
2045         struct rte_eth_fdir_flex_conf *flex_conf;
2046         int i, idx = 0;
2047
2048         port = &ports[port_id];
2049         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2050         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
2051                 if (cfg->type == flex_conf->flex_set[i].type) {
2052                         idx = i;
2053                         break;
2054                 }
2055         }
2056         if (i >= RTE_ETH_PAYLOAD_MAX) {
2057                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
2058                         idx = flex_conf->nb_payloads;
2059                         flex_conf->nb_payloads++;
2060                 } else {
2061                         printf("The flex payload table is full. Can not set"
2062                                 " flex payload for type(%u).", cfg->type);
2063                         return;
2064                 }
2065         }
2066         (void)rte_memcpy(&flex_conf->flex_set[idx],
2067                          cfg,
2068                          sizeof(struct rte_eth_flex_payload_cfg));
2069
2070 }
2071
2072 void
2073 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
2074 {
2075         int diag;
2076
2077         if (port_id_is_invalid(port_id))
2078                 return;
2079         if (is_rx)
2080                 diag = rte_eth_dev_set_vf_rx(port_id,vf,on);
2081         else
2082                 diag = rte_eth_dev_set_vf_tx(port_id,vf,on);
2083         if (diag == 0)
2084                 return;
2085         if(is_rx)
2086                 printf("rte_eth_dev_set_vf_rx for port_id=%d failed "
2087                         "diag=%d\n", port_id, diag);
2088         else
2089                 printf("rte_eth_dev_set_vf_tx for port_id=%d failed "
2090                         "diag=%d\n", port_id, diag);
2091
2092 }
2093
2094 void
2095 set_vf_rx_vlan(portid_t port_id, uint16_t vlan_id, uint64_t vf_mask, uint8_t on)
2096 {
2097         int diag;
2098
2099         if (port_id_is_invalid(port_id))
2100                 return;
2101         if (vlan_id_is_invalid(vlan_id))
2102                 return;
2103         diag = rte_eth_dev_set_vf_vlan_filter(port_id, vlan_id, vf_mask, on);
2104         if (diag == 0)
2105                 return;
2106         printf("rte_eth_dev_set_vf_vlan_filter for port_id=%d failed "
2107                "diag=%d\n", port_id, diag);
2108 }
2109
2110 int
2111 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
2112 {
2113         int diag;
2114         struct rte_eth_link link;
2115
2116         if (port_id_is_invalid(port_id))
2117                 return 1;
2118         rte_eth_link_get_nowait(port_id, &link);
2119         if (rate > link.link_speed) {
2120                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2121                         rate, link.link_speed);
2122                 return 1;
2123         }
2124         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
2125         if (diag == 0)
2126                 return diag;
2127         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
2128                 port_id, diag);
2129         return diag;
2130 }
2131
2132 int
2133 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
2134 {
2135         int diag;
2136         struct rte_eth_link link;
2137
2138         if (q_msk == 0)
2139                 return 0;
2140
2141         if (port_id_is_invalid(port_id))
2142                 return 1;
2143         rte_eth_link_get_nowait(port_id, &link);
2144         if (rate > link.link_speed) {
2145                 printf("Invalid rate value:%u bigger than link speed: %u\n",
2146                         rate, link.link_speed);
2147                 return 1;
2148         }
2149         diag = rte_eth_set_vf_rate_limit(port_id, vf, rate, q_msk);
2150         if (diag == 0)
2151                 return diag;
2152         printf("rte_eth_set_vf_rate_limit for port_id=%d failed diag=%d\n",
2153                 port_id, diag);
2154         return diag;
2155 }
2156
2157 void
2158 get_ethertype_filter(uint8_t port_id, uint16_t index)
2159 {
2160         struct rte_ethertype_filter filter;
2161         int ret = 0;
2162         uint16_t rx_queue;
2163
2164         memset(&filter, 0, sizeof(filter));
2165         ret = rte_eth_dev_get_ethertype_filter(port_id, index,
2166                                 &filter, &rx_queue);
2167         if (ret < 0) {
2168                 if (ret == (-ENOENT))
2169                         printf("filter[%d] is not enabled\n", index);
2170                 else
2171                         printf("get ethertype filter fails(%s)\n", strerror(-ret));
2172                 return;
2173         } else {
2174                 printf("filter[%d]:\n", index);
2175                 printf("    ethertype:  0x%04x\n",
2176                         rte_le_to_cpu_32(filter.ethertype));
2177                 printf("    priority: %s, %d\n",
2178                         filter.priority_en ? "enable" : "disable",
2179                         filter.priority);
2180                 printf("    queue: %d\n", rx_queue);
2181         }
2182 }
2183
2184 void
2185 get_syn_filter(uint8_t port_id)
2186 {
2187         struct rte_syn_filter filter;
2188         int ret = 0;
2189         uint16_t rx_queue;
2190
2191         memset(&filter, 0, sizeof(filter));
2192         ret = rte_eth_dev_get_syn_filter(port_id, &filter, &rx_queue);
2193
2194         if (ret < 0) {
2195                 if (ret == (-ENOENT))
2196                         printf("syn filter is not enabled\n");
2197                 else
2198                         printf("get syn filter fails(%s)\n", strerror(-ret));
2199                 return;
2200         }
2201         printf("syn filter: priority: %s, queue: %d\n",
2202                 filter.hig_pri ? "high" : "low",
2203                 rx_queue);
2204 }
2205 void
2206 get_2tuple_filter(uint8_t port_id, uint16_t index)
2207 {
2208         struct rte_2tuple_filter filter;
2209         int ret = 0;
2210         uint16_t rx_queue;
2211
2212         memset(&filter, 0, sizeof(filter));
2213         ret = rte_eth_dev_get_2tuple_filter(port_id, index,
2214                                 &filter, &rx_queue);
2215         if (ret < 0) {
2216                 if (ret == (-ENOENT))
2217                         printf("filter[%d] is not enabled\n", index);
2218                 else
2219                         printf("get 2tuple filter fails(%s)\n", strerror(-ret));
2220                 return;
2221         } else {
2222                 printf("filter[%d]:\n", index);
2223                 printf("    Destination Port:     0x%04x    mask: %d\n",
2224                         rte_be_to_cpu_16(filter.dst_port),
2225                         filter.dst_port_mask ? 0 : 1);
2226                 printf("    protocol:  0x%02x     mask:%d     tcp_flags: 0x%02x\n",
2227                         filter.protocol, filter.protocol_mask ? 0 : 1,
2228                         filter.tcp_flags);
2229                 printf("    priority: %d    queue: %d\n",
2230                         filter.priority, rx_queue);
2231         }
2232 }
2233
2234 void
2235 get_5tuple_filter(uint8_t port_id, uint16_t index)
2236 {
2237         struct rte_5tuple_filter filter;
2238         int ret = 0;
2239         uint16_t rx_queue;
2240
2241         memset(&filter, 0, sizeof(filter));
2242         ret = rte_eth_dev_get_5tuple_filter(port_id, index,
2243                                 &filter, &rx_queue);
2244         if (ret < 0) {
2245                 if (ret == (-ENOENT))
2246                         printf("filter[%d] is not enabled\n", index);
2247                 else
2248                         printf("get 5tuple filter fails(%s)\n", strerror(-ret));
2249                 return;
2250         } else {
2251                 printf("filter[%d]:\n", index);
2252                 printf("    Destination IP:  0x%08x    mask: %d\n",
2253                         (unsigned)rte_be_to_cpu_32(filter.dst_ip),
2254                         filter.dst_ip_mask ? 0 : 1);
2255                 printf("    Source IP:       0x%08x    mask: %d\n",
2256                         (unsigned)rte_be_to_cpu_32(filter.src_ip),
2257                         filter.src_ip_mask ? 0 : 1);
2258                 printf("    Destination Port:       0x%04x    mask: %d\n",
2259                         rte_be_to_cpu_16(filter.dst_port),
2260                         filter.dst_port_mask ? 0 : 1);
2261                 printf("    Source Port:       0x%04x    mask: %d\n",
2262                         rte_be_to_cpu_16(filter.src_port),
2263                         filter.src_port_mask ? 0 : 1);
2264                 printf("    protocol:           0x%02x    mask: %d\n",
2265                         filter.protocol,
2266                         filter.protocol_mask ? 0 : 1);
2267                 printf("    priority: %d    flags: 0x%02x    queue: %d\n",
2268                         filter.priority, filter.tcp_flags, rx_queue);
2269         }
2270 }
2271 void
2272 get_flex_filter(uint8_t port_id, uint16_t index)
2273
2274 {
2275         struct rte_flex_filter filter;
2276         int ret = 0;
2277         uint16_t rx_queue;
2278         int i, j;
2279
2280         memset(&filter, 0, sizeof(filter));
2281         ret = rte_eth_dev_get_flex_filter(port_id, index,
2282                                 &filter, &rx_queue);
2283         if (ret < 0) {
2284                 if (ret == (-ENOENT))
2285                         printf("filter[%d] is not enabled\n", index);
2286                 else
2287                         printf("get flex filter fails(%s)\n", strerror(-ret));
2288                 return;
2289         } else {
2290                 printf("filter[%d]: ", index);
2291                 printf("\n    length: %d", filter.len);
2292                 printf("\n    dword[]: 0x");
2293                 for (i = 0; i < 32; i++)
2294                         printf("%08x ", (unsigned)rte_be_to_cpu_32(filter.dwords[i]));
2295                 printf("\n    mask[]: 0b");
2296                 for (i = 0; i < 16; i++) {
2297                         for (j = 0; j < 8; j++)
2298                                 printf("%c", (filter.mask[i] & (1 << j)) ? '1' : '0');
2299                 }
2300                 printf("\n    priority: %d    queue: %d\n",
2301                         filter.priority, rx_queue);
2302         }
2303 }