Skip to content

Building the binary ​

What goes into the ours binary is decided by the Cargo features of its manifest (app/ours/Cargo.toml). The kernel knows nothing about features: it gets a ready module list and checks it (Module order).

Build profile: build.toml ​

build.toml in the repository root lists what to build as tables of true/false keys; as shipped, everything is on, the same as default. dist/build.py turns it into features and runs cargo build -p ours --release --no-default-features --features ...:

sh
python3 dist/build.py                            # build.toml
python3 dist/build.py --config router.toml --target x86_64-unknown-linux-musl
python3 dist/build.py --config router.toml --print   # the feature list only

Arguments the script does not know go to cargo. A key maps to the feature of the same name, with the table's prefix where features have one (dns.udp → dns-udp, transports.ws → transport-ws, routing.rules.ip → rule-ip, protocols.trojan.inbound → trojan-inbound); enabled and endpoint are the table's own feature. A key that names no feature is an error. A key that is on pulls in what its feature needs even where the profile turns that off (trojan pulls tls); what each key needs is in What needs what. tests/test_build_config.py checks that the shipped profile builds the same as default, and that table against the features.

The module list LIST ​

app/ours/src/modules.rs — the constant LIST: &[Module], one line per module, each under its own #[cfg(feature = ...)]. Always in the list: dispatch, dial, ui, tls, cache, transport. The rest are under a feature named after the module (router, dns, ntp, rule-process, subscriptions, group, transport-ws, protocols…); mux is under any mux-* feature. A protocol with both sides has two modules, <protocol>-inbound and <protocol>-outbound (the crate's INBOUND and OUTBOUND); the side left out of LIST is not referenced, and LTO drops its code.

modules::get() builds Modules::new(LIST).with_known(KNOWN) once (LazyLock); a panic here is a build error, not a configuration error, and it is caught by the the_list_is_sound test.

The order of lines in LIST does not matter: the kernel orders modules by requires.

Features ​

GroupFeaturesWhat they enable
Protocols<protocol>-inbound, <protocol>-outbound for direct, http, socks, shadowsocks, vless, trojan, anytls, shadowtls, hysteria, hysteria2, tuic; <protocol> enables both. One side only: block, mixed, tproxy, redirect, tun, mtprotothe module's crate (dep:ours-mod-*) and the module of that side; protocols over TLS also enable tls
Multiplexingmux, mux-smux, mux-yamux, mux-h2, mux-xray, mux-brutal, mux-paddingthe mux module and its support in vless, trojan, shadowsocks (in those of them that are enabled); mux enables all six: sing-mux over smux, yamux, h2mux, the mux.cool client (xray key, v2ray-plugin), TCP Brutal and padding
VLESSmux-cool, vlessenccapabilities of ours-mod-vless; mux-cool enables vless-inbound; mux-cool is the mux.cool server of the inbound and needs no mux module
Shadowsocksss-v2ray-pluginthe v2ray-plugin plugin (ours-mod-shadowsocks/v2ray-plugin); enables the crate, mux-xray, tls, transport-ws; obfs-local is always present
WireGuardwireguard, wireguard-awg, wireguard-ciphersthe wireguard module; the AmneziaWG and cipher negotiation extensions (wireguard)
Transportstransport-ws, transport-http, transport-httpupgrade, transport-grpc, transport-xhttpthe transport module; grpc, xhttp and http share the HTTP/2 layer of ours-proto (h2); transport-xhttp also enables the transport over QUIC (ours-mod-transport/quic)
TLStls, unstable-ktlsthe implementation in ours-mod-tls (implementation, unstable-ktls); the tls module itself is always present
Routingrouter, rule-conditions, rule-*, rule-set-inline/local/remote/binary, rule-processthe router module and its conditions/rule-set sources; rule-process is a separate module
Groupsgroup, group-probethe group module; probe — selection by measurements
DNS filteringadblockthe adblock module (requires dns)
DNSdns, dns-udp, dns-tcp, dns-tls, dns-https, dns-quic, dns-h3, dns-local, dns-mdns, dns-hosts, dns-fakeipthe dns module (requires router) and its transports
Subscriptionssubscriptions, sub-links, sub-xray, sub-clashthe subscriptions module (runtime) and the formats
Sniffingsniff, sniff-<protocol>the ours-sniff sniffers; sniff — all of them and the support in dispatch
Routing (.dat)rule-set-dat.dat rule-sets in router
Clientsclientsthe clients module; enables wireguard
Otherntp, cache, auto-nft, api, dashboard, tools, jemallocthe ntp module; the persistent cache file (ours-mod-cache/persistence, ours-mod-subscriptions/cache); tproxy auto_nft; Admin API and UI; the ours tools command; jemalloc as the allocator (see "Allocator" below)

default enables almost everything; outside default are the individual rule-*, sniff-* (they are covered by rule-conditions and sniff), api (covered by dashboard) and the unstable unstable-ktls: kTLS gives no measurable gain and breaks where in-process TLS works. Its tests are cargo test -p ours-mod-tls -p ours-tls13 -p ours-relay -p ours-mod-vless -p ours-mod-http --features unstable-ktls.

The ours-mod-subscriptions crate is always linked (the binary uses its types), but the subscriptions module gets into LIST only with the feature of the same name.

build.rs ​

app/ours/build.rs walks modules/**/src/*.rs and app/ui/src/*.rs (except tests, benches, examples and the #[cfg(test)] mod tests block), finds the constants const X: ours_api::Module = ... and parses their body as text. It writes into OUT_DIR:

  • known.rs — &[Known]: every module of the workspace (built or not) and its slots (point, key), as Provide::slot would give them. The parsing requires literals: name, keys, points are written as strings, otherwise build.rs panics; an unknown Provide::* variant panics too;
  • uses.rs — for every module: the modules whose crates its crate depends on (from the manifest's [dependencies]), with an optional flag;
  • features.rs and enabled.rs — all features of the binary and those enabled in this build (from CARGO_FEATURE_*).

The tests in modules.rs compare them with LIST:

  • every_module_of_the_workspace_is_listed_or_left_out_by_its_feature — for every module from LIST the slots match those found by build.rs; every module not from LIST is named by a feature, and that feature is off;
  • a_module_requires_the_modules_its_crate_depends_on — see Layers;
  • omitted_inbound/outbound_is_refused_with_the_available_types — for every type that is not built, loading the configuration fails with "not built in" and the list of available ones.

Plus const _: () = assert!(requires_are_listed(LIST)) — a check in const context: a feature that enables a module without what it requires does not compile.

"Not built in" ​

KNOWN is there for the sake of the error message. Modules::decoders() hands the loader both the decoders of the modules in the build and known for all modules of the workspace. A key that is in known but not in the build gives ... is not built in (feature `<module>`); available: <keys of this build> (Decoders::find, crates/api/src/config.rs); a key that is nowhere gives unknown .... The same for sections (Decoders::decode_section, Modules::section) and rule-set sources. Without with_known anything unknown to the build would look like a typo.

Checking combinations: tests/features.py ​

Run through tests/run.py:

  1. cargo hack check -p ours --each-feature (with --clippy — clippy -D warnings);
  2. cargo tree --no-default-features: the minimal build must not pull in rustls, ring, quinn, h3, tokio-rustls, ours-mod-dns, ours-mod-router, ours-mod-group, ours-srs;
  3. for every feature from default — a default build without it.

Workspace tests are separate: Cargo unifies features across workspace members.

Minimal build ​

--no-default-features keeps the kernel, the loader, dispatch, dial, transport, the tls module without an implementation and cache without a file. There are no protocols in it: even a configuration without outbounds requires direct (the kernel adds an implicit direct). Example:

sh
cargo build -p ours --release --no-default-features --features direct-outbound,socks-inbound,dns-local

check builds the instance without opening sockets and reports what is not built in before run.

Targets and profiles ​

  • rust-toolchain.toml pins the toolchain and adds the x86_64-unknown-linux-musl target; the E2E bench (tests/e2e/run.sh) builds the binary for it, as for OpenWrt.
  • rust-version in Cargo.toml is the oldest Rust version the tree builds with (currently 1.99: Cargo 1.98 does not accept default-features = false on top of a workspace dependency with default features enabled). It is checked by the msrv CI job (cargo +1.99 check --workspace --all-targets). It is raised only for a change that needs the new version, no higher than the stable release before the one pinned in rust-toolchain.toml, with an entry in CHANGELOG.md.
  • aarch64 is checked by a cross build for aarch64-unknown-linux-gnu and by running ours version under qemu (the aarch64 CI job); tests are not run on aarch64.
  • .cargo/config.toml: for target_env = "musl" it links with -z pack-relative-relocs, so that static-pie does not read ~0.5 MiB of RELA at startup. On x86_64 it turns on the VAES backend of aes and the serial backend of curve25519-dalek: the AVX2 one does not speed up X25519 (within 1 % on DH and on deriving a public key) and pulls in curve25519-dalek-derive with syn 2. A RUSTFLAGS variable replaces these flags, so it must repeat them.
  • [profile.release]: lto = "fat", codegen-units = 1, panic = "unwind" (a panic in a connection's task closes only that connection), opt-level = "s" except the crates every packet or handshake goes through (cryptography, relay, QUIC, tokio and the like), which stay at 3. "s" makes the binary a fifth smaller; a crate that joins the packet or handshake path joins the list.
  • [profile.dist]: the binaries handed to users (the CI artifact), release without the symbol table (strip = "symbols"): 16.8 MB instead of 21.0 for the musl binary. The optimization settings are the same, so tests and benchmarks, which build release and whose backtraces name functions, check the code users get; without the symbol table the linker keeps ~200 functions fewer (0.3 % of .text).
  • Allocator: the ours-alloc crate (libs/alloc). The jemalloc feature (in default) installs tikv-jemallocator as the global allocator on x86_64 and aarch64; on other targets (MIPS) and without the feature the system malloc is used. The reason is musl's malloc: one lock per process, and it returns empty groups to the kernel right away, so a change of a structure's size on the hot path turns into mmap/munmap every few dozen packets. jemalloc is built from source (configure and make) and is licensed under BSD-2-Clause, the wrapper — MIT/Apache-2.0 (see NOTICE). A kernel with a page size larger than 4 KiB (some aarch64) requires building with JEMALLOC_SYS_WITH_LG_PAGE set for that size.
    • The built-in setting is lg_extent_max_active_fit:9 (the _rjem_malloc_conf symbol; the _RJEM_MALLOC_CONF environment variable overrides it). jemalloc merges freed large blocks with their neighbours and by default does not carve a request out of a free range more than 64 times its size. smux and yamux free 16 KiB frames in batches, so merged ranges stop fitting frames and the heap keeps growing into new pages. 2⁹ × 16 KiB = 8 MiB (oversize_threshold): any range jemalloc keeps fits a frame.
    • mux frames are read into uninitialized memory: jemalloc zeroes a calloc of a large block freed by another shard through MADV_DONTNEED, which costs page faults.
    • jemalloc returns dirty pages after dirty_decay_ms (10 s), so memory stays higher for a while after load.
  • quinn, quinn-proto and quinn-udp come from a fork — see The quinn fork.
  • 32-bit MIPS (MT7621) has no 64-bit atomics: counters go through ours-atomic64 (a standard atomic or a lock); ours-mod-ntp takes the 32-bit tv_nsec into account. There is no MIPS target in rust-toolchain.toml, and cross-building is not automated in the repository.

Licenses for a binary release ​

The binary statically includes third-party crates, the Rust standard library and (for *-linux-musl) musl; their licenses (MIT, BSD, ISC, Apache-2.0 and others) require the texts to accompany the binary. Next to the binary, the release archive contains LICENSE and THIRD-PARTY-LICENSES.txt:

sh
python3 dist/third-party-licenses.py --target x86_64-unknown-linux-musl -o THIRD-PARTY-LICENSES.txt \
    -- --no-default-features --features $(python3 dist/build.py --print)

Without arguments after -- the default features are used. The script walks the normal and build dependencies of ours for the target via cargo metadata --locked and writes the project's NOTICE and LICENSES/, then for every crate: the license expression, the source and the LICENSE*/COPYING*/NOTICE* files from its package. A crate with no license at all is an error (exit code 1). The musl CI job builds the file and puts it in the artifact next to the binary. GPL-3.0 §6 additionally requires providing the source: the release tag, and for the quinn fork — its pinned commit (or cargo vendor in the source archive).

Vendored source archives ​

From a release checkout, generate Cargo's source replacement file and vendor directory:

sh
cargo vendor --locked --versioned-dirs vendor > .cargo/vendor.toml
cargo --config .cargo/vendor.toml build --offline --locked --release -p ours

Include Cargo.lock, .cargo/vendor.toml, vendor/, and the repository's license files in the source archive. Keep the license files Cargo copies into vendor/. Cargo's generated config maps the exact Quinn Git revision and the registry dependencies to this directory. The offline build expects the pinned Rust toolchain and the target's native build tools to be installed.

Checking dependency changes ​

Use cargo tree --locked -d to inspect duplicate versions and cargo tree --locked -p ours -e normal to inspect production dependencies. The default production graph uses brotli-decompressor and ruzstd. The full Brotli codec is used by tests. Keep Unicode support in regex for user-supplied patterns. RustCrypto is one generation (x25519-dalek 3, ml-kem 0.3, rand_core 0.10 with getrandom 0.4); two pairs remain: getrandom 0.2 for ring 0.17 and syn 2/3.

The quinn fork ​

QUIC (Hysteria, Hysteria 2, TUIC, VLESS and XHTTP over QUIC, DNS over QUIC) goes through quinn 0.11 from the fork github.com/janeblower/quinn: [patch.crates-io] in the root Cargo.toml, branch ours/0.11.12, the commit pinned with rev. The base is the release commit of quinn-proto 0.11.19 / quinn-udp 0.5.16 (quinn 0.11.12), with a set of our own commits on top. The fork keeps those upstream package versions. Identify the exact source by the Git revision in Cargo.toml and Cargo.lock. The fork includes Quinn's LICENSE-MIT and LICENSE-APACHE.

Why a fork and not upstream: some of the changes alter on-the-wire behaviour so that the QUIC client looks like Chrome — upstream has no such settings and will not have them; the rest are performance changes.

What differs from upstream:

  • performance: the connection driver sends up to 32 datagrams in one GSO call (was 10) and up to 64 per pass (was 20) — on veth and virtual NICs every call costs an interrupt on a neighbouring core; the endpoint receives up to 8 datagrams (each up to a GRO batch) per call rather than 32 — the receive buffer holds on to every page ever written to, and 32 GRO batches held ~3 MiB per endpoint; the search for data in the send buffer starts from the last segment; in-order stream acknowledgements bypass the range set;
  • receive windows grow from a smaller initial value within a shared per-endpoint budget (WindowBudget, receive_window_budget);
  • BBR: the gain coefficients and the number of startup rounds are configurable;
  • Chrome mimicry: the number of the first Initial packet (initial_packet_number), the packet number size in long headers chosen by window (packet_number_window), scrambling of the client's Initial CRYPTO data (chaos_protection), separate Handshake ACKs (handshake_acks_alone), packets not coalesced into one datagram (coalesce_packets).

ours-quic calls this API directly, so the project does not build without the fork.

Consequences:

  • [patch] has no effect when installing from crates.io: cargo install ours is impossible, only building from git. An offline build needs a prior cargo fetch (or cargo vendor);
  • quinn security fixes are carried over to the fork by hand: some of the changes touch connection and streams;
  • the fork is rebased on each 0.11.x release.

Only ours-quic and ours-quinn-ext depend on quinn (tests/layers.py, no exceptions): the modules name its types through ours-quic, and the fork's own API (WindowBudget, the Chrome mimicry) is called by ours-quic alone.

GPL-3.0-or-later