app/test: rework command registration
[dpdk.git] / app / test / test_ivshmem.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 <fcntl.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/mman.h>
39 #include <sys/wait.h>
40 #include <stdio.h>
41
42 #include <cmdline_parse.h>
43
44 #include "test.h"
45
46 #include <rte_common.h>
47 #include <rte_ivshmem.h>
48 #include <rte_string_fns.h>
49 #include "process.h"
50
51 #define DUPLICATE_METADATA "duplicate"
52 #define METADATA_NAME "metadata"
53 #define NONEXISTENT_METADATA "nonexistent"
54 #define FIRST_TEST 'a'
55
56 #define launch_proc(ARGV) process_dup(ARGV, \
57                 sizeof(ARGV)/(sizeof(ARGV[0])), "test_ivshmem")
58
59 #define ASSERT(cond,msg) do {                                           \
60                 if (!(cond)) {                                                          \
61                         printf("**** TEST %s() failed: %s\n",   \
62                                 __func__, msg);                                         \
63                         return -1;                                                              \
64                 }                                                                                       \
65 } while(0)
66
67 static char*
68 get_current_prefix(char * prefix, int size)
69 {
70         char path[PATH_MAX] = {0};
71         char buf[PATH_MAX] = {0};
72
73         /* get file for config (fd is always 3) */
74         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
75
76         /* return NULL on error */
77         if (readlink(path, buf, sizeof(buf)) == -1)
78                 return NULL;
79
80         /* get the basename */
81         snprintf(buf, sizeof(buf), "%s", basename(buf));
82
83         /* copy string all the way from second char up to start of _config */
84         snprintf(prefix, size, "%.*s",
85                         (int)(strnlen(buf, sizeof(buf)) - sizeof("_config")),
86                         &buf[1]);
87
88         return prefix;
89 }
90
91 static struct rte_ivshmem_metadata*
92 mmap_metadata(const char *name)
93 {
94         int fd;
95         char pathname[PATH_MAX];
96         struct rte_ivshmem_metadata *metadata;
97
98         snprintf(pathname, sizeof(pathname),
99                         "/var/run/.dpdk_ivshmem_metadata_%s", name);
100
101         fd = open(pathname, O_RDWR, 0660);
102         if (fd < 0)
103                 return NULL;
104
105         metadata = (struct rte_ivshmem_metadata*) mmap(NULL,
106                         sizeof(struct rte_ivshmem_metadata), PROT_READ | PROT_WRITE,
107                         MAP_SHARED, fd, 0);
108
109         if (metadata == MAP_FAILED)
110                 return NULL;
111
112         close(fd);
113
114         return metadata;
115 }
116
117 static int
118 create_duplicate(void)
119 {
120         /* create a metadata that another process will then try to overwrite */
121         ASSERT (rte_ivshmem_metadata_create(DUPLICATE_METADATA) == 0,
122                         "Creating metadata failed");
123         return 0;
124 }
125
126 static int
127 test_ivshmem_create_lots_of_memzones(void)
128 {
129         int i;
130         char name[IVSHMEM_NAME_LEN];
131         const struct rte_memzone *mz;
132
133         ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
134                         "Failed to create metadata");
135
136         for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES; i++) {
137                 snprintf(name, sizeof(name), "mz_%i", i);
138
139                 mz = rte_memzone_reserve(name, RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
140                 ASSERT(mz != NULL, "Failed to reserve memzone");
141
142                 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
143                                 "Failed to add memzone");
144         }
145         mz = rte_memzone_reserve("one too many", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
146         ASSERT(mz != NULL, "Failed to reserve memzone");
147
148         ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
149                 "Metadata should have been full");
150
151         return 0;
152 }
153
154 static int
155 test_ivshmem_create_duplicate_memzone(void)
156 {
157         const struct rte_memzone *mz;
158
159         ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
160                         "Failed to create metadata");
161
162         mz = rte_memzone_reserve("mz", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
163         ASSERT(mz != NULL, "Failed to reserve memzone");
164
165         ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
166                         "Failed to add memzone");
167
168         ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
169                         "Added the same memzone twice");
170
171         return 0;
172 }
173
174 static int
175 test_ivshmem_api_test(void)
176 {
177         const struct rte_memzone * mz;
178         struct rte_mempool * mp;
179         struct rte_ring * r;
180         char buf[BUFSIZ];
181
182         memset(buf, 0, sizeof(buf));
183
184         r = rte_ring_create("ring", 1, SOCKET_ID_ANY, 0);
185         mp = rte_mempool_create("mempool", 1, 1, 1, 1, NULL, NULL, NULL, NULL,
186                         SOCKET_ID_ANY, 0);
187         mz = rte_memzone_reserve("memzone", 64, SOCKET_ID_ANY, 0);
188
189         ASSERT(r != NULL, "Failed to create ring");
190         ASSERT(mp != NULL, "Failed to create mempool");
191         ASSERT(mz != NULL, "Failed to reserve memzone");
192
193         /* try to create NULL metadata */
194         ASSERT(rte_ivshmem_metadata_create(NULL) < 0,
195                         "Created metadata with NULL name");
196
197         /* create valid metadata to do tests on */
198         ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
199                         "Failed to create metadata");
200
201         /* test adding memzone */
202         ASSERT(rte_ivshmem_metadata_add_memzone(NULL, NULL) < 0,
203                         "Added NULL memzone to NULL metadata");
204         ASSERT(rte_ivshmem_metadata_add_memzone(NULL, METADATA_NAME) < 0,
205                         "Added NULL memzone");
206         ASSERT(rte_ivshmem_metadata_add_memzone(mz, NULL) < 0,
207                         "Added memzone to NULL metadata");
208         ASSERT(rte_ivshmem_metadata_add_memzone(mz, NONEXISTENT_METADATA) < 0,
209                         "Added memzone to nonexistent metadata");
210
211         /* test adding ring */
212         ASSERT(rte_ivshmem_metadata_add_ring(NULL, NULL) < 0,
213                         "Added NULL ring to NULL metadata");
214         ASSERT(rte_ivshmem_metadata_add_ring(NULL, METADATA_NAME) < 0,
215                         "Added NULL ring");
216         ASSERT(rte_ivshmem_metadata_add_ring(r, NULL) < 0,
217                         "Added ring to NULL metadata");
218         ASSERT(rte_ivshmem_metadata_add_ring(r, NONEXISTENT_METADATA) < 0,
219                         "Added ring to nonexistent metadata");
220
221         /* test adding mempool */
222         ASSERT(rte_ivshmem_metadata_add_mempool(NULL, NULL) < 0,
223                         "Added NULL mempool to NULL metadata");
224         ASSERT(rte_ivshmem_metadata_add_mempool(NULL, METADATA_NAME) < 0,
225                         "Added NULL mempool");
226         ASSERT(rte_ivshmem_metadata_add_mempool(mp, NULL) < 0,
227                         "Added mempool to NULL metadata");
228         ASSERT(rte_ivshmem_metadata_add_mempool(mp, NONEXISTENT_METADATA) < 0,
229                         "Added mempool to nonexistent metadata");
230
231         /* test creating command line */
232         ASSERT(rte_ivshmem_metadata_cmdline_generate(NULL, sizeof(buf), METADATA_NAME) < 0,
233                         "Written command line into NULL buffer");
234         ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");
235
236         ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, 0, METADATA_NAME) < 0,
237                         "Written command line into small buffer");
238         ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");
239
240         ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf), NULL) < 0,
241                         "Written command line for NULL metadata");
242         ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");
243
244         ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
245                         NONEXISTENT_METADATA) < 0,
246                         "Writen command line for nonexistent metadata");
247         ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");
248
249         /* add stuff to config */
250         ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
251                         "Failed to add memzone to valid config");
252         ASSERT(rte_ivshmem_metadata_add_ring(r, METADATA_NAME) == 0,
253                         "Failed to add ring to valid config");
254         ASSERT(rte_ivshmem_metadata_add_mempool(mp, METADATA_NAME) == 0,
255                         "Failed to add mempool to valid config");
256
257         /* create config */
258         ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
259                         METADATA_NAME) == 0, "Failed to write command-line");
260
261         /* check if something was written */
262         ASSERT(strnlen(buf, sizeof(buf)) != 0, "Buffer is empty");
263
264         /* make sure we don't segfault */
265         rte_ivshmem_metadata_dump(stdout, NULL);
266
267         /* dump our metadata */
268         rte_ivshmem_metadata_dump(stdout, METADATA_NAME);
269
270         return 0;
271 }
272
273 static int
274 test_ivshmem_create_duplicate_metadata(void)
275 {
276         ASSERT(rte_ivshmem_metadata_create(DUPLICATE_METADATA) < 0,
277                         "Creating duplicate metadata should have failed");
278
279         return 0;
280 }
281
282 static int
283 test_ivshmem_create_metadata_config(void)
284 {
285         struct rte_ivshmem_metadata *metadata;
286
287         rte_ivshmem_metadata_create(METADATA_NAME);
288
289         metadata = mmap_metadata(METADATA_NAME);
290
291         ASSERT(metadata != MAP_FAILED, "Metadata mmaping failed");
292
293         ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
294                         "Magic number is not that magic");
295
296         ASSERT(strncmp(metadata->name, METADATA_NAME, sizeof(metadata->name)) == 0,
297                         "Name has not been set up");
298
299         ASSERT(metadata->entry[0].offset == 0, "Offest is not initialized");
300         ASSERT(metadata->entry[0].mz.addr == 0, "mz.addr is not initialized");
301         ASSERT(metadata->entry[0].mz.len == 0, "mz.len is not initialized");
302
303         return 0;
304 }
305
306 static int
307 test_ivshmem_create_multiple_metadata_configs(void)
308 {
309         int i;
310         char name[IVSHMEM_NAME_LEN];
311         struct rte_ivshmem_metadata *metadata;
312
313         for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_METADATA_FILES / 2; i++) {
314                 snprintf(name, sizeof(name), "test_%d", i);
315                 rte_ivshmem_metadata_create(name);
316                 metadata = mmap_metadata(name);
317
318                 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
319                                 "Magic number is not that magic");
320
321                 ASSERT(strncmp(metadata->name, name, sizeof(metadata->name)) == 0,
322                                 "Name has not been set up");
323         }
324
325         return 0;
326 }
327
328 static int
329 test_ivshmem_create_too_many_metadata_configs(void)
330 {
331         int i;
332         char name[IVSHMEM_NAME_LEN];
333
334         for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_METADATA_FILES; i++) {
335                 snprintf(name, sizeof(name), "test_%d", i);
336                 ASSERT(rte_ivshmem_metadata_create(name) == 0,
337                                 "Create config file failed");
338         }
339
340         ASSERT(rte_ivshmem_metadata_create(name) < 0,
341                         "Create config file didn't fail");
342
343         return 0;
344 }
345
346 enum rte_ivshmem_tests {
347         _test_ivshmem_api_test = 0,
348         _test_ivshmem_create_metadata_config,
349         _test_ivshmem_create_multiple_metadata_configs,
350         _test_ivshmem_create_too_many_metadata_configs,
351         _test_ivshmem_create_duplicate_metadata,
352         _test_ivshmem_create_lots_of_memzones,
353         _test_ivshmem_create_duplicate_memzone,
354         _last_test,
355 };
356
357 #define RTE_IVSHMEM_TEST_ID "RTE_IVSHMEM_TEST_ID"
358
359 static int
360 launch_all_tests_on_secondary_processes(void)
361 {
362         int ret = 0;
363         char id;
364         char testid;
365         char tmp[PATH_MAX] = {0};
366         char prefix[PATH_MAX] = {0};
367
368         get_current_prefix(tmp, sizeof(tmp));
369
370         snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
371
372         const char *argv[] = { prgname, "-c", "1", "-n", "3",
373                         "--proc-type=secondary", prefix };
374
375         for (id = 0; id < _last_test; id++) {
376                 testid = (char)(FIRST_TEST + id);
377                 setenv(RTE_IVSHMEM_TEST_ID, &testid, 1);
378                 if (launch_proc(argv) != 0)
379                         return -1;
380         }
381         return ret;
382 }
383
384 int
385 test_ivshmem(void)
386 {
387         int testid;
388
389         /* We want to have a clean execution for every test without exposing
390          * private global data structures in rte_ivshmem so we launch each test
391          * on a different secondary process. */
392         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
393
394                 /* first, create metadata */
395                 ASSERT(create_duplicate() == 0, "Creating metadata failed");
396
397                 return launch_all_tests_on_secondary_processes();
398         }
399
400         testid = *(getenv(RTE_IVSHMEM_TEST_ID)) - FIRST_TEST;
401
402         printf("Secondary process running test %d \n", testid);
403
404         switch (testid) {
405         case _test_ivshmem_api_test:
406                 return test_ivshmem_api_test();
407
408         case _test_ivshmem_create_metadata_config:
409                 return test_ivshmem_create_metadata_config();
410
411         case _test_ivshmem_create_multiple_metadata_configs:
412                 return test_ivshmem_create_multiple_metadata_configs();
413
414         case _test_ivshmem_create_too_many_metadata_configs:
415                 return test_ivshmem_create_too_many_metadata_configs();
416
417         case _test_ivshmem_create_duplicate_metadata:
418                 return test_ivshmem_create_duplicate_metadata();
419
420         case _test_ivshmem_create_lots_of_memzones:
421                 return test_ivshmem_create_lots_of_memzones();
422
423         case _test_ivshmem_create_duplicate_memzone:
424                 return test_ivshmem_create_duplicate_memzone();
425
426         default:
427                 break;
428         }
429
430         return -1;
431 }
432
433 REGISTER_TEST_COMMAND(ivshmem_autotest, test_ivshmem);