Skip to content

Add accept-time node identity backed by a netmap-mirror cache#50

Merged
danReynolds merged 2 commits into
mainfrom
danreynolds/blissful-mcnulty-d25028
Jun 21, 2026
Merged

Add accept-time node identity backed by a netmap-mirror cache#50
danReynolds merged 2 commits into
mainfrom
danreynolds/blissful-mcnulty-d25028

Conversation

@danReynolds

Copy link
Copy Markdown
Owner

What

Inbound TCP/TLS accepts and Http.bind requests now carry the calling node's identity — conn.identity / request.identity — resolved at accept time without a per-accept LocalAPI round-trip.

final server = await tsnet.http.bind(port: 8080);
await for (final req in server.requests) {
  final id = req.identity; // TailscaleNodeIdentity?
  if (id == null || !id.tags.contains('tag:prod')) {
    await req.respond(statusCode: 403, body: 'forbidden');
    continue;
  }
  await req.respond(body: 'hello ${id.hostName}');
}

Three connected changes

1. Fix LocalAPI loopback latency (~350×). local.Client.DoLocalRequest forks lsof per request (safesocket.readMacosSameUserProof) hunting a macOS GUI credential file that an embedded tsnet process never has. Setting OmitAuth skips it, cutting whois() from ~40 ms to ~230 µs. This was slowing every LocalAPI call (status/prefs/ping), not just whois. A gated diagnostic test (loopback_latency_diag_test.go) localizes the cost and asserts the direction of the win without pinning machine-specific numbers.

2. Netmap-mirror identity cache. The state watcher rebuilds an addr → identity index on every netmap tick; the accept path reads it in ~79 ns / 0 allocs instead of a live WhoIs.

Path Cost Allocs
Loopback (cold) ~40 ms 572
Cached (warm) 79 ns 0

Safety: cold or post-StopWatch lookups fall back to the authoritative loopback WhoIs, so identity is never stale — at worst one netmap behind, never an IP misattribution. WhoIs and the cache share one mapper (nodeIdentityFromView) so they can't drift.

3. HTTP request identity. TailscaleHttpRequest.identity, attached via the same cache-backed lookup — the in-process twin of serve.forward's Tailscale-User-Login headers.

Identity is correctly absent on outbound tcp.dial (caller chose the target), public Funnel callers (no tailnet node), and UDP (connectionless). The user-facing whois() stays on the live — now fast — loopback.

Testing

  • Go unit: cache builder, lookup/invalidate semantics, WhoIs→identity mapping.
  • Go bench (Headscale-gated): loopback vs cached vs pure-map floor.
  • e2e over Headscale: inbound TCP and inbound HTTP both assert the peer's identity over the wire. Full 37-test e2e suite green; dart analyze clean; go vet clean.

🤖 Generated with Claude Code

Inbound TCP/TLS accepts and Http.bind requests now carry the calling
node's identity (conn.identity / request.identity), resolved at accept
time without a per-accept LocalAPI round-trip.

Three connected changes:

- Fix LocalAPI loopback latency. local.Client.DoLocalRequest forks `lsof`
  per request (safesocket.readMacosSameUserProof) hunting a macOS GUI
  credential file an embedded tsnet process never has. Setting OmitAuth
  skips it, cutting whois() from ~40ms to ~0.3ms end to end (Dart-facing).
  A gated diagnostic test (loopback_latency_diag_test.go) localizes the
  cost and asserts the direction of the win without pinning numbers.

- Add a netmap-mirror identity cache (identity_cache.go). The state
  watcher rebuilds an addr->identity index on every netmap tick; the
  accept path reads it in ~79ns/0-alloc instead of a live WhoIs. Cold or
  post-StopWatch lookups fall back to the authoritative loopback WhoIs, so
  identity is never stale: at worst one netmap behind, never an IP
  misattribution. Teardown is race-free: the watcher applies each rebuild
  under watchMu only while its context is live, and stopLocked routes
  through StopWatch, so an in-flight tick can't re-warm a torn-down cache.
  WhoIs and the cache share one mapper (nodeIdentityFromView) so they
  cannot drift.

- Attach identity to inbound HTTP requests (Http.bind) via the same
  cache-backed lookup, exposed as TailscaleHttpRequest.identity -- the
  in-process twin of serve.forward's Tailscale-User-Login headers.

Identity is correctly absent on outbound tcp.dial, public Funnel callers,
and UDP. The user-facing whois() stays on the live (now-fast) loopback.
A warm-cache miss is authoritative (no fallback) to keep the accept path
O(1) and non-DoS-able; *nodeIdentity values are immutable once built since
they are aliased across cache keys and read by concurrent accepts.

Tests: Go unit (cache builder + lookup semantics + WhoIs mapping), Go
bench (loopback vs cached vs map floor), race detector over the watcher
and teardown paths, and e2e over Headscale for both inbound TCP and
inbound HTTP identity. Full e2e suite green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@danReynolds danReynolds force-pushed the danreynolds/blissful-mcnulty-d25028 branch from b0502f4 to aeae68b Compare June 21, 2026 19:05
Accept-time node identity (TCP/TLS conn.identity, Http.bind
request.identity) plus the LocalAPI loopback latency fix.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@danReynolds danReynolds merged commit e265960 into main Jun 21, 2026
3 checks passed
@danReynolds danReynolds deleted the danreynolds/blissful-mcnulty-d25028 branch June 21, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant