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 "rte_stack_pvt.h"
22 TAILQ_HEAD(rte_stack_list
, rte_tailq_entry
);
24 static struct rte_tailq_elem rte_stack_tailq
= {
25 .name
= RTE_TAILQ_STACK_NAME
,
27 EAL_REGISTER_TAILQ(rte_stack_tailq
)
31 rte_stack_init(struct rte_stack
*s
, unsigned int count
, uint32_t flags
)
33 memset(s
, 0, sizeof(*s
));
35 if (flags
& RTE_STACK_F_LF
)
36 rte_stack_lf_init(s
, count
);
38 rte_stack_std_init(s
);
42 rte_stack_get_memsize(unsigned int count
, uint32_t flags
)
44 if (flags
& RTE_STACK_F_LF
)
45 return rte_stack_lf_get_memsize(count
);
47 return rte_stack_std_get_memsize(count
);
51 rte_stack_create(const char *name
, unsigned int count
, int socket_id
,
54 char mz_name
[RTE_MEMZONE_NAMESIZE
];
55 struct rte_stack_list
*stack_list
;
56 const struct rte_memzone
*mz
;
57 struct rte_tailq_entry
*te
;
63 RTE_BUILD_BUG_ON(sizeof(struct rte_stack_lf_head
) != 16);
65 if (flags
& RTE_STACK_F_LF
) {
66 STACK_LOG_ERR("Lock-free stack is not supported on your platform\n");
71 sz
= rte_stack_get_memsize(count
, flags
);
73 ret
= snprintf(mz_name
, sizeof(mz_name
), "%s%s",
74 RTE_STACK_MZ_PREFIX
, name
);
75 if (ret
< 0 || ret
>= (int)sizeof(mz_name
)) {
76 rte_errno
= ENAMETOOLONG
;
80 te
= rte_zmalloc("STACK_TAILQ_ENTRY", sizeof(*te
), 0);
82 STACK_LOG_ERR("Cannot reserve memory for tailq\n");
87 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
89 mz
= rte_memzone_reserve_aligned(mz_name
, sz
, socket_id
,
92 STACK_LOG_ERR("Cannot reserve stack memzone!\n");
93 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
100 rte_stack_init(s
, count
, flags
);
102 /* Store the name for later lookups */
103 ret
= strlcpy(s
->name
, name
, sizeof(s
->name
));
104 if (ret
< 0 || ret
>= (int)sizeof(s
->name
)) {
105 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
107 rte_errno
= ENAMETOOLONG
;
109 rte_memzone_free(mz
);
119 stack_list
= RTE_TAILQ_CAST(rte_stack_tailq
.head
, rte_stack_list
);
121 TAILQ_INSERT_TAIL(stack_list
, te
, next
);
123 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
129 rte_stack_free(struct rte_stack
*s
)
131 struct rte_stack_list
*stack_list
;
132 struct rte_tailq_entry
*te
;
137 stack_list
= RTE_TAILQ_CAST(rte_stack_tailq
.head
, rte_stack_list
);
138 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK
);
140 /* find out tailq entry */
141 TAILQ_FOREACH(te
, stack_list
, next
) {
147 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
151 TAILQ_REMOVE(stack_list
, te
, next
);
153 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK
);
157 rte_memzone_free(s
->memzone
);
161 rte_stack_lookup(const char *name
)
163 struct rte_stack_list
*stack_list
;
164 struct rte_tailq_entry
*te
;
165 struct rte_stack
*r
= NULL
;
172 stack_list
= RTE_TAILQ_CAST(rte_stack_tailq
.head
, rte_stack_list
);
174 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK
);
176 TAILQ_FOREACH(te
, stack_list
, next
) {
177 r
= (struct rte_stack
*) te
->data
;
178 if (strncmp(name
, r
->name
, RTE_STACK_NAMESIZE
) == 0)
182 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK
);
192 RTE_INIT(librte_stack_init_log
)
194 stack_logtype
= rte_log_register("lib.stack");
195 if (stack_logtype
>= 0)
196 rte_log_set_level(stack_logtype
, RTE_LOG_NOTICE
);