4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Derived from FreeBSD's bufring.c
37 **************************************************************************
39 * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions are met:
45 * 1. Redistributions of source code must retain the above copyright notice,
46 * this list of conditions and the following disclaimer.
48 * 2. The name of Kip Macy nor the names of other
49 * contributors may be used to endorse or promote products derived from
50 * this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
53 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
56 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
64 ***************************************************************************/
72 #include <sys/queue.h>
74 #include <rte_common.h>
76 #include <rte_memory.h>
77 #include <rte_memzone.h>
78 #include <rte_malloc.h>
79 #include <rte_launch.h>
81 #include <rte_eal_memconfig.h>
82 #include <rte_atomic.h>
83 #include <rte_per_lcore.h>
84 #include <rte_lcore.h>
85 #include <rte_branch_prediction.h>
86 #include <rte_errno.h>
87 #include <rte_string_fns.h>
88 #include <rte_spinlock.h>
92 TAILQ_HEAD(rte_ring_list
, rte_tailq_entry
);
94 static struct rte_tailq_elem rte_ring_tailq
= {
95 .name
= RTE_TAILQ_RING_NAME
,
97 EAL_REGISTER_TAILQ(rte_ring_tailq
)
99 /* true if x is a power of 2 */
100 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
102 /* return the size of memory occupied by a ring */
104 rte_ring_get_memsize(unsigned count
)
108 /* count must be a power of 2 */
109 if ((!POWEROF2(count
)) || (count
> RTE_RING_SZ_MASK
)) {
111 "Requested size is invalid, must be power of 2, and "
112 "do not exceed the size limit %u\n", RTE_RING_SZ_MASK
);
116 sz
= sizeof(struct rte_ring
) + count
* sizeof(void *);
117 sz
= RTE_ALIGN(sz
, RTE_CACHE_LINE_SIZE
);
122 rte_ring_init(struct rte_ring
*r
, const char *name
, unsigned count
,
127 /* compilation-time checks */
128 RTE_BUILD_BUG_ON((sizeof(struct rte_ring
) &
129 RTE_CACHE_LINE_MASK
) != 0);
130 RTE_BUILD_BUG_ON((offsetof(struct rte_ring
, cons
) &
131 RTE_CACHE_LINE_MASK
) != 0);
132 RTE_BUILD_BUG_ON((offsetof(struct rte_ring
, prod
) &
133 RTE_CACHE_LINE_MASK
) != 0);
135 /* init the ring structure */
136 memset(r
, 0, sizeof(*r
));
137 ret
= snprintf(r
->name
, sizeof(r
->name
), "%s", name
);
138 if (ret
< 0 || ret
>= (int)sizeof(r
->name
))
139 return -ENAMETOOLONG
;
141 r
->prod
.single
= (flags
& RING_F_SP_ENQ
) ? __IS_SP
: __IS_MP
;
142 r
->cons
.single
= (flags
& RING_F_SC_DEQ
) ? __IS_SC
: __IS_MC
;
145 r
->prod
.head
= r
->cons
.head
= 0;
146 r
->prod
.tail
= r
->cons
.tail
= 0;
151 /* create the ring */
153 rte_ring_create(const char *name
, unsigned count
, int socket_id
,
156 char mz_name
[RTE_MEMZONE_NAMESIZE
];
158 struct rte_tailq_entry
*te
;
159 const struct rte_memzone
*mz
;
162 struct rte_ring_list
* ring_list
= NULL
;
165 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
167 ring_size
= rte_ring_get_memsize(count
);
169 rte_errno
= ring_size
;
173 ret
= snprintf(mz_name
, sizeof(mz_name
), "%s%s",
174 RTE_RING_MZ_PREFIX
, name
);
175 if (ret
< 0 || ret
>= (int)sizeof(mz_name
)) {
176 rte_errno
= ENAMETOOLONG
;
180 te
= rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te
), 0);
182 RTE_LOG(ERR
, RING
, "Cannot reserve memory for tailq\n");
187 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
189 /* reserve a memory zone for this ring. If we can't get rte_config or
190 * we are secondary process, the memzone_reserve function will set
191 * rte_errno for us appropriately - hence no check in this this function */
192 mz
= rte_memzone_reserve(mz_name
, ring_size
, socket_id
, mz_flags
);
195 /* no need to check return value here, we already checked the
197 rte_ring_init(r
, name
, count
, flags
);
199 te
->data
= (void *) r
;
202 TAILQ_INSERT_TAIL(ring_list
, te
, next
);
205 RTE_LOG(ERR
, RING
, "Cannot reserve memory\n");
208 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
215 rte_ring_free(struct rte_ring
*r
)
217 struct rte_ring_list
*ring_list
= NULL
;
218 struct rte_tailq_entry
*te
;
224 * Ring was not created with rte_ring_create,
225 * therefore, there is no memzone to free.
227 if (r
->memzone
== NULL
) {
228 RTE_LOG(ERR
, RING
, "Cannot free ring (not created with rte_ring_create()");
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_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
240 /* find out tailq entry */
241 TAILQ_FOREACH(te
, ring_list
, next
) {
242 if (te
->data
== (void *) r
)
247 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
251 TAILQ_REMOVE(ring_list
, te
, next
);
253 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
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
, " ct=%"PRIu32
"\n", r
->cons
.tail
);
266 fprintf(f
, " ch=%"PRIu32
"\n", r
->cons
.head
);
267 fprintf(f
, " pt=%"PRIu32
"\n", r
->prod
.tail
);
268 fprintf(f
, " ph=%"PRIu32
"\n", r
->prod
.head
);
269 fprintf(f
, " used=%u\n", rte_ring_count(r
));
270 fprintf(f
, " avail=%u\n", rte_ring_free_count(r
));
273 /* dump the status of all rings on the console */
275 rte_ring_list_dump(FILE *f
)
277 const struct rte_tailq_entry
*te
;
278 struct rte_ring_list
*ring_list
;
280 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
282 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK
);
284 TAILQ_FOREACH(te
, ring_list
, next
) {
285 rte_ring_dump(f
, (struct rte_ring
*) te
->data
);
288 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK
);
291 /* search a ring from its name */
293 rte_ring_lookup(const char *name
)
295 struct rte_tailq_entry
*te
;
296 struct rte_ring
*r
= NULL
;
297 struct rte_ring_list
*ring_list
;
299 ring_list
= RTE_TAILQ_CAST(rte_ring_tailq
.head
, rte_ring_list
);
301 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK
);
303 TAILQ_FOREACH(te
, ring_list
, next
) {
304 r
= (struct rte_ring
*) te
->data
;
305 if (strncmp(name
, r
->name
, RTE_RING_NAMESIZE
) == 0)
309 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK
);