Skip to content

Commit 1a6d117

Browse files
cardoestevekeay
authored andcommitted
Release native VLAN segment when subport collision fails binding
The native VLAN is only known once the dynamic segment has been allocated, so on a first bind the subport collision check in configure_trunk() can only run afterwards. If it raised, the freshly allocated segment was left on the network. ML2 swallows exceptions from bind_port, so nothing gave it back until the port was deleted, and every retry reused it and failed the same way. Release the segment on failure, but only if this bind allocated it and no other port is bound to it. A segment that already existed is not ours to release.
1 parent 7fed28e commit 1a6d117

2 files changed

Lines changed: 123 additions & 8 deletions

File tree

‎python/neutron-understack/neutron_understack/tests/test_understack_mech.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,110 @@ def test_with_no_trunk(
431431
next_segments_to_bind=[vlan_network_segment],
432432
)
433433

434+
def test_releases_allocated_segment_when_trunk_check_fails(
435+
self,
436+
mocker,
437+
port_context,
438+
understack_driver,
439+
vlan_network_segment,
440+
):
441+
"""A native VLAN that collides with a subport must not leak its segment."""
442+
mocker.patch.object(
443+
port_context, "allocate_dynamic_segment", return_value=vlan_network_segment
444+
)
445+
mocker.patch(f"{MECH_UTILS}.ports_bound_to_segment", return_value=[])
446+
understack_driver.trunk_driver.configure_trunk.side_effect = (
447+
trunk.SubportSegmentationIDError(
448+
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
449+
subport_id="subport-a",
450+
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
451+
)
452+
)
453+
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
454+
port_context._prepare_to_bind(port_context.network.network_segments)
455+
456+
with pytest.raises(trunk.SubportSegmentationIDError):
457+
understack_driver._bind_port_segment(
458+
port_context,
459+
next(
460+
s
461+
for s in port_context.network.network_segments
462+
if s[api.NETWORK_TYPE] == "vxlan"
463+
),
464+
)
465+
466+
release.assert_called_once_with(vlan_network_segment[api.ID])
467+
468+
def test_keeps_allocated_segment_when_other_ports_are_bound(
469+
self,
470+
mocker,
471+
port_context,
472+
understack_driver,
473+
vlan_network_segment,
474+
):
475+
"""allocate_dynamic_segment can return a segment another port is using."""
476+
mocker.patch.object(
477+
port_context, "allocate_dynamic_segment", return_value=vlan_network_segment
478+
)
479+
mocker.patch(
480+
f"{MECH_UTILS}.ports_bound_to_segment", return_value=["other-port"]
481+
)
482+
understack_driver.trunk_driver.configure_trunk.side_effect = (
483+
trunk.SubportSegmentationIDError(
484+
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
485+
subport_id="subport-a",
486+
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
487+
)
488+
)
489+
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
490+
port_context._prepare_to_bind(port_context.network.network_segments)
491+
492+
with pytest.raises(trunk.SubportSegmentationIDError):
493+
understack_driver._bind_port_segment(
494+
port_context,
495+
next(
496+
s
497+
for s in port_context.network.network_segments
498+
if s[api.NETWORK_TYPE] == "vxlan"
499+
),
500+
)
501+
502+
release.assert_not_called()
503+
504+
def test_keeps_reused_segment_when_trunk_check_fails(
505+
self,
506+
mocker,
507+
port_context,
508+
understack_driver,
509+
vlan_network_segment,
510+
):
511+
"""A segment that already existed is not ours to release."""
512+
mocker.patch(
513+
f"{MECH_UTILS}.vlan_segment_for_physnet",
514+
return_value=vlan_network_segment,
515+
)
516+
understack_driver.trunk_driver.configure_trunk.side_effect = (
517+
trunk.SubportSegmentationIDError(
518+
seg_id=vlan_network_segment[api.SEGMENTATION_ID],
519+
subport_id="subport-a",
520+
physical_network=vlan_network_segment[api.PHYSICAL_NETWORK],
521+
)
522+
)
523+
release = mocker.patch(f"{MECH_UTILS}.release_dynamic_segment")
524+
port_context._prepare_to_bind(port_context.network.network_segments)
525+
526+
with pytest.raises(trunk.SubportSegmentationIDError):
527+
understack_driver._bind_port_segment(
528+
port_context,
529+
next(
530+
s
531+
for s in port_context.network.network_segments
532+
if s[api.NETWORK_TYPE] == "vxlan"
533+
),
534+
)
535+
536+
release.assert_not_called()
537+
434538
def test_refuses_unsupported_vnic_type(
435539
self, mocker, port_context, understack_driver
436540
):

‎python/neutron-understack/neutron_understack/understack_mech.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,32 +401,43 @@ def _bind_port_segment(self, context: PortContext, segment):
401401
physnet,
402402
)
403403

404+
freshly_allocated_segment = None
404405
current_vlan_segment = utils.vlan_segment_for_physnet(context, physnet)
406+
405407
if current_vlan_segment:
406408
LOG.info(
407409
"vlan segment: %(segment)s already preset for physnet: %(physnet)s",
408410
{"segment": current_vlan_segment, "physnet": physnet},
409411
)
410412
dynamic_segment = current_vlan_segment
411413
else:
412-
dynamic_segment = context.allocate_dynamic_segment(
414+
freshly_allocated_segment = context.allocate_dynamic_segment(
413415
segment={
414416
"network_type": p_const.TYPE_VLAN,
415417
"physical_network": physnet,
416418
},
417419
)
420+
dynamic_segment = freshly_allocated_segment
418421

419422
LOG.debug("bind_port_segment: Native VLAN segment %s", dynamic_segment)
420423

421424
trunk_details = context.current.get("trunk_details") or {}
422425
port_id = context.current["id"]
423-
if trunk_details:
424-
self.trunk_driver.configure_trunk(
425-
trunk_details=trunk_details,
426-
port_id=port_id,
427-
physical_network=physnet,
428-
native_vlan_id=dynamic_segment[api.SEGMENTATION_ID],
429-
)
426+
try:
427+
if trunk_details:
428+
self.trunk_driver.configure_trunk(
429+
trunk_details=trunk_details,
430+
port_id=port_id,
431+
physical_network=physnet,
432+
native_vlan_id=dynamic_segment[api.SEGMENTATION_ID],
433+
)
434+
except Exception:
435+
# Don't leak the dynamic segment we just allocated
436+
if freshly_allocated_segment and not utils.ports_bound_to_segment(
437+
freshly_allocated_segment[api.ID]
438+
):
439+
utils.release_dynamic_segment(freshly_allocated_segment[api.ID])
440+
raise
430441

431442
LOG.debug("continue_binding for segment: %s", segment)
432443
context.continue_binding(

0 commit comments

Comments
 (0)