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