15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 2734bdeb0SGage Eads * Copyright(c) 2016-2019 Intel Corporation 31263b426SShreyansh Jain */ 41263b426SShreyansh Jain 51263b426SShreyansh Jain #include <stdio.h> 61263b426SShreyansh Jain #include <rte_mempool.h> 7734bdeb0SGage Eads #include <rte_stack.h> 81263b426SShreyansh Jain 91263b426SShreyansh Jain static int 10*e75bc77fSGage Eads __stack_alloc(struct rte_mempool *mp, uint32_t flags) 111263b426SShreyansh Jain { 12734bdeb0SGage Eads char name[RTE_STACK_NAMESIZE]; 13734bdeb0SGage Eads struct rte_stack *s; 14734bdeb0SGage Eads int ret; 151263b426SShreyansh Jain 16734bdeb0SGage Eads ret = snprintf(name, sizeof(name), 17734bdeb0SGage Eads RTE_MEMPOOL_MZ_FORMAT, mp->name); 18734bdeb0SGage Eads if (ret < 0 || ret >= (int)sizeof(name)) { 19734bdeb0SGage Eads rte_errno = ENAMETOOLONG; 20734bdeb0SGage Eads return -rte_errno; 211263b426SShreyansh Jain } 221263b426SShreyansh Jain 23*e75bc77fSGage Eads s = rte_stack_create(name, mp->size, mp->socket_id, flags); 24734bdeb0SGage Eads if (s == NULL) 25734bdeb0SGage Eads return -rte_errno; 261263b426SShreyansh Jain 271263b426SShreyansh Jain mp->pool_data = s; 281263b426SShreyansh Jain 291263b426SShreyansh Jain return 0; 301263b426SShreyansh Jain } 311263b426SShreyansh Jain 321263b426SShreyansh Jain static int 33*e75bc77fSGage Eads stack_alloc(struct rte_mempool *mp) 34*e75bc77fSGage Eads { 35*e75bc77fSGage Eads return __stack_alloc(mp, 0); 36*e75bc77fSGage Eads } 37*e75bc77fSGage Eads 38*e75bc77fSGage Eads static int 39*e75bc77fSGage Eads lf_stack_alloc(struct rte_mempool *mp) 40*e75bc77fSGage Eads { 41*e75bc77fSGage Eads return __stack_alloc(mp, RTE_STACK_F_LF); 42*e75bc77fSGage Eads } 43*e75bc77fSGage Eads 44*e75bc77fSGage Eads static int 451263b426SShreyansh Jain stack_enqueue(struct rte_mempool *mp, void * const *obj_table, 46734bdeb0SGage Eads unsigned int n) 471263b426SShreyansh Jain { 48734bdeb0SGage Eads struct rte_stack *s = mp->pool_data; 491263b426SShreyansh Jain 50734bdeb0SGage Eads return rte_stack_push(s, obj_table, n) == 0 ? -ENOBUFS : 0; 511263b426SShreyansh Jain } 521263b426SShreyansh Jain 531263b426SShreyansh Jain static int 541263b426SShreyansh Jain stack_dequeue(struct rte_mempool *mp, void **obj_table, 55734bdeb0SGage Eads unsigned int n) 561263b426SShreyansh Jain { 57734bdeb0SGage Eads struct rte_stack *s = mp->pool_data; 581263b426SShreyansh Jain 59734bdeb0SGage Eads return rte_stack_pop(s, obj_table, n) == 0 ? -ENOBUFS : 0; 601263b426SShreyansh Jain } 611263b426SShreyansh Jain 621263b426SShreyansh Jain static unsigned 631263b426SShreyansh Jain stack_get_count(const struct rte_mempool *mp) 641263b426SShreyansh Jain { 65734bdeb0SGage Eads struct rte_stack *s = mp->pool_data; 661263b426SShreyansh Jain 67734bdeb0SGage Eads return rte_stack_count(s); 681263b426SShreyansh Jain } 691263b426SShreyansh Jain 701263b426SShreyansh Jain static void 711263b426SShreyansh Jain stack_free(struct rte_mempool *mp) 721263b426SShreyansh Jain { 73734bdeb0SGage Eads struct rte_stack *s = mp->pool_data; 74734bdeb0SGage Eads 75734bdeb0SGage Eads rte_stack_free(s); 761263b426SShreyansh Jain } 771263b426SShreyansh Jain 781263b426SShreyansh Jain static struct rte_mempool_ops ops_stack = { 791263b426SShreyansh Jain .name = "stack", 801263b426SShreyansh Jain .alloc = stack_alloc, 811263b426SShreyansh Jain .free = stack_free, 821263b426SShreyansh Jain .enqueue = stack_enqueue, 831263b426SShreyansh Jain .dequeue = stack_dequeue, 841263b426SShreyansh Jain .get_count = stack_get_count 851263b426SShreyansh Jain }; 861263b426SShreyansh Jain 87*e75bc77fSGage Eads static struct rte_mempool_ops ops_lf_stack = { 88*e75bc77fSGage Eads .name = "lf_stack", 89*e75bc77fSGage Eads .alloc = lf_stack_alloc, 90*e75bc77fSGage Eads .free = stack_free, 91*e75bc77fSGage Eads .enqueue = stack_enqueue, 92*e75bc77fSGage Eads .dequeue = stack_dequeue, 93*e75bc77fSGage Eads .get_count = stack_get_count 94*e75bc77fSGage Eads }; 95*e75bc77fSGage Eads 961263b426SShreyansh Jain MEMPOOL_REGISTER_OPS(ops_stack); 97*e75bc77fSGage Eads MEMPOOL_REGISTER_OPS(ops_lf_stack); 98