diff --git a/CHANGELOG.md b/CHANGELOG.md index e360ff5..9cc7050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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' diff --git a/README.md b/README.md index bbcaf7b..686a4d3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/napalm_openwrt/lldp_mixin.py b/napalm_openwrt/lldp_mixin.py index 439d7fc..86da3f1 100644 --- a/napalm_openwrt/lldp_mixin.py +++ b/napalm_openwrt/lldp_mixin.py @@ -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]] = [] diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 5300de7..8b694c6 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -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)."""