Skip to content

Instance (Instance) ​

crates/kernel/src/instance.rs. Instance is the running configuration: inbounds, outbounds, rule-sets, rules, DNS, the dispatcher between them and the modules' units. The shards on which all of this runs are a separate Runtime.

Lifecycle ​

text
Instance::new(config, modules)   — build everything, opening nothing
Instance::start(&runtime)        — rule-sets → DNS → outbounds → units → inbounds
Instance::reload(config, ..)     — see reload.md
Instance::close(&runtime)        — inbounds (reverse order) → rule-sets → outbounds → units

new opens nothing, so a failed configuration leaves the system as it was. check without --diff calls Instance::check: the same build with Build::checking = true — units do not touch the disk (the cache module does not open the cache file), and the instance is discarded right away.

build() — the build order ​

The function build(config, modules, previous, ...), shared by new, reload and preview_reload:

  1. reject_unimplemented — a route.dial.interfaces.strategy other than the default is rejected unless auto_detect_interface or a default interface is set; an ntp section without the module is a Modules::section error.
  2. SystemHost::detect (ours_runtime::host) from route (interfaces, process/neighbour lookup), Graph::build, Plan::between with the previous graph (an empty plan without it).
  3. Services: the kernel puts in Arc<DialTable> and Arc<Extensions>, then every unit in module order builds its part (Unit::build) and publishes services — Units.
  4. Context { default_mark, dns: &DnsCell, host, services } — what the factories see.
  5. Outbounds in order. Without outbounds in the configuration an implicit direct is added (as in sing-box). One unchanged according to the plan is taken from previous; otherwise — the module's factory by type (the key of the protocol block), and the new object gets inherit_selection from the old one (the selector's choice). New dialers get the table from services and the candidate’s host settings.
  6. check_outbound_dependencies — dangling detours and cycles (Graph).
  7. The default outbound: route.final, otherwise the first one.
  8. Rule-sets: duplicate tags are an error; an unchanged one comes from previous, otherwise from the RuleSets factory (or no_rule_sets) and attach_services right away, so that it starts from what is saved in the cache.
  9. DNS: the Dns factory (or no_dns) with the outbounds, rule-sets, the previous Dns, the plan and DnsContext; route.default_domain_resolver → Resolver.
  10. Rules: with an unchanged route — from previous, otherwise the Rules factory (or no_rules). host.probe(rules.dependencies()) — a condition that needs a mechanism the host lacks fails at build time instead of "never matching". Every outbound from the route/bypass actions must exist.
  11. Inbounds: an unchanged one comes from previous with validate_hot(blocks); otherwise the module's factory and Inbound::attach(cx, dial_table).
  12. Policies (the reload section and the entities' reload blocks) and Routing for the dispatcher.

The result is Built: everything built plus "new" flags for inbounds and rule-sets and the units' stages.

new ​

After build: attach_services for DNS, outbounds and rule-sets, DnsCell::set, DialTable::set, then the dispatcher factory gets Routing and &mut Services — what it publishes (the dispatch module puts in Arc<Tracker> and Arc<Dispatcher>) is stored separately (published) and lives for the whole lifetime of the instance. The units' Stages are deferred until start.

start ​

  1. start_rule_sets — all rule-sets on the first shard at once; waits until each is up to date (downloads and so on); the Updated callback calls the dispatcher's rule_set_updated.
  2. Dns::start.
  3. Outbound::start on the first shard (background tasks: group probes and so on).
  4. Stage::commit of the units' deferred parts on the first shard — after DNS and before the inbounds, the way sing-box starts services: an NTP server may be a domain, and the clock must be right before everything else.
  5. Inbounds in order on every shard (Inbound::start). An error closes the inbounds already opened, the rule-sets and the outbounds.

Access from outside ​

dispatcher(), outbounds(), default_outbound(), rules(), dns(), dial_table(), dns_cell() and service::<T>() — first the services of the running configuration's units, then what the dispatcher published. This is read by explain, the Admin API (app/ui) and auto_nft.

Stubs ​

Without the router module the kernel substitutes no_rules (a configuration with route.rules → "route rules are not built in") and no_rule_sets; without dns — no_dns (crates/api/src/dns.rs): DNS servers and rules in the configuration are an error, name lookups return an error.

GPL-3.0-or-later