Beispiel #1
0
    def _update_netlink(self, nl_msg):
        if getattr(self, "_deleted"):
            raise DeviceDeleted("Device was deleted.")

        if self.ifindex != nl_msg['index']:
            msg = "ifindex of netlink message (%s) doesn't match "\
                  "the device's (%s)." % (nl_msg['index'], self.ifindex)
            raise DeviceError(msg)

        if nl_msg['header']['type'] == RTM_NEWLINK:
            self._nl_msg = nl_msg
        elif nl_msg['header']['type'] == RTM_NEWADDR:
            addr = ipaddress(nl_msg.get_attr('IFA_ADDRESS'),
                             flags=nl_msg.get_attr("IFA_FLAGS"))
            addr.prefixlen = nl_msg["prefixlen"]

            if addr not in self._ip_addrs:
                self._ip_addrs.append(addr)
            else:
                old_idx = self._ip_addrs.index(addr)
                addr_old = self._ip_addrs[old_idx]
                if addr.flags != addr_old.flags:
                    self._ip_addrs.pop(old_idx)
                    self._ip_addrs.append(addr)

        elif nl_msg['header']['type'] == RTM_DELADDR:
            addr = ipaddress(nl_msg.get_attr('IFA_ADDRESS'))
            addr.prefixlen = nl_msg["prefixlen"]

            if addr in self._ip_addrs:
                self._ip_addrs.remove(addr)
Beispiel #2
0
    def __getattr__(self, name):
        if name == "_inited":
            return False
        if not self._inited:
            return super(RemoteDevice, self).__getattr__(name)

        attr = getattr(self._dev_cls, name)

        if self.deleted and not self._cached:
            raise DeviceDeleted(
                "This device was deleted on the slave and does not exist anymore."
            )

        if callable(attr):
            if self._cached:
                raise DeviceReadOnly(
                    "Can't call methods when in ReadOnly cache mode.")

            def dev_method(*args, **kwargs):
                return self._machine.remote_device_method(
                    self.ifindex, name, args, kwargs, self.netns)

            return dev_method
        else:
            if self._cached:
                return self._cache[name]

            return self._machine.remote_device_getattr(self.ifindex, name,
                                                       self.netns)
Beispiel #3
0
    def __setattr__(self, name, value):
        try:
            if getattr(self, "_deleted"):
                raise DeviceDeleted("Device was deleted.")
        except AttributeError:
            pass

        try:
            if not getattr(self, "_enabled") and name[0] != "_":
                raise DeviceDisabled(
                    "Can't set attributes for a disabled device.")
        except AttributeError:
            pass

        return super(Device, self).__setattr__(name, value)
Beispiel #4
0
    def _update_netlink(self, nl_msg):
        if getattr(self, "_deleted"):
            raise DeviceDeleted("Device was deleted.")

        if self.ifindex != nl_msg['index']:
            msg = "ifindex of netlink message (%s) doesn't match "\
                  "the device's (%s)." % (nl_msg['index'], self.ifindex)
            raise DeviceError(msg)

        if nl_msg['header']['type'] == RTM_NEWLINK:
            self._nl_msg = nl_msg
        elif nl_msg['header']['type'] == RTM_NEWADDR:
            if nl_msg['family'] == AF_INET:
                """
                from if_addr.h:
                /*
                 * Important comment:
                 * IFA_ADDRESS is prefix address, rather than local interface address.
                 * It makes no difference for normally configured broadcast interfaces,
                 * but for point-to-point IFA_ADDRESS is DESTINATION address,
                 * local address is supplied in IFA_LOCAL attribute.
                 */
                """
                addr = ipaddress(nl_msg.get_attr('IFA_LOCAL'),
                                 flags=nl_msg.get_attr("IFA_FLAGS"))
            else:
                addr = ipaddress(nl_msg.get_attr('IFA_ADDRESS'),
                                 flags=nl_msg.get_attr("IFA_FLAGS"))

            addr.prefixlen = nl_msg["prefixlen"]

            if addr not in self._ip_addrs:
                self._ip_addrs.append(addr)
            else:
                old_idx = self._ip_addrs.index(addr)
                addr_old = self._ip_addrs[old_idx]
                if addr.flags != addr_old.flags:
                    self._ip_addrs.pop(old_idx)
                    self._ip_addrs.append(addr)

        elif nl_msg['header']['type'] == RTM_DELADDR:
            addr = ipaddress(nl_msg.get_attr('IFA_ADDRESS'))
            addr.prefixlen = nl_msg["prefixlen"]

            if addr in self._ip_addrs:
                self._ip_addrs.remove(addr)
Beispiel #5
0
    def __getattribute__(self, name):
        what = object.__getattribute__(self, name)

        if object.__getattribute__(self, "_deleted"):
            raise DeviceDeleted()

        if not callable(what):
            return what
        else:
            if (object.__getattribute__(self, "_enabled")
                    or name in ["enable", "disable"]):
                return what
            else:

                def noop(*args, **kwargs):
                    pass

                return noop
Beispiel #6
0
    def __getattribute__(self, name):
        what = super(Device, self).__getattribute__(name)

        try:
            if super(Device, self).__getattribute__("_deleted"):
                raise DeviceDeleted("Device was deleted.")
        except AttributeError:
            pass

        try:
            if not callable(what):
                return what
            else:
                if (super(Device, self).__getattribute__("_enabled")
                        or name[0] == "_"):
                    return what
                elif not super(Device, self).__getattribute__("_enabled"):
                    raise DeviceDisabled(
                        "Can't call methods on a disabled device.")
        except AttributeError:
            return what