mod action;
mod clock;
mod config;
pub mod conformance;
mod destination;
mod events;
pub mod host;
mod identity;
mod oper;
mod overlay;
pub mod plane;
mod radio;
mod rpc;
mod security;
mod sensor;
mod state;
pub mod telemetry;
use std::sync::Arc;
use anyhow::Context as _;
use zenkey::V1Context;
use zenoh::Session;
pub use host::{Host, Node, Plane};
pub use modem_contract::documents::{
Ack, ActionEvent, ActionOutcome, ActionRequest, Capabilities, DestinationState, DeviceFault,
Duplex, EndpointLane, EndpointState, Identity, IdentityChange, Lane, LaneTable, LinkState,
Oper, OperChange, RadioState, SecurityState, Sensor,
};
pub use modem_contract::{PRODUCER, PROFILE};
pub use plane::{LaneView, SchedulerCounters, SduPlane};
pub use telemetry::subjects as telemetry_subjects;
#[derive(Clone, Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MgmtConfig {
#[serde(default)]
pub connect: Vec<String>,
#[serde(default)]
pub listen: Vec<String>,
#[serde(default)]
pub scouting: bool,
#[serde(default)]
pub mode: Option<String>,
#[serde(default)]
pub usrpwd: Option<UsrPwd>,
#[serde(default = "default_ttl")]
pub ttl_s: u64,
#[serde(default = "default_telemetry_interval_ms")]
pub telemetry_interval_ms: u64,
#[serde(default)]
pub state_dir: Option<std::path::PathBuf>,
#[serde(default)]
pub provisional_boot_s: Option<u64>,
}
#[derive(Clone, Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UsrPwd {
pub user: String,
pub password_file: std::path::PathBuf,
}
fn default_telemetry_interval_ms() -> u64 {
telemetry::DEFAULT_INTERVAL.as_millis() as u64
}
fn default_ttl() -> u64 {
state::declared_ttl_s()
}
impl Default for MgmtConfig {
fn default() -> Self {
Self {
connect: Vec::new(),
listen: Vec::new(),
scouting: false,
mode: None,
usrpwd: None,
ttl_s: default_ttl(),
telemetry_interval_ms: default_telemetry_interval_ms(),
state_dir: None,
provisional_boot_s: None,
}
}
}
pub struct Mgmt {
session: Session,
ctx: V1Context,
tasks: Vec<tokio::task::JoinHandle<()>>,
_alive: zenoh::liveliness::LivelinessToken,
_device_alive: Arc<
std::sync::Mutex<std::collections::BTreeMap<String, zenoh::liveliness::LivelinessToken>>,
>,
}
pub async fn serve(cfg: &MgmtConfig, host: Arc<Host>) -> anyhow::Result<Mgmt> {
Mgmt::start(cfg, host).await
}
impl Mgmt {
pub fn check(cfg: &MgmtConfig, host: &Host) -> anyhow::Result<()> {
anyhow::ensure!(
!host.nodes().is_empty(),
"a management plane with no device to serve would declare itself alive for nothing"
);
V1Context::for_producer(&PROFILE, PRODUCER)
.context("the producer context is not a valid zenkey context")?;
modem_contract::schema::verify_total()
.context("the describe surface does not cover every declared type (RFC 08 §7)")?;
anyhow::ensure!(
cfg.ttl_s <= state::declared_ttl_s(),
"[management] ttl_s = {} exceeds the {} s the registry declares for \
this producer's state subjects; lower it, or change the registry \
and re-run `just registry-lock`",
cfg.ttl_s,
state::declared_ttl_s(),
);
session_mode(cfg)?;
if let Some(id) = &cfg.usrpwd {
anyhow::ensure!(!id.user.is_empty(), "[management] usrpwd user is empty");
std::fs::metadata(&id.password_file).with_context(|| {
format!(
"[management] usrpwd password_file {}",
id.password_file.display()
)
})?;
}
Ok(())
}
pub async fn start(cfg: &MgmtConfig, host: Arc<Host>) -> anyhow::Result<Self> {
Self::check(cfg, &host)?;
let ctx = V1Context::for_producer(&PROFILE, PRODUCER)
.context("the producer context is not a valid zenkey context")?;
let state_keys: Vec<(Arc<dyn SduPlane>, state::StateKeys)> = host
.sdu_planes()
.map(|p| {
(
p.clone(),
state::StateKeys::of(&PROFILE.local_origin(), p.as_ref()),
)
})
.collect();
let session = open_session(cfg).await?;
if let Some(twin) = twin_alive(&session, &ctx).await {
let _ = session.close().await;
anyhow::bail!(
"another daemon already declares {twin} on this host: one daemon serves \
every device on a host (#118) — stop it, or add this device to its config"
);
}
let state_dir = cfg
.state_dir
.clone()
.or_else(|| std::env::var_os("STATE_DIRECTORY").map(std::path::PathBuf::from));
let configs: Vec<Arc<config::ConfigState>> = host
.nodes()
.iter()
.filter_map(|node| {
config::ConfigState::of(
node.name(),
oper::Source::of(node),
node.file_keys().clone(),
node.writable().clone(),
state_dir
.as_ref()
.map(|d| d.join(format!("{}.overlay.toml", node.name()))),
)
})
.map(Arc::new)
.collect();
let mut provisional = Vec::new();
for state in &configs {
if state.boot().await > 0
&& let Some(s) = cfg.provisional_boot_s
{
provisional.push(state.provisional(std::time::Duration::from_secs(s)));
}
}
let actions: Vec<Arc<action::ActionState>> = host
.nodes()
.iter()
.filter_map(|node| {
action::ActionState::of(
node.name(),
oper::Source::of(node),
node.actions().clone(),
node.action_intervals().clone(),
)
})
.map(Arc::new)
.collect();
let mut tasks = rpc::serve(session.clone(), host.clone(), &configs, &actions)
.await
.context("the @rpc surface could not be declared (RFC 08 §6.1)")?;
let alive = session
.liveliness()
.declare_token(ctx.alive_key().as_keyexpr())
.await
.map_err(|e| anyhow::anyhow!("declare liveliness token: {e}"))?;
let mut device_alive = std::collections::BTreeMap::new();
for node in host.nodes() {
let key = ctx.device_alive_key(node.name());
let token = session
.liveliness()
.declare_token(key.as_keyexpr())
.await
.map_err(|e| anyhow::anyhow!("declare device liveliness token {key}: {e}"))?;
device_alive.insert(node.name().to_string(), token);
}
let device_alive = Arc::new(std::sync::Mutex::new(device_alive));
let devices: Vec<&str> = host.nodes().iter().map(|n| n.name()).collect();
tracing::info!(
origin = %PROFILE.host_id(),
producer = PRODUCER,
devices = ?devices,
"management plane up"
);
let interval = std::time::Duration::from_millis(cfg.telemetry_interval_ms);
for node in host.nodes() {
let name = node.name().to_string();
tasks.push(tokio::spawn(oper::publish_loop(
session.clone(),
PROFILE.local_origin(),
host.clone(),
name.clone(),
oper::Source::of(node),
cfg.ttl_s,
interval,
)));
tasks.push(tokio::spawn(identity::publish_loop(
session.clone(),
PROFILE.local_origin(),
name.clone(),
oper::Source::of(node),
cfg.ttl_s,
)));
tasks.push(tokio::spawn(security::publish_loop(
session.clone(),
PROFILE.local_origin(),
name.clone(),
oper::Source::of(node),
cfg.ttl_s,
)));
tasks.push(tokio::spawn(radio::publish_loop(
session.clone(),
PROFILE.local_origin(),
name.clone(),
oper::Source::of(node),
cfg.ttl_s,
)));
match node.plane() {
Plane::Sdu(plane) => {
tasks.push(tokio::spawn(telemetry::publish_loop(
session.clone(),
PROFILE.local_origin(),
name,
telemetry::Source::Sdu(plane.clone()),
interval,
)));
tasks.push(tokio::spawn(events::publish_loop(
session.clone(),
PROFILE.local_origin(),
plane.clone(),
)));
}
Plane::Netdev(dev) => {
tasks.push(tokio::spawn(presence_loop(
session.clone(),
ctx.device_alive_key(&name),
name.clone(),
dev.clone(),
device_alive.clone(),
interval,
)));
tasks.push(tokio::spawn(telemetry::publish_loop(
session.clone(),
PROFILE.local_origin(),
name.clone(),
telemetry::Source::Netdev(dev.clone()),
interval,
)));
if dev.destinations().is_some() {
tasks.push(tokio::spawn(destination::publish_loop(
session.clone(),
PROFILE.local_origin(),
name.clone(),
dev.clone(),
cfg.ttl_s,
)));
}
let dev: Arc<dyn modem_device::Device> = dev.clone();
tasks.push(tokio::spawn(events::publish_device_loop(
session.clone(),
PROFILE.local_origin(),
name,
dev,
)));
}
}
}
for state in &actions {
tasks.push(tokio::spawn(action::publish_loop(
session.clone(),
PROFILE.local_origin(),
state.clone(),
)));
}
for state in &configs {
tasks.push(tokio::spawn(config::publish_loop(
session.clone(),
PROFILE.local_origin(),
state.clone(),
cfg.ttl_s,
)));
}
tasks.push(tokio::spawn(sensor::publish_loop(
session.clone(),
PROFILE.local_origin(),
host.clone(),
cfg.ttl_s,
)));
for (plane, keys) in state_keys {
tasks.push(tokio::spawn(state::publish_loop(
session.clone(),
keys,
plane,
cfg.ttl_s,
)));
}
tasks.extend(provisional);
Ok(Self {
session,
ctx,
tasks,
_alive: alive,
_device_alive: device_alive,
})
}
pub fn session(&self) -> &Session {
&self.session
}
pub fn context(&self) -> &V1Context {
&self.ctx
}
pub async fn shutdown(self) {
for t in &self.tasks {
t.abort();
}
let _ = self.session.close().await;
}
}
async fn twin_alive(session: &Session, ctx: &V1Context) -> Option<String> {
let key = ctx.alive_key();
let replies = session
.liveliness()
.get(key.as_keyexpr())
.timeout(std::time::Duration::from_secs(2))
.await
.ok()?;
while let Ok(reply) = replies.recv_async().await {
if let Ok(sample) = reply.result() {
return Some(sample.key_expr().to_string());
}
}
None
}
fn session_mode(cfg: &MgmtConfig) -> anyhow::Result<String> {
let mode = cfg.mode.clone().unwrap_or_else(|| {
if cfg.listen.is_empty() && !cfg.connect.is_empty() {
"client".to_string()
} else {
"peer".to_string()
}
});
anyhow::ensure!(
matches!(mode.as_str(), "client" | "peer" | "router"),
"[management] mode {mode:?}: one of client, peer, router"
);
Ok(mode)
}
async fn open_session(cfg: &MgmtConfig) -> anyhow::Result<Session> {
let mode = session_mode(cfg)?;
let mut config = zenoh::Config::default();
let list = |v: &[String]| {
let items: Vec<String> = v.iter().map(|e| format!("{e:?}")).collect();
format!("[{}]", items.join(","))
};
for (path, value) in [
("mode", format!("{mode:?}")),
("scouting/multicast/enabled", cfg.scouting.to_string()),
("connect/endpoints", list(&cfg.connect)),
("listen/endpoints", list(&cfg.listen)),
] {
config
.insert_json5(path, &value)
.map_err(|e| anyhow::anyhow!("management session config {path}: {e}"))?;
}
if let Some(id) = &cfg.usrpwd {
let password = std::fs::read_to_string(&id.password_file).with_context(|| {
format!(
"[management] usrpwd password_file {}",
id.password_file.display()
)
})?;
for (path, value) in [
("transport/auth/usrpwd/user", id.user.as_str()),
(
"transport/auth/usrpwd/password",
password.trim_end_matches(['\n', '\r']),
),
] {
config
.insert_json5(path, &serde_json::to_string(value)?)
.map_err(|e| anyhow::anyhow!("management session config {path}: {e}"))?;
}
}
if mode != "client" {
return zenoh::open(config)
.await
.map_err(|e| anyhow::anyhow!("{e}"));
}
let mut backoff = std::time::Duration::from_secs(1);
let mut said = false;
loop {
match zenoh::open(config.clone()).await {
Ok(session) => return Ok(session),
Err(e) => {
if !said {
tracing::warn!(error = %e, "the management router is not reachable yet; retrying");
said = true;
}
tokio::time::sleep(backoff).await;
backoff = (backoff * 2).min(std::time::Duration::from_secs(10));
}
}
}
}
async fn presence_loop(
session: Session,
key: zenkey::Key,
device: String,
dev: Arc<dyn modem_device::Netdev>,
tokens: Arc<
std::sync::Mutex<std::collections::BTreeMap<String, zenoh::liveliness::LivelinessToken>>,
>,
interval: std::time::Duration,
) {
let mut seen = dev.epoch();
loop {
tokio::time::sleep(interval).await;
let now = dev.epoch();
if now == seen {
continue;
}
seen = now;
drop(tokens.lock().expect("not poisoned").remove(&device));
match session.liveliness().declare_token(key.as_keyexpr()).await {
Ok(token) => {
tokens
.lock()
.expect("not poisoned")
.insert(device.clone(), token);
tracing::info!(device = %device, epoch = now, "the device's presence token cycled");
}
Err(e) => {
tracing::warn!(device = %device, error = %e, "the device's presence token could not be re-declared");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cfg(connect: &[&str], listen: &[&str], mode: Option<&str>) -> MgmtConfig {
MgmtConfig {
connect: connect.iter().map(|s| (*s).to_string()).collect(),
listen: listen.iter().map(|s| (*s).to_string()).collect(),
mode: mode.map(str::to_string),
..MgmtConfig::default()
}
}
#[test]
fn a_session_that_only_connects_is_a_client_of_its_router() {
let mode = session_mode(&cfg(&["tcp/127.0.0.1:7447"], &[], None)).unwrap();
assert_eq!(mode, "client");
}
#[test]
fn a_session_that_listens_is_a_peer_unless_told() {
assert_eq!(
session_mode(&cfg(&[], &["tcp/127.0.0.1:7447"], None)).unwrap(),
"peer"
);
assert_eq!(
session_mode(&cfg(&["tcp/a:1"], &["tcp/b:2"], None)).unwrap(),
"peer",
"a session that both connects and listens is not anybody's client"
);
assert_eq!(
session_mode(&cfg(&["tcp/a:1"], &[], Some("peer"))).unwrap(),
"peer",
"an explicit mode wins"
);
}
#[test]
fn an_unknown_mode_is_refused() {
let err = session_mode(&cfg(&["tcp/a:1"], &[], Some("hub"))).unwrap_err();
assert!(err.to_string().contains("client, peer, router"), "{err}");
}
}