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