Blame 0028-virtiofsd-Start-wiring-up-vhost-user.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:00:57 +0000
1d442b
Subject: [PATCH] virtiofsd: Start wiring up vhost-user
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
Listen on our unix socket for the connection from QEMU, when we get it
1d442b
initialise vhost-user and dive into our own loop variant (currently
1d442b
dummy).
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit f6f3573c6f271af5ded63ce28589a113f7205c72)
1d442b
---
1d442b
 tools/virtiofsd/fuse_i.h         |  4 ++
1d442b
 tools/virtiofsd/fuse_lowlevel.c  |  5 ++
1d442b
 tools/virtiofsd/fuse_lowlevel.h  |  7 +++
1d442b
 tools/virtiofsd/fuse_virtio.c    | 87 +++++++++++++++++++++++++++++++-
1d442b
 tools/virtiofsd/fuse_virtio.h    |  2 +
1d442b
 tools/virtiofsd/passthrough_ll.c |  7 +--
1d442b
 6 files changed, 106 insertions(+), 6 deletions(-)
1d442b
1d442b
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
1d442b
index 82d6ac7115..ec04449069 100644
1d442b
--- a/tools/virtiofsd/fuse_i.h
1d442b
+++ b/tools/virtiofsd/fuse_i.h
1d442b
@@ -13,6 +13,8 @@
1d442b
 #include "fuse.h"
1d442b
 #include "fuse_lowlevel.h"
1d442b
 
1d442b
+struct fv_VuDev;
1d442b
+
1d442b
 struct fuse_req {
1d442b
     struct fuse_session *se;
1d442b
     uint64_t unique;
1d442b
@@ -65,6 +67,8 @@ struct fuse_session {
1d442b
     size_t bufsize;
1d442b
     int error;
1d442b
     char *vu_socket_path;
1d442b
+    int   vu_socketfd;
1d442b
+    struct fv_VuDev *virtio_dev;
1d442b
 };
1d442b
 
1d442b
 struct fuse_chan {
1d442b
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
1d442b
index 5df124e64b..af09fa2b94 100644
1d442b
--- a/tools/virtiofsd/fuse_lowlevel.c
1d442b
+++ b/tools/virtiofsd/fuse_lowlevel.c
1d442b
@@ -2242,6 +2242,11 @@ void fuse_session_unmount(struct fuse_session *se)
1d442b
 {
1d442b
 }
1d442b
 
1d442b
+int fuse_lowlevel_is_virtio(struct fuse_session *se)
1d442b
+{
1d442b
+    return se->vu_socket_path != NULL;
1d442b
+}
1d442b
+
1d442b
 #ifdef linux
1d442b
 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
1d442b
 {
1d442b
diff --git a/tools/virtiofsd/fuse_lowlevel.h b/tools/virtiofsd/fuse_lowlevel.h
1d442b
index 2fa225d40b..f6b34700af 100644
1d442b
--- a/tools/virtiofsd/fuse_lowlevel.h
1d442b
+++ b/tools/virtiofsd/fuse_lowlevel.h
1d442b
@@ -1755,6 +1755,13 @@ void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1d442b
  */
1d442b
 int fuse_req_interrupted(fuse_req_t req);
1d442b
 
1d442b
+/**
1d442b
+ * Check if the session is connected via virtio
1d442b
+ *
1d442b
+ * @param se session object
1d442b
+ * @return 1 if the session is a virtio session
1d442b
+ */
1d442b
+int fuse_lowlevel_is_virtio(struct fuse_session *se);
1d442b
 
1d442b
 /*
1d442b
  * Inquiry functions
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
1d442b
index cbef6ffdda..2ae3c764dd 100644
1d442b
--- a/tools/virtiofsd/fuse_virtio.c
1d442b
+++ b/tools/virtiofsd/fuse_virtio.c
1d442b
@@ -19,18 +19,78 @@
1d442b
 
1d442b
 #include <stdint.h>
1d442b
 #include <stdio.h>
1d442b
+#include <stdlib.h>
1d442b
 #include <string.h>
1d442b
 #include <sys/socket.h>
1d442b
 #include <sys/types.h>
1d442b
 #include <sys/un.h>
1d442b
 #include <unistd.h>
1d442b
 
1d442b
+#include "contrib/libvhost-user/libvhost-user.h"
1d442b
+
1d442b
+/*
1d442b
+ * We pass the dev element into libvhost-user
1d442b
+ * and then use it to get back to the outer
1d442b
+ * container for other data.
1d442b
+ */
1d442b
+struct fv_VuDev {
1d442b
+    VuDev dev;
1d442b
+    struct fuse_session *se;
1d442b
+};
1d442b
+
1d442b
 /* From spec */
1d442b
 struct virtio_fs_config {
1d442b
     char tag[36];
1d442b
     uint32_t num_queues;
1d442b
 };
1d442b
 
1d442b
+/*
1d442b
+ * Callback from libvhost-user if there's a new fd we're supposed to listen
1d442b
+ * to, typically a queue kick?
1d442b
+ */
1d442b
+static void fv_set_watch(VuDev *dev, int fd, int condition, vu_watch_cb cb,
1d442b
+                         void *data)
1d442b
+{
1d442b
+    fuse_log(FUSE_LOG_WARNING, "%s: TODO! fd=%d\n", __func__, fd);
1d442b
+}
1d442b
+
1d442b
+/*
1d442b
+ * Callback from libvhost-user if we're no longer supposed to listen on an fd
1d442b
+ */
1d442b
+static void fv_remove_watch(VuDev *dev, int fd)
1d442b
+{
1d442b
+    fuse_log(FUSE_LOG_WARNING, "%s: TODO! fd=%d\n", __func__, fd);
1d442b
+}
1d442b
+
1d442b
+/* Callback from libvhost-user to panic */
1d442b
+static void fv_panic(VuDev *dev, const char *err)
1d442b
+{
1d442b
+    fuse_log(FUSE_LOG_ERR, "%s: libvhost-user: %s\n", __func__, err);
1d442b
+    /* TODO: Allow reconnects?? */
1d442b
+    exit(EXIT_FAILURE);
1d442b
+}
1d442b
+
1d442b
+static bool fv_queue_order(VuDev *dev, int qidx)
1d442b
+{
1d442b
+    return false;
1d442b
+}
1d442b
+
1d442b
+static const VuDevIface fv_iface = {
1d442b
+    /* TODO: Add other callbacks */
1d442b
+    .queue_is_processed_in_order = fv_queue_order,
1d442b
+};
1d442b
+
1d442b
+int virtio_loop(struct fuse_session *se)
1d442b
+{
1d442b
+    fuse_log(FUSE_LOG_INFO, "%s: Entry\n", __func__);
1d442b
+
1d442b
+    while (1) {
1d442b
+        /* TODO: Add stuffing */
1d442b
+    }
1d442b
+
1d442b
+    fuse_log(FUSE_LOG_INFO, "%s: Exit\n", __func__);
1d442b
+}
1d442b
+
1d442b
 int virtio_session_mount(struct fuse_session *se)
1d442b
 {
1d442b
     struct sockaddr_un un;
1d442b
@@ -75,5 +135,30 @@ int virtio_session_mount(struct fuse_session *se)
1d442b
         return -1;
1d442b
     }
1d442b
 
1d442b
-    return -1;
1d442b
+    fuse_log(FUSE_LOG_INFO, "%s: Waiting for vhost-user socket connection...\n",
1d442b
+             __func__);
1d442b
+    int data_sock = accept(listen_sock, NULL, NULL);
1d442b
+    if (data_sock == -1) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "vhost socket accept: %m\n");
1d442b
+        close(listen_sock);
1d442b
+        return -1;
1d442b
+    }
1d442b
+    close(listen_sock);
1d442b
+    fuse_log(FUSE_LOG_INFO, "%s: Received vhost-user socket connection\n",
1d442b
+             __func__);
1d442b
+
1d442b
+    /* TODO: Some cleanup/deallocation! */
1d442b
+    se->virtio_dev = calloc(sizeof(struct fv_VuDev), 1);
1d442b
+    if (!se->virtio_dev) {
1d442b
+        fuse_log(FUSE_LOG_ERR, "%s: virtio_dev calloc failed\n", __func__);
1d442b
+        close(data_sock);
1d442b
+        return -1;
1d442b
+    }
1d442b
+
1d442b
+    se->vu_socketfd = data_sock;
1d442b
+    se->virtio_dev->se = se;
1d442b
+    vu_init(&se->virtio_dev->dev, 2, se->vu_socketfd, fv_panic, fv_set_watch,
1d442b
+            fv_remove_watch, &fv_iface);
1d442b
+
1d442b
+    return 0;
1d442b
 }
1d442b
diff --git a/tools/virtiofsd/fuse_virtio.h b/tools/virtiofsd/fuse_virtio.h
1d442b
index 8f2edb69ca..23026d6e4c 100644
1d442b
--- a/tools/virtiofsd/fuse_virtio.h
1d442b
+++ b/tools/virtiofsd/fuse_virtio.h
1d442b
@@ -20,4 +20,6 @@ struct fuse_session;
1d442b
 
1d442b
 int virtio_session_mount(struct fuse_session *se);
1d442b
 
1d442b
+int virtio_loop(struct fuse_session *se);
1d442b
+
1d442b
 #endif
1d442b
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
1d442b
index fc9b264d56..037c5d7b26 100644
1d442b
--- a/tools/virtiofsd/passthrough_ll.c
1d442b
+++ b/tools/virtiofsd/passthrough_ll.c
1d442b
@@ -36,6 +36,7 @@
1d442b
  */
1d442b
 
1d442b
 #include "qemu/osdep.h"
1d442b
+#include "fuse_virtio.h"
1d442b
 #include "fuse_lowlevel.h"
1d442b
 #include <assert.h>
1d442b
 #include <dirent.h>
1d442b
@@ -1395,11 +1396,7 @@ int main(int argc, char *argv[])
1d442b
     fuse_daemonize(opts.foreground);
1d442b
 
1d442b
     /* Block until ctrl+c or fusermount -u */
1d442b
-    if (opts.singlethread) {
1d442b
-        ret = fuse_session_loop(se);
1d442b
-    } else {
1d442b
-        ret = fuse_session_loop_mt(se, opts.clone_fd);
1d442b
-    }
1d442b
+    ret = virtio_loop(se);
1d442b
 
1d442b
     fuse_session_unmount(se);
1d442b
 err_out3: