From patchwork Thu Oct 22 16:43:36 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark McLoughlin X-Patchwork-Id: 36711 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A0BE6B7BB9 for ; Fri, 23 Oct 2009 04:18:53 +1100 (EST) Received: from localhost ([127.0.0.1]:52983 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N11JK-0005sU-Ay for incoming@patchwork.ozlabs.org; Thu, 22 Oct 2009 13:18:50 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N10nW-0007G5-7k for qemu-devel@nongnu.org; Thu, 22 Oct 2009 12:45:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N10nL-00070H-Ha for qemu-devel@nongnu.org; Thu, 22 Oct 2009 12:45:53 -0400 Received: from [199.232.76.173] (port=47953 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N10nL-0006yv-1w for qemu-devel@nongnu.org; Thu, 22 Oct 2009 12:45:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57371) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N10nJ-0003Fq-6U for qemu-devel@nongnu.org; Thu, 22 Oct 2009 12:45:45 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9MGjigo020553 for ; Thu, 22 Oct 2009 12:45:44 -0400 Received: from blaa.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n9MGjhSj004570; Thu, 22 Oct 2009 12:45:43 -0400 Received: by blaa.localdomain (Postfix, from userid 500) id 5E0FC45806; Thu, 22 Oct 2009 17:43:50 +0100 (IST) From: Mark McLoughlin To: qemu-devel@nongnu.org Date: Thu, 22 Oct 2009 17:43:36 +0100 Message-Id: <1256229830-28066-6-git-send-email-markmc@redhat.com> In-Reply-To: <1256229830-28066-1-git-send-email-markmc@redhat.com> References: <1256229830-28066-1-git-send-email-markmc@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Mark McLoughlin Subject: [Qemu-devel] [PATCH 05/19] net: refactor tap initialization X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Re-factor things so that there is only one call site for net_tap_fd_init(). Two concerns about the QemuOpts usage here - firstly, we set the script arguments to their default value and, secondly, we set the ifname value to the name allocated by the kernel if none is supplied. Are we okay with such things ending up in writeconfig output? Signed-off-by: Mark McLoughlin --- net.c | 97 +++++++++++++++++++++++++++++++++++------------------------------ 1 files changed, 52 insertions(+), 45 deletions(-) diff --git a/net.c b/net.c index 356a280..fccabdb 100644 --- a/net.c +++ b/net.c @@ -1475,7 +1475,6 @@ static TAPState *net_tap_fd_init(VLANState *vlan, tap_receive, tap_receive_iov, tap_cleanup, s); tap_read_poll(s, 1); - snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); return s; } @@ -1724,38 +1723,34 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) return -1; } -static TAPState *net_tap_init(VLANState *vlan, const char *model, - const char *name, const char *ifname1, - const char *setup_script, const char *down_script) +static int net_tap_init(QemuOpts *opts, int *vnet_hdr) { - TAPState *s; - int fd, vnet_hdr; - char ifname[128]; + int fd; + char ifname[128] = {0,}; + const char *setup_script; - if (ifname1 != NULL) - pstrcpy(ifname, sizeof(ifname), ifname1); - else - ifname[0] = '\0'; - vnet_hdr = 0; - TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr)); - if (fd < 0) - return NULL; + if (qemu_opt_get(opts, "ifname")) { + pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); + } - if (!setup_script || !strcmp(setup_script, "no")) - setup_script = ""; - if (setup_script[0] != '\0' && - launch_script(setup_script, ifname, fd)) { - return NULL; + *vnet_hdr = 0; + TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr)); + if (fd < 0) { + return -1; } - s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr); - snprintf(s->vc->info_str, sizeof(s->vc->info_str), - "ifname=%s,script=%s,downscript=%s", - ifname, setup_script, down_script); - if (down_script && strcmp(down_script, "no")) { - snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); - snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); + + setup_script = qemu_opt_get(opts, "script"); + if (setup_script && + setup_script[0] != '\0' && + strcmp(setup_script, "no") != 0 && + launch_script(setup_script, ifname, fd)) { + close(fd); + return -1; } - return s; + + qemu_opt_set(opts, "ifname", ifname); + + return fd; } #endif /* !_WIN32 */ @@ -2683,10 +2678,9 @@ static int net_init_tap(QemuOpts *opts, VLANState *vlan) { TAPState *s; + int fd, vnet_hdr; if (qemu_opt_get(opts, "fd")) { - int fd; - if (qemu_opt_get(opts, "ifname") || qemu_opt_get(opts, "script") || qemu_opt_get(opts, "downscript")) { @@ -2701,28 +2695,22 @@ static int net_init_tap(QemuOpts *opts, fcntl(fd, F_SETFL, O_NONBLOCK); - s = net_tap_fd_init(vlan, "tap", name, fd, tap_probe_vnet_hdr(fd)); - if (!s) { - close(fd); - } + vnet_hdr = tap_probe_vnet_hdr(fd); } else { - const char *ifname, *script, *downscript; - - ifname = qemu_opt_get(opts, "ifname"); - script = qemu_opt_get(opts, "script"); - downscript = qemu_opt_get(opts, "downscript"); - - if (!script) { - script = DEFAULT_NETWORK_SCRIPT; + if (!qemu_opt_get(opts, "script")) { + qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT); } - if (!downscript) { - downscript = DEFAULT_NETWORK_DOWN_SCRIPT; + + if (!qemu_opt_get(opts, "downscript")) { + qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT); } - s = net_tap_init(vlan, "tap", name, ifname, script, downscript); + fd = net_tap_init(opts, &vnet_hdr); } + s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr); if (!s) { + close(fd); return -1; } @@ -2730,6 +2718,25 @@ static int net_init_tap(QemuOpts *opts, return -1; } + if (qemu_opt_get(opts, "fd")) { + snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); + } else { + const char *ifname, *script, *downscript; + + ifname = qemu_opt_get(opts, "ifname"); + script = qemu_opt_get(opts, "script"); + downscript = qemu_opt_get(opts, "downscript"); + + snprintf(s->vc->info_str, sizeof(s->vc->info_str), + "ifname=%s,script=%s,downscript=%s", + ifname, script, downscript); + + if (strcmp(downscript, "no") != 0) { + snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); + snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); + } + } + if (vlan) { vlan->nb_host_devs++; }