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