4 * Copyright(c) 2010-2017 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.
36 #include <rte_event_ring.h>
44 * Test some basic ops for the event rings.
45 * Does not fully test everything, since most code is reused from rte_ring
46 * library and tested as part of the normal ring autotests.
49 #define RING_SIZE 4096
52 static struct rte_event_ring *r;
55 * ensure failure to create ring with a bad ring size
58 test_event_ring_creation_with_wrong_size(void)
60 struct rte_event_ring *rp = NULL;
62 /* Test if ring size is not power of 2 */
63 rp = rte_event_ring_create("test_bad_ring_size", RING_SIZE + 1,
68 /* Test if ring size is exceeding the limit */
69 rp = rte_event_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1),
77 * Test to check if a non-power-of-2 count causes the create
78 * function to fail correctly
81 test_create_count_odd(void)
83 struct rte_event_ring *r = rte_event_ring_create("test_event_ring_count",
84 4097, SOCKET_ID_ANY, 0);
91 test_lookup_null(void)
93 struct rte_event_ring *rlp = rte_event_ring_lookup("ring_not_found");
94 if (rlp == NULL && rte_errno != ENOENT) {
95 printf("test failed to return error on null pointer\n");
102 test_basic_event_enqueue_dequeue(void)
104 struct rte_event_ring *sr = NULL;
105 struct rte_event evs[16];
106 uint16_t ret, free_count, used_count;
108 memset(evs, 0, sizeof(evs));
109 sr = rte_event_ring_create("spsc_ring", 32, rte_socket_id(),
110 RING_F_SP_ENQ | RING_F_SC_DEQ);
112 printf("Failed to create sp/sc ring\n");
115 if (rte_event_ring_get_capacity(sr) != 31) {
116 printf("Error, invalid capacity\n");
120 /* test sp/sc ring */
121 if (rte_event_ring_count(sr) != 0) {
122 printf("Error, ring not empty as expected\n");
125 if (rte_event_ring_free_count(sr) != rte_event_ring_get_capacity(sr)) {
126 printf("Error, ring free count not as expected\n");
130 ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
131 if (ret != RTE_DIM(evs) ||
132 free_count != rte_event_ring_get_capacity(sr) - ret) {
133 printf("Error, status after enqueue is unexpected\n");
137 ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
138 if (ret != RTE_DIM(evs) - 1 ||
140 printf("Error, status after enqueue is unexpected\n");
144 ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
145 if (ret != RTE_DIM(evs) ||
146 used_count != rte_event_ring_get_capacity(sr) - ret) {
147 printf("Error, status after enqueue is unexpected\n");
150 ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
151 if (ret != RTE_DIM(evs) - 1 ||
153 printf("Error, status after enqueue is unexpected\n");
157 rte_event_ring_free(sr);
160 rte_event_ring_free(sr);
165 test_event_ring_with_exact_size(void)
167 struct rte_event_ring *std_ring, *exact_sz_ring;
168 struct rte_event ev = { .mbuf = NULL };
169 struct rte_event ev_array[16];
170 static const unsigned int ring_sz = RTE_DIM(ev_array);
173 std_ring = rte_event_ring_create("std", ring_sz, rte_socket_id(),
174 RING_F_SP_ENQ | RING_F_SC_DEQ);
175 if (std_ring == NULL) {
176 printf("%s: error, can't create std ring\n", __func__);
179 exact_sz_ring = rte_event_ring_create("exact sz",
180 ring_sz, rte_socket_id(),
181 RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
182 if (exact_sz_ring == NULL) {
183 printf("%s: error, can't create exact size ring\n", __func__);
188 * Check that the exact size ring is bigger than the standard ring
190 if (rte_event_ring_get_size(std_ring) >=
191 rte_event_ring_get_size(exact_sz_ring)) {
192 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
194 rte_event_ring_get_size(std_ring),
195 rte_event_ring_get_size(exact_sz_ring));
199 * check that the exact_sz_ring can hold one more element than the
200 * standard ring. (16 vs 15 elements)
202 for (i = 0; i < ring_sz - 1; i++) {
203 rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL);
204 rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL);
206 if (rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL) != 0) {
207 printf("%s: error, unexpected successful enqueue\n", __func__);
210 if (rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL) != 1) {
211 printf("%s: error, enqueue failed\n", __func__);
215 /* check that dequeue returns the expected number of elements */
216 if (rte_event_ring_dequeue_burst(exact_sz_ring, ev_array,
217 RTE_DIM(ev_array), NULL) != ring_sz) {
218 printf("%s: error, failed to dequeue expected nb of elements\n",
223 /* check that the capacity function returns expected value */
224 if (rte_event_ring_get_capacity(exact_sz_ring) != ring_sz) {
225 printf("%s: error, incorrect ring capacity reported\n",
230 rte_event_ring_free(std_ring);
231 rte_event_ring_free(exact_sz_ring);
236 test_event_ring(void)
239 r = rte_event_ring_create("ev_test", RING_SIZE,
244 /* retrieve the ring from its name */
245 if (rte_event_ring_lookup("ev_test") != r) {
246 printf("Cannot lookup ring from its name\n");
250 /* basic operations */
251 if (test_create_count_odd() < 0) {
252 printf("Test failed to detect odd count\n");
255 printf("Test detected odd count\n");
257 if (test_lookup_null() < 0) {
258 printf("Test failed to detect NULL ring lookup\n");
261 printf("Test detected NULL ring lookup\n");
263 /* test of creating ring with wrong size */
264 if (test_event_ring_creation_with_wrong_size() < 0)
267 if (test_basic_event_enqueue_dequeue() < 0)
270 if (test_event_ring_with_exact_size() < 0)
276 REGISTER_TEST_COMMAND(event_ring_autotest, test_event_ring);