env/memory: return the memory virtual address's file descriptor and offset

For virtio-user library and coming vfio-user feature, the client needs
to send the memory file descriptors to target so that the two processes
can setup shared memory region to do data processing without memory copy.
Currently virtio-user will read /proc/self/maps to get memory file descriptor,
since DPDK already provides this such APIs, so here we can just use it,
for existing virtio-user library we may replace it with the new added
API.

Change-Id: Icfeae465d53826d0c8d1b335287634b03cd174aa
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4428
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Changpeng Liu 2020-09-27 01:46:21 -04:00 committed by Jim Harris
parent 95b76c4dcc
commit c0bf93145a
4 changed files with 37 additions and 1 deletions

View File

@ -1294,6 +1294,16 @@ int spdk_mem_unregister(void *vaddr, size_t len);
*/
int spdk_mem_reserve(void *vaddr, size_t len);
/**
* Get the address's file descriptor and offset, it works with spdk memory allocation APIs
*
* \param vaddr Virtual address to get
* \param offset Virtual address's map offset to the file descriptor
*
* \ return negative errno on failure, otherwise return the file descriptor
*/
int spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset);
#ifdef __cplusplus
}
#endif

View File

@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
SO_VER := 5
SO_MINOR := 0
SO_MINOR := 1
CFLAGS += $(ENV_CFLAGS)
C_SRCS = env.c memory.c pci.c init.c threads.c

View File

@ -1445,3 +1445,28 @@ spdk_vtophys(const void *buf, uint64_t *size)
return paddr_2mb + (vaddr & MASK_2MB);
}
}
int
spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset)
{
struct rte_memseg *seg;
int ret, fd;
seg = rte_mem_virt2memseg(vaddr, NULL);
if (!seg) {
SPDK_ERRLOG("memory %p doesn't exist\n", vaddr);
return -ENOENT;
}
fd = rte_memseg_get_fd_thread_unsafe(seg);
if (fd < 0) {
return fd;
}
ret = rte_memseg_get_fd_offset_thread_unsafe(seg, offset);
if (ret < 0) {
return ret;
}
return fd;
}

View File

@ -103,6 +103,7 @@
spdk_mem_map_translate;
spdk_mem_register;
spdk_mem_unregister;
spdk_mem_get_fd_and_offset;
# Public functions in env_dpdk.h
spdk_env_dpdk_post_init;