Skip to content

net: the connection engine's C++ core (ARIADNE_NET_ENGINE N1) - #1392

Merged
transfix merged 2 commits into
masterfrom
feat/net-engine-n1
Nov 21, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/net-engine-n1

Conversation

@transfix

Copy link
Copy Markdown
Owner

Part N1 of the connection engine in docs/roadmap/ARIADNE_NET_ENGINE.md (#1294): the engine's C++ core. Plain TCP only. TLS is N3, pycvc and arisrvx are N2, client connections and UDP are N4.

What it is

cvc::net::net_engine (inc/cvc/net/net_engine.h, src/cvc/net/net_engine.cpp):

  • Threads. The caller owns the engine; there is no singleton. io_threads Asio io_contexts, 2 by default and at most one per core. A connection lives on one of them for its whole life (its id says which), so nothing inside a connection takes a lock.
  • Listeners. Address, port, name, backlog (1024), max_frame, max_queued (4 MiB), no_delay, flush_timeout, and inbound limits: bytes and frames per window, and the first frame's deadline.
  • Framing. delimited(byte) (the frame includes the delimiter), length_prefixed(1|2|4) (the frame is the payload) and raw (one read, at most max_frame). A frame past max_frame closes with frame_too_long as soon as that is known. set_framing applies from the next byte not yet split.
  • Input. Events (accepted, frames, closed, writable, error) go to one queue. wait(batch, timeout) and poll(batch) take everything queued as a net_batch, whose frames are string_views into refcounted buffers the batch holds until it is cleared. set_notify is the hook for N2's waker. A read is copied once into a buffer sized to it, so an idle connection holds no read buffer.
  • Output. send, send_many and broadcast(group, bytes, exclude) copy the bytes once, and each receiver's queue holds a reference. Commands are queued under one lock, so every I/O thread sees them in issue order. A broadcast goes to each thread once, and each thread walks its own share of the group. What reaches one connection in one turn of its thread goes out in one gathered write.
  • Backpressure. Past max_queued, the connection closes with slow_reader. want_writable(conn, low_mark) asks for one writable event.
  • Closing. close(conn, flush). A flushing close ends our side once the output is out and posts closed then. A small lingerer reads and drops input until the peer ends its side (bounded by flush_timeout), so unread input does not turn the close into a reset that loses the output.
  • Groups. join and leave; a close leaves every group.
  • Counters. stats() reports per engine, per listener and per thread: open, accepted, closed by reason, bytes and frames in, bytes and messages out, broadcasts and receivers, queued bytes (now and the maximum), and busy time. Each thread writes only its own block; stats() sums them.
  • Browser builds get stubs: available() is false and listen() fails.

Seams for later parts: listener_options and conn_info say where N3's TLS fields go. A connection's socket I/O is three calls (wait for readiness, read_some, write_some) for N3 to wrap. N4's client connections would reuse connection with a connect in place of the accept.

What the design got wrong (the doc is fixed in this PR)

  1. Switching framing "between frames" races with read-ahead. The I/O threads split input as it arrives, so a switch the program makes after seeing a frame comes too late for bytes the peer sent right behind it. NMDC's $ADCSND and the file's bytes arrive in one write. Two framing rules were added:

    • pause_after: frame prefixes after which the engine stops splitting and reading that connection until the next set_framing.
    • raw(n): n bytes, then the same pause.

    NetEngineFraming.APausedConnectionSwitchesFramingExactlyAtTheFrame sends $Hello|$ADCSND … 16| plus 16 binary bytes containing |, plus $Next|$Last|, all in one write. The test gets each part exactly.

  2. §4's "under 10 ms of I/O time for a message to 6000" is below the floor here. On this machine one write() to a loopback TCP socket costs 16.5 µs on its own, because the sender's syscall also runs the receiving side's stack. So 6000 receivers cost about 100 ms of CPU, however the engine is written. The engine's own overhead is about 1 µs a receiver. §4 now has the measured numbers and what they mean for N2's soak.

  3. A flushing close waited for the peer's FIN before telling the program. Now the program hears at once and the lingering goes on in the background (§2.4).

Fan-out microbenchmark

NetEngineFanOut.OneMessageToManyLoopbackReceivers: 6000 loopback connections, 113-byte line, 21 rounds after a warm-up, medians. Run on prettyhatemachine (Xeon E5-2650 v2 @ 2.6 GHz, 32 threads, Linux 7.0, retpolines) with the machine otherwise idle. The test's two reader threads run on the same box.

io_threads broadcast() call I/O threads' CPU per receiver all 6000 received
1 14.8 µs 103.8 ms 17.3 µs 103.9 ms
2 (default) 17.8 µs 105.7 ms 17.6 µs 53.9 ms
4 31.0 µs 130.9 ms 21.8 µs 44.4 ms
8 50.1 µs 152.0 ms 25.3 µs 25.4 ms
  • Floor. A bare C loop of write()s to the same 6000 sockets: 99-103 ms, 16.5 µs a write, the same at N=100.
  • Against sublevels' soak (M1: ncdc and AirDC++ interop, and a 6000-user soak, in CI cy-pca/sublevels#8, 35 µs a connection on asyncio's loop): the I/O cost a receiver halves, and the program's loop pays one ~20 µs call instead of 0.2 s.
  • Bursts. 10 broadcasts issued in a row reach each receiver as one gathered write: 5.0 µs a receiver a message, against 42.8 µs for a single broadcast in the same run. That run was on a loaded machine (load average 38, another build), so read it as a ratio, about 8.5x; it is not an absolute number.

CVC_NET_FANOUT_N and CVC_NET_FANOUT_THREADS set N and the thread count. Under TSan, N is 300; on Windows the default is 1000 (see below).

Tests

net_engine_test (23 cases) covers:

  • framing in all three modes, with frames split across reads (down to a byte at a time) and many in one read;
  • oversized frames in both modes;
  • the paused switch, and a plain set_framing;
  • send/broadcast order on two connections on different threads;
  • a join right before a broadcast;
  • groups, exclusion, leave, and a close leaving its groups;
  • send_many;
  • slow-reader closes and writable at the mark;
  • flushing closes with unread input, plain closes, and peer closes;
  • first-frame, bytes and frames limits;
  • counters and ids;
  • bind failures;
  • stop();
  • batch buffer lifetime;
  • the benchmark.

The sanitizer lanes (tsan, asan-ubsan, clang-tsan) run it, and SAN_REQUIRED_NET makes 8 of its cases required in each leg.

Local runs:

  • 23/23 through ctest in a Release CMake build (gcc 13).
  • 23/23 three times under gcc's -fsanitize=thread, with no reports.
  • clang-format 18 clean.

Not done here, follow-ups

  • Windows reads. Reads wait for readiness (async_wait) and then read into the thread's buffer, which is what keeps idle connections free of read buffers. On Windows, Asio implements async_wait with a select() reactor thread beside IOCP, which is portable but slow with thousands of sockets. A Windows read path with async_read_some and per-connection buffers is the fix if Windows hubs matter. It builds and should pass there; CI will say.
  • Listener counters for broadcasts and their receivers are engine-wide only; per listener they read 0.
  • io_uring (§5) is still worth measuring after N2. It would batch the syscalls, but most of a loopback write is the TCP stack.
  • The registry tie-in (§2.6) waits on the seam asked of paws.

Do not merge without Joe's OK.

cvc::net::net_engine: a caller-owned engine of Boost.Asio I/O threads that
accepts TCP connections, splits their input into delimiter, length-prefixed
or raw frames, and queues their output. send, send_many and broadcast copy
the bytes once into a refcounted buffer that each receiver's queue shares;
commands apply in issue order, so a connection's bytes keep the order of its
sends, broadcasts and closes. Groups (join, leave; a close leaves all of
them), max_queued backpressure (slow_reader), want_writable at a low mark,
flushing closes, inbound limits (bytes and frames per window, the first
frame's deadline), and per-thread counters.

A framing's pause_after and raw(n) make a framing switch land exactly after
a frame, since the I/O threads split input ahead of the program.

net_engine_test covers the framings and their switches, ordering, groups,
backpressure, closes, limits and stop, and benchmarks one message to 6000
loopback connections. The sanitizer lanes run it. The design doc records
what building N1 changed, and the measured fan-out cost, whose floor is the
loopback write itself.
macOS accepts an RLIMIT_NOFILE above OPEN_MAX and still refuses descriptors
past it (and past kern.maxfilesperproc): recipe / macos-latest / Debug failed
the 5115th connect with EMFILE. Clamp the limit the receiver count is fitted
to on Apple.
@transfix
transfix merged commit 35f5575 into master Nov 21, 2026
29 checks passed
@transfix
transfix deleted the feat/net-engine-n1 branch November 21, 2026 11:03
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