thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
22c213
From b4af2eff8ecadb4e2c9520602455f77fac2cb943 Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:02 +0100
22c213
Subject: [PATCH 031/116] virtiofsd: Start reading commands from queue
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200127190227.40942-28-dgilbert@redhat.com>
22c213
Patchwork-id: 93484
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 027/112] virtiofsd: Start reading commands from queue
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
22c213
Pop queue elements off queues, copy the data from them and
22c213
pass that to fuse.
22c213
22c213
  Note: 'out' in a VuVirtqElement is from QEMU
22c213
        'in' in libfuse is into the daemon
22c213
22c213
  So we read from the out iov's to get a fuse_in_header
22c213
22c213
When we get a kick we've got to read all the elements until the queue
22c213
is empty.
22c213
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit b509e1228b3e5eb83c14819045988999fc2dbd1b)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/fuse_i.h      |  2 +
22c213
 tools/virtiofsd/fuse_virtio.c | 99 +++++++++++++++++++++++++++++++++++++++++--
22c213
 2 files changed, 98 insertions(+), 3 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
22c213
index ec04449..1126723 100644
22c213
--- a/tools/virtiofsd/fuse_i.h
22c213
+++ b/tools/virtiofsd/fuse_i.h
22c213
@@ -14,6 +14,7 @@
22c213
 #include "fuse_lowlevel.h"
22c213
 
22c213
 struct fv_VuDev;
22c213
+struct fv_QueueInfo;
22c213
 
22c213
 struct fuse_req {
22c213
     struct fuse_session *se;
22c213
@@ -75,6 +76,7 @@ struct fuse_chan {
22c213
     pthread_mutex_t lock;
22c213
     int ctr;
22c213
     int fd;
22c213
+    struct fv_QueueInfo *qi;
22c213
 };
22c213
 
22c213
 /**
22c213
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
22c213
index 05e7258..3841b20 100644
22c213
--- a/tools/virtiofsd/fuse_virtio.c
22c213
+++ b/tools/virtiofsd/fuse_virtio.c
22c213
@@ -12,6 +12,7 @@
22c213
  */
22c213
 
22c213
 #include "qemu/osdep.h"
22c213
+#include "qemu/iov.h"
22c213
 #include "fuse_virtio.h"
22c213
 #include "fuse_i.h"
22c213
 #include "standard-headers/linux/fuse.h"
22c213
@@ -32,6 +33,7 @@
22c213
 
22c213
 #include "contrib/libvhost-user/libvhost-user.h"
22c213
 
22c213
+struct fv_VuDev;
22c213
 struct fv_QueueInfo {
22c213
     pthread_t thread;
22c213
     struct fv_VuDev *virtio_dev;
22c213
@@ -101,10 +103,41 @@ static void fv_panic(VuDev *dev, const char *err)
22c213
     exit(EXIT_FAILURE);
22c213
 }
22c213
 
22c213
+/*
22c213
+ * Copy from an iovec into a fuse_buf (memory only)
22c213
+ * Caller must ensure there is space
22c213
+ */
22c213
+static void copy_from_iov(struct fuse_buf *buf, size_t out_num,
22c213
+                          const struct iovec *out_sg)
22c213
+{
22c213
+    void *dest = buf->mem;
22c213
+
22c213
+    while (out_num) {
22c213
+        size_t onelen = out_sg->iov_len;
22c213
+        memcpy(dest, out_sg->iov_base, onelen);
22c213
+        dest += onelen;
22c213
+        out_sg++;
22c213
+        out_num--;
22c213
+    }
22c213
+}
22c213
+
22c213
 /* Thread function for individual queues, created when a queue is 'started' */
22c213
 static void *fv_queue_thread(void *opaque)
22c213
 {
22c213
     struct fv_QueueInfo *qi = opaque;
22c213
+    struct VuDev *dev = &qi->virtio_dev->dev;
22c213
+    struct VuVirtq *q = vu_get_queue(dev, qi->qidx);
22c213
+    struct fuse_session *se = qi->virtio_dev->se;
22c213
+    struct fuse_chan ch;
22c213
+    struct fuse_buf fbuf;
22c213
+
22c213
+    fbuf.mem = NULL;
22c213
+    fbuf.flags = 0;
22c213
+
22c213
+    fuse_mutex_init(&ch.lock);
22c213
+    ch.fd = (int)0xdaff0d111;
22c213
+    ch.qi = qi;
22c213
+
22c213
     fuse_log(FUSE_LOG_INFO, "%s: Start for queue %d kick_fd %d\n", __func__,
22c213
              qi->qidx, qi->kick_fd);
22c213
     while (1) {
22c213
@@ -141,11 +174,71 @@ static void *fv_queue_thread(void *opaque)
22c213
             fuse_log(FUSE_LOG_ERR, "Eventfd_read for queue: %m\n");
22c213
             break;
22c213
         }
22c213
-        if (qi->virtio_dev->se->debug) {
22c213
-            fprintf(stderr, "%s: Queue %d gave evalue: %zx\n", __func__,
22c213
-                    qi->qidx, (size_t)evalue);
22c213
+        /* out is from guest, in is too guest */
22c213
+        unsigned int in_bytes, out_bytes;
22c213
+        vu_queue_get_avail_bytes(dev, q, &in_bytes, &out_bytes, ~0, ~0);
22c213
+
22c213
+        fuse_log(FUSE_LOG_DEBUG,
22c213
+                 "%s: Queue %d gave evalue: %zx available: in: %u out: %u\n",
22c213
+                 __func__, qi->qidx, (size_t)evalue, in_bytes, out_bytes);
22c213
+
22c213
+        while (1) {
22c213
+            /*
22c213
+             * An element contains one request and the space to send our
22c213
+             * response They're spread over multiple descriptors in a
22c213
+             * scatter/gather set and we can't trust the guest to keep them
22c213
+             * still; so copy in/out.
22c213
+             */
22c213
+            VuVirtqElement *elem = vu_queue_pop(dev, q, sizeof(VuVirtqElement));
22c213
+            if (!elem) {
22c213
+                break;
22c213
+            }
22c213
+
22c213
+            if (!fbuf.mem) {
22c213
+                fbuf.mem = malloc(se->bufsize);
22c213
+                assert(fbuf.mem);
22c213
+                assert(se->bufsize > sizeof(struct fuse_in_header));
22c213
+            }
22c213
+            /* The 'out' part of the elem is from qemu */
22c213
+            unsigned int out_num = elem->out_num;
22c213
+            struct iovec *out_sg = elem->out_sg;
22c213
+            size_t out_len = iov_size(out_sg, out_num);
22c213
+            fuse_log(FUSE_LOG_DEBUG,
22c213
+                     "%s: elem %d: with %d out desc of length %zd\n", __func__,
22c213
+                     elem->index, out_num, out_len);
22c213
+
22c213
+            /*
22c213
+             * The elem should contain a 'fuse_in_header' (in to fuse)
22c213
+             * plus the data based on the len in the header.
22c213
+             */
22c213
+            if (out_len < sizeof(struct fuse_in_header)) {
22c213
+                fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for in_header\n",
22c213
+                         __func__, elem->index);
22c213
+                assert(0); /* TODO */
22c213
+            }
22c213
+            if (out_len > se->bufsize) {
22c213
+                fuse_log(FUSE_LOG_ERR, "%s: elem %d too large for buffer\n",
22c213
+                         __func__, elem->index);
22c213
+                assert(0); /* TODO */
22c213
+            }
22c213
+            copy_from_iov(&fbuf, out_num, out_sg);
22c213
+            fbuf.size = out_len;
22c213
+
22c213
+            /* TODO! Endianness of header */
22c213
+
22c213
+            /* TODO: Fixup fuse_send_msg */
22c213
+            /* TODO: Add checks for fuse_session_exited */
22c213
+            fuse_session_process_buf_int(se, &fbuf, &ch);
22c213
+
22c213
+            /* TODO: vu_queue_push(dev, q, elem, qi->write_count); */
22c213
+            vu_queue_notify(dev, q);
22c213
+
22c213
+            free(elem);
22c213
+            elem = NULL;
22c213
         }
22c213
     }
22c213
+    pthread_mutex_destroy(&ch.lock);
22c213
+    free(fbuf.mem);
22c213
 
22c213
     return NULL;
22c213
 }
22c213
-- 
22c213
1.8.3.1
22c213