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,
122 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand) == rand);
125 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
126 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
127 TEST_RING_VERIFY(rsz == rte_ring_count(r));
128 TEST_RING_VERIFY(rte_ring_full(r));
129 TEST_RING_VERIFY(0 == rte_ring_empty(r));
132 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz) == 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));
139 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
140 rte_ring_dump(stdout, r);
146 test_ring_basic(void)
148 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
150 unsigned i, num_elems;
152 /* alloc dummy object pointers */
153 src = malloc(RING_SIZE*2*sizeof(void *));
157 for (i = 0; i < RING_SIZE*2 ; i++) {
158 src[i] = (void *)(unsigned long)i;
162 /* alloc some room for copied objects */
163 dst = malloc(RING_SIZE*2*sizeof(void *));
167 memset(dst, 0, RING_SIZE*2*sizeof(void *));
170 printf("enqueue 1 obj\n");
171 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
176 printf("enqueue 2 objs\n");
177 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
182 printf("enqueue MAX_BULK objs\n");
183 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
188 printf("dequeue 1 obj\n");
189 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1);
194 printf("dequeue 2 objs\n");
195 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2);
200 printf("dequeue MAX_BULK objs\n");
201 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK);
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");
216 printf("enqueue 1 obj\n");
217 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
222 printf("enqueue 2 objs\n");
223 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
228 printf("enqueue MAX_BULK objs\n");
229 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
234 printf("dequeue 1 obj\n");
235 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1);
240 printf("dequeue 2 objs\n");
241 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2);
246 printf("dequeue MAX_BULK objs\n");
247 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK);
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");
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);
268 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK);
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");
282 if (test_ring_basic_full_empty(src, dst) != 0)
288 printf("test default bulk enqueue / dequeue\n");
294 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
295 cur_src += num_elems;
297 printf("Cannot enqueue\n");
300 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
301 cur_src += num_elems;
303 printf("Cannot enqueue\n");
306 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems);
307 cur_dst += num_elems;
309 printf("Cannot dequeue\n");
312 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems);
313 cur_dst += num_elems;
315 printf("Cannot dequeue2\n");
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");
330 ret = rte_ring_mp_enqueue(r, cur_src);
334 ret = rte_ring_mc_dequeue(r, cur_dst);
349 test_ring_burst_basic(void)
351 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
355 /* alloc dummy object pointers */
356 src = malloc(RING_SIZE*2*sizeof(void *));
360 for (i = 0; i < RING_SIZE*2 ; i++) {
361 src[i] = (void *)(unsigned long)i;
365 /* alloc some room for copied objects */
366 dst = malloc(RING_SIZE*2*sizeof(void *));
370 memset(dst, 0, RING_SIZE*2*sizeof(void *));
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);
377 if ((ret & RTE_RING_SZ_MASK) != 1)
380 printf("enqueue 2 objs\n");
381 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
383 if ((ret & RTE_RING_SZ_MASK) != 2)
386 printf("enqueue MAX_BULK objs\n");
387 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
389 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
392 printf("dequeue 1 obj\n");
393 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1) ;
395 if ((ret & RTE_RING_SZ_MASK) != 1)
398 printf("dequeue 2 objs\n");
399 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2);
401 if ((ret & RTE_RING_SZ_MASK) != 2)
404 printf("dequeue MAX_BULK objs\n");
405 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK);
407 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
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");
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);
425 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK) {
430 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
431 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
433 if ((ret & RTE_RING_SZ_MASK) != 2)
436 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
437 /* Always one free entry left */
438 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
439 cur_src += MAX_BULK - 3;
440 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
443 printf("Test if ring is full \n");
444 if (rte_ring_full(r) != 1)
447 printf("Test enqueue for a full entry \n");
448 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
449 if ((ret & RTE_RING_SZ_MASK) != 0)
452 printf("Test dequeue without enough objects \n");
453 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
454 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK);
456 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
460 /* Available memory space for the exact MAX_BULK entries */
461 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2);
463 if ((ret & RTE_RING_SZ_MASK) != 2)
466 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK);
467 cur_dst += MAX_BULK - 3;
468 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
471 printf("Test if ring is empty \n");
472 /* Check if ring is empty */
473 if (1 != rte_ring_empty(r))
477 if (memcmp(src, dst, cur_dst - dst)) {
478 rte_hexdump(stdout, "src", src, cur_src - src);
479 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
480 printf("data after dequeue is not the same\n");
487 printf("Test MP & MC basic functions \n");
489 printf("enqueue 1 obj\n");
490 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
492 if ((ret & RTE_RING_SZ_MASK) != 1)
495 printf("enqueue 2 objs\n");
496 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
498 if ((ret & RTE_RING_SZ_MASK) != 2)
501 printf("enqueue MAX_BULK objs\n");
502 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
504 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
507 printf("dequeue 1 obj\n");
508 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1);
510 if ((ret & RTE_RING_SZ_MASK) != 1)
513 printf("dequeue 2 objs\n");
514 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2);
516 if ((ret & RTE_RING_SZ_MASK) != 2)
519 printf("dequeue MAX_BULK objs\n");
520 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
522 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
526 if (memcmp(src, dst, cur_dst - dst)) {
527 rte_hexdump(stdout, "src", src, cur_src - src);
528 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
529 printf("data after dequeue is not the same\n");
536 printf("fill and empty the ring\n");
537 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
538 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
540 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
542 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
544 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
549 if (memcmp(src, dst, cur_dst - dst)) {
550 rte_hexdump(stdout, "src", src, cur_src - src);
551 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
552 printf("data after dequeue is not the same\n");
559 printf("Test enqueue without enough memory space \n");
560 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
561 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
563 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
567 /* Available memory space for the exact MAX_BULK objects */
568 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
570 if ((ret & RTE_RING_SZ_MASK) != 2)
573 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
574 cur_src += MAX_BULK - 3;
575 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
579 printf("Test dequeue without enough objects \n");
580 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
581 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
583 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK)
587 /* Available objects - the exact MAX_BULK */
588 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2);
590 if ((ret & RTE_RING_SZ_MASK) != 2)
593 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK);
594 cur_dst += MAX_BULK - 3;
595 if ((ret & RTE_RING_SZ_MASK) != MAX_BULK - 3)
599 if (memcmp(src, dst, cur_dst - dst)) {
600 rte_hexdump(stdout, "src", src, cur_src - src);
601 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
602 printf("data after dequeue is not the same\n");
609 printf("Covering rte_ring_enqueue_burst functions \n");
611 ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
613 if ((ret & RTE_RING_SZ_MASK) != 2)
616 ret = rte_ring_dequeue_burst(r, cur_dst, 2);
621 /* Free memory before test completed */
633 * it will always fail to create ring with a wrong ring size number in this function
636 test_ring_creation_with_wrong_size(void)
638 struct rte_ring * rp = NULL;
640 /* Test if ring size is not power of 2 */
641 rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
646 /* Test if ring size is exceeding the limit */
647 rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
655 * it tests if it would always fail to create ring with an used ring name
658 test_ring_creation_with_an_used_name(void)
660 struct rte_ring * rp;
662 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
670 * Test to if a non-power of 2 count causes the create
671 * function to fail correctly
674 test_create_count_odd(void)
676 struct rte_ring *r = rte_ring_create("test_ring_count",
677 4097, SOCKET_ID_ANY, 0 );
685 test_lookup_null(void)
687 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
689 if (rte_errno != ENOENT){
690 printf( "test failed to returnn error on null pointer\n");
697 * it tests some more basic ring operations
700 test_ring_basic_ex(void)
704 struct rte_ring * rp;
707 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
709 printf("test_ring_basic_ex fail to rte_malloc\n");
713 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
714 RING_F_SP_ENQ | RING_F_SC_DEQ);
716 printf("test_ring_basic_ex fail to create ring\n");
720 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
724 if (rte_ring_empty(rp) != 1) {
725 printf("test_ring_basic_ex ring is not empty but it should be\n");
729 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
731 for (i = 0; i < RING_SIZE; i ++) {
732 rte_ring_enqueue(rp, obj[i]);
735 if (rte_ring_full(rp) != 1) {
736 printf("test_ring_basic_ex ring is not full but it should be\n");
740 for (i = 0; i < RING_SIZE; i ++) {
741 rte_ring_dequeue(rp, &obj[i]);
744 if (rte_ring_empty(rp) != 1) {
745 printf("test_ring_basic_ex ring is not empty but it should be\n");
749 /* Covering the ring burst operation */
750 ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
751 if ((ret & RTE_RING_SZ_MASK) != 2) {
752 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
756 ret = rte_ring_dequeue_burst(rp, obj, 2);
758 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
773 /* some more basic operations */
774 if (test_ring_basic_ex() < 0)
777 rte_atomic32_init(&synchro);
780 r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
784 /* retrieve the ring from its name */
785 if (rte_ring_lookup("test") != r) {
786 printf("Cannot lookup ring from its name\n");
790 /* burst operations */
791 if (test_ring_burst_basic() < 0)
794 /* basic operations */
795 if (test_ring_basic() < 0)
798 /* basic operations */
799 if ( test_create_count_odd() < 0){
800 printf ("Test failed to detect odd count\n");
804 printf ( "Test detected odd count\n");
806 if ( test_lookup_null() < 0){
807 printf ("Test failed to detect NULL ring lookup\n");
811 printf ( "Test detected NULL ring lookup \n");
813 /* test of creating ring with wrong size */
814 if (test_ring_creation_with_wrong_size() < 0)
817 /* test of creation ring with an used name */
818 if (test_ring_creation_with_an_used_name() < 0)
821 /* dump the ring status */
822 rte_ring_list_dump(stdout);
827 REGISTER_TEST_COMMAND(ring_autotest, test_ring);