Skip to content
Merged
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
47 changes: 43 additions & 4 deletions crates/ziggurat-driver/src/zigbee_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use core::future::Future;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering as AtomicOrdering};
use core::time::Duration;
use ziggurat_zigbee::debouncer::Debouncer;
use ziggurat_zigbee::flat_map::FlatMap;
use ziggurat_zigbee::nwk::activity::ActivityParams;
use ziggurat_zigbee::nwk::frame::{NwkFrame, NwkSecurityHeaderKeyId};

mod aps;
Expand Down Expand Up @@ -350,6 +352,10 @@ pub struct Nib {

/// The neighbor table: link quality accounting, link status digestion, aging
pub neighbors: Neighbors,
/// When the next link status broadcast is due
pub link_status_debouncer: Debouncer,
/// When the next many-to-one route request is due
pub mtorr_debouncer: Debouncer,
/// NWK routing state and decision logic (route/discovery/record tables)
pub routing: Routing,
/// Broadcast deduplication and passive acknowledgment accounting
Expand Down Expand Up @@ -508,6 +514,8 @@ impl State {
update_id: config.update_id,
tx_total: 0,
neighbors: Neighbors::default(),
link_status_debouncer: Debouncer::default(),
mtorr_debouncer: Debouncer::default(),
routing: Routing::default(),
broadcasts: Broadcasts::default(),
nwk_security: NwkSecurity::new(
Expand Down Expand Up @@ -716,13 +724,15 @@ pub struct ZigbeeStack<P: RadioPhy, R: Runtime> {
/// network-wide broadcast storm (spec 2.4.3.1.12.2)
pub(crate) parent_annce_received: Mutex<Option<CoreInstant>>,

/// Wakes the MTORR scheduler before its max interval when accumulated route
/// errors or delivery failures cross their thresholds
pub(crate) mtorr_kick: Notify,
/// Wakes the many-to-one route request task when its next advertisement moved
/// earlier
pub(crate) mtorr_wake: 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,
/// Wakes the link status task when the next link status broadcast moved earlier
pub(crate) link_status_wake: Notify,
pub(crate) address_conflict_wake: Notify,
/// Wakes the beacon-spam reactor when a beacon request opens its spray window.
pub(crate) beacon_spam_wake: Notify,
Expand Down Expand Up @@ -798,6 +808,34 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
R::sleep(deadline.saturating_duration_since(self.core_now())).await;
}

/// What the tables need to keep their activity counters.
pub fn activity_params(&self) -> ActivityParams {
ActivityParams {
initial_count: self.tunables.router_age_limit(),
period: self.tunables.link_status_period(),
}
}

/// Sleep until a debouncer fires. `wake` signals that a request moved its next
/// firing earlier: the sleep then starts again, until the new time.
async fn wait_for_debouncer(
&self,
debouncer: impl Fn(&Nib) -> &Debouncer + Send,
wake: &Notify,
) {
loop {
let next = debouncer(&self.core().nib).next();
let due = core::pin::pin!(self.sleep_until_core(next));
let moved = core::pin::pin!(wake.notified());

if let futures::future::Either::Left(((), _)) =
futures::future::select(due, moved).await
{
return;
}
}
}

/// Run `future`, failing with [`Elapsed`] if a [`CoreInstant`] deadline passes first.
async fn timeout_at_core<F>(
&self,
Expand Down Expand Up @@ -841,8 +879,9 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
src_match_sync: Notify::new(),
src_match_written: Mutex::new(SrcMatchTable::default()),
parent_annce_received: Mutex::new(None),
mtorr_kick: Notify::new(),
mtorr_wake: Notify::new(),
link_status_received: Notify::new(),
link_status_wake: Notify::new(),
address_conflict_wake: Notify::new(),
beacon_spam_wake: Notify::new(),
maintenance_wake: Notify::new(),
Expand Down
2 changes: 2 additions & 0 deletions crates/ziggurat-driver/src/zigbee_stack/joining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
relationship: neighbors::Relationship::UnauthenticatedChild,
},
self.core_now(),
self.activity_params(),
);

// A new child deadline may precede everything the maintenance task knows
Expand Down Expand Up @@ -1180,6 +1181,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
},
},
self.core_now(),
self.activity_params(),
);

// A new child deadline may precede everything the maintenance task knows
Expand Down
83 changes: 65 additions & 18 deletions crates/ziggurat-driver/src/zigbee_stack/neighbor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::runtime::Runtime;
use alloc::vec::Vec;
use core::time::Duration;
use ziggurat_ieee_802154::types::{Eui64, Nwk};
use ziggurat_phy::RadioPhy;

use ziggurat_zigbee::nwk::NwkDeviceType;
use ziggurat_zigbee::nwk::commands::{NwkCommand, NwkLinkStatusCommand};
use ziggurat_zigbee::nwk::frame::{BROADCAST_ALL_ROUTERS_AND_COORDINATOR, NwkFrame};

Expand All @@ -17,7 +19,43 @@ const MAX_LINK_STATUSES: usize = 7;

impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
pub(super) fn maybe_recompute_lqa(&self, sender_nwk: Nwk, lqi: u8, _rssi: i8) {
self.core().nib.neighbors.record_lqa(sender_nwk, lqi);
let link_status_due = self.core().nib.neighbors.record_lqa(sender_nwk, lqi);

if link_status_due {
self.request_link_status_for_new_router();
}
}

/// Send the next link status soon: a router it will newly list waits for it to
/// learn its cost.
fn request_link_status_for_new_router(&self) {
// Coordinator only. Routers must not run this as it is: every router that
// hears a new neighbor would send within the same half second, each with its
// whole list, and most of those frames would collide.
if self.state.role == NwkDeviceType::Coordinator {
self.request_early_link_status();
Comment thread
puddly marked this conversation as resolved.
Resolved
}
}

/// Send the next link status after the delay of a gratuitous one (spec 3.6.4.4.2).
fn request_early_link_status(&self) {
let min_jitter = self.tunables.min_router_bootstrap_jitter();
let max_jitter = self.tunables.max_router_bootstrap_jitter();
let jitter = min_jitter
+ max_jitter
.saturating_sub(min_jitter)
.mul_f32(crate::rng::random_f32::<R>());

let at = self.core_now() + jitter;

if self
.core()
.nib
.link_status_debouncer
.request(at, Duration::ZERO)
{
self.link_status_wake.notify_one();
}
}

pub(super) fn end_device_child_eui64(&self, nwk: Nwk) -> Option<Eui64> {
Expand Down Expand Up @@ -84,6 +122,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
lqi,
&link_status_cmd,
self.core_now(),
self.activity_params(),
);

// The neighbor no longer answers at its old address, so routes through it
Expand All @@ -101,6 +140,14 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.invalidate_routes_via(neighbor_nwk);
}

if outcome.link_status_answer_due {
self.request_early_link_status();
} else if outcome.link_status_due
&& self.tunables.hack_early_link_status_for_unknown_routers()
{
self.request_link_status_for_new_router();
}

self.link_status_received.notify_one();
}

Expand All @@ -114,21 +161,12 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
return Err(EnqueueError::NotStarted);
}

// Decrement the `recent_activity` field of every active routing table entry
self.core().nib.routing.decay_activity();

self.maybe_age_neighbors();

// Decrement the inbound and outbound activity fields for neighbors
let mut link_statuses = {
let mut core = self.core();
core.nib.neighbors.decay_activity();

if empty {
Vec::new()
} else {
core.nib.neighbors.link_status_entries()
}
let mut link_statuses = if empty {
Vec::new()
} else {
self.core().nib.neighbors.link_status_entries()
};

// Link statuses are sorted in ascending order
Expand Down Expand Up @@ -184,19 +222,28 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
}
}

fn restart_link_status_period(&self) {
let now = self.core_now();
let period = self.tunables.link_status_period();

self.core().nib.link_status_debouncer.fired(now, period);
}

pub async fn periodic_link_status_broadcast_task(&self) {
let mut next = self.core_now() + self.tunables.link_status_period();
// The network start has just sent one
self.restart_link_status_period();

loop {
self.sleep_until_core(next).await;
self.wait_for_debouncer(|nib| &nib.link_status_debouncer, &self.link_status_wake)
.await;

self.restart_link_status_period();

// The link status broadcast has a radius of 1 so delivery does not wait for
// a relay quorum.
if let Ok(link_status) = self.send_link_status_broadcast(false) {
let _ = link_status.delivered().await;
}

next = next + self.tunables.link_status_period();
}
}
}
31 changes: 22 additions & 9 deletions crates/ziggurat-driver/src/zigbee_stack/nwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
}

pub(super) fn forget_nwk_state_in(&self, mut core: CoreGuard<'_>, nwk: Nwk) {
core.nib.routing.forget(self.state.network_address, nwk);
core.nib.routing.forget(
self.state.network_address,
nwk,
self.core_now(),
self.activity_params(),
);
core.sends.fail_unrouted(nwk);
self.commit_sends(core);
}
Expand Down Expand Up @@ -442,6 +447,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
now,
self.tunables.route_discovery_time(),
self.tunables.pending_route_discovery_attempts(),
self.activity_params(),
);
if let Some(route_request_identifier) = route_request_identifier {
self.broadcast_discovery_request(
Expand Down Expand Up @@ -569,13 +575,19 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
let relaying_ieee = core.nib.address_map.eui64_for(next_hop_address);

if let Some(relaying_ieee) = relaying_ieee {
core.nib.neighbors.record_outbound_activity(relaying_ieee);
core.nib.neighbors.record_outbound_activity(
relaying_ieee,
self.core_now(),
self.tunables.link_status_period(),
);
}

// And the routing table counters
core.nib
.routing
.record_usage(nwk_frame.nwk_header.destination);
core.nib.routing.record_usage(
nwk_frame.nwk_header.destination,
self.core_now(),
self.tunables.link_status_period(),
);
}

self.increment_tx_total();
Expand Down Expand Up @@ -836,10 +848,11 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.maybe_recompute_lqa(sender_nwk, lqi, rssi);

// Receiving a unicast from a neighbor counts as inbound activity
self.core()
.nib
.neighbors
.record_inbound_activity(sender_nwk);
self.core().nib.neighbors.record_inbound_activity(
sender_nwk,
self.core_now(),
self.tunables.link_status_period(),
);

// Spec 3.6.2.2: the radius is decremented on receipt and a frame that reaches
// zero is never retransmitted
Expand Down
Loading
Loading