peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0033-virtiofsd-Start-reading-commands-from-queue.patch

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