New upstream version 18.08
[deb_dpdk.git] / lib / librte_eventdev / rte_event_ring.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 /**
6  * @file
7  * RTE Event Ring
8  *
9  * This provides a ring implementation for passing rte_event structures
10  * from one core to another.
11  */
12
13 #ifndef _RTE_EVENT_RING_
14 #define _RTE_EVENT_RING_
15
16 #include <stdint.h>
17
18 #include <rte_common.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_ring.h>
22 #include "rte_eventdev.h"
23
24 #define RTE_TAILQ_EVENT_RING_NAME "RTE_EVENT_RING"
25
26 /**
27  * Generic ring structure for passing rte_event objects from core to core.
28  *
29  * Based on the primitives given in the rte_ring library. Designed to be
30  * used inside software eventdev implementations and by applications
31  * directly as needed.
32  */
33 struct rte_event_ring {
34         struct rte_ring r;
35 };
36
37 /**
38  * Returns the number of events in the ring
39  *
40  * @param r
41  *   pointer to the event ring
42  * @return
43  *   the number of events in the ring
44  */
45 static __rte_always_inline unsigned int
46 rte_event_ring_count(const struct rte_event_ring *r)
47 {
48         return rte_ring_count(&r->r);
49 }
50
51 /**
52  * Returns the amount of free space in the ring
53  *
54  * @param r
55  *   pointer to the event ring
56  * @return
57  *   the number of free slots in the ring, i.e. the number of events that
58  *   can be successfully enqueued before dequeue must be called
59  */
60 static __rte_always_inline unsigned int
61 rte_event_ring_free_count(const struct rte_event_ring *r)
62 {
63         return rte_ring_free_count(&r->r);
64 }
65
66 /**
67  * Enqueue a set of events onto a ring
68  *
69  * Note: this API enqueues by copying the events themselves onto the ring,
70  * rather than just placing a pointer to each event onto the ring. This
71  * means that statically-allocated events can safely be enqueued by this
72  * API.
73  *
74  * @param r
75  *   pointer to the event ring
76  * @param events
77  *   pointer to an array of struct rte_event objects
78  * @param n
79  *   number of events in the array to enqueue
80  * @param free_space
81  *   if non-null, is updated to indicate the amount of free space in the
82  *   ring once the enqueue has completed.
83  * @return
84  *   the number of elements, n', enqueued to the ring, 0 <= n' <= n
85  */
86 static __rte_always_inline unsigned int
87 rte_event_ring_enqueue_burst(struct rte_event_ring *r,
88                 const struct rte_event *events,
89                 unsigned int n, uint16_t *free_space)
90 {
91         uint32_t prod_head, prod_next;
92         uint32_t free_entries;
93
94         n = __rte_ring_move_prod_head(&r->r, r->r.prod.single, n,
95                         RTE_RING_QUEUE_VARIABLE,
96                         &prod_head, &prod_next, &free_entries);
97         if (n == 0)
98                 goto end;
99
100         ENQUEUE_PTRS(&r->r, &r[1], prod_head, events, n, struct rte_event);
101
102         update_tail(&r->r.prod, prod_head, prod_next, r->r.prod.single, 1);
103 end:
104         if (free_space != NULL)
105                 *free_space = free_entries - n;
106         return n;
107 }
108
109 /**
110  * Dequeue a set of events from a ring
111  *
112  * Note: this API does not work with pointers to events, rather it copies
113  * the events themselves to the destination ``events`` buffer.
114  *
115  * @param r
116  *   pointer to the event ring
117  * @param events
118  *   pointer to an array to hold the struct rte_event objects
119  * @param n
120  *   number of events that can be held in the ``events`` array
121  * @param available
122  *   if non-null, is updated to indicate the number of events remaining in
123  *   the ring once the dequeue has completed
124  * @return
125  *   the number of elements, n', dequeued from the ring, 0 <= n' <= n
126  */
127 static __rte_always_inline unsigned int
128 rte_event_ring_dequeue_burst(struct rte_event_ring *r,
129                 struct rte_event *events,
130                 unsigned int n, uint16_t *available)
131 {
132         uint32_t cons_head, cons_next;
133         uint32_t entries;
134
135         n = __rte_ring_move_cons_head(&r->r, r->r.cons.single, n,
136                         RTE_RING_QUEUE_VARIABLE,
137                         &cons_head, &cons_next, &entries);
138         if (n == 0)
139                 goto end;
140
141         DEQUEUE_PTRS(&r->r, &r[1], cons_head, events, n, struct rte_event);
142
143         update_tail(&r->r.cons, cons_head, cons_next, r->r.cons.single, 0);
144
145 end:
146         if (available != NULL)
147                 *available = entries - n;
148         return n;
149 }
150
151 /*
152  * Initializes an already-allocated ring structure
153  *
154  * @param r
155  *   pointer to the ring memory to be initialized
156  * @param name
157  *   name to be given to the ring
158  * @param count
159  *   the number of elements to be stored in the ring. If the flag
160  *   ``RING_F_EXACT_SZ`` is not set, this must be a power of 2, and the actual
161  *   usable space in the ring will be ``count - 1`` entries. If the flag
162  *   ``RING_F_EXACT_SZ`` is set, the this can be any value up to the ring size
163  *   limit - 1, and the usable space will be exactly that requested.
164  * @param flags
165  *   An OR of the following:
166  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
167  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
168  *      is "single-producer". Otherwise, it is "multi-producers".
169  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
170  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
171  *      is "single-consumer". Otherwise, it is "multi-consumers".
172  *    - RING_F_EXACT_SZ: If this flag is set, the ``count`` parameter is to
173  *      be taken as the exact usable size of the ring, and as such does not
174  *      need to be a power of 2. The underlying ring memory should be a
175  *      power-of-2 size greater than the count value.
176  * @return
177  *   0 on success, or a negative value on error.
178  */
179 int
180 rte_event_ring_init(struct rte_event_ring *r, const char *name,
181         unsigned int count, unsigned int flags);
182
183 /*
184  * Create an event ring structure
185  *
186  * This function allocates memory and initializes an event ring inside that
187  * memory.
188  *
189  * @param name
190  *   name to be given to the ring
191  * @param count
192  *   the number of elements to be stored in the ring. If the flag
193  *   ``RING_F_EXACT_SZ`` is not set, this must be a power of 2, and the actual
194  *   usable space in the ring will be ``count - 1`` entries. If the flag
195  *   ``RING_F_EXACT_SZ`` is set, the this can be any value up to the ring size
196  *   limit - 1, and the usable space will be exactly that requested.
197  * @param socket_id
198  *   The *socket_id* argument is the socket identifier in case of
199  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
200  *   constraint for the reserved zone.
201  * @param flags
202  *   An OR of the following:
203  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
204  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
205  *      is "single-producer". Otherwise, it is "multi-producers".
206  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
207  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
208  *      is "single-consumer". Otherwise, it is "multi-consumers".
209  *    - RING_F_EXACT_SZ: If this flag is set, the ``count`` parameter is to
210  *      be taken as the exact usable size of the ring, and as such does not
211  *      need to be a power of 2. The underlying ring memory should be a
212  *      power-of-2 size greater than the count value.
213  * @return
214  *   On success, the pointer to the new allocated ring. NULL on error with
215  *    rte_errno set appropriately. Possible errno values include:
216  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
217  *    - E_RTE_SECONDARY - function was called from a secondary process instance
218  *    - EINVAL - count provided is not a power of 2
219  *    - ENOSPC - the maximum number of memzones has already been allocated
220  *    - EEXIST - a memzone with the same name already exists
221  *    - ENOMEM - no appropriate memory area found in which to create memzone
222  */
223 struct rte_event_ring *
224 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
225                 unsigned int flags);
226
227 /**
228  * Search for an event ring based on its name
229  *
230  * @param name
231  *   The name of the ring.
232  * @return
233  *   The pointer to the ring matching the name, or NULL if not found,
234  *   with rte_errno set appropriately. Possible rte_errno values include:
235  *    - ENOENT - required entry not available to return.
236  */
237 struct rte_event_ring *
238 rte_event_ring_lookup(const char *name);
239
240 /**
241  * De-allocate all memory used by the ring.
242  *
243  * @param r
244  *   Ring to free
245  */
246 void
247 rte_event_ring_free(struct rte_event_ring *r);
248
249 /**
250  * Return the size of the event ring.
251  *
252  * @param r
253  *   A pointer to the ring structure.
254  * @return
255  *   The size of the data store used by the ring.
256  *   NOTE: this is not the same as the usable space in the ring. To query that
257  *   use ``rte_ring_get_capacity()``.
258  */
259 static inline unsigned int
260 rte_event_ring_get_size(const struct rte_event_ring *r)
261 {
262         return rte_ring_get_size(&r->r);
263 }
264
265 /**
266  * Return the number of elements which can be stored in the event ring.
267  *
268  * @param r
269  *   A pointer to the ring structure.
270  * @return
271  *   The usable size of the ring.
272  */
273 static inline unsigned int
274 rte_event_ring_get_capacity(const struct rte_event_ring *r)
275 {
276         return rte_ring_get_capacity(&r->r);
277 }
278 #endif