4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
37 * Derived from FreeBSD's bufring.c
39 **************************************************************************
41 * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
42 * All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions are met:
47 * 1. Redistributions of source code must retain the above copyright notice,
48 * this list of conditions and the following disclaimer.
50 * 2. The name of Kip Macy nor the names of other
51 * contributors may be used to endorse or promote products derived from
52 * this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
58 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 ***************************************************************************/
74 #include <sys/queue.h>
76 #include <rte_common.h>
78 #include <rte_memory.h>
79 #include <rte_memzone.h>
80 #include <rte_launch.h>
81 #include <rte_tailq.h>
83 #include <rte_atomic.h>
84 #include <rte_per_lcore.h>
85 #include <rte_lcore.h>
86 #include <rte_branch_prediction.h>
87 #include <rte_errno.h>
88 #include <rte_string_fns.h>
92 TAILQ_HEAD(rte_ring_list, rte_ring);
94 /* global list of ring (used for debug/dump) */
95 static struct rte_ring_list *ring_list = NULL;
97 /* true if x is a power of 2 */
98 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
100 /* create the ring */
102 rte_ring_create(const char *name, unsigned count, int socket_id,
105 char mz_name[RTE_MEMZONE_NAMESIZE];
107 const struct rte_memzone *mz;
111 /* compilation-time checks */
112 RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
113 CACHE_LINE_MASK) != 0);
114 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
115 CACHE_LINE_MASK) != 0);
116 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
117 CACHE_LINE_MASK) != 0);
118 #ifdef RTE_LIBRTE_RING_DEBUG
119 RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
120 CACHE_LINE_MASK) != 0);
121 RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
122 CACHE_LINE_MASK) != 0);
125 /* check that we have an initialised tail queue */
126 if (ring_list == NULL)
127 if ((ring_list = RTE_TAILQ_RESERVE("RTE_RING", rte_ring_list)) == NULL){
128 rte_errno = E_RTE_NO_TAILQ;
132 /* count must be a power of 2 */
133 if (!POWEROF2(count)) {
135 RTE_LOG(ERR, RING, "Requested size is not a power of 2\n");
139 rte_snprintf(mz_name, sizeof(mz_name), "RG_%s", name);
140 ring_size = count * sizeof(void *) + sizeof(struct rte_ring);
142 /* reserve a memory zone for this ring. If we can't get rte_config or
143 * we are secondary process, the memzone_reserve function will set
144 * rte_errno for us appropriately - hence no check in this this function */
145 mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
147 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
153 /* init the ring structure */
154 memset(r, 0, sizeof(*r));
155 rte_snprintf(r->name, sizeof(r->name), "%s", name);
157 r->prod.bulk_default = r->cons.bulk_default = 1;
158 r->prod.watermark = count;
159 r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
160 r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ);
161 r->prod.size = r->cons.size = count;
162 r->prod.mask = r->cons.mask = count-1;
163 r->prod.head = r->cons.head = 0;
164 r->prod.tail = r->cons.tail = 0;
166 TAILQ_INSERT_TAIL(ring_list, r, next);
171 * change the high water mark. If *count* is 0, water marking is
175 rte_ring_set_water_mark(struct rte_ring *r, unsigned count)
177 if (count >= r->prod.size)
180 /* if count is 0, disable the watermarking */
182 count = r->prod.size;
184 r->prod.watermark = count;
188 /* dump the status of the ring on the console */
190 rte_ring_dump(const struct rte_ring *r)
192 #ifdef RTE_LIBRTE_RING_DEBUG
193 struct rte_ring_debug_stats sum;
197 printf("ring <%s>@%p\n", r->name, r);
198 printf(" flags=%x\n", r->flags);
199 printf(" size=%"PRIu32"\n", r->prod.size);
200 printf(" ct=%"PRIu32"\n", r->cons.tail);
201 printf(" ch=%"PRIu32"\n", r->cons.head);
202 printf(" pt=%"PRIu32"\n", r->prod.tail);
203 printf(" ph=%"PRIu32"\n", r->prod.head);
204 printf(" used=%u\n", rte_ring_count(r));
205 printf(" avail=%u\n", rte_ring_free_count(r));
206 if (r->prod.watermark == r->prod.size)
207 printf(" watermark=0\n");
209 printf(" watermark=%"PRIu32"\n", r->prod.watermark);
210 printf(" bulk_default=%"PRIu32"\n", r->prod.bulk_default);
212 /* sum and dump statistics */
213 #ifdef RTE_LIBRTE_RING_DEBUG
214 memset(&sum, 0, sizeof(sum));
215 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
216 sum.enq_success_bulk += r->stats[lcore_id].enq_success_bulk;
217 sum.enq_success_objs += r->stats[lcore_id].enq_success_objs;
218 sum.enq_quota_bulk += r->stats[lcore_id].enq_quota_bulk;
219 sum.enq_quota_objs += r->stats[lcore_id].enq_quota_objs;
220 sum.enq_fail_bulk += r->stats[lcore_id].enq_fail_bulk;
221 sum.enq_fail_objs += r->stats[lcore_id].enq_fail_objs;
222 sum.deq_success_bulk += r->stats[lcore_id].deq_success_bulk;
223 sum.deq_success_objs += r->stats[lcore_id].deq_success_objs;
224 sum.deq_fail_bulk += r->stats[lcore_id].deq_fail_bulk;
225 sum.deq_fail_objs += r->stats[lcore_id].deq_fail_objs;
227 printf(" size=%"PRIu32"\n", r->prod.size);
228 printf(" enq_success_bulk=%"PRIu64"\n", sum.enq_success_bulk);
229 printf(" enq_success_objs=%"PRIu64"\n", sum.enq_success_objs);
230 printf(" enq_quota_bulk=%"PRIu64"\n", sum.enq_quota_bulk);
231 printf(" enq_quota_objs=%"PRIu64"\n", sum.enq_quota_objs);
232 printf(" enq_fail_bulk=%"PRIu64"\n", sum.enq_fail_bulk);
233 printf(" enq_fail_objs=%"PRIu64"\n", sum.enq_fail_objs);
234 printf(" deq_success_bulk=%"PRIu64"\n", sum.deq_success_bulk);
235 printf(" deq_success_objs=%"PRIu64"\n", sum.deq_success_objs);
236 printf(" deq_fail_bulk=%"PRIu64"\n", sum.deq_fail_bulk);
237 printf(" deq_fail_objs=%"PRIu64"\n", sum.deq_fail_objs);
239 printf(" no statistics available\n");
243 /* dump the status of all rings on the console */
245 rte_ring_list_dump(void)
247 const struct rte_ring *mp;
249 /* check that we have an initialised tail queue */
250 if (ring_list == NULL)
251 if ((ring_list = RTE_TAILQ_RESERVE("RTE_RING", rte_ring_list)) == NULL){
252 rte_errno = E_RTE_NO_TAILQ;
256 TAILQ_FOREACH(mp, ring_list, next) {
261 /* search a ring from its name */
263 rte_ring_lookup(const char *name)
267 /* check that we have an initialised tail queue */
268 if (ring_list == NULL)
269 if ((ring_list = RTE_TAILQ_RESERVE("RTE_RING", rte_ring_list)) == NULL){
270 rte_errno = E_RTE_NO_TAILQ;
274 TAILQ_FOREACH(r, ring_list, next) {
275 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)