From patchwork Tue Jun 14 19:59:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 635518 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rTgWW0L3lz9t0G for ; Wed, 15 Jun 2016 06:01:11 +1000 (AEST) Received: from localhost ([::1]:37801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCuWK-0007FH-R3 for incoming@patchwork.ozlabs.org; Tue, 14 Jun 2016 16:01:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60806) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCuUu-0005r1-OQ for qemu-devel@nongnu.org; Tue, 14 Jun 2016 15:59:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bCuUt-0001Pl-Mt for qemu-devel@nongnu.org; Tue, 14 Jun 2016 15:59:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55162) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCuUt-0001PW-EI for qemu-devel@nongnu.org; Tue, 14 Jun 2016 15:59:39 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E989F85546; Tue, 14 Jun 2016 19:59:38 +0000 (UTC) Received: from redhat.com (unused [10.16.197.238] (may be forged)) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5EJxbgr021809; Tue, 14 Jun 2016 15:59:38 -0400 Date: Tue, 14 Jun 2016 22:59:37 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <20160614225937-mutt-send-email-mst@redhat.com> References: <1465934227-16558-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1465934227-16558-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 14 Jun 2016 19:59:38 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 03/32] tests/vhost-user-bridge: add client mode 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 , Yuanhan Liu , Victor Kaplansky , =?iso-8859-1?Q?Marc-Andr=E9?= Lureau Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Marc-André Lureau If -c is specified, vubr will try to connect to the socket instead of listening for connections. Signed-off-by: Marc-André Lureau Tested-by: Yuanhan Liu Reviewed-by: Yuanhan Liu Reviewed-by: Victor Kaplansky Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- tests/vhost-user-bridge.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/tests/vhost-user-bridge.c b/tests/vhost-user-bridge.c index 0779ba2..e231ce9 100644 --- a/tests/vhost-user-bridge.c +++ b/tests/vhost-user-bridge.c @@ -1204,12 +1204,13 @@ vubr_accept_cb(int sock, void *ctx) } static VubrDev * -vubr_new(const char *path) +vubr_new(const char *path, bool client) { VubrDev *dev = (VubrDev *) calloc(1, sizeof(VubrDev)); dev->nregions = 0; int i; struct sockaddr_un un; + CallbackFunc cb; size_t len; for (i = 0; i < MAX_NR_VIRTQUEUE; i++) { @@ -1238,21 +1239,30 @@ vubr_new(const char *path) un.sun_family = AF_UNIX; strcpy(un.sun_path, path); len = sizeof(un.sun_family) + strlen(path); - unlink(path); - if (bind(dev->sock, (struct sockaddr *) &un, len) == -1) { - vubr_die("bind"); - } + if (!client) { + unlink(path); - if (listen(dev->sock, 1) == -1) { - vubr_die("listen"); + if (bind(dev->sock, (struct sockaddr *) &un, len) == -1) { + vubr_die("bind"); + } + + if (listen(dev->sock, 1) == -1) { + vubr_die("listen"); + } + cb = vubr_accept_cb; + + DPRINT("Waiting for connections on UNIX socket %s ...\n", path); + } else { + if (connect(dev->sock, (struct sockaddr *)&un, len) == -1) { + vubr_die("connect"); + } + cb = vubr_receive_cb; } dispatcher_init(&dev->dispatcher); - dispatcher_add(&dev->dispatcher, dev->sock, (void *)dev, - vubr_accept_cb); + dispatcher_add(&dev->dispatcher, dev->sock, (void *)dev, cb); - DPRINT("Waiting for connections on UNIX socket %s ...\n", path); return dev; } @@ -1369,8 +1379,9 @@ main(int argc, char *argv[]) { VubrDev *dev; int opt; + bool client = false; - while ((opt = getopt(argc, argv, "l:r:u:")) != -1) { + while ((opt = getopt(argc, argv, "l:r:u:c")) != -1) { switch (opt) { case 'l': @@ -1386,16 +1397,20 @@ main(int argc, char *argv[]) case 'u': ud_socket_path = strdup(optarg); break; + case 'c': + client = true; + break; default: goto out; } } - DPRINT("ud socket: %s\n", ud_socket_path); + DPRINT("ud socket: %s (%s)\n", ud_socket_path, + client ? "client" : "server"); DPRINT("local: %s:%s\n", lhost, lport); DPRINT("remote: %s:%s\n", rhost, rport); - dev = vubr_new(ud_socket_path); + dev = vubr_new(ud_socket_path, client); if (!dev) { return 1; } @@ -1406,13 +1421,14 @@ main(int argc, char *argv[]) out: fprintf(stderr, "Usage: %s ", argv[0]); - fprintf(stderr, "[-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n"); + fprintf(stderr, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost: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", DEFAULT_LHOST, DEFAULT_LPORT); fprintf(stderr, "\t-r remote host and port. default: %s:%s\n", DEFAULT_RHOST, DEFAULT_RPORT); + fprintf(stderr, "\t-c client mode\n"); return 1; }