ring: introduce RTS ring mode
[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 #include "rte_ring_elem.h"
37
38 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
39
40 static struct rte_tailq_elem rte_ring_tailq = {
41         .name = RTE_TAILQ_RING_NAME,
42 };
43 EAL_REGISTER_TAILQ(rte_ring_tailq)
44
45 /* true if x is a power of 2 */
46 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
47
48 /* by default set head/tail distance as 1/8 of ring capacity */
49 #define HTD_MAX_DEF     8
50
51 /* return the size of memory occupied by a ring */
52 ssize_t
53 rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
54 {
55         ssize_t sz;
56
57         /* Check if element size is a multiple of 4B */
58         if (esize % 4 != 0) {
59                 RTE_LOG(ERR, RING, "element size is not a multiple of 4\n");
60
61                 return -EINVAL;
62         }
63
64         /* count must be a power of 2 */
65         if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
66                 RTE_LOG(ERR, RING,
67                         "Requested number of elements is invalid, must be power of 2, and not exceed %u\n",
68                         RTE_RING_SZ_MASK);
69
70                 return -EINVAL;
71         }
72
73         sz = sizeof(struct rte_ring) + count * esize;
74         sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
75         return sz;
76 }
77
78 /* return the size of memory occupied by a ring */
79 ssize_t
80 rte_ring_get_memsize(unsigned int count)
81 {
82         return rte_ring_get_memsize_elem(sizeof(void *), count);
83 }
84
85 /*
86  * internal helper function to reset prod/cons head-tail values.
87  */
88 static void
89 reset_headtail(void *p)
90 {
91         struct rte_ring_headtail *ht;
92         struct rte_ring_rts_headtail *ht_rts;
93
94         ht = p;
95         ht_rts = p;
96
97         switch (ht->sync_type) {
98         case RTE_RING_SYNC_MT:
99         case RTE_RING_SYNC_ST:
100                 ht->head = 0;
101                 ht->tail = 0;
102                 break;
103         case RTE_RING_SYNC_MT_RTS:
104                 ht_rts->head.raw = 0;
105                 ht_rts->tail.raw = 0;
106                 break;
107         default:
108                 /* unknown sync mode */
109                 RTE_ASSERT(0);
110         }
111 }
112
113 void
114 rte_ring_reset(struct rte_ring *r)
115 {
116         reset_headtail(&r->prod);
117         reset_headtail(&r->cons);
118 }
119
120 /*
121  * helper function, calculates sync_type values for prod and cons
122  * based on input flags. Returns zero at success or negative
123  * errno value otherwise.
124  */
125 static int
126 get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
127         enum rte_ring_sync_type *cons_st)
128 {
129         static const uint32_t prod_st_flags =
130                 (RING_F_SP_ENQ | RING_F_MP_RTS_ENQ);
131         static const uint32_t cons_st_flags =
132                 (RING_F_SC_DEQ | RING_F_MC_RTS_DEQ);
133
134         switch (flags & prod_st_flags) {
135         case 0:
136                 *prod_st = RTE_RING_SYNC_MT;
137                 break;
138         case RING_F_SP_ENQ:
139                 *prod_st = RTE_RING_SYNC_ST;
140                 break;
141         case RING_F_MP_RTS_ENQ:
142                 *prod_st = RTE_RING_SYNC_MT_RTS;
143                 break;
144         default:
145                 return -EINVAL;
146         }
147
148         switch (flags & cons_st_flags) {
149         case 0:
150                 *cons_st = RTE_RING_SYNC_MT;
151                 break;
152         case RING_F_SC_DEQ:
153                 *cons_st = RTE_RING_SYNC_ST;
154                 break;
155         case RING_F_MC_RTS_DEQ:
156                 *cons_st = RTE_RING_SYNC_MT_RTS;
157                 break;
158         default:
159                 return -EINVAL;
160         }
161
162         return 0;
163 }
164
165 int
166 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
167         unsigned flags)
168 {
169         int ret;
170
171         /* compilation-time checks */
172         RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
173                           RTE_CACHE_LINE_MASK) != 0);
174         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
175                           RTE_CACHE_LINE_MASK) != 0);
176         RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
177                           RTE_CACHE_LINE_MASK) != 0);
178
179         RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
180                 offsetof(struct rte_ring_rts_headtail, sync_type));
181         RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
182                 offsetof(struct rte_ring_rts_headtail, tail.val.pos));
183
184         /* init the ring structure */
185         memset(r, 0, sizeof(*r));
186         ret = strlcpy(r->name, name, sizeof(r->name));
187         if (ret < 0 || ret >= (int)sizeof(r->name))
188                 return -ENAMETOOLONG;
189         r->flags = flags;
190         ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type);
191         if (ret != 0)
192                 return ret;
193
194         if (flags & RING_F_EXACT_SZ) {
195                 r->size = rte_align32pow2(count + 1);
196                 r->mask = r->size - 1;
197                 r->capacity = count;
198         } else {
199                 if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
200                         RTE_LOG(ERR, RING,
201                                 "Requested size is invalid, must be power of 2, and not exceed the size limit %u\n",
202                                 RTE_RING_SZ_MASK);
203                         return -EINVAL;
204                 }
205                 r->size = count;
206                 r->mask = count - 1;
207                 r->capacity = r->mask;
208         }
209
210         /* set default values for head-tail distance */
211         if (flags & RING_F_MP_RTS_ENQ)
212                 rte_ring_set_prod_htd_max(r, r->capacity / HTD_MAX_DEF);
213         if (flags & RING_F_MC_RTS_DEQ)
214                 rte_ring_set_cons_htd_max(r, r->capacity / HTD_MAX_DEF);
215
216         return 0;
217 }
218
219 /* create the ring for a given element size */
220 struct rte_ring *
221 rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
222                 int socket_id, unsigned int flags)
223 {
224         char mz_name[RTE_MEMZONE_NAMESIZE];
225         struct rte_ring *r;
226         struct rte_tailq_entry *te;
227         const struct rte_memzone *mz;
228         ssize_t ring_size;
229         int mz_flags = 0;
230         struct rte_ring_list* ring_list = NULL;
231         const unsigned int requested_count = count;
232         int ret;
233
234         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
235
236         /* for an exact size ring, round up from count to a power of two */
237         if (flags & RING_F_EXACT_SZ)
238                 count = rte_align32pow2(count + 1);
239
240         ring_size = rte_ring_get_memsize_elem(esize, count);
241         if (ring_size < 0) {
242                 rte_errno = ring_size;
243                 return NULL;
244         }
245
246         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
247                 RTE_RING_MZ_PREFIX, name);
248         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
249                 rte_errno = ENAMETOOLONG;
250                 return NULL;
251         }
252
253         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
254         if (te == NULL) {
255                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
256                 rte_errno = ENOMEM;
257                 return NULL;
258         }
259
260         rte_mcfg_tailq_write_lock();
261
262         /* reserve a memory zone for this ring. If we can't get rte_config or
263          * we are secondary process, the memzone_reserve function will set
264          * rte_errno for us appropriately - hence no check in this this function */
265         mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
266                                          mz_flags, __alignof__(*r));
267         if (mz != NULL) {
268                 r = mz->addr;
269                 /* no need to check return value here, we already checked the
270                  * arguments above */
271                 rte_ring_init(r, name, requested_count, flags);
272
273                 te->data = (void *) r;
274                 r->memzone = mz;
275
276                 TAILQ_INSERT_TAIL(ring_list, te, next);
277         } else {
278                 r = NULL;
279                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
280                 rte_free(te);
281         }
282         rte_mcfg_tailq_write_unlock();
283
284         return r;
285 }
286
287 /* create the ring */
288 struct rte_ring *
289 rte_ring_create(const char *name, unsigned int count, int socket_id,
290                 unsigned int flags)
291 {
292         return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
293                 flags);
294 }
295
296 /* free the ring */
297 void
298 rte_ring_free(struct rte_ring *r)
299 {
300         struct rte_ring_list *ring_list = NULL;
301         struct rte_tailq_entry *te;
302
303         if (r == NULL)
304                 return;
305
306         /*
307          * Ring was not created with rte_ring_create,
308          * therefore, there is no memzone to free.
309          */
310         if (r->memzone == NULL) {
311                 RTE_LOG(ERR, RING,
312                         "Cannot free ring, not created with rte_ring_create()\n");
313                 return;
314         }
315
316         if (rte_memzone_free(r->memzone) != 0) {
317                 RTE_LOG(ERR, RING, "Cannot free memory\n");
318                 return;
319         }
320
321         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
322         rte_mcfg_tailq_write_lock();
323
324         /* find out tailq entry */
325         TAILQ_FOREACH(te, ring_list, next) {
326                 if (te->data == (void *) r)
327                         break;
328         }
329
330         if (te == NULL) {
331                 rte_mcfg_tailq_write_unlock();
332                 return;
333         }
334
335         TAILQ_REMOVE(ring_list, te, next);
336
337         rte_mcfg_tailq_write_unlock();
338
339         rte_free(te);
340 }
341
342 /* dump the status of the ring on the console */
343 void
344 rte_ring_dump(FILE *f, const struct rte_ring *r)
345 {
346         fprintf(f, "ring <%s>@%p\n", r->name, r);
347         fprintf(f, "  flags=%x\n", r->flags);
348         fprintf(f, "  size=%"PRIu32"\n", r->size);
349         fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
350         fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
351         fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
352         fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
353         fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
354         fprintf(f, "  used=%u\n", rte_ring_count(r));
355         fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
356 }
357
358 /* dump the status of all rings on the console */
359 void
360 rte_ring_list_dump(FILE *f)
361 {
362         const struct rte_tailq_entry *te;
363         struct rte_ring_list *ring_list;
364
365         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
366
367         rte_mcfg_tailq_read_lock();
368
369         TAILQ_FOREACH(te, ring_list, next) {
370                 rte_ring_dump(f, (struct rte_ring *) te->data);
371         }
372
373         rte_mcfg_tailq_read_unlock();
374 }
375
376 /* search a ring from its name */
377 struct rte_ring *
378 rte_ring_lookup(const char *name)
379 {
380         struct rte_tailq_entry *te;
381         struct rte_ring *r = NULL;
382         struct rte_ring_list *ring_list;
383
384         ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
385
386         rte_mcfg_tailq_read_lock();
387
388         TAILQ_FOREACH(te, ring_list, next) {
389                 r = (struct rte_ring *) te->data;
390                 if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
391                         break;
392         }
393
394         rte_mcfg_tailq_read_unlock();
395
396         if (te == NULL) {
397                 rte_errno = ENOENT;
398                 return NULL;
399         }
400
401         return r;
402 }