yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From f91a9bdc171142174110e9ff1716b611f6fb0039 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:07 +0100
ddf19c
Subject: [PATCH 036/116] virtiofsd: add --fd=FDNUM fd passing option
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-33-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93487
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 032/112] virtiofsd: add --fd=FDNUM fd passing option
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
Although --socket-path=PATH is useful for manual invocations, management
ddf19c
tools typically create the UNIX domain socket themselves and pass it to
ddf19c
the vhost-user device backend.  This way QEMU can be launched
ddf19c
immediately with a valid socket.  No waiting for the vhost-user device
ddf19c
backend is required when fd passing is used.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit cee8e35d4386e34bf79c3ca2aab7f7b1bb48cf8d)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_i.h        |  1 +
ddf19c
 tools/virtiofsd/fuse_lowlevel.c | 16 ++++++++++++----
ddf19c
 tools/virtiofsd/fuse_virtio.c   | 31 +++++++++++++++++++++++++------
ddf19c
 3 files changed, 38 insertions(+), 10 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
ddf19c
index 1126723..45995f3 100644
ddf19c
--- a/tools/virtiofsd/fuse_i.h
ddf19c
+++ b/tools/virtiofsd/fuse_i.h
ddf19c
@@ -68,6 +68,7 @@ struct fuse_session {
ddf19c
     size_t bufsize;
ddf19c
     int error;
ddf19c
     char *vu_socket_path;
ddf19c
+    int   vu_listen_fd;
ddf19c
     int   vu_socketfd;
ddf19c
     struct fv_VuDev *virtio_dev;
ddf19c
 };
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
index 4f4684d..95f4db8 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.c
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
@@ -2130,6 +2130,7 @@ static const struct fuse_opt fuse_ll_opts[] = {
ddf19c
     LL_OPTION("--debug", debug, 1),
ddf19c
     LL_OPTION("allow_root", deny_others, 1),
ddf19c
     LL_OPTION("--socket-path=%s", vu_socket_path, 0),
ddf19c
+    LL_OPTION("--fd=%d", vu_listen_fd, 0),
ddf19c
     FUSE_OPT_END
ddf19c
 };
ddf19c
 
ddf19c
@@ -2147,7 +2148,8 @@ void fuse_lowlevel_help(void)
ddf19c
      */
ddf19c
     printf(
ddf19c
         "    -o allow_root              allow access by root\n"
ddf19c
-        "    --socket-path=PATH         path for the vhost-user socket\n");
ddf19c
+        "    --socket-path=PATH         path for the vhost-user socket\n"
ddf19c
+        "    --fd=FDNUM                 fd number of vhost-user socket\n");
ddf19c
 }
ddf19c
 
ddf19c
 void fuse_session_destroy(struct fuse_session *se)
ddf19c
@@ -2191,6 +2193,7 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
ddf19c
         goto out1;
ddf19c
     }
ddf19c
     se->fd = -1;
ddf19c
+    se->vu_listen_fd = -1;
ddf19c
     se->conn.max_write = UINT_MAX;
ddf19c
     se->conn.max_readahead = UINT_MAX;
ddf19c
 
ddf19c
@@ -2212,8 +2215,13 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
ddf19c
         goto out4;
ddf19c
     }
ddf19c
 
ddf19c
-    if (!se->vu_socket_path) {
ddf19c
-        fprintf(stderr, "fuse: missing -o vhost_user_socket option\n");
ddf19c
+    if (!se->vu_socket_path && se->vu_listen_fd < 0) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
ddf19c
+        goto out4;
ddf19c
+    }
ddf19c
+    if (se->vu_socket_path && se->vu_listen_fd >= 0) {
ddf19c
+        fuse_log(FUSE_LOG_ERR,
ddf19c
+                 "fuse: --socket-path and --fd cannot be given together\n");
ddf19c
         goto out4;
ddf19c
     }
ddf19c
 
ddf19c
@@ -2253,7 +2261,7 @@ void fuse_session_unmount(struct fuse_session *se)
ddf19c
 
ddf19c
 int fuse_lowlevel_is_virtio(struct fuse_session *se)
ddf19c
 {
ddf19c
-    return se->vu_socket_path != NULL;
ddf19c
+    return !!se->virtio_dev;
ddf19c
 }
ddf19c
 
ddf19c
 #ifdef linux
ddf19c
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
ddf19c
index 7e2711b..635f877 100644
ddf19c
--- a/tools/virtiofsd/fuse_virtio.c
ddf19c
+++ b/tools/virtiofsd/fuse_virtio.c
ddf19c
@@ -638,18 +638,21 @@ int virtio_loop(struct fuse_session *se)
ddf19c
     return 0;
ddf19c
 }
ddf19c
 
ddf19c
-int virtio_session_mount(struct fuse_session *se)
ddf19c
+static int fv_create_listen_socket(struct fuse_session *se)
ddf19c
 {
ddf19c
     struct sockaddr_un un;
ddf19c
     mode_t old_umask;
ddf19c
 
ddf19c
+    /* Nothing to do if fd is already initialized */
ddf19c
+    if (se->vu_listen_fd >= 0) {
ddf19c
+        return 0;
ddf19c
+    }
ddf19c
+
ddf19c
     if (strlen(se->vu_socket_path) >= sizeof(un.sun_path)) {
ddf19c
         fuse_log(FUSE_LOG_ERR, "Socket path too long\n");
ddf19c
         return -1;
ddf19c
     }
ddf19c
 
ddf19c
-    se->fd = -1;
ddf19c
-
ddf19c
     /*
ddf19c
      * Create the Unix socket to communicate with qemu
ddf19c
      * based on QEMU's vhost-user-bridge
ddf19c
@@ -682,15 +685,31 @@ int virtio_session_mount(struct fuse_session *se)
ddf19c
         return -1;
ddf19c
     }
ddf19c
 
ddf19c
+    se->vu_listen_fd = listen_sock;
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
+int virtio_session_mount(struct fuse_session *se)
ddf19c
+{
ddf19c
+    int ret;
ddf19c
+
ddf19c
+    ret = fv_create_listen_socket(se);
ddf19c
+    if (ret < 0) {
ddf19c
+        return ret;
ddf19c
+    }
ddf19c
+
ddf19c
+    se->fd = -1;
ddf19c
+
ddf19c
     fuse_log(FUSE_LOG_INFO, "%s: Waiting for vhost-user socket connection...\n",
ddf19c
              __func__);
ddf19c
-    int data_sock = accept(listen_sock, NULL, NULL);
ddf19c
+    int data_sock = accept(se->vu_listen_fd, NULL, NULL);
ddf19c
     if (data_sock == -1) {
ddf19c
         fuse_log(FUSE_LOG_ERR, "vhost socket accept: %m\n");
ddf19c
-        close(listen_sock);
ddf19c
+        close(se->vu_listen_fd);
ddf19c
         return -1;
ddf19c
     }
ddf19c
-    close(listen_sock);
ddf19c
+    close(se->vu_listen_fd);
ddf19c
+    se->vu_listen_fd = -1;
ddf19c
     fuse_log(FUSE_LOG_INFO, "%s: Received vhost-user socket connection\n",
ddf19c
              __func__);
ddf19c
 
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c