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 From 99271db4a3d75241fe8ccedef0877d7d07e3b96f Mon Sep 17 00:00:00 2001 From: ciaens Date: Mon, 19 Oct 2026 14:04:07 +0200 Subject: interfaces: T6393: accept mirror and redirect targets defined on the CLI Interface types are committed in priority order (ethernet 318, bridge 319, bonding 320, ... tunnel 380). verify_mirror_redirect() required the mirror and redirect target to exist in the Kernel, so "interfaces ethernet eth1 mirror ingress tun0" (or "redirect tun0") could never commit on the first pass at boot, or when both interfaces are created in the same commit: VYOS_TAGNODE_VALUE=eth1 .../interfaces_ethernet.py Requested mirror interface "tun0" does not exist! Sending reply: ERROR_COMMIT with output VYOS_TAGNODE_VALUE=tun0 .../interfaces_tunnel.py Sending reply: SUCCESS with output The whole ethernet node was rolled back, taking its addresses and everything on top of them down with it. The tc filter itself is installed by the target's commit since the previous commit, only verify() blocks. Also accept a target which is defined on the CLI, at its interface name position of the interfaces tree that Config.get_config_dict() attaches to its dictionaries. A target that is neither configured nor existing still fails the commit with the same error message as before. VLAN sub-interface targets are only accepted from the Kernel, as nothing would install their filter later on. VLAN sub-interfaces as sources get the interfaces tree attached to their own dictionaries in get_interface_dict(). A target placed in a network namespace is out of reach for tc(8) and is rejected with its own message, also while its device still sits in the default namespace. A target whose device is created asynchronously by a daemon (OpenVPN) now also passes verify at boot; if its device is not up yet when that interface applies, the filter is installed on the next commit of either side instead of the source interface being rolled back. src/tests/test_configverify.py was removed together with its last test case (T8859) and is re-added with tests for the target verification. --- python/vyos/configdict.py | 8 +++++ python/vyos/configverify.py | 24 +++++++++---- src/tests/test_configverify.py | 76 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 src/tests/test_configverify.py (limited to 'python') diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index 3353c70be..d52a3940e 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -20,6 +20,7 @@ import os import json from vyos.config import Config +from vyos.config import ConfigDict from vyos.utils.dict import dict_search from vyos.utils.process import cmdl @@ -580,6 +581,9 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk dict['ipv6']['address'].update({'interface_identifier_old': interface_identifier}) for vif, vif_config in dict.get('vif', {}).items(): + # T6393: carry the CLI interfaces tree, see Config.get_config_dict() + dict['vif'][vif] = ConfigDict(vif_config) + dict['vif'][vif].interfaces_root = dict.interfaces_root # Add subinterface name to dictionary dict['vif'][vif].update({'ifname' : f'{ifname}.{vif}'}) @@ -607,6 +611,8 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk if dhcpv6: dict['vif'][vif].update({'dhcpv6_options_changed' : {}}) for vif_s, vif_s_config in dict.get('vif_s', {}).items(): + dict['vif_s'][vif_s] = ConfigDict(vif_s_config) + dict['vif_s'][vif_s].interfaces_root = dict.interfaces_root # Add subinterface name to dictionary dict['vif_s'][vif_s].update({'ifname' : f'{ifname}.{vif_s}'}) @@ -635,6 +641,8 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk if dhcpv6: dict['vif_s'][vif_s].update({'dhcpv6_options_changed' : {}}) for vif_c, vif_c_config in vif_s_config.get('vif_c', {}).items(): + dict['vif_s'][vif_s]['vif_c'][vif_c] = ConfigDict(vif_c_config) + dict['vif_s'][vif_s]['vif_c'][vif_c].interfaces_root = dict.interfaces_root # Add subinterface name to dictionary dict['vif_s'][vif_s]['vif_c'][vif_c].update({'ifname' : f'{ifname}.{vif_s}.{vif_c}'}) diff --git a/python/vyos/configverify.py b/python/vyos/configverify.py index d918c53bb..0c2e2f62b 100644 --- a/python/vyos/configverify.py +++ b/python/vyos/configverify.py @@ -188,21 +188,31 @@ def verify_mirror_redirect(config): # limitation from the past raise ConfigError('Cannot use QoS together with mirror!') + def verify_target(kind, target): + # T6393: interface types are committed in priority order, the target + # may only be defined on the CLI and not (yet) exist in the Kernel. + # tc(8) can not reach a target in another netns + for interfaces in getattr(config, 'interfaces_root', {}).values(): + if target in interfaces: + if 'netns' in interfaces[target]: + raise ConfigError( + f'Requested {kind} interface "{target}" is in a ' + 'network namespace!' + ) + return + if not interface_exists(target): + raise ConfigError(f'Requested {kind} interface "{target}" does not exist!') + 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!') + verify_target('mirror', mirror_interface) if mirror_interface == config['ifname']: raise ConfigError(f'Cannot 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!') + verify_target('redirect', config['redirect']) def verify_authentication(config): """ diff --git a/src/tests/test_configverify.py b/src/tests/test_configverify.py new file mode 100644 index 000000000..1066e52e2 --- /dev/null +++ b/src/tests/test_configverify.py @@ -0,0 +1,76 @@ +# Copyright 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 . + + +from unittest import TestCase +from vyos import ConfigError +from vyos.config import ConfigDict +from vyos.configverify import verify_mirror_redirect + + +class TestVerifyMirrorRedirect(TestCase): + # CLI "interfaces" tree, carried by the dictionaries returned from + # Config.get_config_dict() in their interfaces_root attribute + interfaces_root = { + 'dummy': {'dum4711': {}, 'dum4712': {'netns': 'NS4711'}}, + 'ethernet': { + 'eth0': {}, + 'eth1': {'vif': {'100': {'address': ['192.0.2.1/24']}}}, + }, + 'tunnel': {'tun4711': {'encapsulation': 'gre'}}, + # "tun4712" is a WireGuard peer name here, not an interface + 'wireguard': {'wg4711': {'peer': {'tun4712': {'address': '192.0.2.2'}}}}, + } + + def _config(self, ifname, **options): + config = ConfigDict({'ifname': ifname, **options}) + config.interfaces_root = self.interfaces_root + return config + + def test_target_in_kernel(self): + verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'lo'})) + verify_mirror_redirect(self._config('eth0', redirect='lo')) + + def test_target_defined_on_cli_only(self): + # T6393: target defined on the CLI but not (yet) created in the kernel + verify_mirror_redirect( + self._config('eth0', mirror={'ingress': 'tun4711', 'egress': 'dum4711'}) + ) + verify_mirror_redirect(self._config('eth0', redirect='tun4711')) + verify_mirror_redirect(self._config('eth0.100', mirror={'ingress': 'tun4711'})) + + def test_target_missing(self): + with self.assertRaisesRegex(ConfigError, 'mirror interface "tun4712"'): + verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'tun4712'})) + with self.assertRaisesRegex(ConfigError, 'redirect interface "tun4712"'): + verify_mirror_redirect(self._config('eth0', redirect='tun4712')) + # VLAN target defined on the CLI but not existing in the kernel + with self.assertRaises(ConfigError): + verify_mirror_redirect(self._config('eth0', mirror={'egress': 'eth1.100'})) + # a target in another netns is out of reach for tc(8) + with self.assertRaisesRegex(ConfigError, 'network namespace'): + verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'dum4712'})) + # without any knowledge of the CLI the kernel is the only reference + with self.assertRaises(ConfigError): + verify_mirror_redirect({'ifname': 'eth0', 'mirror': {'ingress': 'tun4711'}}) + + def test_mirror_to_self(self): + with self.assertRaises(ConfigError): + verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'eth0'})) + + def test_mirror_and_redirect_exclusive(self): + with self.assertRaises(ConfigError): + verify_mirror_redirect( + self._config('eth0', mirror={'ingress': 'lo'}, redirect='lo') + ) -- cgit v1.2.3 From 9854913e23cf21363a931640cc9994c169969c88 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Nov 2026 23:19:26 +0100 Subject: interfaces: T6393: install mirror and redirect filters for VLAN targets A VLAN sub-interface used as mirror or redirect target (e.g. bond0.10) was only accepted if it already existed in the Kernel. The configuration committed at runtime, but at boot the source interface is committed before the VLAN is created - verify failed and the whole source interface was rolled back, as reported for tunnel targets. A VLAN target is now also accepted if its parent interface has the vif, vif-s or vif-c configured on the CLI. VLAN sub-interfaces learn which interfaces mirror or redirect into them and install their filters once created, as other interfaces already do. Add a config load test mirroring and redirecting into tunnel and bond VLAN targets, which are committed after the ethernet sources. --- python/vyos/configdict.py | 13 +++++ python/vyos/configverify.py | 18 +++++-- .../configs/assert/interfaces-mirror-redirect | 14 +++++ smoketest/configs/interfaces-mirror-redirect | 60 ++++++++++++++++++++++ smoketest/scripts/cli/base_interfaces_test.py | 22 ++++++++ src/tests/test_configverify.py | 23 +++++++-- 6 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 smoketest/configs/assert/interfaces-mirror-redirect create mode 100644 smoketest/configs/interfaces-mirror-redirect (limited to 'python') diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index d52a3940e..8c311616c 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -604,6 +604,10 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk bridge = is_member(config, f'{ifname}.{vif}', 'bridge') if bridge: dict['vif'][vif].update({'is_bridge_member' : bridge}) + # Check if it is a monitor interface + mirror = is_mirror_intf(config, f'{ifname}.{vif}') + if mirror: dict['vif'][vif].update({'is_mirror_intf' : mirror}) + # Check if any DHCP options changed which require a client restart dhcp = is_node_changed(config, base + [ifname, 'vif', vif, 'dhcp-options']) if dhcp: dict['vif'][vif].update({'dhcp_options_changed' : {}}) @@ -634,6 +638,10 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk bridge = is_member(config, f'{ifname}.{vif_s}', 'bridge') if bridge: dict['vif_s'][vif_s].update({'is_bridge_member' : bridge}) + # Check if it is a monitor interface + mirror = is_mirror_intf(config, f'{ifname}.{vif_s}') + if mirror: dict['vif_s'][vif_s].update({'is_mirror_intf' : mirror}) + # Check if any DHCP options changed which require a client restart dhcp = is_node_changed(config, base + [ifname, 'vif-s', vif_s, 'dhcp-options']) if dhcp: dict['vif_s'][vif_s].update({'dhcp_options_changed' : {}}) @@ -666,6 +674,11 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pk if bridge: dict['vif_s'][vif_s]['vif_c'][vif_c].update( {'is_bridge_member' : bridge}) + # Check if it is a monitor interface + mirror = is_mirror_intf(config, f'{ifname}.{vif_s}.{vif_c}') + if mirror: dict['vif_s'][vif_s]['vif_c'][vif_c].update( + {'is_mirror_intf' : mirror}) + # Check if any DHCP options changed which require a client restart dhcp = is_node_changed(config, base + [ifname, 'vif-s', vif_s, 'vif-c', vif_c, 'dhcp-options']) if dhcp: dict['vif_s'][vif_s]['vif_c'][vif_c].update({'dhcp_options_changed' : {}}) diff --git a/python/vyos/configverify.py b/python/vyos/configverify.py index 0c2e2f62b..714f544f0 100644 --- a/python/vyos/configverify.py +++ b/python/vyos/configverify.py @@ -188,18 +188,30 @@ def verify_mirror_redirect(config): # limitation from the past raise ConfigError('Cannot use QoS together with mirror!') + def vlan_configured(interface, vlan): + # vif or vif-s, or a vif-c below a vif-s + if len(vlan) == 1: + return (('vif' in interface and vlan[0] in interface['vif']) or + ('vif-s' in interface and vlan[0] in interface['vif-s'])) + if len(vlan) == 2 and 'vif-s' in interface and vlan[0] in interface['vif-s']: + vif_s = interface['vif-s'][vlan[0]] + return 'vif-c' in vif_s and vlan[1] in vif_s['vif-c'] + return False + def verify_target(kind, target): # T6393: interface types are committed in priority order, the target # may only be defined on the CLI and not (yet) exist in the Kernel. # tc(8) can not reach a target in another netns + ifname, *vlan = target.split('.') for interfaces in getattr(config, 'interfaces_root', {}).values(): - if target in interfaces: - if 'netns' in interfaces[target]: + if ifname in interfaces: + if 'netns' in interfaces[ifname]: raise ConfigError( f'Requested {kind} interface "{target}" is in a ' 'network namespace!' ) - return + if not vlan or vlan_configured(interfaces[ifname], vlan): + return if not interface_exists(target): raise ConfigError(f'Requested {kind} interface "{target}" does not exist!') diff --git a/smoketest/configs/assert/interfaces-mirror-redirect b/smoketest/configs/assert/interfaces-mirror-redirect new file mode 100644 index 000000000..0bff8bc21 --- /dev/null +++ b/smoketest/configs/assert/interfaces-mirror-redirect @@ -0,0 +1,14 @@ +set interfaces bonding bond10 vif 100 +set interfaces ethernet eth0 mirror egress 'tun10' +set interfaces ethernet eth0 mirror ingress 'tun10' +set interfaces ethernet eth0 vif 201 address '198.51.100.1/24' +set interfaces ethernet eth0 vif 201 mirror egress 'bond10.100' +set interfaces ethernet eth0 vif 201 mirror ingress 'bond10.100' +set interfaces ethernet eth0 vif 202 address '203.0.113.1/24' +set interfaces ethernet eth0 vif 202 redirect 'tun20' +set interfaces tunnel tun10 encapsulation 'gre' +set interfaces tunnel tun10 remote '192.0.2.10' +set interfaces tunnel tun10 source-address '198.51.100.1' +set interfaces tunnel tun20 encapsulation 'gre' +set interfaces tunnel tun20 remote '192.0.2.20' +set interfaces tunnel tun20 source-address '203.0.113.1' diff --git a/smoketest/configs/interfaces-mirror-redirect b/smoketest/configs/interfaces-mirror-redirect new file mode 100644 index 000000000..1086470fb --- /dev/null +++ b/smoketest/configs/interfaces-mirror-redirect @@ -0,0 +1,60 @@ +interfaces { + bonding bond10 { + vif 100 { + } + } + ethernet eth0 { + duplex auto + mirror { + egress tun10 + ingress tun10 + } + speed auto + vif 201 { + address 198.51.100.1/24 + mirror { + egress bond10.100 + ingress bond10.100 + } + } + vif 202 { + address 203.0.113.1/24 + redirect tun20 + } + } + loopback lo { + } + tunnel tun10 { + encapsulation gre + remote 192.0.2.10 + source-address 198.51.100.1 + } + tunnel tun20 { + encapsulation gre + remote 192.0.2.20 + source-address 203.0.113.1 + } +} +system { + config-management { + commit-revisions 50 + } + console { + device @SERIAL_DEVICE@ { + speed @SERIAL_SPEED@ + } + } + host-name vyos + login { + user vyos { + authentication { + encrypted-password $6$r/Yw/07NXNY$/ZB.Rjf9jxEV.BYoDyLdH.kH14rU52pOBtrX.4S34qlPt77chflCHvpTCq9a6huLzwaMR50rEICzA5GoIRZlM0 + plaintext-password "" + } + } + } +} + +// Warning: Do not remove the following line. +// vyos-config-version: "bgp@8:broadcast-relay@1:cluster@2:config-management@2:conntrack@6:conntrack-sync@2:container@3:dhcp-relay@2:dhcp-server@11:dhcpv6-server@6:dns-dynamic@5:dns-forwarding@4:firewall@20:flow-accounting@3:https@7:ids@2:interfaces@34:ipoe-server@4:ipsec@14:isis@3:l2tp@10:lldp@3:mdns@1:monitoring@2:nat@8:nat66@3:nhrp@1:ntp@3:openconnect@3:openvpn@6:ospf@3:pim@1:pki@1:policy@9:pppoe-server@13:pptp@6:qos@3:quagga@12:reverse-proxy@3:rip@1:rpki@2:snmp@3:ssh@3:sstp@7:system@33:vpp@6:vrf@4:vrrp@4:vyos-accel-ppp@2:wanloadbalance@4:webproxy@2" +// Release version: 2026.10.26-2027-kernel diff --git a/smoketest/scripts/cli/base_interfaces_test.py b/smoketest/scripts/cli/base_interfaces_test.py index a3618b951..c7eec4a18 100644 --- a/smoketest/scripts/cli/base_interfaces_test.py +++ b/smoketest/scripts/cli/base_interfaces_test.py @@ -628,6 +628,28 @@ class BasicInterfaceTest: self.assertFalse(is_mirrored_to(interface, mirror, 'ffff')) self.assertFalse(is_mirrored_to(interface, mirror, '1')) + def test_span_mirror_vlan_target_created_later(self): + if not self._test_mirror: + self.skipTest(MSG_TESTCASE_UNSUPPORTED) + + # VLAN of a bond, which is committed after ethernet interfaces + bond = 'bond21354' + mirror = f'{bond}.10' + for interface in self._interfaces: + for option in self._options.get(interface, []): + self.cli_set(self._base_path + [interface] + option.split()) + self.cli_set(self._base_path + [interface, 'mirror', 'ingress', mirror]) + self.cli_set(['interfaces', 'bonding', bond, 'vif', '10']) + self.cli_commit() + + for interface in self._interfaces: + self.assertTrue(is_mirrored_to(interface, mirror, 'ffff')) + + for interface in self._interfaces: + self.cli_delete(self._base_path + [interface, 'mirror']) + self.cli_delete(['interfaces', 'bonding', bond]) + self.cli_commit() + def test_redirect_target_created_later(self): if not self._test_redirect: self.skipTest(MSG_TESTCASE_UNSUPPORTED) diff --git a/src/tests/test_configverify.py b/src/tests/test_configverify.py index 1066e52e2..d6e6ee18d 100644 --- a/src/tests/test_configverify.py +++ b/src/tests/test_configverify.py @@ -26,7 +26,13 @@ class TestVerifyMirrorRedirect(TestCase): 'dummy': {'dum4711': {}, 'dum4712': {'netns': 'NS4711'}}, 'ethernet': { 'eth0': {}, - 'eth1': {'vif': {'100': {'address': ['192.0.2.1/24']}}}, + 'eth1': { + 'vif': {'100': {'address': ['192.0.2.1/24']}}, + 'vif-s': {'200': {'vif-c': {'300': {}}}}, + }, + }, + 'virtual-ethernet': { + 'veth4711': {'netns': 'NS4711', 'vif': {'10': {}}}, }, 'tunnel': {'tun4711': {'encapsulation': 'gre'}}, # "tun4712" is a WireGuard peer name here, not an interface @@ -49,18 +55,27 @@ class TestVerifyMirrorRedirect(TestCase): ) verify_mirror_redirect(self._config('eth0', redirect='tun4711')) verify_mirror_redirect(self._config('eth0.100', mirror={'ingress': 'tun4711'})) + # VLAN sub-interface targets install the filter just the same + verify_mirror_redirect(self._config('eth0', mirror={'egress': 'eth1.100'})) + verify_mirror_redirect(self._config('eth0', redirect='eth1.200')) + verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'eth1.200.300'})) def test_target_missing(self): with self.assertRaisesRegex(ConfigError, 'mirror interface "tun4712"'): verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'tun4712'})) with self.assertRaisesRegex(ConfigError, 'redirect interface "tun4712"'): verify_mirror_redirect(self._config('eth0', redirect='tun4712')) - # VLAN target defined on the CLI but not existing in the kernel - with self.assertRaises(ConfigError): - verify_mirror_redirect(self._config('eth0', mirror={'egress': 'eth1.100'})) + # VLAN target neither defined on the CLI nor existing in the kernel + with self.assertRaisesRegex(ConfigError, 'does not exist'): + verify_mirror_redirect(self._config('eth0', mirror={'egress': 'eth1.101'})) + with self.assertRaisesRegex(ConfigError, 'does not exist'): + verify_mirror_redirect(self._config('eth0', redirect='eth1.200.301')) # a target in another netns is out of reach for tc(8) with self.assertRaisesRegex(ConfigError, 'network namespace'): verify_mirror_redirect(self._config('eth0', mirror={'ingress': 'dum4712'})) + # ... as is a VLAN sub-interface of an interface in another netns + with self.assertRaisesRegex(ConfigError, 'network namespace'): + verify_mirror_redirect(self._config('eth0', redirect='veth4711.10')) # without any knowledge of the CLI the kernel is the only reference with self.assertRaises(ConfigError): verify_mirror_redirect({'ifname': 'eth0', 'mirror': {'ingress': 'tun4711'}}) -- cgit v1.2.3 From 31e0ace5d2bac8240c7d3b88c7486f6ce4c22367 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Nov 2026 23:19:42 +0100 Subject: qos: T6393: keep redirect filter when applying an ingress policy An interface with both an ingress QoS policy and "redirect" lost its redirect filter on every QoS commit, including boot. QoS re-applied the interface redirect first and then re-created the ingress qdisc for its own policy, dropping the redirect filter again. Mirror and redirect are now re-applied after the QoS policies, and the redirect re-uses an existing ingress qdisc instead of failing to add it. --- python/vyos/ifconfig/interface.py | 3 ++- smoketest/scripts/cli/test_qos.py | 23 +++++++++++++++++++++++ src/conf_mode/qos.py | 17 +++++++---------- 3 files changed, 32 insertions(+), 11 deletions(-) (limited to 'python') diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 35f4a8ee4..bc59d099a 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -1825,7 +1825,8 @@ class Interface(Control): ) return None - _, err = self._popen(f'tc qdisc add dev {source_if} handle ffff: ingress') + # replace keeps an ingress qdisc of a QoS policy in place + _, err = self._popen(f'tc qdisc replace dev {source_if} handle ffff: ingress') if err: print(f'tc qdisc add for redirect failed!') _, err = self._popen(f'tc filter add dev {source_if} parent ffff: protocol '\ diff --git a/smoketest/scripts/cli/test_qos.py b/smoketest/scripts/cli/test_qos.py index f89cd9229..5a79139ae 100755 --- a/smoketest/scripts/cli/test_qos.py +++ b/smoketest/scripts/cli/test_qos.py @@ -1582,6 +1582,29 @@ class TestQoS(VyOSUnitTestSHIM.TestCase): filter_output = get_tc_filter_details(interface) self.assertNotIn('pedit', filter_output) + def test_29_policy_limiter_with_redirect(self): + # Limiter and redirect share the ingress qdisc, both must be installed + interface = self._interfaces[0] + redirect = 'dum21354' + policy_name = 'qos-redirect' + + self.cli_set(['interfaces', 'dummy', redirect]) + self.cli_set(['interfaces', 'ethernet', interface, 'redirect', redirect]) + self.cli_set(base_path + ['interface', interface, 'ingress', policy_name]) + self.cli_set(base_path + ['policy', 'limiter', policy_name, 'default', 'bandwidth', '10mbit']) + self.cli_commit() + + actions = [] + for tc_filter in get_tc_filter_json(interface, 'ingress'): + if 'options' in tc_filter and 'actions' in tc_filter['options']: + actions += [action['kind'] for action in tc_filter['options']['actions']] + self.assertIn('mirred', actions) + self.assertIn('police', actions) + + # committed together with the base_path removal in tearDown() + self.cli_delete(['interfaces', 'ethernet', interface, 'redirect']) + self.cli_delete(['interfaces', 'dummy', redirect]) + if __name__ == '__main__': unittest.main(verbosity=2, failfast=VyOSUnitTestSHIM.TestCase.debug_on()) diff --git a/src/conf_mode/qos.py b/src/conf_mode/qos.py index 7351f1367..86c348095 100644 --- a/src/conf_mode/qos.py +++ b/src/conf_mode/qos.py @@ -356,10 +356,7 @@ def apply_interface(qos, ifname): # Only tear down the ingress qdisc if this interface actually has an # ingress QoS policy bound. Otherwise this would remove an unrelated - # ingress redirect/mirror configuration (see set_mirror_redirect()) - # that call_dependents() just re-created, and it would never come - # back as only directions present in interface_config are re-applied - # below. + # ingress redirect/mirror configuration (see set_mirror_redirect()). if 'ingress' in interface_config: run(f'tc qdisc del dev {ifname} parent ffff:') run(f'tc qdisc del dev {ifname} root') @@ -391,13 +388,13 @@ def apply(qos): run(f'tc qdisc del dev {interface} parent ffff:') run(f'tc qdisc del dev {interface} root') - call_dependents() + if qos and 'interface' in qos: + for ifname in qos['interface']: + apply_interface(qos, ifname) - if not qos or 'interface' not in qos: - return None - - for ifname in qos['interface']: - apply_interface(qos, ifname) + # Re-apply mirror and redirect last: an ingress QoS policy re-creates the + # ingress qdisc, which would drop a redirect filter installed before + call_dependents() return None -- cgit v1.2.3 From c099da63a6c2dc6210d74a70522740472b3c0e58 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Nov 2026 23:19:48 +0100 Subject: qos: T6393: police ingress traffic before redirecting it With an ingress limiter and "redirect" on the same interface, the redirect filter was evaluated first and took all traffic, so the limiter never applied. The limiter filters of such an interface now redirect the traffic after policing it: what exceeds the rate is handled by "exceed", conforming traffic is redirected. A "not-exceed ok" is passed on to the redirect instead of ending the actions. The interface's own redirect filter moves to the lowest priority and only catches traffic no limiter class matched; an interface commit only replaces that filter and keeps the limiter in place. --- python/vyos/ifconfig/interface.py | 13 +++++-------- python/vyos/qos/base.py | 16 ++++++++++++++++ smoketest/scripts/cli/test_qos.py | 10 ++++++---- src/conf_mode/qos.py | 8 ++++++++ 4 files changed, 35 insertions(+), 12 deletions(-) (limited to 'python') diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index bc59d099a..7ae85ae79 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -1781,13 +1781,8 @@ class Interface(Control): 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"]}' - ) + # keep the QoS qdiscs and filters, only remove our own redirect filter + self._popen(f'tc filter del dev {source_if} parent ffff: pref 65000 2>/dev/null') # Apply interface mirror policy if 'mirror' in config: @@ -1829,8 +1824,10 @@ class Interface(Control): _, err = self._popen(f'tc qdisc replace dev {source_if} handle ffff: ingress') if err: print(f'tc qdisc add for redirect failed!') + # Lowest priority: an ingress QoS policy polices and redirects the + # traffic it matches first, see vyos.qos.base _, 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'all prio 65000 u32 match u32 0 0 flowid 1:1 action mirred '\ f'egress redirect dev {target_if}') if err: print('tc filter for redirect failed') diff --git a/python/vyos/qos/base.py b/python/vyos/qos/base.py index 651d3f1e1..736b197e1 100644 --- a/python/vyos/qos/base.py +++ b/python/vyos/qos/base.py @@ -242,6 +242,12 @@ class QoSBase: import pprint pprint.pprint(config) + # T6393: traffic of an ingress limiter on an interface with "redirect" + # is redirected after policing, see qos.py apply_interface() + redirect = [] + if 'redirect' in config: + redirect = ['action', 'mirred', 'egress', 'redirect', 'dev', config['redirect']] + if 'class' in config: # T9134: every class match is installed as its own tc filter, and # tc ties a filter priority ("prio"/"pref") to a single protocol - @@ -431,6 +437,7 @@ class QoSBase: # add pedit before flowid for filters without police if dscp_action: filter_cmd += dscp_action + filter_cmd += redirect filter_cmd += ['flowid', f'{self._parent:x}:{cls:x}'] self._cmdl(filter_cmd) @@ -450,6 +457,9 @@ class QoSBase: filter_cmd += ['conform-exceed', str(action)] if 'not_exceed' in cls_config: action = cls_config['not_exceed'] + # "ok" would end the action chain before the redirect + if redirect and action == 'ok': + action = 'pipe' filter_cmd[-1] += f'/{action}' if 'bandwidth' in cls_config: @@ -468,6 +478,7 @@ class QoSBase: filter_cmd += dscp_action cls = int(cls) + filter_cmd += redirect filter_cmd += ['flowid', f'{self._parent:x}:{cls:x}'] self._cmdl(filter_cmd) @@ -547,6 +558,9 @@ class QoSBase: filter_cmd += ['conform-exceed', str(action)] if 'not_exceed' in config['default']: action = config['default']['not_exceed'] + # "ok" would end the action chain before the redirect + if redirect and action == 'ok': + action = 'pipe' filter_cmd[-1] += f'/{action}' if 'bandwidth' in config['default']: @@ -561,6 +575,8 @@ class QoSBase: mtu = config['default']['mtu'] filter_cmd += ['mtu', str(mtu)] + filter_cmd += redirect + if 'class' in config: filter_cmd += ['flowid', f'{self._parent:x}:{default_cls_id:x}'] diff --git a/smoketest/scripts/cli/test_qos.py b/smoketest/scripts/cli/test_qos.py index 5a79139ae..bbc0fdeb4 100755 --- a/smoketest/scripts/cli/test_qos.py +++ b/smoketest/scripts/cli/test_qos.py @@ -1583,7 +1583,8 @@ class TestQoS(VyOSUnitTestSHIM.TestCase): self.assertNotIn('pedit', filter_output) def test_29_policy_limiter_with_redirect(self): - # Limiter and redirect share the ingress qdisc, both must be installed + # Limiter and redirect share the ingress qdisc: the traffic is policed + # first, what is not dropped gets redirected interface = self._interfaces[0] redirect = 'dum21354' policy_name = 'qos-redirect' @@ -1597,9 +1598,10 @@ class TestQoS(VyOSUnitTestSHIM.TestCase): actions = [] for tc_filter in get_tc_filter_json(interface, 'ingress'): if 'options' in tc_filter and 'actions' in tc_filter['options']: - actions += [action['kind'] for action in tc_filter['options']['actions']] - self.assertIn('mirred', actions) - self.assertIn('police', actions) + actions.append([action['kind'] for action in tc_filter['options']['actions']]) + # filters in priority order: the policer passes conforming traffic on + # to the redirect, our own redirect catches whatever no class matched + self.assertEqual(actions, [['police', 'mirred'], ['mirred']]) # committed together with the base_path removal in tearDown() self.cli_delete(['interfaces', 'ethernet', interface, 'redirect']) diff --git a/src/conf_mode/qos.py b/src/conf_mode/qos.py index 86c348095..94802d640 100644 --- a/src/conf_mode/qos.py +++ b/src/conf_mode/qos.py @@ -150,6 +150,10 @@ def get_config(config=None): type_node = path.split(" ")[1] # return only interface type node set_dependents(type_node, conf, ifname.split(".")[0]) + # T6393: an ingress policy redirects its traffic after policing it + if 'interface' in qos and ifname in qos['interface'] and conf.exists(f'{path} redirect'): + qos['interface'][ifname]['redirect'] = conf.return_value(f'{path} redirect') + for policy in qos.get('policy', []): if policy in ['random_detect']: for rd_name in list(qos['policy'][policy]): @@ -376,6 +380,10 @@ def apply_interface(qos, ifname): continue shaper_type, shaper_config = get_shaper(qos, interface_config, direction) + # tc rejects the whole filter if the redirect target does not exist (yet) + if (direction == 'ingress' and 'redirect' in interface_config and + interface_config['redirect'] in interfaces()): + shaper_config = dict(shaper_config, redirect=interface_config['redirect']) shaper_type(ifname).update(shaper_config, direction) return None -- cgit v1.2.3