b7f60680b50440eddc796a64458e73fadff55065
[dpdk.git] / test / 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_eal.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
62 #include <rte_atomic.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_ring.h>
65 #include <rte_malloc.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         last_test_result = ret;
108         if (ret == 0)
109                 printf("Test OK\n");
110         else if (ret == TEST_SKIPPED)
111                 printf("Test Skipped\n");
112         else
113                 printf("Test Failed\n");
114         fflush(stdout);
115 }
116
117 cmdline_parse_token_string_t cmd_autotest_autotest =
118         TOKEN_STRING_INITIALIZER(struct cmd_autotest_result, autotest,
119                                  "");
120
121 cmdline_parse_inst_t cmd_autotest = {
122         .f = cmd_autotest_parsed,  /* function to call */
123         .data = NULL,      /* 2nd arg of func */
124         .help_str = "launch autotest",
125         .tokens = {        /* token list, NULL terminated */
126                 (void *)&cmd_autotest_autotest,
127                 NULL,
128         },
129 };
130
131 /****************/
132
133 struct cmd_dump_result {
134         cmdline_fixed_string_t dump;
135 };
136
137 static void
138 dump_struct_sizes(void)
139 {
140 #define DUMP_SIZE(t) printf("sizeof(" #t ") = %u\n", (unsigned)sizeof(t));
141         DUMP_SIZE(struct rte_mbuf);
142         DUMP_SIZE(struct rte_mempool);
143         DUMP_SIZE(struct rte_ring);
144 #undef DUMP_SIZE
145 }
146
147 static void cmd_dump_parsed(void *parsed_result,
148                             __attribute__((unused)) struct cmdline *cl,
149                             __attribute__((unused)) void *data)
150 {
151         struct cmd_dump_result *res = parsed_result;
152
153         if (!strcmp(res->dump, "dump_physmem"))
154                 rte_dump_physmem_layout(stdout);
155         else if (!strcmp(res->dump, "dump_memzone"))
156                 rte_memzone_dump(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         else if (!strcmp(res->dump, "dump_log_types"))
166                 rte_log_dump(stdout);
167         else if (!strcmp(res->dump, "dump_malloc_stats"))
168                 rte_malloc_dump_stats(stdout, NULL);
169 }
170
171 cmdline_parse_token_string_t cmd_dump_dump =
172         TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
173                                  "dump_physmem#"
174                                  "dump_memzone#"
175                                  "dump_struct_sizes#"
176                                  "dump_ring#"
177                                  "dump_mempool#"
178                                  "dump_malloc_stats#"
179                                  "dump_devargs#"
180                                  "dump_log_types");
181
182 cmdline_parse_inst_t cmd_dump = {
183         .f = cmd_dump_parsed,  /* function to call */
184         .data = NULL,      /* 2nd arg of func */
185         .help_str = "dump status",
186         .tokens = {        /* token list, NULL terminated */
187                 (void *)&cmd_dump_dump,
188                 NULL,
189         },
190 };
191
192 /****************/
193
194 struct cmd_dump_one_result {
195         cmdline_fixed_string_t dump;
196         cmdline_fixed_string_t name;
197 };
198
199 static void cmd_dump_one_parsed(void *parsed_result, struct cmdline *cl,
200                                 __attribute__((unused)) void *data)
201 {
202         struct cmd_dump_one_result *res = parsed_result;
203
204         if (!strcmp(res->dump, "dump_ring")) {
205                 struct rte_ring *r;
206                 r = rte_ring_lookup(res->name);
207                 if (r == NULL) {
208                         cmdline_printf(cl, "Cannot find ring\n");
209                         return;
210                 }
211                 rte_ring_dump(stdout, r);
212         }
213         else if (!strcmp(res->dump, "dump_mempool")) {
214                 struct rte_mempool *mp;
215                 mp = rte_mempool_lookup(res->name);
216                 if (mp == NULL) {
217                         cmdline_printf(cl, "Cannot find mempool\n");
218                         return;
219                 }
220                 rte_mempool_dump(stdout, mp);
221         }
222 }
223
224 cmdline_parse_token_string_t cmd_dump_one_dump =
225         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, dump,
226                                  "dump_ring#dump_mempool");
227
228 cmdline_parse_token_string_t cmd_dump_one_name =
229         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, name, NULL);
230
231 cmdline_parse_inst_t cmd_dump_one = {
232         .f = cmd_dump_one_parsed,  /* function to call */
233         .data = NULL,      /* 2nd arg of func */
234         .help_str = "dump one ring/mempool: dump_ring|dump_mempool <name>",
235         .tokens = {        /* token list, NULL terminated */
236                 (void *)&cmd_dump_one_dump,
237                 (void *)&cmd_dump_one_name,
238                 NULL,
239         },
240 };
241
242 /****************/
243
244 struct cmd_quit_result {
245         cmdline_fixed_string_t quit;
246 };
247
248 static void
249 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
250                 struct cmdline *cl,
251                 __attribute__((unused)) void *data)
252 {
253         cmdline_quit(cl);
254 }
255
256 cmdline_parse_token_string_t cmd_quit_quit =
257         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
258                                  "quit");
259
260 cmdline_parse_inst_t cmd_quit = {
261         .f = cmd_quit_parsed,  /* function to call */
262         .data = NULL,      /* 2nd arg of func */
263         .help_str = "exit application",
264         .tokens = {        /* token list, NULL terminated */
265                 (void *)&cmd_quit_quit,
266                 NULL,
267         },
268 };
269
270 /****************/
271
272 struct cmd_set_rxtx_result {
273         cmdline_fixed_string_t set;
274         cmdline_fixed_string_t mode;
275 };
276
277 static void cmd_set_rxtx_parsed(void *parsed_result, struct cmdline *cl,
278                                 __attribute__((unused)) void *data)
279 {
280         struct cmd_set_rxtx_result *res = parsed_result;
281         if (test_set_rxtx_conf(res->mode) < 0)
282                 cmdline_printf(cl, "Cannot find such mode\n");
283 }
284
285 cmdline_parse_token_string_t cmd_set_rxtx_set =
286         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_result, set,
287                                  "set_rxtx_mode");
288
289 cmdline_parse_token_string_t cmd_set_rxtx_mode =
290         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_result, mode, NULL);
291
292 cmdline_parse_inst_t cmd_set_rxtx = {
293         .f = cmd_set_rxtx_parsed,  /* function to call */
294         .data = NULL,      /* 2nd arg of func */
295         .help_str = "set rxtx routine: "
296                         "set_rxtx <mode>",
297         .tokens = {        /* token list, NULL terminated */
298                 (void *)&cmd_set_rxtx_set,
299                 (void *)&cmd_set_rxtx_mode,
300                 NULL,
301         },
302 };
303
304 /****************/
305
306 struct cmd_set_rxtx_anchor {
307         cmdline_fixed_string_t set;
308         cmdline_fixed_string_t type;
309 };
310
311 static void
312 cmd_set_rxtx_anchor_parsed(void *parsed_result,
313                            struct cmdline *cl,
314                            __attribute__((unused)) void *data)
315 {
316         struct cmd_set_rxtx_anchor *res = parsed_result;
317         if (test_set_rxtx_anchor(res->type) < 0)
318                 cmdline_printf(cl, "Cannot find such anchor\n");
319 }
320
321 cmdline_parse_token_string_t cmd_set_rxtx_anchor_set =
322         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_anchor, set,
323                                  "set_rxtx_anchor");
324
325 cmdline_parse_token_string_t cmd_set_rxtx_anchor_type =
326         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_anchor, type, NULL);
327
328 cmdline_parse_inst_t cmd_set_rxtx_anchor = {
329         .f = cmd_set_rxtx_anchor_parsed,  /* function to call */
330         .data = NULL,      /* 2nd arg of func */
331         .help_str = "set rxtx anchor: "
332                         "set_rxtx_anchor <type>",
333         .tokens = {        /* token list, NULL terminated */
334                 (void *)&cmd_set_rxtx_anchor_set,
335                 (void *)&cmd_set_rxtx_anchor_type,
336                 NULL,
337         },
338 };
339
340 /****************/
341
342 /* for stream control */
343 struct cmd_set_rxtx_sc {
344         cmdline_fixed_string_t set;
345         cmdline_fixed_string_t type;
346 };
347
348 static void
349 cmd_set_rxtx_sc_parsed(void *parsed_result,
350                            struct cmdline *cl,
351                            __attribute__((unused)) void *data)
352 {
353         struct cmd_set_rxtx_sc *res = parsed_result;
354         if (test_set_rxtx_sc(res->type) < 0)
355                 cmdline_printf(cl, "Cannot find such stream control\n");
356 }
357
358 cmdline_parse_token_string_t cmd_set_rxtx_sc_set =
359         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_sc, set,
360                                  "set_rxtx_sc");
361
362 cmdline_parse_token_string_t cmd_set_rxtx_sc_type =
363         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_sc, type, NULL);
364
365 cmdline_parse_inst_t cmd_set_rxtx_sc = {
366         .f = cmd_set_rxtx_sc_parsed,  /* function to call */
367         .data = NULL,      /* 2nd arg of func */
368         .help_str = "set rxtx stream control: "
369                         "set_rxtx_sc <type>",
370         .tokens = {        /* token list, NULL terminated */
371                 (void *)&cmd_set_rxtx_sc_set,
372                 (void *)&cmd_set_rxtx_sc_type,
373                 NULL,
374         },
375 };
376
377 /****************/
378
379
380 cmdline_parse_ctx_t main_ctx[] = {
381         (cmdline_parse_inst_t *)&cmd_autotest,
382         (cmdline_parse_inst_t *)&cmd_dump,
383         (cmdline_parse_inst_t *)&cmd_dump_one,
384         (cmdline_parse_inst_t *)&cmd_quit,
385         (cmdline_parse_inst_t *)&cmd_set_rxtx,
386         (cmdline_parse_inst_t *)&cmd_set_rxtx_anchor,
387         (cmdline_parse_inst_t *)&cmd_set_rxtx_sc,
388         NULL,
389 };
390
391 int commands_init(void)
392 {
393         struct test_command *t;
394         char *commands, *ptr;
395         int commands_len = 0;
396
397         TAILQ_FOREACH(t, &commands_list, next) {
398                 commands_len += strlen(t->command) + 1;
399         }
400
401         commands = malloc(commands_len + 1);
402         if (!commands)
403                 return -1;
404
405         ptr = commands;
406         TAILQ_FOREACH(t, &commands_list, next) {
407                 ptr += sprintf(ptr, "%s#", t->command);
408         }
409         ptr--;
410         ptr[0] = '\0';
411
412         cmd_autotest_autotest.string_data.str = commands;
413         return 0;
414 }