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 <netinet/in.h>
14 #ifdef RTE_EXEC_ENV_FREEBSD
15 #include <sys/socket.h>
18 #include <cmdline_rdline.h>
19 #include <cmdline_parse.h>
20 #include <cmdline_parse_ipaddr.h>
21 #include <cmdline_parse_num.h>
22 #include <cmdline_parse_string.h>
25 #include <rte_string_fns.h>
27 #include "parse_obj_list.h"
29 struct object_list global_obj_list;
31 /* not defined under linux */
33 #define NIPQUAD_FMT "%u.%u.%u.%u"
34 #define NIPQUAD(addr) \
35 (unsigned)((unsigned char *)&addr)[0], \
36 (unsigned)((unsigned char *)&addr)[1], \
37 (unsigned)((unsigned char *)&addr)[2], \
38 (unsigned)((unsigned char *)&addr)[3]
42 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
44 (unsigned)((addr).s6_addr[0]), \
45 (unsigned)((addr).s6_addr[1]), \
46 (unsigned)((addr).s6_addr[2]), \
47 (unsigned)((addr).s6_addr[3]), \
48 (unsigned)((addr).s6_addr[4]), \
49 (unsigned)((addr).s6_addr[5]), \
50 (unsigned)((addr).s6_addr[6]), \
51 (unsigned)((addr).s6_addr[7]), \
52 (unsigned)((addr).s6_addr[8]), \
53 (unsigned)((addr).s6_addr[9]), \
54 (unsigned)((addr).s6_addr[10]), \
55 (unsigned)((addr).s6_addr[11]), \
56 (unsigned)((addr).s6_addr[12]), \
57 (unsigned)((addr).s6_addr[13]), \
58 (unsigned)((addr).s6_addr[14]), \
59 (unsigned)((addr).s6_addr[15])
63 /**********************************************************/
65 struct cmd_obj_del_show_result {
66 cmdline_fixed_string_t action;
70 static void cmd_obj_del_show_parsed(void *parsed_result,
72 __rte_unused void *data)
74 struct cmd_obj_del_show_result *res = parsed_result;
75 char ip_str[INET6_ADDRSTRLEN];
77 if (res->obj->ip.family == AF_INET)
78 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
79 NIPQUAD(res->obj->ip.addr.ipv4));
81 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
82 NIP6(res->obj->ip.addr.ipv6));
84 if (strcmp(res->action, "del") == 0) {
85 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
86 cmdline_printf(cl, "Object %s removed, ip=%s\n",
87 res->obj->name, ip_str);
90 else if (strcmp(res->action, "show") == 0) {
91 cmdline_printf(cl, "Object %s, ip=%s\n",
92 res->obj->name, ip_str);
96 cmdline_parse_token_string_t cmd_obj_action =
97 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
99 parse_token_obj_list_t cmd_obj_obj =
100 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
103 cmdline_parse_inst_t cmd_obj_del_show = {
104 .f = cmd_obj_del_show_parsed, /* function to call */
105 .data = NULL, /* 2nd arg of func */
106 .help_str = "Show/del an object",
107 .tokens = { /* token list, NULL terminated */
108 (void *)&cmd_obj_action,
109 (void *)&cmd_obj_obj,
114 /**********************************************************/
116 struct cmd_obj_add_result {
117 cmdline_fixed_string_t action;
118 cmdline_fixed_string_t name;
122 static void cmd_obj_add_parsed(void *parsed_result,
124 __rte_unused void *data)
126 struct cmd_obj_add_result *res = parsed_result;
128 char ip_str[INET6_ADDRSTRLEN];
130 SLIST_FOREACH(o, &global_obj_list, next) {
131 if (!strcmp(res->name, o->name)) {
132 cmdline_printf(cl, "Object %s already exist\n", res->name);
138 o = malloc(sizeof(*o));
140 cmdline_printf(cl, "mem error\n");
143 strlcpy(o->name, res->name, sizeof(o->name));
145 SLIST_INSERT_HEAD(&global_obj_list, o, next);
147 if (o->ip.family == AF_INET)
148 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
149 NIPQUAD(o->ip.addr.ipv4));
151 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
152 NIP6(o->ip.addr.ipv6));
154 cmdline_printf(cl, "Object %s added, ip=%s\n",
158 cmdline_parse_token_string_t cmd_obj_action_add =
159 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
160 cmdline_parse_token_string_t cmd_obj_name =
161 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
162 cmdline_parse_token_ipaddr_t cmd_obj_ip =
163 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
165 cmdline_parse_inst_t cmd_obj_add = {
166 .f = cmd_obj_add_parsed, /* function to call */
167 .data = NULL, /* 2nd arg of func */
168 .help_str = "Add an object (name, val)",
169 .tokens = { /* token list, NULL terminated */
170 (void *)&cmd_obj_action_add,
171 (void *)&cmd_obj_name,
177 /**********************************************************/
179 struct cmd_help_result {
180 cmdline_fixed_string_t help;
183 static void cmd_help_parsed(__rte_unused void *parsed_result,
185 __rte_unused void *data)
188 "Demo example of command line interface in RTE\n\n"
189 "This is a readline-like interface that can be used to\n"
190 "debug your RTE application. It supports some features\n"
191 "of GNU readline like completion, cut/paste, and some\n"
192 "other special bindings.\n\n"
193 "This demo shows how rte_cmdline library can be\n"
194 "extended to handle a list of objects. There are\n"
196 "- add obj_name IP\n"
198 "- show obj_name\n\n");
201 cmdline_parse_token_string_t cmd_help_help =
202 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
204 cmdline_parse_inst_t cmd_help = {
205 .f = cmd_help_parsed, /* function to call */
206 .data = NULL, /* 2nd arg of func */
207 .help_str = "show help",
208 .tokens = { /* token list, NULL terminated */
209 (void *)&cmd_help_help,
215 /**********************************************************/
216 /**********************************************************/
217 /****** CONTEXT (list of instruction) */
219 cmdline_parse_ctx_t main_ctx[] = {
220 (cmdline_parse_inst_t *)&cmd_obj_del_show,
221 (cmdline_parse_inst_t *)&cmd_obj_add,
222 (cmdline_parse_inst_t *)&cmd_help,