1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606  * Copyright(c) 2010-2014 Intel Corporation
3a9643ea8Slogwang  */
4a9643ea8Slogwang 
5a9643ea8Slogwang /*
6a9643ea8Slogwang  * Sample application demostrating how to do packet I/O in a multi-process
7a9643ea8Slogwang  * environment. The same code can be run as a primary process and as a
8a9643ea8Slogwang  * secondary process, just with a different proc-id parameter in each case
9a9643ea8Slogwang  * (apart from the EAL flag to indicate a secondary process).
10a9643ea8Slogwang  *
11a9643ea8Slogwang  * Each process will read from the same ports, given by the port-mask
12a9643ea8Slogwang  * parameter, which should be the same in each case, just using a different
13a9643ea8Slogwang  * queue per port as determined by the proc-id parameter.
14a9643ea8Slogwang  */
15a9643ea8Slogwang 
16a9643ea8Slogwang #include <stdio.h>
17a9643ea8Slogwang #include <string.h>
18a9643ea8Slogwang #include <stdint.h>
19a9643ea8Slogwang #include <stdlib.h>
20a9643ea8Slogwang #include <stdarg.h>
21a9643ea8Slogwang #include <errno.h>
22a9643ea8Slogwang #include <sys/queue.h>
23a9643ea8Slogwang #include <getopt.h>
24a9643ea8Slogwang #include <signal.h>
25a9643ea8Slogwang #include <inttypes.h>
26a9643ea8Slogwang 
27a9643ea8Slogwang #include <rte_common.h>
28a9643ea8Slogwang #include <rte_log.h>
29a9643ea8Slogwang #include <rte_memory.h>
30a9643ea8Slogwang #include <rte_launch.h>
31a9643ea8Slogwang #include <rte_eal.h>
32a9643ea8Slogwang #include <rte_per_lcore.h>
33a9643ea8Slogwang #include <rte_lcore.h>
34a9643ea8Slogwang #include <rte_atomic.h>
35a9643ea8Slogwang #include <rte_branch_prediction.h>
36a9643ea8Slogwang #include <rte_debug.h>
37a9643ea8Slogwang #include <rte_interrupts.h>
38a9643ea8Slogwang #include <rte_ether.h>
39a9643ea8Slogwang #include <rte_ethdev.h>
40a9643ea8Slogwang #include <rte_mempool.h>
41a9643ea8Slogwang #include <rte_memcpy.h>
42a9643ea8Slogwang #include <rte_mbuf.h>
43a9643ea8Slogwang #include <rte_string_fns.h>
44a9643ea8Slogwang #include <rte_cycles.h>
45a9643ea8Slogwang 
46a9643ea8Slogwang #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
47a9643ea8Slogwang 
48a9643ea8Slogwang #define NB_MBUFS 64*1024 /* use 64k mbufs */
49a9643ea8Slogwang #define MBUF_CACHE_SIZE 256
50a9643ea8Slogwang #define PKT_BURST 32
51d30ea906Sjfb8856606 #define RX_RING_SIZE 1024
52d30ea906Sjfb8856606 #define TX_RING_SIZE 1024
53a9643ea8Slogwang 
54a9643ea8Slogwang #define PARAM_PROC_ID "proc-id"
55a9643ea8Slogwang #define PARAM_NUM_PROCS "num-procs"
56a9643ea8Slogwang 
57a9643ea8Slogwang /* for each lcore, record the elements of the ports array to use */
58a9643ea8Slogwang struct lcore_ports{
59a9643ea8Slogwang 	unsigned start_port;
60a9643ea8Slogwang 	unsigned num_ports;
61a9643ea8Slogwang };
62a9643ea8Slogwang 
63a9643ea8Slogwang /* structure to record the rx and tx packets. Put two per cache line as ports
64a9643ea8Slogwang  * used in pairs */
65a9643ea8Slogwang struct port_stats{
66a9643ea8Slogwang 	unsigned rx;
67a9643ea8Slogwang 	unsigned tx;
68a9643ea8Slogwang 	unsigned drop;
69*2d9fd380Sjfb8856606 } __rte_aligned(RTE_CACHE_LINE_SIZE / 2);
70a9643ea8Slogwang 
71a9643ea8Slogwang static int proc_id = -1;
72a9643ea8Slogwang static unsigned num_procs = 0;
73a9643ea8Slogwang 
742bfe3f2eSlogwang static uint16_t ports[RTE_MAX_ETHPORTS];
75a9643ea8Slogwang static unsigned num_ports = 0;
76a9643ea8Slogwang 
77a9643ea8Slogwang static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
78a9643ea8Slogwang static struct port_stats pstats[RTE_MAX_ETHPORTS];
79a9643ea8Slogwang 
80a9643ea8Slogwang /* prints the usage statement and quits with an error message */
81a9643ea8Slogwang static void
smp_usage(const char * prgname,const char * errmsg)82a9643ea8Slogwang smp_usage(const char *prgname, const char *errmsg)
83a9643ea8Slogwang {
84a9643ea8Slogwang 	printf("\nError: %s\n",errmsg);
85a9643ea8Slogwang 	printf("\n%s [EAL options] -- -p <port mask> "
86a9643ea8Slogwang 			"--"PARAM_NUM_PROCS" <n>"
87a9643ea8Slogwang 			" --"PARAM_PROC_ID" <id>\n"
88a9643ea8Slogwang 			"-p         : a hex bitmask indicating what ports are to be used\n"
89a9643ea8Slogwang 			"--num-procs: the number of processes which will be used\n"
90a9643ea8Slogwang 			"--proc-id  : the id of the current process (id < num-procs)\n"
91a9643ea8Slogwang 			"\n",
92a9643ea8Slogwang 			prgname);
93a9643ea8Slogwang 	exit(1);
94a9643ea8Slogwang }
95a9643ea8Slogwang 
96a9643ea8Slogwang 
97a9643ea8Slogwang /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
98a9643ea8Slogwang static void
print_stats(int signum)99a9643ea8Slogwang print_stats(int signum)
100a9643ea8Slogwang {
101a9643ea8Slogwang 	unsigned i;
102a9643ea8Slogwang 	printf("\nExiting on signal %d\n\n", signum);
103a9643ea8Slogwang 	for (i = 0; i < num_ports; i++){
104a9643ea8Slogwang 		const uint8_t p_num = ports[i];
105a9643ea8Slogwang 		printf("Port %u: RX - %u, TX - %u, Drop - %u\n", (unsigned)p_num,
106a9643ea8Slogwang 				pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop);
107a9643ea8Slogwang 	}
108a9643ea8Slogwang 	exit(0);
109a9643ea8Slogwang }
110a9643ea8Slogwang 
111a9643ea8Slogwang /* Parse the argument given in the command line of the application */
112a9643ea8Slogwang static int
smp_parse_args(int argc,char ** argv)113a9643ea8Slogwang smp_parse_args(int argc, char **argv)
114a9643ea8Slogwang {
115a9643ea8Slogwang 	int opt, ret;
116a9643ea8Slogwang 	char **argvopt;
117a9643ea8Slogwang 	int option_index;
118d30ea906Sjfb8856606 	uint16_t i, port_mask = 0;
119a9643ea8Slogwang 	char *prgname = argv[0];
120a9643ea8Slogwang 	static struct option lgopts[] = {
121a9643ea8Slogwang 			{PARAM_NUM_PROCS, 1, 0, 0},
122a9643ea8Slogwang 			{PARAM_PROC_ID, 1, 0, 0},
123a9643ea8Slogwang 			{NULL, 0, 0, 0}
124a9643ea8Slogwang 	};
125a9643ea8Slogwang 
126a9643ea8Slogwang 	argvopt = argv;
127a9643ea8Slogwang 
128a9643ea8Slogwang 	while ((opt = getopt_long(argc, argvopt, "p:", \
129a9643ea8Slogwang 			lgopts, &option_index)) != EOF) {
130a9643ea8Slogwang 
131a9643ea8Slogwang 		switch (opt) {
132a9643ea8Slogwang 		case 'p':
133a9643ea8Slogwang 			port_mask = strtoull(optarg, NULL, 16);
134a9643ea8Slogwang 			break;
135a9643ea8Slogwang 			/* long options */
136a9643ea8Slogwang 		case 0:
137a9643ea8Slogwang 			if (strncmp(lgopts[option_index].name, PARAM_NUM_PROCS, 8) == 0)
138a9643ea8Slogwang 				num_procs = atoi(optarg);
139a9643ea8Slogwang 			else if (strncmp(lgopts[option_index].name, PARAM_PROC_ID, 7) == 0)
140a9643ea8Slogwang 				proc_id = atoi(optarg);
141a9643ea8Slogwang 			break;
142a9643ea8Slogwang 
143a9643ea8Slogwang 		default:
144a9643ea8Slogwang 			smp_usage(prgname, "Cannot parse all command-line arguments\n");
145a9643ea8Slogwang 		}
146a9643ea8Slogwang 	}
147a9643ea8Slogwang 
148a9643ea8Slogwang 	if (optind >= 0)
149a9643ea8Slogwang 		argv[optind-1] = prgname;
150a9643ea8Slogwang 
151a9643ea8Slogwang 	if (proc_id < 0)
152a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing proc-id parameter\n");
153a9643ea8Slogwang 	if (rte_eal_process_type() == RTE_PROC_PRIMARY && num_procs == 0)
154a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing num-procs parameter\n");
155a9643ea8Slogwang 	if (port_mask == 0)
156a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing port mask\n");
157a9643ea8Slogwang 
158a9643ea8Slogwang 	/* get the port numbers from the port mask */
159d30ea906Sjfb8856606 	RTE_ETH_FOREACH_DEV(i)
160a9643ea8Slogwang 		if(port_mask & (1 << i))
161a9643ea8Slogwang 			ports[num_ports++] = (uint8_t)i;
162a9643ea8Slogwang 
163a9643ea8Slogwang 	ret = optind-1;
1642bfe3f2eSlogwang 	optind = 1; /* reset getopt lib */
165a9643ea8Slogwang 
166a9643ea8Slogwang 	return ret;
167a9643ea8Slogwang }
168a9643ea8Slogwang 
169a9643ea8Slogwang /*
170a9643ea8Slogwang  * Initialises a given port using global settings and with the rx buffers
171a9643ea8Slogwang  * coming from the mbuf_pool passed as parameter
172a9643ea8Slogwang  */
173a9643ea8Slogwang static inline int
smp_port_init(uint16_t port,struct rte_mempool * mbuf_pool,uint16_t num_queues)1742bfe3f2eSlogwang smp_port_init(uint16_t port, struct rte_mempool *mbuf_pool,
1752bfe3f2eSlogwang 	       uint16_t num_queues)
176a9643ea8Slogwang {
177a9643ea8Slogwang 	struct rte_eth_conf port_conf = {
178a9643ea8Slogwang 			.rxmode = {
179a9643ea8Slogwang 				.mq_mode	= ETH_MQ_RX_RSS,
180a9643ea8Slogwang 				.split_hdr_size = 0,
181d30ea906Sjfb8856606 				.offloads = DEV_RX_OFFLOAD_CHECKSUM,
182a9643ea8Slogwang 			},
183a9643ea8Slogwang 			.rx_adv_conf = {
184a9643ea8Slogwang 				.rss_conf = {
185a9643ea8Slogwang 					.rss_key = NULL,
186a9643ea8Slogwang 					.rss_hf = ETH_RSS_IP,
187a9643ea8Slogwang 				},
188a9643ea8Slogwang 			},
189a9643ea8Slogwang 			.txmode = {
190a9643ea8Slogwang 				.mq_mode = ETH_MQ_TX_NONE,
191a9643ea8Slogwang 			}
192a9643ea8Slogwang 	};
193a9643ea8Slogwang 	const uint16_t rx_rings = num_queues, tx_rings = num_queues;
194a9643ea8Slogwang 	struct rte_eth_dev_info info;
195d30ea906Sjfb8856606 	struct rte_eth_rxconf rxq_conf;
196d30ea906Sjfb8856606 	struct rte_eth_txconf txq_conf;
197a9643ea8Slogwang 	int retval;
198a9643ea8Slogwang 	uint16_t q;
1992bfe3f2eSlogwang 	uint16_t nb_rxd = RX_RING_SIZE;
2002bfe3f2eSlogwang 	uint16_t nb_txd = TX_RING_SIZE;
201d30ea906Sjfb8856606 	uint64_t rss_hf_tmp;
202a9643ea8Slogwang 
203a9643ea8Slogwang 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
204a9643ea8Slogwang 		return 0;
205a9643ea8Slogwang 
206d30ea906Sjfb8856606 	if (!rte_eth_dev_is_valid_port(port))
207a9643ea8Slogwang 		return -1;
208a9643ea8Slogwang 
2092bfe3f2eSlogwang 	printf("# Initialising port %u... ", port);
210a9643ea8Slogwang 	fflush(stdout);
211a9643ea8Slogwang 
2124418919fSjohnjiang 	retval = rte_eth_dev_info_get(port, &info);
2134418919fSjohnjiang 	if (retval != 0) {
2144418919fSjohnjiang 		printf("Error during getting device (port %u) info: %s\n",
2154418919fSjohnjiang 				port, strerror(-retval));
2164418919fSjohnjiang 		return retval;
2174418919fSjohnjiang 	}
2184418919fSjohnjiang 
219a9643ea8Slogwang 	info.default_rxconf.rx_drop_en = 1;
220a9643ea8Slogwang 
221d30ea906Sjfb8856606 	if (info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
222d30ea906Sjfb8856606 		port_conf.txmode.offloads |=
223d30ea906Sjfb8856606 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
224d30ea906Sjfb8856606 
225d30ea906Sjfb8856606 	rss_hf_tmp = port_conf.rx_adv_conf.rss_conf.rss_hf;
226d30ea906Sjfb8856606 	port_conf.rx_adv_conf.rss_conf.rss_hf &= info.flow_type_rss_offloads;
227d30ea906Sjfb8856606 	if (port_conf.rx_adv_conf.rss_conf.rss_hf != rss_hf_tmp) {
228d30ea906Sjfb8856606 		printf("Port %u modified RSS hash function based on hardware support,"
229d30ea906Sjfb8856606 			"requested:%#"PRIx64" configured:%#"PRIx64"\n",
230d30ea906Sjfb8856606 			port,
231d30ea906Sjfb8856606 			rss_hf_tmp,
232d30ea906Sjfb8856606 			port_conf.rx_adv_conf.rss_conf.rss_hf);
233d30ea906Sjfb8856606 	}
234d30ea906Sjfb8856606 
235a9643ea8Slogwang 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
236a9643ea8Slogwang 	if (retval < 0)
237a9643ea8Slogwang 		return retval;
238a9643ea8Slogwang 
2392bfe3f2eSlogwang 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
2402bfe3f2eSlogwang 	if (retval < 0)
2412bfe3f2eSlogwang 		return retval;
2422bfe3f2eSlogwang 
243d30ea906Sjfb8856606 	rxq_conf = info.default_rxconf;
244d30ea906Sjfb8856606 	rxq_conf.offloads = port_conf.rxmode.offloads;
245a9643ea8Slogwang 	for (q = 0; q < rx_rings; q ++) {
2462bfe3f2eSlogwang 		retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
247a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
248d30ea906Sjfb8856606 				&rxq_conf,
249a9643ea8Slogwang 				mbuf_pool);
250a9643ea8Slogwang 		if (retval < 0)
251a9643ea8Slogwang 			return retval;
252a9643ea8Slogwang 	}
253a9643ea8Slogwang 
254d30ea906Sjfb8856606 	txq_conf = info.default_txconf;
255d30ea906Sjfb8856606 	txq_conf.offloads = port_conf.txmode.offloads;
256a9643ea8Slogwang 	for (q = 0; q < tx_rings; q ++) {
2572bfe3f2eSlogwang 		retval = rte_eth_tx_queue_setup(port, q, nb_txd,
258a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
259d30ea906Sjfb8856606 				&txq_conf);
260a9643ea8Slogwang 		if (retval < 0)
261a9643ea8Slogwang 			return retval;
262a9643ea8Slogwang 	}
263a9643ea8Slogwang 
2644418919fSjohnjiang 	retval = rte_eth_promiscuous_enable(port);
2654418919fSjohnjiang 	if (retval != 0)
2664418919fSjohnjiang 		return retval;
267a9643ea8Slogwang 
268a9643ea8Slogwang 	retval  = rte_eth_dev_start(port);
269a9643ea8Slogwang 	if (retval < 0)
270a9643ea8Slogwang 		return retval;
271a9643ea8Slogwang 
272a9643ea8Slogwang 	return 0;
273a9643ea8Slogwang }
274a9643ea8Slogwang 
275a9643ea8Slogwang /* Goes through each of the lcores and calculates what ports should
276a9643ea8Slogwang  * be used by that core. Fills in the global lcore_ports[] array.
277a9643ea8Slogwang  */
278a9643ea8Slogwang static void
assign_ports_to_cores(void)279a9643ea8Slogwang assign_ports_to_cores(void)
280a9643ea8Slogwang {
281a9643ea8Slogwang 
2824b05018fSfengbojiang 	const unsigned int lcores = rte_lcore_count();
283a9643ea8Slogwang 	const unsigned port_pairs = num_ports / 2;
284a9643ea8Slogwang 	const unsigned pairs_per_lcore = port_pairs / lcores;
285a9643ea8Slogwang 	unsigned extra_pairs = port_pairs % lcores;
286a9643ea8Slogwang 	unsigned ports_assigned = 0;
287a9643ea8Slogwang 	unsigned i;
288a9643ea8Slogwang 
289a9643ea8Slogwang 	RTE_LCORE_FOREACH(i) {
290a9643ea8Slogwang 		lcore_ports[i].start_port = ports_assigned;
291a9643ea8Slogwang 		lcore_ports[i].num_ports = pairs_per_lcore * 2;
292a9643ea8Slogwang 		if (extra_pairs > 0) {
293a9643ea8Slogwang 			lcore_ports[i].num_ports += 2;
294a9643ea8Slogwang 			extra_pairs--;
295a9643ea8Slogwang 		}
296a9643ea8Slogwang 		ports_assigned += lcore_ports[i].num_ports;
297a9643ea8Slogwang 	}
298a9643ea8Slogwang }
299a9643ea8Slogwang 
300a9643ea8Slogwang /* Main function used by the processing threads.
301a9643ea8Slogwang  * Prints out some configuration details for the thread and then begins
302a9643ea8Slogwang  * performing packet RX and TX.
303a9643ea8Slogwang  */
304a9643ea8Slogwang static int
lcore_main(void * arg __rte_unused)305a9643ea8Slogwang lcore_main(void *arg __rte_unused)
306a9643ea8Slogwang {
307a9643ea8Slogwang 	const unsigned id = rte_lcore_id();
308a9643ea8Slogwang 	const unsigned start_port = lcore_ports[id].start_port;
309a9643ea8Slogwang 	const unsigned end_port = start_port + lcore_ports[id].num_ports;
310a9643ea8Slogwang 	const uint16_t q_id = (uint16_t)proc_id;
311a9643ea8Slogwang 	unsigned p, i;
312a9643ea8Slogwang 	char msgbuf[256];
313a9643ea8Slogwang 	int msgbufpos = 0;
314a9643ea8Slogwang 
315a9643ea8Slogwang 	if (start_port == end_port){
316a9643ea8Slogwang 		printf("Lcore %u has nothing to do\n", id);
317a9643ea8Slogwang 		return 0;
318a9643ea8Slogwang 	}
319a9643ea8Slogwang 
320a9643ea8Slogwang 	/* build up message in msgbuf before printing to decrease likelihood
321a9643ea8Slogwang 	 * of multi-core message interleaving.
322a9643ea8Slogwang 	 */
323a9643ea8Slogwang 	msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
324a9643ea8Slogwang 			"Lcore %u using ports ", id);
325a9643ea8Slogwang 	for (p = start_port; p < end_port; p++){
326a9643ea8Slogwang 		msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
327a9643ea8Slogwang 				"%u ", (unsigned)ports[p]);
328a9643ea8Slogwang 	}
329a9643ea8Slogwang 	printf("%s\n", msgbuf);
330a9643ea8Slogwang 	printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
331a9643ea8Slogwang 
332a9643ea8Slogwang 	/* handle packet I/O from the ports, reading and writing to the
333a9643ea8Slogwang 	 * queue number corresponding to our process number (not lcore id)
334a9643ea8Slogwang 	 */
335a9643ea8Slogwang 
336a9643ea8Slogwang 	for (;;) {
337a9643ea8Slogwang 		struct rte_mbuf *buf[PKT_BURST];
338a9643ea8Slogwang 
339a9643ea8Slogwang 		for (p = start_port; p < end_port; p++) {
340a9643ea8Slogwang 			const uint8_t src = ports[p];
341a9643ea8Slogwang 			const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
342a9643ea8Slogwang 			const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
343a9643ea8Slogwang 			if (rx_c == 0)
344a9643ea8Slogwang 				continue;
345a9643ea8Slogwang 			pstats[src].rx += rx_c;
346a9643ea8Slogwang 
347a9643ea8Slogwang 			const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
348a9643ea8Slogwang 			pstats[dst].tx += tx_c;
349a9643ea8Slogwang 			if (tx_c != rx_c) {
350a9643ea8Slogwang 				pstats[dst].drop += (rx_c - tx_c);
351a9643ea8Slogwang 				for (i = tx_c; i < rx_c; i++)
352a9643ea8Slogwang 					rte_pktmbuf_free(buf[i]);
353a9643ea8Slogwang 			}
354a9643ea8Slogwang 		}
355a9643ea8Slogwang 	}
356a9643ea8Slogwang }
357a9643ea8Slogwang 
358a9643ea8Slogwang /* Check the link status of all ports in up to 9s, and print them finally */
359a9643ea8Slogwang static void
check_all_ports_link_status(uint16_t port_num,uint32_t port_mask)3602bfe3f2eSlogwang check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
361a9643ea8Slogwang {
362a9643ea8Slogwang #define CHECK_INTERVAL 100 /* 100ms */
363a9643ea8Slogwang #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3642bfe3f2eSlogwang 	uint16_t portid;
3652bfe3f2eSlogwang 	uint8_t count, all_ports_up, print_flag = 0;
366a9643ea8Slogwang 	struct rte_eth_link link;
3674418919fSjohnjiang 	int ret;
368*2d9fd380Sjfb8856606 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
369a9643ea8Slogwang 
370a9643ea8Slogwang 	printf("\nChecking link status");
371a9643ea8Slogwang 	fflush(stdout);
372a9643ea8Slogwang 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
373a9643ea8Slogwang 		all_ports_up = 1;
374a9643ea8Slogwang 		for (portid = 0; portid < port_num; portid++) {
375a9643ea8Slogwang 			if ((port_mask & (1 << portid)) == 0)
376a9643ea8Slogwang 				continue;
377a9643ea8Slogwang 			memset(&link, 0, sizeof(link));
3784418919fSjohnjiang 			ret = rte_eth_link_get_nowait(portid, &link);
3794418919fSjohnjiang 			if (ret < 0) {
3804418919fSjohnjiang 				all_ports_up = 0;
3814418919fSjohnjiang 				if (print_flag == 1)
3824418919fSjohnjiang 					printf("Port %u link get failed: %s\n",
3834418919fSjohnjiang 						portid, rte_strerror(-ret));
3844418919fSjohnjiang 				continue;
3854418919fSjohnjiang 			}
386a9643ea8Slogwang 			/* print link status if flag set */
387a9643ea8Slogwang 			if (print_flag == 1) {
388*2d9fd380Sjfb8856606 				rte_eth_link_to_str(link_status_text,
389*2d9fd380Sjfb8856606 					sizeof(link_status_text), &link);
390*2d9fd380Sjfb8856606 				printf("Port %d %s\n", portid,
391*2d9fd380Sjfb8856606 				       link_status_text);
392a9643ea8Slogwang 				continue;
393a9643ea8Slogwang 			}
394a9643ea8Slogwang 			/* clear all_ports_up flag if any link down */
395a9643ea8Slogwang 			if (link.link_status == ETH_LINK_DOWN) {
396a9643ea8Slogwang 				all_ports_up = 0;
397a9643ea8Slogwang 				break;
398a9643ea8Slogwang 			}
399a9643ea8Slogwang 		}
400a9643ea8Slogwang 		/* after finally printing all link status, get out */
401a9643ea8Slogwang 		if (print_flag == 1)
402a9643ea8Slogwang 			break;
403a9643ea8Slogwang 
404a9643ea8Slogwang 		if (all_ports_up == 0) {
405a9643ea8Slogwang 			printf(".");
406a9643ea8Slogwang 			fflush(stdout);
407a9643ea8Slogwang 			rte_delay_ms(CHECK_INTERVAL);
408a9643ea8Slogwang 		}
409a9643ea8Slogwang 
410a9643ea8Slogwang 		/* set the print_flag if all ports up or timeout */
411a9643ea8Slogwang 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
412a9643ea8Slogwang 			print_flag = 1;
413a9643ea8Slogwang 			printf("done\n");
414a9643ea8Slogwang 		}
415a9643ea8Slogwang 	}
416a9643ea8Slogwang }
417a9643ea8Slogwang 
418a9643ea8Slogwang /* Main function.
419a9643ea8Slogwang  * Performs initialisation and then calls the lcore_main on each core
420a9643ea8Slogwang  * to do the packet-processing work.
421a9643ea8Slogwang  */
422a9643ea8Slogwang int
main(int argc,char ** argv)423a9643ea8Slogwang main(int argc, char **argv)
424a9643ea8Slogwang {
425a9643ea8Slogwang 	static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
426a9643ea8Slogwang 	int ret;
427a9643ea8Slogwang 	unsigned i;
428a9643ea8Slogwang 	enum rte_proc_type_t proc_type;
429a9643ea8Slogwang 	struct rte_mempool *mp;
430a9643ea8Slogwang 
431a9643ea8Slogwang 	/* set up signal handlers to print stats on exit */
432a9643ea8Slogwang 	signal(SIGINT, print_stats);
433a9643ea8Slogwang 	signal(SIGTERM, print_stats);
434a9643ea8Slogwang 
435a9643ea8Slogwang 	/* initialise the EAL for all */
436a9643ea8Slogwang 	ret = rte_eal_init(argc, argv);
437a9643ea8Slogwang 	if (ret < 0)
438a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
439a9643ea8Slogwang 	argc -= ret;
440a9643ea8Slogwang 	argv += ret;
441a9643ea8Slogwang 
442a9643ea8Slogwang 	/* determine the NIC devices available */
443d30ea906Sjfb8856606 	if (rte_eth_dev_count_avail() == 0)
444a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
445a9643ea8Slogwang 
446a9643ea8Slogwang 	/* parse application arguments (those after the EAL ones) */
447a9643ea8Slogwang 	smp_parse_args(argc, argv);
448a9643ea8Slogwang 
449a9643ea8Slogwang 	proc_type = rte_eal_process_type();
450a9643ea8Slogwang 	mp = (proc_type == RTE_PROC_SECONDARY) ?
451a9643ea8Slogwang 			rte_mempool_lookup(_SMP_MBUF_POOL) :
452a9643ea8Slogwang 			rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
453a9643ea8Slogwang 				MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
454a9643ea8Slogwang 				rte_socket_id());
455a9643ea8Slogwang 	if (mp == NULL)
456a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
457a9643ea8Slogwang 
458a9643ea8Slogwang 	if (num_ports & 1)
459a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
460a9643ea8Slogwang 	for(i = 0; i < num_ports; i++){
461a9643ea8Slogwang 		if(proc_type == RTE_PROC_PRIMARY)
462a9643ea8Slogwang 			if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
463a9643ea8Slogwang 				rte_exit(EXIT_FAILURE, "Error initialising ports\n");
464a9643ea8Slogwang 	}
465a9643ea8Slogwang 
466a9643ea8Slogwang 	if (proc_type == RTE_PROC_PRIMARY)
467a9643ea8Slogwang 		check_all_ports_link_status((uint8_t)num_ports, (~0x0));
468a9643ea8Slogwang 
469a9643ea8Slogwang 	assign_ports_to_cores();
470a9643ea8Slogwang 
471a9643ea8Slogwang 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
472a9643ea8Slogwang 
473*2d9fd380Sjfb8856606 	rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MAIN);
474a9643ea8Slogwang 
475a9643ea8Slogwang 	return 0;
476a9643ea8Slogwang }
477