From a3d0c1a4ae2b59288dcc01e7b1b9fdf6deb62057 Mon Sep 17 00:00:00 2001 From: Adriano Sela Aviles Date: Tue, 6 Oct 2026 14:26:38 -0700 Subject: [PATCH] x Signed-off-by: Adriano Sela Aviles --- ipn/ipnlocal/local.go | 52 ++++++++++++++++++++++++++++++++++++++ ipn/ipnlocal/serve.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 2f6de2f114..b3d5e3427b 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -386,6 +386,13 @@ type LocalBackend struct { serveConfig ipn.ServeConfigView // or !Valid if none ipVIPServiceMap netmap.IPServiceMappings // map of VIPService IPs to their corresponding service names; TODO(nickkhyl): move to nodeBackend + // Diagnostic for the tailscale/corp#44108 VIP services startup race: log the + // first time the serve config is loaded from the store and the first time a + // c2n GET /vip-services is answered, to confirm their relative ordering. A c2n + // response before the first store load reports port-less services to control. + logFirstServeConfigLoadOnce sync.Once + logFirstC2NVIPServicesOnce sync.Once + webClient webClient webClientListeners map[netip.AddrPort]*localListener // listeners for local web client traffic @@ -7475,6 +7482,7 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) { b.setTCPPortsInterceptedFromNetmapAndPrefsLocked(b.pm.CurrentPrefs()) if buildfeatures.HasServe { m := nm.GetIPVIPServiceMap() + logVIPServiceIPChanges(b.logf, b.pm.CurrentPrefs().AdvertiseServices(), oldNetMap.GetVIPServiceIPMap(), nm.GetVIPServiceIPMap()) b.ipVIPServiceMap = m if ns, ok := b.sys.Netstack.GetOK(); ok { ns.UpdateIPServiceMappings(m) @@ -7551,6 +7559,50 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) { } } +// logVIPServiceIPChanges logs when control changes the VIP addresses assigned to +// a service between netmaps, to test the hypothesis that control sometimes +// reassigns a service a different VIP than the one it was already bound to +// (tailscale/corp#44108). advertised is the node's currently advertised services; +// a service that loses its VIP but is no longer advertised was deliberately +// unadvertised, not revoked, so it is not logged. Diagnostic only. +func logVIPServiceIPChanges(logf logger.Logf, advertised views.Slice[string], oldMap, newMap tailcfg.ServiceIPMappings) { + for svc, newAddrs := range newMap { + oldAddrs := oldMap[svc] + if addrsEqualUnordered(oldAddrs, newAddrs) { + continue + } + if len(oldAddrs) == 0 { + continue // first assignment, not a change + } + logf("serve: VIP service %q IP assignment changed: %v -> %v", svc, oldAddrs, newAddrs) + } + for svc, oldAddrs := range oldMap { + if len(oldAddrs) == 0 { + continue + } + if _, ok := newMap[svc]; ok { + continue + } + if !advertised.ContainsFunc(func(s string) bool { return tailcfg.ServiceName(s) == svc }) { + continue // no longer advertised: a deliberate unadvertise, not a revocation + } + logf("serve: VIP service %q lost its IP assignment while still advertised (was %v)", svc, oldAddrs) + } +} + +// addrsEqualUnordered reports whether a and b contain the same addresses, +// ignoring order. +func addrsEqualUnordered(a, b []netip.Addr) bool { + if len(a) != len(b) { + return false + } + as := slices.Clone(a) + bs := slices.Clone(b) + slices.SortFunc(as, func(x, y netip.Addr) int { return x.Compare(y) }) + slices.SortFunc(bs, func(x, y netip.Addr) int { return x.Compare(y) }) + return slices.Equal(as, bs) +} + // HookSetRuntimeMetricsEnabled is an optional hook for the "runtimemetrics" feature. var HookSetRuntimeMetricsEnabled feature.Hook[func(enabled bool)] diff --git a/ipn/ipnlocal/serve.go b/ipn/ipnlocal/serve.go index b83a716e3f..1914fe1124 100644 --- a/ipn/ipnlocal/serve.go +++ b/ipn/ipnlocal/serve.go @@ -542,6 +542,9 @@ func (b *LocalBackend) vipServicesFromPrefsLocked(prefs ipn.PrefsView) []*tailcf services[sn].Active = true } + // Diagnostic for the tailscale/corp#44108 startup race. + b.logVIPServicesStartupRaceLocked(prefs, services) + servicesList := slicesx.MapValues(services) // [slicesx.MapValues] provides the values in an indeterminate order, but since we'll // be hashing a representation of this list later we want it to be in a consistent @@ -552,6 +555,45 @@ func (b *LocalBackend) vipServicesFromPrefsLocked(prefs ipn.PrefsView) []*tailcf return servicesList } +// logVIPServicesStartupRaceLocked logs when a VIP services report would omit an +// advertised service's ports only because the serve config isn't loaded from the +// store yet, while the store does define ports for it — the tailscale/corp#44108 +// startup race. Diagnostic only; it does not change the report. In steady state +// serveConfig is valid and this never reads the store. b.mu must be held. +func (b *LocalBackend) logVIPServicesStartupRaceLocked(prefs ipn.PrefsView, reported map[tailcfg.ServiceName]*tailcfg.VIPService) { + if b.serveConfig.Valid() || prefs.AdvertiseServices().Len() == 0 { + return + } + pid := b.pm.CurrentProfile().ID() + if pid == "" { + return + } + confj, err := b.store.ReadState(ipn.ServeConfigKey(pid)) + if err != nil { + return // no persisted config: a port-less report is expected, not the race + } + var stored ipn.ServeConfig + if err := json.Unmarshal(confj, &stored); err != nil { + return + } + + for _, s := range prefs.AdvertiseServices().All() { + sn := tailcfg.ServiceName(s) + if rep := reported[sn]; rep != nil && len(rep.Ports) > 0 { + continue // about to report with ports: not the race + } + storedCfg, ok := stored.Services[sn] + if !ok || storedCfg == nil { + continue + } + storedPorts := storedCfg.View().ServicePortRange() + if len(storedPorts) == 0 { + continue + } + b.logf("[unexpected] serve: VIP service %q reported port-less during startup; store has ports %v", sn, storedPorts) + } +} + type serviceMeteredConn struct { net.Conn inbound, outbound *usermetric.MultiLabelMap[serveLabels] @@ -1487,6 +1529,10 @@ func handleC2NVIPServicesGet(b *LocalBackend, w http.ResponseWriter, r *http.Req res.VIPServices = b.VIPServices() res.ServicesHash = vipServiceHash(b.logf, res.VIPServices) + b.logFirstC2NVIPServicesOnce.Do(func() { + b.logf("serve: first c2n GET /vip-services answered at %v with hash %q", b.clock.Now().Format(time.RFC3339Nano), res.ServicesHash) + }) + w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(res) } @@ -1670,6 +1716,9 @@ func (b *LocalBackend) reloadServeConfigLocked(prefs ipn.PrefsView) { }) b.serveConfig = conf.View() + b.logFirstServeConfigLoadOnce.Do(func() { + b.logf("serve: first serve config loaded from store at %v", b.clock.Now().Format(time.RFC3339Nano)) + }) b.updateCertRefreshLoopLocked() } @@ -1702,6 +1751,16 @@ func (b *LocalBackend) setVIPServicesTCPPortsInterceptedLocked(svcPorts map[tail } } + // Inverse of the loop above: a service that control HAS assigned a VIP but for + // which we have no local ports. Netstack will accept the connection on the VIP + // and then find no TCP handler, so the service appears up but refuses traffic — + // the portless state at the data plane. + for svcName := range vipServiceIPMap { + if len(svcPorts[svcName]) == 0 { + b.logf("[unexpected] serve: VIP service %q has a VIP assigned but no local ports to serve; connections will be refused", svcName) + } + } + b.shouldInterceptVIPServicesTCPPortAtomic.Store(generateInterceptVIPServicesTCPPortFunc(svcAddrPorts)) }