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>
15 #ifdef RTE_EXEC_ENV_FREEBSD
16 #include <sys/socket.h>
19 #include <cmdline_rdline.h>
20 #include <cmdline_parse.h>
21 #include <cmdline_parse_ipaddr.h>
22 #include <cmdline_parse_num.h>
23 #include <cmdline_parse_string.h>
26 #include <rte_string_fns.h>
28 #include "parse_obj_list.h"
30 struct object_list global_obj_list;
32 /* not defined under linux */
34 #define NIPQUAD_FMT "%u.%u.%u.%u"
35 #define NIPQUAD(addr) \
36 (unsigned)((unsigned char *)&addr)[0], \
37 (unsigned)((unsigned char *)&addr)[1], \
38 (unsigned)((unsigned char *)&addr)[2], \
39 (unsigned)((unsigned char *)&addr)[3]
43 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
45 (unsigned)((addr).s6_addr[0]), \
46 (unsigned)((addr).s6_addr[1]), \
47 (unsigned)((addr).s6_addr[2]), \
48 (unsigned)((addr).s6_addr[3]), \
49 (unsigned)((addr).s6_addr[4]), \
50 (unsigned)((addr).s6_addr[5]), \
51 (unsigned)((addr).s6_addr[6]), \
52 (unsigned)((addr).s6_addr[7]), \
53 (unsigned)((addr).s6_addr[8]), \
54 (unsigned)((addr).s6_addr[9]), \
55 (unsigned)((addr).s6_addr[10]), \
56 (unsigned)((addr).s6_addr[11]), \
57 (unsigned)((addr).s6_addr[12]), \
58 (unsigned)((addr).s6_addr[13]), \
59 (unsigned)((addr).s6_addr[14]), \
60 (unsigned)((addr).s6_addr[15])
64 /**********************************************************/
66 struct cmd_obj_del_show_result {
67 cmdline_fixed_string_t action;
71 static void cmd_obj_del_show_parsed(void *parsed_result,
73 __rte_unused void *data)
75 struct cmd_obj_del_show_result *res = parsed_result;
76 char ip_str[INET6_ADDRSTRLEN];
78 if (res->obj->ip.family == AF_INET)
79 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
80 NIPQUAD(res->obj->ip.addr.ipv4));
82 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
83 NIP6(res->obj->ip.addr.ipv6));
85 if (strcmp(res->action, "del") == 0) {
86 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
87 cmdline_printf(cl, "Object %s removed, ip=%s\n",
88 res->obj->name, ip_str);
91 else if (strcmp(res->action, "show") == 0) {
92 cmdline_printf(cl, "Object %s, ip=%s\n",
93 res->obj->name, ip_str);
97 cmdline_parse_token_string_t cmd_obj_action =
98 TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
100 parse_token_obj_list_t cmd_obj_obj =
101 TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
104 cmdline_parse_inst_t cmd_obj_del_show = {
105 .f = cmd_obj_del_show_parsed, /* function to call */
106 .data = NULL, /* 2nd arg of func */
107 .help_str = "Show/del an object",
108 .tokens = { /* token list, NULL terminated */
109 (void *)&cmd_obj_action,
110 (void *)&cmd_obj_obj,
115 /**********************************************************/
117 struct cmd_obj_add_result {
118 cmdline_fixed_string_t action;
119 cmdline_fixed_string_t name;
123 static void cmd_obj_add_parsed(void *parsed_result,
125 __rte_unused void *data)
127 struct cmd_obj_add_result *res = parsed_result;
129 char ip_str[INET6_ADDRSTRLEN];
131 SLIST_FOREACH(o, &global_obj_list, next) {
132 if (!strcmp(res->name, o->name)) {
133 cmdline_printf(cl, "Object %s already exist\n", res->name);
139 o = malloc(sizeof(*o));
141 cmdline_printf(cl, "mem error\n");
144 strlcpy(o->name, res->name, sizeof(o->name));
146 SLIST_INSERT_HEAD(&global_obj_list, o, next);
148 if (o->ip.family == AF_INET)
149 snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
150 NIPQUAD(o->ip.addr.ipv4));
152 snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
153 NIP6(o->ip.addr.ipv6));
155 cmdline_printf(cl, "Object %s added, ip=%s\n",
159 cmdline_parse_token_string_t cmd_obj_action_add =
160 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
161 cmdline_parse_token_string_t cmd_obj_name =
162 TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
163 cmdline_parse_token_ipaddr_t cmd_obj_ip =
164 TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
166 cmdline_parse_inst_t cmd_obj_add = {
167 .f = cmd_obj_add_parsed, /* function to call */
168 .data = NULL, /* 2nd arg of func */
169 .help_str = "Add an object (name, val)",
170 .tokens = { /* token list, NULL terminated */
171 (void *)&cmd_obj_action_add,
172 (void *)&cmd_obj_name,
178 /**********************************************************/
180 struct cmd_help_result {
181 cmdline_fixed_string_t help;
184 static void cmd_help_parsed(__rte_unused void *parsed_result,
186 __rte_unused void *data)
189 "Demo example of command line interface in RTE\n\n"
190 "This is a readline-like interface that can be used to\n"
191 "debug your RTE application. It supports some features\n"
192 "of GNU readline like completion, cut/paste, and some\n"
193 "other special bindings.\n\n"
194 "This demo shows how rte_cmdline library can be\n"
195 "extended to handle a list of objects. There are\n"
197 "- add obj_name IP\n"
199 "- show obj_name\n\n");
202 cmdline_parse_token_string_t cmd_help_help =
203 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
205 cmdline_parse_inst_t cmd_help = {
206 .f = cmd_help_parsed, /* function to call */
207 .data = NULL, /* 2nd arg of func */
208 .help_str = "show help",
209 .tokens = { /* token list, NULL terminated */
210 (void *)&cmd_help_help,
216 /**********************************************************/
217 /**********************************************************/
218 /****** CONTEXT (list of instruction) */
220 cmdline_parse_ctx_t main_ctx[] = {
221 (cmdline_parse_inst_t *)&cmd_obj_del_show,
222 (cmdline_parse_inst_t *)&cmd_obj_add,
223 (cmdline_parse_inst_t *)&cmd_help,