Exemplo n.º 1
0
def create(dry_run, logger, name, mode, uid=0, gid=0, ifr=None, netns=None, root_ipdb=None):
    """
    Create a tun/tap interface object, using a given interface name.
    """

    description = "%s interface" % mode

    interface._log_create(logger, name, description, netns, root_ipdb)

    if dry_run:
        return Tuntap(logger, name, None, netns, root_ipdb, mode)

    ipdb = interface._ipdb_get(name, description, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb)

    if existing_if:
        if (
            existing_if.kind not in IF_TYPES
            or existing_if.uid != uid
            or existing_if.gid != gid
            or existing_if.ifr != ifr
        ):
            Interface(None, name, existing_if, netns, root_ipdb).remove()
        else:
            return Tuntap(logger, name, existing_if, netns, root_ipdb, mode)

    new_if = ipdb.create(ifname=name, kind="tuntap", mode=mode, uid=uid, gid=gid, ifr=ifr)
    ipdb.commit()

    return Tuntap(logger, name, new_if, netns, root_ipdb, mode)
Exemplo n.º 2
0
def create(dry_run, logger, name, kind,
        # for other parameters, look in pyroute2/netlink/rtnl/ifinfmsg.py
        local, remote, link=None, iflags=32, oflags=32, key=None, ikey=None, okey=None, ttl=16,
        netns=None, root_ipdb=None):
    '''
    Create a gre/gretap interface object, using a given interface name.
    '''

    description = "%s interface" % kind

    interface._log_create(logger, name, description, netns, root_ipdb)

    if dry_run:
        return GRE(logger, name, None, netns, root_ipdb, kind)

    ipdb = interface._ipdb_get(name, description, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb)

    if key is not None:
        ikey = key
        okey = key

    if existing_if:
        if (existing_if.kind not in IF_TYPES or
                existing_if.gre_local != str(local) or
                existing_if.gre_remote != str(remote) or
                existing_if.gre_link != link or
                existing_if.gre_iflags != iflags or
                existing_if.gre_oflags != oflags or
                existing_if.gre_ikey != ikey or
                existing_if.gre_okey != okey or
                existing_if.gre_ttl != ttl):
            logger.debug("removing interface '%s'" % name)
            Interface(None, name, existing_if, netns, root_ipdb).remove()
        else:
            return GRE(logger, name, existing_if, netns, root_ipdb, kind)

    new_if = ipdb.create(
        ifname = name,
        kind = kind,
        gre_local = str(local),
        gre_remote = str(remote),
        gre_link = link,
        gre_iflags = iflags,
        gre_oflags = oflags,
        gre_ikey = ikey,
        gre_okey = okey,
        gre_ttl = ttl,
    )
    ipdb.commit()

    return GRE(logger, name, new_if, netns, root_ipdb, kind)
Exemplo n.º 3
0
def get(dry_run, logger, name, netns=None, root_ipdb=None):
    """
    Tries to find a dummy interface with the given name in the
    chosen namespace and returns it.
    """

    interface._log_get(logger, name, IF_DESCRIPTION, netns, root_ipdb)

    if dry_run:
        return Dummy(logger, name, None, netns, root_ipdb)

    ipdb = interface._ipdb_get(name, IF_DESCRIPTION, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb, IF_TYPE)

    if existing_if:
        return Dummy(logger, name, existing_if, netns, root_ipdb)
    else:
        raise NotFoundError(name, IF_DESCRIPTION, netns, root_ipdb)
Exemplo n.º 4
0
def get(dry_run, logger, name, mode, netns=None, root_ipdb=None):
    """
    Tries to find a tun/tap interface with the given name in the
    chosen namespace and returns it.
    """

    description = "%s interface" % mode

    interface._log_get(logger, name, description, netns, root_ipdb)

    if dry_run:
        return Tuntap(logger, name, None, netns, root_ipdb, mode)

    ipdb = interface._ipdb_get(name, description, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb, mode)

    if existing_if:
        return Tuntap(logger, name, existing_if, netns, root_ipdb, mode)
    else:
        raise NotFoundError(name, mode, netns, root_ipdb)
Exemplo n.º 5
0
def get(dry_run, logger, name, kind, netns=None, root_ipdb=None):
    '''
    Tries to find a gre/gretap interface with the given name in the
    chosen namespace and returns it.
    '''

    description = "%s interface" % kind

    interface._log_get(logger, name, description, netns, root_ipdb)

    if dry_run:
        return GRE(logger, name, None, netns, root_ipdb, kind)

    ipdb = interface._ipdb_get(name, description, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb, kind)

    if existing_if:
        return GRE(logger, name, existing_if, netns, root_ipdb, kind)
    else:
        raise NotFoundError(name, kind, netns, root_ipdb)
Exemplo n.º 6
0
def create(dry_run, logger, name, netns=None, root_ipdb=None):
    """
    Create a dummy interface object, using a given interface name.
    """

    interface._log_create(logger, name, IF_DESCRIPTION, netns, root_ipdb)

    if dry_run:
        return Dummy(logger, name, None, netns, root_ipdb)

    ipdb = interface._ipdb_get(name, IF_DESCRIPTION, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb)

    if existing_if:
        if existing_if.kind != IF_TYPE:
            Interface(None, name, existing_if, netns, root_ipdb).remove()
        else:
            return Dummy(logger, name, existing_if, netns, root_ipdb)

    new_if = ipdb.create(ifname=name, kind=IF_TYPE)
    ipdb.commit()

    return Dummy(logger, name, new_if, netns, root_ipdb)
Exemplo n.º 7
0
def create(dry_run, logger, name, peer, netns=None, root_ipdb=None):
    '''
    Create a veth interface object, using a given interface name.
    '''

    interface._log_create(logger, name, IF_DESCRIPTION, netns, root_ipdb)

    if dry_run:
        return VETH(logger, name, None, netns, root_ipdb, peer)

    ipdb = interface._ipdb_get(name, IF_DESCRIPTION, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb)

    if existing_if:
        if existing_if.kind != IF_TYPE or existing_if.peer != peer:
            Interface(None, name, existing_if, netns, root_ipdb).remove()
        else:
            return VETH(logger, name, existing_if, netns, root_ipdb, peer)

    new_if = ipdb.create(ifname=name, kind=IF_TYPE, peer=peer)
    ipdb.commit()

    return VETH(logger, name, new_if, netns, root_ipdb, peer)
Exemplo n.º 8
0
def create(dry_run, logger, name, netns=None, root_ipdb=None):
    '''
    Create a bridge interface object, using a given interface name.
    '''

    interface._log_create(logger, name, IF_DESCRIPTION, netns, root_ipdb)

    if dry_run:
        return Bridge(logger, name, None, netns, root_ipdb)

    ipdb = interface._ipdb_get(name, IF_DESCRIPTION, netns, root_ipdb)
    existing_if = interface._interface_get(name, ipdb)

    # Always remove any existing interfaces with the given name.
    # Even if it is already a bridge, it could have ports attached
    # to it already. Ideally, this function should be idempotent, but
    # it's not really possible for bridges.
    if existing_if:
        Interface(None, name, existing_if, netns, root_ipdb).remove()

    new_if = ipdb.create(ifname=name, kind=IF_TYPE)
    ipdb.commit()

    return Bridge(logger, name, new_if, netns, root_ipdb)