4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
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:
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.
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.
67 #include <netinet/in.h>
71 #include <sys/socket.h>
73 #include <net/socket.h>
77 #include <cmdline_rdline.h>
78 #include <cmdline_parse.h>
79 #include <cmdline_parse_ipaddr.h>
80 #include <cmdline_parse_num.h>
81 #include <cmdline_parse_string.h>
84 #include <rte_string_fns.h>
86 #include "parse_obj_list.h"
88 struct object_list global_obj_list
;
90 /* not defined under linux */
92 #define NIPQUAD_FMT "%u.%u.%u.%u"
93 #define NIPQUAD(addr) \
94 (unsigned)((unsigned char *)&addr)[0], \
95 (unsigned)((unsigned char *)&addr)[1], \
96 (unsigned)((unsigned char *)&addr)[2], \
97 (unsigned)((unsigned char *)&addr)[3]
101 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
103 (unsigned)((addr).s6_addr[0]), \
104 (unsigned)((addr).s6_addr[1]), \
105 (unsigned)((addr).s6_addr[2]), \
106 (unsigned)((addr).s6_addr[3]), \
107 (unsigned)((addr).s6_addr[4]), \
108 (unsigned)((addr).s6_addr[5]), \
109 (unsigned)((addr).s6_addr[6]), \
110 (unsigned)((addr).s6_addr[7]), \
111 (unsigned)((addr).s6_addr[8]), \
112 (unsigned)((addr).s6_addr[9]), \
113 (unsigned)((addr).s6_addr[10]), \
114 (unsigned)((addr).s6_addr[11]), \
115 (unsigned)((addr).s6_addr[12]), \
116 (unsigned)((addr).s6_addr[13]), \
117 (unsigned)((addr).s6_addr[14]), \
118 (unsigned)((addr).s6_addr[15])
122 /**********************************************************/
124 struct cmd_obj_del_show_result
{
125 cmdline_fixed_string_t action
;
129 static void cmd_obj_del_show_parsed(void *parsed_result
,
131 __attribute__((unused
)) void *data
)
133 struct cmd_obj_del_show_result
*res
= parsed_result
;
134 char ip_str
[INET6_ADDRSTRLEN
];
136 if (res
->obj
->ip
.family
== AF_INET
)
137 snprintf(ip_str
, sizeof(ip_str
), NIPQUAD_FMT
,
138 NIPQUAD(res
->obj
->ip
.addr
.ipv4
));
140 snprintf(ip_str
, sizeof(ip_str
), NIP6_FMT
,
141 NIP6(res
->obj
->ip
.addr
.ipv6
));
143 if (strcmp(res
->action
, "del") == 0) {
144 SLIST_REMOVE(&global_obj_list
, res
->obj
, object
, next
);
145 cmdline_printf(cl
, "Object %s removed, ip=%s\n",
146 res
->obj
->name
, ip_str
);
149 else if (strcmp(res
->action
, "show") == 0) {
150 cmdline_printf(cl
, "Object %s, ip=%s\n",
151 res
->obj
->name
, ip_str
);
155 cmdline_parse_token_string_t cmd_obj_action
=
156 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result
,
158 parse_token_obj_list_t cmd_obj_obj
=
159 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result
, obj
,
162 cmdline_parse_inst_t cmd_obj_del_show
= {
163 .f
= cmd_obj_del_show_parsed
, /* function to call */
164 .data
= NULL
, /* 2nd arg of func */
165 .help_str
= "Show/del an object",
166 .tokens
= { /* token list, NULL terminated */
167 (void *)&cmd_obj_action
,
168 (void *)&cmd_obj_obj
,
173 /**********************************************************/
175 struct cmd_obj_add_result
{
176 cmdline_fixed_string_t action
;
177 cmdline_fixed_string_t name
;
181 static void cmd_obj_add_parsed(void *parsed_result
,
183 __attribute__((unused
)) void *data
)
185 struct cmd_obj_add_result
*res
= parsed_result
;
187 char ip_str
[INET6_ADDRSTRLEN
];
189 SLIST_FOREACH(o
, &global_obj_list
, next
) {
190 if (!strcmp(res
->name
, o
->name
)) {
191 cmdline_printf(cl
, "Object %s already exist\n", res
->name
);
197 o
= malloc(sizeof(*o
));
199 cmdline_printf(cl
, "mem error\n");
202 snprintf(o
->name
, sizeof(o
->name
), "%s", res
->name
);
204 SLIST_INSERT_HEAD(&global_obj_list
, o
, next
);
206 if (o
->ip
.family
== AF_INET
)
207 snprintf(ip_str
, sizeof(ip_str
), NIPQUAD_FMT
,
208 NIPQUAD(o
->ip
.addr
.ipv4
));
210 snprintf(ip_str
, sizeof(ip_str
), NIP6_FMT
,
211 NIP6(o
->ip
.addr
.ipv6
));
213 cmdline_printf(cl
, "Object %s added, ip=%s\n",
217 cmdline_parse_token_string_t cmd_obj_action_add
=
218 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result
, action
, "add");
219 cmdline_parse_token_string_t cmd_obj_name
=
220 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result
, name
, NULL
);
221 cmdline_parse_token_ipaddr_t cmd_obj_ip
=
222 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result
, ip
);
224 cmdline_parse_inst_t cmd_obj_add
= {
225 .f
= cmd_obj_add_parsed
, /* function to call */
226 .data
= NULL
, /* 2nd arg of func */
227 .help_str
= "Add an object (name, val)",
228 .tokens
= { /* token list, NULL terminated */
229 (void *)&cmd_obj_action_add
,
230 (void *)&cmd_obj_name
,
236 /**********************************************************/
238 struct cmd_help_result
{
239 cmdline_fixed_string_t help
;
242 static void cmd_help_parsed(__attribute__((unused
)) void *parsed_result
,
244 __attribute__((unused
)) void *data
)
247 "Demo example of command line interface in RTE\n\n"
248 "This is a readline-like interface that can be used to\n"
249 "debug your RTE application. It supports some features\n"
250 "of GNU readline like completion, cut/paste, and some\n"
251 "other special bindings.\n\n"
252 "This demo shows how rte_cmdline library can be\n"
253 "extended to handle a list of objects. There are\n"
255 "- add obj_name IP\n"
257 "- show obj_name\n\n");
260 cmdline_parse_token_string_t cmd_help_help
=
261 TOKEN_STRING_INITIALIZER(struct cmd_help_result
, help
, "help");
263 cmdline_parse_inst_t cmd_help
= {
264 .f
= cmd_help_parsed
, /* function to call */
265 .data
= NULL
, /* 2nd arg of func */
266 .help_str
= "show help",
267 .tokens
= { /* token list, NULL terminated */
268 (void *)&cmd_help_help
,
274 /**********************************************************/
275 /**********************************************************/
276 /****** CONTEXT (list of instruction) */
278 cmdline_parse_ctx_t main_ctx
[] = {
279 (cmdline_parse_inst_t
*)&cmd_obj_del_show
,
280 (cmdline_parse_inst_t
*)&cmd_obj_add
,
281 (cmdline_parse_inst_t
*)&cmd_help
,