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 #include <rte_common.h>
47 #include <rte_ivshmem.h>
48 #include <rte_string_fns.h>
51 #define DUPLICATE_METADATA "duplicate"
52 #define METADATA_NAME "metadata"
53 #define NONEXISTENT_METADATA "nonexistent"
54 #define FIRST_TEST 'a'
56 #define launch_proc(ARGV) process_dup(ARGV, \
57 sizeof(ARGV)/(sizeof(ARGV[0])), "test_ivshmem")
59 #define ASSERT(cond,msg) do { \
61 printf("**** TEST %s() failed: %s\n", \
68 get_current_prefix(char * prefix, int size)
70 char path[PATH_MAX] = {0};
71 char buf[PATH_MAX] = {0};
73 /* get file for config (fd is always 3) */
74 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
76 /* return NULL on error */
77 if (readlink(path, buf, sizeof(buf)) == -1)
80 /* get the basename */
81 snprintf(buf, sizeof(buf), "%s", basename(buf));
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")),
91 static struct rte_ivshmem_metadata*
92 mmap_metadata(const char *name)
95 char pathname[PATH_MAX];
96 struct rte_ivshmem_metadata *metadata;
98 snprintf(pathname, sizeof(pathname),
99 "/var/run/.dpdk_ivshmem_metadata_%s", name);
101 fd = open(pathname, O_RDWR, 0660);
105 metadata = (struct rte_ivshmem_metadata*) mmap(NULL,
106 sizeof(struct rte_ivshmem_metadata), PROT_READ | PROT_WRITE,
109 if (metadata == MAP_FAILED)
118 create_duplicate(void)
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");
127 test_ivshmem_create_lots_of_memzones(void)
130 char name[IVSHMEM_NAME_LEN];
131 const struct rte_memzone *mz;
133 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
134 "Failed to create metadata");
136 for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES; i++) {
137 snprintf(name, sizeof(name), "mz_%i", i);
139 mz = rte_memzone_reserve(name, RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
140 ASSERT(mz != NULL, "Failed to reserve memzone");
142 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
143 "Failed to add memzone");
145 mz = rte_memzone_reserve("one too many", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
146 ASSERT(mz != NULL, "Failed to reserve memzone");
148 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
149 "Metadata should have been full");
155 test_ivshmem_create_duplicate_memzone(void)
157 const struct rte_memzone *mz;
159 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
160 "Failed to create metadata");
162 mz = rte_memzone_reserve("mz", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
163 ASSERT(mz != NULL, "Failed to reserve memzone");
165 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
166 "Failed to add memzone");
168 ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
169 "Added the same memzone twice");
175 test_ivshmem_api_test(void)
177 const struct rte_memzone * mz;
178 struct rte_mempool * mp;
182 memset(buf, 0, sizeof(buf));
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,
187 mz = rte_memzone_reserve("memzone", 64, SOCKET_ID_ANY, 0);
189 ASSERT(r != NULL, "Failed to create ring");
190 ASSERT(mp != NULL, "Failed to create mempool");
191 ASSERT(mz != NULL, "Failed to reserve memzone");
193 /* try to create NULL metadata */
194 ASSERT(rte_ivshmem_metadata_create(NULL) < 0,
195 "Created metadata with NULL name");
197 /* create valid metadata to do tests on */
198 ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
199 "Failed to create metadata");
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");
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,
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");
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");
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");
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");
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");
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");
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");
258 ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
259 METADATA_NAME) == 0, "Failed to write command-line");
261 /* check if something was written */
262 ASSERT(strnlen(buf, sizeof(buf)) != 0, "Buffer is empty");
264 /* make sure we don't segfault */
265 rte_ivshmem_metadata_dump(stdout, NULL);
267 /* dump our metadata */
268 rte_ivshmem_metadata_dump(stdout, METADATA_NAME);
274 test_ivshmem_create_duplicate_metadata(void)
276 ASSERT(rte_ivshmem_metadata_create(DUPLICATE_METADATA) < 0,
277 "Creating duplicate metadata should have failed");
283 test_ivshmem_create_metadata_config(void)
285 struct rte_ivshmem_metadata *metadata;
287 rte_ivshmem_metadata_create(METADATA_NAME);
289 metadata = mmap_metadata(METADATA_NAME);
291 ASSERT(metadata != MAP_FAILED, "Metadata mmaping failed");
293 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
294 "Magic number is not that magic");
296 ASSERT(strncmp(metadata->name, METADATA_NAME, sizeof(metadata->name)) == 0,
297 "Name has not been set up");
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");
307 test_ivshmem_create_multiple_metadata_configs(void)
310 char name[IVSHMEM_NAME_LEN];
311 struct rte_ivshmem_metadata *metadata;
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);
318 ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
319 "Magic number is not that magic");
321 ASSERT(strncmp(metadata->name, name, sizeof(metadata->name)) == 0,
322 "Name has not been set up");
329 test_ivshmem_create_too_many_metadata_configs(void)
332 char name[IVSHMEM_NAME_LEN];
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");
340 ASSERT(rte_ivshmem_metadata_create(name) < 0,
341 "Create config file didn't fail");
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,
357 #define RTE_IVSHMEM_TEST_ID "RTE_IVSHMEM_TEST_ID"
360 launch_all_tests_on_secondary_processes(void)
365 char tmp[PATH_MAX] = {0};
366 char prefix[PATH_MAX] = {0};
368 get_current_prefix(tmp, sizeof(tmp));
370 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
372 const char *argv[] = { prgname, "-c", "1", "-n", "3",
373 "--proc-type=secondary", prefix };
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)
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) {
394 /* first, create metadata */
395 ASSERT(create_duplicate() == 0, "Creating metadata failed");
397 return launch_all_tests_on_secondary_processes();
400 testid = *(getenv(RTE_IVSHMEM_TEST_ID)) - FIRST_TEST;
402 printf("Secondary process running test %d \n", testid);
405 case _test_ivshmem_api_test:
406 return test_ivshmem_api_test();
408 case _test_ivshmem_create_metadata_config:
409 return test_ivshmem_create_metadata_config();
411 case _test_ivshmem_create_multiple_metadata_configs:
412 return test_ivshmem_create_multiple_metadata_configs();
414 case _test_ivshmem_create_too_many_metadata_configs:
415 return test_ivshmem_create_too_many_metadata_configs();
417 case _test_ivshmem_create_duplicate_metadata:
418 return test_ivshmem_create_duplicate_metadata();
420 case _test_ivshmem_create_lots_of_memzones:
421 return test_ivshmem_create_lots_of_memzones();
423 case _test_ivshmem_create_duplicate_memzone:
424 return test_ivshmem_create_duplicate_memzone();
433 static struct test_command ivshmem_cmd = {
434 .command = "ivshmem_autotest",
435 .callback = test_ivshmem,
437 REGISTER_TEST_COMMAND(ivshmem_cmd);