Skip to content

feat(tools): socket options, bulk and close modes for tcp_client/tcp_server - #329

Merged
randomizedcoder merged 12 commits into
mainfrom
feat/tcp-tool-sockopts
Nov 25, 2026
Merged

randomizedcoder merged 12 commits into
mainfrom
feat/tcp-tool-sockopts

Conversation

@randomizedcoder

@randomizedcoder randomizedcoder commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

The golden microVM workload only makes idle ESTABLISHED loopback sockets with the default congestion algorithm, so many columns never get a value (congestion info, tos/tclass, class_id, sockopt, window scales, non-ESTABLISHED states). This gives tcp_client/tcp_server the knobs a wider workload needs. No workload change here. Done test-first.

Feature

  • tools/internal/sockopt, shared by both tools, set via Control before connect/listen:
    • -cong NAME (TCP_CONGESTION)
    • -tos N (IP_TOS, plus IPV6_TCLASS on IPv6)
    • -priority N (SO_PRIORITY; inet_diag_class_id falls back to it)
    • -rcvbuf SIZE (SO_RCVBUF; sets the window scale)
    • -freebind, -transparent, -bindNoPort (the inet_diag_sockopt bits)
  • The server sets the options again on each accepted socket (ApplyConn): sk_clone zeroes sk_priority (net/core/sock.c), and with net.ipv4.tcp_reflect_tos the child takes the SYN's TOS (net/ipv4/tcp_ipv4.c). The congestion algorithm is inherited
  • SOL_IP options work on IPv6 TCP sockets too (ipv6_setsockopt hands them to ip_setsockopt), so only the traffic class differs by family
  • tcp_client:
    • -bulk SIZE sends that much and reads the echo back before the first message
    • -close closes after one exchange (TIME_WAIT)
    • -halfclose shuts down writing and holds the socket (FIN_WAIT2)
    • -dialTimeout (was a fixed 1s): longer than the kernel's SYN retries, it holds a SYN_SENT socket to a peer that never answers
  • tcp_server -holdEOF keeps a connection after the client's EOF (CLOSE_WAIT)
  • Flag parsing moves to parseFlags in both tools
  • The OCI entrypoint passes TCP_CLIENT_ARGS / TCP_SERVER_ARGS to each half; one shared variable would hand client-only flags to the server in both mode
  • tools/internal/procnet reads TCP states from /proc/net/tcp{,6} for the tests
  • docs/integration-testing.md and CHANGELOG

Tests

  • Table-driven, all four categories: TestParseSize, TestValidate, TestControlApplied (real loopback sockets, getsockopt read-back), TestParseStates, TestStateString, TestParseClientFlags, TestParseServerFlags, TestBulk, TestCloseModes, TestHoldEOF (states read from /proc/net/tcp)
  • -race clean; -count=10 three times clean
  • The transparent row checks the error when it lacks CAP_NET_ADMIN
  • One row changed after the red commit: accepted_inherits assumed a listener's TOS and priority reach accepted sockets; the kernel says otherwise (above), so it now pins that and accepted_reapplied checks ApplyConn
  • End to end on loopback: reno on both ends, 1 MiB bulk echoed, FIN-WAIT-2 / CLOSE-WAIT, client rcv_wscale 1 with -rcvbuf 64KiB

Red (tests commit, before the feature)

tools/internal/sockopt/sockopt_test.go:19:8: undefined: Size
tools/internal/procnet/procnet_test.go:18:12: undefined: State
tools/tcp_client/options_test.go:18:7: undefined: closeMode
tools/tcp_server/options_test.go:46:14: undefined: parseFlags

-dialTimeout, a second red/green pair:

tools/tcp_client/options_test.go:51:75: c.dialTimeout undefined (type clientConfig has no field or method dialTimeout)

Review fixes (each red first)

  • -close read the echo once into a 3000-byte buffer, so a longer echo stayed queued and the close reset the socket (no TIME_WAIT). clientOnce now reads the whole echo (readEcho), discarding past the buffer. Red, TestCloseModes/close_large_echo (8 KiB echo):
close_large_echo [corner] an echo longer than one read is read whole before the close, else the close resets: {server:0 bulk:0 mode:1 pads:8192}: client port 20818 states [], returned true (<nil>); expected TIME_WAIT, returned true, error containing ""
  • procnet.Lookup matched only the local port; at -count=5 a reused ephemeral port still held an earlier row's TIME_WAIT socket (states [TIME_WAIT TIME_WAIT]). It now takes the remote port too (0 = any); TestParseStates gains remote_match, remote_no_match, remote_any, remote_bad

Green

go test ./... and the nix checks (gofmt, go-vet, nix-fmt, lint-baseline, golangci ×3, redteam-lint, golden-records, test-fuzz-all) pass. Includes the nix/default.nix nixfmt commit from #326, since main fails checks.nix-fmt.

🤖 Generated with Claude Code

randomizedcoder and others added 6 commits November 24, 2026 14:49
…server (red)

Table-driven, all four categories: sockopt ParseSize/Validate/Control
on real loopback sockets, procnet state parsing, client and server flag
parsing, bulk echo, close modes (TIME_WAIT, FIN_WAIT2, CLOSE_WAIT read
back from /proc/net/tcp).

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

- tools/internal/sockopt: -cong, -tos (IPV6_TCLASS too on v6), -priority,
  -rcvbuf, -freebind, -transparent, -bindNoPort, set via Control before
  connect/listen. ApplyConn sets them again on accepted sockets: sk_clone
  zeroes sk_priority, and tcp_reflect_tos replaces the listener's TOS.
- tcp_client: -bulk SIZE, -close (TIME_WAIT), -halfclose (FIN_WAIT2);
  flag parsing moves to parseFlags.
- tcp_server: -holdEOF keeps a connection after EOF (CLOSE_WAIT).
- tools/internal/procnet reads states from /proc/net/tcp{,6} for the tests.
- OCI entrypoint: TCP_CLIENT_ARGS / TCP_SERVER_ARGS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main fails checks.nix-fmt on the binaries inherit added in #324.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first dial attempt's timeout was fixed at 1s. Longer than the kernel's
SYN retries, it holds a SYN_SENT socket to a peer that never answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
randomizedcoder and others added 6 commits November 24, 2026 19:25
Review: an 8 KiB echo is read once, the close finds data unread and
resets, and no TIME_WAIT socket is left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clientOnce read once into a 3000-byte buffer, so an 8 KiB echo left
data queued; -close then reset the socket instead of leaving it in
TIME_WAIT. readEcho reads the full echo, discarding past the buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A reused ephemeral port can carry an earlier test's TIME_WAIT socket,
so TestCloseModes -count=5 saw two states on the client port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… socket is not counted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@randomizedcoder
randomizedcoder merged commit 0f8ecf5 into main Nov 25, 2026
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