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 /* by default set head/tail distance as 1/8 of ring capacity */
51 /* return the size of memory occupied by a ring */
53 rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
57 /* Check if element size is a multiple of 4B */
59 RTE_LOG(ERR, RING, "element size is not a multiple of 4\n");
64 /* count must be a power of 2 */
65 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
67 "Requested number of elements is invalid, must be power of 2, and not exceed %u\n",
73 sz = sizeof(struct rte_ring) + count * esize;
74 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
78 /* return the size of memory occupied by a ring */
80 rte_ring_get_memsize(unsigned int count)
82 return rte_ring_get_memsize_elem(sizeof(void *), count);
86 * internal helper function to reset prod/cons head-tail values.
89 reset_headtail(void *p)
91 struct rte_ring_headtail *ht;
92 struct rte_ring_rts_headtail *ht_rts;
97 switch (ht->sync_type) {
98 case RTE_RING_SYNC_MT:
99 case RTE_RING_SYNC_ST:
103 case RTE_RING_SYNC_MT_RTS:
104 ht_rts->head.raw = 0;
105 ht_rts->tail.raw = 0;
108 /* unknown sync mode */
114 rte_ring_reset(struct rte_ring *r)
116 reset_headtail(&r->prod);
117 reset_headtail(&r->cons);
121 * helper function, calculates sync_type values for prod and cons
122 * based on input flags. Returns zero at success or negative
123 * errno value otherwise.
126 get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
127 enum rte_ring_sync_type *cons_st)
129 static const uint32_t prod_st_flags =
130 (RING_F_SP_ENQ | RING_F_MP_RTS_ENQ);
131 static const uint32_t cons_st_flags =
132 (RING_F_SC_DEQ | RING_F_MC_RTS_DEQ);
134 switch (flags & prod_st_flags) {
136 *prod_st = RTE_RING_SYNC_MT;
139 *prod_st = RTE_RING_SYNC_ST;
141 case RING_F_MP_RTS_ENQ:
142 *prod_st = RTE_RING_SYNC_MT_RTS;
148 switch (flags & cons_st_flags) {
150 *cons_st = RTE_RING_SYNC_MT;
153 *cons_st = RTE_RING_SYNC_ST;
155 case RING_F_MC_RTS_DEQ:
156 *cons_st = RTE_RING_SYNC_MT_RTS;
166 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
171 /* compilation-time checks */
172 RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
173 RTE_CACHE_LINE_MASK) != 0);
174 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
175 RTE_CACHE_LINE_MASK) != 0);
176 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
177 RTE_CACHE_LINE_MASK) != 0);
179 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
180 offsetof(struct rte_ring_rts_headtail, sync_type));
181 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
182 offsetof(struct rte_ring_rts_headtail, tail.val.pos));
184 /* init the ring structure */
185 memset(r, 0, sizeof(*r));
186 ret = strlcpy(r->name, name, sizeof(r->name));
187 if (ret < 0 || ret >= (int)sizeof(r->name))
188 return -ENAMETOOLONG;
190 ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type);
194 if (flags & RING_F_EXACT_SZ) {
195 r->size = rte_align32pow2(count + 1);
196 r->mask = r->size - 1;
199 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
201 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
207 r->capacity = r->mask;
210 /* set default values for head-tail distance */
211 if (flags & RING_F_MP_RTS_ENQ)
212 rte_ring_set_prod_htd_max(r, r->capacity / HTD_MAX_DEF);
213 if (flags & RING_F_MC_RTS_DEQ)
214 rte_ring_set_cons_htd_max(r, r->capacity / HTD_MAX_DEF);
219 /* create the ring for a given element size */
221 rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
222 int socket_id, unsigned int flags)
224 char mz_name[RTE_MEMZONE_NAMESIZE];
226 struct rte_tailq_entry *te;
227 const struct rte_memzone *mz;
230 struct rte_ring_list* ring_list = NULL;
231 const unsigned int requested_count = count;
234 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
236 /* for an exact size ring, round up from count to a power of two */
237 if (flags & RING_F_EXACT_SZ)
238 count = rte_align32pow2(count + 1);
240 ring_size = rte_ring_get_memsize_elem(esize, count);
242 rte_errno = ring_size;
246 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
247 RTE_RING_MZ_PREFIX, name);
248 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
249 rte_errno = ENAMETOOLONG;
253 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
255 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
260 rte_mcfg_tailq_write_lock();
262 /* reserve a memory zone for this ring. If we can't get rte_config or
263 * we are secondary process, the memzone_reserve function will set
264 * rte_errno for us appropriately - hence no check in this this function */
265 mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
266 mz_flags, __alignof__(*r));
269 /* no need to check return value here, we already checked the
271 rte_ring_init(r, name, requested_count, flags);
273 te->data = (void *) r;
276 TAILQ_INSERT_TAIL(ring_list, te, next);
279 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
282 rte_mcfg_tailq_write_unlock();
287 /* create the ring */
289 rte_ring_create(const char *name, unsigned int count, int socket_id,
292 return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
298 rte_ring_free(struct rte_ring *r)
300 struct rte_ring_list *ring_list = NULL;
301 struct rte_tailq_entry *te;
307 * Ring was not created with rte_ring_create,
308 * therefore, there is no memzone to free.
310 if (r->memzone == NULL) {
312 "Cannot free ring, not created with rte_ring_create()\n");
316 if (rte_memzone_free(r->memzone) != 0) {
317 RTE_LOG(ERR, RING, "Cannot free memory\n");
321 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
322 rte_mcfg_tailq_write_lock();
324 /* find out tailq entry */
325 TAILQ_FOREACH(te, ring_list, next) {
326 if (te->data == (void *) r)
331 rte_mcfg_tailq_write_unlock();
335 TAILQ_REMOVE(ring_list, te, next);
337 rte_mcfg_tailq_write_unlock();
342 /* dump the status of the ring on the console */
344 rte_ring_dump(FILE *f, const struct rte_ring *r)
346 fprintf(f, "ring <%s>@%p\n", r->name, r);
347 fprintf(f, " flags=%x\n", r->flags);
348 fprintf(f, " size=%"PRIu32"\n", r->size);
349 fprintf(f, " capacity=%"PRIu32"\n", r->capacity);
350 fprintf(f, " ct=%"PRIu32"\n", r->cons.tail);
351 fprintf(f, " ch=%"PRIu32"\n", r->cons.head);
352 fprintf(f, " pt=%"PRIu32"\n", r->prod.tail);
353 fprintf(f, " ph=%"PRIu32"\n", r->prod.head);
354 fprintf(f, " used=%u\n", rte_ring_count(r));
355 fprintf(f, " avail=%u\n", rte_ring_free_count(r));
358 /* dump the status of all rings on the console */
360 rte_ring_list_dump(FILE *f)
362 const struct rte_tailq_entry *te;
363 struct rte_ring_list *ring_list;
365 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
367 rte_mcfg_tailq_read_lock();
369 TAILQ_FOREACH(te, ring_list, next) {
370 rte_ring_dump(f, (struct rte_ring *) te->data);
373 rte_mcfg_tailq_read_unlock();
376 /* search a ring from its name */
378 rte_ring_lookup(const char *name)
380 struct rte_tailq_entry *te;
381 struct rte_ring *r = NULL;
382 struct rte_ring_list *ring_list;
384 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
386 rte_mcfg_tailq_read_lock();
388 TAILQ_FOREACH(te, ring_list, next) {
389 r = (struct rte_ring *) te->data;
390 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
394 rte_mcfg_tailq_read_unlock();