add prefix to cache line macros
[dpdk.git] / lib / librte_ring / rte_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Derived from FreeBSD's bufring.c
36  *
37  **************************************************************************
38  *
39  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions are met:
44  *
45  * 1. Redistributions of source code must retain the above copyright notice,
46  *    this list of conditions and the following disclaimer.
47  *
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.
51  *
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.
63  *
64  ***************************************************************************/
65
66 #include <stdio.h>
67 #include <stdarg.h>
68 #include <string.h>
69 #include <stdint.h>
70 #include <inttypes.h>
71 #include <errno.h>
72 #include <sys/queue.h>
73
74 #include <rte_common.h>
75 #include <rte_log.h>
76 #include <rte_memory.h>
77 #include <rte_memzone.h>
78 #include <rte_malloc.h>
79 #include <rte_launch.h>
80 #include <rte_tailq.h>
81 #include <rte_eal.h>
82 #include <rte_eal_memconfig.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>
89 #include <rte_spinlock.h>
90
91 #include "rte_ring.h"
92
93 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
94
95 /* true if x is a power of 2 */
96 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
97
98 /* return the size of memory occupied by a ring */
99 ssize_t
100 rte_ring_get_memsize(unsigned count)
101 {
102         ssize_t sz;
103
104         /* count must be a power of 2 */
105         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
106                 RTE_LOG(ERR, RING,
107                         "Requested size is invalid, must be power of 2, and "
108                         "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
109                 return -EINVAL;
110         }
111
112         sz = sizeof(struct rte_ring) + count * sizeof(void *);
113         sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
114         return sz;
115 }
116
117 int
118 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
119         unsigned flags)
120 {
121         /* compilation-time checks */
122         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
123                           RTE_CACHE_LINE_MASK) != 0);
124 #ifdef RTE_RING_SPLIT_PROD_CONS
125         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
126                           RTE_CACHE_LINE_MASK) != 0);
127 #endif
128         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
129                           RTE_CACHE_LINE_MASK) != 0);
130 #ifdef RTE_LIBRTE_RING_DEBUG
131         RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
132                           RTE_CACHE_LINE_MASK) != 0);
133         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
134                           RTE_CACHE_LINE_MASK) != 0);
135 #endif
136
137         /* init the ring structure */
138         memset(r, 0, sizeof(*r));
139         snprintf(r->name, sizeof(r->name), "%s", name);
140         r->flags = flags;
141         r->prod.watermark = count;
142         r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
143         r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ);
144         r->prod.size = r->cons.size = count;
145         r->prod.mask = r->cons.mask = count-1;
146         r->prod.head = r->cons.head = 0;
147         r->prod.tail = r->cons.tail = 0;
148
149         return 0;
150 }
151
152 /* create the ring */
153 struct rte_ring *
154 rte_ring_create(const char *name, unsigned count, int socket_id,
155                 unsigned flags)
156 {
157         char mz_name[RTE_MEMZONE_NAMESIZE];
158         struct rte_ring *r;
159         struct rte_tailq_entry *te;
160         const struct rte_memzone *mz;
161         ssize_t ring_size;
162         int mz_flags = 0;
163         struct rte_ring_list* ring_list = NULL;
164
165         /* check that we have an initialised tail queue */
166         if ((ring_list =
167              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
168                 rte_errno = E_RTE_NO_TAILQ;
169                 return NULL;
170         }
171
172         ring_size = rte_ring_get_memsize(count);
173         if (ring_size < 0) {
174                 rte_errno = ring_size;
175                 return NULL;
176         }
177
178         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
179         if (te == NULL) {
180                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
181                 rte_errno = ENOMEM;
182                 return NULL;
183         }
184
185         snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, name);
186
187         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
188
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);
193         if (mz != NULL) {
194                 r = mz->addr;
195                 /* no need to check return value here, we already checked the
196                  * arguments above */
197                 rte_ring_init(r, name, count, flags);
198
199                 te->data = (void *) r;
200
201                 TAILQ_INSERT_TAIL(ring_list, te, next);
202         } else {
203                 r = NULL;
204                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
205                 rte_free(te);
206         }
207         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
208
209         return r;
210 }
211
212 /*
213  * change the high water mark. If *count* is 0, water marking is
214  * disabled
215  */
216 int
217 rte_ring_set_water_mark(struct rte_ring *r, unsigned count)
218 {
219         if (count >= r->prod.size)
220                 return -EINVAL;
221
222         /* if count is 0, disable the watermarking */
223         if (count == 0)
224                 count = r->prod.size;
225
226         r->prod.watermark = count;
227         return 0;
228 }
229
230 /* dump the status of the ring on the console */
231 void
232 rte_ring_dump(FILE *f, const struct rte_ring *r)
233 {
234 #ifdef RTE_LIBRTE_RING_DEBUG
235         struct rte_ring_debug_stats sum;
236         unsigned lcore_id;
237 #endif
238
239         fprintf(f, "ring <%s>@%p\n", r->name, r);
240         fprintf(f, "  flags=%x\n", r->flags);
241         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
242         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
243         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
244         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
245         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
246         fprintf(f, "  used=%u\n", rte_ring_count(r));
247         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
248         if (r->prod.watermark == r->prod.size)
249                 fprintf(f, "  watermark=0\n");
250         else
251                 fprintf(f, "  watermark=%"PRIu32"\n", r->prod.watermark);
252
253         /* sum and dump statistics */
254 #ifdef RTE_LIBRTE_RING_DEBUG
255         memset(&sum, 0, sizeof(sum));
256         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
257                 sum.enq_success_bulk += r->stats[lcore_id].enq_success_bulk;
258                 sum.enq_success_objs += r->stats[lcore_id].enq_success_objs;
259                 sum.enq_quota_bulk += r->stats[lcore_id].enq_quota_bulk;
260                 sum.enq_quota_objs += r->stats[lcore_id].enq_quota_objs;
261                 sum.enq_fail_bulk += r->stats[lcore_id].enq_fail_bulk;
262                 sum.enq_fail_objs += r->stats[lcore_id].enq_fail_objs;
263                 sum.deq_success_bulk += r->stats[lcore_id].deq_success_bulk;
264                 sum.deq_success_objs += r->stats[lcore_id].deq_success_objs;
265                 sum.deq_fail_bulk += r->stats[lcore_id].deq_fail_bulk;
266                 sum.deq_fail_objs += r->stats[lcore_id].deq_fail_objs;
267         }
268         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
269         fprintf(f, "  enq_success_bulk=%"PRIu64"\n", sum.enq_success_bulk);
270         fprintf(f, "  enq_success_objs=%"PRIu64"\n", sum.enq_success_objs);
271         fprintf(f, "  enq_quota_bulk=%"PRIu64"\n", sum.enq_quota_bulk);
272         fprintf(f, "  enq_quota_objs=%"PRIu64"\n", sum.enq_quota_objs);
273         fprintf(f, "  enq_fail_bulk=%"PRIu64"\n", sum.enq_fail_bulk);
274         fprintf(f, "  enq_fail_objs=%"PRIu64"\n", sum.enq_fail_objs);
275         fprintf(f, "  deq_success_bulk=%"PRIu64"\n", sum.deq_success_bulk);
276         fprintf(f, "  deq_success_objs=%"PRIu64"\n", sum.deq_success_objs);
277         fprintf(f, "  deq_fail_bulk=%"PRIu64"\n", sum.deq_fail_bulk);
278         fprintf(f, "  deq_fail_objs=%"PRIu64"\n", sum.deq_fail_objs);
279 #else
280         fprintf(f, "  no statistics available\n");
281 #endif
282 }
283
284 /* dump the status of all rings on the console */
285 void
286 rte_ring_list_dump(FILE *f)
287 {
288         const struct rte_tailq_entry *te;
289         struct rte_ring_list *ring_list;
290
291         /* check that we have an initialised tail queue */
292         if ((ring_list =
293              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
294                 rte_errno = E_RTE_NO_TAILQ;
295                 return;
296         }
297
298         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
299
300         TAILQ_FOREACH(te, ring_list, next) {
301                 rte_ring_dump(f, (struct rte_ring *) te->data);
302         }
303
304         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
305 }
306
307 /* search a ring from its name */
308 struct rte_ring *
309 rte_ring_lookup(const char *name)
310 {
311         struct rte_tailq_entry *te;
312         struct rte_ring *r = NULL;
313         struct rte_ring_list *ring_list;
314
315         /* check that we have an initialized tail queue */
316         if ((ring_list =
317              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
318                 rte_errno = E_RTE_NO_TAILQ;
319                 return NULL;
320         }
321
322         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
323
324         TAILQ_FOREACH(te, ring_list, next) {
325                 r = (struct rte_ring *) te->data;
326                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
327                         break;
328         }
329
330         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
331
332         if (te == NULL) {
333                 rte_errno = ENOENT;
334                 return NULL;
335         }
336
337         return r;
338 }