From 6bd817fae19ee6b0ceb69cbabdac7d2d61d0bb5c Mon Sep 17 00:00:00 2001 From: rchac Date: Mon, 23 Mar 2026 18:27:01 +0000 Subject: [PATCH] changes --- src/bakery_integration_test.py | 1378 +++++++++++++++++- src/rust/lqos_bakery/src/commands.rs | 9 +- src/rust/lqos_bakery/src/diff/circuits.rs | 1 + src/rust/lqos_bakery/src/lib.rs | 1223 ++++++++++++---- src/rust/lqos_bakery/src/utils.rs | 57 +- src/rust/lqos_config/src/network_json.rs | 6 +- src/rust/lqos_config/src/planner.rs | 9 +- src/rust/lqos_python/src/lib.rs | 1 + src/rust/lqos_stormguard/src/site_state.rs | 2 +- src/rust/lqosd/src/main.rs | 8 - src/rust/lqosd/src/throughput_tracker/mod.rs | 2 + src/rust/lqosd/src/treeguard/actor.rs | 15 +- 12 files changed, 2327 insertions(+), 384 deletions(-) diff --git a/src/bakery_integration_test.py b/src/bakery_integration_test.py index 170e132a..4e731dbb 100644 --- a/src/bakery_integration_test.py +++ b/src/bakery_integration_test.py @@ -11,13 +11,14 @@ Important maintenance window. It is NOT intended for production service units under systemd. Manual steps (run in two terminals) -1) Start lqosd manually as root with logs to a file: - sudo RUST_LOG=info lqosd 2>&1 | tee /tmp/lqosd.log +1) Either: + - Start lqosd manually as root with logs to a file: + sudo RUST_LOG=info lqosd 2>&1 | tee /tmp/lqosd.log + and run the harness with `--log-file /tmp/lqosd.log`, or + - Run against the systemd-managed daemon and let the harness read `journalctl -u lqosd`. - Ensure /etc/lqos.conf interfaces and bandwidths are set correctly for this host. - - Stop systemd units first if they are enabled (optional): - sudo systemctl stop lqosd lqos_scheduler lqos_api 2) In another terminal from this directory (LibreQoS/src), run the tests: - python3 bakery_integration_test.py --log-file /tmp/lqosd.log + python3 bakery_integration_test.py - The default run is the quicker tiered suite. - Use --full-suite for the whole harness, or --flat-only/--treeguard-only/etc. for a focused run. @@ -33,18 +34,25 @@ Notes - Flat networks do not have explicit sites, so site add/remove checks are skipped there. - Newer Bakery builds log incremental completion as `Bakery mapped circuit decision (...)` rather than only the older `Bakery changes: ...` line. This harness accepts both. +- In `cpu_aware` TreeGuard mode, the live TreeGuard suite may legitimately report a + skip if no CPU pressure is induced on the box during the test window. Usage examples - Quick default run (tiered only): + python3 bakery_integration_test.py +- Use a tee'd manual log file: python3 bakery_integration_test.py --log-file /tmp/lqosd.log - Full suite: - python3 bakery_integration_test.py --log-file /tmp/lqosd.log --full-suite + python3 bakery_integration_test.py --full-suite - Tiered only: - python3 bakery_integration_test.py --log-file /tmp/lqosd.log --tiered-only + python3 bakery_integration_test.py --tiered-only - TreeGuard runtime suite only: - python3 bakery_integration_test.py --log-file /tmp/lqosd.log --treeguard-only --treeguard-timeout 90 + python3 bakery_integration_test.py --treeguard-only --treeguard-timeout 90 +- Fault-injection reload escalation suite only: + python3 bakery_integration_test.py --fault-reload-only + This destructive safety-path test is opt-in and is not included in --full-suite. - Flat only: - python3 bakery_integration_test.py --log-file /tmp/lqosd.log --flat-only + python3 bakery_integration_test.py --flat-only Exit code - 0 on success, 1 on assertion failure or precondition failure. @@ -62,7 +70,7 @@ import shutil import sys import time from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple +from typing import Dict, List, Optional, Set, Tuple, Union import subprocess @@ -133,6 +141,7 @@ MAPPED_DECISION_PAT = re.compile( r"Bakery mapped circuit decision \(([^)]+)\): requested=(\d+), allowed=(\d+), dropped=(\d+)," ) NO_CHANGES_PAT = re.compile(r"No changes detected in batch, skipping processing\.") +TEST_FAULT_ONCE_PATH = "/tmp/lqos_bakery_fail_purpose_once.txt" @dataclass @@ -144,28 +153,32 @@ class LogResult: raw_lines: List[str] +SnapshotToken = Union[int, float] + + class LogReader: def __init__(self, path: str): self.path = path - def snapshot(self) -> int: + def snapshot(self) -> SnapshotToken: try: return os.path.getsize(self.path) except Exception: return 0 - def read_since(self, offset: int) -> List[str]: + def read_since(self, offset: SnapshotToken) -> Tuple[SnapshotToken, List[str]]: lines: List[str] = [] try: with open(self.path, "r", errors="replace") as f: - f.seek(offset) + f.seek(int(offset)) for line in f: lines.append(line.rstrip("\n")) except Exception: pass - return lines + next_offset = int(offset) + sum(len(l) + 1 for l in lines) + return next_offset, lines - def wait_for_events(self, offset: int, timeout_s: float = 20.0) -> LogResult: + def wait_for_events(self, offset: SnapshotToken, timeout_s: float = 20.0) -> LogResult: deadline = time.time() + timeout_s print(f"Waiting for Bakery events (timeout {timeout_s:.1f}s)...") collected: List[str] = [] @@ -176,12 +189,9 @@ class LogReader: printed_sleep_note = False while time.time() < deadline: - new_lines = self.read_since(offset) + offset, new_lines = self.read_since(offset) if new_lines: collected.extend(new_lines) - # Seek to last byte each time - offset += sum(len(l) + 1 for l in new_lines) - # Parse new chunk for ln in new_lines: if FULL_RELOAD_PAT.search(ln) or FULL_RELOAD_SUMMARY_PAT.search(ln): @@ -225,6 +235,109 @@ class LogReader: ) +class JournalctlLogReader: + def __init__(self, unit: str = "lqosd"): + self.unit = unit + + def snapshot(self) -> SnapshotToken: + return time.time() + + def _since_arg(self, token: SnapshotToken) -> str: + return f"@{float(token):.6f}" + + def read_since(self, token: SnapshotToken) -> Tuple[SnapshotToken, List[str]]: + start = float(token) + proc = subprocess.run( + [ + "journalctl", + "-u", + self.unit, + "--since", + self._since_arg(start), + "--no-pager", + "-o", + "short-unix", + ], + capture_output=True, + text=True, + check=False, + ) + if proc.returncode != 0: + return start, [] + + lines: List[str] = [] + next_token = start + for raw_line in proc.stdout.splitlines(): + line = raw_line.rstrip("\n") + if not line: + continue + lines.append(line) + timestamp_token = line.split(" ", 1)[0].strip() + try: + next_token = max(next_token, float(timestamp_token) + 0.000001) + except Exception: + next_token = max(next_token, time.time()) + + return next_token, lines + + def wait_for_events(self, offset: SnapshotToken, timeout_s: float = 20.0) -> LogResult: + deadline = time.time() + timeout_s + print(f"Waiting for Bakery events from journalctl (timeout {timeout_s:.1f}s)...") + collected: List[str] = [] + full_reload = False + mq_init = False + changes: Optional[Dict[str, int]] = None + incremental_event: Optional[str] = None + printed_sleep_note = False + + while time.time() < deadline: + offset, new_lines = self.read_since(offset) + if new_lines: + collected.extend(new_lines) + for ln in new_lines: + if FULL_RELOAD_PAT.search(ln) or FULL_RELOAD_SUMMARY_PAT.search(ln): + full_reload = True + if MQ_INIT_PAT.search(ln): + mq_init = True + m = CHANGES_PAT.search(ln) + if m: + changes = { + "sites_speed": int(m.group(1)), + "circuits_added": int(m.group(2)), + "removed": int(m.group(3)), + "speed": int(m.group(4)), + "ip": int(m.group(5)), + } + mapped = MAPPED_DECISION_PAT.search(ln) + if mapped: + incremental_event = mapped.group(1) + changes = { + "requested": int(mapped.group(2)), + "allowed": int(mapped.group(3)), + "dropped": int(mapped.group(4)), + } + if "full reload" in incremental_event.lower(): + full_reload = True + if changes or full_reload or any(NO_CHANGES_PAT.search(x) for x in new_lines): + break + + if not printed_sleep_note: + print("No new logs yet; sleeping 0.4s...") + printed_sleep_note = True + time.sleep(0.4) + + return LogResult( + full_reload=full_reload, + mq_init=mq_init, + changes=changes, + incremental_event=incremental_event, + raw_lines=collected, + ) + + +AnyLogReader = Union[LogReader, JournalctlLogReader] + + # ----------------------- # Fixtures and mutators # ----------------------- @@ -700,6 +813,8 @@ def realistic_tiered_circuits_base() -> List[Dict[str, str | int | float]]: max_ul = max(1, max_dl - 1) ip_addr = f"100.64.{third}.{j + 1}" + if j == 0: + ip_addr = f"{ip_addr},100.64.{third}.{j + 101}" row: Dict[str, str | int | float] = { "Circuit ID": str(circuit_id), "Circuit Name": f"CIRCUIT_{circuit_id:04d}", @@ -760,7 +875,7 @@ def realistic_tiered_circuits_base() -> List[Dict[str, str | int | float]]: # ----------------------- -def run_refresh_and_wait(log: LogReader, timeout_s: float) -> LogResult: +def run_refresh_and_wait(log: AnyLogReader, timeout_s: float) -> LogResult: offset = log.snapshot() # Call the core refresh function; this does not require running as __main__ LibreQoS.refreshShapers() @@ -770,6 +885,27 @@ def run_refresh_and_wait(log: LogReader, timeout_s: float) -> LogResult: return log.wait_for_events(offset, timeout_s=timeout_s) +def settle_initial_bakery_logs(log: AnyLogReader, settle_s: float = 1.5) -> None: + offset = log.snapshot() + time.sleep(settle_s) + _ = log.wait_for_events(offset, timeout_s=0.5) + + +def clear_bakery_fault_once() -> None: + try: + os.remove(TEST_FAULT_ONCE_PATH) + except FileNotFoundError: + pass + + +def arm_bakery_fault_once(selector: str) -> None: + with open(TEST_FAULT_ONCE_PATH, "w") as f: + f.write(selector.strip()) + f.write("\n") + f.flush() + os.fsync(f.fileno()) + + # ----------------------- # TC and structure checks # ----------------------- @@ -1268,6 +1404,47 @@ def wait_for_runtime_virtualized_node( return False, last_msgs +def get_treeguard_cpu_mode(config_path: str = "/etc/lqos.conf") -> Optional[str]: + current_section: Optional[str] = None + mode_re = re.compile(r'^mode\s*=\s*"([^"]+)"') + try: + with open(config_path, "r", errors="replace") as f: + for raw in f: + line = raw.strip() + if not line or line.startswith("#"): + continue + if line.startswith("[") and line.endswith("]"): + current_section = line.strip("[]").strip() + continue + if current_section != "treeguard.cpu": + continue + m = mode_re.match(line) + if m: + return m.group(1).strip() + except Exception: + return None + return None + + +def wait_for_circuit_direction_ceil( + circuit_id: str, + timeout_s: float = 5.0, + poll_s: float = 0.5, +) -> Tuple[bool, List[str]]: + deadline = time.time() + timeout_s + last_msgs: List[str] = [] + while time.time() < deadline: + passed, msgs = check_circuit_direction_ceil(circuit_id) + last_msgs = msgs + if passed: + return True, msgs + time.sleep(poll_s) + last_msgs.append( + f"direction check: timed out waiting {timeout_s:.1f}s for circuit {circuit_id} direction mapping to settle" + ) + return False, last_msgs + + def check_ip_mappings_for_circuit(circuit_id: str) -> Tuple[bool, List[str]]: """Ensure that all IPv4s for this circuit are mapped to the correct down (and up if on-a-stick) tc handles.""" ok_tool, msg, entries = _list_ip_mappings() @@ -1338,6 +1515,144 @@ def check_ip_mapping_absent(ip: str) -> Tuple[bool, List[str]]: return True, [f"ipmap check: {ip} successfully absent"] +def _expected_ips_for_circuit(circuit: dict) -> List[str]: + ips: List[str] = [] + for dev in circuit.get("devices", []): + try: + for ip in dev.get("ipv4s", []): + ips.append(str(ip)) + except Exception: + pass + return ips + + +def check_exact_ip_mappings_for_circuit( + circuit_id: str, + forbidden_tcs: Optional[Set[str]] = None, +) -> Tuple[bool, List[str]]: + ok_tool, msg, entries = _list_ip_mappings() + msgs: List[str] = [] + if not ok_tool: + return True, [msg] # soft-skip + + qs = _load_queuing_structure() + if not isinstance(qs, dict): + return False, ["ipmap exact check: queuingStructure.json not found"] + net = qs.get("Network") + if not isinstance(net, dict): + return False, ["ipmap exact check: queuingStructure has no 'Network'"] + found = _walk_nodes_for_circuit(net, circuit_id) + if not found: + return False, [f"ipmap exact check: circuit {circuit_id} not found in structure"] + parent_name, circuit = found + parent_node = _find_parent_node(net, parent_name) + if not isinstance(parent_node, dict): + return False, [f"ipmap exact check: parent node '{parent_name}' not found"] + + down_tc, up_tc, _cpu = _expect_handles_for_circuit(parent_node, circuit) + allowed_tcs = {down_tc.lower()} + if up_tc: + allowed_tcs.add(up_tc.lower()) + forbidden = {tc.lower() for tc in (forbidden_tcs or set())} + + ips = _expected_ips_for_circuit(circuit) + if not ips: + return True, [f"ipmap exact check: circuit {circuit_id} has no IPv4s; skipping"] + + ok = True + for ip in ips: + ip_entries = [(cpu, tc.lower()) for (mapped_ip, cpu, tc) in entries if mapped_ip == ip] + if not ip_entries: + msgs.append(f"ipmap exact check: missing mapping for {ip}") + ok = False + continue + actual_tcs = {tc for (_cpu, tc) in ip_entries} + if not actual_tcs.issubset(allowed_tcs): + msgs.append( + f"ipmap exact check: {ip} has unexpected handles {sorted(actual_tcs - allowed_tcs)} (allowed={sorted(allowed_tcs)})" + ) + ok = False + if forbidden and actual_tcs.intersection(forbidden): + msgs.append( + f"ipmap exact check: {ip} still mapped to forbidden handles {sorted(actual_tcs.intersection(forbidden))}" + ) + ok = False + if ok: + msgs.append(f"ipmap exact check: {ip} only mapped to expected handles {sorted(actual_tcs)}") + + return ok, msgs + + +def check_exact_ip_mappings_for_circuits( + circuit_ids: List[str], + forbidden_by_circuit: Optional[Dict[str, Set[str]]] = None, +) -> Tuple[bool, List[str]]: + ok_tool, msg, entries = _list_ip_mappings() + msgs: List[str] = [] + if not ok_tool: + return True, [msg] # soft-skip + + qs = _load_queuing_structure() + if not isinstance(qs, dict): + return False, ["ipmap batch exact check: queuingStructure.json not found"] + net = qs.get("Network") + if not isinstance(net, dict): + return False, ["ipmap batch exact check: queuingStructure has no 'Network'"] + + ok = True + expected_ip_count = 0 + for circuit_id in circuit_ids: + found = _walk_nodes_for_circuit(net, circuit_id) + if not found: + msgs.append(f"ipmap batch exact check: circuit {circuit_id} not found in structure") + ok = False + continue + parent_name, circuit = found + parent_node = _find_parent_node(net, parent_name) + if not isinstance(parent_node, dict): + msgs.append(f"ipmap batch exact check: parent node '{parent_name}' not found for circuit {circuit_id}") + ok = False + continue + + down_tc, up_tc, _cpu = _expect_handles_for_circuit(parent_node, circuit) + allowed_tcs = {down_tc.lower()} + if up_tc: + allowed_tcs.add(up_tc.lower()) + forbidden = {tc.lower() for tc in (forbidden_by_circuit or {}).get(circuit_id, set())} + + ips = _expected_ips_for_circuit(circuit) + if not ips: + msgs.append(f"ipmap batch exact check: circuit {circuit_id} has no IPv4s; skipping") + continue + expected_ip_count += len(ips) + + for ip in ips: + ip_entries = [(cpu, tc.lower()) for (mapped_ip, cpu, tc) in entries if mapped_ip == ip] + if not ip_entries: + msgs.append(f"ipmap batch exact check: missing mapping for {ip} (circuit {circuit_id})") + ok = False + continue + actual_tcs = {tc for (_cpu, tc) in ip_entries} + if not actual_tcs.issubset(allowed_tcs): + msgs.append( + f"ipmap batch exact check: {ip} (circuit {circuit_id}) has unexpected handles {sorted(actual_tcs - allowed_tcs)} (allowed={sorted(allowed_tcs)})" + ) + ok = False + if forbidden and actual_tcs.intersection(forbidden): + msgs.append( + f"ipmap batch exact check: {ip} (circuit {circuit_id}) still mapped to forbidden handles {sorted(actual_tcs.intersection(forbidden))}" + ) + ok = False + if actual_tcs.issubset(allowed_tcs) and not (forbidden and actual_tcs.intersection(forbidden)): + msgs.append(f"ipmap batch exact check: {ip} (circuit {circuit_id}) only mapped to expected handles {sorted(actual_tcs)}") + + if ok: + msgs.append( + f"ipmap batch exact check: verified {expected_ip_count} expected IPv4 mappings across {len(circuit_ids)} circuits" + ) + return ok, msgs + + def check_generated_pn_tc_bandwidths() -> Tuple[bool, List[str]]: msgs: List[str] = [] qs = _load_queuing_structure() @@ -1476,6 +1791,175 @@ def _class_has_parent(iface: str, maj: int, mnr: int, pmaj: int, pmnr: int) -> T return (pmj == pmaj and pmn == pmnr), line +def _tc_class_occurrences(iface: str, maj: int, mnr: int) -> Tuple[int, List[str]]: + rc, out, err = _tc(["class", "show", "dev", iface]) + if rc != 0: + return 0, [f"tc class occurrences: 'tc class show dev {iface}' failed: {err.strip()}"] + + matches: List[str] = [] + cls_pat = re.compile(r"^class\s+htb\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + for found in cls_pat.finditer(out): + mj = _tc_id_token_to_int(found.group(1)) + mn = _tc_id_token_to_int(found.group(2)) + if mj == maj and mn == mnr: + matches.append(found.group(0)) + return len(matches), matches + + +def _tc_qdisc_occurrences_for_parent(iface: str, maj: int, mnr: int) -> Tuple[int, List[str]]: + rc, out, err = _qdisc_show(iface) + if rc != 0: + return 0, [f"tc qdisc occurrences: 'tc qdisc show dev {iface}' failed: {err.strip()}"] + + matches: List[str] = [] + pat = re.compile(r"^qdisc\s+\S+\s+\S+:\s+parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + for found in pat.finditer(out): + mj = _tc_id_token_to_int(found.group(1)) + mn = _tc_id_token_to_int(found.group(2)) + if mj == maj and mn == mnr: + matches.append(found.group(0)) + return len(matches), matches + + +def _handle_to_parts(handle: Optional[str]) -> Optional[Tuple[int, int]]: + if not handle or ":" not in handle: + return None + left, right = handle.split(":", 1) + major = _tc_id_token_to_int(left) + minor = _tc_id_token_to_int(right) + if major is None or minor is None: + return None + return major, minor + + +def _current_circuit_handle_strings(circuit_id: str) -> Tuple[Optional[str], Optional[str], List[str]]: + qs = _load_queuing_structure() + if not isinstance(qs, dict): + return None, None, ["cleanup check: queuingStructure.json not found"] + net = qs.get("Network") + if not isinstance(net, dict): + return None, None, ["cleanup check: queuingStructure has no 'Network'"] + found = _walk_nodes_for_circuit(net, circuit_id) + if not found: + return None, None, [f"cleanup check: circuit {circuit_id} not found in structure"] + _parent_name, circuit = found + down_tc = str(circuit.get("classid", "")).replace("0x", "").lower() + up_tc = str(circuit.get("up_classid", "")).replace("0x", "").lower() + return down_tc or None, up_tc or None, [] + + +def check_circuit_transition_cleanup( + circuit_id: str, + *, + old_down_tc: Optional[str], + old_up_tc: Optional[str], +) -> Tuple[bool, List[str]]: + msgs: List[str] = [] + ok = True + + new_down_tc, new_up_tc, handle_msgs = _current_circuit_handle_strings(circuit_id) + msgs.extend(handle_msgs) + if handle_msgs: + return False, msgs + + if not interface_a or not callable(interface_a): + return False, ["cleanup check: interface_a() binding unavailable"] + try: + ifa = interface_a() + except Exception as e: + return False, [f"cleanup check: failed to get interface_a(): {e}"] + ifb = None + if interface_b and callable(interface_b): + try: + ifb = interface_b() + except Exception: + ifb = None + + if new_down_tc: + down_parts = _handle_to_parts(new_down_tc) + if down_parts is None: + msgs.append(f"cleanup check: could not parse current down handle {new_down_tc} for circuit {circuit_id}") + ok = False + else: + count, lines = _tc_class_occurrences(ifa, *down_parts) + if count != 1: + msgs.append( + f"cleanup check: expected exactly one current down class {new_down_tc} for circuit {circuit_id} on {ifa}, found {count}" + ) + msgs.extend(lines) + ok = False + else: + msgs.append(f"cleanup check: current down class {new_down_tc} present exactly once on {ifa}") + if new_up_tc and ifb: + up_parts = _handle_to_parts(new_up_tc) + if up_parts is None: + msgs.append(f"cleanup check: could not parse current up handle {new_up_tc} for circuit {circuit_id}") + ok = False + else: + count, lines = _tc_class_occurrences(ifb, *up_parts) + if count != 1: + msgs.append( + f"cleanup check: expected exactly one current up class {new_up_tc} for circuit {circuit_id} on {ifb}, found {count}" + ) + msgs.extend(lines) + ok = False + else: + msgs.append(f"cleanup check: current up class {new_up_tc} present exactly once on {ifb}") + + if old_down_tc and old_down_tc.lower() != (new_down_tc or "").lower(): + old_down_parts = _handle_to_parts(old_down_tc) + if old_down_parts is not None: + count, lines = _tc_class_occurrences(ifa, *old_down_parts) + if count != 0: + msgs.append( + f"cleanup check: stale down class {old_down_tc} still present for circuit {circuit_id} on {ifa}" + ) + msgs.extend(lines) + ok = False + else: + msgs.append(f"cleanup check: stale down class {old_down_tc} absent on {ifa}") + + qcount, qlines = _tc_qdisc_occurrences_for_parent(ifa, *old_down_parts) + if qcount != 0: + msgs.append( + f"cleanup check: stale down qdisc parent {old_down_tc} still present for circuit {circuit_id} on {ifa}" + ) + msgs.extend(qlines) + ok = False + else: + msgs.append(f"cleanup check: stale down qdisc parent {old_down_tc} absent on {ifa}") + + if old_up_tc and ifb and old_up_tc.lower() != (new_up_tc or "").lower(): + old_up_parts = _handle_to_parts(old_up_tc) + if old_up_parts is not None: + count, lines = _tc_class_occurrences(ifb, *old_up_parts) + if count != 0: + msgs.append( + f"cleanup check: stale up class {old_up_tc} still present for circuit {circuit_id} on {ifb}" + ) + msgs.extend(lines) + ok = False + else: + msgs.append(f"cleanup check: stale up class {old_up_tc} absent on {ifb}") + + qcount, qlines = _tc_qdisc_occurrences_for_parent(ifb, *old_up_parts) + if qcount != 0: + msgs.append( + f"cleanup check: stale up qdisc parent {old_up_tc} still present for circuit {circuit_id} on {ifb}" + ) + msgs.extend(qlines) + ok = False + else: + msgs.append(f"cleanup check: stale up qdisc parent {old_up_tc} absent on {ifb}") + + forbidden = {tc for tc in [old_down_tc, old_up_tc] if tc} + passed_ip_exact, ip_msgs = check_exact_ip_mappings_for_circuit(circuit_id, forbidden_tcs=forbidden) + msgs.extend(ip_msgs) + ok &= passed_ip_exact + + return ok, msgs + + def check_orphan_tc_attachment(orphan_id: str = "99901") -> Tuple[bool, List[str]]: msgs: List[str] = [] qs = _load_queuing_structure() @@ -1729,6 +2213,70 @@ def check_circuit_direction_ceil(circuit_id: str) -> Tuple[bool, List[str]]: return ok, msgs +def check_circuit_qdisc_kind( + circuit_id: str, + expected_down_kind: Optional[str] = None, + expected_up_kind: Optional[str] = None, +) -> Tuple[bool, List[str]]: + msgs: List[str] = [] + qs = _load_queuing_structure() + if not isinstance(qs, dict): + return False, ["qdisc kind check: queuingStructure.json not found"] + net = qs.get("Network") + if not isinstance(net, dict): + return False, ["qdisc kind check: queuingStructure has no 'Network'"] + found = _walk_nodes_for_circuit(net, circuit_id) + if not found: + return False, [f"qdisc kind check: circuit {circuit_id} not found in structure"] + _parent_name, circuit = found + + maj = _hex_to_int(str(circuit.get("classMajor"))) + mnr = _hex_to_int(str(circuit.get("classMinor"))) + upm = _hex_to_int(str(circuit.get("up_classMajor"))) + if maj is None or mnr is None: + return False, [f"qdisc kind check: circuit {circuit_id} missing downlink class IDs"] + + def _check_iface(iface: str, parent_major: int, parent_minor: int, expected_kind: Optional[str], label: str) -> Tuple[bool, str]: + rc, out, err = _qdisc_show(iface) + if rc != 0: + return False, f"qdisc kind check: 'tc qdisc show dev {iface}' failed: {err.strip()}" + + pat = re.compile(r"^qdisc\s+(\S+)\s+\S+:\s+parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + for match in pat.finditer(out): + kind = match.group(1).lower() + mj = _tc_id_token_to_int(match.group(2)) + mn = _tc_id_token_to_int(match.group(3)) + if mj == parent_major and mn == parent_minor: + if expected_kind is not None and kind != expected_kind.lower(): + return False, f"qdisc kind check: {label} {iface} expected {expected_kind}, found {kind} for circuit {circuit_id} | {match.group(0)}" + return True, f"qdisc kind check: {label} {iface} found {kind} for circuit {circuit_id} | {match.group(0)}" + + return False, f"qdisc kind check: {label} {iface} no qdisc found for parent {parent_major}:{parent_minor} (circuit {circuit_id})" + + ok = True + if expected_down_kind is not None: + if not interface_a or not callable(interface_a): + return False, ["qdisc kind check: interface_a() binding unavailable"] + try: + ifa = interface_a() + except Exception as e: + return False, [f"qdisc kind check: failed to get interface_a(): {e}"] + passed, message = _check_iface(ifa, maj, mnr, expected_down_kind, "down") + msgs.append(message) + ok &= passed + + if expected_up_kind is not None and interface_b and callable(interface_b) and upm is not None: + try: + ifb = interface_b() + except Exception as e: + return False, [f"qdisc kind check: failed to get interface_b(): {e}"] + passed, message = _check_iface(ifb, upm, mnr, expected_up_kind, "up") + msgs.append(message) + ok &= passed + + return ok, msgs + + def _walk_nodes_for_circuit(node_map: Dict[str, dict], circuit_id: str) -> Optional[Tuple[str, dict]]: for name, node in node_map.items(): if not isinstance(node, dict): @@ -1835,6 +2383,188 @@ def assert_full_reload(tag: str, res: LogResult, step_started_at: Optional[float return True, f"{tag}: OK (full reload)" +def _journal_since_lines(since_ts: float) -> List[str]: + proc = subprocess.run( + [ + "journalctl", + "-u", + "lqosd", + "--since", + f"@{since_ts:.6f}", + "--no-pager", + "-o", + "short-unix", + ], + capture_output=True, + text=True, + check=False, + ) + if proc.returncode != 0: + return [] + return [line.rstrip("\n") for line in proc.stdout.splitlines() if line.strip()] + + +def check_no_hidden_incremental_failures(step_started_at: float) -> Tuple[bool, List[str]]: + lines = _journal_since_lines(step_started_at) + patterns = [ + re.compile(r"reload required", re.IGNORECASE), + re.compile(r"\bRTNETLINK\b", re.IGNORECASE), + re.compile(r"\bDirty\b", re.IGNORECASE), + re.compile(r"Command error for \(", re.IGNORECASE), + re.compile(r"Bakery wrote numbered command failure", re.IGNORECASE), + re.compile(r"full reload is now required", re.IGNORECASE), + ] + + matches = [line for line in lines if any(p.search(line) for p in patterns)] + + last_error_path = "/tmp/lqos_bakery_last_error.txt" + if os.path.exists(last_error_path): + try: + if os.path.getmtime(last_error_path) >= step_started_at: + matches.append(f"last error file updated: {last_error_path}") + except Exception: + pass + + if matches: + return False, [ + "incremental health: unexpected error/desync indicators detected after step", + *matches[-20:], + ] + + return True, ["incremental health: no hidden Bakery desync/error indicators detected after step"] + + +def check_fault_injection_observed(step_started_at: float) -> Tuple[bool, List[str]]: + lines = _journal_since_lines(step_started_at) + fault_patterns = [ + re.compile(r"synthetic Bakery test fault", re.IGNORECASE), + re.compile(r"Bakery test fault injected", re.IGNORECASE), + ] + + fault_matches = [line for line in lines if any(p.search(line) for p in fault_patterns)] + msgs: List[str] = [] + + if os.path.exists(TEST_FAULT_ONCE_PATH): + msgs.append(f"fault injection file still present and was not consumed: {TEST_FAULT_ONCE_PATH}") + return False, msgs + + if not fault_matches: + return False, [ + "fault reload check: synthetic Bakery fault was not observed in recent logs", + *lines[-20:], + ] + + msgs.append("fault reload check: Bakery observed and consumed the injected synthetic fault") + msgs.extend(fault_matches[-5:]) + return True, msgs + + +def check_reload_required_since(step_started_at: float) -> Tuple[bool, List[str]]: + lines = _journal_since_lines(step_started_at) + reload_patterns = [ + re.compile(r"reload required", re.IGNORECASE), + re.compile(r"full reload is now required", re.IGNORECASE), + re.compile(r"full reload required before further incremental topology mutation", re.IGNORECASE), + ] + + reload_matches = [line for line in lines if any(p.search(line) for p in reload_patterns)] + if not reload_matches: + return False, [ + "fault reload check: Bakery did not report reload-required state after injected fault", + *lines[-20:], + ] + + msgs: List[str] = [] + msgs.append("fault reload check: Bakery entered reload-required state after the injected fault") + msgs.extend(reload_matches[-5:]) + return True, msgs + + +def collect_failure_diagnostics( + tag: str, + *, + step_started_at: Optional[float] = None, + circuit_ids: Optional[List[str]] = None, + node_names: Optional[List[str]] = None, +) -> List[str]: + circuit_ids = circuit_ids or [] + node_names = node_names or [] + msgs = [f"diagnostics: begin for {tag}"] + + if step_started_at is not None: + journal_lines = _journal_since_lines(step_started_at) + msgs.append("diagnostics: recent lqosd journal") + msgs.extend(journal_lines[-20:] if journal_lines else ["diagnostics: no recent journal lines found"]) + + planner = None + try: + with open("planner_state.json", "r") as f: + planner = json.load(f) + except Exception as e: + msgs.append(f"diagnostics: failed to read planner_state.json: {e}") + + qs = _load_queuing_structure() + net = qs.get("Network") if isinstance(qs, dict) else None + + if isinstance(planner, dict): + for circuit_id in circuit_ids: + entry = planner.get("circuits", {}).get(str(circuit_id)) + msgs.append(f"diagnostics: planner circuit {circuit_id} -> {json.dumps(entry, sort_keys=True)}") + + if isinstance(net, dict): + for circuit_id in circuit_ids: + found = _walk_nodes_for_circuit(net, circuit_id) + if found: + parent_name, circuit = found + msgs.append( + f"diagnostics: structure circuit {circuit_id} under {parent_name} -> {json.dumps(circuit, sort_keys=True)}" + ) + else: + msgs.append(f"diagnostics: structure circuit {circuit_id} not found") + for node_name in node_names: + node = _find_parent_node(net, node_name) + msgs.append( + f"diagnostics: structure node {node_name} -> {json.dumps(node, sort_keys=True) if node is not None else 'missing'}" + ) + + if interface_a and callable(interface_a): + try: + ifa = interface_a() + rc, out, err = _tc(["class", "show", "dev", ifa]) + msgs.append(f"diagnostics: tc class show dev {ifa} rc={rc}") + msgs.extend(out.splitlines()[-20:] if rc == 0 else [err.strip()]) + rc, out, err = _qdisc_show(ifa) + msgs.append(f"diagnostics: tc qdisc show dev {ifa} rc={rc}") + msgs.extend(out.splitlines()[-20:] if rc == 0 else [err.strip()]) + except Exception as e: + msgs.append(f"diagnostics: failed tc dump for interface_a: {e}") + + if interface_b and callable(interface_b): + try: + ifb = interface_b() + rc, out, err = _tc(["class", "show", "dev", ifb]) + msgs.append(f"diagnostics: tc class show dev {ifb} rc={rc}") + msgs.extend(out.splitlines()[-20:] if rc == 0 else [err.strip()]) + rc, out, err = _qdisc_show(ifb) + msgs.append(f"diagnostics: tc qdisc show dev {ifb} rc={rc}") + msgs.extend(out.splitlines()[-20:] if rc == 0 else [err.strip()]) + except Exception as e: + msgs.append(f"diagnostics: failed tc dump for interface_b: {e}") + + last_error_path = "/tmp/lqos_bakery_last_error.txt" + if os.path.exists(last_error_path): + try: + with open(last_error_path, "r", errors="replace") as f: + lines = f.read().splitlines() + msgs.append(f"diagnostics: {last_error_path}") + msgs.extend(lines[-20:] if lines else ["diagnostics: last error file empty"]) + except Exception as e: + msgs.append(f"diagnostics: failed reading {last_error_path}: {e}") + + msgs.append(f"diagnostics: end for {tag}") + return msgs + + def with_backups(paths: List[str]): class _Ctx: def __enter__(self): @@ -1868,7 +2598,7 @@ def with_backups(paths: List[str]): # ----------------------- -def run_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bool: +def run_tiered_suite(log: AnyLogReader, timeout_s: float, results: List[str]) -> bool: ok = True # Baseline @@ -2185,13 +2915,14 @@ def run_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bo return ok -def run_virtualized_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bool: +def run_virtualized_tiered_suite(log: AnyLogReader, timeout_s: float, results: List[str]) -> bool: ok = True write_network_json(VIRTUALIZED_TIERED_NETWORK_BASE) rows = virtualized_tiered_circuits_base() write_circuits(rows) _ = run_refresh_and_wait(log, timeout_s) + settle_initial_bakery_logs(log) results.append("virtualized: baseline: ok (initial run)") _mark_step("virtualized: operator-authored virtual node promotion") @@ -2273,9 +3004,59 @@ def run_virtualized_tiered_suite(log: LogReader, timeout_s: float, results: List return ok -def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bool: +def run_realistic_tiered_suite(log: AnyLogReader, timeout_s: float, results: List[str]) -> bool: ok = True + def _first_row_index( + rows_local: List[Dict[str, str | int | float]], + parent_name: str, + exclude_ids: Optional[set[str]] = None, + ) -> Optional[int]: + excluded = exclude_ids or set() + for idx, row in enumerate(rows_local): + if str(row.get("Parent Node")) == parent_name and str(row.get("Circuit ID")) not in excluded: + return idx + return None + + def _record_check( + tag: str, + passed: bool, + msgs: List[str], + *, + step_started_at: Optional[float] = None, + circuit_ids: Optional[List[str]] = None, + node_names: Optional[List[str]] = None, + ) -> None: + nonlocal ok + results.extend(msgs) + ok &= passed + if not passed: + results.extend( + collect_failure_diagnostics( + tag, + step_started_at=step_started_at, + circuit_ids=circuit_ids, + node_names=node_names, + ) + ) + + def _record_hidden_health( + tag: str, + step_started_at: float, + *, + circuit_ids: Optional[List[str]] = None, + node_names: Optional[List[str]] = None, + ) -> None: + passed, msgs = check_no_hidden_incremental_failures(step_started_at) + _record_check( + f"{tag} health", + passed, + msgs, + step_started_at=step_started_at, + circuit_ids=circuit_ids, + node_names=node_names, + ) + # Baseline: write realistic network and circuits (including orphans) _mark_step("realistic: baseline") write_network_json(REALISTIC_TIERED_NETWORK) @@ -2328,11 +3109,11 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s passed, msg = assert_no_full_reload("realistic: site speed change", res, step_t0) results.append(msg) ok &= passed + _record_hidden_health("realistic: site speed change", step_t0, node_names=["NET2-1"]) _mark_step("realistic: site direction mapping (NET2-1)") passed_site, msgs_site = check_site_direction_ceil("NET2-1", expected_dl=360.0, expected_ul=240.0) - results.extend(msgs_site) - ok &= passed_site + _record_check("realistic: site direction mapping (NET2-1)", passed_site, msgs_site, circuit_ids=[], node_names=["NET2-1"]) # Circuit speed change for a deep leaf circuit (under NET1-1-1) _mark_step("realistic: circuit speed change") @@ -2360,16 +3141,15 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s passed, msg = assert_no_full_reload("realistic: circuit speed change", res, step_t0) results.append(msg) ok &= passed + _record_hidden_health("realistic: circuit speed change", step_t0, circuit_ids=[target_id]) _mark_step("realistic: circuit direction mapping (ceil)") passed_dir, msgs_dir = check_circuit_direction_ceil(target_id) - results.extend(msgs_dir) - ok &= passed_dir + _record_check("realistic: circuit direction mapping (ceil)", passed_dir, msgs_dir, circuit_ids=[target_id]) _mark_step("realistic: circuit ip mappings (no change)") passed_map, msgs_map = check_ip_mappings_for_circuit(target_id) - results.extend(msgs_map) - ok &= passed_map + _record_check("realistic: circuit ip mappings (no change)", passed_map, msgs_map, circuit_ids=[target_id]) passed_site_snapshot, site_snapshot_msg, site_snapshot = snapshot_site_class_assignments() if not passed_site_snapshot: @@ -2379,6 +3159,7 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s # Circuit parent-node move between existing nodes (should not trigger full reload) _mark_step("realistic: circuit parent move") rows_parent = json.loads(json.dumps(rows2)) + old_down_tc, old_up_tc, _old_handle_msgs = _current_circuit_handle_strings(target_id) old_parent = str(rows_parent[target_idx]["Parent Node"]) new_parent = "NET1-1-2" if old_parent != "NET1-1-2" else "NET1-2" rows_parent[target_idx]["Parent Node"] = new_parent @@ -2388,25 +3169,312 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s passed, msg = assert_no_full_reload("realistic: circuit parent move", res, step_t0) results.append(msg) ok &= passed + _record_hidden_health("realistic: circuit parent move", step_t0, circuit_ids=[target_id]) _mark_step("realistic: site class assignment stability after parent move") passed_sites_stable, msgs_sites_stable = check_site_class_assignments_unchanged(site_snapshot) - results.extend(msgs_sites_stable) - ok &= passed_sites_stable + _record_check("realistic: site class assignment stability after parent move", passed_sites_stable, msgs_sites_stable, circuit_ids=[target_id]) _mark_step("realistic: parent-moved circuit direction mapping (ceil)") - passed_parent_dir, msgs_parent_dir = check_circuit_direction_ceil(target_id) - results.extend(msgs_parent_dir) - ok &= passed_parent_dir + passed_parent_dir, msgs_parent_dir = wait_for_circuit_direction_ceil(target_id) + _record_check("realistic: parent-moved circuit direction mapping (ceil)", passed_parent_dir, msgs_parent_dir, circuit_ids=[target_id], step_started_at=step_t0) _mark_step("realistic: parent-moved circuit ip mappings") passed_parent_map, msgs_parent_map = check_ip_mappings_for_circuit(target_id) - results.extend(msgs_parent_map) - ok &= passed_parent_map + _record_check("realistic: parent-moved circuit ip mappings", passed_parent_map, msgs_parent_map, circuit_ids=[target_id]) + + _mark_step("realistic: parent-moved circuit cleanup") + passed_parent_cleanup, msgs_parent_cleanup = check_circuit_transition_cleanup( + target_id, + old_down_tc=old_down_tc, + old_up_tc=old_up_tc, + ) + _record_check("realistic: parent-moved circuit cleanup", passed_parent_cleanup, msgs_parent_cleanup, circuit_ids=[target_id]) + + _mark_step("realistic: parent-moved circuit batch exact IP mappings") + passed_parent_batch_ip, msgs_parent_batch_ip = check_exact_ip_mappings_for_circuits( + [target_id], + forbidden_by_circuit={target_id: {tc for tc in [old_down_tc, old_up_tc] if tc}}, + ) + _record_check( + "realistic: parent-moved circuit batch exact IP mappings", + passed_parent_batch_ip, + msgs_parent_batch_ip, + circuit_ids=[target_id], + ) + + # Multiple simultaneous parent-node moves in one incremental commit + _mark_step("realistic: multiple circuit parent moves") + rows_multi = json.loads(json.dumps(rows_parent)) + multi_specs = [ + ("NET2-1-1", "NET2-2"), + ("NET3-1", "NET3-2"), + ("NET1-2", "NET1-1-1"), + ] + moved_ids: List[str] = [] + old_handles_by_circuit: Dict[str, Tuple[Optional[str], Optional[str]]] = {} + excluded_ids = {target_id} + for old_parent_name, new_parent_name in multi_specs: + idx = _first_row_index(rows_multi, old_parent_name, excluded_ids) + if idx is None: + results.append( + f"realistic: multiple circuit parent moves: no circuit found under {old_parent_name}" + ) + return False + moved_id = str(rows_multi[idx]["Circuit ID"]) + old_handles_by_circuit[moved_id] = _current_circuit_handle_strings(moved_id)[:2] + rows_multi[idx]["Parent Node"] = new_parent_name + moved_ids.append(moved_id) + excluded_ids.add(moved_id) + write_circuits(rows_multi) + step_t0 = time.time() + res = run_refresh_and_wait(log, timeout_s) + passed, msg = assert_no_full_reload("realistic: multiple circuit parent moves", res, step_t0) + results.append(msg) + ok &= passed + _record_hidden_health("realistic: multiple circuit parent moves", step_t0, circuit_ids=moved_ids) + + _mark_step("realistic: site class assignment stability after multiple parent moves") + passed_sites_stable_multi, msgs_sites_stable_multi = check_site_class_assignments_unchanged(site_snapshot) + _record_check("realistic: site class assignment stability after multiple parent moves", passed_sites_stable_multi, msgs_sites_stable_multi, circuit_ids=moved_ids) + + for moved_id in moved_ids: + _mark_step(f"realistic: moved circuit {moved_id} direction mapping") + passed_move_dir, msgs_move_dir = wait_for_circuit_direction_ceil(moved_id) + _record_check(f"realistic: moved circuit {moved_id} direction mapping", passed_move_dir, msgs_move_dir, circuit_ids=[moved_id], step_started_at=step_t0) + + _mark_step(f"realistic: moved circuit {moved_id} ip mappings") + passed_move_map, msgs_move_map = check_ip_mappings_for_circuit(moved_id) + _record_check(f"realistic: moved circuit {moved_id} ip mappings", passed_move_map, msgs_move_map, circuit_ids=[moved_id]) + + _mark_step(f"realistic: moved circuit {moved_id} cleanup") + old_down_tc_multi, old_up_tc_multi = old_handles_by_circuit.get(moved_id, (None, None)) + passed_move_cleanup, msgs_move_cleanup = check_circuit_transition_cleanup( + moved_id, + old_down_tc=old_down_tc_multi, + old_up_tc=old_up_tc_multi, + ) + _record_check(f"realistic: moved circuit {moved_id} cleanup", passed_move_cleanup, msgs_move_cleanup, circuit_ids=[moved_id]) + + _mark_step("realistic: multiple parent moves batch exact IP mappings") + forbidden_multi = { + circuit_id: {tc for tc in old_handles_by_circuit.get(circuit_id, (None, None)) if tc} + for circuit_id in moved_ids + } + passed_multi_batch_ip, msgs_multi_batch_ip = check_exact_ip_mappings_for_circuits( + moved_ids, + forbidden_by_circuit=forbidden_multi, + ) + _record_check( + "realistic: multiple parent moves batch exact IP mappings", + passed_multi_batch_ip, + msgs_multi_batch_ip, + circuit_ids=moved_ids, + ) + + # Same-circuit mixed change: parent move + rate + IP + SQM in one incremental commit + _mark_step("realistic: same-circuit mixed change") + rows_same = json.loads(json.dumps(rows_multi)) + same_idx = _first_row_index(rows_same, "NET3-2", {target_id, *moved_ids}) + if same_idx is None: + results.append("realistic: same-circuit mixed change: no candidate circuit found under NET3-2") + return False + same_id = str(rows_same[same_idx]["Circuit ID"]) + same_old_ip = str(rows_same[same_idx]["IPv4"]) + same_old_down_tc, same_old_up_tc, _ = _current_circuit_handle_strings(same_id) + rows_same[same_idx]["Parent Node"] = "NET3-1" + rows_same[same_idx]["Download Max Mbps"] = float(rows_same[same_idx]["Download Max Mbps"]) + 19.0 + rows_same[same_idx]["Upload Max Mbps"] = float(rows_same[same_idx]["Upload Max Mbps"]) + 7.0 + rows_same[same_idx]["IPv4"] = "100.64.220.1" + rows_same[same_idx]["sqm"] = "cake/fq_codel" + write_circuits(rows_same) + step_t0 = time.time() + res = run_refresh_and_wait(log, timeout_s) + passed, msg = assert_no_full_reload("realistic: same-circuit mixed change", res, step_t0) + results.append(msg) + ok &= passed + _record_hidden_health("realistic: same-circuit mixed change", step_t0, circuit_ids=[same_id]) + + _mark_step("realistic: same-circuit mixed direction mapping") + passed_same_dir, msgs_same_dir = wait_for_circuit_direction_ceil(same_id) + _record_check("realistic: same-circuit mixed direction mapping", passed_same_dir, msgs_same_dir, circuit_ids=[same_id], step_started_at=step_t0) + + _mark_step("realistic: same-circuit mixed IP mappings") + passed_same_ip, msgs_same_ip = check_ip_mappings_for_circuit(same_id) + _record_check("realistic: same-circuit mixed IP mappings", passed_same_ip, msgs_same_ip, circuit_ids=[same_id]) + passed_same_old_ip, msgs_same_old_ip = check_ip_mapping_absent(same_old_ip) + _record_check("realistic: same-circuit mixed old IP absent", passed_same_old_ip, msgs_same_old_ip, circuit_ids=[same_id]) + + _mark_step("realistic: same-circuit mixed exact IP mappings") + passed_same_exact_ip, msgs_same_exact_ip = check_exact_ip_mappings_for_circuit( + same_id, + forbidden_tcs={tc for tc in [same_old_down_tc, same_old_up_tc] if tc}, + ) + _record_check("realistic: same-circuit mixed exact IP mappings", passed_same_exact_ip, msgs_same_exact_ip, circuit_ids=[same_id]) + + _mark_step("realistic: same-circuit mixed SQM kind") + passed_same_sqm, msgs_same_sqm = check_circuit_qdisc_kind( + same_id, + expected_down_kind="cake", + expected_up_kind="fq_codel", + ) + _record_check("realistic: same-circuit mixed SQM kind", passed_same_sqm, msgs_same_sqm, circuit_ids=[same_id]) + + _mark_step("realistic: same-circuit mixed cleanup") + passed_same_cleanup, msgs_same_cleanup = check_circuit_transition_cleanup( + same_id, + old_down_tc=same_old_down_tc, + old_up_tc=same_old_up_tc, + ) + _record_check("realistic: same-circuit mixed cleanup", passed_same_cleanup, msgs_same_cleanup, circuit_ids=[same_id]) + + _mark_step("realistic: same-circuit mixed batch exact IP mappings") + passed_same_batch_ip, msgs_same_batch_ip = check_exact_ip_mappings_for_circuits( + [same_id], + forbidden_by_circuit={same_id: {tc for tc in [same_old_down_tc, same_old_up_tc] if tc}}, + ) + _record_check( + "realistic: same-circuit mixed batch exact IP mappings", + passed_same_batch_ip, + msgs_same_batch_ip, + circuit_ids=[same_id], + ) + + # Mixed incremental batch: site speed, circuit speed, IP, SQM, and parent move together + _mark_step("realistic: mixed incremental batch") + net_mixed = json.loads(json.dumps(net2)) + try: + net_mixed["NET3"]["children"]["NET3-1"]["downloadBandwidthMbps"] = 275 + net_mixed["NET3"]["children"]["NET3-1"]["uploadBandwidthMbps"] = 155 + except Exception: + results.append("realistic: mixed incremental batch: failed to modify NET3-1 speeds in fixture") + return False + + rows_mixed = json.loads(json.dumps(rows_same)) + excluded_mixed = {target_id, *moved_ids, same_id} + + speed_idx = _first_row_index(rows_mixed, "NET2-2", excluded_mixed) + if speed_idx is None: + results.append("realistic: mixed incremental batch: no speed-change circuit found under NET2-2") + return False + speed_id = str(rows_mixed[speed_idx]["Circuit ID"]) + rows_mixed[speed_idx]["Download Max Mbps"] = float(rows_mixed[speed_idx]["Download Max Mbps"]) + 17.0 + rows_mixed[speed_idx]["Upload Max Mbps"] = float(rows_mixed[speed_idx]["Upload Max Mbps"]) + 6.0 + excluded_mixed.add(speed_id) + + ip_idx = _first_row_index(rows_mixed, "NET3-2", excluded_mixed) + if ip_idx is None: + results.append("realistic: mixed incremental batch: no IP-change circuit found under NET3-2") + return False + ip_id = str(rows_mixed[ip_idx]["Circuit ID"]) + old_ip = str(rows_mixed[ip_idx]["IPv4"]) + rows_mixed[ip_idx]["IPv4"] = "100.64.210.1" + excluded_mixed.add(ip_id) + + sqm_idx = _first_row_index(rows_mixed, "NET1-1-2", excluded_mixed) + if sqm_idx is None: + results.append("realistic: mixed incremental batch: no SQM-change circuit found under NET1-1-2") + return False + sqm_id = str(rows_mixed[sqm_idx]["Circuit ID"]) + rows_mixed[sqm_idx]["sqm"] = "fq_codel/cake" + excluded_mixed.add(sqm_id) + + mixed_move_idx: Optional[int] = None + mixed_move_target: Optional[str] = None + for source_parent, target_parent in [ + ("NET2-2", "NET2-1-1"), + ("NET3-2", "NET3-1"), + ("NET1-1-2", "NET1-2"), + ]: + idx = _first_row_index(rows_mixed, source_parent, excluded_mixed) + if idx is not None: + mixed_move_idx = idx + mixed_move_target = target_parent + break + if mixed_move_idx is None or mixed_move_target is None: + results.append("realistic: mixed incremental batch: no parent-move circuit found in fallback candidate set") + return False + mixed_move_id = str(rows_mixed[mixed_move_idx]["Circuit ID"]) + mixed_old_down_tc, mixed_old_up_tc, _ = _current_circuit_handle_strings(mixed_move_id) + rows_mixed[mixed_move_idx]["Parent Node"] = mixed_move_target + + write_network_json(net_mixed) + write_circuits(rows_mixed) + step_t0 = time.time() + res = run_refresh_and_wait(log, timeout_s) + passed, msg = assert_no_full_reload("realistic: mixed incremental batch", res, step_t0) + results.append(msg) + ok &= passed + _record_hidden_health( + "realistic: mixed incremental batch", + step_t0, + circuit_ids=[speed_id, ip_id, sqm_id, mixed_move_id], + node_names=["NET3-1"], + ) + + _mark_step("realistic: site class assignment stability after mixed batch") + passed_sites_stable_mixed, msgs_sites_stable_mixed = check_site_class_assignments_unchanged(site_snapshot) + _record_check("realistic: site class assignment stability after mixed batch", passed_sites_stable_mixed, msgs_sites_stable_mixed, circuit_ids=[speed_id, ip_id, sqm_id, mixed_move_id], node_names=["NET3-1"]) + + _mark_step("realistic: mixed batch site direction mapping (NET3-1)") + passed_site_mixed, msgs_site_mixed = check_site_direction_ceil("NET3-1", expected_dl=275.0, expected_ul=155.0) + _record_check("realistic: mixed batch site direction mapping (NET3-1)", passed_site_mixed, msgs_site_mixed, node_names=["NET3-1"]) + + _mark_step("realistic: mixed batch circuit speed direction mapping") + passed_speed_dir, msgs_speed_dir = wait_for_circuit_direction_ceil(speed_id) + _record_check("realistic: mixed batch circuit speed direction mapping", passed_speed_dir, msgs_speed_dir, circuit_ids=[speed_id], step_started_at=step_t0) + + _mark_step("realistic: mixed batch circuit IP mappings") + passed_ip_map, msgs_ip_map = check_ip_mappings_for_circuit(ip_id) + _record_check("realistic: mixed batch circuit IP mappings", passed_ip_map, msgs_ip_map, circuit_ids=[ip_id]) + passed_old_ip_absent, msgs_old_ip_absent = check_ip_mapping_absent(old_ip) + _record_check("realistic: mixed batch old IP absent", passed_old_ip_absent, msgs_old_ip_absent, circuit_ids=[ip_id]) + + _mark_step("realistic: mixed batch exact IP mappings") + passed_ip_exact, msgs_ip_exact = check_exact_ip_mappings_for_circuit(ip_id) + _record_check("realistic: mixed batch exact IP mappings", passed_ip_exact, msgs_ip_exact, circuit_ids=[ip_id]) + + _mark_step("realistic: mixed batch circuit SQM kind") + passed_sqm_kind, msgs_sqm_kind = check_circuit_qdisc_kind( + sqm_id, + expected_down_kind="fq_codel", + expected_up_kind="cake", + ) + _record_check("realistic: mixed batch circuit SQM kind", passed_sqm_kind, msgs_sqm_kind, circuit_ids=[sqm_id]) + + _mark_step("realistic: mixed batch parent-moved circuit direction mapping") + passed_mixed_move_dir, msgs_mixed_move_dir = wait_for_circuit_direction_ceil(mixed_move_id) + _record_check("realistic: mixed batch parent-moved circuit direction mapping", passed_mixed_move_dir, msgs_mixed_move_dir, circuit_ids=[mixed_move_id], step_started_at=step_t0) + + _mark_step("realistic: mixed batch parent-moved circuit ip mappings") + passed_mixed_move_map, msgs_mixed_move_map = check_ip_mappings_for_circuit(mixed_move_id) + _record_check("realistic: mixed batch parent-moved circuit ip mappings", passed_mixed_move_map, msgs_mixed_move_map, circuit_ids=[mixed_move_id]) + + _mark_step("realistic: mixed batch parent-moved circuit cleanup") + passed_mixed_move_cleanup, msgs_mixed_move_cleanup = check_circuit_transition_cleanup( + mixed_move_id, + old_down_tc=mixed_old_down_tc, + old_up_tc=mixed_old_up_tc, + ) + _record_check("realistic: mixed batch parent-moved circuit cleanup", passed_mixed_move_cleanup, msgs_mixed_move_cleanup, circuit_ids=[mixed_move_id]) + + _mark_step("realistic: mixed batch exact IP mappings") + passed_mixed_batch_ip, msgs_mixed_batch_ip = check_exact_ip_mappings_for_circuits( + [speed_id, ip_id, sqm_id, mixed_move_id], + forbidden_by_circuit={ + mixed_move_id: {tc for tc in [mixed_old_down_tc, mixed_old_up_tc] if tc}, + }, + ) + _record_check( + "realistic: mixed batch exact IP mappings", + passed_mixed_batch_ip, + msgs_mixed_batch_ip, + circuit_ids=[speed_id, ip_id, sqm_id, mixed_move_id], + ) # Add circuit under a different leaf (NET3-2) _mark_step("realistic: add circuit") - rows3 = json.loads(json.dumps(rows_parent)) + rows3 = json.loads(json.dumps(rows_mixed)) new_circuit_id = "99901" new_circuit_ip = "100.64.200.1" rows3.append({ @@ -2432,11 +3500,11 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s results.append("realistic: add circuit: full reload (allowed for larger tree)") else: results.append("realistic: add circuit: OK (no full reload)") + _record_hidden_health("realistic: add circuit", step_t0, circuit_ids=[new_circuit_id]) _mark_step("realistic: added circuit ip mappings") passed_add_map, msgs_add_map = check_ip_mappings_for_circuit(new_circuit_id) - results.extend(msgs_add_map) - ok &= passed_add_map + _record_check("realistic: added circuit ip mappings", passed_add_map, msgs_add_map, circuit_ids=[new_circuit_id]) # Remove the added circuit _mark_step("realistic: remove circuit") @@ -2448,19 +3516,194 @@ def run_realistic_tiered_suite(log: LogReader, timeout_s: float, results: List[s results.append("realistic: remove circuit: full reload (allowed for larger tree)") else: results.append("realistic: remove circuit: OK (no full reload)") + _record_hidden_health("realistic: remove circuit", step_t0, circuit_ids=[new_circuit_id]) _mark_step("realistic: removed circuit ip unmapped") passed_unmap, msgs_unmap = check_ip_mapping_absent(new_circuit_ip) - results.extend(msgs_unmap) - ok &= passed_unmap + _record_check("realistic: removed circuit ip unmapped", passed_unmap, msgs_unmap, circuit_ids=[new_circuit_id]) return ok +def run_fault_reload_suite(log: AnyLogReader, timeout_s: float, results: List[str]) -> bool: + ok = True + + def _record_check( + tag: str, + passed: bool, + msgs: List[str], + *, + step_started_at: Optional[float] = None, + circuit_ids: Optional[List[str]] = None, + node_names: Optional[List[str]] = None, + ) -> None: + nonlocal ok + results.extend(msgs) + ok &= passed + if not passed: + results.extend( + collect_failure_diagnostics( + tag, + step_started_at=step_started_at, + circuit_ids=circuit_ids, + node_names=node_names, + ) + ) + + clear_bakery_fault_once() + try: + _mark_step("fault-reload: baseline") + write_network_json(REALISTIC_TIERED_NETWORK) + rows = realistic_tiered_circuits_base() + write_circuits(rows) + _ = run_refresh_and_wait(log, timeout_s) + results.append("fault-reload: baseline: ok (initial run)") + + target_idx: Optional[int] = None + for idx, row in enumerate(rows): + if str(row.get("Parent Node")) == "NET1-1-1": + target_idx = idx + break + if target_idx is None: + results.append("fault-reload: no candidate circuit found under NET1-1-1") + return False + + target_id = str(rows[target_idx]["Circuit ID"]) + old_down_tc, old_up_tc, _ = _current_circuit_handle_strings(target_id) + + rows_fault = json.loads(json.dumps(rows)) + rows_fault[target_idx]["Parent Node"] = "NET1-1-2" + arm_bakery_fault_once("migrating circuits between parent nodes") + + _mark_step("fault-reload: injected parent-move failure") + write_circuits(rows_fault) + step_t0 = time.time() + res = run_refresh_and_wait(log, timeout_s) + if res.full_reload and not res.mq_init: + results.append("fault-reload: injected failure triggered an immediate full reload") + else: + results.append("fault-reload: injected failure commit completed without immediate full reload") + + passed_reload_required, msgs_reload_required = check_fault_injection_observed(step_t0) + _record_check( + "fault-reload: fault injection observed", + passed_reload_required, + msgs_reload_required, + step_started_at=step_t0, + circuit_ids=[target_id], + ) + + rows_rebuild = json.loads(json.dumps(rows_fault)) + rows_rebuild[target_idx]["Download Max Mbps"] = float(rows_rebuild[target_idx]["Download Max Mbps"]) + 7.0 + rows_rebuild[target_idx]["Upload Max Mbps"] = float(rows_rebuild[target_idx]["Upload Max Mbps"]) + 3.0 + + _mark_step("fault-reload: follow-up commit forces full reload") + write_circuits(rows_rebuild) + step_t1 = time.time() + res = run_refresh_and_wait(log, timeout_s) + passed_full, msg_full = assert_full_reload( + "fault-reload: follow-up commit forces full reload", + res, + step_started_at=step_t1, + ) + results.append(msg_full) + ok &= passed_full + if not passed_full: + results.extend( + collect_failure_diagnostics( + "fault-reload: follow-up commit forces full reload", + step_started_at=step_t1, + circuit_ids=[target_id], + ) + ) + + passed_reload_gate, msgs_reload_gate = check_reload_required_since(step_t0) + _record_check( + "fault-reload: reload-required gate observed", + passed_reload_gate, + msgs_reload_gate, + step_started_at=step_t0, + circuit_ids=[target_id], + ) + + _mark_step("fault-reload: post-reload circuit direction mapping") + passed_dir, msgs_dir = wait_for_circuit_direction_ceil(target_id) + _record_check( + "fault-reload: post-reload circuit direction mapping", + passed_dir, + msgs_dir, + circuit_ids=[target_id], + step_started_at=step_t1, + ) + + _mark_step("fault-reload: post-reload exact IP mappings") + passed_ip, msgs_ip = check_exact_ip_mappings_for_circuit( + target_id, + forbidden_tcs={tc for tc in [old_down_tc, old_up_tc] if tc}, + ) + _record_check( + "fault-reload: post-reload exact IP mappings", + passed_ip, + msgs_ip, + circuit_ids=[target_id], + ) + + _mark_step("fault-reload: post-reload cleanup") + passed_cleanup, msgs_cleanup = check_circuit_transition_cleanup( + target_id, + old_down_tc=old_down_tc, + old_up_tc=old_up_tc, + ) + _record_check( + "fault-reload: post-reload cleanup", + passed_cleanup, + msgs_cleanup, + circuit_ids=[target_id], + ) + + rows_recovered = json.loads(json.dumps(rows_rebuild)) + rows_recovered[target_idx]["Download Max Mbps"] = float(rows_recovered[target_idx]["Download Max Mbps"]) + 5.0 + rows_recovered[target_idx]["Upload Max Mbps"] = float(rows_recovered[target_idx]["Upload Max Mbps"]) + 2.0 + + _mark_step("fault-reload: post-reload incremental recovery") + write_circuits(rows_recovered) + step_t2 = time.time() + res = run_refresh_and_wait(log, timeout_s) + passed_recovery, msg_recovery = assert_no_full_reload( + "fault-reload: post-reload incremental recovery", + res, + step_started_at=step_t2, + ) + results.append(msg_recovery) + ok &= passed_recovery + if not passed_recovery: + results.extend( + collect_failure_diagnostics( + "fault-reload: post-reload incremental recovery", + step_started_at=step_t2, + circuit_ids=[target_id], + ) + ) + + passed_health, msgs_health = check_no_hidden_incremental_failures(step_t2) + _record_check( + "fault-reload: post-reload incremental recovery health", + passed_health, + msgs_health, + step_started_at=step_t2, + circuit_ids=[target_id], + ) + + return ok + finally: + clear_bakery_fault_once() + + def run_treeguard_runtime_suite( - log: LogReader, timeout_s: float, treeguard_timeout_s: float, results: List[str] + log: AnyLogReader, timeout_s: float, treeguard_timeout_s: float, results: List[str] ) -> bool: ok = True + cpu_mode = get_treeguard_cpu_mode() _mark_step("treeguard: baseline") write_network_json(treeguard_runtime_network_base()) @@ -2480,10 +3723,21 @@ def run_treeguard_runtime_suite( "702", treeguard_timeout_s, ) - results.extend(msgs) - ok &= passed + if passed: + results.extend(msgs) + ok &= passed + elif cpu_mode == "cpu_aware": + still_physical, still_physical_msgs = check_node_present_in_physical_tree("Town_TG") + results.extend(still_physical_msgs) + results.append( + "treeguard runtime check: SKIP under cpu_aware because no runtime virtualization occurred without induced CPU pressure" + ) + ok &= still_physical + else: + results.extend(msgs) + ok &= passed - new_lines = log.read_since(after_baseline_offset) + _, new_lines = log.read_since(after_baseline_offset) if _log_contains_unexpected_full_reload(new_lines): results.append("treeguard: runtime virtualization: unexpected full reload detected") ok = False @@ -2508,7 +3762,7 @@ def run_treeguard_runtime_suite( return ok -def run_flat_suite(log: LogReader, timeout_s: float, results: List[str]) -> bool: +def run_flat_suite(log: AnyLogReader, timeout_s: float, results: List[str]) -> bool: ok = True # Baseline @@ -2583,7 +3837,10 @@ def run_flat_suite(log: LogReader, timeout_s: float, results: List[str]) -> bool def main() -> int: ap = argparse.ArgumentParser(description="LibreQoS/Bakery integration tests") - ap.add_argument("--log-file", required=True, help="Path to lqosd log file (stdout/tee capture)") + ap.add_argument( + "--log-file", + help="Path to lqosd log file (stdout/tee capture). When omitted, the harness reads journalctl -u lqosd.", + ) ap.add_argument("--timeout", type=float, default=8.0, help="Wait time per step (seconds)") ap.add_argument( "--treeguard-timeout", @@ -2598,21 +3855,30 @@ def main() -> int: g.add_argument("--realistic-only", action="store_true", help="Run realistic tiered cases only") g.add_argument("--virtualized-only", action="store_true", help="Run virtualized-node tiered cases only") g.add_argument("--treeguard-only", action="store_true", help="Run live TreeGuard runtime cases only") + g.add_argument( + "--fault-reload-only", + action="store_true", + help="Run the opt-in Bakery fault-injection reload-escalation suite only", + ) ap.add_argument("--no-restore", action="store_true", help="Do not restore original files (debugging)") args = ap.parse_args() - # Pre-flight guidance - if not os.path.exists(args.log_file): - print("ERROR: Log file not found. Please run lqosd with logs, e.g.:") - print(" sudo RUST_LOG=info lqosd 2>&1 | tee /tmp/lqosd.log") - return 1 - if not is_lqosd_alive(): print("WARNING: lqosd does not appear to be running. This test will not observe Bakery logs.") print("Start lqosd and re-run, or continue if already started but lib binding cannot detect it.") - log = LogReader(args.log_file) + log: AnyLogReader + if args.log_file: + if not os.path.exists(args.log_file): + print(f"ERROR: Log file not found: {args.log_file}") + print("Either point --log-file at an existing tee'd log, or omit it to use journalctl -u lqosd.") + return 1 + print(f"Using tee'd lqosd log file: {args.log_file}") + log = LogReader(args.log_file) + else: + print("Using journalctl -u lqosd for Bakery event detection") + log = JournalctlLogReader("lqosd") results: List[str] = [] overall_ok = True @@ -2639,6 +3905,9 @@ def main() -> int: log, args.timeout, args.treeguard_timeout, results ) overall_ok &= ok + elif args.fault_reload_only: + ok = run_fault_reload_suite(log, args.timeout, results) + overall_ok &= ok elif args.full_suite: ok = run_tiered_suite(log, args.timeout, results) overall_ok &= ok @@ -2651,6 +3920,7 @@ def main() -> int: ok = run_realistic_tiered_suite(log, args.timeout, results) overall_ok &= ok + else: ok = run_tiered_suite(log, args.timeout, results) overall_ok &= ok diff --git a/src/rust/lqos_bakery/src/commands.rs b/src/rust/lqos_bakery/src/commands.rs index c2aec095..8e1a6418 100644 --- a/src/rust/lqos_bakery/src/commands.rs +++ b/src/rust/lqos_bakery/src/commands.rs @@ -1076,10 +1076,11 @@ mod tests { #[test] fn mq_setup_on_a_stick_emits_single_root_delete_before_replace() { - let mut cfg = Config::default(); - cfg.bridge = None; - cfg.single_interface = Some(SingleInterfaceConfig::default()); - let config = Arc::new(cfg); + let config = Arc::new(Config { + bridge: None, + single_interface: Some(SingleInterfaceConfig::default()), + ..Config::default() + }); let commands = BakeryCommands::MqSetup { queues_available: 1, diff --git a/src/rust/lqos_bakery/src/diff/circuits.rs b/src/rust/lqos_bakery/src/diff/circuits.rs index c9fdfe1d..1aaf20d0 100644 --- a/src/rust/lqos_bakery/src/diff/circuits.rs +++ b/src/rust/lqos_bakery/src/diff/circuits.rs @@ -172,6 +172,7 @@ mod tests { use std::collections::HashMap; use std::sync::Arc; + #[allow(clippy::too_many_arguments)] fn add_circuit( circuit_hash: i64, parent_class_id: u32, diff --git a/src/rust/lqos_bakery/src/lib.rs b/src/rust/lqos_bakery/src/lib.rs index ecade979..12c3517d 100644 --- a/src/rust/lqos_bakery/src/lib.rs +++ b/src/rust/lqos_bakery/src/lib.rs @@ -25,7 +25,7 @@ mod qdisc_handles; mod queue_math; mod utils; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::{Receiver, RecvTimeoutError, Sender}; use parking_lot::RwLock; use std::collections::VecDeque; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; @@ -33,6 +33,7 @@ use std::path::Path; use std::sync::atomic::Ordering::Relaxed; use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use std::sync::{Arc, OnceLock}; +use std::time::Duration; use tracing::{debug, error, info, warn}; use utils::current_timestamp; pub(crate) const CHANNEL_CAPACITY: usize = 65536; // 64k capacity for Bakery commands @@ -63,6 +64,8 @@ use lqos_config::{ TopLevelPlannerParams, plan_class_identities, plan_top_level_assignments, }; use qdisc_handles::MqDeviceLayout; + +const TEST_FAULT_ONCE_PATH: &str = "/tmp/lqos_bakery_fail_purpose_once.txt"; // ---------------------- Live-Move Types and Helpers (module scope) ---------------------- #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -107,6 +110,9 @@ struct Migration { ips: Vec, // Per-circuit SQM override ("cake" or "fq_codel"), if any sqm_override: Option, + // Desired final circuit state. This is published to the effective circuit map only after + // live TC and IP cutover reach the final class successfully. + desired_cmd: Arc, stage: MigrationStage, } @@ -136,6 +142,7 @@ struct VirtualizedSiteState { const RUNTIME_SITE_PRUNE_RETRY_SECONDS: u64 = 30; const RUNTIME_SITE_PRUNE_MAX_ATTEMPTS: u32 = 5; +const BAKERY_BACKGROUND_INTERVAL_MS: u64 = 250; const RUNTIME_DIRTY_SUBTREE_RELOAD_THRESHOLD: usize = 3; const RUNTIME_NODE_OPERATION_CAPACITY: usize = 32; const RUNTIME_NODE_OPERATION_DEFERRED_RETRY_SECONDS: u64 = 60; @@ -201,6 +208,30 @@ impl RuntimeNodeOperation { } } +fn mark_migration_reload_required(circuit_hash: i64, purpose: &str, result: &ExecuteResult) { + let summary = summarize_apply_result(purpose, result); + mark_reload_required(format!( + "Bakery live migration failed for circuit {} during {}: {}. A full reload is now required before further incremental topology mutations.", + circuit_hash, purpose, summary + )); +} + +fn migration_stage_apply_succeeded( + migration: &mut Migration, + purpose: &str, + result: &ExecuteResult, + next_stage: MigrationStage, +) -> bool { + if result.ok { + migration.stage = next_stage; + return true; + } + + mark_migration_reload_required(migration.circuit_hash, purpose, result); + migration.stage = MigrationStage::Done; + false +} + fn parse_ip_list(s: &str) -> Vec { s.split(',') .map(|x| x.trim().to_string()) @@ -389,11 +420,11 @@ fn queue_runtime_migration( final_minor: *class_minor, ips: parse_ip_list(ip_addresses), sqm_override: sqm_override.clone(), + desired_cmd: Arc::clone(new_cmd), stage: MigrationStage::PrepareShadow, }; migrations.insert(*circuit_hash, mig); - circuits.insert(*circuit_hash, Arc::clone(new_cmd)); true } @@ -417,6 +448,17 @@ fn queue_top_level_runtime_migration( queue_runtime_migration(old_cmd, new_cmd, circuits, live_circuits, migrations, false) } +fn circuits_with_pending_migration_targets( + circuits: &HashMap>, + migrations: &HashMap, +) -> HashMap> { + let mut overlaid = circuits.clone(); + for (circuit_hash, migration) in migrations { + overlaid.insert(*circuit_hash, Arc::clone(&migration.desired_cmd)); + } + overlaid +} + /// Count of Bakery-Managed circuits that are currently active. pub static ACTIVE_CIRCUITS: AtomicUsize = AtomicUsize::new(0); /// True while Bakery is applying a full reload batch to `tc`. @@ -955,6 +997,24 @@ fn summarize_apply_result(purpose: &str, result: &ExecuteResult) -> String { } } +fn consume_test_fault_once_from_path(path: &Path, purpose: &str) -> Option { + let Ok(raw) = std::fs::read_to_string(path) else { + return None; + }; + let needle = raw.trim(); + if needle.is_empty() || !(needle == "*" || purpose.contains(needle)) { + return None; + } + let _ = std::fs::remove_file(path); + Some(format!( + "synthetic Bakery test fault for purpose '{purpose}' matched selector '{needle}'" + )) +} + +fn consume_test_fault_once(purpose: &str) -> Option { + consume_test_fault_once_from_path(Path::new(TEST_FAULT_ONCE_PATH), purpose) +} + fn execute_and_record_live_change(command_buffer: &[Vec], purpose: &str) -> ExecuteResult { let (total, class_commands, qdisc_commands) = count_tc_command_types(command_buffer); mark_bakery_action_started( @@ -962,7 +1022,16 @@ fn execute_and_record_live_change(command_buffer: &[Vec], purpose: &str) "live_change_started", format!("{purpose}: started"), ); - let result = execute_in_memory(command_buffer, purpose); + let result = if let Some(fault_summary) = consume_test_fault_once(purpose) { + warn!("Bakery test fault injected: {fault_summary}"); + ExecuteResult { + ok: false, + duration_ms: 0, + failure_summary: Some(fault_summary), + } + } else { + execute_in_memory(command_buffer, purpose) + }; let summary = summarize_apply_result(purpose, &result); mark_bakery_action_finished(BakeryApplyMetrics { apply_type: BakeryApplyType::LiveChange, @@ -1873,6 +1942,364 @@ fn bakery_main(rx: Receiver, tx: Sender) { }) } + fn migration_final_state_ready( + config: &Arc, + migration: &Migration, + ) -> Result { + let down_snapshot = read_live_class_snapshot(&config.isp_interface())?; + let down_handle = TcHandle::from_u32( + (u32::from(migration.class_major) << 16) | u32::from(migration.final_minor), + ); + let Some(down_entry) = down_snapshot.get(&down_handle) else { + return Ok(false); + }; + if down_entry.leaf_qdisc_major.is_none() { + return Ok(false); + } + + if config.on_a_stick_mode() { + return Ok(true); + } + + let up_snapshot = read_live_class_snapshot(&config.internet_interface())?; + let up_handle = TcHandle::from_u32( + (u32::from(migration.up_class_major) << 16) | u32::from(migration.final_minor), + ); + let Some(up_entry) = up_snapshot.get(&up_handle) else { + return Ok(false); + }; + if up_entry.leaf_qdisc_major.is_none() { + return Ok(false); + } + + Ok(true) + } + + fn process_pending_migrations( + circuits: &mut HashMap>, + live_circuits: &mut HashMap, + sites: &HashMap>, + migrations: &mut HashMap, + mapping_current: &mut HashMap, + virtualized_sites: &mut HashMap, + runtime_node_operations: &mut HashMap, + ) { + if migrations.is_empty() { + return; + } + + let Ok(config) = lqos_config::load_config() else { + error!("Failed to load configuration while processing pending migrations."); + return; + }; + + let mut advanced = 0usize; + let mut to_remove = Vec::new(); + let mut effective_state_changed = false; + + for (_hash, mig) in migrations.iter_mut() { + if advanced >= MIGRATIONS_PER_TICK { + break; + } + + match mig.stage { + MigrationStage::PrepareShadow => { + if let Some(temp) = build_temp_add_cmd( + &BakeryCommands::AddCircuit { + circuit_hash: mig.circuit_hash, + parent_class_id: mig.parent_class_id, + up_parent_class_id: mig.up_parent_class_id, + class_minor: mig.shadow_minor, + download_bandwidth_min: mig.old_down_min, + upload_bandwidth_min: mig.old_up_min, + download_bandwidth_max: mig.old_down_max, + upload_bandwidth_max: mig.old_up_max, + class_major: mig.class_major, + up_class_major: mig.up_class_major, + down_qdisc_handle: mig.down_qdisc_handle, + up_qdisc_handle: mig.up_qdisc_handle, + ip_addresses: "".to_string(), + sqm_override: mig.sqm_override.clone(), + }, + mig.shadow_minor, + mig.old_down_min, + mig.old_down_max, + mig.old_up_min, + mig.old_up_max, + false, + ) { + let mut cmds = Vec::new(); + match config.queues.lazy_queues.as_ref() { + None | Some(LazyQueueMode::No) => { + if let Some(c) = + add_commands_for_circuit(&temp, &config, ExecutionMode::Builder) + { + cmds.extend(c); + } + } + Some(LazyQueueMode::Htb) => { + if let Some(c) = + add_commands_for_circuit(&temp, &config, ExecutionMode::Builder) + { + cmds.extend(c); + } + if let Some(c) = add_commands_for_circuit( + &temp, + &config, + ExecutionMode::LiveUpdate, + ) { + cmds.extend(c); + } + } + Some(LazyQueueMode::Full) => { + if let Some(c) = add_commands_for_circuit( + &temp, + &config, + ExecutionMode::LiveUpdate, + ) { + cmds.extend(c); + } + } + } + if !cmds.is_empty() { + let result = + execute_and_record_live_change(&cmds, "live-move: create shadow"); + let _ = migration_stage_apply_succeeded( + mig, + "live-move: create shadow", + &result, + MigrationStage::SwapToShadow, + ); + } else { + mig.stage = MigrationStage::SwapToShadow; + } + advanced += 1; + } else { + warn!( + "live-move: failed to build shadow add cmd for {}", + mig.circuit_hash + ); + mig.stage = MigrationStage::Done; + advanced += 1; + } + } + MigrationStage::SwapToShadow => { + for ip in &mig.ips { + let (ip_s, prefix) = parse_ip_and_prefix(ip); + let key = MappingKey { + ip: ip_s.clone(), + prefix, + }; + let cpu = mapping_current.get(&key).map(|v| v.cpu).unwrap_or(0); + let handle = tc_handle_from_major_minor(mig.class_major, mig.shadow_minor); + let _ = lqos_sys::add_ip_to_tc(&ip_s, handle, cpu, false, 0, 0); + mapping_current.insert(key, MappingVal { handle, cpu }); + } + let _ = lqos_sys::clear_hot_cache(); + mig.stage = MigrationStage::BuildFinal; + advanced += 1; + } + MigrationStage::BuildFinal => { + if let Some(old_cmd) = build_temp_add_cmd( + &BakeryCommands::AddCircuit { + circuit_hash: mig.circuit_hash, + parent_class_id: mig.old_parent_class_id, + up_parent_class_id: mig.old_up_parent_class_id, + class_minor: mig.old_minor, + download_bandwidth_min: mig.old_down_min, + upload_bandwidth_min: mig.old_up_min, + download_bandwidth_max: mig.old_down_max, + upload_bandwidth_max: mig.old_up_max, + class_major: mig.old_class_major, + up_class_major: mig.old_up_class_major, + down_qdisc_handle: None, + up_qdisc_handle: None, + ip_addresses: "".to_string(), + sqm_override: mig.sqm_override.clone(), + }, + mig.old_minor, + mig.old_down_min, + mig.old_down_max, + mig.old_up_min, + mig.old_up_max, + true, + ) { + let mut cmds = Vec::new(); + if let Some(prune) = old_cmd.to_prune(&config, true) { + cmds.extend(prune); + } + if let Some(final_cmd) = build_temp_add_cmd( + mig.desired_cmd.as_ref(), + mig.final_minor, + mig.new_down_min, + mig.new_down_max, + mig.new_up_min, + mig.new_up_max, + true, + ) { + match config.queues.lazy_queues.as_ref() { + None | Some(LazyQueueMode::No) => { + if let Some(c) = add_commands_for_circuit( + &final_cmd, + &config, + ExecutionMode::Builder, + ) { + cmds.extend(c); + } + } + Some(LazyQueueMode::Htb) => { + if let Some(c) = add_commands_for_circuit( + &final_cmd, + &config, + ExecutionMode::Builder, + ) { + cmds.extend(c); + } + if let Some(c) = add_commands_for_circuit( + &final_cmd, + &config, + ExecutionMode::LiveUpdate, + ) { + cmds.extend(c); + } + } + Some(LazyQueueMode::Full) => { + if let Some(c) = add_commands_for_circuit( + &final_cmd, + &config, + ExecutionMode::LiveUpdate, + ) { + cmds.extend(c); + } + } + } + } + if !cmds.is_empty() { + let result = + execute_and_record_live_change(&cmds, "live-move: build final"); + let _ = migration_stage_apply_succeeded( + mig, + "live-move: build final", + &result, + MigrationStage::SwapToFinal, + ); + } else { + mig.stage = MigrationStage::SwapToFinal; + } + advanced += 1; + } else { + warn!( + "live-move: failed to build old prune cmd for {}", + mig.circuit_hash + ); + mig.stage = MigrationStage::Done; + advanced += 1; + } + } + MigrationStage::SwapToFinal => { + let final_ready = match migration_final_state_ready(&config, mig) { + Ok(ready) => ready, + Err(e) => { + mark_reload_required(format!( + "Bakery live-move final verification failed for circuit {}: {}. A full reload is now required before further incremental topology mutations.", + mig.circuit_hash, e + )); + mig.stage = MigrationStage::Done; + advanced += 1; + continue; + } + }; + if !final_ready { + mark_reload_required(format!( + "Bakery live-move final verification did not find the expected final class/qdisc for circuit {}. A full reload is now required before further incremental topology mutations.", + mig.circuit_hash + )); + mig.stage = MigrationStage::Done; + advanced += 1; + continue; + } + + for ip in &mig.ips { + let (ip_s, prefix) = parse_ip_and_prefix(ip); + let key = MappingKey { + ip: ip_s.clone(), + prefix, + }; + let cpu = mapping_current.get(&key).map(|v| v.cpu).unwrap_or(0); + let handle = tc_handle_from_major_minor(mig.class_major, mig.final_minor); + let _ = lqos_sys::add_ip_to_tc(&ip_s, handle, cpu, false, 0, 0); + mapping_current.insert(key, MappingVal { handle, cpu }); + } + let _ = lqos_sys::clear_hot_cache(); + circuits.insert(mig.circuit_hash, Arc::clone(&mig.desired_cmd)); + effective_state_changed = true; + mig.stage = MigrationStage::TeardownShadow; + advanced += 1; + } + MigrationStage::TeardownShadow => { + if let Some(shadow_cmd) = build_temp_add_cmd( + &BakeryCommands::AddCircuit { + circuit_hash: mig.circuit_hash, + parent_class_id: mig.parent_class_id, + up_parent_class_id: mig.up_parent_class_id, + class_minor: mig.shadow_minor, + download_bandwidth_min: mig.old_down_min, + upload_bandwidth_min: mig.old_up_min, + download_bandwidth_max: mig.old_down_max, + upload_bandwidth_max: mig.old_up_max, + class_major: mig.class_major, + up_class_major: mig.up_class_major, + down_qdisc_handle: None, + up_qdisc_handle: None, + ip_addresses: "".to_string(), + sqm_override: mig.sqm_override.clone(), + }, + mig.shadow_minor, + mig.old_down_min, + mig.old_down_max, + mig.old_up_min, + mig.old_up_max, + false, + ) && let Some(prune) = shadow_cmd.to_prune(&config, true) + { + let result = + execute_and_record_live_change(&prune, "live-move: prune shadow"); + let _ = migration_stage_apply_succeeded( + mig, + "live-move: prune shadow", + &result, + MigrationStage::Done, + ); + } else { + mig.stage = MigrationStage::Done; + } + advanced += 1; + } + MigrationStage::Done => { + to_remove.push(mig.circuit_hash); + } + } + } + + for circuit_hash in to_remove { + migrations.remove(&circuit_hash); + } + + flush_deferred_runtime_site_prunes( + &config, + sites, + circuits, + virtualized_sites, + migrations, + runtime_node_operations, + ); + if effective_state_changed { + update_queue_distribution_snapshot(sites, circuits); + } + + let _ = live_circuits; + } + { let Ok(config) = lqos_config::load_config() else { error!("Failed to load configuration, exiting Bakery thread."); @@ -1890,7 +2317,23 @@ fn bakery_main(rx: Receiver, tx: Sender) { ); } - while let Ok(command) = rx.recv() { + loop { + let command = match rx.recv_timeout(Duration::from_millis(BAKERY_BACKGROUND_INTERVAL_MS)) { + Ok(command) => command, + Err(RecvTimeoutError::Timeout) => { + process_pending_migrations( + &mut circuits, + &mut live_circuits, + &sites, + &mut migrations, + &mut mapping_current, + &mut virtualized_sites, + &mut runtime_node_operations, + ); + continue; + } + Err(RecvTimeoutError::Disconnected) => break, + }; debug!("Bakery received command: {:?}", command); match command { @@ -2030,6 +2473,15 @@ fn bakery_main(rx: Receiver, tx: Sender) { &mut virtualized_sites, &mut runtime_node_operations, ); + process_pending_migrations( + &mut circuits, + &mut live_circuits, + &sites, + &mut migrations, + &mut mapping_current, + &mut virtualized_sites, + &mut runtime_node_operations, + ); } BakeryCommands::MqSetup { .. } => { if let Some(batch) = &mut batch { @@ -2052,289 +2504,13 @@ fn bakery_main(rx: Receiver, tx: Sender) { BakeryCommands::Tick => { // Reset per-cycle counters at the start of the tick handle_tick(&mut circuits, &mut live_circuits, &mut sites); - - // Advance live-move migrations - let Ok(config) = lqos_config::load_config() else { - error!("Failed to load configuration, exiting Bakery thread."); - continue; - }; - let mut advanced = 0usize; - let mut to_remove = Vec::new(); - for (_hash, mig) in migrations.iter_mut() { - if advanced >= MIGRATIONS_PER_TICK { - break; - } - match mig.stage { - MigrationStage::PrepareShadow => { - // Create shadow HTB+CAKE with OLD rates - if let Some(temp) = build_temp_add_cmd( - &BakeryCommands::AddCircuit { - circuit_hash: mig.circuit_hash, - parent_class_id: mig.parent_class_id, - up_parent_class_id: mig.up_parent_class_id, - class_minor: mig.shadow_minor, - download_bandwidth_min: mig.old_down_min, - upload_bandwidth_min: mig.old_up_min, - download_bandwidth_max: mig.old_down_max, - upload_bandwidth_max: mig.old_up_max, - class_major: mig.class_major, - up_class_major: mig.up_class_major, - down_qdisc_handle: mig.down_qdisc_handle, - up_qdisc_handle: mig.up_qdisc_handle, - ip_addresses: "".to_string(), - sqm_override: mig.sqm_override.clone(), - }, - mig.shadow_minor, - mig.old_down_min, - mig.old_down_max, - mig.old_up_min, - mig.old_up_max, - false, - ) { - let mut cmds = Vec::new(); - match config.queues.lazy_queues.as_ref() { - None | Some(LazyQueueMode::No) => { - if let Some(c) = add_commands_for_circuit( - &temp, - &config, - ExecutionMode::Builder, - ) { - cmds.extend(c); - } - } - Some(LazyQueueMode::Htb) => { - if let Some(c) = add_commands_for_circuit( - &temp, - &config, - ExecutionMode::Builder, - ) { - cmds.extend(c); - } - if let Some(c) = add_commands_for_circuit( - &temp, - &config, - ExecutionMode::LiveUpdate, - ) { - cmds.extend(c); - } - } - Some(LazyQueueMode::Full) => { - if let Some(c) = add_commands_for_circuit( - &temp, - &config, - ExecutionMode::LiveUpdate, - ) { - cmds.extend(c); - } - } - } - if !cmds.is_empty() { - execute_and_record_live_change( - &cmds, - "live-move: create shadow", - ); - } - mig.stage = MigrationStage::SwapToShadow; - advanced += 1; - } else { - warn!( - "live-move: failed to build shadow add cmd for {}", - mig.circuit_hash - ); - mig.stage = MigrationStage::Done; - advanced += 1; - } - } - MigrationStage::SwapToShadow => { - // Remap all IPs to shadow handles using existing CPU. - // Upload mapping is derived in the dataplane for on-a-stick mode. - for ip in &mig.ips { - let (ip_s, prefix) = parse_ip_and_prefix(ip); - let key = MappingKey { - ip: ip_s.clone(), - prefix, - }; - let cpu = mapping_current.get(&key).map(|v| v.cpu).unwrap_or(0); - let handle = - tc_handle_from_major_minor(mig.class_major, mig.shadow_minor); - let _ = lqos_sys::add_ip_to_tc(&ip_s, handle, cpu, false, 0, 0); - // Update local mapping view - mapping_current.insert(key, MappingVal { handle, cpu }); - } - // Clear the hot cache directly - let _ = lqos_sys::clear_hot_cache(); - mig.stage = MigrationStage::BuildFinal; - advanced += 1; - } - MigrationStage::BuildFinal => { - // Delete old classes/qdiscs and create final with NEW rates at final minor. - if let Some(old_cmd) = build_temp_add_cmd( - &BakeryCommands::AddCircuit { - circuit_hash: mig.circuit_hash, - parent_class_id: mig.old_parent_class_id, - up_parent_class_id: mig.old_up_parent_class_id, - class_minor: mig.old_minor, - download_bandwidth_min: mig.old_down_min, - upload_bandwidth_min: mig.old_up_min, - download_bandwidth_max: mig.old_down_max, - upload_bandwidth_max: mig.old_up_max, - class_major: mig.old_class_major, - up_class_major: mig.old_up_class_major, - down_qdisc_handle: None, - up_qdisc_handle: None, - ip_addresses: "".to_string(), - sqm_override: mig.sqm_override.clone(), - }, - mig.old_minor, - mig.old_down_min, - mig.old_down_max, - mig.old_up_min, - mig.old_up_max, - true, - ) { - let mut cmds = Vec::new(); - if let Some(prune) = old_cmd.to_prune(&config, true) { - cmds.extend(prune); - } - // Final add (new rates) at final_minor - if let Some(final_cmd) = build_temp_add_cmd( - &BakeryCommands::AddCircuit { - circuit_hash: mig.circuit_hash, - parent_class_id: mig.parent_class_id, - up_parent_class_id: mig.up_parent_class_id, - class_minor: mig.final_minor, - download_bandwidth_min: mig.new_down_min, - upload_bandwidth_min: mig.new_up_min, - download_bandwidth_max: mig.new_down_max, - upload_bandwidth_max: mig.new_up_max, - class_major: mig.class_major, - up_class_major: mig.up_class_major, - down_qdisc_handle: mig.down_qdisc_handle, - up_qdisc_handle: mig.up_qdisc_handle, - ip_addresses: "".to_string(), - sqm_override: mig.sqm_override.clone(), - }, - mig.final_minor, - mig.new_down_min, - mig.new_down_max, - mig.new_up_min, - mig.new_up_max, - true, - ) { - match config.queues.lazy_queues.as_ref() { - None | Some(LazyQueueMode::No) => { - if let Some(c) = add_commands_for_circuit( - &final_cmd, - &config, - ExecutionMode::Builder, - ) { - cmds.extend(c); - } - } - Some(LazyQueueMode::Htb) => { - if let Some(c) = add_commands_for_circuit( - &final_cmd, - &config, - ExecutionMode::Builder, - ) { - cmds.extend(c); - } - if let Some(c) = add_commands_for_circuit( - &final_cmd, - &config, - ExecutionMode::LiveUpdate, - ) { - cmds.extend(c); - } - } - Some(LazyQueueMode::Full) => { - if let Some(c) = add_commands_for_circuit( - &final_cmd, - &config, - ExecutionMode::LiveUpdate, - ) { - cmds.extend(c); - } - } - } - } - if !cmds.is_empty() { - execute_and_record_live_change(&cmds, "live-move: build final"); - } - mig.stage = MigrationStage::SwapToFinal; - advanced += 1; - } else { - warn!( - "live-move: failed to build old prune cmd for {}", - mig.circuit_hash - ); - mig.stage = MigrationStage::Done; - advanced += 1; - } - } - MigrationStage::SwapToFinal => { - for ip in &mig.ips { - let (ip_s, prefix) = parse_ip_and_prefix(ip); - let key = MappingKey { - ip: ip_s.clone(), - prefix, - }; - let cpu = mapping_current.get(&key).map(|v| v.cpu).unwrap_or(0); - let handle = - tc_handle_from_major_minor(mig.class_major, mig.final_minor); - let _ = lqos_sys::add_ip_to_tc(&ip_s, handle, cpu, false, 0, 0); - mapping_current.insert(key, MappingVal { handle, cpu }); - } - let _ = lqos_sys::clear_hot_cache(); - mig.stage = MigrationStage::TeardownShadow; - advanced += 1; - } - MigrationStage::TeardownShadow => { - // Remove shadow classes - if let Some(shadow_cmd) = build_temp_add_cmd( - &BakeryCommands::AddCircuit { - circuit_hash: mig.circuit_hash, - parent_class_id: mig.parent_class_id, - up_parent_class_id: mig.up_parent_class_id, - class_minor: mig.shadow_minor, - download_bandwidth_min: mig.old_down_min, - upload_bandwidth_min: mig.old_up_min, - download_bandwidth_max: mig.old_down_max, - upload_bandwidth_max: mig.old_up_max, - class_major: mig.class_major, - up_class_major: mig.up_class_major, - down_qdisc_handle: None, - up_qdisc_handle: None, - ip_addresses: "".to_string(), - sqm_override: mig.sqm_override.clone(), - }, - mig.shadow_minor, - mig.old_down_min, - mig.old_down_max, - mig.old_up_min, - mig.old_up_max, - false, - ) && let Some(prune) = shadow_cmd.to_prune(&config, true) - { - execute_and_record_live_change(&prune, "live-move: prune shadow"); - } - mig.stage = MigrationStage::Done; - advanced += 1; - } - MigrationStage::Done => { - to_remove.push(mig.circuit_hash); - } - } - } - for h in to_remove { - migrations.remove(&h); - } - flush_deferred_runtime_site_prunes( - &config, + process_pending_migrations( + &mut circuits, + &mut live_circuits, &sites, - &circuits, + &mut migrations, + &mut mapping_current, &mut virtualized_sites, - &migrations, &mut runtime_node_operations, ); } @@ -2594,7 +2770,8 @@ fn handle_commit_batch( return; } - let circuit_change_mode = diff_circuits(&new_batch, circuits); + let circuits_for_diff = circuits_with_pending_migration_targets(circuits, migrations); + let circuit_change_mode = diff_circuits(&new_batch, &circuits_for_diff); // If neither has changed, there's nothing to do. if matches!(site_change_mode, SiteDiffResult::NoChange) @@ -2855,7 +3032,13 @@ fn handle_commit_batch( warn!("Bakery: missing MQ layout during circuit migrations"); return; }; + let down_live_snapshot = read_live_class_snapshot(&config.isp_interface()).ok(); + let up_live_snapshot = read_live_class_snapshot(&config.internet_interface()).ok(); let mut immediate_commands = Vec::new(); + let mut migrated_updates = Vec::new(); + let mut prepared_migrations = Vec::new(); + let mut protected_down_classes = HashSet::new(); + let mut protected_up_classes = HashSet::new(); for cmd in &categories.migrated { let mut enriched_cmd = with_assigned_qdisc_handles(cmd, &config, layout, qdisc_handles); @@ -2876,7 +3059,17 @@ fn handle_commit_batch( layout, qdisc_handles, ); + let BakeryCommands::AddCircuit { circuit_hash, .. } = enriched_cmd.as_ref() else { + continue; + }; + if let Some((down_class, up_class)) = circuit_class_handles(enriched_cmd.as_ref()) { + protected_down_classes.insert(down_class); + protected_up_classes.insert(up_class); + } + prepared_migrations.push((*circuit_hash, old_cmd, enriched_cmd)); + } + for (circuit_hash, old_cmd, enriched_cmd) in prepared_migrations { if queue_live_migration( old_cmd.as_ref(), &enriched_cmd, @@ -2887,11 +3080,7 @@ fn handle_commit_batch( continue; } - let BakeryCommands::AddCircuit { circuit_hash, .. } = enriched_cmd.as_ref() else { - continue; - }; - let was_activated = live_circuits.contains_key(circuit_hash); - if was_activated { + if live_circuits.contains_key(&circuit_hash) { warn!( "Bakery: falling back to full reload for active migrated circuit {} because live migration setup failed", circuit_hash @@ -2926,7 +3115,14 @@ fn handle_commit_batch( match config.queues.lazy_queues.as_ref() { None | Some(LazyQueueMode::No) => { - if let Some(prune) = old_cmd.to_prune(&config, true) { + if let Some(prune) = observed_circuit_prune_commands( + &config, + old_cmd.as_ref(), + down_live_snapshot.as_ref(), + up_live_snapshot.as_ref(), + &protected_down_classes, + &protected_up_classes, + ) { immediate_commands.extend(prune); } if let Some(add) = enriched_cmd.to_commands(&config, ExecutionMode::Builder) @@ -2935,7 +3131,14 @@ fn handle_commit_batch( } } Some(LazyQueueMode::Htb) => { - if let Some(prune) = old_cmd.to_prune(&config, true) { + if let Some(prune) = observed_circuit_prune_commands( + &config, + old_cmd.as_ref(), + down_live_snapshot.as_ref(), + up_live_snapshot.as_ref(), + &protected_down_classes, + &protected_up_classes, + ) { immediate_commands.extend(prune); } if let Some(add_htb) = @@ -2946,13 +3149,27 @@ fn handle_commit_batch( } Some(LazyQueueMode::Full) => {} } - circuits.insert(*circuit_hash, enriched_cmd); + migrated_updates.push((circuit_hash, enriched_cmd)); } if !immediate_commands.is_empty() { - execute_and_record_live_change( + let result = execute_and_record_live_change( &immediate_commands, "migrating circuits between parent nodes (fallback)", ); + if !result.ok { + let summary = summarize_apply_result( + "migrating circuits between parent nodes (fallback)", + &result, + ); + mark_reload_required(format!( + "Bakery circuit parent-move fallback failed: {}. A full reload is now required before further incremental topology mutations.", + summary + )); + migrated_updates.clear(); + } + } + for (circuit_hash, enriched_cmd) in migrated_updates { + circuits.insert(circuit_hash, enriched_cmd); } } @@ -3590,6 +3807,7 @@ fn collect_direct_circuit_hashes( hashes } +#[allow(clippy::too_many_arguments)] fn apply_direct_circuit_reparents( config: &Arc, circuit_hashes: &[i64], @@ -3800,6 +4018,23 @@ fn current_circuit_queue(circuit: &BakeryCommands) -> Option { Some(u32::from(*class_major)) } +fn circuit_class_handles(circuit: &BakeryCommands) -> Option<(TcHandle, TcHandle)> { + let BakeryCommands::AddCircuit { + class_minor, + class_major, + up_class_major, + .. + } = circuit + else { + return None; + }; + + Some(( + tc_handle_from_major_minor(*class_major, *class_minor), + tc_handle_from_major_minor(*up_class_major, *class_minor), + )) +} + fn rebuild_queue_distribution_snapshot( sites: &HashMap>, circuits: &HashMap>, @@ -3907,6 +4142,122 @@ fn rebuild_runtime_operations_snapshot( } } +fn observed_circuit_prune_commands( + config: &Arc, + old_cmd: &BakeryCommands, + down_snapshot: Option<&HashMap>, + up_snapshot: Option<&HashMap>, + protected_down_classes: &HashSet, + protected_up_classes: &HashSet, +) -> Option>> { + let BakeryCommands::AddCircuit { + parent_class_id, + up_parent_class_id, + class_minor, + class_major, + up_class_major, + sqm_override, + .. + } = old_cmd + else { + return old_cmd.to_prune(config, true); + }; + + let down_class = TcHandle::from_u32((u32::from(*class_major) << 16) | u32::from(*class_minor)); + let up_class = TcHandle::from_u32((u32::from(*up_class_major) << 16) | u32::from(*class_minor)); + + let (down_override_opt, up_override_opt) = match sqm_override.as_ref() { + None => (None, None), + Some(s) if s.contains('/') => { + let mut it = s.splitn(2, '/'); + let down = it.next().unwrap_or("").trim(); + let up = it.next().unwrap_or("").trim(); + let map = |t: &str| -> Option { + if t.is_empty() { + None + } else { + Some(t.to_string()) + } + }; + (map(down), map(up)) + } + Some(s) => (Some(s.clone()), Some(s.clone())), + }; + + let prune_down_qdisc = + !matches!(down_override_opt.as_deref(), Some(s) if s.eq_ignore_ascii_case("none")); + let prune_up_qdisc = + !matches!(up_override_opt.as_deref(), Some(s) if s.eq_ignore_ascii_case("none")); + + let mut result = Vec::new(); + + if !protected_up_classes.contains(&up_class) + && let Some(snapshot) = up_snapshot + && let Some(entry) = snapshot.get(&up_class) + { + if prune_up_qdisc && !config.on_a_stick_mode() && entry.leaf_qdisc_major.is_some() { + result.push(vec![ + "qdisc".to_string(), + "del".to_string(), + "dev".to_string(), + config.internet_interface(), + "parent".to_string(), + format!("0x{:x}:0x{:x}", up_class_major, class_minor), + ]); + } + result.push(vec![ + "class".to_string(), + "del".to_string(), + "dev".to_string(), + config.internet_interface(), + "parent".to_string(), + up_parent_class_id.as_tc_string(), + "classid".to_string(), + format!( + "0x{:x}:0x{:x}", + up_parent_class_id.get_major_minor().0, + class_minor + ), + ]); + } + + if !protected_down_classes.contains(&down_class) + && let Some(snapshot) = down_snapshot + && let Some(entry) = snapshot.get(&down_class) + { + if prune_down_qdisc && entry.leaf_qdisc_major.is_some() { + result.push(vec![ + "qdisc".to_string(), + "del".to_string(), + "dev".to_string(), + config.isp_interface(), + "parent".to_string(), + format!("0x{:x}:0x{:x}", class_major, class_minor), + ]); + } + result.push(vec![ + "class".to_string(), + "del".to_string(), + "dev".to_string(), + config.isp_interface(), + "parent".to_string(), + parent_class_id.as_tc_string(), + "classid".to_string(), + format!( + "0x{:x}:0x{:x}", + parent_class_id.get_major_minor().0, + class_minor + ), + ]); + } + + if result.is_empty() { + old_cmd.to_prune(config, true).map(|_| Vec::new()) + } else { + Some(result) + } +} + fn site_parent_hash( site_hash: i64, sites: &HashMap>, @@ -4229,7 +4580,7 @@ fn build_top_level_virtualization_plan( current_timestamp() as f64, &TopLevelPlannerParams { mode: TopLevelPlannerMode::StableGreedy, - move_budget_per_run: std::cmp::max(1, std::cmp::min(32, planner_items.len())), + move_budget_per_run: planner_items.len().clamp(1, 32), ..TopLevelPlannerParams::default() }, ); @@ -4663,6 +5014,7 @@ fn apply_site_command_updates( Ok(()) } +#[allow(clippy::too_many_arguments)] fn apply_circuit_command_updates( config: &Arc, circuits: &mut HashMap>, @@ -4766,6 +5118,7 @@ fn circuit_qdisc_parent_changed( old_down_parent != new_down_parent || old_up_parent != new_up_parent } +#[allow(clippy::too_many_arguments)] fn apply_top_level_circuit_command_updates( config: &Arc, circuits: &mut HashMap>, @@ -6414,6 +6767,7 @@ mod tests { final_minor: 0x10, ips: vec!["192.0.2.40/32".to_string()], sqm_override: None, + desired_cmd: mk_add_circuit(40, "192.0.2.40/32"), stage: MigrationStage::PrepareShadow, }, ); @@ -6572,6 +6926,99 @@ mod tests { assert_eq!(commands[1][0], "class"); } + #[test] + fn observed_circuit_prune_commands_only_delete_existing_old_state() { + let config = Arc::new(lqos_config::Config::default()); + let circuit = mk_test_circuit(9300, 0x10020, 0x20020, 0x21, 0x1, 0x2, "192.0.2.94/32"); + + let mut down_snapshot = HashMap::new(); + down_snapshot.insert( + TcHandle::from_u32(0x10021), + LiveTcClassEntry { + class_id: TcHandle::from_u32(0x10021), + parent: Some(TcHandle::from_u32(0x10020)), + leaf_qdisc_major: Some(0x9000), + }, + ); + let up_snapshot = HashMap::new(); + + let commands = observed_circuit_prune_commands( + &config, + circuit.as_ref(), + Some(&down_snapshot), + Some(&up_snapshot), + &HashSet::new(), + &HashSet::new(), + ) + .expect("observed circuit prune commands"); + + assert_eq!(commands.len(), 2); + assert_eq!(commands[0][0], "qdisc"); + assert_eq!(commands[0][3], config.isp_interface()); + assert_eq!(commands[1][0], "class"); + assert_eq!(commands[1][3], config.isp_interface()); + } + + #[test] + fn observed_circuit_prune_commands_skip_missing_old_classes() { + let config = Arc::new(lqos_config::Config::default()); + let circuit = mk_test_circuit(9301, 0x10020, 0x20020, 0x21, 0x1, 0x2, "192.0.2.95/32"); + let down_snapshot = HashMap::new(); + let up_snapshot = HashMap::new(); + + let commands = observed_circuit_prune_commands( + &config, + circuit.as_ref(), + Some(&down_snapshot), + Some(&up_snapshot), + &HashSet::new(), + &HashSet::new(), + ) + .expect("observed circuit prune commands"); + + assert!(commands.is_empty()); + } + + #[test] + fn observed_circuit_prune_commands_skip_handles_reserved_by_batch_targets() { + let config = Arc::new(lqos_config::Config::default()); + let circuit = mk_test_circuit(9302, 0x10020, 0x20020, 0x21, 0x1, 0x2, "192.0.2.96/32"); + + let mut down_snapshot = HashMap::new(); + down_snapshot.insert( + TcHandle::from_u32(0x10021), + LiveTcClassEntry { + class_id: TcHandle::from_u32(0x10021), + parent: Some(TcHandle::from_u32(0x10020)), + leaf_qdisc_major: Some(0x9000), + }, + ); + let mut up_snapshot = HashMap::new(); + up_snapshot.insert( + TcHandle::from_u32(0x20021), + LiveTcClassEntry { + class_id: TcHandle::from_u32(0x20021), + parent: Some(TcHandle::from_u32(0x20020)), + leaf_qdisc_major: Some(0x9001), + }, + ); + + let protected_down = HashSet::from([TcHandle::from_u32(0x10021)]); + let protected_up = HashSet::from([TcHandle::from_u32(0x20021)]); + + let commands = observed_circuit_prune_commands( + &config, + circuit.as_ref(), + Some(&down_snapshot), + Some(&up_snapshot), + &protected_down, + &protected_up, + ) + .expect("observed circuit prune commands"); + + assert!(commands.is_empty()); + } + #[test] fn sync_runtime_virtualized_site_qdisc_handles_from_live_snapshot_recovers_leaf_handles() { let mut state = VirtualizedSiteState { @@ -7349,8 +7796,9 @@ mod tests { assert_ne!(migration.shadow_minor, migration.old_minor); assert!(matches!( circuits.get(&9001), - Some(cmd) if Arc::ptr_eq(cmd, &new_cmd) + Some(cmd) if Arc::ptr_eq(cmd, &old_cmd) )); + assert!(Arc::ptr_eq(&migration.desired_cmd, &new_cmd)); } #[test] @@ -7393,6 +7841,184 @@ mod tests { )); } + #[test] + fn pending_migration_overlay_uses_desired_state_for_diff_only() { + let old_cmd = mk_test_circuit(9300, 0x10020, 0x20020, 0x21, 0x1, 0x2, "192.0.2.94/32"); + let new_cmd = mk_test_circuit(9300, 0x10034, 0x20034, 0x35, 0x3, 0x4, "192.0.2.94/32"); + + let mut circuits = HashMap::from([(9300, Arc::clone(&old_cmd))]); + let live_circuits = HashMap::from([(9300, 1u64)]); + let mut migrations = HashMap::new(); + + assert!(queue_live_migration( + old_cmd.as_ref(), + &new_cmd, + &mut circuits, + &live_circuits, + &mut migrations, + )); + + assert!(matches!( + circuits.get(&9300), + Some(cmd) if Arc::ptr_eq(cmd, &old_cmd) + )); + + let overlaid = circuits_with_pending_migration_targets(&circuits, &migrations); + assert!(matches!( + overlaid.get(&9300), + Some(cmd) if Arc::ptr_eq(cmd, &new_cmd) + )); + } + + #[test] + fn migration_stage_failure_marks_reload_required_and_stops_migration() { + clear_reload_required( + "reset before migration_stage_failure_marks_reload_required_and_stops_migration", + ); + let mut migration = Migration { + circuit_hash: 9002, + old_parent_class_id: TcHandle::from_u32(0x10020), + old_up_parent_class_id: TcHandle::from_u32(0x20020), + old_class_major: 0x1, + old_up_class_major: 0x2, + parent_class_id: TcHandle::from_u32(0x10034), + up_parent_class_id: TcHandle::from_u32(0x20034), + class_major: 0x1, + up_class_major: 0x2, + down_qdisc_handle: Some(0x9000), + up_qdisc_handle: Some(0x9001), + old_down_min: 1.0, + old_down_max: 10.0, + old_up_min: 1.0, + old_up_max: 10.0, + new_down_min: 1.0, + new_down_max: 20.0, + new_up_min: 1.0, + new_up_max: 20.0, + old_minor: 0x21, + shadow_minor: 0x2000, + final_minor: 0x35, + ips: vec!["192.0.2.92/32".to_string()], + sqm_override: None, + desired_cmd: mk_test_circuit(9002, 0x10034, 0x20034, 0x35, 0x1, 0x2, "192.0.2.92/32"), + stage: MigrationStage::BuildFinal, + }; + let result = ExecuteResult { + ok: false, + duration_ms: 1, + failure_summary: Some("RTNETLINK answers: Invalid argument".to_string()), + }; + + let advanced = migration_stage_apply_succeeded( + &mut migration, + "live-move: build final", + &result, + MigrationStage::SwapToFinal, + ); + + assert!(!advanced); + assert_eq!(migration.stage, MigrationStage::Done); + let reason = bakery_reload_required_reason().expect("reload required reason should be set"); + assert!(reason.contains("9002")); + assert!(reason.contains("live-move: build final")); + clear_reload_required( + "reset after migration_stage_failure_marks_reload_required_and_stops_migration", + ); + } + + #[test] + fn migration_stage_success_advances_without_reload_required() { + clear_reload_required( + "reset before migration_stage_success_advances_without_reload_required", + ); + let mut migration = Migration { + circuit_hash: 9003, + old_parent_class_id: TcHandle::from_u32(0x10020), + old_up_parent_class_id: TcHandle::from_u32(0x20020), + old_class_major: 0x1, + old_up_class_major: 0x2, + parent_class_id: TcHandle::from_u32(0x10034), + up_parent_class_id: TcHandle::from_u32(0x20034), + class_major: 0x1, + up_class_major: 0x2, + down_qdisc_handle: Some(0x9000), + up_qdisc_handle: Some(0x9001), + old_down_min: 1.0, + old_down_max: 10.0, + old_up_min: 1.0, + old_up_max: 10.0, + new_down_min: 1.0, + new_down_max: 20.0, + new_up_min: 1.0, + new_up_max: 20.0, + old_minor: 0x21, + shadow_minor: 0x2000, + final_minor: 0x35, + ips: vec!["192.0.2.93/32".to_string()], + sqm_override: None, + desired_cmd: mk_test_circuit(9003, 0x10034, 0x20034, 0x35, 0x1, 0x2, "192.0.2.93/32"), + stage: MigrationStage::BuildFinal, + }; + let result = ExecuteResult { + ok: true, + duration_ms: 1, + failure_summary: None, + }; + + let advanced = migration_stage_apply_succeeded( + &mut migration, + "live-move: build final", + &result, + MigrationStage::SwapToFinal, + ); + + assert!(advanced); + assert_eq!(migration.stage, MigrationStage::SwapToFinal); + assert!(bakery_reload_required_reason().is_none()); + } + + #[test] + fn test_fault_once_matches_selector_and_clears_file() { + let dir = std::env::temp_dir(); + let path = dir.join(format!( + "lqos-bakery-fault-{}.txt", + SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time") + .as_nanos() + )); + std::fs::write(&path, "migrating circuits between parent nodes").expect("fault file"); + + let summary = consume_test_fault_once_from_path( + &path, + "migrating circuits between parent nodes (fallback)", + ) + .expect("fault should trigger"); + assert!(summary.contains("synthetic Bakery test fault")); + assert!(!path.exists()); + } + + #[test] + fn test_fault_once_ignores_non_matching_selector() { + let dir = std::env::temp_dir(); + let path = dir.join(format!( + "lqos-bakery-fault-{}.txt", + SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time") + .as_nanos() + )); + std::fs::write(&path, "TreeGuard runtime circuit reparent").expect("fault file"); + + let summary = consume_test_fault_once_from_path( + &path, + "migrating circuits between parent nodes (fallback)", + ); + assert!(summary.is_none()); + assert!(path.exists()); + let _ = std::fs::remove_file(&path); + } + #[test] fn queue_top_level_runtime_migration_succeeds_for_inactive_parent_move() { let old_cmd = mk_test_circuit(9200, 0x10020, 0x20020, 0x21, 0x1, 0x2, "192.0.2.92/32"); @@ -7419,8 +8045,9 @@ mod tests { assert_ne!(migration.shadow_minor, migration.old_minor); assert!(matches!( circuits.get(&9200), - Some(cmd) if Arc::ptr_eq(cmd, &new_cmd) + Some(cmd) if Arc::ptr_eq(cmd, &old_cmd) )); + assert!(Arc::ptr_eq(&migration.desired_cmd, &new_cmd)); } #[test] diff --git a/src/rust/lqos_bakery/src/utils.rs b/src/rust/lqos_bakery/src/utils.rs index 5639b293..c58b45e0 100644 --- a/src/rust/lqos_bakery/src/utils.rs +++ b/src/rust/lqos_bakery/src/utils.rs @@ -54,6 +54,26 @@ fn run_tc_batch(path: &Path, purpose: &str) -> Result Option { + let stderr = String::from_utf8_lossy(&output.stderr); + let stderr = stderr.trim(); + + if output.status.success() { + return (!stderr.is_empty()).then(|| stderr.to_string()); + } + + let status_summary = match output.status.code() { + Some(code) => format!("tc batch exited with status {code}"), + None => "tc batch terminated by signal".to_string(), + }; + + if stderr.is_empty() { + Some(status_summary) + } else { + Some(format!("{status_summary}: {stderr}")) + } +} + pub(crate) fn read_memory_snapshot() -> Result { let raw = std::fs::read_to_string("/proc/meminfo") .map_err(|e| format!("Failed to read /proc/meminfo: {e}"))?; @@ -348,13 +368,12 @@ where error!("Command output for ({purpose}): {:?}", output_str.trim()); } - let error_str = String::from_utf8_lossy(&output.stderr); - if !error_str.is_empty() { + if let Some(failure_summary) = summarize_tc_batch_failure(&output) { let numbered = format_numbered_lines(&lines, global_line_start); let chunk_line_end = global_line_start + chunk.len().saturating_sub(1); let detailed = format!( "Command error for ({purpose}): {}\nFailed chunk {}/{} (global lines {}-{})\nFull batch: {}\nChunk batch: {}\nChunk commands with global line numbers:\n{}", - error_str.trim(), + failure_summary, completed_chunks + 1, total_chunks, global_line_start, @@ -388,7 +407,7 @@ where return ExecuteResult { ok: false, duration_ms: started.elapsed().as_millis() as u64, - failure_summary: Some(error_str.trim().to_string()), + failure_summary: Some(failure_summary), }; } @@ -483,6 +502,15 @@ pub(crate) fn write_command_file(path: &Path, commands: &[Vec]) -> Optio #[cfg(test)] mod tests { use super::*; + use std::os::unix::process::ExitStatusExt; + + fn mock_tc_output(status: i32, stdout: &str, stderr: &str) -> std::process::Output { + std::process::Output { + status: std::process::ExitStatus::from_raw(status << 8), + stdout: stdout.as_bytes().to_vec(), + stderr: stderr.as_bytes().to_vec(), + } + } #[test] fn parse_live_qdisc_handle_majors_collects_non_zero_handles() { @@ -554,4 +582,25 @@ MemFree: 1024000 kB let err = parse_memory_snapshot(raw).expect_err("memavailable should be required"); assert!(err.contains("MemAvailable")); } + + #[test] + fn summarize_tc_batch_failure_rejects_nonzero_exit_without_stderr() { + let output = mock_tc_output(1, "", ""); + let summary = summarize_tc_batch_failure(&output).expect("nonzero exit should fail"); + assert!(summary.contains("status 1")); + } + + #[test] + fn summarize_tc_batch_failure_includes_stderr_with_exit_status() { + let output = mock_tc_output(2, "", "RTNETLINK answers: Invalid argument\n"); + let summary = summarize_tc_batch_failure(&output).expect("stderr failure should be kept"); + assert!(summary.contains("status 2")); + assert!(summary.contains("Invalid argument")); + } + + #[test] + fn summarize_tc_batch_failure_accepts_clean_success() { + let output = mock_tc_output(0, "", ""); + assert!(summarize_tc_batch_failure(&output).is_none()); + } } diff --git a/src/rust/lqos_config/src/network_json.rs b/src/rust/lqos_config/src/network_json.rs index ca327a62..f6c4225a 100644 --- a/src/rust/lqos_config/src/network_json.rs +++ b/src/rust/lqos_config/src/network_json.rs @@ -643,8 +643,10 @@ mod test { .find(|n| n.name == "Bad Tower") .expect("Bad Tower must be present"); - assert!((good.latitude.unwrap() - 45.123).abs() < 0.001); - assert!((good.longitude.unwrap() + 111.75).abs() < 0.001); + let good_latitude = good.latitude.expect("Tower A latitude must parse"); + let good_longitude = good.longitude.expect("Tower A longitude must parse"); + assert!((good_latitude - 45.123).abs() < 0.001); + assert!((good_longitude + 111.75).abs() < 0.001); assert_eq!(bad.latitude, None); assert_eq!(bad.longitude, None); diff --git a/src/rust/lqos_config/src/planner.rs b/src/rust/lqos_config/src/planner.rs index fe1c832b..33c38573 100644 --- a/src/rust/lqos_config/src/planner.rs +++ b/src/rust/lqos_config/src/planner.rs @@ -16,22 +16,17 @@ pub struct TopLevelPlannerItem { } /// Supported planner strategies. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)] pub enum TopLevelPlannerMode { /// Deterministic round-robin assignment by item id. RoundRobin, /// Deterministic greedy balancing with no hysteresis or cooldown. Greedy, /// Greedy balancing that prefers to keep prior assignments unless a move is worthwhile. + #[default] StableGreedy, } -impl Default for TopLevelPlannerMode { - fn default() -> Self { - Self::StableGreedy - } -} - /// Tunables for top-level queue planning. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct TopLevelPlannerParams { diff --git a/src/rust/lqos_python/src/lib.rs b/src/rust/lqos_python/src/lib.rs index fb9ef326..d8286d83 100644 --- a/src/rust/lqos_python/src/lib.rs +++ b/src/rust/lqos_python/src/lib.rs @@ -127,6 +127,7 @@ fn get_bool(d: &pyo3::Bound<'_, pyo3::types::PyDict>, key: &str, default: bool) hysteresis_threshold=None ) )] +#[allow(clippy::too_many_arguments)] fn plan_top_level_cpu_bins( py: Python, items: PyObject, diff --git a/src/rust/lqos_stormguard/src/site_state.rs b/src/rust/lqos_stormguard/src/site_state.rs index b8af7da3..c48c1915 100644 --- a/src/rust/lqos_stormguard/src/site_state.rs +++ b/src/rust/lqos_stormguard/src/site_state.rs @@ -1019,7 +1019,7 @@ mod tests { fn site_override_update_keeps_only_changed_directions() { let site = site_state(75, 50, 100, 50); let update = SiteStateTracker::site_override_update_from_state(&site); - assert_eq!(update.download_bandwidth_mbps, Some(75)); + assert_eq!(update.download_bandwidth_mbps, Some(75.0)); assert_eq!(update.upload_bandwidth_mbps, None); } diff --git a/src/rust/lqosd/src/main.rs b/src/rust/lqosd/src/main.rs index d6c91c45..6178fcbf 100644 --- a/src/rust/lqosd/src/main.rs +++ b/src/rust/lqosd/src/main.rs @@ -37,8 +37,6 @@ use crate::{ ip_mapping::{clear_ip_flows, del_ip_flow, list_mapped_ips, map_ip_to_flow}, throughput_tracker::flow_data::{FlowActor, flowbee_handle_events, setup_netflow_tracker}, }; -#[cfg(feature = "flamegraphs")] -use allocative::Allocative; use anyhow::Result; use lqos_bus::{BusRequest, BusResponse, InsightLicenseSummary, UnixSocketServer}; use lqos_heimdall::{n_second_packet_dump, perf_interface::heimdall_handle_events, start_heimdall}; @@ -65,12 +63,6 @@ use crate::blackboard::{BLACKBOARD_SENDER, BlackboardCommand}; use crate::lts2_sys::get_lts_license_status; use crate::lts2_sys::shared_types::LtsStatus; use crate::remote_commands::start_remote_commands; -#[cfg(feature = "flamegraphs")] -use crate::shaped_devices_tracker::NETWORK_JSON; -#[cfg(feature = "flamegraphs")] -use crate::throughput_tracker::THROUGHPUT_TRACKER; -#[cfg(feature = "flamegraphs")] -use crate::throughput_tracker::flow_data::{ALL_FLOWS, RECENT_FLOWS}; use lqos_stormguard::{STORMGUARD_DEBUG, STORMGUARD_STATS}; use tracing::level_filters::LevelFilter; // Use MiMalloc only on supported platforms diff --git a/src/rust/lqosd/src/throughput_tracker/mod.rs b/src/rust/lqosd/src/throughput_tracker/mod.rs index f6ab1f62..09b941d6 100644 --- a/src/rust/lqosd/src/throughput_tracker/mod.rs +++ b/src/rust/lqosd/src/throughput_tracker/mod.rs @@ -1358,6 +1358,7 @@ mod compatibility_tests { use super::Lts2Circuit; use serde::Deserialize; + #[allow(dead_code)] #[derive(Debug, Deserialize)] struct OldLts2Device { device_id: String, @@ -1369,6 +1370,7 @@ mod compatibility_tests { comment: String, } + #[allow(dead_code)] #[derive(Debug, Deserialize)] struct OldLts2Circuit { circuit_id: String, diff --git a/src/rust/lqosd/src/treeguard/actor.rs b/src/rust/lqosd/src/treeguard/actor.rs index d34e15a2..f8bb58ab 100644 --- a/src/rust/lqosd/src/treeguard/actor.rs +++ b/src/rust/lqosd/src/treeguard/actor.rs @@ -2805,6 +2805,7 @@ mod tests { use std::sync::Arc; use std::sync::Once; + #[allow(clippy::too_many_arguments)] fn pending_link_decision( node_name: &str, node_index: usize, @@ -2846,8 +2847,8 @@ mod tests { assert!(paused); assert_eq!(tick_seconds, 5); - assert_eq!(status.enabled, true); - assert_eq!(status.dry_run, false); + assert!(status.enabled); + assert!(!status.dry_run); assert_eq!( status.last_action_summary.as_deref(), Some("Paused while Bakery full reload in progress") @@ -3288,8 +3289,10 @@ mod tests { state.down.idle_since_unix = Some(0); state.up.idle_since_unix = Some(0); - let mut circuits_cfg = lqos_config::TreeguardCircuitsConfig::default(); - circuits_cfg.persist_sqm_overrides = false; + let circuits_cfg = lqos_config::TreeguardCircuitsConfig { + persist_sqm_overrides: false, + ..lqos_config::TreeguardCircuitsConfig::default() + }; let mut circuit_change_budget_remaining = 1usize; let mut deferred_circuit_sqm_changes = 0usize; @@ -3593,8 +3596,8 @@ mod tests { .join() .expect("system stats responder should join"); - assert_eq!(status.enabled, true); - assert_eq!(status.dry_run, false); + assert!(status.enabled); + assert!(!status.dry_run); assert_eq!(status.managed_circuits, 1); assert_eq!(status.fq_codel_circuits, 1); assert_eq!( -- knotmirror 0.0.1