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.
41 #include <sys/queue.h>
43 #include <rte_common.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_launch.h>
49 #include <rte_cycles.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_atomic.h>
54 #include <rte_branch_prediction.h>
56 #include <rte_mempool.h>
57 #include <rte_spinlock.h>
58 #include <rte_malloc.h>
60 #ifdef RTE_LIBRTE_HASH
62 #include <rte_fbk_hash.h>
63 #include <rte_jhash.h>
64 #endif /* RTE_LIBRTE_HASH */
68 #endif /* RTE_LIBRTE_LPM */
70 #include <rte_string_fns.h>
74 typedef int (*case_func_t)(void* arg);
75 typedef void (*case_clean_t)(unsigned lcore_id);
77 #define MAX_STRING_SIZE (256)
78 #define MAX_ITER_TIMES (16)
79 #define MAX_LPM_ITER_TIMES (8)
81 #define MEMPOOL_ELT_SIZE (0)
82 #define MEMPOOL_SIZE (4)
84 #define MAX_LCORES RTE_MAX_MEMZONE / (MAX_ITER_TIMES * 4U)
86 static rte_atomic32_t synchro = RTE_ATOMIC32_INIT(0);
88 #define WAIT_SYNCHRO_FOR_SLAVES() do{ \
89 if (lcore_self != rte_get_master_lcore()) \
90 while (rte_atomic32_read(&synchro) == 0); \
94 * rte_eal_init only init once
97 test_eal_init_once(__attribute__((unused)) void *arg)
99 unsigned lcore_self = rte_lcore_id();
101 WAIT_SYNCHRO_FOR_SLAVES();
103 if (rte_eal_init(0, NULL) != -1)
110 * ring create/lookup reentrancy test
113 ring_create_lookup(__attribute__((unused)) void *arg)
115 unsigned lcore_self = rte_lcore_id();
116 struct rte_ring * rp;
117 char ring_name[MAX_STRING_SIZE];
120 WAIT_SYNCHRO_FOR_SLAVES();
122 /* create the same ring simultaneously on all threads */
123 for (i = 0; i < MAX_ITER_TIMES; i++) {
124 rp = rte_ring_create("fr_test_once", 4096, SOCKET_ID_ANY, 0);
125 if ((NULL == rp) && (rte_ring_lookup("fr_test_once") == NULL))
129 /* create/lookup new ring several times */
130 for (i = 0; i < MAX_ITER_TIMES; i++) {
131 snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
132 rp = rte_ring_create(ring_name, 4096, SOCKET_ID_ANY, 0);
135 if (rte_ring_lookup(ring_name) != rp)
139 /* verify all ring created sucessful */
140 for (i = 0; i < MAX_ITER_TIMES; i++) {
141 snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
142 if (rte_ring_lookup(ring_name) == NULL)
150 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
151 void *obj, unsigned i)
153 uint32_t *objnum = obj;
154 memset(obj, 0, mp->elt_size);
159 mempool_create_lookup(__attribute__((unused)) void *arg)
161 unsigned lcore_self = rte_lcore_id();
162 struct rte_mempool * mp;
163 char mempool_name[MAX_STRING_SIZE];
166 WAIT_SYNCHRO_FOR_SLAVES();
168 /* create the same mempool simultaneously on all threads */
169 for (i = 0; i < MAX_ITER_TIMES; i++) {
170 mp = rte_mempool_create("fr_test_once", MEMPOOL_SIZE,
171 MEMPOOL_ELT_SIZE, 0, 0,
175 if ((NULL == mp) && (rte_mempool_lookup("fr_test_once") == NULL))
179 /* create/lookup new ring several times */
180 for (i = 0; i < MAX_ITER_TIMES; i++) {
181 snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
182 mp = rte_mempool_create(mempool_name, MEMPOOL_SIZE,
183 MEMPOOL_ELT_SIZE, 0, 0,
189 if (rte_mempool_lookup(mempool_name) != mp)
193 /* verify all ring created sucessful */
194 for (i = 0; i < MAX_ITER_TIMES; i++) {
195 snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
196 if (rte_mempool_lookup(mempool_name) == NULL)
203 #ifdef RTE_LIBRTE_HASH
205 hash_clean(unsigned lcore_id)
207 char hash_name[MAX_STRING_SIZE];
208 struct rte_hash *handle;
211 for (i = 0; i < MAX_ITER_TIMES; i++) {
212 snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_id, i);
214 if ((handle = rte_hash_find_existing(hash_name)) != NULL)
215 rte_hash_free(handle);
220 hash_create_free(__attribute__((unused)) void *arg)
222 unsigned lcore_self = rte_lcore_id();
223 struct rte_hash *handle;
224 char hash_name[MAX_STRING_SIZE];
226 struct rte_hash_parameters hash_params = {
231 .hash_func = (rte_hash_function)rte_jhash2,
232 .hash_func_init_val = 0,
236 WAIT_SYNCHRO_FOR_SLAVES();
238 /* create the same hash simultaneously on all threads */
239 hash_params.name = "fr_test_once";
240 for (i = 0; i < MAX_ITER_TIMES; i++) {
241 handle = rte_hash_create(&hash_params);
242 if ((NULL == handle) && (rte_hash_find_existing("fr_test_once") == NULL))
246 /* create mutiple times simultaneously */
247 for (i = 0; i < MAX_ITER_TIMES; i++) {
248 snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
249 hash_params.name = hash_name;
251 handle = rte_hash_create(&hash_params);
255 /* verify correct existing and then free all */
256 if (handle != rte_hash_find_existing(hash_name))
259 rte_hash_free(handle);
262 /* verify free correct */
263 for (i = 0; i < MAX_ITER_TIMES; i++) {
264 snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
266 if (NULL != rte_hash_find_existing(hash_name))
274 fbk_clean(unsigned lcore_id)
276 char fbk_name[MAX_STRING_SIZE];
277 struct rte_fbk_hash_table *handle;
280 for (i = 0; i < MAX_ITER_TIMES; i++) {
281 snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_id, i);
283 if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
284 rte_fbk_hash_free(handle);
289 fbk_create_free(__attribute__((unused)) void *arg)
291 unsigned lcore_self = rte_lcore_id();
292 struct rte_fbk_hash_table *handle;
293 char fbk_name[MAX_STRING_SIZE];
295 struct rte_fbk_hash_params fbk_params = {
298 .entries_per_bucket = 4,
300 .hash_func = rte_jhash_1word,
301 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
304 WAIT_SYNCHRO_FOR_SLAVES();
306 /* create the same fbk hash table simultaneously on all threads */
307 fbk_params.name = "fr_test_once";
308 for (i = 0; i < MAX_ITER_TIMES; i++) {
309 handle = rte_fbk_hash_create(&fbk_params);
310 if ((NULL == handle) && (rte_fbk_hash_find_existing("fr_test_once") == NULL))
314 /* create mutiple fbk tables simultaneously */
315 for (i = 0; i < MAX_ITER_TIMES; i++) {
316 snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
317 fbk_params.name = fbk_name;
319 handle = rte_fbk_hash_create(&fbk_params);
323 /* verify correct existing and then free all */
324 if (handle != rte_fbk_hash_find_existing(fbk_name))
327 rte_fbk_hash_free(handle);
330 /* verify free correct */
331 for (i = 0; i < MAX_ITER_TIMES; i++) {
332 snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
334 if (NULL != rte_fbk_hash_find_existing(fbk_name))
340 #endif /* RTE_LIBRTE_HASH */
342 #ifdef RTE_LIBRTE_LPM
344 lpm_clean(unsigned lcore_id)
346 char lpm_name[MAX_STRING_SIZE];
350 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
351 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_id, i);
353 if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
359 lpm_create_free(__attribute__((unused)) void *arg)
361 unsigned lcore_self = rte_lcore_id();
363 char lpm_name[MAX_STRING_SIZE];
366 WAIT_SYNCHRO_FOR_SLAVES();
368 /* create the same lpm simultaneously on all threads */
369 for (i = 0; i < MAX_ITER_TIMES; i++) {
370 lpm = rte_lpm_create("fr_test_once", SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
371 if ((NULL == lpm) && (rte_lpm_find_existing("fr_test_once") == NULL))
375 /* create mutiple fbk tables simultaneously */
376 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
377 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
378 lpm = rte_lpm_create(lpm_name, SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
382 /* verify correct existing and then free all */
383 if (lpm != rte_lpm_find_existing(lpm_name))
389 /* verify free correct */
390 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
391 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
392 if (NULL != rte_lpm_find_existing(lpm_name))
398 #endif /* RTE_LIBRTE_LPM */
404 char name[MAX_STRING_SIZE];
407 /* All test cases in the test suite */
408 struct test_case test_cases[] = {
409 { test_eal_init_once, NULL, NULL, "eal init once" },
410 { ring_create_lookup, NULL, NULL, "ring create/lookup" },
411 { mempool_create_lookup, NULL, NULL, "mempool create/lookup" },
412 #ifdef RTE_LIBRTE_HASH
413 { hash_create_free, NULL, hash_clean, "hash create/free" },
414 { fbk_create_free, NULL, fbk_clean, "fbk create/free" },
415 #endif /* RTE_LIBRTE_HASH */
416 #ifdef RTE_LIBRTE_LPM
417 { lpm_create_free, NULL, lpm_clean, "lpm create/free" },
418 #endif /* RTE_LIBRTE_LPM */
422 * launch test case in two separate thread
425 launch_test(struct test_case *pt_case)
429 unsigned cores_save = rte_lcore_count();
430 unsigned cores = RTE_MIN(cores_save, MAX_LCORES);
432 if (pt_case->func == NULL)
435 rte_atomic32_set(&synchro, 0);
437 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
441 rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
444 rte_atomic32_set(&synchro, 1);
446 if (pt_case->func(pt_case->arg) < 0)
450 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
454 if (rte_eal_wait_lcore(lcore_id) < 0)
457 if (pt_case->clean != NULL)
458 pt_case->clean(lcore_id);
465 * Main entry of func_reentrancy test
468 test_func_reentrancy(void)
471 struct test_case *pt_case = NULL;
473 if (rte_lcore_count() <= 1) {
474 printf("Not enough lcore for testing\n");
477 else if (rte_lcore_count() > MAX_LCORES)
478 printf("Too many lcores, some cores will be disabled\n");
480 for (case_id = 0; case_id < sizeof(test_cases)/sizeof(struct test_case); case_id ++) {
481 pt_case = &test_cases[case_id];
482 if (pt_case->func == NULL)
485 if (launch_test(pt_case) < 0) {
486 printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
489 printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);
495 static struct test_command func_reentrancy_cmd = {
496 .command = "func_reentrancy_autotest",
497 .callback = test_func_reentrancy,
499 REGISTER_TEST_COMMAND(func_reentrancy_cmd);