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>
70 #include <net/socket.h>
73 #include <cmdline_rdline.h>
74 #include <cmdline_parse.h>
75 #include <cmdline_parse_ipaddr.h>
76 #include <cmdline_parse_num.h>
77 #include <cmdline_parse_string.h>
80 #include <rte_string_fns.h>
82 #include "parse_obj_list.h"
84 struct object_list global_obj_list;
86 /* not defined under linux */
88 #define NIPQUAD_FMT "%u.%u.%u.%u"
89 #define NIPQUAD(addr) \
90 (unsigned)((unsigned char *)&addr)[0], \
91 (unsigned)((unsigned char *)&addr)[1], \
92 (unsigned)((unsigned char *)&addr)[2], \
93 (unsigned)((unsigned char *)&addr)[3]
97 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
99 (unsigned)((addr).s6_addr[0]), \
100 (unsigned)((addr).s6_addr[1]), \
101 (unsigned)((addr).s6_addr[2]), \
102 (unsigned)((addr).s6_addr[3]), \
103 (unsigned)((addr).s6_addr[4]), \
104 (unsigned)((addr).s6_addr[5]), \
105 (unsigned)((addr).s6_addr[6]), \
106 (unsigned)((addr).s6_addr[7]), \
107 (unsigned)((addr).s6_addr[8]), \
108 (unsigned)((addr).s6_addr[9]), \
109 (unsigned)((addr).s6_addr[10]), \
110 (unsigned)((addr).s6_addr[11]), \
111 (unsigned)((addr).s6_addr[12]), \
112 (unsigned)((addr).s6_addr[13]), \
113 (unsigned)((addr).s6_addr[14]), \
114 (unsigned)((addr).s6_addr[15])
118 /**********************************************************/
120 struct cmd_obj_del_show_result {
121 cmdline_fixed_string_t action;
125 static void cmd_obj_del_show_parsed(void *parsed_result,
127 __attribute__((unused)) void *data)
129 struct cmd_obj_del_show_result *res = parsed_result;
130 char ip_str[INET6_ADDRSTRLEN];
132 if (res->obj->ip.family == AF_INET)
133 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
134 NIPQUAD(res->obj->ip.addr.ipv4));
136 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
137 NIP6(res->obj->ip.addr.ipv6));
139 if (strcmp(res->action, "del") == 0) {
140 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
141 cmdline_printf(cl, "Object %s removed, ip=%s\n",
142 res->obj->name, ip_str);
145 else if (strcmp(res->action, "show") == 0) {
146 cmdline_printf(cl, "Object %s, ip=%s\n",
147 res->obj->name, ip_str);
151 cmdline_parse_token_string_t cmd_obj_action =
152 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
154 parse_token_obj_list_t cmd_obj_obj =
155 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
158 cmdline_parse_inst_t cmd_obj_del_show = {
159 .f = cmd_obj_del_show_parsed, /* function to call */
160 .data = NULL, /* 2nd arg of func */
161 .help_str = "Show/del an object",
162 .tokens = { /* token list, NULL terminated */
163 (void *)&cmd_obj_action,
164 (void *)&cmd_obj_obj,
169 /**********************************************************/
171 struct cmd_obj_add_result {
172 cmdline_fixed_string_t action;
173 cmdline_fixed_string_t name;
177 static void cmd_obj_add_parsed(void *parsed_result,
179 __attribute__((unused)) void *data)
181 struct cmd_obj_add_result *res = parsed_result;
183 char ip_str[INET6_ADDRSTRLEN];
185 SLIST_FOREACH(o, &global_obj_list, next) {
186 if (!strcmp(res->name, o->name)) {
187 cmdline_printf(cl, "Object %s already exist\n", res->name);
193 o = malloc(sizeof(*o));
195 cmdline_printf(cl, "mem error\n");
198 rte_snprintf(o->name, sizeof(o->name), "%s", res->name);
200 SLIST_INSERT_HEAD(&global_obj_list, o, next);
202 if (o->ip.family == AF_INET)
203 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
204 NIPQUAD(o->ip.addr.ipv4));
206 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
207 NIP6(o->ip.addr.ipv6));
209 cmdline_printf(cl, "Object %s added, ip=%s\n",
213 cmdline_parse_token_string_t cmd_obj_action_add =
214 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
215 cmdline_parse_token_string_t cmd_obj_name =
216 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
217 cmdline_parse_token_ipaddr_t cmd_obj_ip =
218 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
220 cmdline_parse_inst_t cmd_obj_add = {
221 .f = cmd_obj_add_parsed, /* function to call */
222 .data = NULL, /* 2nd arg of func */
223 .help_str = "Add an object (name, val)",
224 .tokens = { /* token list, NULL terminated */
225 (void *)&cmd_obj_action_add,
226 (void *)&cmd_obj_name,
232 /**********************************************************/
234 struct cmd_help_result {
235 cmdline_fixed_string_t help;
238 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
240 __attribute__((unused)) void *data)
243 "Demo example of command line interface in RTE\n\n"
244 "This is a readline-like interface that can be used to\n"
245 "debug your RTE application. It supports some features\n"
246 "of GNU readline like completion, cut/paste, and some\n"
247 "other special bindings.\n\n"
248 "This demo shows how rte_cmdline library can be\n"
249 "extended to handle a list of objects. There are\n"
251 "- add obj_name IP\n"
253 "- show obj_name\n\n");
256 cmdline_parse_token_string_t cmd_help_help =
257 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
259 cmdline_parse_inst_t cmd_help = {
260 .f = cmd_help_parsed, /* function to call */
261 .data = NULL, /* 2nd arg of func */
262 .help_str = "show help",
263 .tokens = { /* token list, NULL terminated */
264 (void *)&cmd_help_help,
270 /**********************************************************/
271 /**********************************************************/
272 /****** CONTEXT (list of instruction) */
274 cmdline_parse_ctx_t main_ctx[] = {
275 (cmdline_parse_inst_t *)&cmd_obj_del_show,
276 (cmdline_parse_inst_t *)&cmd_obj_add,
277 (cmdline_parse_inst_t *)&cmd_help,