Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_ring / rte_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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_eal.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>
89
90 #include "rte_ring.h"
91
92 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
93
94 static struct rte_tailq_elem rte_ring_tailq = {
95         .name = RTE_TAILQ_RING_NAME,
96 };
97 EAL_REGISTER_TAILQ(rte_ring_tailq)
98
99 /* true if x is a power of 2 */
100 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
101
102 /* return the size of memory occupied by a ring */
103 ssize_t
104 rte_ring_get_memsize(unsigned count)
105 {
106         ssize_t sz;
107
108         /* count must be a power of 2 */
109         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
110                 RTE_LOG(ERR, RING,
111                         "Requested size is invalid, must be power of 2, and "
112                         "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
113                 return -EINVAL;
114         }
115
116         sz = sizeof(struct rte_ring) + count * sizeof(void *);
117         sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
118         return sz;
119 }
120
121 int
122 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
123         unsigned flags)
124 {
125         int ret;
126
127         /* compilation-time checks */
128         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
129                           RTE_CACHE_LINE_MASK) != 0);
130 #ifdef RTE_RING_SPLIT_PROD_CONS
131         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
132                           RTE_CACHE_LINE_MASK) != 0);
133 #endif
134         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
135                           RTE_CACHE_LINE_MASK) != 0);
136 #ifdef RTE_LIBRTE_RING_DEBUG
137         RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
138                           RTE_CACHE_LINE_MASK) != 0);
139         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
140                           RTE_CACHE_LINE_MASK) != 0);
141 #endif
142
143         /* init the ring structure */
144         memset(r, 0, sizeof(*r));
145         ret = snprintf(r->name, sizeof(r->name), "%s", name);
146         if (ret < 0 || ret >= (int)sizeof(r->name))
147                 return -ENAMETOOLONG;
148         r->flags = flags;
149         r->prod.watermark = count;
150         r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
151         r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ);
152         r->prod.size = r->cons.size = count;
153         r->prod.mask = r->cons.mask = count-1;
154         r->prod.head = r->cons.head = 0;
155         r->prod.tail = r->cons.tail = 0;
156
157         return 0;
158 }
159
160 /* create the ring */
161 struct rte_ring *
162 rte_ring_create(const char *name, unsigned count, int socket_id,
163                 unsigned flags)
164 {
165         char mz_name[RTE_MEMZONE_NAMESIZE];
166         struct rte_ring *r;
167         struct rte_tailq_entry *te;
168         const struct rte_memzone *mz;
169         ssize_t ring_size;
170         int mz_flags = 0;
171         struct rte_ring_list* ring_list = NULL;
172         int ret;
173
174         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
175
176         ring_size = rte_ring_get_memsize(count);
177         if (ring_size < 0) {
178                 rte_errno = ring_size;
179                 return NULL;
180         }
181
182         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
183                 RTE_RING_MZ_PREFIX, name);
184         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
185                 rte_errno = ENAMETOOLONG;
186                 return NULL;
187         }
188
189         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
190         if (te == NULL) {
191                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
192                 rte_errno = ENOMEM;
193                 return NULL;
194         }
195
196         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
197
198         /* reserve a memory zone for this ring. If we can't get rte_config or
199          * we are secondary process, the memzone_reserve function will set
200          * rte_errno for us appropriately - hence no check in this this function */
201         mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
202         if (mz != NULL) {
203                 r = mz->addr;
204                 /* no need to check return value here, we already checked the
205                  * arguments above */
206                 rte_ring_init(r, name, count, flags);
207
208                 te->data = (void *) r;
209                 r->memzone = mz;
210
211                 TAILQ_INSERT_TAIL(ring_list, te, next);
212         } else {
213                 r = NULL;
214                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
215                 rte_free(te);
216         }
217         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
218
219         return r;
220 }
221
222 /* free the ring */
223 void
224 rte_ring_free(struct rte_ring *r)
225 {
226         struct rte_ring_list *ring_list = NULL;
227         struct rte_tailq_entry *te;
228
229         if (r == NULL)
230                 return;
231
232         /*
233          * Ring was not created with rte_ring_create,
234          * therefore, there is no memzone to free.
235          */
236         if (r->memzone == NULL) {
237                 RTE_LOG(ERR, RING, "Cannot free ring (not created with rte_ring_create()");
238                 return;
239         }
240
241         if (rte_memzone_free(r->memzone) != 0) {
242                 RTE_LOG(ERR, RING, "Cannot free memory\n");
243                 return;
244         }
245
246         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
247         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
248
249         /* find out tailq entry */
250         TAILQ_FOREACH(te, ring_list, next) {
251                 if (te->data == (void *) r)
252                         break;
253         }
254
255         if (te == NULL) {
256                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
257                 return;
258         }
259
260         TAILQ_REMOVE(ring_list, te, next);
261
262         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
263
264         rte_free(te);
265 }
266
267 /*
268  * change the high water mark. If *count* is 0, water marking is
269  * disabled
270  */
271 int
272 rte_ring_set_water_mark(struct rte_ring *r, unsigned count)
273 {
274         if (count >= r->prod.size)
275                 return -EINVAL;
276
277         /* if count is 0, disable the watermarking */
278         if (count == 0)
279                 count = r->prod.size;
280
281         r->prod.watermark = count;
282         return 0;
283 }
284
285 /* dump the status of the ring on the console */
286 void
287 rte_ring_dump(FILE *f, const struct rte_ring *r)
288 {
289 #ifdef RTE_LIBRTE_RING_DEBUG
290         struct rte_ring_debug_stats sum;
291         unsigned lcore_id;
292 #endif
293
294         fprintf(f, "ring <%s>@%p\n", r->name, r);
295         fprintf(f, "  flags=%x\n", r->flags);
296         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
297         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
298         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
299         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
300         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
301         fprintf(f, "  used=%u\n", rte_ring_count(r));
302         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
303         if (r->prod.watermark == r->prod.size)
304                 fprintf(f, "  watermark=0\n");
305         else
306                 fprintf(f, "  watermark=%"PRIu32"\n", r->prod.watermark);
307
308         /* sum and dump statistics */
309 #ifdef RTE_LIBRTE_RING_DEBUG
310         memset(&sum, 0, sizeof(sum));
311         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
312                 sum.enq_success_bulk += r->stats[lcore_id].enq_success_bulk;
313                 sum.enq_success_objs += r->stats[lcore_id].enq_success_objs;
314                 sum.enq_quota_bulk += r->stats[lcore_id].enq_quota_bulk;
315                 sum.enq_quota_objs += r->stats[lcore_id].enq_quota_objs;
316                 sum.enq_fail_bulk += r->stats[lcore_id].enq_fail_bulk;
317                 sum.enq_fail_objs += r->stats[lcore_id].enq_fail_objs;
318                 sum.deq_success_bulk += r->stats[lcore_id].deq_success_bulk;
319                 sum.deq_success_objs += r->stats[lcore_id].deq_success_objs;
320                 sum.deq_fail_bulk += r->stats[lcore_id].deq_fail_bulk;
321                 sum.deq_fail_objs += r->stats[lcore_id].deq_fail_objs;
322         }
323         fprintf(f, "  size=%"PRIu32"\n", r->prod.size);
324         fprintf(f, "  enq_success_bulk=%"PRIu64"\n", sum.enq_success_bulk);
325         fprintf(f, "  enq_success_objs=%"PRIu64"\n", sum.enq_success_objs);
326         fprintf(f, "  enq_quota_bulk=%"PRIu64"\n", sum.enq_quota_bulk);
327         fprintf(f, "  enq_quota_objs=%"PRIu64"\n", sum.enq_quota_objs);
328         fprintf(f, "  enq_fail_bulk=%"PRIu64"\n", sum.enq_fail_bulk);
329         fprintf(f, "  enq_fail_objs=%"PRIu64"\n", sum.enq_fail_objs);
330         fprintf(f, "  deq_success_bulk=%"PRIu64"\n", sum.deq_success_bulk);
331         fprintf(f, "  deq_success_objs=%"PRIu64"\n", sum.deq_success_objs);
332         fprintf(f, "  deq_fail_bulk=%"PRIu64"\n", sum.deq_fail_bulk);
333         fprintf(f, "  deq_fail_objs=%"PRIu64"\n", sum.deq_fail_objs);
334 #else
335         fprintf(f, "  no statistics available\n");
336 #endif
337 }
338
339 /* dump the status of all rings on the console */
340 void
341 rte_ring_list_dump(FILE *f)
342 {
343         const struct rte_tailq_entry *te;
344         struct rte_ring_list *ring_list;
345
346         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
347
348         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
349
350         TAILQ_FOREACH(te, ring_list, next) {
351                 rte_ring_dump(f, (struct rte_ring *) te->data);
352         }
353
354         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
355 }
356
357 /* search a ring from its name */
358 struct rte_ring *
359 rte_ring_lookup(const char *name)
360 {
361         struct rte_tailq_entry *te;
362         struct rte_ring *r = NULL;
363         struct rte_ring_list *ring_list;
364
365         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
366
367         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
368
369         TAILQ_FOREACH(te, ring_list, next) {
370                 r = (struct rte_ring *) te->data;
371                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
372                         break;
373         }
374
375         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
376
377         if (te == NULL) {
378                 rte_errno = ENOENT;
379                 return NULL;
380         }
381
382         return r;
383 }