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