[PATCH 3/7] vhost: make use of FD manager init function

Maxime Coquelin maxime.coquelin at redhat.com
Thu Feb 29 13:24:58 CET 2024


Instead of statically initialize the fdset, this patch
converts VDUSE and Vhost-user to use fdset_init() function,
which now also initialize the mutexes.

This is preliminary rework to hide FDs manager pipe from
its users.

Signed-off-by: Maxime Coquelin <maxime.coquelin at redhat.com>
---
 lib/vhost/fd_man.c |  9 +++++++--
 lib/vhost/fd_man.h |  2 +-
 lib/vhost/socket.c | 11 +++++------
 lib/vhost/vduse.c  | 14 ++++++--------
 4 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c
index 5dde40e51a..d33036a171 100644
--- a/lib/vhost/fd_man.c
+++ b/lib/vhost/fd_man.c
@@ -96,19 +96,24 @@ fdset_add_fd(struct fdset *pfdset, int idx, int fd,
 	pfd->revents = 0;
 }
 
-void
+int
 fdset_init(struct fdset *pfdset)
 {
 	int i;
 
 	if (pfdset == NULL)
-		return;
+		return -1;
+
+	pthread_mutex_init(&pfdset->fd_mutex, NULL);
+	pthread_mutex_init(&pfdset->fd_polling_mutex, NULL);
 
 	for (i = 0; i < MAX_FDS; i++) {
 		pfdset->fd[i].fd = -1;
 		pfdset->fd[i].dat = NULL;
 	}
 	pfdset->num = 0;
+
+	return 0;
 }
 
 /**
diff --git a/lib/vhost/fd_man.h b/lib/vhost/fd_man.h
index 2517ae5a9b..92d24d8591 100644
--- a/lib/vhost/fd_man.h
+++ b/lib/vhost/fd_man.h
@@ -43,7 +43,7 @@ struct fdset {
 };
 
 
-void fdset_init(struct fdset *pfdset);
+int fdset_init(struct fdset *pfdset);
 
 int fdset_add(struct fdset *pfdset, int fd,
 	fd_cb rcb, fd_cb wcb, void *dat);
diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index a2fdac30a4..b544e39be7 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -89,12 +89,6 @@ static int create_unix_socket(struct vhost_user_socket *vsocket);
 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
 
 static struct vhost_user vhost_user = {
-	.fdset = {
-		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
-		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
-		.num = 0
-	},
 	.vsocket_cnt = 0,
 	.mutex = PTHREAD_MUTEX_INITIALIZER,
 };
@@ -1187,6 +1181,11 @@ rte_vhost_driver_start(const char *path)
 		return vduse_device_create(path, vsocket->net_compliant_ol_flags);
 
 	if (fdset_tid.opaque_id == 0) {
+		if (fdset_init(&vhost_user.fdset) < 0) {
+			VHOST_CONFIG_LOG(path, ERR, "Failed to init Vhost-user fdset");
+			return -1;
+		}
+
 		/**
 		 * create a pipe which will be waited by poll and notified to
 		 * rebuild the wait list of poll.
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index d462428d2c..d83d7b0d7c 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -31,14 +31,7 @@ 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 struct vduse vduse;
 
 static bool vduse_events_thread;
 
@@ -434,6 +427,11 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
 
 	/* If first device, create events dispatcher thread */
 	if (vduse_events_thread == false) {
+		if (fdset_init(&vduse.fdset) < 0) {
+			VHOST_CONFIG_LOG(path, ERR, "Failed to init VDUSE fdset");
+			return -1;
+		}
+
 		/**
 		 * create a pipe which will be waited by poll and notified to
 		 * rebuild the wait list of poll.
-- 
2.43.2



More information about the dev mailing list