xref: /dpdk/examples/cmdline/commands.c (revision e9d48c0072d36eb6423b45fba4ec49d0def6c36f)
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 
34 /*
35  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36  * All rights reserved.
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions are met:
39  *
40  *     * Redistributions of source code must retain the above copyright
41  *       notice, this list of conditions and the following disclaimer.
42  *     * Redistributions in binary form must reproduce the above copyright
43  *       notice, this list of conditions and the following disclaimer in the
44  *       documentation and/or other materials provided with the distribution.
45  *     * Neither the name of the University of California, Berkeley nor the
46  *       names of its contributors may be used to endorse or promote products
47  *       derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <stdio.h>
62 #include <stdint.h>
63 #include <string.h>
64 #include <stdlib.h>
65 #include <stdarg.h>
66 #include <errno.h>
67 #include <netinet/in.h>
68 #include <termios.h>
69 #ifndef __linux__
70 #include <net/socket.h>
71 #endif
72 
73 #include <cmdline_rdline.h>
74 #include <cmdline_parse.h>
75 #include <cmdline_parse_ipaddr.h>
76 #include <cmdline_parse_num.h>
77 #include <cmdline_parse_string.h>
78 #include <cmdline.h>
79 
80 #include <rte_string_fns.h>
81 
82 #include "parse_obj_list.h"
83 
84 struct object_list global_obj_list;
85 
86 /* not defined under linux */
87 #ifndef NIPQUAD
88 #define NIPQUAD_FMT "%u.%u.%u.%u"
89 #define NIPQUAD(addr)				\
90 	(unsigned)((unsigned char *)&addr)[0],	\
91 	(unsigned)((unsigned char *)&addr)[1],	\
92 	(unsigned)((unsigned char *)&addr)[2],	\
93 	(unsigned)((unsigned char *)&addr)[3]
94 #endif
95 
96 #ifndef NIP6
97 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
98 #define NIP6(addr)					\
99 	(unsigned)((addr).s6_addr[0]),			\
100 	(unsigned)((addr).s6_addr[1]),			\
101 	(unsigned)((addr).s6_addr[2]),			\
102 	(unsigned)((addr).s6_addr[3]),			\
103 	(unsigned)((addr).s6_addr[4]),			\
104 	(unsigned)((addr).s6_addr[5]),			\
105 	(unsigned)((addr).s6_addr[6]),			\
106 	(unsigned)((addr).s6_addr[7]),			\
107 	(unsigned)((addr).s6_addr[8]),			\
108 	(unsigned)((addr).s6_addr[9]),			\
109 	(unsigned)((addr).s6_addr[10]),			\
110 	(unsigned)((addr).s6_addr[11]),			\
111 	(unsigned)((addr).s6_addr[12]),			\
112 	(unsigned)((addr).s6_addr[13]),			\
113 	(unsigned)((addr).s6_addr[14]),			\
114 	(unsigned)((addr).s6_addr[15])
115 #endif
116 
117 
118 /**********************************************************/
119 
120 struct cmd_obj_del_show_result {
121 	cmdline_fixed_string_t action;
122 	struct object *obj;
123 };
124 
125 static void cmd_obj_del_show_parsed(void *parsed_result,
126 				    struct cmdline *cl,
127 				    __attribute__((unused)) void *data)
128 {
129 	struct cmd_obj_del_show_result *res = parsed_result;
130 	char ip_str[INET6_ADDRSTRLEN];
131 
132 	if (res->obj->ip.family == AF_INET)
133 		rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
134 			 NIPQUAD(res->obj->ip.addr.ipv4));
135 	else
136 		rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
137 			 NIP6(res->obj->ip.addr.ipv6));
138 
139 	if (strcmp(res->action, "del") == 0) {
140 		SLIST_REMOVE(&global_obj_list, res->obj, object, next);
141 		cmdline_printf(cl, "Object %s removed, ip=%s\n",
142 			       res->obj->name, ip_str);
143 		free(res->obj);
144 	}
145 	else if (strcmp(res->action, "show") == 0) {
146 		cmdline_printf(cl, "Object %s, ip=%s\n",
147 			       res->obj->name, ip_str);
148 	}
149 }
150 
151 cmdline_parse_token_string_t cmd_obj_action =
152 	TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
153 				 action, "show#del");
154 parse_token_obj_list_t cmd_obj_obj =
155 	TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
156 				   &global_obj_list);
157 
158 cmdline_parse_inst_t cmd_obj_del_show = {
159 	.f = cmd_obj_del_show_parsed,  /* function to call */
160 	.data = NULL,      /* 2nd arg of func */
161 	.help_str = "Show/del an object",
162 	.tokens = {        /* token list, NULL terminated */
163 		(void *)&cmd_obj_action,
164 		(void *)&cmd_obj_obj,
165 		NULL,
166 	},
167 };
168 
169 /**********************************************************/
170 
171 struct cmd_obj_add_result {
172 	cmdline_fixed_string_t action;
173 	cmdline_fixed_string_t name;
174 	cmdline_ipaddr_t ip;
175 };
176 
177 static void cmd_obj_add_parsed(void *parsed_result,
178 			       struct cmdline *cl,
179 			       __attribute__((unused)) void *data)
180 {
181 	struct cmd_obj_add_result *res = parsed_result;
182 	struct object *o;
183 	char ip_str[INET6_ADDRSTRLEN];
184 
185 	SLIST_FOREACH(o, &global_obj_list, next) {
186 		if (!strcmp(res->name, o->name)) {
187 			cmdline_printf(cl, "Object %s already exist\n", res->name);
188 			return;
189 		}
190 		break;
191 	}
192 
193 	o = malloc(sizeof(*o));
194 	if (!o) {
195 		cmdline_printf(cl, "mem error\n");
196 		return;
197 	}
198 	rte_snprintf(o->name, sizeof(o->name), "%s", res->name);
199 	o->ip = res->ip;
200 	SLIST_INSERT_HEAD(&global_obj_list, o, next);
201 
202 	if (o->ip.family == AF_INET)
203 		rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
204 			 NIPQUAD(o->ip.addr.ipv4));
205 	else
206 		rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
207 			 NIP6(o->ip.addr.ipv6));
208 
209 	cmdline_printf(cl, "Object %s added, ip=%s\n",
210 		       o->name, ip_str);
211 }
212 
213 cmdline_parse_token_string_t cmd_obj_action_add =
214 	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
215 cmdline_parse_token_string_t cmd_obj_name =
216 	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
217 cmdline_parse_token_ipaddr_t cmd_obj_ip =
218 	TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
219 
220 cmdline_parse_inst_t cmd_obj_add = {
221 	.f = cmd_obj_add_parsed,  /* function to call */
222 	.data = NULL,      /* 2nd arg of func */
223 	.help_str = "Add an object (name, val)",
224 	.tokens = {        /* token list, NULL terminated */
225 		(void *)&cmd_obj_action_add,
226 		(void *)&cmd_obj_name,
227 		(void *)&cmd_obj_ip,
228 		NULL,
229 	},
230 };
231 
232 /**********************************************************/
233 
234 struct cmd_help_result {
235 	cmdline_fixed_string_t help;
236 };
237 
238 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
239 			    struct cmdline *cl,
240 			    __attribute__((unused)) void *data)
241 {
242 	cmdline_printf(cl,
243 		       "Demo example of command line interface in RTE\n\n"
244 		       "This is a readline-like interface that can be used to\n"
245 		       "debug your RTE application. It supports some features\n"
246 		       "of GNU readline like completion, cut/paste, and some\n"
247 		       "other special bindings.\n\n"
248 		       "This demo shows how rte_cmdline library can be\n"
249 		       "extended to handle a list of objects. There are\n"
250 		       "3 commands:\n"
251 		       "- add obj_name IP\n"
252 		       "- del obj_name\n"
253 		       "- show obj_name\n\n");
254 }
255 
256 cmdline_parse_token_string_t cmd_help_help =
257 	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
258 
259 cmdline_parse_inst_t cmd_help = {
260 	.f = cmd_help_parsed,  /* function to call */
261 	.data = NULL,      /* 2nd arg of func */
262 	.help_str = "show help",
263 	.tokens = {        /* token list, NULL terminated */
264 		(void *)&cmd_help_help,
265 		NULL,
266 	},
267 };
268 
269 
270 /**********************************************************/
271 /**********************************************************/
272 /****** CONTEXT (list of instruction) */
273 
274 cmdline_parse_ctx_t main_ctx[] = {
275 	(cmdline_parse_inst_t *)&cmd_obj_del_show,
276 	(cmdline_parse_inst_t *)&cmd_obj_add,
277 	(cmdline_parse_inst_t *)&cmd_help,
278 	NULL,
279 };
280 
281