b5899f91a802632d75794c14a3b07dbfc7a3f140
[dpdk.git] / lib / librte_ring / rte_ring.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 /*
36  * Derived from FreeBSD's bufring.c
37  *
38  **************************************************************************
39  *
40  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions are met:
45  *
46  * 1. Redistributions of source code must retain the above copyright notice,
47  *    this list of conditions and the following disclaimer.
48  *
49  * 2. The name of Kip Macy nor the names of other
50  *    contributors may be used to endorse or promote products derived from
51  *    this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
57  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63  * POSSIBILITY OF SUCH DAMAGE.
64  *
65  ***************************************************************************/
66
67 #include <stdio.h>
68 #include <stdarg.h>
69 #include <string.h>
70 #include <stdint.h>
71 #include <inttypes.h>
72 #include <errno.h>
73 #include <sys/queue.h>
74
75 #include <rte_common.h>
76 #include <rte_log.h>
77 #include <rte_memory.h>
78 #include <rte_memzone.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_ring);
94
95 /* true if x is a power of 2 */
96 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
97
98 /* create the ring */
99 struct rte_ring *
100 rte_ring_create(const char *name, unsigned count, int socket_id,
101                 unsigned flags)
102 {
103         char mz_name[RTE_MEMZONE_NAMESIZE];
104         struct rte_ring *r;
105         const struct rte_memzone *mz;
106         size_t ring_size;
107         int mz_flags = 0;
108         struct rte_ring_list* ring_list = NULL;
109
110         /* compilation-time checks */
111         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
112                           CACHE_LINE_MASK) != 0);
113 #ifdef RTE_RING_SPLIT_PROD_CONS
114         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
115                           CACHE_LINE_MASK) != 0);
116 #endif
117         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
118                           CACHE_LINE_MASK) != 0);
119 #ifdef RTE_LIBRTE_RING_DEBUG
120         RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
121                           CACHE_LINE_MASK) != 0);
122         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
123                           CACHE_LINE_MASK) != 0);
124 #endif
125
126         /* check that we have an initialised tail queue */
127         if ((ring_list = 
128              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
129                 rte_errno = E_RTE_NO_TAILQ;
130                 return NULL;    
131         }
132
133         /* count must be a power of 2 */
134         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
135                 rte_errno = EINVAL;
136                 RTE_LOG(ERR, RING, "Requested size is invalid, must be power of 2, and "
137                                 "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
138                 return NULL;
139         }
140
141         rte_snprintf(mz_name, sizeof(mz_name), "RG_%s", name);
142         ring_size = count * sizeof(void *) + sizeof(struct rte_ring);
143
144         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
145
146         /* reserve a memory zone for this ring. If we can't get rte_config or
147          * we are secondary process, the memzone_reserve function will set
148          * rte_errno for us appropriately - hence no check in this this function */
149         mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
150         if (mz != NULL) {
151                 r = mz->addr;
152
153                 /* init the ring structure */
154                 memset(r, 0, sizeof(*r));
155                 rte_snprintf(r->name, sizeof(r->name), "%s", name);
156                 r->flags = flags;
157                 r->prod.watermark = count;
158                 r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
159                 r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ);
160                 r->prod.size = r->cons.size = count;
161                 r->prod.mask = r->cons.mask = count-1;
162                 r->prod.head = r->cons.head = 0;
163                 r->prod.tail = r->cons.tail = 0;
164
165                 TAILQ_INSERT_TAIL(ring_list, r, next);
166         } else {
167                 r = NULL;
168                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
169         }
170         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
171         
172         return r;
173 }
174
175 /*
176  * change the high water mark. If *count* is 0, water marking is
177  * disabled
178  */
179 int
180 rte_ring_set_water_mark(struct rte_ring *r, unsigned count)
181 {
182         if (count >= r->prod.size)
183                 return -EINVAL;
184
185         /* if count is 0, disable the watermarking */
186         if (count == 0)
187                 count = r->prod.size;
188
189         r->prod.watermark = count;
190         return 0;
191 }
192
193 /* dump the status of the ring on the console */
194 void
195 rte_ring_dump(const struct rte_ring *r)
196 {
197 #ifdef RTE_LIBRTE_RING_DEBUG
198         struct rte_ring_debug_stats sum;
199         unsigned lcore_id;
200 #endif
201
202         printf("ring <%s>@%p\n", r->name, r);
203         printf("  flags=%x\n", r->flags);
204         printf("  size=%"PRIu32"\n", r->prod.size);
205         printf("  ct=%"PRIu32"\n", r->cons.tail);
206         printf("  ch=%"PRIu32"\n", r->cons.head);
207         printf("  pt=%"PRIu32"\n", r->prod.tail);
208         printf("  ph=%"PRIu32"\n", r->prod.head);
209         printf("  used=%u\n", rte_ring_count(r));
210         printf("  avail=%u\n", rte_ring_free_count(r));
211         if (r->prod.watermark == r->prod.size)
212                 printf("  watermark=0\n");
213         else
214                 printf("  watermark=%"PRIu32"\n", r->prod.watermark);
215
216         /* sum and dump statistics */
217 #ifdef RTE_LIBRTE_RING_DEBUG
218         memset(&sum, 0, sizeof(sum));
219         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
220                 sum.enq_success_bulk += r->stats[lcore_id].enq_success_bulk;
221                 sum.enq_success_objs += r->stats[lcore_id].enq_success_objs;
222                 sum.enq_quota_bulk += r->stats[lcore_id].enq_quota_bulk;
223                 sum.enq_quota_objs += r->stats[lcore_id].enq_quota_objs;
224                 sum.enq_fail_bulk += r->stats[lcore_id].enq_fail_bulk;
225                 sum.enq_fail_objs += r->stats[lcore_id].enq_fail_objs;
226                 sum.deq_success_bulk += r->stats[lcore_id].deq_success_bulk;
227                 sum.deq_success_objs += r->stats[lcore_id].deq_success_objs;
228                 sum.deq_fail_bulk += r->stats[lcore_id].deq_fail_bulk;
229                 sum.deq_fail_objs += r->stats[lcore_id].deq_fail_objs;
230         }
231         printf("  size=%"PRIu32"\n", r->prod.size);
232         printf("  enq_success_bulk=%"PRIu64"\n", sum.enq_success_bulk);
233         printf("  enq_success_objs=%"PRIu64"\n", sum.enq_success_objs);
234         printf("  enq_quota_bulk=%"PRIu64"\n", sum.enq_quota_bulk);
235         printf("  enq_quota_objs=%"PRIu64"\n", sum.enq_quota_objs);
236         printf("  enq_fail_bulk=%"PRIu64"\n", sum.enq_fail_bulk);
237         printf("  enq_fail_objs=%"PRIu64"\n", sum.enq_fail_objs);
238         printf("  deq_success_bulk=%"PRIu64"\n", sum.deq_success_bulk);
239         printf("  deq_success_objs=%"PRIu64"\n", sum.deq_success_objs);
240         printf("  deq_fail_bulk=%"PRIu64"\n", sum.deq_fail_bulk);
241         printf("  deq_fail_objs=%"PRIu64"\n", sum.deq_fail_objs);
242 #else
243         printf("  no statistics available\n");
244 #endif
245 }
246
247 /* dump the status of all rings on the console */
248 void
249 rte_ring_list_dump(void)
250 {
251         const struct rte_ring *mp;
252         struct rte_ring_list *ring_list;
253
254         /* check that we have an initialised tail queue */
255         if ((ring_list = 
256              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
257                 rte_errno = E_RTE_NO_TAILQ;
258                 return; 
259         }
260
261         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
262
263         TAILQ_FOREACH(mp, ring_list, next) {
264                 rte_ring_dump(mp);
265         }
266
267         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
268 }
269
270 /* search a ring from its name */
271 struct rte_ring *
272 rte_ring_lookup(const char *name)
273 {
274         struct rte_ring *r;
275         struct rte_ring_list *ring_list;
276
277         /* check that we have an initialized tail queue */
278         if ((ring_list = 
279              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
280                 rte_errno = E_RTE_NO_TAILQ;
281                 return NULL;    
282         }
283
284         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
285         
286         TAILQ_FOREACH(r, ring_list, next) {
287                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
288                         break;
289         }
290
291         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
292
293         if (r == NULL)
294                 rte_errno = ENOENT;
295
296         return r;
297 }