Skip to content

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 for HALF_CLOSE_TIMEOUT; returns the bytes each way.
  • relay_stream_ended(a, b, counters, half_close) → Ended — the same, also counting into the tracker's Counters and 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 formMeaning
Raw(Side)a socket splice can read; nothing of the protocol is left in this direction
Piecespieces the stream already holds (QUIC, HTTP/2 tunnel, XHTTP); it may decline a read
Rangesrecords opened in place in the relay's buffer (TLS of our own)
CopyAsyncRead into the relay's buffer
Write formMeaning
Raw(Side)a socket splice can write
Ownedtakes the relay's buffer whole, with headroom in front (QUIC, HTTP/2, XHTTP); may also be filled from a socket (write_from, HTTP/2)
Recordsseals TLS records in place (TLS of our own); filled from a socket (write_from) or handed laid-out records (poll_write_records, h2)
CopyAsyncWrite 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 \ writersocketrawOwnedRecordsCopy
socketsplicesplicefill if it fills, else ownedfill if it fills, else own + writeown + write
rawsplicespliceownedown + writeown + write
Piecespieces + zerocopy (all), else writevpieces + vectored writepieces + vectored writepieces + vectored writepieces + vectored write
Rangesranges + zerocopy (all), else writevranges + vectored writeownedranges + vectored writeranges + vectored write
Copyown + zerocopyown + writeownedown + writeown + 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 by writev. Ranges and pieces go so only with runtime.zerocopy: all (or OURS_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. With all, our TLS sends its record batches by zerocopy when it runs right on a plain socket (not with unstable-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) adds ReadForm::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 for idle. Moved carries the outbound and the RedirectReceiver through 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 default DEFAULT_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 nftables tproxy redirects to a socket, handed to the routing with its original destination; UDP replies leave from sockets bound to the address the client sent to. The tproxy inbound and wireguard'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-ktls feature (off by default) only enables the splices_into_kernel_tls and copies_through_tls_in_the_process tests (TLS in the kernel and in the process, with a KeyUpdate in the middle), which need ours-mod-tls from 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).

GPL-3.0-or-later