def __init__(self, ifmanager, *args, **kwargs): if "local" not in kwargs and "remote" not in kwargs: raise DeviceConfigError("One of local/remote MUST be defined.") if "key" in kwargs: try: int(kwargs["key"]) except ValueError: raise DeviceConfigError("key value must be an integer.") kwargs["ikey"] = kwargs["key"] kwargs["okey"] = kwargs["key"] del kwargs["key"] super(_BaseVtiDevice, self).__init__(ifmanager, *args, **kwargs)
def _write_pause_frames(self, rx_val, tx_val): ethtool_cmd = "ethtool -A {}".format(self.name) ethtool_opts = "" for feature, value in [('rx', rx_val), ('tx', tx_val)]: if value is None: continue ethtool_opts += " {} {}".format(feature, 'on' if value else 'off') if len(ethtool_opts) == 0: return try: exec_cmd(ethtool_cmd + ethtool_opts) except ExecCmdFail as e: if e.get_retval() == 79: raise DeviceConfigError( "Could not modify pause settings for %s." % self.name) timeout = 5 while timeout > 0: if self._pause_frames_match(rx_val, tx_val): break time.sleep(1) timeout -= 1 if timeout == 0: raise DeviceError("Pause frames not set!")
def create_device(self, clsname, args=[], kwargs={}): devcls = self._device_classes[clsname] try: device = devcls(self, *args, **kwargs) except KeyError as e: raise DeviceConfigError("%s is a mandatory argument" % e) device._create() device._bulk_enabled = False self.request_netlink_dump() self.pull_netlink_messages_into_queue() device_found = False while len(self._msg_queue): msg = self._msg_queue.popleft() if msg.get_attr("IFLA_IFNAME") == device.name: device_found = True device._init_netlink(msg) self._devices[msg['index']] = device else: self._handle_netlink_msg(msg) if device_found: return device else: raise DeviceError("Device creation failed")
def xmit_hash_policy(self, val): m = ["layer2", "layer2+3", "layer3+4", "encap2+3", "encap3+4"] if val in m: self._set_linkinfo_data_attr("IFLA_BOND_XMIT_HASH_POLICY", val) else: raise DeviceConfigError("Invalid value, must be in {}}.".format(m)) self._nl_link_sync("set")
def _create(self): self._update_attr(self._link_type, "IFLA_LINKINFO", "IFLA_INFO_KIND") try: self._nl_link_sync("add", bulk=True) except Exception as e: log_exc_traceback() raise DeviceConfigError("Creating link {} failed: {}".format( self.name, str(e)))
def slave_add(self, dev, port_config={}): if not isinstance(port_config, dict): raise DeviceConfigError(f"team link {dev.name} port config must be dict") port_json = json.dumps(port_config) opts = "-D" if self.dbus else "" exec_cmd(f"teamdctl {opts} {self.name} port config " f"update {dev.name} '{port_json}'") dev.master = self
def ad_select(self, val): m = dict(stable=0, bandwidth=1, count=2) if val in m: self._set_linkinfo_data_attr("IFLA_BOND_AD_SELECT", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_AD_SELECT", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def lacp_rate(self, val): m = dict(slow=0, fast=1) if val in m: self._set_linkinfo_data_attr("IFLA_BOND_LACP_RATE", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_LACP_RATE", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def primary_reselect(self, val): m = {"always": 0, "better": 1, "failure": 2} if val in m: self._set_linkinfo_data_attr("IFLA_BOND_PRIMARY_RESELECT", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_PRIMARY_RESELECT", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def remap_device(self, ifindex, clsname, args=[], kwargs={}): devcls = self._device_classes[clsname] old_device = self.get_device(ifindex) kwargs["name"] = old_device.name try: remapped_device = devcls(self, *args, **kwargs) except KeyError as e: raise DeviceConfigError("%s is a mandatory argument" % e) remapped_device._bulk_enabled = False remapped_device.ifindex = ifindex self.replace_dev(ifindex, remapped_device) self.rescan_devices()
def arp_all_targets(self, val): m = dict(any=0, all=1) if val in m: self._set_linkinfo_data_attr("IFLA_BOND_ARP_ALL_TARGETS", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_ARP_ALL_TARGETS", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def fail_over_mac(self, val): m = dict(none=0, active=1, follow=2) if val in m: self._set_linkinfo_data_attr("IFLA_BOND_FAIL_OVER_MAC", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_FAIL_OVER_MAC", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def __init__(self, ifmanager, *args, **kwargs): self._name = None for i in self._mandatory_opts: if i not in kwargs: raise DeviceConfigError( "Option {} is mandatory for type {}".format( i, self.__class__.__name__)) self._tunnel_id = tunnel_id = kwargs["tunnel_id"] self._session_id = kwargs["session_id"] self._peer_session_id = kwargs["peer_session_id"] self._ifmanager = ifmanager super(L2TPSessionDevice, self).__init__(ifmanager)
def __init__(self, ifmanager, **kwargs): super(SoftDevice, self).__init__(ifmanager) self._bulk_enabled = True if "name" not in kwargs: kwargs["name"] = ifmanager.assign_name(self._name_template) self._orig_name = kwargs["name"] for i in self._mandatory_opts: if i not in kwargs: raise DeviceConfigError( "Option {} is mandatory for type {}".format( i, self.__class__.__name__)) for k, v in kwargs.items(): setattr(self, k, v)
def arp_validate(self, val): m = dict(none=0, active=1, backup=2, all=3, filter=4, filter_active=5, filter_backup=6) if val in m: self._set_linkinfo_data_attr("IFLA_BOND_ARP_VALIDATE", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_ARP_VALIDATE", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def mode(self, val): m = { "balance-rr": 0, "active-backup": 1, "balance-xor": 2, "broadcast": 3, "802.3ad": 4, "balance-tlb": 5, "balance-alb": 6 } if val in m: self._set_linkinfo_data_attr("IFLA_BOND_MODE", m[val]) elif val in m.values(): self._set_linkinfo_data_attr("IFLA_BOND_MODE", val) else: raise DeviceConfigError( "Invalid value, must be in {} or {}.".format( list(m.keys()), list(m.values()))) self._nl_link_sync("set")
def _ipr_wrapper(self, obj_name, op_name, *args, **kwargs): pretty_attrs = pprint.pformat({"args": args, "kwargs": kwargs}) logging.debug( "Performing pyroute.IPRoute().{}({}, *args, **kwargs)".format( obj_name, op_name)) logging.debug("{}".format(pretty_attrs)) ret_val = None with pyroute2.IPRoute() as ipr: try: obj = getattr(ipr, obj_name) if op_name is not None: ret_val = obj(op_name, *args, **kwargs) else: ret_val = obj(*args, **kwargs) self._if_manager.rescan_devices() except Exception as e: log_exc_traceback() raise DeviceConfigError( "Object {} operation {} on link {} failed: {}".format( obj_name, op_name, self.name, str(e))) return ret_val
def realdev(self, val): if not isinstance(val, Device): raise DeviceConfigError("realdev value must be a Device object.") self._set_linkinfo_data_attr("IFLA_VTI_LINK", val.ifindex) self._nl_link_sync("set")
def id(self, val): if int(val) < 0 or int(val) > 16777215: raise DeviceConfigError("Invalid value, must be 0-16777215.") self._set_linkinfo_data_attr("IFLA_GENEVE_ID", int(val)) self._nl_link_sync("set")
def primary(self, val): if not isinstance(val, Device): raise DeviceConfigError("Invalid value, must be Device.") self._set_linkinfo_data_attr("IFLA_BOND_PRIMARY", val.ifindex) self._nl_link_sync("set")
def packets_per_slave(self, val): if int(val) < 0 or int(val) > 65535: raise DeviceConfigError("Invalid value, must be 0-65535.") self._set_linkinfo_data_attr("IFLA_BOND_PACKETS_PER_SLAVE", int(val)) self._nl_link_sync("set")
def realdev(self, val): if not isinstance(val, Device): raise DeviceConfigError("Value must be a Device object.") self._update_attr(val.ifindex, "IFLA_LINK") self._nl_link_sync("set")
def active_slave(self, val): if not isinstance(val, Device): raise DeviceConfigError("Invalid value, must be Device.") self._set_linkinfo_data_attr("IFLA_BOND_ACTIVE_SLAVE", val.ifindex) self._nl_link_sync("set")
def dbus(self, v: bool): if not isinstance(v, bool): raise DeviceConfigError("team dbus setting must be bool") self._dbus = v
def resend_igmp(self, val): if int(val) < 0 or int(val) > 255: raise DeviceConfigError("Invalid value, must be 0-255.") self._set_linkinfo_data_attr("IFLA_BOND_RESEND_IGMP", int(val)) self._nl_link_sync("set")
def config(self, v: dict): if not isinstance(v, dict): raise DeviceConfigError("team device config must be dict") self._config = v
def lp_interval(self, val): if int(val) < 1 or int(val) > 0x7fffffff: raise DeviceConfigError("Invalid value, must be 1-0x7fffffff.") self._set_linkinfo_data_attr("IFLA_BOND_LP_INTERVAL", int(val)) self._nl_link_sync("set")
def vlan_id(self, val): if int(val) < 1 or int(val) > 4095: raise DeviceConfigError("Invalid value, must be 1-4095.") self._set_linkinfo_data_attr("IFLA_VLAN_ID", int(val)) self._nl_link_sync("set")
def ad_actor_sys_prio(self, val): if int(val) < 1 or int(val) > 65535: raise DeviceConfigError("Invalid value, must be 1-65535.") self._set_linkinfo_data_attr("IFLA_BOND_AD_ACTOR_SYS_PRIO", int(val)) self._nl_link_sync("set")
def _chk_exec(self, op, kwargs): if kwargs: raise DeviceConfigError("Unexpected options with %s: %s" % (op, kwargs)) exec_cmd(self._cmd)