English
Add a module to the binary
A module is described by an ours_api::Module constant in an ours-mod-<name> crate in modules/<group>/<name>. Most crates have one, MODULE; a protocol crate has two, INBOUND and OUTBOUND. The kernel learns about modules only from the binary's LIST.
1. The crate
- The directory
modules/<group>/<name>/(groups:protocols,transports,routing,dns,system,config). The workspace members aremodules/*/*, no need to add it separately. Cargo.toml:name = "ours-mod-<name>",version.workspace = trueand so on, at the end[lints] workspace = true(it hasunsafe_code = "forbid").tests/layers.pyrequires: the crates undermodules/are exactly those whose name starts withours-mod-.- Dependencies:
ours-api,ours-codec,ours-jsonand the neededlibs//crates/; notours-kernel(only in[dev-dependencies]for tests). - A line in the root
Cargo.toml,[workspace.dependencies]:ours-mod-<name> = { path = "modules/<group>/<name>" }(withdefault-features = falseif the crate has features the binary controls).
2. MODULE
rust
pub const MODULE: ours_api::Module = ours_api::Module {
name: "<name>", // = the binary's feature name
requires: &["dial"], // modules whose points, services or crates are used
provides: &[ /* Provide::... */ ],
};nameis unique in the binary and matches the feature that enables the module: by it a configuration error says which feature to enable (not built in (feature…)). For a protocol the names are<protocol>-inboundand<protocol>-outbound(see Add a protocol).app/ours/build.rsfinds modules as text: the lineconst …: ours_api::Module =(or: Module =) in thesrc/of a crate undermodules/, and the literalsname:,key:,point:,block:inside. So:nameand theProvidekeys are string literals right in the description. A crate may describe several modules this way.- The
Providevariants and what they mean — Contract.
3. The binary's feature and LIST
app/ours/Cargo.toml:[dependencies]:ours-mod-<name> = { workspace = true, optional = true };[features]:<name> = ["dep:ours-mod-<name>", …]— plus the features of the modules it requires, if they can be turned off themselves (trojan-inbound = ["dep:ours-mod-trojan", "tls"],dns = ["dep:ours-mod-dns", "router"]); a protocol also gets an umbrella feature,trojan = ["trojan-inbound", "trojan-outbound"];- add
<name>todefaultif the module should be in the ordinary build.
app/ours/src/modules.rs, theLISTarray:rust#[cfg(feature = "<name>")] ours_mod_<name>::MODULE,The order in
LISTdoes not matter:Modules::new(crates/kernel/src/modules.rs) orders modules after the ones they require.
4. What is checked automatically
The checks of requires (the const check, Modules::new, a_module_requires_…, tests/features.py) are listed in requires. Besides them:
Modules::new: names and keys are unique; exactly oneDispatcher.every_module_of_the_workspace_is_listed_or_left_out_by_its_feature: a module is either inLISTwith the slotsbuild.rsfound, or turned off by the feature with its name.omitted_inbound_…/omitted_outbound_…: a type turned off by a feature is rejected by the loader withnot built inand the list of available ones.
5. Running
sh
python3 tests/run.py -- cargo test -p ours-mod-<name>
python3 tests/run.py -- cargo test -p ours --bin ours modules
python3 tests/layers.py
python3 tests/run.py -- python3 tests/features.pyBefore merging — the full set from Checks (workspace, Clippy, MIPS).