test prog
[dpdk.git] / app / test / commands.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <netinet/in.h>
12 #include <termios.h>
13 #include <inttypes.h>
14 #include <errno.h>
15 #include <sys/queue.h>
16
17 #include <rte_common.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_memzone.h>
23 #include <rte_launch.h>
24 #include <rte_cycles.h>
25 #include <rte_eal.h>
26 #include <rte_per_lcore.h>
27 #include <rte_lcore.h>
28 #include <rte_atomic.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_ring.h>
31 #include <rte_malloc.h>
32 #include <rte_mempool.h>
33 #include <rte_mbuf.h>
34 #include <rte_devargs.h>
35
36 #include <cmdline_rdline.h>
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_ipaddr.h>
39 #include <cmdline_parse_num.h>
40 #include <cmdline_parse_string.h>
41 #include <cmdline.h>
42 #include <rte_string_fns.h>
43
44 #include "test.h"
45
46 /****************/
47
48 static struct test_commands_list commands_list =
49         TAILQ_HEAD_INITIALIZER(commands_list);
50
51 void
52 add_test_command(struct test_command *t)
53 {
54         TAILQ_INSERT_TAIL(&commands_list, t, next);
55 }
56
57 struct cmd_autotest_result {
58         cmdline_fixed_string_t autotest;
59 };
60
61 static void cmd_autotest_parsed(void *parsed_result,
62                                 __attribute__((unused)) struct cmdline *cl,
63                                 __attribute__((unused)) void *data)
64 {
65         struct test_command *t;
66         struct cmd_autotest_result *res = parsed_result;
67         int ret = 0;
68
69         TAILQ_FOREACH(t, &commands_list, next) {
70                 if (!strcmp(res->autotest, t->command))
71                         ret = t->callback();
72         }
73
74         last_test_result = ret;
75         if (ret == 0)
76                 printf("Test OK\n");
77         else if (ret == TEST_SKIPPED)
78                 printf("Test Skipped\n");
79         else
80                 printf("Test Failed\n");
81         fflush(stdout);
82 }
83
84 cmdline_parse_token_string_t cmd_autotest_autotest =
85         TOKEN_STRING_INITIALIZER(struct cmd_autotest_result, autotest,
86                                  "");
87
88 cmdline_parse_inst_t cmd_autotest = {
89         .f = cmd_autotest_parsed,  /* function to call */
90         .data = NULL,      /* 2nd arg of func */
91         .help_str = "launch autotest",
92         .tokens = {        /* token list, NULL terminated */
93                 (void *)&cmd_autotest_autotest,
94                 NULL,
95         },
96 };
97
98 /****************/
99
100 struct cmd_dump_result {
101         cmdline_fixed_string_t dump;
102 };
103
104 static void
105 dump_struct_sizes(void)
106 {
107 #define DUMP_SIZE(t) printf("sizeof(" #t ") = %u\n", (unsigned)sizeof(t));
108         DUMP_SIZE(struct rte_mbuf);
109         DUMP_SIZE(struct rte_mempool);
110         DUMP_SIZE(struct rte_ring);
111 #undef DUMP_SIZE
112 }
113
114 static void cmd_dump_parsed(void *parsed_result,
115                             __attribute__((unused)) struct cmdline *cl,
116                             __attribute__((unused)) void *data)
117 {
118         struct cmd_dump_result *res = parsed_result;
119
120         if (!strcmp(res->dump, "dump_physmem"))
121                 rte_dump_physmem_layout(stdout);
122         else if (!strcmp(res->dump, "dump_memzone"))
123                 rte_memzone_dump(stdout);
124         else if (!strcmp(res->dump, "dump_struct_sizes"))
125                 dump_struct_sizes();
126         else if (!strcmp(res->dump, "dump_ring"))
127                 rte_ring_list_dump(stdout);
128         else if (!strcmp(res->dump, "dump_mempool"))
129                 rte_mempool_list_dump(stdout);
130         else if (!strcmp(res->dump, "dump_devargs"))
131                 rte_devargs_dump(stdout);
132         else if (!strcmp(res->dump, "dump_log_types"))
133                 rte_log_dump(stdout);
134         else if (!strcmp(res->dump, "dump_malloc_stats"))
135                 rte_malloc_dump_stats(stdout, NULL);
136         else if (!strcmp(res->dump, "dump_malloc_heaps"))
137                 rte_malloc_dump_heaps(stdout);
138         else if (!strcmp(res->dump, "pouet")) {
139                 struct rte_mempool *mbuf_pool;
140                 unsigned mbuf_pool_size = 2097151;
141
142                 printf("Creating mbuf pool size=%u\n", mbuf_pool_size);
143                 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", mbuf_pool_size,
144                                                 256, 0, RTE_MBUF_DEFAULT_BUF_SIZE, 0);
145
146                 printf("mbuf_pool %p\n", mbuf_pool);
147         }
148 }
149
150 cmdline_parse_token_string_t cmd_dump_dump =
151         TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
152                                  "dump_physmem#"
153                                  "dump_memzone#"
154                                  "dump_struct_sizes#"
155                                  "dump_ring#"
156                                  "dump_mempool#"
157                                  "dump_malloc_stats#"
158                                  "dump_malloc_heaps#"
159                                  "dump_devargs#"
160                                  "dump_log_types#pouet");
161
162 cmdline_parse_inst_t cmd_dump = {
163         .f = cmd_dump_parsed,  /* function to call */
164         .data = NULL,      /* 2nd arg of func */
165         .help_str = "dump status",
166         .tokens = {        /* token list, NULL terminated */
167                 (void *)&cmd_dump_dump,
168                 NULL,
169         },
170 };
171
172 /****************/
173
174 struct cmd_dump_one_result {
175         cmdline_fixed_string_t dump;
176         cmdline_fixed_string_t name;
177 };
178
179 static void cmd_dump_one_parsed(void *parsed_result, struct cmdline *cl,
180                                 __attribute__((unused)) void *data)
181 {
182         struct cmd_dump_one_result *res = parsed_result;
183
184         if (!strcmp(res->dump, "dump_ring")) {
185                 struct rte_ring *r;
186                 r = rte_ring_lookup(res->name);
187                 if (r == NULL) {
188                         cmdline_printf(cl, "Cannot find ring\n");
189                         return;
190                 }
191                 rte_ring_dump(stdout, r);
192         }
193         else if (!strcmp(res->dump, "dump_mempool")) {
194                 struct rte_mempool *mp;
195                 mp = rte_mempool_lookup(res->name);
196                 if (mp == NULL) {
197                         cmdline_printf(cl, "Cannot find mempool\n");
198                         return;
199                 }
200                 rte_mempool_dump(stdout, mp);
201         }
202 }
203
204 cmdline_parse_token_string_t cmd_dump_one_dump =
205         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, dump,
206                                  "dump_ring#dump_mempool");
207
208 cmdline_parse_token_string_t cmd_dump_one_name =
209         TOKEN_STRING_INITIALIZER(struct cmd_dump_one_result, name, NULL);
210
211 cmdline_parse_inst_t cmd_dump_one = {
212         .f = cmd_dump_one_parsed,  /* function to call */
213         .data = NULL,      /* 2nd arg of func */
214         .help_str = "dump one ring/mempool: dump_ring|dump_mempool <name>",
215         .tokens = {        /* token list, NULL terminated */
216                 (void *)&cmd_dump_one_dump,
217                 (void *)&cmd_dump_one_name,
218                 NULL,
219         },
220 };
221
222 /****************/
223
224 struct cmd_quit_result {
225         cmdline_fixed_string_t quit;
226 };
227
228 static void
229 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
230                 struct cmdline *cl,
231                 __attribute__((unused)) void *data)
232 {
233         cmdline_quit(cl);
234 }
235
236 cmdline_parse_token_string_t cmd_quit_quit =
237         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
238                                  "quit");
239
240 cmdline_parse_inst_t cmd_quit = {
241         .f = cmd_quit_parsed,  /* function to call */
242         .data = NULL,      /* 2nd arg of func */
243         .help_str = "exit application",
244         .tokens = {        /* token list, NULL terminated */
245                 (void *)&cmd_quit_quit,
246                 NULL,
247         },
248 };
249
250 /****************/
251
252 struct cmd_set_rxtx_result {
253         cmdline_fixed_string_t set;
254         cmdline_fixed_string_t mode;
255 };
256
257 static void cmd_set_rxtx_parsed(void *parsed_result, struct cmdline *cl,
258                                 __attribute__((unused)) void *data)
259 {
260         struct cmd_set_rxtx_result *res = parsed_result;
261         if (test_set_rxtx_conf(res->mode) < 0)
262                 cmdline_printf(cl, "Cannot find such mode\n");
263 }
264
265 cmdline_parse_token_string_t cmd_set_rxtx_set =
266         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_result, set,
267                                  "set_rxtx_mode");
268
269 cmdline_parse_token_string_t cmd_set_rxtx_mode =
270         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_result, mode, NULL);
271
272 cmdline_parse_inst_t cmd_set_rxtx = {
273         .f = cmd_set_rxtx_parsed,  /* function to call */
274         .data = NULL,      /* 2nd arg of func */
275         .help_str = "set rxtx routine: "
276                         "set_rxtx <mode>",
277         .tokens = {        /* token list, NULL terminated */
278                 (void *)&cmd_set_rxtx_set,
279                 (void *)&cmd_set_rxtx_mode,
280                 NULL,
281         },
282 };
283
284 /****************/
285
286 struct cmd_set_rxtx_anchor {
287         cmdline_fixed_string_t set;
288         cmdline_fixed_string_t type;
289 };
290
291 static void
292 cmd_set_rxtx_anchor_parsed(void *parsed_result,
293                            struct cmdline *cl,
294                            __attribute__((unused)) void *data)
295 {
296         struct cmd_set_rxtx_anchor *res = parsed_result;
297         if (test_set_rxtx_anchor(res->type) < 0)
298                 cmdline_printf(cl, "Cannot find such anchor\n");
299 }
300
301 cmdline_parse_token_string_t cmd_set_rxtx_anchor_set =
302         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_anchor, set,
303                                  "set_rxtx_anchor");
304
305 cmdline_parse_token_string_t cmd_set_rxtx_anchor_type =
306         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_anchor, type, NULL);
307
308 cmdline_parse_inst_t cmd_set_rxtx_anchor = {
309         .f = cmd_set_rxtx_anchor_parsed,  /* function to call */
310         .data = NULL,      /* 2nd arg of func */
311         .help_str = "set rxtx anchor: "
312                         "set_rxtx_anchor <type>",
313         .tokens = {        /* token list, NULL terminated */
314                 (void *)&cmd_set_rxtx_anchor_set,
315                 (void *)&cmd_set_rxtx_anchor_type,
316                 NULL,
317         },
318 };
319
320 /****************/
321
322 /* for stream control */
323 struct cmd_set_rxtx_sc {
324         cmdline_fixed_string_t set;
325         cmdline_fixed_string_t type;
326 };
327
328 static void
329 cmd_set_rxtx_sc_parsed(void *parsed_result,
330                            struct cmdline *cl,
331                            __attribute__((unused)) void *data)
332 {
333         struct cmd_set_rxtx_sc *res = parsed_result;
334         if (test_set_rxtx_sc(res->type) < 0)
335                 cmdline_printf(cl, "Cannot find such stream control\n");
336 }
337
338 cmdline_parse_token_string_t cmd_set_rxtx_sc_set =
339         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_sc, set,
340                                  "set_rxtx_sc");
341
342 cmdline_parse_token_string_t cmd_set_rxtx_sc_type =
343         TOKEN_STRING_INITIALIZER(struct cmd_set_rxtx_sc, type, NULL);
344
345 cmdline_parse_inst_t cmd_set_rxtx_sc = {
346         .f = cmd_set_rxtx_sc_parsed,  /* function to call */
347         .data = NULL,      /* 2nd arg of func */
348         .help_str = "set rxtx stream control: "
349                         "set_rxtx_sc <type>",
350         .tokens = {        /* token list, NULL terminated */
351                 (void *)&cmd_set_rxtx_sc_set,
352                 (void *)&cmd_set_rxtx_sc_type,
353                 NULL,
354         },
355 };
356
357 /****************/
358
359
360 cmdline_parse_ctx_t main_ctx[] = {
361         (cmdline_parse_inst_t *)&cmd_autotest,
362         (cmdline_parse_inst_t *)&cmd_dump,
363         (cmdline_parse_inst_t *)&cmd_dump_one,
364         (cmdline_parse_inst_t *)&cmd_quit,
365         (cmdline_parse_inst_t *)&cmd_set_rxtx,
366         (cmdline_parse_inst_t *)&cmd_set_rxtx_anchor,
367         (cmdline_parse_inst_t *)&cmd_set_rxtx_sc,
368         NULL,
369 };
370
371 int commands_init(void)
372 {
373         struct test_command *t;
374         char *commands;
375         int commands_len = 0;
376
377         TAILQ_FOREACH(t, &commands_list, next) {
378                 commands_len += strlen(t->command) + 1;
379         }
380
381         commands = (char *)calloc(commands_len, sizeof(char));
382         if (!commands)
383                 return -1;
384
385         TAILQ_FOREACH(t, &commands_list, next) {
386                 strlcat(commands, t->command, commands_len);
387                 if (TAILQ_NEXT(t, next) != NULL)
388                         strlcat(commands, "#", commands_len);
389         }
390
391         cmd_autotest_autotest.string_data.str = commands;
392         return 0;
393 }