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

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