1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <[email protected]>
4 * All rights reserved.
5 */
6
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <errno.h>
12
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>
18 #include <cmdline.h>
19
20 #include <rte_string_fns.h>
21
22 #include "parse_obj_list.h"
23
24 struct object_list global_obj_list;
25
26 /* not defined under linux */
27 #ifndef NIPQUAD
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]
34 #endif
35
36 #ifndef NIP6
37 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
38 #define NIP6(addr) \
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])
55 #endif
56
57
58 /**********************************************************/
59
60 /* Show or delete tokens. 8< */
61 struct cmd_obj_del_show_result {
62 cmdline_fixed_string_t action;
63 struct object *obj;
64 };
65
cmd_obj_del_show_parsed(void * parsed_result,struct cmdline * cl,__rte_unused void * data)66 static void cmd_obj_del_show_parsed(void *parsed_result,
67 struct cmdline *cl,
68 __rte_unused void *data)
69 {
70 struct cmd_obj_del_show_result *res = parsed_result;
71 char ip_str[INET6_ADDRSTRLEN];
72
73 if (res->obj->ip.family == AF_INET)
74 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
75 NIPQUAD(res->obj->ip.addr.ipv4));
76 else
77 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
78 NIP6(res->obj->ip.addr.ipv6));
79
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);
84 free(res->obj);
85 }
86 else if (strcmp(res->action, "show") == 0) {
87 cmdline_printf(cl, "Object %s, ip=%s\n",
88 res->obj->name, ip_str);
89 }
90 }
91
92 cmdline_parse_token_string_t cmd_obj_action =
93 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
94 action, "show#del");
95 parse_token_obj_list_t cmd_obj_obj =
96 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
97 &global_obj_list);
98
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,
106 NULL,
107 },
108 };
109 /* >8 End of show or delete tokens. */
110
111 /**********************************************************/
112
113 struct cmd_obj_add_result {
114 cmdline_fixed_string_t action;
115 cmdline_fixed_string_t name;
116 cmdline_ipaddr_t ip;
117 };
118
cmd_obj_add_parsed(void * parsed_result,struct cmdline * cl,__rte_unused void * data)119 static void cmd_obj_add_parsed(void *parsed_result,
120 struct cmdline *cl,
121 __rte_unused void *data)
122 {
123 struct cmd_obj_add_result *res = parsed_result;
124 struct object *o;
125 char ip_str[INET6_ADDRSTRLEN];
126
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);
130 return;
131 }
132 break;
133 }
134
135 o = malloc(sizeof(*o));
136 if (!o) {
137 cmdline_printf(cl, "mem error\n");
138 return;
139 }
140 strlcpy(o->name, res->name, sizeof(o->name));
141 o->ip = res->ip;
142 SLIST_INSERT_HEAD(&global_obj_list, o, next);
143
144 if (o->ip.family == AF_INET)
145 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
146 NIPQUAD(o->ip.addr.ipv4));
147 else
148 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
149 NIP6(o->ip.addr.ipv6));
150
151 cmdline_printf(cl, "Object %s added, ip=%s\n",
152 o->name, ip_str);
153 }
154
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);
161
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,
169 (void *)&cmd_obj_ip,
170 NULL,
171 },
172 };
173
174 /**********************************************************/
175
176 struct cmd_help_result {
177 cmdline_fixed_string_t help;
178 };
179
cmd_help_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)180 static void cmd_help_parsed(__rte_unused void *parsed_result,
181 struct cmdline *cl,
182 __rte_unused void *data)
183 {
184 cmdline_printf(cl,
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"
192 "3 commands:\n"
193 "- add obj_name IP\n"
194 "- del obj_name\n"
195 "- show obj_name\n\n");
196 }
197
198 cmdline_parse_token_string_t cmd_help_help =
199 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
200
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,
207 NULL,
208 },
209 };
210
211
212 /**********************************************************/
213 /**********************************************************/
214 /****** CONTEXT (list of instruction) */
215
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,
221 NULL,
222 };
223 /* >8 End of context list. */
224