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 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 snprintf(buf, sizeof(buf), "%s", basename(buf));
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")),
93 static struct rte_ivshmem_metadata*
94 mmap_metadata(const char *name)
97 char pathname[PATH_MAX];
98 struct rte_ivshmem_metadata *metadata;
100 snprintf(pathname, sizeof(pathname),
101 "/var/run/.dpdk_ivshmem_metadata_%s", name);
103 fd = open(pathname, O_RDWR, 0660);
107 metadata = (struct rte_ivshmem_metadata*) mmap(NULL,
108 sizeof(struct rte_ivshmem_metadata), PROT_READ | PROT_WRITE,
111 if (metadata == MAP_FAILED)
120 create_duplicate(void)
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");
129 test_ivshmem_create_lots_of_memzones(void)
132 char name[IVSHMEM_NAME_LEN];
133 const struct rte_memzone *mz;
135 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
136 "Failed to create metadata");
138 for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES; i++) {
139 snprintf(name, sizeof(name), "mz_%i", i);
141 mz = rte_memzone_reserve(name, CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
142 ASSERT(mz != NULL, "Failed to reserve memzone");
144 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
145 "Failed to add memzone");
147 mz = rte_memzone_reserve("one too many", CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
148 ASSERT(mz != NULL, "Failed to reserve memzone");
150 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
151 "Metadata should have been full");
157 test_ivshmem_create_duplicate_memzone(void)
159 const struct rte_memzone *mz;
161 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
162 "Failed to create metadata");
164 mz = rte_memzone_reserve("mz", CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
165 ASSERT(mz != NULL, "Failed to reserve memzone");
167 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
168 "Failed to add memzone");
170 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
171 "Added the same memzone twice");
177 test_ivshmem_api_test(void)
179 const struct rte_memzone * mz;
180 struct rte_mempool * mp;
184 memset(buf, 0, sizeof(buf));
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,
189 mz = rte_memzone_reserve("memzone", 64, SOCKET_ID_ANY, 0);
191 ASSERT(r != NULL, "Failed to create ring");
192 ASSERT(mp != NULL, "Failed to create mempool");
193 ASSERT(mz != NULL, "Failed to reserve memzone");
195 /* try to create NULL metadata */
196 ASSERT(rte_ivshmem_metadata_create(NULL) < 0,
197 "Created metadata with NULL name");
199 /* create valid metadata to do tests on */
200 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
201 "Failed to create metadata");
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");
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,
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");
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");
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");
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");
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");
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");
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");
260 ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
261 METADATA_NAME) == 0, "Failed to write command-line");
263 /* check if something was written */
264 ASSERT(strnlen(buf, sizeof(buf)) != 0, "Buffer is empty");
266 /* make sure we don't segfault */
267 rte_ivshmem_metadata_dump(stdout, NULL);
269 /* dump our metadata */
270 rte_ivshmem_metadata_dump(stdout, METADATA_NAME);
276 test_ivshmem_create_duplicate_metadata(void)
278 ASSERT(rte_ivshmem_metadata_create(DUPLICATE_METADATA) < 0,
279 "Creating duplicate metadata should have failed");
285 test_ivshmem_create_metadata_config(void)
287 struct rte_ivshmem_metadata *metadata;
289 rte_ivshmem_metadata_create(METADATA_NAME);
291 metadata = mmap_metadata(METADATA_NAME);
293 ASSERT(metadata != MAP_FAILED, "Metadata mmaping failed");
295 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
296 "Magic number is not that magic");
298 ASSERT(strncmp(metadata->name, METADATA_NAME, sizeof(metadata->name)) == 0,
299 "Name has not been set up");
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");
309 test_ivshmem_create_multiple_metadata_configs(void)
312 char name[IVSHMEM_NAME_LEN];
313 struct rte_ivshmem_metadata *metadata;
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);
320 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
321 "Magic number is not that magic");
323 ASSERT(strncmp(metadata->name, name, sizeof(metadata->name)) == 0,
324 "Name has not been set up");
331 test_ivshmem_create_too_many_metadata_configs(void)
334 char name[IVSHMEM_NAME_LEN];
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");
342 ASSERT(rte_ivshmem_metadata_create(name) < 0,
343 "Create config file didn't fail");
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,
359 #define RTE_IVSHMEM_TEST_ID "RTE_IVSHMEM_TEST_ID"
362 launch_all_tests_on_secondary_processes(void)
367 char tmp[PATH_MAX] = {0};
368 char prefix[PATH_MAX] = {0};
370 get_current_prefix(tmp, sizeof(tmp));
372 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
374 const char *argv[] = { prgname, "-c", "1", "-n", "3",
375 "--proc-type=secondary", prefix };
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)
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) {
396 /* first, create metadata */
397 ASSERT(create_duplicate() == 0, "Creating metadata failed");
399 return launch_all_tests_on_secondary_processes();
402 testid = *(getenv(RTE_IVSHMEM_TEST_ID)) - FIRST_TEST;
404 printf("Secondary process running test %d \n", testid);
407 case _test_ivshmem_api_test:
408 return test_ivshmem_api_test();
410 case _test_ivshmem_create_metadata_config:
411 return test_ivshmem_create_metadata_config();
413 case _test_ivshmem_create_multiple_metadata_configs:
414 return test_ivshmem_create_multiple_metadata_configs();
416 case _test_ivshmem_create_too_many_metadata_configs:
417 return test_ivshmem_create_too_many_metadata_configs();
419 case _test_ivshmem_create_duplicate_metadata:
420 return test_ivshmem_create_duplicate_metadata();
422 case _test_ivshmem_create_lots_of_memzones:
423 return test_ivshmem_create_lots_of_memzones();
425 case _test_ivshmem_create_duplicate_memzone:
426 return test_ivshmem_create_duplicate_memzone();
434 #else /* RTE_LIBRTE_IVSHMEM */
439 printf("This binary was not compiled with IVSHMEM support!\n");
442 #endif /* RTE_LIBRTE_IVSHMEM */