mbuf: remove rte_ctrlmbuf
[dpdk.git] / app / test / commands.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <netinet/in.h>
41 #include <termios.h>
42 #ifndef __linux__
43 #ifndef __FreeBSD__
44 #include <net/socket.h>
45 #endif
46 #endif
47 #include <inttypes.h>
48 #include <errno.h>
49 #include <sys/queue.h>
50
51 #include <rte_common.h>
52 #include <rte_log.h>
53 #include <rte_debug.h>
54 #include <rte_memory.h>
55 #include <rte_memcpy.h>
56 #include <rte_memzone.h>
57 #include <rte_launch.h>
58 #include <rte_cycles.h>
59 #include <rte_tailq.h>
60 #include <rte_eal.h>
61 #include <rte_per_lcore.h>
62 #include <rte_lcore.h>
63 #include <rte_atomic.h>
64 #include <rte_branch_prediction.h>
65 #include <rte_ring.h>
66 #include <rte_mempool.h>
67 #include <rte_mbuf.h>
68 #include <rte_devargs.h>
69
70 #include <cmdline_rdline.h>
71 #include <cmdline_parse.h>
72 #include <cmdline_parse_ipaddr.h>
73 #include <cmdline_parse_num.h>
74 #include <cmdline_parse_string.h>
75 #include <cmdline.h>
76
77 #include "test.h"
78
79 /****************/
80
81 static struct test_commands_list commands_list =
82         TAILQ_HEAD_INITIALIZER(commands_list);
83
84 void
85 add_test_command(struct test_command *t)
86 {
87         TAILQ_INSERT_TAIL(&commands_list, t, next);
88 }
89
90 struct cmd_autotest_result {
91         cmdline_fixed_string_t autotest;
92 };
93
94 static void cmd_autotest_parsed(void *parsed_result,
95                                 __attribute__((unused)) struct cmdline *cl,
96                                 __attribute__((unused)) void *data)
97 {
98         struct test_command *t;
99         struct cmd_autotest_result *res = parsed_result;
100         int ret = 0;
101
102         TAILQ_FOREACH(t, &commands_list, next) {
103                 if (!strcmp(res->autotest, t->command))
104                         ret = t->callback();
105         }
106
107         if (ret == 0)
108                 printf("Test OK\n");
109         else
110                 printf("Test Failed\n");
111         fflush(stdout);
112 }
113
114 cmdline_parse_token_string_t cmd_autotest_autotest =
115         TOKEN_STRING_INITIALIZER(struct cmd_autotest_result, autotest,
116                                  "");
117
118 cmdline_parse_inst_t cmd_autotest = {
119         .f = cmd_autotest_parsed,  /* function to call */
120         .data = NULL,      /* 2nd arg of func */
121         .help_str = "launch autotest",
122         .tokens = {        /* token list, NULL terminated */
123                 (void *)&cmd_autotest_autotest,
124                 NULL,
125         },
126 };
127
128 /****************/
129
130 struct cmd_dump_result {
131         cmdline_fixed_string_t dump;
132 };
133
134 static void
135 dump_struct_sizes(void)
136 {
137 #define DUMP_SIZE(t) printf("sizeof(" #t ") = %u\n", (unsigned)sizeof(t));
138         DUMP_SIZE(struct rte_mbuf);
139         DUMP_SIZE(struct rte_pktmbuf);
140         DUMP_SIZE(struct rte_mempool);
141         DUMP_SIZE(struct rte_ring);
142 #undef DUMP_SIZE
143 }
144
145 static void cmd_dump_parsed(void *parsed_result,
146                             __attribute__((unused)) struct cmdline *cl,
147                             __attribute__((unused)) void *data)
148 {
149         struct cmd_dump_result *res = parsed_result;
150
151         if (!strcmp(res->dump, "dump_physmem"))
152                 rte_dump_physmem_layout(stdout);
153         else if (!strcmp(res->dump, "dump_memzone"))
154                 rte_memzone_dump(stdout);
155         else if (!strcmp(res->dump, "dump_log_history"))
156                 rte_log_dump_history(stdout);
157         else if (!strcmp(res->dump, "dump_struct_sizes"))
158                 dump_struct_sizes();
159         else if (!strcmp(res->dump, "dump_ring"))
160                 rte_ring_list_dump(stdout);
161         else if (!strcmp(res->dump, "dump_mempool"))
162                 rte_mempool_list_dump(stdout);
163         else if (!strcmp(res->dump, "dump_devargs"))
164                 rte_eal_devargs_dump(stdout);
165 }
166
167 cmdline_parse_token_string_t cmd_dump_dump =
168         TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
169                                  "dump_physmem#dump_memzone#dump_log_history#"
170                                  "dump_struct_sizes#dump_ring#dump_mempool#"
171                                  "dump_devargs");
172
173 cmdline_parse_inst_t cmd_dump = {
174         .f = cmd_dump_parsed,  /* function to call */
175         .data = NULL,      /* 2nd arg of func */
176         .help_str = "dump status",
177         .tokens = {        /* token list, NULL terminated */
178                 (void *)&cmd_dump_dump,
179                 NULL,
180         },
181 };
182
183 /****************/
184
185 struct cmd_dump_one_result {
186         cmdline_fixed_string_t dump;
187         cmdline_fixed_string_t name;
188 };
189
190 static void cmd_dump_one_parsed(void *parsed_result, struct cmdline *cl,
191                                 __attribute__((unused)) void *data)
192 {
193         struct cmd_dump_one_result *res = parsed_result;
194
195         if (!strcmp(res->dump, "dump_ring")) {
196                 struct rte_ring *r;
197                 r = rte_ring_lookup(res->name);
198                 if (r == NULL) {
199                         cmdline_printf(cl, "Cannot find ring\n");
200                         return;
201                 }
202                 rte_ring_dump(stdout, r);
203         }
204         else if (!strcmp(res->dump, "dump_mempool")) {
205                 struct rte_mempool *mp;
206                 mp = rte_mempool_lookup(res->name);
207                 if (mp == NULL) {
208                         cmdline_printf(cl, "Cannot find mempool\n");
209                         return;
210                 }
211                 rte_mempool_dump(stdout, mp);
212         }
213 }
214
215 cmdline_parse_token_string_t cmd_dump_one_dump =
216         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, dump,
217                                  "dump_ring#dump_mempool");
218
219 cmdline_parse_token_string_t cmd_dump_one_name =
220         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, name, NULL);
221
222 cmdline_parse_inst_t cmd_dump_one = {
223         .f = cmd_dump_one_parsed,  /* function to call */
224         .data = NULL,      /* 2nd arg of func */
225         .help_str = "dump one ring/mempool: dump_ring|dump_mempool <name>",
226         .tokens = {        /* token list, NULL terminated */
227                 (void *)&cmd_dump_one_dump,
228                 (void *)&cmd_dump_one_name,
229                 NULL,
230         },
231 };
232
233 /****************/
234
235 struct cmd_set_ring_result {
236         cmdline_fixed_string_t set;
237         cmdline_fixed_string_t name;
238         uint32_t value;
239 };
240
241 static void cmd_set_ring_parsed(void *parsed_result, struct cmdline *cl,
242                                 __attribute__((unused)) void *data)
243 {
244         struct cmd_set_ring_result *res = parsed_result;
245         struct rte_ring *r;
246         int ret;
247
248         r = rte_ring_lookup(res->name);
249         if (r == NULL) {
250                 cmdline_printf(cl, "Cannot find ring\n");
251                 return;
252         }
253
254         if (!strcmp(res->set, "set_watermark")) {
255                 ret = rte_ring_set_water_mark(r, res->value);
256                 if (ret != 0)
257                         cmdline_printf(cl, "Cannot set water mark\n");
258         }
259 }
260
261 cmdline_parse_token_string_t cmd_set_ring_set =
262         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, set,
263                                  "set_watermark");
264
265 cmdline_parse_token_string_t cmd_set_ring_name =
266         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, name, NULL);
267
268 cmdline_parse_token_num_t cmd_set_ring_value =
269         TOKEN_NUM_INITIALIZER(struct cmd_set_ring_result, value, UINT32);
270
271 cmdline_parse_inst_t cmd_set_ring = {
272         .f = cmd_set_ring_parsed,  /* function to call */
273         .data = NULL,      /* 2nd arg of func */
274         .help_str = "set watermark: "
275                         "set_watermark <ring_name> <value>",
276         .tokens = {        /* token list, NULL terminated */
277                 (void *)&cmd_set_ring_set,
278                 (void *)&cmd_set_ring_name,
279                 (void *)&cmd_set_ring_value,
280                 NULL,
281         },
282 };
283
284 /****************/
285
286 struct cmd_quit_result {
287         cmdline_fixed_string_t quit;
288 };
289
290 static void
291 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
292                 struct cmdline *cl,
293                 __attribute__((unused)) void *data)
294 {
295         cmdline_quit(cl);
296 }
297
298 cmdline_parse_token_string_t cmd_quit_quit =
299         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
300                                  "quit");
301
302 cmdline_parse_inst_t cmd_quit = {
303         .f = cmd_quit_parsed,  /* function to call */
304         .data = NULL,      /* 2nd arg of func */
305         .help_str = "exit application",
306         .tokens = {        /* token list, NULL terminated */
307                 (void *)&cmd_quit_quit,
308                 NULL,
309         },
310 };
311
312 /****************/
313
314 cmdline_parse_ctx_t main_ctx[] = {
315         (cmdline_parse_inst_t *)&cmd_autotest,
316         (cmdline_parse_inst_t *)&cmd_dump,
317         (cmdline_parse_inst_t *)&cmd_dump_one,
318         (cmdline_parse_inst_t *)&cmd_set_ring,
319         (cmdline_parse_inst_t *)&cmd_quit,
320         NULL,
321 };
322
323 int commands_init(void)
324 {
325         struct test_command *t;
326         char *commands, *ptr;
327         int commands_len = 0;
328
329         TAILQ_FOREACH(t, &commands_list, next) {
330                 commands_len += strlen(t->command) + 1;
331         }
332
333         commands = malloc(commands_len);
334         if (!commands)
335                 return -1;
336
337         ptr = commands;
338         TAILQ_FOREACH(t, &commands_list, next) {
339                 ptr += sprintf(ptr, "%s#", t->command);
340         }
341         ptr--;
342         ptr[0] = '\0';
343
344         cmd_autotest_autotest.string_data.str = commands;
345         return 0;
346 }