ring: support configurable element size
[dpdk.git] / lib / librte_ring / rte_ring.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2010-2015 Intel Corporation
4  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
5  * All rights reserved.
6  * Derived from FreeBSD's bufring.h
7  * Used as BSD-3 Licensed with permission from Kip Macy.
8  */
9
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <inttypes.h>
15 #include <errno.h>
16 #include <sys/queue.h>
17
18 #include <rte_common.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memzone.h>
22 #include <rte_malloc.h>
23 #include <rte_launch.h>
24 #include <rte_eal.h>
25 #include <rte_eal_memconfig.h>
26 #include <rte_atomic.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_errno.h>
31 #include <rte_string_fns.h>
32 #include <rte_spinlock.h>
33 #include <rte_tailq.h>
34
35 #include "rte_ring.h"
36 #include "rte_ring_elem.h"
37
38 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
39
40 static struct rte_tailq_elem rte_ring_tailq = {
41         .name = RTE_TAILQ_RING_NAME,
42 };
43 EAL_REGISTER_TAILQ(rte_ring_tailq)
44
45 /* true if x is a power of 2 */
46 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
47
48 /* return the size of memory occupied by a ring */
49 ssize_t
50 rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
51 {
52         ssize_t sz;
53
54         /* Check if element size is a multiple of 4B */
55         if (esize % 4 != 0) {
56                 RTE_LOG(ERR, RING, "element size is not a multiple of 4\n");
57
58                 return -EINVAL;
59         }
60
61         /* count must be a power of 2 */
62         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
63                 RTE_LOG(ERR, RING,
64                         "Requested number of elements is invalid, must be power of 2, and not exceed %u\n",
65                         RTE_RING_SZ_MASK);
66
67                 return -EINVAL;
68         }
69
70         sz = sizeof(struct rte_ring) + count * esize;
71         sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
72         return sz;
73 }
74
75 /* return the size of memory occupied by a ring */
76 ssize_t
77 rte_ring_get_memsize(unsigned int count)
78 {
79         return rte_ring_get_memsize_elem(sizeof(void *), count);
80 }
81
82 void
83 rte_ring_reset(struct rte_ring *r)
84 {
85         r->prod.head = r->cons.head = 0;
86         r->prod.tail = r->cons.tail = 0;
87 }
88
89 int
90 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
91         unsigned flags)
92 {
93         int ret;
94
95         /* compilation-time checks */
96         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
97                           RTE_CACHE_LINE_MASK) != 0);
98         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
99                           RTE_CACHE_LINE_MASK) != 0);
100         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
101                           RTE_CACHE_LINE_MASK) != 0);
102
103         /* init the ring structure */
104         memset(r, 0, sizeof(*r));
105         ret = strlcpy(r->name, name, sizeof(r->name));
106         if (ret < 0 || ret >= (int)sizeof(r->name))
107                 return -ENAMETOOLONG;
108         r->flags = flags;
109         r->prod.single = (flags & RING_F_SP_ENQ) ? __IS_SP : __IS_MP;
110         r->cons.single = (flags & RING_F_SC_DEQ) ? __IS_SC : __IS_MC;
111
112         if (flags & RING_F_EXACT_SZ) {
113                 r->size = rte_align32pow2(count + 1);
114                 r->mask = r->size - 1;
115                 r->capacity = count;
116         } else {
117                 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
118                         RTE_LOG(ERR, RING,
119                                 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
120                                 RTE_RING_SZ_MASK);
121                         return -EINVAL;
122                 }
123                 r->size = count;
124                 r->mask = count - 1;
125                 r->capacity = r->mask;
126         }
127         r->prod.head = r->cons.head = 0;
128         r->prod.tail = r->cons.tail = 0;
129
130         return 0;
131 }
132
133 /* create the ring for a given element size */
134 struct rte_ring *
135 rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
136                 int socket_id, unsigned int flags)
137 {
138         char mz_name[RTE_MEMZONE_NAMESIZE];
139         struct rte_ring *r;
140         struct rte_tailq_entry *te;
141         const struct rte_memzone *mz;
142         ssize_t ring_size;
143         int mz_flags = 0;
144         struct rte_ring_list* ring_list = NULL;
145         const unsigned int requested_count = count;
146         int ret;
147
148         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
149
150         /* for an exact size ring, round up from count to a power of two */
151         if (flags & RING_F_EXACT_SZ)
152                 count = rte_align32pow2(count + 1);
153
154         ring_size = rte_ring_get_memsize_elem(esize, count);
155         if (ring_size < 0) {
156                 rte_errno = ring_size;
157                 return NULL;
158         }
159
160         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
161                 RTE_RING_MZ_PREFIX, name);
162         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
163                 rte_errno = ENAMETOOLONG;
164                 return NULL;
165         }
166
167         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
168         if (te == NULL) {
169                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
170                 rte_errno = ENOMEM;
171                 return NULL;
172         }
173
174         rte_mcfg_tailq_write_lock();
175
176         /* reserve a memory zone for this ring. If we can't get rte_config or
177          * we are secondary process, the memzone_reserve function will set
178          * rte_errno for us appropriately - hence no check in this this function */
179         mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
180                                          mz_flags, __alignof__(*r));
181         if (mz != NULL) {
182                 r = mz->addr;
183                 /* no need to check return value here, we already checked the
184                  * arguments above */
185                 rte_ring_init(r, name, requested_count, flags);
186
187                 te->data = (void *) r;
188                 r->memzone = mz;
189
190                 TAILQ_INSERT_TAIL(ring_list, te, next);
191         } else {
192                 r = NULL;
193                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
194                 rte_free(te);
195         }
196         rte_mcfg_tailq_write_unlock();
197
198         return r;
199 }
200
201 /* create the ring */
202 struct rte_ring *
203 rte_ring_create(const char *name, unsigned int count, int socket_id,
204                 unsigned int flags)
205 {
206         return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
207                 flags);
208 }
209
210 /* free the ring */
211 void
212 rte_ring_free(struct rte_ring *r)
213 {
214         struct rte_ring_list *ring_list = NULL;
215         struct rte_tailq_entry *te;
216
217         if (r == NULL)
218                 return;
219
220         /*
221          * Ring was not created with rte_ring_create,
222          * therefore, there is no memzone to free.
223          */
224         if (r->memzone == NULL) {
225                 RTE_LOG(ERR, RING,
226                         "Cannot free ring, not created with rte_ring_create()\n");
227                 return;
228         }
229
230         if (rte_memzone_free(r->memzone) != 0) {
231                 RTE_LOG(ERR, RING, "Cannot free memory\n");
232                 return;
233         }
234
235         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
236         rte_mcfg_tailq_write_lock();
237
238         /* find out tailq entry */
239         TAILQ_FOREACH(te, ring_list, next) {
240                 if (te->data == (void *) r)
241                         break;
242         }
243
244         if (te == NULL) {
245                 rte_mcfg_tailq_write_unlock();
246                 return;
247         }
248
249         TAILQ_REMOVE(ring_list, te, next);
250
251         rte_mcfg_tailq_write_unlock();
252
253         rte_free(te);
254 }
255
256 /* dump the status of the ring on the console */
257 void
258 rte_ring_dump(FILE *f, const struct rte_ring *r)
259 {
260         fprintf(f, "ring <%s>@%p\n", r->name, r);
261         fprintf(f, "  flags=%x\n", r->flags);
262         fprintf(f, "  size=%"PRIu32"\n", r->size);
263         fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
264         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
265         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
266         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
267         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
268         fprintf(f, "  used=%u\n", rte_ring_count(r));
269         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
270 }
271
272 /* dump the status of all rings on the console */
273 void
274 rte_ring_list_dump(FILE *f)
275 {
276         const struct rte_tailq_entry *te;
277         struct rte_ring_list *ring_list;
278
279         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
280
281         rte_mcfg_tailq_read_lock();
282
283         TAILQ_FOREACH(te, ring_list, next) {
284                 rte_ring_dump(f, (struct rte_ring *) te->data);
285         }
286
287         rte_mcfg_tailq_read_unlock();
288 }
289
290 /* search a ring from its name */
291 struct rte_ring *
292 rte_ring_lookup(const char *name)
293 {
294         struct rte_tailq_entry *te;
295         struct rte_ring *r = NULL;
296         struct rte_ring_list *ring_list;
297
298         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
299
300         rte_mcfg_tailq_read_lock();
301
302         TAILQ_FOREACH(te, ring_list, next) {
303                 r = (struct rte_ring *) te->data;
304                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
305                         break;
306         }
307
308         rte_mcfg_tailq_read_unlock();
309
310         if (te == NULL) {
311                 rte_errno = ENOENT;
312                 return NULL;
313         }
314
315         return r;
316 }