4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
42 #include <cmdline_parse.h>
46 #ifdef RTE_LIBRTE_IVSHMEM
48 #include <rte_common.h>
49 #include <rte_ivshmem.h>
50 #include <rte_string_fns.h>
53 #define DUPLICATE_METADATA "duplicate"
54 #define METADATA_NAME "metadata"
55 #define NONEXISTENT_METADATA "nonexistent"
56 #define FIRST_TEST 'a'
58 #define launch_proc(ARGV) process_dup(ARGV, \
59 sizeof(ARGV)/(sizeof(ARGV[0])), "test_ivshmem")
61 #define ASSERT(cond,msg) do { \
63 printf("**** TEST %s() failed: %s\n", \
70 get_current_prefix(char * prefix, int size)
72 char path[PATH_MAX] = {0};
73 char buf[PATH_MAX] = {0};
75 /* get file for config (fd is always 3) */
76 rte_snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
78 /* return NULL on error */
79 if (readlink(path, buf, sizeof(buf)) == -1)
82 /* get the basename */
83 rte_snprintf(buf, sizeof(buf), "%s", basename(buf));
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]);
92 static struct rte_ivshmem_metadata*
93 mmap_metadata(const char *name)
96 char pathname[PATH_MAX];
97 struct rte_ivshmem_metadata *metadata;
99 rte_snprintf(pathname, sizeof(pathname),
100 "/var/run/.dpdk_ivshmem_metadata_%s", name);
102 fd = open(pathname, O_RDWR, 0660);
106 metadata = (struct rte_ivshmem_metadata*) mmap(NULL,
107 sizeof(struct rte_ivshmem_metadata), PROT_READ | PROT_WRITE,
110 if (metadata == MAP_FAILED)
119 create_duplicate(void)
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");
128 test_ivshmem_create_lots_of_memzones(void)
131 char name[IVSHMEM_NAME_LEN];
132 const struct rte_memzone *mz;
134 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
135 "Failed to create metadata");
137 for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES; i++) {
138 rte_snprintf(name, sizeof(name), "mz_%i", i);
140 mz = rte_memzone_reserve(name, CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
141 ASSERT(mz != NULL, "Failed to reserve memzone");
143 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
144 "Failed to add memzone");
146 mz = rte_memzone_reserve("one too many", CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
147 ASSERT(mz != NULL, "Failed to reserve memzone");
149 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
150 "Metadata should have been full");
156 test_ivshmem_create_duplicate_memzone(void)
158 const struct rte_memzone *mz;
160 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
161 "Failed to create metadata");
163 mz = rte_memzone_reserve("mz", CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
164 ASSERT(mz != NULL, "Failed to reserve memzone");
166 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
167 "Failed to add memzone");
169 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
170 "Added the same memzone twice");
176 test_ivshmem_api_test(void)
178 const struct rte_memzone * mz;
179 struct rte_mempool * mp;
183 memset(buf, 0, sizeof(buf));
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,
188 mz = rte_memzone_reserve("memzone", 64, SOCKET_ID_ANY, 0);
190 ASSERT(r != NULL, "Failed to create ring");
191 ASSERT(mp != NULL, "Failed to create mempool");
192 ASSERT(mz != NULL, "Failed to reserve memzone");
194 /* try to create NULL metadata */
195 ASSERT(rte_ivshmem_metadata_create(NULL) < 0,
196 "Created metadata with NULL name");
198 /* create valid metadata to do tests on */
199 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
200 "Failed to create metadata");
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");
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,
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");
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");
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");
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");
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");
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");
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");
259 ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
260 METADATA_NAME) == 0, "Failed to write command-line");
262 /* check if something was written */
263 ASSERT(strnlen(buf, sizeof(buf)) != 0, "Buffer is empty");
265 /* make sure we don't segfault */
266 rte_ivshmem_metadata_dump(NULL);
268 /* dump our metadata */
269 rte_ivshmem_metadata_dump(METADATA_NAME);
275 test_ivshmem_create_duplicate_metadata(void)
277 ASSERT(rte_ivshmem_metadata_create(DUPLICATE_METADATA) < 0,
278 "Creating duplicate metadata should have failed");
284 test_ivshmem_create_metadata_config(void)
286 struct rte_ivshmem_metadata *metadata;
288 rte_ivshmem_metadata_create(METADATA_NAME);
290 metadata = mmap_metadata(METADATA_NAME);
292 ASSERT(metadata != MAP_FAILED, "Metadata mmaping failed");
294 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
295 "Magic number is not that magic");
297 ASSERT(strncmp(metadata->name, METADATA_NAME, sizeof(metadata->name)) == 0,
298 "Name has not been set up");
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");
308 test_ivshmem_create_multiple_metadata_configs(void)
311 char name[IVSHMEM_NAME_LEN];
312 struct rte_ivshmem_metadata *metadata;
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);
319 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
320 "Magic number is not that magic");
322 ASSERT(strncmp(metadata->name, name, sizeof(metadata->name)) == 0,
323 "Name has not been set up");
330 test_ivshmem_create_too_many_metadata_configs(void)
333 char name[IVSHMEM_NAME_LEN];
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");
341 ASSERT(rte_ivshmem_metadata_create(name) < 0,
342 "Create config file didn't fail");
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,
358 #define RTE_IVSHMEM_TEST_ID "RTE_IVSHMEM_TEST_ID"
361 launch_all_tests_on_secondary_processes(void)
366 char tmp[PATH_MAX] = {0};
367 char prefix[PATH_MAX] = {0};
369 get_current_prefix(tmp, sizeof(tmp));
371 rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
373 const char *argv[] = { prgname, "-c", "1", "-n", "3",
374 "--proc-type=secondary", prefix };
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)
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) {
395 /* first, create metadata */
396 ASSERT(create_duplicate() == 0, "Creating metadata failed");
398 return launch_all_tests_on_secondary_processes();
401 testid = *(getenv(RTE_IVSHMEM_TEST_ID)) - FIRST_TEST;
403 printf("Secondary process running test %d \n", testid);
406 case _test_ivshmem_api_test:
407 return test_ivshmem_api_test();
409 case _test_ivshmem_create_metadata_config:
410 return test_ivshmem_create_metadata_config();
412 case _test_ivshmem_create_multiple_metadata_configs:
413 return test_ivshmem_create_multiple_metadata_configs();
415 case _test_ivshmem_create_too_many_metadata_configs:
416 return test_ivshmem_create_too_many_metadata_configs();
418 case _test_ivshmem_create_duplicate_metadata:
419 return test_ivshmem_create_duplicate_metadata();
421 case _test_ivshmem_create_lots_of_memzones:
422 return test_ivshmem_create_lots_of_memzones();
424 case _test_ivshmem_create_duplicate_memzone:
425 return test_ivshmem_create_duplicate_memzone();
433 #else /* RTE_LIBRTE_IVSHMEM */
438 printf("This binary was not compiled with IVSHMEM support!\n");
441 #endif /* RTE_LIBRTE_IVSHMEM */