cryptospore / rpms / qemu-kvm

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