Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2017 Intel Corporation
3 : : */
4 : :
5 : : #include "test.h"
6 : :
7 : : #include <string.h>
8 : :
9 : : #ifdef RTE_EXEC_ENV_WINDOWS
10 : : static int
11 : : test_event_ring(void)
12 : : {
13 : : printf("event_ring not supported on Windows, skipping test\n");
14 : : return TEST_SKIPPED;
15 : : }
16 : :
17 : : #else
18 : :
19 : : #include <rte_event_ring.h>
20 : :
21 : : /*
22 : : * Event Ring
23 : : * ===========
24 : : *
25 : : * Test some basic ops for the event rings.
26 : : * Does not fully test everything, since most code is reused from rte_ring
27 : : * library and tested as part of the normal ring autotests.
28 : : */
29 : :
30 : : #define RING_SIZE 4096
31 : : #define MAX_BULK 32
32 : :
33 : : static struct rte_event_ring *r;
34 : :
35 : : /*
36 : : * ensure failure to create ring with a bad ring size
37 : : */
38 : : static int
39 : 1 : test_event_ring_creation_with_wrong_size(void)
40 : : {
41 : : struct rte_event_ring *rp = NULL;
42 : :
43 : : /* Test if ring size is not power of 2 */
44 : 1 : rp = rte_event_ring_create("test_bad_ring_size", RING_SIZE + 1,
45 : : SOCKET_ID_ANY, 0);
46 [ + - ]: 1 : if (rp != NULL)
47 : : return -1;
48 : :
49 : : /* Test if ring size is exceeding the limit */
50 : 1 : rp = rte_event_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1),
51 : : SOCKET_ID_ANY, 0);
52 [ - + ]: 1 : if (rp != NULL)
53 : 0 : return -1;
54 : : return 0;
55 : : }
56 : :
57 : : /*
58 : : * Test to check if a non-power-of-2 count causes the create
59 : : * function to fail correctly
60 : : */
61 : : static int
62 : : test_create_count_odd(void)
63 : : {
64 : 1 : struct rte_event_ring *r = rte_event_ring_create("test_event_ring_count",
65 : : 4097, SOCKET_ID_ANY, 0);
66 [ + - ]: 1 : if (r != NULL)
67 : : return -1;
68 : : return 0;
69 : : }
70 : :
71 : : static int
72 : 1 : test_lookup_null(void)
73 : : {
74 : 1 : struct rte_event_ring *rlp = rte_event_ring_lookup("ring_not_found");
75 [ + - - + ]: 1 : if (rlp == NULL && rte_errno != ENOENT) {
76 : : printf("test failed to return error on null pointer\n");
77 : 0 : return -1;
78 : : }
79 : : return 0;
80 : : }
81 : :
82 : : static int
83 : 1 : test_basic_event_enqueue_dequeue(void)
84 : : {
85 : : struct rte_event_ring *sr = NULL;
86 : : struct rte_event evs[16];
87 : : uint16_t ret, free_count, used_count;
88 : :
89 : : memset(evs, 0, sizeof(evs));
90 : 1 : sr = rte_event_ring_create("spsc_ring", 32, rte_socket_id(),
91 : : RING_F_SP_ENQ | RING_F_SC_DEQ);
92 [ - + ]: 1 : if (sr == NULL) {
93 : : printf("Failed to create sp/sc ring\n");
94 : 0 : return -1;
95 : : }
96 [ - + ]: 1 : if (rte_event_ring_get_capacity(sr) != 31) {
97 : : printf("Error, invalid capacity\n");
98 : 0 : goto error;
99 : : }
100 : :
101 : : /* test sp/sc ring */
102 [ - + ]: 1 : if (rte_event_ring_count(sr) != 0) {
103 : : printf("Error, ring not empty as expected\n");
104 : 0 : goto error;
105 : : }
106 [ - + ]: 1 : if (rte_event_ring_free_count(sr) != rte_event_ring_get_capacity(sr)) {
107 : : printf("Error, ring free count not as expected\n");
108 : 0 : goto error;
109 : : }
110 : :
111 : 1 : ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
112 [ + - ]: 1 : if (ret != RTE_DIM(evs) ||
113 [ - + ]: 1 : free_count != rte_event_ring_get_capacity(sr) - ret) {
114 : : printf("Error, status after enqueue is unexpected\n");
115 : 0 : goto error;
116 : : }
117 : :
118 : 1 : ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
119 [ + - - + ]: 1 : if (ret != RTE_DIM(evs) - 1 ||
120 : : free_count != 0) {
121 : : printf("Error, status after enqueue is unexpected\n");
122 : 0 : goto error;
123 : : }
124 : :
125 : 1 : ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
126 [ + - ]: 1 : if (ret != RTE_DIM(evs) ||
127 [ - + ]: 1 : used_count != rte_event_ring_get_capacity(sr) - ret) {
128 : : printf("Error, status after enqueue is unexpected\n");
129 : 0 : goto error;
130 : : }
131 : 1 : ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
132 [ + - - + ]: 1 : if (ret != RTE_DIM(evs) - 1 ||
133 : : used_count != 0) {
134 : : printf("Error, status after enqueue is unexpected\n");
135 : 0 : goto error;
136 : : }
137 : :
138 : 1 : rte_event_ring_free(sr);
139 : 1 : return 0;
140 : 0 : error:
141 : 0 : rte_event_ring_free(sr);
142 : 0 : return -1;
143 : : }
144 : :
145 : : static int
146 : 1 : test_event_ring_with_exact_size(void)
147 : : {
148 : : struct rte_event_ring *std_ring, *exact_sz_ring;
149 : 1 : struct rte_event ev = { .mbuf = NULL };
150 : : struct rte_event ev_array[16];
151 : : static const unsigned int ring_sz = RTE_DIM(ev_array);
152 : : unsigned int i;
153 : :
154 : 1 : std_ring = rte_event_ring_create("std", ring_sz, rte_socket_id(),
155 : : RING_F_SP_ENQ | RING_F_SC_DEQ);
156 [ - + ]: 1 : if (std_ring == NULL) {
157 : : printf("%s: error, can't create std ring\n", __func__);
158 : 0 : return -1;
159 : : }
160 : 1 : exact_sz_ring = rte_event_ring_create("exact sz",
161 : 1 : ring_sz, rte_socket_id(),
162 : : RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
163 [ - + ]: 1 : if (exact_sz_ring == NULL) {
164 : : printf("%s: error, can't create exact size ring\n", __func__);
165 : 0 : return -1;
166 : : }
167 : :
168 : : /*
169 : : * Check that the exact size ring is bigger than the standard ring
170 : : */
171 [ - + ]: 1 : if (rte_event_ring_get_size(std_ring) >=
172 : : rte_event_ring_get_size(exact_sz_ring)) {
173 : : printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
174 : : __func__,
175 : : rte_event_ring_get_size(std_ring),
176 : : rte_event_ring_get_size(exact_sz_ring));
177 : 0 : return -1;
178 : : }
179 : : /*
180 : : * check that the exact_sz_ring can hold one more element than the
181 : : * standard ring. (16 vs 15 elements)
182 : : */
183 [ + + ]: 16 : for (i = 0; i < ring_sz - 1; i++) {
184 : : rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL);
185 : : rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL);
186 : : }
187 [ - + ]: 1 : if (rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL) != 0) {
188 : : printf("%s: error, unexpected successful enqueue\n", __func__);
189 : 0 : return -1;
190 : : }
191 [ - + ]: 1 : if (rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL) != 1) {
192 : : printf("%s: error, enqueue failed\n", __func__);
193 : 0 : return -1;
194 : : }
195 : :
196 : : /* check that dequeue returns the expected number of elements */
197 [ - + ]: 1 : if (rte_event_ring_dequeue_burst(exact_sz_ring, ev_array,
198 : : RTE_DIM(ev_array), NULL) != ring_sz) {
199 : : printf("%s: error, failed to dequeue expected nb of elements\n",
200 : : __func__);
201 : 0 : return -1;
202 : : }
203 : :
204 : : /* check that the capacity function returns expected value */
205 [ - + ]: 1 : if (rte_event_ring_get_capacity(exact_sz_ring) != ring_sz) {
206 : : printf("%s: error, incorrect ring capacity reported\n",
207 : : __func__);
208 : 0 : return -1;
209 : : }
210 : :
211 : 1 : rte_event_ring_free(std_ring);
212 : 1 : rte_event_ring_free(exact_sz_ring);
213 : 1 : return 0;
214 : : }
215 : :
216 : : static int
217 : 1 : test_event_ring(void)
218 : : {
219 [ + - ]: 1 : if (r == NULL)
220 : 1 : r = rte_event_ring_create("ev_test", RING_SIZE,
221 : : SOCKET_ID_ANY, 0);
222 [ + - ]: 1 : if (r == NULL)
223 : : return -1;
224 : :
225 : : /* retrieve the ring from its name */
226 [ - + ]: 1 : if (rte_event_ring_lookup("ev_test") != r) {
227 : : printf("Cannot lookup ring from its name\n");
228 : 0 : return -1;
229 : : }
230 : :
231 : : /* basic operations */
232 : : if (test_create_count_odd() < 0) {
233 : : printf("Test failed to detect odd count\n");
234 : 0 : return -1;
235 : : }
236 : : printf("Test detected odd count\n");
237 : :
238 [ - + ]: 1 : if (test_lookup_null() < 0) {
239 : : printf("Test failed to detect NULL ring lookup\n");
240 : 0 : return -1;
241 : : }
242 : : printf("Test detected NULL ring lookup\n");
243 : :
244 : : /* test of creating ring with wrong size */
245 [ + - ]: 1 : if (test_event_ring_creation_with_wrong_size() < 0)
246 : : return -1;
247 : :
248 [ + - ]: 1 : if (test_basic_event_enqueue_dequeue() < 0)
249 : : return -1;
250 : :
251 [ - + ]: 1 : if (test_event_ring_with_exact_size() < 0)
252 : 0 : return -1;
253 : :
254 : : return 0;
255 : : }
256 : :
257 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
258 : :
259 : 251 : REGISTER_FAST_TEST(event_ring_autotest, true, true, test_event_ring);
|