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 (flags & RTE_STACK_F_LF) {
69 STACK_LOG_ERR("Lock-free stack is not supported on your platform\n");
74 sz = rte_stack_get_memsize(count, flags);
76 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
77 RTE_STACK_MZ_PREFIX, name);
78 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
79 rte_errno = ENAMETOOLONG;
83 te = rte_zmalloc("STACK_TAILQ_ENTRY", sizeof(*te), 0);
85 STACK_LOG_ERR("Cannot reserve memory for tailq\n");
90 rte_mcfg_tailq_write_lock();
92 mz = rte_memzone_reserve_aligned(mz_name, sz, socket_id,
95 STACK_LOG_ERR("Cannot reserve stack memzone!\n");
96 rte_mcfg_tailq_write_unlock();
103 rte_stack_init(s, count, flags);
105 /* Store the name for later lookups */
106 ret = strlcpy(s->name, name, sizeof(s->name));
107 if (ret < 0 || ret >= (int)sizeof(s->name)) {
108 rte_mcfg_tailq_write_unlock();
110 rte_errno = ENAMETOOLONG;
112 rte_memzone_free(mz);
122 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
124 TAILQ_INSERT_TAIL(stack_list, te, next);
126 rte_mcfg_tailq_write_unlock();
132 rte_stack_free(struct rte_stack *s)
134 struct rte_stack_list *stack_list;
135 struct rte_tailq_entry *te;
140 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
141 rte_mcfg_tailq_write_lock();
143 /* find out tailq entry */
144 TAILQ_FOREACH(te, stack_list, next) {
150 rte_mcfg_tailq_write_unlock();
154 TAILQ_REMOVE(stack_list, te, next);
156 rte_mcfg_tailq_write_unlock();
160 rte_memzone_free(s->memzone);
164 rte_stack_lookup(const char *name)
166 struct rte_stack_list *stack_list;
167 struct rte_tailq_entry *te;
168 struct rte_stack *r = NULL;
175 stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
177 rte_mcfg_tailq_read_lock();
179 TAILQ_FOREACH(te, stack_list, next) {
180 r = (struct rte_stack *) te->data;
181 if (strncmp(name, r->name, RTE_STACK_NAMESIZE) == 0)
185 rte_mcfg_tailq_read_unlock();
195 RTE_LOG_REGISTER(stack_logtype, lib.stack, NOTICE);