Skip to content

Commit

Permalink
T6199: replace netifaces.interfaces() with common custom helpers
Browse files Browse the repository at this point in the history
* Use interface_exists() outside of verify()
* Use verify_interface_exists() in verify() to drop common error message
  • Loading branch information
c-po committed Apr 2, 2024
1 parent 86b6328 commit 4c7c168
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 81 deletions.
4 changes: 2 additions & 2 deletions python/vyos/configverify.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def verify_source_interface(config):
required by e.g. peth/MACvlan, MACsec ...
"""
import re
from netifaces import interfaces
from vyos.utils.network import interface_exists

ifname = config['ifname']
if 'source_interface' not in config:
Expand All @@ -287,7 +287,7 @@ def verify_source_interface(config):
if tmp.match(src_ifname):
raise ConfigError(f'Can not source "{ifname}" from dynamic interface "{src_ifname}"!')

if src_ifname not in interfaces():
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:
Expand Down
7 changes: 3 additions & 4 deletions python/vyos/ifconfig/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
# 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/>.

from netifaces import interfaces

from vyos.ifconfig.interface import Interface
from vyos.utils.assertion import assert_boolean
from vyos.utils.assertion import assert_list
from vyos.utils.assertion import assert_positive
from vyos.utils.dict import dict_search
from vyos.utils.network import interface_exists
from vyos.configdict import get_vlan_ids
from vyos.configdict import list_diff

Expand Down Expand Up @@ -314,7 +313,7 @@ def update(self, config):
# remove interface from bridge
tmp = dict_search('member.interface_remove', config)
for member in (tmp or []):
if member in interfaces():
if interface_exists(member):
self.del_port(member)

# enable/disable VLAN Filter
Expand Down Expand Up @@ -345,7 +344,7 @@ def update(self, config):
for interface, interface_config in tmp.items():
# if interface does yet not exist bail out early and
# add it later
if interface not in interfaces():
if not interface_exists(interface):
continue

# Bridge lower "physical" interface
Expand Down
8 changes: 4 additions & 4 deletions src/conf_mode/interfaces_bonding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2023 VyOS maintainers and contributors
# Copyright (C) 2019-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
Expand All @@ -17,7 +17,7 @@
import os

from sys import exit
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
from vyos.configdict import is_node_changed
Expand All @@ -29,7 +29,6 @@
from vyos.configverify import verify_dhcpv6
from vyos.configverify import verify_mirror_redirect
from vyos.configverify import verify_mtu_ipv6
from vyos.configverify import verify_source_interface
from vyos.configverify import verify_vlan_config
from vyos.configverify import verify_vrf
from vyos.ifconfig import BondIf
Expand All @@ -38,6 +37,7 @@
from vyos.template import render_to_string
from vyos.utils.dict import dict_search
from vyos.utils.dict import dict_to_paths_values
from vyos.utils.network import interface_exists
from vyos.configdict import has_address_configured
from vyos.configdict import has_vrf_configured
from vyos.configdep import set_dependents, call_dependents
Expand Down Expand Up @@ -209,7 +209,7 @@ def verify(bond):
if interface == 'lo':
raise ConfigError('Loopback interface "lo" can not be added to a bond')

if interface not in interfaces():
if not interface_exists(interface):
raise ConfigError(error_msg + 'it does not exist!')

if 'is_bridge_member' in interface_config:
Expand Down
8 changes: 4 additions & 4 deletions src/conf_mode/interfaces_geneve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2022 VyOS maintainers and contributors
# Copyright (C) 2019-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
Expand All @@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from sys import exit
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
Expand All @@ -26,6 +25,7 @@
from vyos.configverify import verify_mirror_redirect
from vyos.configverify import verify_bond_bridge_member
from vyos.ifconfig import GeneveIf
from vyos.utils.network import interface_exists
from vyos import ConfigError

from vyos import airbag
Expand Down Expand Up @@ -77,8 +77,8 @@ def generate(geneve):
def apply(geneve):
# Check if GENEVE interface already exists
if 'rebuild_required' in geneve or 'delete' in geneve:
if geneve['ifname'] in interfaces():
g = GeneveIf(geneve['ifname'])
if interface_exists(geneve['ifname']):
g = GeneveIf(**geneve)
# GENEVE is super picky and the tunnel always needs to be recreated,
# thus we can simply always delete it first.
g.remove()
Expand Down
8 changes: 3 additions & 5 deletions src/conf_mode/interfaces_l2tpv3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2020 VyOS maintainers and contributors
# Copyright (C) 2019-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
Expand All @@ -14,10 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from sys import exit
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
Expand All @@ -30,6 +27,7 @@
from vyos.ifconfig import L2TPv3If
from vyos.utils.kernel import check_kmod
from vyos.utils.network import is_addr_assigned
from vyos.utils.network import interface_exists
from vyos import ConfigError
from vyos import airbag
airbag.enable()
Expand Down Expand Up @@ -87,7 +85,7 @@ def generate(l2tpv3):

def apply(l2tpv3):
# Check if L2TPv3 interface already exists
if l2tpv3['ifname'] in interfaces():
if interface_exists(l2tpv3['ifname']):
# L2TPv3 is picky when changing tunnels/sessions, thus we can simply
# always delete it first.
l = L2TPv3If(**l2tpv3)
Expand Down
8 changes: 4 additions & 4 deletions src/conf_mode/interfaces_macsec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2020-2023 VyOS maintainers and contributors
# Copyright (C) 2020-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
Expand All @@ -16,7 +16,6 @@

import os

from netifaces import interfaces
from sys import exit

from vyos.config import Config
Expand All @@ -35,6 +34,7 @@
from vyos.template import render
from vyos.utils.process import call
from vyos.utils.dict import dict_search
from vyos.utils.network import interface_exists
from vyos.utils.process import is_systemd_service_running
from vyos import ConfigError
from vyos import airbag
Expand Down Expand Up @@ -172,8 +172,8 @@ def apply(macsec):
if 'deleted' in macsec or 'shutdown_required' in macsec:
call(f'systemctl stop {systemd_service}')

if macsec['ifname'] in interfaces():
tmp = MACsecIf(macsec['ifname'])
if interface_exists(macsec['ifname']):
tmp = MACsecIf(**macsec)
tmp.remove()

if 'deleted' in macsec:
Expand Down
4 changes: 2 additions & 2 deletions src/conf_mode/interfaces_openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from ipaddress import IPv6Address
from ipaddress import IPv6Network
from ipaddress import summarize_address_range
from netifaces import interfaces
from secrets import SystemRandom
from shutil import rmtree

Expand Down Expand Up @@ -63,6 +62,7 @@
from vyos.utils.permission import chown
from vyos.utils.process import cmd
from vyos.utils.network import is_addr_assigned
from vyos.utils.network import interface_exists

from vyos import ConfigError
from vyos import airbag
Expand Down Expand Up @@ -683,7 +683,7 @@ def apply(openvpn):
if os.path.isfile(cleanup_file):
os.unlink(cleanup_file)

if interface in interfaces():
if interface_exists(interface):
VTunIf(interface).remove()

# dynamically load/unload DCO Kernel extension if requested
Expand Down
4 changes: 1 addition & 3 deletions src/conf_mode/interfaces_pppoe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2021 VyOS maintainers and contributors
# Copyright (C) 2019-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
Expand All @@ -18,15 +18,13 @@

from sys import exit
from copy import deepcopy
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
from vyos.configdict import is_node_changed
from vyos.configdict import get_pppoe_interfaces
from vyos.configverify import verify_authentication
from vyos.configverify import verify_source_interface
from vyos.configverify import verify_interface_exists
from vyos.configverify import verify_vrf
from vyos.configverify import verify_mtu_ipv6
from vyos.configverify import verify_mirror_redirect
Expand Down
9 changes: 4 additions & 5 deletions src/conf_mode/interfaces_pseudo-ethernet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2022 VyOS maintainers and contributors
# Copyright (C) 2019-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
Expand All @@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from sys import exit
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
Expand All @@ -29,8 +28,8 @@
from vyos.configverify import verify_vlan_config
from vyos.configverify import verify_mtu_parent
from vyos.configverify import verify_mirror_redirect
from vyos.configverify import verify_bond_bridge_member
from vyos.ifconfig import MACVLANIf
from vyos.utils.network import interface_exists
from vyos import ConfigError

from vyos import airbag
Expand Down Expand Up @@ -84,8 +83,8 @@ def generate(peth):
def apply(peth):
# Check if the MACVLAN interface already exists
if 'rebuild_required' in peth or 'deleted' in peth:
if peth['ifname'] in interfaces():
p = MACVLANIf(peth['ifname'])
if interface_exists(peth['ifname']):
p = MACVLANIf(**peth)
# MACVLAN is always needs to be recreated,
# thus we can simply always delete it first.
p.remove()
Expand Down
11 changes: 4 additions & 7 deletions src/conf_mode/interfaces_tunnel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2018-2022 yOS maintainers and contributors
# Copyright (C) 2018-2024 yOS 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
Expand All @@ -14,10 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from sys import exit
from netifaces import interfaces

from vyos.config import Config
from vyos.configdict import get_interface_dict
Expand All @@ -31,10 +28,10 @@
from vyos.configverify import verify_tunnel
from vyos.configverify import verify_bond_bridge_member
from vyos.ifconfig import Interface
from vyos.ifconfig import Section
from vyos.ifconfig import TunnelIf
from vyos.utils.network import get_interface_config
from vyos.utils.dict import dict_search
from vyos.utils.network import get_interface_config
from vyos.utils.network import interface_exists
from vyos import ConfigError
from vyos import airbag
airbag.enable()
Expand Down Expand Up @@ -202,7 +199,7 @@ def apply(tunnel):
if ('deleted' in tunnel or 'encapsulation_changed' in tunnel or encap in
['gretap', 'ip6gretap', 'erspan', 'ip6erspan'] or remote in ['any'] or
'key_changed' in tunnel):
if interface in interfaces():
if interface_exists(interface):
tmp = Interface(interface)
tmp.remove()
if 'deleted' in tunnel:
Expand Down
9 changes: 4 additions & 5 deletions src/conf_mode/interfaces_virtual-ethernet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2022 VyOS maintainers and contributors
# Copyright (C) 2022-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
Expand All @@ -16,7 +16,6 @@

from sys import exit

from netifaces import interfaces
from vyos import ConfigError
from vyos import airbag
from vyos.config import Config
Expand All @@ -25,7 +24,7 @@
from vyos.configverify import verify_bridge_delete
from vyos.configverify import verify_vrf
from vyos.ifconfig import VethIf

from vyos.utils.network import interface_exists
airbag.enable()

def get_config(config=None):
Expand Down Expand Up @@ -92,8 +91,8 @@ def generate(peth):
def apply(veth):
# Check if the Veth interface already exists
if 'rebuild_required' in veth or 'deleted' in veth:
if veth['ifname'] in interfaces():
p = VethIf(veth['ifname'])
if interface_exists(veth['ifname']):
p = VethIf(**veth)
p.remove()

if 'deleted' not in veth:
Expand Down
3 changes: 1 addition & 2 deletions src/conf_mode/interfaces_vti.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2021 VyOS maintainers and contributors
# Copyright (C) 2021-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
Expand All @@ -14,7 +14,6 @@
# 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 netifaces import interfaces
from sys import exit

from vyos.config import Config
Expand Down
Loading

0 comments on commit 4c7c168

Please sign in to comment.