1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
9 * This provides a ring implementation for passing rte_event structures
10 * from one core to another.
13 #ifndef _RTE_EVENT_RING_
14 #define _RTE_EVENT_RING_
18 #include <rte_common.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
22 #include "rte_eventdev.h"
24 #define RTE_TAILQ_EVENT_RING_NAME "RTE_EVENT_RING"
27 * Generic ring structure for passing rte_event objects from core to core.
29 * Based on the primitives given in the rte_ring library. Designed to be
30 * used inside software eventdev implementations and by applications
33 struct rte_event_ring {
38 * Returns the number of events in the ring
41 * pointer to the event ring
43 * the number of events in the ring
45 static __rte_always_inline unsigned int
46 rte_event_ring_count(const struct rte_event_ring *r)
48 return rte_ring_count(&r->r);
52 * Returns the amount of free space in the ring
55 * pointer to the event ring
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
60 static __rte_always_inline unsigned int
61 rte_event_ring_free_count(const struct rte_event_ring *r)
63 return rte_ring_free_count(&r->r);
67 * Enqueue a set of events onto a ring
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
75 * pointer to the event ring
77 * pointer to an array of struct rte_event objects
79 * number of events in the array to enqueue
81 * if non-null, is updated to indicate the amount of free space in the
82 * ring once the enqueue has completed.
84 * the number of elements, n', enqueued to the ring, 0 <= n' <= n
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)
91 uint32_t prod_head, prod_next;
92 uint32_t free_entries;
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);
100 ENQUEUE_PTRS(&r->r, &r[1], prod_head, events, n, struct rte_event);
102 update_tail(&r->r.prod, prod_head, prod_next, r->r.prod.single, 1);
104 if (free_space != NULL)
105 *free_space = free_entries - n;
110 * Dequeue a set of events from a ring
112 * Note: this API does not work with pointers to events, rather it copies
113 * the events themselves to the destination ``events`` buffer.
116 * pointer to the event ring
118 * pointer to an array to hold the struct rte_event objects
120 * number of events that can be held in the ``events`` array
122 * if non-null, is updated to indicate the number of events remaining in
123 * the ring once the dequeue has completed
125 * the number of elements, n', dequeued from the ring, 0 <= n' <= n
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)
132 uint32_t cons_head, cons_next;
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);
141 DEQUEUE_PTRS(&r->r, &r[1], cons_head, events, n, struct rte_event);
143 update_tail(&r->r.cons, cons_head, cons_next, r->r.cons.single, 0);
146 if (available != NULL)
147 *available = entries - n;
152 * Initializes an already-allocated ring structure
155 * pointer to the ring memory to be initialized
157 * name to be given to the ring
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.
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.
177 * 0 on success, or a negative value on error.
180 rte_event_ring_init(struct rte_event_ring *r, const char *name,
181 unsigned int count, unsigned int flags);
184 * Create an event ring structure
186 * This function allocates memory and initializes an event ring inside that
190 * name to be given to the ring
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.
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.
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.
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
223 struct rte_event_ring *
224 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
228 * Search for an event ring based on its name
231 * The name of the ring.
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.
237 struct rte_event_ring *
238 rte_event_ring_lookup(const char *name);
241 * De-allocate all memory used by the ring.
247 rte_event_ring_free(struct rte_event_ring *r);
250 * Return the size of the event ring.
253 * A pointer to the ring structure.
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()``.
259 static inline unsigned int
260 rte_event_ring_get_size(const struct rte_event_ring *r)
262 return rte_ring_get_size(&r->r);
266 * Return the number of elements which can be stored in the event ring.
269 * A pointer to the ring structure.
271 * The usable size of the ring.
273 static inline unsigned int
274 rte_event_ring_get_capacity(const struct rte_event_ring *r)
276 return rte_ring_get_capacity(&r->r);