diff --git a/control/controlknobs/controlknobs.go b/control/controlknobs/controlknobs.go index bde36df0d..6069d400b 100644 --- a/control/controlknobs/controlknobs.go +++ b/control/controlknobs/controlknobs.go @@ -20,55 +20,68 @@ import ( // adjust at runtime. type Knobs struct { // DisableUPnP indicates whether to attempt UPnP mapping. + // Set by [nodecap.DisableUPnP]. DisableUPnP atomic.Bool // RandomizeClientPort is whether control says we should randomize // the client port. + // Set by [nodecap.RandomizeClientPort]. RandomizeClientPort atomic.Bool // OneCGNAT is whether the node should make one big CGNAT route // in the OS rather than one /32 per peer. + // Set by [nodecap.OneCGNATEnable] and [nodecap.OneCGNATDisable]. OneCGNAT syncs.AtomicValue[opt.Bool] // ForceBackgroundSTUN forces netcheck STUN queries to keep // running in magicsock, even when idle. + // Set by [nodecap.DebugForceBackgroundSTUN]. ForceBackgroundSTUN atomic.Bool // DisableDeltaUpdates is whether the node should not process // incremental (delta) netmap updates and should treat all netmap // changes as "full" ones as tailscaled did in 1.48.x and earlier. + // Set by [nodecap.DisableDeltaUpdates]. DisableDeltaUpdates atomic.Bool // PeerMTUEnable is whether the node should do peer path MTU discovery. + // Set by [nodecap.PeerMTUEnable]. PeerMTUEnable atomic.Bool // DisableDNSForwarderTCPRetries is whether the DNS forwarder should // skip retrying truncated queries over TCP. + // Set by [nodecap.DNSForwarderDisableTCPRetries]. DisableDNSForwarderTCPRetries atomic.Bool // SilentDisco is whether the node should suppress disco heartbeats to its // peers. + // Set by [nodecap.SilentDisco]. SilentDisco atomic.Bool // LinuxForceIPTables is whether the node should use iptables for Linux // netfiltering, unless overridden by the user. + // Set by [nodecap.LinuxMustUseIPTables]. LinuxForceIPTables atomic.Bool // LinuxForceNfTables is whether the node should use nftables for Linux // netfiltering, unless overridden by the user. + // Set by [nodecap.LinuxMustUseNfTables]. LinuxForceNfTables atomic.Bool // ProbeUDPLifetime is whether the node should probe UDP path lifetime on // the tail end of an active direct connection in magicsock. + // Set by [nodecap.ProbeUDPLifetime]. ProbeUDPLifetime atomic.Bool // AppCStoreRoutes is whether the node should store RouteInfo to StateStore // if it's an app connector. + // Set by [nodecap.StoreAppCRoutes]. AppCStoreRoutes atomic.Bool // UserDialUseRoutes is whether tsdial.Dialer.UserDial should use routes to determine // how to dial the destination address. When true, it also makes the DNS forwarder // use UserDial instead of SystemDial when dialing resolvers. + // Set by [nodecap.UserDialUseRoutes]. UserDialUseRoutes atomic.Bool // DisableSplitDNSWhenNoCustomResolvers indicates that the node's DNS manager @@ -78,6 +91,7 @@ type Knobs struct { // node's DNS forwarder doesn't need to handle all DNS traffic. // This is for now (2024-06-06) an iOS-specific battery life optimization, // and this knob allows us to disable the optimization remotely if needed. + // Set by [nodecap.DisableSplitDNSWhenNoCustomResolvers]. DisableSplitDNSWhenNoCustomResolvers atomic.Bool // DisableLocalDNSOverrideViaNRPT indicates that the node's DNS manager should not @@ -87,59 +101,67 @@ type Knobs struct { // a Mullvad exit node is being used, resulting in DNS leaks. // We began creating this rule on 2024-06-14, and this knob // allows us to disable the new behavior remotely if needed. + // Set by [nodecap.DisableLocalDNSOverrideViaNRPT]. DisableLocalDNSOverrideViaNRPT atomic.Bool // DisableCaptivePortalDetection is whether the node should not perform captive portal detection // automatically when the network state changes. + // Set by [nodecap.DisableCaptivePortalDetection]. DisableCaptivePortalDetection atomic.Bool // DisableSkipStatusQueue is whether the node should disable skipping // of queued netmap.NetworkMap between the controlclient and LocalBackend. // See tailscale/tailscale#14768. + // Set by [nodecap.DisableSkipStatusQueue]. DisableSkipStatusQueue atomic.Bool // DisableHostsFileUpdates indicates that the node's DNS manager should not create // hosts file entries when it normally would, such as when we're not the primary // resolver on Windows or when the host is domain-joined and its primary domain // takes precedence over MagicDNS. As of 2026-02-13, it is only used on Windows. + // Set by [nodecap.DisableHostsFileUpdates]. DisableHostsFileUpdates atomic.Bool // ForceRegisterMagicDNSIPv4Only is whether the node should only register // its IPv4 MagicDNS service IP and not its IPv6 one. The IPv6 one, - // tsaddr.TailscaleServiceIPv6String, still works in either case. This knob - // controls only whether we tell systemd/etc about the IPv6 one. + // [tailscale.com/net/tsaddr.TailscaleServiceIPv6String], still works in either case. + // This knob controls only whether we tell systemd/etc about the IPv6 one. // See https://github.com/tailscale/tailscale/issues/15404. + // Set by [nodecap.ForceRegisterMagicDNSIPv4Only] // TODO(bradfitz): remove this a few releases after 2026-02-16. ForceRegisterMagicDNSIPv4Only atomic.Bool // EmitRuntimeMetrics is whether the node should poll and emit [runtime/metrics] // as [tailscale.com/util/clientmetric]'s. + // Set by [nodecap.EmitRuntimeMetrics] EmitRuntimeMetrics atomic.Bool - // DisableUDPGRO disables UDP GRO on the magicsock UDP socket. See - // [tailcfg.NodeAttrDisableUDPGRO]. + // DisableUDPGRO disables UDP GRO on the magicsock UDP socket. + // Set by [nodecap.DisableUDPGRO]. DisableUDPGRO atomic.Bool - // DisableUDPGSO disables UDP GSO on the magicsock UDP socket. See - // [tailcfg.NodeAttrDisableUDPGSO]. + // DisableUDPGSO disables UDP GSO on the magicsock UDP socket. + // Set by [nodecap.DisableUDPGSO]. DisableUDPGSO atomic.Bool - // DisableTUNUDPGRO disables UDP GRO on the Tailscale TUN device. See - // [tailcfg.NodeAttrDisableTUNUDPGRO]. + // DisableTUNUDPGRO disables UDP GRO on the Tailscale TUN device. + // Set by [nodecap.DisableTUNUDPGRO]. DisableTUNUDPGRO atomic.Bool - // DisableTUNTCPGRO disables TCP GRO on the Tailscale TUN device. See - // [tailcfg.NodeAttrDisableTUNTCPGRO]. + // DisableTUNTCPGRO disables TCP GRO on the Tailscale TUN device. + // Set by [nodecap.DisableTUNTCPGRO]. DisableTUNTCPGRO atomic.Bool // NeverGSOEqualTail enables a UDP GSO sentinel-tail workaround in the // underlay UDP packet TX path on Linux. Applies to magicsock and peer relay - // UDP sockets. See [tailcfg.NodeAttrNeverGSOEqualTail]. + // UDP sockets. + // Set by [nodecap.NeverGSOEqualTail]. NeverGSOEqualTail atomic.Bool // CacheNetworkMaps is whether the node should persistently cache network // maps and use them to establish peer connectivity on start, if doing so // is supported by the client and storage is available. + // Set by [nodecap.CacheNetworkMaps]. CacheNetworkMaps atomic.Bool // ScopeQuad100OnMacOS lets sandboxed macOS scope quad-100 for simple DNS @@ -147,6 +169,7 @@ type Knobs struct { // Custom split resolvers or unrouted forward/PTR records still require // quad-100 as primary. Off by default; has no effect on other platforms. // See tailscale/corp#45534 and tailscale/corp#48693. + // Set by [nodecap.ScopeQuad100OnMacOS]. ScopeQuad100OnMacOS atomic.Bool } @@ -156,70 +179,47 @@ func (k *Knobs) UpdateFromNodeAttributes(capMap tailcfg.NodeCapMap) { if k == nil { return } - has := capMap.Contains - var ( - disableUPnP = has(nodecap.DisableUPnP) - randomizeClientPort = has(nodecap.RandomizeClientPort) - disableDeltaUpdates = has(nodecap.DisableDeltaUpdates) - oneCGNAT opt.Bool - forceBackgroundSTUN = has(nodecap.DebugForceBackgroundSTUN) - peerMTUEnable = has(nodecap.PeerMTUEnable) - dnsForwarderDisableTCPRetries = has(nodecap.DNSForwarderDisableTCPRetries) - silentDisco = has(nodecap.SilentDisco) - forceIPTables = has(nodecap.LinuxMustUseIPTables) - forceNfTables = has(nodecap.LinuxMustUseNfTables) - probeUDPLifetime = has(nodecap.ProbeUDPLifetime) - appCStoreRoutes = has(nodecap.StoreAppCRoutes) - userDialUseRoutes = has(nodecap.UserDialUseRoutes) - disableSplitDNSWhenNoCustomResolvers = has(nodecap.DisableSplitDNSWhenNoCustomResolvers) - disableLocalDNSOverrideViaNRPT = has(nodecap.DisableLocalDNSOverrideViaNRPT) - disableCaptivePortalDetection = has(nodecap.DisableCaptivePortalDetection) - disableSkipStatusQueue = has(nodecap.DisableSkipStatusQueue) - disableHostsFileUpdates = has(nodecap.DisableHostsFileUpdates) - forceRegisterMagicDNSIPv4Only = has(nodecap.ForceRegisterMagicDNSIPv4Only) - emitRuntimeMetrics = has(nodecap.EmitRuntimeMetrics) - disableUDPGRO = has(nodecap.DisableUDPGRO) - disableUDPGSO = has(nodecap.DisableUDPGSO) - disableTUNUDPGRO = has(nodecap.DisableTUNUDPGRO) - disableTUNTCPGRO = has(nodecap.DisableTUNTCPGRO) - neverGSOEqualTail = has(nodecap.NeverGSOEqualTail) - cacheNetworkMaps = has(nodecap.CacheNetworkMaps) - scopeQuad100OnMacOS = has(nodecap.ScopeQuad100OnMacOS) - ) - if has(nodecap.OneCGNATEnable) { - oneCGNAT.Set(true) - } else if has(nodecap.OneCGNATDisable) { - oneCGNAT.Set(false) + setBool := func(b *atomic.Bool, cap nodecap.Cap) { + b.Store(capMap.Contains(cap)) + } + setOptBool := func(b *syncs.AtomicValue[opt.Bool], enable, disable nodecap.Cap) { + var v opt.Bool + if capMap.Contains(enable) { + v.Set(true) + } else if capMap.Contains(disable) { + v.Set(false) + } + b.Store(v) } - k.DisableUPnP.Store(disableUPnP) - k.RandomizeClientPort.Store(randomizeClientPort) - k.OneCGNAT.Store(oneCGNAT) - k.ForceBackgroundSTUN.Store(forceBackgroundSTUN) - k.DisableDeltaUpdates.Store(disableDeltaUpdates) - k.PeerMTUEnable.Store(peerMTUEnable) - k.DisableDNSForwarderTCPRetries.Store(dnsForwarderDisableTCPRetries) - k.SilentDisco.Store(silentDisco) - k.LinuxForceIPTables.Store(forceIPTables) - k.LinuxForceNfTables.Store(forceNfTables) - k.ProbeUDPLifetime.Store(probeUDPLifetime) - k.AppCStoreRoutes.Store(appCStoreRoutes) - k.UserDialUseRoutes.Store(userDialUseRoutes) - k.DisableSplitDNSWhenNoCustomResolvers.Store(disableSplitDNSWhenNoCustomResolvers) - k.DisableLocalDNSOverrideViaNRPT.Store(disableLocalDNSOverrideViaNRPT) - k.DisableCaptivePortalDetection.Store(disableCaptivePortalDetection) - k.DisableSkipStatusQueue.Store(disableSkipStatusQueue) - k.DisableHostsFileUpdates.Store(disableHostsFileUpdates) - k.ForceRegisterMagicDNSIPv4Only.Store(forceRegisterMagicDNSIPv4Only) - k.EmitRuntimeMetrics.Store(emitRuntimeMetrics) - k.DisableUDPGRO.Store(disableUDPGRO) - k.DisableUDPGSO.Store(disableUDPGSO) - k.DisableTUNUDPGRO.Store(disableTUNUDPGRO) - k.DisableTUNTCPGRO.Store(disableTUNTCPGRO) - k.NeverGSOEqualTail.Store(neverGSOEqualTail) - k.CacheNetworkMaps.Store(cacheNetworkMaps) - k.ScopeQuad100OnMacOS.Store(scopeQuad100OnMacOS) + setBool(&k.DisableUPnP, nodecap.DisableUPnP) + setBool(&k.RandomizeClientPort, nodecap.RandomizeClientPort) + setOptBool(&k.OneCGNAT, nodecap.OneCGNATEnable, nodecap.OneCGNATDisable) + setBool(&k.ForceBackgroundSTUN, nodecap.DebugForceBackgroundSTUN) + setBool(&k.DisableDeltaUpdates, nodecap.DisableDeltaUpdates) + setBool(&k.PeerMTUEnable, nodecap.PeerMTUEnable) + setBool(&k.DisableDNSForwarderTCPRetries, nodecap.DNSForwarderDisableTCPRetries) + setBool(&k.SilentDisco, nodecap.SilentDisco) + setBool(&k.LinuxForceIPTables, nodecap.LinuxMustUseIPTables) + setBool(&k.LinuxForceNfTables, nodecap.LinuxMustUseNfTables) + setBool(&k.ProbeUDPLifetime, nodecap.ProbeUDPLifetime) + setBool(&k.AppCStoreRoutes, nodecap.StoreAppCRoutes) + setBool(&k.UserDialUseRoutes, nodecap.UserDialUseRoutes) + setBool(&k.DisableSplitDNSWhenNoCustomResolvers, nodecap.DisableSplitDNSWhenNoCustomResolvers) + setBool(&k.DisableLocalDNSOverrideViaNRPT, nodecap.DisableLocalDNSOverrideViaNRPT) + setBool(&k.DisableCaptivePortalDetection, nodecap.DisableCaptivePortalDetection) + setBool(&k.DisableSkipStatusQueue, nodecap.DisableSkipStatusQueue) + setBool(&k.DisableHostsFileUpdates, nodecap.DisableHostsFileUpdates) + setBool(&k.ForceRegisterMagicDNSIPv4Only, nodecap.ForceRegisterMagicDNSIPv4Only) + setBool(&k.EmitRuntimeMetrics, nodecap.EmitRuntimeMetrics) + setBool(&k.DisableUDPGRO, nodecap.DisableUDPGRO) + setBool(&k.DisableUDPGSO, nodecap.DisableUDPGSO) + setBool(&k.DisableTUNUDPGRO, nodecap.DisableTUNUDPGRO) + setBool(&k.DisableTUNTCPGRO, nodecap.DisableTUNTCPGRO) + setBool(&k.NeverGSOEqualTail, nodecap.NeverGSOEqualTail) + setBool(&k.CacheNetworkMaps, nodecap.CacheNetworkMaps) + setBool(&k.ScopeQuad100OnMacOS, nodecap.ScopeQuad100OnMacOS) } // AsDebugJSON returns k as something that can be marshalled with json.Marshal diff --git a/control/controlknobs/controlknobs_test.go b/control/controlknobs/controlknobs_test.go index 495535b1e..bd5bd05e7 100644 --- a/control/controlknobs/controlknobs_test.go +++ b/control/controlknobs/controlknobs_test.go @@ -4,21 +4,113 @@ package controlknobs import ( + "go/ast" + "go/constant" + "go/types" + "maps" "reflect" + "regexp" + "slices" "testing" + gcmp "github.com/google/go-cmp/cmp" + "golang.org/x/tools/go/packages" + + "tailscale.com/tailcfg" + "tailscale.com/tailcfg/nodecap" "tailscale.com/types/logger" ) +func TestKnobContainsNodeCapDocComment(t *testing.T) { + cfg := &packages.Config{Mode: packages.NeedSyntax | packages.NeedTypes} + pkgs, err := packages.Load(cfg, ".") + if err != nil { + t.Fatal(err) + } + if len(pkgs) != 1 { + t.Fatalf("loaded too many packages: %v", pkgs) + } + pkg := pkgs[0] + + if len(pkg.Errors) != 0 { + t.Fatalf("loading package: %v", pkg.Errors) + } + + var nodecaps *types.Scope + for _, imp := range pkg.Types.Imports() { + if imp.Path() == reflect.TypeFor[nodecap.Cap]().PkgPath() { + nodecaps = imp.Scope() + break + } + } + if nodecaps == nil { + t.Fatal("nodecap package not imported") + } + + var fields []*ast.Field +Fields: + for _, f := range pkg.Syntax { + for n := range ast.Preorder(f) { + if ts, ok := n.(*ast.TypeSpec); ok && ts.Name.Name == "Knobs" { + fields = ts.Type.(*ast.StructType).Fields.List + break Fields + } + } + } + if fields == nil { + t.Fatal("Knobs fields not found") + } + + base := new(Knobs) + base.UpdateFromNodeAttributes(nil) + baseJSON := base.AsDebugJSON() + + docRef := regexp.MustCompile(`\[nodecap\.(\w+)\]`) + for _, f := range fields { + for _, id := range f.Names { + refs := docRef.FindAllStringSubmatch(f.Doc.Text(), -1) + if len(refs) == 0 { + t.Errorf("%s: doc comment missing link to [nodecap.Cap] for this knob", id.Name) + break + } + + for _, ref := range refs { + nc, ok := nodecaps.Lookup(ref[1]).(*types.Const) + if !ok { + t.Errorf("%s: doc comment names unknown nodecap.%s", id.Name, ref[1]) + continue + } + + k := new(Knobs) + k.UpdateFromNodeAttributes(tailcfg.NodeCapMap{ + nodecap.Cap(constant.StringVal(nc.Val())): nil, + }) + if got := k.AsDebugJSON()[id.Name]; got == baseJSON[id.Name] { + t.Errorf("%s: nodecap.%s did not affect this knob; still %v", id.Name, ref[1], got) + } + } + } + } +} + func TestAsDebugJSON(t *testing.T) { var nilPtr *Knobs if got := nilPtr.AsDebugJSON(); got != nil { t.Errorf("AsDebugJSON(nil) = %v; want nil", got) } + + var wantKeys []string + for f := range reflect.TypeFor[Knobs]().Fields() { + wantKeys = append(wantKeys, f.Name) + } + slices.Sort(wantKeys) + k := new(Knobs) got := k.AsDebugJSON() - if want := reflect.TypeFor[Knobs]().NumField(); len(got) != want { - t.Errorf("AsDebugJSON map has %d fields; want %v", len(got), want) + gotKeys := slices.Sorted(maps.Keys(got)) + + if diff := gcmp.Diff(wantKeys, gotKeys); diff != "" { + t.Errorf("AsDebugJSON keys (-want +got)\n%s", diff) } t.Logf("Got: %v", logger.AsJSON(got)) }