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>
73 #include <cmdline_parse.h>
77 typedef int (*case_func_t)(void* arg);
78 typedef void (*case_clean_t)(unsigned lcore_id);
80 #define MAX_STRING_SIZE (256)
81 #define MAX_ITER_TIMES (16)
82 #define MAX_LPM_ITER_TIMES (8)
84 #define MEMPOOL_ELT_SIZE (0)
85 #define MEMPOOL_SIZE (4)
87 #define MAX_LCORES RTE_MAX_MEMZONE / (MAX_ITER_TIMES * 4U)
89 static rte_atomic32_t synchro = RTE_ATOMIC32_INIT(0);
91 #define WAIT_SYNCHRO_FOR_SLAVES() do{ \
92 if (lcore_self != rte_get_master_lcore()) \
93 while (rte_atomic32_read(&synchro) == 0); \
97 * rte_eal_init only init once
100 test_eal_init_once(__attribute__((unused)) void *arg)
102 unsigned lcore_self = rte_lcore_id();
104 WAIT_SYNCHRO_FOR_SLAVES();
106 if (rte_eal_init(0, NULL) != -1)
113 * ring create/lookup reentrancy test
116 ring_create_lookup(__attribute__((unused)) void *arg)
118 unsigned lcore_self = rte_lcore_id();
119 struct rte_ring * rp;
120 char ring_name[MAX_STRING_SIZE];
123 WAIT_SYNCHRO_FOR_SLAVES();
125 /* create the same ring simultaneously on all threads */
126 for (i = 0; i < MAX_ITER_TIMES; i++) {
127 rp = rte_ring_create("fr_test_once", 4096, SOCKET_ID_ANY, 0);
128 if ((NULL == rp) && (rte_ring_lookup("fr_test_once") == NULL))
132 /* create/lookup new ring several times */
133 for (i = 0; i < MAX_ITER_TIMES; i++) {
134 rte_snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
135 rp = rte_ring_create(ring_name, 4096, SOCKET_ID_ANY, 0);
138 if (rte_ring_lookup(ring_name) != rp)
142 /* verify all ring created sucessful */
143 for (i = 0; i < MAX_ITER_TIMES; i++) {
144 rte_snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
145 if (rte_ring_lookup(ring_name) == NULL)
153 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
154 void *obj, unsigned i)
156 uint32_t *objnum = obj;
157 memset(obj, 0, mp->elt_size);
162 mempool_create_lookup(__attribute__((unused)) void *arg)
164 unsigned lcore_self = rte_lcore_id();
165 struct rte_mempool * mp;
166 char mempool_name[MAX_STRING_SIZE];
169 WAIT_SYNCHRO_FOR_SLAVES();
171 /* create the same mempool simultaneously on all threads */
172 for (i = 0; i < MAX_ITER_TIMES; i++) {
173 mp = rte_mempool_create("fr_test_once", MEMPOOL_SIZE,
174 MEMPOOL_ELT_SIZE, 0, 0,
178 if ((NULL == mp) && (rte_mempool_lookup("fr_test_once") == NULL))
182 /* create/lookup new ring several times */
183 for (i = 0; i < MAX_ITER_TIMES; i++) {
184 rte_snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
185 mp = rte_mempool_create(mempool_name, MEMPOOL_SIZE,
186 MEMPOOL_ELT_SIZE, 0, 0,
192 if (rte_mempool_lookup(mempool_name) != mp)
196 /* verify all ring created sucessful */
197 for (i = 0; i < MAX_ITER_TIMES; i++) {
198 rte_snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
199 if (rte_mempool_lookup(mempool_name) == NULL)
206 #ifdef RTE_LIBRTE_HASH
208 hash_clean(unsigned lcore_id)
210 char hash_name[MAX_STRING_SIZE];
211 struct rte_hash *handle;
214 for (i = 0; i < MAX_ITER_TIMES; i++) {
215 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_id, i);
217 if ((handle = rte_hash_find_existing(hash_name)) != NULL)
218 rte_hash_free(handle);
223 hash_create_free(__attribute__((unused)) void *arg)
225 unsigned lcore_self = rte_lcore_id();
226 struct rte_hash *handle;
227 char hash_name[MAX_STRING_SIZE];
229 struct rte_hash_parameters hash_params = {
234 .hash_func = (rte_hash_function)rte_jhash2,
235 .hash_func_init_val = 0,
239 WAIT_SYNCHRO_FOR_SLAVES();
241 /* create the same hash simultaneously on all threads */
242 hash_params.name = "fr_test_once";
243 for (i = 0; i < MAX_ITER_TIMES; i++) {
244 handle = rte_hash_create(&hash_params);
245 if ((NULL == handle) && (rte_hash_find_existing("fr_test_once") == NULL))
249 /* create mutiple times simultaneously */
250 for (i = 0; i < MAX_ITER_TIMES; i++) {
251 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
252 hash_params.name = hash_name;
254 handle = rte_hash_create(&hash_params);
258 /* verify correct existing and then free all */
259 if (handle != rte_hash_find_existing(hash_name))
262 rte_hash_free(handle);
265 /* verify free correct */
266 for (i = 0; i < MAX_ITER_TIMES; i++) {
267 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
269 if (NULL != rte_hash_find_existing(hash_name))
277 fbk_clean(unsigned lcore_id)
279 char fbk_name[MAX_STRING_SIZE];
280 struct rte_fbk_hash_table *handle;
283 for (i = 0; i < MAX_ITER_TIMES; i++) {
284 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_id, i);
286 if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
287 rte_fbk_hash_free(handle);
292 fbk_create_free(__attribute__((unused)) void *arg)
294 unsigned lcore_self = rte_lcore_id();
295 struct rte_fbk_hash_table *handle;
296 char fbk_name[MAX_STRING_SIZE];
298 struct rte_fbk_hash_params fbk_params = {
301 .entries_per_bucket = 4,
303 .hash_func = rte_jhash_1word,
304 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
307 WAIT_SYNCHRO_FOR_SLAVES();
309 /* create the same fbk hash table simultaneously on all threads */
310 fbk_params.name = "fr_test_once";
311 for (i = 0; i < MAX_ITER_TIMES; i++) {
312 handle = rte_fbk_hash_create(&fbk_params);
313 if ((NULL == handle) && (rte_fbk_hash_find_existing("fr_test_once") == NULL))
317 /* create mutiple fbk tables simultaneously */
318 for (i = 0; i < MAX_ITER_TIMES; i++) {
319 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
320 fbk_params.name = fbk_name;
322 handle = rte_fbk_hash_create(&fbk_params);
326 /* verify correct existing and then free all */
327 if (handle != rte_fbk_hash_find_existing(fbk_name))
330 rte_fbk_hash_free(handle);
333 /* verify free correct */
334 for (i = 0; i < MAX_ITER_TIMES; i++) {
335 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
337 if (NULL != rte_fbk_hash_find_existing(fbk_name))
343 #endif /* RTE_LIBRTE_HASH */
345 #ifdef RTE_LIBRTE_LPM
347 lpm_clean(unsigned lcore_id)
349 char lpm_name[MAX_STRING_SIZE];
353 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
354 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_id, i);
356 if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
362 lpm_create_free(__attribute__((unused)) void *arg)
364 unsigned lcore_self = rte_lcore_id();
366 char lpm_name[MAX_STRING_SIZE];
369 WAIT_SYNCHRO_FOR_SLAVES();
371 /* create the same lpm simultaneously on all threads */
372 for (i = 0; i < MAX_ITER_TIMES; i++) {
373 lpm = rte_lpm_create("fr_test_once", SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
374 if ((NULL == lpm) && (rte_lpm_find_existing("fr_test_once") == NULL))
378 /* create mutiple fbk tables simultaneously */
379 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
380 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
381 lpm = rte_lpm_create(lpm_name, SOCKET_ID_ANY, 4, RTE_LPM_HEAP);
385 /* verify correct existing and then free all */
386 if (lpm != rte_lpm_find_existing(lpm_name))
392 /* verify free correct */
393 for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
394 rte_snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
395 if (NULL != rte_lpm_find_existing(lpm_name))
401 #endif /* RTE_LIBRTE_LPM */
407 char name[MAX_STRING_SIZE];
410 /* All test cases in the test suite */
411 struct test_case test_cases[] = {
412 { test_eal_init_once, NULL, NULL, "eal init once" },
413 { ring_create_lookup, NULL, NULL, "ring create/lookup" },
414 { mempool_create_lookup, NULL, NULL, "mempool create/lookup" },
415 #ifdef RTE_LIBRTE_HASH
416 { hash_create_free, NULL, hash_clean, "hash create/free" },
417 { fbk_create_free, NULL, fbk_clean, "fbk create/free" },
418 #endif /* RTE_LIBRTE_HASH */
419 #ifdef RTE_LIBRTE_LPM
420 { lpm_create_free, NULL, lpm_clean, "lpm create/free" },
421 #endif /* RTE_LIBRTE_LPM */
425 * launch test case in two separate thread
428 launch_test(struct test_case *pt_case)
432 unsigned cores_save = rte_lcore_count();
433 unsigned cores = RTE_MIN(cores_save, MAX_LCORES);
435 if (pt_case->func == NULL)
438 rte_atomic32_set(&synchro, 0);
440 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
444 rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
447 rte_atomic32_set(&synchro, 1);
449 if (pt_case->func(pt_case->arg) < 0)
453 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
457 if (rte_eal_wait_lcore(lcore_id) < 0)
460 if (pt_case->clean != NULL)
461 pt_case->clean(lcore_id);
468 * Main entry of func_reentrancy test
471 test_func_reentrancy(void)
474 struct test_case *pt_case = NULL;
476 if (rte_lcore_count() <= 1) {
477 printf("Not enough lcore for testing\n");
480 else if (rte_lcore_count() > MAX_LCORES)
481 printf("Too many lcores, some cores will be disabled\n");
483 for (case_id = 0; case_id < sizeof(test_cases)/sizeof(struct test_case); case_id ++) {
484 pt_case = &test_cases[case_id];
485 if (pt_case->func == NULL)
488 if (launch_test(pt_case) < 0) {
489 printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
492 printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);