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