1
# Copyright 2015 Hewlett-Packard Development Company, L.P.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
16
from oslo_log import log as logging
18
from neutron.agent.linux import ip_lib
19
from neutron.i18n import _LE
21
LOG = logging.getLogger(__name__)
23
NS_PREFIX = 'qrouter-'
24
INTERNAL_DEV_PREFIX = 'qr-'
25
EXTERNAL_DEV_PREFIX = 'qg-'
26
# TODO(Carl) It is odd that this file needs this. It is a dvr detail.
27
ROUTER_2_FIP_DEV_PREFIX = 'rfp-'
30
class Namespace(object):
32
def __init__(self, name, agent_conf, driver, use_ipv6):
34
self.ip_wrapper_root = ip_lib.IPWrapper()
35
self.agent_conf = agent_conf
37
self.use_ipv6 = use_ipv6
40
ip_wrapper = self.ip_wrapper_root.ensure_namespace(self.name)
41
cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1']
42
ip_wrapper.netns.execute(cmd)
44
cmd = ['sysctl', '-w', 'net.ipv6.conf.all.forwarding=1']
45
ip_wrapper.netns.execute(cmd)
48
if self.agent_conf.router_delete_namespaces:
50
self.ip_wrapper_root.netns.delete(self.name)
52
msg = _LE('Failed trying to delete namespace: %s')
53
LOG.exception(msg, self.name)
56
class RouterNamespace(Namespace):
58
def __init__(self, router_id, agent_conf, driver, use_ipv6):
59
self.router_id = router_id
60
name = self._get_ns_name(router_id)
61
super(RouterNamespace, self).__init__(
62
name, agent_conf, driver, use_ipv6)
65
def _get_ns_name(router_id):
66
return (NS_PREFIX + router_id)
69
ns_ip = ip_lib.IPWrapper(namespace=self.name)
70
for d in ns_ip.get_devices(exclude_loopback=True):
71
if d.name.startswith(INTERNAL_DEV_PREFIX):
72
# device is on default bridge
73
self.driver.unplug(d.name, namespace=self.name,
74
prefix=INTERNAL_DEV_PREFIX)
75
elif d.name.startswith(ROUTER_2_FIP_DEV_PREFIX):
76
ns_ip.del_veth(d.name)
77
elif d.name.startswith(EXTERNAL_DEV_PREFIX):
80
bridge=self.agent_conf.external_network_bridge,
82
prefix=EXTERNAL_DEV_PREFIX)
84
super(RouterNamespace, self).delete()