Skip to content

Improve Link Status handling and LQA computation - #144

Merged
puddly merged 3 commits into
devfrom
puddly/link-status-reading-fixes
Nov 18, 2026
Merged

puddly merged 3 commits into
devfrom
puddly/link-status-reading-fixes

Conversation

@puddly

@puddly puddly commented Nov 17, 2026

Copy link
Copy Markdown
Member

We had a few defects in how Link Status was handled:

  • LQA was computed only after three samples were received. This is too strict, we are allowed to compute LQA with fewer than three.
  • We did not properly parse incoming Link Status lists. This technically was not an issue because our NWK was assumed to be 0x0000 but this isn't something we should rely on.
  • If a link status omits our address, the outgoing cost to the neighbor should not be zeroed out.

This addresses part of #143

Copilot AI balanced review requested due to automatic review settings November 17, 2026 15:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Zigbee link-quality and Link Status changes.

No open issues 🎉

What changed in this PR

This PR improves Zigbee neighbor link-quality calculation and Link Status handling, addressing part of the startup routing problem described in #143.

Changes:

  • Compute median link quality from available samples without waiting for a full window, including in ZDP neighbor responses.
  • Respect Link Status frame address ranges and preserve outgoing costs for empty frames or frames that do not cover the local address.
  • Add regression tests for partial sample windows, missing samples, and Link Status frame handling.
File Description
crates/​ziggurat-zigbee/​src/​nwk/​neighbors.rs Updates median calculation and outgoing-cost handling; adds regression tests.
crates/​ziggurat-driver/​src/​zigbee_stack/​zdp.rs Uses the shared median calculation for reported neighbor link quality.

Review effort: Balanced

@puddly
puddly added this pull request to stack #146 November 17, 2026 16:12
@zigpy-review-bot zigpy-review-bot added the bugfix This PR fixes a bug label Nov 17, 2026
@puddly
puddly merged commit 61c26b9 into dev Nov 18, 2026
3 checks passed
t3hk0d3 added a commit to t3hk0d3/ziggurat that referenced this pull request Nov 18, 2026
The stack starts with an empty sibling table, so an early link status sent
within the first period lists only the routers heard so far. A router that
has not spoken yet (it waits for its own 15 s period) reads that list as
"I do not hear you" and, with zigpy#144, zeroes its cost toward us: it then drops
our route requests and cannot carry route replies back to us until our next
link status. On the bench this kept 0x0391 on a cost-4 detour after every
restart.

Debouncer::hold keeps requests from moving a firing before a given time.
The periodic link status task holds its debouncer for the first period
when the new hold_early_link_status_after_start tunable is on (default).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
t3hk0d3 added a commit to t3hk0d3/ziggurat that referenced this pull request Nov 18, 2026
The stack starts with an empty sibling table, so an early link status sent
within the first period lists only the routers heard so far. A router that
has not spoken yet (it waits for its own 15 s period) reads that list as
"I do not hear you" and, with zigpy#144, zeroes its cost toward us: it then drops
our route requests and cannot carry route replies back to us until our next
link status. On the bench this kept 0x0391 on a cost-4 detour after every
restart.

Debouncer::hold keeps requests from moving a firing before a given time.
The periodic link status task holds its debouncer for the first period
when the new hold_early_link_status_after_start tunable is on (default).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@zigpy-review-bot zigpy-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge review. Using the samples held for the median matches spec 3.6.3, and the covers_us range check is correct for first, middle and last frames, for the boundary entry two frames share, and for an own address of 0x0000. This PR also fixes a real problem the old code had: zeroing the cost on every first frame made a non-coordinator listed in a later frame see a lost_link every period. One item needs a decision, and there is one small bug that is older than this PR.

1. An empty list now never zeroes the cost (maintainer call)

Skipping empty lists is a deliberate deviation from 3.6.4.4.2. The spec discards a frame whose range does not cover the receiver "unless the link status list was completely empty", so an empty list is processed, and the natural reading is that the receiver is then not found and its outgoing cost is set to 0. It fixes the startup problem described in #143, but nothing bounds it. A neighbor that stops hearing us and has no other router neighbor sends empty lists every period. Each one refreshes last_link_status_timestamp, so age() never ages the link out, the cost stays non-zero, and lost_link never fires. A probe with 100 empty lists at 15 s spacing kept the cost at 1. Details and options are in #154.

2. The first link status from an unknown router counts as two samples (older than this PR)

The receive path buffers every frame's LQI before on_link_status runs. For an unknown sender, on_link_status builds the entry from that buffer and then pushes the same LQI again. With the median now taken from the samples held, one outlier first frame decides the link cost until two more frames arrive. Fix: #153.

Notes, no change asked

  • age() clears samples only for entries with a non-zero outgoing cost. A router whose link status we heard once but that never listed us keeps its samples, so it stays in our link status after it is gone. That cannot affect routing (its outgoing cost is 0), but it takes up entries. One overheard frame is now enough to be listed.
  • RouterConnectivity and RouterNeighborSetDiversity count the boundary entry two frames share twice. Nothing reads either field yet.
Verified (6 checks)
  • Read the diff and every caller of the removed TableEntry::lqa(): incoming_link_cost (route request/reply costs, broadcast relayer selection), link_status_entries, Mgmt_Lqi_rsp.
  • covers_us probed for first, middle, last, shared-boundary and first+last frames, own address 0x0000 included.
  • The new tests test what their names say.
  • Probe tests (not committed) for the empty-list case and the aging note.
  • Confirmed by a failing test on dev that the first link status from an unknown router counts twice (cost 5 instead of 3).
  • cargo test --workspace --all-features at 34f6e5f: 199 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants