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>
50 #include <rte_tailq.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
57 #include <rte_mempool.h>
58 #include <rte_spinlock.h>
59 #include <rte_malloc.h>
61 #ifdef RTE_LIBRTE_HASH
63 #include <rte_fbk_hash.h>
64 #include <rte_jhash.h>
65 #endif /* RTE_LIBRTE_HASH */
69 #endif /* RTE_LIBRTE_LPM */
71 #include <rte_string_fns.h>
75 typedef int (*case_func_t)(void* arg);
76 typedef void (*case_clean_t)(unsigned lcore_id);
78 #define MAX_STRING_SIZE (256)
79 #define MAX_ITER_TIMES (16)
80 #define MAX_LPM_ITER_TIMES (8)
82 #define MEMPOOL_ELT_SIZE (0)
83 #define MEMPOOL_SIZE (4)
85 #define MAX_LCORES RTE_MAX_MEMZONE / (MAX_ITER_TIMES * 4U)
87 static rte_atomic32_t synchro = RTE_ATOMIC32_INIT(0);
89 #define WAIT_SYNCHRO_FOR_SLAVES() do{ \
90 if (lcore_self != rte_get_master_lcore()) \
91 while (rte_atomic32_read(&synchro) == 0); \
95 * rte_eal_init only init once
98 test_eal_init_once(__attribute__((unused)) void *arg)
100 unsigned lcore_self = rte_lcore_id();
102 WAIT_SYNCHRO_FOR_SLAVES();
104 if (rte_eal_init(0, NULL) != -1)
111 * ring create/lookup reentrancy test
114 ring_create_lookup(__attribute__((unused)) void *arg)
116 unsigned lcore_self = rte_lcore_id();
117 struct rte_ring * rp;
118 char ring_name[MAX_STRING_SIZE];
121 WAIT_SYNCHRO_FOR_SLAVES();
123 /* create the same ring simultaneously on all threads */
124 for (i = 0; i < MAX_ITER_TIMES; i++) {
125 rp = rte_ring_create("fr_test_once", 4096, SOCKET_ID_ANY, 0);
126 if ((NULL == rp) && (rte_ring_lookup("fr_test_once") == NULL))
130 /* create/lookup new ring several times */
131 for (i = 0; i < MAX_ITER_TIMES; i++) {
132 rte_snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
133 rp = rte_ring_create(ring_name, 4096, SOCKET_ID_ANY, 0);
136 if (rte_ring_lookup(ring_name) != rp)
140 /* verify all ring created sucessful */
141 for (i = 0; i < MAX_ITER_TIMES; i++) {
142 rte_snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
143 if (rte_ring_lookup(ring_name) == NULL)
151 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
152 void *obj, unsigned i)
154 uint32_t *objnum = obj;
155 memset(obj, 0, mp->elt_size);
160 mempool_create_lookup(__attribute__((unused)) void *arg)
162 unsigned lcore_self = rte_lcore_id();
163 struct rte_mempool * mp;
164 char mempool_name[MAX_STRING_SIZE];
167 WAIT_SYNCHRO_FOR_SLAVES();
169 /* create the same mempool simultaneously on all threads */
170 for (i = 0; i < MAX_ITER_TIMES; i++) {
171 mp = rte_mempool_create("fr_test_once", MEMPOOL_SIZE,
172 MEMPOOL_ELT_SIZE, 0, 0,
176 if ((NULL == mp) && (rte_mempool_lookup("fr_test_once") == NULL))
180 /* create/lookup new ring several times */
181 for (i = 0; i < MAX_ITER_TIMES; i++) {
182 rte_snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
183 mp = rte_mempool_create(mempool_name, MEMPOOL_SIZE,
184 MEMPOOL_ELT_SIZE, 0, 0,
190 if (rte_mempool_lookup(mempool_name) != mp)
194 /* verify all ring created sucessful */
195 for (i = 0; i < MAX_ITER_TIMES; i++) {
196 rte_snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
197 if (rte_mempool_lookup(mempool_name) == NULL)
204 #ifdef RTE_LIBRTE_HASH
206 hash_clean(unsigned lcore_id)
208 char hash_name[MAX_STRING_SIZE];
209 struct rte_hash *handle;
212 for (i = 0; i < MAX_ITER_TIMES; i++) {
213 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_id, i);
215 if ((handle = rte_hash_find_existing(hash_name)) != NULL)
216 rte_hash_free(handle);
221 hash_create_free(__attribute__((unused)) void *arg)
223 unsigned lcore_self = rte_lcore_id();
224 struct rte_hash *handle;
225 char hash_name[MAX_STRING_SIZE];
227 struct rte_hash_parameters hash_params = {
232 .hash_func = (rte_hash_function)rte_jhash2,
233 .hash_func_init_val = 0,
237 WAIT_SYNCHRO_FOR_SLAVES();
239 /* create the same hash simultaneously on all threads */
240 hash_params.name = "fr_test_once";
241 for (i = 0; i < MAX_ITER_TIMES; i++) {
242 handle = rte_hash_create(&hash_params);
243 if ((NULL == handle) && (rte_hash_find_existing("fr_test_once") == NULL))
247 /* create mutiple times simultaneously */
248 for (i = 0; i < MAX_ITER_TIMES; i++) {
249 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
250 hash_params.name = hash_name;
252 handle = rte_hash_create(&hash_params);
256 /* verify correct existing and then free all */
257 if (handle != rte_hash_find_existing(hash_name))
260 rte_hash_free(handle);
263 /* verify free correct */
264 for (i = 0; i < MAX_ITER_TIMES; i++) {
265 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
267 if (NULL != rte_hash_find_existing(hash_name))
275 fbk_clean(unsigned lcore_id)
277 char fbk_name[MAX_STRING_SIZE];
278 struct rte_fbk_hash_table *handle;
281 for (i = 0; i < MAX_ITER_TIMES; i++) {
282 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_id, i);
284 if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
285 rte_fbk_hash_free(handle);
290 fbk_create_free(__attribute__((unused)) void *arg)
292 unsigned lcore_self = rte_lcore_id();
293 struct rte_fbk_hash_table *handle;
294 char fbk_name[MAX_STRING_SIZE];
296 struct rte_fbk_hash_params fbk_params = {
299 .entries_per_bucket = 4,
301 .hash_func = rte_jhash_1word,
302 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
305 WAIT_SYNCHRO_FOR_SLAVES();
307 /* create the same fbk hash table simultaneously on all threads */
308 fbk_params.name = "fr_test_once";
309 for (i = 0; i < MAX_ITER_TIMES; i++) {
310 handle = rte_fbk_hash_create(&fbk_params);
311 if ((NULL == handle) && (rte_fbk_hash_find_existing("fr_test_once") == NULL))
315 /* create mutiple fbk tables simultaneously */
316 for (i = 0; i < MAX_ITER_TIMES; i++) {
317 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
318 fbk_params.name = fbk_name;
320 handle = rte_fbk_hash_create(&fbk_params);
324 /* verify correct existing and then free all */
325 if (handle != rte_fbk_hash_find_existing(fbk_name))
328 rte_fbk_hash_free(handle);
331 /* verify free correct */
332 for (i = 0; i < MAX_ITER_TIMES; i++) {
333 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
335 if (NULL != rte_fbk_hash_find_existing(fbk_name))
341 #endif /* RTE_LIBRTE_HASH */
343 #ifdef RTE_LIBRTE_LPM
345 lpm_clean(unsigned lcore_id)
347 char lpm_name[MAX_STRING_SIZE];
351 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
352 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_id, i);
354 if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
360 lpm_create_free(__attribute__((unused)) void *arg)
362 unsigned lcore_self = rte_lcore_id();
364 char lpm_name[MAX_STRING_SIZE];
367 WAIT_SYNCHRO_FOR_SLAVES();
369 /* create the same lpm simultaneously on all threads */
370 for (i = 0; i < MAX_ITER_TIMES; i++) {
371 lpm = rte_lpm_create("fr_test_once", SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
372 if ((NULL == lpm) && (rte_lpm_find_existing("fr_test_once") == NULL))
376 /* create mutiple fbk tables simultaneously */
377 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
378 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
379 lpm = rte_lpm_create(lpm_name, SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
383 /* verify correct existing and then free all */
384 if (lpm != rte_lpm_find_existing(lpm_name))
390 /* verify free correct */
391 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
392 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
393 if (NULL != rte_lpm_find_existing(lpm_name))
399 #endif /* RTE_LIBRTE_LPM */
405 char name[MAX_STRING_SIZE];
408 /* All test cases in the test suite */
409 struct test_case test_cases[] = {
410 { test_eal_init_once, NULL, NULL, "eal init once" },
411 { ring_create_lookup, NULL, NULL, "ring create/lookup" },
412 { mempool_create_lookup, NULL, NULL, "mempool create/lookup" },
413 #ifdef RTE_LIBRTE_HASH
414 { hash_create_free, NULL, hash_clean, "hash create/free" },
415 { fbk_create_free, NULL, fbk_clean, "fbk create/free" },
416 #endif /* RTE_LIBRTE_HASH */
417 #ifdef RTE_LIBRTE_LPM
418 { lpm_create_free, NULL, lpm_clean, "lpm create/free" },
419 #endif /* RTE_LIBRTE_LPM */
423 * launch test case in two separate thread
426 launch_test(struct test_case *pt_case)
430 unsigned cores_save = rte_lcore_count();
431 unsigned cores = RTE_MIN(cores_save, MAX_LCORES);
433 if (pt_case->func == NULL)
436 rte_atomic32_set(&synchro, 0);
438 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
442 rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
445 rte_atomic32_set(&synchro, 1);
447 if (pt_case->func(pt_case->arg) < 0)
451 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
455 if (rte_eal_wait_lcore(lcore_id) < 0)
458 if (pt_case->clean != NULL)
459 pt_case->clean(lcore_id);
466 * Main entry of func_reentrancy test
469 test_func_reentrancy(void)
472 struct test_case *pt_case = NULL;
474 if (rte_lcore_count() <= 1) {
475 printf("Not enough lcore for testing\n");
478 else if (rte_lcore_count() > MAX_LCORES)
479 printf("Too many lcores, some cores will be disabled\n");
481 for (case_id = 0; case_id < sizeof(test_cases)/sizeof(struct test_case); case_id ++) {
482 pt_case = &test_cases[case_id];
483 if (pt_case->func == NULL)
486 if (launch_test(pt_case) < 0) {
487 printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
490 printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);