New upstream version 17.11-rc3
[deb_dpdk.git] / test / test / test_func_reentrancy.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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_launch.h>
48 #include <rte_cycles.h>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_ring.h>
55 #include <rte_mempool.h>
56 #include <rte_spinlock.h>
57 #include <rte_malloc.h>
58
59 #ifdef RTE_LIBRTE_HASH
60 #include <rte_hash.h>
61 #include <rte_fbk_hash.h>
62 #include <rte_jhash.h>
63 #endif /* RTE_LIBRTE_HASH */
64
65 #ifdef RTE_LIBRTE_LPM
66 #include <rte_lpm.h>
67 #endif /* RTE_LIBRTE_LPM */
68
69 #include <rte_string_fns.h>
70
71 #include "test.h"
72
73 typedef int (*case_func_t)(void* arg);
74 typedef void (*case_clean_t)(unsigned lcore_id);
75
76 #define MAX_STRING_SIZE                     (256)
77 #define MAX_ITER_TIMES                      (16)
78 #define MAX_LPM_ITER_TIMES                  (8)
79
80 #define MEMPOOL_ELT_SIZE                    (sizeof(uint32_t))
81 #define MEMPOOL_SIZE                        (4)
82
83 #define MAX_LCORES      RTE_MAX_MEMZONE / (MAX_ITER_TIMES * 4U)
84
85 static rte_atomic32_t obj_count = RTE_ATOMIC32_INIT(0);
86 static rte_atomic32_t synchro = RTE_ATOMIC32_INIT(0);
87
88 #define WAIT_SYNCHRO_FOR_SLAVES()   do{ \
89         if (lcore_self != rte_get_master_lcore())                  \
90                 while (rte_atomic32_read(&synchro) == 0);        \
91 } while(0)
92
93 /*
94  * rte_eal_init only init once
95  */
96 static int
97 test_eal_init_once(__attribute__((unused)) void *arg)
98 {
99         unsigned lcore_self =  rte_lcore_id();
100
101         WAIT_SYNCHRO_FOR_SLAVES();
102
103         rte_atomic32_set(&obj_count, 1); /* silent the check in the caller */
104         if (rte_eal_init(0, NULL) != -1)
105                 return -1;
106
107         return 0;
108 }
109
110 /*
111  * ring create/lookup reentrancy test
112  */
113 static int
114 ring_create_lookup(__attribute__((unused)) void *arg)
115 {
116         unsigned lcore_self = rte_lcore_id();
117         struct rte_ring * rp;
118         char ring_name[MAX_STRING_SIZE];
119         int i;
120
121         WAIT_SYNCHRO_FOR_SLAVES();
122
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 (rp != NULL)
127                         rte_atomic32_inc(&obj_count);
128         }
129
130         /* create/lookup new ring several times */
131         for (i = 0; i < MAX_ITER_TIMES; i++) {
132                 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);
134                 if (NULL == rp)
135                         return -1;
136                 if (rte_ring_lookup(ring_name) != rp)
137                         return -1;
138         }
139
140         /* verify all ring created successful */
141         for (i = 0; i < MAX_ITER_TIMES; i++) {
142                 snprintf(ring_name, sizeof(ring_name), "fr_test_%d_%d", lcore_self, i);
143                 if (rte_ring_lookup(ring_name) == NULL)
144                         return -1;
145         }
146
147         return 0;
148 }
149
150 static void
151 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
152             void *obj, unsigned i)
153 {
154         uint32_t *objnum = obj;
155         memset(obj, 0, mp->elt_size);
156         *objnum = i;
157 }
158
159 static int
160 mempool_create_lookup(__attribute__((unused)) void *arg)
161 {
162         unsigned lcore_self = rte_lcore_id();
163         struct rte_mempool * mp;
164         char mempool_name[MAX_STRING_SIZE];
165         int i;
166
167         WAIT_SYNCHRO_FOR_SLAVES();
168
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,
173                                         NULL, NULL,
174                                         my_obj_init, NULL,
175                                         SOCKET_ID_ANY, 0);
176                 if (mp != NULL)
177                         rte_atomic32_inc(&obj_count);
178         }
179
180         /* create/lookup new ring several times */
181         for (i = 0; i < MAX_ITER_TIMES; i++) {
182                 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,
185                                                 NULL, NULL,
186                                                 my_obj_init, NULL,
187                                                 SOCKET_ID_ANY, 0);
188                 if (NULL == mp)
189                         return -1;
190                 if (rte_mempool_lookup(mempool_name) != mp)
191                         return -1;
192         }
193
194         /* verify all ring created successful */
195         for (i = 0; i < MAX_ITER_TIMES; i++) {
196                 snprintf(mempool_name, sizeof(mempool_name), "fr_test_%d_%d", lcore_self, i);
197                 if (rte_mempool_lookup(mempool_name) == NULL)
198                         return -1;
199         }
200
201         return 0;
202 }
203
204 #ifdef RTE_LIBRTE_HASH
205 static void
206 hash_clean(unsigned lcore_id)
207 {
208         char hash_name[MAX_STRING_SIZE];
209         struct rte_hash *handle;
210         int i;
211
212         for (i = 0; i < MAX_ITER_TIMES; i++) {
213                 snprintf(hash_name, sizeof(hash_name), "fr_test_%d_%d",  lcore_id, i);
214
215                 if ((handle = rte_hash_find_existing(hash_name)) != NULL)
216                         rte_hash_free(handle);
217         }
218 }
219
220 static int
221 hash_create_free(__attribute__((unused)) void *arg)
222 {
223         unsigned lcore_self = rte_lcore_id();
224         struct rte_hash *handle;
225         char hash_name[MAX_STRING_SIZE];
226         int i;
227         struct rte_hash_parameters hash_params = {
228                 .name = NULL,
229                 .entries = 16,
230                 .key_len = 4,
231                 .hash_func = (rte_hash_function)rte_jhash_32b,
232                 .hash_func_init_val = 0,
233                 .socket_id = 0,
234         };
235
236         WAIT_SYNCHRO_FOR_SLAVES();
237
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 (handle != NULL)
243                         rte_atomic32_inc(&obj_count);
244         }
245
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;
250
251                 handle = rte_hash_create(&hash_params);
252                 if (NULL == handle)
253                         return -1;
254
255                 /* verify correct existing and then free all */
256                 if (handle != rte_hash_find_existing(hash_name))
257                         return -1;
258
259                 rte_hash_free(handle);
260         }
261
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);
265
266                 if (NULL != rte_hash_find_existing(hash_name))
267                         return -1;
268         }
269
270         return 0;
271 }
272
273 static void
274 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         for (i = 0; i < MAX_ITER_TIMES; i++) {
281                 snprintf(fbk_name, sizeof(fbk_name), "fr_test_%d_%d",  lcore_id, i);
282
283                 if ((handle = rte_fbk_hash_find_existing(fbk_name)) != NULL)
284                         rte_fbk_hash_free(handle);
285         }
286 }
287
288 static int
289 fbk_create_free(__attribute__((unused)) void *arg)
290 {
291         unsigned lcore_self = rte_lcore_id();
292         struct rte_fbk_hash_table *handle;
293         char fbk_name[MAX_STRING_SIZE];
294         int i;
295         struct rte_fbk_hash_params fbk_params = {
296                 .name = NULL,
297                 .entries = 4,
298                 .entries_per_bucket = 4,
299                 .socket_id = 0,
300                 .hash_func = rte_jhash_1word,
301                 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT,
302         };
303
304         WAIT_SYNCHRO_FOR_SLAVES();
305
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 (handle != NULL)
311                         rte_atomic32_inc(&obj_count);
312         }
313
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;
318
319                 handle = rte_fbk_hash_create(&fbk_params);
320                 if (NULL == handle)
321                         return -1;
322
323                 /* verify correct existing and then free all */
324                 if (handle != rte_fbk_hash_find_existing(fbk_name))
325                         return -1;
326
327                 rte_fbk_hash_free(handle);
328         }
329
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);
333
334                 if (NULL != rte_fbk_hash_find_existing(fbk_name))
335                         return -1;
336         }
337
338         return 0;
339 }
340 #endif /* RTE_LIBRTE_HASH */
341
342 #ifdef RTE_LIBRTE_LPM
343 static void
344 lpm_clean(unsigned lcore_id)
345 {
346         char lpm_name[MAX_STRING_SIZE];
347         struct rte_lpm *lpm;
348         int i;
349
350         for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
351                 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d",  lcore_id, i);
352
353                 if ((lpm = rte_lpm_find_existing(lpm_name)) != NULL)
354                         rte_lpm_free(lpm);
355         }
356 }
357
358 static int
359 lpm_create_free(__attribute__((unused)) void *arg)
360 {
361         unsigned lcore_self = rte_lcore_id();
362         struct rte_lpm *lpm;
363         struct rte_lpm_config config;
364
365         config.max_rules = 4;
366         config.number_tbl8s = 256;
367         config.flags = 0;
368         char lpm_name[MAX_STRING_SIZE];
369         int i;
370
371         WAIT_SYNCHRO_FOR_SLAVES();
372
373         /* create the same lpm simultaneously on all threads */
374         for (i = 0; i < MAX_ITER_TIMES; i++) {
375                 lpm = rte_lpm_create("fr_test_once",  SOCKET_ID_ANY, &config);
376                 if (lpm != NULL)
377                         rte_atomic32_inc(&obj_count);
378         }
379
380         /* create mutiple fbk tables simultaneously */
381         for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
382                 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d", lcore_self, i);
383                 lpm = rte_lpm_create(lpm_name, SOCKET_ID_ANY, &config);
384                 if (NULL == lpm)
385                         return -1;
386
387                 /* verify correct existing and then free all */
388                 if (lpm != rte_lpm_find_existing(lpm_name))
389                         return -1;
390
391                 rte_lpm_free(lpm);
392         }
393
394         /* verify free correct */
395         for (i = 0; i < MAX_LPM_ITER_TIMES; i++) {
396                 snprintf(lpm_name, sizeof(lpm_name), "fr_test_%d_%d",  lcore_self, i);
397                 if (NULL != rte_lpm_find_existing(lpm_name))
398                         return -1;
399         }
400
401         return 0;
402 }
403 #endif /* RTE_LIBRTE_LPM */
404
405 struct test_case{
406         case_func_t    func;
407         void*          arg;
408         case_clean_t   clean;
409         char           name[MAX_STRING_SIZE];
410 };
411
412 /* All test cases in the test suite */
413 struct test_case test_cases[] = {
414         { test_eal_init_once,     NULL,  NULL,         "eal init once" },
415         { ring_create_lookup,     NULL,  NULL,         "ring create/lookup" },
416         { mempool_create_lookup,  NULL,  NULL,         "mempool create/lookup" },
417 #ifdef RTE_LIBRTE_HASH
418         { hash_create_free,       NULL,  hash_clean,   "hash create/free" },
419         { fbk_create_free,        NULL,  fbk_clean,    "fbk create/free" },
420 #endif /* RTE_LIBRTE_HASH */
421 #ifdef RTE_LIBRTE_LPM
422         { lpm_create_free,        NULL,  lpm_clean,    "lpm create/free" },
423 #endif /* RTE_LIBRTE_LPM */
424 };
425
426 /**
427  * launch test case in two separate thread
428  */
429 static int
430 launch_test(struct test_case *pt_case)
431 {
432         int ret = 0;
433         unsigned lcore_id;
434         unsigned cores_save = rte_lcore_count();
435         unsigned cores = RTE_MIN(cores_save, MAX_LCORES);
436         unsigned count;
437
438         if (pt_case->func == NULL)
439                 return -1;
440
441         rte_atomic32_set(&obj_count, 0);
442         rte_atomic32_set(&synchro, 0);
443
444         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
445                 if (cores == 1)
446                         break;
447                 cores--;
448                 rte_eal_remote_launch(pt_case->func, pt_case->arg, lcore_id);
449         }
450
451         rte_atomic32_set(&synchro, 1);
452
453         if (pt_case->func(pt_case->arg) < 0)
454                 ret = -1;
455
456         cores = cores_save;
457         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
458                 if (cores == 1)
459                         break;
460                 cores--;
461                 if (rte_eal_wait_lcore(lcore_id) < 0)
462                         ret = -1;
463
464                 if (pt_case->clean != NULL)
465                         pt_case->clean(lcore_id);
466         }
467
468         count = rte_atomic32_read(&obj_count);
469         if (count != 1) {
470                 printf("%s: common object allocated %d times (should be 1)\n",
471                         pt_case->name, count);
472                 ret = -1;
473         }
474
475         return ret;
476 }
477
478 /**
479  * Main entry of func_reentrancy test
480  */
481 static int
482 test_func_reentrancy(void)
483 {
484         uint32_t case_id;
485         struct test_case *pt_case = NULL;
486
487         if (rte_lcore_count() <= 1) {
488                 printf("Not enough lcore for testing\n");
489                 return -1;
490         }
491         else if (rte_lcore_count() > MAX_LCORES)
492                 printf("Too many lcores, some cores will be disabled\n");
493
494         for (case_id = 0; case_id < sizeof(test_cases)/sizeof(struct test_case); case_id ++) {
495                 pt_case = &test_cases[case_id];
496                 if (pt_case->func == NULL)
497                         continue;
498
499                 if (launch_test(pt_case) < 0) {
500                         printf("Func-ReEnt CASE %"PRIu32": %s FAIL\n", case_id, pt_case->name);
501                         return -1;
502                 }
503                 printf("Func-ReEnt CASE %"PRIu32": %s PASS\n", case_id, pt_case->name);
504         }
505
506         return 0;
507 }
508
509 REGISTER_TEST_COMMAND(func_reentrancy_autotest, test_func_reentrancy);