xref: /dpdk/app/test-pmd/testpmd.c (revision a47aa8b97afe46da6032910d88109948b4c49b21)
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 	.hw_vlan_strip  = 1, /**< VLAN strip enabled. */
235 	.hw_vlan_extend = 0, /**< Extended VLAN disabled. */
236 	.jumbo_frame    = 0, /**< Jumbo Frame Support disabled. */
237 	.hw_strip_crc   = 0, /**< CRC stripping by hardware disabled. */
238 };
239 
240 struct rte_fdir_conf fdir_conf = {
241 	.mode = RTE_FDIR_MODE_NONE,
242 	.pballoc = RTE_FDIR_PBALLOC_64K,
243 	.status = RTE_FDIR_REPORT_STATUS,
244 	.flexbytes_offset = 0x6,
245 	.drop_queue = 127,
246 };
247 
248 static volatile int test_done = 1; /* stop packet forwarding when set to 1. */
249 
250 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
251 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
252 
253 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
254 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
255 
256 uint16_t nb_tx_queue_stats_mappings = 0;
257 uint16_t nb_rx_queue_stats_mappings = 0;
258 
259 /* Forward function declarations */
260 static void map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port);
261 
262 /*
263  * Setup default configuration.
264  */
265 static void
266 set_default_fwd_lcores_config(void)
267 {
268 	unsigned int i;
269 	unsigned int nb_lc;
270 
271 	nb_lc = 0;
272 	for (i = 0; i < RTE_MAX_LCORE; i++) {
273 		if (! rte_lcore_is_enabled(i))
274 			continue;
275 		if (i == rte_get_master_lcore())
276 			continue;
277 		fwd_lcores_cpuids[nb_lc++] = i;
278 	}
279 	nb_lcores = (lcoreid_t) nb_lc;
280 	nb_cfg_lcores = nb_lcores;
281 	nb_fwd_lcores = 1;
282 }
283 
284 static void
285 set_def_peer_eth_addrs(void)
286 {
287 	portid_t i;
288 
289 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
290 		peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
291 		peer_eth_addrs[i].addr_bytes[5] = i;
292 	}
293 }
294 
295 static void
296 set_default_fwd_ports_config(void)
297 {
298 	portid_t pt_id;
299 
300 	for (pt_id = 0; pt_id < nb_ports; pt_id++)
301 		fwd_ports_ids[pt_id] = pt_id;
302 
303 	nb_cfg_ports = nb_ports;
304 	nb_fwd_ports = nb_ports;
305 }
306 
307 void
308 set_def_fwd_config(void)
309 {
310 	set_default_fwd_lcores_config();
311 	set_def_peer_eth_addrs();
312 	set_default_fwd_ports_config();
313 }
314 
315 /*
316  * Configuration initialisation done once at init time.
317  */
318 struct mbuf_ctor_arg {
319 	uint16_t seg_buf_offset; /**< offset of data in data segment of mbuf. */
320 	uint16_t seg_buf_size;   /**< size of data segment in mbuf. */
321 };
322 
323 struct mbuf_pool_ctor_arg {
324 	uint16_t seg_buf_size; /**< size of data segment in mbuf. */
325 };
326 
327 static void
328 testpmd_mbuf_ctor(struct rte_mempool *mp,
329 		  void *opaque_arg,
330 		  void *raw_mbuf,
331 		  __attribute__((unused)) unsigned i)
332 {
333 	struct mbuf_ctor_arg *mb_ctor_arg;
334 	struct rte_mbuf    *mb;
335 
336 	mb_ctor_arg = (struct mbuf_ctor_arg *) opaque_arg;
337 	mb = (struct rte_mbuf *) raw_mbuf;
338 
339 	mb->pool         = mp;
340 	mb->buf_addr     = (void *) ((char *)mb + mb_ctor_arg->seg_buf_offset);
341 	mb->buf_physaddr = (uint64_t) (rte_mempool_virt2phy(mp, mb) +
342 			mb_ctor_arg->seg_buf_offset);
343 	mb->buf_len      = mb_ctor_arg->seg_buf_size;
344 	mb->type         = RTE_MBUF_PKT;
345 	mb->ol_flags     = 0;
346 	mb->pkt.data     = (char *) mb->buf_addr + RTE_PKTMBUF_HEADROOM;
347 	mb->pkt.nb_segs  = 1;
348 	mb->pkt.vlan_macip.data = 0;
349 	mb->pkt.hash.rss = 0;
350 }
351 
352 static void
353 testpmd_mbuf_pool_ctor(struct rte_mempool *mp,
354 		       void *opaque_arg)
355 {
356 	struct mbuf_pool_ctor_arg      *mbp_ctor_arg;
357 	struct rte_pktmbuf_pool_private *mbp_priv;
358 
359 	if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
360 		printf("%s(%s) private_data_size %d < %d\n",
361 		       __func__, mp->name, (int) mp->private_data_size,
362 		       (int) sizeof(struct rte_pktmbuf_pool_private));
363 		return;
364 	}
365 	mbp_ctor_arg = (struct mbuf_pool_ctor_arg *) opaque_arg;
366 	mbp_priv = (struct rte_pktmbuf_pool_private *)
367 		((char *)mp + sizeof(struct rte_mempool));
368 	mbp_priv->mbuf_data_room_size = mbp_ctor_arg->seg_buf_size;
369 }
370 
371 static void
372 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
373 		 unsigned int socket_id)
374 {
375 	char pool_name[RTE_MEMPOOL_NAMESIZE];
376 	struct rte_mempool *rte_mp;
377 	struct mbuf_pool_ctor_arg mbp_ctor_arg;
378 	struct mbuf_ctor_arg mb_ctor_arg;
379 	uint32_t mb_size;
380 
381 	mbp_ctor_arg.seg_buf_size = (uint16_t) (RTE_PKTMBUF_HEADROOM +
382 						mbuf_seg_size);
383 	mb_ctor_arg.seg_buf_offset =
384 		(uint16_t) CACHE_LINE_ROUNDUP(sizeof(struct rte_mbuf));
385 	mb_ctor_arg.seg_buf_size = mbp_ctor_arg.seg_buf_size;
386 	mb_size = mb_ctor_arg.seg_buf_offset + mb_ctor_arg.seg_buf_size;
387 	mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
388 	rte_mp = rte_mempool_create(pool_name, nb_mbuf, (unsigned) mb_size,
389 				    (unsigned) mb_mempool_cache,
390 				    sizeof(struct rte_pktmbuf_pool_private),
391 				    testpmd_mbuf_pool_ctor, &mbp_ctor_arg,
392 				    testpmd_mbuf_ctor, &mb_ctor_arg,
393 				    socket_id, 0);
394 	if (rte_mp == NULL) {
395 		rte_exit(EXIT_FAILURE, "Creation of mbuf pool for socket %u failed\n",
396 		       socket_id);
397 	}
398 }
399 
400 static void
401 init_config(void)
402 {
403 	struct rte_port *port;
404 	struct rte_mempool *mbp;
405 	unsigned int nb_mbuf_per_pool;
406 	streamid_t sm_id;
407 	lcoreid_t  lc_id;
408 	portid_t   pt_id;
409 
410 	/* Configuration of logical cores. */
411 	fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
412 				sizeof(struct fwd_lcore *) * nb_lcores,
413 				CACHE_LINE_SIZE);
414 	if (fwd_lcores == NULL) {
415 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) failed\n",
416 		       nb_lcores);
417 	}
418 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
419 		fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
420 					       sizeof(struct fwd_lcore),
421 					       CACHE_LINE_SIZE);
422 		if (fwd_lcores[lc_id] == NULL) {
423 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) failed\n");
424 		}
425 		fwd_lcores[lc_id]->cpuid_idx = lc_id;
426 	}
427 
428 	/*
429 	 * Create pools of mbuf.
430 	 * If NUMA support is disabled, create a single pool of mbuf in
431 	 * socket 0 memory.
432 	 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
433 	 */
434 	nb_mbuf_per_pool = nb_rxd + (nb_lcores * mb_mempool_cache) +
435 		nb_txd + MAX_PKT_BURST;
436 	if (numa_support) {
437 		nb_mbuf_per_pool = nb_mbuf_per_pool * (nb_ports >> 1);
438 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
439 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 1);
440 	} else {
441 		nb_mbuf_per_pool = (nb_mbuf_per_pool * nb_ports);
442 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
443 	}
444 
445 	/*
446 	 * Records which Mbuf pool to use by each logical core, if needed.
447 	 */
448 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
449 		mbp = mbuf_pool_find(rte_lcore_to_socket_id(lc_id));
450 		if (mbp == NULL)
451 			mbp = mbuf_pool_find(0);
452 		fwd_lcores[lc_id]->mbp = mbp;
453 	}
454 
455 	/* Configuration of Ethernet ports. */
456 	ports = rte_zmalloc("testpmd: ports",
457 			    sizeof(struct rte_port) * nb_ports,
458 			    CACHE_LINE_SIZE);
459 	if (ports == NULL) {
460 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d struct rte_port) failed\n",
461 		       nb_ports);
462 	}
463 	port = ports;
464 	for (pt_id = 0; pt_id < nb_ports; pt_id++, port++) {
465 		rte_eth_dev_info_get(pt_id, &port->dev_info);
466 		if (nb_rxq > port->dev_info.max_rx_queues) {
467 			rte_exit(EXIT_FAILURE, "Port %d: max RX queues %d < nb_rxq %d\n",
468 			       (int) pt_id,
469 			       (int) port->dev_info.max_rx_queues,
470 			       (int) nb_rxq);
471 		}
472 		if (nb_txq > port->dev_info.max_tx_queues) {
473 			rte_exit(EXIT_FAILURE, "Port %d: max TX queues %d < nb_txq %d\n",
474 			       (int) pt_id,
475 			       (int) port->dev_info.max_tx_queues,
476 			       (int) nb_txq);
477 		}
478 
479 		if (numa_support)
480 			port->socket_id = (pt_id < (nb_ports >> 1)) ? 0 : 1;
481 		else
482 			port->socket_id = 0;
483 	}
484 
485 	/* Configuration of packet forwarding streams. */
486 	nb_fwd_streams = (streamid_t) (nb_ports * nb_rxq);
487 	fwd_streams = rte_zmalloc("testpmd: fwd_streams",
488 				  sizeof(struct fwd_stream *) * nb_fwd_streams,
489 				  CACHE_LINE_SIZE);
490 	if (fwd_streams == NULL) {
491 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) failed\n",
492 		       nb_fwd_streams);
493 	}
494 	for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
495 		fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
496 						 sizeof(struct fwd_stream),
497 						 CACHE_LINE_SIZE);
498 		if (fwd_streams[sm_id] == NULL) {
499 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream) failed\n");
500 		}
501 	}
502 }
503 
504 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
505 static void
506 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
507 {
508 	unsigned int total_burst;
509 	unsigned int nb_burst;
510 	unsigned int burst_stats[3];
511 	uint16_t pktnb_stats[3];
512 	uint16_t nb_pkt;
513 	int burst_percent[3];
514 
515 	/*
516 	 * First compute the total number of packet bursts and the
517 	 * two highest numbers of bursts of the same number of packets.
518 	 */
519 	total_burst = 0;
520 	burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
521 	pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
522 	for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
523 		nb_burst = pbs->pkt_burst_spread[nb_pkt];
524 		if (nb_burst == 0)
525 			continue;
526 		total_burst += nb_burst;
527 		if (nb_burst > burst_stats[0]) {
528 			burst_stats[1] = burst_stats[0];
529 			pktnb_stats[1] = pktnb_stats[0];
530 			burst_stats[0] = nb_burst;
531 			pktnb_stats[0] = nb_pkt;
532 		}
533 	}
534 	if (total_burst == 0)
535 		return;
536 	burst_percent[0] = (burst_stats[0] * 100) / total_burst;
537 	printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
538 	       burst_percent[0], (int) pktnb_stats[0]);
539 	if (burst_stats[0] == total_burst) {
540 		printf("]\n");
541 		return;
542 	}
543 	if (burst_stats[0] + burst_stats[1] == total_burst) {
544 		printf(" + %d%% of %d pkts]\n",
545 		       100 - burst_percent[0], pktnb_stats[1]);
546 		return;
547 	}
548 	burst_percent[1] = (burst_stats[1] * 100) / total_burst;
549 	burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
550 	if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
551 		printf(" + %d%% of others]\n", 100 - burst_percent[0]);
552 		return;
553 	}
554 	printf(" + %d%% of %d pkts + %d%% of others]\n",
555 	       burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
556 }
557 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
558 
559 static void
560 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
561 {
562 	struct rte_port *port;
563 
564 	static const char *fwd_stats_border = "----------------------";
565 
566 	port = &ports[port_id];
567 	printf("\n  %s Forward statistics for port %-2d %s\n",
568 		fwd_stats_border, port_id, fwd_stats_border);
569 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
570 	       "%-"PRIu64"\n",
571 	       stats->ipackets, stats->ierrors,
572 	       (uint64_t) (stats->ipackets + stats->ierrors));
573 
574 	if (cur_fwd_eng == &csum_fwd_engine)
575 		printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
576 				port->rx_bad_ip_csum, port->rx_bad_l4_csum);
577 
578 	printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
579 	       "%-"PRIu64"\n",
580 	       stats->opackets, port->tx_dropped,
581 	       (uint64_t) (stats->opackets + port->tx_dropped));
582 
583 	if (stats->rx_nombuf > 0)
584 		printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
585 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
586 	if (port->rx_stream)
587 		pkt_burst_stats_display("RX", &port->rx_stream->rx_burst_stats);
588 	if (port->tx_stream)
589 		pkt_burst_stats_display("TX", &port->tx_stream->tx_burst_stats);
590 #endif
591 	/* stats fdir */
592 	if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
593 		printf("  Fdirmiss: %-14"PRIu64"   Fdirmatch: %-14"PRIu64"\n",
594 		       stats->fdirmiss,
595 		       stats->fdirmatch);
596 
597 	printf("  %s--------------------------------%s\n",
598 	       fwd_stats_border, fwd_stats_border);
599 }
600 
601 static void
602 fwd_stream_stats_display(streamid_t stream_id)
603 {
604 	struct fwd_stream *fs;
605 	static const char *fwd_top_stats_border = "-------";
606 
607 	fs = fwd_streams[stream_id];
608 	if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
609 	    (fs->fwd_dropped == 0))
610 		return;
611 	printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
612 	       "TX Port=%2d/Queue=%2d %s\n",
613 	       fwd_top_stats_border, fs->rx_port, fs->rx_queue,
614 	       fs->tx_port, fs->tx_queue, fwd_top_stats_border);
615 	printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
616 	       fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
617 
618 	/* if checksum mode */
619 	if (cur_fwd_eng == &csum_fwd_engine) {
620 	       printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: %-14u\n",
621 	       fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
622 	}
623 
624 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
625 	pkt_burst_stats_display("RX", &fs->rx_burst_stats);
626 	pkt_burst_stats_display("TX", &fs->tx_burst_stats);
627 #endif
628 }
629 
630 static void
631 flush_all_rx_queues(void)
632 {
633 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
634 	portid_t  rxp;
635 	queueid_t rxq;
636 	uint16_t  nb_rx;
637 	uint16_t  i;
638 	uint8_t   j;
639 
640 	for (j = 0; j < 2; j++) {
641 		for (rxp = 0; rxp < nb_ports; rxp++) {
642 			for (rxq = 0; rxq < nb_rxq; rxq++) {
643 				do {
644 					nb_rx = rte_eth_rx_burst(rxp, rxq,
645 								 pkts_burst,
646 								 MAX_PKT_BURST);
647 					for (i = 0; i < nb_rx; i++)
648 						rte_pktmbuf_free(pkts_burst[i]);
649 				} while (nb_rx > 0);
650 			}
651 		}
652 		rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
653 	}
654 }
655 
656 static void
657 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
658 {
659 	struct fwd_stream **fsm;
660 	streamid_t nb_fs;
661 	streamid_t sm_id;
662 
663 	fsm = &fwd_streams[fc->stream_idx];
664 	nb_fs = fc->stream_nb;
665 	do {
666 		for (sm_id = 0; sm_id < nb_fs; sm_id++)
667 			(*pkt_fwd)(fsm[sm_id]);
668 	} while (! fc->stopped);
669 }
670 
671 static int
672 start_pkt_forward_on_core(void *fwd_arg)
673 {
674 	run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
675 			     cur_fwd_config.fwd_eng->packet_fwd);
676 	return 0;
677 }
678 
679 /*
680  * Run the TXONLY packet forwarding engine to send a single burst of packets.
681  * Used to start communication flows in network loopback test configurations.
682  */
683 static int
684 run_one_txonly_burst_on_core(void *fwd_arg)
685 {
686 	struct fwd_lcore *fwd_lc;
687 	struct fwd_lcore tmp_lcore;
688 
689 	fwd_lc = (struct fwd_lcore *) fwd_arg;
690 	tmp_lcore = *fwd_lc;
691 	tmp_lcore.stopped = 1;
692 	run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
693 	return 0;
694 }
695 
696 /*
697  * Launch packet forwarding:
698  *     - Setup per-port forwarding context.
699  *     - launch logical cores with their forwarding configuration.
700  */
701 static void
702 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
703 {
704 	port_fwd_begin_t port_fwd_begin;
705 	unsigned int i;
706 	unsigned int lc_id;
707 	int diag;
708 
709 	port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
710 	if (port_fwd_begin != NULL) {
711 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
712 			(*port_fwd_begin)(fwd_ports_ids[i]);
713 	}
714 	for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
715 		lc_id = fwd_lcores_cpuids[i];
716 		if ((interactive == 0) || (lc_id != rte_lcore_id())) {
717 			fwd_lcores[i]->stopped = 0;
718 			diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
719 						     fwd_lcores[i], lc_id);
720 			if (diag != 0)
721 				printf("launch lcore %u failed - diag=%d\n",
722 				       lc_id, diag);
723 		}
724 	}
725 }
726 
727 /*
728  * Launch packet forwarding configuration.
729  */
730 void
731 start_packet_forwarding(int with_tx_first)
732 {
733 	port_fwd_begin_t port_fwd_begin;
734 	port_fwd_end_t  port_fwd_end;
735 	struct rte_port *port;
736 	unsigned int i;
737 	portid_t   pt_id;
738 	streamid_t sm_id;
739 
740 	if (test_done == 0) {
741 		printf("Packet forwarding already started\n");
742 		return;
743 	}
744 	test_done = 0;
745 	flush_all_rx_queues();
746 	fwd_config_setup();
747 	rxtx_config_display();
748 
749 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
750 		pt_id = fwd_ports_ids[i];
751 		port = &ports[pt_id];
752 		rte_eth_stats_get(pt_id, &port->stats);
753 		port->tx_dropped = 0;
754 	}
755 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
756 		fwd_streams[sm_id]->rx_packets = 0;
757 		fwd_streams[sm_id]->tx_packets = 0;
758 		fwd_streams[sm_id]->fwd_dropped = 0;
759 		fwd_streams[sm_id]->rx_bad_ip_csum = 0;
760 		fwd_streams[sm_id]->rx_bad_l4_csum = 0;
761 
762 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
763 		memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
764 		       sizeof(fwd_streams[sm_id]->rx_burst_stats));
765 		memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
766 		       sizeof(fwd_streams[sm_id]->tx_burst_stats));
767 #endif
768 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
769 		fwd_streams[sm_id]->core_cycles = 0;
770 #endif
771 	}
772 	if (with_tx_first) {
773 		port_fwd_begin = tx_only_engine.port_fwd_begin;
774 		if (port_fwd_begin != NULL) {
775 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
776 				(*port_fwd_begin)(fwd_ports_ids[i]);
777 		}
778 		launch_packet_forwarding(run_one_txonly_burst_on_core);
779 		rte_eal_mp_wait_lcore();
780 		port_fwd_end = tx_only_engine.port_fwd_end;
781 		if (port_fwd_end != NULL) {
782 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
783 				(*port_fwd_end)(fwd_ports_ids[i]);
784 		}
785 	}
786 	launch_packet_forwarding(start_pkt_forward_on_core);
787 }
788 
789 void
790 stop_packet_forwarding(void)
791 {
792 	struct rte_eth_stats stats;
793 	struct rte_port *port;
794 	port_fwd_end_t  port_fwd_end;
795 	int i;
796 	portid_t   pt_id;
797 	streamid_t sm_id;
798 	lcoreid_t  lc_id;
799 	uint64_t total_recv;
800 	uint64_t total_xmit;
801 	uint64_t total_rx_dropped;
802 	uint64_t total_tx_dropped;
803 	uint64_t total_rx_nombuf;
804 	uint64_t tx_dropped;
805 	uint64_t rx_bad_ip_csum;
806 	uint64_t rx_bad_l4_csum;
807 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
808 	uint64_t fwd_cycles;
809 #endif
810 	static const char *acc_stats_border = "+++++++++++++++";
811 
812 	if (test_done) {
813 		printf("Packet forwarding not started\n");
814 		return;
815 	}
816 	printf("Telling cores to stop...");
817 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
818 		fwd_lcores[lc_id]->stopped = 1;
819 	printf("\nWaiting for lcores to finish...\n");
820 	rte_eal_mp_wait_lcore();
821 	port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
822 	if (port_fwd_end != NULL) {
823 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
824 			pt_id = fwd_ports_ids[i];
825 			(*port_fwd_end)(pt_id);
826 		}
827 	}
828 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
829 	fwd_cycles = 0;
830 #endif
831 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
832 		if (cur_fwd_config.nb_fwd_streams >
833 		    cur_fwd_config.nb_fwd_ports) {
834 			fwd_stream_stats_display(sm_id);
835 			ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
836 			ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
837 		} else {
838 			ports[fwd_streams[sm_id]->tx_port].tx_stream =
839 				fwd_streams[sm_id];
840 			ports[fwd_streams[sm_id]->rx_port].rx_stream =
841 				fwd_streams[sm_id];
842 		}
843 		tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
844 		tx_dropped = (uint64_t) (tx_dropped +
845 					 fwd_streams[sm_id]->fwd_dropped);
846 		ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
847 
848 		rx_bad_ip_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
849 		rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
850 					 fwd_streams[sm_id]->rx_bad_ip_csum);
851 		ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum = rx_bad_ip_csum;
852 
853 		rx_bad_l4_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
854 		rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
855 					 fwd_streams[sm_id]->rx_bad_l4_csum);
856 		ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum = rx_bad_l4_csum;
857 
858 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
859 		fwd_cycles = (uint64_t) (fwd_cycles +
860 					 fwd_streams[sm_id]->core_cycles);
861 #endif
862 	}
863 	total_recv = 0;
864 	total_xmit = 0;
865 	total_rx_dropped = 0;
866 	total_tx_dropped = 0;
867 	total_rx_nombuf  = 0;
868 	for (i = 0; i < ((cur_fwd_config.nb_fwd_ports + 1) & ~0x1); i++) {
869 		pt_id = fwd_ports_ids[i];
870 
871 		port = &ports[pt_id];
872 		rte_eth_stats_get(pt_id, &stats);
873 		stats.ipackets -= port->stats.ipackets;
874 		port->stats.ipackets = 0;
875 		stats.opackets -= port->stats.opackets;
876 		port->stats.opackets = 0;
877 		stats.ibytes   -= port->stats.ibytes;
878 		port->stats.ibytes = 0;
879 		stats.obytes   -= port->stats.obytes;
880 		port->stats.obytes = 0;
881 		stats.ierrors  -= port->stats.ierrors;
882 		port->stats.ierrors = 0;
883 		stats.oerrors  -= port->stats.oerrors;
884 		port->stats.oerrors = 0;
885 		stats.rx_nombuf -= port->stats.rx_nombuf;
886 		port->stats.rx_nombuf = 0;
887 		stats.fdirmatch -= port->stats.fdirmatch;
888 		port->stats.rx_nombuf = 0;
889 		stats.fdirmiss -= port->stats.fdirmiss;
890 		port->stats.rx_nombuf = 0;
891 
892 		total_recv += stats.ipackets;
893 		total_xmit += stats.opackets;
894 		total_rx_dropped += stats.ierrors;
895 		total_tx_dropped += port->tx_dropped;
896 		total_rx_nombuf  += stats.rx_nombuf;
897 
898 		fwd_port_stats_display(pt_id, &stats);
899 	}
900 	printf("\n  %s Accumulated forward statistics for all ports"
901 	       "%s\n",
902 	       acc_stats_border, acc_stats_border);
903 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
904 	       "%-"PRIu64"\n"
905 	       "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
906 	       "%-"PRIu64"\n",
907 	       total_recv, total_rx_dropped, total_recv + total_rx_dropped,
908 	       total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
909 	if (total_rx_nombuf > 0)
910 		printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
911 	printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
912 	       "%s\n",
913 	       acc_stats_border, acc_stats_border);
914 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
915 	if (total_recv > 0)
916 		printf("\n  CPU cycles/packet=%u (total cycles="
917 		       "%"PRIu64" / total RX packets=%"PRIu64")\n",
918 		       (unsigned int)(fwd_cycles / total_recv),
919 		       fwd_cycles, total_recv);
920 #endif
921 	printf("\nDone.\n");
922 	test_done = 1;
923 }
924 
925 void
926 pmd_test_exit(void)
927 {
928 	portid_t pt_id;
929 
930 	for (pt_id = 0; pt_id < nb_ports; pt_id++) {
931 		printf("Stopping port %d...", pt_id);
932 		fflush(stdout);
933 		rte_eth_dev_close(pt_id);
934 		printf("done\n");
935 	}
936 	printf("bye...\n");
937 }
938 
939 typedef void (*cmd_func_t)(void);
940 struct pmd_test_command {
941 	const char *cmd_name;
942 	cmd_func_t cmd_func;
943 };
944 
945 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
946 
947 static void
948 fatal_init_error(const char *func_name, uint8_t port_id, int diag)
949 {
950 	rte_panic("%s(port_id=%d) failed - diag=%d\n",
951 		  func_name, port_id, diag);
952 }
953 
954 static void
955 init_ports(void)
956 {
957 	struct rte_eth_link   link;
958 	struct rte_eth_conf   port_conf = {
959 		.intr_conf = {
960 			.lsc = 0,
961 		},
962 	};
963 	struct rte_eth_rxconf rx_conf;
964 	struct rte_eth_txconf tx_conf;
965 	struct rte_port *port;
966 	unsigned int sock_id;
967 	portid_t  pi;
968 	queueid_t qi;
969 	int diag;
970 
971 	port_conf.rxmode = rx_mode;
972 	port_conf.fdir_conf = fdir_conf;
973 
974 	if (nb_rxq > 0) { /* configure RSS */
975 		port_conf.rx_adv_conf.rss_conf.rss_key = NULL;
976 		/* use default hash key */
977 		port_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
978 	} else
979 		port_conf.rx_adv_conf.rss_conf.rss_hf = 0;
980 	rx_conf.rx_thresh = rx_thresh;
981 	rx_conf.rx_free_thresh = rx_free_thresh;
982 	tx_conf.tx_thresh = tx_thresh;
983 	tx_conf.tx_rs_thresh = tx_rs_thresh;
984 	tx_conf.tx_free_thresh = tx_free_thresh;
985 
986 	for (pi = 0; pi < nb_ports; pi++) {
987 		port = &ports[pi];
988 		memcpy(&port->dev_conf, &port_conf, sizeof(port_conf));
989 		sock_id = port->socket_id;
990 		printf("Initializing port %d... ", pi);
991 		fflush(stdout);
992 		diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq, &port_conf);
993 		if (diag != 0) {
994 			fatal_init_error("rte_eth_dev_configure", pi, diag);
995 			/* NOT REACHED */
996 		}
997 		rte_eth_macaddr_get(pi, &port->eth_addr);
998 		for (qi = 0; qi < nb_txq; qi++) {
999 			diag = rte_eth_tx_queue_setup(pi, qi, nb_txd,
1000 						      sock_id,
1001 						      &tx_conf);
1002 			if (diag != 0) {
1003 				fatal_init_error("rte_eth_tx_queue_setup",
1004 						 pi, diag);
1005 				/* NOT REACHED */
1006 			}
1007 		}
1008 		for (qi = 0; qi < nb_rxq; qi++) {
1009 			diag = rte_eth_rx_queue_setup(pi, qi, nb_rxd, sock_id,
1010 						      &rx_conf,
1011 						      mbuf_pool_find(sock_id));
1012 			if (diag != 0) {
1013 				fatal_init_error("rte_eth_rx_queue_setup",
1014 						 pi , diag);
1015 				/* NOT REACHED */
1016 			}
1017 		}
1018 
1019 		/* Start device */
1020 		diag = rte_eth_dev_start(pi);
1021 		if (diag != 0) {
1022 			fatal_init_error("rte_eth_dev_start", pi, diag);
1023 			/* NOT REACHED */
1024 		}
1025 		printf("done: ");
1026 		rte_eth_link_get(pi, &link);
1027 		if (link.link_status) {
1028 			printf(" Link Up - speed %u Mbps - %s\n",
1029 			       (unsigned) link.link_speed,
1030 			       (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1031 			       ("full-duplex") : ("half-duplex\n"));
1032 		} else {
1033 			printf(" Link Down\n");
1034 		}
1035 
1036 		/*
1037 		 * If enabled, put device in promiscuous mode.
1038 		 * This allows the PMD test in IO forwarding mode to forward
1039 		 * packets to itself through 2 cross-connected  ports of the
1040 		 * target machine.
1041 		 */
1042 		if (promiscuous_on)
1043 			rte_eth_promiscuous_enable(pi);
1044 	}
1045 }
1046 
1047 #ifdef RTE_EXEC_ENV_BAREMETAL
1048 #define main _main
1049 #endif
1050 
1051 int
1052 main(int argc, char** argv)
1053 {
1054 	int  diag;
1055 
1056 	diag = rte_eal_init(argc, argv);
1057 	if (diag < 0)
1058 		rte_panic("Cannot init EAL\n");
1059 
1060 	if (rte_pmd_init_all())
1061 		rte_panic("Cannot init PMD\n");
1062 
1063 	if (rte_eal_pci_probe())
1064 		rte_panic("Cannot probe PCI\n");
1065 
1066 	nb_ports = (portid_t) rte_eth_dev_count();
1067 	if (nb_ports == 0)
1068 		rte_exit(EXIT_FAILURE, "No probed ethernet devices - check that "
1069 			  "CONFIG_RTE_LIBRTE_IGB_PMD=y and that "
1070 			  "CONFIG_RTE_LIBRTE_EM_PMD=y and that "
1071 			  "CONFIG_RTE_LIBRTE_IXGBE_PMD=y in your "
1072 			  "configuration file\n");
1073 
1074 	set_def_fwd_config();
1075 	if (nb_lcores == 0)
1076 		rte_panic("Empty set of forwarding logical cores - check the "
1077 			  "core mask supplied in the command parameters\n");
1078 
1079 	argc -= diag;
1080 	argv += diag;
1081 	if (argc > 1)
1082 		launch_args_parse(argc, argv);
1083 
1084 	if (nb_rxq > nb_txq)
1085 		printf("Warning: nb_rxq=%d enables RSS configuration, "
1086 		       "but nb_txq=%d will prevent to fully test it.\n",
1087 		       nb_rxq, nb_txq);
1088 
1089 	init_config();
1090 
1091 	init_ports();
1092 
1093 	if (interactive == 1)
1094 		prompt();
1095 	else {
1096 		char c;
1097 		int rc;
1098 
1099 		printf("No commandline core given, start packet forwarding\n");
1100 		start_packet_forwarding(0);
1101 		printf("Press enter to exit\n");
1102 		rc = read(0, &c, 1);
1103 		if (rc < 0)
1104 			return 1;
1105 	}
1106 
1107 	return 0;
1108 }
1109