summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configdict.py237
-rw-r--r--python/vyos/configverify.py71
-rw-r--r--python/vyos/ifconfig/bridge.py6
-rw-r--r--python/vyos/ifconfig/geneve.py13
-rw-r--r--python/vyos/ifconfig/interface.py75
-rw-r--r--python/vyos/ifconfig/l2tpv3.py34
-rw-r--r--python/vyos/ifconfig/macsec.py16
-rw-r--r--python/vyos/ifconfig/macvlan.py15
-rw-r--r--python/vyos/ifconfig/vxlan.py14
-rw-r--r--python/vyos/ifconfig/wireless.py43
-rw-r--r--python/vyos/validate.py41
11 files changed, 302 insertions, 263 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py
index bfc70b772..58ecd3f17 100644
--- a/python/vyos/configdict.py
+++ b/python/vyos/configdict.py
@@ -18,9 +18,12 @@ A library for retrieving value dicts from VyOS configs in a declarative fashion.
"""
import os
-from enum import Enum
from copy import deepcopy
+from vyos.util import vyos_dict_search
+from vyos.xml import defaults
+from vyos.xml import is_tag
+from vyos.xml import is_leaf
from vyos import ConfigError
def retrieve_config(path_hash, base_path, config):
@@ -104,47 +107,6 @@ def list_diff(first, second):
second = set(second)
return [item for item in first if item not in second]
-def T2665_default_dict_cleanup(dict):
- """ Cleanup default keys for tag nodes https://phabricator.vyos.net/T2665. """
- # Cleanup
- for vif in ['vif', 'vif_s']:
- if vif in dict:
- for key in ['ip', 'mtu', 'dhcpv6_options']:
- if key in dict[vif]:
- del dict[vif][key]
-
- # cleanup VIF-S defaults
- if 'vif_c' in dict[vif]:
- for key in ['ip', 'mtu', 'dhcpv6_options']:
- if key in dict[vif]['vif_c']:
- del dict[vif]['vif_c'][key]
- # If there is no vif-c defined and we just cleaned the default
- # keys - we can clean the entire vif-c dict as it's useless
- if not dict[vif]['vif_c']:
- del dict[vif]['vif_c']
-
- # If there is no real vif/vif-s defined and we just cleaned the default
- # keys - we can clean the entire vif dict as it's useless
- if not dict[vif]:
- del dict[vif]
-
- if 'dhcpv6_options' in dict and 'pd' in dict['dhcpv6_options']:
- if 'length' in dict['dhcpv6_options']['pd']:
- del dict['dhcpv6_options']['pd']['length']
-
- # delete empty dicts
- if 'dhcpv6_options' in dict:
- if 'pd' in dict['dhcpv6_options']:
- # test if 'pd' is an empty node so we can remove it
- if not dict['dhcpv6_options']['pd']:
- del dict['dhcpv6_options']['pd']
-
- # test if 'dhcpv6_options' is an empty node so we can remove it
- if not dict['dhcpv6_options']:
- del dict['dhcpv6_options']
-
- return dict
-
def leaf_node_changed(conf, path):
"""
Check if a leaf node was altered. If it has been altered - values has been
@@ -207,16 +169,100 @@ def get_removed_vlans(conf, dict):
return dict
+def T2665_set_dhcpv6pd_defaults(config_dict):
+ """ Properly configure DHCPv6 default options in the dictionary. If there is
+ no DHCPv6 configured at all, it is safe to remove the entire configuration.
+ """
+ # As this is the same for every interface type it is safe to assume this
+ # for ethernet
+ pd_defaults = defaults(['interfaces', 'ethernet', 'dhcpv6-options', 'pd'])
-def dict_add_dhcpv6pd_defaults(defaults, config_dict):
# Implant default dictionary for DHCPv6-PD instances
- if 'dhcpv6_options' in config_dict and 'pd' in config_dict['dhcpv6_options']:
- for pd, pd_config in config_dict['dhcpv6_options']['pd'].items():
- config_dict['dhcpv6_options']['pd'][pd] = dict_merge(
- defaults, pd_config)
+ if vyos_dict_search('dhcpv6_options.pd.length', config_dict):
+ del config_dict['dhcpv6_options']['pd']['length']
+
+ for pd in (vyos_dict_search('dhcpv6_options.pd', config_dict) or []):
+ config_dict['dhcpv6_options']['pd'][pd] = dict_merge(pd_defaults,
+ config_dict['dhcpv6_options']['pd'][pd])
return config_dict
+def is_member(conf, interface, intftype=None):
+ """
+ Checks if passed interface is member of other interface of specified type.
+ intftype is optional, if not passed it will search all known types
+ (currently bridge and bonding)
+
+ Returns:
+ None -> Interface is not a member
+ interface name -> Interface is a member of this interface
+ False -> interface type cannot have members
+ """
+ ret_val = None
+ intftypes = ['bonding', 'bridge']
+ if intftype not in intftypes + [None]:
+ raise ValueError((
+ f'unknown interface type "{intftype}" or it cannot '
+ f'have member interfaces'))
+
+ intftype = intftypes if intftype == None else [intftype]
+
+ # set config level to root
+ old_level = conf.get_level()
+ conf.set_level([])
+
+ for it in intftype:
+ base = ['interfaces', it]
+ for intf in conf.list_nodes(base):
+ memberintf = base + [intf, 'member', 'interface']
+ if is_tag(memberintf):
+ if interface in conf.list_nodes(memberintf):
+ ret_val = intf
+ break
+ elif is_leaf(memberintf):
+ if ( conf.exists(memberintf) and
+ interface in conf.return_values(memberintf) ):
+ ret_val = intf
+ break
+
+ old_level = conf.set_level(old_level)
+ return ret_val
+
+def is_source_interface(conf, interface, intftype=None):
+ """
+ Checks if passed interface is configured as source-interface of other
+ interfaces of specified type. intftype is optional, if not passed it will
+ search all known types (currently pppoe, macsec, pseudo-ethernet, tunnel
+ and vxlan)
+
+ Returns:
+ None -> Interface is not a member
+ interface name -> Interface is a member of this interface
+ False -> interface type cannot have members
+ """
+ ret_val = None
+ intftypes = ['macsec', 'pppoe', 'pseudo-ethernet', 'tunnel', 'vxlan']
+ if intftype not in intftypes + [None]:
+ raise ValueError(f'unknown interface type "{intftype}" or it can not '
+ 'have a source-interface')
+
+ intftype = intftypes if intftype == None else [intftype]
+
+ # set config level to root
+ old_level = conf.get_level()
+ conf.set_level([])
+
+ for it in intftype:
+ base = ['interfaces', it]
+ for intf in conf.list_nodes(base):
+ lower_intf = base + [intf, 'source-interface']
+ if conf.exists(lower_intf) and interface in conf.return_values(lower_intf):
+ ret_val = intf
+ break
+
+ old_level = conf.set_level(old_level)
+ return ret_val
+
def get_interface_dict(config, base, ifname=''):
"""
Common utility function to retrieve and mandgle the interfaces available
@@ -225,10 +271,6 @@ def get_interface_dict(config, base, ifname=''):
Will return a dictionary with the necessary interface configuration
"""
- from vyos.util import vyos_dict_search
- from vyos.validate import is_member
- from vyos.xml import defaults
-
if not ifname:
# determine tagNode instance
if 'VYOS_TAGNODE_VALUE' not in os.environ:
@@ -238,6 +280,12 @@ def get_interface_dict(config, base, ifname=''):
# retrieve interface default values
default_values = defaults(base)
+ # We take care about VLAN (vif, vif-s, vif-c) default values later on when
+ # parsing vlans in default dict and merge the "proper" values in correctly,
+ # see T2665.
+ for vif in ['vif', 'vif_s']:
+ if vif in default_values: del default_values[vif]
+
# setup config level which is extracted in get_removed_vlans()
config.set_level(base + [ifname])
dict = config.get_config_dict([], key_mangling=('-', '_'), get_first_key=True)
@@ -249,24 +297,42 @@ def get_interface_dict(config, base, ifname=''):
# Add interface instance name into dictionary
dict.update({'ifname': ifname})
+ # XXX: T2665: When there is no DHCPv6-PD configuration given, we can safely
+ # remove the default values from the dict.
+ if 'dhcpv6_options' not in dict:
+ if 'dhcpv6_options' in default_values:
+ del default_values['dhcpv6_options']
+
# We have gathered the dict representation of the CLI, but there are
# default options which we need to update into the dictionary
# retrived.
dict = dict_merge(default_values, dict)
+ # XXX: T2665: blend in proper DHCPv6-PD default values
+ dict = T2665_set_dhcpv6pd_defaults(dict)
+
# Check if we are a member of a bridge device
bridge = is_member(config, ifname, 'bridge')
- if bridge:
- dict.update({'is_bridge_member' : bridge})
+ if bridge: dict.update({'is_bridge_member' : bridge})
# Check if we are a member of a bond device
bond = is_member(config, ifname, 'bonding')
- if bond:
- dict.update({'is_bond_member' : bond})
+ if bond: dict.update({'is_bond_member' : bond})
+
+ # Some interfaces come with a source_interface which must also not be part
+ # of any other bond or bridge interface as it is exclusivly assigned as the
+ # Kernels "lower" interface to this new "virtual/upper" interface.
+ if 'source_interface' in dict:
+ # Check if source interface is member of another bridge
+ tmp = is_member(config, dict['source_interface'], 'bridge')
+ if tmp: dict.update({'source_interface_is_bridge_member' : tmp})
+
+ # Check if source interface is member of another bridge
+ tmp = is_member(config, dict['source_interface'], 'bonding')
+ if tmp: dict.update({'source_interface_is_bond_member' : tmp})
mac = leaf_node_changed(config, ['mac'])
- if mac:
- dict.update({'mac_old' : mac})
+ if mac: dict.update({'mac_old' : mac})
eui64 = leaf_node_changed(config, ['ipv6', 'address', 'eui64'])
if eui64:
@@ -276,36 +342,49 @@ def get_interface_dict(config, base, ifname=''):
else:
dict['ipv6']['address'].update({'eui64_old': eui64})
- # remove wrongly inserted values
- dict = T2665_default_dict_cleanup(dict)
-
- # Implant default dictionary for DHCPv6-PD instances
- default_pd_values = defaults(base + ['dhcpv6-options', 'pd'])
- dict = dict_add_dhcpv6pd_defaults(default_pd_values, dict)
-
# Implant default dictionary in vif/vif-s VLAN interfaces. Values are
# identical for all types of VLAN interfaces as they all include the same
# XML definitions which hold the defaults.
- default_vif_values = defaults(base + ['vif'])
for vif, vif_config in dict.get('vif', {}).items():
- dict['vif'][vif] = dict_add_dhcpv6pd_defaults(
- default_pd_values, vif_config)
- dict['vif'][vif] = T2665_default_dict_cleanup(
- dict_merge(default_vif_values, vif_config))
+ default_vif_values = defaults(base + ['vif'])
+ # XXX: T2665: When there is no DHCPv6-PD configuration given, we can safely
+ # remove the default values from the dict.
+ if not 'dhcpv6_options' in vif_config:
+ del default_vif_values['dhcpv6_options']
+
+ dict['vif'][vif] = dict_merge(default_vif_values, vif_config)
+ # XXX: T2665: blend in proper DHCPv6-PD default values
+ dict['vif'][vif] = T2665_set_dhcpv6pd_defaults(dict['vif'][vif])
for vif_s, vif_s_config in dict.get('vif_s', {}).items():
- dict['vif_s'][vif_s] = dict_add_dhcpv6pd_defaults(
- default_pd_values, vif_s_config)
- dict['vif_s'][vif_s] = T2665_default_dict_cleanup(
- dict_merge(default_vif_values, vif_s_config))
+ default_vif_s_values = defaults(base + ['vif-s'])
+ # XXX: T2665: we only wan't the vif-s defaults - do not care about vif-c
+ if 'vif_c' in default_vif_s_values: del default_vif_s_values['vif_c']
+
+ # XXX: T2665: When there is no DHCPv6-PD configuration given, we can safely
+ # remove the default values from the dict.
+ if not 'dhcpv6_options' in vif_s_config:
+ del default_vif_s_values['dhcpv6_options']
+
+ dict['vif_s'][vif_s] = dict_merge(default_vif_s_values, vif_s_config)
+ # XXX: T2665: blend in proper DHCPv6-PD default values
+ dict['vif_s'][vif_s] = T2665_set_dhcpv6pd_defaults(
+ dict['vif_s'][vif_s])
+
for vif_c, vif_c_config in vif_s_config.get('vif_c', {}).items():
- dict['vif_s'][vif_s]['vif_c'][vif_c] = dict_add_dhcpv6pd_defaults(
- default_pd_values, vif_c_config)
- dict['vif_s'][vif_s]['vif_c'][vif_c] = T2665_default_dict_cleanup(
- dict_merge(default_vif_values, vif_c_config))
+ default_vif_c_values = defaults(base + ['vif-s', 'vif-c'])
+
+ # XXX: T2665: When there is no DHCPv6-PD configuration given, we can safely
+ # remove the default values from the dict.
+ if not 'dhcpv6_options' in vif_c_config:
+ del default_vif_c_values['dhcpv6_options']
+
+ dict['vif_s'][vif_s]['vif_c'][vif_c] = dict_merge(
+ default_vif_c_values, vif_c_config)
+ # XXX: T2665: blend in proper DHCPv6-PD default values
+ dict['vif_s'][vif_s]['vif_c'][vif_c] = T2665_set_dhcpv6pd_defaults(
+ dict['vif_s'][vif_s]['vif_c'][vif_c])
# Check vif, vif-s/vif-c VLAN interfaces for removal
dict = get_removed_vlans(config, dict)
-
return dict
-
diff --git a/python/vyos/configverify.py b/python/vyos/configverify.py
index 7e1930878..944fc4294 100644
--- a/python/vyos/configverify.py
+++ b/python/vyos/configverify.py
@@ -23,6 +23,57 @@
from vyos import ConfigError
+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'])
+ min_mtu = tmp.get_min_mtu()
+ max_mtu = tmp.get_max_mtu()
+
+ 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_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.validate import is_ipv6
+ from vyos.util import vyos_dict_search
+ # 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}" ' \
+ f'thus the minimum MTU requirement is {min_mtu}!'
+
+ if not vyos_dict_search('ipv6.address.no_default_link_local', config):
+ raise ConfigError('link-local ' + error_msg)
+
+ for address in (vyos_dict_search('address', config) or []):
+ if address in ['dhcpv6'] or is_ipv6(address):
+ raise ConfigError(error_msg)
+
+ if vyos_dict_search('ipv6.address.autoconf', config):
+ raise ConfigError(error_msg)
+
+ if vyos_dict_search('ipv6.address.eui64', config):
+ raise ConfigError(error_msg)
+
+
def verify_vrf(config):
"""
Common helper function used by interface implementations to perform
@@ -82,9 +133,20 @@ def verify_source_interface(config):
if 'source_interface' not in config:
raise ConfigError('Physical source-interface required for '
'interface "{ifname}"'.format(**config))
+
if config['source_interface'] not in interfaces():
- raise ConfigError('Source interface {source_interface} does not '
- 'exist'.format(**config))
+ raise ConfigError('Specified source-interface {source_interface} does '
+ 'not exist'.format(**config))
+
+ if 'source_interface_is_bridge_member' in config:
+ raise ConfigError('Invalid source-interface {source_interface}. Interface '
+ 'is already a member of bridge '
+ '{source_interface_is_bridge_member}'.format(**config))
+
+ if 'source_interface_is_bond_member' in config:
+ raise ConfigError('Invalid source-interface {source_interface}. Interface '
+ 'is already a member of bond '
+ '{source_interface_is_bond_member}'.format(**config))
def verify_dhcpv6(config):
"""
@@ -102,6 +164,11 @@ def verify_dhcpv6(config):
# assigned IPv6 subnet from a delegated prefix
for pd in vyos_dict_search('dhcpv6_options.pd', config):
sla_ids = []
+
+ if not vyos_dict_search(f'dhcpv6_options.pd.{pd}.interface', config):
+ raise ConfigError('DHCPv6-PD requires an interface where to assign '
+ 'the delegated prefix!')
+
for interface in vyos_dict_search(f'dhcpv6_options.pd.{pd}.interface', config):
sla_id = vyos_dict_search(
f'dhcpv6_options.pd.{pd}.interface.{interface}.sla_id', config)
diff --git a/python/vyos/ifconfig/bridge.py b/python/vyos/ifconfig/bridge.py
index 4c76fe996..c133a56fc 100644
--- a/python/vyos/ifconfig/bridge.py
+++ b/python/vyos/ifconfig/bridge.py
@@ -13,6 +13,8 @@
# 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.ifconfig.stp import STP
from vyos.validate import assert_boolean
@@ -228,8 +230,8 @@ class BridgeIf(Interface):
# remove interface from bridge
tmp = vyos_dict_search('member.interface_remove', config)
- if tmp:
- for member in tmp:
+ for member in (tmp or []):
+ if member in interfaces():
self.del_port(member)
STPBridgeIf = STP.enable(BridgeIf)
diff --git a/python/vyos/ifconfig/geneve.py b/python/vyos/ifconfig/geneve.py
index 0a13043cc..5c4597be8 100644
--- a/python/vyos/ifconfig/geneve.py
+++ b/python/vyos/ifconfig/geneve.py
@@ -13,7 +13,6 @@
# 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 copy import deepcopy
from vyos.ifconfig.interface import Interface
@Interface.register
@@ -51,18 +50,6 @@ class GeneveIf(Interface):
# interface is always A/D down. It needs to be enabled explicitly
self.set_admin_state('down')
- @classmethod
- def get_config(cls):
- """
- GENEVE interfaces require a configuration when they are added using
- iproute2. This static method will provide the configuration dictionary
- used by this class.
-
- Example:
- >> dict = GeneveIf().get_config()
- """
- return deepcopy(cls.default)
-
def update(self, config):
""" General helper function which works on a dictionary retrived by
get_config_dict(). It's main intention is to consolidate the scattered
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index be97b411b..d200fc7a8 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -50,14 +50,6 @@ from vyos.ifconfig.vrrp import VRRP
from vyos.ifconfig.operational import Operational
from vyos.ifconfig import Section
-def get_ethertype(ethertype_val):
- if ethertype_val == '0x88A8':
- return '802.1ad'
- elif ethertype_val == '0x8100':
- return '802.1q'
- else:
- raise ConfigError('invalid ethertype "{}"'.format(ethertype_val))
-
class Interface(Control):
# This is the class which will be used to create
# self.operational, it allows subclasses, such as
@@ -86,6 +78,14 @@ class Interface(Control):
'shellcmd': 'ip -json link show dev {ifname}',
'format': lambda j: 'up' if 'UP' in jmespath.search('[*].flags | [0]', json.loads(j)) else 'down',
},
+ 'min_mtu': {
+ 'shellcmd': 'ip -json -detail link list dev {ifname}',
+ 'format': lambda j: jmespath.search('[*].min_mtu | [0]', json.loads(j)),
+ },
+ 'max_mtu': {
+ 'shellcmd': 'ip -json -detail link list dev {ifname}',
+ 'format': lambda j: jmespath.search('[*].max_mtu | [0]', json.loads(j)),
+ },
}
_command_set = {
@@ -182,6 +182,15 @@ class Interface(Control):
def exists(cls, ifname):
return os.path.exists(f'/sys/class/net/{ifname}')
+ @classmethod
+ def get_config(cls):
+ """
+ Some but not all interfaces require a configuration when they are added
+ using iproute2. This method will provide the configuration dictionary
+ used by this class.
+ """
+ return deepcopy(cls.default)
+
def __init__(self, ifname, **kargs):
"""
This is the base interface class which supports basic IP/MAC address
@@ -281,6 +290,28 @@ class Interface(Control):
cmd = 'ip link del dev {ifname}'.format(**self.config)
return self._cmd(cmd)
+ def get_min_mtu(self):
+ """
+ Get hardware minimum supported MTU
+
+ Example:
+ >>> from vyos.ifconfig import Interface
+ >>> Interface('eth0').get_min_mtu()
+ '60'
+ """
+ return int(self.get_interface('min_mtu'))
+
+ def get_max_mtu(self):
+ """
+ Get hardware maximum supported MTU
+
+ Example:
+ >>> from vyos.ifconfig import Interface
+ >>> Interface('eth0').get_max_mtu()
+ '9000'
+ """
+ return int(self.get_interface('max_mtu'))
+
def get_mtu(self):
"""
Get/set interface mtu in bytes.
@@ -290,7 +321,7 @@ class Interface(Control):
>>> Interface('eth0').get_mtu()
'1500'
"""
- return self.get_interface('mtu')
+ return int(self.get_interface('mtu'))
def set_mtu(self, mtu):
"""
@@ -1013,7 +1044,7 @@ class Interface(Control):
# create/update 802.1ad (Q-in-Q VLANs)
for vif_s_id, vif_s_config in config.get('vif_s', {}).items():
tmp = deepcopy(VLANIf.get_config())
- tmp['ethertype'] = get_ethertype(vif_s_config.get('ethertype', '0x88A8'))
+ tmp['protocol'] = vif_s_config['protocol']
tmp['source_interface'] = ifname
tmp['vlan_id'] = vif_s_id
@@ -1061,13 +1092,13 @@ class VLANIf(Interface):
'type': 'vlan',
'source_interface': '',
'vlan_id': '',
- 'ethertype': '',
+ 'protocol': '',
'ingress_qos': '',
'egress_qos': '',
}
options = Interface.options + \
- ['source_interface', 'vlan_id', 'ethertype', 'ingress_qos', 'egress_qos']
+ ['source_interface', 'vlan_id', 'protocol', 'ingress_qos', 'egress_qos']
def remove(self):
"""
@@ -1092,12 +1123,12 @@ class VLANIf(Interface):
def _create(self):
# bail out early if interface already exists
- if os.path.exists(f'/sys/class/net/{self.ifname}'):
+ if self.exists(f'{self.ifname}'):
return
cmd = 'ip link add link {source_interface} name {ifname} type vlan id {vlan_id}'
- if self.config['ethertype']:
- cmd += ' proto {ethertype}'
+ if self.config['protocol']:
+ cmd += ' protocol {protocol}'
if self.config['ingress_qos']:
cmd += ' ingress-qos-map {ingress_qos}'
if self.config['egress_qos']:
@@ -1108,20 +1139,6 @@ class VLANIf(Interface):
# interface is always A/D down. It needs to be enabled explicitly
self.set_admin_state('down')
- @staticmethod
- def get_config():
- """
- MACsec interfaces require a configuration when they are added using
- iproute2. This static method will provide the configuration dictionary
- used by this class.
-
- Example:
- >> dict = VLANIf().get_config()
- """
- config = deepcopy(__class__.default)
- del config['type']
- return config
-
def set_admin_state(self, state):
"""
Set interface administrative state to be 'up' or 'down'
diff --git a/python/vyos/ifconfig/l2tpv3.py b/python/vyos/ifconfig/l2tpv3.py
index 33740921e..5fd90f9cf 100644
--- a/python/vyos/ifconfig/l2tpv3.py
+++ b/python/vyos/ifconfig/l2tpv3.py
@@ -13,7 +13,6 @@
# 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/>.
-import os
from vyos.ifconfig.interface import Interface
@Interface.register
@@ -28,6 +27,15 @@ class L2TPv3If(Interface):
default = {
'type': 'l2tp',
+ 'peer_tunnel_id': '',
+ 'local_port': 0,
+ 'remote_port': 0,
+ 'encapsulation': 'udp',
+ 'local_address': '',
+ 'remote_address': '',
+ 'session_id': '',
+ 'tunnel_id': '',
+ 'peer_session_id': ''
}
definition = {
**Interface.definition,
@@ -73,7 +81,7 @@ class L2TPv3If(Interface):
>>> i.remove()
"""
- if os.path.exists('/sys/class/net/{}'.format(self.config['ifname'])):
+ if self.exists(self.config['ifname']):
# interface is always A/D down. It needs to be enabled explicitly
self.set_admin_state('down')
@@ -86,25 +94,3 @@ class L2TPv3If(Interface):
cmd = 'ip l2tp del tunnel tunnel_id {tunnel_id}'
self._cmd(cmd.format(**self.config))
- @staticmethod
- def get_config():
- """
- L2TPv3 interfaces require a configuration when they are added using
- iproute2. This static method will provide the configuration dictionary
- used by this class.
-
- Example:
- >> dict = L2TPv3If().get_config()
- """
- config = {
- 'peer_tunnel_id': '',
- 'local_port': 0,
- 'remote_port': 0,
- 'encapsulation': 'udp',
- 'local_address': '',
- 'remote_address': '',
- 'session_id': '',
- 'tunnel_id': '',
- 'peer_session_id': ''
- }
- return config
diff --git a/python/vyos/ifconfig/macsec.py b/python/vyos/ifconfig/macsec.py
index 6f570d162..456686ea6 100644
--- a/python/vyos/ifconfig/macsec.py
+++ b/python/vyos/ifconfig/macsec.py
@@ -56,22 +56,6 @@ class MACsecIf(Interface):
# interface is always A/D down. It needs to be enabled explicitly
self.set_admin_state('down')
- @staticmethod
- def get_config():
- """
- MACsec interfaces require a configuration when they are added using
- iproute2. This static method will provide the configuration dictionary
- used by this class.
-
- Example:
- >> dict = MACsecIf().get_config()
- """
- config = {
- 'security_cipher': '',
- 'source_interface': '',
- }
- return config
-
def update(self, config):
""" General helper function which works on a dictionary retrived by
get_config_dict(). It's main intention is to consolidate the scattered
diff --git a/python/vyos/ifconfig/macvlan.py b/python/vyos/ifconfig/macvlan.py
index 9c1d09c1c..2447fec77 100644
--- a/python/vyos/ifconfig/macvlan.py
+++ b/python/vyos/ifconfig/macvlan.py
@@ -1,4 +1,4 @@
-# Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 2019-2020 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
@@ -13,7 +13,6 @@
# 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 copy import deepcopy
from vyos.ifconfig.interface import Interface
@Interface.register
@@ -53,18 +52,6 @@ class MACVLANIf(Interface):
cmd = f'ip link set dev {ifname} type macvlan mode {mode}'
return self._cmd(cmd)
- @classmethod
- def get_config(cls):
- """
- MACVLAN interfaces require a configuration when they are added using
- iproute2. This method will provide the configuration dictionary used
- by this class.
-
- Example:
- >> dict = MACVLANIf().get_config()
- """
- return deepcopy(cls.default)
-
def update(self, config):
""" General helper function which works on a dictionary retrived by
get_config_dict(). It's main intention is to consolidate the scattered
diff --git a/python/vyos/ifconfig/vxlan.py b/python/vyos/ifconfig/vxlan.py
index dba62b61a..ad1f605ed 100644
--- a/python/vyos/ifconfig/vxlan.py
+++ b/python/vyos/ifconfig/vxlan.py
@@ -13,8 +13,6 @@
# 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 copy import deepcopy
-
from vyos import ConfigError
from vyos.ifconfig.interface import Interface
@@ -97,18 +95,6 @@ class VXLANIf(Interface):
self._cmd(cmd)
- @classmethod
- def get_config(cls):
- """
- VXLAN interfaces require a configuration when they are added using
- iproute2. This static method will provide the configuration dictionary
- used by this class.
-
- Example:
- >> dict = VXLANIf().get_config()
- """
- return deepcopy(cls.default)
-
def update(self, config):
""" General helper function which works on a dictionary retrived by
get_config_dict(). It's main intention is to consolidate the scattered
diff --git a/python/vyos/ifconfig/wireless.py b/python/vyos/ifconfig/wireless.py
index 346577119..deca68bf0 100644
--- a/python/vyos/ifconfig/wireless.py
+++ b/python/vyos/ifconfig/wireless.py
@@ -23,8 +23,10 @@ class WiFiIf(Interface):
default = {
'type': 'wifi',
- 'phy': 'phy0'
+ 'phy': '',
+ 'wds': 'off',
}
+
definition = {
**Interface.definition,
**{
@@ -33,12 +35,19 @@ class WiFiIf(Interface):
'bridgeable': True,
}
}
+
options = Interface.options + \
['phy', 'op_mode']
+ _command_set = {**Interface._command_set, **{
+ '4addr': {
+ 'shellcmd': 'iw dev {ifname} set 4addr {value}',
+ },
+ }}
+
def _create(self):
# all interfaces will be added in monitor mode
- cmd = 'iw phy {phy} interface add {ifname} type monitor' \
+ cmd = 'iw phy {phy} interface add {ifname} type monitor 4addr {wds}' \
.format(**self.config)
self._cmd(cmd)
@@ -50,21 +59,8 @@ class WiFiIf(Interface):
.format(**self.config)
self._cmd(cmd)
- @staticmethod
- def get_config():
- """
- WiFi interfaces require a configuration when they are added using
- iw (type/phy). This static method will provide the configuration
- ictionary used by this class.
-
- Example:
- >> conf = WiFiIf().get_config()
- """
- config = {
- 'phy': 'phy0'
- }
- return config
-
+ def set_4aadr_mode(self, state):
+ return self.set_interface('4addr', state)
def update(self, config):
""" General helper function which works on a dictionary retrived by
@@ -72,22 +68,11 @@ class WiFiIf(Interface):
interface setup code and provide a single point of entry when workin
on any interface. """
- # We can not call add_to_bridge() until wpa_supplicant is running, thus
- # we will remove the key from the config dict and react to this specal
- # case in thie derived class.
- # re-add ourselves to any bridge we might have fallen out of
- bridge_member = ''
- if 'is_bridge_member' in config:
- bridge_member = config['is_bridge_member']
- del config['is_bridge_member']
+ self.set_4aadr_mode('on' if 'wds' in config else 'off')
# call base class first
super().update(config)
- # re-add ourselves to any bridge we might have fallen out of
- if bridge_member:
- self.add_to_bridge(bridge_member)
-
# Enable/Disable of an interface must always be done at the end of the
# derived class to make use of the ref-counting set_admin_state()
# function. We will only enable the interface if 'up' was called as
diff --git a/python/vyos/validate.py b/python/vyos/validate.py
index ceeb6888a..691cf3c8e 100644
--- a/python/vyos/validate.py
+++ b/python/vyos/validate.py
@@ -19,7 +19,6 @@ import netifaces
import ipaddress
from vyos.util import cmd
-from vyos import xml
# Important note when you are adding new validation functions:
#
@@ -267,46 +266,6 @@ def assert_mac(m):
raise ValueError(f'{m} is a VRRP MAC address')
-def is_member(conf, interface, intftype=None):
- """
- Checks if passed interface is member of other interface of specified type.
- intftype is optional, if not passed it will search all known types
- (currently bridge and bonding)
-
- Returns:
- None -> Interface is not a member
- interface name -> Interface is a member of this interface
- False -> interface type cannot have members
- """
- ret_val = None
- if intftype not in ['bonding', 'bridge', None]:
- raise ValueError((
- f'unknown interface type "{intftype}" or it cannot '
- f'have member interfaces'))
-
- intftype = ['bonding', 'bridge'] if intftype == None else [intftype]
-
- # set config level to root
- old_level = conf.get_level()
- conf.set_level([])
-
- for it in intftype:
- base = ['interfaces', it]
- for intf in conf.list_nodes(base):
- memberintf = base + [intf, 'member', 'interface']
- if xml.is_tag(memberintf):
- if interface in conf.list_nodes(memberintf):
- ret_val = intf
- break
- elif xml.is_leaf(memberintf):
- if ( conf.exists(memberintf) and
- interface in conf.return_values(memberintf) ):
- ret_val = intf
- break
-
- old_level = conf.set_level(old_level)
- return ret_val
-
def has_address_configured(conf, intf):
"""
Checks if interface has an address configured.