From 3fc52dee3cdc336d2fa4e2887e5096c2a00b1987 Mon Sep 17 00:00:00 2001 From: Carl Baldwin Date: Thu, 9 Jul 2015 21:08:19 +0000 Subject: [PATCH] Move more snat code to dvr class that does snat A few methods were left in the wrong class when splitting up the dvr classes. This commit reduces the amount of dependency between the two. Change-Id: Id1b4f4e99a5c51576eddadd5eb0c973c0d5b46b8 --- neutron/agent/l3/dvr_edge_router.py | 33 ++++++++++++++++++++++- neutron/agent/l3/dvr_local_router.py | 30 +-------------------- neutron/tests/unit/agent/l3/test_agent.py | 3 +-- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/neutron/agent/l3/dvr_edge_router.py b/neutron/agent/l3/dvr_edge_router.py index 167df080a..dd128dc88 100644 --- a/neutron/agent/l3/dvr_edge_router.py +++ b/neutron/agent/l3/dvr_edge_router.py @@ -28,6 +28,7 @@ class DvrEdgeRouter(dvr_local_router.DvrLocalRouter): def __init__(self, agent, host, *args, **kwargs): super(DvrEdgeRouter, self).__init__(agent, host, *args, **kwargs) self.snat_namespace = None + self.snat_iptables_manager = None def external_gateway_added(self, ex_gw_port, interface_name): super(DvrEdgeRouter, self).external_gateway_added( @@ -145,4 +146,34 @@ class DvrEdgeRouter(dvr_local_router.DvrLocalRouter): return long_name[:self.driver.DEV_NAME_LEN] def _is_this_snat_host(self): - return self.get_gw_port_host() == self.host + return self._get_gw_port_host() == self.host + + def _get_gw_port_host(self): + host = self.router.get('gw_port_host') + if not host: + LOG.debug("gw_port_host missing from router: %s", + self.router['id']) + return host + + def _handle_router_snat_rules(self, ex_gw_port, + interface_name, action): + if not self.snat_iptables_manager: + LOG.debug("DVR router: no snat rules to be handled") + return + + with self.snat_iptables_manager.defer_apply(): + self._empty_snat_chains(self.snat_iptables_manager) + + # NOTE DVR doesn't add the jump to float snat like the super class. + + self._add_snat_rules(ex_gw_port, self.snat_iptables_manager, + interface_name, action) + + def perform_snat_action(self, snat_callback, *args): + # NOTE DVR skips this step in a few cases... + if not self.get_ex_gw_port(): + return + if self._get_gw_port_host() != self.host: + return + + super(DvrEdgeRouter, self).perform_snat_action(snat_callback, *args) diff --git a/neutron/agent/l3/dvr_local_router.py b/neutron/agent/l3/dvr_local_router.py index e306c7c91..ca72d0d95 100755 --- a/neutron/agent/l3/dvr_local_router.py +++ b/neutron/agent/l3/dvr_local_router.py @@ -39,7 +39,6 @@ class DvrLocalRouter(router.RouterInfo): self.host = host self.floating_ips_dict = {} - self.snat_iptables_manager = None # Linklocal subnet for router and floating IP namespace link self.rtr_fip_subnet = None self.dist_fip_count = None @@ -291,13 +290,6 @@ class DvrLocalRouter(router.RouterInfo): """Removes rules and routes for SNAT redirection.""" self._snat_redirect_modify(gateway, sn_port, sn_int, is_add=False) - def get_gw_port_host(self): - host = self.router.get('gw_port_host') - if not host: - LOG.debug("gw_port_host missing from router: %s", - self.router['id']) - return host - def internal_network_added(self, port): super(DvrLocalRouter, self).internal_network_added(port) @@ -387,27 +379,7 @@ class DvrLocalRouter(router.RouterInfo): def _handle_router_snat_rules(self, ex_gw_port, interface_name, action): - if not self.snat_iptables_manager: - LOG.debug("DVR router: no snat rules to be handled") - return - - with self.snat_iptables_manager.defer_apply(): - self._empty_snat_chains(self.snat_iptables_manager) - - # NOTE DVR doesn't add the jump to float snat like the super class. - - self._add_snat_rules(ex_gw_port, self.snat_iptables_manager, - interface_name, action) - - def perform_snat_action(self, snat_callback, *args): - # NOTE DVR skips this step in a few cases... - if not self.get_ex_gw_port(): - return - if self.get_gw_port_host() != self.host: - return - - super(DvrLocalRouter, - self).perform_snat_action(snat_callback, *args) + pass def process_external(self, agent): ex_gw_port = self.get_ex_gw_port() diff --git a/neutron/tests/unit/agent/l3/test_agent.py b/neutron/tests/unit/agent/l3/test_agent.py index b683727fd..73dcd42db 100644 --- a/neutron/tests/unit/agent/l3/test_agent.py +++ b/neutron/tests/unit/agent/l3/test_agent.py @@ -29,7 +29,6 @@ from neutron.agent.common import config as agent_config from neutron.agent.l3 import agent as l3_agent from neutron.agent.l3 import config as l3_config from neutron.agent.l3 import dvr_edge_router as dvr_router -from neutron.agent.l3 import dvr_local_router from neutron.agent.l3 import dvr_snat_ns from neutron.agent.l3 import ha from neutron.agent.l3 import legacy_router @@ -1488,7 +1487,7 @@ class TestBasicRouterOperations(BasicRouterOperationsFramework): **self.ri_kwargs) ri.iptables_manager = mock.Mock() - with mock.patch.object(dvr_local_router.LOG, + with mock.patch.object(dvr_router.LOG, 'debug') as log_debug: ri._handle_router_snat_rules(mock.ANY, mock.ANY, mock.ANY) self.assertIsNone(ri.snat_iptables_manager) -- 2.45.2