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