xref: /dpdk/examples/multi_process/simple_mp/main.c (revision 72b452c5f2599f970f47fd17d3e8e5d60bfebe7a)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3af75078fSIntel  */
4af75078fSIntel 
5af75078fSIntel /*
6af75078fSIntel  * This sample application is a simple multi-process application which
77be78d02SJosh Soref  * demonstrates sharing of queues and memory pools between processes, and
8af75078fSIntel  * using those queues/pools for communication between the processes.
9af75078fSIntel  *
10af75078fSIntel  * Application is designed to run with two processes, a primary and a
11af75078fSIntel  * secondary, and each accepts commands on the commandline, the most
12af75078fSIntel  * important of which is "send", which just sends a string to the other
13af75078fSIntel  * process.
14af75078fSIntel  */
15af75078fSIntel 
16af75078fSIntel #include <stdio.h>
17af75078fSIntel #include <string.h>
18af75078fSIntel #include <stdint.h>
19*72b452c5SDmitry Kozlyuk #include <stdlib.h>
20af75078fSIntel #include <inttypes.h>
21af75078fSIntel #include <stdarg.h>
22af75078fSIntel #include <errno.h>
23af75078fSIntel #include <unistd.h>
24af75078fSIntel #include <termios.h>
25af75078fSIntel #include <sys/queue.h>
26af75078fSIntel 
27af75078fSIntel #include <rte_common.h>
28af75078fSIntel #include <rte_memory.h>
29af75078fSIntel #include <rte_launch.h>
30af75078fSIntel #include <rte_eal.h>
31af75078fSIntel #include <rte_per_lcore.h>
32af75078fSIntel #include <rte_lcore.h>
33af75078fSIntel #include <rte_debug.h>
34af75078fSIntel #include <rte_branch_prediction.h>
35af75078fSIntel #include <rte_ring.h>
36af75078fSIntel #include <rte_log.h>
37af75078fSIntel #include <rte_mempool.h>
38af75078fSIntel #include <cmdline_rdline.h>
39af75078fSIntel #include <cmdline_parse.h>
400acd8ed3SXueming Li #include <cmdline_parse_string.h>
41af75078fSIntel #include <cmdline_socket.h>
42af75078fSIntel #include <cmdline.h>
43af75078fSIntel #include "mp_commands.h"
44af75078fSIntel 
45af75078fSIntel #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
46af75078fSIntel 
47af75078fSIntel static const char *_MSG_POOL = "MSG_POOL";
48af75078fSIntel static const char *_SEC_2_PRI = "SEC_2_PRI";
49af75078fSIntel static const char *_PRI_2_SEC = "PRI_2_SEC";
50af75078fSIntel 
51af75078fSIntel struct rte_ring *send_ring, *recv_ring;
52af75078fSIntel struct rte_mempool *message_pool;
53af75078fSIntel volatile int quit = 0;
54af75078fSIntel 
55af75078fSIntel static int
lcore_recv(__rte_unused void * arg)56f2fc83b4SThomas Monjalon lcore_recv(__rte_unused void *arg)
57af75078fSIntel {
58af75078fSIntel 	unsigned lcore_id = rte_lcore_id();
59af75078fSIntel 
60af75078fSIntel 	printf("Starting core %u\n", lcore_id);
61af75078fSIntel 	while (!quit){
62af75078fSIntel 		void *msg;
63af75078fSIntel 		if (rte_ring_dequeue(recv_ring, &msg) < 0){
64af75078fSIntel 			usleep(5);
65af75078fSIntel 			continue;
66af75078fSIntel 		}
67af75078fSIntel 		printf("core %u: Received '%s'\n", lcore_id, (char *)msg);
68af75078fSIntel 		rte_mempool_put(message_pool, msg);
69af75078fSIntel 	}
70af75078fSIntel 
71af75078fSIntel 	return 0;
72af75078fSIntel }
73af75078fSIntel 
74af75078fSIntel int
main(int argc,char ** argv)75af75078fSIntel main(int argc, char **argv)
76af75078fSIntel {
77af75078fSIntel 	const unsigned flags = 0;
78af75078fSIntel 	const unsigned ring_size = 64;
79af75078fSIntel 	const unsigned pool_size = 1024;
80af75078fSIntel 	const unsigned pool_cache = 32;
81af75078fSIntel 	const unsigned priv_data_sz = 0;
82af75078fSIntel 
83af75078fSIntel 	int ret;
84af75078fSIntel 	unsigned lcore_id;
85af75078fSIntel 
86af75078fSIntel 	ret = rte_eal_init(argc, argv);
87af75078fSIntel 	if (ret < 0)
88af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
89af75078fSIntel 
909a212dc0SConor Fogarty 	/* Start of ring structure. 8< */
91af75078fSIntel 	if (rte_eal_process_type() == RTE_PROC_PRIMARY){
92e60f71ebSIntel 		send_ring = rte_ring_create(_PRI_2_SEC, ring_size, rte_socket_id(), flags);
93e60f71ebSIntel 		recv_ring = rte_ring_create(_SEC_2_PRI, ring_size, rte_socket_id(), flags);
94af75078fSIntel 		message_pool = rte_mempool_create(_MSG_POOL, pool_size,
950acd8ed3SXueming Li 				STR_TOKEN_SIZE, pool_cache, priv_data_sz,
96af75078fSIntel 				NULL, NULL, NULL, NULL,
97e60f71ebSIntel 				rte_socket_id(), flags);
98af75078fSIntel 	} else {
99af75078fSIntel 		recv_ring = rte_ring_lookup(_PRI_2_SEC);
100af75078fSIntel 		send_ring = rte_ring_lookup(_SEC_2_PRI);
101af75078fSIntel 		message_pool = rte_mempool_lookup(_MSG_POOL);
102af75078fSIntel 	}
1039a212dc0SConor Fogarty 	/* >8 End of ring structure. */
104af75078fSIntel 	if (send_ring == NULL)
105af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting sending ring\n");
106af75078fSIntel 	if (recv_ring == NULL)
107af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting receiving ring\n");
108af75078fSIntel 	if (message_pool == NULL)
109af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
110af75078fSIntel 
111af75078fSIntel 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
112af75078fSIntel 
113cb056611SStephen Hemminger 	/* call lcore_recv() on every worker lcore */
114cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
115af75078fSIntel 		rte_eal_remote_launch(lcore_recv, NULL, lcore_id);
116af75078fSIntel 	}
117af75078fSIntel 
118cb056611SStephen Hemminger 	/* call cmd prompt on main lcore */
119af75078fSIntel 	struct cmdline *cl = cmdline_stdin_new(simple_mp_ctx, "\nsimple_mp > ");
120af75078fSIntel 	if (cl == NULL)
121af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot create cmdline instance\n");
122af75078fSIntel 	cmdline_interact(cl);
123af75078fSIntel 	cmdline_stdin_exit(cl);
124af75078fSIntel 
125af75078fSIntel 	rte_eal_mp_wait_lcore();
12610aa3757SChengchang Tang 
12710aa3757SChengchang Tang 	/* clean up the EAL */
12810aa3757SChengchang Tang 	rte_eal_cleanup();
12910aa3757SChengchang Tang 
130af75078fSIntel 	return 0;
131af75078fSIntel }
132