4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
37 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions are met:
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * * Neither the name of the University of California, Berkeley nor the
48 * names of its contributors may be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include <netinet/in.h>
72 #include <net/socket.h>
75 #include <cmdline_rdline.h>
76 #include <cmdline_parse.h>
77 #include <cmdline_parse_ipaddr.h>
78 #include <cmdline_parse_num.h>
79 #include <cmdline_parse_string.h>
82 #include <rte_string_fns.h>
84 #include "parse_obj_list.h"
86 struct object_list global_obj_list;
88 /* not defined under linux */
90 #define NIPQUAD_FMT "%u.%u.%u.%u"
91 #define NIPQUAD(addr) \
92 (unsigned)((unsigned char *)&addr)[0], \
93 (unsigned)((unsigned char *)&addr)[1], \
94 (unsigned)((unsigned char *)&addr)[2], \
95 (unsigned)((unsigned char *)&addr)[3]
99 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
101 (unsigned)((addr).s6_addr[0]), \
102 (unsigned)((addr).s6_addr[1]), \
103 (unsigned)((addr).s6_addr[2]), \
104 (unsigned)((addr).s6_addr[3]), \
105 (unsigned)((addr).s6_addr[4]), \
106 (unsigned)((addr).s6_addr[5]), \
107 (unsigned)((addr).s6_addr[6]), \
108 (unsigned)((addr).s6_addr[7]), \
109 (unsigned)((addr).s6_addr[8]), \
110 (unsigned)((addr).s6_addr[9]), \
111 (unsigned)((addr).s6_addr[10]), \
112 (unsigned)((addr).s6_addr[11]), \
113 (unsigned)((addr).s6_addr[12]), \
114 (unsigned)((addr).s6_addr[13]), \
115 (unsigned)((addr).s6_addr[14]), \
116 (unsigned)((addr).s6_addr[15])
120 /**********************************************************/
122 struct cmd_obj_del_show_result {
123 cmdline_fixed_string_t action;
127 static void cmd_obj_del_show_parsed(void *parsed_result,
129 __attribute__((unused)) void *data)
131 struct cmd_obj_del_show_result *res = parsed_result;
132 char ip_str[INET6_ADDRSTRLEN];
134 if (res->obj->ip.family == AF_INET)
135 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
136 NIPQUAD(res->obj->ip.addr.ipv4));
138 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
139 NIP6(res->obj->ip.addr.ipv6));
141 if (strcmp(res->action, "del") == 0) {
142 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
143 cmdline_printf(cl, "Object %s removed, ip=%s\n",
144 res->obj->name, ip_str);
147 else if (strcmp(res->action, "show") == 0) {
148 cmdline_printf(cl, "Object %s, ip=%s\n",
149 res->obj->name, ip_str);
153 cmdline_parse_token_string_t cmd_obj_action =
154 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
156 parse_token_obj_list_t cmd_obj_obj =
157 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
160 cmdline_parse_inst_t cmd_obj_del_show = {
161 .f = cmd_obj_del_show_parsed, /* function to call */
162 .data = NULL, /* 2nd arg of func */
163 .help_str = "Show/del an object",
164 .tokens = { /* token list, NULL terminated */
165 (void *)&cmd_obj_action,
166 (void *)&cmd_obj_obj,
171 /**********************************************************/
173 struct cmd_obj_add_result {
174 cmdline_fixed_string_t action;
175 cmdline_fixed_string_t name;
179 static void cmd_obj_add_parsed(void *parsed_result,
181 __attribute__((unused)) void *data)
183 struct cmd_obj_add_result *res = parsed_result;
185 char ip_str[INET6_ADDRSTRLEN];
187 SLIST_FOREACH(o, &global_obj_list, next) {
188 if (!strcmp(res->name, o->name)) {
189 cmdline_printf(cl, "Object %s already exist\n", res->name);
195 o = malloc(sizeof(*o));
197 cmdline_printf(cl, "mem error\n");
200 rte_snprintf(o->name, sizeof(o->name), "%s", res->name);
202 SLIST_INSERT_HEAD(&global_obj_list, o, next);
204 if (o->ip.family == AF_INET)
205 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
206 NIPQUAD(o->ip.addr.ipv4));
208 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
209 NIP6(o->ip.addr.ipv6));
211 cmdline_printf(cl, "Object %s added, ip=%s\n",
215 cmdline_parse_token_string_t cmd_obj_action_add =
216 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
217 cmdline_parse_token_string_t cmd_obj_name =
218 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
219 cmdline_parse_token_ipaddr_t cmd_obj_ip =
220 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
222 cmdline_parse_inst_t cmd_obj_add = {
223 .f = cmd_obj_add_parsed, /* function to call */
224 .data = NULL, /* 2nd arg of func */
225 .help_str = "Add an object (name, val)",
226 .tokens = { /* token list, NULL terminated */
227 (void *)&cmd_obj_action_add,
228 (void *)&cmd_obj_name,
234 /**********************************************************/
236 struct cmd_help_result {
237 cmdline_fixed_string_t help;
240 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
242 __attribute__((unused)) void *data)
245 "Demo example of command line interface in RTE\n\n"
246 "This is a readline-like interface that can be used to\n"
247 "debug your RTE application. It supports some features\n"
248 "of GNU readline like completion, cut/paste, and some\n"
249 "other special bindings.\n\n"
250 "This demo shows how rte_cmdline library can be\n"
251 "extended to handle a list of objects. There are\n"
253 "- add obj_name IP\n"
255 "- show obj_name\n\n");
258 cmdline_parse_token_string_t cmd_help_help =
259 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
261 cmdline_parse_inst_t cmd_help = {
262 .f = cmd_help_parsed, /* function to call */
263 .data = NULL, /* 2nd arg of func */
264 .help_str = "show help",
265 .tokens = { /* token list, NULL terminated */
266 (void *)&cmd_help_help,
272 /**********************************************************/
273 /**********************************************************/
274 /****** CONTEXT (list of instruction) */
276 cmdline_parse_ctx_t main_ctx[] = {
277 (cmdline_parse_inst_t *)&cmd_obj_del_show,
278 (cmdline_parse_inst_t *)&cmd_obj_add,
279 (cmdline_parse_inst_t *)&cmd_help,