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