diff options
Diffstat (limited to 'python/vyos/ifconfig')
-rw-r--r-- | python/vyos/ifconfig/control.py | 17 | ||||
-rw-r--r-- | python/vyos/ifconfig/ethernet.py | 29 | ||||
-rw-r--r-- | python/vyos/ifconfig/interface.py | 16 | ||||
-rw-r--r-- | python/vyos/ifconfig/wireless.py | 3 |
4 files changed, 43 insertions, 22 deletions
diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py index e8f25c014..1c9f7e284 100644 --- a/python/vyos/ifconfig/control.py +++ b/python/vyos/ifconfig/control.py @@ -28,15 +28,20 @@ class Control(Register): if os.path.isfile('/tmp/vyos.ifconfig.debug'): print('DEBUG/{:<6} {}'.format(self.config['ifname'], msg)) - def _cmd(self, command): + def _popen(self, command): p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True) tmp = p.communicate()[0].strip() self._debug_msg(f"cmd '{command}'") decoded = tmp.decode() if decoded: self._debug_msg(f"returned:\n{decoded}") - if p.returncode != 0: - raise RuntimeError(f'{command}\nreturned: {decoded}') + return decoded, p.returncode + + def _cmd(self, command): + decoded, code = self._popen(command) + if code != 0: + # error code can be recovered with .errno + raise OSError(code, f'{command}\nreturned: {decoded}') return decoded def _get_command(self, config, name): @@ -50,9 +55,6 @@ class Control(Register): """ Using the defined names, set data write to sysfs. """ - if not value and not self._command_set[name].get('force', False): - return None - # the code can pass int as int value = str(value) @@ -110,9 +112,6 @@ class Control(Register): """ Using the defined names, set data write to sysfs. """ - if not value and not self._sysfs_set[name].get('force', False): - return None - # the code can pass int as int value = str(value) diff --git a/python/vyos/ifconfig/ethernet.py b/python/vyos/ifconfig/ethernet.py index b3e652409..50552dc71 100644 --- a/python/vyos/ifconfig/ethernet.py +++ b/python/vyos/ifconfig/ethernet.py @@ -101,6 +101,8 @@ class EthernetIf(Interface): >>> i = EthernetIf('eth0') >>> i.set_flow_control(True) """ + ifname = self.config['ifname'] + if enable not in ['on', 'off']: raise ValueError("Value out of range") @@ -110,8 +112,15 @@ class EthernetIf(Interface): return # Get current flow control settings: - cmd = '/sbin/ethtool --show-pause {0}'.format(self.config['ifname']) - tmp = self._cmd(cmd) + cmd = f'/sbin/ethtool --show-pause {ifname}' + output, code = self._popen(cmd) + if code == 76: + # the interface does not support it + return '' + if code: + # never fail here as it prevent vyos to boot + print(f'unexpected return code {code} from {cmd}') + return '' # The above command returns - with tabs: # @@ -119,23 +128,21 @@ class EthernetIf(Interface): # Autonegotiate: on # RX: off # TX: off - if re.search("Autonegotiate:\ton", tmp): + if re.search("Autonegotiate:\ton", output): if enable == "on": # flowcontrol is already enabled - no need to re-enable it again # this will prevent the interface from flapping as applying the # flow-control settings will take the interface down and bring # it back up every time. - return + return '' # Assemble command executed on system. Unfortunately there is no way # to change this setting via sysfs - cmd = '/sbin/ethtool --pause {0} autoneg {1} tx {1} rx {1}'.format( - self.config['ifname'], enable) - try: - # An exception will be thrown if the settings are not changed - return self._cmd(cmd) - except RuntimeError: - pass + cmd = f'/sbin/ethtool --pause {ifname} autoneg {enable} tx {enable} rx {enable}' + output, code = self._popen(cmd) + if code: + print(f'could not set flowcontrol for {ifname}') + return output def set_speed_duplex(self, speed, duplex): """ diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 9fd0dcca5..0fddc67f3 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -66,13 +66,15 @@ class Interface(DHCP): 'shellcmd': 'ip link set dev {ifname} address {value}', }, 'vrf': { - 'force': True, 'convert': lambda v: f'master {v}' if v else 'nomaster', 'shellcmd': 'ip link set dev {ifname} {value}', }, } _sysfs_get = { + 'alias': { + 'location': '/sys/class/net/{ifname}/ifalias', + }, 'mac': { 'location': '/sys/class/net/{ifname}/address', }, @@ -259,7 +261,7 @@ class Interface(DHCP): >>> Interface('eth0').get_mac() '00:50:ab:cd:ef:00' """ - self.get_interface('mac') + return self.get_interface('mac') def set_mac(self, mac): """ @@ -394,6 +396,16 @@ class Interface(DHCP): """ return self.set_interface('link_detect', link_filter) + def get_alias(self): + """ + Get interface alias name used by e.g. SNMP + + Example: + >>> Interface('eth0').get_alias() + 'interface description as set by user' + """ + return self.get_interface('alias') + def set_alias(self, ifalias=''): """ Set interface alias name used by e.g. SNMP diff --git a/python/vyos/ifconfig/wireless.py b/python/vyos/ifconfig/wireless.py index a1f50b71d..932d07d01 100644 --- a/python/vyos/ifconfig/wireless.py +++ b/python/vyos/ifconfig/wireless.py @@ -46,6 +46,9 @@ class WiFiIf(Interface): .format(**self.config) self._cmd(cmd) + # wireless interface is administratively down by default + self.set_state('down') + def _delete(self): cmd = 'iw dev {ifname} del' \ .format(**self.config) |