From 61b82c994528595a61833f2b8e315cae60c4b90c Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Tue, 13 Oct 2026 10:37:41 +0200 Subject: [PATCH 1/9] add interfaces to l2x err template --- cosmo/tests/test_case_l2x_err_template.yaml | 92 ++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/cosmo/tests/test_case_l2x_err_template.yaml b/cosmo/tests/test_case_l2x_err_template.yaml index e9a6e003..0bc1d95c 100644 --- a/cosmo/tests/test_case_l2x_err_template.yaml +++ b/cosmo/tests/test_case_l2x_err_template.yaml @@ -4,7 +4,97 @@ device_list: slug: S9510-28DC-BN0B __typename: DeviceType id: '17649' - interfaces: [] + interfaces: + - custom_fields: + inner_tag: null + outer_tag: null + __typename: InterfaceType + description: '' + enabled: true + id: '30747' + ip_addresses: [ ] + lag: + __typename: InterfaceType + id: '30820' + name: ae0 + mac_address: 94:BF:41:41:41:F3 + mode: null + mtu: null + name: et-0/0/0 + tagged_vlans: [ ] + tags: [ ] + type: A_100GBASE_X_QSFP28 + untagged_vlan: null + vrf: null + - custom_fields: + inner_tag: null + outer_tag: null + __typename: InterfaceType + description: '' + enabled: true + id: '30748' + ip_addresses: [ ] + lag: + __typename: InterfaceType + id: '30820' + name: ae0 + mac_address: 94:BF:41:41:41:F7 + mode: null + mtu: null + name: et-0/0/1 + tagged_vlans: [ ] + tags: [ ] + type: A_100GBASE_X_QSFP28 + untagged_vlan: null + vrf: null + - custom_fields: + bpdufilter: null + inner_tag: null + outer_tag: null + storm_control__broadcast: null + storm_control__multicast: null + storm_control__unknown_unicast: null + __typename: InterfaceType + description: CBSC1101 + enabled: true + id: '30820' + ip_addresses: [ ] + lag: null + mac_address: 94:BF:94:3E:88:D4 + mode: TAGGED + mtu: null + name: ae0 + tagged_vlans: [ ] + tags: [ ] + type: LAG + untagged_vlan: null + vrf: null + - custom_fields: + bpdufilter: false + inner_tag: null + outer_tag: null + storm_control__broadcast: null + storm_control__multicast: null + storm_control__unknown_unicast: null + description: 'Customer: Test-VLAN2' + __typename: InterfaceType + enabled: false + id: '30821' + ip_addresses: [ ] + lag: null + mac_address: null + mode: ACCESS + mtu: null + name: ae0.150 + tagged_vlans: [ ] + tags: [ ] + type: VIRTUAL + untagged_vlan: + __typename: VLANType + id: '1159' + name: TEST-VLAN2 + vid: 150 + vrf: null name: TEST0001 platform: __typename: PlatformType From af25d636901e981a304ce5715e9a74f637d60ada Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Mon, 12 Oct 2026 16:02:11 +0200 Subject: [PATCH 2/9] write test for service MTU enforcement --- cosmo/tests/test_serializer.py | 84 ++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/cosmo/tests/test_serializer.py b/cosmo/tests/test_serializer.py index 6ced29d7..01e6444c 100644 --- a/cosmo/tests/test_serializer.py +++ b/cosmo/tests/test_serializer.py @@ -285,6 +285,90 @@ def test_l2vpn_errors(capsys, mock_cosmo_config_fixture): ): serialize(vpws_non_interface_term) + l2x_incoherent_explicit_logical_mtu = copy.deepcopy(template) + l2x_incoherent_explicit_logical_mtu["l2vpn_list"].append( + { + "__typename": "L2VPNType", + "id": "55", + "identifier": 123456, + "name": "WAN: explicit logical MTU higher than max computed implicit phy MTU", + "type": "VPWS", + "terminations": [ + { + "__typename": "L2VPNTerminationType", + "assigned_object": { + "__typename": "InterfaceType", + "id": "30821", + "name": "ae0.1", + "mtu": "9000", + "custom_fields": {}, + "untagged_vlan": None, + "tagged_vlans": [], + "device": { + "__typename": "DeviceType", + "id": "17649", + "name": "TEST0001", + }, + }, + }, + { + "__typename": "L2VPNTerminationType", + "assigned_object": { + "__typename": "InterfaceType", + }, + }, + ], + } + ) + + with pytest.raises( + DeviceSerializationError, + match=r"Mismatched L2VPN MTU configuration", + ): + serialize(l2x_incoherent_explicit_logical_mtu) + + l2x_incoherent_explicit_logical_physical_mtu = copy.deepcopy(template) + l2x_incoherent_explicit_logical_physical_mtu["l2vpn_list"].append( + { + "__typename": "L2VPNType", + "id": "55", + "identifier": 123456, + "name": "WAN: explicit logical MTU higher than max computed explicit phy MTU", + "type": "VPWS", + "terminations": [ + { + "__typename": "L2VPNTerminationType", + "assigned_object": { + "__typename": "InterfaceType", + "id": "30821", + "name": "ae0.1", + "mtu": "9000", + "custom_fields": {}, + "untagged_vlan": None, + "tagged_vlans": [], + "device": { + "__typename": "DeviceType", + "id": "17649", + "name": "TEST0001", + }, + }, + }, + { + "__typename": "L2VPNTerminationType", + "assigned_object": { + "__typename": "InterfaceType", + }, + }, + ], + } + ) + + with pytest.raises( + DeviceSerializationError, + match=r"Mismatched L2VPN MTU configuration", + ): + serialize(l2x_incoherent_explicit_logical_physical_mtu) + def test_router_physical_interface(): [sd] = get_router_sd_from_path("./test_case_1.yaml") From 05ec1a1afea379a574a64e9c2f308483a1315f4d Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Tue, 13 Oct 2026 12:16:25 +0200 Subject: [PATCH 3/9] MTU sanity checks implem, with min. MTU for encap except when auto --- cosmo/clients/queries/l2vpn.graphql | 1 + cosmo/common.py | 5 ++++ cosmo/l2vpnhelpertypes.py | 44 +++++++++++++++++++++++----- cosmo/routerl2vpnvisitor.py | 45 ++++++++++++++++++++++++++++- cosmo/routervisitor.py | 1 + 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/cosmo/clients/queries/l2vpn.graphql b/cosmo/clients/queries/l2vpn.graphql index 1b1fbb4e..eb5eb559 100644 --- a/cosmo/clients/queries/l2vpn.graphql +++ b/cosmo/clients/queries/l2vpn.graphql @@ -39,6 +39,7 @@ query { __typename id name + mtu custom_fields untagged_vlan { __typename diff --git a/cosmo/common.py b/cosmo/common.py index fd601082..0625363a 100644 --- a/cosmo/common.py +++ b/cosmo/common.py @@ -6,6 +6,11 @@ APP_NAME = "cosmo" +IEEE8021AD_HDR_BYTES = 8 +IEE8021Q_HDR_BYTES = 4 +IEE80213_HDR_BYTES = 14 +DEFAULT_LOGICAL_MTU = 1500 + class AbstractRecoverableError(Exception, ABC): def __init__(self, text: str, on: Optional[object] = None, *args): diff --git a/cosmo/l2vpnhelpertypes.py b/cosmo/l2vpnhelpertypes.py index 5c1aec53..fbc2e75b 100644 --- a/cosmo/l2vpnhelpertypes.py +++ b/cosmo/l2vpnhelpertypes.py @@ -9,6 +9,8 @@ CosmoOutputType, L2VPNSerializationError, DeviceSerializationError, + IEE8021Q_HDR_BYTES, + IEEE8021AD_HDR_BYTES, ) from cosmo.config.cosmo_config import CosmoConfig from cosmo.loopbacks import LoopbackHelper @@ -32,6 +34,14 @@ class AbstractEncapCapability(metaclass=ABCMeta): def accept(self, o): warn(f"cannot find suitable encapsulation.", o) + @abstractmethod + def __str__(self) -> str: + pass + + @abstractmethod + def supplementaryHeaderBytes(self) -> int: + pass + class EthernetCccEncapCapability(AbstractEncapCapability, metaclass=ABCMeta): @singledispatchmethod @@ -54,7 +64,13 @@ def _(self, o: InterfaceType): ) ) if len(sub_units) == 1: - return "ethernet-ccc" + return self + + def supplementaryHeaderBytes(self) -> int: + return 0 # ethernet-ccc has no overhead, as the whole interface is used for 1 service termination + + def __str__(self) -> str: + return "ethernet-ccc" class VlanCccEncapCapability(AbstractEncapCapability, metaclass=ABCMeta): @@ -64,13 +80,19 @@ def accept(self, o): @accept.register def _(self, o: VLANType): - return "vlan-ccc" + return self @accept.register def _(self, o: InterfaceType): # If there is at least one tagged VLAN on the interface or Untagged is not a "unit 0"... if len(o.getTaggedVLANS()) or o.getUntaggedVLAN(): - return "vlan-ccc" + return self + + def supplementaryHeaderBytes(self) -> int: + return IEE8021Q_HDR_BYTES + + def __str__(self) -> str: + return "vlan-ccc" class VlanBridgeEncapCapability(AbstractEncapCapability, metaclass=ABCMeta): @@ -80,6 +102,12 @@ def accept(self, o): @accept.register def _(self, o: InterfaceType | VLANType): + return self + + def supplementaryHeaderBytes(self) -> int: + return IEEE8021AD_HDR_BYTES + + def __str__(self): return "vlan-bridge" @@ -142,8 +170,8 @@ def isValidRawName(self, name: str) -> bool: def needsL2VPNIdentifierAsMandatory(self) -> bool: return False - def getChosenEncapType(self, o: AbstractNetboxType) -> str | None: - chosen_encap = head( + def getChosenEncapType(self, o: AbstractNetboxType) -> AbstractEncapCapability: + return head( list( filter( lambda encap: encap is not None, @@ -151,7 +179,9 @@ def getChosenEncapType(self, o: AbstractNetboxType) -> str | None: ) ) ) - return chosen_encap + + def getChosenEncapStr(self, o: AbstractNetboxType) -> str | None: + return str(self.getChosenEncapType(o)) def processInterfaceTypeTermination(self, o: InterfaceType) -> dict | None: warn( @@ -168,7 +198,7 @@ def processVLANTypeTermination(self, o: VLANType) -> dict | None: return None def spitInterfaceEncapFor(self, o: VLANType | InterfaceType): - encap_type = self.getChosenEncapType(o) + encap_type = self.getChosenEncapStr(o) inner_info = ( {"encapsulation": encap_type} if encap_type else {} ) # encap type is optional! diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index 52442fa3..fa2f172b 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -2,7 +2,13 @@ from multimethod import multimethod as singledispatchmethod from cosmo.abstractroutervisitor import AbstractRouterExporterVisitor -from cosmo.common import head, L2VPNSerializationError +from cosmo.common import ( + head, + L2VPNSerializationError, + DeviceSerializationError, + DEFAULT_LOGICAL_MTU, + IEE80213_HDR_BYTES, +) from cosmo.config.cosmo_config import CosmoConfig from cosmo.l2vpnhelpertypes import ( L2VpnVisitorClassFactoryFromL2VpnTypeObject, @@ -43,6 +49,9 @@ def getL2VpnTypeTerminationObjectFrom( class RouterL2VPNValidatorVisitor(AbstractL2VPNVisitor): + # 8021ad (vlan-bridge) and 8021q (vlan-ccc) encaps auto-sets phy MTU to correct MTU if logical MTU is not specified + MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEE80213_HDR_BYTES + IEE80213_HDR_BYTES + @singledispatchmethod def accept(self, o): return super().accept(o) @@ -69,10 +78,44 @@ def isCompliantWANL2VPN(self, o: L2VPNType): f"Accepted types are: {l2vpn_type.getAcceptedTerminationTypes()}" ) + def isCompliantInterface(self, o: InterfaceType): + if o.getAssociatedDevice() != o.getParent( + DeviceType + ): # guard: reject interfaces associated to other devices + return + phy = o.getPhysicalInterfaceByFilter() + if not phy: + raise L2VPNSerializationError( + "cannot find physical interface attached to L2VPN termination", on=o + ) + phy_mtu = int(phy.getMTU() or self.MAX_DEFAULT_PHY_MTU) + l2vpn_type = self.getL2VpnTypeTerminationObjectFrom(o.getParent(L2VPNType)) + encap_type = l2vpn_type.getChosenEncapType(o) + encap_supplementary_bytes = encap_type.supplementaryHeaderBytes() + if phy != o: # sub interface + logical_mtu = int(o.getMTU() or DEFAULT_LOGICAL_MTU) + target_phy_mtu = ( + logical_mtu + encap_supplementary_bytes + IEE80213_HDR_BYTES + ) + if phy_mtu < target_phy_mtu: + raise L2VPNSerializationError( + "Mismatched L2VPN MTU configuration, check physical and logical interface MTU please. " + f"Expected {target_phy_mtu}, got {phy_mtu} for encap type {encap_type}", + on=o, + ) + @accept.register def _(self, o: L2VPNType): self.isCompliantWANL2VPN(o) + @accept.register + def _(self, o: InterfaceType): + self.isCompliantInterface(o) + + @accept.register + def _(self, o: VLANType): + pass # TODO + class RouterL2VPNExporterVisitor(AbstractL2VPNVisitor): @singledispatchmethod diff --git a/cosmo/routervisitor.py b/cosmo/routervisitor.py index d573bd10..c62e2abc 100644 --- a/cosmo/routervisitor.py +++ b/cosmo/routervisitor.py @@ -346,6 +346,7 @@ def _(self, o: InterfaceType): # guard: do not process VLAN interface info return if o.hasParentAboveWithType(L2VPNTerminationType): + self.l2vpn_validator.accept(o) return self.l2vpn_exporter.accept(o) # interface in interface is lag info if o.hasParentAboveWithType(InterfaceType): From 4e2b178dc4df3f63cfba6212463087f6043143b2 Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Wed, 14 Oct 2026 13:00:01 +0200 Subject: [PATCH 4/9] fix missing none check this is related to issue #78 --- cosmo/routerl2vpnvisitor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index fa2f172b..2d84fe65 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -89,10 +89,12 @@ def isCompliantInterface(self, o: InterfaceType): "cannot find physical interface attached to L2VPN termination", on=o ) phy_mtu = int(phy.getMTU() or self.MAX_DEFAULT_PHY_MTU) - l2vpn_type = self.getL2VpnTypeTerminationObjectFrom(o.getParent(L2VPNType)) - encap_type = l2vpn_type.getChosenEncapType(o) - encap_supplementary_bytes = encap_type.supplementaryHeaderBytes() if phy != o: # sub interface + l2vpn_type = self.getL2VpnTypeTerminationObjectFrom(o.getParent(L2VPNType)) + encap_type = l2vpn_type.getChosenEncapType(o) + encap_supplementary_bytes = ( + encap_type.supplementaryHeaderBytes() if encap_type else 0 + ) logical_mtu = int(o.getMTU() or DEFAULT_LOGICAL_MTU) target_phy_mtu = ( logical_mtu + encap_supplementary_bytes + IEE80213_HDR_BYTES From 4aae68c94b8da6bf0f0788a6a3fc9763649f6b8b Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Tue, 20 Oct 2026 15:27:00 +0200 Subject: [PATCH 5/9] fix typo --- cosmo/common.py | 4 ++-- cosmo/l2vpnhelpertypes.py | 4 ++-- cosmo/routerl2vpnvisitor.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cosmo/common.py b/cosmo/common.py index 0625363a..b304aa8b 100644 --- a/cosmo/common.py +++ b/cosmo/common.py @@ -7,8 +7,8 @@ APP_NAME = "cosmo" IEEE8021AD_HDR_BYTES = 8 -IEE8021Q_HDR_BYTES = 4 -IEE80213_HDR_BYTES = 14 +IEEE8021Q_HDR_BYTES = 4 +IEEE8023_HDR_BYTES = 14 DEFAULT_LOGICAL_MTU = 1500 diff --git a/cosmo/l2vpnhelpertypes.py b/cosmo/l2vpnhelpertypes.py index fbc2e75b..6f45e55b 100644 --- a/cosmo/l2vpnhelpertypes.py +++ b/cosmo/l2vpnhelpertypes.py @@ -9,7 +9,7 @@ CosmoOutputType, L2VPNSerializationError, DeviceSerializationError, - IEE8021Q_HDR_BYTES, + IEEE8021Q_HDR_BYTES, IEEE8021AD_HDR_BYTES, ) from cosmo.config.cosmo_config import CosmoConfig @@ -89,7 +89,7 @@ def _(self, o: InterfaceType): return self def supplementaryHeaderBytes(self) -> int: - return IEE8021Q_HDR_BYTES + return IEEE8021Q_HDR_BYTES def __str__(self) -> str: return "vlan-ccc" diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index 2d84fe65..3b41a010 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -7,7 +7,7 @@ L2VPNSerializationError, DeviceSerializationError, DEFAULT_LOGICAL_MTU, - IEE80213_HDR_BYTES, + IEEE8023_HDR_BYTES, ) from cosmo.config.cosmo_config import CosmoConfig from cosmo.l2vpnhelpertypes import ( @@ -50,7 +50,7 @@ def getL2VpnTypeTerminationObjectFrom( class RouterL2VPNValidatorVisitor(AbstractL2VPNVisitor): # 8021ad (vlan-bridge) and 8021q (vlan-ccc) encaps auto-sets phy MTU to correct MTU if logical MTU is not specified - MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEE80213_HDR_BYTES + IEE80213_HDR_BYTES + MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEEE8023_HDR_BYTES + IEEE8023_HDR_BYTES @singledispatchmethod def accept(self, o): @@ -97,7 +97,7 @@ def isCompliantInterface(self, o: InterfaceType): ) logical_mtu = int(o.getMTU() or DEFAULT_LOGICAL_MTU) target_phy_mtu = ( - logical_mtu + encap_supplementary_bytes + IEE80213_HDR_BYTES + logical_mtu + encap_supplementary_bytes + IEEE8023_HDR_BYTES ) if phy_mtu < target_phy_mtu: raise L2VPNSerializationError( From 8ddddd5b7b09b8beeb0ada0cd5aadbb3dddb56d3 Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Tue, 20 Oct 2026 16:20:02 +0200 Subject: [PATCH 6/9] do not take double tagging into account - unused for now --- cosmo/l2vpnhelpertypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmo/l2vpnhelpertypes.py b/cosmo/l2vpnhelpertypes.py index 6f45e55b..2ff9a343 100644 --- a/cosmo/l2vpnhelpertypes.py +++ b/cosmo/l2vpnhelpertypes.py @@ -105,7 +105,7 @@ def _(self, o: InterfaceType | VLANType): return self def supplementaryHeaderBytes(self) -> int: - return IEEE8021AD_HDR_BYTES + return IEEE8021Q_HDR_BYTES # we do not use double tagging with logical interfaces atm. def __str__(self): return "vlan-bridge" From 6c92fbf66902c7f3621d1acb74aafecbb0ebd360 Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Tue, 20 Oct 2026 16:45:42 +0200 Subject: [PATCH 7/9] change default --- cosmo/routerl2vpnvisitor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index 3b41a010..58933621 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -8,6 +8,7 @@ DeviceSerializationError, DEFAULT_LOGICAL_MTU, IEEE8023_HDR_BYTES, + IEEE8021Q_HDR_BYTES, ) from cosmo.config.cosmo_config import CosmoConfig from cosmo.l2vpnhelpertypes import ( @@ -50,7 +51,7 @@ def getL2VpnTypeTerminationObjectFrom( class RouterL2VPNValidatorVisitor(AbstractL2VPNVisitor): # 8021ad (vlan-bridge) and 8021q (vlan-ccc) encaps auto-sets phy MTU to correct MTU if logical MTU is not specified - MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEEE8023_HDR_BYTES + IEEE8023_HDR_BYTES + MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEEE8021Q_HDR_BYTES @singledispatchmethod def accept(self, o): From 234b821383fe371a5e88307e3197c09016fef9c1 Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Thu, 22 Oct 2026 17:42:31 +0200 Subject: [PATCH 8/9] rework after review + add tests --- cosmo/common.py | 1 + cosmo/routerl2vpnvisitor.py | 54 ++++++++++++++++++++++------- cosmo/tests/test_serializer.py | 63 +++++++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 18 deletions(-) diff --git a/cosmo/common.py b/cosmo/common.py index b304aa8b..f6d9f729 100644 --- a/cosmo/common.py +++ b/cosmo/common.py @@ -10,6 +10,7 @@ IEEE8021Q_HDR_BYTES = 4 IEEE8023_HDR_BYTES = 14 DEFAULT_LOGICAL_MTU = 1500 +DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEEE8023_HDR_BYTES # l2 overhead class AbstractRecoverableError(Exception, ABC): diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index 58933621..bb35578d 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -1,6 +1,7 @@ import ipaddress from multimethod import multimethod as singledispatchmethod +from cosmo import log from cosmo.abstractroutervisitor import AbstractRouterExporterVisitor from cosmo.common import ( head, @@ -9,6 +10,7 @@ DEFAULT_LOGICAL_MTU, IEEE8023_HDR_BYTES, IEEE8021Q_HDR_BYTES, + DEFAULT_PHY_MTU, ) from cosmo.config.cosmo_config import CosmoConfig from cosmo.l2vpnhelpertypes import ( @@ -50,9 +52,6 @@ def getL2VpnTypeTerminationObjectFrom( class RouterL2VPNValidatorVisitor(AbstractL2VPNVisitor): - # 8021ad (vlan-bridge) and 8021q (vlan-ccc) encaps auto-sets phy MTU to correct MTU if logical MTU is not specified - MAX_DEFAULT_PHY_MTU = DEFAULT_LOGICAL_MTU + IEEE8021Q_HDR_BYTES - @singledispatchmethod def accept(self, o): return super().accept(o) @@ -89,23 +88,52 @@ def isCompliantInterface(self, o: InterfaceType): raise L2VPNSerializationError( "cannot find physical interface attached to L2VPN termination", on=o ) - phy_mtu = int(phy.getMTU() or self.MAX_DEFAULT_PHY_MTU) + current_media_mtu = int(phy.getMTU() or DEFAULT_PHY_MTU) + manual_media_mtu = phy.getMTU() is not None or False if phy != o: # sub interface l2vpn_type = self.getL2VpnTypeTerminationObjectFrom(o.getParent(L2VPNType)) encap_type = l2vpn_type.getChosenEncapType(o) encap_supplementary_bytes = ( encap_type.supplementaryHeaderBytes() if encap_type else 0 ) - logical_mtu = int(o.getMTU() or DEFAULT_LOGICAL_MTU) - target_phy_mtu = ( - logical_mtu + encap_supplementary_bytes + IEEE8023_HDR_BYTES - ) - if phy_mtu < target_phy_mtu: - raise L2VPNSerializationError( - "Mismatched L2VPN MTU configuration, check physical and logical interface MTU please. " - f"Expected {target_phy_mtu}, got {phy_mtu} for encap type {encap_type}", - on=o, + current_logical_mtu = int(o.getMTU() or DEFAULT_LOGICAL_MTU) + manual_logical_mtu = o.getMTU() is not None or False + min_target_media_mtu = DEFAULT_PHY_MTU + encap_supplementary_bytes + # if the logical MTU is too high relatively to media MTU, + # it will automatically be capped to the appropriate value, taking into account + # protocol overhead. this may result in an effective logical MTU lower than the + # "standard" 1500 bytes, especially in cases where encapsulations such as + # vlan-bridge and vlan-ccc are used. + if ( + current_media_mtu < min_target_media_mtu + ): # logical mtu insufficient to avoid clamping + log.warn( + f"media MTU of {current_media_mtu} is insufficient for {l2vpn_type.getNetboxTypeName()}. " + f"customer traffic will be clamped below standard size.", + o, ) + if ( + current_logical_mtu + IEEE8023_HDR_BYTES > current_media_mtu + ): # clamping active + if ( + manual_logical_mtu and manual_media_mtu + ): # clamping active and both media MTU and logical MTU are configured by user + # generation should be aborted if a configured physical MTU clamps a configured logical MTU + # and is not effective. + raise L2VPNSerializationError( + f"Mismatched L2VPN MTU configuration, check physical and " + f"logical interface MTU please. Expected {min_target_media_mtu}, got {current_media_mtu} " + f"for encap type {encap_type}", + on=o, + ) + else: # clamping active without above specific case + # user should be warned if a higher configured logical MTU is clamped and not effective, + # as not to break existing configurations. + log.warn( + f"logical MTU of {current_logical_mtu} will be clamped by media " + f"MTU of {current_media_mtu}, please check your configuration", + on=o, + ) @accept.register def _(self, o: L2VPNType): diff --git a/cosmo/tests/test_serializer.py b/cosmo/tests/test_serializer.py index 01e6444c..f624bd2b 100644 --- a/cosmo/tests/test_serializer.py +++ b/cosmo/tests/test_serializer.py @@ -296,6 +296,7 @@ def test_l2vpn_errors(capsys, mock_cosmo_config_fixture): "terminations": [ { "__typename": "L2VPNTerminationType", + "id": "483", "assigned_object": { "__typename": "InterfaceType", "id": "30821", @@ -313,6 +314,7 @@ def test_l2vpn_errors(capsys, mock_cosmo_config_fixture): }, { "__typename": "L2VPNTerminationType", + "id": "485", "assigned_object": { "__typename": "InterfaceType", }, @@ -320,14 +322,65 @@ def test_l2vpn_errors(capsys, mock_cosmo_config_fixture): ], } ) + # should warn that clamping is active + serialize(l2x_incoherent_explicit_logical_mtu) + output = capsys.readouterr() + assert re.search( + r"logical MTU of 9000 will be clamped by media MTU of 1514, please check your configuration", + output.out, + ) - with pytest.raises( - DeviceSerializationError, - match=r"Mismatched L2VPN MTU configuration", - ): - serialize(l2x_incoherent_explicit_logical_mtu) + l2x_insufficient_explicit_media_mtu = copy.deepcopy(template) + l2x_insufficient_explicit_media_mtu["device_list"][0]["interfaces"][2][ + "mtu" + ] = "1514" + l2x_insufficient_explicit_media_mtu["l2vpn_list"].append( + { + "__typename": "L2VPNType", + "id": "55", + "identifier": 123456, + "name": "WAN: VS_234098", + "type": "MPLS-EVPN", + "terminations": [ + { + "__typename": "L2VPNTerminationType", + "id": "483", + "assigned_object": { + "__typename": "InterfaceType", + "id": "30821", + "name": "ae0.1", + "custom_fields": {}, + "untagged_vlan": None, + "tagged_vlans": [], + "device": { + "__typename": "DeviceType", + "id": "17649", + "name": "TEST0001", + }, + }, + }, + { + "__typename": "L2VPNTerminationType", + "id": "485", + "assigned_object": { + "__typename": "InterfaceType", + }, + }, + ], + } + ) + # should warn that media MTU is insufficient + serialize(l2x_insufficient_explicit_media_mtu) + output = capsys.readouterr() + assert re.search( + r"media MTU of 1514 is insufficient for mpls-evpn.", + output.out, + ) l2x_incoherent_explicit_logical_physical_mtu = copy.deepcopy(template) + l2x_incoherent_explicit_logical_physical_mtu["device_list"][0]["interfaces"][2][ + "mtu" + ] = "1514" l2x_incoherent_explicit_logical_physical_mtu["l2vpn_list"].append( { "__typename": "L2VPNType", From 848c7252d8506abcc48e4695c1c101a0013d6b1a Mon Sep 17 00:00:00 2001 From: lou lecrivain Date: Wed, 28 Oct 2026 16:44:05 +0100 Subject: [PATCH 9/9] also validate VLAN L2VPN terminations --- cosmo/routerl2vpnvisitor.py | 10 +++++++++- cosmo/routervisitor.py | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cosmo/routerl2vpnvisitor.py b/cosmo/routerl2vpnvisitor.py index bb35578d..56e060a1 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -145,7 +145,15 @@ def _(self, o: InterfaceType): @accept.register def _(self, o: VLANType): - pass # TODO + interface_list = filter( + lambda i: i.getAssociatedDevice() == o.getParent(DeviceType), + [ + *o.getInterfacesAsUntagged(), + *o.getInterfacesAsTagged(), + ], + ) + for interface in interface_list: + self.isCompliantInterface(interface) class RouterL2VPNExporterVisitor(AbstractL2VPNVisitor): diff --git a/cosmo/routervisitor.py b/cosmo/routervisitor.py index c62e2abc..abd9bb95 100644 --- a/cosmo/routervisitor.py +++ b/cosmo/routervisitor.py @@ -516,6 +516,7 @@ def processUntaggedVLAN(self, o: VLANType): @accept.register def _(self, o: VLANType): if o.hasParentAboveWithType(L2VPNTerminationType): + self.l2vpn_validator.accept(o) return self.l2vpn_exporter.accept(o) parent_interface = o.getParent(InterfaceType) if (