English
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 → unitsnew 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:
reject_unimplemented— aroute.dial.interfaces.strategyother than the default is rejected unlessauto_detect_interfaceor a default interface is set; anntpsection without the module is aModules::sectionerror.SystemHost::detect(ours_runtime::host) fromroute(interfaces, process/neighbour lookup),Graph::build,Plan::betweenwith thepreviousgraph (an empty plan without it).Services: the kernel puts inArc<DialTable>andArc<Extensions>, then every unit in module order builds its part (Unit::build) and publishes services — Units.Context { default_mark, dns: &DnsCell, host, services }— what the factories see.- Outbounds in order. Without outbounds in the configuration an implicit
directis added (as in sing-box). One unchanged according to the plan is taken fromprevious; otherwise — the module's factory by type (the key of the protocol block), and the new object getsinherit_selectionfrom the old one (the selector's choice). New dialers get the table from services and the candidate’s host settings. check_outbound_dependencies— danglingdetours and cycles (Graph).- The default outbound:
route.final, otherwise the first one. - Rule-sets: duplicate tags are an error; an unchanged one comes from
previous, otherwise from theRuleSetsfactory (orno_rule_sets) andattach_servicesright away, so that it starts from what is saved in the cache. - DNS: the
Dnsfactory (orno_dns) with the outbounds, rule-sets, the previousDns, the plan andDnsContext;route.default_domain_resolver→Resolver. - Rules: with an unchanged
route— fromprevious, otherwise theRulesfactory (orno_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 theroute/bypassactions must exist. - Inbounds: an unchanged one comes from
previouswithvalidate_hot(blocks); otherwise the module's factory andInbound::attach(cx, dial_table). Policies(thereloadsection and the entities'reloadblocks) andRoutingfor 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
start_rule_sets— all rule-sets on the first shard at once; waits until each is up to date (downloads and so on); theUpdatedcallback calls the dispatcher'srule_set_updated.Dns::start.Outbound::starton the first shard (background tasks: group probes and so on).Stage::commitof 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.- 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.