Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019 Intel Corporation
3 : : * Copyright(c) 2026 SmartShare Systems
4 : : */
5 : :
6 : : /**
7 : : * @file rte_stack.h
8 : : *
9 : : * RTE Stack.
10 : : *
11 : : * librte_stack provides an API for configuration and use of a bounded stack of
12 : : * pointers. Push and pop operations are MT-safe, allowing concurrent access,
13 : : * and the interface supports pushing and popping multiple pointers at a time.
14 : : */
15 : :
16 : : #ifndef _RTE_STACK_H_
17 : : #define _RTE_STACK_H_
18 : :
19 : : #include <stdalign.h>
20 : :
21 : : #include <rte_debug.h>
22 : : #include <rte_errno.h>
23 : : #include <rte_memzone.h>
24 : : #include <rte_spinlock.h>
25 : :
26 : : #define RTE_TAILQ_STACK_NAME "RTE_STACK"
27 : : #define RTE_STACK_MZ_PREFIX "STK_"
28 : : /** The maximum length of a stack name. */
29 : : #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
30 : : sizeof(RTE_STACK_MZ_PREFIX) + 1)
31 : :
32 : : static_assert(((sizeof(void *) * RTE_STACK_PILE_BULK_SIZE) & RTE_CACHE_LINE_MASK) == 0,
33 : : "Pile bulk size must be divisible by CPU cache line size");
34 : : static_assert(RTE_IS_POWER_OF_2(RTE_STACK_PILE_BULK_SIZE),
35 : : "Pile bulk size must be power of 2");
36 : :
37 : : /* Note: Also used as solo (single-object) pile element. */
38 : : struct rte_stack_lf_elem {
39 : : void *data; /**< Data pointer */
40 : : struct rte_stack_lf_elem *next; /**< Next pointer */
41 : : };
42 : :
43 : : /*
44 : : * Bulk (multi-object) pile element.
45 : : * Inherited from the rte_stack_lf_elem (single-object) class,
46 : : * and extended with an array for holding a bulk of object pointers.
47 : : */
48 : : struct rte_stack_pile_bulk_elem {
49 : : /* The first part must be ABI compatible with the rte_stack_lf_elem parent class. */
50 : : struct rte_stack_lf_elem elem;
51 : : /* The second part differs. */
52 : : alignas(RTE_CACHE_LINE_SIZE)
53 : : void *objs[RTE_STACK_PILE_BULK_SIZE]; /**< Bulk (multi-object) pointers */
54 : : };
55 : :
56 : : static_assert(offsetof(struct rte_stack_pile_bulk_elem, elem) == 0,
57 : : "Inherited type mismatch");
58 : :
59 : : struct __rte_aligned(16) rte_stack_lf_head {
60 : : struct rte_stack_lf_elem *top; /**< Stack top */
61 : : uint64_t cnt; /**< Modification counter for avoiding ABA problem */
62 : : };
63 : :
64 : : struct rte_stack_lf_list {
65 : : /** List head */
66 : : struct rte_stack_lf_head head;
67 : : /** List len */
68 : : RTE_ATOMIC(uint64_t) len;
69 : : };
70 : :
71 : : /* Structure containing two lock-free LIFO lists: the stack itself and a list
72 : : * of free linked-list elements.
73 : : */
74 : : struct rte_stack_lf {
75 : : /** LIFO list of elements */
76 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list used;
77 : : RTE_CACHE_GUARD;
78 : : /** LIFO list of free elements */
79 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list free;
80 : : RTE_CACHE_GUARD;
81 : : /** LIFO elements */
82 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_elem elems[];
83 : : };
84 : :
85 : : /* Pile structure containing three lock-free LIFO-like lists:
86 : : * - A list of elements, each element holding a bulk of pointers to objects.
87 : : * - A list of elements, each element holding one pointer to an object.
88 : : * - A list of free linked-list elements.
89 : : */
90 : : struct rte_stack_pile {
91 : : /** LIFO list of bulk (multi-object) elements */
92 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list bulk;
93 : : RTE_CACHE_GUARD;
94 : : /** LIFO list of solo (single-object) elements */
95 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list solo;
96 : : RTE_CACHE_GUARD;
97 : : /** LIFO list of free bulk elements */
98 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list free_bulk;
99 : : RTE_CACHE_GUARD;
100 : : /** LIFO list of free solo elements */
101 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list free_solo;
102 : : RTE_CACHE_GUARD;
103 : : /** LIFO elements follow, first bulk, then solo */
104 : : alignas(RTE_CACHE_LINE_SIZE) void *elems[];
105 : : };
106 : :
107 : : /* Structure containing the LIFO, its current length, and a lock for mutual
108 : : * exclusion.
109 : : */
110 : : struct rte_stack_std {
111 : : rte_spinlock_t lock; /**< LIFO lock */
112 : : uint32_t len; /**< LIFO len */
113 : : void *objs[]; /**< LIFO pointer table */
114 : : };
115 : :
116 : : /* The RTE stack structure contains the LIFO structure itself, plus metadata
117 : : * such as its name and memzone pointer.
118 : : */
119 : : struct __rte_cache_aligned rte_stack {
120 : : /** Name of the stack. */
121 : : alignas(RTE_CACHE_LINE_SIZE) char name[RTE_STACK_NAMESIZE];
122 : : /** Memzone containing the rte_stack structure. */
123 : : const struct rte_memzone *memzone;
124 : : uint32_t capacity; /**< Usable size of the stack. */
125 : : uint32_t flags; /**< Flags supplied at creation. */
126 : : union {
127 : : struct rte_stack_lf stack_lf; /**< Lock-free LIFO structure. */
128 : : struct rte_stack_pile stack_pile; /**< Lock-free pile (LIFO-like) structure. */
129 : : struct rte_stack_std stack_std; /**< LIFO structure. */
130 : : };
131 : : };
132 : :
133 : : /**
134 : : * The stack uses lock-free push and pop functions. This flag is only
135 : : * supported on x86_64 or arm64 platforms, currently.
136 : : */
137 : : #define RTE_STACK_F_LF 0x0001
138 : :
139 : : /**
140 : : * The stack-like pile uses lock-free push and pop functions.
141 : : * It is optimized for bulks of objects, and is not strictly LIFO, and
142 : : * is not strictly bounded by its size, but might hold more objects.
143 : : * This flag is only supported on x86_64 or arm64 platforms, currently.
144 : : *
145 : : * @warning
146 : : * @b EXPERIMENTAL: this API may change, or be removed, without prior notice.
147 : : */
148 : : #define RTE_STACK_F_PILE 0x0002
149 : :
150 : : #include "rte_stack_std.h"
151 : : #include "rte_stack_lf.h"
152 : : #include "rte_stack_pile.h"
153 : :
154 : : #ifdef __cplusplus
155 : : extern "C" {
156 : : #endif
157 : :
158 : : /**
159 : : * Push several objects on the stack (MT-safe).
160 : : *
161 : : * @param s
162 : : * A pointer to the stack structure.
163 : : * @param obj_table
164 : : * A pointer to a table of void * pointers (objects).
165 : : * @param n
166 : : * The number of objects to push on the stack from the obj_table.
167 : : * @return
168 : : * Actual number of objects pushed (either 0 or *n*).
169 : : */
170 : : static __rte_always_inline unsigned int
171 : : rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
172 : : {
173 : : RTE_ASSERT(s != NULL);
174 : : RTE_ASSERT(obj_table != NULL);
175 : :
176 [ + + + + : 495374 : if (s->flags & RTE_STACK_F_LF)
+ + - + -
+ - + +
+ ]
177 : : return __rte_stack_lf_push(s, obj_table, n);
178 [ + + + + : 329709 : else if (s->flags & RTE_STACK_F_PILE)
+ + + - +
- + - +
+ ]
179 : : return __rte_stack_pile_push(s, obj_table, n);
180 : : else
181 : : return __rte_stack_std_push(s, obj_table, n);
182 : : }
183 : :
184 : : /**
185 : : * Pop several objects from the stack (MT-safe).
186 : : *
187 : : * @param s
188 : : * A pointer to the stack structure.
189 : : * @param obj_table
190 : : * A pointer to a table of void * pointers (objects).
191 : : * @param n
192 : : * The number of objects to pull from the stack.
193 : : * @return
194 : : * Actual number of objects popped (either 0 or *n*).
195 : : */
196 : : static __rte_always_inline unsigned int
197 : : rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
198 : : {
199 : : RTE_ASSERT(s != NULL);
200 : : RTE_ASSERT(obj_table != NULL);
201 : :
202 [ + + + + : 496216 : if (s->flags & RTE_STACK_F_LF)
+ + - + -
+ - + +
+ ]
203 : : return __rte_stack_lf_pop(s, obj_table, n);
204 [ + + + + : 330557 : else if (s->flags & RTE_STACK_F_PILE)
+ + + - +
- + - +
+ ]
205 : : return __rte_stack_pile_pop(s, obj_table, n);
206 : : else
207 : : return __rte_stack_std_pop(s, obj_table, n);
208 : : }
209 : :
210 : : /**
211 : : * Return the number of used entries in a stack.
212 : : *
213 : : * @param s
214 : : * A pointer to the stack structure.
215 : : * @return
216 : : * The number of used entries in the stack.
217 : : */
218 : : static __rte_always_inline unsigned int
219 : : rte_stack_count(struct rte_stack *s)
220 : : {
221 : : RTE_ASSERT(s != NULL);
222 : :
223 [ + + - - : 394001 : if (s->flags & RTE_STACK_F_LF)
- - - + -
- - - - +
- - - - -
+ - - - -
- + - - -
- - + - -
- - - + -
- - - + +
- - - - +
+ - - -
- ]
224 : 262658 : return __rte_stack_lf_count(s);
225 [ + + - - : 525336 : else if (s->flags & RTE_STACK_F_PILE)
+ + - - +
- - - + -
- - + - -
- + - - -
+ - - - +
- - - + -
- - + - -
- + - - -
+ - - - +
- - - + -
- - + + -
- + + - -
+ + - - +
+ - - ]
226 : 262670 : return __rte_stack_pile_count(s);
227 : : else
228 : 262666 : return __rte_stack_std_count(s);
229 : : }
230 : :
231 : : /**
232 : : * Return the number of free entries in a stack.
233 : : *
234 : : * @param s
235 : : * A pointer to the stack structure.
236 : : * @return
237 : : * The number of free entries in the stack.
238 : : */
239 : : static __rte_always_inline unsigned int
240 : : rte_stack_free_count(struct rte_stack *s)
241 : : {
242 : : RTE_ASSERT(s != NULL);
243 : :
244 [ + + - + : 787986 : return s->capacity - rte_stack_count(s);
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + +
+ - + + +
- + ]
245 : : }
246 : :
247 : : /**
248 : : * Create a new stack named *name* in memory.
249 : : *
250 : : * This function uses ``memzone_reserve()`` to allocate memory for a stack of
251 : : * size *count*. The behavior of the stack is controlled by the *flags*.
252 : : *
253 : : * @param name
254 : : * The name of the stack.
255 : : * @param count
256 : : * The size of the stack.
257 : : * @param socket_id
258 : : * The *socket_id* argument is the socket identifier in case of
259 : : * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
260 : : * constraint for the reserved zone.
261 : : * @param flags
262 : : * An OR of the following:
263 : : * - RTE_STACK_F_LF: If this flag is set, the stack uses lock-free
264 : : * variants of the push and pop functions. Otherwise, it achieves
265 : : * thread-safety using a lock.
266 : : * @return
267 : : * On success, the pointer to the new allocated stack. NULL on error with
268 : : * rte_errno set appropriately. Possible errno values include:
269 : : * - ENOSPC - the maximum number of memzones has already been allocated
270 : : * - EEXIST - a stack with the same name already exists
271 : : * - ENOMEM - insufficient memory to create the stack
272 : : * - ENAMETOOLONG - name size exceeds RTE_STACK_NAMESIZE
273 : : * - ENOTSUP - platform does not support given flags combination.
274 : : */
275 : : struct rte_stack *
276 : : rte_stack_create(const char *name, unsigned int count, int socket_id,
277 : : uint32_t flags);
278 : :
279 : : /**
280 : : * Free all memory used by the stack.
281 : : *
282 : : * @param s
283 : : * Pointer to stack created with rte_stack_create().
284 : : * If s is NULL, no operation is performed.
285 : : */
286 : : void
287 : : rte_stack_free(struct rte_stack *s);
288 : :
289 : : /**
290 : : * Lookup a stack by its name.
291 : : *
292 : : * @param name
293 : : * The name of the stack.
294 : : * @return
295 : : * The pointer to the stack matching the name, or NULL if not found,
296 : : * with rte_errno set appropriately. Possible rte_errno values include:
297 : : * - ENOENT - Stack with name *name* not found.
298 : : * - EINVAL - *name* pointer is NULL.
299 : : */
300 : : struct rte_stack *
301 : : rte_stack_lookup(const char *name);
302 : :
303 : : #ifdef __cplusplus
304 : : }
305 : : #endif
306 : :
307 : : #endif /* _RTE_STACK_H_ */
|