def from_ioctl(cls, link_settings): map_supported, map_advertising, map_lp_advertising = \ IoctlEthtool.get_link_mode_masks(link_settings) bits_supported = IoctlEthtool.get_link_mode_bits(map_supported) supported_ports = [] supported_modes = [] for bit in bits_supported: if bit.type == LMBTypePort: supported_ports.append(bit.name) elif bit.type == LMBTypeMode: supported_modes.append(bit.name) return cls( speed=link_settings.speed, duplex=link_settings.duplex, autoneg=link_settings.autoneg, supported_ports=supported_ports, supported_modes=supported_modes, )
def __init__(self): self._with_ioctl = IoctlEthtool() self._with_nl = NlEthtool() self._with_nl.module_err_level = 'debug' self._is_nl_working = self._with_nl.is_nlethtool_in_kernel()
class Ethtool(object): def __init__(self): self._with_ioctl = IoctlEthtool() self._with_nl = NlEthtool() self._with_nl.module_err_level = 'debug' self._is_nl_working = self._with_nl.is_nlethtool_in_kernel() def _nl_exec(self, f, with_netlink, *args, **kwargs): if with_netlink is None: with_netlink = self._is_nl_working if with_netlink is False: raise UseIoctl() try: return f(*args, **kwargs) except NetlinkError: raise UseIoctl() def get_link_mode(self, ifname, with_netlink=None): try: link_mode = self._nl_exec(self._with_nl.get_linkmode, with_netlink, ifname) link_mode = EthtoolLinkMode.from_netlink(link_mode) except UseIoctl: self._with_ioctl.change_ifname(ifname) link_settings = self._with_ioctl.get_link_settings() link_mode = EthtoolLinkMode.from_ioctl(link_settings) return link_mode def get_link_info(self, ifname, with_netlink=None): try: link_info = self._nl_exec(self._with_nl.get_linkinfo, with_netlink, ifname) link_info = EthtoolLinkInfo.from_netlink(link_info) except UseIoctl: self._with_ioctl.change_ifname(ifname) link_settings = self._with_ioctl.get_link_settings() link_info = EthtoolLinkInfo.from_ioctl(link_settings) return link_info def get_strings_set(self, ifname, with_netlink=None): try: stringsets = self._nl_exec(self._with_nl.get_stringset, with_netlink, ifname) return EthtoolStringBit.from_netlink(stringsets) except UseIoctl: self._with_ioctl.change_ifname(ifname) stringsets = self._with_ioctl.get_stringset() return EthtoolStringBit.from_ioctl(stringsets) def get_wol(self, ifname): nl_working = self._is_nl_working if nl_working is True: try: wol = self._with_nl.get_wol(ifname) return EthtoolWakeOnLan.from_netlink(wol) except NetlinkError: nl_working = False if nl_working is False: self._with_ioctl.change_ifname(ifname) wol_mode = self._with_ioctl.get_wol() return EthtoolWakeOnLan.from_ioctl(wol_mode) def get_features(self, ifname): self._with_ioctl.change_ifname(ifname) return EthtoolFeatures.from_ioctl(self._with_ioctl.get_features()) def set_features(self, ifname, features): self._with_ioctl.change_ifname(ifname) ioctl_features = self._with_ioctl.get_features() EthtoolFeatures.to_ioctl(ioctl_features, features) self._with_ioctl.set_features(ioctl_features) def get_coalesce(self, ifname): self._with_ioctl.change_ifname(ifname) return EthtoolCoalesce.from_ioctl(self._with_ioctl.get_coalesce()) def set_coalesce(self, ifname, coalesce): self._with_ioctl.change_ifname(ifname) ioctl_coalesce = self._with_ioctl.get_coalesce() EthtoolCoalesce.to_ioctl(ioctl_coalesce, coalesce) self._with_ioctl.set_coalesce(ioctl_coalesce) def close(self): self._with_nl.close()
def __init__(self): self._with_ioctl = IoctlEthtool() self._with_nl = NlEthtool() self._is_nl_working = self._with_nl.is_nlethtool_in_kernel()
import sys from pyroute2.ethtool.ioctl import IoctlEthtool from pyroute2.ethtool.ioctl import NotSupportedError if len(sys.argv) != 2: raise Exception("USAGE: {0} IFNAME".format(sys.argv[0])) dev = IoctlEthtool(sys.argv[1]) print("=== Device cmd: ===") try: for name, value in dev.get_cmd().items(): print("\t{}: {}".format(name, value)) except NotSupportedError: print("Not supported by driver.\n") print("") print("=== Device feature: ===") for name, value, not_fixed, _, _ in dev.get_features(): value = "on" if value else "off" if not not_fixed: # I love double negations value += " [fixed]" print("\t{}: {}".format(name, value)) print("\n=== Device coalesce: ===") for name, value in dev.get_coalesce().items(): print("\t{}: {}".format(name, value))