[RFC,22/27] vhost: add VDUSE events handler

Message ID 20230331154259.1447831-23-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series Add VDUSE support to Vhost library |

Commit Message

Maxime Coquelin March 31, 2023, 3:42 p.m. UTC
  This patch makes use of Vhost lib's FD manager to install
a handler for VDUSE events occurring on the VDUSE device FD.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/vduse.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
  

Comments

Chenbo Xia May 9, 2023, 5:34 a.m. UTC | #1
> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Friday, March 31, 2023 11:43 PM
> To: dev@dpdk.org; david.marchand@redhat.com; Xia, Chenbo
> <chenbo.xia@intel.com>; mkp@redhat.com; fbl@redhat.com;
> jasowang@redhat.com; Liang, Cunming <cunming.liang@intel.com>; Xie, Yongji
> <xieyongji@bytedance.com>; echaudro@redhat.com; eperezma@redhat.com;
> amorenoz@redhat.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [RFC 22/27] vhost: add VDUSE events handler
> 
> This patch makes use of Vhost lib's FD manager to install
> a handler for VDUSE events occurring on the VDUSE device FD.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/vhost/vduse.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 102 insertions(+)
> 
> diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
> index afa8a39498..2a183130d3 100644
> --- a/lib/vhost/vduse.c
> +++ b/lib/vhost/vduse.c
> @@ -17,6 +17,7 @@
> 
>  #include <rte_common.h>
> 
> +#include "fd_man.h"
>  #include "iotlb.h"
>  #include "vduse.h"
>  #include "vhost.h"
> @@ -32,6 +33,27 @@
>  				(1ULL << VIRTIO_F_IN_ORDER) | \
>  				(1ULL << VIRTIO_F_IOMMU_PLATFORM))
> 
> +struct vduse {
> +	struct fdset fdset;
> +};
> +
> +static struct vduse vduse = {
> +	.fdset = {
> +		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
> +		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
> +		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
> +		.num = 0
> +	},
> +};
> +
> +static bool vduse_events_thread;
> +
> +static const char * const vduse_reqs_str[] = {
> +	"VDUSE_GET_VQ_STATE",
> +	"VDUSE_SET_STATUS",
> +	"VDUSE_UPDATE_IOTLB",
> +};
> +
>  static int
>  vduse_inject_irq(struct virtio_net *dev, struct vhost_virtqueue *vq)
>  {
> @@ -105,16 +127,84 @@ static struct vhost_backend_ops vduse_backend_ops =
> {
>  	.inject_irq = vduse_inject_irq,
>  };
> 
> +static void
> +vduse_events_handler(int fd, void *arg, int *remove __rte_unused)
> +{
> +	struct virtio_net *dev = arg;
> +	struct vduse_dev_request req;
> +	struct vduse_dev_response resp;
> +	int ret;
> +
> +	memset(&resp, 0, sizeof(resp));
> +
> +	ret = read(fd, &req, sizeof(req));
> +	if (ret < 0) {
> +		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to read
> request: %s\n",
> +				strerror(errno));
> +		return;
> +	} else if (ret < (int)sizeof(req)) {
> +		VHOST_LOG_CONFIG(dev->ifname, ERR, "Incomplete to read
> request %d\n", ret);
> +		return;
> +	}
> +
> +	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
> +
> +	VHOST_LOG_CONFIG(dev->ifname, INFO, "New request: %s (%u)\n",
> +			req.type < RTE_DIM(vduse_reqs_str) ?
> +			vduse_reqs_str[req.type] : "Unknown",
> +			req.type);
> +
> +	switch (req.type) {
> +	default:
> +		resp.result = VDUSE_REQ_RESULT_FAILED;
> +		break;
> +	}
> +
> +	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
> +
> +	resp.request_id = req.request_id;
> +
> +	ret = write(dev->vduse_dev_fd, &resp, sizeof(resp));
> +	if (ret != sizeof(resp)) {
> +		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to write
> response %s\n",
> +				strerror(errno));
> +		return;
> +	}
> +}
> +
>  int
>  vduse_device_create(const char *path)
>  {
>  	int control_fd, dev_fd, vid, ret;
> +	pthread_t fdset_tid;
>  	uint32_t i;
>  	struct virtio_net *dev;
>  	uint64_t ver = VHOST_VDUSE_API_VERSION;
>  	struct vduse_dev_config *dev_config = NULL;
>  	const char *name = path + strlen("/dev/vduse/");
> 
> +	/* If first device, create events dispatcher thread */
> +	if (vduse_events_thread == false) {
> +		/**
> +		 * create a pipe which will be waited by poll and notified to
> +		 * rebuild the wait list of poll.
> +		 */
> +		if (fdset_pipe_init(&vduse.fdset) < 0) {
> +			VHOST_LOG_CONFIG(path, ERR, "failed to create pipe for
> vduse fdset\n");
> +			return -1;
> +		}
> +
> +		ret = rte_ctrl_thread_create(&fdset_tid, "vduse-events", NULL,
> +				fdset_event_dispatch, &vduse.fdset);
> +		if (ret != 0) {
> +			VHOST_LOG_CONFIG(path, ERR, "failed to create vduse
> fdset handling thread\n");
> +			fdset_pipe_uninit(&vduse.fdset);
> +			return -1;
> +		}
> +
> +		vduse_events_thread = true;
> +	}
> +
>  	control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
>  	if (control_fd < 0) {
>  		VHOST_LOG_CONFIG(name, ERR, "Failed to open %s: %s\n",
> @@ -198,6 +288,13 @@ vduse_device_create(const char *path)
>  		}
>  	}
> 
> +	ret = fdset_add(&vduse.fdset, dev->vduse_dev_fd,
> vduse_events_handler, NULL, dev);
> +	if (ret) {
> +		VHOST_LOG_CONFIG(name, ERR, "Failed to add fd %d to vduse
> fdset\n",
> +				dev->vduse_dev_fd);
> +		goto out_dev_destroy;
> +	}
> +
>  	free(dev_config);
> 
>  	return 0;
> @@ -236,11 +333,16 @@ vduse_device_destroy(const char *path)
>  	if (vid == RTE_MAX_VHOST_DEVICE)
>  		return -1;
> 
> +	fdset_del(&vduse.fdset, dev->vduse_dev_fd);
> +
>  	if (dev->vduse_dev_fd >= 0) {
>  		close(dev->vduse_dev_fd);
>  		dev->vduse_dev_fd = -1;
>  	}
> 
> +	sleep(2); //ToDo: Need to rework fdman to ensure the deleted FD is
> no
> +		  //more being polled, otherwise VDUSE_DESTROY_DEV will fail.
> +
>  	if (dev->vduse_ctrl_fd >= 0) {
>  		ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name);
>  		if (ret)
> --
> 2.39.2

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
  

Patch

diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index afa8a39498..2a183130d3 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -17,6 +17,7 @@ 
 
 #include <rte_common.h>
 
+#include "fd_man.h"
 #include "iotlb.h"
 #include "vduse.h"
 #include "vhost.h"
@@ -32,6 +33,27 @@ 
 				(1ULL << VIRTIO_F_IN_ORDER) | \
 				(1ULL << VIRTIO_F_IOMMU_PLATFORM))
 
+struct vduse {
+	struct fdset fdset;
+};
+
+static struct vduse vduse = {
+	.fdset = {
+		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
+		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
+		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
+		.num = 0
+	},
+};
+
+static bool vduse_events_thread;
+
+static const char * const vduse_reqs_str[] = {
+	"VDUSE_GET_VQ_STATE",
+	"VDUSE_SET_STATUS",
+	"VDUSE_UPDATE_IOTLB",
+};
+
 static int
 vduse_inject_irq(struct virtio_net *dev, struct vhost_virtqueue *vq)
 {
@@ -105,16 +127,84 @@  static struct vhost_backend_ops vduse_backend_ops = {
 	.inject_irq = vduse_inject_irq,
 };
 
+static void
+vduse_events_handler(int fd, void *arg, int *remove __rte_unused)
+{
+	struct virtio_net *dev = arg;
+	struct vduse_dev_request req;
+	struct vduse_dev_response resp;
+	int ret;
+
+	memset(&resp, 0, sizeof(resp));
+
+	ret = read(fd, &req, sizeof(req));
+	if (ret < 0) {
+		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to read request: %s\n",
+				strerror(errno));
+		return;
+	} else if (ret < (int)sizeof(req)) {
+		VHOST_LOG_CONFIG(dev->ifname, ERR, "Incomplete to read request %d\n", ret);
+		return;
+	}
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+
+	VHOST_LOG_CONFIG(dev->ifname, INFO, "New request: %s (%u)\n",
+			req.type < RTE_DIM(vduse_reqs_str) ?
+			vduse_reqs_str[req.type] : "Unknown",
+			req.type);
+
+	switch (req.type) {
+	default:
+		resp.result = VDUSE_REQ_RESULT_FAILED;
+		break;
+	}
+
+	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+
+	resp.request_id = req.request_id;
+
+	ret = write(dev->vduse_dev_fd, &resp, sizeof(resp));
+	if (ret != sizeof(resp)) {
+		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to write response %s\n",
+				strerror(errno));
+		return;
+	}
+}
+
 int
 vduse_device_create(const char *path)
 {
 	int control_fd, dev_fd, vid, ret;
+	pthread_t fdset_tid;
 	uint32_t i;
 	struct virtio_net *dev;
 	uint64_t ver = VHOST_VDUSE_API_VERSION;
 	struct vduse_dev_config *dev_config = NULL;
 	const char *name = path + strlen("/dev/vduse/");
 
+	/* If first device, create events dispatcher thread */
+	if (vduse_events_thread == false) {
+		/**
+		 * create a pipe which will be waited by poll and notified to
+		 * rebuild the wait list of poll.
+		 */
+		if (fdset_pipe_init(&vduse.fdset) < 0) {
+			VHOST_LOG_CONFIG(path, ERR, "failed to create pipe for vduse fdset\n");
+			return -1;
+		}
+
+		ret = rte_ctrl_thread_create(&fdset_tid, "vduse-events", NULL,
+				fdset_event_dispatch, &vduse.fdset);
+		if (ret != 0) {
+			VHOST_LOG_CONFIG(path, ERR, "failed to create vduse fdset handling thread\n");
+			fdset_pipe_uninit(&vduse.fdset);
+			return -1;
+		}
+
+		vduse_events_thread = true;
+	}
+
 	control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
 	if (control_fd < 0) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to open %s: %s\n",
@@ -198,6 +288,13 @@  vduse_device_create(const char *path)
 		}
 	}
 
+	ret = fdset_add(&vduse.fdset, dev->vduse_dev_fd, vduse_events_handler, NULL, dev);
+	if (ret) {
+		VHOST_LOG_CONFIG(name, ERR, "Failed to add fd %d to vduse fdset\n",
+				dev->vduse_dev_fd);
+		goto out_dev_destroy;
+	}
+
 	free(dev_config);
 
 	return 0;
@@ -236,11 +333,16 @@  vduse_device_destroy(const char *path)
 	if (vid == RTE_MAX_VHOST_DEVICE)
 		return -1;
 
+	fdset_del(&vduse.fdset, dev->vduse_dev_fd);
+
 	if (dev->vduse_dev_fd >= 0) {
 		close(dev->vduse_dev_fd);
 		dev->vduse_dev_fd = -1;
 	}
 
+	sleep(2); //ToDo: Need to rework fdman to ensure the deleted FD is no
+		  //more being polled, otherwise VDUSE_DESTROY_DEV will fail.
+
 	if (dev->vduse_ctrl_fd >= 0) {
 		ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name);
 		if (ret)