a1ffd43b6daa7d00560279464a0ba1bb54b97bcf
[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 (!strcmp(res->autotest, "version_autotest"))
108                 ret = test_version();
109         if (!strcmp(res->autotest, "eal_fs_autotest"))
110                 ret = test_eal_fs();
111         if (!strcmp(res->autotest, "debug_autotest"))
112                 ret = test_debug();
113         if (!strcmp(res->autotest, "pci_autotest"))
114                 ret = test_pci();
115         if (!strcmp(res->autotest, "prefetch_autotest"))
116                 ret = test_prefetch();
117         if (!strcmp(res->autotest, "byteorder_autotest"))
118                 ret = test_byteorder();
119         if (!strcmp(res->autotest, "per_lcore_autotest"))
120                 ret = test_per_lcore();
121         if (!strcmp(res->autotest, "atomic_autotest"))
122                 ret = test_atomic();
123         if (!strcmp(res->autotest, "malloc_autotest"))
124                 ret = test_malloc();
125         if (!strcmp(res->autotest, "spinlock_autotest"))
126                 ret = test_spinlock();
127         if (!strcmp(res->autotest, "memory_autotest"))
128                 ret = test_memory();
129         if (!strcmp(res->autotest, "memzone_autotest"))
130                 ret = test_memzone();
131         if (!strcmp(res->autotest, "rwlock_autotest"))
132                 ret = test_rwlock();
133         if (!strcmp(res->autotest, "mbuf_autotest"))
134                 ret = test_mbuf();
135         if (!strcmp(res->autotest, "logs_autotest"))
136                 ret = test_logs();
137         if (!strcmp(res->autotest, "errno_autotest"))
138                 ret = test_errno();
139         if (!strcmp(res->autotest, "hash_autotest"))
140                 ret = test_hash();
141         if (!strcmp(res->autotest, "hash_perf_autotest"))
142                 ret = test_hash_perf();
143         if (!strcmp(res->autotest, "lpm_autotest"))
144                 ret = test_lpm();
145         if (!strcmp(res->autotest, "lpm6_autotest"))
146                 ret = test_lpm6();
147         if (!strcmp(res->autotest, "cpuflags_autotest"))
148                 ret = test_cpuflags();
149         if (!strcmp(res->autotest, "cmdline_autotest"))
150                 ret = test_cmdline();
151         if (!strcmp(res->autotest, "tailq_autotest"))
152                 ret = test_tailq();
153         if (!strcmp(res->autotest, "multiprocess_autotest"))
154                 ret = test_mp_secondary();
155         if (!strcmp(res->autotest, "memcpy_autotest"))
156                 ret = test_memcpy();
157         if (!strcmp(res->autotest, "string_autotest"))
158                 ret = test_string_fns();
159         if (!strcmp(res->autotest, "eal_flags_autotest"))
160                 ret = test_eal_flags();
161         if (!strcmp(res->autotest, "alarm_autotest"))
162                 ret = test_alarm();
163         if (!strcmp(res->autotest, "interrupt_autotest"))
164                 ret = test_interrupt();
165         if (!strcmp(res->autotest, "cycles_autotest"))
166                 ret = test_cycles();
167         if (!strcmp(res->autotest, "ring_autotest"))
168                 ret = test_ring();
169         if (!strcmp(res->autotest, "table_autotest"))
170                 ret = test_table();
171         if (!strcmp(res->autotest, "ring_perf_autotest"))
172                 ret = test_ring_perf();
173         if (!strcmp(res->autotest, "timer_autotest"))
174                 ret = test_timer();
175         if (!strcmp(res->autotest, "timer_perf_autotest"))
176                 ret = test_timer_perf();
177 #ifdef RTE_LIBRTE_PMD_BOND
178         if (!strcmp(res->autotest, "link_bonding_autotest"))
179                 ret = test_link_bonding();
180 #endif
181         if (!strcmp(res->autotest, "mempool_autotest"))
182                 ret = test_mempool();
183         if (!strcmp(res->autotest, "mempool_perf_autotest"))
184                 ret = test_mempool_perf();
185         if (!strcmp(res->autotest, "memcpy_perf_autotest"))
186                 ret = test_memcpy_perf();
187         if (!strcmp(res->autotest, "func_reentrancy_autotest"))
188                 ret = test_func_reentrancy();
189         if (!strcmp(res->autotest, "red_autotest"))
190                 ret = test_red();
191         if (!strcmp(res->autotest, "sched_autotest"))
192                 ret = test_sched();
193         if (!strcmp(res->autotest, "meter_autotest"))
194                 ret = test_meter();
195         if (!strcmp(res->autotest, "kni_autotest"))
196                 ret = test_kni();
197         if (!strcmp(res->autotest, "power_autotest"))
198                 ret = test_power();
199         if (!strcmp(res->autotest, "common_autotest"))
200                 ret = test_common();
201         if (!strcmp(res->autotest, "ivshmem_autotest"))
202                 ret = test_ivshmem();
203         if (!strcmp(res->autotest, "distributor_autotest"))
204                 ret = test_distributor();
205         if (!strcmp(res->autotest, "distributor_perf_autotest"))
206                 ret = test_distributor_perf();
207         if (!strcmp(res->autotest, "devargs_autotest"))
208                 ret = test_devargs();
209 #ifdef RTE_LIBRTE_PMD_RING
210         if (!strcmp(res->autotest, "ring_pmd_autotest"))
211                 ret = test_pmd_ring();
212 #endif /* RTE_LIBRTE_PMD_RING */
213
214 #ifdef RTE_LIBRTE_ACL
215         if (!strcmp(res->autotest, "acl_autotest"))
216                 ret = test_acl();
217 #endif /* RTE_LIBRTE_ACL */
218 #ifdef RTE_LIBRTE_KVARGS
219         if (!strcmp(res->autotest, "kvargs_autotest"))
220                 ret |= test_kvargs();
221 #endif /* RTE_LIBRTE_KVARGS */
222
223         if (ret == 0)
224                 printf("Test OK\n");
225         else
226                 printf("Test Failed\n");
227         fflush(stdout);
228 }
229
230 cmdline_parse_token_string_t cmd_autotest_autotest =
231         TOKEN_STRING_INITIALIZER(struct cmd_autotest_result, autotest,
232                                  "");
233
234 cmdline_parse_inst_t cmd_autotest = {
235         .f = cmd_autotest_parsed,  /* function to call */
236         .data = NULL,      /* 2nd arg of func */
237         .help_str = "launch autotest",
238         .tokens = {        /* token list, NULL terminated */
239                 (void *)&cmd_autotest_autotest,
240                 NULL,
241         },
242 };
243
244 /****************/
245
246 struct cmd_dump_result {
247         cmdline_fixed_string_t dump;
248 };
249
250 static void
251 dump_struct_sizes(void)
252 {
253 #define DUMP_SIZE(t) printf("sizeof(" #t ") = %u\n", (unsigned)sizeof(t));
254         DUMP_SIZE(struct rte_mbuf);
255         DUMP_SIZE(struct rte_pktmbuf);
256         DUMP_SIZE(struct rte_ctrlmbuf);
257         DUMP_SIZE(struct rte_mempool);
258         DUMP_SIZE(struct rte_ring);
259 #undef DUMP_SIZE
260 }
261
262 static void cmd_dump_parsed(void *parsed_result,
263                             __attribute__((unused)) struct cmdline *cl,
264                             __attribute__((unused)) void *data)
265 {
266         struct cmd_dump_result *res = parsed_result;
267
268         if (!strcmp(res->dump, "dump_physmem"))
269                 rte_dump_physmem_layout(stdout);
270         else if (!strcmp(res->dump, "dump_memzone"))
271                 rte_memzone_dump(stdout);
272         else if (!strcmp(res->dump, "dump_log_history"))
273                 rte_log_dump_history(stdout);
274         else if (!strcmp(res->dump, "dump_struct_sizes"))
275                 dump_struct_sizes();
276         else if (!strcmp(res->dump, "dump_ring"))
277                 rte_ring_list_dump(stdout);
278         else if (!strcmp(res->dump, "dump_mempool"))
279                 rte_mempool_list_dump(stdout);
280         else if (!strcmp(res->dump, "dump_devargs"))
281                 rte_eal_devargs_dump(stdout);
282 }
283
284 cmdline_parse_token_string_t cmd_dump_dump =
285         TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
286                                  "dump_physmem#dump_memzone#dump_log_history#"
287                                  "dump_struct_sizes#dump_ring#dump_mempool#"
288                                  "dump_devargs");
289
290 cmdline_parse_inst_t cmd_dump = {
291         .f = cmd_dump_parsed,  /* function to call */
292         .data = NULL,      /* 2nd arg of func */
293         .help_str = "dump status",
294         .tokens = {        /* token list, NULL terminated */
295                 (void *)&cmd_dump_dump,
296                 NULL,
297         },
298 };
299
300 /****************/
301
302 struct cmd_dump_one_result {
303         cmdline_fixed_string_t dump;
304         cmdline_fixed_string_t name;
305 };
306
307 static void cmd_dump_one_parsed(void *parsed_result, struct cmdline *cl,
308                                 __attribute__((unused)) void *data)
309 {
310         struct cmd_dump_one_result *res = parsed_result;
311
312         if (!strcmp(res->dump, "dump_ring")) {
313                 struct rte_ring *r;
314                 r = rte_ring_lookup(res->name);
315                 if (r == NULL) {
316                         cmdline_printf(cl, "Cannot find ring\n");
317                         return;
318                 }
319                 rte_ring_dump(stdout, r);
320         }
321         else if (!strcmp(res->dump, "dump_mempool")) {
322                 struct rte_mempool *mp;
323                 mp = rte_mempool_lookup(res->name);
324                 if (mp == NULL) {
325                         cmdline_printf(cl, "Cannot find mempool\n");
326                         return;
327                 }
328                 rte_mempool_dump(stdout, mp);
329         }
330 }
331
332 cmdline_parse_token_string_t cmd_dump_one_dump =
333         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, dump,
334                                  "dump_ring#dump_mempool");
335
336 cmdline_parse_token_string_t cmd_dump_one_name =
337         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, name, NULL);
338
339 cmdline_parse_inst_t cmd_dump_one = {
340         .f = cmd_dump_one_parsed,  /* function to call */
341         .data = NULL,      /* 2nd arg of func */
342         .help_str = "dump one ring/mempool: dump_ring|dump_mempool <name>",
343         .tokens = {        /* token list, NULL terminated */
344                 (void *)&cmd_dump_one_dump,
345                 (void *)&cmd_dump_one_name,
346                 NULL,
347         },
348 };
349
350 /****************/
351
352 struct cmd_set_ring_result {
353         cmdline_fixed_string_t set;
354         cmdline_fixed_string_t name;
355         uint32_t value;
356 };
357
358 static void cmd_set_ring_parsed(void *parsed_result, struct cmdline *cl,
359                                 __attribute__((unused)) void *data)
360 {
361         struct cmd_set_ring_result *res = parsed_result;
362         struct rte_ring *r;
363         int ret;
364
365         r = rte_ring_lookup(res->name);
366         if (r == NULL) {
367                 cmdline_printf(cl, "Cannot find ring\n");
368                 return;
369         }
370
371         if (!strcmp(res->set, "set_watermark")) {
372                 ret = rte_ring_set_water_mark(r, res->value);
373                 if (ret != 0)
374                         cmdline_printf(cl, "Cannot set water mark\n");
375         }
376 }
377
378 cmdline_parse_token_string_t cmd_set_ring_set =
379         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, set,
380                                  "set_watermark");
381
382 cmdline_parse_token_string_t cmd_set_ring_name =
383         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, name, NULL);
384
385 cmdline_parse_token_num_t cmd_set_ring_value =
386         TOKEN_NUM_INITIALIZER(struct cmd_set_ring_result, value, UINT32);
387
388 cmdline_parse_inst_t cmd_set_ring = {
389         .f = cmd_set_ring_parsed,  /* function to call */
390         .data = NULL,      /* 2nd arg of func */
391         .help_str = "set watermark: "
392                         "set_watermark <ring_name> <value>",
393         .tokens = {        /* token list, NULL terminated */
394                 (void *)&cmd_set_ring_set,
395                 (void *)&cmd_set_ring_name,
396                 (void *)&cmd_set_ring_value,
397                 NULL,
398         },
399 };
400
401 /****************/
402
403 struct cmd_quit_result {
404         cmdline_fixed_string_t quit;
405 };
406
407 static void
408 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
409                 struct cmdline *cl,
410                 __attribute__((unused)) void *data)
411 {
412         cmdline_quit(cl);
413 }
414
415 cmdline_parse_token_string_t cmd_quit_quit =
416         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
417                                  "quit");
418
419 cmdline_parse_inst_t cmd_quit = {
420         .f = cmd_quit_parsed,  /* function to call */
421         .data = NULL,      /* 2nd arg of func */
422         .help_str = "exit application",
423         .tokens = {        /* token list, NULL terminated */
424                 (void *)&cmd_quit_quit,
425                 NULL,
426         },
427 };
428
429 /****************/
430
431 cmdline_parse_ctx_t main_ctx[] = {
432         (cmdline_parse_inst_t *)&cmd_autotest,
433         (cmdline_parse_inst_t *)&cmd_dump,
434         (cmdline_parse_inst_t *)&cmd_dump_one,
435         (cmdline_parse_inst_t *)&cmd_set_ring,
436         (cmdline_parse_inst_t *)&cmd_quit,
437         NULL,
438 };
439
440 int commands_init(void)
441 {
442         struct test_command *t;
443         char builtin_commands[] =
444                         "pci_autotest#memory_autotest#"
445                         "per_lcore_autotest#spinlock_autotest#"
446                         "rwlock_autotest#atomic_autotest#"
447                         "byteorder_autotest#prefetch_autotest#"
448                         "cycles_autotest#logs_autotest#"
449                         "memzone_autotest#ring_autotest#"
450                         "mempool_autotest#mbuf_autotest#"
451                         "timer_autotest#malloc_autotest#"
452                         "memcpy_autotest#hash_autotest#"
453                         "lpm_autotest#debug_autotest#"
454                         "lpm6_autotest#"
455                         "errno_autotest#tailq_autotest#"
456                         "string_autotest#multiprocess_autotest#"
457                         "cpuflags_autotest#eal_flags_autotest#"
458                         "alarm_autotest#interrupt_autotest#"
459                         "version_autotest#eal_fs_autotest#"
460                         "cmdline_autotest#func_reentrancy_autotest#"
461 #ifdef RTE_LIBRTE_PMD_BOND
462                         "link_bonding_autotest#"
463 #endif
464                         "mempool_perf_autotest#hash_perf_autotest#"
465                         "memcpy_perf_autotest#ring_perf_autotest#"
466                         "red_autotest#meter_autotest#sched_autotest#"
467                         "memcpy_perf_autotest#kni_autotest#"
468                         "ivshmem_autotest#"
469                         "devargs_autotest#table_autotest#"
470 #ifdef RTE_LIBRTE_ACL
471                         "acl_autotest#"
472 #endif
473                         "power_autotest#"
474                         "timer_perf_autotest#"
475 #ifdef RTE_LIBRTE_PMD_RING
476                         "ring_pmd_autotest#"
477 #endif
478 #ifdef RTE_LIBRTE_KVARGS
479                         "kvargs_autotest#"
480 #endif
481                         "common_autotest#"
482                         "distributor_autotest#distributor_perf_autotest";
483         char *commands, *ptr;
484         int commands_len = strlen(builtin_commands) + 1;
485
486         TAILQ_FOREACH(t, &commands_list, next) {
487                 commands_len += strlen(t->command) + 1;
488         }
489
490         commands = malloc(commands_len);
491         if (!commands)
492                 return -1;
493
494         ptr = commands;
495         TAILQ_FOREACH(t, &commands_list, next) {
496                 ptr += sprintf(ptr, "%s#", t->command);
497         }
498         ptr += sprintf(ptr, "%s", builtin_commands);
499
500         cmd_autotest_autotest.string_data.str = commands;
501         return 0;
502 }