From patchwork Sat May 21 04:23:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 805192 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4L4NVkJ001014 for ; Sat, 21 May 2011 04:23:32 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751259Ab1EUEX2 (ORCPT ); Sat, 21 May 2011 00:23:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:64808 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751064Ab1EUEX1 (ORCPT ); Sat, 21 May 2011 00:23:27 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4L4NQxx007494 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 21 May 2011 00:23:26 -0400 Received: from freedom.redhat.com (vpn-9-106.rdu.redhat.com [10.11.9.106]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4L4NMqq010171; Sat, 21 May 2011 00:23:24 -0400 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues , Amos Kong Subject: [PATCH 1/5] KVM test: Adding framework code to control bridge creation Date: Sat, 21 May 2011 01:23:26 -0300 Message-Id: <1305951810-5933-2-git-send-email-lmr@redhat.com> In-Reply-To: <1305951810-5933-1-git-send-email-lmr@redhat.com> References: <1305951810-5933-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Sat, 21 May 2011 04:23:32 +0000 (UTC) Provide in framework utility code to control the creation of a bridge, in order to provide TAP functionality for autotest users without relying on previous setup made by the user. This is a reimplementation of Amos's code, the differences are: * Implemented as a setup class, taking advantage of object internal state to use in different places of the code * Split up the operations to make it easier to understand the steps and why we are doing them * Use of autotest API instead of commands Signed-off-by: Lucas Meneghel Rodrigues Signed-off-by: Amos Kong --- client/virt/virt_test_setup.py | 103 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 103 insertions(+), 0 deletions(-) diff --git a/client/virt/virt_test_setup.py b/client/virt/virt_test_setup.py index f915c1b..cdc0e23 100644 --- a/client/virt/virt_test_setup.py +++ b/client/virt/virt_test_setup.py @@ -105,3 +105,106 @@ class HugePageConfig(object): return utils.system("echo 0 > %s" % self.kernel_hp_file) logging.debug("Hugepage memory successfuly dealocated") + + +class PrivateBridgeError(Exception): + def __init__(self, brname): + self.brname = brname + + def __str__(self): + return "Bridge %s not available after setup" % self.brname + + +class PrivateBridgeConfig(object): + __shared_state = {} + def __init__(self, params=None): + self.__dict__ = self.__shared_state + if params is not None: + self.brname = params.get("priv_brname", 'atbr0') + self.subnet = params.get("priv_subnet", '192.168.58') + self.ip_version = params.get("bridge_ip_version", "ipv4") + self.dhcp_server_pid = None + + + def _add_bridge(self): + utils.system("brctl addbr %s" % self.brname) + ip_fwd_path = "/proc/sys/net/%s/ip_forward" % self.ip_version + ip_fwd = open(ip_fwd_path, "w") + ip_fwd.write("1\n") + utils.system("brctl stp %s on" % self.brname) + utils.system("brctl setfd %s 0" % self.brname) + + + def _bring_bridge_up(self): + utils.system("ifconfig %s %s.1 up" % (self.brname, self.subnet)) + + + def _enable_nat(self): + utils.system("iptables -t nat -A POSTROUTING -s %s.254/24 ! " + "-d %s.254/24 -j MASQUERADE" % (self.subnet, self.subnet)) + + + def _start_dhcp_server(self): + utils.system("service dnsmasq stop") + utils.system("dnsmasq --strict-order --bind-interfaces " + "--listen-address %s.1 --dhcp-range %s.1,%s.254 " + "--pid-file=/tmp/dnsmasq.pid " + "--log-facility=/tmp/dnsmasq.log" % + (self.subnet, self.subnet, self.subnet)) + try: + self.dhcp_server_pid = int(open('/tmp/dnsmasq.pid', 'r').read()) + except ValueError: + raise PrivateBridgeError(self.brname) + logging.debug("Started internal DHCP server with PID %s", + self.dhcp_server_pid) + + + def _verify_bridge(self): + brctl_output = utils.system_output("brctl show") + if self.brname not in brctl_output: + raise PrivateBridgeError(self.brname) + + + def setup(self): + brctl_output = utils.system_output("brctl show") + if self.brname not in brctl_output: + logging.info("Configuring KVM test private bridge %s", self.brname) + self._add_bridge() + self._bring_bridge_up() + self._enable_nat() + self._start_dhcp_server() + self._verify_bridge() + + + def _stop_dhcp_server(self): + os.kill(self.dhcp_server_pid, 15) + + + def _bring_bridge_down(self): + utils.system("ifconfig %s down" % self.brname) + + + def _disable_nat(self): + utils.system("iptables -t nat -D POSTROUTING -s %s.254/24 !" + " -d %s.254/24 -j MASQUERADE" % (self.subnet, self.subnet)) + + + def _remove_bridge(self): + utils.system("brctl delbr %s" % self.brname) + + + def cleanup(self): + brctl_output = utils.system_output("brctl show") + cleanup = False + for line in brctl_output: + if line.startswith(self.brname): + # len == 4 means there is a TAP using the bridge + # so don't try to clean up + if len(line.split()) < 4: + cleanup = True + if cleanup: + logging.debug("Cleaning up KVM test private bridge %s", self.brname) + self._stop_dhcp_server() + self._bring_bridge_down() + self._disable_nat() + self._remove_bridge()