yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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