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