English
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 beyondrmem_maxwhere the process is allowed (QUIC sockets);OwnMark(theown_markfield) — the mark every socket of an instance carries, so that theauto_nftoutput chain lets the proxy's own traffic through (and thetuninbound'sauto_redirect);original_destination—SO_ORIGINAL_DST(theredirectandtuninbounds); 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(throughours-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 withIP_ORIGDSTADDR,reply_socket— a reply from the original destination's address; the*_handed/inherited_*variants for reload.cmsg— control messages in bytes: reading whatrecvmsgfilled (a length past the filled bytes or short of a header ends the walk) and writing one forsendmsg.udp— UDP batches (recvmmsg/sendmmsg, GSO/GRO):MAX_BATCH,receive,send,poll_send_with_zerocopy,offloads_enabled; singlesendmsg/recvmsgwith GSO/GRO for WireGuard:send_segments,receive_segments,segments_control,enable_gro.zerocopy—MSG_ZEROCOPYfor 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 thanMIN_WRITEare 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.Writablewaits 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 fromruntime.zerocopybyset_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, soAllallows at mostALL_IN_FLIGHT(2) on a TCP socket (8 otherwise): more did not raise throughput and cost memory.OURS_ZEROCOPY=0andOURS_ZEROCOPY=alloverride the configuration.OURS_ZEROCOPY=forcekeeps zerocopy on where the kernel copies anyway (veth, loopback), for measurements between namespaces.
- 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
splice— TCP↔TCP through a pipe: data goes from socket to socket in the kernel. Either side may be kTLS (theKernelTlstrait): 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:setnschanges 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'slookup_host).watch— watching a file, like sing-box'sfswatch: 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 bydirect, 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]hasworkspace = true, that is,unsafeis forbidden. System calls go through the safe wrappers ofrustixandsocket2, and the few they lack throughours-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.rsandzerocopy.rsregister a descriptor with the deprecatedAsyncFd::new/with_interestunder#[allow(deprecated)], because the replacement isunsafe.
Tests
Unit tests in src/; libs/net-sys/tests/ — batch.rs, socket_options.rs, udp.rs, zerocopy.rs on real loopback sockets.