xref: /dpdk/app/test-pmd/testpmd.c (revision ce8d561418d45cca86755be0f3cabc7a1a9dba4b)
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 uint32_t param_total_num_mbufs = 0;  /**< number of mbufs in all pools - if
146                                       * specified on command-line. */
147 
148 /*
149  * Configuration of packet segments used by the "txonly" processing engine.
150  */
151 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
152 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
153 	TXONLY_DEF_PACKET_LEN,
154 };
155 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
156 
157 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
158 uint16_t mb_mempool_cache = DEF_PKT_BURST; /**< Size of mbuf mempool cache. */
159 
160 /*
161  * Ethernet Ports Configuration.
162  */
163 int promiscuous_on = 1; /**< Ports set in promiscuous mode by default. */
164 
165 /*
166  * Configurable number of RX/TX queues.
167  */
168 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
169 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
170 
171 /*
172  * Configurable number of RX/TX ring descriptors.
173  */
174 #define RTE_TEST_RX_DESC_DEFAULT 128
175 #define RTE_TEST_TX_DESC_DEFAULT 512
176 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
177 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
178 
179 /*
180  * Configurable values of RX and TX ring threshold registers.
181  */
182 #define RX_PTHRESH 8 /**< Default value of RX prefetch threshold register. */
183 #define RX_HTHRESH 8 /**< Default value of RX host threshold register. */
184 #define RX_WTHRESH 4 /**< Default value of RX write-back threshold register. */
185 
186 #define TX_PTHRESH 36 /**< Default value of TX prefetch threshold register. */
187 #define TX_HTHRESH 0 /**< Default value of TX host threshold register. */
188 #define TX_WTHRESH 0 /**< Default value of TX write-back threshold register. */
189 
190 struct rte_eth_thresh rx_thresh = {
191 	.pthresh = RX_PTHRESH,
192 	.hthresh = RX_HTHRESH,
193 	.wthresh = RX_WTHRESH,
194 };
195 
196 struct rte_eth_thresh tx_thresh = {
197 	.pthresh = TX_PTHRESH,
198 	.hthresh = TX_HTHRESH,
199 	.wthresh = TX_WTHRESH,
200 };
201 
202 /*
203  * Configurable value of RX free threshold.
204  */
205 uint16_t rx_free_thresh = 0; /* Immediately free RX descriptors by default. */
206 
207 /*
208  * Configurable value of RX drop enable.
209  */
210 uint8_t rx_drop_en = 0; /* Drop packets when no descriptors for queue. */
211 
212 /*
213  * Configurable value of TX free threshold.
214  */
215 uint16_t tx_free_thresh = 0; /* Use default values. */
216 
217 /*
218  * Configurable value of TX RS bit threshold.
219  */
220 uint16_t tx_rs_thresh = 0; /* Use default values. */
221 
222 /*
223  * Configurable value of TX queue flags.
224  */
225 uint32_t txq_flags = 0; /* No flags set. */
226 
227 /*
228  * Receive Side Scaling (RSS) configuration.
229  */
230 uint16_t rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6; /* RSS IP by default. */
231 
232 /*
233  * Port topology configuration
234  */
235 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
236 
237 /*
238  * Ethernet device configuration.
239  */
240 struct rte_eth_rxmode rx_mode = {
241 	.max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
242 	.split_hdr_size = 0,
243 	.header_split   = 0, /**< Header Split disabled. */
244 	.hw_ip_checksum = 0, /**< IP checksum offload disabled. */
245 	.hw_vlan_filter = 1, /**< VLAN filtering enabled. */
246 	.hw_vlan_strip  = 1, /**< VLAN strip enabled. */
247 	.hw_vlan_extend = 0, /**< Extended VLAN disabled. */
248 	.jumbo_frame    = 0, /**< Jumbo Frame Support disabled. */
249 	.hw_strip_crc   = 0, /**< CRC stripping by hardware disabled. */
250 };
251 
252 struct rte_fdir_conf fdir_conf = {
253 	.mode = RTE_FDIR_MODE_NONE,
254 	.pballoc = RTE_FDIR_PBALLOC_64K,
255 	.status = RTE_FDIR_REPORT_STATUS,
256 	.flexbytes_offset = 0x6,
257 	.drop_queue = 127,
258 };
259 
260 static volatile int test_done = 1; /* stop packet forwarding when set to 1. */
261 
262 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
263 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
264 
265 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
266 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
267 
268 uint16_t nb_tx_queue_stats_mappings = 0;
269 uint16_t nb_rx_queue_stats_mappings = 0;
270 
271 /* Forward function declarations */
272 static void map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port);
273 static void check_all_ports_link_status(uint8_t port_num, uint32_t port_mask);
274 
275 /*
276  * Check if all the ports are started.
277  * If yes, return positive value. If not, return zero.
278  */
279 static int all_ports_started(void);
280 
281 /*
282  * Setup default configuration.
283  */
284 static void
285 set_default_fwd_lcores_config(void)
286 {
287 	unsigned int i;
288 	unsigned int nb_lc;
289 
290 	nb_lc = 0;
291 	for (i = 0; i < RTE_MAX_LCORE; i++) {
292 		if (! rte_lcore_is_enabled(i))
293 			continue;
294 		if (i == rte_get_master_lcore())
295 			continue;
296 		fwd_lcores_cpuids[nb_lc++] = i;
297 	}
298 	nb_lcores = (lcoreid_t) nb_lc;
299 	nb_cfg_lcores = nb_lcores;
300 	nb_fwd_lcores = 1;
301 }
302 
303 static void
304 set_def_peer_eth_addrs(void)
305 {
306 	portid_t i;
307 
308 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
309 		peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
310 		peer_eth_addrs[i].addr_bytes[5] = i;
311 	}
312 }
313 
314 static void
315 set_default_fwd_ports_config(void)
316 {
317 	portid_t pt_id;
318 
319 	for (pt_id = 0; pt_id < nb_ports; pt_id++)
320 		fwd_ports_ids[pt_id] = pt_id;
321 
322 	nb_cfg_ports = nb_ports;
323 	nb_fwd_ports = nb_ports;
324 }
325 
326 void
327 set_def_fwd_config(void)
328 {
329 	set_default_fwd_lcores_config();
330 	set_def_peer_eth_addrs();
331 	set_default_fwd_ports_config();
332 }
333 
334 /*
335  * Configuration initialisation done once at init time.
336  */
337 struct mbuf_ctor_arg {
338 	uint16_t seg_buf_offset; /**< offset of data in data segment of mbuf. */
339 	uint16_t seg_buf_size;   /**< size of data segment in mbuf. */
340 };
341 
342 struct mbuf_pool_ctor_arg {
343 	uint16_t seg_buf_size; /**< size of data segment in mbuf. */
344 };
345 
346 static void
347 testpmd_mbuf_ctor(struct rte_mempool *mp,
348 		  void *opaque_arg,
349 		  void *raw_mbuf,
350 		  __attribute__((unused)) unsigned i)
351 {
352 	struct mbuf_ctor_arg *mb_ctor_arg;
353 	struct rte_mbuf    *mb;
354 
355 	mb_ctor_arg = (struct mbuf_ctor_arg *) opaque_arg;
356 	mb = (struct rte_mbuf *) raw_mbuf;
357 
358 	mb->pool         = mp;
359 	mb->buf_addr     = (void *) ((char *)mb + mb_ctor_arg->seg_buf_offset);
360 	mb->buf_physaddr = (uint64_t) (rte_mempool_virt2phy(mp, mb) +
361 			mb_ctor_arg->seg_buf_offset);
362 	mb->buf_len      = mb_ctor_arg->seg_buf_size;
363 	mb->type         = RTE_MBUF_PKT;
364 	mb->ol_flags     = 0;
365 	mb->pkt.data     = (char *) mb->buf_addr + RTE_PKTMBUF_HEADROOM;
366 	mb->pkt.nb_segs  = 1;
367 	mb->pkt.vlan_macip.data = 0;
368 	mb->pkt.hash.rss = 0;
369 }
370 
371 static void
372 testpmd_mbuf_pool_ctor(struct rte_mempool *mp,
373 		       void *opaque_arg)
374 {
375 	struct mbuf_pool_ctor_arg      *mbp_ctor_arg;
376 	struct rte_pktmbuf_pool_private *mbp_priv;
377 
378 	if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
379 		printf("%s(%s) private_data_size %d < %d\n",
380 		       __func__, mp->name, (int) mp->private_data_size,
381 		       (int) sizeof(struct rte_pktmbuf_pool_private));
382 		return;
383 	}
384 	mbp_ctor_arg = (struct mbuf_pool_ctor_arg *) opaque_arg;
385 	mbp_priv = (struct rte_pktmbuf_pool_private *)
386 		((char *)mp + sizeof(struct rte_mempool));
387 	mbp_priv->mbuf_data_room_size = mbp_ctor_arg->seg_buf_size;
388 }
389 
390 static void
391 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
392 		 unsigned int socket_id)
393 {
394 	char pool_name[RTE_MEMPOOL_NAMESIZE];
395 	struct rte_mempool *rte_mp;
396 	struct mbuf_pool_ctor_arg mbp_ctor_arg;
397 	struct mbuf_ctor_arg mb_ctor_arg;
398 	uint32_t mb_size;
399 
400 	mbp_ctor_arg.seg_buf_size = (uint16_t) (RTE_PKTMBUF_HEADROOM +
401 						mbuf_seg_size);
402 	mb_ctor_arg.seg_buf_offset =
403 		(uint16_t) CACHE_LINE_ROUNDUP(sizeof(struct rte_mbuf));
404 	mb_ctor_arg.seg_buf_size = mbp_ctor_arg.seg_buf_size;
405 	mb_size = mb_ctor_arg.seg_buf_offset + mb_ctor_arg.seg_buf_size;
406 	mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
407 	rte_mp = rte_mempool_create(pool_name, nb_mbuf, (unsigned) mb_size,
408 				    (unsigned) mb_mempool_cache,
409 				    sizeof(struct rte_pktmbuf_pool_private),
410 				    testpmd_mbuf_pool_ctor, &mbp_ctor_arg,
411 				    testpmd_mbuf_ctor, &mb_ctor_arg,
412 				    socket_id, 0);
413 	if (rte_mp == NULL) {
414 		rte_exit(EXIT_FAILURE, "Creation of mbuf pool for socket %u "
415 						"failed\n", socket_id);
416 	}
417 }
418 
419 static void
420 init_config(void)
421 {
422 	portid_t pid;
423 	struct rte_port *port;
424 	struct rte_mempool *mbp;
425 	unsigned int nb_mbuf_per_pool;
426 	lcoreid_t  lc_id;
427 
428 	/* Configuration of logical cores. */
429 	fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
430 				sizeof(struct fwd_lcore *) * nb_lcores,
431 				CACHE_LINE_SIZE);
432 	if (fwd_lcores == NULL) {
433 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
434 							"failed\n", nb_lcores);
435 	}
436 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
437 		fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
438 					       sizeof(struct fwd_lcore),
439 					       CACHE_LINE_SIZE);
440 		if (fwd_lcores[lc_id] == NULL) {
441 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
442 								"failed\n");
443 		}
444 		fwd_lcores[lc_id]->cpuid_idx = lc_id;
445 	}
446 
447 	/*
448 	 * Create pools of mbuf.
449 	 * If NUMA support is disabled, create a single pool of mbuf in
450 	 * socket 0 memory.
451 	 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
452 	 *
453 	 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
454 	 * nb_txd can be configured at run time.
455 	 */
456 	if (param_total_num_mbufs)
457 		nb_mbuf_per_pool = param_total_num_mbufs;
458 	else {
459 		nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX + (nb_lcores * mb_mempool_cache)
460 				+ RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
461 		nb_mbuf_per_pool = (nb_mbuf_per_pool * nb_ports);
462 	}
463 	if (numa_support) {
464 		nb_mbuf_per_pool /= 2;
465 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
466 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 1);
467 	} else {
468 		mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
469 	}
470 
471 	/*
472 	 * Records which Mbuf pool to use by each logical core, if needed.
473 	 */
474 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
475 		mbp = mbuf_pool_find(rte_lcore_to_socket_id(lc_id));
476 		if (mbp == NULL)
477 			mbp = mbuf_pool_find(0);
478 		fwd_lcores[lc_id]->mbp = mbp;
479 	}
480 
481 	/* Configuration of Ethernet ports. */
482 	ports = rte_zmalloc("testpmd: ports",
483 			    sizeof(struct rte_port) * nb_ports,
484 			    CACHE_LINE_SIZE);
485 	if (ports == NULL) {
486 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d struct rte_port) "
487 							"failed\n", nb_ports);
488 	}
489 
490 	for (pid = 0; pid < nb_ports; pid++) {
491 		port = &ports[pid];
492 		rte_eth_dev_info_get(pid, &port->dev_info);
493 
494 		/* set flag to initialize port/queue */
495 		port->need_reconfig = 1;
496 		port->need_reconfig_queues = 1;
497 	}
498 
499 	init_port_config();
500 
501 	/* Configuration of packet forwarding streams. */
502 	if (init_fwd_streams() < 0)
503 		rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
504 }
505 
506 int
507 init_fwd_streams(void)
508 {
509 	portid_t pid;
510 	struct rte_port *port;
511 	streamid_t sm_id, nb_fwd_streams_new;
512 
513 	/* set socket id according to numa or not */
514 	for (pid = 0; pid < nb_ports; pid++) {
515 		port = &ports[pid];
516 		if (nb_rxq > port->dev_info.max_rx_queues) {
517 			printf("Fail: nb_rxq(%d) is greater than "
518 				"max_rx_queues(%d)\n", nb_rxq,
519 				port->dev_info.max_rx_queues);
520 			return -1;
521 		}
522 		if (nb_txq > port->dev_info.max_tx_queues) {
523 			printf("Fail: nb_txq(%d) is greater than "
524 				"max_tx_queues(%d)\n", nb_txq,
525 				port->dev_info.max_tx_queues);
526 			return -1;
527 		}
528 		if (numa_support)
529 			port->socket_id = (pid < (nb_ports >> 1)) ? 0 : 1;
530 		else
531 			port->socket_id = 0;
532 	}
533 
534 	nb_fwd_streams_new = (streamid_t)(nb_ports * nb_rxq);
535 	if (nb_fwd_streams_new == nb_fwd_streams)
536 		return 0;
537 	/* clear the old */
538 	if (fwd_streams != NULL) {
539 		for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
540 			if (fwd_streams[sm_id] == NULL)
541 				continue;
542 			rte_free(fwd_streams[sm_id]);
543 			fwd_streams[sm_id] = NULL;
544 		}
545 		rte_free(fwd_streams);
546 		fwd_streams = NULL;
547 	}
548 
549 	/* init new */
550 	nb_fwd_streams = nb_fwd_streams_new;
551 	fwd_streams = rte_zmalloc("testpmd: fwd_streams",
552 		sizeof(struct fwd_stream *) * nb_fwd_streams, CACHE_LINE_SIZE);
553 	if (fwd_streams == NULL)
554 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) "
555 						"failed\n", nb_fwd_streams);
556 
557 	for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
558 		fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
559 				sizeof(struct fwd_stream), CACHE_LINE_SIZE);
560 		if (fwd_streams[sm_id] == NULL)
561 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream)"
562 								" failed\n");
563 	}
564 
565 	return 0;
566 }
567 
568 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
569 static void
570 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
571 {
572 	unsigned int total_burst;
573 	unsigned int nb_burst;
574 	unsigned int burst_stats[3];
575 	uint16_t pktnb_stats[3];
576 	uint16_t nb_pkt;
577 	int burst_percent[3];
578 
579 	/*
580 	 * First compute the total number of packet bursts and the
581 	 * two highest numbers of bursts of the same number of packets.
582 	 */
583 	total_burst = 0;
584 	burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
585 	pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
586 	for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
587 		nb_burst = pbs->pkt_burst_spread[nb_pkt];
588 		if (nb_burst == 0)
589 			continue;
590 		total_burst += nb_burst;
591 		if (nb_burst > burst_stats[0]) {
592 			burst_stats[1] = burst_stats[0];
593 			pktnb_stats[1] = pktnb_stats[0];
594 			burst_stats[0] = nb_burst;
595 			pktnb_stats[0] = nb_pkt;
596 		}
597 	}
598 	if (total_burst == 0)
599 		return;
600 	burst_percent[0] = (burst_stats[0] * 100) / total_burst;
601 	printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
602 	       burst_percent[0], (int) pktnb_stats[0]);
603 	if (burst_stats[0] == total_burst) {
604 		printf("]\n");
605 		return;
606 	}
607 	if (burst_stats[0] + burst_stats[1] == total_burst) {
608 		printf(" + %d%% of %d pkts]\n",
609 		       100 - burst_percent[0], pktnb_stats[1]);
610 		return;
611 	}
612 	burst_percent[1] = (burst_stats[1] * 100) / total_burst;
613 	burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
614 	if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
615 		printf(" + %d%% of others]\n", 100 - burst_percent[0]);
616 		return;
617 	}
618 	printf(" + %d%% of %d pkts + %d%% of others]\n",
619 	       burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
620 }
621 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
622 
623 static void
624 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
625 {
626 	struct rte_port *port;
627 
628 	static const char *fwd_stats_border = "----------------------";
629 
630 	port = &ports[port_id];
631 	printf("\n  %s Forward statistics for port %-2d %s\n",
632 		fwd_stats_border, port_id, fwd_stats_border);
633 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
634 	       "%-"PRIu64"\n",
635 	       stats->ipackets, stats->ierrors,
636 	       (uint64_t) (stats->ipackets + stats->ierrors));
637 
638 	if (cur_fwd_eng == &csum_fwd_engine)
639 		printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
640 				port->rx_bad_ip_csum, port->rx_bad_l4_csum);
641 
642 	printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
643 	       "%-"PRIu64"\n",
644 	       stats->opackets, port->tx_dropped,
645 	       (uint64_t) (stats->opackets + port->tx_dropped));
646 
647 	if (stats->rx_nombuf > 0)
648 		printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
649 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
650 	if (port->rx_stream)
651 		pkt_burst_stats_display("RX", &port->rx_stream->rx_burst_stats);
652 	if (port->tx_stream)
653 		pkt_burst_stats_display("TX", &port->tx_stream->tx_burst_stats);
654 #endif
655 	/* stats fdir */
656 	if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
657 		printf("  Fdirmiss: %-14"PRIu64"   Fdirmatch: %-14"PRIu64"\n",
658 		       stats->fdirmiss,
659 		       stats->fdirmatch);
660 
661 	printf("  %s--------------------------------%s\n",
662 	       fwd_stats_border, fwd_stats_border);
663 }
664 
665 static void
666 fwd_stream_stats_display(streamid_t stream_id)
667 {
668 	struct fwd_stream *fs;
669 	static const char *fwd_top_stats_border = "-------";
670 
671 	fs = fwd_streams[stream_id];
672 	if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
673 	    (fs->fwd_dropped == 0))
674 		return;
675 	printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
676 	       "TX Port=%2d/Queue=%2d %s\n",
677 	       fwd_top_stats_border, fs->rx_port, fs->rx_queue,
678 	       fs->tx_port, fs->tx_queue, fwd_top_stats_border);
679 	printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
680 	       fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
681 
682 	/* if checksum mode */
683 	if (cur_fwd_eng == &csum_fwd_engine) {
684 	       printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: %-14u\n",
685 	       fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
686 	}
687 
688 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
689 	pkt_burst_stats_display("RX", &fs->rx_burst_stats);
690 	pkt_burst_stats_display("TX", &fs->tx_burst_stats);
691 #endif
692 }
693 
694 static void
695 flush_all_rx_queues(void)
696 {
697 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
698 	portid_t  rxp;
699 	queueid_t rxq;
700 	uint16_t  nb_rx;
701 	uint16_t  i;
702 	uint8_t   j;
703 
704 	for (j = 0; j < 2; j++) {
705 		for (rxp = 0; rxp < nb_ports; rxp++) {
706 			for (rxq = 0; rxq < nb_rxq; rxq++) {
707 				do {
708 					nb_rx = rte_eth_rx_burst(rxp, rxq,
709 								 pkts_burst,
710 								 MAX_PKT_BURST);
711 					for (i = 0; i < nb_rx; i++)
712 						rte_pktmbuf_free(pkts_burst[i]);
713 				} while (nb_rx > 0);
714 			}
715 		}
716 		rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
717 	}
718 }
719 
720 static void
721 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
722 {
723 	struct fwd_stream **fsm;
724 	streamid_t nb_fs;
725 	streamid_t sm_id;
726 
727 	fsm = &fwd_streams[fc->stream_idx];
728 	nb_fs = fc->stream_nb;
729 	do {
730 		for (sm_id = 0; sm_id < nb_fs; sm_id++)
731 			(*pkt_fwd)(fsm[sm_id]);
732 	} while (! fc->stopped);
733 }
734 
735 static int
736 start_pkt_forward_on_core(void *fwd_arg)
737 {
738 	run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
739 			     cur_fwd_config.fwd_eng->packet_fwd);
740 	return 0;
741 }
742 
743 /*
744  * Run the TXONLY packet forwarding engine to send a single burst of packets.
745  * Used to start communication flows in network loopback test configurations.
746  */
747 static int
748 run_one_txonly_burst_on_core(void *fwd_arg)
749 {
750 	struct fwd_lcore *fwd_lc;
751 	struct fwd_lcore tmp_lcore;
752 
753 	fwd_lc = (struct fwd_lcore *) fwd_arg;
754 	tmp_lcore = *fwd_lc;
755 	tmp_lcore.stopped = 1;
756 	run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
757 	return 0;
758 }
759 
760 /*
761  * Launch packet forwarding:
762  *     - Setup per-port forwarding context.
763  *     - launch logical cores with their forwarding configuration.
764  */
765 static void
766 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
767 {
768 	port_fwd_begin_t port_fwd_begin;
769 	unsigned int i;
770 	unsigned int lc_id;
771 	int diag;
772 
773 	port_fwd_begin = cur_fwd_config.fwd_eng->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 	for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
779 		lc_id = fwd_lcores_cpuids[i];
780 		if ((interactive == 0) || (lc_id != rte_lcore_id())) {
781 			fwd_lcores[i]->stopped = 0;
782 			diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
783 						     fwd_lcores[i], lc_id);
784 			if (diag != 0)
785 				printf("launch lcore %u failed - diag=%d\n",
786 				       lc_id, diag);
787 		}
788 	}
789 }
790 
791 /*
792  * Launch packet forwarding configuration.
793  */
794 void
795 start_packet_forwarding(int with_tx_first)
796 {
797 	port_fwd_begin_t port_fwd_begin;
798 	port_fwd_end_t  port_fwd_end;
799 	struct rte_port *port;
800 	unsigned int i;
801 	portid_t   pt_id;
802 	streamid_t sm_id;
803 
804 	if (all_ports_started() == 0) {
805 		printf("Not all ports were started\n");
806 		return;
807 	}
808 	if (test_done == 0) {
809 		printf("Packet forwarding already started\n");
810 		return;
811 	}
812 	test_done = 0;
813 	flush_all_rx_queues();
814 	fwd_config_setup();
815 	rxtx_config_display();
816 
817 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
818 		pt_id = fwd_ports_ids[i];
819 		port = &ports[pt_id];
820 		rte_eth_stats_get(pt_id, &port->stats);
821 		port->tx_dropped = 0;
822 	}
823 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
824 		fwd_streams[sm_id]->rx_packets = 0;
825 		fwd_streams[sm_id]->tx_packets = 0;
826 		fwd_streams[sm_id]->fwd_dropped = 0;
827 		fwd_streams[sm_id]->rx_bad_ip_csum = 0;
828 		fwd_streams[sm_id]->rx_bad_l4_csum = 0;
829 
830 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
831 		memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
832 		       sizeof(fwd_streams[sm_id]->rx_burst_stats));
833 		memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
834 		       sizeof(fwd_streams[sm_id]->tx_burst_stats));
835 #endif
836 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
837 		fwd_streams[sm_id]->core_cycles = 0;
838 #endif
839 	}
840 	if (with_tx_first) {
841 		port_fwd_begin = tx_only_engine.port_fwd_begin;
842 		if (port_fwd_begin != NULL) {
843 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
844 				(*port_fwd_begin)(fwd_ports_ids[i]);
845 		}
846 		launch_packet_forwarding(run_one_txonly_burst_on_core);
847 		rte_eal_mp_wait_lcore();
848 		port_fwd_end = tx_only_engine.port_fwd_end;
849 		if (port_fwd_end != NULL) {
850 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
851 				(*port_fwd_end)(fwd_ports_ids[i]);
852 		}
853 	}
854 	launch_packet_forwarding(start_pkt_forward_on_core);
855 }
856 
857 void
858 stop_packet_forwarding(void)
859 {
860 	struct rte_eth_stats stats;
861 	struct rte_port *port;
862 	port_fwd_end_t  port_fwd_end;
863 	int i;
864 	portid_t   pt_id;
865 	streamid_t sm_id;
866 	lcoreid_t  lc_id;
867 	uint64_t total_recv;
868 	uint64_t total_xmit;
869 	uint64_t total_rx_dropped;
870 	uint64_t total_tx_dropped;
871 	uint64_t total_rx_nombuf;
872 	uint64_t tx_dropped;
873 	uint64_t rx_bad_ip_csum;
874 	uint64_t rx_bad_l4_csum;
875 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
876 	uint64_t fwd_cycles;
877 #endif
878 	static const char *acc_stats_border = "+++++++++++++++";
879 
880 	if (all_ports_started() == 0) {
881 		printf("Not all ports were started\n");
882 		return;
883 	}
884 	if (test_done) {
885 		printf("Packet forwarding not started\n");
886 		return;
887 	}
888 	printf("Telling cores to stop...");
889 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
890 		fwd_lcores[lc_id]->stopped = 1;
891 	printf("\nWaiting for lcores to finish...\n");
892 	rte_eal_mp_wait_lcore();
893 	port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
894 	if (port_fwd_end != NULL) {
895 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
896 			pt_id = fwd_ports_ids[i];
897 			(*port_fwd_end)(pt_id);
898 		}
899 	}
900 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
901 	fwd_cycles = 0;
902 #endif
903 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
904 		if (cur_fwd_config.nb_fwd_streams >
905 		    cur_fwd_config.nb_fwd_ports) {
906 			fwd_stream_stats_display(sm_id);
907 			ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
908 			ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
909 		} else {
910 			ports[fwd_streams[sm_id]->tx_port].tx_stream =
911 				fwd_streams[sm_id];
912 			ports[fwd_streams[sm_id]->rx_port].rx_stream =
913 				fwd_streams[sm_id];
914 		}
915 		tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
916 		tx_dropped = (uint64_t) (tx_dropped +
917 					 fwd_streams[sm_id]->fwd_dropped);
918 		ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
919 
920 		rx_bad_ip_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
921 		rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
922 					 fwd_streams[sm_id]->rx_bad_ip_csum);
923 		ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum = rx_bad_ip_csum;
924 
925 		rx_bad_l4_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
926 		rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
927 					 fwd_streams[sm_id]->rx_bad_l4_csum);
928 		ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum = rx_bad_l4_csum;
929 
930 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
931 		fwd_cycles = (uint64_t) (fwd_cycles +
932 					 fwd_streams[sm_id]->core_cycles);
933 #endif
934 	}
935 	total_recv = 0;
936 	total_xmit = 0;
937 	total_rx_dropped = 0;
938 	total_tx_dropped = 0;
939 	total_rx_nombuf  = 0;
940 	for (i = 0; i < ((cur_fwd_config.nb_fwd_ports + 1) & ~0x1); i++) {
941 		pt_id = fwd_ports_ids[i];
942 
943 		port = &ports[pt_id];
944 		rte_eth_stats_get(pt_id, &stats);
945 		stats.ipackets -= port->stats.ipackets;
946 		port->stats.ipackets = 0;
947 		stats.opackets -= port->stats.opackets;
948 		port->stats.opackets = 0;
949 		stats.ibytes   -= port->stats.ibytes;
950 		port->stats.ibytes = 0;
951 		stats.obytes   -= port->stats.obytes;
952 		port->stats.obytes = 0;
953 		stats.ierrors  -= port->stats.ierrors;
954 		port->stats.ierrors = 0;
955 		stats.oerrors  -= port->stats.oerrors;
956 		port->stats.oerrors = 0;
957 		stats.rx_nombuf -= port->stats.rx_nombuf;
958 		port->stats.rx_nombuf = 0;
959 		stats.fdirmatch -= port->stats.fdirmatch;
960 		port->stats.rx_nombuf = 0;
961 		stats.fdirmiss -= port->stats.fdirmiss;
962 		port->stats.rx_nombuf = 0;
963 
964 		total_recv += stats.ipackets;
965 		total_xmit += stats.opackets;
966 		total_rx_dropped += stats.ierrors;
967 		total_tx_dropped += port->tx_dropped;
968 		total_rx_nombuf  += stats.rx_nombuf;
969 
970 		fwd_port_stats_display(pt_id, &stats);
971 	}
972 	printf("\n  %s Accumulated forward statistics for all ports"
973 	       "%s\n",
974 	       acc_stats_border, acc_stats_border);
975 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
976 	       "%-"PRIu64"\n"
977 	       "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
978 	       "%-"PRIu64"\n",
979 	       total_recv, total_rx_dropped, total_recv + total_rx_dropped,
980 	       total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
981 	if (total_rx_nombuf > 0)
982 		printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
983 	printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
984 	       "%s\n",
985 	       acc_stats_border, acc_stats_border);
986 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
987 	if (total_recv > 0)
988 		printf("\n  CPU cycles/packet=%u (total cycles="
989 		       "%"PRIu64" / total RX packets=%"PRIu64")\n",
990 		       (unsigned int)(fwd_cycles / total_recv),
991 		       fwd_cycles, total_recv);
992 #endif
993 	printf("\nDone.\n");
994 	test_done = 1;
995 }
996 
997 static int
998 all_ports_started(void)
999 {
1000 	portid_t pi;
1001 	struct rte_port *port;
1002 
1003 	for (pi = 0; pi < nb_ports; pi++) {
1004 		port = &ports[pi];
1005 		/* Check if there is a port which is not started */
1006 		if (port->port_status != RTE_PORT_STARTED)
1007 			return 0;
1008 	}
1009 
1010 	/* No port is not started */
1011 	return 1;
1012 }
1013 
1014 void
1015 start_port(portid_t pid)
1016 {
1017 	int diag, need_check_link_status = 0;
1018 	portid_t pi;
1019 	queueid_t qi;
1020 	struct rte_port *port;
1021 
1022 	if (test_done == 0) {
1023 		printf("Please stop forwarding first\n");
1024 		return;
1025 	}
1026 
1027 	if (init_fwd_streams() < 0) {
1028 		printf("Fail from init_fwd_streams()\n");
1029 		return;
1030 	}
1031 
1032 	if(dcb_config)
1033 		dcb_test = 1;
1034 	for (pi = 0; pi < nb_ports; pi++) {
1035 		if (pid < nb_ports && pid != pi)
1036 			continue;
1037 
1038 		port = &ports[pi];
1039 		if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
1040 						 RTE_PORT_HANDLING) == 0) {
1041 			printf("Port %d is now not stopped\n", pi);
1042 			continue;
1043 		}
1044 
1045 		if (port->need_reconfig > 0) {
1046 			port->need_reconfig = 0;
1047 
1048 			printf("Configuring Port %d\n", pi);
1049 			/* configure port */
1050 			diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq,
1051 						&(port->dev_conf));
1052 			if (diag != 0) {
1053 				if (rte_atomic16_cmpset(&(port->port_status),
1054 				RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1055 					printf("Port %d can not be set back "
1056 							"to stopped\n", pi);
1057 				printf("Fail to configure port %d\n", pi);
1058 				/* try to reconfigure port next time */
1059 				port->need_reconfig = 1;
1060 				return;
1061 			}
1062 		}
1063 
1064 		if (port->need_reconfig_queues > 0) {
1065 			port->need_reconfig_queues = 0;
1066 
1067 			/* setup tx queues */
1068 			for (qi = 0; qi < nb_txq; qi++) {
1069 				diag = rte_eth_tx_queue_setup(pi, qi, nb_txd,
1070 					port->socket_id, &(port->tx_conf));
1071 				if (diag == 0)
1072 					continue;
1073 
1074 				/* Fail to setup tx queue, return */
1075 				if (rte_atomic16_cmpset(&(port->port_status),
1076 							RTE_PORT_HANDLING,
1077 							RTE_PORT_STOPPED) == 0)
1078 					printf("Port %d can not be set back "
1079 							"to stopped\n", pi);
1080 				printf("Fail to configure port %d tx queues\n", pi);
1081 				/* try to reconfigure queues next time */
1082 				port->need_reconfig_queues = 1;
1083 				return;
1084 			}
1085 			/* setup rx queues */
1086 			for (qi = 0; qi < nb_rxq; qi++) {
1087 				diag = rte_eth_rx_queue_setup(pi, qi, nb_rxd,
1088 					port->socket_id, &(port->rx_conf),
1089 					mbuf_pool_find(port->socket_id));
1090 				if (diag == 0)
1091 					continue;
1092 
1093 				/* Fail to setup rx queue, return */
1094 				if (rte_atomic16_cmpset(&(port->port_status),
1095 							RTE_PORT_HANDLING,
1096 							RTE_PORT_STOPPED) == 0)
1097 					printf("Port %d can not be set back "
1098 							"to stopped\n", pi);
1099 				printf("Fail to configure port %d rx queues\n", pi);
1100 				/* try to reconfigure queues next time */
1101 				port->need_reconfig_queues = 1;
1102 				return;
1103 			}
1104 		}
1105 
1106 		/* start port */
1107 		if (rte_eth_dev_start(pi) < 0) {
1108 			printf("Fail to start port %d\n", pi);
1109 
1110 			/* Fail to setup rx queue, return */
1111 			if (rte_atomic16_cmpset(&(port->port_status),
1112 				RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1113 				printf("Port %d can not be set back to "
1114 							"stopped\n", pi);
1115 			continue;
1116 		}
1117 
1118 		if (rte_atomic16_cmpset(&(port->port_status),
1119 			RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
1120 			printf("Port %d can not be set into started\n", pi);
1121 
1122 		/* at least one port started, need checking link status */
1123 		need_check_link_status = 1;
1124 	}
1125 
1126 	if (need_check_link_status)
1127 		check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
1128 	else
1129 		printf("Please stop the ports first\n");
1130 
1131 	printf("Done\n");
1132 }
1133 
1134 void
1135 stop_port(portid_t pid)
1136 {
1137 	portid_t pi;
1138 	struct rte_port *port;
1139 	int need_check_link_status = 0;
1140 
1141 	if (test_done == 0) {
1142 		printf("Please stop forwarding first\n");
1143 		return;
1144 	}
1145 	if (dcb_test) {
1146 		dcb_test = 0;
1147 		dcb_config = 0;
1148 	}
1149 	printf("Stopping ports...\n");
1150 
1151 	for (pi = 0; pi < nb_ports; pi++) {
1152 		if (pid < nb_ports && pid != pi)
1153 			continue;
1154 
1155 		port = &ports[pi];
1156 		if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
1157 						RTE_PORT_HANDLING) == 0)
1158 			continue;
1159 
1160 		rte_eth_dev_stop(pi);
1161 
1162 		if (rte_atomic16_cmpset(&(port->port_status),
1163 			RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1164 			printf("Port %d can not be set into stopped\n", pi);
1165 		need_check_link_status = 1;
1166 	}
1167 	if (need_check_link_status)
1168 		check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
1169 
1170 	printf("Done\n");
1171 }
1172 
1173 void
1174 close_port(portid_t pid)
1175 {
1176 	portid_t pi;
1177 	struct rte_port *port;
1178 
1179 	if (test_done == 0) {
1180 		printf("Please stop forwarding first\n");
1181 		return;
1182 	}
1183 
1184 	printf("Closing ports...\n");
1185 
1186 	for (pi = 0; pi < nb_ports; pi++) {
1187 		if (pid < nb_ports && pid != pi)
1188 			continue;
1189 
1190 		port = &ports[pi];
1191 		if (rte_atomic16_cmpset(&(port->port_status),
1192 			RTE_PORT_STOPPED, RTE_PORT_HANDLING) == 0) {
1193 			printf("Port %d is now not stopped\n", pi);
1194 			continue;
1195 		}
1196 
1197 		rte_eth_dev_close(pi);
1198 
1199 		if (rte_atomic16_cmpset(&(port->port_status),
1200 			RTE_PORT_HANDLING, RTE_PORT_CLOSED) == 0)
1201 			printf("Port %d can not be set into stopped\n", pi);
1202 	}
1203 
1204 	printf("Done\n");
1205 }
1206 
1207 int
1208 all_ports_stopped(void)
1209 {
1210 	portid_t pi;
1211 	struct rte_port *port;
1212 
1213 	for (pi = 0; pi < nb_ports; pi++) {
1214 		port = &ports[pi];
1215 		if (port->port_status != RTE_PORT_STOPPED)
1216 			return 0;
1217 	}
1218 
1219 	return 1;
1220 }
1221 
1222 void
1223 pmd_test_exit(void)
1224 {
1225 	portid_t pt_id;
1226 
1227 	for (pt_id = 0; pt_id < nb_ports; pt_id++) {
1228 		printf("Stopping port %d...", pt_id);
1229 		fflush(stdout);
1230 		rte_eth_dev_close(pt_id);
1231 		printf("done\n");
1232 	}
1233 	printf("bye...\n");
1234 }
1235 
1236 typedef void (*cmd_func_t)(void);
1237 struct pmd_test_command {
1238 	const char *cmd_name;
1239 	cmd_func_t cmd_func;
1240 };
1241 
1242 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
1243 
1244 /* Check the link status of all ports in up to 9s, and print them finally */
1245 static void
1246 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1247 {
1248 #define CHECK_INTERVAL 100 /* 100ms */
1249 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1250 	uint8_t portid, count, all_ports_up, print_flag = 0;
1251 	struct rte_eth_link link;
1252 
1253 	printf("Checking link statuses...\n");
1254 	fflush(stdout);
1255 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1256 		all_ports_up = 1;
1257 		for (portid = 0; portid < port_num; portid++) {
1258 			if ((port_mask & (1 << portid)) == 0)
1259 				continue;
1260 			memset(&link, 0, sizeof(link));
1261 			rte_eth_link_get_nowait(portid, &link);
1262 			/* print link status if flag set */
1263 			if (print_flag == 1) {
1264 				if (link.link_status)
1265 					printf("Port %d Link Up - speed %u "
1266 						"Mbps - %s\n", (uint8_t)portid,
1267 						(unsigned)link.link_speed,
1268 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1269 					("full-duplex") : ("half-duplex\n"));
1270 				else
1271 					printf("Port %d Link Down\n",
1272 						(uint8_t)portid);
1273 				continue;
1274 			}
1275 			/* clear all_ports_up flag if any link down */
1276 			if (link.link_status == 0) {
1277 				all_ports_up = 0;
1278 				break;
1279 			}
1280 		}
1281 		/* after finally printing all link status, get out */
1282 		if (print_flag == 1)
1283 			break;
1284 
1285 		if (all_ports_up == 0) {
1286 			fflush(stdout);
1287 			rte_delay_ms(CHECK_INTERVAL);
1288 		}
1289 
1290 		/* set the print_flag if all ports up or timeout */
1291 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1292 			print_flag = 1;
1293 		}
1294 	}
1295 }
1296 
1297 static void
1298 init_ports(void)
1299 {
1300 	struct rte_eth_link   link;
1301 	struct rte_eth_conf   port_conf = {
1302 		.intr_conf = {
1303 			.lsc = 0,
1304 		},
1305 	};
1306 	struct rte_eth_rxconf rx_conf;
1307 	struct rte_eth_txconf tx_conf;
1308 	struct rte_port *port;
1309 	unsigned int sock_id;
1310 	portid_t  pi;
1311 	queueid_t qi;
1312 	int diag;
1313 
1314 	port_conf.rxmode = rx_mode;
1315 	port_conf.fdir_conf = fdir_conf;
1316 
1317 	if (nb_rxq > 0) { /* configure RSS */
1318 		port_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1319 		/* use default hash key */
1320 		port_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
1321 	} else
1322 		port_conf.rx_adv_conf.rss_conf.rss_hf = 0;
1323 	rx_conf.rx_thresh = rx_thresh;
1324 	rx_conf.rx_free_thresh = rx_free_thresh;
1325 	tx_conf.tx_thresh = tx_thresh;
1326 	tx_conf.tx_rs_thresh = tx_rs_thresh;
1327 	tx_conf.tx_free_thresh = tx_free_thresh;
1328 
1329 	for (pi = 0; pi < nb_ports; pi++) {
1330 		port = &ports[pi];
1331 		memcpy(&port->dev_conf, &port_conf, sizeof(port_conf));
1332 		sock_id = port->socket_id;
1333 		printf("Initializing port %d... ", pi);
1334 		fflush(stdout);
1335 		diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq, &port_conf);
1336 		if (diag != 0) {
1337 			fatal_init_error("rte_eth_dev_configure", pi, diag);
1338 			/* NOT REACHED */
1339 		}
1340 		rte_eth_macaddr_get(pi, &port->eth_addr);
1341 		for (qi = 0; qi < nb_txq; qi++) {
1342 			diag = rte_eth_tx_queue_setup(pi, qi, nb_txd,
1343 						      sock_id,
1344 						      &tx_conf);
1345 			if (diag != 0) {
1346 				fatal_init_error("rte_eth_tx_queue_setup",
1347 						 pi, diag);
1348 				/* NOT REACHED */
1349 			}
1350 		}
1351 		for (qi = 0; qi < nb_rxq; qi++) {
1352 			diag = rte_eth_rx_queue_setup(pi, qi, nb_rxd, sock_id,
1353 						      &rx_conf,
1354 						      mbuf_pool_find(sock_id));
1355 			if (diag != 0) {
1356 				fatal_init_error("rte_eth_rx_queue_setup",
1357 						 pi , diag);
1358 				/* NOT REACHED */
1359 			}
1360 		}
1361 
1362 		/* Start device */
1363 		diag = rte_eth_dev_start(pi);
1364 		if (diag != 0) {
1365 			fatal_init_error("rte_eth_dev_start", pi, diag);
1366 			/* NOT REACHED */
1367 		}
1368 		printf("done: ");
1369 		rte_eth_link_get(pi, &link);
1370 		if (link.link_status) {
1371 			printf(" Link Up - speed %u Mbps - %s\n",
1372 			       (unsigned) link.link_speed,
1373 			       (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1374 			       ("full-duplex") : ("half-duplex\n"));
1375 		} else {
1376 			printf(" Link Down\n");
1377 		}
1378 
1379 		/*
1380 		 * If enabled, put device in promiscuous mode.
1381 		 * This allows the PMD test in IO forwarding mode to forward
1382 		 * packets to itself through 2 cross-connected  ports of the
1383 		 * target machine.
1384 		 */
1385 		if (promiscuous_on)
1386 			rte_eth_promiscuous_enable(pi);
1387 	}
1388 }
1389 
1390 #ifdef RTE_EXEC_ENV_BAREMETAL
1391 #define main _main
1392 #endif
1393 
1394 int
1395 main(int argc, char** argv)
1396 {
1397 	int  diag;
1398 
1399 	diag = rte_eal_init(argc, argv);
1400 	if (diag < 0)
1401 		rte_panic("Cannot init EAL\n");
1402 
1403 	if (rte_pmd_init_all())
1404 		rte_panic("Cannot init PMD\n");
1405 
1406 	if (rte_eal_pci_probe())
1407 		rte_panic("Cannot probe PCI\n");
1408 
1409 	nb_ports = (portid_t) rte_eth_dev_count();
1410 	if (nb_ports == 0)
1411 		rte_exit(EXIT_FAILURE, "No probed ethernet devices - check that "
1412 			  "CONFIG_RTE_LIBRTE_IGB_PMD=y and that "
1413 			  "CONFIG_RTE_LIBRTE_EM_PMD=y and that "
1414 			  "CONFIG_RTE_LIBRTE_IXGBE_PMD=y in your "
1415 			  "configuration file\n");
1416 
1417 	set_def_fwd_config();
1418 	if (nb_lcores == 0)
1419 		rte_panic("Empty set of forwarding logical cores - check the "
1420 			  "core mask supplied in the command parameters\n");
1421 
1422 	argc -= diag;
1423 	argv += diag;
1424 	if (argc > 1)
1425 		launch_args_parse(argc, argv);
1426 
1427 	if (nb_rxq > nb_txq)
1428 		printf("Warning: nb_rxq=%d enables RSS configuration, "
1429 		       "but nb_txq=%d will prevent to fully test it.\n",
1430 		       nb_rxq, nb_txq);
1431 
1432 	init_config();
1433 	start_port(RTE_PORT_ALL);
1434 
1435 	/* set all ports to promiscuous mode by default */
1436 	for (port_id = 0; port_id < nb_ports; port_id++)
1437 		rte_eth_promiscuous_enable(port_id);
1438 
1439 	if (interactive == 1)
1440 		prompt();
1441 	else {
1442 		char c;
1443 		int rc;
1444 
1445 		printf("No commandline core given, start packet forwarding\n");
1446 		start_packet_forwarding(0);
1447 		printf("Press enter to exit\n");
1448 		rc = read(0, &c, 1);
1449 		if (rc < 0)
1450 			return 1;
1451 	}
1452 
1453 	return 0;
1454 }
1455