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