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