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