thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ddf19c
From bb1f691dc410ce11ac9675ced70e78a3ce2511b0 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:03 +0100
ddf19c
Subject: [PATCH 032/116] virtiofsd: Send replies to messages
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-29-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93485
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 028/112] virtiofsd: Send replies to messages
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
Route fuse out messages back through the same queue elements
ddf19c
that had the command that triggered the request.
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 df57ba919ec3edef9cc208d35685095e6e92713e)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_lowlevel.c |   4 ++
ddf19c
 tools/virtiofsd/fuse_virtio.c   | 107 ++++++++++++++++++++++++++++++++++++++--
ddf19c
 tools/virtiofsd/fuse_virtio.h   |   4 ++
ddf19c
 3 files changed, 111 insertions(+), 4 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
index af09fa2..380d93b 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.c
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
@@ -171,6 +171,10 @@ static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
ddf19c
         }
ddf19c
     }
ddf19c
 
ddf19c
+    if (fuse_lowlevel_is_virtio(se)) {
ddf19c
+        return virtio_send_msg(se, ch, iov, count);
ddf19c
+    }
ddf19c
+
ddf19c
     abort(); /* virtio should have taken it before here */
ddf19c
     return 0;
ddf19c
 }
ddf19c
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
ddf19c
index 3841b20..05d0e29 100644
ddf19c
--- a/tools/virtiofsd/fuse_virtio.c
ddf19c
+++ b/tools/virtiofsd/fuse_virtio.c
ddf19c
@@ -41,6 +41,9 @@ struct fv_QueueInfo {
ddf19c
     /* Our queue index, corresponds to array position */
ddf19c
     int qidx;
ddf19c
     int kick_fd;
ddf19c
+
ddf19c
+    /* The element for the command currently being processed */
ddf19c
+    VuVirtqElement *qe;
ddf19c
 };
ddf19c
 
ddf19c
 /*
ddf19c
@@ -121,6 +124,105 @@ static void copy_from_iov(struct fuse_buf *buf, size_t out_num,
ddf19c
     }
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * Copy from one iov to another, the given number of bytes
ddf19c
+ * The caller must have checked sizes.
ddf19c
+ */
ddf19c
+static void copy_iov(struct iovec *src_iov, int src_count,
ddf19c
+                     struct iovec *dst_iov, int dst_count, size_t to_copy)
ddf19c
+{
ddf19c
+    size_t dst_offset = 0;
ddf19c
+    /* Outer loop copies 'src' elements */
ddf19c
+    while (to_copy) {
ddf19c
+        assert(src_count);
ddf19c
+        size_t src_len = src_iov[0].iov_len;
ddf19c
+        size_t src_offset = 0;
ddf19c
+
ddf19c
+        if (src_len > to_copy) {
ddf19c
+            src_len = to_copy;
ddf19c
+        }
ddf19c
+        /* Inner loop copies contents of one 'src' to maybe multiple dst. */
ddf19c
+        while (src_len) {
ddf19c
+            assert(dst_count);
ddf19c
+            size_t dst_len = dst_iov[0].iov_len - dst_offset;
ddf19c
+            if (dst_len > src_len) {
ddf19c
+                dst_len = src_len;
ddf19c
+            }
ddf19c
+
ddf19c
+            memcpy(dst_iov[0].iov_base + dst_offset,
ddf19c
+                   src_iov[0].iov_base + src_offset, dst_len);
ddf19c
+            src_len -= dst_len;
ddf19c
+            to_copy -= dst_len;
ddf19c
+            src_offset += dst_len;
ddf19c
+            dst_offset += dst_len;
ddf19c
+
ddf19c
+            assert(dst_offset <= dst_iov[0].iov_len);
ddf19c
+            if (dst_offset == dst_iov[0].iov_len) {
ddf19c
+                dst_offset = 0;
ddf19c
+                dst_iov++;
ddf19c
+                dst_count--;
ddf19c
+            }
ddf19c
+        }
ddf19c
+        src_iov++;
ddf19c
+        src_count--;
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+/*
ddf19c
+ * Called back by ll whenever it wants to send a reply/message back
ddf19c
+ * The 1st element of the iov starts with the fuse_out_header
ddf19c
+ * 'unique'==0 means it's a notify message.
ddf19c
+ */
ddf19c
+int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch,
ddf19c
+                    struct iovec *iov, int count)
ddf19c
+{
ddf19c
+    VuVirtqElement *elem;
ddf19c
+    VuVirtq *q;
ddf19c
+
ddf19c
+    assert(count >= 1);
ddf19c
+    assert(iov[0].iov_len >= sizeof(struct fuse_out_header));
ddf19c
+
ddf19c
+    struct fuse_out_header *out = iov[0].iov_base;
ddf19c
+    /* TODO: Endianness! */
ddf19c
+
ddf19c
+    size_t tosend_len = iov_size(iov, count);
ddf19c
+
ddf19c
+    /* unique == 0 is notification, which we don't support */
ddf19c
+    assert(out->unique);
ddf19c
+    /* For virtio we always have ch */
ddf19c
+    assert(ch);
ddf19c
+    elem = ch->qi->qe;
ddf19c
+    q = &ch->qi->virtio_dev->dev.vq[ch->qi->qidx];
ddf19c
+
ddf19c
+    /* The 'in' part of the elem is to qemu */
ddf19c
+    unsigned int in_num = elem->in_num;
ddf19c
+    struct iovec *in_sg = elem->in_sg;
ddf19c
+    size_t in_len = iov_size(in_sg, in_num);
ddf19c
+    fuse_log(FUSE_LOG_DEBUG, "%s: elem %d: with %d in desc of length %zd\n",
ddf19c
+             __func__, elem->index, in_num, in_len);
ddf19c
+
ddf19c
+    /*
ddf19c
+     * The elem should have room for a 'fuse_out_header' (out from fuse)
ddf19c
+     * plus the data based on the len in the header.
ddf19c
+     */
ddf19c
+    if (in_len < sizeof(struct fuse_out_header)) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for out_header\n",
ddf19c
+                 __func__, elem->index);
ddf19c
+        return -E2BIG;
ddf19c
+    }
ddf19c
+    if (in_len < tosend_len) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len %zd\n",
ddf19c
+                 __func__, elem->index, tosend_len);
ddf19c
+        return -E2BIG;
ddf19c
+    }
ddf19c
+
ddf19c
+    copy_iov(iov, count, in_sg, in_num, tosend_len);
ddf19c
+    vu_queue_push(&se->virtio_dev->dev, q, elem, tosend_len);
ddf19c
+    vu_queue_notify(&se->virtio_dev->dev, q);
ddf19c
+
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
 /* Thread function for individual queues, created when a queue is 'started' */
ddf19c
 static void *fv_queue_thread(void *opaque)
ddf19c
 {
ddf19c
@@ -226,13 +328,10 @@ static void *fv_queue_thread(void *opaque)
ddf19c
 
ddf19c
             /* TODO! Endianness of header */
ddf19c
 
ddf19c
-            /* TODO: Fixup fuse_send_msg */
ddf19c
             /* TODO: Add checks for fuse_session_exited */
ddf19c
             fuse_session_process_buf_int(se, &fbuf, &ch);
ddf19c
 
ddf19c
-            /* TODO: vu_queue_push(dev, q, elem, qi->write_count); */
ddf19c
-            vu_queue_notify(dev, q);
ddf19c
-
ddf19c
+            qi->qe = NULL;
ddf19c
             free(elem);
ddf19c
             elem = NULL;
ddf19c
         }
ddf19c
diff --git a/tools/virtiofsd/fuse_virtio.h b/tools/virtiofsd/fuse_virtio.h
ddf19c
index 23026d6..135a148 100644
ddf19c
--- a/tools/virtiofsd/fuse_virtio.h
ddf19c
+++ b/tools/virtiofsd/fuse_virtio.h
ddf19c
@@ -22,4 +22,8 @@ int virtio_session_mount(struct fuse_session *se);
ddf19c
 
ddf19c
 int virtio_loop(struct fuse_session *se);
ddf19c
 
ddf19c
+
ddf19c
+int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch,
ddf19c
+                    struct iovec *iov, int count);
ddf19c
+
ddf19c
 #endif
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c