1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 * Copyright(c) 2020 Arm Limited
13 #include <sys/queue.h>
15 #include <rte_common.h>
17 #include <rte_memory.h>
18 #include <rte_launch.h>
19 #include <rte_cycles.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_malloc.h>
27 #include <rte_ring_elem.h>
28 #include <rte_random.h>
29 #include <rte_errno.h>
30 #include <rte_hexdump.h>
33 #include "test_ring.h"
39 * #. Functional tests. Tests single/bulk/burst, default/SPSC/MPMC,
40 * legacy/custom element size (4B, 8B, 16B, 20B) APIs.
41 * Some tests incorporate unaligned addresses for objects.
42 * The enqueued/dequeued data is validated for correctness.
44 * #. Performance tests are in test_ring_perf.c
47 #define RING_SIZE 4096
51 * Validate the return value of test cases and print details of the
52 * ring if validation fails
55 * Expression to validate return value.
57 * A pointer to the ring structure.
59 #define TEST_RING_VERIFY(exp, r, errst) do { \
61 printf("error at %s:%d\tcondition " #exp " failed\n", \
62 __func__, __LINE__); \
63 rte_ring_dump(stdout, (r)); \
68 #define TEST_RING_FULL_EMPTY_ITER 8
70 static const int esize[] = {-1, 4, 8, 16, 20};
72 /* Wrappers around the zero-copy APIs. The wrappers match
73 * the normal enqueue/dequeue API declarations.
76 test_ring_enqueue_zc_bulk(struct rte_ring *r, void * const *obj_table,
77 unsigned int n, unsigned int *free_space)
80 struct rte_ring_zc_data zcd;
82 ret = rte_ring_enqueue_zc_bulk_start(r, n, &zcd, free_space);
84 /* Copy the data to the ring */
85 test_ring_copy_to(&zcd, obj_table, sizeof(void *), ret);
86 rte_ring_enqueue_zc_finish(r, ret);
93 test_ring_enqueue_zc_bulk_elem(struct rte_ring *r, const void *obj_table,
94 unsigned int esize, unsigned int n, unsigned int *free_space)
97 struct rte_ring_zc_data zcd;
99 ret = rte_ring_enqueue_zc_bulk_elem_start(r, esize, n,
102 /* Copy the data to the ring */
103 test_ring_copy_to(&zcd, obj_table, esize, ret);
104 rte_ring_enqueue_zc_finish(r, ret);
111 test_ring_enqueue_zc_burst(struct rte_ring *r, void * const *obj_table,
112 unsigned int n, unsigned int *free_space)
115 struct rte_ring_zc_data zcd;
117 ret = rte_ring_enqueue_zc_burst_start(r, n, &zcd, free_space);
119 /* Copy the data to the ring */
120 test_ring_copy_to(&zcd, obj_table, sizeof(void *), ret);
121 rte_ring_enqueue_zc_finish(r, ret);
128 test_ring_enqueue_zc_burst_elem(struct rte_ring *r, const void *obj_table,
129 unsigned int esize, unsigned int n, unsigned int *free_space)
132 struct rte_ring_zc_data zcd;
134 ret = rte_ring_enqueue_zc_burst_elem_start(r, esize, n,
137 /* Copy the data to the ring */
138 test_ring_copy_to(&zcd, obj_table, esize, ret);
139 rte_ring_enqueue_zc_finish(r, ret);
146 test_ring_dequeue_zc_bulk(struct rte_ring *r, void **obj_table,
147 unsigned int n, unsigned int *available)
150 struct rte_ring_zc_data zcd;
152 ret = rte_ring_dequeue_zc_bulk_start(r, n, &zcd, available);
154 /* Copy the data from the ring */
155 test_ring_copy_from(&zcd, obj_table, sizeof(void *), ret);
156 rte_ring_dequeue_zc_finish(r, ret);
163 test_ring_dequeue_zc_bulk_elem(struct rte_ring *r, void *obj_table,
164 unsigned int esize, unsigned int n, unsigned int *available)
167 struct rte_ring_zc_data zcd;
169 ret = rte_ring_dequeue_zc_bulk_elem_start(r, esize, n,
172 /* Copy the data from the ring */
173 test_ring_copy_from(&zcd, obj_table, esize, ret);
174 rte_ring_dequeue_zc_finish(r, ret);
181 test_ring_dequeue_zc_burst(struct rte_ring *r, void **obj_table,
182 unsigned int n, unsigned int *available)
185 struct rte_ring_zc_data zcd;
187 ret = rte_ring_dequeue_zc_burst_start(r, n, &zcd, available);
189 /* Copy the data from the ring */
190 test_ring_copy_from(&zcd, obj_table, sizeof(void *), ret);
191 rte_ring_dequeue_zc_finish(r, ret);
198 test_ring_dequeue_zc_burst_elem(struct rte_ring *r, void *obj_table,
199 unsigned int esize, unsigned int n, unsigned int *available)
202 struct rte_ring_zc_data zcd;
204 ret = rte_ring_dequeue_zc_burst_elem_start(r, esize, n,
207 /* Copy the data from the ring */
208 test_ring_copy_from(&zcd, obj_table, esize, ret);
209 rte_ring_dequeue_zc_finish(r, ret);
215 static const struct {
218 uint32_t create_flags;
220 unsigned int (*flegacy)(struct rte_ring *r,
221 void * const *obj_table, unsigned int n,
222 unsigned int *free_space);
223 unsigned int (*felem)(struct rte_ring *r, const void *obj_table,
224 unsigned int esize, unsigned int n,
225 unsigned int *free_space);
228 unsigned int (*flegacy)(struct rte_ring *r,
229 void **obj_table, unsigned int n,
230 unsigned int *available);
231 unsigned int (*felem)(struct rte_ring *r, void *obj_table,
232 unsigned int esize, unsigned int n,
233 unsigned int *available);
235 } test_enqdeq_impl[] = {
237 .desc = "MP/MC sync mode",
238 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_DEF,
241 .flegacy = rte_ring_enqueue_bulk,
242 .felem = rte_ring_enqueue_bulk_elem,
245 .flegacy = rte_ring_dequeue_bulk,
246 .felem = rte_ring_dequeue_bulk_elem,
250 .desc = "SP/SC sync mode",
251 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_SPSC,
252 .create_flags = RING_F_SP_ENQ | RING_F_SC_DEQ,
254 .flegacy = rte_ring_sp_enqueue_bulk,
255 .felem = rte_ring_sp_enqueue_bulk_elem,
258 .flegacy = rte_ring_sc_dequeue_bulk,
259 .felem = rte_ring_sc_dequeue_bulk_elem,
263 .desc = "MP/MC sync mode",
264 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_MPMC,
267 .flegacy = rte_ring_mp_enqueue_bulk,
268 .felem = rte_ring_mp_enqueue_bulk_elem,
271 .flegacy = rte_ring_mc_dequeue_bulk,
272 .felem = rte_ring_mc_dequeue_bulk_elem,
276 .desc = "MP_RTS/MC_RTS sync mode",
277 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_DEF,
278 .create_flags = RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ,
280 .flegacy = rte_ring_enqueue_bulk,
281 .felem = rte_ring_enqueue_bulk_elem,
284 .flegacy = rte_ring_dequeue_bulk,
285 .felem = rte_ring_dequeue_bulk_elem,
289 .desc = "MP_HTS/MC_HTS sync mode",
290 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_DEF,
291 .create_flags = RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ,
293 .flegacy = rte_ring_enqueue_bulk,
294 .felem = rte_ring_enqueue_bulk_elem,
297 .flegacy = rte_ring_dequeue_bulk,
298 .felem = rte_ring_dequeue_bulk_elem,
302 .desc = "MP/MC sync mode",
303 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_DEF,
306 .flegacy = rte_ring_enqueue_burst,
307 .felem = rte_ring_enqueue_burst_elem,
310 .flegacy = rte_ring_dequeue_burst,
311 .felem = rte_ring_dequeue_burst_elem,
315 .desc = "SP/SC sync mode",
316 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_SPSC,
317 .create_flags = RING_F_SP_ENQ | RING_F_SC_DEQ,
319 .flegacy = rte_ring_sp_enqueue_burst,
320 .felem = rte_ring_sp_enqueue_burst_elem,
323 .flegacy = rte_ring_sc_dequeue_burst,
324 .felem = rte_ring_sc_dequeue_burst_elem,
328 .desc = "MP/MC sync mode",
329 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_MPMC,
332 .flegacy = rte_ring_mp_enqueue_burst,
333 .felem = rte_ring_mp_enqueue_burst_elem,
336 .flegacy = rte_ring_mc_dequeue_burst,
337 .felem = rte_ring_mc_dequeue_burst_elem,
341 .desc = "MP_RTS/MC_RTS sync mode",
342 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_DEF,
343 .create_flags = RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ,
345 .flegacy = rte_ring_enqueue_burst,
346 .felem = rte_ring_enqueue_burst_elem,
349 .flegacy = rte_ring_dequeue_burst,
350 .felem = rte_ring_dequeue_burst_elem,
354 .desc = "MP_HTS/MC_HTS sync mode",
355 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_DEF,
356 .create_flags = RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ,
358 .flegacy = rte_ring_enqueue_burst,
359 .felem = rte_ring_enqueue_burst_elem,
362 .flegacy = rte_ring_dequeue_burst,
363 .felem = rte_ring_dequeue_burst_elem,
367 .desc = "SP/SC sync mode (ZC)",
368 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_SPSC,
369 .create_flags = RING_F_SP_ENQ | RING_F_SC_DEQ,
371 .flegacy = test_ring_enqueue_zc_bulk,
372 .felem = test_ring_enqueue_zc_bulk_elem,
375 .flegacy = test_ring_dequeue_zc_bulk,
376 .felem = test_ring_dequeue_zc_bulk_elem,
380 .desc = "MP_HTS/MC_HTS sync mode (ZC)",
381 .api_type = TEST_RING_ELEM_BULK | TEST_RING_THREAD_DEF,
382 .create_flags = RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ,
384 .flegacy = test_ring_enqueue_zc_bulk,
385 .felem = test_ring_enqueue_zc_bulk_elem,
388 .flegacy = test_ring_dequeue_zc_bulk,
389 .felem = test_ring_dequeue_zc_bulk_elem,
393 .desc = "SP/SC sync mode (ZC)",
394 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_SPSC,
395 .create_flags = RING_F_SP_ENQ | RING_F_SC_DEQ,
397 .flegacy = test_ring_enqueue_zc_burst,
398 .felem = test_ring_enqueue_zc_burst_elem,
401 .flegacy = test_ring_dequeue_zc_burst,
402 .felem = test_ring_dequeue_zc_burst_elem,
406 .desc = "MP_HTS/MC_HTS sync mode (ZC)",
407 .api_type = TEST_RING_ELEM_BURST | TEST_RING_THREAD_DEF,
408 .create_flags = RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ,
410 .flegacy = test_ring_enqueue_zc_burst,
411 .felem = test_ring_enqueue_zc_burst_elem,
414 .flegacy = test_ring_dequeue_zc_burst,
415 .felem = test_ring_dequeue_zc_burst_elem,
421 test_ring_enq_impl(struct rte_ring *r, void **obj, int esize, unsigned int n,
422 unsigned int test_idx)
425 return test_enqdeq_impl[test_idx].enq.flegacy(r, obj, n, NULL);
427 return test_enqdeq_impl[test_idx].enq.felem(r, obj, esize, n,
432 test_ring_deq_impl(struct rte_ring *r, void **obj, int esize, unsigned int n,
433 unsigned int test_idx)
436 return test_enqdeq_impl[test_idx].deq.flegacy(r, obj, n, NULL);
438 return test_enqdeq_impl[test_idx].deq.felem(r, obj, esize, n,
443 test_ring_mem_init(void *obj, unsigned int count, int esize)
447 /* Legacy queue APIs? */
449 for (i = 0; i < count; i++)
450 ((void **)obj)[i] = (void *)(uintptr_t)i;
452 for (i = 0; i < (count * esize / sizeof(uint32_t)); i++)
453 ((uint32_t *)obj)[i] = i;
457 test_ring_mem_cmp(void *src, void *dst, unsigned int size)
461 ret = memcmp(src, dst, size);
463 rte_hexdump(stdout, "src", src, size);
464 rte_hexdump(stdout, "dst", dst, size);
465 printf("data after dequeue is not the same\n");
472 test_ring_print_test_string(const char *istr, unsigned int api_type, int esize)
474 printf("\n%s: ", istr);
477 printf("legacy APIs: ");
479 printf("elem APIs: element size %dB ", esize);
481 if (api_type == TEST_RING_IGNORE_API_TYPE)
484 if (api_type & TEST_RING_THREAD_DEF)
485 printf(": default enqueue/dequeue: ");
486 else if (api_type & TEST_RING_THREAD_SPSC)
488 else if (api_type & TEST_RING_THREAD_MPMC)
491 if (api_type & TEST_RING_ELEM_SINGLE)
493 else if (api_type & TEST_RING_ELEM_BULK)
495 else if (api_type & TEST_RING_ELEM_BURST)
500 * Various negative test cases.
503 test_ring_negative_tests(void)
505 struct rte_ring *rp = NULL;
506 struct rte_ring *rt = NULL;
509 /* Test with esize not a multiple of 4 */
510 rp = test_ring_create("test_bad_element_size", 23,
511 RING_SIZE + 1, SOCKET_ID_ANY, 0);
513 printf("Test failed to detect invalid element size\n");
518 for (i = 0; i < RTE_DIM(esize); i++) {
519 /* Test if ring size is not power of 2 */
520 rp = test_ring_create("test_bad_ring_size", esize[i],
521 RING_SIZE + 1, SOCKET_ID_ANY, 0);
523 printf("Test failed to detect odd count\n");
527 /* Test if ring size is exceeding the limit */
528 rp = test_ring_create("test_bad_ring_size", esize[i],
529 RTE_RING_SZ_MASK + 1, SOCKET_ID_ANY, 0);
531 printf("Test failed to detect limits\n");
535 /* Tests if lookup returns NULL on non-existing ring */
536 rp = rte_ring_lookup("ring_not_found");
537 if (rp != NULL && rte_errno != ENOENT) {
538 printf("Test failed to detect NULL ring lookup\n");
542 /* Test to if a non-power of 2 count causes the create
543 * function to fail correctly
545 rp = test_ring_create("test_ring_count", esize[i], 4097,
550 rp = test_ring_create("test_ring_negative", esize[i], RING_SIZE,
552 RING_F_SP_ENQ | RING_F_SC_DEQ);
554 printf("test_ring_negative fail to create ring\n");
558 TEST_RING_VERIFY(rte_ring_lookup("test_ring_negative") == rp,
561 TEST_RING_VERIFY(rte_ring_empty(rp) == 1, rp, goto test_fail);
563 /* Tests if it would always fail to create ring with an used
566 rt = test_ring_create("test_ring_negative", esize[i], RING_SIZE,
584 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
585 * Random number of elements are enqueued and dequeued.
588 test_ring_burst_bulk_tests1(unsigned int test_idx)
591 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
593 unsigned int i, j, temp_sz;
595 const unsigned int rsz = RING_SIZE - 1;
597 for (i = 0; i < RTE_DIM(esize); i++) {
598 test_ring_print_test_string(test_enqdeq_impl[test_idx].desc,
599 test_enqdeq_impl[test_idx].api_type, esize[i]);
601 /* Create the ring */
602 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
603 RING_SIZE, SOCKET_ID_ANY,
604 test_enqdeq_impl[test_idx].create_flags);
606 /* alloc dummy object pointers */
607 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
610 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
613 /* alloc some room for copied objects */
614 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
619 printf("Random full/empty test\n");
621 for (j = 0; j != TEST_RING_FULL_EMPTY_ITER; j++) {
622 /* random shift in the ring */
623 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
624 printf("%s: iteration %u, random shift: %u;\n",
626 ret = test_ring_enq_impl(r, cur_src, esize[i], rand,
628 TEST_RING_VERIFY(ret != 0, r, goto fail);
630 ret = test_ring_deq_impl(r, cur_dst, esize[i], rand,
632 TEST_RING_VERIFY(ret == rand, r, goto fail);
635 ret = test_ring_enq_impl(r, cur_src, esize[i], rsz,
637 TEST_RING_VERIFY(ret != 0, r, goto fail);
639 TEST_RING_VERIFY(rte_ring_free_count(r) == 0, r, goto fail);
640 TEST_RING_VERIFY(rsz == rte_ring_count(r), r, goto fail);
641 TEST_RING_VERIFY(rte_ring_full(r), r, goto fail);
642 TEST_RING_VERIFY(rte_ring_empty(r) == 0, r, goto fail);
645 ret = test_ring_deq_impl(r, cur_dst, esize[i], rsz,
647 TEST_RING_VERIFY(ret == (int)rsz, r, goto fail);
649 TEST_RING_VERIFY(rsz == rte_ring_free_count(r), r, goto fail);
650 TEST_RING_VERIFY(rte_ring_count(r) == 0, r, goto fail);
651 TEST_RING_VERIFY(rte_ring_full(r) == 0, r, goto fail);
652 TEST_RING_VERIFY(rte_ring_empty(r), r, goto fail);
655 temp_sz = rsz * sizeof(void *);
657 temp_sz = rsz * esize[i];
658 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
659 temp_sz) == 0, r, goto fail);
662 /* Free memory before test completed */
680 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
681 * Sequence of simple enqueues/dequeues and validate the enqueued and
685 test_ring_burst_bulk_tests2(unsigned int test_idx)
688 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
692 for (i = 0; i < RTE_DIM(esize); i++) {
693 test_ring_print_test_string(test_enqdeq_impl[test_idx].desc,
694 test_enqdeq_impl[test_idx].api_type, esize[i]);
696 /* Create the ring */
697 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
698 RING_SIZE, SOCKET_ID_ANY,
699 test_enqdeq_impl[test_idx].create_flags);
701 /* alloc dummy object pointers */
702 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
705 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
708 /* alloc some room for copied objects */
709 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
714 printf("enqueue 1 obj\n");
715 ret = test_ring_enq_impl(r, cur_src, esize[i], 1, test_idx);
716 TEST_RING_VERIFY(ret == 1, r, goto fail);
717 cur_src = test_ring_inc_ptr(cur_src, esize[i], 1);
719 printf("enqueue 2 objs\n");
720 ret = test_ring_enq_impl(r, cur_src, esize[i], 2, test_idx);
721 TEST_RING_VERIFY(ret == 2, r, goto fail);
722 cur_src = test_ring_inc_ptr(cur_src, esize[i], 2);
724 printf("enqueue MAX_BULK objs\n");
725 ret = test_ring_enq_impl(r, cur_src, esize[i], MAX_BULK,
727 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
729 printf("dequeue 1 obj\n");
730 ret = test_ring_deq_impl(r, cur_dst, esize[i], 1, test_idx);
731 TEST_RING_VERIFY(ret == 1, r, goto fail);
732 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 1);
734 printf("dequeue 2 objs\n");
735 ret = test_ring_deq_impl(r, cur_dst, esize[i], 2, test_idx);
736 TEST_RING_VERIFY(ret == 2, r, goto fail);
737 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
739 printf("dequeue MAX_BULK objs\n");
740 ret = test_ring_deq_impl(r, cur_dst, esize[i], MAX_BULK,
742 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
743 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], MAX_BULK);
746 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
747 RTE_PTR_DIFF(cur_dst, dst)) == 0,
750 /* Free memory before test completed */
768 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
769 * Enqueue and dequeue to cover the entire ring length.
772 test_ring_burst_bulk_tests3(unsigned int test_idx)
775 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
779 for (i = 0; i < RTE_DIM(esize); i++) {
780 test_ring_print_test_string(test_enqdeq_impl[test_idx].desc,
781 test_enqdeq_impl[test_idx].api_type, esize[i]);
783 /* Create the ring */
784 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
785 RING_SIZE, SOCKET_ID_ANY,
786 test_enqdeq_impl[test_idx].create_flags);
788 /* alloc dummy object pointers */
789 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
792 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
795 /* alloc some room for copied objects */
796 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
801 printf("fill and empty the ring\n");
802 for (j = 0; j < RING_SIZE / MAX_BULK; j++) {
803 ret = test_ring_enq_impl(r, cur_src, esize[i], MAX_BULK,
805 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
806 cur_src = test_ring_inc_ptr(cur_src, esize[i],
809 ret = test_ring_deq_impl(r, cur_dst, esize[i], MAX_BULK,
811 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
812 cur_dst = test_ring_inc_ptr(cur_dst, esize[i],
817 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
818 RTE_PTR_DIFF(cur_dst, dst)) == 0,
821 /* Free memory before test completed */
839 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
840 * Enqueue till the ring is full and dequeue till the ring becomes empty.
843 test_ring_burst_bulk_tests4(unsigned int test_idx)
846 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
849 unsigned int api_type, num_elems;
851 api_type = test_enqdeq_impl[test_idx].api_type;
853 for (i = 0; i < RTE_DIM(esize); i++) {
854 test_ring_print_test_string(test_enqdeq_impl[test_idx].desc,
855 test_enqdeq_impl[test_idx].api_type, esize[i]);
857 /* Create the ring */
858 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
859 RING_SIZE, SOCKET_ID_ANY,
860 test_enqdeq_impl[test_idx].create_flags);
862 /* alloc dummy object pointers */
863 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
866 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
869 /* alloc some room for copied objects */
870 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
875 printf("Test enqueue without enough memory space\n");
876 for (j = 0; j < (RING_SIZE/MAX_BULK - 1); j++) {
877 ret = test_ring_enq_impl(r, cur_src, esize[i], MAX_BULK,
879 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
880 cur_src = test_ring_inc_ptr(cur_src, esize[i],
884 printf("Enqueue 2 objects, free entries = MAX_BULK - 2\n");
885 ret = test_ring_enq_impl(r, cur_src, esize[i], 2, test_idx);
886 TEST_RING_VERIFY(ret == 2, r, goto fail);
887 cur_src = test_ring_inc_ptr(cur_src, esize[i], 2);
889 printf("Enqueue the remaining entries = MAX_BULK - 3\n");
890 /* Bulk APIs enqueue exact number of elements */
891 if ((api_type & TEST_RING_ELEM_BULK) == TEST_RING_ELEM_BULK)
892 num_elems = MAX_BULK - 3;
894 num_elems = MAX_BULK;
895 /* Always one free entry left */
896 ret = test_ring_enq_impl(r, cur_src, esize[i], num_elems,
898 TEST_RING_VERIFY(ret == MAX_BULK - 3, r, goto fail);
899 cur_src = test_ring_inc_ptr(cur_src, esize[i], MAX_BULK - 3);
901 printf("Test if ring is full\n");
902 TEST_RING_VERIFY(rte_ring_full(r) == 1, r, goto fail);
904 printf("Test enqueue for a full entry\n");
905 ret = test_ring_enq_impl(r, cur_src, esize[i], MAX_BULK,
907 TEST_RING_VERIFY(ret == 0, r, goto fail);
909 printf("Test dequeue without enough objects\n");
910 for (j = 0; j < RING_SIZE / MAX_BULK - 1; j++) {
911 ret = test_ring_deq_impl(r, cur_dst, esize[i], MAX_BULK,
913 TEST_RING_VERIFY(ret == MAX_BULK, r, goto fail);
914 cur_dst = test_ring_inc_ptr(cur_dst, esize[i],
918 /* Available memory space for the exact MAX_BULK entries */
919 ret = test_ring_deq_impl(r, cur_dst, esize[i], 2, test_idx);
920 TEST_RING_VERIFY(ret == 2, r, goto fail);
921 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
923 /* Bulk APIs enqueue exact number of elements */
924 if ((api_type & TEST_RING_ELEM_BULK) == TEST_RING_ELEM_BULK)
925 num_elems = MAX_BULK - 3;
927 num_elems = MAX_BULK;
928 ret = test_ring_deq_impl(r, cur_dst, esize[i], num_elems,
930 TEST_RING_VERIFY(ret == MAX_BULK - 3, r, goto fail);
931 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], MAX_BULK - 3);
933 printf("Test if ring is empty\n");
934 /* Check if ring is empty */
935 TEST_RING_VERIFY(rte_ring_empty(r) == 1, r, goto fail);
938 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
939 RTE_PTR_DIFF(cur_dst, dst)) == 0,
942 /* Free memory before test completed */
960 * Test default, single element, bulk and burst APIs
963 test_ring_basic_ex(void)
967 struct rte_ring *rp = NULL;
968 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
970 for (i = 0; i < RTE_DIM(esize); i++) {
971 rp = test_ring_create("test_ring_basic_ex", esize[i], RING_SIZE,
973 RING_F_SP_ENQ | RING_F_SC_DEQ);
975 printf("%s: failed to create ring\n", __func__);
979 /* alloc dummy object pointers */
980 src = test_ring_calloc(RING_SIZE, esize[i]);
982 printf("%s: failed to alloc src memory\n", __func__);
985 test_ring_mem_init(src, RING_SIZE, esize[i]);
988 /* alloc some room for copied objects */
989 dst = test_ring_calloc(RING_SIZE, esize[i]);
991 printf("%s: failed to alloc dst memory\n", __func__);
996 TEST_RING_VERIFY(rte_ring_lookup("test_ring_basic_ex") == rp,
999 TEST_RING_VERIFY(rte_ring_empty(rp) == 1, rp, goto fail_test);
1001 printf("%u ring entries are now free\n",
1002 rte_ring_free_count(rp));
1004 for (j = 0; j < RING_SIZE - 1; j++) {
1005 ret = test_ring_enqueue(rp, cur_src, esize[i], 1,
1006 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1007 TEST_RING_VERIFY(ret == 0, rp, goto fail_test);
1008 cur_src = test_ring_inc_ptr(cur_src, esize[i], 1);
1011 TEST_RING_VERIFY(rte_ring_full(rp) == 1, rp, goto fail_test);
1013 for (j = 0; j < RING_SIZE - 1; j++) {
1014 ret = test_ring_dequeue(rp, cur_dst, esize[i], 1,
1015 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1016 TEST_RING_VERIFY(ret == 0, rp, goto fail_test);
1017 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 1);
1020 TEST_RING_VERIFY(rte_ring_empty(rp) == 1, rp, goto fail_test);
1023 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
1024 RTE_PTR_DIFF(cur_dst, dst)) == 0,
1025 rp, goto fail_test);
1027 /* Following tests use the configured flags to decide
1030 /* reset memory of dst */
1031 memset(dst, 0, RTE_PTR_DIFF(cur_dst, dst));
1033 /* reset cur_src and cur_dst */
1037 /* Covering the ring burst operation */
1038 ret = test_ring_enqueue(rp, cur_src, esize[i], 2,
1039 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
1040 TEST_RING_VERIFY(ret == 2, rp, goto fail_test);
1041 cur_src = test_ring_inc_ptr(cur_src, esize[i], 2);
1043 ret = test_ring_dequeue(rp, cur_dst, esize[i], 2,
1044 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
1045 TEST_RING_VERIFY(ret == 2, rp, goto fail_test);
1046 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
1048 /* Covering the ring bulk operation */
1049 ret = test_ring_enqueue(rp, cur_src, esize[i], 2,
1050 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BULK);
1051 TEST_RING_VERIFY(ret == 2, rp, goto fail_test);
1053 ret = test_ring_dequeue(rp, cur_dst, esize[i], 2,
1054 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BULK);
1055 TEST_RING_VERIFY(ret == 2, rp, goto fail_test);
1056 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
1059 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
1060 RTE_PTR_DIFF(cur_dst, dst)) == 0,
1061 rp, goto fail_test);
1081 * Basic test cases with exact size ring.
1084 test_ring_with_exact_size(void)
1086 struct rte_ring *std_r = NULL, *exact_sz_r = NULL;
1087 void **src_orig = NULL, **dst_orig = NULL;
1088 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
1089 const unsigned int ring_sz = 16;
1093 for (i = 0; i < RTE_DIM(esize); i++) {
1094 test_ring_print_test_string("Test exact size ring",
1095 TEST_RING_IGNORE_API_TYPE,
1098 std_r = test_ring_create("std", esize[i], ring_sz,
1100 RING_F_SP_ENQ | RING_F_SC_DEQ);
1101 if (std_r == NULL) {
1102 printf("%s: error, can't create std ring\n", __func__);
1105 exact_sz_r = test_ring_create("exact sz", esize[i], ring_sz,
1107 RING_F_SP_ENQ | RING_F_SC_DEQ |
1109 if (exact_sz_r == NULL) {
1110 printf("%s: error, can't create exact size ring\n",
1115 /* alloc object pointers. Allocate one extra object
1116 * and create an unaligned address.
1118 src_orig = test_ring_calloc(17, esize[i]);
1119 if (src_orig == NULL)
1121 test_ring_mem_init(src_orig, 17, esize[i]);
1122 src = (void **)((uintptr_t)src_orig + 1);
1125 dst_orig = test_ring_calloc(17, esize[i]);
1126 if (dst_orig == NULL)
1128 dst = (void **)((uintptr_t)dst_orig + 1);
1132 * Check that the exact size ring is bigger than the
1135 TEST_RING_VERIFY(rte_ring_get_size(std_r) <=
1136 rte_ring_get_size(exact_sz_r),
1137 std_r, goto test_fail);
1140 * check that the exact_sz_ring can hold one more element
1141 * than the standard ring. (16 vs 15 elements)
1143 for (j = 0; j < ring_sz - 1; j++) {
1144 ret = test_ring_enqueue(std_r, cur_src, esize[i], 1,
1145 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1146 TEST_RING_VERIFY(ret == 0, std_r, goto test_fail);
1147 ret = test_ring_enqueue(exact_sz_r, cur_src, esize[i], 1,
1148 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1149 TEST_RING_VERIFY(ret == 0, exact_sz_r, goto test_fail);
1150 cur_src = test_ring_inc_ptr(cur_src, esize[i], 1);
1152 ret = test_ring_enqueue(std_r, cur_src, esize[i], 1,
1153 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1154 TEST_RING_VERIFY(ret == -ENOBUFS, std_r, goto test_fail);
1155 ret = test_ring_enqueue(exact_sz_r, cur_src, esize[i], 1,
1156 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
1157 TEST_RING_VERIFY(ret != -ENOBUFS, exact_sz_r, goto test_fail);
1159 /* check that dequeue returns the expected number of elements */
1160 ret = test_ring_dequeue(exact_sz_r, cur_dst, esize[i], ring_sz,
1161 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
1162 TEST_RING_VERIFY(ret == (int)ring_sz, exact_sz_r, goto test_fail);
1163 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], ring_sz);
1165 /* check that the capacity function returns expected value */
1166 TEST_RING_VERIFY(rte_ring_get_capacity(exact_sz_r) == ring_sz,
1167 exact_sz_r, goto test_fail);
1170 TEST_RING_VERIFY(test_ring_mem_cmp(src, dst,
1171 RTE_PTR_DIFF(cur_dst, dst)) == 0,
1172 exact_sz_r, goto test_fail);
1176 rte_ring_free(std_r);
1177 rte_ring_free(exact_sz_r);
1189 rte_ring_free(std_r);
1190 rte_ring_free(exact_sz_r);
1200 /* Negative test cases */
1201 if (test_ring_negative_tests() < 0)
1204 /* Some basic operations */
1205 if (test_ring_basic_ex() < 0)
1208 if (test_ring_with_exact_size() < 0)
1211 /* Burst and bulk operations with sp/sc, mp/mc and default.
1212 * The test cases are split into smaller test cases to
1213 * help clang compile faster.
1215 for (i = 0; i != RTE_DIM(test_enqdeq_impl); i++) {
1218 rc = test_ring_burst_bulk_tests1(i);
1222 rc = test_ring_burst_bulk_tests2(i);
1226 rc = test_ring_burst_bulk_tests3(i);
1230 rc = test_ring_burst_bulk_tests4(i);
1235 /* dump the ring status */
1236 rte_ring_list_dump(stdout);
1245 REGISTER_TEST_COMMAND(ring_autotest, test_ring);