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