From fc6fa8c6683988d09ed4f53ec69cc7a96ed03a41 Mon Sep 17 00:00:00 2001 From: ciaens Date: Mon, 19 Oct 2026 14:04:07 +0200 Subject: interfaces: T6393: install tc filters for every source of a target When an interface is used as the mirror target of other interfaces, its commit installs the tc(8) mirror filters on their behalf, so that mirroring works even if the target did not exist yet when the sources were configured. is_mirror_intf() however only remembered the last source it found, ignored the target's own mirror configuration whenever it had sources, and did not consider VLAN sub-interfaces or "redirect" at all. Return all sources (including vif, vif-s, vif-c and redirecting interfaces), found by walking the interfaces dictionary once instead of calling list_nodes() and exists() per node, and apply the interface's own configuration plus one set of filters per source. A source whose target does not exist yet is skipped with a warning, the target installs the filter once it exists. Only sources missing a filter into the target are touched (each configured direction is checked), so a commit of the target no longer drops and re-adds the qdiscs of every source. A filter left behind by a re-created target is reported by tc as "*" and replaced. A redirecting source may carry a QoS policy: its dictionary gets the "qos" marker of get_interface_dict(), and only the mirred filters are removed instead of the qdiscs. This also drops the stale redirect filter once "redirect" is deleted from a shaped interface. --- python/vyos/configdict.py | 56 ++++++++++++++++---------- python/vyos/ifconfig/interface.py | 82 +++++++++++++++++++++++++++++++-------- 2 files changed, 102 insertions(+), 36 deletions(-) (limited to 'python') diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index d316ece3b..3353c70be 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -233,37 +233,53 @@ def is_member(conf, interface, intftype=None): def is_mirror_intf(conf, interface, direction=None): """ - Check whether the passed interface is used for port mirroring. Direction - is optional, if not passed it will search all known direction - (currently ingress and egress) + Check whether the passed interface is used as the mirror or redirect + target of other interfaces, including VLAN (vif, vif-s, vif-c) + sub-interfaces. Direction is optional, if not passed it will search all + known direction (currently ingress and egress) Returns: None -> Interface is not a monitor interface - Array() -> This interface is a monitor interface of interfaces + dict -> {source interface name : source interface config dict} for every + interface mirroring or redirecting into the passed interface """ - from vyos.ifconfig import Section - directions = ['ingress', 'egress'] if direction not in directions + [None]: raise ValueError(f'Unknown interface mirror direction "{direction}"') direction = directions if direction == None else [direction] - ret_val = None - base = ['interfaces'] - - for dir in direction: - for iftype in conf.list_nodes(base): - iftype_base = base + [iftype] - for intf in conf.list_nodes(iftype_base): - mirror = iftype_base + [intf, 'mirror', dir, interface] - if conf.exists(mirror): - path = ['interfaces', Section.section(intf), intf] - tmp = conf.get_config_dict(path, key_mangling=('-', '_'), - get_first_key=True) - ret_val = {intf : tmp} + ret_val = {} + interfaces = conf.get_config_dict( + ['interfaces'], + key_mangling=('-', '_'), + get_first_key=True, + no_tag_node_value_mangle=True, + ) - return ret_val + def add_source(ifname, config): + targets = [config.get('mirror', {}).get(dir) for dir in direction] + # redirect only works on ingress + if 'ingress' in direction: + targets.append(config.get('redirect')) + if interface not in targets: + return + ret_val[ifname] = config + # QoS marker, see get_interface_dict() + if conf.exists(['qos', 'interface', ifname]): + ret_val[ifname]['qos'] = {} + + for intfs in interfaces.values(): + for intf, config in intfs.items(): + add_source(intf, config) + for vif, vif_config in config.get('vif', {}).items(): + add_source(f'{intf}.{vif}', vif_config) + for vif_s, vif_s_config in config.get('vif_s', {}).items(): + add_source(f'{intf}.{vif_s}', vif_s_config) + for vif_c, vif_c_config in vif_s_config.get('vif_c', {}).items(): + add_source(f'{intf}.{vif_s}.{vif_c}', vif_c_config) + + return ret_val or None def has_address_configured(conf, intf): """ diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index b00bbc18a..35f4a8ee4 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -26,6 +26,7 @@ from netifaces import ifaddresses # pylint: disable = no-name-in-module from socket import AF_INET from socket import AF_INET6 +from vyos.base import Warning from vyos.configdict import list_diff from vyos.configdict import dict_merge from vyos.configdict import get_vlan_ids @@ -41,6 +42,7 @@ from vyos.utils.network import get_interface_config from vyos.utils.network import get_interface_address from vyos.utils.network import get_interface_namespace from vyos.utils.network import get_vrf_tableid +from vyos.utils.network import interface_exists from vyos.utils.network import is_netns_interface from vyos.utils.process import is_systemd_service_active from vyos.utils.process import stop_systemd_unit @@ -1729,33 +1731,74 @@ class Interface(Control): # Please refer to the document for details # - https://man7.org/linux/man-pages/man8/tc.8.html # - https://man7.org/linux/man-pages/man8/tc-mirred.8.html - # Depending if we are the source or the target interface of the port - # mirror we need to setup some variables. # Don't allow for netns yet if 'netns' in self.config: return None - source_if = self.ifname + # Our own mirror/redirect configuration + self._apply_mirror_redirect(self.ifname, self.config) - mirror_config = None - if 'mirror' in self.config: - mirror_config = self.config['mirror'] - if 'is_mirror_intf' in self.config: - source_if = next(iter(self.config['is_mirror_intf'])) - mirror_config = self.config['is_mirror_intf'][source_if].get('mirror', None) - - redirect_config = None + # Install the filters of interfaces mirroring or redirecting into us, + # they could not do it themselves if we did not exist yet (T3089, T6393) + for source_if, source_config in self.config.get('is_mirror_intf', {}).items(): + if not interface_exists(source_if): + continue + if self._mirror_redirect_installed(source_if, source_config): + continue + self._apply_mirror_redirect(source_if, source_config) + + def _mirred_filters(self, dev, parent): + """tc(8) filters with a mirred action on dev/parent: (filter, target)""" + out, _ = self._popen(f'tc -j filter show dev {dev} parent {parent}') + for tc_filter in json.loads(out or '[]'): + for action in dict_search('options.actions', tc_filter) or []: + if action.get('kind') == 'mirred': + yield tc_filter, action.get('to_dev') + + def _mirror_redirect_installed(self, source_if, config): + """Check if source_if has all of its tc(8) mirred filters into us""" + parents = [] + for direction, target in config.get('mirror', {}).items(): + if target == self.ifname: + parents.append('ffff:' if direction == 'ingress' else '1:') + if config.get('redirect') == self.ifname: + parents.append('ffff:') + for parent in parents: + targets = [target for _, target in self._mirred_filters(source_if, parent)] + if self.ifname not in targets: + return False + return True + def _apply_mirror_redirect(self, source_if, config): + """ + Install the tc(8) mirror or redirect filters of source_if according to + its configuration dictionary, we are either source_if or its target. + """ # clear existing ingess - ignore errors (e.g. "Error: Cannot find specified # qdisc on specified device") - we simply cleanup all stuff here - if not 'qos' in self.config: + if 'qos' not in config: self._popen(f'tc qdisc del dev {source_if} root 2>/dev/null') self._popen(f'tc qdisc del dev {source_if} ingress 2>/dev/null') + else: + # keep the QoS qdiscs, only remove our own (mirred) filters + for tc_filter, _ in self._mirred_filters(source_if, 'ffff:'): + self._popen( + f'tc filter del dev {source_if} parent ffff: ' + f'handle {tc_filter["options"]["fh"]} pref {tc_filter["pref"]} ' + f'protocol {tc_filter["protocol"]} {tc_filter["kind"]}' + ) # Apply interface mirror policy - if mirror_config: - for direction, target_if in mirror_config.items(): + if 'mirror' in config: + for direction, target_if in config['mirror'].items(): + if not interface_exists(target_if): + Warning( + f'Mirror target "{target_if}" of "{source_if}" does not ' + 'exist yet, tc filter will be installed once it is created' + ) + continue + if direction == 'ingress': handle = 'ffff: ingress' parent = 'ffff:' @@ -1773,11 +1816,18 @@ class Interface(Control): if err: print('tc filter for mirror port failed') # Apply interface traffic redirection policy - elif 'redirect' in self.config: + elif 'redirect' in config: + target_if = config['redirect'] + if not interface_exists(target_if): + Warning( + f'Redirect target "{target_if}" of "{source_if}" does not ' + 'exist yet, tc filter will be installed once it is created' + ) + return None + _, err = self._popen(f'tc qdisc add dev {source_if} handle ffff: ingress') if err: print(f'tc qdisc add for redirect failed!') - target_if = self.config['redirect'] _, err = self._popen(f'tc filter add dev {source_if} parent ffff: protocol '\ f'all prio 10 u32 match u32 0 0 flowid 1:1 action mirred '\ f'egress redirect dev {target_if}') -- cgit v1.2.3