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