1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2010-2020 Intel Corporation
4 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Derived from FreeBSD's bufring.h
7 * Used as BSD-3 Licensed with permission from Kip Macy.
10 #ifndef _RTE_RING_PEEK_H_
11 #define _RTE_RING_PEEK_H_
15 * @b EXPERIMENTAL: this API may change without prior notice
16 * It is not recommended to include this file directly.
17 * Please include <rte_ring_elem.h> instead.
20 * Introduction of rte_ring with serialized producer/consumer (HTS sync mode)
21 * makes possible to split public enqueue/dequeue API into two phases:
22 * - enqueue/dequeue start
23 * - enqueue/dequeue finish
24 * That allows user to inspect objects in the ring without removing them
25 * from it (aka MT safe peek).
26 * Note that right now this new API is available only for two sync modes:
27 * 1) Single Producer/Single Consumer (RTE_RING_SYNC_ST)
28 * 2) Serialized Producer/Serialized Consumer (RTE_RING_SYNC_MT_HTS).
29 * It is a user responsibility to create/init ring with appropriate sync
32 * // read 1 elem from the ring:
33 * n = rte_ring_dequeue_bulk_start(ring, &obj, 1, NULL);
36 * if (object_examine(obj) == KEEP)
37 * //decided to keep it in the ring.
38 * rte_ring_dequeue_finish(ring, 0);
40 * //decided to remove it from the ring.
41 * rte_ring_dequeue_finish(ring, n);
43 * Note that between _start_ and _finish_ none other thread can proceed
44 * with enqueue(/dequeue) operation till _finish_ completes.
51 #include <rte_ring_peek_elem_pvt.h>
54 * Start to enqueue several objects on the ring.
55 * Note that no actual objects are put in the queue by this function,
56 * it just reserves for user such ability.
57 * User has to call appropriate enqueue_elem_finish() to copy objects into the
58 * queue and complete given enqueue operation.
61 * A pointer to the ring structure.
63 * The number of objects to add in the ring from the obj_table.
65 * if non-NULL, returns the amount of space in the ring after the
66 * enqueue operation has finished.
68 * The number of objects that can be enqueued, either 0 or n
71 static __rte_always_inline unsigned int
72 rte_ring_enqueue_bulk_elem_start(struct rte_ring *r, unsigned int n,
73 unsigned int *free_space)
75 return __rte_ring_do_enqueue_start(r, n, RTE_RING_QUEUE_FIXED,
80 * Start to enqueue several objects on the ring.
81 * Note that no actual objects are put in the queue by this function,
82 * it just reserves for user such ability.
83 * User has to call appropriate enqueue_finish() to copy objects into the
84 * queue and complete given enqueue operation.
87 * A pointer to the ring structure.
89 * The number of objects to add in the ring from the obj_table.
91 * if non-NULL, returns the amount of space in the ring after the
92 * enqueue operation has finished.
94 * The number of objects that can be enqueued, either 0 or n
97 static __rte_always_inline unsigned int
98 rte_ring_enqueue_bulk_start(struct rte_ring *r, unsigned int n,
99 unsigned int *free_space)
101 return rte_ring_enqueue_bulk_elem_start(r, n, free_space);
105 * Start to enqueue several objects on the ring.
106 * Note that no actual objects are put in the queue by this function,
107 * it just reserves for user such ability.
108 * User has to call appropriate enqueue_elem_finish() to copy objects into the
109 * queue and complete given enqueue operation.
112 * A pointer to the ring structure.
114 * The number of objects to add in the ring from the obj_table.
116 * if non-NULL, returns the amount of space in the ring after the
117 * enqueue operation has finished.
119 * Actual number of objects that can be enqueued.
122 static __rte_always_inline unsigned int
123 rte_ring_enqueue_burst_elem_start(struct rte_ring *r, unsigned int n,
124 unsigned int *free_space)
126 return __rte_ring_do_enqueue_start(r, n, RTE_RING_QUEUE_VARIABLE,
131 * Start to enqueue several objects on the ring.
132 * Note that no actual objects are put in the queue by this function,
133 * it just reserves for user such ability.
134 * User has to call appropriate enqueue_finish() to copy objects into the
135 * queue and complete given enqueue operation.
138 * A pointer to the ring structure.
140 * The number of objects to add in the ring from the obj_table.
142 * if non-NULL, returns the amount of space in the ring after the
143 * enqueue operation has finished.
145 * Actual number of objects that can be enqueued.
148 static __rte_always_inline unsigned int
149 rte_ring_enqueue_burst_start(struct rte_ring *r, unsigned int n,
150 unsigned int *free_space)
152 return rte_ring_enqueue_burst_elem_start(r, n, free_space);
156 * Complete to enqueue several objects on the ring.
157 * Note that number of objects to enqueue should not exceed previous
158 * enqueue_start return value.
161 * A pointer to the ring structure.
163 * A pointer to a table of objects.
165 * The size of ring element, in bytes. It must be a multiple of 4.
166 * This must be the same value used while creating the ring. Otherwise
167 * the results are undefined.
169 * The number of objects to add to the ring from the obj_table.
172 static __rte_always_inline void
173 rte_ring_enqueue_elem_finish(struct rte_ring *r, const void *obj_table,
174 unsigned int esize, unsigned int n)
178 switch (r->prod.sync_type) {
179 case RTE_RING_SYNC_ST:
180 n = __rte_ring_st_get_tail(&r->prod, &tail, n);
182 __rte_ring_enqueue_elems(r, tail, obj_table, esize, n);
183 __rte_ring_st_set_head_tail(&r->prod, tail, n, 1);
185 case RTE_RING_SYNC_MT_HTS:
186 n = __rte_ring_hts_get_tail(&r->hts_prod, &tail, n);
188 __rte_ring_enqueue_elems(r, tail, obj_table, esize, n);
189 __rte_ring_hts_set_head_tail(&r->hts_prod, tail, n, 1);
191 case RTE_RING_SYNC_MT:
192 case RTE_RING_SYNC_MT_RTS:
194 /* unsupported mode, shouldn't be here */
200 * Complete to enqueue several objects on the ring.
201 * Note that number of objects to enqueue should not exceed previous
202 * enqueue_start return value.
205 * A pointer to the ring structure.
207 * A pointer to a table of objects.
209 * The number of objects to add to the ring from the obj_table.
212 static __rte_always_inline void
213 rte_ring_enqueue_finish(struct rte_ring *r, void * const *obj_table,
216 rte_ring_enqueue_elem_finish(r, obj_table, sizeof(uintptr_t), n);
220 * Start to dequeue several objects from the ring.
221 * Note that user has to call appropriate dequeue_finish()
222 * to complete given dequeue operation and actually remove objects the ring.
225 * A pointer to the ring structure.
227 * A pointer to a table of objects that will be filled.
229 * The size of ring element, in bytes. It must be a multiple of 4.
230 * This must be the same value used while creating the ring. Otherwise
231 * the results are undefined.
233 * The number of objects to dequeue from the ring to the obj_table.
235 * If non-NULL, returns the number of remaining ring entries after the
236 * dequeue has finished.
238 * The number of objects dequeued, either 0 or n.
241 static __rte_always_inline unsigned int
242 rte_ring_dequeue_bulk_elem_start(struct rte_ring *r, void *obj_table,
243 unsigned int esize, unsigned int n, unsigned int *available)
245 return __rte_ring_do_dequeue_start(r, obj_table, esize, n,
246 RTE_RING_QUEUE_FIXED, available);
250 * Start to dequeue several objects from the ring.
251 * Note that user has to call appropriate dequeue_finish()
252 * to complete given dequeue operation and actually remove objects the ring.
255 * A pointer to the ring structure.
257 * A pointer to a table of void * pointers (objects) that will be filled.
259 * The number of objects to dequeue from the ring to the obj_table.
261 * If non-NULL, returns the number of remaining ring entries after the
262 * dequeue has finished.
264 * Actual number of objects dequeued.
267 static __rte_always_inline unsigned int
268 rte_ring_dequeue_bulk_start(struct rte_ring *r, void **obj_table,
269 unsigned int n, unsigned int *available)
271 return rte_ring_dequeue_bulk_elem_start(r, obj_table, sizeof(uintptr_t),
276 * Start to dequeue several objects from the ring.
277 * Note that user has to call appropriate dequeue_finish()
278 * to complete given dequeue operation and actually remove objects the ring.
281 * A pointer to the ring structure.
283 * A pointer to a table of objects that will be filled.
285 * The size of ring element, in bytes. It must be a multiple of 4.
286 * This must be the same value used while creating the ring. Otherwise
287 * the results are undefined.
289 * The number of objects to dequeue from the ring to the obj_table.
291 * If non-NULL, returns the number of remaining ring entries after the
292 * dequeue has finished.
294 * The actual number of objects dequeued.
297 static __rte_always_inline unsigned int
298 rte_ring_dequeue_burst_elem_start(struct rte_ring *r, void *obj_table,
299 unsigned int esize, unsigned int n, unsigned int *available)
301 return __rte_ring_do_dequeue_start(r, obj_table, esize, n,
302 RTE_RING_QUEUE_VARIABLE, available);
306 * Start to dequeue several objects from the ring.
307 * Note that user has to call appropriate dequeue_finish()
308 * to complete given dequeue operation and actually remove objects the ring.
311 * A pointer to the ring structure.
313 * A pointer to a table of void * pointers (objects) that will be filled.
315 * The number of objects to dequeue from the ring to the obj_table.
317 * If non-NULL, returns the number of remaining ring entries after the
318 * dequeue has finished.
320 * The actual number of objects dequeued.
323 static __rte_always_inline unsigned int
324 rte_ring_dequeue_burst_start(struct rte_ring *r, void **obj_table,
325 unsigned int n, unsigned int *available)
327 return rte_ring_dequeue_burst_elem_start(r, obj_table,
328 sizeof(uintptr_t), n, available);
332 * Complete to dequeue several objects from the ring.
333 * Note that number of objects to dequeue should not exceed previous
334 * dequeue_start return value.
337 * A pointer to the ring structure.
339 * The number of objects to remove from the ring.
342 static __rte_always_inline void
343 rte_ring_dequeue_elem_finish(struct rte_ring *r, unsigned int n)
347 switch (r->cons.sync_type) {
348 case RTE_RING_SYNC_ST:
349 n = __rte_ring_st_get_tail(&r->cons, &tail, n);
350 __rte_ring_st_set_head_tail(&r->cons, tail, n, 0);
352 case RTE_RING_SYNC_MT_HTS:
353 n = __rte_ring_hts_get_tail(&r->hts_cons, &tail, n);
354 __rte_ring_hts_set_head_tail(&r->hts_cons, tail, n, 0);
356 case RTE_RING_SYNC_MT:
357 case RTE_RING_SYNC_MT_RTS:
359 /* unsupported mode, shouldn't be here */
365 * Complete to dequeue several objects from the ring.
366 * Note that number of objects to dequeue should not exceed previous
367 * dequeue_start return value.
370 * A pointer to the ring structure.
372 * The number of objects to remove from the ring.
375 static __rte_always_inline void
376 rte_ring_dequeue_finish(struct rte_ring *r, unsigned int n)
378 rte_ring_dequeue_elem_finish(r, n);
385 #endif /* _RTE_RING_PEEK_H_ */