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