app/testpmd: add engine that replies to ARP and ICMP echo requests
[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-errors: %-10"PRIu64"RX-bytes: "
130                        "%-"PRIu64"\n"
131                        "  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64"TX-bytes: "
132                        "%-"PRIu64"\n",
133                        stats.ipackets, stats.ierrors, stats.ibytes,
134                        stats.opackets, stats.oerrors, stats.obytes);
135         }
136         else {
137                 printf("  RX-packets:              %10"PRIu64"    RX-errors: %10"PRIu64
138                        "    RX-bytes: %10"PRIu64"\n"
139                        "  TX-packets:              %10"PRIu64"    TX-errors: %10"PRIu64
140                        "    TX-bytes: %10"PRIu64"\n",
141                        stats.ipackets, stats.ierrors, stats.ibytes,
142                        stats.opackets, stats.oerrors, stats.obytes);
143         }
144
145         /* stats fdir */
146         if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
147                 printf("  Fdirmiss:   %-10"PRIu64" Fdirmatch: %-10"PRIu64"\n",
148                        stats.fdirmiss,
149                        stats.fdirmatch);
150
151         if (port->rx_queue_stats_mapping_enabled) {
152                 printf("\n");
153                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
154                         printf("  Stats reg %2d RX-packets: %10"PRIu64
155                                "    RX-errors: %10"PRIu64
156                                "    RX-bytes: %10"PRIu64"\n",
157                                i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
158                 }
159         }
160         if (port->tx_queue_stats_mapping_enabled) {
161                 printf("\n");
162                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
163                         printf("  Stats reg %2d TX-packets: %10"PRIu64
164                                "                             TX-bytes: %10"PRIu64"\n",
165                                i, stats.q_opackets[i], stats.q_obytes[i]);
166                 }
167         }
168
169         /* Display statistics of XON/XOFF pause frames, if any. */
170         if ((stats.tx_pause_xon  | stats.rx_pause_xon |
171              stats.tx_pause_xoff | stats.rx_pause_xoff) > 0) {
172                 printf("  RX-XOFF:    %-10"PRIu64" RX-XON:    %-10"PRIu64"\n",
173                        stats.rx_pause_xoff, stats.rx_pause_xon);
174                 printf("  TX-XOFF:    %-10"PRIu64" TX-XON:    %-10"PRIu64"\n",
175                        stats.tx_pause_xoff, stats.tx_pause_xon);
176         }
177         printf("  %s############################%s\n",
178                nic_stats_border, nic_stats_border);
179 }
180
181 void
182 nic_stats_clear(portid_t port_id)
183 {
184         if (port_id >= nb_ports) {
185                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
186                 return;
187         }
188         rte_eth_stats_reset(port_id);
189         printf("\n  NIC statistics for port %d cleared\n", port_id);
190 }
191
192
193 void
194 nic_stats_mapping_display(portid_t port_id)
195 {
196         struct rte_port *port = &ports[port_id];
197         uint16_t i;
198
199         static const char *nic_stats_mapping_border = "########################";
200
201         if (port_id >= nb_ports) {
202                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
203                 return;
204         }
205
206         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
207                 printf("Port id %d - either does not support queue statistic mapping or"
208                        " no queue statistic mapping set\n", port_id);
209                 return;
210         }
211
212         printf("\n  %s NIC statistics mapping for port %-2d %s\n",
213                nic_stats_mapping_border, port_id, nic_stats_mapping_border);
214
215         if (port->rx_queue_stats_mapping_enabled) {
216                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
217                         if (rx_queue_stats_mappings[i].port_id == port_id) {
218                                 printf("  RX-queue %2d mapped to Stats Reg %2d\n",
219                                        rx_queue_stats_mappings[i].queue_id,
220                                        rx_queue_stats_mappings[i].stats_counter_id);
221                         }
222                 }
223                 printf("\n");
224         }
225
226
227         if (port->tx_queue_stats_mapping_enabled) {
228                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
229                         if (tx_queue_stats_mappings[i].port_id == port_id) {
230                                 printf("  TX-queue %2d mapped to Stats Reg %2d\n",
231                                        tx_queue_stats_mappings[i].queue_id,
232                                        tx_queue_stats_mappings[i].stats_counter_id);
233                         }
234                 }
235         }
236
237         printf("  %s####################################%s\n",
238                nic_stats_mapping_border, nic_stats_mapping_border);
239 }
240
241 void
242 port_infos_display(portid_t port_id)
243 {
244         struct rte_port *port;
245         struct rte_eth_link link;
246         int vlan_offload;
247         struct rte_mempool * mp;
248         static const char *info_border = "*********************";
249
250         if (port_id >= nb_ports) {
251                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
252                 return;
253         }
254         port = &ports[port_id];
255         rte_eth_link_get_nowait(port_id, &link);
256         printf("\n%s Infos for port %-2d %s\n",
257                info_border, port_id, info_border);
258         print_ethaddr("MAC address: ", &port->eth_addr);
259         printf("\nConnect to socket: %u", port->socket_id);
260
261         if (port_numa[port_id] != NUMA_NO_CONFIG) {
262                 mp = mbuf_pool_find(port_numa[port_id]);
263                 if (mp)
264                         printf("\nmemory allocation on the socket: %d",
265                                                         port_numa[port_id]);
266         } else
267                 printf("\nmemory allocation on the socket: %u",port->socket_id);
268
269         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
270         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
271         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
272                ("full-duplex") : ("half-duplex"));
273         printf("Promiscuous mode: %s\n",
274                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
275         printf("Allmulticast mode: %s\n",
276                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
277         printf("Maximum number of MAC addresses: %u\n",
278                (unsigned int)(port->dev_info.max_mac_addrs));
279         printf("Maximum number of MAC addresses of hash filtering: %u\n",
280                (unsigned int)(port->dev_info.max_hash_mac_addrs));
281
282         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
283         if (vlan_offload >= 0){
284                 printf("VLAN offload: \n");
285                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
286                         printf("  strip on \n");
287                 else
288                         printf("  strip off \n");
289
290                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
291                         printf("  filter on \n");
292                 else
293                         printf("  filter off \n");
294
295                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
296                         printf("  qinq(extend) on \n");
297                 else
298                         printf("  qinq(extend) off \n");
299         }
300 }
301
302 static int
303 port_id_is_invalid(portid_t port_id)
304 {
305         if (port_id < nb_ports)
306                 return 0;
307         printf("Invalid port %d (must be < nb_ports=%d)\n", port_id, nb_ports);
308         return 1;
309 }
310
311 static int
312 vlan_id_is_invalid(uint16_t vlan_id)
313 {
314         if (vlan_id < 4096)
315                 return 0;
316         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
317         return 1;
318 }
319
320 static int
321 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
322 {
323         uint64_t pci_len;
324
325         if (reg_off & 0x3) {
326                 printf("Port register offset 0x%X not aligned on a 4-byte "
327                        "boundary\n",
328                        (unsigned)reg_off);
329                 return 1;
330         }
331         pci_len = ports[port_id].dev_info.pci_dev->mem_resource[0].len;
332         if (reg_off >= pci_len) {
333                 printf("Port %d: register offset %u (0x%X) out of port PCI "
334                        "resource (length=%"PRIu64")\n",
335                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
336                 return 1;
337         }
338         return 0;
339 }
340
341 static int
342 reg_bit_pos_is_invalid(uint8_t bit_pos)
343 {
344         if (bit_pos <= 31)
345                 return 0;
346         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
347         return 1;
348 }
349
350 #define display_port_and_reg_off(port_id, reg_off) \
351         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
352
353 static inline void
354 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
355 {
356         display_port_and_reg_off(port_id, (unsigned)reg_off);
357         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
358 }
359
360 void
361 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
362 {
363         uint32_t reg_v;
364
365
366         if (port_id_is_invalid(port_id))
367                 return;
368         if (port_reg_off_is_invalid(port_id, reg_off))
369                 return;
370         if (reg_bit_pos_is_invalid(bit_x))
371                 return;
372         reg_v = port_id_pci_reg_read(port_id, reg_off);
373         display_port_and_reg_off(port_id, (unsigned)reg_off);
374         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
375 }
376
377 void
378 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
379                            uint8_t bit1_pos, uint8_t bit2_pos)
380 {
381         uint32_t reg_v;
382         uint8_t  l_bit;
383         uint8_t  h_bit;
384
385         if (port_id_is_invalid(port_id))
386                 return;
387         if (port_reg_off_is_invalid(port_id, reg_off))
388                 return;
389         if (reg_bit_pos_is_invalid(bit1_pos))
390                 return;
391         if (reg_bit_pos_is_invalid(bit2_pos))
392                 return;
393         if (bit1_pos > bit2_pos)
394                 l_bit = bit2_pos, h_bit = bit1_pos;
395         else
396                 l_bit = bit1_pos, h_bit = bit2_pos;
397
398         reg_v = port_id_pci_reg_read(port_id, reg_off);
399         reg_v >>= l_bit;
400         if (h_bit < 31)
401                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
402         display_port_and_reg_off(port_id, (unsigned)reg_off);
403         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
404                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
405 }
406
407 void
408 port_reg_display(portid_t port_id, uint32_t reg_off)
409 {
410         uint32_t reg_v;
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         reg_v = port_id_pci_reg_read(port_id, reg_off);
417         display_port_reg_value(port_id, reg_off, reg_v);
418 }
419
420 void
421 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
422                  uint8_t bit_v)
423 {
424         uint32_t reg_v;
425
426         if (port_id_is_invalid(port_id))
427                 return;
428         if (port_reg_off_is_invalid(port_id, reg_off))
429                 return;
430         if (reg_bit_pos_is_invalid(bit_pos))
431                 return;
432         if (bit_v > 1) {
433                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
434                 return;
435         }
436         reg_v = port_id_pci_reg_read(port_id, reg_off);
437         if (bit_v == 0)
438                 reg_v &= ~(1 << bit_pos);
439         else
440                 reg_v |= (1 << bit_pos);
441         port_id_pci_reg_write(port_id, reg_off, reg_v);
442         display_port_reg_value(port_id, reg_off, reg_v);
443 }
444
445 void
446 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
447                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
448 {
449         uint32_t max_v;
450         uint32_t reg_v;
451         uint8_t  l_bit;
452         uint8_t  h_bit;
453
454         if (port_id_is_invalid(port_id))
455                 return;
456         if (port_reg_off_is_invalid(port_id, reg_off))
457                 return;
458         if (reg_bit_pos_is_invalid(bit1_pos))
459                 return;
460         if (reg_bit_pos_is_invalid(bit2_pos))
461                 return;
462         if (bit1_pos > bit2_pos)
463                 l_bit = bit2_pos, h_bit = bit1_pos;
464         else
465                 l_bit = bit1_pos, h_bit = bit2_pos;
466
467         if ((h_bit - l_bit) < 31)
468                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
469         else
470                 max_v = 0xFFFFFFFF;
471
472         if (value > max_v) {
473                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
474                                 (unsigned)value, (unsigned)value,
475                                 (unsigned)max_v, (unsigned)max_v);
476                 return;
477         }
478         reg_v = port_id_pci_reg_read(port_id, reg_off);
479         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
480         reg_v |= (value << l_bit); /* Set changed bits */
481         port_id_pci_reg_write(port_id, reg_off, reg_v);
482         display_port_reg_value(port_id, reg_off, reg_v);
483 }
484
485 void
486 port_reg_set(portid_t port_id, uint32_t reg_off, 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         port_id_pci_reg_write(port_id, reg_off, reg_v);
493         display_port_reg_value(port_id, reg_off, reg_v);
494 }
495
496 /*
497  * RX/TX ring descriptors display functions.
498  */
499 static int
500 rx_queue_id_is_invalid(queueid_t rxq_id)
501 {
502         if (rxq_id < nb_rxq)
503                 return 0;
504         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
505         return 1;
506 }
507
508 static int
509 tx_queue_id_is_invalid(queueid_t txq_id)
510 {
511         if (txq_id < nb_txq)
512                 return 0;
513         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
514         return 1;
515 }
516
517 static int
518 rx_desc_id_is_invalid(uint16_t rxdesc_id)
519 {
520         if (rxdesc_id < nb_rxd)
521                 return 0;
522         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
523                rxdesc_id, nb_rxd);
524         return 1;
525 }
526
527 static int
528 tx_desc_id_is_invalid(uint16_t txdesc_id)
529 {
530         if (txdesc_id < nb_txd)
531                 return 0;
532         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
533                txdesc_id, nb_txd);
534         return 1;
535 }
536
537 static const struct rte_memzone *
538 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
539 {
540         char mz_name[RTE_MEMZONE_NAMESIZE];
541         const struct rte_memzone *mz;
542
543         rte_snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
544                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
545         mz = rte_memzone_lookup(mz_name);
546         if (mz == NULL)
547                 printf("%s ring memory zoneof (port %d, queue %d) not"
548                        "found (zone name = %s\n",
549                        ring_name, port_id, q_id, mz_name);
550         return (mz);
551 }
552
553 union igb_ring_dword {
554         uint64_t dword;
555         struct {
556                 uint32_t hi;
557                 uint32_t lo;
558         } words;
559 };
560
561 struct igb_ring_desc {
562         union igb_ring_dword lo_dword;
563         union igb_ring_dword hi_dword;
564 };
565
566 static void
567 ring_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
568 {
569         struct igb_ring_desc *ring;
570         struct igb_ring_desc rd;
571
572         ring = (struct igb_ring_desc *) ring_mz->addr;
573         rd.lo_dword = rte_le_to_cpu_64(ring[desc_id].lo_dword);
574         rd.hi_dword = rte_le_to_cpu_64(ring[desc_id].hi_dword);
575         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
576                 (unsigned)rd.lo_dword.words.lo, (unsigned)rd.lo_dword.words.hi,
577                 (unsigned)rd.hi_dword.words.lo, (unsigned)rd.hi_dword.words.hi);
578 }
579
580 void
581 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
582 {
583         const struct rte_memzone *rx_mz;
584
585         if (port_id_is_invalid(port_id))
586                 return;
587         if (rx_queue_id_is_invalid(rxq_id))
588                 return;
589         if (rx_desc_id_is_invalid(rxd_id))
590                 return;
591         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
592         if (rx_mz == NULL)
593                 return;
594         ring_descriptor_display(rx_mz, rxd_id);
595 }
596
597 void
598 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
599 {
600         const struct rte_memzone *tx_mz;
601
602         if (port_id_is_invalid(port_id))
603                 return;
604         if (tx_queue_id_is_invalid(txq_id))
605                 return;
606         if (tx_desc_id_is_invalid(txd_id))
607                 return;
608         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
609         if (tx_mz == NULL)
610                 return;
611         ring_descriptor_display(tx_mz, txd_id);
612 }
613
614 void
615 fwd_lcores_config_display(void)
616 {
617         lcoreid_t lc_id;
618
619         printf("List of forwarding lcores:");
620         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
621                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
622         printf("\n");
623 }
624 void
625 rxtx_config_display(void)
626 {
627         printf("  %s packet forwarding - CRC stripping %s - "
628                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
629                rx_mode.hw_strip_crc ? "enabled" : "disabled",
630                nb_pkt_per_burst);
631
632         if (cur_fwd_eng == &tx_only_engine)
633                 printf("  packet len=%u - nb packet segments=%d\n",
634                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
635
636         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
637                nb_fwd_lcores, nb_fwd_ports);
638         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
639                nb_rxq, nb_rxd, rx_free_thresh);
640         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
641                rx_thresh.pthresh, rx_thresh.hthresh, rx_thresh.wthresh);
642         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
643                nb_txq, nb_txd, tx_free_thresh);
644         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
645                tx_thresh.pthresh, tx_thresh.hthresh, tx_thresh.wthresh);
646         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
647                tx_rs_thresh, txq_flags);
648 }
649
650 void
651 port_rss_reta_info(portid_t port_id,struct rte_eth_rss_reta *reta_conf)
652 {
653         uint8_t i,j;
654         int ret;
655
656         if (port_id_is_invalid(port_id)) 
657                 return;
658
659         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf);
660         if (ret != 0) {
661                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
662                 return;
663         }
664
665         if (reta_conf->mask_lo != 0) {
666                 for (i = 0; i< ETH_RSS_RETA_NUM_ENTRIES/2; i++) {
667                         if (reta_conf->mask_lo & (uint64_t)(1ULL << i))
668                                 printf("RSS RETA configuration: hash index=%d,"
669                                         "queue=%d\n",i,reta_conf->reta[i]);     
670                 }
671         }
672         
673         if (reta_conf->mask_hi != 0) {
674                 for (i = 0; i< ETH_RSS_RETA_NUM_ENTRIES/2; i++) {
675                         if(reta_conf->mask_hi & (uint64_t)(1ULL << i)) {
676                                 j = (uint8_t)(i + ETH_RSS_RETA_NUM_ENTRIES/2);          
677                                 printf("RSS RETA configuration: hash index=%d,"
678                                         "queue=%d\n",j,reta_conf->reta[j]);
679                         }
680                 }
681         }
682 }
683
684 /*
685  * Setup forwarding configuration for each logical core.
686  */
687 static void
688 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
689 {
690         streamid_t nb_fs_per_lcore;
691         streamid_t nb_fs;
692         streamid_t sm_id;
693         lcoreid_t  nb_extra;
694         lcoreid_t  nb_fc;
695         lcoreid_t  nb_lc;
696         lcoreid_t  lc_id;
697
698         nb_fs = cfg->nb_fwd_streams;
699         nb_fc = cfg->nb_fwd_lcores;
700         if (nb_fs <= nb_fc) {
701                 nb_fs_per_lcore = 1;
702                 nb_extra = 0;
703         } else {
704                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
705                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
706         }
707
708         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
709         sm_id = 0;
710         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
711                 fwd_lcores[lc_id]->stream_idx = sm_id;
712                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
713                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
714         }
715
716         /*
717          * Assign extra remaining streams, if any.
718          */
719         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
720         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
721                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
722                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
723                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
724         }
725 }
726
727 static void
728 simple_fwd_config_setup(void)
729 {
730         portid_t i;
731         portid_t j;
732         portid_t inc = 2;
733
734         if (port_topology == PORT_TOPOLOGY_CHAINED ||
735             port_topology == PORT_TOPOLOGY_LOOP) {
736                 inc = 1;
737         } else if (nb_fwd_ports % 2) {
738                 printf("\nWarning! Cannot handle an odd number of ports "
739                        "with the current port topology. Configuration "
740                        "must be changed to have an even number of ports, "
741                        "or relaunch application with "
742                        "--port-topology=chained\n\n");
743         }
744
745         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
746         cur_fwd_config.nb_fwd_streams =
747                 (streamid_t) cur_fwd_config.nb_fwd_ports;
748
749         /* reinitialize forwarding streams */
750         init_fwd_streams();
751
752         /*
753          * In the simple forwarding test, the number of forwarding cores
754          * must be lower or equal to the number of forwarding ports.
755          */
756         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
757         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
758                 cur_fwd_config.nb_fwd_lcores =
759                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
760         setup_fwd_config_of_each_lcore(&cur_fwd_config);
761
762         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
763                 if (port_topology != PORT_TOPOLOGY_LOOP)
764                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
765                 else
766                         j = i;
767                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
768                 fwd_streams[i]->rx_queue  = 0;
769                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
770                 fwd_streams[i]->tx_queue  = 0;
771                 fwd_streams[i]->peer_addr = j;
772
773                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
774                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
775                         fwd_streams[j]->rx_queue  = 0;
776                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
777                         fwd_streams[j]->tx_queue  = 0;
778                         fwd_streams[j]->peer_addr = i;
779                 }
780         }
781 }
782
783 /**
784  * For the RSS forwarding test, each core is assigned on every port a transmit
785  * queue whose index is the index of the core itself. This approach limits the
786  * maximumm number of processing cores of the RSS test to the maximum number of
787  * TX queues supported by the devices.
788  *
789  * Each core is assigned a single stream, each stream being composed of
790  * a RX queue to poll on a RX port for input messages, associated with
791  * a TX queue of a TX port where to send forwarded packets.
792  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
793  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
794  * following rules:
795  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
796  *    - TxQl = RxQj
797  */
798 static void
799 rss_fwd_config_setup(void)
800 {
801         portid_t   rxp;
802         portid_t   txp;
803         queueid_t  rxq;
804         queueid_t  nb_q;
805         lcoreid_t  lc_id;
806
807         nb_q = nb_rxq;
808         if (nb_q > nb_txq)
809                 nb_q = nb_txq;
810         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
811         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
812         cur_fwd_config.nb_fwd_streams =
813                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
814         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
815                 cur_fwd_config.nb_fwd_streams =
816                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
817         else
818                 cur_fwd_config.nb_fwd_lcores =
819                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
820
821         /* reinitialize forwarding streams */
822         init_fwd_streams();
823
824         setup_fwd_config_of_each_lcore(&cur_fwd_config);
825         rxp = 0; rxq = 0;
826         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
827                 struct fwd_stream *fs;
828
829                 fs = fwd_streams[lc_id];
830
831                 if ((rxp & 0x1) == 0)
832                         txp = (portid_t) (rxp + 1);
833                 else
834                         txp = (portid_t) (rxp - 1);
835                 /*
836                  * if we are in loopback, simply send stuff out through the
837                  * ingress port
838                  */
839                 if (port_topology == PORT_TOPOLOGY_LOOP)
840                         txp = rxp;
841
842                 fs->rx_port = fwd_ports_ids[rxp];
843                 fs->rx_queue = rxq;
844                 fs->tx_port = fwd_ports_ids[txp];
845                 fs->tx_queue = rxq;
846                 fs->peer_addr = fs->tx_port;
847                 rxq = (queueid_t) (rxq + 1);
848                 if (rxq < nb_q)
849                         continue;
850                 /*
851                  * rxq == nb_q
852                  * Restart from RX queue 0 on next RX port
853                  */
854                 rxq = 0;
855                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
856                         rxp = (portid_t)
857                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
858                 else
859                         rxp = (portid_t) (rxp + 1);
860         }
861 }
862
863 /*
864  * In DCB and VT on,the mapping of 128 receive queues to 128 transmit queues.
865  */
866 static void
867 dcb_rxq_2_txq_mapping(queueid_t rxq, queueid_t *txq)
868 {
869         if(dcb_q_mapping == DCB_4_TCS_Q_MAPPING) {
870
871                 if (rxq < 32)
872                         /* tc0: 0-31 */ 
873                         *txq = rxq;  
874                 else if (rxq < 64) {
875                         /* tc1: 64-95 */ 
876                         *txq =  (uint16_t)(rxq + 32);
877                 } 
878                 else {  
879                         /* tc2: 96-111;tc3:112-127 */
880                         *txq =  (uint16_t)(rxq/2 + 64);
881                 }
882         }
883         else {
884                 if (rxq < 16)
885                         /* tc0 mapping*/
886                         *txq = rxq;
887                 else if (rxq < 32) {
888                         /* tc1 mapping*/
889                          *txq = (uint16_t)(rxq + 16);
890                 }
891                 else if (rxq < 64) {
892                         /*tc2,tc3 mapping */
893                         *txq =  (uint16_t)(rxq + 32);
894                 }
895                 else {
896                         /* tc4,tc5,tc6 and tc7 mapping */
897                         *txq =  (uint16_t)(rxq/2 + 64);
898                 }
899         }
900 }
901
902 /**
903  * For the DCB forwarding test, each core is assigned on every port multi-transmit
904  * queue. 
905  *
906  * Each core is assigned a multi-stream, each stream being composed of
907  * a RX queue to poll on a RX port for input messages, associated with
908  * a TX queue of a TX port where to send forwarded packets.
909  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
910  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
911  * following rules:
912  * In VT mode,
913  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
914  *    - TxQl = RxQj
915  * In non-VT mode,
916  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd  
917  *    There is a mapping of RxQj to TxQl to be required,and the mapping was implemented
918  *    in dcb_rxq_2_txq_mapping function.
919  */
920 static void
921 dcb_fwd_config_setup(void)
922 {
923         portid_t   rxp;
924         portid_t   txp;
925         queueid_t  rxq;
926         queueid_t  nb_q;
927         lcoreid_t  lc_id;
928         uint16_t sm_id;
929
930         nb_q = nb_rxq;
931
932         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
933         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
934         cur_fwd_config.nb_fwd_streams = 
935                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
936
937         /* reinitialize forwarding streams */
938         init_fwd_streams();
939
940         setup_fwd_config_of_each_lcore(&cur_fwd_config);
941         rxp = 0; rxq = 0;
942         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
943                 /* a fwd core can run multi-streams */
944                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++)
945                 {
946                         struct fwd_stream *fs;
947                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
948                         if ((rxp & 0x1) == 0)
949                                 txp = (portid_t) (rxp + 1);
950                         else
951                                 txp = (portid_t) (rxp - 1);
952                         fs->rx_port = fwd_ports_ids[rxp];
953                         fs->rx_queue = rxq;
954                         fs->tx_port = fwd_ports_ids[txp];
955                         if (dcb_q_mapping == DCB_VT_Q_MAPPING)
956                                 fs->tx_queue = rxq;
957                         else
958                                 dcb_rxq_2_txq_mapping(rxq, &fs->tx_queue);
959                         fs->peer_addr = fs->tx_port;
960                         rxq = (queueid_t) (rxq + 1);
961                         if (rxq < nb_q)
962                                 continue;
963                         rxq = 0;
964                         if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
965                                 rxp = (portid_t)
966                                         (rxp + ((nb_ports >> 1) / nb_fwd_ports));
967                         else
968                                 rxp = (portid_t) (rxp + 1);
969                 }
970         }
971 }
972
973 static void
974 icmp_echo_config_setup(void)
975 {
976         portid_t  rxp;
977         queueid_t rxq;
978         lcoreid_t lc_id;
979         uint16_t  sm_id;
980
981         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
982                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
983                         (nb_txq * nb_fwd_ports);
984         else
985                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
986         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
987         cur_fwd_config.nb_fwd_streams =
988                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
989         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
990                 cur_fwd_config.nb_fwd_lcores =
991                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
992         if (verbose_level > 0) {
993                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
994                        __FUNCTION__,
995                        cur_fwd_config.nb_fwd_lcores,
996                        cur_fwd_config.nb_fwd_ports,
997                        cur_fwd_config.nb_fwd_streams);
998         }
999
1000         /* reinitialize forwarding streams */
1001         init_fwd_streams();
1002         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1003         rxp = 0; rxq = 0;
1004         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
1005                 if (verbose_level > 0)
1006                         printf("  core=%d: \n", lc_id);
1007                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1008                         struct fwd_stream *fs;
1009                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1010                         fs->rx_port = fwd_ports_ids[rxp];
1011                         fs->rx_queue = rxq;
1012                         fs->tx_port = fs->rx_port;
1013                         fs->tx_queue = lc_id;
1014                         fs->peer_addr = fs->tx_port;
1015                         if (verbose_level > 0)
1016                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
1017                                        sm_id, fs->rx_port, fs->rx_queue,
1018                                        fs->tx_queue);
1019                         rxq = (queueid_t) (rxq + 1);
1020                         if (rxq == nb_rxq) {
1021                                 rxq = 0;
1022                                 rxp = (portid_t) (rxp + 1);
1023                         }
1024                 }
1025         }
1026 }
1027
1028 void
1029 fwd_config_setup(void)
1030 {
1031         cur_fwd_config.fwd_eng = cur_fwd_eng;
1032         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
1033                 icmp_echo_config_setup();
1034                 return;
1035         }
1036         if ((nb_rxq > 1) && (nb_txq > 1)){
1037                 if (dcb_config)
1038                         dcb_fwd_config_setup();
1039                 else
1040                         rss_fwd_config_setup();
1041         }
1042         else
1043                 simple_fwd_config_setup();
1044 }
1045
1046 static void
1047 pkt_fwd_config_display(struct fwd_config *cfg)
1048 {
1049         struct fwd_stream *fs;
1050         lcoreid_t  lc_id;
1051         streamid_t sm_id;
1052
1053         printf("%s packet forwarding - ports=%d - cores=%d - streams=%d - "
1054                 "NUMA support %s, MP over anonymous pages %s\n",
1055                 cfg->fwd_eng->fwd_mode_name,
1056                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
1057                 numa_support == 1 ? "enabled" : "disabled",
1058                 mp_anon != 0 ? "enabled" : "disabled");
1059
1060         if (strcmp(cfg->fwd_eng->fwd_mode_name, "mac_retry") == 0)
1061                 printf("TX retry num: %u, delay between TX retries: %uus\n",
1062                         burst_tx_retry_num, burst_tx_delay_time);
1063         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
1064                 printf("Logical Core %u (socket %u) forwards packets on "
1065                        "%d streams:",
1066                        fwd_lcores_cpuids[lc_id],
1067                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
1068                        fwd_lcores[lc_id]->stream_nb);
1069                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
1070                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
1071                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
1072                                "P=%d/Q=%d (socket %u) ",
1073                                fs->rx_port, fs->rx_queue,
1074                                ports[fs->rx_port].socket_id,
1075                                fs->tx_port, fs->tx_queue,
1076                                ports[fs->tx_port].socket_id);
1077                         print_ethaddr("peer=",
1078                                       &peer_eth_addrs[fs->peer_addr]);
1079                 }
1080                 printf("\n");
1081         }
1082         printf("\n");
1083 }
1084
1085
1086 void
1087 fwd_config_display(void)
1088 {
1089         if((dcb_config) && (nb_fwd_lcores == 1)) {
1090                 printf("In DCB mode,the nb forwarding cores should be larger than 1\n");
1091                 return;
1092         } 
1093         fwd_config_setup();
1094         pkt_fwd_config_display(&cur_fwd_config);
1095 }
1096
1097 int
1098 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
1099 {
1100         unsigned int i;
1101         unsigned int lcore_cpuid;
1102         int record_now;
1103
1104         record_now = 0;
1105  again:
1106         for (i = 0; i < nb_lc; i++) {
1107                 lcore_cpuid = lcorelist[i];
1108                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
1109                         printf("lcore %u not enabled\n", lcore_cpuid);
1110                         return -1;
1111                 }
1112                 if (lcore_cpuid == rte_get_master_lcore()) {
1113                         printf("lcore %u cannot be masked on for running "
1114                                "packet forwarding, which is the master lcore "
1115                                "and reserved for command line parsing only\n",
1116                                lcore_cpuid);
1117                         return -1;
1118                 }
1119                 if (record_now)
1120                         fwd_lcores_cpuids[i] = lcore_cpuid;
1121         }
1122         if (record_now == 0) {
1123                 record_now = 1;
1124                 goto again;
1125         }
1126         nb_cfg_lcores = (lcoreid_t) nb_lc;
1127         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
1128                 printf("previous number of forwarding cores %u - changed to "
1129                        "number of configured cores %u\n",
1130                        (unsigned int) nb_fwd_lcores, nb_lc);
1131                 nb_fwd_lcores = (lcoreid_t) nb_lc;
1132         }
1133
1134         return 0;
1135 }
1136
1137 int
1138 set_fwd_lcores_mask(uint64_t lcoremask)
1139 {
1140         unsigned int lcorelist[64];
1141         unsigned int nb_lc;
1142         unsigned int i;
1143
1144         if (lcoremask == 0) {
1145                 printf("Invalid NULL mask of cores\n");
1146                 return -1;
1147         }
1148         nb_lc = 0;
1149         for (i = 0; i < 64; i++) {
1150                 if (! ((uint64_t)(1ULL << i) & lcoremask))
1151                         continue;
1152                 lcorelist[nb_lc++] = i;
1153         }
1154         return set_fwd_lcores_list(lcorelist, nb_lc);
1155 }
1156
1157 void
1158 set_fwd_lcores_number(uint16_t nb_lc)
1159 {
1160         if (nb_lc > nb_cfg_lcores) {
1161                 printf("nb fwd cores %u > %u (max. number of configured "
1162                        "lcores) - ignored\n",
1163                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
1164                 return;
1165         }
1166         nb_fwd_lcores = (lcoreid_t) nb_lc;
1167         printf("Number of forwarding cores set to %u\n",
1168                (unsigned int) nb_fwd_lcores);
1169 }
1170
1171 void
1172 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
1173 {
1174         unsigned int i;
1175         portid_t port_id;
1176         int record_now;
1177
1178         record_now = 0;
1179  again:
1180         for (i = 0; i < nb_pt; i++) {
1181                 port_id = (portid_t) portlist[i];
1182                 if (port_id >= nb_ports) {
1183                         printf("Invalid port id %u >= %u\n",
1184                                (unsigned int) port_id,
1185                                (unsigned int) nb_ports);
1186                         return;
1187                 }
1188                 if (record_now)
1189                         fwd_ports_ids[i] = port_id;
1190         }
1191         if (record_now == 0) {
1192                 record_now = 1;
1193                 goto again;
1194         }
1195         nb_cfg_ports = (portid_t) nb_pt;
1196         if (nb_fwd_ports != (portid_t) nb_pt) {
1197                 printf("previous number of forwarding ports %u - changed to "
1198                        "number of configured ports %u\n",
1199                        (unsigned int) nb_fwd_ports, nb_pt);
1200                 nb_fwd_ports = (portid_t) nb_pt;
1201         }
1202 }
1203
1204 void
1205 set_fwd_ports_mask(uint64_t portmask)
1206 {
1207         unsigned int portlist[64];
1208         unsigned int nb_pt;
1209         unsigned int i;
1210
1211         if (portmask == 0) {
1212                 printf("Invalid NULL mask of ports\n");
1213                 return;
1214         }
1215         nb_pt = 0;
1216         for (i = 0; i < 64; i++) {
1217                 if (! ((uint64_t)(1ULL << i) & portmask))
1218                         continue;
1219                 portlist[nb_pt++] = i;
1220         }
1221         set_fwd_ports_list(portlist, nb_pt);
1222 }
1223
1224 void
1225 set_fwd_ports_number(uint16_t nb_pt)
1226 {
1227         if (nb_pt > nb_cfg_ports) {
1228                 printf("nb fwd ports %u > %u (number of configured "
1229                        "ports) - ignored\n",
1230                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
1231                 return;
1232         }
1233         nb_fwd_ports = (portid_t) nb_pt;
1234         printf("Number of forwarding ports set to %u\n",
1235                (unsigned int) nb_fwd_ports);
1236 }
1237
1238 void
1239 set_nb_pkt_per_burst(uint16_t nb)
1240 {
1241         if (nb > MAX_PKT_BURST) {
1242                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
1243                        " ignored\n",
1244                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
1245                 return;
1246         }
1247         nb_pkt_per_burst = nb;
1248         printf("Number of packets per burst set to %u\n",
1249                (unsigned int) nb_pkt_per_burst);
1250 }
1251
1252 void
1253 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
1254 {
1255         uint16_t tx_pkt_len;
1256         unsigned i;
1257
1258         if (nb_segs >= (unsigned) nb_txd) {
1259                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
1260                        nb_segs, (unsigned int) nb_txd);
1261                 return;
1262         }
1263
1264         /*
1265          * Check that each segment length is greater or equal than
1266          * the mbuf data sise.
1267          * Check also that the total packet length is greater or equal than the
1268          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
1269          */
1270         tx_pkt_len = 0;
1271         for (i = 0; i < nb_segs; i++) {
1272                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
1273                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
1274                                i, seg_lengths[i], (unsigned) mbuf_data_size);
1275                         return;
1276                 }
1277                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
1278         }
1279         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
1280                 printf("total packet length=%u < %d - give up\n",
1281                                 (unsigned) tx_pkt_len,
1282                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
1283                 return;
1284         }
1285
1286         for (i = 0; i < nb_segs; i++)
1287                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
1288
1289         tx_pkt_length  = tx_pkt_len;
1290         tx_pkt_nb_segs = (uint8_t) nb_segs;
1291 }
1292
1293 char*
1294 list_pkt_forwarding_modes(void)
1295 {
1296         static char fwd_modes[128] = "";
1297         const char *separator = "|";
1298         struct fwd_engine *fwd_eng;
1299         unsigned i = 0;
1300
1301         if (strlen (fwd_modes) == 0) {
1302                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
1303                         strcat(fwd_modes, fwd_eng->fwd_mode_name);
1304                         strcat(fwd_modes, separator);
1305                 }
1306                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
1307         }
1308
1309         return fwd_modes;
1310 }
1311
1312 void
1313 set_pkt_forwarding_mode(const char *fwd_mode_name)
1314 {
1315         struct fwd_engine *fwd_eng;
1316         unsigned i;
1317
1318         i = 0;
1319         while ((fwd_eng = fwd_engines[i]) != NULL) {
1320                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
1321                         printf("Set %s packet forwarding mode\n",
1322                                fwd_mode_name);
1323                         cur_fwd_eng = fwd_eng;
1324                         return;
1325                 }
1326                 i++;
1327         }
1328         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
1329 }
1330
1331 void
1332 set_verbose_level(uint16_t vb_level)
1333 {
1334         printf("Change verbose level from %u to %u\n",
1335                (unsigned int) verbose_level, (unsigned int) vb_level);
1336         verbose_level = vb_level;
1337 }
1338
1339 void
1340 vlan_extend_set(portid_t port_id, int on)
1341 {
1342         int diag;
1343         int vlan_offload;
1344
1345         if (port_id_is_invalid(port_id))
1346                 return;
1347
1348         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1349
1350         if (on)
1351                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
1352         else
1353                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
1354
1355         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1356         if (diag < 0)
1357                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
1358                "diag=%d\n", port_id, on, diag);
1359 }
1360
1361 void
1362 rx_vlan_strip_set(portid_t port_id, int on)
1363 {
1364         int diag;
1365         int vlan_offload;
1366
1367         if (port_id_is_invalid(port_id))
1368                 return;
1369
1370         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1371
1372         if (on)
1373                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
1374         else
1375                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
1376
1377         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1378         if (diag < 0)
1379                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
1380                "diag=%d\n", port_id, on, diag);
1381 }
1382
1383 void
1384 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
1385 {
1386         int diag;
1387
1388         if (port_id_is_invalid(port_id))
1389                 return;
1390
1391         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
1392         if (diag < 0)
1393                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
1394                "diag=%d\n", port_id, queue_id, on, diag);
1395 }
1396
1397 void
1398 rx_vlan_filter_set(portid_t port_id, int on)
1399 {
1400         int diag;
1401         int vlan_offload;
1402
1403         if (port_id_is_invalid(port_id))
1404                 return;
1405
1406         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
1407
1408         if (on)
1409                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1410         else
1411                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
1412
1413         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
1414         if (diag < 0)
1415                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
1416                "diag=%d\n", port_id, on, diag);
1417 }
1418
1419 void
1420 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
1421 {
1422         int diag;
1423
1424         if (port_id_is_invalid(port_id))
1425                 return;
1426         if (vlan_id_is_invalid(vlan_id))
1427                 return;
1428         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
1429         if (diag == 0)
1430                 return;
1431         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
1432                "diag=%d\n",
1433                port_id, vlan_id, on, diag);
1434 }
1435
1436 void
1437 rx_vlan_all_filter_set(portid_t port_id, int on)
1438 {
1439         uint16_t vlan_id;
1440
1441         if (port_id_is_invalid(port_id))
1442                 return;
1443         for (vlan_id = 0; vlan_id < 4096; vlan_id++)
1444                 rx_vft_set(port_id, vlan_id, on);
1445 }
1446
1447 void
1448 vlan_tpid_set(portid_t port_id, uint16_t tp_id)
1449 {
1450         int diag;
1451         if (port_id_is_invalid(port_id))
1452                 return;
1453
1454         diag = rte_eth_dev_set_vlan_ether_type(port_id, tp_id);
1455         if (diag == 0)
1456                 return;
1457
1458         printf("tx_vlan_tpid_set(port_pi=%d, tpid=%d) failed "
1459                "diag=%d\n",
1460                port_id, tp_id, diag);
1461 }
1462
1463 void
1464 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
1465 {
1466         if (port_id_is_invalid(port_id))
1467                 return;
1468         if (vlan_id_is_invalid(vlan_id))
1469                 return;
1470         ports[port_id].tx_ol_flags |= PKT_TX_VLAN_PKT;
1471         ports[port_id].tx_vlan_id = vlan_id;
1472 }
1473
1474 void
1475 tx_vlan_reset(portid_t port_id)
1476 {
1477         if (port_id_is_invalid(port_id))
1478                 return;
1479         ports[port_id].tx_ol_flags &= ~PKT_TX_VLAN_PKT;
1480 }
1481
1482 void
1483 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
1484 {
1485         uint16_t i;
1486         uint8_t existing_mapping_found = 0;
1487
1488         if (port_id_is_invalid(port_id))
1489                 return;
1490
1491         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
1492                 return;
1493
1494         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1495                 printf("map_value not in required range 0..%d\n",
1496                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1497                 return;
1498         }
1499
1500         if (!is_rx) { /*then tx*/
1501                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1502                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1503                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
1504                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
1505                                 existing_mapping_found = 1;
1506                                 break;
1507                         }
1508                 }
1509                 if (!existing_mapping_found) { /* A new additional mapping... */
1510                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
1511                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
1512                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
1513                         nb_tx_queue_stats_mappings++;
1514                 }
1515         }
1516         else { /*rx*/
1517                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1518                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1519                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
1520                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
1521                                 existing_mapping_found = 1;
1522                                 break;
1523                         }
1524                 }
1525                 if (!existing_mapping_found) { /* A new additional mapping... */
1526                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
1527                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
1528                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
1529                         nb_rx_queue_stats_mappings++;
1530                 }
1531         }
1532 }
1533
1534 void
1535 tx_cksum_set(portid_t port_id, uint8_t cksum_mask)
1536 {
1537         uint16_t tx_ol_flags;
1538         if (port_id_is_invalid(port_id))
1539                 return;
1540         /* Clear last 4 bits and then set L3/4 checksum mask again */
1541         tx_ol_flags = (uint16_t) (ports[port_id].tx_ol_flags & 0xFFF0);
1542         ports[port_id].tx_ol_flags = (uint16_t) ((cksum_mask & 0xf) | tx_ol_flags);
1543 }
1544
1545 void
1546 fdir_add_signature_filter(portid_t port_id, uint8_t queue_id,
1547                           struct rte_fdir_filter *fdir_filter)
1548 {
1549         int diag;
1550
1551         if (port_id_is_invalid(port_id))
1552                 return;
1553
1554         diag = rte_eth_dev_fdir_add_signature_filter(port_id, fdir_filter,
1555                                                      queue_id);
1556         if (diag == 0)
1557                 return;
1558
1559         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1560                "diag=%d\n", port_id, diag);
1561 }
1562
1563 void
1564 fdir_update_signature_filter(portid_t port_id, uint8_t queue_id,
1565                              struct rte_fdir_filter *fdir_filter)
1566 {
1567         int diag;
1568
1569         if (port_id_is_invalid(port_id))
1570                 return;
1571
1572         diag = rte_eth_dev_fdir_update_signature_filter(port_id, fdir_filter,
1573                                                         queue_id);
1574         if (diag == 0)
1575                 return;
1576
1577         printf("rte_eth_dev_fdir_update_signature_filter for port_id=%d failed "
1578                "diag=%d\n", port_id, diag);
1579 }
1580
1581 void
1582 fdir_remove_signature_filter(portid_t port_id,
1583                              struct rte_fdir_filter *fdir_filter)
1584 {
1585         int diag;
1586
1587         if (port_id_is_invalid(port_id))
1588                 return;
1589
1590         diag = rte_eth_dev_fdir_remove_signature_filter(port_id, fdir_filter);
1591         if (diag == 0)
1592                 return;
1593
1594         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1595                "diag=%d\n", port_id, diag);
1596
1597 }
1598
1599 void
1600 fdir_get_infos(portid_t port_id)
1601 {
1602         struct rte_eth_fdir fdir_infos;
1603
1604         static const char *fdir_stats_border = "########################";
1605
1606         if (port_id_is_invalid(port_id))
1607                 return;
1608
1609         rte_eth_dev_fdir_get_infos(port_id, &fdir_infos);
1610
1611         printf("\n  %s FDIR infos for port %-2d     %s\n",
1612                fdir_stats_border, port_id, fdir_stats_border);
1613
1614         printf("  collision: %-10"PRIu64"  free:     %"PRIu64"\n"
1615                "  maxhash:   %-10"PRIu64"  maxlen:   %"PRIu64"\n"
1616                "  add:       %-10"PRIu64"  remove:   %"PRIu64"\n"
1617                "  f_add:     %-10"PRIu64"  f_remove: %"PRIu64"\n",
1618                (uint64_t)(fdir_infos.collision), (uint64_t)(fdir_infos.free),
1619                (uint64_t)(fdir_infos.maxhash), (uint64_t)(fdir_infos.maxlen),
1620                fdir_infos.add, fdir_infos.remove,
1621                fdir_infos.f_add, fdir_infos.f_remove);
1622         printf("  %s############################%s\n",
1623                fdir_stats_border, fdir_stats_border);
1624 }
1625
1626 void
1627 fdir_add_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1628                         uint8_t drop, struct rte_fdir_filter *fdir_filter)
1629 {
1630         int diag;
1631
1632         if (port_id_is_invalid(port_id))
1633                 return;
1634
1635         diag = rte_eth_dev_fdir_add_perfect_filter(port_id, fdir_filter,
1636                                                    soft_id, queue_id, drop);
1637         if (diag == 0)
1638                 return;
1639
1640         printf("rte_eth_dev_fdir_add_perfect_filter for port_id=%d failed "
1641                "diag=%d\n", port_id, diag);
1642 }
1643
1644 void
1645 fdir_update_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1646                            uint8_t drop, struct rte_fdir_filter *fdir_filter)
1647 {
1648         int diag;
1649
1650         if (port_id_is_invalid(port_id))
1651                 return;
1652
1653         diag = rte_eth_dev_fdir_update_perfect_filter(port_id, fdir_filter,
1654                                                       soft_id, queue_id, drop);
1655         if (diag == 0)
1656                 return;
1657
1658         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1659                "diag=%d\n", port_id, diag);
1660 }
1661
1662 void
1663 fdir_remove_perfect_filter(portid_t port_id, uint16_t soft_id,
1664                            struct rte_fdir_filter *fdir_filter)
1665 {
1666         int diag;
1667
1668         if (port_id_is_invalid(port_id))
1669                 return;
1670
1671         diag = rte_eth_dev_fdir_remove_perfect_filter(port_id, fdir_filter,
1672                                                       soft_id);
1673         if (diag == 0)
1674                 return;
1675
1676         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1677                "diag=%d\n", port_id, diag);
1678 }
1679
1680 void
1681 fdir_set_masks(portid_t port_id, struct rte_fdir_masks *fdir_masks)
1682 {
1683         int diag;
1684
1685         if (port_id_is_invalid(port_id))
1686                 return;
1687
1688         diag = rte_eth_dev_fdir_set_masks(port_id, fdir_masks);
1689         if (diag == 0)
1690                 return;
1691
1692         printf("rte_eth_dev_set_masks_filter for port_id=%d failed "
1693                "diag=%d\n", port_id, diag);
1694 }
1695
1696 void 
1697 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
1698 {
1699         int diag;
1700         
1701         if (port_id_is_invalid(port_id))
1702                 return;
1703         if (is_rx)
1704                 diag = rte_eth_dev_set_vf_rx(port_id,vf,on);
1705         else
1706                 diag = rte_eth_dev_set_vf_tx(port_id,vf,on);
1707         if (diag == 0)
1708                 return;
1709         if(is_rx)       
1710                 printf("rte_eth_dev_set_vf_rx for port_id=%d failed "
1711                         "diag=%d\n", port_id, diag);
1712         else
1713                 printf("rte_eth_dev_set_vf_tx for port_id=%d failed "
1714                         "diag=%d\n", port_id, diag);
1715                 
1716 }
1717
1718 void
1719 set_vf_rx_vlan(portid_t port_id, uint16_t vlan_id, uint64_t vf_mask, uint8_t on)
1720 {
1721         int diag;
1722
1723         if (port_id_is_invalid(port_id))
1724                 return;
1725         if (vlan_id_is_invalid(vlan_id))
1726                 return;
1727         diag = rte_eth_dev_set_vf_vlan_filter(port_id, vlan_id, vf_mask, on);
1728         if (diag == 0)
1729                 return;
1730         printf("rte_eth_dev_set_vf_vlan_filter for port_id=%d failed "
1731                "diag=%d\n", port_id, diag);
1732 }
1733