fix(dhcp): release an address before another reservation takes it
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m25s
driver / test (3.13) (pull_request) Successful in 1m13s
driver / lowest (pull_request) Successful in 45s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 3m6s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m25s
driver / test (3.13) (pull_request) Successful in 1m13s
driver / lowest (pull_request) Successful in 45s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 3m6s
driver / publish (pull_request) Skipped
apply_dhcp_reservationset wrote adds first, then updates in the order of the desired set. Kea keeps addresses unique at each write, so when an address moved from one reservation to another, the reservation taking it was refused as a duplicate whenever it came first. The changes are now ordered (_dhcp_order): an update that moves a reservation off its address goes before the change that takes that address, along a chain from its free end, and adds come after every update. A swap or rotation of addresses has no such order; its changes are still tried, so a server that checks only at the commit takes them, and a refused one's [failed] line says it was a swap and with which reservations. Nothing is moved to a temporary address: the contract knows no free one, and a failed second write would leave it there. The diff is computed against one read of the reservations, shared with diff_dhcp_reservations. Refused rows still do not stop the others, the rest is committed and DhcpApplyError follows, as since 4.35.1. Closes #106
This commit is contained in:
1 parent
5258a43dc9
commit
b4cdcfd347
7 files changed
+347
-43
No files matched your search
@@ -0,0 +1,116 @@
|
||||
"""The order an applied reservation set is written in (#106).
|
||||
|
||||
A DHCP server such as Kea keeps every address unique at each write, not only at the
|
||||
commit: a reservation that takes an address another one still holds is refused as a
|
||||
duplicate. So the changes are ordered for an address to be released before it is taken:
|
||||
|
||||
* An update that moves a reservation off its address goes before the change that takes
|
||||
that address -- along a chain, from its free end.
|
||||
* Adds come after every update: they release nothing, and an update may free their
|
||||
address.
|
||||
|
||||
A *swap* -- two reservations trading addresses, or a longer rotation -- has no such
|
||||
order. Its changes are still tried, in the order of the desired set: a server that checks
|
||||
only at the commit takes them. A server that refuses them refuses each with its own
|
||||
message, and the progress line says it was a swap. Nothing is moved to a temporary
|
||||
address: the contract knows no free one (pools, leases and reservations nobody manages
|
||||
all hold addresses), and a second write that failed would leave the reservation on it.
|
||||
|
||||
Without I/O: a reconciler's part, see ``docs/STRUCTURE.md``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
from dataclasses import dataclass
|
||||
|
||||
from napalm_device_types.models import DhcpReservationDict, DhcpReservationDiffDict
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReservationChange:
|
||||
"""One write of an applied set, in the order it is made."""
|
||||
|
||||
reservation: DhcpReservationDict
|
||||
#: The live reservation's id for an update, None for an add.
|
||||
uuid: str | None
|
||||
#: The changed fields of an update (``"ip, hostname"``), empty for an add.
|
||||
fields: str = ""
|
||||
#: The other reservations' MACs of the swap this change is part of, if any.
|
||||
swap: tuple[str, ...] = ()
|
||||
|
||||
|
||||
def _address(ip: str | None) -> str:
|
||||
"""*ip* in one spelling, so ``192.0.2.010``-style differences cannot hide a match."""
|
||||
text = (ip or "").strip()
|
||||
try:
|
||||
return str(ipaddress.ip_address(text))
|
||||
except ValueError:
|
||||
return text.lower()
|
||||
|
||||
|
||||
def _waits_on(updates: list[tuple[str, str]]) -> list[int | None]:
|
||||
"""For each ``(old, new)`` address pair, the update whose release it waits for."""
|
||||
released = {old: i for i, (old, new) in enumerate(updates) if old and old != new}
|
||||
return [released.get(new) if new != old else None for old, new in updates]
|
||||
|
||||
|
||||
def _cycles(waits_on: list[int | None]) -> dict[int, list[int]]:
|
||||
"""Each update that is part of a cycle, with the cycle's members in order."""
|
||||
members: dict[int, list[int]] = {}
|
||||
for start in range(len(waits_on)):
|
||||
path: list[int] = []
|
||||
node: int | None = start
|
||||
while node is not None and node not in path and node not in members:
|
||||
path.append(node)
|
||||
node = waits_on[node]
|
||||
if node is not None and node in path:
|
||||
cycle = sorted(path[path.index(node) :])
|
||||
members.update(dict.fromkeys(cycle, cycle))
|
||||
return members
|
||||
|
||||
|
||||
def _update_order(waits_on: list[int | None], cycles: dict[int, list[int]]) -> list[int]:
|
||||
"""Every update after the one it waits on; stable otherwise. Where only cycles are
|
||||
left, the first of them goes as it is, and so on."""
|
||||
order: list[int] = []
|
||||
done: set[int] = set()
|
||||
pending = list(range(len(waits_on)))
|
||||
while pending:
|
||||
ready = [i for i in pending if waits_on[i] is None or waits_on[i] in done]
|
||||
if not ready:
|
||||
ready = [next(i for i in pending if i in cycles)]
|
||||
order += ready
|
||||
done.update(ready)
|
||||
pending = [i for i in pending if i not in done]
|
||||
return order
|
||||
|
||||
|
||||
def order_reservation_changes(
|
||||
diff: DhcpReservationDiffDict, live: list[DhcpReservationDict]
|
||||
) -> list[ReservationChange]:
|
||||
"""The changes of *diff* in the order they are written; see the module.
|
||||
|
||||
:param diff: What ``diff_dhcp_reservations`` found.
|
||||
:param live: The reservations it compared against, for the addresses updates release.
|
||||
"""
|
||||
held = {r.get("uuid"): _address(r.get("ip")) for r in live if r.get("uuid")}
|
||||
updates = diff["update"]
|
||||
waits_on = _waits_on(
|
||||
[(held.get(e["uuid"], ""), _address(e["reservation"].get("ip"))) for e in updates]
|
||||
)
|
||||
cycles = _cycles(waits_on)
|
||||
|
||||
def swap(i: int) -> tuple[str, ...]:
|
||||
return tuple(updates[j]["reservation"]["mac"] for j in cycles.get(i, []) if j != i)
|
||||
|
||||
ordered = [
|
||||
ReservationChange(
|
||||
updates[i]["reservation"],
|
||||
updates[i]["uuid"],
|
||||
", ".join(updates[i]["changed_fields"]),
|
||||
swap(i),
|
||||
)
|
||||
for i in _update_order(waits_on, cycles)
|
||||
]
|
||||
return ordered + [ReservationChange(r, None) for r in diff["add"]]
|
||||
+65
-40
@@ -26,6 +26,7 @@ import re
|
||||
from collections.abc import Iterator
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from napalm_device_types._dhcp_order import ReservationChange, order_reservation_changes
|
||||
from napalm_device_types._matching import check_desired, index_live
|
||||
from napalm_device_types.models import (
|
||||
DhcpHaDict,
|
||||
@@ -57,6 +58,12 @@ _DHCP_SUBNET_COMPARE_FIELDS = (
|
||||
|
||||
_HEX_ONLY = re.compile(r"[^0-9a-f]")
|
||||
|
||||
#: Added to a refused change that is part of an address swap (#106).
|
||||
_SWAP_NOTE = (
|
||||
"an address swap with {}: no order frees an address first; "
|
||||
"move one of them to a free address in a run of its own"
|
||||
)
|
||||
|
||||
|
||||
class DhcpApplyError(RuntimeError):
|
||||
"""Some reservations of an applied set were refused by the device.
|
||||
@@ -119,6 +126,43 @@ def _subnet_field_differs(field: str, live: DhcpSubnetDict, desired: DhcpSubnetD
|
||||
return any(live_options.get(option) != value for option, value in desired_options.items())
|
||||
|
||||
|
||||
def _reservation_key(reservation: DhcpReservationDict) -> str:
|
||||
return normalize_mac(reservation.get("mac"))
|
||||
|
||||
|
||||
def _diff_reservations(
|
||||
live: list[DhcpReservationDict], desired: list[DhcpReservationDict]
|
||||
) -> DhcpReservationDiffDict:
|
||||
"""``diff_dhcp_reservations`` against reservations already read."""
|
||||
live_by_mac, shared = index_live(live, _reservation_key)
|
||||
check_desired(desired, _reservation_key, shared, "MAC")
|
||||
|
||||
add: list[DhcpReservationDict] = []
|
||||
update: list[DhcpReservationUpdateDict] = []
|
||||
|
||||
for desired_reservation in desired:
|
||||
current = live_by_mac.get(_reservation_key(desired_reservation))
|
||||
if current is None:
|
||||
add.append(desired_reservation)
|
||||
continue
|
||||
|
||||
changed_fields = [
|
||||
field
|
||||
for field in _DHCP_RESERVATION_COMPARE_FIELDS
|
||||
if current.get(field) != desired_reservation.get(field)
|
||||
]
|
||||
if changed_fields:
|
||||
update.append(
|
||||
{
|
||||
"uuid": current["uuid"],
|
||||
"reservation": desired_reservation,
|
||||
"changed_fields": changed_fields,
|
||||
}
|
||||
)
|
||||
|
||||
return {"add": add, "update": update}
|
||||
|
||||
|
||||
class DhcpServerMixin:
|
||||
"""Mixin providing DHCP reservation and subnet read/diff/apply.
|
||||
|
||||
@@ -271,36 +315,7 @@ class DhcpServerMixin:
|
||||
:raises ValueError: When a desired reservation cannot name a single live one.
|
||||
"""
|
||||
|
||||
def key(reservation: DhcpReservationDict) -> str:
|
||||
return normalize_mac(reservation.get("mac"))
|
||||
|
||||
live_by_mac, shared = index_live(self.get_dhcp_reservations(), key)
|
||||
check_desired(desired, key, shared, "MAC")
|
||||
|
||||
add: list[DhcpReservationDict] = []
|
||||
update: list[DhcpReservationUpdateDict] = []
|
||||
|
||||
for desired_reservation in desired:
|
||||
live = live_by_mac.get(key(desired_reservation))
|
||||
if live is None:
|
||||
add.append(desired_reservation)
|
||||
continue
|
||||
|
||||
changed_fields = [
|
||||
field
|
||||
for field in _DHCP_RESERVATION_COMPARE_FIELDS
|
||||
if live.get(field) != desired_reservation.get(field)
|
||||
]
|
||||
if changed_fields:
|
||||
update.append(
|
||||
{
|
||||
"uuid": live["uuid"],
|
||||
"reservation": desired_reservation,
|
||||
"changed_fields": changed_fields,
|
||||
}
|
||||
)
|
||||
|
||||
return {"add": add, "update": update}
|
||||
return _diff_reservations(self.get_dhcp_reservations(), desired)
|
||||
|
||||
def apply_dhcp_reservationset(self, desired: list[DhcpReservationDict]) -> Iterator[str]:
|
||||
"""
|
||||
@@ -311,6 +326,9 @@ class DhcpServerMixin:
|
||||
committing reloads the DHCP daemon and drops in-flight requests, which
|
||||
is too high a price for a no-op run.
|
||||
|
||||
The changes are written so an address is released before it is taken; a
|
||||
swap of addresses has no such order and is reported as one (``_dhcp_order``).
|
||||
|
||||
A change the device refuses (say, an address in a subnet its DHCP server
|
||||
does not manage) does not stop the others: it gets a ``[failed]`` line,
|
||||
the rest are written and committed, and ``DhcpApplyError`` is raised at
|
||||
@@ -322,7 +340,8 @@ class DhcpServerMixin:
|
||||
plus a final commit line.
|
||||
:raises DhcpApplyError: after the commit, when a change was refused.
|
||||
"""
|
||||
diff = self.diff_dhcp_reservations(desired)
|
||||
live = self.get_dhcp_reservations()
|
||||
diff = _diff_reservations(live, desired)
|
||||
attempted = len(diff["add"]) + len(diff["update"])
|
||||
if not attempted:
|
||||
yield "[commit] no changes"
|
||||
@@ -330,17 +349,13 @@ class DhcpServerMixin:
|
||||
|
||||
added = updated = refused = 0
|
||||
|
||||
for reservation in diff["add"]:
|
||||
line, ok = self._write_reservation(reservation, None, "[add]")
|
||||
added, refused = added + ok, refused + (not ok)
|
||||
for change in order_reservation_changes(diff, live):
|
||||
line, ok = self._write_change(change)
|
||||
refused += not ok
|
||||
added += ok and change.uuid is None
|
||||
updated += ok and change.uuid is not None
|
||||
yield line
|
||||
|
||||
for entry in diff["update"]:
|
||||
fields = ", ".join(entry["changed_fields"])
|
||||
line, ok = self._write_reservation(entry["reservation"], entry["uuid"], "[update]")
|
||||
updated, refused = updated + ok, refused + (not ok)
|
||||
yield f"{line} ({fields})" if ok else line
|
||||
|
||||
# Committed even when every write was refused: a run that stopped before its
|
||||
# commit left saved writes behind, which the device reports as configured, so
|
||||
# no later diff would ever commit them.
|
||||
@@ -350,6 +365,16 @@ class DhcpServerMixin:
|
||||
if refused:
|
||||
raise DhcpApplyError(refused, attempted)
|
||||
|
||||
def _write_change(self, change: ReservationChange) -> tuple[str, bool]:
|
||||
"""Write one change of an ordered set; its progress line, and whether it went in."""
|
||||
tag = "[add]" if change.uuid is None else "[update]"
|
||||
line, ok = self._write_reservation(change.reservation, change.uuid, tag)
|
||||
if ok and change.fields:
|
||||
line = f"{line} ({change.fields})"
|
||||
elif not ok and change.swap:
|
||||
line = f"{line} -- {_SWAP_NOTE.format(', '.join(change.swap))}"
|
||||
return line, ok
|
||||
|
||||
def _write_reservation(
|
||||
self, reservation: DhcpReservationDict, uuid: str | None, tag: str
|
||||
) -> tuple[str, bool]:
|
||||
|
||||
Reference in new issue
Block a user