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