summaryrefslogtreecommitdiffstats
path: root/examples/cmdline/commands.c
blob: 06916d7832c7f8d74973c6a977c7cdb3ce3c02ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation.
 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
 * All rights reserved.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <netinet/in.h>
#include <termios.h>
#ifndef __linux__
	#ifdef __FreeBSD__
		#include <sys/socket.h>
	#else
		#include <net/socket.h>
	#endif
#endif

#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_ipaddr.h>
#include <cmdline_parse_num.h>
#include <cmdline_parse_string.h>
#include <cmdline.h>

#include <rte_string_fns.h>

#include "parse_obj_list.h"

struct object_list global_obj_list;

/* not defined under linux */
#ifndef NIPQUAD
#define NIPQUAD_FMT "%u.%u.%u.%u"
#define NIPQUAD(addr)				\
	(unsigned)((unsigned char *)&addr)[0],	\
	(unsigned)((unsigned char *)&addr)[1],	\
	(unsigned)((unsigned char *)&addr)[2],	\
	(unsigned)((unsigned char *)&addr)[3]
#endif

#ifndef NIP6
#define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
#define NIP6(addr)					\
	(unsigned)((addr).s6_addr[0]),			\
	(unsigned)((addr).s6_addr[1]),			\
	(unsigned)((addr).s6_addr[2]),			\
	(unsigned)((addr).s6_addr[3]),			\
	(unsigned)((addr).s6_addr[4]),			\
	(unsigned)((addr).s6_addr[5]),			\
	(unsigned)((addr).s6_addr[6]),			\
	(unsigned)((addr).s6_addr[7]),			\
	(unsigned)((addr).s6_addr[8]),			\
	(unsigned)((addr).s6_addr[9]),			\
	(unsigned)((addr).s6_addr[10]),			\
	(unsigned)((addr).s6_addr[11]),			\
	(unsigned)((addr).s6_addr[12]),			\
	(unsigned)((addr).s6_addr[13]),			\
	(unsigned)((addr).s6_addr[14]),			\
	(unsigned)((addr).s6_addr[15])
#endif


/**********************************************************/

struct cmd_obj_del_show_result {
	cmdline_fixed_string_t action;
	struct object *obj;
};

static void cmd_obj_del_show_parsed(void *parsed_result,
				    struct cmdline *cl,
				    __attribute__((unused)) void *data)
{
	struct cmd_obj_del_show_result *res = parsed_result;
	char ip_str[INET6_ADDRSTRLEN];

	if (res->obj->ip.family == AF_INET)
		snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
			 NIPQUAD(res->obj->ip.addr.ipv4));
	else
		snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
			 NIP6(res->obj->ip.addr.ipv6));

	if (strcmp(res->action, "del") == 0) {
		SLIST_REMOVE(&global_obj_list, res->obj, object, next);
		cmdline_printf(cl, "Object %s removed, ip=%s\n",
			       res->obj->name, ip_str);
		free(res->obj);
	}
	else if (strcmp(res->action, "show") == 0) {
		cmdline_printf(cl, "Object %s, ip=%s\n",
			       res->obj->name, ip_str);
	}
}

cmdline_parse_token_string_t cmd_obj_action =
	TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
				 action, "show#del");
parse_token_obj_list_t cmd_obj_obj =
	TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
				   &global_obj_list);

cmdline_parse_inst_t cmd_obj_del_show = {
	.f = cmd_obj_del_show_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "Show/del an object",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_obj_action,
		(void *)&cmd_obj_obj,
		NULL,
	},
};

/**********************************************************/

struct cmd_obj_add_result {
	cmdline_fixed_string_t action;
	cmdline_fixed_string_t name;
	cmdline_ipaddr_t ip;
};

static void cmd_obj_add_parsed(void *parsed_result,
			       struct cmdline *cl,
			       __attribute__((unused)) void *data)
{
	struct cmd_obj_add_result *res = parsed_result;
	struct object *o;
	char ip_str[INET6_ADDRSTRLEN];

	SLIST_FOREACH(o, &global_obj_list, next) {
		if (!strcmp(res->name, o->name)) {
			cmdline_printf(cl, "Object %s already exist\n", res->name);
			return;
		}
		break;
	}

	o = malloc(sizeof(*o));
	if (!o) {
		cmdline_printf(cl, "mem error\n");
		return;
	}
	snprintf(o->name, sizeof(o->name), "%s", res->name);
	o->ip = res->ip;
	SLIST_INSERT_HEAD(&global_obj_list, o, next);

	if (o->ip.family == AF_INET)
		snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
			 NIPQUAD(o->ip.addr.ipv4));
	else
		snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
			 NIP6(o->ip.addr.ipv6));

	cmdline_printf(cl, "Object %s added, ip=%s\n",
		       o->name, ip_str);
}

cmdline_parse_token_string_t cmd_obj_action_add =
	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
cmdline_parse_token_string_t cmd_obj_name =
	TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
cmdline_parse_token_ipaddr_t cmd_obj_ip =
	TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);

cmdline_parse_inst_t cmd_obj_add = {
	.f = cmd_obj_add_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "Add an object (name, val)",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_obj_action_add,
		(void *)&cmd_obj_name,
		(void *)&cmd_obj_ip,
		NULL,
	},
};

/**********************************************************/

struct cmd_help_result {
	cmdline_fixed_string_t help;
};

static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
			    struct cmdline *cl,
			    __attribute__((unused)) void *data)
{
	cmdline_printf(cl,
		       "Demo example of command line interface in RTE\n\n"
		       "This is a readline-like interface that can be used to\n"
		       "debug your RTE application. It supports some features\n"
		       "of GNU readline like completion, cut/paste, and some\n"
		       "other special bindings.\n\n"
		       "This demo shows how rte_cmdline library can be\n"
		       "extended to handle a list of objects. There are\n"
		       "3 commands:\n"
		       "- add obj_name IP\n"
		       "- del obj_name\n"
		       "- show obj_name\n\n");
}

cmdline_parse_token_string_t cmd_help_help =
	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");

cmdline_parse_inst_t cmd_help = {
	.f = cmd_help_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "show help",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_help_help,
		NULL,
	},
};


/**********************************************************/
/**********************************************************/
/****** CONTEXT (list of instruction) */

cmdline_parse_ctx_t main_ctx[] = {
	(cmdline_parse_inst_t *)&cmd_obj_del_show,
	(cmdline_parse_inst_t *)&cmd_obj_add,
	(cmdline_parse_inst_t *)&cmd_help,
	NULL,
};