From 9c86567201b9cf51e1ed0b0c55a9d614d4988dce Mon Sep 17 00:00:00 2001 From: Edgar Magana Date: Mon, 11 Mar 2013 11:46:27 -0700 Subject: [PATCH] Add delete_net_interface function This commit adds a new function that deletes a specific tap device. This bug fix is targeted for Havana. Change-Id: I26be5d94550db35358caae642fb9000953346826 fixes: bug #1138408 --- nova/network/linux_net.py | 54 ++++++++++++--------------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 3ee5d74003d1..60a4fe78d986 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -1219,12 +1219,7 @@ def _create_veth_pair(dev1_name, dev2_name): deleting any previous devices with those names. """ for dev in [dev1_name, dev2_name]: - if device_exists(dev): - try: - utils.execute('ip', 'link', 'delete', dev1_name, - run_as_root=True, check_exit_code=[0, 2, 254]) - except exception.ProcessExecutionError: - LOG.exception(_("Error clearing stale veth %s") % dev) + delete_net_dev(dev) utils.execute('ip', 'link', 'add', dev1_name, 'type', 'veth', 'peer', 'name', dev2_name, run_as_root=True) @@ -1248,8 +1243,7 @@ def create_ovs_vif_port(bridge, dev, iface_id, mac, instance_id): def delete_ovs_vif_port(bridge, dev): utils.execute('ovs-vsctl', 'del-port', bridge, dev, run_as_root=True) - utils.execute('ip', 'link', 'delete', dev, - run_as_root=True) + delete_net_dev(dev) def create_tap_dev(dev, mac_address=None): @@ -1268,6 +1262,18 @@ def create_tap_dev(dev, mac_address=None): check_exit_code=[0, 2, 254]) +def delete_net_dev(dev): + """Delete a network device only if it exists.""" + if device_exists(dev): + try: + utils.execute('ip', 'link', 'delete', dev, run_as_root=True, + check_exit_code=[0, 2, 254]) + LOG.debug(_("Net device removed: '%s'"), dev) + except exception.ProcessExecutionError: + with excutils.save_and_reraise_exception(): + LOG.error(_("Failed removing net device: '%s'"), dev) + + # Similar to compute virt layers, the Linux network node # code uses a flexible driver model to support different ways # of creating ethernet interfaces and attaching them to the network. @@ -1407,17 +1413,7 @@ class LinuxBridgeInterfaceDriver(LinuxNetInterfaceDriver): def remove_vlan(cls, vlan_num): """Delete a vlan.""" vlan_interface = 'vlan%s' % vlan_num - if not device_exists(vlan_interface): - return - else: - try: - utils.execute('ip', 'link', 'delete', vlan_interface, - run_as_root=True, check_exit_code=[0, 2, 254]) - except exception.ProcessExecutionError: - with excutils.save_and_reraise_exception(): - LOG.error(_("Failed unplugging VLAN interface '%s'"), - vlan_interface) - LOG.debug(_("Unplugged VLAN interface '%s'"), vlan_interface) + delete_net_dev(vlan_interface) @classmethod @lockutils.synchronized('lock_bridge', 'nova-', external=True) @@ -1528,15 +1524,7 @@ class LinuxBridgeInterfaceDriver(LinuxNetInterfaceDriver): ipv4_filter.remove_rule('FORWARD', ('--out-interface %s -j %s' % (bridge, drop_action))) - try: - utils.execute('ip', 'link', 'delete', bridge, run_as_root=True, - check_exit_code=[0, 2, 254]) - except exception.ProcessExecutionError: - with excutils.save_and_reraise_exception(): - LOG.error(_("Failed unplugging bridge interface '%s'"), - bridge) - - LOG.debug(_("Unplugged bridge interface '%s'"), bridge) + delete_net_dev(bridge) @lockutils.synchronized('ebtables', 'nova-', external=True) @@ -1751,18 +1739,10 @@ class QuantumLinuxBridgeInterfaceDriver(LinuxNetInterfaceDriver): def unplug(self, network): dev = self.get_dev(network) - if not device_exists(dev): return None else: - try: - utils.execute('ip', 'link', 'delete', dev, run_as_root=True, - check_exit_code=[0, 2, 254]) - except exception.ProcessExecutionError: - with excutils.save_and_reraise_exception(): - LOG.error(_("Failed unplugging gateway interface '%s'"), - dev) - LOG.debug(_("Unplugged gateway interface '%s'"), dev) + delete_net_dev(dev) return dev def get_dev(self, network):