[dpdk-dev] [PATCH 3/9] vfio_user: implement device and region related APIs

Xing, Beilei beilei.xing at intel.com
Wed Jan 6 06:51:42 CET 2021



> -----Original Message-----
> From: dev <dev-bounces at dpdk.org> On Behalf Of Chenbo Xia
> Sent: Friday, December 18, 2020 3:39 PM
> To: dev at dpdk.org; thomas at monjalon.net; david.marchand at redhat.com
> Cc: stephen at networkplumber.org; Liang, Cunming
> <cunming.liang at intel.com>; Lu, Xiuchun <xiuchun.lu at intel.com>; Li, Miao
> <miao.li at intel.com>; Wu, Jingjing <jingjing.wu at intel.com>
> Subject: [dpdk-dev] [PATCH 3/9] vfio_user: implement device and region
> related APIs
> 
> This patch introduces device and region related APIs, which are
> rte_vfio_user_set_dev_info() and rte_vfio_user_set_reg_info().
> The corresponding vfio-user command handling is also added with the
> definition of all vfio-user command identity.
> 
> Signed-off-by: Chenbo Xia <chenbo.xia at intel.com>
> Signed-off-by: Xiuchun Lu <xiuchun.lu at intel.com>
> ---
>  lib/librte_vfio_user/rte_vfio_user.h    |  60 ++++++
>  lib/librte_vfio_user/version.map        |   2 +
>  lib/librte_vfio_user/vfio_user_base.c   |  12 ++
>  lib/librte_vfio_user/vfio_user_base.h   |  32 +++-
>  lib/librte_vfio_user/vfio_user_server.c | 232 ++++++++++++++++++++++++
>  lib/librte_vfio_user/vfio_user_server.h |   2 +
>  6 files changed, 339 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/librte_vfio_user/rte_vfio_user.h
> b/lib/librte_vfio_user/rte_vfio_user.h
> index 0d4f6c1be2..8a999c7aa0 100644
> --- a/lib/librte_vfio_user/rte_vfio_user.h
> +++ b/lib/librte_vfio_user/rte_vfio_user.h
> @@ -5,13 +5,35 @@
>  #ifndef _RTE_VFIO_USER_H
>  #define _RTE_VFIO_USER_H
> 

<snip>

> +static int vfio_user_device_get_reg_info(struct vfio_user_server *dev,
> +	VFIO_USER_MSG *msg)
> +{
> +	struct vfio_user_reg *reg = &msg->payload.reg_info;
> +	struct rte_vfio_user_reg_info *reg_info;
> +	struct vfio_region_info *vinfo;
> +
> +	if (vfio_user_check_msg_fdnum(msg, 0) != 0)
> +		return -EINVAL;
> +
> +	if (msg->size > sizeof(*reg) + VFIO_USER_MSG_HDR_SIZE ||
> +		dev->reg->reg_num <= reg->reg_info.index) {
> +		VFIO_USER_LOG(ERR, "Invalid message for get dev info\n");

Invalid message for get region info?

> +		return -EINVAL;
> +	}
> +
> +	reg_info = &dev->reg->reg_info[reg->reg_info.index];
> +	vinfo = reg_info->info;
> +	memcpy(reg, vinfo, vinfo->argsz);
> +
> +	if (reg_info->fd != -1) {
> +		msg->fd_num = 1;
> +		msg->fds[0] = reg_info->fd;
> +	}
> +
> +	VFIO_USER_LOG(DEBUG, "Region(%u) info: addr(0x%" PRIx64 "),
> fd(%d), "
> +		"sz(0x%llx), argsz(0x%x), c_off(0x%x), flags(0x%x) "
> +		"off(0x%llx)\n", vinfo->index, (uint64_t)reg_info->base,
> +		reg_info->fd, vinfo->size, vinfo->argsz, vinfo->cap_offset,
> +		vinfo->flags, vinfo->offset);
> +
> +	return 0;
> +}
> +

<snip>

> +int rte_vfio_user_set_dev_info(const char *sock_addr,
> +	struct vfio_device_info *dev_info)
> +{
> +	struct vfio_user_server *dev;
> +	struct vfio_user_server_socket *sk;
> +	int dev_id;
> +
> +	if (!dev_info)
> +		return -1;
> +
> +	pthread_mutex_lock(&vfio_ep_sock.mutex);
> +	sk = find_vfio_user_socket(sock_addr);
> +	pthread_mutex_unlock(&vfio_ep_sock.mutex);
> +
> +	if (!sk) {
> +		VFIO_USER_LOG(ERR, "Failed to set device info with
> sock_addr "
> +			"%s: addr not registered.\n", sock_addr);
> +		return -1;
> +	}
> +
> +	dev_id = sk->sock.dev_id;
> +	dev = vfio_user_get_device(dev_id);
> +	if (!dev) {
> +		VFIO_USER_LOG(ERR, "Failed to set device info:"
> +			"device %d not found.\n", dev_id);
> +		return -1;
> +	}
> +
> +	if (dev->started) {
> +		VFIO_USER_LOG(ERR, "Failed to set device info for
> device %d\n"
> +			 ", device already started\n", dev_id);
> +		return -1;
> +	}
> +
> +	dev->dev_info = dev_info;
> +
> +	return 0;
> +}
> +
> +int rte_vfio_user_set_reg_info(const char *sock_addr,
> +	struct rte_vfio_user_regions *reg)
> +{
> +	struct vfio_user_server *dev;
> +	struct vfio_user_server_socket *sk;
> +	int dev_id;
> +
> +	if (!reg)
> +		return -1;
> +
> +	pthread_mutex_lock(&vfio_ep_sock.mutex);
> +	sk = find_vfio_user_socket(sock_addr);
> +	pthread_mutex_unlock(&vfio_ep_sock.mutex);
> +
> +	if (!sk) {
> +		VFIO_USER_LOG(ERR, "Failed to set region info with
> sock_addr:"
> +			"%s: addr not registered.\n", sock_addr);
> +		return -1;
> +	}
> +
> +	dev_id = sk->sock.dev_id;
> +	dev = vfio_user_get_device(dev_id);
> +	if (!dev) {
> +		VFIO_USER_LOG(ERR, "Failed to set region info:"
> +			"device %d not found.\n", dev_id);
> +		return -1;
> +	}
> +
> +	if (dev->started) {
> +		VFIO_USER_LOG(ERR, "Failed to set region info for
> device %d\n"
> +			 ", device already started\n", dev_id);
> +		return -1;
> +	}
> +
> +	dev->reg = reg;
> +
> +	return 0;
> +}

Do you think if we can define a static function to cover the duplicated check part of function rte_vfio_user_set_dev_info and rte_vfio_user_set_reg_info?

> diff --git a/lib/librte_vfio_user/vfio_user_server.h
> b/lib/librte_vfio_user/vfio_user_server.h
> index 00e3f6353d..e8fb61cb3e 100644
> --- a/lib/librte_vfio_user/vfio_user_server.h
> +++ b/lib/librte_vfio_user/vfio_user_server.h
> @@ -16,6 +16,8 @@ struct vfio_user_server {
>  	uint32_t msg_id;
>  	char sock_addr[PATH_MAX];
>  	struct vfio_user_version ver;
> +	struct vfio_device_info *dev_info;
> +	struct rte_vfio_user_regions *reg;
>  };
> 
>  typedef int (*event_handler)(int fd, void *data);
> --
> 2.17.1



More information about the dev mailing list