feat(wireless): get_radio_status says whether each radio runs (NetOrk/netork#31)

up, pending and setup_failed come from netifd (ubus call network.wireless
status, its retry_setup_failed for the last), beside the configuration read
before. A key netifd does not answer with a boolean is left out, and all
three are left out where the call fails or breaks off; the configuration
stays either way. On 2026-07-16 every radio of the fleet sat in a restart
loop with its configuration saying enabled; this is the state that showed it.

The htmode table moves to module level, which keeps get_radio_status within
the quality ratchet.
This commit is contained in:
Christian Manivong committed 2026-11-28 03:02:21 +01:00
1 parent ecf8c427fc
commit dcb4932535
4 files changed
+248 -22

No files matched your search

+73 -21
View File
@@ -12,6 +12,7 @@
from __future__ import annotations
import json
import logging
import re
from typing import Any
@@ -33,6 +34,55 @@ _ACL_OPTIONS = ("macfilter", "maclist")
_MACFILTER_BY_MODE = {"whitelist": "allow", "blacklist": "deny", "off": None}
#: netifd's state of each radio: whether it runs, not what it is configured to do.
_WIRELESS_STATUS = "ubus call network.wireless status"
#: The runtime keys of ``RadioStatusDict`` and netifd's name for each.
_RUNTIME_KEYS = {"up": "up", "pending": "pending", "setup_failed": "retry_setup_failed"}
#: Channel bandwidth in MHz by ``htmode`` (``VHT80`` is 80 MHz).
_HTMODE_WIDTH = {
"HT20": 20,
"HT40": 40,
"VHT20": 20,
"VHT40": 40,
"VHT80": 80,
"VHT80+80": 80,
"VHT160": 160,
"HE20": 20,
"HE40": 40,
"HE80": 80,
"HE160": 160,
"EHT20": 20,
"EHT40": 40,
"EHT80": 80,
"EHT160": 160,
"EHT320": 320,
}
def parse_wireless_status(text: str) -> dict[str, dict[str, bool]]:
"""``up``, ``pending`` and ``setup_failed`` per radio from netifd's JSON answer.
A key netifd does not give as a boolean is left out, never guessed: before
OpenWrt 19.07 it had no ``retry_setup_failed``. Text that is no JSON object
(a refused call) gives {}.
"""
try:
status = json.loads(text)
except ValueError:
return {}
if not isinstance(status, dict):
return {}
radios: dict[str, dict[str, bool]] = {}
for radio, state in status.items():
if isinstance(state, dict):
radios[radio] = {
key: state[name]
for key, name in _RUNTIME_KEYS.items()
if isinstance(state.get(name), bool)
}
return radios
def _mac_acl_changes(sec: UciSection, mode: str, macs: list[str]) -> list[str]:
"""The ``uci`` changes that give *sec* the ACL *mode* with *macs*.
@@ -659,10 +709,11 @@ class OpenWrtWirelessMixin(UciStepsMixin):
self._uci_apply(_WIRELESS, changes, _WIFI_RELOAD)
def get_radio_status(self) -> dict[str, Any]:
"""Return radio status from UCI and iwinfo.
"""Return radio status from UCI, iwinfo and netifd.
Combines ``uci show wireless`` for static config with ``iwinfo``
output for runtime channel/frequency and tx-power data.
output for runtime channel/frequency and tx-power data, and
``ubus call network.wireless status`` for whether each radio runs.
Returns a dict keyed by radio name (e.g. ``"radio0"``) with:
@@ -674,6 +725,8 @@ class OpenWrtWirelessMixin(UciStepsMixin):
* frequency (float) — centre frequency in MHz (0 if unknown)
* htmode (str) — e.g. ``"HT20"``, ``"VHT80"``, ``"HE80"``
* country (str) — regulatory country code, e.g. ``"DE"``
* up, pending, setup_failed (bool) — netifd's state; left out without
an answer from ubus (see :func:`parse_wireless_status`)
"""
from napalm_device_types.models import RadioStatusDict
@@ -714,25 +767,6 @@ class OpenWrtWirelessMixin(UciStepsMixin):
htmode = cfg.get("htmode", "")
country = cfg.get("country", "")
# Derive channel_width from htmode string (e.g. VHT80 → 80 MHz)
_HTMODE_WIDTH = {
"HT20": 20,
"HT40": 40,
"VHT20": 20,
"VHT40": 40,
"VHT80": 80,
"VHT80+80": 80,
"VHT160": 160,
"HE20": 20,
"HE40": 40,
"HE80": 80,
"HE160": 160,
"EHT20": 20,
"EHT40": 40,
"EHT80": 80,
"EHT160": 160,
"EHT320": 320,
}
channel_width = _HTMODE_WIDTH.get(htmode.upper(), 0)
result[radio] = {
@@ -790,4 +824,22 @@ class OpenWrtWirelessMixin(UciStepsMixin):
if pwr_m:
result[current_radio]["tx_power"] = int(pwr_m.group(1))
self._add_radio_runtime(result)
return result
def _add_radio_runtime(self, radios: dict[str, Any]) -> None:
"""Add to *radios* whether each runs, as netifd says it (NetOrk/netork#31).
Nothing is added without an answer, and no radio the configuration lacks.
"""
try:
answer = self.run_command(_WIRELESS_STATUS, timeout=30)
except Exception as exc: # noqa: BLE001 - like iwinfo: the configuration stays
logger.debug("%s failed: %s", _WIRELESS_STATUS, exc)
return
if answer.exit_code:
logger.debug("%s exited %s: %s", _WIRELESS_STATUS, answer.exit_code, answer.stderr)
return
for radio, state in parse_wireless_status(answer.stdout).items():
if radio in radios:
radios[radio].update(state)