Commit Graph
49 Commits
Author SHA1 Message Date
Brad Fitzpatrick 2ebb76b1af kernel/linux/src: scale to 50,000 peers, and rename peers atomically
For a 50,000-node tailnet, every peer operation must be O(1), and a peer
that rotates its key must keep its traffic. Measured in a lab VM with
up to 50,000 peers (tailscale.com's TestLeanScale), three things were
off, plus key rotation:

Deleting a device removed its peers one at a time, each waiting for an
RCU grace period (netif_napi_del) and three workqueue flushes: 6ms a
peer, 5 minutes for 50,000, on every tailscaled restart. Now all of a
device's peers wait together (peer_remove_dead_batch): 50,000 in
0.1-0.2s.

Each peer's route cache was per CPU, 40 bytes on each: 5KB a peer on a
128-CPU host, 250MB for 50,000 peers, most of which never send. It's now
allocated on a peer's first send. A peer is 2.1KB, whatever the CPUs.

The pubkey and index hash tables had 2^11 and 2^13 buckets, chains of
25 at 50,000 peers; now 2^16 and 2^17 (1.5MB a device).

And TSWGPEER_A_NEW_PUBLIC_KEY renames a peer: in one step, under
device_update_lock, it moves to its new key in the pubkey table, with
its static-static and cookie keys recomputed and its sessions cleared,
keeping its allowed IPs, endpoint and queues. Removing it and adding
it anew would drop its traffic in between.

At 50,000 peers, adding, removing, renaming and editing one take 26-83
microseconds, as at 500.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I2abc5369ce8321267b39df9d9dc137ef7dc040a7
2026-11-09 03:10:05 +00:00
Brad Fitzpatrick 867eaea830 kernel/linux/src: recruit fewer crypt workers, and go inline for slow peers
One 7 Gbps tunnel between 128-CPU hosts had its crypt work spread over
33 CPUs, each about 15% busy, mostly waking up and going back to sleep,
for up to 1.6x wireguard-go's CPU per GB: a worker was recruited
whenever more than one GRO packet's worth waited, which GRO's bursts
always did. Now one is recruited only when more than recruit_factor
(default 16) times that waits for each worker draining, capped at a
quarter of the ring, and from a compact set of CPUs after the caller's,
twice as many as were draining at once in the last 10ms, rather than
round robin over every CPU.

And for request/response traffic, the handoff to a worker is on each
response's critical path, so a local socket's bulk sends to a peer
sending under tx_inline_mbps (default 2000) are encrypted inline, and
rx_inline_mbps drops from 4000 to 1000, so faster peers' receives go
the parallel way sooner.

Between two c6in.32xlarge hosts on EC2, at EC2's per-flow limit, tswg
now uses 0.52-1.02x wireguard-go's CPU per GB (was up to 1.6x), and one
HTTP-like stream gets 3.0 Gbps to its 3.3 (was 2.5). Crawling 37 peers
goes from 76 to 89 Gbps; routing and serving are unchanged.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I777adf1eadb4f309c62111ed3b2695a816350c56
2026-11-08 23:20:47 +00:00
Brad Fitzpatrick e7d0021cac kernel/linux/DESIGN.md: describe the kernel interface in one place
The module's userspace interface was described piecemeal, across the
sections on the shared socket, roaming, events, peer restarts and
hybrid handshakes, and its module parameters only in the performance
notes. Add a "Kernel interface" reference section: the link kind, the
genetlink family and its permissions, every tswg device and peer
attribute and flag, the events group, the shared socket's
classification, the module parameters with their defaults, and what
versioning there is (none yet beyond rejecting unknown attributes).

Writing it found that the events group needs no privilege to join, so
any process in the namespace can watch peers' keys and activity; noted
as a TODO there.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I7084d45a7394b77eaf969171312147331c7010e4
2026-11-08 21:45:54 +00:00
Brad Fitzpatrick b74f56be78 kernel/linux/{src,DESIGN.md}: pick steering homes by load; record wireguard-go's best
Steering homes were the first CPU, from a point a peer's ID hashed to,
not receiving from the NIC or known to be overloaded. But a CPU learned
its own load only when it received from the NIC, which homes don't, so
busy homes looked idle, and peers piled onto a few. Now any CPU's load
comes from its idle time (get_cpu_idle_time_us, for any CPU), and a
peer's home is the least loaded of 8 spread from its hash, kept while
it isn't overloaded. With 37 peers on 11 of the NIC's 32 queues (this
boot's RSS key), uploads through the router go from 28 to 44 Gbps,
crawling from 42 to 51.

Also record wireguard-go's scaling with flow count across all the
scenarios, so as not to misrepresent it: 6-11 Gbps from 1 client to 37,
and 1 flow each to 64, best with fewer flows, using 3-6% of the host's
CPU (serialized, not CPU-bound), and unmoved by GOGC or GOMAXPROCS. And
what the sweep found against tswg: on one tunnel between big hosts, both
at EC2's per-flow limit, tswg uses up to 2x wireguard-go's CPU per GB;
and the NIC's random RSS key decides how many queues few peers get,
which swung tswg's results 2x between boots.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I0760b24dd84f246005a4bc7c43429a016287223d
2026-11-08 20:36:38 +00:00
Brad Fitzpatrick 96695d4464 kernel/linux/src: scale a big host serving and crawling many peers
A 128-CPU host on the tailnet, serving HTTP-like responses to 37 peers
or crawling them all at once, was held back by three things, each fixed
here:

Sending by combining: a peer's encrypted packets went out from its tx
worker, one work item on one CPU at a time, which on a NIC without UDP
segmentation offload (EC2's ENA) also segments them, so the CPUs the
tx workers landed on ran at 90% and more. Now the encrypt worker that
makes an entry ready sends what's ready, in order, itself, unless
another CPU is sending the peer's (a trylock), which then sends it.

Local sockets' lone packets (TCP's ACKs, small requests) are encrypted
and sent inline, when nothing of the peer's is on its way through the
workers to be overtaken: a crawler spent 7% of its CPU on the encrypt
ring's lock for its ACKs.

Steering: with more peers than NIC queues, RSS puts several peers' UDP
flows on some CPUs, and receiving inline left those at 100% and the
rest idle. When a CPU is over rx_steer_busy (default 90%) busy, by its
idle time, or its softirq work runs out of budget, its peers' packets
go to each peer's home CPU (one not receiving from a NIC, by hash)
through a lockless per-CPU list and that CPU's NAPI: RPS, per peer, for
our packets, when needed. A peer changes between inline, steered and the
parallel way only when the way it was going is empty, so as not to
reorder.

On EC2 (c6in.32xlarge, 32 NIC queues, MTU 1280): serving goes from
84-98 to 133-137 Gbps (plain TCP without a tunnel: 152; wireguard-go:
5.5), at 3.9 CPU-s/GB, and crawling from 41-51 to 80-83 (plain: 128;
wireguard-go: 8.5), where the NIC's busiest queues, with two or three
peers each, are at ENA's limit of about 680k packets per second.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I011ab98d61296b5beef172ae1581d463820179a5
2026-11-08 11:52:01 +00:00
Brad Fitzpatrick 925022517f kernel/linux/src: run forwarded packets and most received ones to completion
On a 128-CPU subnet router sending big responses to hundreds of flows,
75-83% of the router's CPU spun on the encrypt ring's consumer lock.
Upstream's design hands every packet to a crypt worker on another CPU,
through a ring all CPUs share, and back through the peer's in-order
queue to a third, so that one flow's packets can be encrypted in
parallel and still go out in order. Forwarded packets don't need that:
RSS already spreads flows over CPUs and keeps each in order on one.

So tswg_xmit_now encrypts and sends packets with no socket (forwarded)
right in wg_xmit, on the CPU they arrived on, as IPsec does: no queues,
rings or other CPUs. Local sockets' packets still go to the workers, so
that one flow is encrypted on several CPUs; much of a TCP sender's
sending is in softirq too, as ACKs arrive, and inlining it cost a
stream half its throughput. Module parameter tx_inline: 0 never, 1
forwarded (the default), 2 always.

And tswg_receive_now decrypts a peer's packets in encap_rcv, on the CPU
its UDP flow arrives on, delivering them through the device's per-CPU
GRO cells, as VXLAN does, rather than through the decrypt ring, a
worker, the peer's in-order queue and its NAPI. That's one CPU per peer,
which a fast one outruns (a stream: 12.2 -> 6.4 Gbps in the lab), so a
peer receiving more than rx_inline_mbps (default 4000) over 10ms goes
the parallel way for at least 100ms, coming back once that way has
delivered what it had, so that switching doesn't reorder.

On EC2's 128-CPU c6in router with 37 clients: HTTP-like fetches go from
37 to 69 Gbps, and 27 to 3.7 CPU-s/GB; uploads from 66 to 68 Gbps, and
6.8 to 3.1 CPU-s/GB; wireguard-go does 6.2 either way. The 32 CPUs
taking the NIC's interrupts are now the limit, which RPS lifts for
fetches to 78 Gbps. In the lab, one stream through a tunnel is
unchanged at 12 Gbps, and RTT under load is down from 1.5 to 1.1ms.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I8b1cbbed3c87a8e6a9a7cf8a91a2c18dcfb4e62a
2026-11-08 06:48:49 +00:00
Brad Fitzpatrick 830919f464 kernel/linux/DESIGN.md: record the subnet router's HTTP-like results, and its ring lock
With clients making requests and the backend answering with 1 MiB, so
that the router encrypts big responses, encryption straight from GSO
packets takes the 128-CPU router from 15-17 Gbps to 36-38 in 8 of 14
runs and 18-19 in the rest (wireguard-go: 6.2). Either way, most of the
router's CPU spins on the encrypt ring's consumer lock; record that, and
what didn't fix it, as a TODO.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I401e258680cc182e40bf456727e771f80b8c679f
2026-11-08 05:04:06 +00:00
Brad Fitzpatrick a444291254 kernel/linux/src: encrypt straight from GSO packets, rather than segmenting them
wg_xmit segmented GSO packets with skb_gso_segment(skb, 0), as upstream
does, which allocates a packet per segment and, with no features, also
copies and checksums each segment's payload into it, after which
tswg_encrypt_list copied it all again to encrypt it.

Now TCP and UDP GSO packets, from local sockets or made by GRO and being
forwarded, stay whole (tswg_gso_whole_ok), get a block of nonces, and
tswg_encrypt_list writes each segment's plaintext straight into its
pages: the headers, fixed up as TCP's, UDP's, IPv4's and IPv6's
segmentation would (tswg_copy_segment), and the segment's slice of the
payload, checksummed while it's copied. Every packet a GSO packet is
encrypted into gets its socket ownership, as segments would. Others
(untrusted, tunnels, frag lists, TX timestamps) are still segmented,
but with NETIF_F_SG | NETIF_F_HW_CSUM, so that segments share pages and
get their checksums in our copy.

In the lab, with 1 TCP stream, throughput goes from 10.5 to 12.2 Gbps
and the sender's CPU from 1.82 to 1.21 CPU-s/GB (wireguard-go: 9.8
Gbps, 1.97); the features alone were 11.7 Gbps and 1.65. Forwarding
GRO's packets goes from 5.3 to 5.8 Gbps.

Module parameter gso_whole turns it off. verify_gso, for tests, checks
every segment against skb_gso_segment's, counting into gso_verified and
gso_verify_errors; tailscale.com's TestLeanGSOVerify and
TestLeanGSOVerifyForwarded find millions of segments byte-identical, on
the KASAN and lockdep kernel too, and catch a planted bug.

SKB_GSO_TCP_ACCECN gets a compat probe, for older kernels.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I0370d74be47bcb7a3833f721465fa33cd00aac7d
2026-11-08 03:33:16 +00:00
Brad Fitzpatrick 25dafe52cc kernel/linux/DESIGN.md: record MagicDNS in kernel mode, and the restart flake's end
MagicDNS works in kernel mode via a TUN device for just the routes to
the Tailscale service IPs (tailscale.com f282b6a2b). The restart flake
isn't reproducible since the kernel mode private key race was fixed,
and new mode switching and restart tests cover it. Also add the 50,000
node tailnet scaling item.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I787f8912d831fd04622477497bbcd728bb22ac2f
2026-11-08 03:03:32 +00:00
Brad Fitzpatrick ee627ef16e kernel/linux/src: implement wireguard-go's hybrid post-quantum handshake
Tailscale's wireguard-go has a hybrid handshake,
Noise_IKhfs+psk2_25519+MLKEM768, which both peers must agree to use,
with no fallback to the classic one. A kernel-mode node without it
would be cut off from any peer that asked for it.

The initiation now may carry an ephemeral ML-KEM-768 encapsulation
key, sealed under the es key (with the static key then using nonce
1), and the response a ciphertext for it, sealed under the ee key,
whose shared secret is mixed into the chaining key before se. That's
per peer, with the new TSWGPEER_F_HYBRID flag. Hybrid retries carry
wireguard-go's retry bit (bit 31 of the type), which we accept, set and
echo, and which the socket's message classifier now allows.

Linux has no ML-KEM, and its SHA-3 is too new for the kernels we build
for, so src/mlkem.c is a FIPS 203 ML-KEM-768 with its own Keccak.
tests/mlkem checks it against Go's crypto/mlkem: the same keys from the
same seeds, each decapsulating the other's ciphertexts, and the same
implicit rejection. The natlab tests in tailscale.com check the
handshake against wireguard-go, in both roles.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ic524483d6cd27d36900a730553e1b21e4530c399
2026-11-08 02:24:12 +00:00
Brad Fitzpatrick c306c02b44 kernel/linux/src: do lone packets' crypto inline, and run NAPI in softirq
Handing a packet to a crypt worker, the tx worker or a NAPI kthread on
another CPU can wake a halted vCPU, which in a VM is a VM exit and a
trip through the host's scheduler. tswg's idle round trip time was
1.5-2x wireguard-go's in VMs because of it.

So when the crypt workers look idle, a packet that isn't part of a
batch is encrypted or decrypted where it is, after going on the peer's
in-order queue as before. To keep TCP ACKs under load from moving
crypto onto the CPUs taking the NIC's interrupts (8% off a subnet
router), it's limited to 8 packets per CPU per jiffy, and never done
for encryption from softirq (forwarding). And peers' NAPI runs in
softirq again, not kthreads, unless napi_threaded is set.

Idle RTT p50 is now 0.51ms in the lab (wireguard-go 0.60) and 0.75ms
on EC2 (0.97), with subnet router throughput unchanged at the same
NAPI setting. Threaded NAPI is still 4-5% faster for a CPU-bound
router.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ib5ebd1edcda1a515dac0ae4e55540bcfe52d1d7f
2026-11-08 00:43:31 +00:00
Brad Fitzpatrick 60ed5d4cff kernel/linux/DESIGN.md: record the real tailscaled subnet router results
With real tailscaled on the router and 37 clients (a c6in.32xlarge, MTU
1280), kernel mode forwards 69.2 Gbps, 10x TUN mode's 6.9, at 0.74x its
CPU per GB, matching the bare tswg tunnel. Getting there found two
kernel mode bugs in tailscale.com (a private key race, and the device
left behind on exit), now fixed, and exercised kernel mode over IPv6.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I6af1beb0c0b3bd72eeb76d3340de9a977ba5f09c
2026-11-08 00:03:56 +00:00
Brad Fitzpatrick 66609f56b0 kernel/linux/DESIGN.md: compare the subnet router with plain routing at MTU 1280
The plain routing baseline used jumbo frames (165 Gbps). Clamped to MTU
1280, the same size packets as through the tunnels, it gets 88.9 Gbps
through the same c6in.32xlarge router with 37 clients, and tswg 70.9
(0.80x), wireguard-go 6.2, kernel WireGuard 8.5. So the platform's
per-packet costs limit routing at normal MTUs with or without a tunnel.

Also record that CPU figures before now came from /proc/stat, which on
Ubuntu's AWS kernels counts softirq work on otherwise idle CPUs as idle,
understating plain routing and kernel WireGuard most. With idle-state
residency accounting, tswg uses 0.76x wireguard-go's CPU per GB here.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I104bec537a1dcc2c1f460d287245e5ab448442c5
2026-11-07 22:45:37 +00:00
Brad Fitzpatrick 7e5804da87 kernel/linux/src: add zero-copy receive behind a module parameter, off by default
rx_zerocopy (writable at /sys/module/tswg/parameters/rx_zerocopy) makes
tswg_decrypt_gso decrypt data messages in place in the received packet's
pages, when nothing else can see them, and hand out packets that
reference those pages, rather than copy each message into a new packet.

It's off because it doesn't pay: on a CPU-bound subnet router it was 7%
slower, scatterlist decryption costing more than linear decryption of a
cache-hot copy, and on a 200 Gbps c6in.32xlarge router, where the NIC
interrupt CPUs' page allocation is the limit, it took tswg's own page
allocations out but was still no faster (62.5-63.3 Gbps against
63.4-64.7) at 10% more CPU per byte, since ENA's page per received
packet is what binds. It stays for NICs and loads where it might.

The copying path, unchanged, moves into tswg_decrypt_message_copy.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ieb6aa3531f869dd47d768fe367c424fbd22aa3f1
2026-11-07 21:49:17 +00:00
Brad Fitzpatrick a655f887f8 kernel/linux/DESIGN.md: record the c6in subnet router results, up to 74 Gbps
On a c6in.32xlarge router (200 Gbps), with 60 clients at MTU 1280, tswg
forwards 74 Gbps, 12x wireguard-go (6.2) and 9x kernel WireGuard (8.1,
with all 128 CPUs busy). There, with no EC2 allowance exceeded and the
router at 45% CPU, the limit is the NIC's interrupt CPUs, which spend
much of their time on the page allocator's zone lock (ENA allocates a
page per received packet, freed on other CPUs). RPS, bigger per-CPU page
lists and capping GRO for hardware TSO didn't help.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I61acb155937b49375c23e584bad8c98a3d6f5f66
2026-11-07 21:03:13 +00:00
Brad Fitzpatrick 619df26e2d kernel/linux/DESIGN.md: add a "Results at a glance" section
It has the current subnet router and point-to-point results against
wireguard-go and kernel WireGuard in tables, with tswg/wireguard-go
throughput and CPU-per-GB ratios, to keep up to date as results come in.
The TODO list notes that c6in instances are now allowed for the next
subnet router runs.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I1feefcf5d91d4baaf0bc3408d779fbb168643869
2026-11-07 20:14:28 +00:00
Brad Fitzpatrick 003fef170a kernel/linux/DESIGN.md: record the 24-peer, MTU 1280 subnet router results
With 24 clients at MTU 1280, tswg forwards 40 Gbps through a c7i.48xlarge
(5.6x wireguard-go, 3x kernel WireGuard), using about 22 of its 192 CPUs,
and is then limited by EC2's packet rate allowance for the instance.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ifdb78f7d85d182a89a42c2f0f9f77bd14344fb59
2026-11-07 19:21:46 +00:00
Brad Fitzpatrick 95c00517eb kernel/linux/{scripts,DESIGN.md}: resize EC2 instances, and record 50 Gbps results
ec2.sh gets "resize TYPE N..." (for stopped instances) and "start N..."
(some instances), and numbers new instances after all existing ones,
stopped ones included.

With a c7i.48xlarge router (50 Gbps) and 8 clients, tswg forwards 31.5
Gbps at MTU 1280 (wireguard-go 7.2, kernel WireGuard 12.3 using 137 of
192 CPUs), limited by EC2's packet rate per NIC receive queue (about
715k pps, 7.3 Gbps of tunnel packets) with only 8 tunnel flows to spread
over 16 queues; and at line rate with a jumbo tunnel MTU, a data point
only, since MTU 1280 is the target.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I70744651f3385343bf8890607911d9b4e0f70620
2026-11-07 19:16:21 +00:00
Brad Fitzpatrick f810784988 kernel/linux/DESIGN.md: record the 20-peer subnet router results and what was tried
With 20 clients (160 flows), tswg as the subnet router forwards 2.2x (4
CPUs) and 3.6x (8 CPUs) what wireguard-go does, at 38-43% less CPU per
byte. wireguard-go stays at about 6.4 Gbps however many peers or CPUs it
has, so its limit is global, most likely its one UDP-reading goroutine.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I9274a49b47cdbdf9af06f1f9a1f6c8d04f126442
2026-11-07 18:58:15 +00:00
Brad Fitzpatrick 34e785a933 kernel/linux/src: decrypt straight from received packets, and in one ChaCha20 pass
Profiling a CPU-bound tswg subnet router (EC2, 4 CPUs) found two costs
in decrypting merged packets:

Copying. Each message was copied out of the received packet and then
decrypted in place in the copy, two passes over it. When the message is
contiguous in the received packet (in its head or one page frag, as a
message received in one datagram is), decrypt straight from it into the
new packet instead (tswg_contig).

The Poly1305 key. lib/crypto makes it from a separate 32 bytes of
ChaCha20 keystream (block 0), and x86's ChaCha20 does anything up to a
block with its portable code: a scalar ChaCha20 block per message, 5.5%
of the router's CPU. tswg_aead.h has our own ChaCha20-Poly1305 for data
messages, from the library's primitives. When decrypting from one buffer
to another, it runs ChaCha20 from block 0 over the 64 bytes before the
message too (packet headers in the source, headroom in the destination),
and recovers block 0's keystream, the Poly1305 key, as the two XORed:
one SIMD pass per message (tswg_aead_decrypt_ahead). Otherwise, and for
encryption, it asks for two blocks of keystream, which takes the SIMD
path, and sets the counter back.

The 4-CPU router, 4 clients with 16 flows each, medians of 3 runs: 14.1
to 14.9 Gbps, 2.30 to 2.18 CPU-seconds per GB (wireguard-go: 6.6 Gbps,
3.36). Interop with wireguard-go is unchanged; KASAN is quiet.

Also tried, and not kept: zero-copy receive (decrypting in place in the
received pages, 7% worse), and a page pool for decrypted packets (its
pages didn't come back in time to save allocations).

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I98456d17b82d5ef640c55ca1a7fa7a018dc5a279
2026-11-07 18:51:47 +00:00
Brad Fitzpatrick 25d5ffacd7 kernel/linux/scripts: fix debug kernel caching, and sort EC2 hosts numerically
gokrazy-debug-kernel.sh hashed sha256sum's output for its cache key,
which includes the config's random mktemp path, so every run built
another 3.5GB debug kernel; seven of them filled the disk. Hash the
files' contents instead.

ec2.sh sorted hosts by name, which put bradfitz-tswg-10 before
bradfitz-tswg-2; sort by number.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ia758dae0077c54a2ef8f6112ae3c43d6604f66e1
2026-11-07 18:51:40 +00:00
Brad Fitzpatrick df191a1199 kernel/linux/{scripts,DESIGN.md}: add the subnet router benchmark and its results
subnetbench.sh runs tailscale.com's TestLeanSubnetRouter on the EC2
instances for each router CPU count given. ec2.sh now turns off source
and destination checks on its instances, so that one can forward as a
subnet router does, and installs nftables (for the router's masquerade).

DESIGN.md records the results: with 4 clients, tswg as the subnet
router forwards 2.1x (4 CPUs) to 3.2x (8 CPUs) what wireguard-go does,
which uses about 3 CPUs however many it has, at the least CPU per byte
of any tunnel, and reaches the 25 Gbps link at 16 CPUs. Zero-copy
receive was tried there and was 7% worse, so isn't in.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I6b1da8bb8eaa17f9b20cbe36896426411b8b905f
2026-11-07 18:02:37 +00:00
Brad Fitzpatrick f889883537 kernel/linux/{scripts,DESIGN.md}: add EC2 instances for benchmarks, stopped when idle
The QEMU lab runs on a host that's itself a VM, which distorted latency
in particular. ec2.sh manages benchmark instances in the sandbox AWS
account (us-west-2; the org's SCP allows only some regions and requires
IMDSv2): a key pair, a security group open to this host and between its
members, a cluster placement group, and Ubuntu 24.04 instances with IPv6
that install the build tools for their own kernel. Everything is named
bradfitz-tswg... and tagged owner=claude-tswg. It writes the hosts file
that tailscale.com's lean harness takes in remote mode.

To keep costs down, ec2-idle-stop.sh, run every 5 minutes from a systemd
user timer on the always-on dev host, stops the instances once nothing
has touched ~/.cache/tslinux/ec2/last-used for 30 minutes. ec2.sh and the
harness touch it while in use, and "ec2.sh start" brings stopped
instances back.

First EC2 results (c7i.xlarge pair) are in DESIGN.md: throughput is
capped by EC2 at about 7.25 Gbps for both tswg and wireguard-go, with
tswg's receiver using up to 17% less CPU per byte; 1.6x the small
packets; a quarter of the RTT under load at p50; twice the idle RTT.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ibe770a30bea31cb79ac514aeb545f468576b5f05
2026-11-07 17:26:49 +00:00
Brad Fitzpatrick 06bdac96d3 kernel/linux/{DESIGN.md,scripts}: record the performance work, add leancompare.sh
DESIGN.md's Performance section now describes what tswg changes in the
data path and why, the results against wireguard-go (parity to 1.05x on
bulk TCP at 6-10% less CPU per byte, much better small-packet delivery,
worse idle and p99-under-load latency in VMs) and the tools for this
work. The TODO list gets the open performance items.

leancompare.sh runs the lean harness's interleaved A/B comparison for
RTT and TCP with 1 and 4 streams.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Icf8c7ca8bb3711a8172284382af9ce8c3e25ff3d
2026-11-07 14:38:57 +00:00
Brad Fitzpatrick 51ec695feb kernel/linux/src: restore TCP backpressure, and fix tx and queue latency
RTT under a TCP load had grown to about 11ms at p99 (wireguard-go: 3.7).
Instrumenting the queues found three causes:

Backpressure. Upstream encrypts in place, so a packet stays charged to
its socket (for TCP, counted by its small queues, TSQ) until the NIC has
sent it, and TCP is held to the NIC's pace. Our encrypted packets are
new (and earlier ones were orphaned), so TCP could fill the NIC's qdisc
as fast as we encrypt: on a slow uplink with a FIFO qdisc, that would be
massive bufferbloat. Now each encrypted packet takes over its packets'
socket ownership and charge (tswg_move_owner), for destructors that
just uncharge truesize (sock_wfree, and TCP's), settled to its real
size (segments' truesize is about twice their length). TSQ then allowed
TCP only about 1ms in flight, which cost 15-20% of throughput, so
sockets sending over tswg get sk_pacing_shift 9 (about 2ms), as Wi-Fi
drivers set 8 for their aggregation.

The tx worker. Upstream pins a peer's tx worker to one CPU by peer ID.
When that's the CPU taking the NIC's interrupts, softirq work starves
it, and encrypted packets waited up to 20ms to be sent. Queue it on the
CPU that finished encrypting instead (also saving an IPI).

Queue length. The peer queue's limit counted entries, but a GRO entry
is up to 64 packets; count packets, with a limit of 2048.

Medians of interleaved runs in the lean harness, tswg vs. wireguard-go:
TCP 1 stream 10.1 vs. 9.7 Gbps, 4 streams 12.3 vs. 10.8; RTT under load
p50 1.7 vs. 1.7ms. p99 under load is still about 10ms on this testbed:
at full load, the receiving VM's workers sometimes don't run for tens of
milliseconds. That's next to investigate.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ief80aac9a1e443727c89b990c86c7c8f71690954
2026-11-07 14:32:55 +00:00
Brad Fitzpatrick 842a082cc5 kernel/linux/src: free sent packets on the tx worker, not the encrypt workers
The encrypt workers freed each list's original packets as soon as they
were encrypted. For TCP, freeing releases the socket's small queues
accounting (tcp_wfree), whose TSQ handlers then ran on all the encrypt
workers' CPUs at once and contended for the socket lock (about 5% of
each sender CPU in queued_spin_lock_slowpath). Free them instead on the
peer's tx worker once their encrypted packets are sent, which is also
when upstream releases them.

Bulk TCP in the lean harness (medians of interleaved runs): 1 stream
10.4 Gbps (wireguard-go 10.0), 4 streams 12.7 Gbps (wireguard-go 11.1).

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I904dea12ccfd7bc0534d616eb319fd692eefd832
2026-11-07 13:32:47 +00:00
Brad Fitzpatrick c9f9c52c0c kernel/linux/src: add GRO for WireGuard data messages, and decrypt merged packets whole
Our UDP sockets now have gro_receive/gro_complete callbacks, as UDP
tunnels do. They merge consecutive data messages of a flow that use the
same receiving key and are all the same size but perhaps the last, and
UDP hands the merged packet (SKB_GSO_UDP_TUNNEL) to encap_rcv whole. It
goes through the decrypt ring and the peer's queue as one entry: one
worker decrypts each message into a new packet (copying it out and
decrypting in place there, rather than linearizing in place), and the
peer's NAPI poll delivers them in order. So the IP/UDP input path, the
ring and queue handoffs and the worker kick are paid once per merged
packet rather than per packet. Upstream gets no outer GRO at all.

skb_gro_receive isn't exported to modules, so socket.c has a copy
(tswg_gro_merge). gro_receive_network_flush and udp_tunnel_update_gro_rcv
are newer than 6.8, so they're probed. The latter matters: 7.x may make
the socket's gro_receive a static call, which detaching a user socket
must undo, or it would outlive the module.

Bulk TCP in the lean harness, now with guest USO off (so the receiving
VM sees individual datagrams, as from a physical network): about 9.8
Gbps, against 6.5 before, and within a few percent of wireguard-go
(about 10.0), at about 18% less receiver CPU. Making workers poll the
ring briefly before going idle (to save wakeup IPIs) was tried and
didn't help, so isn't included.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ic22482120c6ce75b9a4d896f16b6cf77aca28b4c
2026-11-07 13:29:26 +00:00
Brad Fitzpatrick 9dc696cc4d kernel/linux/{scripts,src}: add a KASAN/lockdep debug kernel, and rename slab caches
gokrazy-debug-kernel.sh builds the gokrazy kernel package's kernel again
with KASAN, lockdep, DEBUG_VM and DEBUG_NET, for tailscale.com's lean
harness to boot (TSWG_DEBUG_KERNEL=1). build-for-gokrazy.sh builds
against it when given TSWG_KDIR, since those options change kernel data
structures.

Its first find: our slab caches had upstream WireGuard's names (via
KMEM_CACHE), which clash with wireguard.ko or a built-in WireGuard, as
gokrazy has, and make slab_common warn at load. Give them tswg_ names.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I2e7d5448bcf8b2fb68810486f7e5d4bf2af4558d
2026-11-07 13:29:15 +00:00
Brad Fitzpatrick 2c3ba4df08 kernel/linux/src: encrypt into page frags, and kick crypt workers on demand
Three changes to the data path, from profiling bulk TCP in the lean
harness:

Encryption. Upstream encrypts each packet in place, which first means
making it linear and writable (skb_cow_data, usually a copy), and the
previous commit then chained the encrypted packets as a GSO frag_list,
which segmentation copied again (skb_segment must unshare each clone's
head to write its headers). Now the encrypt worker copies each inner
packet (finishing its checksum on the way, if needed) into pages of its
own, encrypts it there, and gathers each run of equal-size messages into
a UDP GSO packet whose payload is those pages. Segmenting that needs only
page references, and a device with UDP segmentation offload takes it
whole: virtio with USO now sends 64KB superpackets. Sender CPU at the same
throughput went from about 65% to 43% per vCPU.

Worker kicks. Instead of kicking a crypt worker per packet (or per 16),
kick only when no worker is draining the ring, or when the backlog shows
the busy ones aren't keeping up, and prefer other CPUs than the one
taking packets from the NIC. A tswg_busy flag, paired with the ring
under full barriers, closes the race with a worker going idle.

Queue length. The peer's in-order queue overflowed at 1024 packets (1.5ms
at 7 Gbps) whenever a decrypt worker was held up, and TCP took those
silent drops (0.2% of packets) as congestion. Allow 4096 entries, and
count drops in rx_dropped and rx_fifo_errors, which upstream doesn't.

The receiver is now the bottleneck (about 6.5 Gbps): UDP packets arriving
coalesced are segmented back, with software checksums, before reaching
encap_rcv. That's next.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ib8d9cf9e5541b665e06d57a16b46f653d49bc3ea
2026-11-07 12:26:13 +00:00
Brad Fitzpatrick b9a0f5d572 kernel/linux/src: batch crypt worker kicks, and send with outer UDP GSO
Profiling bulk TCP in the lean harness (tailscale.com's tstest/tswgbench)
showed kernel WireGuard serializing on two per-packet costs:

On receive, the single softirq CPU spent half its time in queue_work_on:
upstream kicks the next CPU's crypt worker for every packet, round robin,
which usually means waking it with an IPI (a VM exit in a VM). All the
workers drain the same ring, so instead stay on one CPU for 16 packets at
a time, and skip queue_work_on when the work is already pending (a read,
rather than queue_work_on's atomic test-and-set on a cache line the
worker writes). The same pending check applies to the per-peer transmit
work.

On send, the per-peer transmit worker sent each encrypted packet through
the UDP/IP stack and the device on its own, which with virtio is a device
kick, a VM exit, per packet. Packets from one GSO packet encrypt to the
same size (WireGuard pads to 16 bytes) except perhaps the last, so chain
each such run, without copying, as the frag_list of its first packet and
send it as one UDP GSO (SKB_GSO_UDP_L4) packet, with its checksum set up
as for a UDP_SEGMENT socket. The stack (or a capable device) segments it,
and the segments go to the device in one batch.

The coalesced packets are orphaned first: segmenting a frag_list of
packets still owned by the inner TCP socket uncharged the socket more
than it was charged (refcount underflow in tcp_wfree). wireguard-go has
the same effect, since the TUN device frees the inner packet once read.

Bulk TCP over tswg goes from 3.6 to 5.8 Gbps (wireguard-go: about 10.5).

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I649a7f1790c35d1af584d2c14f588195232d5ef4
2026-11-07 12:06:16 +00:00
Brad Fitzpatrick 7648a28d11 kernel/linux/{scripts,DESIGN.md}: add leanbench.sh and record lean benchmark results
leanbench.sh runs tailscale.com's new lean data plane benchmark, which
compares wireguard-go, tswg, the kernel's WireGuard and no tunnel between
two QEMU VMs on a host bridge with vhost-net, without natlab, over IPv4
and IPv6. With -smoke it runs the quick correctness test instead.

The results overturn the natlab ones, which natlab's userspace network
had capped: kernel WireGuard (and tswg, identically) gets about a third
of wireguard-go's bulk TCP throughput, at 1.5x to 2.5x the CPU per byte,
without being CPU-bound. So outer UDP GSO/GRO and finding where the
kernel serializes are now the critical performance work.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I2a24de8d82370fc8dfb6a9af0971da20831ea307
2026-11-07 06:06:16 +00:00
Brad Fitzpatrick ac26c5bfce kernel/linux/{scripts,DESIGN.md}: add bench.sh and record first natlab benchmarks
bench.sh runs tailscale.com's TestTSWGBenchmark, which compares
tailscaled in kernel mode against tailscaled in TUN mode (wireguard-go)
on the same pair of natlab gokrazy VMs, and saves the results as JSON.

Record the first results: bulk TCP throughput and CPU per byte are about
the same (natlab caps throughput, and the kernel lacks outer UDP
GSO/GRO), while the kernel receives about 1.7x the small packets per
second at about 30% less CPU per packet and roughly halves latency under
load. Also record the plan for a lean benchmark harness without natlab,
whose userspace switch is expensive, and a flake seen when both peers
restart at once.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I69e696817f98e685576a30d766c42740d4d04810
2026-11-07 05:52:04 +00:00
Brad Fitzpatrick 0b23989672 kernel/linux/DESIGN.md: document the event-driven kernel data plane
Record how the kernel data plane now runs without polling: config
changes trigger full syncs, magicsock's kernel path hook (with a trust
expiry timer for idle sessions) triggers per-peer endpoint updates, and
the module's peer events drive disco key advertisements and activity
feedback, all applied by one goroutine.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I8c555a623e8bba7cf7561fe94e4bc9d17fd40003
2026-11-07 05:37:17 +00:00
Brad Fitzpatrick 1b969a6b3a kernel/linux/src: multicast peer events so that userspace needn't poll
tailscaled was dumping the device every second to notice new sessions
(to send its disco key advertisement, as wireguard-go does) and traffic
on direct paths (to keep magicsock heartbeating active sessions).

Instead, multicast TSWG_CMD_PEER_EVENT messages on a new "events"
genetlink group, in the device's network namespace: one when a new
session is established (from wg_timers_session_derived), and TX and RX
activity notices, each rate limited to one per peer every 2 seconds with
a single comparison on the packet path. Messages are only built when
someone is listening.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I08bdaa23a9f9b01a0bb3a4404296cae5961cde86
2026-11-07 05:37:16 +00:00
Brad Fitzpatrick f7c80e0a32 kernel/linux/DESIGN.md: document TSMP and peer restarts in kernel mode
Record how tailscaled exchanges TSMP and its other generated packets with
peers in kernel mode (raw sockets on the tswg device, and a tstun
outbound inject hook), how it emulates wireguard-go's disco key
advertisement on session establishment, how it clears the kernel session
when a peer restarts, and that wireguard-go must never get peers in
kernel mode. Mark the packet filter and MagicDNS as deliberately
deferred.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I632c5a75b1c2e1dc74680faaa34bbd799e52fe74
2026-11-07 05:00:05 +00:00
Brad Fitzpatrick c32ba694d1 kernel/linux/src: let userspace clear a peer's session
When tailscaled learns that a peer restarted (its disco key changed), the
peer has lost its WireGuard keys, but the kernel would keep sending with
the dead session until WireGuard's own timers start a new handshake about
15 seconds later.

Add TSWGPEER_A_CLEAR_SESSION, an NLA_FLAG in the nested WGPEER_A_TSWG
attribute, which forgets the peer's keypairs and any handshake in
progress, as wg_pm_notification does for every peer before suspend, and
resets the handshake rate limit, so that the next packet to the peer is
staged and starts a new handshake right away.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ia9821a47e6c8114bbabd81d8d5a2be528295e74f
2026-11-07 05:00:03 +00:00
Brad Fitzpatrick fe0c4864e9 kernel/linux/DESIGN.md: document the loopback bridge and tailscaled integration
DERP via 127.0.0.0/8 loopback endpoints turned out to need no kernel
changes; record how the bridge works (including peer relays and the
untrusted-path spray) and how tailscaled now drives tswg on the
tailscale.com bradfitz/tswg branch, with its known gaps, the biggest
being that kernel mode has no packet filter yet.

Also record that Tailscale's wireguard-go is about to start using a
hybrid post-quantum handshake, which tswg doesn't implement, so the
assumption that tswg is wire-compatible with every Tailscale peer no
longer holds, and the options for dealing with that.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I2447db2494bfd1a572f8ce4d093a42a1949aa3d4
2026-11-07 00:09:41 +00:00
Brad Fitzpatrick 189b463b86 kernel/linux/src: add a per-peer flag to stop roaming
Upstream WireGuard moves a peer's endpoint to the source of each
authenticated packet. In Tailscale, disco picks the path, so that would
fight it: a packet that arrives via DERP would steal the endpoint from a
good direct path.

Add TSWGPEER_F_NO_ROAM, set through TSWGPEER_A_FLAGS nested in a new
WGPEER_A_TSWG attribute (numbered 64, like WGDEVICE_A_TSWG) and reported
in dumps. With it set, the endpoint learned from handshake initiations,
handshake responses and data packets is ignored once the peer has an
endpoint, and only userspace changes it. A peer with no endpoint yet
still gets one from its first authenticated packet, so it stays
reachable if it contacts us before userspace has configured one.

The gate, tswg_peer_may_roam, sits at the two learned-endpoint paths
rather than in wg_socket_set_peer_endpoint, which netlink also uses for
explicit updates. The flag is applied before WGPEER_A_ENDPOINT in the
same request, so pinning and setting an endpoint are one step.

Tested by TestTSWG{Gokrazy,Ubuntu}NoRoam in tailscale.com's natlab
vmtests, which fail as expected with the gate disabled.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I621a6e86d3947e154c4cfa582aa845bb49d42b2f
2026-11-06 23:07:32 +00:00
Brad Fitzpatrick 7398e67950 kernel/linux/DESIGN.md: make performance the primary goal, with its own TODOs
Performance is why we're doing this, so say so in the Goal section and
add a Performance section: what we inherit from upstream WireGuard (inner
GSO on send, inner GRO via NAPI on receive, parallel crypto), what's
missing (outer UDP GSO on send and outer UDP GRO on receive, the kernel
analogues of what makes wireguard-go fast), why tailscaled shouldn't set
UDP_GRO on the shared socket in kernel mode, and how to measure.

Group the performance work at the top of the TODO list.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: If844d30a81f5f2fe52bc2fbf2b31d7bbbb9359c7
2026-11-06 23:00:12 +00:00
Brad Fitzpatrick 4c916c40ab kernel/linux/src: let userspace share its UDP socket with a tswg device
Tailscale needs disco, STUN and WireGuard to share one UDP 4-tuple, so
that the NAT mappings disco discovers are the ones WireGuard uses. So let
tailscaled create and bind the socket itself and hand its fd to the
device, as L2TP allows for its tunnel sockets.

The new TSWGDEVICE_A_SOCK_FD4 and TSWGDEVICE_A_SOCK_FD6 attributes, nested
in a new WGDEVICE_A_TSWG attribute numbered 64 to stay clear of future
upstream attributes, take the fd of a bound UDP socket (IPV6_V6ONLY for
IPv6). The device hooks the socket's UDP encap_rcv, consumes WireGuard
messages (a first byte of 1 through 4 and three zero bytes), and returns
everything else to the normal UDP path for userspace to read. Userspace
can send its own datagrams on the socket as usual.

The device keeps a reference to the struct sock, not the file, and drops
the socket from its encap_destroy hook when userspace closes it, so the
socket lives exactly as long as userspace's fd. Passing -1 detaches the
socket, restoring it to plain UDP, and the device goes back to creating
its own sockets. User sockets persist across ifdown/ifup, and changing
the listen port while one is attached fails with EBUSY. Dumps report
which families have a user socket in TSWGDEVICE_A_FLAGS.

A global mutex serializes attach and detach, including detach from
encap_destroy, which runs from the user's close() with none of our locks
held.

Tested by TestTSWGGokrazySharedSocket (7.2.8) and
TestTSWGUbuntuSharedSocket (6.8) in tailscale.com's natlab vmtests.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I2e7a97952ced03df4bc954ba398a49daf9128d41
2026-11-06 22:13:43 +00:00
Brad Fitzpatrick 7fccee622e kernel/linux/DESIGN.md: note that the gokrazy test kernel is bumped by bots
A gokrazy bot bumps github.com/gokrazy/kernel.amd64 to new kernel
releases and a tailscale.com cron bot bumps that go.mod dependency, so
the gokrazy vmtests follow upstream kernels automatically. Record that,
and that a failure after such a bump most likely means kernel API churn
needing a new compat shim.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I12428a124604edb65a61f101b72c87caed6c9332
2026-11-06 21:59:18 +00:00
Brad Fitzpatrick d3c49a3c40 kernel/linux/DESIGN.md: make gokrazy the primary test guest
Record the decision to test primarily on natlab's gokrazy image, which
boots a recent upstream kernel and has no userspace tools, as in the end
state where tailscaled drives the kernel entirely. Describe how its
module build tree is recreated and how the test agent loads and
configures tswg. Keep Ubuntu 24.04 as a secondary guest for distro and
DKMS-style coverage.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I89714b58c944be4f373f23471e3aeef11942b1f6
2026-11-06 21:50:15 +00:00
Brad Fitzpatrick 309157b88f kernel/linux/scripts: build tswg.ko for gokrazy kernels
The natlab gokrazy image runs a recent upstream kernel (7.2.8 today) that
gokrazy rebuilds and tailscale.com bumps regularly, which makes it a
better primary test target than a distro kernel: it keeps us honest
against new kernel APIs, and gokrazy has no userspace tools, as in the
end state where tailscaled drives the kernel entirely.

gokrazy kernels are kernel.org releases plus the patches in
_build/series, built with an embedded config and without MODVERSIONS or
module signing. gokrazy-kdir.sh downloads the release, applies the
patches, installs the config pulled out of vmlinuz with the kernel's
extract-ikconfig (vendored), and runs modules_prepare, caching the
result by release and config hash. It takes about 20 seconds the first
time. The only config differences after olddefconfig with the host's gcc
13 (gokrazy uses Debian 12's gcc 12) are toolchain capability bits.

Factor the generic part of build-for-ubuntu.sh into build.sh, which now
copies only sources (not objects from an in-place build in src/), and
sets KBUILD_MODPOST_WARN=1 for trees without a Module.symvers. Add
build-for-gokrazy.sh.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Id28d9c013bdfaa7cec28aeff846f7f6087e25518
2026-11-06 21:44:29 +00:00
Brad Fitzpatrick d095c5b6ef kernel/linux/DESIGN.md: record compat, Go client and testbed decisions
Document the feature-probe compat framework, that the Go netlink client
and tswgctl live in tailscale.com on the bradfitz/tswg branch, and how
the natlab vmtests build, install and exercise tswg.ko. Update the TODO
list: TestTSWGManualTunnel now passes against Ubuntu 24.04's stock
6.8.0-106-generic kernel.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ie279a07e251d1ee81cdc91478f3eec9d0dd46afb
2026-11-06 21:07:52 +00:00
Brad Fitzpatrick 370c6a69ea kernel/linux/scripts: add build-for-ubuntu.sh and vmtest.sh
build-for-ubuntu.sh builds tswg.ko for a given Ubuntu kernel release in a
private copy of src/, so test builds for different guest kernels don't
share object files with each other or with a developer's in-place build.
The Makefile's SRC is now overridable to allow that.

vmtest.sh runs the tswg natlab vmtests, which live in a tailscale.com
checkout (on the bradfitz/tswg branch) because they use its natlab
framework and the Go tswg netlink client there.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I00eb5cbf8df5a203d6d39d541779060db74da9a1
2026-11-06 21:06:08 +00:00
Brad Fitzpatrick ff6f9e8409 kernel/linux/{src/compat,scripts}: add feature-probe compat layer for 6.8 and 7.0
The fork tracks the newest upstream WireGuard (7.4-rc1), but has to build
on distro kernels. Our first target is the natlab Ubuntu 24.04 guest
(6.8.0-106-generic) and the dev host is on 7.0.

Rather than LINUX_VERSION_CODE checks, which lie on heavily backported
kernels such as RHEL's, scripts/probe.sh compile-tests each
src/compat/probes/HAVE_*.c against the target kernel's build directory
(in parallel, about one second in total) and writes
src/compat/generated/autoconf.h. Kbuild force-includes src/compat/compat.h,
which shims whatever is missing: udp_get_len_short, the struct sock
udp_tunnel APIs, the ipcb_flags xmit arguments, the blake2s_ctx API,
WQ_PERCPU, netif_threaded_enable, rtnl_newlink_params, sockaddr_inet, the
kmalloc_obj family and timer_container_of.

Three spots in device.c can't be shimmed from a header (the lltx bit, the
old rtnl newlink signature, and pernet exit_rtnl) and get small #ifdefs
marked TSWG_COMPAT. device.c also always sets ndo_get_stats64 to
dev_get_tstats64, since older kernels don't fold tstats in
dev_get_stats(). That is equivalent on new kernels.

tswg.ko now builds against both 6.8.0-106-generic and 7.0.0-1009-aws.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I0ed279318b4f623688d1f178a9b8fbcc0b585219
2026-11-06 21:01:41 +00:00
Brad Fitzpatrick af92b849a7 kernel/linux/src: fork WireGuard as the out-of-tree tswg module
Copy upstream/drivers/net/wireguard into src and rename only the
user-visible identifiers: the module (and thus the rtnl link kind and
device type, which come from KBUILD_MODNAME) is now tswg, and the
genetlink family is "tswg". The uapi header moves to
src/include/uapi/linux/tswg.h so it can't collide with the kernel's.
Internal wg_ symbols keep their names to keep upstream merges easy; they
aren't exported, so tswg.ko can coexist with wireguard.ko.

Add an out-of-tree Kbuild and top-level Makefile, plus
scripts/ubuntu-headers.sh, which fetches and unpacks an Ubuntu kernel's
headers so we can build for the natlab Ubuntu guest without installing
anything.

This still only builds against 7.4-era kernels; compat shims follow.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ia36ef82a53efbbcdb301ed069785162372efda55
2026-11-06 20:55:25 +00:00
Brad Fitzpatrick 24131ef7cd kernel/linux/DESIGN.md: add initial design and TODO list
Record the decisions made so far: an out-of-tree C fork of in-tree
WireGuard (no Rust, no upstreaming), a shared UDP socket passed in from
tailscaled, explicit endpoints with auto-roaming disabled, DERP via
127.0.0.0/8 loopback endpoints, and userspace spray. Also describe the
natlab vmtest testbed and the TODO list.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ie18d356974a0a0d8fce19674b590861144be8cea
2026-11-06 20:53:38 +00:00
Brad Fitzpatrick e6b80dad42 kernel/linux: add the GPL-2.0 license for the tswg kernel module
The tswg kernel module, under kernel/linux, is derived from Linux's WireGuard,
and so is GPL-2.0, unlike the rest of this repository.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I8cab6152232f771364a06d79ecd8b406f3ef01dc
2026-11-06 20:52:49 +00:00