Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <string.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <stdint.h>
9 : : #include <inttypes.h>
10 : : #include <stdarg.h>
11 : : #include <errno.h>
12 : : #include <sys/queue.h>
13 : :
14 : : #include <rte_common.h>
15 : : #include <rte_log.h>
16 : : #include <rte_debug.h>
17 : : #include <rte_memory.h>
18 : : #include <rte_launch.h>
19 : : #include <rte_cycles.h>
20 : : #include <rte_eal.h>
21 : : #include <rte_per_lcore.h>
22 : : #include <rte_lcore.h>
23 : : #include <rte_branch_prediction.h>
24 : : #include <rte_ring.h>
25 : : #include <rte_mempool.h>
26 : : #include <rte_spinlock.h>
27 : : #include <rte_malloc.h>
28 : :
29 : : #ifdef RTE_LIB_HASH
30 : : #include <rte_hash.h>
31 : : #include <rte_fbk_hash.h>
32 : : #include <rte_jhash.h>
33 : : #endif /* RTE_LIB_HASH */
34 : :
35 : : #ifdef RTE_LIB_LPM
36 : : #include <rte_lpm.h>
37 : : #endif /* RTE_LIB_LPM */
38 : :
39 : : #include <rte_string_fns.h>
40 : :
41 : : #include "test.h"
42 : :
43 : : typedef int (*case_func_t)(void* arg);
44 : : typedef void (*case_clean_t)(unsigned lcore_id);
45 : :
46 : : #define MAX_STRING_SIZE (256)
47 : : #define MAX_ITER_MULTI (16)
48 : : #define MAX_ITER_ONCE (4)
49 : : #define MAX_LPM_ITER_TIMES (6)
50 : :
51 : : #define MEMPOOL_ELT_SIZE (sizeof(uint32_t))
52 : : #define MEMPOOL_SIZE (4)
53 : :
54 : : #define MAX_LCORES (rte_memzone_max_get() / (MAX_ITER_MULTI * 4U))
55 : :
56 : : static uint32_t obj_count;
57 : : static uint32_t synchro;
58 : :
59 : : #define WAIT_SYNCHRO_FOR_WORKERS() do { \
60 : : if (lcore_self != rte_get_main_lcore()) \
61 : : rte_wait_until_equal_32(&synchro, 1, __ATOMIC_RELAXED); \
62 : : } while(0)
63 : :
64 : : /*
65 : : * rte_eal_init only init once
66 : : */
67 : : static int
68 : 2 : test_eal_init_once(__rte_unused void *arg)
69 : : {
70 : : unsigned lcore_self = rte_lcore_id();
71 : :
72 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
73 : :
74 : 2 : __atomic_store_n(&obj_count, 1, __ATOMIC_RELAXED); /* silent the check in the caller */
75 [ - + ]: 2 : if (rte_eal_init(0, NULL) != -1)
76 : 0 : return -1;
77 : :
78 : : return 0;
79 : : }
80 : :
81 : : /*
82 : : * ring create/lookup reentrancy test
83 : : */
84 : : static void
85 : 2 : ring_clean(unsigned int lcore_id)
86 : : {
87 : : struct rte_ring *rp;
88 : : char ring_name[MAX_STRING_SIZE];
89 : : int i;
90 : :
91 : 2 : rp = rte_ring_lookup("fr_test_once");
92 : 2 : rte_ring_free(rp);
93 : :
94 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
95 : : snprintf(ring_name, sizeof(ring_name),
96 : : "fr_test_%d_%d", lcore_id, i);
97 : 32 : rp = rte_ring_lookup(ring_name);
98 : 32 : rte_ring_free(rp);
99 : : }
100 : 2 : }
101 : :
102 : : static int
103 : 2 : ring_create_lookup(__rte_unused void *arg)
104 : : {
105 : : unsigned lcore_self = rte_lcore_id();
106 : : struct rte_ring * rp;
107 : : char ring_name[MAX_STRING_SIZE];
108 : : int i;
109 : :
110 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
111 : :
112 : : /* create the same ring simultaneously on all threads */
113 [ + + ]: 9 : for (i = 0; i < MAX_ITER_ONCE; i++) {
114 : 7 : rp = rte_ring_create("fr_test_once", 4096, SOCKET_ID_ANY, 0);
115 [ + + ]: 8 : if (rp != NULL)
116 : 1 : __atomic_fetch_add(&obj_count, 1, __ATOMIC_RELAXED);
117 : : }
118 : :
119 : : /* create/lookup new ring several times */
120 [ + + ]: 31 : for (i = 0; i < MAX_ITER_MULTI; i++) {
121 : : snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
122 : 29 : rp = rte_ring_create(ring_name, 4096, SOCKET_ID_ANY, 0);
123 [ + - ]: 32 : if (NULL == rp)
124 : : return -1;
125 [ + - ]: 32 : if (rte_ring_lookup(ring_name) != rp)
126 : : return -1;
127 : :
128 : : /* verify all ring created successful */
129 [ + - ]: 29 : if (rte_ring_lookup(ring_name) == NULL)
130 : : return -1;
131 : : }
132 : :
133 : : return 0;
134 : : }
135 : :
136 : : static void
137 : 132 : my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
138 : : void *obj, unsigned i)
139 : : {
140 : : uint32_t *objnum = obj;
141 : 132 : memset(obj, 0, mp->elt_size);
142 : 132 : *objnum = i;
143 : 132 : }
144 : :
145 : : static void
146 : 2 : mempool_clean(unsigned int lcore_id)
147 : : {
148 : : struct rte_mempool *mp;
149 : : char mempool_name[MAX_STRING_SIZE];
150 : : int i;
151 : :
152 : 2 : mp = rte_mempool_lookup("fr_test_once");
153 : 2 : rte_mempool_free(mp);
154 : :
155 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
156 : : snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d",
157 : : lcore_id, i);
158 : 32 : mp = rte_mempool_lookup(mempool_name);
159 : 32 : rte_mempool_free(mp);
160 : : }
161 : 2 : }
162 : :
163 : : static int
164 : 2 : mempool_create_lookup(__rte_unused void *arg)
165 : : {
166 : : unsigned lcore_self = rte_lcore_id();
167 : : struct rte_mempool * mp;
168 : : char mempool_name[MAX_STRING_SIZE];
169 : : int i;
170 : :
171 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
172 : :
173 : : /* create the same mempool simultaneously on all threads */
174 [ + + ]: 9 : for (i = 0; i < MAX_ITER_ONCE; i++) {
175 : 7 : mp = rte_mempool_create("fr_test_once", MEMPOOL_SIZE,
176 : : MEMPOOL_ELT_SIZE, 0, 0,
177 : : NULL, NULL,
178 : : my_obj_init, NULL,
179 : : SOCKET_ID_ANY, 0);
180 [ + + ]: 8 : if (mp != NULL)
181 : 1 : __atomic_fetch_add(&obj_count, 1, __ATOMIC_RELAXED);
182 : : }
183 : :
184 : : /* create/lookup new ring several times */
185 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
186 : : snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
187 : 32 : mp = rte_mempool_create(mempool_name, MEMPOOL_SIZE,
188 : : MEMPOOL_ELT_SIZE, 0, 0,
189 : : NULL, NULL,
190 : : my_obj_init, NULL,
191 : : SOCKET_ID_ANY, 0);
192 [ + - ]: 32 : if (NULL == mp)
193 : : return -1;
194 [ + - ]: 32 : if (rte_mempool_lookup(mempool_name) != mp)
195 : : return -1;
196 : :
197 : : /* verify all ring created successful */
198 [ + - ]: 32 : if (rte_mempool_lookup(mempool_name) == NULL)
199 : : return -1;
200 : : }
201 : :
202 : : return 0;
203 : : }
204 : :
205 : : #ifdef RTE_LIB_HASH
206 : : static void
207 : 2 : hash_clean(unsigned lcore_id)
208 : : {
209 : : char hash_name[MAX_STRING_SIZE];
210 : : struct rte_hash *handle;
211 : : int i;
212 : :
213 : 2 : handle = rte_hash_find_existing("fr_test_once");
214 : 2 : rte_hash_free(handle);
215 : :
216 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
217 : : snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_id, i);
218 : :
219 [ - + ]: 32 : if ((handle = rte_hash_find_existing(hash_name)) != NULL)
220 : 0 : rte_hash_free(handle);
221 : : }
222 : 2 : }
223 : :
224 : : static int
225 : 2 : hash_create_free(__rte_unused void *arg)
226 : : {
227 : : unsigned lcore_self = rte_lcore_id();
228 : : struct rte_hash *handle;
229 : : char hash_name[MAX_STRING_SIZE];
230 : : int i;
231 : 2 : struct rte_hash_parameters hash_params = {
232 : : .name = NULL,
233 : : .entries = 16,
234 : : .key_len = 4,
235 : : .hash_func = (rte_hash_function)rte_jhash_32b,
236 : : .hash_func_init_val = 0,
237 : : .socket_id = 0,
238 : : };
239 : :
240 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
241 : :
242 : : /* create the same hash simultaneously on all threads */
243 : 2 : hash_params.name = "fr_test_once";
244 [ + + ]: 10 : for (i = 0; i < MAX_ITER_ONCE; i++) {
245 : 8 : handle = rte_hash_create(&hash_params);
246 [ + + ]: 8 : if (handle != NULL)
247 : 1 : __atomic_fetch_add(&obj_count, 1, __ATOMIC_RELAXED);
248 : : }
249 : :
250 : : /* create multiple times simultaneously */
251 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
252 : : snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d", lcore_self, i);
253 : 32 : hash_params.name = hash_name;
254 : :
255 : 32 : handle = rte_hash_create(&hash_params);
256 [ + - ]: 32 : if (NULL == handle)
257 : : return -1;
258 : :
259 : : /* verify correct existing and then free all */
260 [ + - ]: 32 : if (handle != rte_hash_find_existing(hash_name))
261 : : return -1;
262 : :
263 : 29 : rte_hash_free(handle);
264 : :
265 : : /* verify free correct */
266 [ + - ]: 32 : if (NULL != rte_hash_find_existing(hash_name))
267 : : return -1;
268 : : }
269 : :
270 : : return 0;
271 : : }
272 : :
273 : : static void
274 : 2 : fbk_clean(unsigned lcore_id)
275 : : {
276 : : char fbk_name[MAX_STRING_SIZE];
277 : : struct rte_fbk_hash_table *handle;
278 : : int i;
279 : :
280 : 2 : handle = rte_fbk_hash_find_existing("fr_test_once");
281 : 2 : rte_fbk_hash_free(handle);
282 : :
283 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
284 : : snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_id, i);
285 : :
286 [ - + ]: 32 : if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
287 : 0 : rte_fbk_hash_free(handle);
288 : : }
289 : 2 : }
290 : :
291 : : static int
292 : 2 : fbk_create_free(__rte_unused void *arg)
293 : : {
294 : : unsigned lcore_self = rte_lcore_id();
295 : : struct rte_fbk_hash_table *handle;
296 : : char fbk_name[MAX_STRING_SIZE];
297 : : int i;
298 : 2 : struct rte_fbk_hash_params fbk_params = {
299 : : .name = NULL,
300 : : .entries = 4,
301 : : .entries_per_bucket = 4,
302 : : .socket_id = 0,
303 : : .hash_func = rte_jhash_1word,
304 : : .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
305 : : };
306 : :
307 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
308 : :
309 : : /* create the same fbk hash table simultaneously on all threads */
310 : 2 : fbk_params.name = "fr_test_once";
311 [ + + ]: 10 : for (i = 0; i < MAX_ITER_ONCE; i++) {
312 : 8 : handle = rte_fbk_hash_create(&fbk_params);
313 [ + + ]: 8 : if (handle != NULL)
314 : 1 : __atomic_fetch_add(&obj_count, 1, __ATOMIC_RELAXED);
315 : : }
316 : :
317 : : /* create multiple fbk tables simultaneously */
318 [ + + ]: 34 : for (i = 0; i < MAX_ITER_MULTI; i++) {
319 : : snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d", lcore_self, i);
320 : 32 : fbk_params.name = fbk_name;
321 : :
322 : 32 : handle = rte_fbk_hash_create(&fbk_params);
323 [ + - ]: 32 : if (NULL == handle)
324 : : return -1;
325 : :
326 : : /* verify correct existing and then free all */
327 [ + - ]: 32 : if (handle != rte_fbk_hash_find_existing(fbk_name))
328 : : return -1;
329 : :
330 : 32 : rte_fbk_hash_free(handle);
331 : :
332 : : /* verify free correct */
333 [ + - ]: 32 : if (NULL != rte_fbk_hash_find_existing(fbk_name))
334 : : return -1;
335 : : }
336 : :
337 : : return 0;
338 : : }
339 : : #endif /* RTE_LIB_HASH */
340 : :
341 : : #ifdef RTE_LIB_LPM
342 : : static void
343 : 2 : lpm_clean(unsigned int lcore_id)
344 : : {
345 : : char lpm_name[MAX_STRING_SIZE];
346 : : struct rte_lpm *lpm;
347 : : int i;
348 : :
349 : 2 : lpm = rte_lpm_find_existing("fr_test_once");
350 : 2 : rte_lpm_free(lpm);
351 : :
352 [ + + ]: 14 : for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
353 : : snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_id, i);
354 : :
355 [ - + ]: 12 : if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
356 : 0 : rte_lpm_free(lpm);
357 : : }
358 : 2 : }
359 : :
360 : : static int
361 : 2 : lpm_create_free(__rte_unused void *arg)
362 : : {
363 : : unsigned lcore_self = rte_lcore_id();
364 : : struct rte_lpm *lpm;
365 : : struct rte_lpm_config config;
366 : :
367 : 2 : config.max_rules = 4;
368 : 2 : config.number_tbl8s = 256;
369 : 2 : config.flags = 0;
370 : : char lpm_name[MAX_STRING_SIZE];
371 : : int i;
372 : :
373 [ + + ]: 2 : WAIT_SYNCHRO_FOR_WORKERS();
374 : :
375 : : /* create the same lpm simultaneously on all threads */
376 [ + + ]: 9 : for (i = 0; i < MAX_ITER_ONCE; i++) {
377 : 7 : lpm = rte_lpm_create("fr_test_once", SOCKET_ID_ANY, &config);
378 [ + + ]: 8 : if (lpm != NULL)
379 : 1 : __atomic_fetch_add(&obj_count, 1, __ATOMIC_RELAXED);
380 : : }
381 : :
382 : : /* create multiple fbk tables simultaneously */
383 [ + + ]: 14 : for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
384 : : snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
385 : 12 : lpm = rte_lpm_create(lpm_name, SOCKET_ID_ANY, &config);
386 [ + - ]: 12 : if (NULL == lpm)
387 : : return -1;
388 : :
389 : : /* verify correct existing and then free all */
390 [ + - ]: 12 : if (lpm != rte_lpm_find_existing(lpm_name))
391 : : return -1;
392 : :
393 : 12 : rte_lpm_free(lpm);
394 : :
395 : : /* verify free correct */
396 [ + - ]: 12 : if (NULL != rte_lpm_find_existing(lpm_name))
397 : : return -1;
398 : : }
399 : :
400 : : return 0;
401 : : }
402 : : #endif /* RTE_LIB_LPM */
403 : :
404 : : struct test_case{
405 : : case_func_t func;
406 : : void* arg;
407 : : case_clean_t clean;
408 : : char name[MAX_STRING_SIZE];
409 : : };
410 : :
411 : : /* All test cases in the test suite */
412 : : struct test_case test_cases[] = {
413 : : { test_eal_init_once, NULL, NULL, "eal init once" },
414 : : { ring_create_lookup, NULL, ring_clean, "ring create/lookup" },
415 : : { mempool_create_lookup, NULL, mempool_clean,
416 : : "mempool create/lookup" },
417 : : #ifdef RTE_LIB_HASH
418 : : { hash_create_free, NULL, hash_clean, "hash create/free" },
419 : : { fbk_create_free, NULL, fbk_clean, "fbk create/free" },
420 : : #endif /* RTE_LIB_HASH */
421 : : #ifdef RTE_LIB_LPM
422 : : { lpm_create_free, NULL, lpm_clean, "lpm create/free" },
423 : : #endif /* RTE_LIB_LPM */
424 : : };
425 : :
426 : : /**
427 : : * launch test case in two separate thread
428 : : */
429 : : static int
430 : 6 : launch_test(struct test_case *pt_case)
431 : : {
432 : : unsigned int lcore_id;
433 : : unsigned int cores;
434 : : unsigned int count;
435 : : int ret = 0;
436 : :
437 [ + - ]: 6 : if (pt_case->func == NULL)
438 : : return -1;
439 : :
440 : 6 : __atomic_store_n(&obj_count, 0, __ATOMIC_RELAXED);
441 : 6 : __atomic_store_n(&synchro, 0, __ATOMIC_RELAXED);
442 : :
443 : 6 : cores = RTE_MIN(rte_lcore_count(), MAX_LCORES);
444 [ + + ]: 12 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
445 [ + - ]: 6 : if (cores == 1)
446 : : break;
447 : 6 : cores--;
448 : 6 : rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
449 : : }
450 : :
451 : 6 : __atomic_store_n(&synchro, 1, __ATOMIC_RELAXED);
452 : :
453 [ - + ]: 6 : if (pt_case->func(pt_case->arg) < 0)
454 : : ret = -1;
455 : :
456 [ + + ]: 12 : RTE_LCORE_FOREACH_WORKER(lcore_id) {
457 [ - + ]: 6 : if (rte_eal_wait_lcore(lcore_id) < 0)
458 : : ret = -1;
459 : : }
460 : :
461 [ + + ]: 18 : RTE_LCORE_FOREACH(lcore_id) {
462 [ + + ]: 12 : if (pt_case->clean != NULL)
463 : 10 : pt_case->clean(lcore_id);
464 : : }
465 : :
466 : 6 : count = __atomic_load_n(&obj_count, __ATOMIC_RELAXED);
467 [ - + ]: 6 : if (count != 1) {
468 : : printf("%s: common object allocated %d times (should be 1)\n",
469 : 0 : pt_case->name, count);
470 : : ret = -1;
471 : : }
472 : :
473 : : return ret;
474 : : }
475 : :
476 : : /**
477 : : * Main entry of func_reentrancy test
478 : : */
479 : : static int
480 : 1 : test_func_reentrancy(void)
481 : : {
482 : : uint32_t case_id;
483 : : struct test_case *pt_case = NULL;
484 : :
485 : : if (RTE_EXEC_ENV_IS_WINDOWS)
486 : : return TEST_SKIPPED;
487 : :
488 [ - + ]: 1 : if (rte_lcore_count() < 2) {
489 : : printf("Not enough cores for func_reentrancy_autotest, expecting at least 2\n");
490 : 0 : return TEST_SKIPPED;
491 : : }
492 [ - + ]: 1 : else if (rte_lcore_count() > MAX_LCORES)
493 : : printf("Too many lcores, some cores will be disabled\n");
494 : :
495 [ + + ]: 7 : for (case_id = 0; case_id < RTE_DIM(test_cases); case_id++) {
496 : 6 : pt_case = &test_cases[case_id];
497 [ - + ]: 6 : if (pt_case->func == NULL)
498 : 0 : continue;
499 : :
500 [ - + ]: 6 : if (launch_test(pt_case) < 0) {
501 : 0 : printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
502 : 0 : return -1;
503 : : }
504 : 6 : printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);
505 : : }
506 : :
507 : : return 0;
508 : : }
509 : :
510 : 235 : REGISTER_FAST_TEST(func_reentrancy_autotest, false, true, test_func_reentrancy);
|