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