gns3-server/gns3server/utils/kernel_anchor.py
YueGuobin 90bcb32473
feat(esw): cascade two switches through a veth pair
Switch-to-switch links were the last shape left on the relay: the switch
fast path works by absorbing the *peer's* anchor, and a switch has none —
its ports are its own bridge, and a kernel interface belongs to exactly
one bridge, so two bridges cannot be joined by enslaving anything.

Join them with a veth pair instead: one end per bridge, each side
enslaving its own end and applying its own port mode to it — an access
port on one side and a trunk on the other behaves like the real cable it
models. uBridge's docker create_veth/delete_veth (the primitive every
Docker adapter is already born from) creates and destroys the pair; the
names derive from the link id (utils.kernel_anchor.kernel_cascade_names),
so each side computes its own end and the names survive a restart.

The pair belongs to the link, and the two ends are wired by two
independent uBridge processes — the project-open batch drives different
nodes concurrently — so the protocol is order-free: both sides create the
pair when their own end is missing (one create wins; the loser's failure
is re-checked against the kernel and "the pair is there" is not an
error), and either side's teardown destroys it (deleting one veth end
takes the other with it, so both converge on the same gone pair and the
second delif answers ENOENT). A pre-existing end — this switch restarted,
or a leftover from a previous life of the link id — is reused rather than
recreated, because the other end may still be a live port of the peer
switch; a stale tc qdisc is all a reused end can carry.

No new wire format was needed: a cascade is an anchor NIO on both ends,
the absorbed-anchor case plus the peer end's name. That also corrects the
filter routing — the single-sided rule exists because an absorbed anchor
is one interface, while a cascade has one per switch, so impairments go
to both ends like any other kernel link.

Tests: the wire format, the eligibility flip and the both-ends re-push on
node start (controller); the naming contract (utils); the switch-side
lifecycle — create, reuse, lost create race, delete destroys the pair
(compute routes).

Drive-by: the relay filter test's compute mock carried no awaitable
`put`, so tests/controller/test_kernel_datapath_link.py failed when run
alone while an argument-less full run passed; callers now get one.
2026-10-06 00:21:09 +08:00

85 lines
3.1 KiB
Python

#
# 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/>.
"""
The kernel-datapath anchor naming contract, in one place.
Every node type whose adapters own a host-side interface names that anchor
deterministically: ``g<type>{node_id[:8]}e{adapter}p{port}`` — ``gv`` for a
Docker veth host end, ``gq`` for a QEMU TAP, ``gi`` for an IOU port TAP,
``gd`` for a Dynamips port TAP. The prefixes keep anchors out of the
``gns3`` bridge/TAP name space and name-collision-free on a host; the 8
hex chars of the node id plus adapter/port keep every name unique and
within IFNAMSIZ (15).
The compute classes create their anchors with these helpers (single source
of truth), and so does the controller when a switch link has to name the
peer's anchor — the name is a pure function of (node type, node id,
adapter, port), valid whether or not the node is currently running, which
is what makes links to stopped nodes wireable.
"""
import logging
log = logging.getLogger(__name__)
# Node type -> the anchor prefix its interface names carry. ``gc`` (the
# Docker veth guest end) is container-local and deliberately absent.
ANCHOR_PREFIX = {
"docker": "gv",
"qemu": "gq",
"iou": "gi",
"dynamips": "gd",
}
def anchor_suffix(node_id, adapter_number, port_number=0):
"""
The shared ``{node_id[:8]}e{adapter}p{port}`` name stem (8 hex chars of
the node id, no dashes).
"""
return f"{str(node_id).replace('-', '')[:8]}e{adapter_number}p{port_number}"
def kernel_anchor_name(node_type, node_id, adapter_number, port_number=0):
"""
The host-side kernel-datapath anchor of one adapter port, or None for a
node type without one (the anchor-less types never anchor a kernel
link).
"""
prefix = ANCHOR_PREFIX.get(node_type)
if prefix is None:
return None
return prefix + anchor_suffix(node_id, adapter_number, port_number)
def kernel_cascade_names(link_id):
"""
The two veth ends of a switch-to-switch cascade link, as
``(owner end, peer end)``. The pair belongs to the *link* (not to either
switch): both names are a pure function of the link id, so each side
computes its own end and the controller can hand each switch exactly
the name to enslave — the same deterministic-name trick per-link
bridges use. ``gs`` keeps the cascade ends out of the node-anchor and
``gns3`` name spaces; 10 hex chars of the link id plus the side index
stay within IFNAMSIZ (15).
"""
stem = str(link_id).replace("-", "")[:10]
return f"gs{stem}0", f"gs{stem}1"