Better utilize the L3 Namespace class

The Namespace class can create namespaces and enable
IP forwarding.  It also already initializes an ip_lib
class instance to run commands in the root namespace.
Change the DVR FIP code to use it.

Change-Id: I625c919af5b009999987382df64cd3491a932fe5
This commit is contained in:
Brian Haley 2016-06-06 15:00:23 -04:00
parent 3ecbd74fb8
commit 3aed33275d
2 changed files with 11 additions and 16 deletions

View File

@ -132,13 +132,13 @@ class FipNamespace(namespaces.Namespace):
ip_wrapper.netns.execute(cmd, check_exit_code=False)
def create(self):
# TODO(Carl) Get this functionality from mlavelle's namespace baseclass
LOG.debug("DVR: add fip namespace: %s", self.name)
ip_wrapper_root = ip_lib.IPWrapper()
ip_wrapper = ip_wrapper_root.ensure_namespace(self.get_name())
# parent class will ensure the namespace exists and turn-on forwarding
super(FipNamespace, self).create()
# Somewhere in the 3.19 kernel timeframe ip_nonlocal_bind was
# changed to be a per-namespace attribute. To be backwards
# compatible we need to try both if at first we fail.
ip_wrapper = ip_lib.IPWrapper(namespace=self.name)
try:
ip_wrapper.netns.execute(['sysctl',
'-w',
@ -149,15 +149,10 @@ class FipNamespace(namespaces.Namespace):
LOG.debug('DVR: fip namespace (%s) does not support setting '
'net.ipv4.ip_nonlocal_bind, trying in root namespace',
self.name)
ip_wrapper_root.netns.execute(['sysctl',
'-w',
'net.ipv4.ip_nonlocal_bind=1'],
run_as_root=True)
ip_wrapper.netns.execute(['sysctl', '-w', 'net.ipv4.ip_forward=1'])
if self.use_ipv6:
ip_wrapper.netns.execute(['sysctl', '-w',
'net.ipv6.conf.all.forwarding=1'])
self.ip_wrapper_root.netns.execute(['sysctl',
'-w',
'net.ipv4.ip_nonlocal_bind=1'],
run_as_root=True)
# no connection tracking needed in fip namespace
self._iptables_manager.ipv4['raw'].add_rule('PREROUTING',

View File

@ -165,10 +165,10 @@ class TestDvrFipNs(base.BaseTestCase):
@mock.patch.object(ip_lib.IpNetnsCommand, 'exists')
def _test_create(self, old_kernel, exists, execute, IPTables):
exists.return_value = True
# There are up to four sysctl calls - two for ip_nonlocal_bind,
# and two to enable forwarding
execute.side_effect = [RuntimeError if old_kernel else None,
None, None, None]
# There are up to four sysctl calls - two to enable forwarding,
# and two for ip_nonlocal_bind
execute.side_effect = [None, None,
RuntimeError if old_kernel else None, None]
self.fip_ns._iptables_manager = IPTables()
self.fip_ns.create()