Blame SOURCES/kvm-libvhost-user-support-host-notifier.patch

357786
From bf38c0c9b897040dddc38456f731006c817e3c39 Mon Sep 17 00:00:00 2001
357786
From: "plai@redhat.com" <plai@redhat.com>
357786
Date: Thu, 21 Jun 2018 18:54:45 +0200
357786
Subject: [PATCH 36/57] libvhost-user: support host notifier
357786
357786
RH-Author: plai@redhat.com
357786
Message-id: <1529607285-9942-11-git-send-email-plai@redhat.com>
357786
Patchwork-id: 80939
357786
O-Subject: [RHEL7.6 PATCH BZ 1526645 10/10] libvhost-user: support host notifier
357786
Bugzilla: 1526645
357786
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
357786
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
357786
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
357786
357786
From: Tiwei Bie <tiwei.bie@intel.com>
357786
357786
This patch introduces the host notifier support in
357786
libvhost-user. A new API is added to support setting
357786
host notifier for each queue.
357786
357786
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
357786
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
357786
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
357786
(cherry picked from commit d84599f56c820d8c1ac9928a76500dcdfbbf194d)
357786
Signed-off-by: Paul Lai <plai@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 contrib/libvhost-user/libvhost-user.c | 81 +++++++++++++++++++++++++++++++----
357786
 contrib/libvhost-user/libvhost-user.h | 32 ++++++++++++++
357786
 2 files changed, 105 insertions(+), 8 deletions(-)
357786
357786
diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c
357786
index beeed0c..fa06eee 100644
357786
--- a/contrib/libvhost-user/libvhost-user.c
357786
+++ b/contrib/libvhost-user/libvhost-user.c
357786
@@ -314,11 +314,6 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
357786
         msg.msg_controllen = 0;
357786
     }
357786
 
357786
-    /* Set the version in the flags when sending the reply */
357786
-    vmsg->flags &= ~VHOST_USER_VERSION_MASK;
357786
-    vmsg->flags |= VHOST_USER_VERSION;
357786
-    vmsg->flags |= VHOST_USER_REPLY_MASK;
357786
-
357786
     do {
357786
         rc = sendmsg(conn_fd, &msg, 0);
357786
     } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
357786
@@ -339,6 +334,39 @@ vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
357786
     return true;
357786
 }
357786
 
357786
+static bool
357786
+vu_send_reply(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
357786
+{
357786
+    /* Set the version in the flags when sending the reply */
357786
+    vmsg->flags &= ~VHOST_USER_VERSION_MASK;
357786
+    vmsg->flags |= VHOST_USER_VERSION;
357786
+    vmsg->flags |= VHOST_USER_REPLY_MASK;
357786
+
357786
+    return vu_message_write(dev, conn_fd, vmsg);
357786
+}
357786
+
357786
+static bool
357786
+vu_process_message_reply(VuDev *dev, const VhostUserMsg *vmsg)
357786
+{
357786
+    VhostUserMsg msg_reply;
357786
+
357786
+    if ((vmsg->flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
357786
+        return true;
357786
+    }
357786
+
357786
+    if (!vu_message_read(dev, dev->slave_fd, &msg_reply)) {
357786
+        return false;
357786
+    }
357786
+
357786
+    if (msg_reply.request != vmsg->request) {
357786
+        DPRINT("Received unexpected msg type. Expected %d received %d",
357786
+               vmsg->request, msg_reply.request);
357786
+        return false;
357786
+    }
357786
+
357786
+    return msg_reply.payload.u64 == 0;
357786
+}
357786
+
357786
 /* Kick the log_call_fd if required. */
357786
 static void
357786
 vu_log_kick(VuDev *dev)
357786
@@ -534,7 +562,7 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
357786
 
357786
     /* Send the message back to qemu with the addresses filled in */
357786
     vmsg->fd_num = 0;
357786
-    if (!vu_message_write(dev, dev->sock, vmsg)) {
357786
+    if (!vu_send_reply(dev, dev->sock, vmsg)) {
357786
         vu_panic(dev, "failed to respond to set-mem-table for postcopy");
357786
         return false;
357786
     }
357786
@@ -914,6 +942,41 @@ void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
357786
     }
357786
 }
357786
 
357786
+bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
357786
+                                int size, int offset)
357786
+{
357786
+    int qidx = vq - dev->vq;
357786
+    int fd_num = 0;
357786
+    VhostUserMsg vmsg = {
357786
+        .request = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
357786
+        .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
357786
+        .size = sizeof(vmsg.payload.area),
357786
+        .payload.area = {
357786
+            .u64 = qidx & VHOST_USER_VRING_IDX_MASK,
357786
+            .size = size,
357786
+            .offset = offset,
357786
+        },
357786
+    };
357786
+
357786
+    if (fd == -1) {
357786
+        vmsg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
357786
+    } else {
357786
+        vmsg.fds[fd_num++] = fd;
357786
+    }
357786
+
357786
+    vmsg.fd_num = fd_num;
357786
+
357786
+    if ((dev->protocol_features & VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) == 0) {
357786
+        return false;
357786
+    }
357786
+
357786
+    if (!vu_message_write(dev, dev->slave_fd, &vmsg)) {
357786
+        return false;
357786
+    }
357786
+
357786
+    return vu_process_message_reply(dev, &vmsg);
357786
+}
357786
+
357786
 static bool
357786
 vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg)
357786
 {
357786
@@ -966,7 +1029,9 @@ static bool
357786
 vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
357786
 {
357786
     uint64_t features = 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD |
357786
-                        1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ;
357786
+                        1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ |
357786
+                        1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER |
357786
+                        1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD;
357786
 
357786
     if (have_userfault()) {
357786
         features |= 1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT;
357786
@@ -1250,7 +1315,7 @@ vu_dispatch(VuDev *dev)
357786
         goto end;
357786
     }
357786
 
357786
-    if (!vu_message_write(dev, dev->sock, &vmsg)) {
357786
+    if (!vu_send_reply(dev, dev->sock, &vmsg)) {
357786
         goto end;
357786
     }
357786
 
357786
diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/libvhost-user.h
357786
index b27075e..4aa55b4 100644
357786
--- a/contrib/libvhost-user/libvhost-user.h
357786
+++ b/contrib/libvhost-user/libvhost-user.h
357786
@@ -51,6 +51,8 @@ enum VhostUserProtocolFeature {
357786
     VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
357786
     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
357786
     VHOST_USER_PROTOCOL_F_CONFIG = 9,
357786
+    VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
357786
+    VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
357786
 
357786
     VHOST_USER_PROTOCOL_F_MAX
357786
 };
357786
@@ -92,6 +94,14 @@ typedef enum VhostUserRequest {
357786
     VHOST_USER_MAX
357786
 } VhostUserRequest;
357786
 
357786
+typedef enum VhostUserSlaveRequest {
357786
+    VHOST_USER_SLAVE_NONE = 0,
357786
+    VHOST_USER_SLAVE_IOTLB_MSG = 1,
357786
+    VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
357786
+    VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
357786
+    VHOST_USER_SLAVE_MAX
357786
+}  VhostUserSlaveRequest;
357786
+
357786
 typedef struct VhostUserMemoryRegion {
357786
     uint64_t guest_phys_addr;
357786
     uint64_t memory_size;
357786
@@ -122,6 +132,12 @@ static VhostUserConfig c __attribute__ ((unused));
357786
                                    + sizeof(c.size) \
357786
                                    + sizeof(c.flags))
357786
 
357786
+typedef struct VhostUserVringArea {
357786
+    uint64_t u64;
357786
+    uint64_t size;
357786
+    uint64_t offset;
357786
+} VhostUserVringArea;
357786
+
357786
 #if defined(_WIN32)
357786
 # define VU_PACKED __attribute__((gcc_struct, packed))
357786
 #else
357786
@@ -133,6 +149,7 @@ typedef struct VhostUserMsg {
357786
 
357786
 #define VHOST_USER_VERSION_MASK     (0x3)
357786
 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
357786
+#define VHOST_USER_NEED_REPLY_MASK  (0x1 << 3)
357786
     uint32_t flags;
357786
     uint32_t size; /* the following payload size */
357786
 
357786
@@ -145,6 +162,7 @@ typedef struct VhostUserMsg {
357786
         VhostUserMemory memory;
357786
         VhostUserLog log;
357786
         VhostUserConfig config;
357786
+        VhostUserVringArea area;
357786
     } payload;
357786
 
357786
     int fds[VHOST_MEMORY_MAX_NREGIONS];
357786
@@ -368,6 +386,20 @@ VuVirtq *vu_get_queue(VuDev *dev, int qidx);
357786
 void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
357786
                           vu_queue_handler_cb handler);
357786
 
357786
+/**
357786
+ * vu_set_queue_host_notifier:
357786
+ * @dev: a VuDev context
357786
+ * @vq: a VuVirtq queue
357786
+ * @fd: a file descriptor
357786
+ * @size: host page size
357786
+ * @offset: notifier offset in @fd file
357786
+ *
357786
+ * Set queue's host notifier. This function may be called several
357786
+ * times for the same queue. If called with -1 @fd, the notifier
357786
+ * is removed.
357786
+ */
357786
+bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
357786
+                                int size, int offset);
357786
 
357786
 /**
357786
  * vu_queue_set_notification:
357786
-- 
357786
1.8.3.1
357786