eal: hide shared memory config
[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 int
67 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
68         unsigned flags)
69 {
70         int ret;
71
72         /* compilation-time checks */
73         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
74                           RTE_CACHE_LINE_MASK) != 0);
75         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
76                           RTE_CACHE_LINE_MASK) != 0);
77         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
78                           RTE_CACHE_LINE_MASK) != 0);
79
80         /* init the ring structure */
81         memset(r, 0, sizeof(*r));
82         ret = strlcpy(r->name, name, sizeof(r->name));
83         if (ret < 0 || ret >= (int)sizeof(r->name))
84                 return -ENAMETOOLONG;
85         r->flags = flags;
86         r->prod.single = (flags & RING_F_SP_ENQ) ? __IS_SP : __IS_MP;
87         r->cons.single = (flags & RING_F_SC_DEQ) ? __IS_SC : __IS_MC;
88
89         if (flags & RING_F_EXACT_SZ) {
90                 r->size = rte_align32pow2(count + 1);
91                 r->mask = r->size - 1;
92                 r->capacity = count;
93         } else {
94                 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
95                         RTE_LOG(ERR, RING,
96                                 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
97                                 RTE_RING_SZ_MASK);
98                         return -EINVAL;
99                 }
100                 r->size = count;
101                 r->mask = count - 1;
102                 r->capacity = r->mask;
103         }
104         r->prod.head = r->cons.head = 0;
105         r->prod.tail = r->cons.tail = 0;
106
107         return 0;
108 }
109
110 /* create the ring */
111 struct rte_ring *
112 rte_ring_create(const char *name, unsigned count, int socket_id,
113                 unsigned flags)
114 {
115         char mz_name[RTE_MEMZONE_NAMESIZE];
116         struct rte_ring *r;
117         struct rte_tailq_entry *te;
118         const struct rte_memzone *mz;
119         ssize_t ring_size;
120         int mz_flags = 0;
121         struct rte_ring_list* ring_list = NULL;
122         const unsigned int requested_count = count;
123         int ret;
124
125         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
126
127         /* for an exact size ring, round up from count to a power of two */
128         if (flags & RING_F_EXACT_SZ)
129                 count = rte_align32pow2(count + 1);
130
131         ring_size = rte_ring_get_memsize(count);
132         if (ring_size < 0) {
133                 rte_errno = ring_size;
134                 return NULL;
135         }
136
137         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
138                 RTE_RING_MZ_PREFIX, name);
139         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
140                 rte_errno = ENAMETOOLONG;
141                 return NULL;
142         }
143
144         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
145         if (te == NULL) {
146                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
147                 rte_errno = ENOMEM;
148                 return NULL;
149         }
150
151         rte_mcfg_tailq_write_lock();
152
153         /* reserve a memory zone for this ring. If we can't get rte_config or
154          * we are secondary process, the memzone_reserve function will set
155          * rte_errno for us appropriately - hence no check in this this function */
156         mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
157                                          mz_flags, __alignof__(*r));
158         if (mz != NULL) {
159                 r = mz->addr;
160                 /* no need to check return value here, we already checked the
161                  * arguments above */
162                 rte_ring_init(r, name, requested_count, flags);
163
164                 te->data = (void *) r;
165                 r->memzone = mz;
166
167                 TAILQ_INSERT_TAIL(ring_list, te, next);
168         } else {
169                 r = NULL;
170                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
171                 rte_free(te);
172         }
173         rte_mcfg_tailq_write_unlock();
174
175         return r;
176 }
177
178 /* free the ring */
179 void
180 rte_ring_free(struct rte_ring *r)
181 {
182         struct rte_ring_list *ring_list = NULL;
183         struct rte_tailq_entry *te;
184
185         if (r == NULL)
186                 return;
187
188         /*
189          * Ring was not created with rte_ring_create,
190          * therefore, there is no memzone to free.
191          */
192         if (r->memzone == NULL) {
193                 RTE_LOG(ERR, RING,
194                         "Cannot free ring, not created with rte_ring_create()\n");
195                 return;
196         }
197
198         if (rte_memzone_free(r->memzone) != 0) {
199                 RTE_LOG(ERR, RING, "Cannot free memory\n");
200                 return;
201         }
202
203         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
204         rte_mcfg_tailq_write_lock();
205
206         /* find out tailq entry */
207         TAILQ_FOREACH(te, ring_list, next) {
208                 if (te->data == (void *) r)
209                         break;
210         }
211
212         if (te == NULL) {
213                 rte_mcfg_tailq_write_unlock();
214                 return;
215         }
216
217         TAILQ_REMOVE(ring_list, te, next);
218
219         rte_mcfg_tailq_write_unlock();
220
221         rte_free(te);
222 }
223
224 /* dump the status of the ring on the console */
225 void
226 rte_ring_dump(FILE *f, const struct rte_ring *r)
227 {
228         fprintf(f, "ring <%s>@%p\n", r->name, r);
229         fprintf(f, "  flags=%x\n", r->flags);
230         fprintf(f, "  size=%"PRIu32"\n", r->size);
231         fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
232         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
233         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
234         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
235         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
236         fprintf(f, "  used=%u\n", rte_ring_count(r));
237         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
238 }
239
240 /* dump the status of all rings on the console */
241 void
242 rte_ring_list_dump(FILE *f)
243 {
244         const struct rte_tailq_entry *te;
245         struct rte_ring_list *ring_list;
246
247         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
248
249         rte_mcfg_tailq_read_lock();
250
251         TAILQ_FOREACH(te, ring_list, next) {
252                 rte_ring_dump(f, (struct rte_ring *) te->data);
253         }
254
255         rte_mcfg_tailq_read_unlock();
256 }
257
258 /* search a ring from its name */
259 struct rte_ring *
260 rte_ring_lookup(const char *name)
261 {
262         struct rte_tailq_entry *te;
263         struct rte_ring *r = NULL;
264         struct rte_ring_list *ring_list;
265
266         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
267
268         rte_mcfg_tailq_read_lock();
269
270         TAILQ_FOREACH(te, ring_list, next) {
271                 r = (struct rte_ring *) te->data;
272                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
273                         break;
274         }
275
276         rte_mcfg_tailq_read_unlock();
277
278         if (te == NULL) {
279                 rte_errno = ENOENT;
280                 return NULL;
281         }
282
283         return r;
284 }