Kabir Sikand 12a4fa2d99 net/tstun: keep the subnet counters alive across a disable, and count only forwarded traffic
Fixes five related defects in the per-route subnet counters, all in how the
lifetime of the counting state and the definition of "forwarded" were handled.

SetSubnetRoutes(nil) stored nil, discarding the whole subnetCounters. That
broke the documented promise that a re-advertised route resumes its previous
total, and the next non-empty call built a fresh instance that registered a
second MultiLabelMap under the same registry name. Observably, labeled tx_bytes
went 1000000 -> 0 across a toggle while the aggregate stayed at 1000000, so the
two views permanently disagreed. A Prometheus counter must never decrease; this
produced phantom rate() spikes. tailscale down/up, a pref toggle, a profile
switch, and Reconfig(&router.Config{}) from enterStateLocked all drive it.

The instance now outlives a disable. Disabling clears the lookup table instead,
and the packet path gates on subnetCountersActive() -- two nil-able atomic
loads, no decode and no lookup when off, benchmarked at 0.73 ns/op. The
retained expvar.Int values stay registered with the aggregates, which is
required anyway: AggregateCounter has no unregister-one API, so the old code
also leaked 4 values per route per toggle into a process-global set that
Value() walks on every scrape and every logtail delta (10 cycles leaked 10
values; now 0).

Retention is now bounded at maxRetainedRoutes (4096); 20,000 single-route
reconfigs previously retained 20,000 routeCounters at ~1.4 KiB each. Eviction
takes the least recently advertised withdrawn routes, never an advertised one,
and folds their totals into a residual counter that stays registered with the
aggregates. So eviction cannot make the aggregates decrease -- the only thing
lost is the per-route breakdown of an entry that has not been advertised for at
least 4096 reconfigs, and whose labeled series were removed on withdrawal
anyway. Eviction goes down to half the cap so it is amortized rather than
running on every reconfig once at the cap.

"Exactly one Tailscale endpoint" does not establish that a packet is
*forwarded*: it is equally true of traffic the node itself originates toward an
address inside one of its own advertised prefixes. Such a packet leaves through
the TUN read path, so it was counted as rx -- the opposite of reality, since rx
means received from the subnet and returned to the peer. Counting now also
requires the tailnet end to be a peer rather than this node. The node's own
addresses reach the Wrapper via SetWGConfig, which already computed exactly
these two addresses for the TAP path and is called from Reconfig immediately
before SetSubnetRoutes. The check is two address comparisons behind one atomic
load; the hook stays at zero allocations per packet.

The Read hook read p.Src/p.Dst without an IPVersion check. Decode does not
clear them when it bails out early and p is pool-reused across the loop, so a
stale address from a previous packet could be counted. Both hooks now share
countSubnetRouteParsed, which checks it.

An exit-node-only route set installed the hook, published zero series, and paid
a decode plus a never-hitting lookup on every packet, because the enable
decision was len(routes) == 0 while the countable decision was Bits() > 0. The
enable decision is now anyCountableRoute.

4via6 routes are excluded from countableRoute for M1: their prefixes are inside
fd7a:115c:a1e0::/48, so both endpoints are Tailscale addresses and no packet
can ever be attributed to one. They previously published four permanently-zero
series, which reads as an idle router rather than an unsupported route type.

Adds clientmetric.AggregateCounter.RegisteredCountForTest so the leak is
assertable without reflection, and benchmarks for the real hook in both the
enabled and disabled states.

Updates tailscale/corp#45684

Change-Id: If0c7f1129e2f7c8b44349ab8937f5b15f9b1cee4
Signed-off-by: Kabir Sikand <[email protected]>
2026-07-29 17:32:01 +00:00
2026-07-10 14:55:33 -07:00
2026-07-20 12:37:39 -07:00
2026-07-10 17:39:16 -07:00
2026-07-10 17:39:16 -07:00
2026-07-10 17:39:16 -07:00
2026-01-27 16:15:17 -08:00
2026-06-02 10:59:29 -07:00
2026-07-08 17:15:35 -07:00
2026-07-08 17:15:35 -07:00

Tailscale

https://tailscale.com

Private WireGuard® networks made easy

Overview

This repository contains the majority of Tailscale's open source code. Notably, it includes the tailscaled daemon and the tailscale CLI tool. The tailscaled daemon runs on Linux, Windows, macOS, and to varying degrees on FreeBSD and OpenBSD. The Tailscale iOS and Android apps use this repo's code, but this repo doesn't contain the mobile GUI code.

Other Tailscale repos of note:

For background on which parts of Tailscale are open source and why, see https://tailscale.com/opensource/.

Using

We serve packages for a variety of distros and platforms at https://pkgs.tailscale.com.

Other clients

The macOS, iOS, and Windows clients use the code in this repository but additionally include small GUI wrappers. The GUI wrappers on non-open source platforms are themselves not open source.

Building

We always require the latest Go release, currently Go 1.26. (While we build releases with our Go fork, its use is not required.)

go install tailscale.com/cmd/tailscale{,d}

If you're packaging Tailscale for distribution, use build_dist.sh instead, to burn commit IDs and version info into the binaries:

./build_dist.sh tailscale.com/cmd/tailscale
./build_dist.sh tailscale.com/cmd/tailscaled

If your distro has conventions that preclude the use of build_dist.sh, please do the equivalent of what it does in your distro's way, so that bug reports contain useful version information.

Bugs

Please file any issues about this code or the hosted service on the issue tracker.

Contributing

PRs welcome! But please file bugs. Commit messages should reference bugs.

We require Developer Certificate of Origin Signed-off-by lines in commits.

See commit-messages.md (or skim git log) for our commit message style.

About Us

Tailscale is primarily developed by the people at https://github.com/orgs/tailscale/people. For other contributors, see:

WireGuard is a registered trademark of Jason A. Donenfeld.

Languages
Go 96.5%
C 1.2%
TypeScript 0.8%
Shell 0.5%
Swift 0.3%
Other 0.4%