New upstream version 17.11.1
[deb_dpdk.git] / test / test / test_ring.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 <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_memory.h>
46 #include <rte_launch.h>
47 #include <rte_cycles.h>
48 #include <rte_eal.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_malloc.h>
54 #include <rte_ring.h>
55 #include <rte_random.h>
56 #include <rte_errno.h>
57 #include <rte_hexdump.h>
58
59 #include "test.h"
60
61 /*
62  * Ring
63  * ====
64  *
65  * #. Basic tests: done on one core:
66  *
67  *    - Using single producer/single consumer functions:
68  *
69  *      - Enqueue one object, two objects, MAX_BULK objects
70  *      - Dequeue one object, two objects, MAX_BULK objects
71  *      - Check that dequeued pointers are correct
72  *
73  *    - Using multi producers/multi consumers functions:
74  *
75  *      - Enqueue one object, two objects, MAX_BULK objects
76  *      - Dequeue one object, two objects, MAX_BULK objects
77  *      - Check that dequeued pointers are correct
78  *
79  * #. Performance tests.
80  *
81  * Tests done in test_ring_perf.c
82  */
83
84 #define RING_SIZE 4096
85 #define MAX_BULK 32
86
87 static rte_atomic32_t synchro;
88
89 #define TEST_RING_VERIFY(exp)                                           \
90         if (!(exp)) {                                                   \
91                 printf("error at %s:%d\tcondition " #exp " failed\n",   \
92                     __func__, __LINE__);                                \
93                 rte_ring_dump(stdout, r);                               \
94                 return -1;                                              \
95         }
96
97 #define TEST_RING_FULL_EMTPY_ITER       8
98
99 /*
100  * helper routine for test_ring_basic
101  */
102 static int
103 test_ring_basic_full_empty(struct rte_ring *r, void * const src[], void *dst[])
104 {
105         unsigned i, rand;
106         const unsigned rsz = RING_SIZE - 1;
107
108         printf("Basic full/empty test\n");
109
110         for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
111
112                 /* random shift in the ring */
113                 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
114                 printf("%s: iteration %u, random shift: %u;\n",
115                     __func__, i, rand);
116                 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand,
117                                 NULL) != 0);
118                 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand,
119                                 NULL) == rand);
120
121                 /* fill the ring */
122                 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
123                 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
124                 TEST_RING_VERIFY(rsz == rte_ring_count(r));
125                 TEST_RING_VERIFY(rte_ring_full(r));
126                 TEST_RING_VERIFY(0 == rte_ring_empty(r));
127
128                 /* empty the ring */
129                 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz,
130                                 NULL) == rsz);
131                 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
132                 TEST_RING_VERIFY(0 == rte_ring_count(r));
133                 TEST_RING_VERIFY(0 == rte_ring_full(r));
134                 TEST_RING_VERIFY(rte_ring_empty(r));
135
136                 /* check data */
137                 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
138                 rte_ring_dump(stdout, r);
139         }
140         return 0;
141 }
142
143 static int
144 test_ring_basic(struct rte_ring *r)
145 {
146         void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
147         int ret;
148         unsigned i, num_elems;
149
150         /* alloc dummy object pointers */
151         src = malloc(RING_SIZE*2*sizeof(void *));
152         if (src == NULL)
153                 goto fail;
154
155         for (i = 0; i < RING_SIZE*2 ; i++) {
156                 src[i] = (void *)(unsigned long)i;
157         }
158         cur_src = src;
159
160         /* alloc some room for copied objects */
161         dst = malloc(RING_SIZE*2*sizeof(void *));
162         if (dst == NULL)
163                 goto fail;
164
165         memset(dst, 0, RING_SIZE*2*sizeof(void *));
166         cur_dst = dst;
167
168         printf("enqueue 1 obj\n");
169         ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
170         cur_src += 1;
171         if (ret == 0)
172                 goto fail;
173
174         printf("enqueue 2 objs\n");
175         ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
176         cur_src += 2;
177         if (ret == 0)
178                 goto fail;
179
180         printf("enqueue MAX_BULK objs\n");
181         ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
182         cur_src += MAX_BULK;
183         if (ret == 0)
184                 goto fail;
185
186         printf("dequeue 1 obj\n");
187         ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1, NULL);
188         cur_dst += 1;
189         if (ret == 0)
190                 goto fail;
191
192         printf("dequeue 2 objs\n");
193         ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2, NULL);
194         cur_dst += 2;
195         if (ret == 0)
196                 goto fail;
197
198         printf("dequeue MAX_BULK objs\n");
199         ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
200         cur_dst += MAX_BULK;
201         if (ret == 0)
202                 goto fail;
203
204         /* check data */
205         if (memcmp(src, dst, cur_dst - dst)) {
206                 rte_hexdump(stdout, "src", src, cur_src - src);
207                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
208                 printf("data after dequeue is not the same\n");
209                 goto fail;
210         }
211         cur_src = src;
212         cur_dst = dst;
213
214         printf("enqueue 1 obj\n");
215         ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
216         cur_src += 1;
217         if (ret == 0)
218                 goto fail;
219
220         printf("enqueue 2 objs\n");
221         ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
222         cur_src += 2;
223         if (ret == 0)
224                 goto fail;
225
226         printf("enqueue MAX_BULK objs\n");
227         ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
228         cur_src += MAX_BULK;
229         if (ret == 0)
230                 goto fail;
231
232         printf("dequeue 1 obj\n");
233         ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1, NULL);
234         cur_dst += 1;
235         if (ret == 0)
236                 goto fail;
237
238         printf("dequeue 2 objs\n");
239         ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2, NULL);
240         cur_dst += 2;
241         if (ret == 0)
242                 goto fail;
243
244         printf("dequeue MAX_BULK objs\n");
245         ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
246         cur_dst += MAX_BULK;
247         if (ret == 0)
248                 goto fail;
249
250         /* check data */
251         if (memcmp(src, dst, cur_dst - dst)) {
252                 rte_hexdump(stdout, "src", src, cur_src - src);
253                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
254                 printf("data after dequeue is not the same\n");
255                 goto fail;
256         }
257         cur_src = src;
258         cur_dst = dst;
259
260         printf("fill and empty the ring\n");
261         for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
262                 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
263                 cur_src += MAX_BULK;
264                 if (ret == 0)
265                         goto fail;
266                 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
267                 cur_dst += MAX_BULK;
268                 if (ret == 0)
269                         goto fail;
270         }
271
272         /* check data */
273         if (memcmp(src, dst, cur_dst - dst)) {
274                 rte_hexdump(stdout, "src", src, cur_src - src);
275                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
276                 printf("data after dequeue is not the same\n");
277                 goto fail;
278         }
279
280         if (test_ring_basic_full_empty(r, src, dst) != 0)
281                 goto fail;
282
283         cur_src = src;
284         cur_dst = dst;
285
286         printf("test default bulk enqueue / dequeue\n");
287         num_elems = 16;
288
289         cur_src = src;
290         cur_dst = dst;
291
292         ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
293         cur_src += num_elems;
294         if (ret == 0) {
295                 printf("Cannot enqueue\n");
296                 goto fail;
297         }
298         ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
299         cur_src += num_elems;
300         if (ret == 0) {
301                 printf("Cannot enqueue\n");
302                 goto fail;
303         }
304         ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
305         cur_dst += num_elems;
306         if (ret == 0) {
307                 printf("Cannot dequeue\n");
308                 goto fail;
309         }
310         ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
311         cur_dst += num_elems;
312         if (ret == 0) {
313                 printf("Cannot dequeue2\n");
314                 goto fail;
315         }
316
317         /* check data */
318         if (memcmp(src, dst, cur_dst - dst)) {
319                 rte_hexdump(stdout, "src", src, cur_src - src);
320                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
321                 printf("data after dequeue is not the same\n");
322                 goto fail;
323         }
324
325         cur_src = src;
326         cur_dst = dst;
327
328         ret = rte_ring_mp_enqueue(r, cur_src);
329         if (ret != 0)
330                 goto fail;
331
332         ret = rte_ring_mc_dequeue(r, cur_dst);
333         if (ret != 0)
334                 goto fail;
335
336         free(src);
337         free(dst);
338         return 0;
339
340  fail:
341         free(src);
342         free(dst);
343         return -1;
344 }
345
346 static int
347 test_ring_burst_basic(struct rte_ring *r)
348 {
349         void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
350         int ret;
351         unsigned i;
352
353         /* alloc dummy object pointers */
354         src = malloc(RING_SIZE*2*sizeof(void *));
355         if (src == NULL)
356                 goto fail;
357
358         for (i = 0; i < RING_SIZE*2 ; i++) {
359                 src[i] = (void *)(unsigned long)i;
360         }
361         cur_src = src;
362
363         /* alloc some room for copied objects */
364         dst = malloc(RING_SIZE*2*sizeof(void *));
365         if (dst == NULL)
366                 goto fail;
367
368         memset(dst, 0, RING_SIZE*2*sizeof(void *));
369         cur_dst = dst;
370
371         printf("Test SP & SC basic functions \n");
372         printf("enqueue 1 obj\n");
373         ret = rte_ring_sp_enqueue_burst(r, cur_src, 1, NULL);
374         cur_src += 1;
375         if (ret != 1)
376                 goto fail;
377
378         printf("enqueue 2 objs\n");
379         ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
380         cur_src += 2;
381         if (ret != 2)
382                 goto fail;
383
384         printf("enqueue MAX_BULK objs\n");
385         ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
386         cur_src += MAX_BULK;
387         if (ret != MAX_BULK)
388                 goto fail;
389
390         printf("dequeue 1 obj\n");
391         ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1, NULL);
392         cur_dst += 1;
393         if (ret != 1)
394                 goto fail;
395
396         printf("dequeue 2 objs\n");
397         ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
398         cur_dst += 2;
399         if (ret != 2)
400                 goto fail;
401
402         printf("dequeue MAX_BULK objs\n");
403         ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
404         cur_dst += MAX_BULK;
405         if (ret != MAX_BULK)
406                 goto fail;
407
408         /* check data */
409         if (memcmp(src, dst, cur_dst - dst)) {
410                 rte_hexdump(stdout, "src", src, cur_src - src);
411                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
412                 printf("data after dequeue is not the same\n");
413                 goto fail;
414         }
415
416         cur_src = src;
417         cur_dst = dst;
418
419         printf("Test enqueue without enough memory space \n");
420         for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
421                 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
422                 cur_src += MAX_BULK;
423                 if (ret != MAX_BULK)
424                         goto fail;
425         }
426
427         printf("Enqueue 2 objects, free entries = MAX_BULK - 2  \n");
428         ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
429         cur_src += 2;
430         if (ret != 2)
431                 goto fail;
432
433         printf("Enqueue the remaining entries = MAX_BULK - 2  \n");
434         /* Always one free entry left */
435         ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
436         cur_src += MAX_BULK - 3;
437         if (ret != MAX_BULK - 3)
438                 goto fail;
439
440         printf("Test if ring is full  \n");
441         if (rte_ring_full(r) != 1)
442                 goto fail;
443
444         printf("Test enqueue for a full entry  \n");
445         ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
446         if (ret != 0)
447                 goto fail;
448
449         printf("Test dequeue without enough objects \n");
450         for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
451                 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
452                 cur_dst += MAX_BULK;
453                 if (ret != MAX_BULK)
454                         goto fail;
455         }
456
457         /* Available memory space for the exact MAX_BULK entries */
458         ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
459         cur_dst += 2;
460         if (ret != 2)
461                 goto fail;
462
463         ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
464         cur_dst += MAX_BULK - 3;
465         if (ret != MAX_BULK - 3)
466                 goto fail;
467
468         printf("Test if ring is empty \n");
469         /* Check if ring is empty */
470         if (1 != rte_ring_empty(r))
471                 goto fail;
472
473         /* check data */
474         if (memcmp(src, dst, cur_dst - dst)) {
475                 rte_hexdump(stdout, "src", src, cur_src - src);
476                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
477                 printf("data after dequeue is not the same\n");
478                 goto fail;
479         }
480
481         cur_src = src;
482         cur_dst = dst;
483
484         printf("Test MP & MC basic functions \n");
485
486         printf("enqueue 1 obj\n");
487         ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
488         cur_src += 1;
489         if (ret != 1)
490                 goto fail;
491
492         printf("enqueue 2 objs\n");
493         ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
494         cur_src += 2;
495         if (ret != 2)
496                 goto fail;
497
498         printf("enqueue MAX_BULK objs\n");
499         ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
500         cur_src += MAX_BULK;
501         if (ret != MAX_BULK)
502                 goto fail;
503
504         printf("dequeue 1 obj\n");
505         ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1, NULL);
506         cur_dst += 1;
507         if (ret != 1)
508                 goto fail;
509
510         printf("dequeue 2 objs\n");
511         ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
512         cur_dst += 2;
513         if (ret != 2)
514                 goto fail;
515
516         printf("dequeue MAX_BULK objs\n");
517         ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
518         cur_dst += MAX_BULK;
519         if (ret != MAX_BULK)
520                 goto fail;
521
522         /* check data */
523         if (memcmp(src, dst, cur_dst - dst)) {
524                 rte_hexdump(stdout, "src", src, cur_src - src);
525                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
526                 printf("data after dequeue is not the same\n");
527                 goto fail;
528         }
529
530         cur_src = src;
531         cur_dst = dst;
532
533         printf("fill and empty the ring\n");
534         for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
535                 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
536                 cur_src += MAX_BULK;
537                 if (ret != MAX_BULK)
538                         goto fail;
539                 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
540                 cur_dst += MAX_BULK;
541                 if (ret != MAX_BULK)
542                         goto fail;
543         }
544
545         /* check data */
546         if (memcmp(src, dst, cur_dst - dst)) {
547                 rte_hexdump(stdout, "src", src, cur_src - src);
548                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
549                 printf("data after dequeue is not the same\n");
550                 goto fail;
551         }
552
553         cur_src = src;
554         cur_dst = dst;
555
556         printf("Test enqueue without enough memory space \n");
557         for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
558                 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
559                 cur_src += MAX_BULK;
560                 if (ret != MAX_BULK)
561                         goto fail;
562         }
563
564         /* Available memory space for the exact MAX_BULK objects */
565         ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
566         cur_src += 2;
567         if (ret != 2)
568                 goto fail;
569
570         ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
571         cur_src += MAX_BULK - 3;
572         if (ret != MAX_BULK - 3)
573                 goto fail;
574
575
576         printf("Test dequeue without enough objects \n");
577         for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
578                 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
579                 cur_dst += MAX_BULK;
580                 if (ret != MAX_BULK)
581                         goto fail;
582         }
583
584         /* Available objects - the exact MAX_BULK */
585         ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
586         cur_dst += 2;
587         if (ret != 2)
588                 goto fail;
589
590         ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
591         cur_dst += MAX_BULK - 3;
592         if (ret != MAX_BULK - 3)
593                 goto fail;
594
595         /* check data */
596         if (memcmp(src, dst, cur_dst - dst)) {
597                 rte_hexdump(stdout, "src", src, cur_src - src);
598                 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
599                 printf("data after dequeue is not the same\n");
600                 goto fail;
601         }
602
603         cur_src = src;
604         cur_dst = dst;
605
606         printf("Covering rte_ring_enqueue_burst functions \n");
607
608         ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
609         cur_src += 2;
610         if (ret != 2)
611                 goto fail;
612
613         ret = rte_ring_dequeue_burst(r, cur_dst, 2, NULL);
614         cur_dst += 2;
615         if (ret != 2)
616                 goto fail;
617
618         /* Free memory before test completed */
619         free(src);
620         free(dst);
621         return 0;
622
623  fail:
624         free(src);
625         free(dst);
626         return -1;
627 }
628
629 /*
630  * it will always fail to create ring with a wrong ring size number in this function
631  */
632 static int
633 test_ring_creation_with_wrong_size(void)
634 {
635         struct rte_ring * rp = NULL;
636
637         /* Test if ring size is not power of 2 */
638         rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
639         if (NULL != rp) {
640                 return -1;
641         }
642
643         /* Test if ring size is exceeding the limit */
644         rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
645         if (NULL != rp) {
646                 return -1;
647         }
648         return 0;
649 }
650
651 /*
652  * it tests if it would always fail to create ring with an used ring name
653  */
654 static int
655 test_ring_creation_with_an_used_name(void)
656 {
657         struct rte_ring * rp;
658
659         rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
660         if (NULL != rp)
661                 return -1;
662
663         return 0;
664 }
665
666 /*
667  * Test to if a non-power of 2 count causes the create
668  * function to fail correctly
669  */
670 static int
671 test_create_count_odd(void)
672 {
673         struct rte_ring *r = rte_ring_create("test_ring_count",
674                         4097, SOCKET_ID_ANY, 0 );
675         if(r != NULL){
676                 return -1;
677         }
678         return 0;
679 }
680
681 static int
682 test_lookup_null(void)
683 {
684         struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
685         if (rlp ==NULL)
686         if (rte_errno != ENOENT){
687                 printf( "test failed to returnn error on null pointer\n");
688                 return -1;
689         }
690         return 0;
691 }
692
693 /*
694  * it tests some more basic ring operations
695  */
696 static int
697 test_ring_basic_ex(void)
698 {
699         int ret = -1;
700         unsigned i;
701         struct rte_ring *rp = NULL;
702         void **obj = NULL;
703
704         obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
705         if (obj == NULL) {
706                 printf("test_ring_basic_ex fail to rte_malloc\n");
707                 goto fail_test;
708         }
709
710         rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
711                         RING_F_SP_ENQ | RING_F_SC_DEQ);
712         if (rp == NULL) {
713                 printf("test_ring_basic_ex fail to create ring\n");
714                 goto fail_test;
715         }
716
717         if (rte_ring_lookup("test_ring_basic_ex") != rp) {
718                 goto fail_test;
719         }
720
721         if (rte_ring_empty(rp) != 1) {
722                 printf("test_ring_basic_ex ring is not empty but it should be\n");
723                 goto fail_test;
724         }
725
726         printf("%u ring entries are now free\n", rte_ring_free_count(rp));
727
728         for (i = 0; i < RING_SIZE; i ++) {
729                 rte_ring_enqueue(rp, obj[i]);
730         }
731
732         if (rte_ring_full(rp) != 1) {
733                 printf("test_ring_basic_ex ring is not full but it should be\n");
734                 goto fail_test;
735         }
736
737         for (i = 0; i < RING_SIZE; i ++) {
738                 rte_ring_dequeue(rp, &obj[i]);
739         }
740
741         if (rte_ring_empty(rp) != 1) {
742                 printf("test_ring_basic_ex ring is not empty but it should be\n");
743                 goto fail_test;
744         }
745
746         /* Covering the ring burst operation */
747         ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
748         if (ret != 2) {
749                 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
750                 goto fail_test;
751         }
752
753         ret = rte_ring_dequeue_burst(rp, obj, 2, NULL);
754         if (ret != 2) {
755                 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
756                 goto fail_test;
757         }
758
759         ret = 0;
760 fail_test:
761         rte_ring_free(rp);
762         if (obj != NULL)
763                 rte_free(obj);
764
765         return ret;
766 }
767
768 static int
769 test_ring_with_exact_size(void)
770 {
771         struct rte_ring *std_ring = NULL, *exact_sz_ring = NULL;
772         void *ptr_array[16];
773         static const unsigned int ring_sz = RTE_DIM(ptr_array);
774         unsigned int i;
775         int ret = -1;
776
777         std_ring = rte_ring_create("std", ring_sz, rte_socket_id(),
778                         RING_F_SP_ENQ | RING_F_SC_DEQ);
779         if (std_ring == NULL) {
780                 printf("%s: error, can't create std ring\n", __func__);
781                 goto end;
782         }
783         exact_sz_ring = rte_ring_create("exact sz", ring_sz, rte_socket_id(),
784                         RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
785         if (exact_sz_ring == NULL) {
786                 printf("%s: error, can't create exact size ring\n", __func__);
787                 goto end;
788         }
789
790         /*
791          * Check that the exact size ring is bigger than the standard ring
792          */
793         if (rte_ring_get_size(std_ring) >= rte_ring_get_size(exact_sz_ring)) {
794                 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
795                                 __func__,
796                                 rte_ring_get_size(std_ring),
797                                 rte_ring_get_size(exact_sz_ring));
798                 goto end;
799         }
800         /*
801          * check that the exact_sz_ring can hold one more element than the
802          * standard ring. (16 vs 15 elements)
803          */
804         for (i = 0; i < ring_sz - 1; i++) {
805                 rte_ring_enqueue(std_ring, NULL);
806                 rte_ring_enqueue(exact_sz_ring, NULL);
807         }
808         if (rte_ring_enqueue(std_ring, NULL) != -ENOBUFS) {
809                 printf("%s: error, unexpected successful enqueue\n", __func__);
810                 goto end;
811         }
812         if (rte_ring_enqueue(exact_sz_ring, NULL) == -ENOBUFS) {
813                 printf("%s: error, enqueue failed\n", __func__);
814                 goto end;
815         }
816
817         /* check that dequeue returns the expected number of elements */
818         if (rte_ring_dequeue_burst(exact_sz_ring, ptr_array,
819                         RTE_DIM(ptr_array), NULL) != ring_sz) {
820                 printf("%s: error, failed to dequeue expected nb of elements\n",
821                                 __func__);
822                 goto end;
823         }
824
825         /* check that the capacity function returns expected value */
826         if (rte_ring_get_capacity(exact_sz_ring) != ring_sz) {
827                 printf("%s: error, incorrect ring capacity reported\n",
828                                 __func__);
829                 goto end;
830         }
831
832         ret = 0; /* all ok if we get here */
833 end:
834         rte_ring_free(std_ring);
835         rte_ring_free(exact_sz_ring);
836         return ret;
837 }
838
839 static int
840 test_ring(void)
841 {
842         struct rte_ring *r = NULL;
843
844         /* some more basic operations */
845         if (test_ring_basic_ex() < 0)
846                 goto test_fail;
847
848         rte_atomic32_init(&synchro);
849
850         r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
851         if (r == NULL)
852                 goto test_fail;
853
854         /* retrieve the ring from its name */
855         if (rte_ring_lookup("test") != r) {
856                 printf("Cannot lookup ring from its name\n");
857                 goto test_fail;
858         }
859
860         /* burst operations */
861         if (test_ring_burst_basic(r) < 0)
862                 goto test_fail;
863
864         /* basic operations */
865         if (test_ring_basic(r) < 0)
866                 goto test_fail;
867
868         /* basic operations */
869         if ( test_create_count_odd() < 0){
870                 printf("Test failed to detect odd count\n");
871                 goto test_fail;
872         } else
873                 printf("Test detected odd count\n");
874
875         if ( test_lookup_null() < 0){
876                 printf("Test failed to detect NULL ring lookup\n");
877                 goto test_fail;
878         } else
879                 printf("Test detected NULL ring lookup\n");
880
881         /* test of creating ring with wrong size */
882         if (test_ring_creation_with_wrong_size() < 0)
883                 goto test_fail;
884
885         /* test of creation ring with an used name */
886         if (test_ring_creation_with_an_used_name() < 0)
887                 goto test_fail;
888
889         if (test_ring_with_exact_size() < 0)
890                 goto test_fail;
891
892         /* dump the ring status */
893         rte_ring_list_dump(stdout);
894
895         rte_ring_free(r);
896
897         return 0;
898
899 test_fail:
900         rte_ring_free(r);
901
902         return -1;
903 }
904
905 REGISTER_TEST_COMMAND(ring_autotest, test_ring);