1 # Copyright (c) 2013 OpenStack Foundation
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 from oslo_config import cfg
17 from oslo_db import exception as db_exc
18 from oslo_log import log
20 import sqlalchemy as sa
22 from neutron._i18n import _, _LI, _LW
23 from neutron.common import exceptions as exc
24 from neutron.db import model_base
25 from neutron.plugins.common import constants as p_const
26 from neutron.plugins.ml2 import driver_api as api
27 from neutron.plugins.ml2.drivers import helpers
29 LOG = log.getLogger(__name__)
32 cfg.ListOpt('flat_networks',
34 help=_("List of physical_network names with which flat "
35 "networks can be created. Use default '*' to allow "
36 "flat networks with arbitrary physical_network names. "
37 "Use an empty list to disable flat networks."))
40 cfg.CONF.register_opts(flat_opts, "ml2_type_flat")
43 class FlatAllocation(model_base.BASEV2):
44 """Represent persistent allocation state of a physical network.
46 If a record exists for a physical network, then that physical
47 network has been allocated as a flat network.
50 __tablename__ = 'ml2_flat_allocations'
52 physical_network = sa.Column(sa.String(64), nullable=False,
56 class FlatTypeDriver(helpers.BaseTypeDriver):
57 """Manage state for flat networks with ML2.
59 The FlatTypeDriver implements the 'flat' network_type. Flat
60 network segments provide connectivity between VMs and other
61 devices using any connected IEEE 802.1D conformant
62 physical_network, without the use of VLAN tags, tunneling, or
63 other segmentation mechanisms. Therefore at most one flat network
64 segment can exist on each available physical_network.
68 super(FlatTypeDriver, self).__init__()
69 self._parse_networks(cfg.CONF.ml2_type_flat.flat_networks)
71 def _parse_networks(self, entries):
72 self.flat_networks = entries
73 if '*' in self.flat_networks:
74 LOG.info(_LI("Arbitrary flat physical_network names allowed"))
75 self.flat_networks = None
76 elif not self.flat_networks:
77 LOG.info(_LI("Flat networks are disabled"))
79 LOG.info(_LI("Allowable flat physical_network names: %s"),
83 return p_const.TYPE_FLAT
86 LOG.info(_LI("ML2 FlatTypeDriver initialization complete"))
88 def is_partial_segment(self, segment):
91 def validate_provider_segment(self, segment):
92 physical_network = segment.get(api.PHYSICAL_NETWORK)
93 if not physical_network:
94 msg = _("physical_network required for flat provider network")
95 raise exc.InvalidInput(error_message=msg)
96 if self.flat_networks is not None and not self.flat_networks:
97 msg = _("Flat provider networks are disabled")
98 raise exc.InvalidInput(error_message=msg)
99 if self.flat_networks and physical_network not in self.flat_networks:
100 msg = (_("physical_network '%s' unknown for flat provider network")
102 raise exc.InvalidInput(error_message=msg)
104 for key, value in six.iteritems(segment):
105 if value and key not in [api.NETWORK_TYPE,
106 api.PHYSICAL_NETWORK]:
107 msg = _("%s prohibited for flat provider network") % key
108 raise exc.InvalidInput(error_message=msg)
110 def reserve_provider_segment(self, session, segment):
111 physical_network = segment[api.PHYSICAL_NETWORK]
112 with session.begin(subtransactions=True):
114 LOG.debug("Reserving flat network on physical "
115 "network %s", physical_network)
116 alloc = FlatAllocation(physical_network=physical_network)
118 except db_exc.DBDuplicateEntry:
119 raise exc.FlatNetworkInUse(
120 physical_network=physical_network)
121 segment[api.MTU] = self.get_mtu(alloc.physical_network)
124 def allocate_tenant_segment(self, session):
125 # Tenant flat networks are not supported.
128 def release_segment(self, session, segment):
129 physical_network = segment[api.PHYSICAL_NETWORK]
130 with session.begin(subtransactions=True):
131 count = (session.query(FlatAllocation).
132 filter_by(physical_network=physical_network).
135 LOG.debug("Releasing flat network on physical network %s",
138 LOG.warning(_LW("No flat network found on physical network %s"),
141 def get_mtu(self, physical_network):
142 seg_mtu = super(FlatTypeDriver, self).get_mtu()
146 if physical_network in self.physnet_mtus:
147 mtu.append(int(self.physnet_mtus[physical_network]))
148 return min(mtu) if mtu else 0