fix(lldp): read neighbours without starting or reconfiguring lldpd
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 53s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m19s
driver / publish (pull_request) Skipped

get_lldp_neighbors, which a poll calls every time, enabled and started a
stopped lldpd and rewrote lldpd's interface list with uci, restarting
the daemon. A getter must not change the device. Both LLDP getters now
only run lldpctl; installing and starting lldpd are ensure_package and
manage_service.

Closes #8
This commit is contained in:
Christian Manivong committed 2026-11-01 21:48:31 +01:00
1 parent 5e71f0b471
commit 783a4ee0cb
4 files changed
+39 -41

No files matched your search

+5
View File
@@ -12,6 +12,11 @@ recorded here; their history is the git log.
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
3.12 venv no longer has.
- `get_lldp_neighbors()` and `get_lldp_neighbors_detail()` only read. They no longer
enable and start a stopped lldpd, and no longer rewrite lldpd's interface list
and restart it, which could happen on every poll. A device whose lldpd is stopped
now reports no neighbours until lldpd is started, for example with
`manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8).
### Fixed
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
+2 -2
View File
@@ -62,8 +62,8 @@ device.close()
| `get_mac_address_table` | ✅ | Uses `bridge fdb show` |
| `get_config` | ✅ | Uses `uci export` |
| `get_environment` | ✅ | CPU from `/proc/stat`, memory from `/proc/meminfo` |
| `get_lldp_neighbors` | ✅ | Requires `lldpd` package installed on device |
| `get_lldp_neighbors_detail` | ✅ | Requires `lldpd` package installed on device |
| `get_lldp_neighbors` | ✅ | Requires `lldpd` installed and running; reads only, never starts or configures it |
| `get_lldp_neighbors_detail` | ✅ | Requires `lldpd` installed and running; reads only, never starts or configures it |
| `get_listening_sockets` | ✅ | busybox `netstat -lntup`, procd service from the process's cgroup (napalm-device-types `ListeningSocketsMixin`); over an SSH exec channel |
## Configuration management
+4 -39
View File
@@ -46,7 +46,7 @@ class OpenWrtLLDPMixin:
"""Mixin providing LLDP NAPALM getters."""
def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]:
"""Return LLDP neighbors (requires ``lldpd`` to be installed on the device).
"""Return LLDP neighbors (requires ``lldpd`` installed and running on the device).
Uses ``lldpctl -f keyvalue`` output::
@@ -60,48 +60,13 @@ class OpenWrtLLDPMixin:
)
return neighbors
def _lldpd_fix_interface(self) -> None:
"""Ensure lldpd UCI config uses the actual management interface.
Detects the interface carrying the default route (e.g. br-lan.10 on a
VLAN trunk) and replaces any stale/wrong interface entry in the lldpd
UCI config. Idempotent — only writes if the config needs updating.
"""
mgmt_iface = self._send_command(
"ip route show default 2>/dev/null | head -1 | grep -oE 'dev [^ ]+' | awk '{print $2}'"
).strip()
if not mgmt_iface:
mgmt_iface = "br-lan"
# LLDP is a L2 protocol — it must run on the bridge, not a VLAN subinterface.
# Strip .VID suffix so br-ap.10 → br-ap.
if "." in mgmt_iface:
mgmt_iface = mgmt_iface.rsplit(".", 1)[0]
current = self._send_command("uci get lldpd.config.interface 2>/dev/null").strip()
if mgmt_iface in current:
return # already correct
# Reset the interface list to just the management interface
self._send_command(
"uci delete lldpd.config.interface 2>/dev/null; "
f"uci add_list lldpd.config.interface='{mgmt_iface}' 2>/dev/null; "
"uci commit lldpd 2>/dev/null; "
"/etc/init.d/lldpd restart 2>/dev/null"
)
def _get_lldp_table(self) -> list[dict[str, Any]]:
"""Parse ``lldpctl -f keyvalue`` into a list of row dicts.
Ensures ``lldpd`` is enabled and running before querying; if it was
not already running the daemon needs time to discover neighbors so
the first call after a fresh install will return an empty list.
Reads only: lldpd is neither started nor reconfigured here. Installing,
starting and configuring it are the caller's (``ensure_package``,
``manage_service``).
"""
# Ensure lldpd is running on the correct management interface
self._send_command(
"pgrep lldpd >/dev/null 2>&1 || "
"(/etc/init.d/lldpd enable 2>/dev/null; /etc/init.d/lldpd start 2>/dev/null)"
)
self._lldpd_fix_interface()
output = self._send_command("lldpctl -f keyvalue")
rows: list[dict[str, Any]] = []
+28
View File
@@ -365,6 +365,34 @@ lldp.eth0.port.ifname=ether1
"""
class TestLldpGettersOnlyRead:
"""Reading the neighbours neither starts lldpd nor rewrites its configuration (#8)."""
def _answer(self, driver) -> list:
sent: list = []
def send(cmd, **kw):
sent.append(cmd)
# A default route on a VLAN of another bridge: the old code rewrote
# lldpd's interface list for it and restarted the daemon.
if "ip route" in cmd:
return "br-ap"
if "uci get lldpd" in cmd:
return "br-lan"
return LLDPCTL_KV
driver._send_command = send
return sent
@pytest.mark.parametrize("getter", ["get_lldp_neighbors", "get_lldp_neighbors_detail"])
def test_only_lldpctl_is_asked(self, driver, getter):
sent = self._answer(driver)
getattr(driver, getter)()
assert sent == ["lldpctl -f keyvalue"]
class TestLldpSeveralNeighboursOnOnePort:
"""Each neighbour on a port is its own entry; none is lost (#9)."""