Skip to content

ours-net-sys ​

libs/net-sys — Linux sockets beyond what tokio opens itself. Every socket is created through socket2 and rustix, which keep system calls behind safe interfaces (what they lack — through ours-sys), and is then handed to tokio.

Modules ​

  • lib.rs — SocketOptions (interface, mark, keep-alive, UDP options: apply_udp, apply_keep_alive, local_address), Family; widen_receive_buffer — a receive buffer beyond rmem_max where the process is allowed (QUIC sockets); OwnMark (the own_mark field) — the mark every socket of an instance carries, so that the auto_nft output chain lets the proxy's own traffic through (and the tun inbound's auto_redirect); original_destination — SO_ORIGINAL_DST (the redirect and tun inbounds); handing over listening sockets on reload (inherited_tcp_listener, inherited_udp_socket: the duplicate shares the socket, the accept queue and the flags, both owners wake up until the old one lets go); reserve_descriptors (growing the fd table while the process is single-threaded: in a multi-threaded one this waits for RCU); allowed_cpus, set_thread_affinity, current_thread_id — binding shards to CPUs; thread_cpu_time (through ours-sys) — a thread's CPU time.
  • tproxy — transparent proxy sockets (IP_TRANSPARENT): a TCP listener (the original destination address is the local address of the accepted connection), UDP with IP_ORIGDSTADDR, reply_socket — a reply from the original destination's address; the *_handed/inherited_* variants for reload.
  • cmsg — control messages in bytes: reading what recvmsg filled (a length past the filled bytes or short of a header ends the walk) and writing one for sendmsg.
  • udp — UDP batches (recvmmsg/sendmmsg, GSO/GRO): MAX_BATCH, receive, send, poll_send_with_zerocopy, offloads_enabled; single sendmsg/recvmsg with GSO/GRO for WireGuard: send_segments, receive_segments, segments_control, enable_gro.
  • zerocopy — MSG_ZEROCOPY for TCP/UDP:
    • How it works: a completion collector, not the sending task, holds the buffers until the kernel releases them. Shared UDP sockets share one ID sequence through SO_COOKIE. Writes shorter than MIN_WRITE are copied.
    • Writable: a completion notification raises EPOLLERR, and tokio keeps it as a final "closed for writing". After that the socket's own readiness calls a full socket ready, and the writer spins until the task's budget runs out. Writable waits for writability through its own registration of a duplicate descriptor and recreates it when a notification reaches it. The relay (copy and splice) and the batched UDP sender write through it.
    • Extent, set from runtime.zerocopy by set_extent: Off; Basic (default) — a whole buffer the relay read itself into a plain TCP socket, and UDP batches; All — also the relay's ranges and pieces and the record batches of our TLS. Each send in flight pins a buffer, so All allows at most ALL_IN_FLIGHT (2) on a TCP socket (8 otherwise): more did not raise throughput and cost memory. OURS_ZEROCOPY=0 and OURS_ZEROCOPY=all override the configuration. OURS_ZEROCOPY=force keeps zerocopy on where the kernel copies anyway (veth, loopback), for measurements between namespaces.
  • splice — TCP↔TCP through a pipe: data goes from socket to socket in the kernel. Either side may be kTLS (the KernelTls trait): the process parses its non-data records itself. A pipe is created only when data has appeared in the direction (an idle connection holds no pipe), starts at 64 KiB and grows up to 1 MiB, as in Go, when one call has filled it.
  • netns — a system call inside a network namespace: setns changes only the thread, so dialing in a namespace goes on a dedicated thread that ends together with it (a pooled thread would carry the namespace into someone else's work, including tokio's lookup_host).
  • watch — watching a file, like sing-box's fswatch: it watches the directory (a file replacement by rename is visible), a burst of writes counts as one change after a pause.
  • stream — the buffer forms in which a stream end can give or take data now; the relay picks its path for the pair from them.
  • batch — UDP batching shared by direct, the TPROXY NAT and framed UDP transports.
  • retry — the retry delay shared by the TCP accept and UDP receive loops.

Who uses it ​

ours-api, ours-config, ours-runtime, ours-kernel, ours-relay (splice), ours-proto, ours-quic, ours-tls13 (kTLS splice), the binary (reserve_descriptors, CPU binding), the modules dial, dispatch, tls, dns, adblock, router, subscriptions (watch) and most protocol modules (direct, tproxy, redirect, tun, wireguard, shadowsocks, trojan, vless, anytls, shadowtls, mux, mtproto and others).

Specifics ​

  • [lints] has workspace = true, that is, unsafe is forbidden. System calls go through the safe wrappers of rustix and socket2, and the few they lack through ours-sys; control messages are read and written in safe code over bytes.
  • Where a new API requires unsafe, the old safe one is kept: watch.rs and zerocopy.rs register a descriptor with the deprecated AsyncFd::new/with_interest under #[allow(deprecated)], because the replacement is unsafe.

Tests ​

Unit tests in src/; libs/net-sys/tests/ — batch.rs, socket_options.rs, udp.rs, zerocopy.rs on real loopback sockets.

GPL-3.0-or-later