English
Component graph
crates/kernel/src/graph.rs, the identity types — crates/api/src/component.rs.
A component is something reload can keep, rebuild or remove separately: an inbound, an outbound, a DNS server, a rule-set and so on, plus singletons for the sections around them. The graph is built from Config and opens nothing; check --diff builds it too.
Identity
ComponentId { kind: ComponentKind, tag: String }. An empty tag is a singleton. Without a tag a component gets an index (effective_tag), as in sing-box. In output: outbound[proxy], route.
ComponentKind (the order affects sorting in output): Log, Ntp, Certificate, Cache, NetworkNamespace, HttpClient, Outbound, Inbound, DnsServer, Dns, RuleSet, Route, and Other(name) for a kind a module declares itself (adblock). Names (ComponentKind::name): log, ntp, certificate, cache, network-namespace, http-client, outbound, inbound, dns-server, dns, rule-set, route.
Where components come from
Graph::build(config):
| Source | Component | Its "options" for the fingerprint |
|---|---|---|
| sections | one per Part from Decoded::parts | part.cold + members of other sections from part.reads |
config.outbounds | outbound[tag] | type, tag, Blocks::cold() |
config.inbounds | inbound[tag] | the same |
config.rule_sets | rule-set[tag] from the module's parts, otherwise by blocks | as above |
| kernel | route (always) | the kernel's RouteOptions (without the rules) |
| kernel | dns (always) | null |
A Part describes the module owning a section (crates/api/src/config.rs; from the loader's side — Canonical form and reload): kind (ComponentKind), name, cold, references, defaults (the kinds whose default component it uses), reads. The helpers Decoded::whole(kind) (the section is one component) and Decoded::each(kind, name) (every list element is a component). A section that is one component declares its kind in Provide::Section, and Decoders::decode_section calls whole for it: log, certificate, cache, ntp, adblock and route.rules (Route). http_clients, network_namespaces (each), DNS servers and the DNS router, rule-sets name their kinds in their parts.
The route, reload, runtime and admin sections declare no kind and give no components: changing the reload policy rebuilds nothing.
Entries with the same id are merged: options — into an array, references and defaults — are united. This way the kernel's route and the router module's route (the rules) are one component.
Fingerprint
fingerprint is a hash of ours_json::to_string_pretty of the component's options. The options are the canonical form (Decoded::canonical), not the source text: identical options written differently give one fingerprint.
For inbound/outbound Blocks::cold() is taken — the blocks without what reload applies in place: a block with cold = Some(Null) ("all hot", e.g. the reload block) contributes nothing, a block with cold = Some(v) contributes v. The protocol module decides itself what is hot (for example, the inbound's users): such changes do not change the fingerprint, but are applied through Inbound::hot_changed (Reload).
Dependencies
References (ours_codec::Reference { kind: RefKind, tag }) are collected by the decoders through References. RefKind → ComponentKind: Inbound, Outbound, DnsServer, RuleSet, NetworkNamespace, HttpClient. For every component:
- a reference that resolved to an existing component is a
dependenciesedge; - a reference to nowhere or to itself goes to
unresolved(the kernel rejects an outbound with an unresolved reference to an outbound —check_outbound_dependenciesininstance.rs); defaults— an edge to the "default" component of the given kind: the one named by a section (for outbound —route.final, for others — the sections'Decoded::defaults), otherwise the first in order;implicit— for aroutewithoutfinal, an edge to the first outbound.
transitive_dependencies(id) — all dependencies, direct and indirect; used by the Plan.
Checks on the graph
check_outbound_dependencies (crates/kernel/src/instance.rs) after the outbounds are built: an unresolved detour of an outbound → outbound `y` names `x`, which is no outbound or group; an unresolved inbound name (a rule's inbound condition, an inbound's detour) → <component> names inbound `x`, which is not in the configuration; a cycle between outbounds → circular outbound dependency: a -> b -> a. The walk is iterative, so that a long detour chain from the configuration does not overflow the stack.