cmd/tailscale/cli,wgengine: add --collect-load-metrics and wire it up

Adds the Linux-only CLI flag that opts a subnet router into per-route load
metrics, and plumbs the pref through router.Config so the engine can enable
counting on the TUN wrapper during Reconfig.

When the pref is off the engine passes no routes, which leaves the counting
hook uninstalled, so nodes that have not opted in pay only a nil atomic load
per packet.

Updates tailscale/corp#45684

Change-Id: I2d23184b244b4b32bab071ebcc580923ca8d558d
Signed-off-by: Kabir Sikand <[email protected]>
This commit is contained in:
Kabir Sikand committed 2026-07-29 02:09:15 +00:00
1 parent a73f5930d9
commit a35885f663
9 files changed
+75 -2

No files matched your search

+2
View File
@@ -184,6 +184,8 @@ func prefValue(flagName string, prefs *ipn.Prefs, st *ipnstate.Status) any {
return false
}
return true
case "collect-load-metrics":
return prefs.CollectLoadMetrics.EqualBool(true)
case "netfilter-mode":
return prefs.NetfilterMode.String()
case "unattended":
+3
View File
@@ -65,6 +65,7 @@ type setArgsT struct {
remoteConfig bool
snat bool
statefulFiltering bool
collectLoadMetrics bool
sync bool
netfilterMode string
relayServerPort string
@@ -116,6 +117,7 @@ func newSetFlagSet(goos string, setArgs *setArgsT) *flag.FlagSet {
case "linux":
setf.BoolVar(&setArgs.snat, "snat-subnet-routes", true, "source NAT traffic to local routes advertised with --advertise-routes")
setf.BoolVar(&setArgs.statefulFiltering, "stateful-filtering", false, "apply stateful filtering to forwarded packets (subnet routers, exit nodes, and so on)")
setf.BoolVar(&setArgs.collectLoadMetrics, "collect-load-metrics", false, "collect per-route load metrics for routes advertised with --advertise-routes; not supported on App Connectors")
setf.StringVar(&setArgs.netfilterMode, "netfilter-mode", defaultNetfilterMode(), "netfilter mode (one of on, nodivert, off)")
case "windows":
setf.BoolVar(&setArgs.forceDaemon, "unattended", false, "run in \"Unattended Mode\" where Tailscale keeps running even after the current GUI user logs out (Windows-only)")
@@ -167,6 +169,7 @@ func runSet(ctx context.Context, args []string) (retErr error) {
PostureChecking: setArgs.reportPosture,
RemoteConfig: setArgs.remoteConfig,
NoStatefulFiltering: opt.NewBool(!setArgs.statefulFiltering),
CollectLoadMetrics: opt.NewBool(setArgs.collectLoadMetrics),
},
}
+1
View File
@@ -916,6 +916,7 @@ func init() {
addPrefFlagMapping("shields-up", "ShieldsUp")
addPrefFlagMapping("snat-subnet-routes", "NoSNAT")
addPrefFlagMapping("stateful-filtering", "NoStatefulFiltering")
addPrefFlagMapping("collect-load-metrics", "CollectLoadMetrics")
addPrefFlagMapping("exit-node-allow-lan-access", "ExitNodeAllowLANAccess")
addPrefFlagMapping("unattended", "ForceDaemon")
addPrefFlagMapping("operator", "OperatorUser")
+1
View File
@@ -6553,6 +6553,7 @@ func (b *LocalBackend) routerConfigLocked(cfg *wgcfg.Config, prefs ipn.PrefsView
Routes: b.currentNode().osRoutes(),
NetfilterKind: netfilterKind,
RemoveCGNATDropRule: nm.HasCap(tailcfg.NodeAttrDisableLinuxCGNATDropRule),
CollectLoadMetrics: prefs.CollectLoadMetrics().EqualBool(true),
}
if buildfeatures.HasSynology && distro.Get() == distro.Synology {
+6
View File
@@ -1566,6 +1566,12 @@ func (t *Wrapper) subnetCounters() *subnetCounters {
return t.subnetRouteCounters.Load()
}
// SubnetRouteCountingEnabledForTest reports whether per-route subnet load
// metrics are currently being collected.
func (t *Wrapper) SubnetRouteCountingEnabledForTest() bool {
return t.subnetRouteCounters.Load() != nil
}
// countSubnetRouteTraffic attributes a packet to an advertised subnet route,
// if it is forwarded traffic for one.
//
+8 -1
View File
@@ -128,9 +128,16 @@ type Config struct {
// SubnetRoutes is the list of subnets that this node is
// advertising to other Tailscale nodes.
// As of 2023-10-11, this field is only used for network
// flow logging and is otherwise ignored.
// flow logging and is otherwise ignored. As of 2026-07-28, it is
// additionally used for per-route load metrics when CollectLoadMetrics
// is set.
SubnetRoutes []netip.Prefix
// CollectLoadMetrics is whether to count forwarded bytes and packets per
// entry in SubnetRoutes. It is opt-in because it adds a longest-prefix
// lookup to the packet path.
CollectLoadMetrics bool
// Linux-only things below, ignored on other platforms.
SNATSubnetRoutes bool // SNAT traffic to local subnets
StatefulFiltering bool // Apply stateful filtering to inbound connections
+2 -1
View File
@@ -14,7 +14,8 @@ import (
func TestConfigEqual(t *testing.T) {
testedFields := []string{
"LocalAddrs", "Routes", "LocalRoutes", "NewMTU",
"SubnetRoutes", "SNATSubnetRoutes", "StatefulFiltering",
"SubnetRoutes", "CollectLoadMetrics",
"SNATSubnetRoutes", "StatefulFiltering",
"NetfilterMode", "NetfilterKind", "RemoveCGNATDropRule",
}
configType := reflect.TypeFor[Config]()
+8
View File
@@ -813,6 +813,14 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config,
defer e.wgLock.Unlock()
e.tundev.SetWGConfig(cfg)
// Per-route load metrics are opt-in, so pass no routes when they are
// disabled; that leaves the counting hook uninstalled entirely.
if routerCfg.CollectLoadMetrics {
e.tundev.SetSubnetRoutes(routerCfg.SubnetRoutes)
} else {
e.tundev.SetSubnetRoutes(nil)
}
e.mu.Lock()
self := e.selfNode
e.mu.Unlock()
+44
View File
@@ -643,3 +643,47 @@ func TestDERPAppNamePlumbing(t *testing.T) {
t.Fatal("timeout waiting for engine to connect to the test DERP server")
}
}
// TestReconfigSubnetRouteLoadMetrics verifies that the CollectLoadMetrics
// opt-in reaches the TUN wrapper, and that per-route counting stays off
// without it.
func TestReconfigSubnetRouteLoadMetrics(t *testing.T) {
bus := eventbustest.NewBus(t)
ht := health.NewTracker(bus)
reg := new(usermetric.Registry)
e, err := NewFakeUserspaceEngine(t.Logf, 0, ht, reg, bus)
if err != nil {
t.Fatal(err)
}
t.Cleanup(e.Close)
ue := e.(*userspaceEngine)
routes := []netip.Prefix{netip.MustParsePrefix("10.1.0.0/16")}
// Opted out: no counting, even though routes are advertised.
if err := ue.Reconfig(&wgcfg.Config{}, &router.Config{SubnetRoutes: routes}, &dns.Config{}); err != nil {
t.Fatalf("Reconfig: %v", err)
}
if got := ue.tundev.SubnetRouteCountingEnabledForTest(); got {
t.Error("per-route counting is enabled without CollectLoadMetrics")
}
// Opted in: counting enabled.
if err := ue.Reconfig(&wgcfg.Config{}, &router.Config{
SubnetRoutes: routes,
CollectLoadMetrics: true,
}, &dns.Config{}); err != nil {
t.Fatalf("Reconfig: %v", err)
}
if got := ue.tundev.SubnetRouteCountingEnabledForTest(); !got {
t.Error("per-route counting is disabled with CollectLoadMetrics set")
}
// Opting back out disables it again.
if err := ue.Reconfig(&wgcfg.Config{}, &router.Config{SubnetRoutes: routes}, &dns.Config{}); err != nil {
t.Fatalf("Reconfig: %v", err)
}
if got := ue.tundev.SubnetRouteCountingEnabledForTest(); got {
t.Error("per-route counting stayed enabled after opting out")
}
}