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_hts_headtail *ht_hts;
93 struct rte_ring_rts_headtail *ht_rts;
99 switch (ht->sync_type) {
100 case RTE_RING_SYNC_MT:
101 case RTE_RING_SYNC_ST:
105 case RTE_RING_SYNC_MT_RTS:
106 ht_rts->head.raw = 0;
107 ht_rts->tail.raw = 0;
109 case RTE_RING_SYNC_MT_HTS:
113 /* unknown sync mode */
119 rte_ring_reset(struct rte_ring *r)
121 reset_headtail(&r->prod);
122 reset_headtail(&r->cons);
126 * helper function, calculates sync_type values for prod and cons
127 * based on input flags. Returns zero at success or negative
128 * errno value otherwise.
131 get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
132 enum rte_ring_sync_type *cons_st)
134 static const uint32_t prod_st_flags =
135 (RING_F_SP_ENQ | RING_F_MP_RTS_ENQ | RING_F_MP_HTS_ENQ);
136 static const uint32_t cons_st_flags =
137 (RING_F_SC_DEQ | RING_F_MC_RTS_DEQ | RING_F_MC_HTS_DEQ);
139 switch (flags & prod_st_flags) {
141 *prod_st = RTE_RING_SYNC_MT;
144 *prod_st = RTE_RING_SYNC_ST;
146 case RING_F_MP_RTS_ENQ:
147 *prod_st = RTE_RING_SYNC_MT_RTS;
149 case RING_F_MP_HTS_ENQ:
150 *prod_st = RTE_RING_SYNC_MT_HTS;
156 switch (flags & cons_st_flags) {
158 *cons_st = RTE_RING_SYNC_MT;
161 *cons_st = RTE_RING_SYNC_ST;
163 case RING_F_MC_RTS_DEQ:
164 *cons_st = RTE_RING_SYNC_MT_RTS;
166 case RING_F_MC_HTS_DEQ:
167 *cons_st = RTE_RING_SYNC_MT_HTS;
177 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
182 /* compilation-time checks */
183 RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
184 RTE_CACHE_LINE_MASK) != 0);
185 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
186 RTE_CACHE_LINE_MASK) != 0);
187 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
188 RTE_CACHE_LINE_MASK) != 0);
190 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
191 offsetof(struct rte_ring_hts_headtail, sync_type));
192 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
193 offsetof(struct rte_ring_hts_headtail, ht.pos.tail));
195 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
196 offsetof(struct rte_ring_rts_headtail, sync_type));
197 RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
198 offsetof(struct rte_ring_rts_headtail, tail.val.pos));
200 /* init the ring structure */
201 memset(r, 0, sizeof(*r));
202 ret = strlcpy(r->name, name, sizeof(r->name));
203 if (ret < 0 || ret >= (int)sizeof(r->name))
204 return -ENAMETOOLONG;
206 ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type);
210 if (flags & RING_F_EXACT_SZ) {
211 r->size = rte_align32pow2(count + 1);
212 r->mask = r->size - 1;
215 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
217 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
223 r->capacity = r->mask;
226 /* set default values for head-tail distance */
227 if (flags & RING_F_MP_RTS_ENQ)
228 rte_ring_set_prod_htd_max(r, r->capacity / HTD_MAX_DEF);
229 if (flags & RING_F_MC_RTS_DEQ)
230 rte_ring_set_cons_htd_max(r, r->capacity / HTD_MAX_DEF);
235 /* create the ring for a given element size */
237 rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
238 int socket_id, unsigned int flags)
240 char mz_name[RTE_MEMZONE_NAMESIZE];
242 struct rte_tailq_entry *te;
243 const struct rte_memzone *mz;
246 struct rte_ring_list* ring_list = NULL;
247 const unsigned int requested_count = count;
250 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
252 /* for an exact size ring, round up from count to a power of two */
253 if (flags & RING_F_EXACT_SZ)
254 count = rte_align32pow2(count + 1);
256 ring_size = rte_ring_get_memsize_elem(esize, count);
258 rte_errno = ring_size;
262 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
263 RTE_RING_MZ_PREFIX, name);
264 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
265 rte_errno = ENAMETOOLONG;
269 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
271 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
276 rte_mcfg_tailq_write_lock();
278 /* reserve a memory zone for this ring. If we can't get rte_config or
279 * we are secondary process, the memzone_reserve function will set
280 * rte_errno for us appropriately - hence no check in this this function */
281 mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
282 mz_flags, __alignof__(*r));
285 /* no need to check return value here, we already checked the
287 rte_ring_init(r, name, requested_count, flags);
289 te->data = (void *) r;
292 TAILQ_INSERT_TAIL(ring_list, te, next);
295 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
298 rte_mcfg_tailq_write_unlock();
303 /* create the ring */
305 rte_ring_create(const char *name, unsigned int count, int socket_id,
308 return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
314 rte_ring_free(struct rte_ring *r)
316 struct rte_ring_list *ring_list = NULL;
317 struct rte_tailq_entry *te;
323 * Ring was not created with rte_ring_create,
324 * therefore, there is no memzone to free.
326 if (r->memzone == NULL) {
328 "Cannot free ring, not created with rte_ring_create()\n");
332 if (rte_memzone_free(r->memzone) != 0) {
333 RTE_LOG(ERR, RING, "Cannot free memory\n");
337 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
338 rte_mcfg_tailq_write_lock();
340 /* find out tailq entry */
341 TAILQ_FOREACH(te, ring_list, next) {
342 if (te->data == (void *) r)
347 rte_mcfg_tailq_write_unlock();
351 TAILQ_REMOVE(ring_list, te, next);
353 rte_mcfg_tailq_write_unlock();
358 /* dump the status of the ring on the console */
360 rte_ring_dump(FILE *f, const struct rte_ring *r)
362 fprintf(f, "ring <%s>@%p\n", r->name, r);
363 fprintf(f, " flags=%x\n", r->flags);
364 fprintf(f, " size=%"PRIu32"\n", r->size);
365 fprintf(f, " capacity=%"PRIu32"\n", r->capacity);
366 fprintf(f, " ct=%"PRIu32"\n", r->cons.tail);
367 fprintf(f, " ch=%"PRIu32"\n", r->cons.head);
368 fprintf(f, " pt=%"PRIu32"\n", r->prod.tail);
369 fprintf(f, " ph=%"PRIu32"\n", r->prod.head);
370 fprintf(f, " used=%u\n", rte_ring_count(r));
371 fprintf(f, " avail=%u\n", rte_ring_free_count(r));
374 /* dump the status of all rings on the console */
376 rte_ring_list_dump(FILE *f)
378 const struct rte_tailq_entry *te;
379 struct rte_ring_list *ring_list;
381 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
383 rte_mcfg_tailq_read_lock();
385 TAILQ_FOREACH(te, ring_list, next) {
386 rte_ring_dump(f, (struct rte_ring *) te->data);
389 rte_mcfg_tailq_read_unlock();
392 /* search a ring from its name */
394 rte_ring_lookup(const char *name)
396 struct rte_tailq_entry *te;
397 struct rte_ring *r = NULL;
398 struct rte_ring_list *ring_list;
400 ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
402 rte_mcfg_tailq_read_lock();
404 TAILQ_FOREACH(te, ring_list, next) {
405 r = (struct rte_ring *) te->data;
406 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
410 rte_mcfg_tailq_read_unlock();