Exemplo n.º 1
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)
Exemplo n.º 2
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
                args['link'] = self.lookup_interface(ip, link)
            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)
Exemplo n.º 3
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 []
        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)
Exemplo n.º 4
0
 def exists(self, device):
     """Return True if the device exists."""
     with iproute.IPRoute() as ip:
         try:
             self.lookup_interface(ip, device)
             return True
         except Exception:
             return False
Exemplo n.º 5
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})
Exemplo n.º 6
0
def add_conf(dev):

    table_id = int(str(dev)[3:]) + 1  # table_id based on ppp interface int

    handler = iproute.IPRoute()
    index = handler.link_lookup(ifname=str(dev))[0]
    vpnclient = (handler.get_addr(
        match=lambda x: x['index'] == int(index)))[0]['attrs'][0][1]

    command('ip rule add from ' + str(get_ip_address(dev)) + ' table ' +
            str(table_id))
    command('ip route add default scope global via ' + str(vpnclient) +
            ' dev ' + str(dev) + ' table ' + str(table_id))
    return
Exemplo n.º 7
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)
Exemplo n.º 8
0
    def set(self, device, check_exit_code=None, state=None, mtu=None,
            address=None, promisc=None, master=None):
        check_exit_code = check_exit_code or []
        with iproute.IPRoute() as ip:
            idx = self.lookup_interface(ip, device)
            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 master:
                args['master'] = self.lookup_interface(ip, master)

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

            return self._ip_link(ip, 'set', check_exit_code, **args)
Exemplo n.º 9
0
 def exists(self, device):
     """Return True if the device exists."""
     with iproute.IPRoute() as ip:
         idx = ip.link_lookup(ifname=device)
         return True if idx else False
Exemplo n.º 10
0
 def delete(self, device, check_exit_code=None):
     check_exit_code = check_exit_code or []
     with iproute.IPRoute() as ip:
         idx = self.lookup_interface(ip, device)
         return self._ip_link(ip, 'del', check_exit_code, **{'index': idx})