From patchwork Mon Jul 20 09:28:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Du, Fan" X-Patchwork-Id: 6825991 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 C1BEAC05AC for ; Mon, 20 Jul 2015 09:30:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E22482064E for ; Mon, 20 Jul 2015 09:30:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EF4852063A for ; Mon, 20 Jul 2015 09:30:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752562AbbGTJaH (ORCPT ); Mon, 20 Jul 2015 05:30:07 -0400 Received: from mga14.intel.com ([192.55.52.115]:8488 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751949AbbGTJaG (ORCPT ); Mon, 20 Jul 2015 05:30:06 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP; 20 Jul 2015 02:30:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,507,1432623600"; d="scan'208";a="750647607" Received: from dufan-optiplex-9010.bj.intel.com ([10.238.155.102]) by fmsmga001.fm.intel.com with ESMTP; 20 Jul 2015 02:30:05 -0700 From: Fan Du To: kvm@vger.kernel.org Cc: penberg@kernel.org, Fan Du Subject: [PATCH 1/3] kvm-tool: Introduce downscript option Date: Mon, 20 Jul 2015 17:28:45 +0800 Message-Id: <1437384527-22987-2-git-send-email-fan.du@intel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1437384527-22987-1-git-send-email-fan.du@intel.com> References: <1437384527-22987-1-git-send-email-fan.du@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, 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 the reverse of "script" does. Signed-off-by: Fan Du --- tools/kvm/include/kvm/virtio-net.h | 1 + tools/kvm/virtio/net.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tools/kvm/include/kvm/virtio-net.h b/tools/kvm/include/kvm/virtio-net.h index f435cc3..d136a09 100644 --- a/tools/kvm/include/kvm/virtio-net.h +++ b/tools/kvm/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/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c index 25b9496..f3f7200 100644 --- a/tools/kvm/virtio/net.c +++ b/tools/kvm/virtio/net.c @@ -696,6 +696,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) { @@ -871,6 +873,32 @@ virtio_dev_init(virtio_net__init); int virtio_net__exit(struct kvm *kvm) { + int pid; + int status; + 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")) { + pid = fork(); + if (pid == 0) { + execl(ndev->params->downscript, + params->downscript, ndev->tap_name, NULL); + _exit(1); + } else { + waitpid(pid, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { + pr_warning("Fail to cleanup tap by %s", + params->script); + } + } + } + } return 0; } virtio_dev_exit(virtio_net__exit);