thebeanogamer / rpms / qemu-kvm

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