From nobody Mon Feb 9 07:55:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527870919561321.5732374729348; Fri, 1 Jun 2018 09:35:19 -0700 (PDT) Received: from localhost ([::1]:56605 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOn1K-0008IJ-I5 for importer@patchew.org; Fri, 01 Jun 2018 12:35:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38570) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOmtE-000269-Ku for qemu-devel@nongnu.org; Fri, 01 Jun 2018 12:26:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fOmtD-00079w-CY for qemu-devel@nongnu.org; Fri, 01 Jun 2018 12:26:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:60282 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fOmtD-00079K-1p for qemu-devel@nongnu.org; Fri, 01 Jun 2018 12:26:55 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2B026401EF23; Fri, 1 Jun 2018 16:26:54 +0000 (UTC) Received: from redhat.com (ovpn-121-148.rdu2.redhat.com [10.10.121.148]) by smtp.corp.redhat.com (Postfix) with SMTP id BB2BA210C6CF; Fri, 1 Jun 2018 16:26:53 +0000 (UTC) Date: Fri, 1 Jun 2018 19:26:53 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <20180601162620.32362-7-mst@redhat.com> References: <20180601162620.32362-1-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20180601162620.32362-1-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 01 Jun 2018 16:26:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 01 Jun 2018 16:26:54 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mst@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 06/31] vhost-user-bridge: support host notifier X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Dr. David Alan Gilbert" , Tiwei Bie , =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tiwei Bie This patch introduces the host notifier support in vhost-user-bridge. A new option (-H) is added to use the host notifier. This is mainly used to test the host notifier implementation in vhost user. Signed-off-by: Tiwei Bie Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- tests/vhost-user-bridge.c | 98 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c index e0605a529e..0884294141 100644 --- a/tests/vhost-user-bridge.c +++ b/tests/vhost-user-bridge.c @@ -29,6 +29,7 @@ =20 #define _FILE_OFFSET_BITS 64 =20 +#include "qemu/atomic.h" #include "qemu/osdep.h" #include "qemu/iov.h" #include "standard-headers/linux/virtio_net.h" @@ -65,6 +66,11 @@ typedef struct VubrDev { int sock; int ready; int quit; + struct { + int fd; + void *addr; + pthread_t thread; + } notifier; } VubrDev; =20 static void @@ -445,14 +451,22 @@ static uint64_t vubr_get_features(VuDev *dev) { return 1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE | - 1ULL << VIRTIO_NET_F_MRG_RXBUF; + 1ULL << VIRTIO_NET_F_MRG_RXBUF | + 1ULL << VIRTIO_F_VERSION_1; } =20 static void vubr_queue_set_started(VuDev *dev, int qidx, bool started) { + VubrDev *vubr =3D container_of(dev, VubrDev, vudev); VuVirtq *vq =3D vu_get_queue(dev, qidx); =20 + if (started && vubr->notifier.fd >=3D 0) { + vu_set_queue_host_notifier(dev, vq, vubr->notifier.fd, + getpagesize(), + qidx * getpagesize()); + } + if (qidx % 2 =3D=3D 1) { vu_set_queue_handler(dev, vq, started ? vubr_handle_tx : NULL); } @@ -522,6 +536,8 @@ vubr_new(const char *path, bool client) vubr_die("socket"); } =20 + dev->notifier.fd =3D -1; + un.sun_family =3D AF_UNIX; strcpy(un.sun_path, path); len =3D sizeof(un.sun_family) + strlen(path); @@ -559,6 +575,73 @@ vubr_new(const char *path, bool client) return dev; } =20 +static void *notifier_thread(void *arg) +{ + VuDev *dev =3D (VuDev *)arg; + VubrDev *vubr =3D container_of(dev, VubrDev, vudev); + int pagesize =3D getpagesize(); + int qidx; + + while (true) { + for (qidx =3D 0; qidx < VHOST_MAX_NR_VIRTQUEUE; qidx++) { + uint16_t *n =3D vubr->notifier.addr + pagesize * qidx; + + if (*n =3D=3D qidx) { + *n =3D 0xffff; + /* We won't miss notifications if we reset + * the memory first. */ + smp_mb(); + + DPRINT("Got a notification for queue%d via host notifier.\= n", + qidx); + + if (qidx % 2 =3D=3D 1) { + vubr_handle_tx(dev, qidx); + } + } + usleep(1000); + } + } + + return NULL; +} + +static void +vubr_host_notifier_setup(VubrDev *dev) +{ + char template[] =3D "/tmp/vubr-XXXXXX"; + pthread_t thread; + size_t length; + void *addr; + int fd; + + length =3D getpagesize() * VHOST_MAX_NR_VIRTQUEUE; + + fd =3D mkstemp(template); + if (fd < 0) { + vubr_die("mkstemp()"); + } + + if (posix_fallocate(fd, 0, length) !=3D 0) { + vubr_die("posix_fallocate()"); + } + + addr =3D mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (addr =3D=3D MAP_FAILED) { + vubr_die("mmap()"); + } + + memset(addr, 0xff, length); + + if (pthread_create(&thread, NULL, notifier_thread, &dev->vudev) !=3D 0= ) { + vubr_die("pthread_create()"); + } + + dev->notifier.fd =3D fd; + dev->notifier.addr =3D addr; + dev->notifier.thread =3D thread; +} + static void vubr_set_host(struct sockaddr_in *saddr, const char *host) { @@ -673,8 +756,9 @@ main(int argc, char *argv[]) VubrDev *dev; int opt; bool client =3D false; + bool host_notifier =3D false; =20 - while ((opt =3D getopt(argc, argv, "l:r:u:c")) !=3D -1) { + while ((opt =3D getopt(argc, argv, "l:r:u:cH")) !=3D -1) { =20 switch (opt) { case 'l': @@ -693,6 +777,9 @@ main(int argc, char *argv[]) case 'c': client =3D true; break; + case 'H': + host_notifier =3D true; + break; default: goto out; } @@ -708,6 +795,10 @@ main(int argc, char *argv[]) return 1; } =20 + if (host_notifier) { + vubr_host_notifier_setup(dev); + } + vubr_backend_udp_setup(dev, lhost, lport, rhost, rport); vubr_run(dev); =20 @@ -717,7 +808,7 @@ main(int argc, char *argv[]) =20 out: fprintf(stderr, "Usage: %s ", argv[0]); - fprintf(stderr, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost:r= port]\n"); + fprintf(stderr, "[-c] [-H] [-u ud_socket_path] [-l lhost:lport] [-r rh= ost:rport]\n"); fprintf(stderr, "\t-u path to unix doman socket. default: %s\n", DEFAULT_UD_SOCKET); fprintf(stderr, "\t-l local host and port. default: %s:%s\n", @@ -725,6 +816,7 @@ out: fprintf(stderr, "\t-r remote host and port. default: %s:%s\n", DEFAULT_RHOST, DEFAULT_RPORT); fprintf(stderr, "\t-c client mode\n"); + fprintf(stderr, "\t-H use host notifier\n"); =20 return 1; } --=20 MST