Files
napalm-openwrt/napalm_openwrt/time_sync_mixin.py
T
Christian Manivong 85a679fe1a
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m2s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m6s
driver / lowest (pull_request) Successful in 33s
driver / build (pull_request) Successful in 42s
driver / security (pull_request) Successful in 1m17s
CI / driver (pull_request) Successful in 2m36s
driver / publish (pull_request) Skipped
feat(time-sync): keep DHCP's NTP servers and read them apart
DHCP option 42 counts (NetOrk/netork#2380): a router that gets the
site's time giver from its lease is aligned. The first time sync commit
set use_dhcp=0 on every write, which cut a router off from the giver its
DHCP server hands out.

A write now leaves use_dhcp as the router has it, and the "already has
these servers" check no longer rewrites a router only because use_dhcp
is on. get_time_sync() reads what sysntpd adds to ntpd's list while
use_dhcp is on -- each interface's data.ntpserver in ubus call
network.interface dump, which /lib/netifd/dhcp.script fills from
udhcpc's ntpsrv, only the dhcp_interface list's where it names any -- in
the same round trip as uci, into dhcp_ntp_servers: [] while use_dhcp or
the client is off, None where netifd gives no answer.

Needs napalm-device-types 4.36.0 (TimeSyncDict.dhcp_ntp_servers).
2026-11-11 22:13:19 +01:00

359 lines
15 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Time sync on OpenWrt: UCI's ``system`` config (napalm-device-types ``TimeSyncMixin``).
OpenWrt keeps both values in ``/etc/config/system`` (NetOrk/netork#2364):
* **The servers** are the ``system.ntp.server`` list, which ``/etc/init.d/sysntpd``
hands to BusyBox ntpd while ``system.ntp.enabled`` is on. A disabled client has no
servers: the list names nothing the clock takes time from.
* **What DHCP brings** (option 42) counts too (NetOrk/netork#2380): while ``use_dhcp``
is on (the default) sysntpd adds the NTP servers of every interface's lease -- or of
the ``dhcp_interface`` list's only -- that netifd keeps as ``data.ntpserver`` in
``ubus call network.interface dump`` (``/lib/netifd/dhcp.script`` stores udhcpc's
``ntpsrv`` there). They are read from the same place, in the same order, as
``dhcp_ntp_servers``; a write leaves ``use_dhcp`` as the router has it.
* **The zone** is kept twice: ``zonename`` (the IANA name, as LuCI writes it -- with
spaces where the IANA name has underscores) and ``timezone``, the POSIX rule
``/etc/init.d/system`` writes to ``/tmp/TZ`` for libc. The rule is what the clock
runs on: an IANA name written there left an AP on UTC (NetOrk/netork#1849). So the
name is read as the zone only where the rule in force is that zone's; a rule
without a matching name is read as the one zone that has it, or as no zone where
several do (``CET-1CEST,...`` is Berlin, Paris, Madrid, ...).
Reading is one round trip as the login user: ``uci -q show system``, then netifd's
dump (:data:`READ_WITH_DHCP`). A write reads ``uci`` alone. Every write compares
first and sends nothing when the router already has the values -- an application
aligns on every poll -- else sets the options, commits ``system`` and applies them:
``/etc/init.d/sysntpd restart`` for the servers, ``/etc/init.d/system reload`` and a
restart of logd, which reads the zone when it starts, for the zone. Writes run as
root (``run_command(privileged=True)``); OpenWrt has no sudo.
``synchronized`` is None: BusyBox ntpd keeps no state another process can read, and
``ubus call system info`` says nothing about the clock's source.
"""
from __future__ import annotations
import json
import re
import shlex
from collections import defaultdict
from dataclasses import dataclass, field
from functools import lru_cache
from importlib.resources import files
from typing import TYPE_CHECKING, Any, ClassVar
from napalm_device_types import FixStepOutcomeDict
from napalm_device_types.posix_tz import is_iana_zone, parse_posix_tz, posix_tz
from napalm_device_types.time_sync import TIME_SYNC_VALUES, TimeSyncDict, TimeSyncMixin
if TYPE_CHECKING:
from napalm_device_types import CommandResult
READ_COMMAND = "uci -q show system"
#: The line between uci's answer and netifd's in :data:`READ_WITH_DHCP`.
DHCP_MARK = "@@napalm-openwrt: network.interface dump"
#: ``get_time_sync``'s reading: uci's ``system`` config, then netifd's interfaces, where
#: a lease keeps its NTP servers. Its exit code is uci's: ubus may refuse, uci may not.
READ_WITH_DHCP = (
f"{READ_COMMAND} && {{ echo {shlex.quote(DHCP_MARK)}; "
"ubus call network.interface dump 2>/dev/null || true; }"
)
#: The rule ``/etc/init.d/system`` uses where the section names none.
DEFAULT_RULE = "UTC"
#: A zone of the database that stands for "not known", not for a place.
_NO_PLACE = frozenset({"Factory"})
#: ``system.<section>.<option>='value' ['value' ...]`` of the two sections that count.
_OPTION = re.compile(r"^system\.(?P<section>@system\[0\]|ntp)\.(?P<option>\w+)=(?P<value>.*)$")
_FALSE = frozenset({"0", "no", "off", "false", "disabled"})
@dataclass
class SystemUci:
"""What ``uci -q show system`` says about the clock."""
has_system: bool = False
has_ntp: bool = False
system: dict[str, list[str]] = field(default_factory=dict)
ntp: dict[str, list[str]] = field(default_factory=dict)
def _one(self, options: dict[str, list[str]], name: str) -> str | None:
values = options.get(name)
return values[0] if values else None
@property
def configured_servers(self) -> list[str]:
"""The list as it stands; an option of servers apart by spaces is split, as sysntpd does."""
return [s for value in self.ntp.get("server", []) for s in value.split()]
@property
def ntp_enabled(self) -> bool:
return (self._one(self.ntp, "enabled") or "1").lower() not in _FALSE
@property
def dhcp_servers(self) -> bool:
"""Whether sysntpd adds a lease's servers: the client runs and ``use_dhcp`` is on."""
use_dhcp = (self._one(self.ntp, "use_dhcp") or "1").lower() not in _FALSE
return self.has_ntp and self.ntp_enabled and use_dhcp
@property
def dhcp_interfaces(self) -> list[str]:
"""The interfaces whose leases count; [] for every interface, as sysntpd does."""
return [i for value in self.ntp.get("dhcp_interface", []) for i in value.split()]
@property
def servers(self) -> list[str]:
"""The servers the clock takes time from: none while the client is off."""
return self.configured_servers if self.has_ntp and self.ntp_enabled else []
@property
def zonename(self) -> str | None:
"""``zonename`` as an IANA name: LuCI's spaces back to underscores."""
name = self._one(self.system, "zonename")
return name.strip().replace(" ", "_") if name and name.strip() else None
@property
def rule(self) -> str:
return (self._one(self.system, "timezone") or "").strip() or DEFAULT_RULE
@property
def zone(self) -> str | None:
"""The zone the clock runs in, by name; None where no single zone is it."""
if not self.has_system:
return None
name = self.zonename
named = posix_tz(name) if name and is_iana_zone(name) else None
if name and named and same_time(self.rule, named):
return name
return zone_for_rule(self.rule)
def parse_system_uci(output: str) -> SystemUci:
"""``uci show system`` in parts: the first system section and the ``ntp`` one."""
parsed = SystemUci()
for line in output.splitlines():
line = line.strip()
if line.startswith("system.@system[0]="):
parsed.has_system = True
elif line.startswith("system.ntp="):
parsed.has_ntp = True
match = _OPTION.match(line)
if match is None:
continue
try:
values = shlex.split(match["value"])
except ValueError:
continue
options = parsed.system if match["section"] == "@system[0]" else parsed.ntp
options[match["option"]] = values
return parsed
def _lease_servers(entry: Any, interfaces: list[str]) -> list[str]:
"""One interface's ``data.ntpserver``, if it is one of *interfaces* (or they are [])."""
if not isinstance(entry, dict) or (interfaces and entry.get("interface") not in interfaces):
return []
data = entry.get("data")
servers = data.get("ntpserver") if isinstance(data, dict) else None
return servers.split() if isinstance(servers, str) else []
def lease_ntp_servers(dump: str, interfaces: list[str]) -> list[str] | None:
"""The NTP servers in netifd's ``network.interface dump``, as sysntpd collects them:
each interface's ``data.ntpserver`` (apart by spaces) in netifd's order, of
*interfaces* only where it names any, each once. None for no readable answer."""
try:
parsed = json.loads(dump)
except ValueError:
return None
if not isinstance(parsed, dict) or not isinstance(parsed.get("interface"), list):
return None
found: list[str] = []
for entry in parsed["interface"]:
found.extend(s for s in _lease_servers(entry, interfaces) if s not in found)
return found
def _time_of(rule: str) -> tuple[Any, ...] | None:
"""The offsets and transitions of *rule*, without the names it gives them."""
parts = parse_posix_tz(rule)
if parts is None:
return None
return (
parts["utc_offset"],
parts["dst_offset"],
tuple(sorted((parts["dst_start"] or {}).items())),
tuple(sorted((parts["dst_end"] or {}).items())),
)
def same_time(rule: str, other: str) -> bool:
"""Whether two rules keep the same time: ``GMT0`` is ``UTC0`` by another name."""
time = _time_of(rule)
return rule == other if time is None else time == _time_of(other)
@lru_cache(maxsize=1)
def _zones_by_rule() -> dict[str, tuple[str, ...]]:
"""Every zone of the database (not its links) by its rule, names and all."""
source = files("tzdata").joinpath("zoneinfo", "tzdata.zi").read_text(encoding="utf-8")
zones: defaultdict[str, list[str]] = defaultdict(list)
for line in source.splitlines():
if not line.startswith("Z "):
continue
zone = line.split()[1]
rule = posix_tz(zone)
if rule is not None and zone not in _NO_PLACE:
zones[_rule_key(rule)].append(zone)
return {rule: tuple(names) for rule, names in zones.items()}
def _rule_key(rule: str) -> str:
parts = parse_posix_tz(rule)
return rule if parts is None else repr(sorted(parts.items()))
def zone_for_rule(rule: str) -> str | None:
"""The one zone whose rule *rule* is; None where none or several are.
Several zones share most rules (``CET-1CEST,M3.5.0,M10.5.0/3`` is Berlin, Paris,
Madrid, ...): which of them a router means is not the driver's to guess. Links
(``Asia/Calcutta``) count as the zone they point at.
"""
if not rule:
return None
zones = _zones_by_rule().get(_rule_key(rule), ())
return zones[0] if len(zones) == 1 else None
def _luci_name(zone: str) -> str:
"""*zone* as LuCI keeps it in ``zonename``, so its zone list shows it chosen."""
return zone.replace("_", " ")
def _uci_command(*words: str) -> str:
return shlex.join(words)
class OpenWrtTimeSyncMixin(TimeSyncMixin):
"""The servers and the zone in UCI's ``system`` config; see the module."""
time_sync_writes: ClassVar[frozenset[str]] = TIME_SYNC_VALUES
if TYPE_CHECKING: # pragma: no cover - the driver's
def run_command(
self,
command: str,
*,
privileged: bool = False,
timeout: float = 60,
stdin: bytes | None = None,
) -> CommandResult: ...
# -- reading ---------------------------------------------------------------------------------
def _ts_run_read(self, command: str) -> str:
result = self.run_command(command, timeout=30)
if result.exit_code != 0:
said = result.stderr.strip() or result.stdout.strip() or f"exit {result.exit_code}"
raise RuntimeError(f"{READ_COMMAND}: {said}")
return result.stdout
def _ts_read(self) -> SystemUci:
return parse_system_uci(self._ts_run_read(READ_COMMAND))
def get_time_sync(self) -> TimeSyncDict:
"""The router's time servers and zone, and what its DHCP leases bring, in one
round trip (:data:`READ_WITH_DHCP`).
``dhcp_ntp_servers`` is None where sysntpd would add a lease's servers but
netifd gave no answer.
:raises RuntimeError: when uci could not be read.
"""
uci, _, dump = self._ts_run_read(READ_WITH_DHCP).partition(f"{DHCP_MARK}\n")
reading = parse_system_uci(uci)
dhcp = lease_ntp_servers(dump, reading.dhcp_interfaces) if reading.dhcp_servers else []
return {
"ntp_servers": reading.servers,
"timezone": reading.zone,
"synchronized": None,
"daemon": None,
"dhcp_ntp_servers": dhcp,
}
def get_ntp_servers(self) -> dict[str, dict[str, Any]]:
"""The servers of :meth:`get_time_sync`, as NAPALM's ``{server: {}}``."""
return {server: {} for server in self._ts_read().servers}
def get_ntp_peers(self) -> dict[str, dict[str, Any]]:
"""BusyBox ntpd has no peers apart from its servers: the same list."""
return self.get_ntp_servers()
# -- writing ---------------------------------------------------------------------------------
def _ts_apply(self, commands: list[str], done: str) -> FixStepOutcomeDict:
result = self.run_command(" && ".join(commands), privileged=True, timeout=60)
if result.exit_code != 0:
said = result.stderr.strip() or result.stdout.strip() or f"exit {result.exit_code}"
return {"ok": False, "output": said[-300:]}
return {"ok": True, "output": done}
def _write_ntp_servers(self, servers: list[str]) -> FixStepOutcomeDict:
now = self._ts_read()
if [s.lower() for s in now.servers] == [s.lower() for s in servers]:
return {"ok": True, "output": "the router already has these servers"}
commands = []
if not now.has_ntp:
commands.append(_uci_command("uci", "set", "system.ntp=timeserver"))
if "server" in now.ntp:
commands.append(_uci_command("uci", "delete", "system.ntp.server"))
commands += [_uci_command("uci", "add_list", f"system.ntp.server={s}") for s in servers]
commands += [
"uci set system.ntp.enabled=1",
"uci commit system",
"/etc/init.d/sysntpd enable",
"/etc/init.d/sysntpd restart",
]
return self._ts_apply(commands, f"ntpd takes time from {', '.join(servers)}")
def _write_time_zone(self, zone: str) -> FixStepOutcomeDict:
rule = posix_tz(zone)
if rule is None: # the template lets only zones of the database through
return {"ok": False, "output": f"no POSIX rule for {zone}"}
now = self._ts_read()
if now.has_system and now.zonename == zone and same_time(now.rule, rule):
return {"ok": True, "output": f"the router is already in {zone}"}
commands = [
_uci_command("uci", "set", f"system.@system[0].zonename={_luci_name(zone)}"),
_uci_command("uci", "set", f"system.@system[0].timezone={rule}"),
"uci commit system",
"/etc/init.d/system reload",
"/etc/init.d/log restart",
]
return self._ts_apply(commands, f"time zone set to {zone} ({rule})")
__all__ = [
"DHCP_MARK",
"READ_WITH_DHCP",
"OpenWrtTimeSyncMixin",
"SystemUci",
"lease_ntp_servers",
"parse_system_uci",
"same_time",
"zone_for_rule",
]