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
7*7be78d02SJosh 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>
19af75078fSIntel #include <inttypes.h>
20af75078fSIntel #include <stdarg.h>
21af75078fSIntel #include <errno.h>
22af75078fSIntel #include <unistd.h>
23af75078fSIntel #include <termios.h>
24af75078fSIntel #include <sys/queue.h>
25af75078fSIntel 
26af75078fSIntel #include <rte_common.h>
27af75078fSIntel #include <rte_memory.h>
28af75078fSIntel #include <rte_launch.h>
29af75078fSIntel #include <rte_eal.h>
30af75078fSIntel #include <rte_per_lcore.h>
31af75078fSIntel #include <rte_lcore.h>
32af75078fSIntel #include <rte_debug.h>
33af75078fSIntel #include <rte_branch_prediction.h>
34af75078fSIntel #include <rte_ring.h>
35af75078fSIntel #include <rte_log.h>
36af75078fSIntel #include <rte_mempool.h>
37af75078fSIntel #include <cmdline_rdline.h>
38af75078fSIntel #include <cmdline_parse.h>
390acd8ed3SXueming Li #include <cmdline_parse_string.h>
40af75078fSIntel #include <cmdline_socket.h>
41af75078fSIntel #include <cmdline.h>
42af75078fSIntel #include "mp_commands.h"
43af75078fSIntel 
44af75078fSIntel #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
45af75078fSIntel 
46af75078fSIntel static const char *_MSG_POOL = "MSG_POOL";
47af75078fSIntel static const char *_SEC_2_PRI = "SEC_2_PRI";
48af75078fSIntel static const char *_PRI_2_SEC = "PRI_2_SEC";
49af75078fSIntel 
50af75078fSIntel struct rte_ring *send_ring, *recv_ring;
51af75078fSIntel struct rte_mempool *message_pool;
52af75078fSIntel volatile int quit = 0;
53af75078fSIntel 
54af75078fSIntel static int
lcore_recv(__rte_unused void * arg)55f2fc83b4SThomas Monjalon lcore_recv(__rte_unused void *arg)
56af75078fSIntel {
57af75078fSIntel 	unsigned lcore_id = rte_lcore_id();
58af75078fSIntel 
59af75078fSIntel 	printf("Starting core %u\n", lcore_id);
60af75078fSIntel 	while (!quit){
61af75078fSIntel 		void *msg;
62af75078fSIntel 		if (rte_ring_dequeue(recv_ring, &msg) < 0){
63af75078fSIntel 			usleep(5);
64af75078fSIntel 			continue;
65af75078fSIntel 		}
66af75078fSIntel 		printf("core %u: Received '%s'\n", lcore_id, (char *)msg);
67af75078fSIntel 		rte_mempool_put(message_pool, msg);
68af75078fSIntel 	}
69af75078fSIntel 
70af75078fSIntel 	return 0;
71af75078fSIntel }
72af75078fSIntel 
73af75078fSIntel int
main(int argc,char ** argv)74af75078fSIntel main(int argc, char **argv)
75af75078fSIntel {
76af75078fSIntel 	const unsigned flags = 0;
77af75078fSIntel 	const unsigned ring_size = 64;
78af75078fSIntel 	const unsigned pool_size = 1024;
79af75078fSIntel 	const unsigned pool_cache = 32;
80af75078fSIntel 	const unsigned priv_data_sz = 0;
81af75078fSIntel 
82af75078fSIntel 	int ret;
83af75078fSIntel 	unsigned lcore_id;
84af75078fSIntel 
85af75078fSIntel 	ret = rte_eal_init(argc, argv);
86af75078fSIntel 	if (ret < 0)
87af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
88af75078fSIntel 
899a212dc0SConor Fogarty 	/* Start of ring structure. 8< */
90af75078fSIntel 	if (rte_eal_process_type() == RTE_PROC_PRIMARY){
91e60f71ebSIntel 		send_ring = rte_ring_create(_PRI_2_SEC, ring_size, rte_socket_id(), flags);
92e60f71ebSIntel 		recv_ring = rte_ring_create(_SEC_2_PRI, ring_size, rte_socket_id(), flags);
93af75078fSIntel 		message_pool = rte_mempool_create(_MSG_POOL, pool_size,
940acd8ed3SXueming Li 				STR_TOKEN_SIZE, pool_cache, priv_data_sz,
95af75078fSIntel 				NULL, NULL, NULL, NULL,
96e60f71ebSIntel 				rte_socket_id(), flags);
97af75078fSIntel 	} else {
98af75078fSIntel 		recv_ring = rte_ring_lookup(_PRI_2_SEC);
99af75078fSIntel 		send_ring = rte_ring_lookup(_SEC_2_PRI);
100af75078fSIntel 		message_pool = rte_mempool_lookup(_MSG_POOL);
101af75078fSIntel 	}
1029a212dc0SConor Fogarty 	/* >8 End of ring structure. */
103af75078fSIntel 	if (send_ring == NULL)
104af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting sending ring\n");
105af75078fSIntel 	if (recv_ring == NULL)
106af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting receiving ring\n");
107af75078fSIntel 	if (message_pool == NULL)
108af75078fSIntel 		rte_exit(EXIT_FAILURE, "Problem getting message pool\n");
109af75078fSIntel 
110af75078fSIntel 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
111af75078fSIntel 
112cb056611SStephen Hemminger 	/* call lcore_recv() on every worker lcore */
113cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
114af75078fSIntel 		rte_eal_remote_launch(lcore_recv, NULL, lcore_id);
115af75078fSIntel 	}
116af75078fSIntel 
117cb056611SStephen Hemminger 	/* call cmd prompt on main lcore */
118af75078fSIntel 	struct cmdline *cl = cmdline_stdin_new(simple_mp_ctx, "\nsimple_mp > ");
119af75078fSIntel 	if (cl == NULL)
120af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot create cmdline instance\n");
121af75078fSIntel 	cmdline_interact(cl);
122af75078fSIntel 	cmdline_stdin_exit(cl);
123af75078fSIntel 
124af75078fSIntel 	rte_eal_mp_wait_lcore();
12510aa3757SChengchang Tang 
12610aa3757SChengchang Tang 	/* clean up the EAL */
12710aa3757SChengchang Tang 	rte_eal_cleanup();
12810aa3757SChengchang Tang 
129af75078fSIntel 	return 0;
130af75078fSIntel }
131