diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-08-30 11:09:30 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2019-08-30 11:10:11 +0200 |
commit | d1af812bd2e8f4471d4b4c1361370038b5b47520 (patch) | |
tree | 23bdd6163dad999a5bb22a321dd7b403fbb50467 /python/vyos/interfaceconfig.py | |
parent | e7fee6a6f163ddd9a96142ef0298fdb2804b7928 (diff) | |
download | vyos-1x-d1af812bd2e8f4471d4b4c1361370038b5b47520.tar.gz vyos-1x-d1af812bd2e8f4471d4b4c1361370038b5b47520.zip |
Python/ifconfig: re-work mtu getter/setter
Instead of calling iprotue2 via a subprocess (which is only complicated and
expensive), we rather directly interact with sysfs).
Diffstat (limited to 'python/vyos/interfaceconfig.py')
-rw-r--r-- | python/vyos/interfaceconfig.py | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/python/vyos/interfaceconfig.py b/python/vyos/interfaceconfig.py index 3ba9c225c..3f751e421 100644 --- a/python/vyos/interfaceconfig.py +++ b/python/vyos/interfaceconfig.py @@ -48,27 +48,39 @@ class Interface: @property def mtu(self): - try: - ret = subprocess.check_output( - ['ip -j link list dev ' + self._ifname], shell=True).decode() - a = json.loads(ret)[0] - return a['mtu'] - except subprocess.CalledProcessError as e: - if self._debug(): - self._debug(e) - return None + """ + Get/set interface mtu in bytes. + + Example: + + from vyos.interfaceconfig import Interface + mtu = Interface('ens192').mtu + print(mtu) + """ + + mtu = 0 + with open('/sys/class/net/{0}/mtu'.format(self._ifname), 'r') as f: + mtu = f.read().rstrip('\n') + return mtu + @mtu.setter def mtu(self, mtu=None): + """ + Get/set interface mtu in bytes. + + Example: + + from vyos.interfaceconfig import Interface + Interface('ens192').mtu = 1400 + """ + if mtu < 68 or mtu > 9000: - raise ValueError("mtu size invalid value") - self._mtu = mtu - try: - ret = subprocess.check_output( - ['ip link set mtu ' + str(mtu) + ' dev ' + self._ifname], shell=True).decode() - except subprocess.CalledProcessError as e: - if self._debug(): - self._debug(e) + raise ValueError('Invalid MTU size: "{}"'.format(mru)) + + with open('/sys/class/net/{0}/mtu'.format(self._ifname), 'w') as f: + f.write(str(mtu)) + @property def macaddr(self): |