1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2010-2015 Intel Corporation
4 * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
6 * Derived from FreeBSD's bufring.h
7 * Used as BSD-3 Licensed with permission from Kip Macy.
16 #include <sys/queue.h>
18 #include <rte_common.h>
20 #include <rte_memory.h>
21 #include <rte_memzone.h>
22 #include <rte_malloc.h>
23 #include <rte_launch.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>
36 #include "rte_ring_elem.h"
38 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
40 static struct rte_tailq_elem rte_ring_tailq = {
41 .name = RTE_TAILQ_RING_NAME,
43 EAL_REGISTER_TAILQ(rte_ring_tailq)
45 /* true if x is a power of 2 */
46 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
48 /* return the size of memory occupied by a ring */
50 rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
54 /* Check if element size is a multiple of 4B */
56 RTE_LOG(ERR, RING, "element size is not a multiple of 4\n");
61 /* count must be a power of 2 */
62 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
64 "Requested number of elements is invalid, must be power of 2, and not exceed %u\n",
70 sz = sizeof(struct rte_ring) + count * esize;
71 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
75 /* return the size of memory occupied by a ring */
77 rte_ring_get_memsize(unsigned int count)
79 return rte_ring_get_memsize_elem(sizeof(void *), count);
83 rte_ring_reset(struct rte_ring *r)
85 r->prod.head = r->cons.head = 0;
86 r->prod.tail = r->cons.tail = 0;
90 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
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);
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;
109 r->prod.sync_type = (flags & RING_F_SP_ENQ) ?
110 RTE_RING_SYNC_ST : RTE_RING_SYNC_MT;
111 r->cons.sync_type = (flags & RING_F_SC_DEQ) ?
112 RTE_RING_SYNC_ST : RTE_RING_SYNC_MT;
114 if (flags & RING_F_EXACT_SZ) {
115 r->size = rte_align32pow2(count + 1);
116 r->mask = r->size - 1;
119 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
121 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
127 r->capacity = r->mask;
129 r->prod.head = r->cons.head = 0;
130 r->prod.tail = r->cons.tail = 0;
135 /* create the ring for a given element size */
137 rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
138 int socket_id, unsigned int flags)
140 char mz_name[RTE_MEMZONE_NAMESIZE];
142 struct rte_tailq_entry *te;
143 const struct rte_memzone *mz;
146 struct rte_ring_list* ring_list = NULL;
147 const unsigned int requested_count = count;
150 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
152 /* for an exact size ring, round up from count to a power of two */
153 if (flags & RING_F_EXACT_SZ)
154 count = rte_align32pow2(count + 1);
156 ring_size = rte_ring_get_memsize_elem(esize, count);
158 rte_errno = ring_size;
162 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
163 RTE_RING_MZ_PREFIX, name);
164 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
165 rte_errno = ENAMETOOLONG;
169 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
171 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
176 rte_mcfg_tailq_write_lock();
178 /* reserve a memory zone for this ring. If we can't get rte_config or
179 * we are secondary process, the memzone_reserve function will set
180 * rte_errno for us appropriately - hence no check in this this function */
181 mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
182 mz_flags, __alignof__(*r));
185 /* no need to check return value here, we already checked the
187 rte_ring_init(r, name, requested_count, flags);
189 te->data = (void *) r;
192 TAILQ_INSERT_TAIL(ring_list, te, next);
195 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
198 rte_mcfg_tailq_write_unlock();
203 /* create the ring */
205 rte_ring_create(const char *name, unsigned int count, int socket_id,
208 return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
214 rte_ring_free(struct rte_ring *r)
216 struct rte_ring_list *ring_list = NULL;
217 struct rte_tailq_entry *te;
223 * Ring was not created with rte_ring_create,
224 * therefore, there is no memzone to free.
226 if (r->memzone == NULL) {
228 "Cannot free ring, not created with rte_ring_create()\n");
232 if (rte_memzone_free(r->memzone) != 0) {
233 RTE_LOG(ERR, RING, "Cannot free memory\n");
237 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
238 rte_mcfg_tailq_write_lock();
240 /* find out tailq entry */
241 TAILQ_FOREACH(te, ring_list, next) {
242 if (te->data == (void *) r)
247 rte_mcfg_tailq_write_unlock();
251 TAILQ_REMOVE(ring_list, te, next);
253 rte_mcfg_tailq_write_unlock();
258 /* dump the status of the ring on the console */
260 rte_ring_dump(FILE *f, const struct rte_ring *r)
262 fprintf(f, "ring <%s>@%p\n", r->name, r);
263 fprintf(f, " flags=%x\n", r->flags);
264 fprintf(f, " size=%"PRIu32"\n", r->size);
265 fprintf(f, " capacity=%"PRIu32"\n", r->capacity);
266 fprintf(f, " ct=%"PRIu32"\n", r->cons.tail);
267 fprintf(f, " ch=%"PRIu32"\n", r->cons.head);
268 fprintf(f, " pt=%"PRIu32"\n", r->prod.tail);
269 fprintf(f, " ph=%"PRIu32"\n", r->prod.head);
270 fprintf(f, " used=%u\n", rte_ring_count(r));
271 fprintf(f, " avail=%u\n", rte_ring_free_count(r));
274 /* dump the status of all rings on the console */
276 rte_ring_list_dump(FILE *f)
278 const struct rte_tailq_entry *te;
279 struct rte_ring_list *ring_list;
281 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
283 rte_mcfg_tailq_read_lock();
285 TAILQ_FOREACH(te, ring_list, next) {
286 rte_ring_dump(f, (struct rte_ring *) te->data);
289 rte_mcfg_tailq_read_unlock();
292 /* search a ring from its name */
294 rte_ring_lookup(const char *name)
296 struct rte_tailq_entry *te;
297 struct rte_ring *r = NULL;
298 struct rte_ring_list *ring_list;
300 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
302 rte_mcfg_tailq_read_lock();
304 TAILQ_FOREACH(te, ring_list, next) {
305 r = (struct rte_ring *) te->data;
306 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
310 rte_mcfg_tailq_read_unlock();