summaryrefslogtreecommitdiff
path: root/nova/network/l3.py
blob: 4fac2f135b257ac939eadbbc19d592103e1013a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 Nicira Networks, Inc
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from nova.network import linux_net
from nova.openstack.common import log as logging
from nova import utils

LOG = logging.getLogger(__name__)


class L3Driver(object):
    """Abstract class that defines a generic L3 API."""

    def __init__(self, l3_lib=None):
        raise NotImplementedError()

    def initialize(self, **kwargs):
        """Set up basic L3 networking functionality."""
        raise NotImplementedError()

    def initialize_network(self, network):
        """Enable rules for a specific network."""
        raise NotImplementedError()

    def initialize_gateway(self, network):
        """Set up a gateway on this network."""
        raise NotImplementedError()

    def remove_gateway(self, network_ref):
        """Remove an existing gateway on this network."""
        raise NotImplementedError()

    def is_initialized(self):
        """:returns: True/False (whether the driver is initialized)."""
        raise NotImplementedError()

    def add_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                        network=None):
        """Add a floating IP bound to the fixed IP with an optional
           l3_interface_id.  Some drivers won't care about the
           l3_interface_id so just pass None in that case. Network
           is also an optional parameter.
        """
        raise NotImplementedError()

    def remove_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                           network=None):
        raise NotImplementedError()

    def add_vpn(self, public_ip, port, private_ip):
        raise NotImplementedError()

    def remove_vpn(self, public_ip, port, private_ip):
        raise NotImplementedError()

    def clean_conntrack(self, fixed_ip):
        raise NotImplementedError()

    def teardown(self):
        raise NotImplementedError()


class LinuxNetL3(L3Driver):
    """L3 driver that uses linux_net as the backend."""
    def __init__(self):
        self.initialized = False

    def initialize(self, **kwargs):
        if self.initialized:
            return
        LOG.debug("Initializing linux_net L3 driver")
        fixed_range = kwargs.get('fixed_range', False)
        networks = kwargs.get('networks', None)
        if not fixed_range and networks is not None:
            for network in networks:
                self.initialize_network(network['cidr'])
        else:
            linux_net.init_host()
        linux_net.ensure_metadata_ip()
        linux_net.metadata_forward()
        self.initialized = True

    def is_initialized(self):
        return self.initialized

    def initialize_network(self, cidr):
        linux_net.init_host(cidr)

    def initialize_gateway(self, network_ref):
        mac_address = utils.generate_mac_address()
        dev = linux_net.plug(network_ref, mac_address,
                    gateway=(network_ref['gateway'] is not None))
        linux_net.initialize_gateway_device(dev, network_ref)

    def remove_gateway(self, network_ref):
        linux_net.unplug(network_ref)

    def add_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                        network=None):
        linux_net.ensure_floating_forward(floating_ip, fixed_ip,
                                          l3_interface_id, network)
        linux_net.bind_floating_ip(floating_ip, l3_interface_id)

    def remove_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                           network=None):
        linux_net.unbind_floating_ip(floating_ip, l3_interface_id)
        linux_net.remove_floating_forward(floating_ip, fixed_ip,
                                          l3_interface_id, network)

    def add_vpn(self, public_ip, port, private_ip):
        linux_net.ensure_vpn_forward(public_ip, port, private_ip)

    def remove_vpn(self, public_ip, port, private_ip):
        # Linux net currently doesn't implement any way of removing
        # the VPN forwarding rules
        pass

    def clean_conntrack(self, fixed_ip):
        linux_net.clean_conntrack(fixed_ip)

    def teardown(self):
        pass


class NullL3(L3Driver):
    """The L3 driver that doesn't do anything.  This class can be used when
       nova-network should not manipulate L3 forwarding at all (e.g., in a Flat
       or FlatDHCP scenario).
    """
    def __init__(self):
        pass

    def initialize(self, **kwargs):
        pass

    def is_initialized(self):
        return True

    def initialize_network(self, cidr):
        pass

    def initialize_gateway(self, network_ref):
        pass

    def remove_gateway(self, network_ref):
        pass

    def add_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                        network=None):
        pass

    def remove_floating_ip(self, floating_ip, fixed_ip, l3_interface_id,
                           network=None):
        pass

    def add_vpn(self, public_ip, port, private_ip):
        pass

    def remove_vpn(self, public_ip, port, private_ip):
        pass

    def clean_conntrack(self, fixed_ip):
        pass

    def teardown(self):
        pass