Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : *
3 : : * Copyright (c) 2010-2020 Intel Corporation
4 : : * Copyright (c) 2007-2009 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 : : #ifndef _RTE_RING_H_
11 : : #define _RTE_RING_H_
12 : :
13 : : /**
14 : : * @file
15 : : * RTE Ring
16 : : *
17 : : * The Ring Manager is a fixed-size queue, implemented as a table of
18 : : * pointers. Head and tail pointers are modified atomically, allowing
19 : : * concurrent access to it. It has the following features:
20 : : *
21 : : * - FIFO (First In First Out)
22 : : * - Maximum size is fixed; the pointers are stored in a table.
23 : : * - Lockless implementation.
24 : : * - Multi- or single-consumer dequeue.
25 : : * - Multi- or single-producer enqueue.
26 : : * - Bulk dequeue.
27 : : * - Bulk enqueue.
28 : : * - Ability to select different sync modes for producer/consumer.
29 : : * - Dequeue start/finish (depending on consumer sync modes).
30 : : * - Enqueue start/finish (depending on producer sync mode).
31 : : *
32 : : * Note: the ring implementation is not preemptible. Refer to Programmer's
33 : : * guide/Environment Abstraction Layer/Multiple pthread/Known Issues/rte_ring
34 : : * for more information.
35 : : */
36 : :
37 : : #include <rte_common.h>
38 : : #include <rte_ring_core.h>
39 : : #include <rte_ring_elem.h>
40 : :
41 : : #ifdef __cplusplus
42 : : extern "C" {
43 : : #endif
44 : :
45 : : /**
46 : : * Calculate the memory size needed for a ring
47 : : *
48 : : * This function returns the number of bytes needed for a ring, given
49 : : * the number of elements in it. This value is the sum of the size of
50 : : * the structure rte_ring and the size of the memory needed by the
51 : : * objects pointers. The value is aligned to a cache line size.
52 : : *
53 : : * @param count
54 : : * The number of elements in the ring (must be a power of 2).
55 : : * @return
56 : : * - The memory size needed for the ring on success.
57 : : * - -EINVAL if count is not a power of 2.
58 : : */
59 : : ssize_t rte_ring_get_memsize(unsigned int count);
60 : :
61 : : /**
62 : : * Initialize a ring structure.
63 : : *
64 : : * Initialize a ring structure in memory pointed by "r". The size of the
65 : : * memory area must be large enough to store the ring structure and the
66 : : * object table. It is advised to use rte_ring_get_memsize() to get the
67 : : * appropriate size.
68 : : *
69 : : * The ring size is set to *count*, which must be a power of two.
70 : : * The real usable ring size is *count-1* instead of *count* to
71 : : * differentiate a full ring from an empty ring.
72 : : *
73 : : * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
74 : : * memory given by the caller may not be shareable among dpdk
75 : : * processes.
76 : : *
77 : : * @param r
78 : : * The pointer to the ring structure followed by the objects table.
79 : : * @param name
80 : : * The name of the ring.
81 : : * @param count
82 : : * The number of elements in the ring (must be a power of 2,
83 : : * unless RING_F_EXACT_SZ is set in flags).
84 : : * @param flags
85 : : * An OR of the following:
86 : : * - One of mutually exclusive flags that define producer behavior:
87 : : * - RING_F_SP_ENQ: If this flag is set, the default behavior when
88 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
89 : : * is "single-producer".
90 : : * - RING_F_MP_RTS_ENQ: If this flag is set, the default behavior when
91 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
92 : : * is "multi-producer RTS mode".
93 : : * - RING_F_MP_HTS_ENQ: If this flag is set, the default behavior when
94 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
95 : : * is "multi-producer HTS mode".
96 : : * If none of these flags is set, then default "multi-producer"
97 : : * behavior is selected.
98 : : * - One of mutually exclusive flags that define consumer behavior:
99 : : * - RING_F_SC_DEQ: If this flag is set, the default behavior when
100 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
101 : : * is "single-consumer". Otherwise, it is "multi-consumers".
102 : : * - RING_F_MC_RTS_DEQ: If this flag is set, the default behavior when
103 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
104 : : * is "multi-consumer RTS mode".
105 : : * - RING_F_MC_HTS_DEQ: If this flag is set, the default behavior when
106 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
107 : : * is "multi-consumer HTS mode".
108 : : * If none of these flags is set, then default "multi-consumer"
109 : : * behavior is selected.
110 : : * - RING_F_EXACT_SZ: If this flag is set, the ring will hold exactly the
111 : : * requested number of entries, and the requested size will be rounded up
112 : : * to the next power of two, but the usable space will be exactly that
113 : : * requested. Worst case, if a power-of-2 size is requested, half the
114 : : * ring space will be wasted.
115 : : * Without this flag set, the ring size requested must be a power of 2,
116 : : * and the usable space will be that size - 1.
117 : : * @return
118 : : * 0 on success, or a negative value on error.
119 : : */
120 : : int rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
121 : : unsigned int flags);
122 : :
123 : : /**
124 : : * De-allocate all memory used by the ring.
125 : : *
126 : : * @param r
127 : : * Ring to free.
128 : : * If NULL then, the function does nothing.
129 : : */
130 : : void rte_ring_free(struct rte_ring *r);
131 : :
132 : : /**
133 : : * Create a new ring named *name* in memory.
134 : : *
135 : : * This function uses ``memzone_reserve()`` to allocate memory. Then it
136 : : * calls rte_ring_init() to initialize an empty ring.
137 : : *
138 : : * The new ring size is set to *count*, which must be a power of two.
139 : : * The real usable ring size is *count-1* instead of *count* to
140 : : * differentiate a full ring from an empty ring.
141 : : *
142 : : * The ring is added in RTE_TAILQ_RING list.
143 : : *
144 : : * @param name
145 : : * The name of the ring.
146 : : * @param count
147 : : * The size of the ring (must be a power of 2,
148 : : * unless RING_F_EXACT_SZ is set in flags).
149 : : * @param socket_id
150 : : * The *socket_id* argument is the socket identifier in case of
151 : : * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
152 : : * constraint for the reserved zone.
153 : : * @param flags
154 : : * An OR of the following:
155 : : * - One of mutually exclusive flags that define producer behavior:
156 : : * - RING_F_SP_ENQ: If this flag is set, the default behavior when
157 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
158 : : * is "single-producer".
159 : : * - RING_F_MP_RTS_ENQ: If this flag is set, the default behavior when
160 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
161 : : * is "multi-producer RTS mode".
162 : : * - RING_F_MP_HTS_ENQ: If this flag is set, the default behavior when
163 : : * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
164 : : * is "multi-producer HTS mode".
165 : : * If none of these flags is set, then default "multi-producer"
166 : : * behavior is selected.
167 : : * - One of mutually exclusive flags that define consumer behavior:
168 : : * - RING_F_SC_DEQ: If this flag is set, the default behavior when
169 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
170 : : * is "single-consumer". Otherwise, it is "multi-consumers".
171 : : * - RING_F_MC_RTS_DEQ: If this flag is set, the default behavior when
172 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
173 : : * is "multi-consumer RTS mode".
174 : : * - RING_F_MC_HTS_DEQ: If this flag is set, the default behavior when
175 : : * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
176 : : * is "multi-consumer HTS mode".
177 : : * If none of these flags is set, then default "multi-consumer"
178 : : * behavior is selected.
179 : : * - RING_F_EXACT_SZ: If this flag is set, the ring will hold exactly the
180 : : * requested number of entries, and the requested size will be rounded up
181 : : * to the next power of two, but the usable space will be exactly that
182 : : * requested. Worst case, if a power-of-2 size is requested, half the
183 : : * ring space will be wasted.
184 : : * Without this flag set, the ring size requested must be a power of 2,
185 : : * and the usable space will be that size - 1.
186 : : * @return
187 : : * On success, the pointer to the new allocated ring. NULL on error with
188 : : * rte_errno set appropriately. Possible errno values include:
189 : : * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
190 : : * - EINVAL - count provided is not a power of 2
191 : : * - ENOSPC - the maximum number of memzones has already been allocated
192 : : * - EEXIST - a memzone with the same name already exists
193 : : * - ENOMEM - no appropriate memory area found in which to create memzone
194 : : */
195 : : struct rte_ring *rte_ring_create(const char *name, unsigned int count,
196 : : int socket_id, unsigned int flags)
197 : : __rte_malloc __rte_dealloc(rte_ring_free, 1);
198 : :
199 : : /**
200 : : * Dump the status of the ring to a file.
201 : : *
202 : : * @param f
203 : : * A pointer to a file for output
204 : : * @param r
205 : : * A pointer to the ring structure.
206 : : */
207 : : void rte_ring_dump(FILE *f, const struct rte_ring *r);
208 : :
209 : : /**
210 : : * @warning
211 : : * @b EXPERIMENTAL: this API may change without prior notice.
212 : : *
213 : : * Dump the status of a headtail to a file.
214 : : *
215 : : * @param f
216 : : * A pointer to a file for output
217 : : * @param prefix
218 : : * A string to prefix each output line with
219 : : * @param r
220 : : * A pointer to a ring headtail structure.
221 : : */
222 : : __rte_experimental
223 : : void
224 : : rte_ring_headtail_dump(FILE *f, const char *prefix,
225 : : const struct rte_ring_headtail *r);
226 : :
227 : : /**
228 : : * Enqueue several objects on the ring (multi-producers safe).
229 : : *
230 : : * This function uses a "compare and set" instruction to move the
231 : : * producer index atomically.
232 : : *
233 : : * @param r
234 : : * A pointer to the ring structure.
235 : : * @param obj_table
236 : : * A pointer to a table of void * pointers (objects).
237 : : * @param n
238 : : * The number of objects to add in the ring from the obj_table.
239 : : * @param free_space
240 : : * if non-NULL, returns the amount of space in the ring after the
241 : : * enqueue operation has finished.
242 : : * @return
243 : : * The number of objects enqueued, either 0 or n
244 : : */
245 : : static __rte_always_inline unsigned int
246 : 277 : rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
247 : : unsigned int n, unsigned int *free_space)
248 : : {
249 : 277 : return rte_ring_mp_enqueue_bulk_elem(r, obj_table, sizeof(void *),
250 : : n, free_space);
251 : : }
252 : :
253 : : /**
254 : : * Enqueue several objects on a ring (NOT multi-producers safe).
255 : : *
256 : : * @param r
257 : : * A pointer to the ring structure.
258 : : * @param obj_table
259 : : * A pointer to a table of void * pointers (objects).
260 : : * @param n
261 : : * The number of objects to add in the ring from the obj_table.
262 : : * @param free_space
263 : : * if non-NULL, returns the amount of space in the ring after the
264 : : * enqueue operation has finished.
265 : : * @return
266 : : * The number of objects enqueued, either 0 or n
267 : : */
268 : : static __rte_always_inline unsigned int
269 : 277 : rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
270 : : unsigned int n, unsigned int *free_space)
271 : : {
272 : 277 : return rte_ring_sp_enqueue_bulk_elem(r, obj_table, sizeof(void *),
273 : : n, free_space);
274 : : }
275 : :
276 : : /**
277 : : * Enqueue several objects on a ring.
278 : : *
279 : : * This function calls the multi-producer or the single-producer
280 : : * version depending on the default behavior that was specified at
281 : : * ring creation time (see flags).
282 : : *
283 : : * @param r
284 : : * A pointer to the ring structure.
285 : : * @param obj_table
286 : : * A pointer to a table of void * pointers (objects).
287 : : * @param n
288 : : * The number of objects to add in the ring from the obj_table.
289 : : * @param free_space
290 : : * if non-NULL, returns the amount of space in the ring after the
291 : : * enqueue operation has finished.
292 : : * @return
293 : : * The number of objects enqueued, either 0 or n
294 : : */
295 : : static __rte_always_inline unsigned int
296 [ + - + + : 831 : rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
- ]
297 : : unsigned int n, unsigned int *free_space)
298 : : {
299 : 831 : return rte_ring_enqueue_bulk_elem(r, obj_table, sizeof(void *),
300 : : n, free_space);
301 : : }
302 : :
303 : : /**
304 : : * Enqueue one object on a ring (multi-producers safe).
305 : : *
306 : : * This function uses a "compare and set" instruction to move the
307 : : * producer index atomically.
308 : : *
309 : : * @param r
310 : : * A pointer to the ring structure.
311 : : * @param obj
312 : : * A pointer to the object to be added.
313 : : * @return
314 : : * - 0: Success; objects enqueued.
315 : : * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
316 : : */
317 : : static __rte_always_inline int
318 : : rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
319 : : {
320 : : return rte_ring_mp_enqueue_elem(r, &obj, sizeof(void *));
321 : : }
322 : :
323 : : /**
324 : : * Enqueue one object on a ring (NOT multi-producers safe).
325 : : *
326 : : * @param r
327 : : * A pointer to the ring structure.
328 : : * @param obj
329 : : * A pointer to the object to be added.
330 : : * @return
331 : : * - 0: Success; objects enqueued.
332 : : * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
333 : : */
334 : : static __rte_always_inline int
335 : : rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
336 : : {
337 : : return rte_ring_sp_enqueue_elem(r, &obj, sizeof(void *));
338 : : }
339 : :
340 : : /**
341 : : * Enqueue one object on a ring.
342 : : *
343 : : * This function calls the multi-producer or the single-producer
344 : : * version, depending on the default behaviour that was specified at
345 : : * ring creation time (see flags).
346 : : *
347 : : * @param r
348 : : * A pointer to the ring structure.
349 : : * @param obj
350 : : * A pointer to the object to be added.
351 : : * @return
352 : : * - 0: Success; objects enqueued.
353 : : * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
354 : : */
355 : : static __rte_always_inline int
356 : : rte_ring_enqueue(struct rte_ring *r, void *obj)
357 : : {
358 : : return rte_ring_enqueue_elem(r, &obj, sizeof(void *));
359 : : }
360 : :
361 : : /**
362 : : * Dequeue several objects from a ring (multi-consumers safe).
363 : : *
364 : : * This function uses a "compare and set" instruction to move the
365 : : * consumer index atomically.
366 : : *
367 : : * @param r
368 : : * A pointer to the ring structure.
369 : : * @param obj_table
370 : : * A pointer to a table of void * pointers (objects) that will be filled.
371 : : * @param n
372 : : * The number of objects to dequeue from the ring to the obj_table.
373 : : * @param available
374 : : * If non-NULL, returns the number of remaining ring entries after the
375 : : * dequeue has finished.
376 : : * @return
377 : : * The number of objects dequeued, either 0 or n
378 : : */
379 : : static __rte_always_inline unsigned int
380 : 276 : rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
381 : : unsigned int n, unsigned int *available)
382 : : {
383 : 276 : return rte_ring_mc_dequeue_bulk_elem(r, obj_table, sizeof(void *),
384 : : n, available);
385 : : }
386 : :
387 : : /**
388 : : * Dequeue several objects from a ring (NOT multi-consumers safe).
389 : : *
390 : : * @param r
391 : : * A pointer to the ring structure.
392 : : * @param obj_table
393 : : * A pointer to a table of void * pointers (objects) that will be filled.
394 : : * @param n
395 : : * The number of objects to dequeue from the ring to the obj_table,
396 : : * must be strictly positive.
397 : : * @param available
398 : : * If non-NULL, returns the number of remaining ring entries after the
399 : : * dequeue has finished.
400 : : * @return
401 : : * The number of objects dequeued, either 0 or n
402 : : */
403 : : static __rte_always_inline unsigned int
404 : 276 : rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
405 : : unsigned int n, unsigned int *available)
406 : : {
407 : 276 : return rte_ring_sc_dequeue_bulk_elem(r, obj_table, sizeof(void *),
408 : : n, available);
409 : : }
410 : :
411 : : /**
412 : : * Dequeue several objects from a ring.
413 : : *
414 : : * This function calls the multi-consumers or the single-consumer
415 : : * version, depending on the default behaviour that was specified at
416 : : * ring creation time (see flags).
417 : : *
418 : : * @param r
419 : : * A pointer to the ring structure.
420 : : * @param obj_table
421 : : * A pointer to a table of void * pointers (objects) that will be filled.
422 : : * @param n
423 : : * The number of objects to dequeue from the ring to the obj_table.
424 : : * @param available
425 : : * If non-NULL, returns the number of remaining ring entries after the
426 : : * dequeue has finished.
427 : : * @return
428 : : * The number of objects dequeued, either 0 or n
429 : : */
430 : : static __rte_always_inline unsigned int
431 [ + - + + : 828 : rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
- ]
432 : : unsigned int *available)
433 : : {
434 : 828 : return rte_ring_dequeue_bulk_elem(r, obj_table, sizeof(void *),
435 : : n, available);
436 : : }
437 : :
438 : : /**
439 : : * Dequeue one object from a ring (multi-consumers safe).
440 : : *
441 : : * This function uses a "compare and set" instruction to move the
442 : : * consumer index atomically.
443 : : *
444 : : * @param r
445 : : * A pointer to the ring structure.
446 : : * @param obj_p
447 : : * A pointer to a void * pointer (object) that will be filled.
448 : : * @return
449 : : * - 0: Success; objects dequeued.
450 : : * - -ENOENT: Not enough entries in the ring to dequeue; no object is
451 : : * dequeued.
452 : : */
453 : : static __rte_always_inline int
454 : : rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
455 : : {
456 : : return rte_ring_mc_dequeue_elem(r, obj_p, sizeof(void *));
457 : : }
458 : :
459 : : /**
460 : : * Dequeue one object from a ring (NOT multi-consumers safe).
461 : : *
462 : : * @param r
463 : : * A pointer to the ring structure.
464 : : * @param obj_p
465 : : * A pointer to a void * pointer (object) that will be filled.
466 : : * @return
467 : : * - 0: Success; objects dequeued.
468 : : * - -ENOENT: Not enough entries in the ring to dequeue, no object is
469 : : * dequeued.
470 : : */
471 : : static __rte_always_inline int
472 : : rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
473 : : {
474 : : return rte_ring_sc_dequeue_elem(r, obj_p, sizeof(void *));
475 : : }
476 : :
477 : : /**
478 : : * Dequeue one object from a ring.
479 : : *
480 : : * This function calls the multi-consumers or the single-consumer
481 : : * version depending on the default behaviour that was specified at
482 : : * ring creation time (see flags).
483 : : *
484 : : * @param r
485 : : * A pointer to the ring structure.
486 : : * @param obj_p
487 : : * A pointer to a void * pointer (object) that will be filled.
488 : : * @return
489 : : * - 0: Success, objects dequeued.
490 : : * - -ENOENT: Not enough entries in the ring to dequeue, no object is
491 : : * dequeued.
492 : : */
493 : : static __rte_always_inline int
494 : : rte_ring_dequeue(struct rte_ring *r, void **obj_p)
495 : : {
496 : : return rte_ring_dequeue_elem(r, obj_p, sizeof(void *));
497 : : }
498 : :
499 : : /**
500 : : * Flush a ring.
501 : : *
502 : : * This function flush all the elements in a ring
503 : : *
504 : : * @warning
505 : : * Make sure the ring is not in use while calling this function.
506 : : *
507 : : * @param r
508 : : * A pointer to the ring structure.
509 : : */
510 : : void
511 : : rte_ring_reset(struct rte_ring *r);
512 : :
513 : : /**
514 : : * Return the number of entries in a ring.
515 : : *
516 : : * @param r
517 : : * A pointer to the ring structure.
518 : : * @return
519 : : * The number of entries in the ring.
520 : : */
521 : : static inline unsigned int
522 : 0 : rte_ring_count(const struct rte_ring *r)
523 : : {
524 : 8402495 : uint32_t prod_tail = r->prod.tail;
525 : 8402495 : uint32_t cons_tail = r->cons.tail;
526 [ - + - + ]: 8400174 : uint32_t count = (prod_tail - cons_tail) & r->mask;
527 [ + + - + : 8399990 : return (count > r->capacity) ? r->capacity : count;
+ + + + #
# # # #
# ]
528 : : }
529 : :
530 : : /**
531 : : * Return the number of free entries in a ring.
532 : : *
533 : : * @param r
534 : : * A pointer to the ring structure.
535 : : * @return
536 : : * The number of free entries in the ring.
537 : : */
538 : : static inline unsigned int
539 : : rte_ring_free_count(const struct rte_ring *r)
540 : : {
541 [ - + - + : 8398205 : return r->capacity - rte_ring_count(r);
- + - + ]
542 : : }
543 : :
544 : : /**
545 : : * Test if a ring is full.
546 : : *
547 : : * @param r
548 : : * A pointer to the ring structure.
549 : : * @return
550 : : * - 1: The ring is full.
551 : : * - 0: The ring is not full.
552 : : */
553 : : static inline int
554 : : rte_ring_full(const struct rte_ring *r)
555 : : {
556 [ # # ]: 0 : return rte_ring_free_count(r) == 0;
557 : : }
558 : :
559 : : /**
560 : : * Test if a ring is empty.
561 : : *
562 : : * @param r
563 : : * A pointer to the ring structure.
564 : : * @return
565 : : * - 1: The ring is empty.
566 : : * - 0: The ring is not empty.
567 : : */
568 : : static inline int
569 : : rte_ring_empty(const struct rte_ring *r)
570 : : {
571 : 12857048 : uint32_t prod_tail = r->prod.tail;
572 [ + + - + : 12857048 : uint32_t cons_tail = r->cons.tail;
- + - + -
+ - + ]
573 [ # # ]: 0 : return cons_tail == prod_tail;
574 : : }
575 : :
576 : : /**
577 : : * Return the size of the ring.
578 : : *
579 : : * @param r
580 : : * A pointer to the ring structure.
581 : : * @return
582 : : * The size of the data store used by the ring.
583 : : * NOTE: this is not the same as the usable space in the ring. To query that
584 : : * use ``rte_ring_get_capacity()``.
585 : : */
586 : : static inline unsigned int
587 : : rte_ring_get_size(const struct rte_ring *r)
588 : : {
589 [ + + ]: 6 : return r->size;
590 : : }
591 : :
592 : : /**
593 : : * Return the number of elements which can be stored in the ring.
594 : : *
595 : : * @param r
596 : : * A pointer to the ring structure.
597 : : * @return
598 : : * The usable size of the ring.
599 : : */
600 : : static inline unsigned int
601 : : rte_ring_get_capacity(const struct rte_ring *r)
602 : : {
603 [ + + - + : 1059 : return r->capacity;
- + - + ]
604 : : }
605 : :
606 : : /**
607 : : * Return sync type used by producer in the ring.
608 : : *
609 : : * @param r
610 : : * A pointer to the ring structure.
611 : : * @return
612 : : * Producer sync type value.
613 : : */
614 : : static inline enum rte_ring_sync_type
615 : : rte_ring_get_prod_sync_type(const struct rte_ring *r)
616 : : {
617 [ - - + - ]: 45 : return r->prod.sync_type;
618 : : }
619 : :
620 : : /**
621 : : * Check is the ring for single producer.
622 : : *
623 : : * @param r
624 : : * A pointer to the ring structure.
625 : : * @return
626 : : * true if ring is SP, zero otherwise.
627 : : */
628 : : static inline int
629 : : rte_ring_is_prod_single(const struct rte_ring *r)
630 : : {
631 : : return (rte_ring_get_prod_sync_type(r) == RTE_RING_SYNC_ST);
632 : : }
633 : :
634 : : /**
635 : : * Return sync type used by consumer in the ring.
636 : : *
637 : : * @param r
638 : : * A pointer to the ring structure.
639 : : * @return
640 : : * Consumer sync type value.
641 : : */
642 : : static inline enum rte_ring_sync_type
643 : : rte_ring_get_cons_sync_type(const struct rte_ring *r)
644 : : {
645 [ + - ]: 48 : return r->cons.sync_type;
646 : : }
647 : :
648 : : /**
649 : : * Check is the ring for single consumer.
650 : : *
651 : : * @param r
652 : : * A pointer to the ring structure.
653 : : * @return
654 : : * true if ring is SC, zero otherwise.
655 : : */
656 : : static inline int
657 : : rte_ring_is_cons_single(const struct rte_ring *r)
658 : : {
659 : : return (rte_ring_get_cons_sync_type(r) == RTE_RING_SYNC_ST);
660 : : }
661 : :
662 : : /**
663 : : * Dump the status of all rings on the console
664 : : *
665 : : * @param f
666 : : * A pointer to a file for output
667 : : */
668 : : void rte_ring_list_dump(FILE *f);
669 : :
670 : : /**
671 : : * Search a ring from its name
672 : : *
673 : : * @param name
674 : : * The name of the ring.
675 : : * @return
676 : : * The pointer to the ring matching the name, or NULL if not found,
677 : : * with rte_errno set appropriately. Possible rte_errno values include:
678 : : * - ENOENT - required entry not available to return.
679 : : */
680 : : struct rte_ring *rte_ring_lookup(const char *name);
681 : :
682 : : /**
683 : : * Enqueue several objects on the ring (multi-producers safe).
684 : : *
685 : : * This function uses a "compare and set" instruction to move the
686 : : * producer index atomically.
687 : : *
688 : : * @param r
689 : : * A pointer to the ring structure.
690 : : * @param obj_table
691 : : * A pointer to a table of void * pointers (objects).
692 : : * @param n
693 : : * The number of objects to add in the ring from the obj_table.
694 : : * @param free_space
695 : : * if non-NULL, returns the amount of space in the ring after the
696 : : * enqueue operation has finished.
697 : : * @return
698 : : * - n: Actual number of objects enqueued.
699 : : */
700 : : static __rte_always_inline unsigned int
701 : 277 : rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
702 : : unsigned int n, unsigned int *free_space)
703 : : {
704 : 277 : return rte_ring_mp_enqueue_burst_elem(r, obj_table, sizeof(void *),
705 : : n, free_space);
706 : : }
707 : :
708 : : /**
709 : : * Enqueue several objects on a ring (NOT multi-producers safe).
710 : : *
711 : : * @param r
712 : : * A pointer to the ring structure.
713 : : * @param obj_table
714 : : * A pointer to a table of void * pointers (objects).
715 : : * @param n
716 : : * The number of objects to add in the ring from the obj_table.
717 : : * @param free_space
718 : : * if non-NULL, returns the amount of space in the ring after the
719 : : * enqueue operation has finished.
720 : : * @return
721 : : * - n: Actual number of objects enqueued.
722 : : */
723 : : static __rte_always_inline unsigned int
724 : 277 : rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
725 : : unsigned int n, unsigned int *free_space)
726 : : {
727 : 277 : return rte_ring_sp_enqueue_burst_elem(r, obj_table, sizeof(void *),
728 : : n, free_space);
729 : : }
730 : :
731 : : /**
732 : : * Enqueue several objects on a ring.
733 : : *
734 : : * This function calls the multi-producer or the single-producer
735 : : * version depending on the default behavior that was specified at
736 : : * ring creation time (see flags).
737 : : *
738 : : * @param r
739 : : * A pointer to the ring structure.
740 : : * @param obj_table
741 : : * A pointer to a table of void * pointers (objects).
742 : : * @param n
743 : : * The number of objects to add in the ring from the obj_table.
744 : : * @param free_space
745 : : * if non-NULL, returns the amount of space in the ring after the
746 : : * enqueue operation has finished.
747 : : * @return
748 : : * - n: Actual number of objects enqueued.
749 : : */
750 : : static __rte_always_inline unsigned int
751 [ + - + + : 831 : rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
- ]
752 : : unsigned int n, unsigned int *free_space)
753 : : {
754 : 831 : return rte_ring_enqueue_burst_elem(r, obj_table, sizeof(void *),
755 : : n, free_space);
756 : : }
757 : :
758 : : /**
759 : : * Dequeue several objects from a ring (multi-consumers safe). When the request
760 : : * objects are more than the available objects, only dequeue the actual number
761 : : * of objects
762 : : *
763 : : * This function uses a "compare and set" instruction to move the
764 : : * consumer index atomically.
765 : : *
766 : : * @param r
767 : : * A pointer to the ring structure.
768 : : * @param obj_table
769 : : * A pointer to a table of void * pointers (objects) that will be filled.
770 : : * @param n
771 : : * The number of objects to dequeue from the ring to the obj_table.
772 : : * @param available
773 : : * If non-NULL, returns the number of remaining ring entries after the
774 : : * dequeue has finished.
775 : : * @return
776 : : * - n: Actual number of objects dequeued, 0 if ring is empty
777 : : */
778 : : static __rte_always_inline unsigned int
779 : 276 : rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
780 : : unsigned int n, unsigned int *available)
781 : : {
782 : 276 : return rte_ring_mc_dequeue_burst_elem(r, obj_table, sizeof(void *),
783 : : n, available);
784 : : }
785 : :
786 : : /**
787 : : * Dequeue several objects from a ring (NOT multi-consumers safe).When the
788 : : * request objects are more than the available objects, only dequeue the
789 : : * actual number of objects
790 : : *
791 : : * @param r
792 : : * A pointer to the ring structure.
793 : : * @param obj_table
794 : : * A pointer to a table of void * pointers (objects) that will be filled.
795 : : * @param n
796 : : * The number of objects to dequeue from the ring to the obj_table.
797 : : * @param available
798 : : * If non-NULL, returns the number of remaining ring entries after the
799 : : * dequeue has finished.
800 : : * @return
801 : : * - n: Actual number of objects dequeued, 0 if ring is empty
802 : : */
803 : : static __rte_always_inline unsigned int
804 : 276 : rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
805 : : unsigned int n, unsigned int *available)
806 : : {
807 : 276 : return rte_ring_sc_dequeue_burst_elem(r, obj_table, sizeof(void *),
808 : : n, available);
809 : : }
810 : :
811 : : /**
812 : : * Dequeue multiple objects from a ring up to a maximum number.
813 : : *
814 : : * This function calls the multi-consumers or the single-consumer
815 : : * version, depending on the default behaviour that was specified at
816 : : * ring creation time (see flags).
817 : : *
818 : : * @param r
819 : : * A pointer to the ring structure.
820 : : * @param obj_table
821 : : * A pointer to a table of void * pointers (objects) that will be filled.
822 : : * @param n
823 : : * The number of objects to dequeue from the ring to the obj_table.
824 : : * @param available
825 : : * If non-NULL, returns the number of remaining ring entries after the
826 : : * dequeue has finished.
827 : : * @return
828 : : * - Number of objects dequeued
829 : : */
830 : : static __rte_always_inline unsigned int
831 [ + - + + : 828 : rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
- ]
832 : : unsigned int n, unsigned int *available)
833 : : {
834 : 828 : return rte_ring_dequeue_burst_elem(r, obj_table, sizeof(void *),
835 : : n, available);
836 : : }
837 : :
838 : : #ifdef __cplusplus
839 : : }
840 : : #endif
841 : :
842 : : #endif /* _RTE_RING_H_ */
|