Skip to content

guestnet: add a private userspace network for each VM guest - #20

Merged
bradfitz merged 10 commits into
mainfrom
bradfitz/guestnet_linux
Nov 20, 2026
Merged

bradfitz merged 10 commits into
mainfrom
bradfitz/guestnet_linux

Conversation

@bradfitz

Copy link
Copy Markdown
Member

CI guest VMs on Linux hosts (Firecracker and QEMU) and macOS hosts
currently get their networking from the host's own network stack:
per-slot bridges with iptables NAT and DNAT rules on Linux, and pf rules
on macOS. guestnet does it all in Go instead, the same way on every
platform. That makes our platforms more consistent, removes the need to
manage iptables and pf rules at all, and lets the guest networking code
be shared across platforms. It also gives us more options for how to run
hostlets in the future: a guest that needs no host network configuration
is much easier to run in, say, a Kubernetes pod on EKS, or under a VMM
other than Tart on macOS.

A guest's virtual NIC hands us raw Ethernet frames and they all
terminate in a gVisor netstack owned by the guest's Net, so a guest can
only reach what we connect it to, and can't see other guests:

  • The gateway, IMDS (169.254.169.254) and DNS (169.254.169.253)
    addresses are on our NIC, so gVisor answers ARP and ICMP for them,
    and the guest needs no route or DNS changes to reach them. ARP for
    any other address is dropped: the NIC is promiscuous, and answering
    the guest's DHCP duplicate-address probe makes it decline its lease.
  • DHCP is answered from the frame loop with one fixed lease, the same
    in every slot, so a VM memory snapshot restores anywhere.
  • Each TCP SYN and UDP flow is dispatched by destination to a
    registered Service (a guest-facing server, or a forward to a
    host-side address), to DNS (any port 53) or NTP (any port 123)
    interception, or to egress dialed from the host, subject to a deny
    list. Every flow and DNS query is logged.
  • The host reaches the guest only by DialGuest; there is no host
    route to it.

Frames arrive over one of two links:

  • A SOCK_DGRAM unix socketpair, for macOS's
    VZFileHandleNetworkDeviceAttachment and QEMU's "-netdev dgram".
  • On Linux, a TAPLink on the host side of a TAP device, for VMMs like
    Firecracker that only open a TAP by name. The TAP has no addresses
    and is on no bridge; an AF_PACKET socket reads and writes its
    frames. It uses PACKET_VNET_HDR because Firecracker enables
    checksum and segmentation offloads, so guest frames can carry a
    partial checksum, which TAPLink completes, or be one large TCP
    segment, which gVisor takes whole.

Egress UDP behaves like a typical home router, an "easy NAT" for NAT
traversal: each guest source address gets one host socket that all its
flows share, so it keeps one host address and port whatever the
destination (endpoint-independent mapping, RFC 4787), and datagrams
reach the guest only from remotes it has sent to (address- and
port-dependent filtering).

Egress is dialed from the host, so DenyLocal extends a deny list with
the host's own interface addresses, keeping guests away from host
listeners on non-loopback addresses.

guestnettest provides a fake guest stack for tests of code built on it.

Tested with a modified ciguestlet in the corp repo (separate PR),
running Linux guests on Firecracker and FreeBSD, Windows, and Plan 9
guests on QEMU. No testing has been done on macOS yet, but this is based
on code that was originally developed and tested on the macOS
guestlet/hostlet, so it probably still works there; that's not the goal
today.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

@bradfitz
bradfitz requested a review from a team November 18, 2026 03:38
@bradfitz
bradfitz force-pushed the bradfitz/guestnet_linux branch 2 times, most recently from 302300e to a7aa581 Compare November 18, 2026 16:20

@tomhjp tomhjp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting this up into digestible chunks. It helped to have related files for related changes more easily collected together, and I also learned a ton reviewing this.

Comment thread guestnet/guestnet_test.go Resolved
Comment thread guestnet/tap_linux.go Resolved
Comment thread guestnet/guestnet.go Resolved
Comment thread guestnet/guestnettest/guestnettest.go Resolved
Comment thread guestnet/tap_linux.go Resolved
Comment thread guestnet/link.go Resolved
Comment thread guestnet/tap_linux.go Resolved
Comment thread guestnet/tap_linux.go Resolved
Comment thread guestnet/dns.go Resolved
Comment thread guestnet/tcp.go Resolved
Comment thread go.mod Resolved
@bradfitz
bradfitz force-pushed the bradfitz/guestnet_linux branch from a7aa581 to 11b299a Compare November 20, 2026 13:54
This starts guestnet, a private userspace network for each VM guest,
which a following commit builds on these. A FrameLink carries raw
Ethernet frames, one per call, between a guest's virtual NIC and us.

  - FileLink is a FrameLink over a datagram socket held as an *os.File.
  - NewSocketPairLink makes a connected SOCK_DGRAM unix socketpair: one
    end for the VMM (macOS's VZFileHandleNetworkDeviceAttachment, or
    QEMU's "-netdev dgram"), and a FileLink for us.
  - MemLink is an in-memory pair for tests.

Its test comes with the network that uses it, in a following commit.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I9fae04170234d696ddf978ab286abe928b97a380
Log is a fixed-size ring of records with subscribers, for the guest
network's per-flow, per-DNS-query and notable event records. Slow
subscribers miss records rather than block the network.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I539a6e9dbc4b747ed66596e79879585514ca2587
CI guest VMs on Linux hosts (Firecracker and QEMU) and macOS hosts
currently get their networking from the host's own network stack:
per-slot bridges with iptables NAT and DNAT rules on Linux, and pf rules
on macOS. guestnet does it all in Go instead, the same way on every
platform. That makes our platforms more consistent, removes the need to
manage iptables and pf rules at all, and lets the guest networking code
be shared across platforms. It also gives us more options for how to run
hostlets in the future: a guest that needs no host network configuration
is much easier to run in, say, a Kubernetes pod on EKS, or under a VMM
other than Tart on macOS.

This is the core. A guest's frames all terminate in a gVisor netstack
owned by the guest's Net, so a guest can only reach what we connect it
to, and can't see other guests:

  - The gateway, IMDS (169.254.169.254) and DNS (169.254.169.253)
    addresses are on our NIC, so gVisor answers ARP and ICMP for them,
    and the guest needs no route changes to reach them. ARP for any
    other address is dropped: the NIC is promiscuous, and answering a
    guest's duplicate-address probe makes it decline its address.
  - Each TCP SYN is dispatched by destination to a registered Service (a
    guest-facing server, or a forward to a host-side address), or to
    egress dialed from the host, subject to a deny list (the tailnet
    CGNAT range, loopback and link-local by default). Every flow is
    logged.
  - The host reaches the guest only by DialGuest; there is no host route
    to it.

Every guest gets the same fixed addresses; each has its own L2 domain,
so they don't conflict. DHCP, UDP, DNS and NTP follow in later commits.

guestnettest provides a fake guest stack for tests of code built on it.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I5b359a459ae52218e97cef3b74ecc14a806e0cc7
DHCP is answered from the frame loop with one fixed lease, the same in
every slot, so a VM memory snapshot restores anywhere. A request for
some other address (a lease carried over from elsewhere) gets a NAK so
the client starts over and takes ours. Leased reports the first
acknowledged lease.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I1a4ff60af33705202083fb176ead34e6f14289f0
Each UDP flow (a guest source and destination pair) is dispatched like
TCP: to egress dialed from the host, subject to the deny list, relaying
datagrams until the flow is idle. Multicast and broadcast chatter is
dropped silently.

Each flow gets its own host socket here; the next commit changes that.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I37e4e11cfcb2900c4b4b63270b834136cb761b91
Giving each UDP flow its own host socket made guestnet a symmetric NAT:
one guest port got a different host port for every destination, which
makes NAT traversal (Tailscale's own included) much harder.

Instead, egress UDP now behaves like a typical home router, an "easy
NAT": each guest source address gets one host socket that all its flows
share, so it keeps one host address and port whatever the destination
(endpoint-independent mapping, RFC 4787), and datagrams reach the guest
only from remotes it has sent to (address- and port-dependent
filtering). Traffic either way keeps a flow alive.

Config.ListenPacket opens those sockets; Dialer is now for TCP only.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I49d3c19755407b47246d73aa6bba5db87cf04ac3
Guest DNS on any port 53, over UDP or TCP and to any address, is
answered by a Resolver rather than forwarded, and each query is logged.
The default UpstreamResolver forwards to public resolvers over UDP with
TCP fallback on truncation; StaticResolver is for tests.

A response too big for what the guest accepts over UDP (its EDNS0
payload size, or 512 bytes without EDNS0) is cut to its header and
question with the TC bit set, so the guest retries over TCP.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I7eb668b36f361da21835c259af8163d86cf58350
Guest NTP on any port 123 is answered with the host's time, so a guest
restored from a memory snapshot can fix its stale clock by syncing
against any server.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I2d78c6e36609e32e4161f65351e6ea5b98cb5be1
Egress is dialed from the host, so DenyLocal extends a deny list with
the host's own interface addresses, keeping guests away from host
listeners on non-loopback addresses (a service on the host's LAN or VPC
address, say).

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: Ia5d4862159747431c02abbffca42126c3b4f7cfd
Firecracker's only network backend is a TAP it opens by name, so it
can't be handed a socketpair, and a QEMU guest restored from a memory
snapshot taken with a TAP backend needs one again. TAPLink is a
FrameLink on the host side of such a TAP: the TAP has no addresses and
is on no bridge, and an AF_PACKET socket reads and writes its frames.

The host's network stack never handles guest traffic: NewTAPLink adds a
tc clsact qdisc to the TAP with a match-all drop filter on its ingress,
which drops every guest frame right after the AF_PACKET socket sees it
and before any protocol handler does. As defense in depth it also
disables IPv6, ARP replies and forwarding on the TAP, and it fails
rather than carrying on if it can't do any of that. It configures tc
over netlink, with github.com/tailscale/netlink, so no tc binary is
needed.

It uses PACKET_VNET_HDR because Firecracker enables checksum and
segmentation offloads, so guest frames can carry a partial checksum,
which TAPLink completes, or be one large TCP segment, which gVisor takes
whole. The virtio header is read and written with readv and writev, so
it costs no copy and doesn't eat into the frame buffer.

With this, guestnet was tested with a modified ciguestlet in the corp
repo (separate PR), running Linux guests on Firecracker and FreeBSD,
Windows, and Plan 9 guests on QEMU. No testing has been done on macOS
yet, but this is based on code that was originally developed and tested
on the macOS guestlet/hostlet, so it probably still works there; that's
not the goal today.

Updates tailscale/corp#39994
Updates tailscale/corp#51705

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: Ic3bb10b567c562fdbf33facd988687bb4e3f5aca
@bradfitz
bradfitz force-pushed the bradfitz/guestnet_linux branch from 11b299a to c23ffcb Compare November 20, 2026 14:48
@bradfitz
bradfitz merged commit d7eeb13 into main Nov 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants