DPDK  19.08.2
rte_ring.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2010-2017 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 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <sys/queue.h>
42 #include <errno.h>
43 #include <rte_common.h>
44 #include <rte_config.h>
45 #include <rte_memory.h>
46 #include <rte_lcore.h>
47 #include <rte_atomic.h>
48 #include <rte_branch_prediction.h>
49 #include <rte_memzone.h>
50 #include <rte_pause.h>
51 
52 #define RTE_TAILQ_RING_NAME "RTE_RING"
53 
54 enum rte_ring_queue_behavior {
55  RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
56  RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items as possible from ring */
57 };
58 
59 #define RTE_RING_MZ_PREFIX "RG_"
60 
61 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
62  sizeof(RTE_RING_MZ_PREFIX) + 1)
63 
64 /* structure to hold a pair of head/tail values and other metadata */
65 struct rte_ring_headtail {
66  volatile uint32_t head;
67  volatile uint32_t tail;
68  uint32_t single;
69 };
70 
81 struct rte_ring {
82  /*
83  * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
84  * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
85  * next time the ABI changes
86  */
88  int flags;
89  const struct rte_memzone *memzone;
91  uint32_t size;
92  uint32_t mask;
93  uint32_t capacity;
95  char pad0 __rte_cache_aligned;
98  struct rte_ring_headtail prod __rte_cache_aligned;
99  char pad1 __rte_cache_aligned;
102  struct rte_ring_headtail cons __rte_cache_aligned;
104 };
105 
106 #define RING_F_SP_ENQ 0x0001
107 #define RING_F_SC_DEQ 0x0002
116 #define RING_F_EXACT_SZ 0x0004
117 #define RTE_RING_SZ_MASK (0x7fffffffU)
119 /* @internal defines for passing to the enqueue dequeue worker functions */
120 #define __IS_SP 1
121 #define __IS_MP 0
122 #define __IS_SC 1
123 #define __IS_MC 0
124 
139 ssize_t rte_ring_get_memsize(unsigned count);
140 
175 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
176  unsigned flags);
177 
217 struct rte_ring *rte_ring_create(const char *name, unsigned count,
218  int socket_id, unsigned flags);
225 void rte_ring_free(struct rte_ring *r);
226 
235 void rte_ring_dump(FILE *f, const struct rte_ring *r);
236 
237 /* the actual enqueue of pointers on the ring.
238  * Placed here since identical code needed in both
239  * single and multi producer enqueue functions */
240 #define ENQUEUE_PTRS(r, ring_start, prod_head, obj_table, n, obj_type) do { \
241  unsigned int i; \
242  const uint32_t size = (r)->size; \
243  uint32_t idx = prod_head & (r)->mask; \
244  obj_type *ring = (obj_type *)ring_start; \
245  if (likely(idx + n < size)) { \
246  for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
247  ring[idx] = obj_table[i]; \
248  ring[idx+1] = obj_table[i+1]; \
249  ring[idx+2] = obj_table[i+2]; \
250  ring[idx+3] = obj_table[i+3]; \
251  } \
252  switch (n & 0x3) { \
253  case 3: \
254  ring[idx++] = obj_table[i++]; /* fallthrough */ \
255  case 2: \
256  ring[idx++] = obj_table[i++]; /* fallthrough */ \
257  case 1: \
258  ring[idx++] = obj_table[i++]; \
259  } \
260  } else { \
261  for (i = 0; idx < size; i++, idx++)\
262  ring[idx] = obj_table[i]; \
263  for (idx = 0; i < n; i++, idx++) \
264  ring[idx] = obj_table[i]; \
265  } \
266 } while (0)
267 
268 /* the actual copy of pointers on the ring to obj_table.
269  * Placed here since identical code needed in both
270  * single and multi consumer dequeue functions */
271 #define DEQUEUE_PTRS(r, ring_start, cons_head, obj_table, n, obj_type) do { \
272  unsigned int i; \
273  uint32_t idx = cons_head & (r)->mask; \
274  const uint32_t size = (r)->size; \
275  obj_type *ring = (obj_type *)ring_start; \
276  if (likely(idx + n < size)) { \
277  for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
278  obj_table[i] = ring[idx]; \
279  obj_table[i+1] = ring[idx+1]; \
280  obj_table[i+2] = ring[idx+2]; \
281  obj_table[i+3] = ring[idx+3]; \
282  } \
283  switch (n & 0x3) { \
284  case 3: \
285  obj_table[i++] = ring[idx++]; /* fallthrough */ \
286  case 2: \
287  obj_table[i++] = ring[idx++]; /* fallthrough */ \
288  case 1: \
289  obj_table[i++] = ring[idx++]; \
290  } \
291  } else { \
292  for (i = 0; idx < size; i++, idx++) \
293  obj_table[i] = ring[idx]; \
294  for (idx = 0; i < n; i++, idx++) \
295  obj_table[i] = ring[idx]; \
296  } \
297 } while (0)
298 
299 /* Between load and load. there might be cpu reorder in weak model
300  * (powerpc/arm).
301  * There are 2 choices for the users
302  * 1.use rmb() memory barrier
303  * 2.use one-direction load_acquire/store_release barrier,defined by
304  * CONFIG_RTE_USE_C11_MEM_MODEL=y
305  * It depends on performance test results.
306  * By default, move common functions to rte_ring_generic.h
307  */
308 #ifdef RTE_USE_C11_MEM_MODEL
309 #include "rte_ring_c11_mem.h"
310 #else
311 #include "rte_ring_generic.h"
312 #endif
313 
334 static __rte_always_inline unsigned int
335 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
336  unsigned int n, enum rte_ring_queue_behavior behavior,
337  unsigned int is_sp, unsigned int *free_space)
338 {
339  uint32_t prod_head, prod_next;
340  uint32_t free_entries;
341 
342  n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
343  &prod_head, &prod_next, &free_entries);
344  if (n == 0)
345  goto end;
346 
347  ENQUEUE_PTRS(r, &r[1], prod_head, obj_table, n, void *);
348 
349  update_tail(&r->prod, prod_head, prod_next, is_sp, 1);
350 end:
351  if (free_space != NULL)
352  *free_space = free_entries - n;
353  return n;
354 }
355 
376 static __rte_always_inline unsigned int
377 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
378  unsigned int n, enum rte_ring_queue_behavior behavior,
379  unsigned int is_sc, unsigned int *available)
380 {
381  uint32_t cons_head, cons_next;
382  uint32_t entries;
383 
384  n = __rte_ring_move_cons_head(r, (int)is_sc, n, behavior,
385  &cons_head, &cons_next, &entries);
386  if (n == 0)
387  goto end;
388 
389  DEQUEUE_PTRS(r, &r[1], cons_head, obj_table, n, void *);
390 
391  update_tail(&r->cons, cons_head, cons_next, is_sc, 0);
392 
393 end:
394  if (available != NULL)
395  *available = entries - n;
396  return n;
397 }
398 
417 static __rte_always_inline unsigned int
418 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
419  unsigned int n, unsigned int *free_space)
420 {
421  return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
422  __IS_MP, free_space);
423 }
424 
440 static __rte_always_inline unsigned int
441 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
442  unsigned int n, unsigned int *free_space)
443 {
444  return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
445  __IS_SP, free_space);
446 }
447 
467 static __rte_always_inline unsigned int
468 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
469  unsigned int n, unsigned int *free_space)
470 {
471  return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
472  r->prod.single, free_space);
473 }
474 
489 static __rte_always_inline int
490 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
491 {
492  return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
493 }
494 
506 static __rte_always_inline int
507 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
508 {
509  return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
510 }
511 
527 static __rte_always_inline int
528 rte_ring_enqueue(struct rte_ring *r, void *obj)
529 {
530  return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
531 }
532 
551 static __rte_always_inline unsigned int
552 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
553  unsigned int n, unsigned int *available)
554 {
555  return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
556  __IS_MC, available);
557 }
558 
575 static __rte_always_inline unsigned int
576 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
577  unsigned int n, unsigned int *available)
578 {
579  return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
580  __IS_SC, available);
581 }
582 
602 static __rte_always_inline unsigned int
603 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
604  unsigned int *available)
605 {
606  return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
607  r->cons.single, available);
608 }
609 
625 static __rte_always_inline int
626 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
627 {
628  return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
629 }
630 
643 static __rte_always_inline int
644 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
645 {
646  return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
647 }
648 
665 static __rte_always_inline int
666 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
667 {
668  return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
669 }
670 
684 __rte_experimental
685 void
686 rte_ring_reset(struct rte_ring *r);
687 
696 static inline unsigned
697 rte_ring_count(const struct rte_ring *r)
698 {
699  uint32_t prod_tail = r->prod.tail;
700  uint32_t cons_tail = r->cons.tail;
701  uint32_t count = (prod_tail - cons_tail) & r->mask;
702  return (count > r->capacity) ? r->capacity : count;
703 }
704 
713 static inline unsigned
715 {
716  return r->capacity - rte_ring_count(r);
717 }
718 
728 static inline int
729 rte_ring_full(const struct rte_ring *r)
730 {
731  return rte_ring_free_count(r) == 0;
732 }
733 
743 static inline int
744 rte_ring_empty(const struct rte_ring *r)
745 {
746  return rte_ring_count(r) == 0;
747 }
748 
759 static inline unsigned int
760 rte_ring_get_size(const struct rte_ring *r)
761 {
762  return r->size;
763 }
764 
773 static inline unsigned int
775 {
776  return r->capacity;
777 }
778 
785 void rte_ring_list_dump(FILE *f);
786 
797 struct rte_ring *rte_ring_lookup(const char *name);
798 
817 static __rte_always_inline unsigned
818 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
819  unsigned int n, unsigned int *free_space)
820 {
821  return __rte_ring_do_enqueue(r, obj_table, n,
822  RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
823 }
824 
840 static __rte_always_inline unsigned
841 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
842  unsigned int n, unsigned int *free_space)
843 {
844  return __rte_ring_do_enqueue(r, obj_table, n,
845  RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
846 }
847 
867 static __rte_always_inline unsigned
868 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
869  unsigned int n, unsigned int *free_space)
870 {
871  return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
872  r->prod.single, free_space);
873 }
874 
895 static __rte_always_inline unsigned
896 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
897  unsigned int n, unsigned int *available)
898 {
899  return __rte_ring_do_dequeue(r, obj_table, n,
900  RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
901 }
902 
920 static __rte_always_inline unsigned
921 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
922  unsigned int n, unsigned int *available)
923 {
924  return __rte_ring_do_dequeue(r, obj_table, n,
925  RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
926 }
927 
947 static __rte_always_inline unsigned
948 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
949  unsigned int n, unsigned int *available)
950 {
951  return __rte_ring_do_dequeue(r, obj_table, n,
952  RTE_RING_QUEUE_VARIABLE,
953  r->cons.single, available);
954 }
955 
956 #ifdef __cplusplus
957 }
958 #endif
959 
960 #endif /* _RTE_RING_H_ */
#define __rte_always_inline
Definition: rte_common.h:153
const struct rte_memzone * memzone
Definition: rte_ring.h:89
static __rte_always_inline unsigned int rte_ring_enqueue_bulk(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:468
char pad2 __rte_cache_aligned
Definition: rte_ring.h:103
int flags
Definition: rte_ring.h:88
char name[RTE_MEMZONE_NAMESIZE] __rte_cache_aligned
Definition: rte_ring.h:87
static __rte_always_inline int rte_ring_dequeue(struct rte_ring *r, void **obj_p)
Definition: rte_ring.h:666
static __rte_always_inline unsigned rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:896
static int rte_ring_empty(const struct rte_ring *r)
Definition: rte_ring.h:744
static __rte_always_inline unsigned rte_ring_mp_enqueue_burst(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:818
static __rte_always_inline int rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
Definition: rte_ring.h:626
void rte_ring_list_dump(FILE *f)
static __rte_always_inline unsigned int rte_ring_mp_enqueue_bulk(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:418
static __rte_always_inline int rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
Definition: rte_ring.h:507
static __rte_always_inline int rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
Definition: rte_ring.h:490
static unsigned int rte_ring_get_capacity(const struct rte_ring *r)
Definition: rte_ring.h:774
static unsigned int rte_ring_get_size(const struct rte_ring *r)
Definition: rte_ring.h:760
char pad0 __rte_cache_aligned
Definition: rte_ring.h:95
static __rte_always_inline int rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
Definition: rte_ring.h:644
uint32_t size
Definition: rte_ring.h:91
char pad1 __rte_cache_aligned
Definition: rte_ring.h:99
void rte_ring_free(struct rte_ring *r)
static __rte_always_inline unsigned rte_ring_sp_enqueue_burst(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:841
static __rte_always_inline unsigned rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:921
static __rte_always_inline unsigned rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:948
static __rte_always_inline unsigned int rte_ring_sp_enqueue_bulk(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:441
void rte_ring_dump(FILE *f, const struct rte_ring *r)
static unsigned rte_ring_count(const struct rte_ring *r)
Definition: rte_ring.h:697
uint32_t mask
Definition: rte_ring.h:92
struct rte_ring * rte_ring_create(const char *name, unsigned count, int socket_id, unsigned flags)
static __rte_always_inline unsigned int rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:603
static __rte_always_inline unsigned int rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:576
struct rte_ring * rte_ring_lookup(const char *name)
uint32_t capacity
Definition: rte_ring.h:93
static unsigned rte_ring_free_count(const struct rte_ring *r)
Definition: rte_ring.h:714
static __rte_always_inline int rte_ring_enqueue(struct rte_ring *r, void *obj)
Definition: rte_ring.h:528
static __rte_always_inline unsigned rte_ring_enqueue_burst(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
Definition: rte_ring.h:868
__rte_experimental void rte_ring_reset(struct rte_ring *r)
static __rte_always_inline unsigned int rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
Definition: rte_ring.h:552
int rte_ring_init(struct rte_ring *r, const char *name, unsigned count, unsigned flags)
static int rte_ring_full(const struct rte_ring *r)
Definition: rte_ring.h:729
ssize_t rte_ring_get_memsize(unsigned count)
#define RTE_MEMZONE_NAMESIZE
Definition: rte_memzone.h:51