1*d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*d30ea906Sjfb8856606  * 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
51*d30ea906Sjfb8856606 #define RX_RING_SIZE 1024
52*d30ea906Sjfb8856606 #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;
69a9643ea8Slogwang } __attribute__((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
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
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
113a9643ea8Slogwang smp_parse_args(int argc, char **argv)
114a9643ea8Slogwang {
115a9643ea8Slogwang 	int opt, ret;
116a9643ea8Slogwang 	char **argvopt;
117a9643ea8Slogwang 	int option_index;
118*d30ea906Sjfb8856606 	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 */
159*d30ea906Sjfb8856606 	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
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,
181*d30ea906Sjfb8856606 				.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;
195*d30ea906Sjfb8856606 	struct rte_eth_rxconf rxq_conf;
196*d30ea906Sjfb8856606 	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;
201*d30ea906Sjfb8856606 	uint64_t rss_hf_tmp;
202a9643ea8Slogwang 
203a9643ea8Slogwang 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
204a9643ea8Slogwang 		return 0;
205a9643ea8Slogwang 
206*d30ea906Sjfb8856606 	if (!rte_eth_dev_is_valid_port(port))
207a9643ea8Slogwang 		return -1;
208a9643ea8Slogwang 
2092bfe3f2eSlogwang 	printf("# Initialising port %u... ", port);
210a9643ea8Slogwang 	fflush(stdout);
211a9643ea8Slogwang 
212a9643ea8Slogwang 	rte_eth_dev_info_get(port, &info);
213a9643ea8Slogwang 	info.default_rxconf.rx_drop_en = 1;
214a9643ea8Slogwang 
215*d30ea906Sjfb8856606 	if (info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
216*d30ea906Sjfb8856606 		port_conf.txmode.offloads |=
217*d30ea906Sjfb8856606 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
218*d30ea906Sjfb8856606 
219*d30ea906Sjfb8856606 	rss_hf_tmp = port_conf.rx_adv_conf.rss_conf.rss_hf;
220*d30ea906Sjfb8856606 	port_conf.rx_adv_conf.rss_conf.rss_hf &= info.flow_type_rss_offloads;
221*d30ea906Sjfb8856606 	if (port_conf.rx_adv_conf.rss_conf.rss_hf != rss_hf_tmp) {
222*d30ea906Sjfb8856606 		printf("Port %u modified RSS hash function based on hardware support,"
223*d30ea906Sjfb8856606 			"requested:%#"PRIx64" configured:%#"PRIx64"\n",
224*d30ea906Sjfb8856606 			port,
225*d30ea906Sjfb8856606 			rss_hf_tmp,
226*d30ea906Sjfb8856606 			port_conf.rx_adv_conf.rss_conf.rss_hf);
227*d30ea906Sjfb8856606 	}
228*d30ea906Sjfb8856606 
229a9643ea8Slogwang 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
230a9643ea8Slogwang 	if (retval < 0)
231a9643ea8Slogwang 		return retval;
232a9643ea8Slogwang 
2332bfe3f2eSlogwang 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
2342bfe3f2eSlogwang 	if (retval < 0)
2352bfe3f2eSlogwang 		return retval;
2362bfe3f2eSlogwang 
237*d30ea906Sjfb8856606 	rxq_conf = info.default_rxconf;
238*d30ea906Sjfb8856606 	rxq_conf.offloads = port_conf.rxmode.offloads;
239a9643ea8Slogwang 	for (q = 0; q < rx_rings; q ++) {
2402bfe3f2eSlogwang 		retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
241a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
242*d30ea906Sjfb8856606 				&rxq_conf,
243a9643ea8Slogwang 				mbuf_pool);
244a9643ea8Slogwang 		if (retval < 0)
245a9643ea8Slogwang 			return retval;
246a9643ea8Slogwang 	}
247a9643ea8Slogwang 
248*d30ea906Sjfb8856606 	txq_conf = info.default_txconf;
249*d30ea906Sjfb8856606 	txq_conf.offloads = port_conf.txmode.offloads;
250a9643ea8Slogwang 	for (q = 0; q < tx_rings; q ++) {
2512bfe3f2eSlogwang 		retval = rte_eth_tx_queue_setup(port, q, nb_txd,
252a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
253*d30ea906Sjfb8856606 				&txq_conf);
254a9643ea8Slogwang 		if (retval < 0)
255a9643ea8Slogwang 			return retval;
256a9643ea8Slogwang 	}
257a9643ea8Slogwang 
258a9643ea8Slogwang 	rte_eth_promiscuous_enable(port);
259a9643ea8Slogwang 
260a9643ea8Slogwang 	retval  = rte_eth_dev_start(port);
261a9643ea8Slogwang 	if (retval < 0)
262a9643ea8Slogwang 		return retval;
263a9643ea8Slogwang 
264a9643ea8Slogwang 	return 0;
265a9643ea8Slogwang }
266a9643ea8Slogwang 
267a9643ea8Slogwang /* Goes through each of the lcores and calculates what ports should
268a9643ea8Slogwang  * be used by that core. Fills in the global lcore_ports[] array.
269a9643ea8Slogwang  */
270a9643ea8Slogwang static void
271a9643ea8Slogwang assign_ports_to_cores(void)
272a9643ea8Slogwang {
273a9643ea8Slogwang 
274a9643ea8Slogwang 	const unsigned lcores = rte_eal_get_configuration()->lcore_count;
275a9643ea8Slogwang 	const unsigned port_pairs = num_ports / 2;
276a9643ea8Slogwang 	const unsigned pairs_per_lcore = port_pairs / lcores;
277a9643ea8Slogwang 	unsigned extra_pairs = port_pairs % lcores;
278a9643ea8Slogwang 	unsigned ports_assigned = 0;
279a9643ea8Slogwang 	unsigned i;
280a9643ea8Slogwang 
281a9643ea8Slogwang 	RTE_LCORE_FOREACH(i) {
282a9643ea8Slogwang 		lcore_ports[i].start_port = ports_assigned;
283a9643ea8Slogwang 		lcore_ports[i].num_ports = pairs_per_lcore * 2;
284a9643ea8Slogwang 		if (extra_pairs > 0) {
285a9643ea8Slogwang 			lcore_ports[i].num_ports += 2;
286a9643ea8Slogwang 			extra_pairs--;
287a9643ea8Slogwang 		}
288a9643ea8Slogwang 		ports_assigned += lcore_ports[i].num_ports;
289a9643ea8Slogwang 	}
290a9643ea8Slogwang }
291a9643ea8Slogwang 
292a9643ea8Slogwang /* Main function used by the processing threads.
293a9643ea8Slogwang  * Prints out some configuration details for the thread and then begins
294a9643ea8Slogwang  * performing packet RX and TX.
295a9643ea8Slogwang  */
296a9643ea8Slogwang static int
297a9643ea8Slogwang lcore_main(void *arg __rte_unused)
298a9643ea8Slogwang {
299a9643ea8Slogwang 	const unsigned id = rte_lcore_id();
300a9643ea8Slogwang 	const unsigned start_port = lcore_ports[id].start_port;
301a9643ea8Slogwang 	const unsigned end_port = start_port + lcore_ports[id].num_ports;
302a9643ea8Slogwang 	const uint16_t q_id = (uint16_t)proc_id;
303a9643ea8Slogwang 	unsigned p, i;
304a9643ea8Slogwang 	char msgbuf[256];
305a9643ea8Slogwang 	int msgbufpos = 0;
306a9643ea8Slogwang 
307a9643ea8Slogwang 	if (start_port == end_port){
308a9643ea8Slogwang 		printf("Lcore %u has nothing to do\n", id);
309a9643ea8Slogwang 		return 0;
310a9643ea8Slogwang 	}
311a9643ea8Slogwang 
312a9643ea8Slogwang 	/* build up message in msgbuf before printing to decrease likelihood
313a9643ea8Slogwang 	 * of multi-core message interleaving.
314a9643ea8Slogwang 	 */
315a9643ea8Slogwang 	msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
316a9643ea8Slogwang 			"Lcore %u using ports ", id);
317a9643ea8Slogwang 	for (p = start_port; p < end_port; p++){
318a9643ea8Slogwang 		msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
319a9643ea8Slogwang 				"%u ", (unsigned)ports[p]);
320a9643ea8Slogwang 	}
321a9643ea8Slogwang 	printf("%s\n", msgbuf);
322a9643ea8Slogwang 	printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
323a9643ea8Slogwang 
324a9643ea8Slogwang 	/* handle packet I/O from the ports, reading and writing to the
325a9643ea8Slogwang 	 * queue number corresponding to our process number (not lcore id)
326a9643ea8Slogwang 	 */
327a9643ea8Slogwang 
328a9643ea8Slogwang 	for (;;) {
329a9643ea8Slogwang 		struct rte_mbuf *buf[PKT_BURST];
330a9643ea8Slogwang 
331a9643ea8Slogwang 		for (p = start_port; p < end_port; p++) {
332a9643ea8Slogwang 			const uint8_t src = ports[p];
333a9643ea8Slogwang 			const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
334a9643ea8Slogwang 			const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
335a9643ea8Slogwang 			if (rx_c == 0)
336a9643ea8Slogwang 				continue;
337a9643ea8Slogwang 			pstats[src].rx += rx_c;
338a9643ea8Slogwang 
339a9643ea8Slogwang 			const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
340a9643ea8Slogwang 			pstats[dst].tx += tx_c;
341a9643ea8Slogwang 			if (tx_c != rx_c) {
342a9643ea8Slogwang 				pstats[dst].drop += (rx_c - tx_c);
343a9643ea8Slogwang 				for (i = tx_c; i < rx_c; i++)
344a9643ea8Slogwang 					rte_pktmbuf_free(buf[i]);
345a9643ea8Slogwang 			}
346a9643ea8Slogwang 		}
347a9643ea8Slogwang 	}
348a9643ea8Slogwang }
349a9643ea8Slogwang 
350a9643ea8Slogwang /* Check the link status of all ports in up to 9s, and print them finally */
351a9643ea8Slogwang static void
3522bfe3f2eSlogwang check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
353a9643ea8Slogwang {
354a9643ea8Slogwang #define CHECK_INTERVAL 100 /* 100ms */
355a9643ea8Slogwang #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3562bfe3f2eSlogwang 	uint16_t portid;
3572bfe3f2eSlogwang 	uint8_t count, all_ports_up, print_flag = 0;
358a9643ea8Slogwang 	struct rte_eth_link link;
359a9643ea8Slogwang 
360a9643ea8Slogwang 	printf("\nChecking link status");
361a9643ea8Slogwang 	fflush(stdout);
362a9643ea8Slogwang 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
363a9643ea8Slogwang 		all_ports_up = 1;
364a9643ea8Slogwang 		for (portid = 0; portid < port_num; portid++) {
365a9643ea8Slogwang 			if ((port_mask & (1 << portid)) == 0)
366a9643ea8Slogwang 				continue;
367a9643ea8Slogwang 			memset(&link, 0, sizeof(link));
368a9643ea8Slogwang 			rte_eth_link_get_nowait(portid, &link);
369a9643ea8Slogwang 			/* print link status if flag set */
370a9643ea8Slogwang 			if (print_flag == 1) {
371a9643ea8Slogwang 				if (link.link_status)
3722bfe3f2eSlogwang 					printf(
3732bfe3f2eSlogwang 					"Port%d Link Up. Speed %u Mbps - %s\n",
3742bfe3f2eSlogwang 						portid, link.link_speed,
375a9643ea8Slogwang 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
376a9643ea8Slogwang 					("full-duplex") : ("half-duplex\n"));
377a9643ea8Slogwang 				else
3782bfe3f2eSlogwang 					printf("Port %d Link Down\n", portid);
379a9643ea8Slogwang 				continue;
380a9643ea8Slogwang 			}
381a9643ea8Slogwang 			/* clear all_ports_up flag if any link down */
382a9643ea8Slogwang 			if (link.link_status == ETH_LINK_DOWN) {
383a9643ea8Slogwang 				all_ports_up = 0;
384a9643ea8Slogwang 				break;
385a9643ea8Slogwang 			}
386a9643ea8Slogwang 		}
387a9643ea8Slogwang 		/* after finally printing all link status, get out */
388a9643ea8Slogwang 		if (print_flag == 1)
389a9643ea8Slogwang 			break;
390a9643ea8Slogwang 
391a9643ea8Slogwang 		if (all_ports_up == 0) {
392a9643ea8Slogwang 			printf(".");
393a9643ea8Slogwang 			fflush(stdout);
394a9643ea8Slogwang 			rte_delay_ms(CHECK_INTERVAL);
395a9643ea8Slogwang 		}
396a9643ea8Slogwang 
397a9643ea8Slogwang 		/* set the print_flag if all ports up or timeout */
398a9643ea8Slogwang 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
399a9643ea8Slogwang 			print_flag = 1;
400a9643ea8Slogwang 			printf("done\n");
401a9643ea8Slogwang 		}
402a9643ea8Slogwang 	}
403a9643ea8Slogwang }
404a9643ea8Slogwang 
405a9643ea8Slogwang /* Main function.
406a9643ea8Slogwang  * Performs initialisation and then calls the lcore_main on each core
407a9643ea8Slogwang  * to do the packet-processing work.
408a9643ea8Slogwang  */
409a9643ea8Slogwang int
410a9643ea8Slogwang main(int argc, char **argv)
411a9643ea8Slogwang {
412a9643ea8Slogwang 	static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
413a9643ea8Slogwang 	int ret;
414a9643ea8Slogwang 	unsigned i;
415a9643ea8Slogwang 	enum rte_proc_type_t proc_type;
416a9643ea8Slogwang 	struct rte_mempool *mp;
417a9643ea8Slogwang 
418a9643ea8Slogwang 	/* set up signal handlers to print stats on exit */
419a9643ea8Slogwang 	signal(SIGINT, print_stats);
420a9643ea8Slogwang 	signal(SIGTERM, print_stats);
421a9643ea8Slogwang 
422a9643ea8Slogwang 	/* initialise the EAL for all */
423a9643ea8Slogwang 	ret = rte_eal_init(argc, argv);
424a9643ea8Slogwang 	if (ret < 0)
425a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
426a9643ea8Slogwang 	argc -= ret;
427a9643ea8Slogwang 	argv += ret;
428a9643ea8Slogwang 
429a9643ea8Slogwang 	/* determine the NIC devices available */
430*d30ea906Sjfb8856606 	if (rte_eth_dev_count_avail() == 0)
431a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
432a9643ea8Slogwang 
433a9643ea8Slogwang 	/* parse application arguments (those after the EAL ones) */
434a9643ea8Slogwang 	smp_parse_args(argc, argv);
435a9643ea8Slogwang 
436a9643ea8Slogwang 	proc_type = rte_eal_process_type();
437a9643ea8Slogwang 	mp = (proc_type == RTE_PROC_SECONDARY) ?
438a9643ea8Slogwang 			rte_mempool_lookup(_SMP_MBUF_POOL) :
439a9643ea8Slogwang 			rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
440a9643ea8Slogwang 				MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
441a9643ea8Slogwang 				rte_socket_id());
442a9643ea8Slogwang 	if (mp == NULL)
443a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
444a9643ea8Slogwang 
445a9643ea8Slogwang 	if (num_ports & 1)
446a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
447a9643ea8Slogwang 	for(i = 0; i < num_ports; i++){
448a9643ea8Slogwang 		if(proc_type == RTE_PROC_PRIMARY)
449a9643ea8Slogwang 			if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
450a9643ea8Slogwang 				rte_exit(EXIT_FAILURE, "Error initialising ports\n");
451a9643ea8Slogwang 	}
452a9643ea8Slogwang 
453a9643ea8Slogwang 	if (proc_type == RTE_PROC_PRIMARY)
454a9643ea8Slogwang 		check_all_ports_link_status((uint8_t)num_ports, (~0x0));
455a9643ea8Slogwang 
456a9643ea8Slogwang 	assign_ports_to_cores();
457a9643ea8Slogwang 
458a9643ea8Slogwang 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
459a9643ea8Slogwang 
460a9643ea8Slogwang 	rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MASTER);
461a9643ea8Slogwang 
462a9643ea8Slogwang 	return 0;
463a9643ea8Slogwang }
464