-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtrunk.py
More file actions
311 lines (274 loc) · 11.7 KB
/
Copy pathtrunk.py
File metadata and controls
311 lines (274 loc) · 11.7 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from neutron.objects.network import NetworkSegment
from neutron.objects.ports import Port
from neutron.objects.ports import PortBindingLevel
from neutron.objects.trunk import SubPort
from neutron.services.trunk.drivers import base as trunk_base
from neutron.services.trunk.models import Trunk
from neutron_lib import exceptions as exc
from neutron_lib.api.definitions import portbindings
from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib.services.trunk import constants as trunk_consts
from oslo_config import cfg
from oslo_log import log
from neutron_understack import utils
LOG = log.getLogger(__name__)
SUPPORTED_INTERFACES = (portbindings.VIF_TYPE_OTHER,)
SUPPORTED_SEGMENTATION_TYPES = (trunk_consts.SEGMENTATION_TYPE_VLAN,)
class SubportSegmentationIDError(exc.NeutronException):
message = (
"Segmentation ID: %(seg_id)s cannot be set to the Subport: "
"%(subport_id)s as it falls outside of allowed ranges: "
"%(network_segment_ranges)s. Please use different Segmentation ID."
)
class UnderStackTrunkDriver(trunk_base.DriverBase):
def __init__(
self,
name,
interfaces,
segmentation_types,
agent_type=None,
can_trunk_bound_port=False,
):
super().__init__(
name,
interfaces,
segmentation_types,
agent_type=agent_type,
can_trunk_bound_port=can_trunk_bound_port,
)
self.undersync = self.plugin_driver.undersync
self.ironic_client = self.plugin_driver.ironic_client
@property
def is_loaded(self):
try:
return "understack" in cfg.CONF.ml2.mechanism_drivers
except cfg.NoSuchOptError:
return False
@classmethod
def create(cls, plugin_driver):
cls.plugin_driver = plugin_driver
return cls(
"understack",
SUPPORTED_INTERFACES,
SUPPORTED_SEGMENTATION_TYPES,
None,
can_trunk_bound_port=True,
)
@registry.receives(resources.TRUNK_PLUGIN, [events.AFTER_INIT])
def register(self, resource, event, trigger, payload=None):
super().register(resource, event, trigger, payload=payload)
registry.subscribe(
self.subports_added,
resources.SUBPORTS,
events.PRECOMMIT_CREATE,
cancellable=True,
)
registry.subscribe(
self.subports_added_post,
resources.SUBPORTS,
events.AFTER_CREATE,
cancellable=True,
)
registry.subscribe(
self.subports_deleted,
resources.SUBPORTS,
events.AFTER_DELETE,
cancellable=True,
)
registry.subscribe(
self.trunk_created,
resources.TRUNK,
events.PRECOMMIT_CREATE,
cancellable=True,
)
registry.subscribe(
self.trunk_deleted,
resources.TRUNK,
events.AFTER_DELETE,
cancellable=True,
)
def _handle_tenant_vlan_id_and_switchport_config(
self, subports: list[SubPort], trunk: Trunk
) -> None:
self._check_subports_segmentation_id(subports, trunk.id)
parent_port_obj = utils.fetch_port_object(trunk.port_id)
if utils.parent_port_is_bound(parent_port_obj):
self._add_subports_networks_to_parent_port_switchport(
parent_port_obj, subports
)
def _check_subports_segmentation_id(
self, subports: list[SubPort], trunk_id: str
) -> None:
"""Checks if a subport's segmentation_id is within the allowed range.
A switchport cannot have a mapped VLAN ID equal to the native VLAN ID.
Since the user specifies the VLAN ID (segmentation_id) when adding a
subport, an error is raised if it falls within any VLAN network segment
range, as these ranges are used to allocate VLAN tags for all VLAN
segments, including native VLANs.
The only case where this check is not required is for a network node
trunk, since its subport segmentation_ids are the same as the network
segment VLAN tags allocated to the subports. Therefore, there is no
possibility of conflict with the native VLAN.
"""
if trunk_id == utils.fetch_network_node_trunk_id():
return
ns_ranges = utils.allowed_tenant_vlan_id_ranges()
for subport in subports:
seg_id = subport.segmentation_id
if not utils.segmentation_id_in_ranges(seg_id, ns_ranges):
raise SubportSegmentationIDError(
seg_id=seg_id,
subport_id=subport.port_id,
network_segment_ranges=utils.printable_ranges(ns_ranges),
)
def configure_trunk(self, trunk_details: dict, port_id: str) -> None:
parent_port_obj = utils.fetch_port_object(port_id)
subports = trunk_details.get("sub_ports", [])
self._add_subports_networks_to_parent_port_switchport(
parent_port=parent_port_obj, subports=subports
)
def _handle_segment_allocation(
self, subports: list[SubPort], vlan_group_name: str, binding_host: str
) -> set:
allowed_vlan_ids = set()
for subport in subports:
subport_network_id = utils.fetch_subport_network_id(
subport_id=subport["port_id"]
)
current_segment = utils.network_segment_by_physnet(
network_id=subport_network_id,
physnet=vlan_group_name,
)
network_segment = current_segment or utils.allocate_dynamic_segment(
network_id=subport_network_id,
physnet=vlan_group_name,
)
allowed_vlan_ids.add(int(network_segment["segmentation_id"]))
utils.create_binding_profile_level(
port_id=subport["port_id"],
host=binding_host,
level=0,
segment_id=network_segment["id"],
)
return allowed_vlan_ids
def _add_subports_networks_to_parent_port_switchport(
self, parent_port: Port, subports: list[SubPort]
) -> None:
binding_profile = parent_port.bindings[0].profile
binding_host = parent_port.bindings[0].host
vlan_group_name = binding_profile.get("physical_network")
if not vlan_group_name:
raise exc.BadRequest(
resource="port",
msg=(
"physical_network is required in binding_profile for baremetal "
f"port trunk configuration. Port {parent_port.id} does not have "
"physical_network in its binding_profile. "
"Please ensure the port's binding_profile contains "
"physical_network in local_link_information."
),
)
self._handle_segment_allocation(subports, vlan_group_name, binding_host)
def clean_trunk(
self, trunk_details: dict, binding_profile: dict, host: str
) -> None:
subports = trunk_details.get("sub_ports", [])
self._handle_subports_removal(
binding_profile=binding_profile,
binding_host=host,
subports=subports,
invoke_undersync=False,
)
def _clean_parent_port_switchport_config(
self, trunk: Trunk, subports: list[SubPort]
) -> None:
parent_port_obj = utils.fetch_port_object(trunk.port_id)
if not utils.parent_port_is_bound(parent_port_obj):
return
binding_profile = parent_port_obj.bindings[0].profile
binding_host = parent_port_obj.bindings[0].host
vlan_group_name = binding_profile.get("physical_network")
if not vlan_group_name:
port_id = parent_port_obj.id
raise exc.BadRequest(
resource="port",
msg=(
"physical_network is required in binding_profile for baremetal "
f"port trunk configuration. Port {port_id} does not have "
"physical_network in its binding_profile. "
"Please ensure the port's binding_profile contains "
"physical_network in local_link_information."
),
)
self._handle_subports_removal(
binding_profile=binding_profile,
binding_host=binding_host,
subports=subports,
vlan_group_name=vlan_group_name,
)
def _delete_binding_level(self, port_id: str, host: str) -> PortBindingLevel:
binding_level = utils.port_binding_level_by_port_id(port_id, host)
binding_level.delete()
return binding_level
def _delete_unused_segment(self, segment_id: str) -> NetworkSegment:
network_segment = utils.network_segment_by_id(segment_id)
if not utils.ports_bound_to_segment(
segment_id
) and utils.is_dynamic_network_segment(segment_id):
utils.release_dynamic_segment(segment_id)
return network_segment
def _handle_segment_deallocation(self, subports: list[SubPort], host: str):
for subport in subports:
subport_binding_level = self._delete_binding_level(subport["port_id"], host)
self._delete_unused_segment(subport_binding_level.segment_id)
def _handle_subports_removal(
self,
binding_profile: dict,
binding_host: str,
subports: list[SubPort],
invoke_undersync: bool = True,
vlan_group_name: str | None = None,
) -> None:
self._handle_segment_deallocation(subports, binding_host)
if invoke_undersync and vlan_group_name:
self.undersync.sync(vlan_group_name)
def subports_added(self, resource, event, trunk_plugin, payload):
trunk = payload.states[0]
subports = payload.metadata["subports"]
self._handle_tenant_vlan_id_and_switchport_config(subports, trunk)
def subports_added_post(self, resource, event, trunk_plugin, payload):
trunk = payload.states[0]
parent_port = utils.fetch_port_object(trunk.port_id)
if utils.parent_port_is_bound(parent_port):
binding_profile = parent_port.bindings[0].profile
vlan_group_name = binding_profile.get("physical_network")
if not vlan_group_name:
port_id = parent_port.id
raise exc.BadRequest(
resource="port",
msg=(
"physical_network is required in binding_profile for "
f"baremetal port trunk configuration. Port {port_id} does not "
"have physical_network in its binding_profile. "
"Please ensure the port's binding_profile contains "
"physical_network in local_link_information."
),
)
LOG.debug("subports_added_post found vlan_group_name=%s", vlan_group_name)
self.undersync.sync(vlan_group_name)
def subports_deleted(self, resource, event, trunk_plugin, payload):
trunk = payload.states[0]
subports = payload.metadata["subports"]
self._clean_parent_port_switchport_config(trunk, subports)
def trunk_created(self, resource, event, trunk_plugin, payload):
trunk = payload.latest_state
subports = trunk.sub_ports
if subports:
self._handle_tenant_vlan_id_and_switchport_config(subports, trunk)
def trunk_deleted(self, resource, event, trunk_plugin, payload):
trunk = payload.states[0]
subports = trunk.sub_ports
if subports:
self._clean_parent_port_switchport_config(trunk, subports)