remove version in all files
[dpdk.git] / app / test-pmd / config.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdint.h>
41 #include <inttypes.h>
42
43 #include <sys/queue.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_debug.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
53 #include <rte_tailq.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_mempool.h>
61 #include <rte_mbuf.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_string_fns.h>
67
68 #include "testpmd.h"
69
70 static void
71 print_ethaddr(const char *name, struct ether_addr *eth_addr)
72 {
73         printf("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
74                eth_addr->addr_bytes[0],
75                eth_addr->addr_bytes[1],
76                eth_addr->addr_bytes[2],
77                eth_addr->addr_bytes[3],
78                eth_addr->addr_bytes[4],
79                eth_addr->addr_bytes[5]);
80 }
81
82 void
83 nic_stats_display(portid_t port_id)
84 {
85         struct rte_eth_stats stats;
86
87         static const char *nic_stats_border = "########################";
88
89         if (port_id >= nb_ports) {
90                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
91                 return;
92         }
93         rte_eth_stats_get(port_id, &stats);
94         printf("\n  %s NIC statistics for port %-2d %s\n",
95                nic_stats_border, port_id, nic_stats_border);
96         printf("  RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64"RX-bytes: "
97                "%-"PRIu64"\n"
98                "  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64"TX-bytes: "
99                "%-"PRIu64"\n",
100                stats.ipackets, stats.ierrors, stats.ibytes,
101                stats.opackets, stats.oerrors, stats.obytes);
102
103         /* stats fdir */
104         if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
105                 printf("  Fdirmiss:   %-10"PRIu64" Fdirmatch: %-10"PRIu64"\n",
106                        stats.fdirmiss,
107                        stats.fdirmatch);
108
109         printf("  %s############################%s\n",
110                nic_stats_border, nic_stats_border);
111 }
112
113 void
114 nic_stats_clear(portid_t port_id)
115 {
116         if (port_id >= nb_ports) {
117                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
118                 return;
119         }
120         rte_eth_stats_reset(port_id);
121         printf("\n  NIC statistics for port %d cleared\n", port_id);
122 }
123
124 void
125 port_infos_display(portid_t port_id)
126 {
127         struct rte_port *port;
128         struct rte_eth_link link;
129         static const char *info_border = "*********************";
130
131         if (port_id >= nb_ports) {
132                 printf("Invalid port, range is [0, %d]\n", nb_ports - 1);
133                 return;
134         }
135         port = &ports[port_id];
136         rte_eth_link_get(port_id, &link);
137         printf("\n%s Infos for port %-2d %s\n",
138                info_border, port_id, info_border);
139         print_ethaddr("MAC address: ", &port->eth_addr);
140         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
141         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
142         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
143                ("full-duplex") : ("half-duplex"));
144         printf("Promiscuous mode: %s\n",
145                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
146         printf("Allmulticast mode: %s\n",
147                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
148         printf("Maximum number of MAC addresses: %u\n",
149                (unsigned int)(port->dev_info.max_mac_addrs));
150 }
151
152 static int
153 port_id_is_invalid(portid_t port_id)
154 {
155         if (port_id < nb_ports)
156                 return 0;
157         printf("Invalid port %d (must be < nb_ports=%d)\n", port_id, nb_ports);
158         return 1;
159 }
160
161 static int
162 vlan_id_is_invalid(uint16_t vlan_id)
163 {
164         if (vlan_id < 4096)
165                 return 0;
166         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
167         return 1;
168 }
169
170 static int
171 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
172 {
173         uint64_t pci_len;
174
175         if (reg_off & 0x3) {
176                 printf("Port register offset 0x%X not aligned on a 4-byte "
177                        "boundary\n",
178                        (unsigned)reg_off);
179                 return 1;
180         }
181         pci_len = ports[port_id].dev_info.pci_dev->mem_resource.len;
182         if (reg_off >= pci_len) {
183                 printf("Port %d: register offset %u (0x%X) out of port PCI "
184                        "resource (length=%"PRIu64")\n",
185                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
186                 return 1;
187         }
188         return 0;
189 }
190
191 static int
192 reg_bit_pos_is_invalid(uint8_t bit_pos)
193 {
194         if (bit_pos <= 31)
195                 return 0;
196         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
197         return 1;
198 }
199
200 #define display_port_and_reg_off(port_id, reg_off) \
201         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
202
203 static inline void
204 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
205 {
206         display_port_and_reg_off(port_id, (unsigned)reg_off);
207         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
208 }
209
210 void
211 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
212 {
213         uint32_t reg_v;
214
215
216         if (port_id_is_invalid(port_id))
217                 return;
218         if (port_reg_off_is_invalid(port_id, reg_off))
219                 return;
220         if (reg_bit_pos_is_invalid(bit_x))
221                 return;
222         reg_v = port_id_pci_reg_read(port_id, reg_off);
223         display_port_and_reg_off(port_id, (unsigned)reg_off);
224         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
225 }
226
227 void
228 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
229                            uint8_t bit1_pos, uint8_t bit2_pos)
230 {
231         uint32_t reg_v;
232         uint8_t  l_bit;
233         uint8_t  h_bit;
234
235         if (port_id_is_invalid(port_id))
236                 return;
237         if (port_reg_off_is_invalid(port_id, reg_off))
238                 return;
239         if (reg_bit_pos_is_invalid(bit1_pos))
240                 return;
241         if (reg_bit_pos_is_invalid(bit2_pos))
242                 return;
243         if (bit1_pos > bit2_pos)
244                 l_bit = bit2_pos, h_bit = bit1_pos;
245         else
246                 l_bit = bit1_pos, h_bit = bit2_pos;
247
248         reg_v = port_id_pci_reg_read(port_id, reg_off);
249         reg_v >>= l_bit;
250         if (h_bit < 31)
251                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
252         display_port_and_reg_off(port_id, (unsigned)reg_off);
253         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
254                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
255 }
256
257 void
258 port_reg_display(portid_t port_id, uint32_t reg_off)
259 {
260         uint32_t reg_v;
261
262         if (port_id_is_invalid(port_id))
263                 return;
264         if (port_reg_off_is_invalid(port_id, reg_off))
265                 return;
266         reg_v = port_id_pci_reg_read(port_id, reg_off);
267         display_port_reg_value(port_id, reg_off, reg_v);
268 }
269
270 void
271 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
272                  uint8_t bit_v)
273 {
274         uint32_t reg_v;
275
276         if (port_id_is_invalid(port_id))
277                 return;
278         if (port_reg_off_is_invalid(port_id, reg_off))
279                 return;
280         if (reg_bit_pos_is_invalid(bit_pos))
281                 return;
282         if (bit_v > 1) {
283                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
284                 return;
285         }
286         reg_v = port_id_pci_reg_read(port_id, reg_off);
287         if (bit_v == 0)
288                 reg_v &= ~(1 << bit_pos);
289         else
290                 reg_v |= (1 << bit_pos);
291         port_id_pci_reg_write(port_id, reg_off, reg_v);
292         display_port_reg_value(port_id, reg_off, reg_v);
293 }
294
295 void
296 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
297                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
298 {
299         uint32_t max_v;
300         uint32_t reg_v;
301         uint8_t  l_bit;
302         uint8_t  h_bit;
303
304         if (port_id_is_invalid(port_id))
305                 return;
306         if (port_reg_off_is_invalid(port_id, reg_off))
307                 return;
308         if (reg_bit_pos_is_invalid(bit1_pos))
309                 return;
310         if (reg_bit_pos_is_invalid(bit2_pos))
311                 return;
312         if (bit1_pos > bit2_pos)
313                 l_bit = bit2_pos, h_bit = bit1_pos;
314         else
315                 l_bit = bit1_pos, h_bit = bit2_pos;
316
317         if ((h_bit - l_bit) < 31)
318                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
319         else
320                 max_v = 0xFFFFFFFF;
321
322         if (value > max_v) {
323                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
324                                 (unsigned)value, (unsigned)value,
325                                 (unsigned)max_v, (unsigned)max_v);
326                 return;
327         }
328         reg_v = port_id_pci_reg_read(port_id, reg_off);
329         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
330         reg_v |= (value << l_bit); /* Set changed bits */
331         port_id_pci_reg_write(port_id, reg_off, reg_v);
332         display_port_reg_value(port_id, reg_off, reg_v);
333 }
334
335 void
336 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
337 {
338         if (port_id_is_invalid(port_id))
339                 return;
340         if (port_reg_off_is_invalid(port_id, reg_off))
341                 return;
342         port_id_pci_reg_write(port_id, reg_off, reg_v);
343         display_port_reg_value(port_id, reg_off, reg_v);
344 }
345
346 /*
347  * RX/TX ring descriptors display functions.
348  */
349 static int
350 rx_queue_id_is_invalid(queueid_t rxq_id)
351 {
352         if (rxq_id < nb_rxq)
353                 return 0;
354         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
355         return 1;
356 }
357
358 static int
359 tx_queue_id_is_invalid(queueid_t txq_id)
360 {
361         if (txq_id < nb_txq)
362                 return 0;
363         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
364         return 1;
365 }
366
367 static int
368 rx_desc_id_is_invalid(uint16_t rxdesc_id)
369 {
370         if (rxdesc_id < nb_rxd)
371                 return 0;
372         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
373                rxdesc_id, nb_rxd);
374         return 1;
375 }
376
377 static int
378 tx_desc_id_is_invalid(uint16_t txdesc_id)
379 {
380         if (txdesc_id < nb_txd)
381                 return 0;
382         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
383                txdesc_id, nb_txd);
384         return 1;
385 }
386
387 static const struct rte_memzone *
388 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
389 {
390         char mz_name[RTE_MEMZONE_NAMESIZE];
391         const struct rte_memzone *mz;
392
393         rte_snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
394                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
395         mz = rte_memzone_lookup(mz_name);
396         if (mz == NULL)
397                 printf("%s ring memory zoneof (port %d, queue %d) not"
398                        "found (zone name = %s\n",
399                        ring_name, port_id, q_id, mz_name);
400         return (mz);
401 }
402
403 union igb_ring_dword {
404         uint64_t dword;
405         struct {
406                 uint32_t hi;
407                 uint32_t lo;
408         } words;
409 };
410
411 struct igb_ring_desc {
412         union igb_ring_dword lo_dword;
413         union igb_ring_dword hi_dword;
414 };
415
416 static void
417 ring_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
418 {
419         struct igb_ring_desc *ring;
420         struct igb_ring_desc rd;
421
422         ring = (struct igb_ring_desc *) ring_mz->addr;
423         rd.lo_dword = rte_le_to_cpu_64(ring[desc_id].lo_dword);
424         rd.hi_dword = rte_le_to_cpu_64(ring[desc_id].hi_dword);
425         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
426                 (unsigned)rd.lo_dword.words.lo, (unsigned)rd.lo_dword.words.hi,
427                 (unsigned)rd.hi_dword.words.lo, (unsigned)rd.hi_dword.words.hi);
428 }
429
430 void
431 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
432 {
433         const struct rte_memzone *rx_mz;
434
435         if (port_id_is_invalid(port_id))
436                 return;
437         if (rx_queue_id_is_invalid(rxq_id))
438                 return;
439         if (rx_desc_id_is_invalid(rxd_id))
440                 return;
441         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
442         if (rx_mz == NULL)
443                 return;
444         ring_descriptor_display(rx_mz, rxd_id);
445 }
446
447 void
448 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
449 {
450         const struct rte_memzone *tx_mz;
451
452         if (port_id_is_invalid(port_id))
453                 return;
454         if (tx_queue_id_is_invalid(txq_id))
455                 return;
456         if (tx_desc_id_is_invalid(txd_id))
457                 return;
458         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
459         if (tx_mz == NULL)
460                 return;
461         ring_descriptor_display(tx_mz, txd_id);
462 }
463
464 void
465 fwd_lcores_config_display(void)
466 {
467         lcoreid_t lc_id;
468
469         printf("List of forwarding lcores:");
470         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
471                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
472         printf("\n");
473 }
474 void
475 rxtx_config_display(void)
476 {
477         printf("  %s packet forwarding - CRC stripping %s - "
478                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
479                rx_mode.hw_strip_crc ? "enabled" : "disabled",
480                nb_pkt_per_burst);
481
482         if (cur_fwd_eng == &tx_only_engine)
483                 printf("  packet len=%u - nb packet segments=%d\n",
484                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
485
486         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
487                nb_fwd_lcores, nb_fwd_ports);
488         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
489                nb_rxq, nb_rxd, rx_free_thresh);
490         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
491                rx_thresh.pthresh, rx_thresh.hthresh, rx_thresh.wthresh);
492         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
493                nb_txq, nb_txd, tx_free_thresh);
494         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
495                tx_thresh.pthresh, tx_thresh.hthresh, tx_thresh.wthresh);
496         printf("  TX RS bit threshold=%d\n", tx_rs_thresh);
497 }
498
499 /*
500  * Setup forwarding configuration for each logical core.
501  */
502 static void
503 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
504 {
505         streamid_t nb_fs_per_lcore;
506         streamid_t nb_fs;
507         streamid_t sm_id;
508         lcoreid_t  nb_extra;
509         lcoreid_t  nb_fc;
510         lcoreid_t  nb_lc;
511         lcoreid_t  lc_id;
512
513         nb_fs = cfg->nb_fwd_streams;
514         nb_fc = cfg->nb_fwd_lcores;
515         if (nb_fs <= nb_fc) {
516                 nb_fs_per_lcore = 1;
517                 nb_extra = 0;
518         } else {
519                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
520                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
521         }
522         nb_extra = (lcoreid_t) (nb_fs % nb_fc);
523
524         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
525         sm_id = 0;
526         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
527                 fwd_lcores[lc_id]->stream_idx = sm_id;
528                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
529                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
530         }
531
532         /*
533          * Assign extra remaining streams, if any.
534          */
535         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
536         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
537                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
538                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
539                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
540         }
541 }
542
543 static void
544 simple_fwd_config_setup(void)
545 {
546         portid_t i;
547         portid_t j;
548         portid_t inc = 2;
549
550         if (nb_fwd_ports % 2) {
551                 if (port_topology == PORT_TOPOLOGY_CHAINED) {
552                         inc = 1;
553                 }
554                 else {
555                         printf("\nWarning! Cannot handle an odd number of ports "
556                                "with the current port topology. Configuration "
557                                "must be changed to have an even number of ports, "
558                                "or relaunch application with "
559                                "--port-topology=chained\n\n");
560                 }
561         }
562
563         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
564         cur_fwd_config.nb_fwd_streams =
565                 (streamid_t) cur_fwd_config.nb_fwd_ports;
566
567         /*
568          * In the simple forwarding test, the number of forwarding cores
569          * must be lower or equal to the number of forwarding ports.
570          */
571         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
572         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
573                 cur_fwd_config.nb_fwd_lcores =
574                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
575         setup_fwd_config_of_each_lcore(&cur_fwd_config);
576
577         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
578                 j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
579                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
580                 fwd_streams[i]->rx_queue  = 0;
581                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
582                 fwd_streams[i]->tx_queue  = 0;
583                 fwd_streams[i]->peer_addr = j;
584
585                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
586                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
587                         fwd_streams[j]->rx_queue  = 0;
588                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
589                         fwd_streams[j]->tx_queue  = 0;
590                         fwd_streams[j]->peer_addr = i;
591                 }
592         }
593 }
594
595 /**
596  * For the RSS forwarding test, each core is assigned on every port a transmit
597  * queue whose index is the index of the core itself. This approach limits the
598  * maximumm number of processing cores of the RSS test to the maximum number of
599  * TX queues supported by the devices.
600  *
601  * Each core is assigned a single stream, each stream being composed of
602  * a RX queue to poll on a RX port for input messages, associated with
603  * a TX queue of a TX port where to send forwarded packets.
604  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
605  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
606  * following rules:
607  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
608  *    - TxQl = RxQj
609  */
610 static void
611 rss_fwd_config_setup(void)
612 {
613         portid_t   rxp;
614         portid_t   txp;
615         queueid_t  rxq;
616         queueid_t  nb_q;
617         lcoreid_t  lc_id;
618
619         nb_q = nb_rxq;
620         if (nb_q > nb_txq)
621                 nb_q = nb_txq;
622         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
623         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
624         cur_fwd_config.nb_fwd_streams =
625                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
626         if (cur_fwd_config.nb_fwd_streams > cur_fwd_config.nb_fwd_lcores)
627                 cur_fwd_config.nb_fwd_streams =
628                         (streamid_t)cur_fwd_config.nb_fwd_lcores;
629         else
630                 cur_fwd_config.nb_fwd_lcores =
631                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
632         setup_fwd_config_of_each_lcore(&cur_fwd_config);
633         rxp = 0; rxq = 0;
634         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
635                 struct fwd_stream *fs;
636
637                 fs = fwd_streams[lc_id];
638                 if ((rxp & 0x1) == 0)
639                         txp = (portid_t) (rxp + 1);
640                 else
641                         txp = (portid_t) (rxp - 1);
642                 fs->rx_port = fwd_ports_ids[rxp];
643                 fs->rx_queue = rxq;
644                 fs->tx_port = fwd_ports_ids[txp];
645                 fs->tx_queue = rxq;
646                 fs->peer_addr = fs->tx_port;
647                 rxq = (queueid_t) (rxq + 1);
648                 if (rxq < nb_q)
649                         continue;
650                 /*
651                  * rxq == nb_q
652                  * Restart from RX queue 0 on next RX port
653                  */
654                 rxq = 0;
655                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
656                         rxp = (portid_t)
657                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
658                 else
659                         rxp = (portid_t) (rxp + 1);
660         }
661 }
662
663 void
664 fwd_config_setup(void)
665 {
666         cur_fwd_config.fwd_eng = cur_fwd_eng;
667         if ((nb_rxq > 1) && (nb_txq > 1))
668                 rss_fwd_config_setup();
669         else
670                 simple_fwd_config_setup();
671 }
672
673 static void
674 pkt_fwd_config_display(struct fwd_config *cfg)
675 {
676         struct fwd_stream *fs;
677         lcoreid_t  lc_id;
678         streamid_t sm_id;
679
680         printf("%s packet forwarding - ports=%d - cores=%d - streams=%d - "
681                "NUMA support %s\n",
682                cfg->fwd_eng->fwd_mode_name,
683                cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
684                numa_support == 1 ? "enabled" : "disabled");
685         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
686                 printf("Logical Core %u (socket %u) forwards packets on "
687                        "%d streams:",
688                        fwd_lcores_cpuids[lc_id],
689                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
690                        fwd_lcores[lc_id]->stream_nb);
691                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
692                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
693                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
694                                "P=%d/Q=%d (socket %u) ",
695                                fs->rx_port, fs->rx_queue,
696                                ports[fs->rx_port].socket_id,
697                                fs->tx_port, fs->tx_queue,
698                                ports[fs->tx_port].socket_id);
699                         print_ethaddr("peer=",
700                                       &peer_eth_addrs[fs->peer_addr]);
701                 }
702                 printf("\n");
703         }
704         printf("\n");
705 }
706
707
708 void
709 fwd_config_display(void)
710 {
711         fwd_config_setup();
712         pkt_fwd_config_display(&cur_fwd_config);
713 }
714
715 void
716 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
717 {
718         unsigned int i;
719         unsigned int lcore_cpuid;
720         int record_now;
721
722         record_now = 0;
723  again:
724         for (i = 0; i < nb_lc; i++) {
725                 lcore_cpuid = lcorelist[i];
726                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
727                         printf("Logical core %u not enabled\n", lcore_cpuid);
728                         return;
729                 }
730                 if (lcore_cpuid == rte_get_master_lcore()) {
731                         printf("Master core %u cannot forward packets\n",
732                                lcore_cpuid);
733                         return;
734                 }
735                 if (record_now)
736                         fwd_lcores_cpuids[i] = lcore_cpuid;
737         }
738         if (record_now == 0) {
739                 record_now = 1;
740                 goto again;
741         }
742         nb_cfg_lcores = (lcoreid_t) nb_lc;
743         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
744                 printf("previous number of forwarding cores %u - changed to "
745                        "number of configured cores %u\n",
746                        (unsigned int) nb_fwd_lcores, nb_lc);
747                 nb_fwd_lcores = (lcoreid_t) nb_lc;
748         }
749 }
750
751 void
752 set_fwd_lcores_mask(uint64_t lcoremask)
753 {
754         unsigned int lcorelist[64];
755         unsigned int nb_lc;
756         unsigned int i;
757
758         if (lcoremask == 0) {
759                 printf("Invalid NULL mask of cores\n");
760                 return;
761         }
762         nb_lc = 0;
763         for (i = 0; i < 64; i++) {
764                 if (! ((uint64_t)(1ULL << i) & lcoremask))
765                         continue;
766                 lcorelist[nb_lc++] = i;
767         }
768         set_fwd_lcores_list(lcorelist, nb_lc);
769 }
770
771 void
772 set_fwd_lcores_number(uint16_t nb_lc)
773 {
774         if (nb_lc > nb_cfg_lcores) {
775                 printf("nb fwd cores %u > %u (max. number of configured "
776                        "lcores) - ignored\n",
777                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
778                 return;
779         }
780         nb_fwd_lcores = (lcoreid_t) nb_lc;
781         printf("Number of forwarding cores set to %u\n",
782                (unsigned int) nb_fwd_lcores);
783 }
784
785 void
786 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
787 {
788         unsigned int i;
789         portid_t port_id;
790         int record_now;
791
792         record_now = 0;
793  again:
794         for (i = 0; i < nb_pt; i++) {
795                 port_id = (portid_t) portlist[i];
796                 if (port_id >= nb_ports) {
797                         printf("Invalid port id %u > %u\n",
798                                (unsigned int) port_id,
799                                (unsigned int) nb_ports);
800                         return;
801                 }
802                 if (record_now)
803                         fwd_ports_ids[i] = port_id;
804         }
805         if (record_now == 0) {
806                 record_now = 1;
807                 goto again;
808         }
809         nb_cfg_ports = (portid_t) nb_pt;
810         if (nb_fwd_ports != (portid_t) nb_pt) {
811                 printf("previous number of forwarding ports %u - changed to "
812                        "number of configured ports %u\n",
813                        (unsigned int) nb_fwd_ports, nb_pt);
814                 nb_fwd_ports = (portid_t) nb_pt;
815         }
816 }
817
818 void
819 set_fwd_ports_mask(uint64_t portmask)
820 {
821         unsigned int portlist[64];
822         unsigned int nb_pt;
823         unsigned int i;
824
825         if (portmask == 0) {
826                 printf("Invalid NULL mask of ports\n");
827                 return;
828         }
829         nb_pt = 0;
830         for (i = 0; i < 64; i++) {
831                 if (! ((uint64_t)(1ULL << i) & portmask))
832                         continue;
833                 portlist[nb_pt++] = i;
834         }
835         set_fwd_ports_list(portlist, nb_pt);
836 }
837
838 void
839 set_fwd_ports_number(uint16_t nb_pt)
840 {
841         if (nb_pt > nb_cfg_ports) {
842                 printf("nb fwd ports %u > %u (number of configured "
843                        "ports) - ignored\n",
844                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
845                 return;
846         }
847         nb_fwd_ports = (portid_t) nb_pt;
848         printf("Number of forwarding ports set to %u\n",
849                (unsigned int) nb_fwd_ports);
850 }
851
852 void
853 set_nb_pkt_per_burst(uint16_t nb)
854 {
855         if (nb > MAX_PKT_BURST) {
856                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
857                        " ignored\n",
858                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
859                 return;
860         }
861         nb_pkt_per_burst = nb;
862         printf("Number of packets per burst set to %u\n",
863                (unsigned int) nb_pkt_per_burst);
864 }
865
866 void
867 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
868 {
869         uint16_t tx_pkt_len;
870         unsigned i;
871
872         if (nb_segs >= (unsigned) nb_txd) {
873                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
874                        nb_segs, (unsigned int) nb_txd);
875                 return;
876         }
877
878         /*
879          * Check that each segment length is greater or equal than
880          * the mbuf data sise.
881          * Check also that the total packet length is greater or equal than the
882          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
883          */
884         tx_pkt_len = 0;
885         for (i = 0; i < nb_segs; i++) {
886                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
887                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
888                                i, seg_lengths[i], (unsigned) mbuf_data_size);
889                         return;
890                 }
891                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
892         }
893         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
894                 printf("total packet length=%u < %d - give up\n",
895                                 (unsigned) tx_pkt_len,
896                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
897                 return;
898         }
899
900         for (i = 0; i < nb_segs; i++)
901                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
902
903         tx_pkt_length  = tx_pkt_len;
904         tx_pkt_nb_segs = (uint8_t) nb_segs;
905 }
906
907 void
908 set_pkt_forwarding_mode(const char *fwd_mode_name)
909 {
910         struct fwd_engine *fwd_eng;
911         unsigned i;
912
913         i = 0;
914         while ((fwd_eng = fwd_engines[i]) != NULL) {
915                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
916                         printf("Set %s packet forwarding mode\n",
917                                fwd_mode_name);
918                         cur_fwd_eng = fwd_eng;
919                         return;
920                 }
921                 i++;
922         }
923         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
924 }
925
926 void
927 set_verbose_level(uint16_t vb_level)
928 {
929         printf("Change verbose level from %u to %u\n",
930                (unsigned int) verbose_level, (unsigned int) vb_level);
931         verbose_level = vb_level;
932 }
933
934 void
935 rx_vlan_filter_set(portid_t port_id, uint16_t vlan_id, int on)
936 {
937         int diag;
938
939         if (port_id_is_invalid(port_id))
940                 return;
941         if (vlan_id_is_invalid(vlan_id))
942                 return;
943         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
944         if (diag == 0)
945                 return;
946         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
947                "diag=%d\n",
948                port_id, vlan_id, on, diag);
949 }
950
951 void
952 rx_vlan_all_filter_set(portid_t port_id, int on)
953 {
954         uint16_t vlan_id;
955
956         if (port_id_is_invalid(port_id))
957                 return;
958         for (vlan_id = 0; vlan_id < 4096; vlan_id++)
959                 rx_vlan_filter_set(port_id, vlan_id, on);
960 }
961
962 void
963 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
964 {
965         if (port_id_is_invalid(port_id))
966                 return;
967         if (vlan_id_is_invalid(vlan_id))
968                 return;
969         ports[port_id].tx_ol_flags |= PKT_TX_VLAN_PKT;
970         ports[port_id].tx_vlan_id = vlan_id;
971 }
972
973 void
974 tx_vlan_reset(portid_t port_id)
975 {
976         if (port_id_is_invalid(port_id))
977                 return;
978         ports[port_id].tx_ol_flags &= ~PKT_TX_VLAN_PKT;
979 }
980
981 void
982 tx_cksum_set(portid_t port_id, uint8_t cksum_mask)
983 {
984         uint16_t tx_ol_flags;
985         if (port_id_is_invalid(port_id))
986                 return;
987         /* Clear last 4 bits and then set L3/4 checksum mask again */
988         tx_ol_flags = (uint16_t) (ports[port_id].tx_ol_flags & 0xFFF0);
989         ports[port_id].tx_ol_flags = (uint16_t) ((cksum_mask & 0xf) | tx_ol_flags);
990 }
991
992 void
993 fdir_add_signature_filter(portid_t port_id, uint8_t queue_id,
994                           struct rte_fdir_filter *fdir_filter)
995 {
996         int diag;
997
998         if (port_id_is_invalid(port_id))
999                 return;
1000
1001         diag = rte_eth_dev_fdir_add_signature_filter(port_id, fdir_filter,
1002                                                      queue_id);
1003         if (diag == 0)
1004                 return;
1005
1006         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1007                "diag=%d\n", port_id, diag);
1008 }
1009
1010 void
1011 fdir_update_signature_filter(portid_t port_id, uint8_t queue_id,
1012                              struct rte_fdir_filter *fdir_filter)
1013 {
1014         int diag;
1015
1016         if (port_id_is_invalid(port_id))
1017                 return;
1018
1019         diag = rte_eth_dev_fdir_update_signature_filter(port_id, fdir_filter,
1020                                                         queue_id);
1021         if (diag == 0)
1022                 return;
1023
1024         printf("rte_eth_dev_fdir_update_signature_filter for port_id=%d failed "
1025                "diag=%d\n", port_id, diag);
1026 }
1027
1028 void
1029 fdir_remove_signature_filter(portid_t port_id,
1030                              struct rte_fdir_filter *fdir_filter)
1031 {
1032         int diag;
1033
1034         if (port_id_is_invalid(port_id))
1035                 return;
1036
1037         diag = rte_eth_dev_fdir_remove_signature_filter(port_id, fdir_filter);
1038         if (diag == 0)
1039                 return;
1040
1041         printf("rte_eth_dev_fdir_add_signature_filter for port_id=%d failed "
1042                "diag=%d\n", port_id, diag);
1043
1044 }
1045
1046 void
1047 fdir_get_infos(portid_t port_id)
1048 {
1049         struct rte_eth_fdir fdir_infos;
1050
1051         static const char *fdir_stats_border = "########################";
1052
1053         if (port_id_is_invalid(port_id))
1054                 return;
1055
1056         rte_eth_dev_fdir_get_infos(port_id, &fdir_infos);
1057
1058         printf("\n  %s FDIR infos for port %-2d %s\n",
1059                fdir_stats_border, port_id, fdir_stats_border);
1060
1061         printf("  collision: %-10"PRIu64" free: %-10"PRIu64"\n"
1062                "  maxhash: %-10"PRIu64" maxlen: %-10"PRIu64"\n"
1063                "  add : %-10"PRIu64"   remove : %-10"PRIu64"\n"
1064                "  f_add: %-10"PRIu64" f_remove: %-10"PRIu64"\n",
1065                (uint64_t)(fdir_infos.collision), (uint64_t)(fdir_infos.free),
1066                (uint64_t)(fdir_infos.maxhash), (uint64_t)(fdir_infos.maxlen),
1067                fdir_infos.add, fdir_infos.remove,
1068                fdir_infos.f_add, fdir_infos.f_remove);
1069         printf("  %s############################%s\n",
1070                fdir_stats_border, fdir_stats_border);
1071 }
1072
1073 void
1074 fdir_add_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1075                         uint8_t drop, struct rte_fdir_filter *fdir_filter)
1076 {
1077         int diag;
1078
1079         if (port_id_is_invalid(port_id))
1080                 return;
1081
1082         diag = rte_eth_dev_fdir_add_perfect_filter(port_id, fdir_filter,
1083                                                    soft_id, queue_id, drop);
1084         if (diag == 0)
1085                 return;
1086
1087         printf("rte_eth_dev_fdir_add_perfect_filter for port_id=%d failed "
1088                "diag=%d\n", port_id, diag);
1089 }
1090
1091 void
1092 fdir_update_perfect_filter(portid_t port_id, uint16_t soft_id, uint8_t queue_id,
1093                            uint8_t drop, struct rte_fdir_filter *fdir_filter)
1094 {
1095         int diag;
1096
1097         if (port_id_is_invalid(port_id))
1098                 return;
1099
1100         diag = rte_eth_dev_fdir_update_perfect_filter(port_id, fdir_filter,
1101                                                       soft_id, queue_id, drop);
1102         if (diag == 0)
1103                 return;
1104
1105         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1106                "diag=%d\n", port_id, diag);
1107 }
1108
1109 void
1110 fdir_remove_perfect_filter(portid_t port_id, uint16_t soft_id,
1111                            struct rte_fdir_filter *fdir_filter)
1112 {
1113         int diag;
1114
1115         if (port_id_is_invalid(port_id))
1116                 return;
1117
1118         diag = rte_eth_dev_fdir_remove_perfect_filter(port_id, fdir_filter,
1119                                                       soft_id);
1120         if (diag == 0)
1121                 return;
1122
1123         printf("rte_eth_dev_fdir_update_perfect_filter for port_id=%d failed "
1124                "diag=%d\n", port_id, diag);
1125 }
1126
1127 void
1128 fdir_set_masks(portid_t port_id, struct rte_fdir_masks *fdir_masks)
1129 {
1130         int diag;
1131
1132         if (port_id_is_invalid(port_id))
1133                 return;
1134
1135         diag = rte_eth_dev_fdir_set_masks(port_id, fdir_masks);
1136         if (diag == 0)
1137                 return;
1138
1139         printf("rte_eth_dev_set_masks_filter for port_id=%d failed "
1140                "diag=%d\n", port_id, diag);
1141 }