feat: cross-compute VXLAN kernel datapath

Cross-compute links can ride kernel VXLAN tunnels instead of the uBridge
UDP relay: each endpoint creates one VXLAN device (uBridge's rtnetlink
vxlan module) enslaved into the same per-link Linux bridge the same-host
datapath uses, so filters, markers, capture and suspend keep running on
the anchor unchanged. Opt-in (Server.enable_vxlan_cross_compute, default
off) because the tunnel caps the usable MTU at the underlay minus the
outer headers; the relay stays the default and the fallback.

The controller derives what the pair must agree on: one VNI per link
(upper VNI space, per-link random salt) and deterministic device names
(gvl{link}{node}) that make crashed-run leftovers claimable
(show -> delete -> create). Same-host compute pairs, switch endpoints
and serial/unix-socket containers stay on the relay.

A VNI the kernel refuses self-heals on every path it can surface:
interactive create retries a fresh salt then degrades to the relay;
project open re-dispatches refused batch entries the same way; node
start recognises the refused device, stops the half-started node,
re-wires both ends on a fresh VNI (or degrades the link) and retries
the start. The device MTU is derived from the lower device (dev=) and
the per-link bridge inherits it - no configuration. The capability
probe (ubridge_vxlan in /capabilities) runs one scratch create/delete
on a scratch port that never equals the configured dstport.

The e2e scenario runs two computes on one machine (a network namespace
joined by a veth pair as the fabric) and needs root.
This commit is contained in:
YueGuobin committed 2026-10-17 14:11:00 +08:00
1 parent 5514b2e929
commit 21b866aeed
24 files changed
+2107 -59

No files matched your search

@@ -45,6 +45,7 @@ class TestCapabilitiesRoutes:
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
@@ -73,6 +74,7 @@ class TestCapabilitiesRoutes:
"ubridge_iol_tap": None,
"ubridge_bridge_tap": None,
"vpcs_tap": None,
"ubridge_vxlan": None,
}
async def test_get_on_gns3vm(self, app: FastAPI, compute_client: AsyncClient, on_gns3vm, monkeypatch) -> None:
@@ -85,6 +87,7 @@ class TestCapabilitiesRoutes:
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
@@ -113,6 +116,7 @@ class TestCapabilitiesRoutes:
"ubridge_iol_tap": None,
"ubridge_bridge_tap": None,
"vpcs_tap": None,
"ubridge_vxlan": None,
}
async def test_get_reports_ubridge_tc(self, app: FastAPI, compute_client: AsyncClient, monkeypatch) -> None:
@@ -199,6 +203,25 @@ class TestCapabilitiesRoutes:
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
assert response.status_code == status.HTTP_200_OK
assert response.json()["vpcs_tap"] is True
async def test_get_reports_ubridge_vxlan(self, app: FastAPI, compute_client: AsyncClient, monkeypatch) -> None:
"""
The vxlan capability is what makes a cross-compute link tunnel-
eligible, so it travels with the other compute capabilities.
"""
async def fake_vxlan_probe():
return True
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", fake_vxlan_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
assert response.status_code == status.HTTP_200_OK
assert response.json()["ubridge_vxlan"] is True
@@ -22,6 +22,7 @@ fallback and cleanup.
"""
import os
import socket
import uuid
from unittest.mock import MagicMock, call, patch
@@ -32,6 +33,7 @@ from gns3server.compute.compute_error import ComputeError
from gns3server.compute.docker import Docker
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error
from gns3server.compute.docker.docker_vm import DockerVM
from gns3server.compute.error import NodeError
from gns3server.compute.nios.nio_bridge import NIOBridge
from gns3server.compute.ubridge.ubridge_error import UbridgeError, UbridgeNamespaceError
from tests.utils import AsyncioMagicMock
@@ -1468,3 +1470,147 @@ async def test_stop_clears_relay_bridge_registry(vm):
await vm.stop()
assert vm._bridges == set()
assert vm._ubridge_tc_caps is None
# ---------------------------------------------------------------------------
# Cross-compute VXLAN tunnel
# ---------------------------------------------------------------------------
VXLAN_SPEC = {"name": "gvl01234567abcd", "vni": 1048577, "remote": "192.168.1.2", "dstport": 14789}
def test_create_nio_bridge_carries_vxlan_spec(vm):
"""
The vxlan sub-spec of a cross-compute NIO survives the factory and the
asdict round-trip (the node replays the NIO from asdict on restart).
"""
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
assert isinstance(nio, NIOBridge)
assert nio.vxlan == VXLAN_SPEC
assert nio.asdict()["vxlan"] == VXLAN_SPEC
@pytest.mark.asyncio
async def test_add_ubridge_kernel_connection_with_vxlan(vm):
"""
A vxlan-carrying NIO creates this end's tunnel device (lower device
resolved locally from the peer address) and enslaves it into the link's
kernel bridge next to the anchor.
"""
async def send(command):
if command.startswith("vxlan show"):
raise UbridgeError("VXLAN does not exist")
vm._ubridge_hypervisor = MagicMock()
vm._ubridge_hypervisor.send = AsyncioMagicMock(side_effect=send)
vm._namespace = 42
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
with patch.object(DockerVM, "_vxlan_lowerdev", return_value="eth0"):
await vm._add_ubridge_connection(nio, 0)
host_ifc, _ = vm._veth_names(0, 0)
sends = [c.args[0] for c in vm._ubridge_hypervisor.send.call_args_list]
show = f'vxlan show "{VXLAN_SPEC["name"]}"'
create = (
f'vxlan create "{VXLAN_SPEC["name"]}" {VXLAN_SPEC["vni"]} '
f"remote={VXLAN_SPEC['remote']} dstport={VXLAN_SPEC['dstport']} dev=eth0"
)
assert sends.index(show) < sends.index(create)
# The tunnel device is enslaved after the node's anchor, and the anchor
# still goes up.
assert sends.index(f'brctl addif "{BRIDGE}" "{host_ifc}"') < sends.index(
f'brctl addif "{BRIDGE}" "{VXLAN_SPEC["name"]}"'
)
assert f'link set "{host_ifc}" up' in sends
# A failed show (device absent) must not trigger a delete.
assert f'vxlan delete "{VXLAN_SPEC["name"]}"' not in sends
@pytest.mark.asyncio
async def test_add_ubridge_kernel_connection_replaces_vxlan_leftover(vm):
"""
A device found under this link's deterministic name is this link's own
leftover (a crashed run, a node restart): deleted and re-created, which
makes wiring idempotent.
"""
vm._ubridge_hypervisor = MagicMock()
vm._namespace = 42
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
with patch.object(DockerVM, "_vxlan_lowerdev", return_value="eth0"):
await vm._add_ubridge_connection(nio, 0)
sends = [c.args[0] for c in vm._ubridge_hypervisor.send.call_args_list]
show = f'vxlan show "{VXLAN_SPEC["name"]}"'
delete = f'vxlan delete "{VXLAN_SPEC["name"]}"'
create = (
f'vxlan create "{VXLAN_SPEC["name"]}" {VXLAN_SPEC["vni"]} '
f"remote={VXLAN_SPEC['remote']} dstport={VXLAN_SPEC['dstport']} dev=eth0"
)
assert sends.index(show) < sends.index(delete) < sends.index(create)
@pytest.mark.asyncio
async def test_remove_kernel_nio_deletes_vxlan_device(vm):
"""
Link teardown deletes this end's tunnel device (the kernel un-enrolls it
from the bridge on unregister) before the bridge delete.
"""
vm._ubridge_hypervisor = MagicMock()
vm._ubridge_send = AsyncioMagicMock()
vm.status = "started"
host_ifc, _ = vm._veth_names(0, 0)
vm._kernel_veths[(0, 0)] = host_ifc
vm._ethernet_adapters[0].add_nio(
0, vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
)
await vm.adapter_remove_nio_binding(0)
vm._ubridge_send.assert_has_calls(
[
call(f'brctl delif "{BRIDGE}" "{host_ifc}"'),
call(f'vxlan delete "{VXLAN_SPEC["name"]}"'),
call(f'brctl delete "{BRIDGE}"'),
],
any_order=False,
)
@pytest.mark.asyncio
async def test_vxlan_lowerdev_unresolvable_peer_raises(vm):
"""
A peer that cannot be resolved (or routed to) is a wiring error of this
link — the controller retries and then degrades it to the relay.
"""
with patch("gns3server.compute.kernel_datapath.socket.getaddrinfo", side_effect=socket.gaierror("nope")):
with pytest.raises(NodeError):
await vm._vxlan_lowerdev("192.0.2.1")
def test_interface_with_address_matches_the_owning_interface():
"""
The route's chosen source address is matched back to its interface,
scope suffixes stripped (link-local IPv6).
"""
class _Addr:
def __init__(self, address):
self.address = address
with patch(
"gns3server.compute.kernel_datapath.psutil.net_if_addrs",
return_value={
"eth0": [_Addr("192.168.1.1")],
"eth1": [_Addr("fe80::1%eth1")],
},
):
assert DockerVM._interface_with_address("192.168.1.1") == "eth0"
assert DockerVM._interface_with_address("fe80::1%eth1") == "eth1"
assert DockerVM._interface_with_address("10.0.0.1") is None
+51
View File
@@ -29,8 +29,10 @@ from gns3server.compute.ubridge.tc_probe import (
probe_iol_tap_support,
probe_tap_support,
probe_tc_capabilities,
probe_vxlan_support,
)
from gns3server.compute.ubridge.ubridge_error import UbridgeError
from gns3server.config import Config
pytestmark = pytest.mark.asyncio
@@ -92,6 +94,7 @@ def probe_env(monkeypatch, tmp_path):
tc_probe._tap_cache.clear()
tc_probe._iol_tap_cache.clear()
tc_probe._bridge_tap_cache.clear()
tc_probe._vxlan_cache.clear()
FakeHypervisor.last = None
# a real file on disk: the cache is keyed on its (path, mtime, size,
# ctime)
@@ -275,3 +278,51 @@ async def test_probe_bridge_tap_support_missing_binary(monkeypatch):
monkeypatch.setattr(tc_probe.shutil, "which", lambda name: None)
assert await probe_bridge_tap_support() is None
assert FakeHypervisor.spawned == 0
class _FixedUuid:
"""A uuid4 stand-in whose .int/.hex pin the probe's random draws."""
def __init__(self, value):
self.int = value
self.hex = f"{value:032x}"
def _probe_create_port():
"""The dstport of the one `vxlan create` the probe issued."""
create = next(c for c in FakeHypervisor.last.commands if c.startswith("vxlan create "))
return int(create.rsplit("dstport=", 1)[1])
async def test_probe_vxlan_avoids_the_configured_dstport(monkeypatch, probe_env):
"""
A scratch draw landing exactly on the configured dstport must move: on
that port the scratch VNI shares a demux table with the live links'
devices, and an unlucky VNI draw would make the probe report the
capability unknown for the process's lifetime (the result is cached).
"""
# int % 10000 == 9999 -> the scratch window draws its very last value
monkeypatch.setattr(tc_probe.uuid, "uuid4", lambda: _FixedUuid(29999))
settings = Config.instance().settings.Server
monkeypatch.setattr(settings, "vxlan_dstport", 29999)
assert await probe_vxlan_support() is True
assert _probe_create_port() == 20000
async def test_probe_vxlan_scratch_port_stays_off_the_configured_one(monkeypatch, probe_env):
"""
Whatever the configured dstport, the probe's socket never lands on it
and stays inside its scratch window.
"""
settings = Config.instance().settings.Server
for configured in (14789, 20000, 25000, 29999):
monkeypatch.setattr(settings, "vxlan_dstport", configured)
tc_probe._vxlan_cache.clear()
assert await probe_vxlan_support() is True
port = _probe_create_port()
assert 20000 <= port <= 29999, port
assert port != configured, (port, configured)
+351 -1
View File
@@ -20,18 +20,21 @@ Controller-side kernel-datapath link selection: eligibility of Docker-to-
Docker links, NIO emission and the filters/markers guards.
"""
from contextlib import contextmanager
from unittest.mock import MagicMock, patch
import pytest
from gns3server.config import Config
from gns3server.controller.controller_error import ControllerError
from gns3server.controller.compute import ComputeError
from gns3server.controller.controller_error import ComputeConflictError, ControllerError
from gns3server.controller.link import FILTERS
from gns3server.controller.node import Node
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.controller.ports.serial_port import SerialPort
from gns3server.controller.udp_link import UDPLink
from gns3server.utils.kernel_anchor import kernel_anchor_name, kernel_cascade_names
from gns3server.utils.vxlan import vxlan_device_name
from tests.utils import AsyncioMagicMock
@@ -1174,3 +1177,350 @@ async def test_node_started_repushes_both_cascade_ends(project):
sw1.put.assert_called_once_with("/adapters/0/ports/0/nio", data=entries[0][3], timeout=120)
sw2.put.assert_called_once_with("/adapters/0/ports/0/nio", data=entries[1][3], timeout=120)
# ---------------------------------------------------------------------------
# Cross-compute VXLAN links
# ---------------------------------------------------------------------------
@contextmanager
def _cross_compute_enabled():
settings = Config.instance().settings.Server
original = settings.enable_vxlan_cross_compute
settings.enable_vxlan_cross_compute = True
try:
yield settings
finally:
settings.enable_vxlan_cross_compute = original
def _vxlan_compute(compute_id, vxlan=True):
compute = MagicMock()
compute.id = compute_id
compute.capabilities = {"ubridge_vxlan": vxlan}
return compute
async def _cross_compute_link(project, vxlan1=True, vxlan2=True, same_host=False):
"""A docker↔docker link whose endpoints live on two computes, with the
peer resolution (and the relay fallback's port reservation) mocked."""
compute1 = _vxlan_compute("compute-1", vxlan1)
compute2 = _vxlan_compute("compute-2", vxlan2)
async def subnet_callback(other):
return ("192.168.1.1", "192.168.1.1" if same_host else "192.168.1.2")
compute1.get_ip_on_same_subnet.side_effect = subnet_callback
async def post_callback(path, data=None, **kwargs):
response = MagicMock()
response.json = {"udp_port": 1024}
return response
compute1.post.side_effect = post_callback
compute2.post.side_effect = post_callback
node1 = _node(project, compute1, "docker1")
node2 = _node(project, compute2, "docker2")
link = UDPLink(project)
await link.add_node(node1, 0, 0, batch=True, dump=False)
await link.add_node(node2, 0, 0, batch=True, dump=False)
return link, node1, node2
@pytest.mark.asyncio
async def test_vxlan_cross_compute_disabled_by_config(project):
"""
Cross-compute links stay on the relay until the deployment opts in —
the VXLAN tunnel caps the usable MTU at the underlay minus the outer
headers, so it is opt-in.
"""
link, node1, node2 = await _cross_compute_link(project)
assert link._kernel_datapath_eligible(node1, node2) is False
with _cross_compute_enabled():
assert link._kernel_datapath_eligible(node1, node2) is True
@pytest.mark.asyncio
async def test_vxlan_cross_compute_requires_the_vxlan_capability(project):
"""
Both computes' uBridge must be able to create the tunnel device; an
unreported/failed probe (None) or an old build (False) keeps the link
on the relay, exactly like the other capability gates.
"""
with _cross_compute_enabled():
for vxlan1, vxlan2 in ((False, True), (True, False), (True, None), (None, None)):
link, node1, node2 = await _cross_compute_link(project, vxlan1=vxlan1, vxlan2=vxlan2)
assert link._kernel_datapath_eligible(node1, node2) is False
@pytest.mark.asyncio
async def test_vxlan_cross_compute_switch_stays_on_relay(project):
"""
The switch fast path absorbs the peer's anchor into the switch's own
bridge — a one-host shape; a switch link across computes keeps the
relay.
"""
switch = _node(project, _vxlan_compute("compute-1"), "switch1", node_type="ethernet_switch")
peer = _node(project, _vxlan_compute("compute-2"), "docker2")
link = await _link(project, switch, peer)
with _cross_compute_enabled():
assert link._kernel_datapath_eligible(switch, peer) is False
@pytest.mark.asyncio
async def test_prepare_vxlan_link_emits_tunnel_specs(project):
"""
Both ends get one nio_bridge sharing the per-link bridge name, each
carrying the same VNI/dstport (the pair must agree, and neither end can
ask the other) and the peer's address, with per-endpoint deterministic
device names.
"""
with _cross_compute_enabled():
link, node1, node2 = await _cross_compute_link(project)
entries = await link._prepare()
assert len(entries) == 2
expected_bridge = "gns3" + link.id.replace("-", "")[:11]
dstport = Config.instance().settings.Server.vxlan_dstport
specs = [entry[3] for entry in entries]
assert all(spec["type"] == "nio_bridge" for spec in specs)
assert all(spec["bridge"] == expected_bridge for spec in specs)
vxlan1, vxlan2 = specs[0]["vxlan"], specs[1]["vxlan"]
assert vxlan1["vni"] == vxlan2["vni"]
assert vxlan1["dstport"] == vxlan2["dstport"] == dstport
assert vxlan1["remote"] == "192.168.1.2"
assert vxlan2["remote"] == "192.168.1.1"
assert vxlan1["name"] == vxlan_device_name(link.id, node1.id)
assert vxlan2["name"] == vxlan_device_name(link.id, node2.id)
assert vxlan1["name"] != vxlan2["name"]
@pytest.mark.asyncio
async def test_prepare_vxlan_same_host_pair_falls_back_to_relay(project):
"""
Equal resolved addresses mean both computes share one host: the kernel
shares one VXLAN socket per dstport, so a tunnel pair cannot live there
— such links keep the relay, where the ends differ by UDP port.
"""
with _cross_compute_enabled():
link, node1, node2 = await _cross_compute_link(project, same_host=True)
entries = await link._prepare()
assert all(entry[3]["type"] == "nio_udp" for entry in entries)
@pytest.mark.asyncio
async def test_commit_vxlan_retries_with_a_fresh_vni(project):
"""
A refused commit (reactively: the VNI was already registered on the
host's shared socket) re-prepares the link with a bumped salt — a fresh
VNI, identical on both ends — and recommits.
"""
with _cross_compute_enabled():
link, node1, node2 = await _cross_compute_link(project)
vnis = []
async def commit(entries):
vnis.append(entries[0][3]["vxlan"]["vni"])
if len(vnis) == 1:
raise ComputeError("Could not create vxlan gvl...: File exists")
with patch.object(link, "_commit_nios", commit):
await link.create()
assert len(vnis) == 2
assert vnis[0] != vnis[1]
assert link._vxlan_downgraded is False
@pytest.mark.asyncio
async def test_commit_vxlan_degrades_to_relay_after_retries(project):
"""
When every VXLAN attempt fails, the link degrades to the UDP relay —
the opt-in datapath must never make a link uncreatable.
"""
with _cross_compute_enabled():
link, node1, node2 = await _cross_compute_link(project)
commits = []
async def commit(entries):
commits.append(entries)
if any(entry[3].get("vxlan") for entry in entries):
raise ComputeError("Could not create vxlan gvl...: File exists")
with patch.object(link, "_commit_nios", commit):
await link.create()
assert link._vxlan_downgraded is True
assert len(commits) == 4 # the three VXLAN attempts, then the relay
assert all(entry[3].get("vxlan") for entry in commits[0])
assert all(entry[3]["type"] == "nio_udp" for entry in commits[-1])
async def _prepared_vxlan_link(project):
"""A cross-compute link with its tunnel NIOs prepared (the shape a node
start replays) — call inside ``_cross_compute_enabled`` so the heal's
re-preparation stays on the tunnel too. _bind_members mirrors the
project-open finalize (the node's link back-reference is what lets the
heal find the conflicted link)."""
link, node1, node2 = await _cross_compute_link(project)
await link._prepare()
link._bind_members()
link._created = True
return link, node1, node2
def _vni_conflict(node, link):
"""The compute's 409 when the kernel refuses the link's VNI on the
shared dstport socket — the wording KernelDatapathMixin emits."""
device = vxlan_device_name(link.id, node.id)
return ComputeConflictError(
"http://compute", {"message": f"Could not create VXLAN device {device} to 192.168.1.2 via eth0: File exists"}
)
@pytest.mark.asyncio
async def test_node_start_self_heals_a_vni_conflict(project):
"""
A link wired while its nodes were stopped hits its VNI collision at
node start (the kernel's refusal is the first time anyone asks). The
start stops the half-started node, re-wires the link on a fresh VNI —
both ends, through the per-compute batch — and retries.
"""
with _cross_compute_enabled():
link, node1, node2 = await _prepared_vxlan_link(project)
salt = link._vxlan_salt
starts = []
async def post_callback(path, data=None, **kwargs):
if path.endswith("/start"):
starts.append(path)
if len(starts) == 1:
raise _vni_conflict(node1, link)
response = MagicMock()
response.json = {"udp_port": 1024}
return response
node1.compute.post.side_effect = post_callback
await node1.start()
assert len(starts) == 2
assert link._vxlan_downgraded is False
assert link._vxlan_salt == salt + 1
# the half-started node was stopped before the retry (a start on a
# running container early-returns without wiring)
assert any(call.args[0].endswith("/stop") for call in node1.compute.post.call_args_list)
# the fresh-VNI NIOs were re-dispatched to both computes
for compute in (node1.compute, node2.compute):
batches = [c for c in compute.post.call_args_list if str(c.args[0]).endswith("/nios/batch")]
assert len(batches) == 1
@pytest.mark.asyncio
async def test_node_start_degrades_the_link_after_a_second_conflict(project):
"""
A conflict that survives the fresh VNI degrades the link to the relay
— the start must succeed no later than that round, the same guarantee
create() and the project-open batch path give.
"""
with _cross_compute_enabled():
link, node1, node2 = await _prepared_vxlan_link(project)
starts = []
async def post_callback(path, data=None, **kwargs):
if path.endswith("/start"):
starts.append(path)
if len(starts) <= 2:
raise _vni_conflict(node1, link)
response = MagicMock()
response.json = {"udp_port": 1024}
return response
node1.compute.post.side_effect = post_callback
await node1.start()
assert len(starts) == 3
assert link._vxlan_downgraded is True
assert all(nio["type"] == "nio_udp" for nio in link._link_data)
@pytest.mark.asyncio
async def test_node_start_does_not_retry_other_failures(project):
"""
Only the VXLAN device conflict heals: any other start failure surfaces
unchanged, with a single attempt and no stop/redispatch side effects.
"""
with _cross_compute_enabled():
link, node1, node2 = await _prepared_vxlan_link(project)
salt = link._vxlan_salt
async def post_callback(path, data=None, **kwargs):
if path.endswith("/start"):
raise ComputeError("DockerError: image not found")
response = MagicMock()
response.json = {}
return response
node1.compute.post.side_effect = post_callback
with pytest.raises(ComputeError):
await node1.start()
starts = [c for c in node1.compute.post.call_args_list if str(c.args[0]).endswith("/start")]
assert len(starts) == 1
assert not any(call.args[0].endswith("/stop") for call in node1.compute.post.call_args_list)
assert link._vxlan_salt == salt
assert link._vxlan_downgraded is False
@pytest.mark.asyncio
async def test_open_heal_tries_the_full_vni_candidate_count(project):
"""
The project-open heal gives a refused link the same VNI candidate count
an interactive creation gets (fresh-salt rounds), degrading to the
relay only after they run out.
"""
link, node1, node2 = await _cross_compute_link(project)
rounds = []
async def redispatch(links, downgrade):
rounds.append(downgrade)
return links if not downgrade else []
with patch.object(project, "_redispatch_vxlan_links", redispatch):
await project._heal_refused_vxlan_links([link])
assert rounds == [False, False, True]
@pytest.mark.asyncio
async def test_open_heal_stops_at_the_first_successful_vni(project):
"""
A link healed by the first fresh-VNI round needs no further rounds —
no pointless second redispatch, no relay degradation.
"""
link, node1, node2 = await _cross_compute_link(project)
rounds = []
async def redispatch(links, downgrade):
rounds.append(downgrade)
return []
with patch.object(project, "_redispatch_vxlan_links", redispatch):
await project._heal_refused_vxlan_links([link])
assert rounds == [False]
+3
View File
@@ -27,6 +27,7 @@ Beyond a Linux kernel with the datapath features (bridges, veth/TAP devices, tc
* **`tc` (iproute2)** for the filter scenarios: their qdisc and classifier assertions read it from `PATH`. Without it those assertions cannot run.
* **A Docker daemon** for the Docker scenarios (the harness's own guest driver talks to the same daemon as the server; socket override: `GNS3_E2E_DOCKER_SOCKET`).
* **A C compiler that can link statically** (`cc -static`) for the raw-frame probe of the link-local scenario; without it that probe skips.
* **Root (or passwordless sudo) for the cross-compute scenario** — a second network stack on one machine is a network namespace, and Linux confines creating and entering one to CAP_SYS_ADMIN (uBridge's file capabilities do not help there). The scenario skips with the exact run command otherwise; it also writes the second compute's node files, which the harness's passwordless-sudo path cannot do as the test user.
* **Emulator images** — see the matrix below.
## Test images
@@ -93,3 +94,5 @@ Every kernel scenario also asserts the L2-anchor spec's server-side guarantees (
* `GNS3_E2E_DOCKER_SOCKET` — the Docker daemon socket the harness's guest driver talks to (default `/var/run/docker.sock` — the same daemon the server uses).
* `GNS3_E2E_IDLEPC` — skip idle-PC detection for the Dynamips scenarios and use the given value.
* `GNS3_E2E_DEBUG=1` — run the isolated instance with debug logging (the uBridge command stream lands in its `server.log`).
* `test_vxlan_cross_compute.py` — the cross-compute VXLAN datapath, with two computes on one machine: the isolated instance on the host and a second isolated instance inside a network namespace joined to the host by a veth pair (`harness.cross_compute_fabric` — two computes cannot tunnel from the host namespace, since the kernel keeps one VXLAN socket per (namespace, dstport) and the pair must share one VNI on it). One real Alpine container per compute, linked across the tunnel: both ends agree on the VNI and the shared dstport, each per-link bridge enslaves exactly `[anchor, tunnel]`, the tunnel device's MTU is the underlay minus 50 with the bridges inheriting it (a frame at it crosses, one past it is dropped at the bridge egress), dual-stack ICMP crosses, a `delay 100` filter shapes both directions, a stale device squatting on the link's deterministic name is replaced on node restart, a device squatting the link's VNI at node start self-heals (both ends re-wired on a fresh VNI, the start succeeding), link delete leaves nothing behind on either machine, and a project close/reopen rebuilds the tunnel through the batch path. **Needs root** (see host requirements) — run it with `sudo env GNS3_E2E_IMAGES=$HOME/GNS3/images pytest -m e2e tests/e2e/test_vxlan_cross_compute.py -s`.
+257 -16
View File
@@ -67,6 +67,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
import uuid
import pytest
from websockets.sync.client import connect as ws_connect
@@ -145,14 +146,15 @@ class Compute:
{"compute_id": "local", "name": name, "node_type": "dynamips", "properties": properties},
)
def create_docker_node(self, project_id, name, image, adapters=1):
def create_docker_node(self, project_id, name, image, adapters=1, compute_id="local"):
"""A standard Docker node (the veth datapath): the scenario addresses
its eth0 from the host and pings through it."""
its eth0 from the host and pings through it. *compute_id* places it
on a remote compute (the cross-compute VXLAN scenario's peer)."""
return self.call(
"POST",
f"/projects/{project_id}/nodes",
{
"compute_id": "local",
"compute_id": compute_id,
"name": name,
"node_type": "docker",
"properties": {"image": image, "adapters": adapters, "console_type": "telnet"},
@@ -295,10 +297,11 @@ default_admin_username = admin
default_admin_password = admin
[Server]
host = 127.0.0.1
host = {host}
port = {port}
ubridge_path = {ubridge}
enable_kernel_datapath = {kernel}
enable_vxlan_cross_compute = {vxlan}
report_errors = False
projects_path = {tmp}/projects
configs_path = {tmp}/configs
@@ -330,10 +333,14 @@ class Server:
instance (own config dir — the controller DB and every path follow it —
under a temporary directory removed on stop)."""
def __init__(self, compute, process=None, tmpdir=None):
def __init__(self, compute, process=None, tmpdir=None, privilege=()):
self.compute = compute
self.process = process
self.tmpdir = tmpdir
# Command prefix for cleaning up the temp directory when the process
# ran privileged (a compute spawned inside a netns as root writes
# root-owned files the test user cannot remove itself).
self.privilege = privilege
@property
def isolated(self):
@@ -361,6 +368,11 @@ class Server:
self._stop_process()
if self.tmpdir:
shutil.rmtree(self.tmpdir, ignore_errors=True)
if self.privilege and os.path.exists(self.tmpdir):
# S603: fixed argv (the privilege prefix is sudo -n or empty)
subprocess.run( # noqa: S603
[*self.privilege, "rm", "-rf", self.tmpdir], capture_output=True, check=False
)
def restart(self, kernel=True):
"""Restart an isolated instance in place: stop the process — the
@@ -376,10 +388,15 @@ class Server:
return self
def _spawn_isolated(tmpdir, kernel):
def _spawn_isolated(tmpdir, kernel, host="127.0.0.1", vxlan=False, netns=None, privilege=()):
"""Start one isolated instance inside *tmpdir* (its config is written
there; a fresh free port each boot) and return ``(process, compute)``,
logged in. Shared by the first boot and by ``Server.restart``."""
logged in. Shared by the first boot and by ``Server.restart``.
With *netns* the instance is spawned inside that network namespace
(through *privilege*, e.g. ``sudo -n``) and binds *host* — the pattern
the cross-compute VXLAN scenario uses for its second "machine".
"""
ubridge = shutil.which("ubridge")
dynamips = shutil.which("dynamips")
if not ubridge:
@@ -402,7 +419,15 @@ def _spawn_isolated(tmpdir, kernel):
fork_build = os.path.expanduser("~/myCode/GNS3/vpcs/src/vpcs")
vpcs = fork_build if os.path.isfile(fork_build) else (shutil.which("vpcs") or "vpcs")
config = ISOLATED_CONFIG.format(
port=port, ubridge=ubridge, dynamips=dynamips, images=images, tmp=tmpdir, kernel=kernel, vpcs=vpcs
port=port,
ubridge=ubridge,
dynamips=dynamips,
images=images,
tmp=tmpdir,
kernel=kernel,
vpcs=vpcs,
vxlan=vxlan,
host=host,
)
config_path = os.path.join(tmpdir, "gns3_server.conf")
with open(config_path, "w") as f:
@@ -410,7 +435,15 @@ def _spawn_isolated(tmpdir, kernel):
log = open(os.path.join(tmpdir, "server.log"), "a")
# S603: fixed argv, this interpreter, no shell involvement
argv = [sys.executable, "-m", "gns3server", "--config", config_path]
argv = [
*privilege,
*(["ip", "netns", "exec", netns] if netns else []),
sys.executable,
"-m",
"gns3server",
"--config",
config_path,
]
if os.environ.get("GNS3_E2E_DEBUG"):
argv.append("-d") # debug logging — the uBridge command stream lands in server.log
process = subprocess.Popen( # noqa: S603
@@ -420,13 +453,13 @@ def _spawn_isolated(tmpdir, kernel):
stderr=subprocess.STDOUT,
)
process.log = log # keep the file object alive with the process handle
compute = Compute(f"http://127.0.0.1:{port}/v3")
compute = Compute(f"http://{host}:{port}/v3")
deadline = time.time() + 60
while time.time() < deadline:
try:
# S310: the port is ours, the host loopback
with urllib.request.urlopen(f"http://127.0.0.1:{port}/v3/version", timeout=2):
# S310: the address is ours (loopback or the scenario's veth fabric)
with urllib.request.urlopen(f"http://{host}:{port}/v3/version", timeout=2):
break
except Exception:
if process.poll() is not None:
@@ -446,13 +479,13 @@ def _spawn_isolated(tmpdir, kernel):
return process, compute
def _start_isolated(kernel):
def _start_isolated(kernel, vxlan=False):
tmpdir = tempfile.mkdtemp(prefix="gns3-e2e-")
process, compute = _spawn_isolated(tmpdir, kernel)
process, compute = _spawn_isolated(tmpdir, kernel, vxlan=vxlan)
return Server(compute, process, tmpdir)
def live_server(kernel=True):
def live_server(kernel=True, vxlan=False):
"""The server under test.
With ``GNS3_E2E_URL`` set, that server is used for kernel mode (it must
@@ -475,7 +508,7 @@ def live_server(kernel=True):
).login()
server = Server(compute)
else:
server = _start_isolated(kernel)
server = _start_isolated(kernel, vxlan=vxlan)
if not wait_until(lambda: (server.compute.capabilities() or {}).get("version"), timeout=45):
raise AssertionError(
f"{server.compute.url}: the controller's local compute did not report capabilities within 45s"
@@ -1505,3 +1538,211 @@ def pcap_icmp_rtt(path, ethertype):
deltas.append((ts - pending) * 1000)
pending = None
return sum(deltas) / len(deltas) if deltas else None
# ---------------------------------------------------------------------------
# Cross-compute fabric: a second "machine" for VXLAN links
# ---------------------------------------------------------------------------
# The /24 both ends of the veth "fabric" are addressed in. RFC 2544's
# benchmarking range: never routed on a real network, so it cannot collide
# with anything the host legitimately talks to.
FABRIC_SUBNET_PREFIX = "198.18.7"
def fabric_privilege():
"""The command prefix for network-namespace operations: nothing when the
suite runs as root, ``sudo -n`` when passwordless sudo is configured,
None otherwise (the caller skips with the explanation).
A second network stack — the thing a cross-compute VXLAN link needs on
a single machine — is a network namespace, and Linux confines both
creating one and entering one to CAP_SYS_ADMIN of its owning user
namespace. uBridge's file capabilities do not help here (they are caps
for netlink *operations*, not for joining a namespace), so either the
whole run is root or sudo carries the two calls.
"""
if os.geteuid() == 0:
return []
sudo = shutil.which("sudo")
# S603: resolved binary, fixed argv, no shell
if sudo and subprocess.run([sudo, "-n", "true"], capture_output=True, check=False).returncode == 0: # noqa: S603
return [sudo, "-n"]
return None
class CrossComputeFabric:
"""A second "machine" on one host: a network namespace joined to the
host by a veth pair, both ends addressed inside ``198.18.7.0/24``.
Two computes on one machine cannot tunnel to each other from the host
namespace: the kernel keeps one VXLAN socket per (namespace, dstport)
and both ends must register the same VNI on it — the pair collides by
design. Distinct network namespaces give the pair its two sockets, and
the veth pair is the L2 fabric the tunnels ride. uBridge's file
capabilities make the *devices* work inside the namespace; only the
namespace itself needs privilege (see ``fabric_privilege``).
"""
def __init__(self, netns, host_ifc, peer_ifc, host_ip, peer_ip, prefix):
self.netns = netns
self.host_ifc = host_ifc
self.peer_ifc = peer_ifc
self.host_ip = host_ip
self.peer_ip = peer_ip
self.prefix = prefix
def netns_run(self, *argv, **kwargs):
"""Run a command inside the namespace (privileged, like every netns
access)."""
kwargs.setdefault("capture_output", True)
kwargs.setdefault("text", True)
# S603: fixed argv (the caller builds the netns-side command)
return subprocess.run( # noqa: S603
[*self.prefix, "ip", "netns", "exec", self.netns, *argv], check=False, **kwargs
)
def iface_mtu(self, name):
"""The MTU of an interface inside the namespace (None when absent)."""
out = self.netns_run("ip", "-o", "link", "show", name).stdout
match = re.search(r"\bmtu (\d+)", out)
return int(match.group(1)) if match else None
def iface_exists(self, name):
return self.netns_run("ip", "link", "show", name).returncode == 0
def vxlan_show(self, name):
"""``ip -d link show`` details of an interface inside the namespace,
as one string (None when absent) — carries the ``vxlan id <vni>
... dstport <port>`` attributes."""
result = self.netns_run("ip", "-d", "link", "show", name)
return result.stdout if result.returncode == 0 and name in result.stdout else None
def bridge_members(self, bridge):
"""Interfaces enslaved to a bridge inside the namespace, or None
when the bridge does not exist."""
result = self.netns_run("ls", f"/sys/class/net/{bridge}/brif")
if result.returncode != 0:
return None
return sorted(result.stdout.split())
def brport_state(self, name):
"""An interface's bridge-port STP state inside the namespace (None
when it is not enslaved; the kernel reports 3 for forwarding)."""
result = self.netns_run("cat", f"/sys/class/net/{name}/brport/state")
if result.returncode != 0:
return None
return result.stdout.strip() or None
def teardown(self):
"""Delete the namespace (all its devices go with it) and any host
end a half-created pair left behind."""
# S603: fixed argv, resolved ip binary, no shell
subprocess.run( # noqa: S603
[*self.prefix, "ip", "netns", "del", self.netns], capture_output=True, check=False
)
subprocess.run( # noqa: S603
[*self.prefix, "ip", "link", "del", self.host_ifc], capture_output=True, check=False
)
def cross_compute_fabric():
"""Create the fabric, or skip the calling test when this host cannot.
The names carry a random suffix so concurrent runs (and leftovers of a
crashed one) cannot collide; the caller must call ``teardown()`` in a
finally."""
prefix = fabric_privilege()
if prefix is None:
pytest.skip(
"cross-compute VXLAN needs a second network namespace: run the suite as root, e.g. "
"`sudo env GNS3_E2E_IMAGES=$HOME/GNS3/images pytest -m e2e "
"tests/e2e/test_vxlan_cross_compute.py -s`, or configure passwordless sudo"
)
suffix = uuid.uuid4().hex[:6]
fabric = CrossComputeFabric(
netns=f"gns3vx{suffix}",
host_ifc=f"vxh{suffix}",
peer_ifc=f"vxp{suffix}",
host_ip=f"{FABRIC_SUBNET_PREFIX}.1",
peer_ip=f"{FABRIC_SUBNET_PREFIX}.2",
prefix=prefix,
)
try:
# S603: fixed argv, resolved ip binary, no shell
subprocess.run( # noqa: S603
[*prefix, "ip", "netns", "add", fabric.netns], check=True, capture_output=True
)
subprocess.run( # noqa: S603
[*prefix, "ip", "link", "add", fabric.host_ifc, "type", "veth", "peer", "name", fabric.peer_ifc],
check=True,
capture_output=True,
)
subprocess.run( # noqa: S603
[*prefix, "ip", "link", "set", fabric.peer_ifc, "netns", fabric.netns], check=True, capture_output=True
)
subprocess.run( # noqa: S603
[*prefix, "ip", "addr", "add", f"{fabric.host_ip}/24", "dev", fabric.host_ifc],
check=True,
capture_output=True,
)
subprocess.run( # noqa: S603
[*prefix, "ip", "link", "set", fabric.host_ifc, "up"], check=True, capture_output=True
)
for argv in (
("ip", "addr", "add", f"{fabric.peer_ip}/24", "dev", fabric.peer_ifc),
("ip", "link", "set", fabric.peer_ifc, "up"),
("ip", "link", "set", "lo", "up"),
):
fabric.netns_run(*argv, check=True)
except subprocess.CalledProcessError:
fabric.teardown()
raise
return fabric
def start_isolated_peer(fabric, kernel=True):
"""An isolated instance on the other side of the fabric: spawned inside
the namespace and bound to its address, so the first server can register
it as a remote compute whose network stack is the second "machine"."""
tmpdir = tempfile.mkdtemp(prefix="gns3-e2e-peer-")
process, compute = _spawn_isolated(tmpdir, kernel, host=fabric.peer_ip, netns=fabric.netns, privilege=fabric.prefix)
return Server(compute, process, tmpdir, privilege=fabric.prefix)
def register_remote_compute(server, peer, name="vxlan-peer"):
"""Register the peer's server on the first server's controller, so links
between nodes on the two computes are cross-compute links. Waits until
the controller connected it (capabilities reported — the vxlan probe in
particular: the controller gates tunnel eligibility on it)."""
compute = server.compute.call(
"POST",
"/computes",
{
"name": name,
"protocol": "http",
"host": peer.compute.url.split("://", 1)[1].split(":", 1)[0],
"port": int(peer.compute.url.rsplit(":", 1)[1].split("/", 1)[0]),
"user": peer.compute.username,
"password": peer.compute.password,
},
)
compute_id = compute["compute_id"]
if not wait_until(lambda: (server.compute.call("GET", f"/computes/{compute_id}") or {}).get("version"), timeout=60):
raise AssertionError(f"controller did not connect the remote compute {compute_id} within 60s")
if not wait_until(
lambda: (
((server.compute.call("GET", f"/computes/{compute_id}") or {}).get("capabilities") or {}).get(
"ubridge_vxlan"
)
is True
),
timeout=60,
):
raise AssertionError(f"remote compute {compute_id} never reported ubridge_vxlan (its netns probe failed)")
return compute_id
+305
View File
@@ -0,0 +1,305 @@
#
# Copyright (C) 2026 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Live end-to-end test for cross-compute VXLAN links (pytest marker ``e2e``):
two computes on one machine — the harness's isolated instance on the host and
a second isolated instance inside a network namespace joined to the host by a
veth pair (``harness.cross_compute_fabric``) — with one real Alpine container
per compute, linked across the tunnel.
Two computes on one machine cannot tunnel from the host namespace: the kernel
keeps one VXLAN socket per (namespace, dstport), and both ends of a link must
register the same VNI on it, so the pair collides by design (the controller
falls back to the relay for same-host pairs). Separate network namespaces
give the pair its two sockets, and the veth pair is the L2 fabric the tunnels
ride — the closest a single machine gets to two hosts. The same-host
fallback itself is asserted by the controller unit tests; this scenario is
the real-kernel proof of the tunnel.
Needs root: a network namespace is privileged to create and to enter, and the
second compute's node files (its project directories) are written by the
test. Run it as::
sudo env GNS3_E2E_IMAGES=$HOME/GNS3/images pytest -m e2e \\
tests/e2e/test_vxlan_cross_compute.py -s
What it asserts:
* both ends report the same VNI on the shared dstport, each pointing at the
other machine's address, and each per-link bridge enslaves exactly
``[anchor, tunnel]``;
* the tunnel device's MTU is the underlay MTU minus 50 (the kernel derives
it from the lower device the peer route resolves to), the bridges inherit
it, and a frame past it is dropped at the bridge egress while one at it
crosses — the reason the datapath is opt-in on non-jumbo fabrics;
* real dual-stack ICMP crosses, a ``delay 100`` filter shapes it (one end
per direction), link delete leaves no device or bridge on either machine;
* a stale tunnel device squatting on the link's deterministic name is
replaced by the attach path, and the tunnel recovers;
* a device squatting the link's VNI at node start self-heals: the start
re-wires the link on a fresh VNI — the peer re-wired too — and succeeds.
"""
import os
import re
import subprocess
import time
import pytest
from gns3server.utils.vxlan import VXLAN_VNI_MAX, VXLAN_VNI_MIN, vxlan_device_name
from tests.e2e import harness
pytestmark = pytest.mark.e2e
# every standard Docker adapter has a single port
ETH = (0, 0)
G1_IP = "10.66.1.1"
G2_IP = "10.66.1.2"
# the dual-stack half: a ULA mirroring the v4 addressing
G1_IP6 = "fd00:66:1::1"
G2_IP6 = "fd00:66:1::2"
# ICMP payloads against the derived tunnel MTU (underlay 1500 -> tunnel
# 1450): 1400 + 42 bytes of headers is a 1442-byte frame, under the bridge
# egress gate; 1500 + 42 is a 1542-byte frame, over it — and over the
# guest's own 1500 MTU too, so the IP stack may fragment it; either way the
# pieces cannot cross the 1450-wide egress and the ping must fail silently.
FIT_PAYLOAD = 1400
TOO_BIG_PAYLOAD = 1500
def _ip(*argv):
"""Run an iproute2 command on the host (the scenario runs as root)."""
ip = harness.IP
assert ip, "ip binary not found"
# S603: resolved binary, fixed argv from the test itself
return subprocess.run([ip, *argv], capture_output=True, text=True, check=True) # noqa: S603
def _host_vxlan_attrs(name):
"""(vni, dstport, remote) of a host-side tunnel device, from
``ip -d link show``."""
details = _ip("-d", "link", "show", name).stdout
vni = re.search(r"\bvxlan id (\d+)", details)
dstport = re.search(r"\bdstport (\d+)", details)
remote = re.search(r"\bremote (\S+)", details)
assert vni and dstport and remote, details
return int(vni.group(1)), int(dstport.group(1)), remote.group(1)
def _peer_vxlan_attrs(fabric, name):
details = fabric.vxlan_show(name)
assert details, f"tunnel device {name} missing inside {fabric.netns}"
vni = re.search(r"\bvxlan id (\d+)", details)
dstport = re.search(r"\bdstport (\d+)", details)
remote = re.search(r"\bremote (\S+)", details)
assert vni and dstport and remote, details
return int(vni.group(1)), int(dstport.group(1)), remote.group(1)
def _container_id(daemon, node):
container_id = daemon.container_id(f"GNS3.{node['name']}.{node['project_id']}")
assert container_id, f"container for {node['name']} not found on the Docker daemon"
node["container_id"] = container_id
return container_id
def _start_pair(compute, pid, daemon, n1, n2):
"""Start both containers and wait for their userspace to be reachable:
the v4 address from the interfaces file (init.sh), the v6 twin from the
harness (the daemon's disable_ipv6 on bridge networks makes the
interfaces-file route fail). The container ids are re-resolved after the
starts: a project reopen can replace a stale container with a fresh one
under the same deterministic name (a new id)."""
for node in (n1, n2):
compute.call("POST", f"/projects/{pid}/nodes/{node['node_id']}/start")
_container_id(daemon, node)
for node, address in ((n1, G1_IP), (n2, G2_IP)):
harness.docker_wait_address(daemon, node["container_id"], address)
for node, address6 in ((n1, G1_IP6), (n2, G2_IP6)):
harness.docker_bring_up_ipv6(daemon, node["container_id"], address6)
def test_vxlan_cross_compute_link():
if os.geteuid() != 0:
pytest.skip(
"cross-compute VXLAN needs a second network stack (a netns) and writes the second "
"compute's node files: run as root, e.g. `sudo env GNS3_E2E_IMAGES=$HOME/GNS3/images "
"pytest -m e2e tests/e2e/test_vxlan_cross_compute.py -s`"
)
fabric = harness.cross_compute_fabric()
server = harness.live_server(kernel=True, vxlan=True)
peer = harness.start_isolated_peer(fabric)
compute = server.compute
daemon = harness.DockerDaemon()
failed = True
pid = None
try:
peer_id = harness.register_remote_compute(server, peer)
image = harness.ensure_docker_image(server)
project = compute.create_project("vxlan-e2e")
pid = project["project_id"]
print(".. creating one container per compute")
n1 = compute.create_docker_node(pid, "VXE1", image)
harness.configure_docker_interfaces(n1, G1_IP)
_container_id(daemon, n1)
n2 = compute.create_docker_node(pid, "VXE2", image, compute_id=peer_id)
harness.configure_docker_interfaces(n2, G2_IP)
_container_id(daemon, n2)
assert n1["compute_id"] == "local", n1["compute_id"]
assert n2["compute_id"] == peer_id, n2["compute_id"]
print(".. linking them across the computes")
link = compute.create_link(pid, (n1["node_id"], *ETH), (n2["node_id"], *ETH))
lid = link["link_id"]
assert link["kernel_datapath"] is True, link
bridge = harness.link_bridge_name(lid)
a1 = harness.docker_anchor_name(n1["node_id"], *ETH)
a2 = harness.docker_anchor_name(n2["node_id"], *ETH)
vx1 = vxlan_device_name(lid, n1["node_id"])
vx2 = vxlan_device_name(lid, n2["node_id"])
assert not harness.tap_exists(vx1), "the tunnel lands with node start"
print(".. starting the containers (the wiring lands with node start)")
_start_pair(compute, pid, daemon, n1, n2)
print(".. asserting the tunnel on both machines")
assert harness.tap_exists(vx1), f"tunnel device {vx1} missing on the host"
assert harness.bridge_members(bridge) == sorted([a1, vx1]), harness.bridge_members(bridge)
assert fabric.bridge_members(bridge) == sorted([a2, vx2]), fabric.bridge_members(bridge)
vni1, port1, remote1 = _host_vxlan_attrs(vx1)
vni2, port2, remote2 = _peer_vxlan_attrs(fabric, vx2)
assert vni1 == vni2, (vni1, vni2)
assert VXLAN_VNI_MIN <= vni1 <= VXLAN_VNI_MAX, vni1
assert port1 == port2, (port1, port2)
assert remote1 == fabric.peer_ip, (remote1, fabric.peer_ip)
assert remote2 == fabric.host_ip, (remote2, fabric.host_ip)
# MTU: the kernel derives the tunnel device's MTU from the lower
# device the peer route resolves to (underlay - 50 for the outer
# headers), and the per-link bridge — no MTU of its own — reports
# the minimum of its ports, i.e. the honest "what this fabric can
# carry".
underlay_mtu = int(harness.sysfs(f"/sys/class/net/{fabric.host_ifc}/mtu"))
assert underlay_mtu == 1500, underlay_mtu # the veth default; the derived MTU follows from it
assert int(harness.sysfs(f"/sys/class/net/{vx1}/mtu")) == underlay_mtu - 50
assert int(harness.sysfs(f"/sys/class/net/{bridge}/mtu")) == underlay_mtu - 50
assert fabric.iface_mtu(vx2) == underlay_mtu - 50
assert fabric.iface_mtu(bridge) == underlay_mtu - 50
assert fabric.iface_mtu(fabric.peer_ifc) == underlay_mtu
# The tunnel device is an L2 anchor like any other: hardened
# (lenient — the shared with plain `ip` in the doc's example) and
# FORWARDING on both bridges.
if harness.l2only_supported():
harness.assert_pure_l2(vx1)
harness.assert_forwarding(vx1)
assert fabric.brport_state(vx2) == "3", fabric.brport_state(vx2)
print(".. pinging across the tunnel")
ping = harness.docker_wait_ping(daemon, n1["container_id"], G2_IP)
assert ping["loss"] == 0, ping["raw"]
assert ping["avg"] is not None and ping["avg"] < 50, ping
ping6 = harness.docker_wait_ping(daemon, n1["container_id"], G2_IP6)
assert ping6["loss"] == 0, ping6["raw"]
# The derived MTU is the hard limit: a frame at it crosses, one past
# it is dropped at the bridge egress — silently, which is why the
# deployment has to size the underlay for the frames its guests send.
fit = harness.docker_ping(daemon, n1["container_id"], G2_IP, size=FIT_PAYLOAD)
assert fit["loss"] == 0, fit["raw"]
too_big = harness.docker_ping(daemon, n1["container_id"], G2_IP, count=2, size=TOO_BIG_PAYLOAD)
assert too_big["loss"] > 0 or too_big["exit"] != 0, too_big["raw"]
print(".. delay 100 shapes the tunnel on both ends (one direction each)")
compute.call("PUT", f"/projects/{pid}/links/{lid}", {"filters": {"delay": [100]}})
time.sleep(1)
delayed = harness.docker_ping(daemon, n1["container_id"], G2_IP, count=5, interval=None, timeout=2)
assert delayed["loss"] == 0, delayed["raw"]
assert 150 <= (delayed["avg"] or 0) <= 450, delayed
compute.call("PUT", f"/projects/{pid}/links/{lid}", {"filters": {}})
print(".. a stale device on the link's name is replaced on node restart")
compute.call("POST", f"/projects/{pid}/nodes/{n1['node_id']}/stop")
_ip("link", "del", vx1)
# A stale tunnel: same deterministic name, some other VNI/port —
# what a crashed run or a hand-made device would leave behind.
_ip("link", "add", vx1, "type", "vxlan", "id", "4242", "dstport", "47999", "remote", "127.0.0.1")
compute.call("POST", f"/projects/{pid}/nodes/{n1['node_id']}/start")
harness.docker_wait_address(daemon, n1["container_id"], G1_IP)
harness.docker_bring_up_ipv6(daemon, n1["container_id"], G1_IP6)
stale_vni, stale_port, stale_remote = _host_vxlan_attrs(vx1)
assert (stale_vni, stale_port, stale_remote) == (vni1, port1, remote1), (stale_vni, stale_port, stale_remote)
assert harness.bridge_members(bridge) == sorted([a1, vx1]), harness.bridge_members(bridge)
assert int(harness.sysfs(f"/sys/class/net/{bridge}/mtu")) == underlay_mtu - 50
assert harness.docker_wait_ping(daemon, n1["container_id"], G2_IP, attempts=5)["loss"] == 0
print(".. a VNI squatted at node start self-heals on a fresh VNI")
compute.call("POST", f"/projects/{pid}/nodes/{n1['node_id']}/stop")
# A device holding the link's exact VNI on the shared dstport socket
# — what a colliding link (or an operator) would have registered.
# The node's start replays the stored NIO, the kernel refuses the
# VNI, and the controller must re-wire the link on a fresh one and
# retry the start instead of failing it.
_ip("link", "add", "gvxsquat", "type", "vxlan", "id", str(vni1), "dstport", str(port1), "remote", "127.0.0.1")
try:
compute.call("POST", f"/projects/{pid}/nodes/{n1['node_id']}/start")
harness.docker_wait_address(daemon, n1["container_id"], G1_IP)
harness.docker_bring_up_ipv6(daemon, n1["container_id"], G1_IP6)
healed_vni, healed_port, healed_remote = _host_vxlan_attrs(vx1)
assert healed_vni != vni1, (healed_vni, vni1)
assert VXLAN_VNI_MIN <= healed_vni <= VXLAN_VNI_MAX, healed_vni
assert healed_port == port1, (healed_port, port1)
assert healed_remote == remote1, (healed_remote, remote1)
# the peer was re-wired to the fresh VNI too — the pair must agree
peer_vni, _, _ = _peer_vxlan_attrs(fabric, vx2)
assert peer_vni == healed_vni, (peer_vni, healed_vni)
assert harness.docker_wait_ping(daemon, n1["container_id"], G2_IP, attempts=5)["loss"] == 0
finally:
_ip("link", "del", "gvxsquat")
print(".. deleting the link leaves no tunnel or bridge behind on either machine")
compute.call("DELETE", f"/projects/{pid}/links/{lid}")
assert not harness.tap_exists(vx1)
assert not harness.tap_exists(bridge)
assert not fabric.iface_exists(vx2), f"tunnel device {vx2} survived on the peer"
assert fabric.bridge_members(bridge) is None
print(".. closing and reopening the project rebuilds the tunnel through the batch path")
compute.call("POST", f"/projects/{pid}/close")
compute.call("POST", f"/projects/{pid}/open")
_start_pair(compute, pid, daemon, n1, n2)
assert harness.tap_exists(vx1), f"tunnel device {vx1} not rebuilt on reopen"
assert fabric.iface_exists(vx2), f"tunnel device {vx2} not rebuilt on reopen"
assert harness.docker_wait_ping(daemon, n1["container_id"], G2_IP, attempts=5)["loss"] == 0
failed = False
finally:
keep = failed and harness.keep_on_failure()
if not keep:
peer.stop()
if pid is not None:
harness.release(server, pid, failed=failed)
else:
server.stop()
if keep:
print(f"\n!! kept the fabric {fabric.netns} (and its peer compute) for inspection")
else:
fabric.teardown()
+71
View File
@@ -0,0 +1,71 @@
#
# Copyright (C) 2026 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Cross-compute VXLAN link identity: the derived VNI and device name.
"""
import uuid
from gns3server.utils.vxlan import VXLAN_NAME_PREFIX, VXLAN_VNI_MAX, VXLAN_VNI_MIN, derive_vni, vxlan_device_name
def test_derive_vni_is_deterministic_and_bounded():
link_id = str(uuid.uuid4())
vni = derive_vni(link_id)
assert derive_vni(link_id) == vni
assert VXLAN_VNI_MIN <= vni <= VXLAN_VNI_MAX
def test_derive_vni_salt_moves_the_value():
link_id = str(uuid.uuid4())
values = {derive_vni(link_id, salt) for salt in range(16)}
# Every salt lands in range; a bumped salt is a fresh point of the space
# (a collision here would defeat the retry, and 16 draws out of ~16.8M
# values collapsing is a hash failure, not bad luck).
assert all(VXLAN_VNI_MIN <= v <= VXLAN_VNI_MAX for v in values)
assert len(values) == 16
def test_derive_vni_avoids_low_well_known_vnis():
# Flannel starts at 1, Docker overlay networks at 256: derived links
# must never land in the low range whatever the link id.
for _ in range(200):
assert derive_vni(str(uuid.uuid4()), uuid.uuid4().int) >= VXLAN_VNI_MIN
def test_vxlan_device_name_deterministic_and_within_ifnamsiz():
link_id = str(uuid.uuid4())
node_id = str(uuid.uuid4())
name = vxlan_device_name(link_id, node_id)
assert name == vxlan_device_name(link_id, node_id)
assert name.startswith(VXLAN_NAME_PREFIX)
# IFNAMSIZ (16) includes the NUL terminator: the kernel accepts 15 chars.
assert len(name) <= 15
def test_vxlan_device_name_unique_per_endpoint_and_link():
link1 = str(uuid.uuid4())
link2 = str(uuid.uuid4())
node1 = str(uuid.uuid4())
node2 = str(uuid.uuid4())
names = {
vxlan_device_name(link1, node1),
vxlan_device_name(link1, node2),
vxlan_device_name(link2, node1),
}
assert len(names) == 3