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>
36 TAILQ_HEAD(rte_ring_list
, rte_tailq_entry
);
38 static struct rte_tailq_elem rte_ring_tailq
= {
39 .name
= RTE_TAILQ_RING_NAME
,
41 EAL_REGISTER_TAILQ(rte_ring_tailq
)
43 /* true if x is a power of 2 */
44 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
46 /* return the size of memory occupied by a ring */
48 rte_ring_get_memsize(unsigned count
)
52 /* count must be a power of 2 */
53 if ((!POWEROF2(count
)) || (count
> RTE_RING_SZ_MASK
)) {
55 "Requested size is invalid, must be power of 2, and "
56 "do not exceed the size limit %u\n", RTE_RING_SZ_MASK
);
60 sz
= sizeof(struct rte_ring
) + count
* sizeof(void *);
61 sz
= RTE_ALIGN(sz
, RTE_CACHE_LINE_SIZE
);
66 rte_ring_init(struct rte_ring
*r
, const char *name
, unsigned count
,
71 /* compilation-time checks */
72 RTE_BUILD_BUG_ON((sizeof(struct rte_ring
) &
73 RTE_CACHE_LINE_MASK
) != 0);
74 RTE_BUILD_BUG_ON((offsetof(struct rte_ring
, cons
) &
75 RTE_CACHE_LINE_MASK
) != 0);
76 RTE_BUILD_BUG_ON((offsetof(struct rte_ring
, prod
) &
77 RTE_CACHE_LINE_MASK
) != 0);
79 /* init the ring structure */
80 memset(r
, 0, sizeof(*r
));
81 ret
= snprintf(r
->name
, sizeof(r
->name
), "%s", name
);
82 if (ret
< 0 || ret
>= (int)sizeof(r
->name
))
85 r
->prod
.single
= (flags
& RING_F_SP_ENQ
) ? __IS_SP
: __IS_MP
;
86 r
->cons
.single
= (flags
& RING_F_SC_DEQ
) ? __IS_SC
: __IS_MC
;
88 if (flags
& RING_F_EXACT_SZ
) {
89 r
->size
= rte_align32pow2(count
+ 1);
90 r
->mask
= r
->size
- 1;
93 if ((!POWEROF2(count
)) || (count
> RTE_RING_SZ_MASK
)) {
95 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
101 r
->capacity
= r
->mask
;
103 r
->prod
.head
= r
->cons
.head
= 0;
104 r
->prod
.tail
= r
->cons
.tail
= 0;
109 /* create the ring */
111 rte_ring_create(const char *name
, unsigned count
, int socket_id
,
114 char mz_name
[RTE_MEMZONE_NAMESIZE
];
116 struct rte_tailq_entry
*te
;
117 const struct rte_memzone
*mz
;
120 struct rte_ring_list
* ring_list
= NULL
;
121 const unsigned int requested_count
= count
;
124 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
126 /* for an exact size ring, round up from count to a power of two */
127 if (flags
& RING_F_EXACT_SZ
)
128 count
= rte_align32pow2(count
+ 1);
130 ring_size
= rte_ring_get_memsize(count
);
132 rte_errno
= ring_size
;
136 ret
= snprintf(mz_name
, sizeof(mz_name
), "%s%s",
137 RTE_RING_MZ_PREFIX
, name
);
138 if (ret
< 0 || ret
>= (int)sizeof(mz_name
)) {
139 rte_errno
= ENAMETOOLONG
;
143 te
= rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te
), 0);
145 RTE_LOG(ERR
, RING
, "Cannot reserve memory for tailq\n");
150 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
152 /* reserve a memory zone for this ring. If we can't get rte_config or
153 * we are secondary process, the memzone_reserve function will set
154 * rte_errno for us appropriately - hence no check in this this function */
155 mz
= rte_memzone_reserve_aligned(mz_name
, ring_size
, socket_id
,
156 mz_flags
, __alignof__(*r
));
159 /* no need to check return value here, we already checked the
161 rte_ring_init(r
, name
, requested_count
, flags
);
163 te
->data
= (void *) r
;
166 TAILQ_INSERT_TAIL(ring_list
, te
, next
);
169 RTE_LOG(ERR
, RING
, "Cannot reserve memory\n");
172 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
179 rte_ring_free(struct rte_ring
*r
)
181 struct rte_ring_list
*ring_list
= NULL
;
182 struct rte_tailq_entry
*te
;
188 * Ring was not created with rte_ring_create,
189 * therefore, there is no memzone to free.
191 if (r
->memzone
== NULL
) {
192 RTE_LOG(ERR
, RING
, "Cannot free ring (not created with rte_ring_create()");
196 if (rte_memzone_free(r
->memzone
) != 0) {
197 RTE_LOG(ERR
, RING
, "Cannot free memory\n");
201 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
202 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
204 /* find out tailq entry */
205 TAILQ_FOREACH(te
, ring_list
, next
) {
206 if (te
->data
== (void *) r
)
211 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
215 TAILQ_REMOVE(ring_list
, te
, next
);
217 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
222 /* dump the status of the ring on the console */
224 rte_ring_dump(FILE *f
, const struct rte_ring
*r
)
226 fprintf(f
, "ring <%s>@%p\n", r
->name
, r
);
227 fprintf(f
, " flags=%x\n", r
->flags
);
228 fprintf(f
, " size=%"PRIu32
"\n", r
->size
);
229 fprintf(f
, " capacity=%"PRIu32
"\n", r
->capacity
);
230 fprintf(f
, " ct=%"PRIu32
"\n", r
->cons
.tail
);
231 fprintf(f
, " ch=%"PRIu32
"\n", r
->cons
.head
);
232 fprintf(f
, " pt=%"PRIu32
"\n", r
->prod
.tail
);
233 fprintf(f
, " ph=%"PRIu32
"\n", r
->prod
.head
);
234 fprintf(f
, " used=%u\n", rte_ring_count(r
));
235 fprintf(f
, " avail=%u\n", rte_ring_free_count(r
));
238 /* dump the status of all rings on the console */
240 rte_ring_list_dump(FILE *f
)
242 const struct rte_tailq_entry
*te
;
243 struct rte_ring_list
*ring_list
;
245 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
247 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK
);
249 TAILQ_FOREACH(te
, ring_list
, next
) {
250 rte_ring_dump(f
, (struct rte_ring
*) te
->data
);
253 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK
);
256 /* search a ring from its name */
258 rte_ring_lookup(const char *name
)
260 struct rte_tailq_entry
*te
;
261 struct rte_ring
*r
= NULL
;
262 struct rte_ring_list
*ring_list
;
264 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
266 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK
);
268 TAILQ_FOREACH(te
, ring_list
, next
) {
269 r
= (struct rte_ring
*) te
->data
;
270 if (strncmp(name
, r
->name
, RTE_RING_NAMESIZE
) == 0)
274 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK
);