1a9643ea8Slogwang /*-
2a9643ea8Slogwang  *   BSD LICENSE
3a9643ea8Slogwang  *
4a9643ea8Slogwang  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5a9643ea8Slogwang  *   All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  *   Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  *   modification, are permitted provided that the following conditions
9a9643ea8Slogwang  *   are met:
10a9643ea8Slogwang  *
11a9643ea8Slogwang  *     * Redistributions of source code must retain the above copyright
12a9643ea8Slogwang  *       notice, this list of conditions and the following disclaimer.
13a9643ea8Slogwang  *     * Redistributions in binary form must reproduce the above copyright
14a9643ea8Slogwang  *       notice, this list of conditions and the following disclaimer in
15a9643ea8Slogwang  *       the documentation and/or other materials provided with the
16a9643ea8Slogwang  *       distribution.
17a9643ea8Slogwang  *     * Neither the name of Intel Corporation nor the names of its
18a9643ea8Slogwang  *       contributors may be used to endorse or promote products derived
19a9643ea8Slogwang  *       from this software without specific prior written permission.
20a9643ea8Slogwang  *
21a9643ea8Slogwang  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22a9643ea8Slogwang  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23a9643ea8Slogwang  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24a9643ea8Slogwang  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25a9643ea8Slogwang  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26a9643ea8Slogwang  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27a9643ea8Slogwang  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28a9643ea8Slogwang  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29a9643ea8Slogwang  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30a9643ea8Slogwang  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31a9643ea8Slogwang  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang /*
35a9643ea8Slogwang  * Sample application demostrating how to do packet I/O in a multi-process
36a9643ea8Slogwang  * environment. The same code can be run as a primary process and as a
37a9643ea8Slogwang  * secondary process, just with a different proc-id parameter in each case
38a9643ea8Slogwang  * (apart from the EAL flag to indicate a secondary process).
39a9643ea8Slogwang  *
40a9643ea8Slogwang  * Each process will read from the same ports, given by the port-mask
41a9643ea8Slogwang  * parameter, which should be the same in each case, just using a different
42a9643ea8Slogwang  * queue per port as determined by the proc-id parameter.
43a9643ea8Slogwang  */
44a9643ea8Slogwang 
45a9643ea8Slogwang #include <stdio.h>
46a9643ea8Slogwang #include <string.h>
47a9643ea8Slogwang #include <stdint.h>
48a9643ea8Slogwang #include <stdlib.h>
49a9643ea8Slogwang #include <stdarg.h>
50a9643ea8Slogwang #include <errno.h>
51a9643ea8Slogwang #include <sys/queue.h>
52a9643ea8Slogwang #include <getopt.h>
53a9643ea8Slogwang #include <signal.h>
54a9643ea8Slogwang #include <inttypes.h>
55a9643ea8Slogwang 
56a9643ea8Slogwang #include <rte_common.h>
57a9643ea8Slogwang #include <rte_log.h>
58a9643ea8Slogwang #include <rte_memory.h>
59a9643ea8Slogwang #include <rte_launch.h>
60a9643ea8Slogwang #include <rte_eal.h>
61a9643ea8Slogwang #include <rte_per_lcore.h>
62a9643ea8Slogwang #include <rte_lcore.h>
63a9643ea8Slogwang #include <rte_atomic.h>
64a9643ea8Slogwang #include <rte_branch_prediction.h>
65a9643ea8Slogwang #include <rte_debug.h>
66a9643ea8Slogwang #include <rte_interrupts.h>
67a9643ea8Slogwang #include <rte_ether.h>
68a9643ea8Slogwang #include <rte_ethdev.h>
69a9643ea8Slogwang #include <rte_mempool.h>
70a9643ea8Slogwang #include <rte_memcpy.h>
71a9643ea8Slogwang #include <rte_mbuf.h>
72a9643ea8Slogwang #include <rte_string_fns.h>
73a9643ea8Slogwang #include <rte_cycles.h>
74a9643ea8Slogwang 
75a9643ea8Slogwang #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
76a9643ea8Slogwang 
77a9643ea8Slogwang #define NB_MBUFS 64*1024 /* use 64k mbufs */
78a9643ea8Slogwang #define MBUF_CACHE_SIZE 256
79a9643ea8Slogwang #define PKT_BURST 32
80a9643ea8Slogwang #define RX_RING_SIZE 128
81a9643ea8Slogwang #define TX_RING_SIZE 512
82a9643ea8Slogwang 
83a9643ea8Slogwang #define PARAM_PROC_ID "proc-id"
84a9643ea8Slogwang #define PARAM_NUM_PROCS "num-procs"
85a9643ea8Slogwang 
86a9643ea8Slogwang /* for each lcore, record the elements of the ports array to use */
87a9643ea8Slogwang struct lcore_ports{
88a9643ea8Slogwang 	unsigned start_port;
89a9643ea8Slogwang 	unsigned num_ports;
90a9643ea8Slogwang };
91a9643ea8Slogwang 
92a9643ea8Slogwang /* structure to record the rx and tx packets. Put two per cache line as ports
93a9643ea8Slogwang  * used in pairs */
94a9643ea8Slogwang struct port_stats{
95a9643ea8Slogwang 	unsigned rx;
96a9643ea8Slogwang 	unsigned tx;
97a9643ea8Slogwang 	unsigned drop;
98a9643ea8Slogwang } __attribute__((aligned(RTE_CACHE_LINE_SIZE / 2)));
99a9643ea8Slogwang 
100a9643ea8Slogwang static int proc_id = -1;
101a9643ea8Slogwang static unsigned num_procs = 0;
102a9643ea8Slogwang 
103*2bfe3f2eSlogwang static uint16_t ports[RTE_MAX_ETHPORTS];
104a9643ea8Slogwang static unsigned num_ports = 0;
105a9643ea8Slogwang 
106a9643ea8Slogwang static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
107a9643ea8Slogwang static struct port_stats pstats[RTE_MAX_ETHPORTS];
108a9643ea8Slogwang 
109a9643ea8Slogwang /* prints the usage statement and quits with an error message */
110a9643ea8Slogwang static void
111a9643ea8Slogwang smp_usage(const char *prgname, const char *errmsg)
112a9643ea8Slogwang {
113a9643ea8Slogwang 	printf("\nError: %s\n",errmsg);
114a9643ea8Slogwang 	printf("\n%s [EAL options] -- -p <port mask> "
115a9643ea8Slogwang 			"--"PARAM_NUM_PROCS" <n>"
116a9643ea8Slogwang 			" --"PARAM_PROC_ID" <id>\n"
117a9643ea8Slogwang 			"-p         : a hex bitmask indicating what ports are to be used\n"
118a9643ea8Slogwang 			"--num-procs: the number of processes which will be used\n"
119a9643ea8Slogwang 			"--proc-id  : the id of the current process (id < num-procs)\n"
120a9643ea8Slogwang 			"\n",
121a9643ea8Slogwang 			prgname);
122a9643ea8Slogwang 	exit(1);
123a9643ea8Slogwang }
124a9643ea8Slogwang 
125a9643ea8Slogwang 
126a9643ea8Slogwang /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
127a9643ea8Slogwang static void
128a9643ea8Slogwang print_stats(int signum)
129a9643ea8Slogwang {
130a9643ea8Slogwang 	unsigned i;
131a9643ea8Slogwang 	printf("\nExiting on signal %d\n\n", signum);
132a9643ea8Slogwang 	for (i = 0; i < num_ports; i++){
133a9643ea8Slogwang 		const uint8_t p_num = ports[i];
134a9643ea8Slogwang 		printf("Port %u: RX - %u, TX - %u, Drop - %u\n", (unsigned)p_num,
135a9643ea8Slogwang 				pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop);
136a9643ea8Slogwang 	}
137a9643ea8Slogwang 	exit(0);
138a9643ea8Slogwang }
139a9643ea8Slogwang 
140a9643ea8Slogwang /* Parse the argument given in the command line of the application */
141a9643ea8Slogwang static int
142a9643ea8Slogwang smp_parse_args(int argc, char **argv)
143a9643ea8Slogwang {
144a9643ea8Slogwang 	int opt, ret;
145a9643ea8Slogwang 	char **argvopt;
146a9643ea8Slogwang 	int option_index;
147a9643ea8Slogwang 	unsigned i, port_mask = 0;
148a9643ea8Slogwang 	char *prgname = argv[0];
149a9643ea8Slogwang 	static struct option lgopts[] = {
150a9643ea8Slogwang 			{PARAM_NUM_PROCS, 1, 0, 0},
151a9643ea8Slogwang 			{PARAM_PROC_ID, 1, 0, 0},
152a9643ea8Slogwang 			{NULL, 0, 0, 0}
153a9643ea8Slogwang 	};
154a9643ea8Slogwang 
155a9643ea8Slogwang 	argvopt = argv;
156a9643ea8Slogwang 
157a9643ea8Slogwang 	while ((opt = getopt_long(argc, argvopt, "p:", \
158a9643ea8Slogwang 			lgopts, &option_index)) != EOF) {
159a9643ea8Slogwang 
160a9643ea8Slogwang 		switch (opt) {
161a9643ea8Slogwang 		case 'p':
162a9643ea8Slogwang 			port_mask = strtoull(optarg, NULL, 16);
163a9643ea8Slogwang 			break;
164a9643ea8Slogwang 			/* long options */
165a9643ea8Slogwang 		case 0:
166a9643ea8Slogwang 			if (strncmp(lgopts[option_index].name, PARAM_NUM_PROCS, 8) == 0)
167a9643ea8Slogwang 				num_procs = atoi(optarg);
168a9643ea8Slogwang 			else if (strncmp(lgopts[option_index].name, PARAM_PROC_ID, 7) == 0)
169a9643ea8Slogwang 				proc_id = atoi(optarg);
170a9643ea8Slogwang 			break;
171a9643ea8Slogwang 
172a9643ea8Slogwang 		default:
173a9643ea8Slogwang 			smp_usage(prgname, "Cannot parse all command-line arguments\n");
174a9643ea8Slogwang 		}
175a9643ea8Slogwang 	}
176a9643ea8Slogwang 
177a9643ea8Slogwang 	if (optind >= 0)
178a9643ea8Slogwang 		argv[optind-1] = prgname;
179a9643ea8Slogwang 
180a9643ea8Slogwang 	if (proc_id < 0)
181a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing proc-id parameter\n");
182a9643ea8Slogwang 	if (rte_eal_process_type() == RTE_PROC_PRIMARY && num_procs == 0)
183a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing num-procs parameter\n");
184a9643ea8Slogwang 	if (port_mask == 0)
185a9643ea8Slogwang 		smp_usage(prgname, "Invalid or missing port mask\n");
186a9643ea8Slogwang 
187a9643ea8Slogwang 	/* get the port numbers from the port mask */
188a9643ea8Slogwang 	for(i = 0; i < rte_eth_dev_count(); i++)
189a9643ea8Slogwang 		if(port_mask & (1 << i))
190a9643ea8Slogwang 			ports[num_ports++] = (uint8_t)i;
191a9643ea8Slogwang 
192a9643ea8Slogwang 	ret = optind-1;
193*2bfe3f2eSlogwang 	optind = 1; /* reset getopt lib */
194a9643ea8Slogwang 
195a9643ea8Slogwang 	return ret;
196a9643ea8Slogwang }
197a9643ea8Slogwang 
198a9643ea8Slogwang /*
199a9643ea8Slogwang  * Initialises a given port using global settings and with the rx buffers
200a9643ea8Slogwang  * coming from the mbuf_pool passed as parameter
201a9643ea8Slogwang  */
202a9643ea8Slogwang static inline int
203*2bfe3f2eSlogwang smp_port_init(uint16_t port, struct rte_mempool *mbuf_pool,
204*2bfe3f2eSlogwang 	       uint16_t num_queues)
205a9643ea8Slogwang {
206a9643ea8Slogwang 	struct rte_eth_conf port_conf = {
207a9643ea8Slogwang 			.rxmode = {
208a9643ea8Slogwang 				.mq_mode	= ETH_MQ_RX_RSS,
209a9643ea8Slogwang 				.split_hdr_size = 0,
210a9643ea8Slogwang 				.header_split   = 0, /**< Header Split disabled */
211a9643ea8Slogwang 				.hw_ip_checksum = 1, /**< IP checksum offload enabled */
212a9643ea8Slogwang 				.hw_vlan_filter = 0, /**< VLAN filtering disabled */
213a9643ea8Slogwang 				.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
214*2bfe3f2eSlogwang 				.hw_strip_crc   = 1, /**< CRC stripped by hardware */
215a9643ea8Slogwang 			},
216a9643ea8Slogwang 			.rx_adv_conf = {
217a9643ea8Slogwang 				.rss_conf = {
218a9643ea8Slogwang 					.rss_key = NULL,
219a9643ea8Slogwang 					.rss_hf = ETH_RSS_IP,
220a9643ea8Slogwang 				},
221a9643ea8Slogwang 			},
222a9643ea8Slogwang 			.txmode = {
223a9643ea8Slogwang 				.mq_mode = ETH_MQ_TX_NONE,
224a9643ea8Slogwang 			}
225a9643ea8Slogwang 	};
226a9643ea8Slogwang 	const uint16_t rx_rings = num_queues, tx_rings = num_queues;
227a9643ea8Slogwang 	struct rte_eth_dev_info info;
228a9643ea8Slogwang 	int retval;
229a9643ea8Slogwang 	uint16_t q;
230*2bfe3f2eSlogwang 	uint16_t nb_rxd = RX_RING_SIZE;
231*2bfe3f2eSlogwang 	uint16_t nb_txd = TX_RING_SIZE;
232a9643ea8Slogwang 
233a9643ea8Slogwang 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
234a9643ea8Slogwang 		return 0;
235a9643ea8Slogwang 
236a9643ea8Slogwang 	if (port >= rte_eth_dev_count())
237a9643ea8Slogwang 		return -1;
238a9643ea8Slogwang 
239*2bfe3f2eSlogwang 	printf("# Initialising port %u... ", port);
240a9643ea8Slogwang 	fflush(stdout);
241a9643ea8Slogwang 
242a9643ea8Slogwang 	rte_eth_dev_info_get(port, &info);
243a9643ea8Slogwang 	info.default_rxconf.rx_drop_en = 1;
244a9643ea8Slogwang 
245a9643ea8Slogwang 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
246a9643ea8Slogwang 	if (retval < 0)
247a9643ea8Slogwang 		return retval;
248a9643ea8Slogwang 
249*2bfe3f2eSlogwang 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
250*2bfe3f2eSlogwang 	if (retval < 0)
251*2bfe3f2eSlogwang 		return retval;
252*2bfe3f2eSlogwang 
253a9643ea8Slogwang 	for (q = 0; q < rx_rings; q ++) {
254*2bfe3f2eSlogwang 		retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
255a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
256a9643ea8Slogwang 				&info.default_rxconf,
257a9643ea8Slogwang 				mbuf_pool);
258a9643ea8Slogwang 		if (retval < 0)
259a9643ea8Slogwang 			return retval;
260a9643ea8Slogwang 	}
261a9643ea8Slogwang 
262a9643ea8Slogwang 	for (q = 0; q < tx_rings; q ++) {
263*2bfe3f2eSlogwang 		retval = rte_eth_tx_queue_setup(port, q, nb_txd,
264a9643ea8Slogwang 				rte_eth_dev_socket_id(port),
265a9643ea8Slogwang 				NULL);
266a9643ea8Slogwang 		if (retval < 0)
267a9643ea8Slogwang 			return retval;
268a9643ea8Slogwang 	}
269a9643ea8Slogwang 
270a9643ea8Slogwang 	rte_eth_promiscuous_enable(port);
271a9643ea8Slogwang 
272a9643ea8Slogwang 	retval  = rte_eth_dev_start(port);
273a9643ea8Slogwang 	if (retval < 0)
274a9643ea8Slogwang 		return retval;
275a9643ea8Slogwang 
276a9643ea8Slogwang 	return 0;
277a9643ea8Slogwang }
278a9643ea8Slogwang 
279a9643ea8Slogwang /* Goes through each of the lcores and calculates what ports should
280a9643ea8Slogwang  * be used by that core. Fills in the global lcore_ports[] array.
281a9643ea8Slogwang  */
282a9643ea8Slogwang static void
283a9643ea8Slogwang assign_ports_to_cores(void)
284a9643ea8Slogwang {
285a9643ea8Slogwang 
286a9643ea8Slogwang 	const unsigned lcores = rte_eal_get_configuration()->lcore_count;
287a9643ea8Slogwang 	const unsigned port_pairs = num_ports / 2;
288a9643ea8Slogwang 	const unsigned pairs_per_lcore = port_pairs / lcores;
289a9643ea8Slogwang 	unsigned extra_pairs = port_pairs % lcores;
290a9643ea8Slogwang 	unsigned ports_assigned = 0;
291a9643ea8Slogwang 	unsigned i;
292a9643ea8Slogwang 
293a9643ea8Slogwang 	RTE_LCORE_FOREACH(i) {
294a9643ea8Slogwang 		lcore_ports[i].start_port = ports_assigned;
295a9643ea8Slogwang 		lcore_ports[i].num_ports = pairs_per_lcore * 2;
296a9643ea8Slogwang 		if (extra_pairs > 0) {
297a9643ea8Slogwang 			lcore_ports[i].num_ports += 2;
298a9643ea8Slogwang 			extra_pairs--;
299a9643ea8Slogwang 		}
300a9643ea8Slogwang 		ports_assigned += lcore_ports[i].num_ports;
301a9643ea8Slogwang 	}
302a9643ea8Slogwang }
303a9643ea8Slogwang 
304a9643ea8Slogwang /* Main function used by the processing threads.
305a9643ea8Slogwang  * Prints out some configuration details for the thread and then begins
306a9643ea8Slogwang  * performing packet RX and TX.
307a9643ea8Slogwang  */
308a9643ea8Slogwang static int
309a9643ea8Slogwang lcore_main(void *arg __rte_unused)
310a9643ea8Slogwang {
311a9643ea8Slogwang 	const unsigned id = rte_lcore_id();
312a9643ea8Slogwang 	const unsigned start_port = lcore_ports[id].start_port;
313a9643ea8Slogwang 	const unsigned end_port = start_port + lcore_ports[id].num_ports;
314a9643ea8Slogwang 	const uint16_t q_id = (uint16_t)proc_id;
315a9643ea8Slogwang 	unsigned p, i;
316a9643ea8Slogwang 	char msgbuf[256];
317a9643ea8Slogwang 	int msgbufpos = 0;
318a9643ea8Slogwang 
319a9643ea8Slogwang 	if (start_port == end_port){
320a9643ea8Slogwang 		printf("Lcore %u has nothing to do\n", id);
321a9643ea8Slogwang 		return 0;
322a9643ea8Slogwang 	}
323a9643ea8Slogwang 
324a9643ea8Slogwang 	/* build up message in msgbuf before printing to decrease likelihood
325a9643ea8Slogwang 	 * of multi-core message interleaving.
326a9643ea8Slogwang 	 */
327a9643ea8Slogwang 	msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
328a9643ea8Slogwang 			"Lcore %u using ports ", id);
329a9643ea8Slogwang 	for (p = start_port; p < end_port; p++){
330a9643ea8Slogwang 		msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
331a9643ea8Slogwang 				"%u ", (unsigned)ports[p]);
332a9643ea8Slogwang 	}
333a9643ea8Slogwang 	printf("%s\n", msgbuf);
334a9643ea8Slogwang 	printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
335a9643ea8Slogwang 
336a9643ea8Slogwang 	/* handle packet I/O from the ports, reading and writing to the
337a9643ea8Slogwang 	 * queue number corresponding to our process number (not lcore id)
338a9643ea8Slogwang 	 */
339a9643ea8Slogwang 
340a9643ea8Slogwang 	for (;;) {
341a9643ea8Slogwang 		struct rte_mbuf *buf[PKT_BURST];
342a9643ea8Slogwang 
343a9643ea8Slogwang 		for (p = start_port; p < end_port; p++) {
344a9643ea8Slogwang 			const uint8_t src = ports[p];
345a9643ea8Slogwang 			const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
346a9643ea8Slogwang 			const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
347a9643ea8Slogwang 			if (rx_c == 0)
348a9643ea8Slogwang 				continue;
349a9643ea8Slogwang 			pstats[src].rx += rx_c;
350a9643ea8Slogwang 
351a9643ea8Slogwang 			const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
352a9643ea8Slogwang 			pstats[dst].tx += tx_c;
353a9643ea8Slogwang 			if (tx_c != rx_c) {
354a9643ea8Slogwang 				pstats[dst].drop += (rx_c - tx_c);
355a9643ea8Slogwang 				for (i = tx_c; i < rx_c; i++)
356a9643ea8Slogwang 					rte_pktmbuf_free(buf[i]);
357a9643ea8Slogwang 			}
358a9643ea8Slogwang 		}
359a9643ea8Slogwang 	}
360a9643ea8Slogwang }
361a9643ea8Slogwang 
362a9643ea8Slogwang /* Check the link status of all ports in up to 9s, and print them finally */
363a9643ea8Slogwang static void
364*2bfe3f2eSlogwang check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
365a9643ea8Slogwang {
366a9643ea8Slogwang #define CHECK_INTERVAL 100 /* 100ms */
367a9643ea8Slogwang #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
368*2bfe3f2eSlogwang 	uint16_t portid;
369*2bfe3f2eSlogwang 	uint8_t count, all_ports_up, print_flag = 0;
370a9643ea8Slogwang 	struct rte_eth_link link;
371a9643ea8Slogwang 
372a9643ea8Slogwang 	printf("\nChecking link status");
373a9643ea8Slogwang 	fflush(stdout);
374a9643ea8Slogwang 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
375a9643ea8Slogwang 		all_ports_up = 1;
376a9643ea8Slogwang 		for (portid = 0; portid < port_num; portid++) {
377a9643ea8Slogwang 			if ((port_mask & (1 << portid)) == 0)
378a9643ea8Slogwang 				continue;
379a9643ea8Slogwang 			memset(&link, 0, sizeof(link));
380a9643ea8Slogwang 			rte_eth_link_get_nowait(portid, &link);
381a9643ea8Slogwang 			/* print link status if flag set */
382a9643ea8Slogwang 			if (print_flag == 1) {
383a9643ea8Slogwang 				if (link.link_status)
384*2bfe3f2eSlogwang 					printf(
385*2bfe3f2eSlogwang 					"Port%d Link Up. Speed %u Mbps - %s\n",
386*2bfe3f2eSlogwang 						portid, link.link_speed,
387a9643ea8Slogwang 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
388a9643ea8Slogwang 					("full-duplex") : ("half-duplex\n"));
389a9643ea8Slogwang 				else
390*2bfe3f2eSlogwang 					printf("Port %d Link Down\n", portid);
391a9643ea8Slogwang 				continue;
392a9643ea8Slogwang 			}
393a9643ea8Slogwang 			/* clear all_ports_up flag if any link down */
394a9643ea8Slogwang 			if (link.link_status == ETH_LINK_DOWN) {
395a9643ea8Slogwang 				all_ports_up = 0;
396a9643ea8Slogwang 				break;
397a9643ea8Slogwang 			}
398a9643ea8Slogwang 		}
399a9643ea8Slogwang 		/* after finally printing all link status, get out */
400a9643ea8Slogwang 		if (print_flag == 1)
401a9643ea8Slogwang 			break;
402a9643ea8Slogwang 
403a9643ea8Slogwang 		if (all_ports_up == 0) {
404a9643ea8Slogwang 			printf(".");
405a9643ea8Slogwang 			fflush(stdout);
406a9643ea8Slogwang 			rte_delay_ms(CHECK_INTERVAL);
407a9643ea8Slogwang 		}
408a9643ea8Slogwang 
409a9643ea8Slogwang 		/* set the print_flag if all ports up or timeout */
410a9643ea8Slogwang 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
411a9643ea8Slogwang 			print_flag = 1;
412a9643ea8Slogwang 			printf("done\n");
413a9643ea8Slogwang 		}
414a9643ea8Slogwang 	}
415a9643ea8Slogwang }
416a9643ea8Slogwang 
417a9643ea8Slogwang /* Main function.
418a9643ea8Slogwang  * Performs initialisation and then calls the lcore_main on each core
419a9643ea8Slogwang  * to do the packet-processing work.
420a9643ea8Slogwang  */
421a9643ea8Slogwang int
422a9643ea8Slogwang main(int argc, char **argv)
423a9643ea8Slogwang {
424a9643ea8Slogwang 	static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
425a9643ea8Slogwang 	int ret;
426a9643ea8Slogwang 	unsigned i;
427a9643ea8Slogwang 	enum rte_proc_type_t proc_type;
428a9643ea8Slogwang 	struct rte_mempool *mp;
429a9643ea8Slogwang 
430a9643ea8Slogwang 	/* set up signal handlers to print stats on exit */
431a9643ea8Slogwang 	signal(SIGINT, print_stats);
432a9643ea8Slogwang 	signal(SIGTERM, print_stats);
433a9643ea8Slogwang 
434a9643ea8Slogwang 	/* initialise the EAL for all */
435a9643ea8Slogwang 	ret = rte_eal_init(argc, argv);
436a9643ea8Slogwang 	if (ret < 0)
437a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
438a9643ea8Slogwang 	argc -= ret;
439a9643ea8Slogwang 	argv += ret;
440a9643ea8Slogwang 
441a9643ea8Slogwang 	/* determine the NIC devices available */
442a9643ea8Slogwang 	if (rte_eth_dev_count() == 0)
443a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
444a9643ea8Slogwang 
445a9643ea8Slogwang 	/* parse application arguments (those after the EAL ones) */
446a9643ea8Slogwang 	smp_parse_args(argc, argv);
447a9643ea8Slogwang 
448a9643ea8Slogwang 	proc_type = rte_eal_process_type();
449a9643ea8Slogwang 	mp = (proc_type == RTE_PROC_SECONDARY) ?
450a9643ea8Slogwang 			rte_mempool_lookup(_SMP_MBUF_POOL) :
451a9643ea8Slogwang 			rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
452a9643ea8Slogwang 				MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
453a9643ea8Slogwang 				rte_socket_id());
454a9643ea8Slogwang 	if (mp == NULL)
455a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
456a9643ea8Slogwang 
457a9643ea8Slogwang 	if (num_ports & 1)
458a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
459a9643ea8Slogwang 	for(i = 0; i < num_ports; i++){
460a9643ea8Slogwang 		if(proc_type == RTE_PROC_PRIMARY)
461a9643ea8Slogwang 			if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
462a9643ea8Slogwang 				rte_exit(EXIT_FAILURE, "Error initialising ports\n");
463a9643ea8Slogwang 	}
464a9643ea8Slogwang 
465a9643ea8Slogwang 	if (proc_type == RTE_PROC_PRIMARY)
466a9643ea8Slogwang 		check_all_ports_link_status((uint8_t)num_ports, (~0x0));
467a9643ea8Slogwang 
468a9643ea8Slogwang 	assign_ports_to_cores();
469a9643ea8Slogwang 
470a9643ea8Slogwang 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
471a9643ea8Slogwang 
472a9643ea8Slogwang 	rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MASTER);
473a9643ea8Slogwang 
474a9643ea8Slogwang 	return 0;
475a9643ea8Slogwang }
476