From patchwork Fri Sep 30 09:45:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 117068 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9CFC0B6F64 for ; Fri, 30 Sep 2011 19:45:49 +1000 (EST) Received: from localhost ([::1]:36669 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R9Zf7-0001sx-2S for incoming@patchwork.ozlabs.org; Fri, 30 Sep 2011 05:45:45 -0400 Received: from eggs.gnu.org ([140.186.70.92]:33886) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R9Zey-0001sX-Py for qemu-devel@nongnu.org; Fri, 30 Sep 2011 05:45:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R9Zeu-0006RG-30 for qemu-devel@nongnu.org; Fri, 30 Sep 2011 05:45:36 -0400 Received: from mail-bw0-f45.google.com ([209.85.214.45]:59636) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R9Zet-0006QU-Nz for qemu-devel@nongnu.org; Fri, 30 Sep 2011 05:45:32 -0400 Received: by bkbzv15 with SMTP id zv15so1657769bkb.4 for ; Fri, 30 Sep 2011 02:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; bh=Gd7qZiJU5L+TCox6hKPTM++dRvJk7R1pASpE+tLkM80=; b=d9ul9g6czKWjvyMAyWRisPv86t0f62Mzk3l8/GDuCqh2GaMZZcoXA9Y19QhwFmmldx XtPMR6VwTXWNu4v1RdC5WPcISHd9RxdMvzalE5mEMAQXfzA5CwLQMpQNfrLos76Haohh XOE6P+MMsZoYgIK/uP9aSxZierJxVDqtMNpiU= Received: by 10.204.145.14 with SMTP id b14mr1030413bkv.113.1317375930633; Fri, 30 Sep 2011 02:45:30 -0700 (PDT) Received: from localhost.localdomain ([109.65.218.131]) by mx.google.com with ESMTPS id z7sm4329270bkt.5.2011.09.30.02.45.28 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 30 Sep 2011 02:45:29 -0700 (PDT) From: Sasha Levin To: qemu-devel@nongnu.org Date: Fri, 30 Sep 2011 12:45:21 +0300 Message-Id: <1317375921-19336-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.6.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.214.45 Cc: swoop3r@gmx.de, Anthony Liguori , Sasha Levin Subject: [Qemu-devel] [PATCH v2] tap: Add optional parameters to up/down script X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This allows the user to add custom parameters to the up or down scripts. Extra parameters are useful in more complex networking scenarios where we would like to configure network devices when starting or stopping the guest. Cc: Anthony Liguori Signed-off-by: Sasha Levin --- net.c | 8 ++++++++ net/tap.c | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/net.c b/net.c index d05930c..bb27598 100644 --- a/net.c +++ b/net.c @@ -952,10 +952,18 @@ static const struct { .type = QEMU_OPT_STRING, .help = "script to initialize the interface", }, { + .name = "scriptparams", + .type = QEMU_OPT_STRING, + .help = "parameters for the initialization script", + }, { .name = "downscript", .type = QEMU_OPT_STRING, .help = "script to shut down the interface", }, { + .name = "downscriptparams", + .type = QEMU_OPT_STRING, + .help = "parameters for the deinitialization script", + }, { .name = "sndbuf", .type = QEMU_OPT_SIZE, .help = "send buffer limit" diff --git a/net/tap.c b/net/tap.c index 1f26dc9..f2f1fe5 100644 --- a/net/tap.c +++ b/net/tap.c @@ -53,6 +53,7 @@ typedef struct TAPState { int fd; char down_script[1024]; char down_script_arg[128]; + char down_script_params[1024]; uint8_t buf[TAP_BUFSIZE]; unsigned int read_poll : 1; unsigned int write_poll : 1; @@ -62,7 +63,8 @@ typedef struct TAPState { unsigned host_vnet_hdr_len; } TAPState; -static int launch_script(const char *setup_script, const char *ifname, int fd); +static int launch_script(const char *setup_script, const char *ifname, + const char *params, int fd); static int tap_can_send(void *opaque); static void tap_send(void *opaque); @@ -287,7 +289,8 @@ static void tap_cleanup(VLANClientState *nc) qemu_purge_queued_packets(nc); if (s->down_script[0]) - launch_script(s->down_script, s->down_script_arg, s->fd); + launch_script(s->down_script, s->down_script_arg, + s->down_script_params, s->fd); tap_read_poll(s, 0); tap_write_poll(s, 0); @@ -344,11 +347,12 @@ static TAPState *net_tap_fd_init(VLANState *vlan, return s; } -static int launch_script(const char *setup_script, const char *ifname, int fd) +static int launch_script(const char *setup_script, const char *ifname, + const char *params, int fd) { sigset_t oldmask, mask; int pid, status; - char *args[3]; + char *args[4]; char **parg; sigemptyset(&mask); @@ -371,6 +375,7 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) parg = args; *parg++ = (char *)setup_script; *parg++ = (char *)ifname; + *parg++ = (char *)params; *parg = NULL; execv(setup_script, args); _exit(1); @@ -392,7 +397,7 @@ static int net_tap_init(QemuOpts *opts, int *vnet_hdr) { int fd, vnet_hdr_required; char ifname[128] = {0,}; - const char *setup_script; + const char *setup_script, *setup_script_params; if (qemu_opt_get(opts, "ifname")) { pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); @@ -411,10 +416,15 @@ static int net_tap_init(QemuOpts *opts, int *vnet_hdr) } setup_script = qemu_opt_get(opts, "script"); + setup_script_params = qemu_opt_get(opts, "scriptparams"); + if (setup_script_params == NULL) { + setup_script_params = ""; + } + if (setup_script && setup_script[0] != '\0' && strcmp(setup_script, "no") != 0 && - launch_script(setup_script, ifname, fd)) { + launch_script(setup_script, ifname, setup_script_params, fd)) { close(fd); return -1; } @@ -432,9 +442,12 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan if (qemu_opt_get(opts, "fd")) { if (qemu_opt_get(opts, "ifname") || qemu_opt_get(opts, "script") || + qemu_opt_get(opts, "scriptparams") || qemu_opt_get(opts, "downscript") || + qemu_opt_get(opts, "downscriptparams") || qemu_opt_get(opts, "vnet_hdr")) { - error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd="); + error_report("ifname=, script=, downscript=, scriptparams=, " + "downscriptparams= and vnet_hdr= is invalid with fd="); return -1; } @@ -455,6 +468,14 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT); } + if (!qemu_opt_get(opts, "scriptparams")) { + qemu_opt_set(opts, "scriptparams", ""); + } + + if (!qemu_opt_get(opts, "downscriptparams")) { + qemu_opt_set(opts, "downscriptparams", ""); + } + fd = net_tap_init(opts, &vnet_hdr); if (fd == -1) { return -1; @@ -475,18 +496,24 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); } else { const char *ifname, *script, *downscript; + const char *scriptparams, *downscriptparams; ifname = qemu_opt_get(opts, "ifname"); script = qemu_opt_get(opts, "script"); downscript = qemu_opt_get(opts, "downscript"); + scriptparams = qemu_opt_get(opts, "scriptparams"); + downscriptparams = qemu_opt_get(opts, "downscriptparams"); snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "ifname=%s,script=%s,downscript=%s", - ifname, script, downscript); + "ifname=%s,script=%s,scriptparams=%s,downscript=%s," + "downscriptparams=%s", ifname, script, scriptparams, + downscript, downscriptparams); 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); + snprintf(s->down_script_params, sizeof(s->down_script_params), "%s", + downscriptparams); } }