ring: add reset function for flushing
[dpdk.git] / lib / librte_ring / rte_ring.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2010-2015 Intel Corporation
4  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
5  * All rights reserved.
6  * Derived from FreeBSD's bufring.h
7  * Used as BSD-3 Licensed with permission from Kip Macy.
8  */
9
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <inttypes.h>
15 #include <errno.h>
16 #include <sys/queue.h>
17
18 #include <rte_common.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memzone.h>
22 #include <rte_malloc.h>
23 #include <rte_launch.h>
24 #include <rte_eal.h>
25 #include <rte_eal_memconfig.h>
26 #include <rte_atomic.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_errno.h>
31 #include <rte_string_fns.h>
32 #include <rte_spinlock.h>
33 #include <rte_tailq.h>
34
35 #include "rte_ring.h"
36
37 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
38
39 static struct rte_tailq_elem rte_ring_tailq = {
40         .name = RTE_TAILQ_RING_NAME,
41 };
42 EAL_REGISTER_TAILQ(rte_ring_tailq)
43
44 /* true if x is a power of 2 */
45 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
46
47 /* return the size of memory occupied by a ring */
48 ssize_t
49 rte_ring_get_memsize(unsigned count)
50 {
51         ssize_t sz;
52
53         /* count must be a power of 2 */
54         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
55                 RTE_LOG(ERR, RING,
56                         "Requested size is invalid, must be power of 2, and "
57                         "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
58                 return -EINVAL;
59         }
60
61         sz = sizeof(struct rte_ring) + count * sizeof(void *);
62         sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
63         return sz;
64 }
65
66 void
67 rte_ring_reset(struct rte_ring *r)
68 {
69         r->prod.head = r->cons.head = 0;
70         r->prod.tail = r->cons.tail = 0;
71 }
72
73 int
74 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
75         unsigned flags)
76 {
77         int ret;
78
79         /* compilation-time checks */
80         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
81                           RTE_CACHE_LINE_MASK) != 0);
82         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
83                           RTE_CACHE_LINE_MASK) != 0);
84         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
85                           RTE_CACHE_LINE_MASK) != 0);
86
87         /* init the ring structure */
88         memset(r, 0, sizeof(*r));
89         ret = strlcpy(r->name, name, sizeof(r->name));
90         if (ret < 0 || ret >= (int)sizeof(r->name))
91                 return -ENAMETOOLONG;
92         r->flags = flags;
93         r->prod.single = (flags & RING_F_SP_ENQ) ? __IS_SP : __IS_MP;
94         r->cons.single = (flags & RING_F_SC_DEQ) ? __IS_SC : __IS_MC;
95
96         if (flags & RING_F_EXACT_SZ) {
97                 r->size = rte_align32pow2(count + 1);
98                 r->mask = r->size - 1;
99                 r->capacity = count;
100         } else {
101                 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
102                         RTE_LOG(ERR, RING,
103                                 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
104                                 RTE_RING_SZ_MASK);
105                         return -EINVAL;
106                 }
107                 r->size = count;
108                 r->mask = count - 1;
109                 r->capacity = r->mask;
110         }
111         r->prod.head = r->cons.head = 0;
112         r->prod.tail = r->cons.tail = 0;
113
114         return 0;
115 }
116
117 /* create the ring */
118 struct rte_ring *
119 rte_ring_create(const char *name, unsigned count, int socket_id,
120                 unsigned flags)
121 {
122         char mz_name[RTE_MEMZONE_NAMESIZE];
123         struct rte_ring *r;
124         struct rte_tailq_entry *te;
125         const struct rte_memzone *mz;
126         ssize_t ring_size;
127         int mz_flags = 0;
128         struct rte_ring_list* ring_list = NULL;
129         const unsigned int requested_count = count;
130         int ret;
131
132         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
133
134         /* for an exact size ring, round up from count to a power of two */
135         if (flags & RING_F_EXACT_SZ)
136                 count = rte_align32pow2(count + 1);
137
138         ring_size = rte_ring_get_memsize(count);
139         if (ring_size < 0) {
140                 rte_errno = ring_size;
141                 return NULL;
142         }
143
144         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
145                 RTE_RING_MZ_PREFIX, name);
146         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
147                 rte_errno = ENAMETOOLONG;
148                 return NULL;
149         }
150
151         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
152         if (te == NULL) {
153                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
154                 rte_errno = ENOMEM;
155                 return NULL;
156         }
157
158         rte_mcfg_tailq_write_lock();
159
160         /* reserve a memory zone for this ring. If we can't get rte_config or
161          * we are secondary process, the memzone_reserve function will set
162          * rte_errno for us appropriately - hence no check in this this function */
163         mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
164                                          mz_flags, __alignof__(*r));
165         if (mz != NULL) {
166                 r = mz->addr;
167                 /* no need to check return value here, we already checked the
168                  * arguments above */
169                 rte_ring_init(r, name, requested_count, flags);
170
171                 te->data = (void *) r;
172                 r->memzone = mz;
173
174                 TAILQ_INSERT_TAIL(ring_list, te, next);
175         } else {
176                 r = NULL;
177                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
178                 rte_free(te);
179         }
180         rte_mcfg_tailq_write_unlock();
181
182         return r;
183 }
184
185 /* free the ring */
186 void
187 rte_ring_free(struct rte_ring *r)
188 {
189         struct rte_ring_list *ring_list = NULL;
190         struct rte_tailq_entry *te;
191
192         if (r == NULL)
193                 return;
194
195         /*
196          * Ring was not created with rte_ring_create,
197          * therefore, there is no memzone to free.
198          */
199         if (r->memzone == NULL) {
200                 RTE_LOG(ERR, RING,
201                         "Cannot free ring, not created with rte_ring_create()\n");
202                 return;
203         }
204
205         if (rte_memzone_free(r->memzone) != 0) {
206                 RTE_LOG(ERR, RING, "Cannot free memory\n");
207                 return;
208         }
209
210         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
211         rte_mcfg_tailq_write_lock();
212
213         /* find out tailq entry */
214         TAILQ_FOREACH(te, ring_list, next) {
215                 if (te->data == (void *) r)
216                         break;
217         }
218
219         if (te == NULL) {
220                 rte_mcfg_tailq_write_unlock();
221                 return;
222         }
223
224         TAILQ_REMOVE(ring_list, te, next);
225
226         rte_mcfg_tailq_write_unlock();
227
228         rte_free(te);
229 }
230
231 /* dump the status of the ring on the console */
232 void
233 rte_ring_dump(FILE *f, const struct rte_ring *r)
234 {
235         fprintf(f, "ring <%s>@%p\n", r->name, r);
236         fprintf(f, "  flags=%x\n", r->flags);
237         fprintf(f, "  size=%"PRIu32"\n", r->size);
238         fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
239         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
240         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
241         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
242         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
243         fprintf(f, "  used=%u\n", rte_ring_count(r));
244         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
245 }
246
247 /* dump the status of all rings on the console */
248 void
249 rte_ring_list_dump(FILE *f)
250 {
251         const struct rte_tailq_entry *te;
252         struct rte_ring_list *ring_list;
253
254         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
255
256         rte_mcfg_tailq_read_lock();
257
258         TAILQ_FOREACH(te, ring_list, next) {
259                 rte_ring_dump(f, (struct rte_ring *) te->data);
260         }
261
262         rte_mcfg_tailq_read_unlock();
263 }
264
265 /* search a ring from its name */
266 struct rte_ring *
267 rte_ring_lookup(const char *name)
268 {
269         struct rte_tailq_entry *te;
270         struct rte_ring *r = NULL;
271         struct rte_ring_list *ring_list;
272
273         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
274
275         rte_mcfg_tailq_read_lock();
276
277         TAILQ_FOREACH(te, ring_list, next) {
278                 r = (struct rte_ring *) te->data;
279                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
280                         break;
281         }
282
283         rte_mcfg_tailq_read_unlock();
284
285         if (te == NULL) {
286                 rte_errno = ENOENT;
287                 return NULL;
288         }
289
290         return r;
291 }