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