English
ours-relay
crates/relay — pumping data between the two sides of a connection, both directions in one task, and the UDP flow table for inbounds with a shared socket.
Streams
relay_stream(a, b)— until both sides close, or one has and the other stays idle forHALF_CLOSE_TIMEOUT; returns the bytes each way.relay_stream_ended(a, b, counters, half_close)→Ended— the same, also counting into the tracker'sCountersand telling which side closed first.- Each direction is joined in the kernel with splice (
ours-net-sys) as soon as both of its ends are sockets the process does not need to look into: plain TCP, kTLS or a protocol that has finished framing. Until then copying goes through a pooled buffer (ours-bufpool), taken only while there is data: an idle connection does not hold a buffer. - What each direction does next is picked by the buffer forms of its two ends (below).
- Both relays (stream and packet) hold their shard's
ours_runtime::balance::Activity: a live relay counts in the shard's load, and the first change of the byte counter in a new epoch marks it active (one comparison per pass) — Runtime.
Buffer forms
A stream says how it reads and writes now: Stream::read_form / write_form, for a protocol stream Splice::read_form / write_form (ours_net_sys::stream). A layer that passes the bytes of the stream under it on as they are hands on that stream's forms (only those it lets through, and only once it holds nothing of its own); a layer that changes them gives forms of its own or plain copy.
| Read form | Meaning |
|---|---|
Raw(Side) | a socket splice can read; nothing of the protocol is left in this direction |
Pieces | pieces the stream already holds (QUIC, HTTP/2 tunnel, XHTTP); it may decline a read |
Ranges | records opened in place in the relay's buffer (TLS of our own) |
Copy | AsyncRead into the relay's buffer |
| Write form | Meaning |
|---|---|
Raw(Side) | a socket splice can write |
Owned | takes the relay's buffer whole, with headroom in front (QUIC, HTTP/2, XHTTP); may also be filled from a socket (write_from, HTTP/2) |
Records | seals TLS records in place (TLS of our own); filled from a socket (write_from) or handed laid-out records (poll_write_records, h2) |
Copy | AsyncWrite from the relay's buffer |
The relay also tells a plain TCP connection (Stream::Tcp, "socket") from a protocol's raw socket: it reads and writes the former itself. One function, route, picks the path for a pair:
| Reader \ writer | socket | raw | Owned | Records | Copy |
|---|---|---|---|---|---|
| socket | splice | splice | fill if it fills, else owned | fill if it fills, else own + write | own + write |
| raw | splice | splice | owned | own + write | own + write |
Pieces | pieces + zerocopy (all), else writev | pieces + vectored write | pieces + vectored write | pieces + vectored write | pieces + vectored write |
Ranges | ranges + zerocopy (all), else writev | ranges + vectored write | owned | ranges + vectored write | ranges + vectored write |
Copy | own + zerocopy | own + write | owned | own + write | own + write |
"Own" is a pool buffer of the relay. "+ zerocopy" sends with MSG_ZEROCOPY. A Pieces reader that declines a read is read as Copy.
- When zerocopy applies: to one send of
MIN_WRITE(10 KiB) or more — the own buffer, the ranges in it, or up to 64 pieces — and only to a plain socket; smaller sends, and sends the socket refuses, go bywritev. Ranges and pieces go so only withruntime.zerocopy: all(orOURS_ZEROCOPY=all), the own buffer unless zerocopy is off (net-sys). Every other writer frames, seals or encrypts into buffers of its own and sends those itself. Withall, our TLS sends its record batches by zerocopy when it runs right on a plain socket (not withunstable-ktls). A protocol's raw socket is written through the protocol until splice, since some (Vision) write it themselves past their TLS. - Buffer lifetime: data sent by zerocopy stays with the kernel until it reports the send complete. The buffer stays out of its pool (a piece keeps its allocation), and the next read takes a new one.
- Kernel TLS (
unstable-ktls) addsReadForm::KernelTls: spliced to a raw writer, read as ranges otherwise. The normal build has no such form.
Packets
relay_packet_ended(inbound, moved, idle, counters)→PacketEnded— pumps datagrams until a side fails or nothing moves foridle.Movedcarries the outbound and theRedirectReceiverthrough which a reload moves the flow to another outbound.udpnat::UdpNat— flows from a socket that receives from many clients (tproxy): the first packet from a source opens a flow, which is routed like a connection; the following ones join it.UdpNatConfig:mapping— whether the destination address is part of the flow's identity,filtering— which remote sides may reply into it,max_flows(by defaultDEFAULT_UDP_NAT_MAX) — a new flow evicts the one idle the longest.ReplySender— how to reply to the client;UdpNatStats— counters.tproxy::{accept, receive}— what nftablestproxyredirects to a socket, handed to the routing with its original destination; UDP replies leave from sockets bound to the address the client sent to. Thetproxyinbound andwireguard's kernel mode serve their sockets with it.
Who uses it
ours-mod-dispatch, ours-mod-direct, ours-proto (http-server), ours-mod-shadowsocks, ours-mod-tproxy, ours-mod-tun, ours-mod-wireguard; dev — the grpc and xhttp transports.
Specifics
- Depends on
ours-api,ours-bufpool,ours-net-sys,ours-runtime,ours-tracker,ours-codec,bytes. - The
unstable-ktlsfeature (off by default) only enables thesplices_into_kernel_tlsandcopies_through_tls_in_the_processtests (TLS in the kernel and in the process, with a KeyUpdate in the middle), which needours-mod-tlsfrom the dev dependencies.
Tests
Unit tests in src/ (splice, buffers, the NAT table). tests/stream.rs — a full side does not stall the other direction; tests/copied.rs — zerocopy on a route the kernel copies for turns itself off. tests/forms.rs relays between every pair of 17 kinds of stream (plain TCP, a raw protocol socket and the fakes of ours_testkit::forms for each read form × write form), random data both ways at once, and checks by the fakes' call counts that each pair took the path of the table; a failing pair is named by its seed (OURS_FORMS_SEED).