doc: whitespace changes in licenses
[dpdk.git] / app / test / test_func_reentrancy.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.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>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_mempool.h>
58 #include <rte_spinlock.h>
59 #include <rte_malloc.h>
60
61 #ifdef RTE_LIBRTE_HASH
62 #include <rte_hash.h>
63 #include <rte_fbk_hash.h>
64 #include <rte_jhash.h>
65 #endif /* RTE_LIBRTE_HASH */
66
67 #ifdef RTE_LIBRTE_LPM
68 #include <rte_lpm.h>
69 #endif /* RTE_LIBRTE_LPM */
70
71 #include <rte_string_fns.h>
72
73 #include <cmdline_parse.h>
74
75 #include "test.h"
76
77 typedef int (*case_func_t)(void* arg);
78 typedef void (*case_clean_t)(unsigned lcore_id);
79
80 #define MAX_STRING_SIZE                     (256)
81 #define MAX_ITER_TIMES                      (16)
82 #define MAX_LPM_ITER_TIMES                  (8)
83
84 #define MEMPOOL_ELT_SIZE                    (0)
85 #define MEMPOOL_SIZE                        (4)
86
87 #define MAX_LCORES      RTE_MAX_MEMZONE / (MAX_ITER_TIMES * 4U)
88
89 static rte_atomic32_t synchro = RTE_ATOMIC32_INIT(0);
90
91 #define WAIT_SYNCHRO_FOR_SLAVES()   do{ \
92         if (lcore_self != rte_get_master_lcore())                  \
93                 while (rte_atomic32_read(&synchro) == 0);        \
94 } while(0)
95
96 /*
97  * rte_eal_init only init once 
98  */
99 static int
100 test_eal_init_once(__attribute__((unused)) void *arg)
101 {
102         unsigned lcore_self =  rte_lcore_id();
103
104         WAIT_SYNCHRO_FOR_SLAVES();
105
106         if (rte_eal_init(0, NULL) != -1)
107                 return -1;
108
109         return 0;
110 }
111
112 /*
113  * ring create/lookup reentrancy test
114  */
115 static int
116 ring_create_lookup(__attribute__((unused)) void *arg)
117 {
118         unsigned lcore_self = rte_lcore_id();
119         struct rte_ring * rp;
120         char ring_name[MAX_STRING_SIZE];
121         int i;
122
123         WAIT_SYNCHRO_FOR_SLAVES();
124         
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))
129                         return -1;
130         }
131
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);
136                 if (NULL == rp)
137                         return -1;
138                 if (rte_ring_lookup(ring_name) != rp)
139                         return -1;
140         }
141
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)
146                         return -1;
147         }
148
149         return 0;
150 }
151
152 static void
153 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
154             void *obj, unsigned i)
155 {
156         uint32_t *objnum = obj;
157         memset(obj, 0, mp->elt_size);
158         *objnum = i;
159 }
160
161 static int
162 mempool_create_lookup(__attribute__((unused)) void *arg)
163 {
164         unsigned lcore_self = rte_lcore_id();
165         struct rte_mempool * mp;
166         char mempool_name[MAX_STRING_SIZE];
167         int i;
168
169         WAIT_SYNCHRO_FOR_SLAVES();
170
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,
175                                         NULL, NULL,
176                                         my_obj_init, NULL,
177                                         SOCKET_ID_ANY, 0);
178                 if ((NULL == mp) && (rte_mempool_lookup("fr_test_once") == NULL))
179                         return -1;
180         }
181
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,
187                                                 NULL, NULL,
188                                                 my_obj_init, NULL,
189                                                 SOCKET_ID_ANY, 0);
190                 if (NULL == mp)
191                         return -1;
192                 if (rte_mempool_lookup(mempool_name) != mp)
193                         return -1;
194         }
195
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)
200                         return -1;
201         }
202
203         return 0;
204 }
205
206 #ifdef RTE_LIBRTE_HASH
207 static void
208 hash_clean(unsigned lcore_id)
209 {
210         char hash_name[MAX_STRING_SIZE];
211         struct rte_hash *handle;
212         int i;
213
214         for (i = 0; i < MAX_ITER_TIMES; i++) {
215                 rte_snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d",  lcore_id, i);
216
217                 if ((handle = rte_hash_find_existing(hash_name)) != NULL)
218                         rte_hash_free(handle);
219         }
220 }
221
222 static int
223 hash_create_free(__attribute__((unused)) void *arg)
224 {
225         unsigned lcore_self = rte_lcore_id();
226         struct rte_hash *handle;
227         char hash_name[MAX_STRING_SIZE];
228         int i;
229         struct rte_hash_parameters hash_params = {
230                 .name = NULL,
231                 .entries = 16,
232                 .bucket_entries = 4,
233                 .key_len = 4,
234                 .hash_func = (rte_hash_function)rte_jhash2,
235                 .hash_func_init_val = 0,
236                 .socket_id = 0,
237         };
238
239         WAIT_SYNCHRO_FOR_SLAVES();
240         
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))
246                         return -1;
247         }
248
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;
253
254                 handle = rte_hash_create(&hash_params);
255                 if (NULL == handle) 
256                         return -1;
257
258                 /* verify correct existing and then free all */
259                 if (handle != rte_hash_find_existing(hash_name))
260                         return -1;
261
262                 rte_hash_free(handle);
263         }
264
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);
268
269                 if (NULL != rte_hash_find_existing(hash_name))
270                         return -1;
271         }
272
273         return 0;
274 }
275
276 static void
277 fbk_clean(unsigned lcore_id)
278 {
279         char fbk_name[MAX_STRING_SIZE];
280         struct rte_fbk_hash_table *handle;
281         int i;
282
283         for (i = 0; i < MAX_ITER_TIMES; i++) {
284                 rte_snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d",  lcore_id, i);
285
286                 if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
287                         rte_fbk_hash_free(handle);
288         }
289 }
290
291 static int
292 fbk_create_free(__attribute__((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         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         WAIT_SYNCHRO_FOR_SLAVES();
308         
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))
314                         return -1;
315         }
316
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;
321
322                 handle = rte_fbk_hash_create(&fbk_params);
323                 if (NULL == handle)
324                         return -1;      
325
326                 /* verify correct existing and then free all */
327                 if (handle != rte_fbk_hash_find_existing(fbk_name))
328                         return -1;
329
330                 rte_fbk_hash_free(handle);
331         }
332
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);
336
337                 if (NULL != rte_fbk_hash_find_existing(fbk_name))
338                         return -1;
339         }
340
341         return 0;
342 }
343 #endif /* RTE_LIBRTE_HASH */
344
345 #ifdef RTE_LIBRTE_LPM
346 static void
347 lpm_clean(unsigned lcore_id)
348 {
349         char lpm_name[MAX_STRING_SIZE];
350         struct rte_lpm *lpm;
351         int i;
352
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);
355
356                 if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
357                         rte_lpm_free(lpm);
358         }
359 }
360
361 static int
362 lpm_create_free(__attribute__((unused)) void *arg)
363 {
364         unsigned lcore_self = rte_lcore_id();
365         struct rte_lpm *lpm;
366         char lpm_name[MAX_STRING_SIZE];
367         int i;
368
369         WAIT_SYNCHRO_FOR_SLAVES();
370         
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))
375                         return -1;
376         }
377
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);
382                 if (NULL == lpm)
383                         return -1;      
384
385                 /* verify correct existing and then free all */
386                 if (lpm != rte_lpm_find_existing(lpm_name))
387                         return -1;
388
389                 rte_lpm_free(lpm);
390         }
391
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))
396                         return -1;
397         }
398
399         return 0;
400 }
401 #endif /* RTE_LIBRTE_LPM */
402
403 struct test_case{
404         case_func_t    func;
405         void*          arg;
406         case_clean_t   clean;
407         char           name[MAX_STRING_SIZE];
408 };
409
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 */
422 };
423
424 /** 
425  * launch test case in two separate thread 
426  */
427 static int
428 launch_test(struct test_case *pt_case)
429 {
430         int ret = 0;
431         unsigned lcore_id;
432         unsigned cores_save = rte_lcore_count();
433         unsigned cores = RTE_MIN(cores_save, MAX_LCORES);
434
435         if (pt_case->func == NULL)
436                 return -1;
437
438         rte_atomic32_set(&synchro, 0);
439
440         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
441                 if (cores == 1)
442                         break;
443                 cores--;
444                 rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
445         }
446
447         rte_atomic32_set(&synchro, 1);
448
449         if (pt_case->func(pt_case->arg) < 0)
450                 ret = -1;
451
452         cores = cores_save;
453         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
454                 if (cores == 1)
455                         break;
456                 cores--;
457                 if (rte_eal_wait_lcore(lcore_id) < 0)
458                         ret = -1;
459                 
460                 if (pt_case->clean != NULL)
461                         pt_case->clean(lcore_id);
462         }
463         
464         return ret;
465 }
466
467 /**
468  * Main entry of func_reentrancy test
469  */
470 int
471 test_func_reentrancy(void)
472 {
473         uint32_t case_id;
474         struct test_case *pt_case = NULL;
475
476         if (rte_lcore_count() <= 1) {
477                 printf("Not enough lcore for testing\n");
478                 return -1;
479         }
480         else if (rte_lcore_count() > MAX_LCORES)
481                 printf("Too many lcores, some cores will be disabled\n");
482
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)
486                         continue;
487                        
488                 if (launch_test(pt_case) < 0) {
489                         printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
490                         return -1;
491                 }
492                 printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);
493         }
494
495         return 0;
496 }