Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosmo/clients/queries/l2vpn.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ query {
__typename
id
name
mtu
custom_fields
untagged_vlan {
__typename
Expand Down
6 changes: 6 additions & 0 deletions cosmo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
44 changes: 37 additions & 7 deletions cosmo/l2vpnhelpertypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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"


Expand Down Expand Up @@ -142,16 +170,18 @@ 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,
[t().accept(o) for t in self.getSupportedEncapTraits()],
)
)
)
return chosen_encap

def getChosenEncapStr(self, o: AbstractNetboxType) -> str | None:
return str(self.getChosenEncapType(o))

def processInterfaceTypeTermination(self, o: InterfaceType) -> dict | None:
warn(
Expand All @@ -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!
Expand Down
84 changes: 83 additions & 1 deletion cosmo/routerl2vpnvisitor.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cosmo/routervisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 (
Expand Down
92 changes: 91 additions & 1 deletion cosmo/tests/test_case_l2x_err_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading