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

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