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