22c213
From 6f413d8b76ff38e5bc01f36515ca71d7fd6e6144 Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:00:58 +0100
22c213
Subject: [PATCH 027/116] virtiofsd: Add main virtio loop
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200127190227.40942-24-dgilbert@redhat.com>
22c213
Patchwork-id: 93475
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 023/112] virtiofsd: Add main virtio loop
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
22c213
Processes incoming requests on the vhost-user fd.
22c213
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 204d8ae57b3c57098642c79b3c03d42495149c09)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/fuse_virtio.c | 42 +++++++++++++++++++++++++++++++++++++++---
22c213
 1 file changed, 39 insertions(+), 3 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
22c213
index 2ae3c76..1928a20 100644
22c213
--- a/tools/virtiofsd/fuse_virtio.c
22c213
+++ b/tools/virtiofsd/fuse_virtio.c
22c213
@@ -11,12 +11,14 @@
22c213
  * See the file COPYING.LIB
22c213
  */
22c213
 
22c213
+#include "fuse_virtio.h"
22c213
 #include "fuse_i.h"
22c213
 #include "standard-headers/linux/fuse.h"
22c213
 #include "fuse_misc.h"
22c213
 #include "fuse_opt.h"
22c213
-#include "fuse_virtio.h"
22c213
 
22c213
+#include <assert.h>
22c213
+#include <errno.h>
22c213
 #include <stdint.h>
22c213
 #include <stdio.h>
22c213
 #include <stdlib.h>
22c213
@@ -80,15 +82,49 @@ static const VuDevIface fv_iface = {
22c213
     .queue_is_processed_in_order = fv_queue_order,
22c213
 };
22c213
 
22c213
+/*
22c213
+ * Main loop; this mostly deals with events on the vhost-user
22c213
+ * socket itself, and not actual fuse data.
22c213
+ */
22c213
 int virtio_loop(struct fuse_session *se)
22c213
 {
22c213
     fuse_log(FUSE_LOG_INFO, "%s: Entry\n", __func__);
22c213
 
22c213
-    while (1) {
22c213
-        /* TODO: Add stuffing */
22c213
+    while (!fuse_session_exited(se)) {
22c213
+        struct pollfd pf[1];
22c213
+        pf[0].fd = se->vu_socketfd;
22c213
+        pf[0].events = POLLIN;
22c213
+        pf[0].revents = 0;
22c213
+
22c213
+        fuse_log(FUSE_LOG_DEBUG, "%s: Waiting for VU event\n", __func__);
22c213
+        int poll_res = ppoll(pf, 1, NULL, NULL);
22c213
+
22c213
+        if (poll_res == -1) {
22c213
+            if (errno == EINTR) {
22c213
+                fuse_log(FUSE_LOG_INFO, "%s: ppoll interrupted, going around\n",
22c213
+                         __func__);
22c213
+                continue;
22c213
+            }
22c213
+            fuse_log(FUSE_LOG_ERR, "virtio_loop ppoll: %m\n");
22c213
+            break;
22c213
+        }
22c213
+        assert(poll_res == 1);
22c213
+        if (pf[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
22c213
+            fuse_log(FUSE_LOG_ERR, "%s: Unexpected poll revents %x\n", __func__,
22c213
+                     pf[0].revents);
22c213
+            break;
22c213
+        }
22c213
+        assert(pf[0].revents & POLLIN);
22c213
+        fuse_log(FUSE_LOG_DEBUG, "%s: Got VU event\n", __func__);
22c213
+        if (!vu_dispatch(&se->virtio_dev->dev)) {
22c213
+            fuse_log(FUSE_LOG_ERR, "%s: vu_dispatch failed\n", __func__);
22c213
+            break;
22c213
+        }
22c213
     }
22c213
 
22c213
     fuse_log(FUSE_LOG_INFO, "%s: Exit\n", __func__);
22c213
+
22c213
+    return 0;
22c213
 }
22c213
 
22c213
 int virtio_session_mount(struct fuse_session *se)
22c213
-- 
22c213
1.8.3.1
22c213