1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
7 #include <rte_string_fns.h>
8 #include <rte_atomic.h>
10 #include <rte_eal_memconfig.h>
11 #include <rte_errno.h>
12 #include <rte_malloc.h>
13 #include <rte_memzone.h>
14 #include <rte_rwlock.h>
15 #include <rte_tailq.h>
17 #include "rte_stack.h"
18 #include "stack_pvt.h"
20 TAILQ_HEAD(rte_stack_list, rte_tailq_entry);
22 static struct rte_tailq_elem rte_stack_tailq = {
23 .name = RTE_TAILQ_STACK_NAME,
25 EAL_REGISTER_TAILQ(rte_stack_tailq)
29 rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)
31 memset(s, 0, sizeof(*s));
33 if (flags & RTE_STACK_F_LF)
34 rte_stack_lf_init(s, count);
36 rte_stack_std_init(s);
40 rte_stack_get_memsize(unsigned int count, uint32_t flags)
42 if (flags & RTE_STACK_F_LF)
43 return rte_stack_lf_get_memsize(count);
45 return rte_stack_std_get_memsize(count);
49 rte_stack_create(const char *name, unsigned int count, int socket_id,
52 char mz_name[RTE_MEMZONE_NAMESIZE];
53 struct rte_stack_list *stack_list;
54 const struct rte_memzone *mz;
55 struct rte_tailq_entry *te;
60 if (flags & ~(RTE_STACK_F_LF)) {
61 STACK_LOG_ERR("Unsupported stack flags %#x\n", flags);
66 RTE_BUILD_BUG_ON(sizeof(struct rte_stack_lf_head) != 16);
68 #if !defined(RTE_STACK_LF_SUPPORTED)
69 if (flags & RTE_STACK_F_LF) {
70 STACK_LOG_ERR("Lock-free stack is not supported on your platform\n");
76 sz = rte_stack_get_memsize(count, flags);
78 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
79 RTE_STACK_MZ_PREFIX, name);
80 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
81 rte_errno = ENAMETOOLONG;
85 te = rte_zmalloc("STACK_TAILQ_ENTRY", sizeof(*te), 0);
87 STACK_LOG_ERR("Cannot reserve memory for tailq\n");
92 rte_mcfg_tailq_write_lock();
94 mz = rte_memzone_reserve_aligned(mz_name, sz, socket_id,
97 STACK_LOG_ERR("Cannot reserve stack memzone!\n");
98 rte_mcfg_tailq_write_unlock();
105 rte_stack_init(s, count, flags);
107 /* Store the name for later lookups */
108 ret = strlcpy(s->name, name, sizeof(s->name));
109 if (ret < 0 || ret >= (int)sizeof(s->name)) {
110 rte_mcfg_tailq_write_unlock();
112 rte_errno = ENAMETOOLONG;
114 rte_memzone_free(mz);
124 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
126 TAILQ_INSERT_TAIL(stack_list, te, next);
128 rte_mcfg_tailq_write_unlock();
134 rte_stack_free(struct rte_stack *s)
136 struct rte_stack_list *stack_list;
137 struct rte_tailq_entry *te;
142 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
143 rte_mcfg_tailq_write_lock();
145 /* find out tailq entry */
146 TAILQ_FOREACH(te, stack_list, next) {
152 rte_mcfg_tailq_write_unlock();
156 TAILQ_REMOVE(stack_list, te, next);
158 rte_mcfg_tailq_write_unlock();
162 rte_memzone_free(s->memzone);
166 rte_stack_lookup(const char *name)
168 struct rte_stack_list *stack_list;
169 struct rte_tailq_entry *te;
170 struct rte_stack *r = NULL;
177 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
179 rte_mcfg_tailq_read_lock();
181 TAILQ_FOREACH(te, stack_list, next) {
182 r = (struct rte_stack *) te->data;
183 if (strncmp(name, r->name, RTE_STACK_NAMESIZE) == 0)
187 rte_mcfg_tailq_read_unlock();
197 RTE_LOG_REGISTER_DEFAULT(stack_logtype, NOTICE);