4 * Copyright(c) 2010-2013 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.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 #include <netinet/in.h>
71 #include <net/socket.h>
74 #include <cmdline_rdline.h>
75 #include <cmdline_parse.h>
76 #include <cmdline_parse_ipaddr.h>
77 #include <cmdline_parse_num.h>
78 #include <cmdline_parse_string.h>
81 #include <rte_string_fns.h>
83 #include "parse_obj_list.h"
85 struct object_list global_obj_list;
87 /* not defined under linux */
89 #define NIPQUAD_FMT "%u.%u.%u.%u"
90 #define NIPQUAD(addr) \
91 (unsigned)((unsigned char *)&addr)[0], \
92 (unsigned)((unsigned char *)&addr)[1], \
93 (unsigned)((unsigned char *)&addr)[2], \
94 (unsigned)((unsigned char *)&addr)[3]
98 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
100 (unsigned)((addr).s6_addr[0]), \
101 (unsigned)((addr).s6_addr[1]), \
102 (unsigned)((addr).s6_addr[2]), \
103 (unsigned)((addr).s6_addr[3]), \
104 (unsigned)((addr).s6_addr[4]), \
105 (unsigned)((addr).s6_addr[5]), \
106 (unsigned)((addr).s6_addr[6]), \
107 (unsigned)((addr).s6_addr[7]), \
108 (unsigned)((addr).s6_addr[8]), \
109 (unsigned)((addr).s6_addr[9]), \
110 (unsigned)((addr).s6_addr[10]), \
111 (unsigned)((addr).s6_addr[11]), \
112 (unsigned)((addr).s6_addr[12]), \
113 (unsigned)((addr).s6_addr[13]), \
114 (unsigned)((addr).s6_addr[14]), \
115 (unsigned)((addr).s6_addr[15])
119 /**********************************************************/
121 struct cmd_obj_del_show_result {
122 cmdline_fixed_string_t action;
126 static void cmd_obj_del_show_parsed(void *parsed_result,
128 __attribute__((unused)) void *data)
130 struct cmd_obj_del_show_result *res = parsed_result;
131 char ip_str[INET6_ADDRSTRLEN];
133 if (res->obj->ip.family == AF_INET)
134 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
135 NIPQUAD(res->obj->ip.addr.ipv4));
137 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
138 NIP6(res->obj->ip.addr.ipv6));
140 if (strcmp(res->action, "del") == 0) {
141 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
142 cmdline_printf(cl, "Object %s removed, ip=%s\n",
143 res->obj->name, ip_str);
146 else if (strcmp(res->action, "show") == 0) {
147 cmdline_printf(cl, "Object %s, ip=%s\n",
148 res->obj->name, ip_str);
152 cmdline_parse_token_string_t cmd_obj_action =
153 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
155 parse_token_obj_list_t cmd_obj_obj =
156 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
159 cmdline_parse_inst_t cmd_obj_del_show = {
160 .f = cmd_obj_del_show_parsed, /* function to call */
161 .data = NULL, /* 2nd arg of func */
162 .help_str = "Show/del an object",
163 .tokens = { /* token list, NULL terminated */
164 (void *)&cmd_obj_action,
165 (void *)&cmd_obj_obj,
170 /**********************************************************/
172 struct cmd_obj_add_result {
173 cmdline_fixed_string_t action;
174 cmdline_fixed_string_t name;
178 static void cmd_obj_add_parsed(void *parsed_result,
180 __attribute__((unused)) void *data)
182 struct cmd_obj_add_result *res = parsed_result;
184 char ip_str[INET6_ADDRSTRLEN];
186 SLIST_FOREACH(o, &global_obj_list, next) {
187 if (!strcmp(res->name, o->name)) {
188 cmdline_printf(cl, "Object %s already exist\n", res->name);
194 o = malloc(sizeof(*o));
196 cmdline_printf(cl, "mem error\n");
199 rte_snprintf(o->name, sizeof(o->name), "%s", res->name);
201 SLIST_INSERT_HEAD(&global_obj_list, o, next);
203 if (o->ip.family == AF_INET)
204 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
205 NIPQUAD(o->ip.addr.ipv4));
207 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
208 NIP6(o->ip.addr.ipv6));
210 cmdline_printf(cl, "Object %s added, ip=%s\n",
214 cmdline_parse_token_string_t cmd_obj_action_add =
215 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
216 cmdline_parse_token_string_t cmd_obj_name =
217 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
218 cmdline_parse_token_ipaddr_t cmd_obj_ip =
219 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
221 cmdline_parse_inst_t cmd_obj_add = {
222 .f = cmd_obj_add_parsed, /* function to call */
223 .data = NULL, /* 2nd arg of func */
224 .help_str = "Add an object (name, val)",
225 .tokens = { /* token list, NULL terminated */
226 (void *)&cmd_obj_action_add,
227 (void *)&cmd_obj_name,
233 /**********************************************************/
235 struct cmd_help_result {
236 cmdline_fixed_string_t help;
239 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
241 __attribute__((unused)) void *data)
244 "Demo example of command line interface in RTE\n\n"
245 "This is a readline-like interface that can be used to\n"
246 "debug your RTE application. It supports some features\n"
247 "of GNU readline like completion, cut/paste, and some\n"
248 "other special bindings.\n\n"
249 "This demo shows how rte_cmdline library can be\n"
250 "extended to handle a list of objects. There are\n"
252 "- add obj_name IP\n"
254 "- show obj_name\n\n");
257 cmdline_parse_token_string_t cmd_help_help =
258 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
260 cmdline_parse_inst_t cmd_help = {
261 .f = cmd_help_parsed, /* function to call */
262 .data = NULL, /* 2nd arg of func */
263 .help_str = "show help",
264 .tokens = { /* token list, NULL terminated */
265 (void *)&cmd_help_help,
271 /**********************************************************/
272 /**********************************************************/
273 /****** CONTEXT (list of instruction) */
275 cmdline_parse_ctx_t main_ctx[] = {
276 (cmdline_parse_inst_t *)&cmd_obj_del_show,
277 (cmdline_parse_inst_t *)&cmd_obj_add,
278 (cmdline_parse_inst_t *)&cmd_help,