Blame SOURCES/kvm-vhost-user-bridge-support-host-notifier.patch

357786
From c331104889ecc51581ead4a154d37ad4d054ca73 Mon Sep 17 00:00:00 2001
357786
From: "plai@redhat.com" <plai@redhat.com>
357786
Date: Thu, 21 Jun 2018 18:54:40 +0200
357786
Subject: [PATCH 31/57] vhost-user-bridge: support host notifier
357786
357786
RH-Author: plai@redhat.com
357786
Message-id: <1529607285-9942-6-git-send-email-plai@redhat.com>
357786
Patchwork-id: 80934
357786
O-Subject: [RHEL7.6 PATCH BZ 1526645 05/10] vhost-user-bridge: support host notifier
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
This patch introduces the host notifier support in
357786
vhost-user-bridge. A new option (-H) is added to use
357786
the host notifier. This is mainly used to test the
357786
host notifier implementation in vhost user.
357786
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 e3af2928f8270498097af163bb023b633c0d8d1c)
357786
Signed-off-by: Paul Lai <plai@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 tests/vhost-user-bridge.c | 98 +++++++++++++++++++++++++++++++++++++++++++++--
357786
 1 file changed, 95 insertions(+), 3 deletions(-)
357786
357786
diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c
357786
index e0605a5..0884294 100644
357786
--- a/tests/vhost-user-bridge.c
357786
+++ b/tests/vhost-user-bridge.c
357786
@@ -29,6 +29,7 @@
357786
 
357786
 #define _FILE_OFFSET_BITS 64
357786
 
357786
+#include "qemu/atomic.h"
357786
 #include "qemu/osdep.h"
357786
 #include "qemu/iov.h"
357786
 #include "standard-headers/linux/virtio_net.h"
357786
@@ -65,6 +66,11 @@ typedef struct VubrDev {
357786
     int sock;
357786
     int ready;
357786
     int quit;
357786
+    struct {
357786
+        int fd;
357786
+        void *addr;
357786
+        pthread_t thread;
357786
+    } notifier;
357786
 } VubrDev;
357786
 
357786
 static void
357786
@@ -445,14 +451,22 @@ static uint64_t
357786
 vubr_get_features(VuDev *dev)
357786
 {
357786
     return 1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE |
357786
-        1ULL << VIRTIO_NET_F_MRG_RXBUF;
357786
+        1ULL << VIRTIO_NET_F_MRG_RXBUF |
357786
+        1ULL << VIRTIO_F_VERSION_1;
357786
 }
357786
 
357786
 static void
357786
 vubr_queue_set_started(VuDev *dev, int qidx, bool started)
357786
 {
357786
+    VubrDev *vubr = container_of(dev, VubrDev, vudev);
357786
     VuVirtq *vq = vu_get_queue(dev, qidx);
357786
 
357786
+    if (started && vubr->notifier.fd >= 0) {
357786
+        vu_set_queue_host_notifier(dev, vq, vubr->notifier.fd,
357786
+                                   getpagesize(),
357786
+                                   qidx * getpagesize());
357786
+    }
357786
+
357786
     if (qidx % 2 == 1) {
357786
         vu_set_queue_handler(dev, vq, started ? vubr_handle_tx : NULL);
357786
     }
357786
@@ -522,6 +536,8 @@ vubr_new(const char *path, bool client)
357786
         vubr_die("socket");
357786
     }
357786
 
357786
+    dev->notifier.fd = -1;
357786
+
357786
     un.sun_family = AF_UNIX;
357786
     strcpy(un.sun_path, path);
357786
     len = sizeof(un.sun_family) + strlen(path);
357786
@@ -559,6 +575,73 @@ vubr_new(const char *path, bool client)
357786
     return dev;
357786
 }
357786
 
357786
+static void *notifier_thread(void *arg)
357786
+{
357786
+    VuDev *dev = (VuDev *)arg;
357786
+    VubrDev *vubr = container_of(dev, VubrDev, vudev);
357786
+    int pagesize = getpagesize();
357786
+    int qidx;
357786
+
357786
+    while (true) {
357786
+        for (qidx = 0; qidx < VHOST_MAX_NR_VIRTQUEUE; qidx++) {
357786
+            uint16_t *n = vubr->notifier.addr + pagesize * qidx;
357786
+
357786
+            if (*n == qidx) {
357786
+                *n = 0xffff;
357786
+                /* We won't miss notifications if we reset
357786
+                 * the memory first. */
357786
+                smp_mb();
357786
+
357786
+                DPRINT("Got a notification for queue%d via host notifier.\n",
357786
+                       qidx);
357786
+
357786
+                if (qidx % 2 == 1) {
357786
+                    vubr_handle_tx(dev, qidx);
357786
+                }
357786
+            }
357786
+            usleep(1000);
357786
+        }
357786
+    }
357786
+
357786
+    return NULL;
357786
+}
357786
+
357786
+static void
357786
+vubr_host_notifier_setup(VubrDev *dev)
357786
+{
357786
+    char template[] = "/tmp/vubr-XXXXXX";
357786
+    pthread_t thread;
357786
+    size_t length;
357786
+    void *addr;
357786
+    int fd;
357786
+
357786
+    length = getpagesize() * VHOST_MAX_NR_VIRTQUEUE;
357786
+
357786
+    fd = mkstemp(template);
357786
+    if (fd < 0) {
357786
+        vubr_die("mkstemp()");
357786
+    }
357786
+
357786
+    if (posix_fallocate(fd, 0, length) != 0) {
357786
+        vubr_die("posix_fallocate()");
357786
+    }
357786
+
357786
+    addr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
357786
+    if (addr == MAP_FAILED) {
357786
+        vubr_die("mmap()");
357786
+    }
357786
+
357786
+    memset(addr, 0xff, length);
357786
+
357786
+    if (pthread_create(&thread, NULL, notifier_thread, &dev->vudev) != 0) {
357786
+        vubr_die("pthread_create()");
357786
+    }
357786
+
357786
+    dev->notifier.fd = fd;
357786
+    dev->notifier.addr = addr;
357786
+    dev->notifier.thread = thread;
357786
+}
357786
+
357786
 static void
357786
 vubr_set_host(struct sockaddr_in *saddr, const char *host)
357786
 {
357786
@@ -673,8 +756,9 @@ main(int argc, char *argv[])
357786
     VubrDev *dev;
357786
     int opt;
357786
     bool client = false;
357786
+    bool host_notifier = false;
357786
 
357786
-    while ((opt = getopt(argc, argv, "l:r:u:c")) != -1) {
357786
+    while ((opt = getopt(argc, argv, "l:r:u:cH")) != -1) {
357786
 
357786
         switch (opt) {
357786
         case 'l':
357786
@@ -693,6 +777,9 @@ main(int argc, char *argv[])
357786
         case 'c':
357786
             client = true;
357786
             break;
357786
+        case 'H':
357786
+            host_notifier = true;
357786
+            break;
357786
         default:
357786
             goto out;
357786
         }
357786
@@ -708,6 +795,10 @@ main(int argc, char *argv[])
357786
         return 1;
357786
     }
357786
 
357786
+    if (host_notifier) {
357786
+        vubr_host_notifier_setup(dev);
357786
+    }
357786
+
357786
     vubr_backend_udp_setup(dev, lhost, lport, rhost, rport);
357786
     vubr_run(dev);
357786
 
357786
@@ -717,7 +808,7 @@ main(int argc, char *argv[])
357786
 
357786
 out:
357786
     fprintf(stderr, "Usage: %s ", argv[0]);
357786
-    fprintf(stderr, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
357786
+    fprintf(stderr, "[-c] [-H] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
357786
     fprintf(stderr, "\t-u path to unix doman socket. default: %s\n",
357786
             DEFAULT_UD_SOCKET);
357786
     fprintf(stderr, "\t-l local host and port. default: %s:%s\n",
357786
@@ -725,6 +816,7 @@ out:
357786
     fprintf(stderr, "\t-r remote host and port. default: %s:%s\n",
357786
             DEFAULT_RHOST, DEFAULT_RPORT);
357786
     fprintf(stderr, "\t-c client mode\n");
357786
+    fprintf(stderr, "\t-H use host notifier\n");
357786
 
357786
     return 1;
357786
 }
357786
-- 
357786
1.8.3.1
357786