4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
41 #include <sys/queue.h>
43 #include <rte_common.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_launch.h>
48 #include <rte_cycles.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_malloc.h>
56 #include <rte_random.h>
57 #include <rte_common.h>
58 #include <rte_errno.h>
59 #include <rte_hexdump.h>
67 * #. Basic tests: done on one core:
69 * - Using single producer/single consumer functions:
71 * - Enqueue one object, two objects, MAX_BULK objects
72 * - Dequeue one object, two objects, MAX_BULK objects
73 * - Check that dequeued pointers are correct
75 * - Using multi producers/multi consumers functions:
77 * - Enqueue one object, two objects, MAX_BULK objects
78 * - Dequeue one object, two objects, MAX_BULK objects
79 * - Check that dequeued pointers are correct
81 * #. Performance tests.
83 * Tests done in test_ring_perf.c
86 #define RING_SIZE 4096
89 static rte_atomic32_t synchro;
91 static struct rte_ring *r;
93 #define TEST_RING_VERIFY(exp) \
95 printf("error at %s:%d\tcondition " #exp " failed\n", \
96 __func__, __LINE__); \
97 rte_ring_dump(stdout, r); \
101 #define TEST_RING_FULL_EMTPY_ITER 8
104 * helper routine for test_ring_basic
107 test_ring_basic_full_empty(void * const src[], void *dst[])
110 const unsigned rsz = RING_SIZE - 1;
112 printf("Basic full/empty test\n");
114 for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
116 /* random shift in the ring */
117 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
118 printf("%s: iteration %u, random shift: %u;\n",
120 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand) != 0);
121 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand) == rand);
124 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz) != 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));
131 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz) == rsz);
132 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
133 TEST_RING_VERIFY(0 == rte_ring_count(r));
134 TEST_RING_VERIFY(0 == rte_ring_full(r));
135 TEST_RING_VERIFY(rte_ring_empty(r));
138 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
139 rte_ring_dump(stdout, r);
145 test_ring_basic(void)
147 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
149 unsigned i, num_elems;
151 /* alloc dummy object pointers */
152 src = malloc(RING_SIZE*2*sizeof(void *));
156 for (i = 0; i < RING_SIZE*2 ; i++) {
157 src[i] = (void *)(unsigned long)i;
161 /* alloc some room for copied objects */
162 dst = malloc(RING_SIZE*2*sizeof(void *));
166 memset(dst, 0, RING_SIZE*2*sizeof(void *));
169 printf("enqueue 1 obj\n");
170 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1);
175 printf("enqueue 2 objs\n");
176 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2);
181 printf("enqueue MAX_BULK objs\n");
182 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK);
187 printf("dequeue 1 obj\n");
188 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1);
193 printf("dequeue 2 objs\n");
194 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2);
199 printf("dequeue MAX_BULK objs\n");
200 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK);
206 if (memcmp(src, dst, cur_dst - dst)) {
207 rte_hexdump(stdout, "src", src, cur_src - src);
208 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
209 printf("data after dequeue is not the same\n");
215 printf("enqueue 1 obj\n");
216 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1);
221 printf("enqueue 2 objs\n");
222 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2);
227 printf("enqueue MAX_BULK objs\n");
228 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK);
233 printf("dequeue 1 obj\n");
234 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1);
239 printf("dequeue 2 objs\n");
240 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2);
245 printf("dequeue MAX_BULK objs\n");
246 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK);
252 if (memcmp(src, dst, cur_dst - dst)) {
253 rte_hexdump(stdout, "src", src, cur_src - src);
254 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
255 printf("data after dequeue is not the same\n");
261 printf("fill and empty the ring\n");
262 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
263 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK);
267 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK);
274 if (memcmp(src, dst, cur_dst - dst)) {
275 rte_hexdump(stdout, "src", src, cur_src - src);
276 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
277 printf("data after dequeue is not the same\n");
281 if (test_ring_basic_full_empty(src, dst) != 0)
287 printf("test default bulk enqueue / dequeue\n");
293 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems);
294 cur_src += num_elems;
296 printf("Cannot enqueue\n");
299 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems);
300 cur_src += num_elems;
302 printf("Cannot enqueue\n");
305 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems);
306 cur_dst += num_elems;
308 printf("Cannot dequeue\n");
311 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems);
312 cur_dst += num_elems;
314 printf("Cannot dequeue2\n");
319 if (memcmp(src, dst, cur_dst - dst)) {
320 rte_hexdump(stdout, "src", src, cur_src - src);
321 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
322 printf("data after dequeue is not the same\n");
329 ret = rte_ring_mp_enqueue(r, cur_src);
333 ret = rte_ring_mc_dequeue(r, cur_dst);
348 test_ring_burst_basic(void)
350 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
354 /* alloc dummy object pointers */
355 src = malloc(RING_SIZE*2*sizeof(void *));
359 for (i = 0; i < RING_SIZE*2 ; i++) {
360 src[i] = (void *)(unsigned long)i;
364 /* alloc some room for copied objects */
365 dst = malloc(RING_SIZE*2*sizeof(void *));
369 memset(dst, 0, RING_SIZE*2*sizeof(void *));
372 printf("Test SP & SC basic functions \n");
373 printf("enqueue 1 obj\n");
374 ret = rte_ring_sp_enqueue_burst(r, cur_src, 1);
376 if ((ret & RTE_RING_SZ_MASK) != 1)
379 printf("enqueue 2 objs\n");
380 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2);
382 if ((ret & RTE_RING_SZ_MASK) != 2)
385 printf("enqueue MAX_BULK objs\n");
386 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK) ;
388 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
391 printf("dequeue 1 obj\n");
392 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1) ;
394 if ((ret & RTE_RING_SZ_MASK) != 1)
397 printf("dequeue 2 objs\n");
398 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2);
400 if ((ret & RTE_RING_SZ_MASK) != 2)
403 printf("dequeue MAX_BULK objs\n");
404 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK);
406 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
410 if (memcmp(src, dst, cur_dst - dst)) {
411 rte_hexdump(stdout, "src", src, cur_src - src);
412 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
413 printf("data after dequeue is not the same\n");
420 printf("Test enqueue without enough memory space \n");
421 for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
422 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK);
424 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK) {
429 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
430 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2);
432 if ((ret & RTE_RING_SZ_MASK) != 2)
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);
438 cur_src += MAX_BULK - 3;
439 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
442 printf("Test if ring is full \n");
443 if (rte_ring_full(r) != 1)
446 printf("Test enqueue for a full entry \n");
447 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK);
448 if ((ret & RTE_RING_SZ_MASK) != 0)
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);
455 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
459 /* Available memory space for the exact MAX_BULK entries */
460 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2);
462 if ((ret & RTE_RING_SZ_MASK) != 2)
465 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK);
466 cur_dst += MAX_BULK - 3;
467 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
470 printf("Test if ring is empty \n");
471 /* Check if ring is empty */
472 if (1 != rte_ring_empty(r))
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");
486 printf("Test MP & MC basic functions \n");
488 printf("enqueue 1 obj\n");
489 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1);
491 if ((ret & RTE_RING_SZ_MASK) != 1)
494 printf("enqueue 2 objs\n");
495 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2);
497 if ((ret & RTE_RING_SZ_MASK) != 2)
500 printf("enqueue MAX_BULK objs\n");
501 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK);
503 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
506 printf("dequeue 1 obj\n");
507 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1);
509 if ((ret & RTE_RING_SZ_MASK) != 1)
512 printf("dequeue 2 objs\n");
513 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2);
515 if ((ret & RTE_RING_SZ_MASK) != 2)
518 printf("dequeue MAX_BULK objs\n");
519 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
521 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
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");
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);
539 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
541 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
543 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
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");
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);
562 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
566 /* Available memory space for the exact MAX_BULK objects */
567 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2);
569 if ((ret & RTE_RING_SZ_MASK) != 2)
572 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK);
573 cur_src += MAX_BULK - 3;
574 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
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);
582 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
586 /* Available objects - the exact MAX_BULK */
587 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2);
589 if ((ret & RTE_RING_SZ_MASK) != 2)
592 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
593 cur_dst += MAX_BULK - 3;
594 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
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");
608 printf("Covering rte_ring_enqueue_burst functions \n");
610 ret = rte_ring_enqueue_burst(r, cur_src, 2);
612 if ((ret & RTE_RING_SZ_MASK) != 2)
615 ret = rte_ring_dequeue_burst(r, cur_dst, 2);
620 /* Free memory before test completed */
632 * it will always fail to create ring with a wrong ring size number in this function
635 test_ring_creation_with_wrong_size(void)
637 struct rte_ring * rp = NULL;
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);
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);
654 * it tests if it would always fail to create ring with an used ring name
657 test_ring_creation_with_an_used_name(void)
659 struct rte_ring * rp;
661 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
669 * Test to if a non-power of 2 count causes the create
670 * function to fail correctly
673 test_create_count_odd(void)
675 struct rte_ring *r = rte_ring_create("test_ring_count",
676 4097, SOCKET_ID_ANY, 0 );
684 test_lookup_null(void)
686 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
688 if (rte_errno != ENOENT){
689 printf( "test failed to returnn error on null pointer\n");
696 * it tests some more basic ring operations
699 test_ring_basic_ex(void)
703 struct rte_ring * rp;
706 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
708 printf("test_ring_basic_ex fail to rte_malloc\n");
712 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
713 RING_F_SP_ENQ | RING_F_SC_DEQ);
715 printf("test_ring_basic_ex fail to create ring\n");
719 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
723 if (rte_ring_empty(rp) != 1) {
724 printf("test_ring_basic_ex ring is not empty but it should be\n");
728 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
730 for (i = 0; i < RING_SIZE; i ++) {
731 rte_ring_enqueue(rp, obj[i]);
734 if (rte_ring_full(rp) != 1) {
735 printf("test_ring_basic_ex ring is not full but it should be\n");
739 for (i = 0; i < RING_SIZE; i ++) {
740 rte_ring_dequeue(rp, &obj[i]);
743 if (rte_ring_empty(rp) != 1) {
744 printf("test_ring_basic_ex ring is not empty but it should be\n");
748 /* Covering the ring burst operation */
749 ret = rte_ring_enqueue_burst(rp, obj, 2);
750 if ((ret & RTE_RING_SZ_MASK) != 2) {
751 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
755 ret = rte_ring_dequeue_burst(rp, obj, 2);
757 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
772 /* some more basic operations */
773 if (test_ring_basic_ex() < 0)
776 rte_atomic32_init(&synchro);
779 r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
783 /* retrieve the ring from its name */
784 if (rte_ring_lookup("test") != r) {
785 printf("Cannot lookup ring from its name\n");
789 /* burst operations */
790 if (test_ring_burst_basic() < 0)
793 /* basic operations */
794 if (test_ring_basic() < 0)
797 /* basic operations */
798 if ( test_create_count_odd() < 0){
799 printf ("Test failed to detect odd count\n");
803 printf ( "Test detected odd count\n");
805 if ( test_lookup_null() < 0){
806 printf ("Test failed to detect NULL ring lookup\n");
810 printf ( "Test detected NULL ring lookup \n");
812 /* test of creating ring with wrong size */
813 if (test_ring_creation_with_wrong_size() < 0)
816 /* test of creation ring with an used name */
817 if (test_ring_creation_with_an_used_name() < 0)
820 /* dump the ring status */
821 rte_ring_list_dump(stdout);
826 REGISTER_TEST_COMMAND(ring_autotest, test_ring);