示例#1
0
def move_interface(namespace, interface):
    namespace = util.process_namespace(namespace)

    parts = interface.split(".")
    if len(parts) not in range(1, 4):
        raise exceptions.ServerError(f"Only untagged, single and double tagged interfaces are supported")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        all_interfaces = [intf.name for intf in cfg.namespaces[namespace].interfaces]
    else:
        all_interfaces = util.list_interfaces(namespace)

    if interface not in all_interfaces:
        raise exceptions.ServerError(f"Interface {interface} does not exist in {namespace}")

    new_namespace_key: str = "destination_namespace"
    if new_namespace_key not in request.json:
        raise exceptions.ServerError(f"Invalid request: request should contain \"destination_namespace\"")

    new_namespace = request.json[new_namespace_key]

    if app.simulate:
        old_ns: Namespace = cfg.namespaces[namespace]
        new_ns: Namespace = cfg.namespaces[new_namespace]
        iface: Interface = old_ns.get_interface(interface)
        old_ns.interfaces.remove(iface)
        new_ns.interfaces.append(iface)
    else:
        util.move_interface(new_namespace, interface, interface, old_namespace=namespace)

    return jsonify({})
示例#2
0
def cleanup_network() -> None:
    """ Cleanup networking by removing tap devices and network namespace
    """
    for namespace in list_net_ns():
        if namespace.startswith("test-"):
            subprocess.check_output(["ip", "netns", "delete", namespace])

    for link in list_interfaces():
        if link.startswith("test"):
            subprocess.check_output(["ip", "link", "delete", link])
示例#3
0
def list_interfaces(namespace):
    namespace = util.process_namespace(namespace, allow_none=True)

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            return abort(404)

        return jsonify([intf.name for intf in cfg.namespaces[namespace].interfaces])

    return jsonify(util.list_interfaces(namespace))
示例#4
0
def test_sub_interfaces(setup_networking):
    create_sub_interface("test-ns-1", "eth0", 10)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.10" in ifaces

    delete_sub_interface("test-ns-1", "eth0", 10)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.10" not in ifaces

    create_sub_interface("test-ns-1", "eth0", 100, 101)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.100.101" in ifaces
    assert "eth0.100" in ifaces

    delete_sub_interface("test-ns-1", "eth0", 100, 101)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.100.101" not in ifaces
    assert "eth0.100" not in ifaces

    create_sub_interface("test-ns-1", "eth0", 200, 101)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.200.101" in ifaces
    assert "eth0.200" in ifaces

    delete_sub_interface("test-ns-1", "eth0", 200, 101, keep_outer=True)
    ifaces = list_interfaces("test-ns-1")
    assert "eth0.200.101" not in ifaces
    assert "eth0.200" in ifaces
示例#5
0
def list_interfaces(namespace):
    if namespace is None or len(namespace) == 0:
        raise exceptions.ServerError(f"Invalid namespace {namespace}")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            return abort(404)

        return jsonify(
            [intf.name for intf in cfg.namespaces[namespace].interfaces])

    return jsonify(util.list_interfaces(namespace))
示例#6
0
def create_sub_interface(namespace, interface):
    if namespace is None or len(namespace) == 0:
        raise exceptions.ServerError(f"Invalid namespace {namespace}")

    parts = interface.split(".")
    if len(parts) not in [2, 3]:
        raise exceptions.ServerError(
            f"Only single and double tagged interfaces are supported")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        all_interfaces = [
            intf.name for intf in cfg.namespaces[namespace].interfaces
        ]
    else:
        all_interfaces = util.list_interfaces(namespace)

    if interface in all_interfaces:
        raise exceptions.ServerError(
            f"Interface {interface} already exists in {namespace}")

    base_interface = parts.pop(0)

    if base_interface not in all_interfaces:
        raise exceptions.ServerError(
            f"Base interface {base_interface} does not exist")

    outer = int(parts.pop(0))
    inner = 0
    if len(parts):
        inner = int(parts[0])

    if app.simulate:
        ns = cfg.namespaces[namespace]
        base_intf = ns.get_interface(base_interface)
        intf = config.Interface(name=interface, mac=base_intf.mac)
        ns.interfaces.append(intf)
        return jsonify({"interface": intf.get_state()})
    else:
        util.create_sub_interface(namespace, base_interface, outer, inner)
        return jsonify(util.get_interface_state(namespace, interface))
示例#7
0
def delete_sub_interface(namespace, interface):
    namespace = util.process_namespace(namespace)

    parts = interface.split(".")
    if len(parts) not in [2, 3]:
        raise exceptions.ServerError(f"Only single and double tagged interfaces are supported")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        all_interfaces = [intf.name for intf in cfg.namespaces[namespace].interfaces]
    else:
        all_interfaces = util.list_interfaces(namespace)

    if interface not in all_interfaces:
        raise exceptions.ServerError(f"Interface {interface} dot exist in {namespace}")

    base_interface = parts.pop(0)

    if base_interface not in all_interfaces:
        raise exceptions.ServerError(f"Base interface {base_interface} does not exist")

    outer = int(parts.pop(0))
    inner = 0
    if len(parts):
        inner = int(parts[0])

    if app.simulate:
        ns = cfg.namespaces[namespace]
        intf = ns.get_interface(interface)
        ns.interfaces.remove(intf)
    else:
        util.delete_sub_interface(namespace, base_interface, outer, inner)

    return jsonify({})
示例#8
0
def test_list_interfaces(setup_networking):
    interfaces = list_interfaces()
    assert "test7" in interfaces

    interfaces = list_interfaces("test-ns-1")
    assert "eth0" in interfaces