--- a/usr/lib/python3/dist-packages/vyos/vpp/control_vpp.py +++ b/usr/lib/python3/dist-packages/vyos/vpp/control_vpp.py @@ -724,6 +724,82 @@ ) @_Decorators.api_call + def mpls_enabled_interfaces(self) -> list[int]: + """List the interfaces with MPLS enabled + + Returns: + list[int]: interface indexes + """ + return [ + iface.sw_if_index + for iface in self.__vpp_api_client.api.mpls_interface_dump() + ] + + @_Decorators.api_call + def vlan_sub_interfaces(self, ifname: str) -> list[str]: + """List the VLAN sub-interfaces of an interface + + Args: + ifname (str): name of an interface in VPP + + Returns: + list[str]: names of the sub-interfaces in VPP + """ + index = self.get_sw_if_index(ifname) + return [ + iface.interface_name + for iface in self.__vpp_api_client.api.sw_interface_dump() + if iface.sup_sw_if_index == index and iface.sw_if_index != index + ] + + def disable_mpls_tree(self, ifname: str) -> None: + """Disable MPLS on an interface and on its VLAN sub-interfaces + + VPP counts MPLS per interface index and keeps the count when the + interface is deleted. A new interface with the same index would then + never get the MPLS feature enabled, so MPLS has to be off before an + interface or a VLAN is deleted. + + Args: + ifname (str): name of an interface in VPP + """ + for child in self.vlan_sub_interfaces(ifname): + self.disable_mpls_tree(child) + if self.get_sw_if_index(ifname) in self.mpls_enabled_interfaces(): + self.set_interface_mpls(ifname, False) + + @_Decorators.api_call + def set_interface_mpls(self, ifname: str, enable: bool = True) -> None: + """Enable or disable MPLS on an interface + + linux-cp mirrors the state to the kernel interface of the pair. VPP + refuses to enable MPLS while the default MPLS table does not exist, + it is created here. + + Args: + ifname (str): name of an interface in VPP + enable (bool): enable or disable MPLS + """ + sw_if_index = self.get_sw_if_index(ifname) + if sw_if_index is None: + raise VPPValueError(f'Interface {ifname} not found in VPP') + if enable: + reply = self.__vpp_api_client.api.mpls_table_add_del( + mt_is_add=True, mt_table={'mt_table_id': 0} + ) + if reply.retval != 0: + raise VPPValueError(f'Cannot create the MPLS table: {reply.retval}') + reply = self.__vpp_api_client.api.sw_interface_set_mpls_enable( + sw_if_index=sw_if_index, + enable=enable, + ) + if reply.retval != 0: + raise VPPValueError( + f'Cannot {"enable" if enable else "disable"} MPLS on {ifname}: ' + f'{reply.retval}' + ) + + @_Decorators.api_call def set_promisc(self, ifname: str, enable: bool = True) -> None: """Set promiscuous mode for interface --- a/usr/libexec/vyos/conf_mode/interfaces_ethernet.py +++ b/usr/libexec/vyos/conf_mode/interfaces_ethernet.py @@ -30,6 +30,7 @@ from vyos.configdict import node_changed from vyos.configdict import get_flowtable_interfaces from vyos.configdiff import Diff +from vyos.configdiff import get_commit_scripts from vyos.configverify import verify_address from vyos.configverify import verify_dhcpv6 from vyos.configverify import verify_interface_exists @@ -285,6 +286,27 @@ if 'static_arp' in ethernet: set_dependents('static_arp', conf) + # MPLS of a VPP interface and of its VLANs, applied again after the update + # (see apply()), also when the interface is removed from VPP in the same + # commit. The script of the last VPP interface that is rebuilt in the + # commit does it, the VLANs of the others have to exist by then. + if ( + dict_search(f'settings.interface.{ifname}', vpp_config) is not None + or is_node_changed(conf, ['vpp', 'settings', 'interface', ifname]) + ) and conf.exists(['protocols', 'mpls', 'interface']): + vpp_nics = { + *(dict_search('settings.interface', vpp_config) or {}), + *node_changed(conf, ['vpp', 'settings', 'interface']), + } + pending = get_commit_scripts(conf) + called = getattr(conf, 'scripts_called', []) + if not any( + f'interfaces_ethernet_{nic}' in pending + and f'interfaces_ethernet_{nic}' not in called + for nic in vpp_nics - {ifname} + ): + set_dependents('mpls', conf) + # Check vrf membership, to ensure firewall is updated if is_vrf_changed(conf, ifname): set_dependents('firewall', conf) @@ -565,6 +587,29 @@ def apply(ethernet): ifname = ethernet['ifname'] + + # VPP keeps the MPLS count of a deleted VLAN, a VLAN of a VPP interface that + # is deleted needs MPLS off first. The update also deletes and creates a + # VLAN again when the protocol of a vif-s or the QoS mapping of a vif + # changes, these are handled the same way. The mpls dependent turns MPLS + # on again afterwards. + if dict_search(f'vpp.settings.interface.{ifname}', ethernet) is not None and ( + is_systemd_service_running('vpp.service') + ): + vpp_api = VPPControl() + vlan_ids = [ + *ethernet.get('vif_remove', {}), + *ethernet.get('vif_s_remove', {}), + *ethernet.get('vif_s', {}), + *[ + vif_id + for vif_id, vif in ethernet.get('vif', {}).items() + if 'ingress_qos' in vif or 'egress_qos' in vif + ], + ] + for vlan_id in vlan_ids: + vpp_api.disable_mpls_tree(f'{ifname}.{vlan_id}') + e = EthernetIf(ifname) if 'deleted' in ethernet: e.remove() --- a/usr/libexec/vyos/conf_mode/protocols_mpls.py +++ b/usr/libexec/vyos/conf_mode/protocols_mpls.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import os +import re from sys import exit @@ -24,8 +25,10 @@ from vyos.frrender import get_frrender_dict from vyos.utils.dict import dict_search from vyos.utils.file import read_file +from vyos.utils.process import is_systemd_service_active from vyos.utils.system import sysctl_write from vyos.configverify import verify_interface_exists +from vyos.vpp.control_vpp import VPPControl from vyos import ConfigError from vyos import airbag airbag.enable() @@ -47,6 +50,15 @@ if 'interface' in mpls: for interface in mpls['interface']: verify_interface_exists(mpls, interface) + # The entry of a VPP interface (vppbond0, vpplo1, ...) does not + # survive the load of the saved configuration at boot: the name + # does not match the interface name constraint and the interface + # does not exist yet. + if re.match(r'vpp(?!tap|tun)', interface): + raise ConfigError( + f'MPLS is not supported on the VPP interface "{interface}", ' + f'only on physical interfaces and their VLANs!' + ) # Checks to see if LDP is properly configured if 'ldp' in mpls: @@ -117,6 +129,36 @@ for system_interface in system_interfaces: sysctl_write(['net', 'mpls', 'conf', system_interface, 'input'], 0) + # VPP drops labeled packets on an interface that has MPLS disabled, + # whatever the kernel setting is. linux-cp copies the state to the kernel + # interface of the pair, also when the pair is created later, as it is for + # a VLAN sub-interface after a rebuild. So enable it by the VPP name, found + # through the pair of the interface or of its parent. + if is_systemd_service_active('vpp.service'): + vpp = VPPControl() + enabled = vpp.mpls_enabled_interfaces() + pairs = {pair['kernel_name']: pair for pair in vpp.lcp_pairs_list()} + + def vpp_name(name): + if name in pairs: + return pairs[name]['vpp_name_hw'] + parent, _, vlan_id = name.rpartition('.') + parent_name = vpp_name(parent) if parent and vlan_id.isdigit() else None + return f'{parent_name}.{vlan_id}' if parent_name else None + + wanted = [] + for name in mpls.get('interface', {}): + vpp_ifname = vpp_name(name) + index = vpp.get_sw_if_index(vpp_ifname) if vpp_ifname else None + if index is None: + continue + wanted.append(index) + if index not in enabled: + vpp.set_interface_mpls(vpp_ifname, True) + for pair in pairs.values(): + if pair['vpp_index_hw'] in enabled and pair['vpp_index_hw'] not in wanted: + vpp.set_interface_mpls(pair['vpp_name_hw'], False) + return None if __name__ == '__main__': --- a/usr/libexec/vyos/conf_mode/vpp.py +++ b/usr/libexec/vyos/conf_mode/vpp.py @@ -31,6 +31,7 @@ from vyos.config import Config, config_dict_merge from vyos.configdep import set_dependents from vyos.configdep import call_dependents +from vyos.configdiff import get_commit_scripts from vyos.configdict import node_changed, is_member from vyos.configverify import verify_interface_exists from vyos.configverify import verify_virtual_interface_exists @@ -276,6 +277,20 @@ return False +def set_mpls_dependent(conf, vpp_nics: list[str]) -> None: + """Replay protocols mpls after the NICs are set up again + + The Ethernet script of a VPP interface that is part of the same commit + runs later and has no VLANs before that, it replays MPLS itself. + """ + if not conf.exists(['protocols', 'mpls', 'interface']): + return + pending = get_commit_scripts(conf) + if any(f'interfaces_ethernet_{nic}' in pending for nic in vpp_nics): + return + set_dependents('mpls', conf) + + def get_config(config=None): # use persistent config to store interfaces data between executions # this is required because some interfaces after they are connected @@ -339,6 +354,8 @@ if not conf.exists(base): if changed_pppoe_ifaces: set_dependents('pppoe_server', conf) + # the NICs go back to the kernel, MPLS has to be set up there again + set_mpls_dependent(conf, [iface['iface_name'] for iface in removed_ifaces]) return { 'removed_ifaces': removed_ifaces, 'persist_config': eth_ifaces_persist, @@ -567,6 +584,15 @@ set_dependents('pppoe_server', conf) config['changed_pppoe_ifaces'] = changed_pppoe_ifaces + # MPLS dependency, a VPP restart drops the MPLS state of the interfaces + set_mpls_dependent( + conf, + [ + *config.get('settings', {}).get('interface', {}), + *[iface['iface_name'] for iface in removed_ifaces], + ], + ) + return config Nur in orig-edit/usr/libexec/vyos/conf_mode: vpp.py.orig.