1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
13 #include <cmdline_rdline.h>
14 #include <cmdline_parse.h>
15 #include <cmdline_parse_ipaddr.h>
16 #include <cmdline_parse_num.h>
17 #include <cmdline_parse_string.h>
20 #include <rte_string_fns.h>
22 #include "parse_obj_list.h"
24 struct object_list global_obj_list;
26 /* not defined under linux */
28 #define NIPQUAD_FMT "%u.%u.%u.%u"
29 #define NIPQUAD(addr) \
30 (unsigned)((unsigned char *)&addr)[0], \
31 (unsigned)((unsigned char *)&addr)[1], \
32 (unsigned)((unsigned char *)&addr)[2], \
33 (unsigned)((unsigned char *)&addr)[3]
37 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
39 (unsigned)((addr).s6_addr[0]), \
40 (unsigned)((addr).s6_addr[1]), \
41 (unsigned)((addr).s6_addr[2]), \
42 (unsigned)((addr).s6_addr[3]), \
43 (unsigned)((addr).s6_addr[4]), \
44 (unsigned)((addr).s6_addr[5]), \
45 (unsigned)((addr).s6_addr[6]), \
46 (unsigned)((addr).s6_addr[7]), \
47 (unsigned)((addr).s6_addr[8]), \
48 (unsigned)((addr).s6_addr[9]), \
49 (unsigned)((addr).s6_addr[10]), \
50 (unsigned)((addr).s6_addr[11]), \
51 (unsigned)((addr).s6_addr[12]), \
52 (unsigned)((addr).s6_addr[13]), \
53 (unsigned)((addr).s6_addr[14]), \
54 (unsigned)((addr).s6_addr[15])
58 /**********************************************************/
60 /* Show or delete tokens. 8< */
61 struct cmd_obj_del_show_result {
62 cmdline_fixed_string_t action;
66 static void cmd_obj_del_show_parsed(void *parsed_result,
68 __rte_unused void *data)
70 struct cmd_obj_del_show_result *res = parsed_result;
71 char ip_str[INET6_ADDRSTRLEN];
73 if (res->obj->ip.family == AF_INET)
74 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
75 NIPQUAD(res->obj->ip.addr.ipv4));
77 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
78 NIP6(res->obj->ip.addr.ipv6));
80 if (strcmp(res->action, "del") == 0) {
81 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
82 cmdline_printf(cl, "Object %s removed, ip=%s\n",
83 res->obj->name, ip_str);
86 else if (strcmp(res->action, "show") == 0) {
87 cmdline_printf(cl, "Object %s, ip=%s\n",
88 res->obj->name, ip_str);
92 cmdline_parse_token_string_t cmd_obj_action =
93 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
95 parse_token_obj_list_t cmd_obj_obj =
96 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
99 cmdline_parse_inst_t cmd_obj_del_show = {
100 .f = cmd_obj_del_show_parsed, /* function to call */
101 .data = NULL, /* 2nd arg of func */
102 .help_str = "Show/del an object",
103 .tokens = { /* token list, NULL terminated */
104 (void *)&cmd_obj_action,
105 (void *)&cmd_obj_obj,
109 /* >8 End of show or delete tokens. */
111 /**********************************************************/
113 struct cmd_obj_add_result {
114 cmdline_fixed_string_t action;
115 cmdline_fixed_string_t name;
119 static void cmd_obj_add_parsed(void *parsed_result,
121 __rte_unused void *data)
123 struct cmd_obj_add_result *res = parsed_result;
125 char ip_str[INET6_ADDRSTRLEN];
127 SLIST_FOREACH(o, &global_obj_list, next) {
128 if (!strcmp(res->name, o->name)) {
129 cmdline_printf(cl, "Object %s already exist\n", res->name);
135 o = malloc(sizeof(*o));
137 cmdline_printf(cl, "mem error\n");
140 strlcpy(o->name, res->name, sizeof(o->name));
142 SLIST_INSERT_HEAD(&global_obj_list, o, next);
144 if (o->ip.family == AF_INET)
145 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
146 NIPQUAD(o->ip.addr.ipv4));
148 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
149 NIP6(o->ip.addr.ipv6));
151 cmdline_printf(cl, "Object %s added, ip=%s\n",
155 cmdline_parse_token_string_t cmd_obj_action_add =
156 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
157 cmdline_parse_token_string_t cmd_obj_name =
158 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
159 cmdline_parse_token_ipaddr_t cmd_obj_ip =
160 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
162 cmdline_parse_inst_t cmd_obj_add = {
163 .f = cmd_obj_add_parsed, /* function to call */
164 .data = NULL, /* 2nd arg of func */
165 .help_str = "Add an object (name, val)",
166 .tokens = { /* token list, NULL terminated */
167 (void *)&cmd_obj_action_add,
168 (void *)&cmd_obj_name,
174 /**********************************************************/
176 struct cmd_help_result {
177 cmdline_fixed_string_t help;
180 static void cmd_help_parsed(__rte_unused void *parsed_result,
182 __rte_unused void *data)
185 "Demo example of command line interface in RTE\n\n"
186 "This is a readline-like interface that can be used to\n"
187 "debug your RTE application. It supports some features\n"
188 "of GNU readline like completion, cut/paste, and some\n"
189 "other special bindings.\n\n"
190 "This demo shows how rte_cmdline library can be\n"
191 "extended to handle a list of objects. There are\n"
193 "- add obj_name IP\n"
195 "- show obj_name\n\n");
198 cmdline_parse_token_string_t cmd_help_help =
199 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
201 cmdline_parse_inst_t cmd_help = {
202 .f = cmd_help_parsed, /* function to call */
203 .data = NULL, /* 2nd arg of func */
204 .help_str = "show help",
205 .tokens = { /* token list, NULL terminated */
206 (void *)&cmd_help_help,
212 /**********************************************************/
213 /**********************************************************/
214 /****** CONTEXT (list of instruction) */
216 /* Cmdline context list of commands in NULL-terminated table. 8< */
217 cmdline_parse_ctx_t main_ctx[] = {
218 (cmdline_parse_inst_t *)&cmd_obj_del_show,
219 (cmdline_parse_inst_t *)&cmd_obj_add,
220 (cmdline_parse_inst_t *)&cmd_help,
223 /* >8 End of context list. */