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._i18n import _LE
19 from neutron.agent.linux import ip_lib
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 def build_ns_name(prefix, identifier):
31 """Builds a namespace name from the given prefix and identifier
33 :param prefix: The prefix which must end with '-' for legacy reasons
34 :param identifier: The id associated with the namespace
36 return prefix + identifier
39 def get_prefix_from_ns_name(ns_name):
40 """Parses prefix from prefix-identifier
42 :param ns_name: The name of a namespace
43 :returns: The prefix ending with a '-' or None if there is no '-'
45 dash_index = ns_name.find('-')
47 return ns_name[:dash_index + 1]
50 def get_id_from_ns_name(ns_name):
51 """Parses identifier from prefix-identifier
53 :param ns_name: The name of a namespace
54 :returns: Identifier or None if there is no - to end the prefix
56 dash_index = ns_name.find('-')
58 return ns_name[dash_index + 1:]
61 class Namespace(object):
63 def __init__(self, name, agent_conf, driver, use_ipv6):
65 self.ip_wrapper_root = ip_lib.IPWrapper()
66 self.agent_conf = agent_conf
68 self.use_ipv6 = use_ipv6
71 ip_wrapper = self.ip_wrapper_root.ensure_namespace(self.name)
72 cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1']
73 ip_wrapper.netns.execute(cmd)
75 cmd = ['sysctl', '-w', 'net.ipv6.conf.all.forwarding=1']
76 ip_wrapper.netns.execute(cmd)
80 self.ip_wrapper_root.netns.delete(self.name)
82 msg = _LE('Failed trying to delete namespace: %s')
83 LOG.exception(msg, self.name)
86 class RouterNamespace(Namespace):
88 def __init__(self, router_id, agent_conf, driver, use_ipv6):
89 self.router_id = router_id
90 name = self._get_ns_name(router_id)
91 super(RouterNamespace, self).__init__(
92 name, agent_conf, driver, use_ipv6)
95 def _get_ns_name(cls, router_id):
96 return build_ns_name(NS_PREFIX, router_id)
99 ns_ip = ip_lib.IPWrapper(namespace=self.name)
100 for d in ns_ip.get_devices(exclude_loopback=True):
101 if d.name.startswith(INTERNAL_DEV_PREFIX):
102 # device is on default bridge
103 self.driver.unplug(d.name, namespace=self.name,
104 prefix=INTERNAL_DEV_PREFIX)
105 elif d.name.startswith(ROUTER_2_FIP_DEV_PREFIX):
106 ns_ip.del_veth(d.name)
107 elif d.name.startswith(EXTERNAL_DEV_PREFIX):
110 bridge=self.agent_conf.external_network_bridge,
112 prefix=EXTERNAL_DEV_PREFIX)
114 super(RouterNamespace, self).delete()