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

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