diff --git a/python/vyos/configverify.py b/python/vyos/configverify.py index 4cb84194a..b49d66c36 100644 --- a/python/vyos/configverify.py +++ b/python/vyos/configverify.py @@ -1,523 +1,524 @@ # Copyright 2020-2024 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. # The sole purpose of this module is to hold common functions used in # all kinds of implementations to verify the CLI configuration. # It is started by migrating the interfaces to the new get_config_dict() # approach which will lead to a lot of code that can be reused. # NOTE: imports should be as local as possible to the function which # makes use of it! from vyos import ConfigError from vyos.utils.dict import dict_search # pattern re-used in ipsec migration script dynamic_interface_pattern = r'(ppp|pppoe|sstpc|l2tp|ipoe)[0-9]+' def verify_mtu(config): """ Common helper function used by interface implementations to perform recurring validation if the specified MTU can be used by the underlaying hardware. """ from vyos.ifconfig import Interface if 'mtu' in config: mtu = int(config['mtu']) tmp = Interface(config['ifname']) # Not all interfaces support min/max MTU # https://vyos.dev/T5011 try: min_mtu = tmp.get_min_mtu() max_mtu = tmp.get_max_mtu() except: # Fallback to defaults min_mtu = 68 max_mtu = 9000 if mtu < min_mtu: raise ConfigError(f'Interface MTU too low, ' \ f'minimum supported MTU is {min_mtu}!') if mtu > max_mtu: raise ConfigError(f'Interface MTU too high, ' \ f'maximum supported MTU is {max_mtu}!') def verify_mtu_parent(config, parent): if 'mtu' not in config or 'mtu' not in parent: return mtu = int(config['mtu']) parent_mtu = int(parent['mtu']) if mtu > parent_mtu: raise ConfigError(f'Interface MTU "{mtu}" too high, ' \ f'parent interface MTU is "{parent_mtu}"!') def verify_mtu_ipv6(config): """ Common helper function used by interface implementations to perform recurring validation if the specified MTU can be used when IPv6 is configured on the interface. IPv6 requires a 1280 bytes MTU. """ from vyos.template import is_ipv6 if 'mtu' in config: # IPv6 minimum required link mtu min_mtu = 1280 if int(config['mtu']) < min_mtu: interface = config['ifname'] error_msg = f'IPv6 address will be configured on interface "{interface}",\n' \ f'the required minimum MTU is "{min_mtu}"!' if 'address' in config: for address in config['address']: if address in ['dhcpv6'] or is_ipv6(address): raise ConfigError(error_msg) tmp = dict_search('ipv6.address.no_default_link_local', config) if tmp == None: raise ConfigError('link-local ' + error_msg) tmp = dict_search('ipv6.address.autoconf', config) if tmp != None: raise ConfigError(error_msg) tmp = dict_search('ipv6.address.eui64', config) if tmp != None: raise ConfigError(error_msg) def verify_vrf(config): """ Common helper function used by interface implementations to perform recurring validation of VRF configuration. """ from vyos.utils.network import interface_exists if 'vrf' in config: vrfs = config['vrf'] if isinstance(vrfs, str): vrfs = [vrfs] for vrf in vrfs: if vrf == 'default': continue if not interface_exists(vrf): raise ConfigError(f'VRF "{vrf}" does not exist!') if 'is_bridge_member' in config: raise ConfigError( 'Interface "{ifname}" cannot be both a member of VRF "{vrf}" ' 'and bridge "{is_bridge_member}"!'.format(**config)) def verify_bond_bridge_member(config): """ Checks if interface has a VRF configured and is also part of a bond or bridge, which is not allowed! """ if 'vrf' in config: ifname = config['ifname'] if 'is_bond_member' in config: raise ConfigError(f'Can not add interface "{ifname}" to bond, it has a VRF assigned!') if 'is_bridge_member' in config: raise ConfigError(f'Can not add interface "{ifname}" to bridge, it has a VRF assigned!') def verify_tunnel(config): """ This helper is used to verify the common part of the tunnel """ from vyos.template import is_ipv4 from vyos.template import is_ipv6 if 'encapsulation' not in config: raise ConfigError('Must configure the tunnel encapsulation for '\ '{ifname}!'.format(**config)) if 'source_address' not in config and 'source_interface' not in config: raise ConfigError('source-address or source-interface required for tunnel!') if 'remote' not in config and config['encapsulation'] != 'gre': raise ConfigError('remote ip address is mandatory for tunnel') if config['encapsulation'] in ['ipip6', 'ip6ip6', 'ip6gre', 'ip6gretap', 'ip6erspan']: error_ipv6 = 'Encapsulation mode requires IPv6' if 'source_address' in config and not is_ipv6(config['source_address']): raise ConfigError(f'{error_ipv6} source-address') if 'remote' in config and not is_ipv6(config['remote']): raise ConfigError(f'{error_ipv6} remote') else: error_ipv4 = 'Encapsulation mode requires IPv4' if 'source_address' in config and not is_ipv4(config['source_address']): raise ConfigError(f'{error_ipv4} source-address') if 'remote' in config and not is_ipv4(config['remote']): raise ConfigError(f'{error_ipv4} remote address') if config['encapsulation'] in ['sit', 'gretap', 'ip6gretap']: if 'source_interface' in config: encapsulation = config['encapsulation'] raise ConfigError(f'Option source-interface can not be used with ' \ f'encapsulation "{encapsulation}"!') elif config['encapsulation'] == 'gre': if 'source_address' in config and is_ipv6(config['source_address']): raise ConfigError('Can not use local IPv6 address is for mGRE tunnels') def verify_mirror_redirect(config): """ Common helper function used by interface implementations to perform recurring validation of mirror and redirect interface configuration via tc(8) It makes no sense to mirror traffic back at yourself! """ from vyos.utils.network import interface_exists if {'mirror', 'redirect'} <= set(config): raise ConfigError('Mirror and redirect can not be enabled at the same time!') if 'mirror' in config: for direction, mirror_interface in config['mirror'].items(): if not interface_exists(mirror_interface): raise ConfigError(f'Requested mirror interface "{mirror_interface}" '\ 'does not exist!') if mirror_interface == config['ifname']: raise ConfigError(f'Can not mirror "{direction}" traffic back '\ 'the originating interface!') if 'redirect' in config: redirect_ifname = config['redirect'] if not interface_exists(redirect_ifname): raise ConfigError(f'Requested redirect interface "{redirect_ifname}" '\ 'does not exist!') if ('mirror' in config or 'redirect' in config) and dict_search('traffic_policy.in', config) is not None: # XXX: support combination of limiting and redirect/mirror - this is an # artificial limitation raise ConfigError('Can not use ingress policy together with mirror or redirect!') def verify_authentication(config): """ Common helper function used by interface implementations to perform recurring validation of authentication for either PPPoE or WWAN interfaces. If authentication CLI option is defined, both username and password must be set! """ if 'authentication' not in config: return if not {'username', 'password'} <= set(config['authentication']): raise ConfigError('Authentication requires both username and ' \ 'password to be set!') def verify_address(config): """ Common helper function used by interface implementations to perform recurring validation of IP address assignment when interface is part of a bridge or bond. """ if {'is_bridge_member', 'address'} <= set(config): interface = config['ifname'] bridge_name = next(iter(config['is_bridge_member'])) raise ConfigError(f'Cannot assign address to interface "{interface}" ' f'as it is a member of bridge "{bridge_name}"!') def verify_bridge_delete(config): """ Common helper function used by interface implementations to perform recurring validation of IP address assignmenr when interface also is part of a bridge. """ if 'is_bridge_member' in config: interface = config['ifname'] bridge_name = next(iter(config['is_bridge_member'])) raise ConfigError(f'Interface "{interface}" cannot be deleted as it ' f'is a member of bridge "{bridge_name}"!') -def verify_interface_exists(ifname, warning_only=False): +def verify_interface_exists(ifname, state_required=False, warning_only=False): """ Common helper function used by interface implementations to perform recurring validation if an interface actually exists. We first probe if the interface is defined on the CLI, if it's not found we try if it exists at the OS level. """ from vyos.base import Warning from vyos.configquery import ConfigTreeQuery from vyos.utils.dict import dict_search_recursive from vyos.utils.network import interface_exists - # Check if interface is present in CLI config - config = ConfigTreeQuery() - tmp = config.get_config_dict(['interfaces'], get_first_key=True) - if bool(list(dict_search_recursive(tmp, ifname))): - return True + if not state_required: + # Check if interface is present in CLI config + config = ConfigTreeQuery() + tmp = config.get_config_dict(['interfaces'], get_first_key=True) + if bool(list(dict_search_recursive(tmp, ifname))): + return True # Interface not found on CLI, try Linux Kernel if interface_exists(ifname): return True message = f'Interface "{ifname}" does not exist!' if warning_only: Warning(message) return False raise ConfigError(message) def verify_source_interface(config): """ Common helper function used by interface implementations to perform recurring validation of the existence of a source-interface required by e.g. peth/MACvlan, MACsec ... """ import re from vyos.utils.network import interface_exists ifname = config['ifname'] if 'source_interface' not in config: raise ConfigError(f'Physical source-interface required for "{ifname}"!') src_ifname = config['source_interface'] # We do not allow sourcing other interfaces (e.g. tunnel) from dynamic interfaces tmp = re.compile(dynamic_interface_pattern) if tmp.match(src_ifname): raise ConfigError(f'Can not source "{ifname}" from dynamic interface "{src_ifname}"!') if not interface_exists(src_ifname): raise ConfigError(f'Specified source-interface {src_ifname} does not exist') if 'source_interface_is_bridge_member' in config: bridge_name = next(iter(config['source_interface_is_bridge_member'])) raise ConfigError(f'Invalid source-interface "{src_ifname}". Interface ' f'is already a member of bridge "{bridge_name}"!') if 'source_interface_is_bond_member' in config: bond_name = next(iter(config['source_interface_is_bond_member'])) raise ConfigError(f'Invalid source-interface "{src_ifname}". Interface ' f'is already a member of bond "{bond_name}"!') if 'is_source_interface' in config: tmp = config['is_source_interface'] raise ConfigError(f'Can not use source-interface "{src_ifname}", it already ' \ f'belongs to interface "{tmp}"!') def verify_dhcpv6(config): """ Common helper function used by interface implementations to perform recurring validation of DHCPv6 options which are mutually exclusive. """ if 'dhcpv6_options' in config: if {'parameters_only', 'temporary'} <= set(config['dhcpv6_options']): raise ConfigError('DHCPv6 temporary and parameters-only options ' 'are mutually exclusive!') # It is not allowed to have duplicate SLA-IDs as those identify an # assigned IPv6 subnet from a delegated prefix for pd in (dict_search('dhcpv6_options.pd', config) or []): sla_ids = [] interfaces = dict_search(f'dhcpv6_options.pd.{pd}.interface', config) if not interfaces: raise ConfigError('DHCPv6-PD requires an interface where to assign ' 'the delegated prefix!') for count, interface in enumerate(interfaces): if 'sla_id' in interfaces[interface]: sla_ids.append(interfaces[interface]['sla_id']) else: sla_ids.append(str(count)) # Check for duplicates duplicates = [x for n, x in enumerate(sla_ids) if x in sla_ids[:n]] if duplicates: raise ConfigError('Site-Level Aggregation Identifier (SLA-ID) ' 'must be unique per prefix-delegation!') def verify_vlan_config(config): """ Common helper function used by interface implementations to perform recurring validation of interface VLANs """ # VLAN and Q-in-Q IDs are not allowed to overlap if 'vif' in config and 'vif_s' in config: duplicate = list(set(config['vif']) & set(config['vif_s'])) if duplicate: raise ConfigError(f'Duplicate VLAN id "{duplicate[0]}" used for vif and vif-s interfaces!') parent_ifname = config['ifname'] # 802.1q VLANs for vlan_id in config.get('vif', {}): vlan = config['vif'][vlan_id] vlan['ifname'] = f'{parent_ifname}.{vlan_id}' verify_dhcpv6(vlan) verify_address(vlan) verify_vrf(vlan) verify_mirror_redirect(vlan) verify_mtu_parent(vlan, config) # 802.1ad (Q-in-Q) VLANs for s_vlan_id in config.get('vif_s', {}): s_vlan = config['vif_s'][s_vlan_id] s_vlan['ifname'] = f'{parent_ifname}.{s_vlan_id}' verify_dhcpv6(s_vlan) verify_address(s_vlan) verify_vrf(s_vlan) verify_mirror_redirect(s_vlan) verify_mtu_parent(s_vlan, config) for c_vlan_id in s_vlan.get('vif_c', {}): c_vlan = s_vlan['vif_c'][c_vlan_id] c_vlan['ifname'] = f'{parent_ifname}.{s_vlan_id}.{c_vlan_id}' verify_dhcpv6(c_vlan) verify_address(c_vlan) verify_vrf(c_vlan) verify_mirror_redirect(c_vlan) verify_mtu_parent(c_vlan, config) verify_mtu_parent(c_vlan, s_vlan) def verify_diffie_hellman_length(file, min_keysize): """ Verify Diffie-Hellamn keypair length given via file. It must be greater then or equal to min_keysize """ import os import re from vyos.utils.process import cmd try: keysize = str(min_keysize) except: return False if os.path.exists(file): out = cmd(f'openssl dhparam -inform PEM -in {file} -text') prog = re.compile('\d+\s+bit') if prog.search(out): bits = prog.search(out)[0].split()[0] if int(bits) >= int(min_keysize): return True return False def verify_common_route_maps(config): """ Common helper function used by routing protocol implementations to perform recurring validation if the specified route-map for either zebra to kernel installation exists (this is the top-level route_map key) or when a route is redistributed with a route-map that it exists! """ # XXX: This function is called in combination with a previous call to: # tmp = conf.get_config_dict(['policy']) - see protocols_ospf.py as example. # We should NOT call this with the key_mangling option as this would rename # route-map hypens '-' to underscores '_' and one could no longer distinguish # what should have been the "proper" route-map name, as foo-bar and foo_bar # are two entire different route-map instances! for route_map in ['route-map', 'route_map']: if route_map not in config: continue tmp = config[route_map] # Check if the specified route-map exists, if not error out if dict_search(f'policy.route-map.{tmp}', config) == None: raise ConfigError(f'Specified route-map "{tmp}" does not exist!') if 'redistribute' in config: for protocol, protocol_config in config['redistribute'].items(): if 'route_map' in protocol_config: verify_route_map(protocol_config['route_map'], config) def verify_route_map(route_map_name, config): """ Common helper function used by routing protocol implementations to perform recurring validation if a specified route-map exists! """ # Check if the specified route-map exists, if not error out if dict_search(f'policy.route-map.{route_map_name}', config) == None: raise ConfigError(f'Specified route-map "{route_map_name}" does not exist!') def verify_prefix_list(prefix_list, config, version=''): """ Common helper function used by routing protocol implementations to perform recurring validation if a specified prefix-list exists! """ # Check if the specified prefix-list exists, if not error out if dict_search(f'policy.prefix-list{version}.{prefix_list}', config) == None: raise ConfigError(f'Specified prefix-list{version} "{prefix_list}" does not exist!') def verify_access_list(access_list, config, version=''): """ Common helper function used by routing protocol implementations to perform recurring validation if a specified prefix-list exists! """ # Check if the specified ACL exists, if not error out if dict_search(f'policy.access-list{version}.{access_list}', config) == None: raise ConfigError(f'Specified access-list{version} "{access_list}" does not exist!') def verify_pki_certificate(config: dict, cert_name: str, no_password_protected: bool=False): """ Common helper function user by PKI consumers to perform recurring validation functions for PEM based certificates """ if 'pki' not in config: raise ConfigError('PKI is not configured!') if 'certificate' not in config['pki']: raise ConfigError('PKI does not contain any certificates!') if cert_name not in config['pki']['certificate']: raise ConfigError(f'Certificate "{cert_name}" not found in configuration!') pki_cert = config['pki']['certificate'][cert_name] if 'certificate' not in pki_cert: raise ConfigError(f'PEM certificate for "{cert_name}" missing in configuration!') if 'private' not in pki_cert or 'key' not in pki_cert['private']: raise ConfigError(f'PEM private key for "{cert_name}" missing in configuration!') if no_password_protected and 'password_protected' in pki_cert['private']: raise ConfigError('Password protected PEM private key is not supported!') def verify_pki_ca_certificate(config: dict, ca_name: str): """ Common helper function user by PKI consumers to perform recurring validation functions for PEM based CA certificates """ if 'pki' not in config: raise ConfigError('PKI is not configured!') if 'ca' not in config['pki']: raise ConfigError('PKI does not contain any CA certificates!') if ca_name not in config['pki']['ca']: raise ConfigError(f'CA Certificate "{ca_name}" not found in configuration!') pki_cert = config['pki']['ca'][ca_name] if 'certificate' not in pki_cert: raise ConfigError(f'PEM CA certificate for "{cert_name}" missing in configuration!') def verify_pki_dh_parameters(config: dict, dh_name: str, min_key_size: int=0): """ Common helper function user by PKI consumers to perform recurring validation functions on DH parameters """ from vyos.pki import load_dh_parameters if 'pki' not in config: raise ConfigError('PKI is not configured!') if 'dh' not in config['pki']: raise ConfigError('PKI does not contain any DH parameters!') if dh_name not in config['pki']['dh']: raise ConfigError(f'DH parameter "{dh_name}" not found in configuration!') if min_key_size: pki_dh = config['pki']['dh'][dh_name] dh_params = load_dh_parameters(pki_dh['parameters']) dh_numbers = dh_params.parameter_numbers() dh_bits = dh_numbers.p.bit_length() if dh_bits < min_key_size: raise ConfigError(f'Minimum DH key-size is {min_key_size} bits!') diff --git a/src/conf_mode/qos.py b/src/conf_mode/qos.py index 45248fb4a..464d7c192 100755 --- a/src/conf_mode/qos.py +++ b/src/conf_mode/qos.py @@ -1,332 +1,332 @@ #!/usr/bin/env python3 # # Copyright (C) 2023-2024 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from sys import exit from netifaces import interfaces from vyos.base import Warning from vyos.config import Config from vyos.configdep import set_dependents from vyos.configdep import call_dependents from vyos.configdict import dict_merge from vyos.configverify import verify_interface_exists from vyos.ifconfig import Section from vyos.qos import CAKE from vyos.qos import DropTail from vyos.qos import FairQueue from vyos.qos import FQCodel from vyos.qos import Limiter from vyos.qos import NetEm from vyos.qos import Priority from vyos.qos import RandomDetect from vyos.qos import RateLimiter from vyos.qos import RoundRobin from vyos.qos import TrafficShaper from vyos.qos import TrafficShaperHFSC from vyos.utils.dict import dict_search_recursive from vyos.utils.process import run from vyos import ConfigError from vyos import airbag from vyos.xml_ref import relative_defaults airbag.enable() map_vyops_tc = { 'cake' : CAKE, 'drop_tail' : DropTail, 'fair_queue' : FairQueue, 'fq_codel' : FQCodel, 'limiter' : Limiter, 'network_emulator' : NetEm, 'priority_queue' : Priority, 'random_detect' : RandomDetect, 'rate_control' : RateLimiter, 'round_robin' : RoundRobin, 'shaper' : TrafficShaper, 'shaper_hfsc' : TrafficShaperHFSC, } def get_shaper(qos, interface_config, direction): policy_name = interface_config[direction] # An interface might have a QoS configuration, search the used # configuration referenced by this. Path will hold the dict element # referenced by the config, as this will be of sort: # # ['policy', 'drop_tail', 'foo-dtail'] <- we are only interested in # drop_tail as the policy/shaper type _, path = next(dict_search_recursive(qos, policy_name)) shaper_type = path[1] shaper_config = qos['policy'][shaper_type][policy_name] return (map_vyops_tc[shaper_type], shaper_config) def _clean_conf_dict(conf): """ Delete empty nodes from config e.g. match ADDRESS30 { ip { source {} } } """ if isinstance(conf, dict): return {node: _clean_conf_dict(val) for node, val in conf.items() if val != {} and _clean_conf_dict(val) != {}} else: return conf def _get_group_filters(config: dict, group_name: str, visited=None) -> dict: filters = dict() if not visited: visited = [group_name, ] else: if group_name in visited: return filters visited.append(group_name) for filter, filter_config in config.get(group_name, {}).items(): if filter == 'match': for match, match_config in filter_config.items(): filters[f'{group_name}-{match}'] = match_config elif filter == 'match_group': for group in filter_config: filters.update(_get_group_filters(config, group, visited)) return filters def _get_group_match(config:dict, group_name:str) -> dict: match = dict() for key, val in _get_group_filters(config, group_name).items(): # delete duplicate matches if val not in match.values(): match[key] = val return match def get_config(config=None): if config: conf = config else: conf = Config() base = ['qos'] if not conf.exists(base): return None qos = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True, no_tag_node_value_mangle=True) for ifname in interfaces(): if_node = Section.get_config_path(ifname) if not if_node: continue path = f'interfaces {if_node}' if conf.exists(f'{path} mirror') or conf.exists(f'{path} redirect'): type_node = path.split(" ")[1] # return only interface type node set_dependents(type_node, conf, ifname.split(".")[0]) for policy in qos.get('policy', []): if policy in ['random_detect']: for rd_name in list(qos['policy'][policy]): # There are eight precedence levels - ensure all are present # to be filled later down with the appropriate default values default_p_val = relative_defaults( ['qos', 'policy', 'random-detect', rd_name, 'precedence'], {'precedence': {'0': {}}}, get_first_key=True, recursive=True )['0'] default_p_val = {key.replace('-', '_'): value for key, value in default_p_val.items()} default_precedence = { 'precedence': {'0': default_p_val, '1': default_p_val, '2': default_p_val, '3': default_p_val, '4': default_p_val, '5': default_p_val, '6': default_p_val, '7': default_p_val}} qos['policy']['random_detect'][rd_name] = dict_merge( default_precedence, qos['policy']['random_detect'][rd_name]) qos = conf.merge_defaults(qos, recursive=True) if 'traffic_match_group' in qos: for group, group_config in qos['traffic_match_group'].items(): if 'match_group' in group_config: qos['traffic_match_group'][group]['match'] = _get_group_match(qos['traffic_match_group'], group) for policy in qos.get('policy', []): for p_name, p_config in qos['policy'][policy].items(): # cleanup empty match config if 'class' in p_config: for cls, cls_config in p_config['class'].items(): if 'match_group' in cls_config: # merge group match to match for group in cls_config['match_group']: for match, match_conf in qos['traffic_match_group'].get(group, {'match': {}})['match'].items(): if 'match' not in cls_config: cls_config['match'] = dict() if match in cls_config['match']: cls_config['match'][f'{group}-{match}'] = match_conf else: cls_config['match'][match] = match_conf if 'match' in cls_config: cls_config['match'] = _clean_conf_dict(cls_config['match']) if cls_config['match'] == {}: del cls_config['match'] return qos def _verify_match(cls_config: dict) -> None: if 'match' in cls_config: for match, match_config in cls_config['match'].items(): if {'ip', 'ipv6'} <= set(match_config): raise ConfigError( f'Can not use both IPv6 and IPv4 in one match ({match})!') def _verify_match_group_exist(cls_config, qos): if 'match_group' in cls_config: for group in cls_config['match_group']: if 'traffic_match_group' not in qos or group not in qos['traffic_match_group']: Warning(f'Match group "{group}" does not exist!') def verify(qos): if not qos or 'interface' not in qos: return None # network policy emulator # reorder rerquires delay to be set if 'policy' in qos: for policy_type in qos['policy']: for policy, policy_config in qos['policy'][policy_type].items(): # a policy with it's given name is only allowed to exist once # on the system. This is because an interface selects a policy # for ingress/egress traffic, and thus there can only be one # policy with a given name. # # We check if the policy name occurs more then once - error out # if this is true counter = 0 for _, path in dict_search_recursive(qos['policy'], policy): counter += 1 if counter > 1: raise ConfigError(f'Conflicting policy name "{policy}", already in use!') if 'class' in policy_config: for cls, cls_config in policy_config['class'].items(): # bandwidth is not mandatory for priority-queue - that is why this is on the exception list if 'bandwidth' not in cls_config and policy_type not in ['priority_queue', 'round_robin', 'shaper_hfsc']: raise ConfigError(f'Bandwidth must be defined for policy "{policy}" class "{cls}"!') _verify_match(cls_config) _verify_match_group_exist(cls_config, qos) if policy_type in ['random_detect']: if 'precedence' in policy_config: for precedence, precedence_config in policy_config['precedence'].items(): max_tr = int(precedence_config['maximum_threshold']) if {'maximum_threshold', 'minimum_threshold'} <= set(precedence_config): min_tr = int(precedence_config['minimum_threshold']) if min_tr >= max_tr: raise ConfigError(f'Policy "{policy}" uses min-threshold "{min_tr}" >= max-threshold "{max_tr}"!') if {'maximum_threshold', 'queue_limit'} <= set(precedence_config): queue_lim = int(precedence_config['queue_limit']) if queue_lim < max_tr: raise ConfigError(f'Policy "{policy}" uses queue-limit "{queue_lim}" < max-threshold "{max_tr}"!') if policy_type in ['priority_queue']: if 'default' not in policy_config: raise ConfigError(f'Policy {policy} misses "default" class!') if 'default' in policy_config: if 'bandwidth' not in policy_config['default'] and policy_type not in ['priority_queue', 'round_robin', 'shaper_hfsc']: raise ConfigError('Bandwidth not defined for default traffic!') # we should check interface ingress/egress configuration after verifying that # the policy name is used only once - this makes the logic easier! for interface, interface_config in qos['interface'].items(): for direction in ['egress', 'ingress']: # bail out early if shaper for given direction is not used at all if direction not in interface_config: continue policy_name = interface_config[direction] if 'policy' not in qos or list(dict_search_recursive(qos['policy'], policy_name)) == []: raise ConfigError(f'Selected QoS policy "{policy_name}" does not exist!') shaper_type, shaper_config = get_shaper(qos, interface_config, direction) tmp = shaper_type(interface).get_direction() if direction not in tmp: raise ConfigError(f'Selected QoS policy on interface "{interface}" only supports "{tmp}"!') if 'traffic_match_group' in qos: for group, group_config in qos['traffic_match_group'].items(): _verify_match(group_config) _verify_match_group_exist(group_config, qos) return None def generate(qos): if not qos or 'interface' not in qos: return None return None def apply(qos): # Always delete "old" shapers first for interface in interfaces(): # Ignore errors (may have no qdisc) run(f'tc qdisc del dev {interface} parent ffff:') run(f'tc qdisc del dev {interface} root') call_dependents() if not qos or 'interface' not in qos: return None for interface, interface_config in qos['interface'].items(): - if not verify_interface_exists(interface, warning_only=True): + if not verify_interface_exists(interface, state_required=True, warning_only=True): # When shaper is bound to a dialup (e.g. PPPoE) interface it is # possible that it is yet not availbale when to QoS code runs. # Skip the configuration and inform the user via warning_only=True continue for direction in ['egress', 'ingress']: # bail out early if shaper for given direction is not used at all if direction not in interface_config: continue shaper_type, shaper_config = get_shaper(qos, interface_config, direction) tmp = shaper_type(interface) tmp.update(shaper_config, direction) return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1)