xref: /dpdk/examples/cmdline/commands.c (revision 52e04a6323319ff1a7b4e1d7ed1df2b45d11a0a4)
10cdc9c96SLee Daly /* SPDX-License-Identifier: BSD-3-Clause
20cdc9c96SLee Daly  * Copyright(c) 2010-2014 Intel Corporation.
3af75078fSIntel  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4af75078fSIntel  * All rights reserved.
5af75078fSIntel  */
6af75078fSIntel 
7af75078fSIntel #include <stdio.h>
8af75078fSIntel #include <stdint.h>
9af75078fSIntel #include <string.h>
10af75078fSIntel #include <stdlib.h>
11af75078fSIntel #include <errno.h>
12af75078fSIntel 
13af75078fSIntel #include <cmdline_rdline.h>
14af75078fSIntel #include <cmdline_parse.h>
15af75078fSIntel #include <cmdline_parse_ipaddr.h>
16af75078fSIntel #include <cmdline_parse_num.h>
17af75078fSIntel #include <cmdline_parse_string.h>
18af75078fSIntel #include <cmdline.h>
19af75078fSIntel 
20af75078fSIntel #include <rte_string_fns.h>
21af75078fSIntel 
22af75078fSIntel #include "parse_obj_list.h"
23af75078fSIntel 
24af75078fSIntel struct object_list global_obj_list;
25af75078fSIntel 
26af75078fSIntel /* not defined under linux */
27af75078fSIntel #ifndef NIPQUAD
28af75078fSIntel #define NIPQUAD_FMT "%u.%u.%u.%u"
29af75078fSIntel #define NIPQUAD(addr)				\
30af75078fSIntel 	(unsigned)((unsigned char *)&addr)[0],	\
31af75078fSIntel 	(unsigned)((unsigned char *)&addr)[1],	\
32af75078fSIntel 	(unsigned)((unsigned char *)&addr)[2],	\
33af75078fSIntel 	(unsigned)((unsigned char *)&addr)[3]
34af75078fSIntel #endif
35af75078fSIntel 
36af75078fSIntel /**********************************************************/
37af75078fSIntel 
389a212dc0SConor Fogarty /* Show or delete tokens. 8< */
39af75078fSIntel struct cmd_obj_del_show_result {
40af75078fSIntel 	cmdline_fixed_string_t action;
41af75078fSIntel 	struct object *obj;
42af75078fSIntel };
43af75078fSIntel 
44af75078fSIntel static void cmd_obj_del_show_parsed(void *parsed_result,
45af75078fSIntel 				    struct cmdline *cl,
46f2fc83b4SThomas Monjalon 				    __rte_unused void *data)
47af75078fSIntel {
48af75078fSIntel 	struct cmd_obj_del_show_result *res = parsed_result;
49af75078fSIntel 	char ip_str[INET6_ADDRSTRLEN];
50af75078fSIntel 
51af75078fSIntel 	if (res->obj->ip.family == AF_INET)
526f41fe75SStephen Hemminger 		snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
53af75078fSIntel 			 NIPQUAD(res->obj->ip.addr.ipv4));
54af75078fSIntel 	else
55*52e04a63SRobin Jarry 		snprintf(ip_str, sizeof(ip_str), RTE_IPV6_ADDR_FMT,
56*52e04a63SRobin Jarry 			 RTE_IPV6_ADDR_SPLIT(&res->obj->ip.addr.ipv6));
57af75078fSIntel 
58af75078fSIntel 	if (strcmp(res->action, "del") == 0) {
59af75078fSIntel 		SLIST_REMOVE(&global_obj_list, res->obj, object, next);
60af75078fSIntel 		cmdline_printf(cl, "Object %s removed, ip=%s\n",
61af75078fSIntel 			       res->obj->name, ip_str);
62af75078fSIntel 		free(res->obj);
63af75078fSIntel 	}
64af75078fSIntel 	else if (strcmp(res->action, "show") == 0) {
65af75078fSIntel 		cmdline_printf(cl, "Object %s, ip=%s\n",
66af75078fSIntel 			       res->obj->name, ip_str);
67af75078fSIntel 	}
68af75078fSIntel }
69af75078fSIntel 
70af75078fSIntel cmdline_parse_token_string_t cmd_obj_action =
71af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
72af75078fSIntel 				 action, "show#del");
73af75078fSIntel parse_token_obj_list_t cmd_obj_obj =
74af75078fSIntel 	TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
75af75078fSIntel 				   &global_obj_list);
76af75078fSIntel 
77af75078fSIntel cmdline_parse_inst_t cmd_obj_del_show = {
78af75078fSIntel 	.f = cmd_obj_del_show_parsed,  /* function to call */
79af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
80af75078fSIntel 	.help_str = "Show/del an object",
81af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
82af75078fSIntel 		(void *)&cmd_obj_action,
83af75078fSIntel 		(void *)&cmd_obj_obj,
84af75078fSIntel 		NULL,
85af75078fSIntel 	},
86af75078fSIntel };
879a212dc0SConor Fogarty /* >8 End of show or delete tokens. */
88af75078fSIntel 
89af75078fSIntel /**********************************************************/
90af75078fSIntel 
91af75078fSIntel struct cmd_obj_add_result {
92af75078fSIntel 	cmdline_fixed_string_t action;
93af75078fSIntel 	cmdline_fixed_string_t name;
94af75078fSIntel 	cmdline_ipaddr_t ip;
95af75078fSIntel };
96af75078fSIntel 
97af75078fSIntel static void cmd_obj_add_parsed(void *parsed_result,
98af75078fSIntel 			       struct cmdline *cl,
99f2fc83b4SThomas Monjalon 			       __rte_unused void *data)
100af75078fSIntel {
101af75078fSIntel 	struct cmd_obj_add_result *res = parsed_result;
102af75078fSIntel 	struct object *o;
103af75078fSIntel 	char ip_str[INET6_ADDRSTRLEN];
104af75078fSIntel 
105af75078fSIntel 	SLIST_FOREACH(o, &global_obj_list, next) {
106af75078fSIntel 		if (!strcmp(res->name, o->name)) {
107af75078fSIntel 			cmdline_printf(cl, "Object %s already exist\n", res->name);
108af75078fSIntel 			return;
109af75078fSIntel 		}
110af75078fSIntel 		break;
111af75078fSIntel 	}
112af75078fSIntel 
113af75078fSIntel 	o = malloc(sizeof(*o));
114af75078fSIntel 	if (!o) {
115af75078fSIntel 		cmdline_printf(cl, "mem error\n");
116af75078fSIntel 		return;
117af75078fSIntel 	}
118f9acaf84SBruce Richardson 	strlcpy(o->name, res->name, sizeof(o->name));
119af75078fSIntel 	o->ip = res->ip;
120af75078fSIntel 	SLIST_INSERT_HEAD(&global_obj_list, o, next);
121af75078fSIntel 
122af75078fSIntel 	if (o->ip.family == AF_INET)
1236f41fe75SStephen Hemminger 		snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
124af75078fSIntel 			 NIPQUAD(o->ip.addr.ipv4));
125af75078fSIntel 	else
126*52e04a63SRobin Jarry 		snprintf(ip_str, sizeof(ip_str), RTE_IPV6_ADDR_FMT,
127*52e04a63SRobin Jarry 			 RTE_IPV6_ADDR_SPLIT(&o->ip.addr.ipv6));
128af75078fSIntel 
129af75078fSIntel 	cmdline_printf(cl, "Object %s added, ip=%s\n",
130af75078fSIntel 		       o->name, ip_str);
131af75078fSIntel }
132af75078fSIntel 
133af75078fSIntel cmdline_parse_token_string_t cmd_obj_action_add =
134af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
135af75078fSIntel cmdline_parse_token_string_t cmd_obj_name =
136af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
137af75078fSIntel cmdline_parse_token_ipaddr_t cmd_obj_ip =
138af75078fSIntel 	TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
139af75078fSIntel 
140af75078fSIntel cmdline_parse_inst_t cmd_obj_add = {
141af75078fSIntel 	.f = cmd_obj_add_parsed,  /* function to call */
142af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
143af75078fSIntel 	.help_str = "Add an object (name, val)",
144af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
145af75078fSIntel 		(void *)&cmd_obj_action_add,
146af75078fSIntel 		(void *)&cmd_obj_name,
147af75078fSIntel 		(void *)&cmd_obj_ip,
148af75078fSIntel 		NULL,
149af75078fSIntel 	},
150af75078fSIntel };
151af75078fSIntel 
152af75078fSIntel /**********************************************************/
153af75078fSIntel 
154af75078fSIntel struct cmd_help_result {
155af75078fSIntel 	cmdline_fixed_string_t help;
156af75078fSIntel };
157af75078fSIntel 
158f2fc83b4SThomas Monjalon static void cmd_help_parsed(__rte_unused void *parsed_result,
159af75078fSIntel 			    struct cmdline *cl,
160f2fc83b4SThomas Monjalon 			    __rte_unused void *data)
161af75078fSIntel {
162af75078fSIntel 	cmdline_printf(cl,
163af75078fSIntel 		       "Demo example of command line interface in RTE\n\n"
164af75078fSIntel 		       "This is a readline-like interface that can be used to\n"
165af75078fSIntel 		       "debug your RTE application. It supports some features\n"
166af75078fSIntel 		       "of GNU readline like completion, cut/paste, and some\n"
167af75078fSIntel 		       "other special bindings.\n\n"
168af75078fSIntel 		       "This demo shows how rte_cmdline library can be\n"
169af75078fSIntel 		       "extended to handle a list of objects. There are\n"
170af75078fSIntel 		       "3 commands:\n"
171af75078fSIntel 		       "- add obj_name IP\n"
172af75078fSIntel 		       "- del obj_name\n"
173af75078fSIntel 		       "- show obj_name\n\n");
174af75078fSIntel }
175af75078fSIntel 
176af75078fSIntel cmdline_parse_token_string_t cmd_help_help =
177af75078fSIntel 	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
178af75078fSIntel 
179af75078fSIntel cmdline_parse_inst_t cmd_help = {
180af75078fSIntel 	.f = cmd_help_parsed,  /* function to call */
181af75078fSIntel 	.data = NULL,      /* 2nd arg of func */
182af75078fSIntel 	.help_str = "show help",
183af75078fSIntel 	.tokens = {        /* token list, NULL terminated */
184af75078fSIntel 		(void *)&cmd_help_help,
185af75078fSIntel 		NULL,
186af75078fSIntel 	},
187af75078fSIntel };
188af75078fSIntel 
189af75078fSIntel 
190af75078fSIntel /**********************************************************/
191af75078fSIntel /**********************************************************/
192af75078fSIntel /****** CONTEXT (list of instruction) */
193af75078fSIntel 
1949a212dc0SConor Fogarty /* Cmdline context list of commands in NULL-terminated table. 8< */
195af75078fSIntel cmdline_parse_ctx_t main_ctx[] = {
196af75078fSIntel 	(cmdline_parse_inst_t *)&cmd_obj_del_show,
197af75078fSIntel 	(cmdline_parse_inst_t *)&cmd_obj_add,
198af75078fSIntel 	(cmdline_parse_inst_t *)&cmd_help,
199af75078fSIntel 	NULL,
200af75078fSIntel };
2019a212dc0SConor Fogarty /* >8 End of context list. */
202