From patchwork Wed Aug 5 10:53:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Du, Fan" X-Patchwork-Id: 6945681 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EF46AC05AC for ; Wed, 5 Aug 2015 02:56:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0B69720414 for ; Wed, 5 Aug 2015 02:56:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 90EB32038A for ; Wed, 5 Aug 2015 02:56:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751838AbbHEC4W (ORCPT ); Tue, 4 Aug 2015 22:56:22 -0400 Received: from mga14.intel.com ([192.55.52.115]:41972 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751776AbbHEC4V (ORCPT ); Tue, 4 Aug 2015 22:56:21 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP; 04 Aug 2015 19:56:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,613,1432623600"; d="scan'208";a="762146689" Received: from grantleyipdc02.bj.intel.com ([10.240.192.117]) by fmsmga001.fm.intel.com with ESMTP; 04 Aug 2015 19:56:19 -0700 From: Fan Du To: kvm@vger.kernel.org Cc: will.deacon@arm.com, andre.przywara@arm.com, marc.zyngier@arm.com, Fan Du Subject: [PATCHv4 kvmtool] kvmtool: Introduce downscript option for virtio-net Date: Wed, 5 Aug 2015 06:53:58 -0400 Message-Id: <1438772038-14697-1-git-send-email-fan.du@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <55AE147F.9050607@arm.com> References: <55AE147F.9050607@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, DATE_IN_FUTURE_06_12, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP To detach tap device automatically from bridge when exiting, just like what the reverse of "script" does. Signed-off-by: Fan Du --- include/kvm/virtio-net.h | 1 + virtio/net.c | 49 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/kvm/virtio-net.h b/include/kvm/virtio-net.h index f435cc3..d136a09 100644 --- a/include/kvm/virtio-net.h +++ b/include/kvm/virtio-net.h @@ -9,6 +9,7 @@ struct virtio_net_params { const char *guest_ip; const char *host_ip; const char *script; + const char *downscript; const char *trans; const char *tapif; char guest_mac[6]; diff --git a/virtio/net.c b/virtio/net.c index 4a6a855..a6e58db 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -294,10 +294,29 @@ static int virtio_net_request_tap(struct net_dev *ndev, struct ifreq *ifr, return ret; } +static int virtio_net_exec_script(const char* script, const char *tap_name) +{ + pid_t pid; + int status; + + pid = fork(); + if (pid == 0) { + execl(script, script, tap_name, NULL); + _exit(1); + } else { + waitpid(pid, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { + pr_warning("Fail to setup tap by %s", script); + return -1; + } + } + return 0; +} + static bool virtio_net__tap_init(struct net_dev *ndev) { int sock = socket(AF_INET, SOCK_STREAM, 0); - int pid, status, offload, hdr_len; + int offload, hdr_len; struct sockaddr_in sin = {0}; struct ifreq ifr; const struct virtio_net_params *params = ndev->params; @@ -339,17 +358,8 @@ static bool virtio_net__tap_init(struct net_dev *ndev) } if (strcmp(params->script, "none")) { - pid = fork(); - if (pid == 0) { - execl(params->script, params->script, ndev->tap_name, NULL); - _exit(1); - } else { - waitpid(pid, &status, 0); - if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { - pr_warning("Fail to setup tap by %s", params->script); - goto fail; - } - } + if(virtio_net_exec_script(params->script, ndev->tap_name) < 0) + goto fail; } else if (!skipconf) { memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, ndev->tap_name, sizeof(ndev->tap_name)); @@ -702,6 +712,8 @@ static int set_net_param(struct kvm *kvm, struct virtio_net_params *p, die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network); } else if (strcmp(param, "script") == 0) { p->script = strdup(val); + } else if (strcmp(param, "downscript") == 0) { + p->downscript = strdup(val); } else if (strcmp(param, "guest_ip") == 0) { p->guest_ip = strdup(val); } else if (strcmp(param, "host_ip") == 0) { @@ -740,6 +752,7 @@ int netdev_parser(const struct option *opt, const char *arg, int unset) .guest_ip = DEFAULT_GUEST_ADDR, .host_ip = DEFAULT_HOST_ADDR, .script = DEFAULT_SCRIPT, + .downscript = DEFAULT_SCRIPT, .mode = NET_MODE_TAP, }; @@ -877,6 +890,18 @@ virtio_dev_init(virtio_net__init); int virtio_net__exit(struct kvm *kvm) { + struct virtio_net_params *params; + struct net_dev *ndev; + struct list_head *ptr; + + list_for_each(ptr, &ndevs) { + ndev = list_entry(ptr, struct net_dev, list); + params = ndev->params; + /* Cleanup any tap device which attached to bridge */ + if (ndev->mode == NET_MODE_TAP && + strcmp(params->downscript, "none")) + virtio_net_exec_script(params->downscript, ndev->tap_name); + } return 0; } virtio_dev_exit(virtio_net__exit);