From d1af812bd2e8f4471d4b4c1361370038b5b47520 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 30 Aug 2019 11:09:30 +0200 Subject: 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). --- python/vyos/interfaceconfig.py | 46 ++++++++++++++++++++++++++---------------- 1 file 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): -- cgit v1.2.3