diff --git a/include/spdk/env.h b/include/spdk/env.h index c54c8c8ed..bac976c30 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright (C) 2015 Intel Corporation. * Copyright (c) NetApp, Inc. + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. * All rights reserved. */ @@ -322,6 +323,14 @@ struct spdk_mempool *spdk_mempool_create(const char *name, size_t count, typedef void (spdk_mempool_obj_cb_t)(struct spdk_mempool *mp, void *opaque, void *obj, unsigned obj_idx); +/** + * A memory chunk callback function for memory pool. + * + * Used by spdk_mempool_mem_iter(). + */ +typedef void (spdk_mempool_mem_cb_t)(struct spdk_mempool *mp, void *opaque, void *addr, + uint64_t iova, size_t len, unsigned mem_idx); + /** * Create a thread-safe memory pool with user provided initialization function * and argument. @@ -414,6 +423,18 @@ size_t spdk_mempool_count(const struct spdk_mempool *pool); uint32_t spdk_mempool_obj_iter(struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb, void *obj_cb_arg); +/** + * Iterate through all memory chunks of the pool and call a function on each one. + * + * \param mp Memory pool to iterate on. + * \param mem_cb Function to call on each memory chunk. + * \param mem_cb_arg Opaque pointer passed to the callback function. + * + * \return Number of memory chunks iterated. + */ +uint32_t spdk_mempool_mem_iter(struct spdk_mempool *mp, spdk_mempool_mem_cb_t mem_cb, + void *mem_cb_arg); + /** * Lookup the memory pool identified by the given name. * diff --git a/lib/env_dpdk/env.c b/lib/env_dpdk/env.c index 7c1fd624e..b6b8135b8 100644 --- a/lib/env_dpdk/env.c +++ b/lib/env_dpdk/env.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright (C) 2016 Intel Corporation. + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. * All rights reserved. */ @@ -283,6 +284,33 @@ spdk_mempool_obj_iter(struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb, obj_cb_arg); } +struct env_mempool_mem_iter_ctx { + spdk_mempool_mem_cb_t *user_cb; + void *user_arg; +}; + +static void +mempool_mem_iter_remap(struct rte_mempool *mp, void *opaque, struct rte_mempool_memhdr *memhdr, + unsigned mem_idx) +{ + struct env_mempool_mem_iter_ctx *ctx = opaque; + + ctx->user_cb((struct spdk_mempool *)mp, ctx->user_arg, memhdr->addr, memhdr->iova, memhdr->len, + mem_idx); +} + +uint32_t +spdk_mempool_mem_iter(struct spdk_mempool *mp, spdk_mempool_mem_cb_t mem_cb, + void *mem_cb_arg) +{ + struct env_mempool_mem_iter_ctx ctx = { + .user_cb = mem_cb, + .user_arg = mem_cb_arg + }; + + return rte_mempool_mem_iter((struct rte_mempool *)mp, mempool_mem_iter_remap, &ctx); +} + struct spdk_mempool * spdk_mempool_lookup(const char *name) { diff --git a/lib/env_dpdk/spdk_env_dpdk.map b/lib/env_dpdk/spdk_env_dpdk.map index 1fc151df8..e2cc179ef 100644 --- a/lib/env_dpdk/spdk_env_dpdk.map +++ b/lib/env_dpdk/spdk_env_dpdk.map @@ -30,6 +30,7 @@ spdk_mempool_put_bulk; spdk_mempool_count; spdk_mempool_obj_iter; + spdk_mempool_mem_iter; spdk_mempool_lookup; spdk_env_get_core_count; spdk_env_get_current_core;