ccb0bf77d1eb3ba2ff928a2d84ed61a0082b0200
[openstack-build/neutron-build.git] / neutron / agent / l3 / namespaces.py
1 # Copyright 2015 Hewlett-Packard Development Company, L.P.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 #    under the License.
14 #
15
16 from oslo_log import log as logging
17
18 from neutron._i18n import _LE
19 from neutron.agent.linux import ip_lib
20
21 LOG = logging.getLogger(__name__)
22
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-'
28
29
30 def build_ns_name(prefix, identifier):
31     """Builds a namespace name from the given prefix and identifier
32
33     :param prefix: The prefix which must end with '-' for legacy reasons
34     :param identifier: The id associated with the namespace
35     """
36     return prefix + identifier
37
38
39 def get_prefix_from_ns_name(ns_name):
40     """Parses prefix from prefix-identifier
41
42     :param ns_name: The name of a namespace
43     :returns: The prefix ending with a '-' or None if there is no '-'
44     """
45     dash_index = ns_name.find('-')
46     if 0 <= dash_index:
47         return ns_name[:dash_index + 1]
48
49
50 def get_id_from_ns_name(ns_name):
51     """Parses identifier from prefix-identifier
52
53     :param ns_name: The name of a namespace
54     :returns: Identifier or None if there is no - to end the prefix
55     """
56     dash_index = ns_name.find('-')
57     if 0 <= dash_index:
58         return ns_name[dash_index + 1:]
59
60
61 class Namespace(object):
62
63     def __init__(self, name, agent_conf, driver, use_ipv6):
64         self.name = name
65         self.ip_wrapper_root = ip_lib.IPWrapper()
66         self.agent_conf = agent_conf
67         self.driver = driver
68         self.use_ipv6 = use_ipv6
69
70     def create(self):
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)
74         if self.use_ipv6:
75             cmd = ['sysctl', '-w', 'net.ipv6.conf.all.forwarding=1']
76             ip_wrapper.netns.execute(cmd)
77
78     def delete(self):
79         try:
80             self.ip_wrapper_root.netns.delete(self.name)
81         except RuntimeError:
82             msg = _LE('Failed trying to delete namespace: %s')
83             LOG.exception(msg, self.name)
84
85
86 class RouterNamespace(Namespace):
87
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)
93
94     @classmethod
95     def _get_ns_name(cls, router_id):
96         return build_ns_name(NS_PREFIX, router_id)
97
98     def delete(self):
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):
108                 self.driver.unplug(
109                     d.name,
110                     bridge=self.agent_conf.external_network_bridge,
111                     namespace=self.name,
112                     prefix=EXTERNAL_DEV_PREFIX)
113
114         super(RouterNamespace, self).delete()