Skip to content

fix(xtcp): collect IPv6 sockets (AF_INET6 dump), test-first - #320

Merged
randomizedcoder merged 15 commits into
mainfrom
fix/ipv6-collection
Nov 24, 2026
Merged

randomizedcoder merged 15 commits into
mainfrom
fix/ipv6-collection

Conversation

@randomizedcoder

Copy link
Copy Markdown
Owner

xtcp2 only asked the kernel for AF_INET sockets, so every AF_INET6 socket went unrecorded, including Go's dual-stack 0.0.0.0 listeners and their accepted connections. Done test-first.

Includes #319's commits (the golden harness this builds on); the diff shrinks once #319 merges.

Fix

  • Each poll dumps AF_INET, then AF_INET6, on each namespace's netlink socket. The AF_INET6 dump is sent on the first DONE (a send mid-dump gets EBUSY), and poll_end still means every namespace finished. AF_UNSPEC isn't possible (sock_diag.c dispatches on family)
  • -maxDumpBytes / -maxRecordsPerNetns are one budget across both dumps
  • Poll gate adds sockstat6 (TCP6: inuse); 5 fds per namespace with the gate on
  • Pre-existing bug found by the new VM check: records sliced their addresses out of the pooled receive buffer, so any multi-packet dump could give earlier records a later socket's address. Records now copy into buffers that recycleRecord keeps; 102k-socket dump 44k allocs (229k with a plain copy, 24k aliasing), same time
  • v4-mapped peers (::ffff:a.b.c.d) read as IPv4 in treatment attribution, discovery, analysis and golden scrubbing
  • tools/tcp_server, tcp_client: -network tcp|tcp4|tcp6, IPv6 literals via JoinHostPort

Tests

  • Unit tables: TestCreateNetLinkRequests, TestPollRequestsBothFamilies, TestPollEndWaitsForBothDumps, TestDumpTrackerSharedBudget, TestRealDumpFamilies (real NETLINK_SOCK_DIAG), TestGateCountsIPv6, TestSockIDXTCPOwnsAddresses, TestRecycleRecord, tool listen/dial tables, TestIndexV4Mapped, TestDiscoveryV4Mapped, TestAnalysisV4Mapped (clickhouse local), TestNormalizeV4Mapped
  • New microVM nix run .#microvm-x86_64-tcp-families: v4-only, v6-only, dual-stack containers and a v6 named netns, rows per family vs ss -4/ss -6

Red (tests commit, before the fix)

ROW v4_counts positive PASS fam-v4 v4=12/12 v6=0/0
ROW v6_counts positive FAIL fam-v6 v4=0/0 v6=0/12
ROW dual_counts positive FAIL fam-dual v4=4/4 v6=0/8 accepted-v4-mapped=0/4
ROW netns_v6 positive FAIL fam-ns v4=0/0 v6=0/3
ROW host_ns corner FAIL host v4=8/8 v6=0/1

Green

ROW v4_counts positive PASS fam-v4 v4=12/12 v6=0/0
ROW v6_counts positive PASS fam-v6 v4=0/0 v6=12/12
ROW dual_counts positive PASS fam-dual v4=4/4 v6=8/8 accepted-v4-mapped=4/4
ROW netns_v6 positive PASS fam-ns v4=0/0 v6=3/3
ROW family_mismatch negative PASS AF_INET rows with a non-IPv4 source: 0
ROW two_polls boundary PASS poll_end=2 poll_timeout=0
ROW host_ns corner PASS host v4=8/8 v6=1/1

(the first green run failed family_mismatch with 6 rows: the aliasing bug)

Verified

  • Golden fixture recaptured: 48 rows (was 23); two captures normalize byte-identical, xtcp2-golden diff clean
  • microVMs: tcp-families, lifecycle (OVERALL_PASS), socket-scale (OVERALL_PASS)
  • go test ./...; gofmt, go-vet, nix-fmt, lint-baseline, golangci quick/default/comprehensive, redteam-lint, golden-records, test-fuzz-all

🤖 Generated with Claude Code

randomizedcoder and others added 15 commits November 23, 2026 15:03
…ze, diff

- pkg/goldenrec: every ParquetRow column in exactly one class (strict,
  presence, ordinal, address, port, volatile, host); Normalize, Scrub
  (Normalize-invariant) and Diff with a column filter, for FP-9 and FP-12.
- cmd/xtcp2-golden: scrub, normalize, diff and check.
- s3parquet-golden microVM flavor and nix run .#capture-golden-records:
  the production binary, 3 Docker containers, a named netns; keeps the
  second poll; metrics over a unix listener so no host TCP is recorded.
- Fixture pkg/xtcp/testdata/golden/7_3_1 (23 rows, scrubbed); two
  independent captures normalize byte-identical.
- checks.golden-records: the JSONL matches the Parquet, the scrub is
  clean, the manifest row count and the schema match.
- Golden rows seed FuzzRowFromProto; protoFromRow round-trip test.
- Plan doc: F0.5 done; F2 TODOs for AF_INET6 collection and the
  ambiguous nsid 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xtcp2 sends one SOCK_DIAG_BY_FAMILY request, AF_INET, so the kernel
never reports AF_INET6 sockets: pure IPv6, and every dual-stack socket
(a Go listener on 0.0.0.0 or [::], its accepted connections). These
tests pin the fix and fail on today's code, only on their IPv6 rows:

- TestCreateNetLinkRequests: an AF_INET and an AF_INET6 request.
- TestPollRequestsBothFamilies / TestPollEndWaitsForBothDumps: each
  namespace gets AF_INET, then AF_INET6 after the first DONE (a send
  during a dump is EBUSY); the poll ends after the second DONE.
- TestDumpTrackerSharedBudget: -maxRecordsPerNetns spans both dumps.
- TestRealDumpFamilies: a real sock_diag dump of loopback sockets.
- TestListenAddress / TestDialAddress: the stress tools and IPv6.
- TestIndexV4Mapped, TestDiscoveryV4Mapped (clickhouse local),
  TestNormalizeV4Mapped: a v4-mapped peer is the IPv4 peer.

Seams only, behavior unchanged: createNetLinkRequests over
dumpFamilies (AF_INET), listenTarget / dialTarget, decodeCandidate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test infrastructure for the IPv6 collection work: the stress tools
built "host:port" with "%s:%d", so ::1 could not be used, and always
used network "tcp". Now net.JoinHostPort, a -network flag (default
tcp, unchanged) checked against the address family of -bind,
-connect and -srcaddr, and a port range check. The stress image takes
TCP_NETWORK. TestListenAddress and TestDialAddress pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nix run .#microvm-x86_64-tcp-families boots the production binary
over IPv4-only (tcp4), IPv6-only (tcp6) and dual-stack containers and
an IPv6 named netns, keeps xtcp2's second poll as the golden capture
does, and compares each unit's rows per inet_diag_msg_family with
ss -4 / ss -6. The metric waits and the object fetch move from
golden-capture.sh to golden-common.sh, shared by both.

Red on this tree, as expected (xtcp2 dumps AF_INET only):
  v4_counts     PASS fam-v4   v4=12/12 v6=0/0
  v6_counts     FAIL fam-v6   v4=0/0   v6=0/12
  dual_counts   FAIL fam-dual v4=4/4   v6=0/8 accepted-v4-mapped=0/4
  netns_v6      FAIL fam-ns   v4=0/0   v6=0/3
  host_ns       FAIL host     v4=8/8   v6=0/1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each poll now sends every namespace's socket an AF_INET dump and, once
its NLMSG_DONE is in, an AF_INET6 dump: the kernel answers a
SOCK_DIAG_BY_FAMILY request only with its family's sockets, and a
second request during a running dump gets EBUSY with no DONE.

- createNetLinkRequests builds both; the AF_INET6 one differs only in
  sdiag_family. The gate builds each level for both families.
- handleNetlinkerDone sends a socket's queued AF_INET6 dump and keeps
  the namespace counted, so poll_end waits for each namespace's last
  dump; a failed send ends the namespace; a timeout clears the queue.
- Both dumps carry the poll's sequence number, so -maxDumpBytes and
  -maxRecordsPerNetns stay the namespace's share of a poll: the
  receiver restarts its budget at a new sequence number, not at every
  NLMSG_DONE. The cap tests' "next dump" rows now use the next poll's.

TestCreateNetLinkRequests, TestPollRequestsBothFamilies,
TestPollEndWaitsForBothDumps, TestDumpTrackerSharedBudget and
TestRealDumpFamilies pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gate sized each namespace's dump from /proc/net/sockstat, which
counts only AF_INET TCP sockets. A namespace holding only IPv6 sockets
looked empty and was skipped once the dump requests covered AF_INET6.

- open /proc/thread-self/net/sockstat6 alongside sockstat per namespace
  and add its "TCP6: inuse" to the namespace's demand (tw is shared
  across families in sockstat, so it is not added twice)
- a sockstat6 fd that is open but unreadable makes the namespace unknown
  (polled in full); a missing one adds nothing
- fdsPerNetns with the gate on goes from 4 to 5

TestGateCountsIPv6 (v6_heavy, no_sockstat6, sockstat6_read_fails,
v6_zero, tcp6_line_not_first) is new; it reaches the sockstat6 handle,
so it does not build without this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DeserializeInetDiagSockIDXTCP sliced the source and destination out of
the netlink receive buffer. That buffer goes back to the pool once its
packet is decoded and is reused for the next packet, while the record
waits in the envelope for the poll's flush, so a record could carry a
later socket's address. The tcp-families microVM caught it once a poll
ran two dumps per namespace: AF_INET rows carried AF_INET6 bytes.

- copy both addresses into buffers the record owns; a new record gets
  one 32-byte array for the pair
- recycleRecord (flushEnvelope, the post-flush drop) resets a record for
  the pool but keeps those buffers, so a recycled record decodes its
  addresses without allocating

TestSockIDXTCPOwnsAddresses failed before the copy (buffer_reused,
family_switch). Benchmarks, 102k-socket dump, 1 netlinker:
- aliasing (before): 24.9k allocs/dump, 106 ms
- copy + Reset: 229k allocs/dump
- copy + recycleRecord + shared array: 44.3k allocs/dump, 105 ms
- BenchmarkDeserialize unchanged at 1 alloc/op

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dual-stack (AF_INET6) socket talking to an IPv4 peer reports the peer
as ::ffff:a.b.c.d. Stamp looked that up as-is, so once xtcp2 dumped
AF_INET6 those rows were never attributed. Unmap before the route
lookup.

TestIndexV4Mapped failed before this change on mapped_matches_v4 and
mapped_zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dual-stack (AF_INET6) socket reports an IPv4 peer as ::ffff:a.b.c.d.
With AF_INET6 now collected, discovery grouped those sockets apart from
the AF_INET ones and reported the peer as an IPv6 /128, and analysis
(matching the candidate's IPv4 hex) never measured them.

- both queries derive peer_hex: a v4-mapped AF_INET6 destination becomes
  the IPv4 address left-aligned, as AF_INET rows store it
- discovery groups and reports on peer_hex (family 2); analysis matches
  the candidate on it
- the clickhouse-local tests share the table DDL and runner

Red before: TestDiscoveryV4Mapped one_peer_one_candidate, mapped_zero,
all_mapped; TestAnalysisV4Mapped v4_and_mapped, mapped_only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With AF_INET6 collected, a dual-stack socket's IPv4 peer appears as
::ffff:a.b.c.d. Normalize/Scrub treated that as any IPv6 address and
mapped it into 2001:db8::/32, so the same peer got two unrelated numbers.

- new addrMapped kind: numbered in the IPv4 sequence (by its unmapped
  address) and written as ::ffff:192.0.2.N
- ::ffff:0.0.0.0 is kept like 0.0.0.0 (netip's IsUnspecified does not
  unmap; IsLoopback already does)
- CheckScrubbed accepts ::ffff:192.0.2.0/120

Red before: TestNormalizeV4Mapped v4_mapped_doc, v4_mapped_zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xtcp2 now dumps AF_INET6, so the Go servers' dual-stack listeners and
the connections they accept are recorded. Compare each container's rows
with ss -Htan (both families) instead of ss -4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- golden fixture 7_3_1: 48 rows (was 23), 12 per container (4 AF_INET
  client ends, 8 AF_INET6 dual-stack listeners and accepted sockets),
  3 in golden-ns, 9 on the host; two captures normalize byte-identical
- CHANGELOG: IPv6 collection, the receive-buffer aliasing fix
- nix/README: tcp-families check; golden notes for both families
- plan doc: close the AF_INET6 TODOs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…test

- pollWith logs a failed send at debug instead of discarding it
- TestSendNetlinkDumpRequest_errorPath asserts the error is returned
- parseSockstat's line search loops on the TCP prefix (QF1006)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@randomizedcoder
randomizedcoder merged commit e0a7901 into main Nov 24, 2026
1 check passed
@randomizedcoder
randomizedcoder deleted the fix/ipv6-collection branch November 24, 2026 06:04
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