first public release
[dpdk.git] / app / test-pmd / testpmd.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 <stdio.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <string.h>
41 #include <time.h>
42 #include <fcntl.h>
43 #include <sys/types.h>
44 #include <errno.h>
45
46 #include <sys/queue.h>
47 #include <sys/stat.h>
48
49 #include <stdint.h>
50 #include <unistd.h>
51 #include <inttypes.h>
52
53 #include <rte_common.h>
54 #include <rte_byteorder.h>
55 #include <rte_log.h>
56 #include <rte_debug.h>
57 #include <rte_cycles.h>
58 #include <rte_memory.h>
59 #include <rte_memcpy.h>
60 #include <rte_memzone.h>
61 #include <rte_launch.h>
62 #include <rte_tailq.h>
63 #include <rte_eal.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_atomic.h>
67 #include <rte_branch_prediction.h>
68 #include <rte_ring.h>
69 #include <rte_mempool.h>
70 #include <rte_malloc.h>
71 #include <rte_mbuf.h>
72 #include <rte_interrupts.h>
73 #include <rte_pci.h>
74 #include <rte_ether.h>
75 #include <rte_ethdev.h>
76 #include <rte_string_fns.h>
77
78 #include "testpmd.h"
79
80 uint16_t verbose_level = 0; /**< Silent by default. */
81
82 /* use master core for command line ? */
83 uint8_t interactive = 0;
84
85 /*
86  * NUMA support configuration.
87  * When set, the NUMA support attempts to dispatch the allocation of the
88  * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
89  * probed ports among the CPU sockets 0 and 1.
90  * Otherwise, all memory is allocated from CPU socket 0.
91  */
92 uint8_t numa_support = 0; /**< No numa support by default */
93
94 /*
95  * Record the Ethernet address of peer target ports to which packets are
96  * forwarded.
97  * Must be instanciated with the ethernet addresses of peer traffic generator
98  * ports.
99  */
100 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
101 portid_t nb_peer_eth_addrs = 0;
102
103 /*
104  * Probed Target Environment.
105  */
106 struct rte_port *ports;        /**< For all probed ethernet ports. */
107 portid_t nb_ports;             /**< Number of probed ethernet ports. */
108 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
109 lcoreid_t nb_lcores;           /**< Number of probed logical cores. */
110
111 /*
112  * Test Forwarding Configuration.
113  *    nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
114  *    nb_fwd_ports  <= nb_cfg_ports  <= nb_ports
115  */
116 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
117 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
118 portid_t  nb_cfg_ports;  /**< Number of configured ports. */
119 portid_t  nb_fwd_ports;  /**< Number of forwarding ports. */
120
121 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
122 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS];      /**< Port ids configuration. */
123
124 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
125 streamid_t nb_fwd_streams;       /**< Is equal to (nb_ports * nb_rxq). */
126
127 /*
128  * Forwarding engines.
129  */
130 struct fwd_engine * fwd_engines[] = {
131         &io_fwd_engine,
132         &mac_fwd_engine,
133         &rx_only_engine,
134         &tx_only_engine,
135         &csum_fwd_engine,
136 #ifdef RTE_LIBRTE_IEEE1588
137         &ieee1588_fwd_engine,
138 #endif
139         NULL,
140 };
141
142 struct fwd_config cur_fwd_config;
143 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
144
145 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
146
147 /*
148  * Configuration of packet segments used by the "txonly" processing engine.
149  */
150 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
151 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
152         TXONLY_DEF_PACKET_LEN,
153 };
154 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
155
156 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
157 uint16_t mb_mempool_cache = DEF_PKT_BURST; /**< Size of mbuf mempool cache. */
158
159 /*
160  * Ethernet Ports Configuration.
161  */
162 int promiscuous_on = 1; /**< Ports set in promiscuous mode by default. */
163
164 /*
165  * Configurable number of RX/TX queues.
166  */
167 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
168 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
169
170 /*
171  * Configurable number of RX/TX ring descriptors.
172  */
173 #define RTE_TEST_RX_DESC_DEFAULT 128
174 #define RTE_TEST_TX_DESC_DEFAULT 512
175 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
176 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
177
178 /*
179  * Configurable values of RX and TX ring threshold registers.
180  */
181 #define RX_PTHRESH 8 /**< Default value of RX prefetch threshold register. */
182 #define RX_HTHRESH 8 /**< Default value of RX host threshold register. */
183 #define RX_WTHRESH 4 /**< Default value of RX write-back threshold register. */
184
185 #define TX_PTHRESH 36 /**< Default value of TX prefetch threshold register. */
186 #define TX_HTHRESH 0 /**< Default value of TX host threshold register. */
187 #define TX_WTHRESH 0 /**< Default value of TX write-back threshold register. */
188
189 struct rte_eth_thresh rx_thresh = {
190         .pthresh = RX_PTHRESH,
191         .hthresh = RX_HTHRESH,
192         .wthresh = RX_WTHRESH,
193 };
194
195 struct rte_eth_thresh tx_thresh = {
196         .pthresh = TX_PTHRESH,
197         .hthresh = TX_HTHRESH,
198         .wthresh = TX_WTHRESH,
199 };
200
201 /*
202  * Configurable value of RX free threshold.
203  */
204 uint16_t rx_free_thresh = 0; /* Immediately free RX descriptors by default. */
205
206 /*
207  * Configurable value of TX free threshold.
208  */
209 uint16_t tx_free_thresh = 0; /* Use default values. */
210
211 /*
212  * Configurable value of TX RS bit threshold.
213  */
214 uint16_t tx_rs_thresh = 0; /* Use default values. */
215
216 /*
217  * Receive Side Scaling (RSS) configuration.
218  */
219 uint16_t rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6; /* RSS IP by default. */
220
221 /*
222  * Port topology configuration
223  */
224 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
225
226 /*
227  * Ethernet device configuration.
228  */
229 struct rte_eth_rxmode rx_mode = {
230         .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
231         .split_hdr_size = 0,
232         .header_split   = 0, /**< Header Split disabled. */
233         .hw_ip_checksum = 0, /**< IP checksum offload disabled. */
234         .hw_vlan_filter = 1, /**< VLAN filtering enabled. */
235         .jumbo_frame    = 0, /**< Jumbo Frame Support disabled. */
236         .hw_strip_crc   = 0, /**< CRC stripping by hardware disabled. */
237 };
238
239 struct rte_fdir_conf fdir_conf = {
240         .mode = RTE_FDIR_MODE_NONE,
241         .pballoc = RTE_FDIR_PBALLOC_64K,
242         .status = RTE_FDIR_REPORT_STATUS,
243         .flexbytes_offset = 0x6,
244         .drop_queue = 127,
245 };
246
247 static volatile int test_done = 1; /* stop packet forwarding when set to 1. */
248
249 /*
250  * Setup default configuration.
251  */
252 static void
253 set_default_fwd_lcores_config(void)
254 {
255         unsigned int i;
256         unsigned int nb_lc;
257
258         nb_lc = 0;
259         for (i = 0; i < RTE_MAX_LCORE; i++) {
260                 if (! rte_lcore_is_enabled(i))
261                         continue;
262                 if (i == rte_get_master_lcore())
263                         continue;
264                 fwd_lcores_cpuids[nb_lc++] = i;
265         }
266         nb_lcores = (lcoreid_t) nb_lc;
267         nb_cfg_lcores = nb_lcores;
268         nb_fwd_lcores = 1;
269 }
270
271 static void
272 set_def_peer_eth_addrs(void)
273 {
274         portid_t i;
275
276         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
277                 peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
278                 peer_eth_addrs[i].addr_bytes[5] = i;
279         }
280 }
281
282 static void
283 set_default_fwd_ports_config(void)
284 {
285         portid_t pt_id;
286
287         for (pt_id = 0; pt_id < nb_ports; pt_id++)
288                 fwd_ports_ids[pt_id] = pt_id;
289
290         nb_cfg_ports = nb_ports;
291         nb_fwd_ports = nb_ports;
292 }
293
294 void
295 set_def_fwd_config(void)
296 {
297         set_default_fwd_lcores_config();
298         set_def_peer_eth_addrs();
299         set_default_fwd_ports_config();
300 }
301
302 /*
303  * Configuration initialisation done once at init time.
304  */
305 struct mbuf_ctor_arg {
306         uint16_t seg_buf_offset; /**< offset of data in data segment of mbuf. */
307         uint16_t seg_buf_size;   /**< size of data segment in mbuf. */
308 };
309
310 struct mbuf_pool_ctor_arg {
311         uint16_t seg_buf_size; /**< size of data segment in mbuf. */
312 };
313
314 static void
315 testpmd_mbuf_ctor(struct rte_mempool *mp,
316                   void *opaque_arg,
317                   void *raw_mbuf,
318                   __attribute__((unused)) unsigned i)
319 {
320         struct mbuf_ctor_arg *mb_ctor_arg;
321         struct rte_mbuf    *mb;
322
323         mb_ctor_arg = (struct mbuf_ctor_arg *) opaque_arg;
324         mb = (struct rte_mbuf *) raw_mbuf;
325
326         mb->pool         = mp;
327         mb->buf_addr     = (void *) ((char *)mb + mb_ctor_arg->seg_buf_offset);
328         mb->buf_physaddr = (uint64_t) (rte_mempool_virt2phy(mp, mb) +
329                         mb_ctor_arg->seg_buf_offset);
330         mb->buf_len      = mb_ctor_arg->seg_buf_size;
331         mb->type         = RTE_MBUF_PKT;
332         mb->ol_flags     = 0;
333         mb->pkt.data     = (char *) mb->buf_addr + RTE_PKTMBUF_HEADROOM;
334         mb->pkt.nb_segs  = 1;
335         mb->pkt.l2_len = 0;
336         mb->pkt.l3_len = 0;
337         mb->pkt.vlan_tci = 0;
338         mb->pkt.hash.rss = 0;
339 }
340
341 static void
342 testpmd_mbuf_pool_ctor(struct rte_mempool *mp,
343                        void *opaque_arg)
344 {
345         struct mbuf_pool_ctor_arg      *mbp_ctor_arg;
346         struct rte_pktmbuf_pool_private *mbp_priv;
347
348         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
349                 printf("%s(%s) private_data_size %d < %d\n",
350                        __func__, mp->name, (int) mp->private_data_size,
351                        (int) sizeof(struct rte_pktmbuf_pool_private));
352                 return;
353         }
354         mbp_ctor_arg = (struct mbuf_pool_ctor_arg *) opaque_arg;
355         mbp_priv = (struct rte_pktmbuf_pool_private *)
356                 ((char *)mp + sizeof(struct rte_mempool));
357         mbp_priv->mbuf_data_room_size = mbp_ctor_arg->seg_buf_size;
358 }
359
360 static void
361 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
362                  unsigned int socket_id)
363 {
364         char pool_name[RTE_MEMPOOL_NAMESIZE];
365         struct rte_mempool *rte_mp;
366         struct mbuf_pool_ctor_arg mbp_ctor_arg;
367         struct mbuf_ctor_arg mb_ctor_arg;
368         uint32_t mb_size;
369
370         mbp_ctor_arg.seg_buf_size = (uint16_t) (RTE_PKTMBUF_HEADROOM +
371                                                 mbuf_seg_size);
372         mb_ctor_arg.seg_buf_offset =
373                 (uint16_t) CACHE_LINE_ROUNDUP(sizeof(struct rte_mbuf));
374         mb_ctor_arg.seg_buf_size = mbp_ctor_arg.seg_buf_size;
375         mb_size = mb_ctor_arg.seg_buf_offset + mb_ctor_arg.seg_buf_size;
376         mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
377         rte_mp = rte_mempool_create(pool_name, nb_mbuf, (unsigned) mb_size,
378                                     (unsigned) mb_mempool_cache,
379                                     sizeof(struct rte_pktmbuf_pool_private),
380                                     testpmd_mbuf_pool_ctor, &mbp_ctor_arg,
381                                     testpmd_mbuf_ctor, &mb_ctor_arg,
382                                     socket_id, 0);
383         if (rte_mp == NULL) {
384                 rte_exit(EXIT_FAILURE, "Creation of mbuf pool for socket %u failed\n",
385                        socket_id);
386         }
387 }
388
389 static void
390 init_config(void)
391 {
392         struct rte_port *port;
393         struct rte_mempool *mbp;
394         unsigned int nb_mbuf_per_pool;
395         streamid_t sm_id;
396         lcoreid_t  lc_id;
397         portid_t   pt_id;
398
399         /* Configuration of logical cores. */
400         fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
401                                 sizeof(struct fwd_lcore *) * nb_lcores,
402                                 CACHE_LINE_SIZE);
403         if (fwd_lcores == NULL) {
404                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) failed\n",
405                        nb_lcores);
406         }
407         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
408                 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
409                                                sizeof(struct fwd_lcore),
410                                                CACHE_LINE_SIZE);
411                 if (fwd_lcores[lc_id] == NULL) {
412                         rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) failed\n");
413                 }
414                 fwd_lcores[lc_id]->cpuid_idx = lc_id;
415         }
416
417         /*
418          * Create pools of mbuf.
419          * If NUMA support is disabled, create a single pool of mbuf in
420          * socket 0 memory.
421          * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
422          */
423         nb_mbuf_per_pool = nb_rxd + (nb_lcores * mb_mempool_cache) +
424                 nb_txd + MAX_PKT_BURST;
425         if (numa_support) {
426                 nb_mbuf_per_pool = nb_mbuf_per_pool * (nb_ports >> 1);
427                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
428                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 1);
429         } else {
430                 nb_mbuf_per_pool = (nb_mbuf_per_pool * nb_ports);
431                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
432         }
433
434         /*
435          * Records which Mbuf pool to use by each logical core, if needed.
436          */
437         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
438                 mbp = mbuf_pool_find(rte_lcore_to_socket_id(lc_id));
439                 if (mbp == NULL)
440                         mbp = mbuf_pool_find(0);
441                 fwd_lcores[lc_id]->mbp = mbp;
442         }
443
444         /* Configuration of Ethernet ports. */
445         ports = rte_zmalloc("testpmd: ports",
446                             sizeof(struct rte_port) * nb_ports,
447                             CACHE_LINE_SIZE);
448         if (ports == NULL) {
449                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d struct rte_port) failed\n",
450                        nb_ports);
451         }
452         port = ports;
453         for (pt_id = 0; pt_id < nb_ports; pt_id++, port++) {
454                 rte_eth_dev_info_get(pt_id, &port->dev_info);
455                 if (nb_rxq > port->dev_info.max_rx_queues) {
456                         rte_exit(EXIT_FAILURE, "Port %d: max RX queues %d < nb_rxq %d\n",
457                                (int) pt_id,
458                                (int) port->dev_info.max_rx_queues,
459                                (int) nb_rxq);
460                 }
461                 if (nb_txq > port->dev_info.max_tx_queues) {
462                         rte_exit(EXIT_FAILURE, "Port %d: max TX queues %d < nb_txq %d\n",
463                                (int) pt_id,
464                                (int) port->dev_info.max_tx_queues,
465                                (int) nb_txq);
466                 }
467
468                 if (numa_support)
469                         port->socket_id = (pt_id < (nb_ports >> 1)) ? 0 : 1;
470                 else
471                         port->socket_id = 0;
472         }
473
474         /* Configuration of packet forwarding streams. */
475         nb_fwd_streams = (streamid_t) (nb_ports * nb_rxq);
476         fwd_streams = rte_zmalloc("testpmd: fwd_streams",
477                                   sizeof(struct fwd_stream *) * nb_fwd_streams,
478                                   CACHE_LINE_SIZE);
479         if (fwd_streams == NULL) {
480                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) failed\n",
481                        nb_fwd_streams);
482         }
483         for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
484                 fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
485                                                  sizeof(struct fwd_stream),
486                                                  CACHE_LINE_SIZE);
487                 if (fwd_streams[sm_id] == NULL) {
488                         rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream) failed\n");
489                 }
490         }
491 }
492
493 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
494 static void
495 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
496 {
497         unsigned int total_burst;
498         unsigned int nb_burst;
499         unsigned int burst_stats[3];
500         uint16_t pktnb_stats[3];
501         uint16_t nb_pkt;
502         int burst_percent[3];
503
504         /*
505          * First compute the total number of packet bursts and the
506          * two highest numbers of bursts of the same number of packets.
507          */
508         total_burst = 0;
509         burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
510         pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
511         for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
512                 nb_burst = pbs->pkt_burst_spread[nb_pkt];
513                 if (nb_burst == 0)
514                         continue;
515                 total_burst += nb_burst;
516                 if (nb_burst > burst_stats[0]) {
517                         burst_stats[1] = burst_stats[0];
518                         pktnb_stats[1] = pktnb_stats[0];
519                         burst_stats[0] = nb_burst;
520                         pktnb_stats[0] = nb_pkt;
521                 }
522         }
523         if (total_burst == 0)
524                 return;
525         burst_percent[0] = (burst_stats[0] * 100) / total_burst;
526         printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
527                burst_percent[0], (int) pktnb_stats[0]);
528         if (burst_stats[0] == total_burst) {
529                 printf("]\n");
530                 return;
531         }
532         if (burst_stats[0] + burst_stats[1] == total_burst) {
533                 printf(" + %d%% of %d pkts]\n",
534                        100 - burst_percent[0], pktnb_stats[1]);
535                 return;
536         }
537         burst_percent[1] = (burst_stats[1] * 100) / total_burst;
538         burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
539         if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
540                 printf(" + %d%% of others]\n", 100 - burst_percent[0]);
541                 return;
542         }
543         printf(" + %d%% of %d pkts + %d%% of others]\n",
544                burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
545 }
546 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
547
548 static void
549 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
550 {
551         struct rte_port *port;
552
553         static const char *fwd_stats_border = "----------------------";
554
555         port = &ports[port_id];
556         printf("\n  %s Forward statistics for port %-2d %s\n",
557                 fwd_stats_border, port_id, fwd_stats_border);
558         printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
559                "%-"PRIu64"\n",
560                stats->ipackets, stats->ierrors,
561                (uint64_t) (stats->ipackets + stats->ierrors));
562
563         if (cur_fwd_eng == &csum_fwd_engine)
564                 printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
565                                 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
566
567         printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
568                "%-"PRIu64"\n",
569                stats->opackets, port->tx_dropped,
570                (uint64_t) (stats->opackets + port->tx_dropped));
571
572         if (stats->rx_nombuf > 0)
573                 printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
574 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
575         if (port->rx_stream)
576                 pkt_burst_stats_display("RX", &port->rx_stream->rx_burst_stats);
577         if (port->tx_stream)
578                 pkt_burst_stats_display("TX", &port->tx_stream->tx_burst_stats);
579 #endif
580         /* stats fdir */
581         if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
582                 printf("  Fdirmiss: %-14"PRIu64"   Fdirmatch: %-14"PRIu64"\n",
583                        stats->fdirmiss,
584                        stats->fdirmatch);
585
586         printf("  %s--------------------------------%s\n",
587                fwd_stats_border, fwd_stats_border);
588 }
589
590 static void
591 fwd_stream_stats_display(streamid_t stream_id)
592 {
593         struct fwd_stream *fs;
594         static const char *fwd_top_stats_border = "-------";
595
596         fs = fwd_streams[stream_id];
597         if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
598             (fs->fwd_dropped == 0))
599                 return;
600         printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
601                "TX Port=%2d/Queue=%2d %s\n",
602                fwd_top_stats_border, fs->rx_port, fs->rx_queue,
603                fs->tx_port, fs->tx_queue, fwd_top_stats_border);
604         printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
605                fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
606
607         /* if checksum mode */
608         if (cur_fwd_eng == &csum_fwd_engine) {
609                printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: %-14u\n",
610                fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
611         }
612
613 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
614         pkt_burst_stats_display("RX", &fs->rx_burst_stats);
615         pkt_burst_stats_display("TX", &fs->tx_burst_stats);
616 #endif
617 }
618
619 static void
620 flush_all_rx_queues(void)
621 {
622         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
623         portid_t  rxp;
624         queueid_t rxq;
625         uint16_t  nb_rx;
626         uint16_t  i;
627         uint8_t   j;
628
629         for (j = 0; j < 2; j++) {
630                 for (rxp = 0; rxp < nb_ports; rxp++) {
631                         for (rxq = 0; rxq < nb_rxq; rxq++) {
632                                 do {
633                                         nb_rx = rte_eth_rx_burst(rxp, rxq,
634                                                                  pkts_burst,
635                                                                  MAX_PKT_BURST);
636                                         for (i = 0; i < nb_rx; i++)
637                                                 rte_pktmbuf_free(pkts_burst[i]);
638                                 } while (nb_rx > 0);
639                         }
640                 }
641                 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
642         }
643 }
644
645 static void
646 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
647 {
648         struct fwd_stream **fsm;
649         streamid_t nb_fs;
650         streamid_t sm_id;
651
652         fsm = &fwd_streams[fc->stream_idx];
653         nb_fs = fc->stream_nb;
654         do {
655                 for (sm_id = 0; sm_id < nb_fs; sm_id++)
656                         (*pkt_fwd)(fsm[sm_id]);
657         } while (! fc->stopped);
658 }
659
660 static int
661 start_pkt_forward_on_core(void *fwd_arg)
662 {
663         run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
664                              cur_fwd_config.fwd_eng->packet_fwd);
665         return 0;
666 }
667
668 /*
669  * Run the TXONLY packet forwarding engine to send a single burst of packets.
670  * Used to start communication flows in network loopback test configurations.
671  */
672 static int
673 run_one_txonly_burst_on_core(void *fwd_arg)
674 {
675         struct fwd_lcore *fwd_lc;
676         struct fwd_lcore tmp_lcore;
677
678         fwd_lc = (struct fwd_lcore *) fwd_arg;
679         tmp_lcore = *fwd_lc;
680         tmp_lcore.stopped = 1;
681         run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
682         return 0;
683 }
684
685 /*
686  * Launch packet forwarding:
687  *     - Setup per-port forwarding context.
688  *     - launch logical cores with their forwarding configuration.
689  */
690 static void
691 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
692 {
693         port_fwd_begin_t port_fwd_begin;
694         unsigned int i;
695         unsigned int lc_id;
696         int diag;
697
698         port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
699         if (port_fwd_begin != NULL) {
700                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
701                         (*port_fwd_begin)(fwd_ports_ids[i]);
702         }
703         for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
704                 lc_id = fwd_lcores_cpuids[i];
705                 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
706                         fwd_lcores[i]->stopped = 0;
707                         diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
708                                                      fwd_lcores[i], lc_id);
709                         if (diag != 0)
710                                 printf("launch lcore %u failed - diag=%d\n",
711                                        lc_id, diag);
712                 }
713         }
714 }
715
716 /*
717  * Launch packet forwarding configuration.
718  */
719 void
720 start_packet_forwarding(int with_tx_first)
721 {
722         port_fwd_begin_t port_fwd_begin;
723         port_fwd_end_t  port_fwd_end;
724         struct rte_port *port;
725         unsigned int i;
726         portid_t   pt_id;
727         streamid_t sm_id;
728
729         if (test_done == 0) {
730                 printf("Packet forwarding already started\n");
731                 return;
732         }
733         test_done = 0;
734         flush_all_rx_queues();
735         fwd_config_setup();
736         rxtx_config_display();
737
738         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
739                 pt_id = fwd_ports_ids[i];
740                 port = &ports[pt_id];
741                 rte_eth_stats_get(pt_id, &port->stats);
742                 port->tx_dropped = 0;
743         }
744         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
745                 fwd_streams[sm_id]->rx_packets = 0;
746                 fwd_streams[sm_id]->tx_packets = 0;
747                 fwd_streams[sm_id]->fwd_dropped = 0;
748                 fwd_streams[sm_id]->rx_bad_ip_csum = 0;
749                 fwd_streams[sm_id]->rx_bad_l4_csum = 0;
750
751 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
752                 memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
753                        sizeof(fwd_streams[sm_id]->rx_burst_stats));
754                 memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
755                        sizeof(fwd_streams[sm_id]->tx_burst_stats));
756 #endif
757 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
758                 fwd_streams[sm_id]->core_cycles = 0;
759 #endif
760         }
761         if (with_tx_first) {
762                 port_fwd_begin = tx_only_engine.port_fwd_begin;
763                 if (port_fwd_begin != NULL) {
764                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
765                                 (*port_fwd_begin)(fwd_ports_ids[i]);
766                 }
767                 launch_packet_forwarding(run_one_txonly_burst_on_core);
768                 rte_eal_mp_wait_lcore();
769                 port_fwd_end = tx_only_engine.port_fwd_end;
770                 if (port_fwd_end != NULL) {
771                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
772                                 (*port_fwd_end)(fwd_ports_ids[i]);
773                 }
774         }
775         launch_packet_forwarding(start_pkt_forward_on_core);
776 }
777
778 void
779 stop_packet_forwarding(void)
780 {
781         struct rte_eth_stats stats;
782         struct rte_port *port;
783         port_fwd_end_t  port_fwd_end;
784         int i;
785         portid_t   pt_id;
786         streamid_t sm_id;
787         lcoreid_t  lc_id;
788         uint64_t total_recv;
789         uint64_t total_xmit;
790         uint64_t total_rx_dropped;
791         uint64_t total_tx_dropped;
792         uint64_t total_rx_nombuf;
793         uint64_t tx_dropped;
794         uint64_t rx_bad_ip_csum;
795         uint64_t rx_bad_l4_csum;
796 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
797         uint64_t fwd_cycles;
798 #endif
799         static const char *acc_stats_border = "+++++++++++++++";
800
801         if (test_done) {
802                 printf("Packet forwarding not started\n");
803                 return;
804         }
805         printf("Telling cores to stop...");
806         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
807                 fwd_lcores[lc_id]->stopped = 1;
808         printf("\nWaiting for lcores to finish...\n");
809         rte_eal_mp_wait_lcore();
810         port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
811         if (port_fwd_end != NULL) {
812                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
813                         pt_id = fwd_ports_ids[i];
814                         (*port_fwd_end)(pt_id);
815                 }
816         }
817 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
818         fwd_cycles = 0;
819 #endif
820         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
821                 if (cur_fwd_config.nb_fwd_streams >
822                     cur_fwd_config.nb_fwd_ports) {
823                         fwd_stream_stats_display(sm_id);
824                         ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
825                         ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
826                 } else {
827                         ports[fwd_streams[sm_id]->tx_port].tx_stream =
828                                 fwd_streams[sm_id];
829                         ports[fwd_streams[sm_id]->rx_port].rx_stream =
830                                 fwd_streams[sm_id];
831                 }
832                 tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
833                 tx_dropped = (uint64_t) (tx_dropped +
834                                          fwd_streams[sm_id]->fwd_dropped);
835                 ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
836
837                 rx_bad_ip_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
838                 rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
839                                          fwd_streams[sm_id]->rx_bad_ip_csum);
840                 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum = rx_bad_ip_csum;
841
842                 rx_bad_l4_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
843                 rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
844                                          fwd_streams[sm_id]->rx_bad_l4_csum);
845                 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum = rx_bad_l4_csum;
846
847 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
848                 fwd_cycles = (uint64_t) (fwd_cycles +
849                                          fwd_streams[sm_id]->core_cycles);
850 #endif
851         }
852         total_recv = 0;
853         total_xmit = 0;
854         total_rx_dropped = 0;
855         total_tx_dropped = 0;
856         total_rx_nombuf  = 0;
857         for (i = 0; i < ((cur_fwd_config.nb_fwd_ports + 1) & ~0x1); i++) {
858                 pt_id = fwd_ports_ids[i];
859
860                 port = &ports[pt_id];
861                 rte_eth_stats_get(pt_id, &stats);
862                 stats.ipackets -= port->stats.ipackets;
863                 port->stats.ipackets = 0;
864                 stats.opackets -= port->stats.opackets;
865                 port->stats.opackets = 0;
866                 stats.ibytes   -= port->stats.ibytes;
867                 port->stats.ibytes = 0;
868                 stats.obytes   -= port->stats.obytes;
869                 port->stats.obytes = 0;
870                 stats.ierrors  -= port->stats.ierrors;
871                 port->stats.ierrors = 0;
872                 stats.oerrors  -= port->stats.oerrors;
873                 port->stats.oerrors = 0;
874                 stats.rx_nombuf -= port->stats.rx_nombuf;
875                 port->stats.rx_nombuf = 0;
876                 stats.fdirmatch -= port->stats.fdirmatch;
877                 port->stats.rx_nombuf = 0;
878                 stats.fdirmiss -= port->stats.fdirmiss;
879                 port->stats.rx_nombuf = 0;
880
881                 total_recv += stats.ipackets;
882                 total_xmit += stats.opackets;
883                 total_rx_dropped += stats.ierrors;
884                 total_tx_dropped += port->tx_dropped;
885                 total_rx_nombuf  += stats.rx_nombuf;
886
887                 fwd_port_stats_display(pt_id, &stats);
888         }
889         printf("\n  %s Accumulated forward statistics for all ports"
890                "%s\n",
891                acc_stats_border, acc_stats_border);
892         printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
893                "%-"PRIu64"\n"
894                "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
895                "%-"PRIu64"\n",
896                total_recv, total_rx_dropped, total_recv + total_rx_dropped,
897                total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
898         if (total_rx_nombuf > 0)
899                 printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
900         printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
901                "%s\n",
902                acc_stats_border, acc_stats_border);
903 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
904         if (total_recv > 0)
905                 printf("\n  CPU cycles/packet=%u (total cycles="
906                        "%"PRIu64" / total RX packets=%"PRIu64")\n",
907                        (unsigned int)(fwd_cycles / total_recv),
908                        fwd_cycles, total_recv);
909 #endif
910         printf("\nDone.\n");
911         test_done = 1;
912 }
913
914 void
915 pmd_test_exit(void)
916 {
917         portid_t pt_id;
918
919         for (pt_id = 0; pt_id < nb_ports; pt_id++) {
920                 printf("Stopping port %d...", pt_id);
921                 fflush(stdout);
922                 rte_eth_dev_close(pt_id);
923                 printf("done\n");
924         }
925         printf("bye...\n");
926 }
927
928 typedef void (*cmd_func_t)(void);
929 struct pmd_test_command {
930         const char *cmd_name;
931         cmd_func_t cmd_func;
932 };
933
934 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
935
936 static void
937 fatal_init_error(const char *func_name, uint8_t port_id, int diag)
938 {
939         rte_panic("%s(port_id=%d) failed - diag=%d\n",
940                   func_name, port_id, diag);
941 }
942
943 static void
944 init_ports(void)
945 {
946         struct rte_eth_link   link;
947         struct rte_eth_conf   port_conf = {
948                 .intr_conf = {
949                         .lsc = 0,
950                 },
951         };
952         struct rte_eth_rxconf rx_conf;
953         struct rte_eth_txconf tx_conf;
954         struct rte_port *port;
955         unsigned int sock_id;
956         portid_t  pi;
957         queueid_t qi;
958         int diag;
959
960         port_conf.rxmode = rx_mode;
961         port_conf.fdir_conf = fdir_conf;
962
963         if (nb_rxq > 0) { /* configure RSS */
964                 port_conf.rx_adv_conf.rss_conf.rss_key = NULL;
965                 /* use default hash key */
966                 port_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
967         } else
968                 port_conf.rx_adv_conf.rss_conf.rss_hf = 0;
969         rx_conf.rx_thresh = rx_thresh;
970         rx_conf.rx_free_thresh = rx_free_thresh;
971         tx_conf.tx_thresh = tx_thresh;
972         tx_conf.tx_rs_thresh = tx_rs_thresh;
973         tx_conf.tx_free_thresh = tx_free_thresh;
974
975         for (pi = 0; pi < nb_ports; pi++) {
976                 port = &ports[pi];
977                 memcpy(&port->dev_conf, &port_conf, sizeof(port_conf));
978                 sock_id = port->socket_id;
979                 printf("Initializing port %d... ", pi);
980                 fflush(stdout);
981                 diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq, &port_conf);
982                 if (diag != 0) {
983                         fatal_init_error("rte_eth_dev_configure", pi, diag);
984                         /* NOT REACHED */
985                 }
986                 rte_eth_macaddr_get(pi, &port->eth_addr);
987                 for (qi = 0; qi < nb_txq; qi++) {
988                         diag = rte_eth_tx_queue_setup(pi, qi, nb_txd,
989                                                       sock_id,
990                                                       &tx_conf);
991                         if (diag != 0) {
992                                 fatal_init_error("rte_eth_tx_queue_setup",
993                                                  pi, diag);
994                                 /* NOT REACHED */
995                         }
996                 }
997                 for (qi = 0; qi < nb_rxq; qi++) {
998                         diag = rte_eth_rx_queue_setup(pi, qi, nb_rxd, sock_id,
999                                                       &rx_conf,
1000                                                       mbuf_pool_find(sock_id));
1001                         if (diag != 0) {
1002                                 fatal_init_error("rte_eth_rx_queue_setup",
1003                                                  pi , diag);
1004                                 /* NOT REACHED */
1005                         }
1006                 }
1007
1008                 /* Start device */
1009                 diag = rte_eth_dev_start(pi);
1010                 if (diag != 0) {
1011                         fatal_init_error("rte_eth_dev_start", pi, diag);
1012                         /* NOT REACHED */
1013                 }
1014                 printf("done: ");
1015                 rte_eth_link_get(pi, &link);
1016                 if (link.link_status) {
1017                         printf(" Link Up - speed %u Mbps - %s\n",
1018                                (unsigned) link.link_speed,
1019                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1020                                ("full-duplex") : ("half-duplex\n"));
1021                 } else {
1022                         printf(" Link Down\n");
1023                 }
1024
1025                 /*
1026                  * If enabled, put device in promiscuous mode.
1027                  * This allows the PMD test in IO forwarding mode to forward
1028                  * packets to itself through 2 cross-connected  ports of the
1029                  * target machine.
1030                  */
1031                 if (promiscuous_on)
1032                         rte_eth_promiscuous_enable(pi);
1033         }
1034 }
1035
1036 #ifdef RTE_EXEC_ENV_BAREMETAL
1037 #define main _main
1038 #endif
1039
1040 int
1041 main(int argc, char** argv)
1042 {
1043         int  diag;
1044
1045         diag = rte_eal_init(argc, argv);
1046         if (diag < 0)
1047                 rte_panic("Cannot init EAL\n");
1048
1049 #ifdef RTE_LIBRTE_IGB_PMD
1050         if (rte_igb_pmd_init())
1051                 rte_panic("Cannot init igb PMD\n");
1052 #endif
1053 #ifdef RTE_LIBRTE_IXGBE_PMD
1054         if (rte_ixgbe_pmd_init())
1055                 rte_panic("Cannot init ixgbe PMD\n");
1056
1057         if (rte_ixgbevf_pmd_init())
1058                 rte_panic("Cannot init ixgbevf PMD\n");
1059 #endif
1060
1061         if (rte_eal_pci_probe())
1062                 rte_panic("Cannot probe PCI\n");
1063
1064         nb_ports = (portid_t) rte_eth_dev_count();
1065         if (nb_ports == 0)
1066                 rte_exit(EXIT_FAILURE, "No probed ethernet devices - check that "
1067                           "CONFIG_RTE_LIBRTE_IGB_PMD=y and that "
1068                           "CONFIG_RTE_LIBRTE_IXGBE_PMD=y in your "
1069                           "configuration file\n");
1070
1071         set_def_fwd_config();
1072         if (nb_lcores == 0)
1073                 rte_panic("Empty set of forwarding logical cores - check the "
1074                           "core mask supplied in the command parameters\n");
1075
1076         argc -= diag;
1077         argv += diag;
1078         if (argc > 1)
1079                 launch_args_parse(argc, argv);
1080
1081         if (nb_rxq > nb_txq)
1082                 printf("Warning: nb_rxq=%d enables RSS configuration, "
1083                        "but nb_txq=%d will prevent to fully test it.\n",
1084                        nb_rxq, nb_txq);
1085
1086         init_config();
1087
1088         init_ports();
1089
1090         if (interactive == 1)
1091                 prompt();
1092         else {
1093                 char c;
1094                 int rc;
1095
1096                 printf("No commandline core given, start packet forwarding\n");
1097                 start_packet_forwarding(0);
1098                 printf("Press enter to exit\n");
1099                 rc = read(0, &c, 1);
1100                 if (rc < 0)
1101                         return 1;
1102         }
1103
1104         return 0;
1105 }