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