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