From 6b8735bb182f87e7a5df54b77e07c3d6d35dfd52 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 13 Mar 2022 11:17:44 +0000 Subject: [PATCH] Add bridge-domain support. Refactor validator main function to avoid 'interface' symbol clash. Add get_mtu() for interfaces, returns the sub-int's MTU or its parent's MTU, defaulting to 1500. Ensure MTU for all bridge-domain members is set to the same value. Ensure all bridge-domain members are L2 (have no LCP, have no address) --- example.yaml | 11 ++++++++++ schema.yaml | 16 +++++++++++---- validator/__init__.py | 16 +++++++-------- validator/bondethernet.py | 2 +- validator/bridgedomain.py | 42 ++++++++++++++++++++++++++++++++++++++- validator/interface.py | 39 +++++++++++++++++++++++++++++++++++- validator/loopback.py | 2 +- 7 files changed, 112 insertions(+), 16 deletions(-) diff --git a/example.yaml b/example.yaml index 1df316e..90caad2 100644 --- a/example.yaml +++ b/example.yaml @@ -21,6 +21,9 @@ interfaces: GigabitEthernet2/0/1: description: "Infra: LAG to xsw1" + GigabitEthernet3/0/0: + description: "Infra: Bridge Doamin 10" + BondEthernet0: description: "Bond, James Bond!" mac: 00:01:02:03:04:05 @@ -46,3 +49,11 @@ loopbacks: mtu: 9216 lcp: "loop0" addresses: [ 192.0.2.1/32, 2001:db8:1::1/128 ] + +bridgedomains: + bd10: + description: "Bridge Domain 10" + mtu: 1500 + lcp: "bvi10" + addresses: [ 192.0.2.9/29, 2001:db8:2::1/64 ] + interfaces: [ BondEthernet0.203, GigabitEthernet3/0/0 ] diff --git a/schema.yaml b/schema.yaml index 3050803..75586d3 100644 --- a/schema.yaml +++ b/schema.yaml @@ -1,11 +1,19 @@ -interfaces: map(include('interface'),key=str(matches='.*GigabitEthernet[0-9]+/[0-9]+/[0-9]+|BondEthernet[0-9]+')) -bondethernets: map(include('bondethernet'),key=str(matches='BondEthernet[0-9]+')) -loopbacks: map(include('loopback'),key=str(matches='loop[0-9]+')) +interfaces: map(include('interface'),key=str(matches='.*GigabitEthernet[0-9]+/[0-9]+/[0-9]+|BondEthernet[0-9]+'),required=False) +bondethernets: map(include('bondethernet'),key=str(matches='BondEthernet[0-9]+'),required=False) +loopbacks: map(include('loopback'),key=str(matches='loop[0-9]+'),required=False) +bridgedomains: map(include('bridgedomain'),key=str(matches='bd[0-9]+'),required=False) +--- +bridgedomain: + description: str(exclude='\'"',required=False) + mtu: int(min=128,max=9216,required=False) + lcp: str(max=8,matches='[a-z]+[a-z0-9-]{,7}',required=False) + addresses: list(ip_interface(),min=1,max=6,required=False) + interfaces: list(str()) --- loopback: description: str(exclude='\'"',required=False) lcp: str(max=8,matches='[a-z]+[a-z0-9-]{,7}',required=False) - mtu: int(min=128,max=9216) + mtu: int(min=128,max=9216,required=False) addresses: list(ip_interface(),min=1,max=6,required=False) --- bondethernet: diff --git a/validator/__init__.py b/validator/__init__.py index 9841510..139237f 100644 --- a/validator/__init__.py +++ b/validator/__init__.py @@ -12,10 +12,10 @@ try: except ImportError: print("ERROR: install yamale manually: sudo pip install yamale") sys.exit(-2) -from validator.loopback import loopback -from validator.bondethernet import bondethernet -from validator.interface import interface -from validator.bridgedomain import bridgedomain +from validator.loopback import validate_loopbacks +from validator.bondethernet import validate_bondethernets +from validator.interface import validate_interfaces +from validator.bridgedomain import validate_bridgedomains from yamale.validators import DefaultValidators, Validator import ipaddress @@ -79,25 +79,25 @@ class Validator(object): self.logger.debug("Validating Semantics...") - rv, msgs = bondethernet(self.args, yaml) + rv, msgs = validate_bondethernets(self.args, yaml) if msgs: ret_msgs.extend(msgs) if not rv: ret_rv = False - rv, msgs = interface(self.args, yaml) + rv, msgs = validate_interfaces(self.args, yaml) if msgs: ret_msgs.extend(msgs) if not rv: ret_rv = False - rv, msgs = loopback(self.args, yaml) + rv, msgs = validate_loopbacks(self.args, yaml) if msgs: ret_msgs.extend(msgs) if not rv: ret_rv = False - rv, msgs = bridgedomain(self.args, yaml) + rv, msgs = validate_bridgedomains(self.args, yaml) if msgs: ret_msgs.extend(msgs) if not rv: diff --git a/validator/bondethernet.py b/validator/bondethernet.py index 0a03505..b3765e0 100644 --- a/validator/bondethernet.py +++ b/validator/bondethernet.py @@ -16,7 +16,7 @@ def get_by_name(yaml, ifname): return None -def bondethernet(args, yaml): +def validate_bondethernets(args, yaml): result = True msgs = [] logger = logging.getLogger('vppcfg.validator') diff --git a/validator/bridgedomain.py b/validator/bridgedomain.py index fb3b3b1..8ba891d 100644 --- a/validator/bridgedomain.py +++ b/validator/bridgedomain.py @@ -1,15 +1,55 @@ import logging +import validator.interface as interface class NullHandler(logging.Handler): def emit(self, record): pass -def bridgedomain(args, yaml): +def get_by_name(yaml, ifname): + """ Return the BridgeDomain by name, if it exists. Return None otherwise. """ + try: + if ifname in yaml['bridgedomains']: + return yaml['bridgedomains'][ifname] + except: + pass + return None + + +def validate_bridgedomains(args, yaml): result = True msgs = [] logger = logging.getLogger('vppcfg.validator') logger.addHandler(NullHandler()) + if not 'bridgedomains' in yaml: + return result, msgs + logger.debug("Validating bridgedomains...") + for ifname, iface in yaml['bridgedomains'].items(): + logger.debug("bridgedomain %s" % iface) + bd_mtu = 1500 + if 'mtu' in iface: + bd_mtu = iface['mtu'] + + if 'interfaces' in iface: + for member in iface['interfaces']: + member_iface = interface.get_by_name(yaml, member) + if not member_iface: + msgs.append("bondethernet %s member %s doesn't exist" % (ifname, member)) + result = False + continue + + if interface.has_lcp(yaml, member): + msgs.append("bridgedomain %s member %s has an LCP" % (ifname, member)) + result = False + if interface.has_address(yaml, member): + msgs.append("bridgedomain %s member %s has an address" % (ifname, member)) + result = False + member_mtu = interface.get_mtu(yaml, member) + if member_mtu != bd_mtu: + msgs.append("bridgedomain %s member %s has MTU %d, while bridge has %d" % (ifname, member, member_mtu, bd_mtu)) + result = False + + return result, msgs diff --git a/validator/interface.py b/validator/interface.py index 62df7b3..a3e7e31 100644 --- a/validator/interface.py +++ b/validator/interface.py @@ -5,6 +5,19 @@ class NullHandler(logging.Handler): def emit(self, record): pass +def get_parent_by_name(yaml, ifname): + """ Returns the sub-interface's parent, or None if the sub-int doesn't exist. """ + if not '.' in ifname: + return None + ifname, subid = ifname.split('.') + subid = int(subid) + try: + iface = yaml['interfaces'][ifname] + return iface + except: + pass + return None + def get_by_name(yaml, ifname): """ Returns the interface or sub-interface by a given name, or None if it does not exist """ if '.' in ifname: @@ -115,7 +128,31 @@ def valid_encapsulation(yaml, sub_ifname): return True -def interface(args, yaml): +def is_l3(yaml, ifname): + """ Returns True if the interface exists and has either an LCP or an address """ + iface = get_by_name(yaml, ifname) + if not iface: + return False + if has_lcp(yaml, ifname): + return True + if has_address(yaml, ifname): + return True + return False + +def get_mtu(yaml, ifname): + """ Returns MTU of the interface. If it's not set, return the parent's MTU, and + return 1500 if no MTU was set on the sub-int or the parent.""" + iface = get_by_name(yaml, ifname) + parent_iface = get_parent_by_name(yaml, ifname) + try: + return iface['mtu'] + return parent_iface['mtu'] + except: + pass + return 1500 + + +def validate_interfaces(args, yaml): result = True msgs = [] logger = logging.getLogger('vppcfg.validator') diff --git a/validator/loopback.py b/validator/loopback.py index 1073f96..00b47a2 100644 --- a/validator/loopback.py +++ b/validator/loopback.py @@ -14,7 +14,7 @@ def get_by_name(yaml, ifname): return None -def loopback(args, yaml): +def validate_loopbacks(args, yaml): result = True msgs = [] logger = logging.getLogger('vppcfg.validator')