This mempool driver can be used with the shared_mem bus driver to create a pool of shared mbufs on a shared memory segment.
Signed-off-by: Bruce Richardson <bruce.richard...@intel.com> --- drivers/mempool/meson.build | 1 + drivers/mempool/shared_mem/meson.build | 10 +++ drivers/mempool/shared_mem/shared_mem_mp.c | 94 ++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 drivers/mempool/shared_mem/meson.build create mode 100644 drivers/mempool/shared_mem/shared_mem_mp.c diff --git a/drivers/mempool/meson.build b/drivers/mempool/meson.build index dc88812585..4326bd0ea3 100644 --- a/drivers/mempool/meson.build +++ b/drivers/mempool/meson.build @@ -8,6 +8,7 @@ drivers = [ 'dpaa2', 'octeontx', 'ring', + 'shared_mem', 'stack', ] diff --git a/drivers/mempool/shared_mem/meson.build b/drivers/mempool/shared_mem/meson.build new file mode 100644 index 0000000000..ee740ccdc9 --- /dev/null +++ b/drivers/mempool/shared_mem/meson.build @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2023 Intel Corporation + +if is_windows + build = false + reason = 'not supported on Windows' +endif +sources = files('shared_mem_mp.c') +require_iova_in_mbuf = false +deps += ['stack', 'bus_shared_mem'] diff --git a/drivers/mempool/shared_mem/shared_mem_mp.c b/drivers/mempool/shared_mem/shared_mem_mp.c new file mode 100644 index 0000000000..7dae8aba92 --- /dev/null +++ b/drivers/mempool/shared_mem/shared_mem_mp.c @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Intel Corporation + */ +#include <sys/types.h> +#include <rte_mbuf.h> +#include <rte_stack.h> +#include <rte_mempool.h> +#include <shared_mem_bus.h> + +RTE_LOG_REGISTER_DEFAULT(shared_mem_mp_log, DEBUG); +#define MP_LOG(level, fmt, args...) rte_log(RTE_LOG_ ## level, \ + shared_mem_mp_log, "## SHARED MP: %s(): " fmt "\n", __func__, ##args) +#define MP_ERR(fmt, args...) MP_LOG(ERR, fmt, ## args) +#define MP_INFO(fmt, args...) MP_LOG(INFO, fmt, ## args) +#define MP_DEBUG(fmt, args...) MP_LOG(DEBUG, fmt, ## args) + +static int +shm_mp_alloc(struct rte_mempool *mp) +{ + char name[RTE_STACK_NAMESIZE]; + struct rte_stack *s; + int ret; + + ret = snprintf(name, sizeof(name), RTE_MEMPOOL_MZ_FORMAT, mp->name); + if (ret < 0 || ret >= (int)sizeof(name)) { + rte_errno = ENAMETOOLONG; + return -rte_errno; + } + + s = rte_stack_create(name, mp->size, mp->socket_id, 0); + if (s == NULL) + return -rte_errno; + MP_DEBUG("Stack created at address: %p", s); + + mp->pool_data = s; + + return 0; +} + +static int +shm_mp_enqueue(struct rte_mempool *mp, void * const *obj_table, + unsigned int n) +{ + struct rte_stack *s = mp->pool_data; + + void *offset_table[n]; + uintptr_t mempool_base = (uintptr_t)rte_shm_bus_get_mem_ptr(0); /* offset 0 == base addr */ + for (uint i = 0; i < n; i++) + offset_table[i] = RTE_PTR_SUB(obj_table[i], mempool_base); + + return rte_stack_push(s, offset_table, n) == 0 ? -ENOBUFS : 0; +} + +static int +shm_mp_dequeue(struct rte_mempool *mp, void **obj_table, + unsigned int n) +{ + struct rte_stack *s = mp->pool_data; + uintptr_t mempool_base = (uintptr_t)rte_shm_bus_get_mem_ptr(0); /* offset 0 == base addr */ + uint16_t priv_size = rte_pktmbuf_priv_size(mp); + + if (rte_stack_pop(s, obj_table, n) == 0) + return -ENOBUFS; + for (uint i = 0; i < n; i++) { + obj_table[i] = RTE_PTR_ADD(obj_table[i], mempool_base); + struct rte_mbuf *mb = obj_table[i]; + mb->buf_addr = RTE_PTR_ADD(mb, sizeof(struct rte_mbuf) + priv_size); + mb->pool = mp; + } + return 0; +} + +static unsigned +shm_mp_get_count(const struct rte_mempool *mp) +{ + return rte_stack_count(mp->pool_data); +} + +static void +shm_mp_free(struct rte_mempool *mp) +{ + rte_stack_free(mp->pool_data); +} + +static struct rte_mempool_ops ops_shared_mem_mp = { + .name = "shared_mem", + .alloc = shm_mp_alloc, + .free = shm_mp_free, + .enqueue = shm_mp_enqueue, + .dequeue = shm_mp_dequeue, + .get_count = shm_mp_get_count, +}; + +RTE_MEMPOOL_REGISTER_OPS(ops_shared_mem_mp); -- 2.39.2