mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-01 06:02:32 -08:00
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]>