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