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..f6d9f729 100644 --- a/cosmo/common.py +++ b/cosmo/common.py @@ -6,6 +6,12 @@ APP_NAME = "cosmo" +IEEE8021AD_HDR_BYTES = 8 +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): def __init__(self, text: str, on: Optional[object] = None, *args): diff --git a/cosmo/l2vpnhelpertypes.py b/cosmo/l2vpnhelpertypes.py index 5c1aec53..2ff9a343 100644 --- a/cosmo/l2vpnhelpertypes.py +++ b/cosmo/l2vpnhelpertypes.py @@ -9,6 +9,8 @@ CosmoOutputType, L2VPNSerializationError, DeviceSerializationError, + IEEE8021Q_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 IEEE8021Q_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 IEEE8021Q_HDR_BYTES # we do not use double tagging with logical interfaces atm. + + 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..56e060a1 100644 --- a/cosmo/routerl2vpnvisitor.py +++ b/cosmo/routerl2vpnvisitor.py @@ -1,8 +1,17 @@ import ipaddress from multimethod import multimethod as singledispatchmethod +from cosmo import log from cosmo.abstractroutervisitor import AbstractRouterExporterVisitor -from cosmo.common import head, L2VPNSerializationError +from cosmo.common import ( + head, + L2VPNSerializationError, + DeviceSerializationError, + DEFAULT_LOGICAL_MTU, + IEEE8023_HDR_BYTES, + IEEE8021Q_HDR_BYTES, + DEFAULT_PHY_MTU, +) from cosmo.config.cosmo_config import CosmoConfig from cosmo.l2vpnhelpertypes import ( L2VpnVisitorClassFactoryFromL2VpnTypeObject, @@ -69,10 +78,83 @@ 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 + ) + 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 + ) + 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): self.isCompliantWANL2VPN(o) + @accept.register + def _(self, o: InterfaceType): + self.isCompliantInterface(o) + + @accept.register + def _(self, o: VLANType): + 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): @singledispatchmethod diff --git a/cosmo/routervisitor.py b/cosmo/routervisitor.py index d573bd10..abd9bb95 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): @@ -515,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 ( 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 diff --git a/cosmo/tests/test_serializer.py b/cosmo/tests/test_serializer.py index 6ced29d7..f624bd2b 100644 --- a/cosmo/tests/test_serializer.py +++ b/cosmo/tests/test_serializer.py @@ -285,6 +285,143 @@ 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", + "id": "483", + "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", + "id": "485", + "assigned_object": { + "__typename": "InterfaceType", + }, + }, + ], + } + ) + # 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, + ) + + 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", + "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")