binding: use cached interface info

`cleanup_veth()` to use IPDB instead of IPRoute.

Reasons:

1. Info caching

`IPRoute.link_lookup()` loads all the interfaces info from the
operating system, while `IPDB.interfaces` keeps it in sync using
netlink broadcast. On systems with huge numbers of interfaces it
matters.

2. Sync state

`IPRoute.link_remove()` triggers the action in the OS kernel,
while `IPDB...remove()` waits the operation to be completed, and
provides a feedback if the operation is failed.

3. Code cleanup

Since `IPDB` is used anyway, the change doesn't create new global
variables, but removes one old one.

Change-Id: I4b40873f065f27f9e05dfda1b89f073703121863
This commit is contained in:
Peter V. Saveliev 2016-06-29 14:41:30 +02:00 committed by Antoni Segura Puimedon
parent dc4ec4e7c6
commit 7dbe5fe693
No known key found for this signature in database
GPG Key ID: 2329618D2967720A
2 changed files with 8 additions and 29 deletions

View File

@ -19,7 +19,6 @@ from kuryr.lib import constants
_IPDB_CACHE = None _IPDB_CACHE = None
_IPROUTE_CACHE = None
FIXED_IP_KEY = 'fixed_ips' FIXED_IP_KEY = 'fixed_ips'
IP_ADDRESS_KEY = 'ip_address' IP_ADDRESS_KEY = 'ip_address'
@ -51,22 +50,6 @@ def get_ipdb():
return _IPDB_CACHE return _IPDB_CACHE
def get_iproute():
"""Returns the already cached or a newly created IPRoute instance.
IPRoute reads the Linux specific file when it's instantiated. This
behaviour prevents Mac OSX users from running unit tests. This function
makes the loading IPDB lazyily and therefore it can be mocked after the
import of modules that import this module.
:returns: The already cached or newly created ``pyroute2.IPRoute`` instance
"""
global _IPROUTE_CACHE
if not _IPROUTE_CACHE:
_IPROUTE_CACHE = pyroute2.IPRoute()
return _IPROUTE_CACHE
def remove_device(ifname): def remove_device(ifname):
"""Removes the device with name ifname. """Removes the device with name ifname.
@ -75,15 +58,15 @@ def remove_device(ifname):
exists, otherwise None exists, otherwise None
:raises: pyroute2.NetlinkError :raises: pyroute2.NetlinkError
""" """
ipr = get_iproute() ip = get_ipdb()
dev_index = ip.interface.get(ifname, {}).get('index', None)
if dev_index:
with ip.interfaces[ifname] as iface:
iface.remove()
devices = ipr.link_lookup(ifname=ifname)
if devices:
dev_index = devices[0]
ipr.link('del', index=dev_index)
return dev_index return dev_index
else:
return None
def is_up(interface): def is_up(interface):

View File

@ -149,7 +149,3 @@ class BindingDriversUtilsTest(base.TestCase):
def test_get_ipdb(self): def test_get_ipdb(self):
ip = utils.get_ipdb() ip = utils.get_ipdb()
self.assertEqual(ip, utils.get_ipdb()) self.assertEqual(ip, utils.get_ipdb())
def test_get_iproute(self):
ipr = utils.get_iproute()
self.assertEqual(ipr, utils.get_iproute())