Example #1
0
 def lookup_interface(self, ip, link):
     # TODO(sean-k-mooney): remove try block after we raise
     # the min pyroute2 version above 0.5.12
     try:
         idx = ip.link_lookup(ifname=link)
     except ipexc.NetlinkError:
         raise exception.NetworkInterfaceNotFound(interface=link)
     if not len(idx):
         raise exception.NetworkInterfaceNotFound(interface=link)
     return idx[0]
Example #2
0
    def set(self,
            device,
            check_exit_code=None,
            state=None,
            mtu=None,
            address=None,
            promisc=None):
        check_exit_code = check_exit_code or []
        ip = iproute.IPRoute()
        idx = ip.link_lookup(ifname=device)
        if not idx:
            raise exception.NetworkInterfaceNotFound(interface=device)
        idx = idx[0]

        args = {'index': idx}
        if state:
            args['state'] = state
        if mtu:
            args['mtu'] = mtu
        if address:
            args['address'] = address
        if promisc is not None:
            flags = ip.link('get', index=idx)[0]['flags']
            args['flags'] = (utils.set_mask(flags, ifinfmsg.IFF_PROMISC)
                             if promisc is True else utils.unset_mask(
                                 flags, ifinfmsg.IFF_PROMISC))

        if isinstance(check_exit_code, int):
            check_exit_code = [check_exit_code]

        return self._ip_link(ip, 'set', check_exit_code, **args)
    def add(self,
            device,
            dev_type,
            check_exit_code=None,
            peer=None,
            link=None,
            vlan_id=None):
        check_exit_code = check_exit_code or []
        with iproute.IPRoute() as ip:
            args = {'ifname': device, 'kind': dev_type}
            if self.TYPE_VLAN == dev_type:
                args['vlan_id'] = vlan_id
                idx = ip.link_lookup(ifname=link)
                if 0 == len(idx):
                    raise exception.NetworkInterfaceNotFound(interface=link)
                args['link'] = idx[0]
            elif self.TYPE_VETH == dev_type:
                args['peer'] = peer
            elif self.TYPE_BRIDGE == dev_type:
                # NOTE(sean-k-mooney): the keys are defined in the pyroute2
                # codebase but are not documented. see the nla_map field
                # in the bridge_data class located in the
                # pyroute2.netlink.rtnl.ifinfmsg module for mode details
                # https://github.com/svinota/pyroute2/blob/3ba9cdde34b2346ef8c2f8ba17cef5dbeb4c6d52/pyroute2/netlink/rtnl/ifinfmsg/__init__.py#L776-L820
                args['IFLA_BR_AGEING_TIME'] = 0  # disable mac learning ageing
                args['IFLA_BR_FORWARD_DELAY'] = 0  # set no delay
                args['IFLA_BR_STP_STATE'] = 0  # disable spanning tree
                args['IFLA_BR_MCAST_SNOOPING'] = 0  # disable snooping
            else:
                raise exception.NetworkInterfaceTypeNotDefined(type=dev_type)

            return self._ip_link(ip, 'add', check_exit_code, **args)
Example #4
0
    def delete(self, device, check_exit_code=None):
        check_exit_code = check_exit_code or []
        ip = iproute.IPRoute()
        idx = ip.link_lookup(ifname=device)
        if len(idx) == 0:
            raise exception.NetworkInterfaceNotFound(interface=device)
        idx = idx[0]

        return self._ip_link(ip, 'del', check_exit_code, **{'index': idx})
Example #5
0
    def add(self,
            device,
            dev_type,
            check_exit_code=None,
            peer=None,
            link=None,
            vlan_id=None,
            ageing=None):
        check_exit_code = check_exit_code or []
        with iproute.IPRoute() as ip:
            args = {'ifname': device, 'kind': dev_type}
            if self.TYPE_VLAN == dev_type:
                args['vlan_id'] = vlan_id
                idx = ip.link_lookup(ifname=link)
                if 0 == len(idx):
                    raise exception.NetworkInterfaceNotFound(interface=link)
                args['link'] = idx[0]
            elif self.TYPE_VETH == dev_type:
                args['peer'] = peer
            elif self.TYPE_BRIDGE == dev_type:
                # NOTE(sean-k-mooney): the keys are defined in the pyroute2
                # codebase but are not documented. see the nla_map field
                # in the bridge_data class located in the
                # pyroute2.netlink.rtnl.ifinfmsg module for mode details
                # https://github.com/svinota/pyroute2/blob/3ba9cdde34b2346ef8c2f8ba17cef5dbeb4c6d52/pyroute2/netlink/rtnl/ifinfmsg/__init__.py#L776-L820
                args['IFLA_BR_FORWARD_DELAY'] = 0  # set no delay
                args['IFLA_BR_STP_STATE'] = 0  # disable spanning tree
                args['IFLA_BR_MCAST_SNOOPING'] = 0  # disable snooping
                # NOTE(sean-k-mooney): we conditionally enable mac ageing as
                # this code is shared between the ovs and linux bridge
                # plugins. For linux bridge we want to allow the default
                # ageing of 300 seconds, whereas for ovs with the ip-tables
                # firewall we want to disable ageing. None was chosen as
                # the default value of ageing to allow the caller to determine
                # what policy to use and keep this code generic.
                if ageing is not None:
                    args['IFLA_BR_AGEING_TIME'] = ageing
            else:
                raise exception.NetworkInterfaceTypeNotDefined(type=dev_type)

            return self._ip_link(ip, 'add', check_exit_code, **args)
Example #6
0
    def add(self,
            device,
            dev_type,
            check_exit_code=None,
            peer=None,
            link=None,
            vlan_id=None):
        check_exit_code = check_exit_code or []
        ip = iproute.IPRoute()
        args = {'ifname': device, 'kind': dev_type}
        if self.TYPE_VLAN == dev_type:
            args['vlan_id'] = vlan_id
            idx = ip.link_lookup(ifname=link)
            if 0 == len(idx):
                raise exception.NetworkInterfaceNotFound(interface=link)
            args['link'] = idx[0]
        elif self.TYPE_VETH == dev_type:
            args['peer'] = peer
        else:
            raise exception.NetworkInterfaceTypeNotDefined(type=dev_type)

        return self._ip_link(ip, 'add', check_exit_code, **args)