From: Christos Sfakianakis <csfakian(a)redhat.com>
Add methods for handling coalesce settings. Raise errors
when these settings cannot be read or modified correctly.
Signed-off-by: Christos Sfakianakis <csfakian(a)redhat.com>
---
lnst/Devices/Device.py | 47 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/lnst/Devices/Device.py b/lnst/Devices/Device.py
index 20cddcf..6a04d1b 100644
--- a/lnst/Devices/Device.py
+++ b/lnst/Devices/Device.py
@@ -17,10 +17,12 @@ import pyroute2
import logging
import pprint
from abc import ABCMeta
+from itertools import product
from pyroute2.netlink.rtnl import ifinfmsg
from lnst.Common.Logs import log_exc_traceback
from lnst.Common.NetUtils import normalize_hwaddr
from lnst.Common.ExecCmd import exec_cmd
+from lnst.Common.LnstError import LnstError
from lnst.Common.DeviceError import DeviceError, DeviceDeleted, DeviceDisabled
from lnst.Common.DeviceError import DeviceConfigError, DeviceConfigValueError
from lnst.Common.IpAddress import ipaddress
@@ -58,6 +60,7 @@ class Device(object):
self._deleted = False
self._ip_addrs = []
+ self._coalesce_settings = ['', '']
self._nl_update = {}
self._bulk_enabled = False
@@ -603,6 +606,50 @@ class Device(object):
"""disable automatic negotiation of speed for this
device"""
exec_cmd("ethtool -s %s autoneg off" % self.name)
+ def _read_coalesce(self):
+ try:
+ res = exec_cmd("ethtool -c %s" % self.name)
+ except:
+ raise DeviceError("Could not fetch coalesce settings of "
+ "%s." % self.name)
+ res = re.findall('\wX: (o[n|f]*)', res[0])
+ if not tuple(res) in product(['off', 'on'], repeat=2):
+ raise LnstError("Invalid values for coalesce settings of "
+ "{} : {}".format(self.name, res))
+ return res
+
+ def _store_coalesce(self):
+ coal = self._read_coalesce()
+ self._coalesce_settings = coal
+
+ def _write_coalesce(self, rx_val, tx_val):
+ if self._coalesce_settings == [rx_val, tx_val]:
+ return
+ try:
+ exec_cmd("ethtool -C %s adaptive-rx %s adaptive-tx %s" %
+ (self.name, rx_val, tx_val))
+ except:
+ raise DeviceConfigError("Not allowed to modify coalesce "
+ "settings for %s." % self.name)
+ if self._read_coalesce != [rx_val, tx_val]:
+ raise LnstError("Could not apply coalesce settings for "
+ "%s effectively." % self.name)
+
+ def _disable_coalesce(self):
+ self._store_coalesce()
+ self._write_coalesce('off', 'off')
+
+ def _restore_coalesce(self):
+ rx_val = self._coalesce_settings[0]
+ tx_val = self._coalesce_settings[1]
+ self._write_coalesce(rx_val, tx_val)
+
+ def disable_coalesce(self):
+ self._disable_coalesce()
+
+ def restore_coalesce(self):
+ self._restore_coalesce()
+
#TODO implement proper Route objects
#consider the same as with tc?
# def route_add(self, dest):
--
2.17.1