76daa3
From f2ef472de8d94edcb42b419116611d3ffc51d099 Mon Sep 17 00:00:00 2001
76daa3
From: Maxime Coquelin <maxime.coquelin@redhat.com>
76daa3
Date: Tue, 6 Jun 2017 12:05:23 +0200
76daa3
Subject: [PATCH 12/17] vhost-user: add slave-req-fd support
76daa3
MIME-Version: 1.0
76daa3
Content-Type: text/plain; charset=UTF-8
76daa3
Content-Transfer-Encoding: 8bit
76daa3
76daa3
RH-Author: Maxime Coquelin <maxime.coquelin@redhat.com>
76daa3
Message-id: <20170606120524.3050-5-maxime.coquelin@redhat.com>
76daa3
Patchwork-id: 75500
76daa3
O-Subject: [RHV-7.4 qemu-kvm-rhev PATCH 4/5] vhost-user: add slave-req-fd support
76daa3
Bugzilla: 1451862
76daa3
RH-Acked-by: Peter Xu <peterx@redhat.com>
76daa3
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
76daa3
RH-Acked-by: Jens Freimann <jfreiman@redhat.com>
76daa3
76daa3
From: Marc-André Lureau <marcandre.lureau@redhat.com>
76daa3
76daa3
Learn to give a socket to the slave to let him make requests to the
76daa3
master.
76daa3
76daa3
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
76daa3
Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
76daa3
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
76daa3
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
76daa3
(cherry picked from commit 4bbeeba023f22c117d7a4c561354b91a0bf62e42)
76daa3
Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
76daa3
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
---
76daa3
 docs/specs/vhost-user.txt |  32 +++++++++++-
76daa3
 hw/virtio/vhost-user.c    | 127 ++++++++++++++++++++++++++++++++++++++++++++++
76daa3
 2 files changed, 157 insertions(+), 2 deletions(-)
76daa3
76daa3
diff --git a/docs/specs/vhost-user.txt b/docs/specs/vhost-user.txt
76daa3
index 036890f..5fa7016 100644
76daa3
--- a/docs/specs/vhost-user.txt
76daa3
+++ b/docs/specs/vhost-user.txt
76daa3
@@ -139,6 +139,7 @@ in the ancillary data:
76daa3
  * VHOST_USER_SET_VRING_KICK
76daa3
  * VHOST_USER_SET_VRING_CALL
76daa3
  * VHOST_USER_SET_VRING_ERR
76daa3
+ * VHOST_USER_SET_SLAVE_REQ_FD
76daa3
 
76daa3
 If Master is unable to send the full message or receives a wrong reply it will
76daa3
 close the connection. An optional reconnection mechanism can be implemented.
76daa3
@@ -252,6 +253,18 @@ Once the source has finished migration, rings will be stopped by
76daa3
 the source. No further update must be done before rings are
76daa3
 restarted.
76daa3
 
76daa3
+Slave communication
76daa3
+-------------------
76daa3
+
76daa3
+An optional communication channel is provided if the slave declares
76daa3
+VHOST_USER_PROTOCOL_F_SLAVE_REQ protocol feature, to allow the slave to make
76daa3
+requests to the master.
76daa3
+
76daa3
+The fd is provided via VHOST_USER_SET_SLAVE_REQ_FD ancillary data.
76daa3
+
76daa3
+A slave may then send VHOST_USER_SLAVE_* messages to the master
76daa3
+using this fd communication channel.
76daa3
+
76daa3
 Protocol features
76daa3
 -----------------
76daa3
 
76daa3
@@ -260,9 +273,10 @@ Protocol features
76daa3
 #define VHOST_USER_PROTOCOL_F_RARP           2
76daa3
 #define VHOST_USER_PROTOCOL_F_REPLY_ACK      3
76daa3
 #define VHOST_USER_PROTOCOL_F_MTU            4
76daa3
+#define VHOST_USER_PROTOCOL_F_SLAVE_REQ      5
76daa3
 
76daa3
-Message types
76daa3
--------------
76daa3
+Master message types
76daa3
+--------------------
76daa3
 
76daa3
  * VHOST_USER_GET_FEATURES
76daa3
 
76daa3
@@ -486,6 +500,20 @@ Message types
76daa3
       If VHOST_USER_PROTOCOL_F_REPLY_ACK is negotiated, slave must respond
76daa3
       with zero in case the specified MTU is valid, or non-zero otherwise.
76daa3
 
76daa3
+ * VHOST_USER_SET_SLAVE_REQ_FD
76daa3
+
76daa3
+      Id: 21
76daa3
+      Equivalent ioctl: N/A
76daa3
+      Master payload: N/A
76daa3
+
76daa3
+      Set the socket file descriptor for slave initiated requests. It is passed
76daa3
+      in the ancillary data.
76daa3
+      This request should be sent only when VHOST_USER_F_PROTOCOL_FEATURES
76daa3
+      has been negotiated, and protocol feature bit VHOST_USER_PROTOCOL_F_SLAVE_REQ
76daa3
+      bit is present in VHOST_USER_GET_PROTOCOL_FEATURES.
76daa3
+      If VHOST_USER_PROTOCOL_F_REPLY_ACK is negotiated, slave must respond
76daa3
+      with zero for success, non-zero otherwise.
76daa3
+
76daa3
 VHOST_USER_PROTOCOL_F_REPLY_ACK:
76daa3
 -------------------------------
76daa3
 The original vhost-user specification only demands replies for certain
76daa3
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
76daa3
index 325319f..0faf5df 100644
76daa3
--- a/hw/virtio/vhost-user.c
76daa3
+++ b/hw/virtio/vhost-user.c
76daa3
@@ -33,6 +33,7 @@ enum VhostUserProtocolFeature {
76daa3
     VHOST_USER_PROTOCOL_F_RARP = 2,
76daa3
     VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
76daa3
     VHOST_USER_PROTOCOL_F_NET_MTU = 4,
76daa3
+    VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5,
76daa3
 
76daa3
     VHOST_USER_PROTOCOL_F_MAX
76daa3
 };
76daa3
@@ -61,9 +62,15 @@ typedef enum VhostUserRequest {
76daa3
     VHOST_USER_SET_VRING_ENABLE = 18,
76daa3
     VHOST_USER_SEND_RARP = 19,
76daa3
     VHOST_USER_NET_SET_MTU = 20,
76daa3
+    VHOST_USER_SET_SLAVE_REQ_FD = 21,
76daa3
     VHOST_USER_MAX
76daa3
 } VhostUserRequest;
76daa3
 
76daa3
+typedef enum VhostUserSlaveRequest {
76daa3
+    VHOST_USER_SLAVE_NONE = 0,
76daa3
+    VHOST_USER_SLAVE_MAX
76daa3
+}  VhostUserSlaveRequest;
76daa3
+
76daa3
 typedef struct VhostUserMemoryRegion {
76daa3
     uint64_t guest_phys_addr;
76daa3
     uint64_t memory_size;
76daa3
@@ -113,6 +120,7 @@ static VhostUserMsg m __attribute__ ((unused));
76daa3
 
76daa3
 struct vhost_user {
76daa3
     CharBackend *chr;
76daa3
+    int slave_fd;
76daa3
 };
76daa3
 
76daa3
 static bool ioeventfd_enabled(void)
76daa3
@@ -579,6 +587,115 @@ static int vhost_user_reset_device(struct vhost_dev *dev)
76daa3
     return 0;
76daa3
 }
76daa3
 
76daa3
+static void slave_read(void *opaque)
76daa3
+{
76daa3
+    struct vhost_dev *dev = opaque;
76daa3
+    struct vhost_user *u = dev->opaque;
76daa3
+    VhostUserMsg msg = { 0, };
76daa3
+    int size, ret = 0;
76daa3
+
76daa3
+    /* Read header */
76daa3
+    size = read(u->slave_fd, &msg, VHOST_USER_HDR_SIZE);
76daa3
+    if (size != VHOST_USER_HDR_SIZE) {
76daa3
+        error_report("Failed to read from slave.");
76daa3
+        goto err;
76daa3
+    }
76daa3
+
76daa3
+    if (msg.size > VHOST_USER_PAYLOAD_SIZE) {
76daa3
+        error_report("Failed to read msg header."
76daa3
+                " Size %d exceeds the maximum %zu.", msg.size,
76daa3
+                VHOST_USER_PAYLOAD_SIZE);
76daa3
+        goto err;
76daa3
+    }
76daa3
+
76daa3
+    /* Read payload */
76daa3
+    size = read(u->slave_fd, &msg.payload, msg.size);
76daa3
+    if (size != msg.size) {
76daa3
+        error_report("Failed to read payload from slave.");
76daa3
+        goto err;
76daa3
+    }
76daa3
+
76daa3
+    switch (msg.request) {
76daa3
+    default:
76daa3
+        error_report("Received unexpected msg type.");
76daa3
+        ret = -EINVAL;
76daa3
+    }
76daa3
+
76daa3
+    /*
76daa3
+     * REPLY_ACK feature handling. Other reply types has to be managed
76daa3
+     * directly in their request handlers.
76daa3
+     */
76daa3
+    if (msg.flags & VHOST_USER_NEED_REPLY_MASK) {
76daa3
+        msg.flags &= ~VHOST_USER_NEED_REPLY_MASK;
76daa3
+        msg.flags |= VHOST_USER_REPLY_MASK;
76daa3
+
76daa3
+        msg.payload.u64 = !!ret;
76daa3
+        msg.size = sizeof(msg.payload.u64);
76daa3
+
76daa3
+        size = write(u->slave_fd, &msg, VHOST_USER_HDR_SIZE + msg.size);
76daa3
+        if (size != VHOST_USER_HDR_SIZE + msg.size) {
76daa3
+            error_report("Failed to send msg reply to slave.");
76daa3
+            goto err;
76daa3
+        }
76daa3
+    }
76daa3
+
76daa3
+    return;
76daa3
+
76daa3
+err:
76daa3
+    qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
76daa3
+    close(u->slave_fd);
76daa3
+    u->slave_fd = -1;
76daa3
+    return;
76daa3
+}
76daa3
+
76daa3
+static int vhost_setup_slave_channel(struct vhost_dev *dev)
76daa3
+{
76daa3
+    VhostUserMsg msg = {
76daa3
+        .request = VHOST_USER_SET_SLAVE_REQ_FD,
76daa3
+        .flags = VHOST_USER_VERSION,
76daa3
+    };
76daa3
+    struct vhost_user *u = dev->opaque;
76daa3
+    int sv[2], ret = 0;
76daa3
+    bool reply_supported = virtio_has_feature(dev->protocol_features,
76daa3
+                                              VHOST_USER_PROTOCOL_F_REPLY_ACK);
76daa3
+
76daa3
+    if (!virtio_has_feature(dev->protocol_features,
76daa3
+                            VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
76daa3
+        return 0;
76daa3
+    }
76daa3
+
76daa3
+    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
76daa3
+        error_report("socketpair() failed");
76daa3
+        return -1;
76daa3
+    }
76daa3
+
76daa3
+    u->slave_fd = sv[0];
76daa3
+    qemu_set_fd_handler(u->slave_fd, slave_read, NULL, dev);
76daa3
+
76daa3
+    if (reply_supported) {
76daa3
+        msg.flags |= VHOST_USER_NEED_REPLY_MASK;
76daa3
+    }
76daa3
+
76daa3
+    ret = vhost_user_write(dev, &msg, &sv[1], 1);
76daa3
+    if (ret) {
76daa3
+        goto out;
76daa3
+    }
76daa3
+
76daa3
+    if (reply_supported) {
76daa3
+        ret = process_message_reply(dev, &msg;;
76daa3
+    }
76daa3
+
76daa3
+out:
76daa3
+    close(sv[1]);
76daa3
+    if (ret) {
76daa3
+        qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
76daa3
+        close(u->slave_fd);
76daa3
+        u->slave_fd = -1;
76daa3
+    }
76daa3
+
76daa3
+    return ret;
76daa3
+}
76daa3
+
76daa3
 static int vhost_user_init(struct vhost_dev *dev, void *opaque)
76daa3
 {
76daa3
     uint64_t features;
76daa3
@@ -589,6 +706,7 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
76daa3
 
76daa3
     u = g_new0(struct vhost_user, 1);
76daa3
     u->chr = opaque;
76daa3
+    u->slave_fd = -1;
76daa3
     dev->opaque = u;
76daa3
 
76daa3
     err = vhost_user_get_features(dev, &features);
76daa3
@@ -629,6 +747,11 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
76daa3
                    "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
76daa3
     }
76daa3
 
76daa3
+    err = vhost_setup_slave_channel(dev);
76daa3
+    if (err < 0) {
76daa3
+        return err;
76daa3
+    }
76daa3
+
76daa3
     return 0;
76daa3
 }
76daa3
 
76daa3
@@ -639,6 +762,10 @@ static int vhost_user_cleanup(struct vhost_dev *dev)
76daa3
     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
76daa3
 
76daa3
     u = dev->opaque;
76daa3
+    if (u->slave_fd >= 0) {
76daa3
+        close(u->slave_fd);
76daa3
+        u->slave_fd = -1;
76daa3
+    }
76daa3
     g_free(u);
76daa3
     dev->opaque = 0;
76daa3
 
76daa3
-- 
76daa3
1.8.3.1
76daa3