Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/ziggurat-driver/src/zigbee_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod mac;
mod neighbor;
mod nwk;
mod route;
mod route_refresh;
mod send_handle;
mod sends;
mod tasklets;
Expand All @@ -57,6 +58,7 @@ pub use ziggurat_zigbee::nwk::broadcast_budget::{
pub use ziggurat_zigbee::nwk::broadcasts::Broadcasts;
pub use ziggurat_zigbee::nwk::neighbors::Neighbors;
pub use ziggurat_zigbee::nwk::next_hop::{HostRoute, RouteDirective, SendMode};
pub use ziggurat_zigbee::nwk::route_refresh::RouteRefresh;
pub use ziggurat_zigbee::nwk::routing::Routing;
pub use ziggurat_zigbee::nwk::security::NwkSecurity;
pub use ziggurat_zigbee::nwk::{neighbors, routing};
Expand Down Expand Up @@ -358,6 +360,8 @@ pub struct Nib {
pub mtorr_debouncer: Debouncer,
/// NWK routing state and decision logic (route/discovery/record tables)
pub routing: Routing,
/// The routes to look at again in the background, and when
pub route_refresh: RouteRefresh,
/// Broadcast deduplication and passive acknowledgment accounting
pub broadcasts: Broadcasts,
/// The network key, its outgoing frame counter, and per-relayer replay protection
Expand Down Expand Up @@ -517,6 +521,7 @@ impl State {
link_status_debouncer: Debouncer::default(),
mtorr_debouncer: Debouncer::default(),
routing: Routing::default(),
route_refresh: RouteRefresh::default(),
broadcasts: Broadcasts::default(),
nwk_security: NwkSecurity::new(
config.network_key.clone(),
Expand Down Expand Up @@ -728,6 +733,9 @@ pub struct ZigbeeStack<P: RadioPhy, R: Runtime> {
/// earlier
pub(crate) mtorr_wake: Notify,

/// Wakes the route refresh task when a trigger queued a destination
pub(crate) route_refresh_kick: Notify,

/// Signaled whenever a link status command is digested; the MTORR startup wait
/// uses it to advertise as soon as a neighbor link is established
pub(crate) link_status_received: Notify,
Expand Down Expand Up @@ -880,6 +888,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
src_match_written: Mutex::new(SrcMatchTable::default()),
parent_annce_received: Mutex::new(None),
mtorr_wake: Notify::new(),
route_refresh_kick: Notify::new(),
link_status_received: Notify::new(),
link_status_wake: Notify::new(),
address_conflict_wake: Notify::new(),
Expand Down Expand Up @@ -1141,6 +1150,11 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
});
}

// Look again, in the background, at routes that may have become cheaper
self.spawn_tracked(|arc_self| async move {
arc_self.route_refresh_task().await;
});

// Reprogram the radio whenever it resets out from under us
self.spawn_tracked(|arc_self| async move {
arc_self.radio_recovery_task().await;
Expand Down
13 changes: 13 additions & 0 deletions crates/ziggurat-driver/src/zigbee_stack/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.request_link_status_for_new_router();
}

// The link works again: the routes that failed through it may be cheaper
// through it than on their detour
if let Some(neighbor_nwk) = outcome.restored_link {
self.note_hop_recovered(neighbor_nwk);
}

self.link_status_received.notify_one();
}

Expand Down Expand Up @@ -233,6 +239,13 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
// The network start has just sent one
self.restart_link_status_period();

// Early link statuses wait for the first periodic one, which lists every
// router heard for a whole period
if self.tunables.hold_early_link_status_after_start() {
let first_period_end = self.core_now() + self.tunables.link_status_period();
self.core().nib.link_status_debouncer.hold(first_period_end);
}

loop {
self.wait_for_debouncer(|nib| &nib.link_status_debouncer, &self.link_status_wake)
.await;
Expand Down
7 changes: 7 additions & 0 deletions crates/ziggurat-driver/src/zigbee_stack/nwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.core_now(),
self.activity_params(),
);
core.nib.route_refresh.forget(nwk);
core.sends.fail_unrouted(nwk);
self.commit_sends(core);
}
Expand Down Expand Up @@ -405,6 +406,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
let mut core = self.core();
nwk_frame.nwk_header.sequence_number = core.nib.next_sequence_number();

let hint_stands_in = self.hint_stands_in_for_a_route(&core.nib, destination, &mode);
let resolved = next_hop::resolve(
&core.nib.routing,
&core.nib.neighbors,
Expand All @@ -416,6 +418,10 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {

let handle = match resolved {
NextHop::Resolved(next_hop) => {
if hint_stands_in {
self.queue_hinted_refresh(&mut core.nib, destination);
}

let poll_address = Self::poll_address(&core.nib, next_hop);
self.enqueue_unicast_in(
&mut core,
Expand Down Expand Up @@ -708,6 +714,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.tunables.broadcast_token_refill(),
self.tunables.broadcast_critical_reserve(),
self.tunables.broadcast_forwarding_reserve(),
self.tunables.broadcast_host_reserve(),
);

if let BroadcastAdmission::Defer { retry_in } = admission {
Expand Down
17 changes: 16 additions & 1 deletion crates/ziggurat-driver/src/zigbee_stack/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.activity_params(),
);

// A reply to our refresh counts as an answer even when it does not replace the
// route, so it is noted before the disposition can drop it
if route_reply_cmd.originator_nwk == self.state.network_address {
self.note_refresh_reply(route_reply_cmd.responder_nwk, outcome.update);
}

let (next_hop_nwk, path_cost) = match outcome.disposition {
RouteReplyDisposition::Drop => return,
RouteReplyDisposition::Established => {
Expand Down Expand Up @@ -408,8 +414,17 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
}
}

/// Every active route through `next_hop` fails. Their destinations are noted as
/// displaced from it, so route refresh tries them again when the hop recovers.
pub(super) fn invalidate_routes_via(&self, next_hop: Nwk) {
self.core().nib.routing.invalidate_via(next_hop);
let now = self.core_now();
let mut guard = self.core();
let nib = &mut guard.nib;

for destination in nib.routing.invalidate_via(next_hop) {
nib.route_refresh.note_displaced(destination, next_hop, now);
}
drop(guard);
}

/// Zigbee spec 3.6.4.8.1: another router could not deliver a frame we originated;
Expand Down
Loading
Loading