1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
10 * librte_stack provides an API for configuration and use of a bounded stack of
11 * pointers. Push and pop operations are MT-safe, allowing concurrent access,
12 * and the interface supports pushing and popping multiple pointers at a time.
22 #include <rte_debug.h>
23 #include <rte_errno.h>
24 #include <rte_memzone.h>
25 #include <rte_spinlock.h>
27 #define RTE_TAILQ_STACK_NAME "RTE_STACK"
28 #define RTE_STACK_MZ_PREFIX "STK_"
29 /** The maximum length of a stack name. */
30 #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
31 sizeof(RTE_STACK_MZ_PREFIX) + 1)
33 struct rte_stack_lf_elem {
34 void *data; /**< Data pointer */
35 struct rte_stack_lf_elem *next; /**< Next pointer */
38 struct rte_stack_lf_head {
39 struct rte_stack_lf_elem *top; /**< Stack top */
40 uint64_t cnt; /**< Modification counter for avoiding ABA problem */
43 struct rte_stack_lf_list {
45 struct rte_stack_lf_head head __rte_aligned(16);
50 /* Structure containing two lock-free LIFO lists: the stack itself and a list
51 * of free linked-list elements.
54 /** LIFO list of elements */
55 struct rte_stack_lf_list used __rte_cache_aligned;
56 /** LIFO list of free elements */
57 struct rte_stack_lf_list free __rte_cache_aligned;
59 struct rte_stack_lf_elem elems[] __rte_cache_aligned;
62 /* Structure containing the LIFO, its current length, and a lock for mutual
65 struct rte_stack_std {
66 rte_spinlock_t lock; /**< LIFO lock */
67 uint32_t len; /**< LIFO len */
68 void *objs[]; /**< LIFO pointer table */
71 /* The RTE stack structure contains the LIFO structure itself, plus metadata
72 * such as its name and memzone pointer.
75 /** Name of the stack. */
76 char name[RTE_STACK_NAMESIZE] __rte_cache_aligned;
77 /** Memzone containing the rte_stack structure. */
78 const struct rte_memzone *memzone;
79 uint32_t capacity; /**< Usable size of the stack. */
80 uint32_t flags; /**< Flags supplied at creation. */
83 struct rte_stack_lf stack_lf; /**< Lock-free LIFO structure. */
84 struct rte_stack_std stack_std; /**< LIFO structure. */
86 } __rte_cache_aligned;
89 * The stack uses lock-free push and pop functions. This flag is only
90 * supported on x86_64 or arm64 platforms, currently.
92 #define RTE_STACK_F_LF 0x0001
94 #include "rte_stack_std.h"
95 #include "rte_stack_lf.h"
98 * Push several objects on the stack (MT-safe).
101 * A pointer to the stack structure.
103 * A pointer to a table of void * pointers (objects).
105 * The number of objects to push on the stack from the obj_table.
107 * Actual number of objects pushed (either 0 or *n*).
109 static __rte_always_inline unsigned int
110 rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
112 RTE_ASSERT(s != NULL);
113 RTE_ASSERT(obj_table != NULL);
115 if (s->flags & RTE_STACK_F_LF)
116 return __rte_stack_lf_push(s, obj_table, n);
118 return __rte_stack_std_push(s, obj_table, n);
122 * Pop several objects from the stack (MT-safe).
125 * A pointer to the stack structure.
127 * A pointer to a table of void * pointers (objects).
129 * The number of objects to pull from the stack.
131 * Actual number of objects popped (either 0 or *n*).
133 static __rte_always_inline unsigned int
134 rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
136 RTE_ASSERT(s != NULL);
137 RTE_ASSERT(obj_table != NULL);
139 if (s->flags & RTE_STACK_F_LF)
140 return __rte_stack_lf_pop(s, obj_table, n);
142 return __rte_stack_std_pop(s, obj_table, n);
146 * Return the number of used entries in a stack.
149 * A pointer to the stack structure.
151 * The number of used entries in the stack.
153 static __rte_always_inline unsigned int
154 rte_stack_count(struct rte_stack *s)
156 RTE_ASSERT(s != NULL);
158 if (s->flags & RTE_STACK_F_LF)
159 return __rte_stack_lf_count(s);
161 return __rte_stack_std_count(s);
165 * Return the number of free entries in a stack.
168 * A pointer to the stack structure.
170 * The number of free entries in the stack.
172 static __rte_always_inline unsigned int
173 rte_stack_free_count(struct rte_stack *s)
175 RTE_ASSERT(s != NULL);
177 return s->capacity - rte_stack_count(s);
181 * Create a new stack named *name* in memory.
183 * This function uses ``memzone_reserve()`` to allocate memory for a stack of
184 * size *count*. The behavior of the stack is controlled by the *flags*.
187 * The name of the stack.
189 * The size of the stack.
191 * The *socket_id* argument is the socket identifier in case of
192 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
193 * constraint for the reserved zone.
195 * An OR of the following:
196 * - RTE_STACK_F_LF: If this flag is set, the stack uses lock-free
197 * variants of the push and pop functions. Otherwise, it achieves
198 * thread-safety using a lock.
200 * On success, the pointer to the new allocated stack. NULL on error with
201 * rte_errno set appropriately. Possible errno values include:
202 * - ENOSPC - the maximum number of memzones has already been allocated
203 * - EEXIST - a stack with the same name already exists
204 * - ENOMEM - insufficient memory to create the stack
205 * - ENAMETOOLONG - name size exceeds RTE_STACK_NAMESIZE
206 * - ENOTSUP - platform does not support given flags combination.
209 rte_stack_create(const char *name, unsigned int count, int socket_id,
213 * Free all memory used by the stack.
219 rte_stack_free(struct rte_stack *s);
222 * Lookup a stack by its name.
225 * The name of the stack.
227 * The pointer to the stack matching the name, or NULL if not found,
228 * with rte_errno set appropriately. Possible rte_errno values include:
229 * - ENOENT - Stack with name *name* not found.
230 * - EINVAL - *name* pointer is NULL.
233 rte_stack_lookup(const char *name);
239 #endif /* _RTE_STACK_H_ */