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