remove version in all files
[dpdk.git] / examples / cmdline / commands.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 /*
36  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37  * All rights reserved.
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in the
45  *       documentation and/or other materials provided with the distribution.
46  *     * Neither the name of the University of California, Berkeley nor the
47  *       names of its contributors may be used to endorse or promote products
48  *       derived from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61
62 #include <stdio.h>
63 #include <stdint.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <stdarg.h>
67 #include <errno.h>
68 #include <netinet/in.h>
69 #include <termios.h>
70 #ifndef __linux__
71 #include <net/socket.h>
72 #endif
73
74 #include <cmdline_rdline.h>
75 #include <cmdline_parse.h>
76 #include <cmdline_parse_ipaddr.h>
77 #include <cmdline_parse_num.h>
78 #include <cmdline_parse_string.h>
79 #include <cmdline.h>
80
81 #include <rte_string_fns.h>
82
83 #include "parse_obj_list.h"
84
85 struct object_list global_obj_list;
86
87 /* not defined under linux */
88 #ifndef NIPQUAD
89 #define NIPQUAD_FMT "%u.%u.%u.%u"
90 #define NIPQUAD(addr)                           \
91         (unsigned)((unsigned char *)&addr)[0],  \
92         (unsigned)((unsigned char *)&addr)[1],  \
93         (unsigned)((unsigned char *)&addr)[2],  \
94         (unsigned)((unsigned char *)&addr)[3]
95 #endif
96
97 #ifndef NIP6
98 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
99 #define NIP6(addr)                                      \
100         (unsigned)((addr).s6_addr[0]),                  \
101         (unsigned)((addr).s6_addr[1]),                  \
102         (unsigned)((addr).s6_addr[2]),                  \
103         (unsigned)((addr).s6_addr[3]),                  \
104         (unsigned)((addr).s6_addr[4]),                  \
105         (unsigned)((addr).s6_addr[5]),                  \
106         (unsigned)((addr).s6_addr[6]),                  \
107         (unsigned)((addr).s6_addr[7]),                  \
108         (unsigned)((addr).s6_addr[8]),                  \
109         (unsigned)((addr).s6_addr[9]),                  \
110         (unsigned)((addr).s6_addr[10]),                 \
111         (unsigned)((addr).s6_addr[11]),                 \
112         (unsigned)((addr).s6_addr[12]),                 \
113         (unsigned)((addr).s6_addr[13]),                 \
114         (unsigned)((addr).s6_addr[14]),                 \
115         (unsigned)((addr).s6_addr[15])
116 #endif
117
118
119 /**********************************************************/
120
121 struct cmd_obj_del_show_result {
122         cmdline_fixed_string_t action;
123         struct object *obj;
124 };
125
126 static void cmd_obj_del_show_parsed(void *parsed_result,
127                                     struct cmdline *cl,
128                                     __attribute__((unused)) void *data)
129 {
130         struct cmd_obj_del_show_result *res = parsed_result;
131         char ip_str[INET6_ADDRSTRLEN];
132
133         if (res->obj->ip.family == AF_INET)
134                 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
135                          NIPQUAD(res->obj->ip.addr.ipv4));
136         else
137                 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
138                          NIP6(res->obj->ip.addr.ipv6));
139
140         if (strcmp(res->action, "del") == 0) {
141                 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
142                 cmdline_printf(cl, "Object %s removed, ip=%s\n",
143                                res->obj->name, ip_str);
144                 free(res->obj);
145         }
146         else if (strcmp(res->action, "show") == 0) {
147                 cmdline_printf(cl, "Object %s, ip=%s\n",
148                                res->obj->name, ip_str);
149         }
150 }
151
152 cmdline_parse_token_string_t cmd_obj_action =
153         TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result,
154                                  action, "show#del");
155 parse_token_obj_list_t cmd_obj_obj =
156         TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj,
157                                    &global_obj_list);
158
159 cmdline_parse_inst_t cmd_obj_del_show = {
160         .f = cmd_obj_del_show_parsed,  /* function to call */
161         .data = NULL,      /* 2nd arg of func */
162         .help_str = "Show/del an object",
163         .tokens = {        /* token list, NULL terminated */
164                 (void *)&cmd_obj_action,
165                 (void *)&cmd_obj_obj,
166                 NULL,
167         },
168 };
169
170 /**********************************************************/
171
172 struct cmd_obj_add_result {
173         cmdline_fixed_string_t action;
174         cmdline_fixed_string_t name;
175         cmdline_ipaddr_t ip;
176 };
177
178 static void cmd_obj_add_parsed(void *parsed_result,
179                                struct cmdline *cl,
180                                __attribute__((unused)) void *data)
181 {
182         struct cmd_obj_add_result *res = parsed_result;
183         struct object *o;
184         char ip_str[INET6_ADDRSTRLEN];
185
186         SLIST_FOREACH(o, &global_obj_list, next) {
187                 if (!strcmp(res->name, o->name)) {
188                         cmdline_printf(cl, "Object %s already exist\n", res->name);
189                         return;
190                 }
191                 break;
192         }
193
194         o = malloc(sizeof(*o));
195         if (!o) {
196                 cmdline_printf(cl, "mem error\n");
197                 return;
198         }
199         rte_snprintf(o->name, sizeof(o->name), "%s", res->name);
200         o->ip = res->ip;
201         SLIST_INSERT_HEAD(&global_obj_list, o, next);
202
203         if (o->ip.family == AF_INET)
204                 rte_snprintf(ip_str, sizeof(ip_str), NIPQUAD_FMT,
205                          NIPQUAD(o->ip.addr.ipv4));
206         else
207                 rte_snprintf(ip_str, sizeof(ip_str), NIP6_FMT,
208                          NIP6(o->ip.addr.ipv6));
209
210         cmdline_printf(cl, "Object %s added, ip=%s\n",
211                        o->name, ip_str);
212 }
213
214 cmdline_parse_token_string_t cmd_obj_action_add =
215         TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
216 cmdline_parse_token_string_t cmd_obj_name =
217         TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
218 cmdline_parse_token_ipaddr_t cmd_obj_ip =
219         TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
220
221 cmdline_parse_inst_t cmd_obj_add = {
222         .f = cmd_obj_add_parsed,  /* function to call */
223         .data = NULL,      /* 2nd arg of func */
224         .help_str = "Add an object (name, val)",
225         .tokens = {        /* token list, NULL terminated */
226                 (void *)&cmd_obj_action_add,
227                 (void *)&cmd_obj_name,
228                 (void *)&cmd_obj_ip,
229                 NULL,
230         },
231 };
232
233 /**********************************************************/
234
235 struct cmd_help_result {
236         cmdline_fixed_string_t help;
237 };
238
239 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
240                             struct cmdline *cl,
241                             __attribute__((unused)) void *data)
242 {
243         cmdline_printf(cl,
244                        "Demo example of command line interface in RTE\n\n"
245                        "This is a readline-like interface that can be used to\n"
246                        "debug your RTE application. It supports some features\n"
247                        "of GNU readline like completion, cut/paste, and some\n"
248                        "other special bindings.\n\n"
249                        "This demo shows how rte_cmdline library can be\n"
250                        "extended to handle a list of objects. There are\n"
251                        "3 commands:\n"
252                        "- add obj_name IP\n"
253                        "- del obj_name\n"
254                        "- show obj_name\n\n");
255 }
256
257 cmdline_parse_token_string_t cmd_help_help =
258         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
259
260 cmdline_parse_inst_t cmd_help = {
261         .f = cmd_help_parsed,  /* function to call */
262         .data = NULL,      /* 2nd arg of func */
263         .help_str = "show help",
264         .tokens = {        /* token list, NULL terminated */
265                 (void *)&cmd_help_help,
266                 NULL,
267         },
268 };
269
270
271 /**********************************************************/
272 /**********************************************************/
273 /****** CONTEXT (list of instruction) */
274
275 cmdline_parse_ctx_t main_ctx[] = {
276         (cmdline_parse_inst_t *)&cmd_obj_del_show,
277         (cmdline_parse_inst_t *)&cmd_obj_add,
278         (cmdline_parse_inst_t *)&cmd_help,
279         NULL,
280 };
281