From patchwork Fri Jun 17 00:30:15 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: 9181941 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D89FE60760 for ; Fri, 17 Jun 2016 00:35:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C95B128392 for ; Fri, 17 Jun 2016 00:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC8AB28396; Fri, 17 Jun 2016 00:35:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 37FB928392 for ; Fri, 17 Jun 2016 00:35:10 +0000 (UTC) Received: from localhost ([::1]:52859 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDhkb-0001BZ-BI for patchwork-qemu-devel@patchwork.kernel.org; Thu, 16 Jun 2016 20:35:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45865) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDhg1-00054t-7u for qemu-devel@nongnu.org; Thu, 16 Jun 2016 20:30:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDhfv-0006Gi-5R for qemu-devel@nongnu.org; Thu, 16 Jun 2016 20:30:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38581) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDhfu-0006Gd-TO for qemu-devel@nongnu.org; Thu, 16 Jun 2016 20:30:19 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (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 5DC25201E8; Fri, 17 Jun 2016 00:30:18 +0000 (UTC) Received: from redhat.com (vpn1-5-5.ams2.redhat.com [10.36.5.5]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5H0UFsq004832; Thu, 16 Jun 2016 20:30:16 -0400 Date: Fri, 17 Jun 2016 03:30:15 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <20160617033015-mutt-send-email-mst@redhat.com> References: <1466123354-22896-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1466123354-22896-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 17 Jun 2016 00:30:18 +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 v3 03/15] 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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; }