43ce1a29d471e994605a1a3dbb5ee941cc12af60
[dpdk.git] / lib / ring / rte_ring_core.h
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_CORE_H_
11 #define _RTE_RING_CORE_H_
12
13 /**
14  * @file
15  * This file contains definion of RTE ring structure itself,
16  * init flags and some related macros.
17  * For majority of DPDK entities, it is not recommended to include
18  * this file directly, use include <rte_ring.h> or <rte_ring_elem.h>
19  * instead.
20  */
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <rte_common.h>
31 #include <rte_config.h>
32 #include <rte_memory.h>
33 #include <rte_lcore.h>
34 #include <rte_atomic.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_memzone.h>
37 #include <rte_pause.h>
38 #include <rte_debug.h>
39
40 #define RTE_TAILQ_RING_NAME "RTE_RING"
41
42 /** enqueue/dequeue behavior types */
43 enum rte_ring_queue_behavior {
44         /** Enq/Deq a fixed number of items from a ring */
45         RTE_RING_QUEUE_FIXED = 0,
46         /** Enq/Deq as many items as possible from ring */
47         RTE_RING_QUEUE_VARIABLE
48 };
49
50 #define RTE_RING_MZ_PREFIX "RG_"
51 /** The maximum length of a ring name. */
52 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
53                            sizeof(RTE_RING_MZ_PREFIX) + 1)
54
55 /** prod/cons sync types */
56 enum rte_ring_sync_type {
57         RTE_RING_SYNC_MT,     /**< multi-thread safe (default mode) */
58         RTE_RING_SYNC_ST,     /**< single thread only */
59 #ifdef ALLOW_EXPERIMENTAL_API
60         RTE_RING_SYNC_MT_RTS, /**< multi-thread relaxed tail sync */
61         RTE_RING_SYNC_MT_HTS, /**< multi-thread head/tail sync */
62 #endif
63 };
64
65 /**
66  * structures to hold a pair of head/tail values and other metadata.
67  * Depending on sync_type format of that structure might be different,
68  * but offset for *sync_type* and *tail* values should remain the same.
69  */
70 struct rte_ring_headtail {
71         volatile uint32_t head;      /**< prod/consumer head. */
72         volatile uint32_t tail;      /**< prod/consumer tail. */
73         RTE_STD_C11
74         union {
75                 /** sync type of prod/cons */
76                 enum rte_ring_sync_type sync_type;
77                 /** deprecated -  True if single prod/cons */
78                 uint32_t single;
79         };
80 };
81
82 union __rte_ring_rts_poscnt {
83         /** raw 8B value to read/write *cnt* and *pos* as one atomic op */
84         uint64_t raw __rte_aligned(8);
85         struct {
86                 uint32_t cnt; /**< head/tail reference counter */
87                 uint32_t pos; /**< head/tail position */
88         } val;
89 };
90
91 struct rte_ring_rts_headtail {
92         volatile union __rte_ring_rts_poscnt tail;
93         enum rte_ring_sync_type sync_type;  /**< sync type of prod/cons */
94         uint32_t htd_max;   /**< max allowed distance between head/tail */
95         volatile union __rte_ring_rts_poscnt head;
96 };
97
98 union __rte_ring_hts_pos {
99         /** raw 8B value to read/write *head* and *tail* as one atomic op */
100         uint64_t raw __rte_aligned(8);
101         struct {
102                 uint32_t head; /**< head position */
103                 uint32_t tail; /**< tail position */
104         } pos;
105 };
106
107 struct rte_ring_hts_headtail {
108         volatile union __rte_ring_hts_pos ht;
109         enum rte_ring_sync_type sync_type;  /**< sync type of prod/cons */
110 };
111
112 /**
113  * An RTE ring structure.
114  *
115  * The producer and the consumer have a head and a tail index. The particularity
116  * of these index is that they are not between 0 and size(ring). These indexes
117  * are between 0 and 2^32, and we mask their value when we access the ring[]
118  * field. Thanks to this assumption, we can do subtractions between 2 index
119  * values in a modulo-32bit base: that's why the overflow of the indexes is not
120  * a problem.
121  */
122 struct rte_ring {
123         /*
124          * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
125          * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
126          * next time the ABI changes
127          */
128         char name[RTE_MEMZONE_NAMESIZE] __rte_cache_aligned;
129         /**< Name of the ring. */
130         int flags;               /**< Flags supplied at creation. */
131         const struct rte_memzone *memzone;
132                         /**< Memzone, if any, containing the rte_ring */
133         uint32_t size;           /**< Size of ring. */
134         uint32_t mask;           /**< Mask (size-1) of ring. */
135         uint32_t capacity;       /**< Usable size of ring */
136
137         char pad0 __rte_cache_aligned; /**< empty cache line */
138
139         /** Ring producer status. */
140         RTE_STD_C11
141         union {
142                 struct rte_ring_headtail prod;
143                 struct rte_ring_hts_headtail hts_prod;
144                 struct rte_ring_rts_headtail rts_prod;
145         }  __rte_cache_aligned;
146
147         char pad1 __rte_cache_aligned; /**< empty cache line */
148
149         /** Ring consumer status. */
150         RTE_STD_C11
151         union {
152                 struct rte_ring_headtail cons;
153                 struct rte_ring_hts_headtail hts_cons;
154                 struct rte_ring_rts_headtail rts_cons;
155         }  __rte_cache_aligned;
156
157         char pad2 __rte_cache_aligned; /**< empty cache line */
158 };
159
160 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
161 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
162 /**
163  * Ring is to hold exactly requested number of entries.
164  * Without this flag set, the ring size requested must be a power of 2, and the
165  * usable space will be that size - 1. With the flag, the requested size will
166  * be rounded up to the next power of two, but the usable space will be exactly
167  * that requested. Worst case, if a power-of-2 size is requested, half the
168  * ring space will be wasted.
169  */
170 #define RING_F_EXACT_SZ 0x0004
171 #define RTE_RING_SZ_MASK  (0x7fffffffU) /**< Ring size mask */
172
173 #define RING_F_MP_RTS_ENQ 0x0008 /**< The default enqueue is "MP RTS". */
174 #define RING_F_MC_RTS_DEQ 0x0010 /**< The default dequeue is "MC RTS". */
175
176 #define RING_F_MP_HTS_ENQ 0x0020 /**< The default enqueue is "MP HTS". */
177 #define RING_F_MC_HTS_DEQ 0x0040 /**< The default dequeue is "MC HTS". */
178
179 #ifdef __cplusplus
180 }
181 #endif
182
183 #endif /* _RTE_RING_CORE_H_ */