xref: /dpdk/examples/multi_process/simple_mp/mp_commands.c (revision 0acd8ed3330a629658e3864d78a6bd4b78de321e)
1af75078fSIntel /*-
2af75078fSIntel  *   BSD LICENSE
3af75078fSIntel  *
4e9d48c00SBruce Richardson  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5af75078fSIntel  *   All rights reserved.
6af75078fSIntel  *
7af75078fSIntel  *   Redistribution and use in source and binary forms, with or without
8af75078fSIntel  *   modification, are permitted provided that the following conditions
9af75078fSIntel  *   are met:
10af75078fSIntel  *
11af75078fSIntel  *     * Redistributions of source code must retain the above copyright
12af75078fSIntel  *       notice, this list of conditions and the following disclaimer.
13af75078fSIntel  *     * Redistributions in binary form must reproduce the above copyright
14af75078fSIntel  *       notice, this list of conditions and the following disclaimer in
15af75078fSIntel  *       the documentation and/or other materials provided with the
16af75078fSIntel  *       distribution.
17af75078fSIntel  *     * Neither the name of Intel Corporation nor the names of its
18af75078fSIntel  *       contributors may be used to endorse or promote products derived
19af75078fSIntel  *       from this software without specific prior written permission.
20af75078fSIntel  *
21af75078fSIntel  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22af75078fSIntel  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23af75078fSIntel  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24af75078fSIntel  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25af75078fSIntel  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26af75078fSIntel  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27af75078fSIntel  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28af75078fSIntel  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29af75078fSIntel  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30af75078fSIntel  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31af75078fSIntel  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32af75078fSIntel  */
33af75078fSIntel #include <stdint.h>
34af75078fSIntel #include <string.h>
35af75078fSIntel #include <stdlib.h>
36af75078fSIntel #include <stdarg.h>
37af75078fSIntel #include <inttypes.h>
38af75078fSIntel #include <stdio.h>
39af75078fSIntel #include <termios.h>
40af75078fSIntel #include <errno.h>
41af75078fSIntel #include <sys/queue.h>
42af75078fSIntel 
43af75078fSIntel #include <rte_common.h>
44af75078fSIntel #include <rte_memory.h>
45af75078fSIntel #include <rte_memzone.h>
46af75078fSIntel #include <rte_eal.h>
47af75078fSIntel #include <rte_atomic.h>
48af75078fSIntel #include <rte_branch_prediction.h>
49af75078fSIntel #include <rte_launch.h>
50af75078fSIntel #include <rte_log.h>
51af75078fSIntel #include <rte_per_lcore.h>
52af75078fSIntel #include <rte_lcore.h>
53af75078fSIntel #include <rte_ring.h>
54af75078fSIntel #include <rte_debug.h>
55af75078fSIntel #include <rte_mempool.h>
56af75078fSIntel #include <rte_string_fns.h>
57af75078fSIntel 
58af75078fSIntel #include <cmdline_rdline.h>
59af75078fSIntel #include <cmdline_parse.h>
60af75078fSIntel #include <cmdline_parse_string.h>
61af75078fSIntel #include <cmdline_socket.h>
62af75078fSIntel #include <cmdline.h>
63af75078fSIntel #include "mp_commands.h"
64af75078fSIntel 
65af75078fSIntel /**********************************************************/
66af75078fSIntel 
67af75078fSIntel struct cmd_send_result {
68af75078fSIntel 	cmdline_fixed_string_t action;
69af75078fSIntel 	cmdline_fixed_string_t message;
70af75078fSIntel };
71af75078fSIntel 
72af75078fSIntel static void cmd_send_parsed(void *parsed_result,
73af75078fSIntel 		__attribute__((unused)) struct cmdline *cl,
74af75078fSIntel 		__attribute__((unused)) void *data)
75af75078fSIntel {
76a974564bSIntel 	void *msg = NULL;
77af75078fSIntel 	struct cmd_send_result *res = parsed_result;
78af75078fSIntel 
79af75078fSIntel 	if (rte_mempool_get(message_pool, &msg) < 0)
80af75078fSIntel 		rte_panic("Failed to get message buffer\n");
81*0acd8ed3SXueming Li 	snprintf((char *)msg, STR_TOKEN_SIZE, "%s", res->message);
82af75078fSIntel 	if (rte_ring_enqueue(send_ring, msg) < 0) {
83af75078fSIntel 		printf("Failed to send message - message discarded\n");
84af75078fSIntel 		rte_mempool_put(message_pool, msg);
85af75078fSIntel 	}
86af75078fSIntel }
87af75078fSIntel 
88af75078fSIntel cmdline_parse_token_string_t cmd_send_action =
89af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_send_result, action, "send");
90af75078fSIntel cmdline_parse_token_string_t cmd_send_message =
91af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_send_result, message, NULL);
92af75078fSIntel 
93af75078fSIntel cmdline_parse_inst_t cmd_send = {
94af75078fSIntel 	.f = cmd_send_parsed,  /* function to call */
95af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
96af75078fSIntel 	.help_str = "send a string to another process",
97af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
98af75078fSIntel 			(void *)&cmd_send_action,
99af75078fSIntel 			(void *)&cmd_send_message,
100af75078fSIntel 			NULL,
101af75078fSIntel 	},
102af75078fSIntel };
103af75078fSIntel 
104af75078fSIntel /**********************************************************/
105af75078fSIntel 
106af75078fSIntel struct cmd_quit_result {
107af75078fSIntel 	cmdline_fixed_string_t quit;
108af75078fSIntel };
109af75078fSIntel 
110af75078fSIntel static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
111af75078fSIntel 			    struct cmdline *cl,
112af75078fSIntel 			    __attribute__((unused)) void *data)
113af75078fSIntel {
114af75078fSIntel 	quit = 1;
115af75078fSIntel 	cmdline_quit(cl);
116af75078fSIntel }
117af75078fSIntel 
118af75078fSIntel cmdline_parse_token_string_t cmd_quit_quit =
119af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
120af75078fSIntel 
121af75078fSIntel cmdline_parse_inst_t cmd_quit = {
122af75078fSIntel 	.f = cmd_quit_parsed,  /* function to call */
123af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
124af75078fSIntel 	.help_str = "close the application",
125af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
126af75078fSIntel 		(void *)&cmd_quit_quit,
127af75078fSIntel 		NULL,
128af75078fSIntel 	},
129af75078fSIntel };
130af75078fSIntel 
131af75078fSIntel /**********************************************************/
132af75078fSIntel 
133af75078fSIntel struct cmd_help_result {
134af75078fSIntel 	cmdline_fixed_string_t help;
135af75078fSIntel };
136af75078fSIntel 
137af75078fSIntel static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
138af75078fSIntel 			    struct cmdline *cl,
139af75078fSIntel 			    __attribute__((unused)) void *data)
140af75078fSIntel {
141af75078fSIntel 	cmdline_printf(cl, "Simple demo example of multi-process in RTE\n\n"
142af75078fSIntel 			"This is a readline-like interface that can be used to\n"
143af75078fSIntel 			"send commands to the simple app. Commands supported are:\n\n"
144af75078fSIntel 			"- send [string]\n" "- help\n" "- quit\n\n");
145af75078fSIntel }
146af75078fSIntel 
147af75078fSIntel cmdline_parse_token_string_t cmd_help_help =
148af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
149af75078fSIntel 
150af75078fSIntel cmdline_parse_inst_t cmd_help = {
151af75078fSIntel 	.f = cmd_help_parsed,  /* function to call */
152af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
153af75078fSIntel 	.help_str = "show help",
154af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
155af75078fSIntel 		(void *)&cmd_help_help,
156af75078fSIntel 		NULL,
157af75078fSIntel 	},
158af75078fSIntel };
159af75078fSIntel 
160af75078fSIntel /****** CONTEXT (list of instruction) */
161af75078fSIntel cmdline_parse_ctx_t simple_mp_ctx[] = {
162af75078fSIntel 		(cmdline_parse_inst_t *)&cmd_send,
163af75078fSIntel 		(cmdline_parse_inst_t *)&cmd_quit,
164af75078fSIntel 		(cmdline_parse_inst_t *)&cmd_help,
165af75078fSIntel 	NULL,
166af75078fSIntel };
167