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