199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2019 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #include <string.h>
6*09e4eceaSJie Zhou #include <sys/queue.h>
799a2dd95SBruce Richardson
899a2dd95SBruce Richardson #include <rte_string_fns.h>
999a2dd95SBruce Richardson #include <rte_eal_memconfig.h>
1099a2dd95SBruce Richardson #include <rte_errno.h>
1199a2dd95SBruce Richardson #include <rte_malloc.h>
1299a2dd95SBruce Richardson #include <rte_memzone.h>
1399a2dd95SBruce Richardson #include <rte_tailq.h>
1499a2dd95SBruce Richardson
1599a2dd95SBruce Richardson #include "rte_stack.h"
1699a2dd95SBruce Richardson #include "stack_pvt.h"
1799a2dd95SBruce Richardson
1899a2dd95SBruce Richardson TAILQ_HEAD(rte_stack_list, rte_tailq_entry);
1999a2dd95SBruce Richardson
2099a2dd95SBruce Richardson static struct rte_tailq_elem rte_stack_tailq = {
2199a2dd95SBruce Richardson .name = RTE_TAILQ_STACK_NAME,
2299a2dd95SBruce Richardson };
EAL_REGISTER_TAILQ(rte_stack_tailq)2399a2dd95SBruce Richardson EAL_REGISTER_TAILQ(rte_stack_tailq)
2499a2dd95SBruce Richardson
2599a2dd95SBruce Richardson
2699a2dd95SBruce Richardson static void
2799a2dd95SBruce Richardson rte_stack_init(struct rte_stack *s, unsigned int count, uint32_t flags)
2899a2dd95SBruce Richardson {
2999a2dd95SBruce Richardson memset(s, 0, sizeof(*s));
3099a2dd95SBruce Richardson
3199a2dd95SBruce Richardson if (flags & RTE_STACK_F_LF)
3299a2dd95SBruce Richardson rte_stack_lf_init(s, count);
3399a2dd95SBruce Richardson else
3499a2dd95SBruce Richardson rte_stack_std_init(s);
3599a2dd95SBruce Richardson }
3699a2dd95SBruce Richardson
3799a2dd95SBruce Richardson static ssize_t
rte_stack_get_memsize(unsigned int count,uint32_t flags)3899a2dd95SBruce Richardson rte_stack_get_memsize(unsigned int count, uint32_t flags)
3999a2dd95SBruce Richardson {
4099a2dd95SBruce Richardson if (flags & RTE_STACK_F_LF)
4199a2dd95SBruce Richardson return rte_stack_lf_get_memsize(count);
4299a2dd95SBruce Richardson else
4399a2dd95SBruce Richardson return rte_stack_std_get_memsize(count);
4499a2dd95SBruce Richardson }
4599a2dd95SBruce Richardson
4699a2dd95SBruce Richardson struct rte_stack *
rte_stack_create(const char * name,unsigned int count,int socket_id,uint32_t flags)4799a2dd95SBruce Richardson rte_stack_create(const char *name, unsigned int count, int socket_id,
4899a2dd95SBruce Richardson uint32_t flags)
4999a2dd95SBruce Richardson {
5099a2dd95SBruce Richardson char mz_name[RTE_MEMZONE_NAMESIZE];
5199a2dd95SBruce Richardson struct rte_stack_list *stack_list;
5299a2dd95SBruce Richardson const struct rte_memzone *mz;
5399a2dd95SBruce Richardson struct rte_tailq_entry *te;
5499a2dd95SBruce Richardson struct rte_stack *s;
5599a2dd95SBruce Richardson unsigned int sz;
5699a2dd95SBruce Richardson int ret;
5799a2dd95SBruce Richardson
5899a2dd95SBruce Richardson if (flags & ~(RTE_STACK_F_LF)) {
5999a2dd95SBruce Richardson STACK_LOG_ERR("Unsupported stack flags %#x\n", flags);
6099a2dd95SBruce Richardson return NULL;
6199a2dd95SBruce Richardson }
6299a2dd95SBruce Richardson
6399a2dd95SBruce Richardson #ifdef RTE_ARCH_64
6499a2dd95SBruce Richardson RTE_BUILD_BUG_ON(sizeof(struct rte_stack_lf_head) != 16);
651abb185dSStanislaw Kardach #endif
661abb185dSStanislaw Kardach #if !defined(RTE_STACK_LF_SUPPORTED)
6799a2dd95SBruce Richardson if (flags & RTE_STACK_F_LF) {
6899a2dd95SBruce Richardson STACK_LOG_ERR("Lock-free stack is not supported on your platform\n");
691abb185dSStanislaw Kardach rte_errno = ENOTSUP;
7099a2dd95SBruce Richardson return NULL;
7199a2dd95SBruce Richardson }
7299a2dd95SBruce Richardson #endif
7399a2dd95SBruce Richardson
7499a2dd95SBruce Richardson sz = rte_stack_get_memsize(count, flags);
7599a2dd95SBruce Richardson
7699a2dd95SBruce Richardson ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
7799a2dd95SBruce Richardson RTE_STACK_MZ_PREFIX, name);
7899a2dd95SBruce Richardson if (ret < 0 || ret >= (int)sizeof(mz_name)) {
7999a2dd95SBruce Richardson rte_errno = ENAMETOOLONG;
8099a2dd95SBruce Richardson return NULL;
8199a2dd95SBruce Richardson }
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson te = rte_zmalloc("STACK_TAILQ_ENTRY", sizeof(*te), 0);
8499a2dd95SBruce Richardson if (te == NULL) {
8599a2dd95SBruce Richardson STACK_LOG_ERR("Cannot reserve memory for tailq\n");
8699a2dd95SBruce Richardson rte_errno = ENOMEM;
8799a2dd95SBruce Richardson return NULL;
8899a2dd95SBruce Richardson }
8999a2dd95SBruce Richardson
9099a2dd95SBruce Richardson rte_mcfg_tailq_write_lock();
9199a2dd95SBruce Richardson
9299a2dd95SBruce Richardson mz = rte_memzone_reserve_aligned(mz_name, sz, socket_id,
9399a2dd95SBruce Richardson 0, __alignof__(*s));
9499a2dd95SBruce Richardson if (mz == NULL) {
9599a2dd95SBruce Richardson STACK_LOG_ERR("Cannot reserve stack memzone!\n");
9699a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
9799a2dd95SBruce Richardson rte_free(te);
9899a2dd95SBruce Richardson return NULL;
9999a2dd95SBruce Richardson }
10099a2dd95SBruce Richardson
10199a2dd95SBruce Richardson s = mz->addr;
10299a2dd95SBruce Richardson
10399a2dd95SBruce Richardson rte_stack_init(s, count, flags);
10499a2dd95SBruce Richardson
10599a2dd95SBruce Richardson /* Store the name for later lookups */
10699a2dd95SBruce Richardson ret = strlcpy(s->name, name, sizeof(s->name));
10799a2dd95SBruce Richardson if (ret < 0 || ret >= (int)sizeof(s->name)) {
10899a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
10999a2dd95SBruce Richardson
11099a2dd95SBruce Richardson rte_errno = ENAMETOOLONG;
11199a2dd95SBruce Richardson rte_free(te);
11299a2dd95SBruce Richardson rte_memzone_free(mz);
11399a2dd95SBruce Richardson return NULL;
11499a2dd95SBruce Richardson }
11599a2dd95SBruce Richardson
11699a2dd95SBruce Richardson s->memzone = mz;
11799a2dd95SBruce Richardson s->capacity = count;
11899a2dd95SBruce Richardson s->flags = flags;
11999a2dd95SBruce Richardson
12099a2dd95SBruce Richardson te->data = s;
12199a2dd95SBruce Richardson
12299a2dd95SBruce Richardson stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
12399a2dd95SBruce Richardson
12499a2dd95SBruce Richardson TAILQ_INSERT_TAIL(stack_list, te, next);
12599a2dd95SBruce Richardson
12699a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
12799a2dd95SBruce Richardson
12899a2dd95SBruce Richardson return s;
12999a2dd95SBruce Richardson }
13099a2dd95SBruce Richardson
13199a2dd95SBruce Richardson void
rte_stack_free(struct rte_stack * s)13299a2dd95SBruce Richardson rte_stack_free(struct rte_stack *s)
13399a2dd95SBruce Richardson {
13499a2dd95SBruce Richardson struct rte_stack_list *stack_list;
13599a2dd95SBruce Richardson struct rte_tailq_entry *te;
13699a2dd95SBruce Richardson
13799a2dd95SBruce Richardson if (s == NULL)
13899a2dd95SBruce Richardson return;
13999a2dd95SBruce Richardson
14099a2dd95SBruce Richardson stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
14199a2dd95SBruce Richardson rte_mcfg_tailq_write_lock();
14299a2dd95SBruce Richardson
14399a2dd95SBruce Richardson /* find out tailq entry */
14499a2dd95SBruce Richardson TAILQ_FOREACH(te, stack_list, next) {
14599a2dd95SBruce Richardson if (te->data == s)
14699a2dd95SBruce Richardson break;
14799a2dd95SBruce Richardson }
14899a2dd95SBruce Richardson
14999a2dd95SBruce Richardson if (te == NULL) {
15099a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
15199a2dd95SBruce Richardson return;
15299a2dd95SBruce Richardson }
15399a2dd95SBruce Richardson
15499a2dd95SBruce Richardson TAILQ_REMOVE(stack_list, te, next);
15599a2dd95SBruce Richardson
15699a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
15799a2dd95SBruce Richardson
15899a2dd95SBruce Richardson rte_free(te);
15999a2dd95SBruce Richardson
16099a2dd95SBruce Richardson rte_memzone_free(s->memzone);
16199a2dd95SBruce Richardson }
16299a2dd95SBruce Richardson
16399a2dd95SBruce Richardson struct rte_stack *
rte_stack_lookup(const char * name)16499a2dd95SBruce Richardson rte_stack_lookup(const char *name)
16599a2dd95SBruce Richardson {
16699a2dd95SBruce Richardson struct rte_stack_list *stack_list;
16799a2dd95SBruce Richardson struct rte_tailq_entry *te;
16899a2dd95SBruce Richardson struct rte_stack *r = NULL;
16999a2dd95SBruce Richardson
17099a2dd95SBruce Richardson if (name == NULL) {
17199a2dd95SBruce Richardson rte_errno = EINVAL;
17299a2dd95SBruce Richardson return NULL;
17399a2dd95SBruce Richardson }
17499a2dd95SBruce Richardson
17599a2dd95SBruce Richardson stack_list = RTE_TAILQ_CAST(rte_stack_tailq.head, rte_stack_list);
17699a2dd95SBruce Richardson
17799a2dd95SBruce Richardson rte_mcfg_tailq_read_lock();
17899a2dd95SBruce Richardson
17999a2dd95SBruce Richardson TAILQ_FOREACH(te, stack_list, next) {
18099a2dd95SBruce Richardson r = (struct rte_stack *) te->data;
18199a2dd95SBruce Richardson if (strncmp(name, r->name, RTE_STACK_NAMESIZE) == 0)
18299a2dd95SBruce Richardson break;
18399a2dd95SBruce Richardson }
18499a2dd95SBruce Richardson
18599a2dd95SBruce Richardson rte_mcfg_tailq_read_unlock();
18699a2dd95SBruce Richardson
18799a2dd95SBruce Richardson if (te == NULL) {
18899a2dd95SBruce Richardson rte_errno = ENOENT;
18999a2dd95SBruce Richardson return NULL;
19099a2dd95SBruce Richardson }
19199a2dd95SBruce Richardson
19299a2dd95SBruce Richardson return r;
19399a2dd95SBruce Richardson }
19499a2dd95SBruce Richardson
195eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(stack_logtype, NOTICE);
196