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