Skip to content

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 ​

  1. The directory modules/<group>/<name>/ (groups: protocols, transports, routing, dns, system, config). The workspace members are modules/*/*, no need to add it separately.
  2. Cargo.toml: name = "ours-mod-<name>", version.workspace = true and so on, at the end [lints] workspace = true (it has unsafe_code = "forbid"). tests/layers.py requires: the crates under modules/ are exactly those whose name starts with ours-mod-.
  3. Dependencies: ours-api, ours-codec, ours-json and the needed libs//crates/; not ours-kernel (only in [dev-dependencies] for tests).
  4. A line in the root Cargo.toml, [workspace.dependencies]: ours-mod-<name> = { path = "modules/<group>/<name>" } (with default-features = false if 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::... */ ],
};
  • name is 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>-inbound and <protocol>-outbound (see Add a protocol).
  • app/ours/build.rs finds modules as text: the line const …: ours_api::Module = (or : Module =) in the src/ of a crate under modules/, and the literals name:, key:, point:, block: inside. So: name and the Provide keys are string literals right in the description. A crate may describe several modules this way.
  • The Provide variants and what they mean — Contract.

3. The binary's feature and LIST ​

  1. 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> to default if the module should be in the ordinary build.
  2. app/ours/src/modules.rs, the LIST array:

    rust
    #[cfg(feature = "<name>")]
    ours_mod_<name>::MODULE,

    The order in LIST does 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 one Dispatcher.
  • every_module_of_the_workspace_is_listed_or_left_out_by_its_feature: a module is either in LIST with the slots build.rs found, 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 with not built in and 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.py

Before merging — the full set from Checks (workspace, Clippy, MIPS).

GPL-3.0-or-later