cryptospore / rpms / qemu-kvm

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