add FreeBSD support
[dpdk.git] / app / test / commands.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <netinet/in.h>
40 #include <termios.h>
41 #ifndef __linux__
42 #ifndef __FreeBSD__
43 #include <net/socket.h>
44 #endif
45 #endif
46 #include <inttypes.h>
47 #include <errno.h>
48 #include <sys/queue.h>
49
50 #include <rte_common.h>
51 #include <rte_log.h>
52 #include <rte_debug.h>
53 #include <rte_memory.h>
54 #include <rte_memcpy.h>
55 #include <rte_memzone.h>
56 #include <rte_launch.h>
57 #include <rte_cycles.h>
58 #include <rte_tailq.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_timer.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 struct cmd_autotest_result {
81         cmdline_fixed_string_t autotest;
82 };
83
84 static void cmd_autotest_parsed(void *parsed_result,
85                                 __attribute__((unused)) struct cmdline *cl,
86                                 __attribute__((unused)) void *data)
87 {
88         struct cmd_autotest_result *res = parsed_result;
89         int ret = 0;
90         int all = 0;
91
92         if (!strcmp(res->autotest, "all_autotests"))
93                 all = 1;
94
95         if (all || !strcmp(res->autotest, "version_autotest"))
96                 ret |= test_version();
97         if (all || !strcmp(res->autotest, "eal_fs_autotest"))
98                 ret |= test_eal_fs();
99         if (all || !strcmp(res->autotest, "debug_autotest"))
100                 ret |= test_debug();
101         if (all || !strcmp(res->autotest, "pci_autotest"))
102                 ret |= test_pci();
103         if (all || !strcmp(res->autotest, "prefetch_autotest"))
104                 ret |= test_prefetch();
105         if (all || !strcmp(res->autotest, "byteorder_autotest"))
106                 ret |= test_byteorder();
107         if (all || !strcmp(res->autotest, "per_lcore_autotest"))
108                 ret |= test_per_lcore();
109         if (all || !strcmp(res->autotest, "atomic_autotest"))
110                 ret |= test_atomic();
111         if (all || !strcmp(res->autotest, "malloc_autotest"))
112                 ret |= test_malloc();
113         if (all || !strcmp(res->autotest, "spinlock_autotest"))
114                 ret |= test_spinlock();
115         if (all || !strcmp(res->autotest, "memory_autotest"))
116                 ret |= test_memory();
117         if (all || !strcmp(res->autotest, "memzone_autotest"))
118                 ret |= test_memzone();
119         if (all || !strcmp(res->autotest, "rwlock_autotest"))
120                 ret |= test_rwlock();
121         if (all || !strcmp(res->autotest, "mbuf_autotest"))
122                 ret |= test_mbuf();
123         if (all || !strcmp(res->autotest, "logs_autotest"))
124                 ret |= test_logs();
125         if (all || !strcmp(res->autotest, "errno_autotest"))
126                 ret |= test_errno();
127         if (all || !strcmp(res->autotest, "hash_autotest"))
128                 ret |= test_hash();
129         if (all || !strcmp(res->autotest, "hash_perf_autotest"))
130                 ret |= test_hash_perf();
131         if (all || !strcmp(res->autotest, "lpm_autotest"))
132                 ret |= test_lpm();
133         if (all || !strcmp(res->autotest, "lpm6_autotest"))
134                 ret |= test_lpm6();
135         if (all || !strcmp(res->autotest, "cpuflags_autotest"))
136                 ret |= test_cpuflags();
137         if (all || !strcmp(res->autotest, "cmdline_autotest"))
138                 ret |= test_cmdline();
139         /* tailq autotest must go after all lpm and hashs tests or any other
140          * tests which need to create tailq objects (ring and mempool are implicitly
141          * created in earlier tests so can go later)
142          */
143         if (all || !strcmp(res->autotest, "tailq_autotest"))
144                 ret |= test_tailq();
145         if (all || !strcmp(res->autotest, "multiprocess_autotest"))
146                 ret |= test_mp_secondary();
147         if (all || !strcmp(res->autotest, "memcpy_autotest"))
148                 ret |= test_memcpy();
149         if (all || !strcmp(res->autotest, "string_autotest"))
150                 ret |= test_string_fns();
151         if (all || !strcmp(res->autotest, "eal_flags_autotest"))
152                 ret |= test_eal_flags();
153         if (all || !strcmp(res->autotest, "alarm_autotest"))
154                 ret |= test_alarm();
155         if (all || !strcmp(res->autotest, "interrupt_autotest"))
156                 ret |= test_interrupt();
157         if (all || !strcmp(res->autotest, "cycles_autotest"))
158                 ret |= test_cycles();
159         if (all || !strcmp(res->autotest, "ring_autotest"))
160                 ret |= test_ring();
161         if (all || !strcmp(res->autotest, "ring_perf_autotest"))
162                 ret |= test_ring_perf();
163         if (all || !strcmp(res->autotest, "timer_autotest"))
164                 ret |= test_timer();
165         if (all || !strcmp(res->autotest, "timer_perf_autotest"))
166                 ret |= test_timer_perf();
167         if (all || !strcmp(res->autotest, "mempool_autotest"))
168                 ret |= test_mempool();
169         if (all || !strcmp(res->autotest, "mempool_perf_autotest"))
170                 ret |= test_mempool_perf();
171         if (all || !strcmp(res->autotest, "memcpy_perf_autotest"))
172                 ret |= test_memcpy_perf();
173         if (all || !strcmp(res->autotest, "func_reentrancy_autotest"))
174                 ret |= test_func_reentrancy();
175         if (all || !strcmp(res->autotest, "red_autotest"))
176                 ret |= test_red();
177         if (all || !strcmp(res->autotest, "sched_autotest"))
178                 ret |= test_sched();
179         if (all || !strcmp(res->autotest, "meter_autotest"))
180                 ret |= test_meter();
181         if (all || !strcmp(res->autotest, "kni_autotest"))
182                 ret |= test_kni();
183         if (all || !strcmp(res->autotest, "power_autotest"))
184                 ret |= test_power();
185         if (all || !strcmp(res->autotest, "common_autotest"))
186                 ret |= test_common();
187 #ifdef RTE_LIBRTE_PMD_RING
188         if (all || !strcmp(res->autotest, "ring_pmd_autotest"))
189                 ret |= test_pmd_ring();
190 #endif /* RTE_LIBRTE_PMD_RING */
191
192 #ifdef RTE_LIBRTE_ACL
193         if (all || !strcmp(res->autotest, "acl_autotest"))
194                 ret |= test_acl();
195 #endif /* RTE_LIBRTE_ACL */
196
197         if (ret == 0)
198                 printf("Test OK\n");
199         else
200                 printf("Test Failed\n");
201         fflush(stdout);
202 }
203
204 cmdline_parse_token_string_t cmd_autotest_autotest =
205         TOKEN_STRING_INITIALIZER(struct cmd_autotest_result, autotest,
206                         "pci_autotest#memory_autotest#"
207                         "per_lcore_autotest#spinlock_autotest#"
208                         "rwlock_autotest#atomic_autotest#"
209                         "byteorder_autotest#prefetch_autotest#"
210                         "cycles_autotest#logs_autotest#"
211                         "memzone_autotest#ring_autotest#"
212                         "mempool_autotest#mbuf_autotest#"
213                         "timer_autotest#malloc_autotest#"
214                         "memcpy_autotest#hash_autotest#"
215                         "lpm_autotest#debug_autotest#"
216                         "lpm6_autotest#debug_autotest#"
217                         "errno_autotest#tailq_autotest#"
218                         "string_autotest#multiprocess_autotest#"
219                         "cpuflags_autotest#eal_flags_autotest#"
220                         "alarm_autotest#interrupt_autotest#"
221                         "version_autotest#eal_fs_autotest#"
222                         "cmdline_autotest#func_reentrancy_autotest#"
223                         "mempool_perf_autotest#hash_perf_autotest#"
224                         "memcpy_perf_autotest#ring_perf_autotest#"
225                         "red_autotest#meter_autotest#sched_autotest#"
226                         "memcpy_perf_autotest#kni_autotest#"
227                         "pm_autotest#"
228 #ifdef RTE_LIBRTE_ACL
229                         "acl_autotest#"
230 #endif
231                         "power_autotest#"
232                         "timer_perf_autotest#"
233 #ifdef RTE_LIBRTE_PMD_RING
234                         "ring_pmd_autotest#"
235 #endif
236                         "common_autotest#all_autotests");
237
238 cmdline_parse_inst_t cmd_autotest = {
239         .f = cmd_autotest_parsed,  /* function to call */
240         .data = NULL,      /* 2nd arg of func */
241         .help_str = "launch autotest",
242         .tokens = {        /* token list, NULL terminated */
243                 (void *)&cmd_autotest_autotest,
244                 NULL,
245         },
246 };
247
248 /****************/
249
250 struct cmd_dump_result {
251         cmdline_fixed_string_t dump;
252 };
253
254 static void
255 dump_struct_sizes(void)
256 {
257 #define DUMP_SIZE(t) printf("sizeof(" #t ") = %u\n", (unsigned)sizeof(t));
258         DUMP_SIZE(struct rte_mbuf);
259         DUMP_SIZE(struct rte_pktmbuf);
260         DUMP_SIZE(struct rte_ctrlmbuf);
261         DUMP_SIZE(struct rte_mempool);
262         DUMP_SIZE(struct rte_ring);
263 #undef DUMP_SIZE
264 }
265
266 static void cmd_dump_parsed(void *parsed_result,
267                             __attribute__((unused)) struct cmdline *cl,
268                             __attribute__((unused)) void *data)
269 {
270         struct cmd_dump_result *res = parsed_result;
271
272         if (!strcmp(res->dump, "dump_physmem"))
273                 rte_dump_physmem_layout();
274         else if (!strcmp(res->dump, "dump_memzone"))
275                 rte_memzone_dump();
276         else if (!strcmp(res->dump, "dump_log_history"))
277                 rte_log_dump_history();
278         else if (!strcmp(res->dump, "dump_struct_sizes"))
279                 dump_struct_sizes();
280         else if (!strcmp(res->dump, "dump_ring"))
281                 rte_ring_list_dump();
282         else if (!strcmp(res->dump, "dump_mempool"))
283                 rte_mempool_list_dump();
284 }
285
286 cmdline_parse_token_string_t cmd_dump_dump =
287         TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
288                                  "dump_physmem#dump_memzone#dump_log_history#"
289                                  "dump_struct_sizes#dump_ring#dump_mempool");
290
291 cmdline_parse_inst_t cmd_dump = {
292         .f = cmd_dump_parsed,  /* function to call */
293         .data = NULL,      /* 2nd arg of func */
294         .help_str = "dump status",
295         .tokens = {        /* token list, NULL terminated */
296                 (void *)&cmd_dump_dump,
297                 NULL,
298         },
299 };
300
301 /****************/
302
303 struct cmd_dump_one_result {
304         cmdline_fixed_string_t dump;
305         cmdline_fixed_string_t name;
306 };
307
308 static void cmd_dump_one_parsed(void *parsed_result, struct cmdline *cl,
309                                 __attribute__((unused)) void *data)
310 {
311         struct cmd_dump_one_result *res = parsed_result;
312
313         if (!strcmp(res->dump, "dump_ring")) {
314                 struct rte_ring *r;
315                 r = rte_ring_lookup(res->name);
316                 if (r == NULL) {
317                         cmdline_printf(cl, "Cannot find ring\n");
318                         return;
319                 }
320                 rte_ring_dump(r);
321         }
322         else if (!strcmp(res->dump, "dump_mempool")) {
323                 struct rte_mempool *mp;
324                 mp = rte_mempool_lookup(res->name);
325                 if (mp == NULL) {
326                         cmdline_printf(cl, "Cannot find mempool\n");
327                         return;
328                 }
329                 rte_mempool_dump(mp);
330         }
331 }
332
333 cmdline_parse_token_string_t cmd_dump_one_dump =
334         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, dump,
335                                  "dump_ring#dump_mempool");
336
337 cmdline_parse_token_string_t cmd_dump_one_name =
338         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, name, NULL);
339
340 cmdline_parse_inst_t cmd_dump_one = {
341         .f = cmd_dump_one_parsed,  /* function to call */
342         .data = NULL,      /* 2nd arg of func */
343         .help_str = "dump one ring/mempool: dump_ring|dump_mempool <name>",
344         .tokens = {        /* token list, NULL terminated */
345                 (void *)&cmd_dump_one_dump,
346                 (void *)&cmd_dump_one_name,
347                 NULL,
348         },
349 };
350
351 /****************/
352
353 struct cmd_set_ring_result {
354         cmdline_fixed_string_t set;
355         cmdline_fixed_string_t name;
356         uint32_t value;
357 };
358
359 static void cmd_set_ring_parsed(void *parsed_result, struct cmdline *cl,
360                                 __attribute__((unused)) void *data)
361 {
362         struct cmd_set_ring_result *res = parsed_result;
363         struct rte_ring *r;
364         int ret;
365
366         r = rte_ring_lookup(res->name);
367         if (r == NULL) {
368                 cmdline_printf(cl, "Cannot find ring\n");
369                 return;
370         }
371
372         if (!strcmp(res->set, "set_watermark")) {
373                 ret = rte_ring_set_water_mark(r, res->value);
374                 if (ret != 0)
375                         cmdline_printf(cl, "Cannot set water mark\n");
376         }
377 }
378
379 cmdline_parse_token_string_t cmd_set_ring_set =
380         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, set,
381                                  "set_watermark");
382
383 cmdline_parse_token_string_t cmd_set_ring_name =
384         TOKEN_STRING_INITIALIZER(struct cmd_set_ring_result, name, NULL);
385
386 cmdline_parse_token_num_t cmd_set_ring_value =
387         TOKEN_NUM_INITIALIZER(struct cmd_set_ring_result, value, UINT32);
388
389 cmdline_parse_inst_t cmd_set_ring = {
390         .f = cmd_set_ring_parsed,  /* function to call */
391         .data = NULL,      /* 2nd arg of func */
392         .help_str = "set watermark: "
393                         "set_watermark <ring_name> <value>",
394         .tokens = {        /* token list, NULL terminated */
395                 (void *)&cmd_set_ring_set,
396                 (void *)&cmd_set_ring_name,
397                 (void *)&cmd_set_ring_value,
398                 NULL,
399         },
400 };
401
402 /****************/
403
404 struct cmd_quit_result {
405         cmdline_fixed_string_t quit;
406 };
407
408 static void
409 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
410                 struct cmdline *cl,
411                 __attribute__((unused)) void *data)
412 {
413         cmdline_quit(cl);
414 }
415
416 cmdline_parse_token_string_t cmd_quit_quit =
417         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
418                                  "quit");
419
420 cmdline_parse_inst_t cmd_quit = {
421         .f = cmd_quit_parsed,  /* function to call */
422         .data = NULL,      /* 2nd arg of func */
423         .help_str = "exit application",
424         .tokens = {        /* token list, NULL terminated */
425                 (void *)&cmd_quit_quit,
426                 NULL,
427         },
428 };
429
430 /****************/
431
432 cmdline_parse_ctx_t main_ctx[] = {
433         (cmdline_parse_inst_t *)&cmd_autotest,
434         (cmdline_parse_inst_t *)&cmd_dump,
435         (cmdline_parse_inst_t *)&cmd_dump_one,
436         (cmdline_parse_inst_t *)&cmd_set_ring,
437         (cmdline_parse_inst_t *)&cmd_quit,
438         NULL,
439 };
440