Blame SOURCES/kvm-vhost-user-allow-slave-to-send-fds-via-slave-channel.patch

383d26
From 4395bef7b50eba7711d47a5a9064ef048cdee8d2 Mon Sep 17 00:00:00 2001
383d26
From: "plai@redhat.com" <plai@redhat.com>
383d26
Date: Thu, 21 Jun 2018 18:54:42 +0200
383d26
Subject: [PATCH 33/57] vhost-user: allow slave to send fds via slave channel
383d26
383d26
RH-Author: plai@redhat.com
383d26
Message-id: <1529607285-9942-8-git-send-email-plai@redhat.com>
383d26
Patchwork-id: 80935
383d26
O-Subject: [RHEL7.6 PATCH BZ 1526645 07/10] vhost-user: allow slave to send fds via slave channel
383d26
Bugzilla: 1526645
383d26
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
383d26
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
383d26
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
383d26
383d26
From: Tiwei Bie <tiwei.bie@intel.com>
383d26
383d26
Introduce VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD protocol
383d26
feature to allow slave to send at most 8 descriptors
383d26
in each message to master via ancillary data using the
383d26
slave channel.
383d26
383d26
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
383d26
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
383d26
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
383d26
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
383d26
(cherry picked from commit 5f57fbeaaf7c4cd33152d7f2e449caab4d4209d9)
383d26
Signed-off-by: Paul Lai <plai@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 docs/interop/vhost-user.txt |  5 +++++
383d26
 hw/virtio/vhost-user.c      | 27 +++++++++++++++++----------
383d26
 2 files changed, 22 insertions(+), 10 deletions(-)
383d26
383d26
diff --git a/docs/interop/vhost-user.txt b/docs/interop/vhost-user.txt
383d26
index 534caab..682a683 100644
383d26
--- a/docs/interop/vhost-user.txt
383d26
+++ b/docs/interop/vhost-user.txt
383d26
@@ -367,6 +367,10 @@ The fd is provided via VHOST_USER_SET_SLAVE_REQ_FD ancillary data.
383d26
 A slave may then send VHOST_USER_SLAVE_* messages to the master
383d26
 using this fd communication channel.
383d26
 
383d26
+If VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD protocol feature is negotiated,
383d26
+slave can send file descriptors (at most 8 descriptors in each message)
383d26
+to master via ancillary data using this fd communication channel.
383d26
+
383d26
 Protocol features
383d26
 -----------------
383d26
 
383d26
@@ -380,6 +384,7 @@ Protocol features
383d26
 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
383d26
 #define VHOST_USER_PROTOCOL_F_PAGEFAULT      8
383d26
 #define VHOST_USER_PROTOCOL_F_CONFIG         9
383d26
+#define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD  10
383d26
 
383d26
 Master message types
383d26
 --------------------
383d26
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
383d26
index ebb946a..e8027ad 100644
383d26
--- a/hw/virtio/vhost-user.c
383d26
+++ b/hw/virtio/vhost-user.c
383d26
@@ -30,6 +30,7 @@
383d26
 
383d26
 #define VHOST_MEMORY_MAX_NREGIONS    8
383d26
 #define VHOST_USER_F_PROTOCOL_FEATURES 30
383d26
+#define VHOST_USER_SLAVE_MAX_FDS     8
383d26
 
383d26
 /*
383d26
  * Maximum size of virtio device config space
383d26
@@ -47,6 +48,7 @@ enum VhostUserProtocolFeature {
383d26
     VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
383d26
     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
383d26
     VHOST_USER_PROTOCOL_F_CONFIG = 9,
383d26
+    VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
383d26
     VHOST_USER_PROTOCOL_F_MAX
383d26
 };
383d26
 
383d26
@@ -854,10 +856,10 @@ static void slave_read(void *opaque)
383d26
     int size, ret = 0;
383d26
     struct iovec iov;
383d26
     struct msghdr msgh;
383d26
-    int fd = -1;
383d26
+    int fd[VHOST_USER_SLAVE_MAX_FDS];
383d26
     char control[CMSG_SPACE(sizeof(fd))];
383d26
     struct cmsghdr *cmsg;
383d26
-    size_t fdsize;
383d26
+    int i, fdsize = 0;
383d26
 
383d26
     memset(&msgh, 0, sizeof(msgh));
383d26
     msgh.msg_iov = &iov;
383d26
@@ -865,6 +867,8 @@ static void slave_read(void *opaque)
383d26
     msgh.msg_control = control;
383d26
     msgh.msg_controllen = sizeof(control);
383d26
 
383d26
+    memset(fd, -1, sizeof(fd));
383d26
+
383d26
     /* Read header */
383d26
     iov.iov_base = &hd;;
383d26
     iov.iov_len = VHOST_USER_HDR_SIZE;
383d26
@@ -885,7 +889,7 @@ static void slave_read(void *opaque)
383d26
             if (cmsg->cmsg_level == SOL_SOCKET &&
383d26
                 cmsg->cmsg_type == SCM_RIGHTS) {
383d26
                     fdsize = cmsg->cmsg_len - CMSG_LEN(0);
383d26
-                    memcpy(&fd, CMSG_DATA(cmsg), fdsize);
383d26
+                    memcpy(fd, CMSG_DATA(cmsg), fdsize);
383d26
                     break;
383d26
             }
383d26
     }
383d26
@@ -913,14 +917,15 @@ static void slave_read(void *opaque)
383d26
         break;
383d26
     default:
383d26
         error_report("Received unexpected msg type.");
383d26
-        if (fd != -1) {
383d26
-            close(fd);
383d26
-        }
383d26
         ret = -EINVAL;
383d26
     }
383d26
 
383d26
-    /* Message handlers need to make sure that fd will be consumed. */
383d26
-    fd = -1;
383d26
+    /* Close the remaining file descriptors. */
383d26
+    for (i = 0; i < fdsize; i++) {
383d26
+        if (fd[i] != -1) {
383d26
+            close(fd[i]);
383d26
+        }
383d26
+    }
383d26
 
383d26
     /*
383d26
      * REPLY_ACK feature handling. Other reply types has to be managed
383d26
@@ -954,8 +959,10 @@ err:
383d26
     qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
383d26
     close(u->slave_fd);
383d26
     u->slave_fd = -1;
383d26
-    if (fd != -1) {
383d26
-        close(fd);
383d26
+    for (i = 0; i < fdsize; i++) {
383d26
+        if (fd[i] != -1) {
383d26
+            close(fd[i]);
383d26
+        }
383d26
     }
383d26
     return;
383d26
 }
383d26
-- 
383d26
1.8.3.1
383d26