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