13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3af75078fSIntel */
4af75078fSIntel #include <stdint.h>
5af75078fSIntel #include <string.h>
6af75078fSIntel #include <stdlib.h>
7af75078fSIntel #include <stdarg.h>
8af75078fSIntel #include <inttypes.h>
9af75078fSIntel #include <stdio.h>
10af75078fSIntel #include <termios.h>
11af75078fSIntel #include <errno.h>
12af75078fSIntel #include <sys/queue.h>
13af75078fSIntel
14af75078fSIntel #include <rte_common.h>
15af75078fSIntel #include <rte_memory.h>
16af75078fSIntel #include <rte_eal.h>
17af75078fSIntel #include <rte_branch_prediction.h>
18af75078fSIntel #include <rte_launch.h>
19af75078fSIntel #include <rte_log.h>
20af75078fSIntel #include <rte_per_lcore.h>
21af75078fSIntel #include <rte_lcore.h>
22af75078fSIntel #include <rte_ring.h>
23af75078fSIntel #include <rte_debug.h>
24af75078fSIntel #include <rte_mempool.h>
25af75078fSIntel #include <rte_string_fns.h>
26af75078fSIntel
27af75078fSIntel #include <cmdline_rdline.h>
28af75078fSIntel #include <cmdline_parse.h>
29af75078fSIntel #include <cmdline_parse_string.h>
30af75078fSIntel #include <cmdline_socket.h>
31af75078fSIntel #include <cmdline.h>
32af75078fSIntel #include "mp_commands.h"
33af75078fSIntel
34af75078fSIntel /**********************************************************/
35af75078fSIntel
36af75078fSIntel struct cmd_send_result {
37af75078fSIntel cmdline_fixed_string_t action;
38af75078fSIntel cmdline_fixed_string_t message;
39af75078fSIntel };
40af75078fSIntel
cmd_send_parsed(void * parsed_result,__rte_unused struct cmdline * cl,__rte_unused void * data)41af75078fSIntel static void cmd_send_parsed(void *parsed_result,
42*f2fc83b4SThomas Monjalon __rte_unused struct cmdline *cl,
43*f2fc83b4SThomas Monjalon __rte_unused void *data)
44af75078fSIntel {
45a974564bSIntel void *msg = NULL;
46af75078fSIntel struct cmd_send_result *res = parsed_result;
47af75078fSIntel
48af75078fSIntel if (rte_mempool_get(message_pool, &msg) < 0)
49af75078fSIntel rte_panic("Failed to get message buffer\n");
50f9acaf84SBruce Richardson strlcpy((char *)msg, res->message, STR_TOKEN_SIZE);
51af75078fSIntel if (rte_ring_enqueue(send_ring, msg) < 0) {
52af75078fSIntel printf("Failed to send message - message discarded\n");
53af75078fSIntel rte_mempool_put(message_pool, msg);
54af75078fSIntel }
55af75078fSIntel }
56af75078fSIntel
57af75078fSIntel cmdline_parse_token_string_t cmd_send_action =
58af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_send_result, action, "send");
59af75078fSIntel cmdline_parse_token_string_t cmd_send_message =
60af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_send_result, message, NULL);
61af75078fSIntel
62af75078fSIntel cmdline_parse_inst_t cmd_send = {
63af75078fSIntel .f = cmd_send_parsed, /* function to call */
64af75078fSIntel .data = NULL, /* 2nd arg of func */
65af75078fSIntel .help_str = "send a string to another process",
66af75078fSIntel .tokens = { /* token list, NULL terminated */
67af75078fSIntel (void *)&cmd_send_action,
68af75078fSIntel (void *)&cmd_send_message,
69af75078fSIntel NULL,
70af75078fSIntel },
71af75078fSIntel };
72af75078fSIntel
73af75078fSIntel /**********************************************************/
74af75078fSIntel
75af75078fSIntel struct cmd_quit_result {
76af75078fSIntel cmdline_fixed_string_t quit;
77af75078fSIntel };
78af75078fSIntel
cmd_quit_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)79*f2fc83b4SThomas Monjalon static void cmd_quit_parsed(__rte_unused void *parsed_result,
80af75078fSIntel struct cmdline *cl,
81*f2fc83b4SThomas Monjalon __rte_unused void *data)
82af75078fSIntel {
83af75078fSIntel quit = 1;
84af75078fSIntel cmdline_quit(cl);
85af75078fSIntel }
86af75078fSIntel
87af75078fSIntel cmdline_parse_token_string_t cmd_quit_quit =
88af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
89af75078fSIntel
90af75078fSIntel cmdline_parse_inst_t cmd_quit = {
91af75078fSIntel .f = cmd_quit_parsed, /* function to call */
92af75078fSIntel .data = NULL, /* 2nd arg of func */
93af75078fSIntel .help_str = "close the application",
94af75078fSIntel .tokens = { /* token list, NULL terminated */
95af75078fSIntel (void *)&cmd_quit_quit,
96af75078fSIntel NULL,
97af75078fSIntel },
98af75078fSIntel };
99af75078fSIntel
100af75078fSIntel /**********************************************************/
101af75078fSIntel
102af75078fSIntel struct cmd_help_result {
103af75078fSIntel cmdline_fixed_string_t help;
104af75078fSIntel };
105af75078fSIntel
cmd_help_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)106*f2fc83b4SThomas Monjalon static void cmd_help_parsed(__rte_unused void *parsed_result,
107af75078fSIntel struct cmdline *cl,
108*f2fc83b4SThomas Monjalon __rte_unused void *data)
109af75078fSIntel {
110af75078fSIntel cmdline_printf(cl, "Simple demo example of multi-process in RTE\n\n"
111af75078fSIntel "This is a readline-like interface that can be used to\n"
112af75078fSIntel "send commands to the simple app. Commands supported are:\n\n"
113af75078fSIntel "- send [string]\n" "- help\n" "- quit\n\n");
114af75078fSIntel }
115af75078fSIntel
116af75078fSIntel cmdline_parse_token_string_t cmd_help_help =
117af75078fSIntel TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
118af75078fSIntel
119af75078fSIntel cmdline_parse_inst_t cmd_help = {
120af75078fSIntel .f = cmd_help_parsed, /* function to call */
121af75078fSIntel .data = NULL, /* 2nd arg of func */
122af75078fSIntel .help_str = "show help",
123af75078fSIntel .tokens = { /* token list, NULL terminated */
124af75078fSIntel (void *)&cmd_help_help,
125af75078fSIntel NULL,
126af75078fSIntel },
127af75078fSIntel };
128af75078fSIntel
129af75078fSIntel /****** CONTEXT (list of instruction) */
130af75078fSIntel cmdline_parse_ctx_t simple_mp_ctx[] = {
131af75078fSIntel (cmdline_parse_inst_t *)&cmd_send,
132af75078fSIntel (cmdline_parse_inst_t *)&cmd_quit,
133af75078fSIntel (cmdline_parse_inst_t *)&cmd_help,
134af75078fSIntel NULL,
135af75078fSIntel };
136