From patchwork Fri Aug 5 16:19:15 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: 1038872 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p75GJN9x026130 for ; Fri, 5 Aug 2011 16:19:23 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756795Ab1HEQTV (ORCPT ); Fri, 5 Aug 2011 12:19:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:31443 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754549Ab1HEQTU (ORCPT ); Fri, 5 Aug 2011 12:19:20 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p75GJJO9001738 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 5 Aug 2011 12:19:19 -0400 Received: from freedom.redhat.com (vpn-8-121.rdu.redhat.com [10.11.8.121]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p75GJHkf028958; Fri, 5 Aug 2011 12:19:17 -0400 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues , Cleber Rosa , Alex Williamson Subject: [PATCH] KVM test: Remove custom bridge managing code Date: Fri, 5 Aug 2011 13:19:15 -0300 Message-Id: <1312561155-14270-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 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]); Fri, 05 Aug 2011 16:19:24 +0000 (UTC) Since we already require some deps to run KVM autotest, and since the majority of the potential KVM autotest users do have libvirt installed and running, it makes little sense to maintain custom code to manage an internal bridge. Let's just require libvirt and use virbr0 as the default kvm autotest virt bridge. It's functional, available and allows to do everything we want. CC: Cleber Rosa CC: Alex Williamson Signed-off-by: Lucas Meneghel Rodrigues --- client/tests/kvm/tests_base.cfg.sample | 11 +-- client/virt/kvm_vm.py | 3 - client/virt/virt_env_process.py | 8 -- client/virt/virt_test_setup.py | 186 -------------------------------- 4 files changed, 2 insertions(+), 206 deletions(-) diff --git a/client/tests/kvm/tests_base.cfg.sample b/client/tests/kvm/tests_base.cfg.sample index acd9883..9e40d65 100644 --- a/client/tests/kvm/tests_base.cfg.sample +++ b/client/tests/kvm/tests_base.cfg.sample @@ -57,16 +57,9 @@ redirs = remote_shell guest_port_remote_shell = 22 # NIC parameters -#nic_mode = user nic_mode = tap -bridge = private -# You can set bridge to -# be a specific bridge -# name, such as 'virbr0' -#bridge = virbr0 -# If you need more ports to be available for comm between host and guest, -# please add them here -priv_bridge_ports = 53 67 +# By default we use libvirt's bridge +bridge = virbr0 run_tcpdump = yes # Misc diff --git a/client/virt/kvm_vm.py b/client/virt/kvm_vm.py index a2f22b4..f7b8345 100644 --- a/client/virt/kvm_vm.py +++ b/client/virt/kvm_vm.py @@ -617,7 +617,6 @@ class VM(virt_vm.BaseVM): @raise TAPCreationError: If fail to create tap fd @raise BRAddIfError: If fail to add a tap to a bridge @raise TAPBringUpError: If fail to bring up a tap - @raise PrivateBridgeError: If fail to bring the private bridge """ error.context("creating '%s'" % self.name) self.destroy(free_mac_addresses=False) @@ -692,8 +691,6 @@ class VM(virt_vm.BaseVM): if nic_params.get("nic_mode") == "tap": ifname = self.get_ifname(vlan) brname = nic_params.get("bridge") - if brname == "private": - brname = virt_test_setup.PrivateBridgeConfig().brname tapfd = virt_utils.open_tap("/dev/net/tun", ifname) virt_utils.add_to_bridge(ifname, brname) virt_utils.bring_up_ifname(ifname) diff --git a/client/virt/virt_env_process.py b/client/virt/virt_env_process.py index 8fd5c21..8ef6497 100644 --- a/client/virt/virt_env_process.py +++ b/client/virt/virt_env_process.py @@ -205,10 +205,6 @@ def preprocess(test, params, env): """ error.context("preprocessing") - if params.get("bridge") == "private": - brcfg = virt_test_setup.PrivateBridgeConfig(params) - brcfg.setup() - # Start tcpdump if it isn't already running if "address_cache" not in env: env["address_cache"] = {} @@ -375,10 +371,6 @@ def postprocess(test, params, env): int(params.get("post_command_timeout", "600")), params.get("post_command_noncritical") == "yes") - if params.get("bridge") == "private": - brcfg = virt_test_setup.PrivateBridgeConfig() - brcfg.cleanup() - def postprocess_on_error(test, params, env): """ diff --git a/client/virt/virt_test_setup.py b/client/virt/virt_test_setup.py index f2ff38b..334c3cf 100644 --- a/client/virt/virt_test_setup.py +++ b/client/virt/virt_test_setup.py @@ -289,189 +289,3 @@ 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 - ports = params.get("priv_bridge_ports", '53 67').split() - s_port = params.get("guest_port_remote_shell", "10022") - if s_port not in ports: - ports.append(s_port) - ft_port = params.get("guest_port_file_transfer", "10023") - if ft_port not in ports: - ports.append(ft_port) - u_port = params.get("guest_port_unattended_install", "13323") - if u_port not in ports: - ports.append(u_port) - self.iptables_rules = self._assemble_iptables_rules(ports) - - - def _assemble_iptables_rules(self, port_list): - rules = [] - index = 0 - for port in port_list: - index += 1 - rules.append("INPUT %s -i %s -p tcp -m tcp --dport %s -j ACCEPT" % - (index, self.brname, port)) - index += 1 - rules.append("INPUT %s -i %s -p udp -m udp --dport %s -j ACCEPT" % - (index, self.brname, port)) - rules.append("FORWARD 1 -m physdev --physdev-is-bridged -j ACCEPT") - rules.append("FORWARD 2 -d %s.0/24 -o %s -m state " - "--state RELATED,ESTABLISHED -j ACCEPT" % - (self.subnet, self.brname)) - rules.append("FORWARD 3 -s %s.0/24 -i %s -j ACCEPT" % - (self.subnet, self.brname)) - rules.append("FORWARD 4 -i %s -o %s -j ACCEPT" % - (self.brname, self.brname)) - return rules - - - 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 _iptables_add(self, cmd): - return utils.system("iptables -I %s" % cmd) - - - def _iptables_del(self, cmd): - return utils.system("iptables -D %s" % cmd) - - - def _enable_nat(self): - for rule in self.iptables_rules: - self._iptables_add(rule) - - - def _start_dhcp_server(self): - utils.run("service dnsmasq stop") - utils.run("dnsmasq --strict-order --bind-interfaces " - "--listen-address %s.1 --dhcp-range %s.2,%s.254 " - "--dhcp-lease-max=253 " - "--dhcp-no-override " - "--pid-file=/tmp/dnsmasq.pid " - "--log-facility=/tmp/dnsmasq.log" % - (self.subnet, self.subnet, self.subnet)) - self.dhcp_server_pid = None - 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.debug("Configuring KVM test private bridge %s", self.brname) - try: - self._add_bridge() - except: - self._remove_bridge() - raise - try: - self._bring_bridge_up() - except: - self._bring_bridge_down() - self._remove_bridge() - raise - try: - self._enable_nat() - except: - self._disable_nat() - self._bring_bridge_down() - self._remove_bridge() - raise - try: - self._start_dhcp_server() - except: - self._stop_dhcp_server() - self._disable_nat() - self._bring_bridge_down() - self._remove_bridge() - raise - self._verify_bridge() - - - def _stop_dhcp_server(self): - if self.dhcp_server_pid is not None: - try: - os.kill(self.dhcp_server_pid, 15) - except OSError: - pass - else: - try: - dhcp_server_pid = int(open('/tmp/dnsmasq.pid', 'r').read()) - except ValueError: - return - try: - os.kill(dhcp_server_pid, 15) - except OSError: - pass - - - def _bring_bridge_down(self): - utils.system("ifconfig %s down" % self.brname, ignore_status=True) - - - def _disable_nat(self): - for rule in self.iptables_rules: - split_list = rule.split(' ') - # We need to remove numbering here - split_list.pop(1) - rule = " ".join(split_list) - self._iptables_del(rule) - - - def _remove_bridge(self): - utils.system("brctl delbr %s" % self.brname, ignore_status=True) - - - def cleanup(self): - brctl_output = utils.system_output("brctl show") - cleanup = False - for line in brctl_output.split("\n"): - if line.startswith(self.brname): - # len == 4 means there is a TAP using the bridge - # so don't try to clean it up - if len(line.split()) < 4: - cleanup = True - break - if cleanup: - logging.debug("Cleaning up KVM test private bridge %s", self.brname) - self._stop_dhcp_server() - self._disable_nat() - self._bring_bridge_down() - self._remove_bridge()