From 26351f8b3bb2c7782bf2fb3bdaf11a6d4690b972 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 9 Oct 2026 14:43:24 +0800 Subject: [PATCH] Fix Fabric device-filtered route readback --- docs/VALIDATION.md | 26 +++ .../tests/test_fabric_first_deploy_entry.py | 195 +++++++++++++++++- engine/tests/test_fabric_route_readback.py | 142 ++++++++++++- engine/virsh_panel/fabric.py | 76 +++++-- 4 files changed, 412 insertions(+), 27 deletions(-) diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index 66194c08..e8446033 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -1,5 +1,31 @@ # 本版验证记录 +## 2026-10-09:真实过滤路由输出省略设备字段 + +- 后台在线更新与面板 SSH 均确认 `b11856d` 已部署且 Engine 已重启;fresh + 检测通过后只对固定现有方案执行一轮重新核对。Debian 13 与 AlmaLinux 10 + 都推进到 `final_readback` 后失败,不能将能力检测通过等同 Fabric 可用。 +- Alma 实际 `ip -o -4 route show table all dev wg-fabric0` 返回 peer host route + 和自身 local route,但省略每行的 `dev` 字段。只读复现确认原解析把 device + 置空并拒绝合法路由。这是查询/输出契约缺陷,不是缺少 chmod 执行位。 +- [iproute2 官方源码](https://github.com/iproute2/iproute2/blob/v6.17.0/ip/iproute.c) + 中接口过滤设置 `oifmask=-1`,序列化时故意省略 device。因此只有对应的 + `dev` 过滤查询允许从其固定查询范围补回设备;无过滤查询仍要求显式设备, + 显式错误/重复/畸形设备和无 OIF 的非普通路由不能被推定为受管接口。 +- 当前现场保留事务及紧急隔离,不清空 nft、删除 journal 或关闭防火墙。 + 本段记录定位证据;修复后的真实双向 overlay 和重启验收仍须单独完成。 +- 稳定源码分组回归:Fabric/proc/nft/NM/systemd 七套 **620 passed、9 skipped**; + route/Engine boundary/VPC/dataplane 四套 **454 passed、23 warnings**。 + 新 route 用例覆盖无设备字段、显式设备漂移、无 OIF 特殊路由、精确 local + 形状,以及未过滤查询不推断设备。渲染 shell、独立交叉审查、py_compile 与 + diff 检查通过;Darwin 的 Linux proc 跳过边界不变。 +- 完整首次部署 helper 入口 **33 passed**,新增 12 项覆盖 `ready/refreshed`、 + 临时/持久 helper、已创建 UP/trusted 接口和已发布 NM/ifupdown profile 的 + 精确恢复;错误快照权限/digest 与同 marker 篡改 verdict 保留隔离并拒绝。 +- 独立静态审查发现 VPC bridge 重复 apply 与 QGA guest JSON route 探测中还有 + 同类依赖过滤输出 `dev` 的问题。本次只修 Fabric apply/BOOT/observe,另两项 + 尚未修复,不能据此宣称租户 VPC 的完整接入、重试与 guest 验收已通过。 + ## 2026-10-09:越过 nft 安装后发现的目录与回读兼容问题 - `6a99181` 已通过后台在线更新部署,SSH 确认 Engine 为新进程;新一轮真实 diff --git a/engine/tests/test_fabric_first_deploy_entry.py b/engine/tests/test_fabric_first_deploy_entry.py index 5ed49f42..764340da 100644 --- a/engine/tests/test_fabric_first_deploy_entry.py +++ b/engine/tests/test_fabric_first_deploy_entry.py @@ -15,6 +15,7 @@ from virsh_panel.fabric import ( FIREWALL_MODE_FIREWALLD, _apply_script, _ifupdown_fragment, + _nm_profile_template, _nft_rules_text, _sysctl_text, parse_fabric_plan, @@ -102,6 +103,11 @@ elif name == "ip": up.touch() elif args == ["link", "set", "dev", "wg-fabric0", "down"]: up.unlink(missing_ok=True) + elif args == ["link", "del", "dev", "wg-fabric0"]: + present.unlink(missing_ok=True) + up.unlink(missing_ok=True) + (root / "proc" / "ipv4" / "conf" / "wg-fabric0" / + "rp_filter").unlink(missing_ok=True) else: raise SystemExit(1) elif name == "nft": @@ -151,15 +157,50 @@ elif name == "firewall-cmd": elif "--get-zones" in args: print("public trusted") elif any(arg.startswith("--query-interface=") for arg in args): - raise SystemExit(1) + zone = root / "runtime-zone" + wanted = next(arg.split("=", 1)[1] for arg in args + if arg.startswith("--zone=")) + raise SystemExit(0 if "--permanent" not in args and zone.is_file() + and zone.read_text().strip() == wanted else 1) + elif args == ["--zone=trusted", "--remove-interface=wg-fabric0"]: + (root / "runtime-zone").unlink(missing_ok=True) elif any(arg.startswith("--query-rich-rule=") for arg in args): raise SystemExit(1) else: raise SystemExit(2) elif name == "nmcli": + profile = (root / "NetworkManager" / "system-connections" / + "virt-cloud-wg-fabric0.nmconnection") + identity = root / "nm-loaded-uuid" + uuid = identity.read_text().strip() if identity.is_file() else "" if args == ["-t", "--escape", "no", "-g", "connection.uuid", "connection", "show", "virt-cloud-wg-fabric0"]: - pass + if uuid: + print(uuid) + elif args == ["-t", "--escape", "no", "-f", "NAME,UUID", + "connection", "show"] and profile.is_file(): + print("virt-cloud-wg-fabric0:" + uuid) + elif args == ["-t", "--escape", "no", "-f", "UUID,FILENAME", + "connection", "show"] and profile.is_file(): + print(uuid + ":" + str(profile)) + elif args == ["-t", "--escape", "no", "-g", "GENERAL.CON-UUID", + "device", "show", "wg-fabric0"]: + if (root / "link-present").exists() and not (root / "nm-unmanaged").exists(): + print(uuid) + elif args == ["-t", "--escape", "no", "-g", "GENERAL.NM-MANAGED", + "device", "show", "wg-fabric0"]: + print("no" if (root / "nm-unmanaged").exists() else "yes") + elif args == ["device", "set", "wg-fabric0", "managed", "no"]: + (root / "nm-unmanaged").touch() + elif args == ["-t", "--escape", "no", "-g", "connection.zone", + "connection", "show", "uuid", uuid] and profile.is_file(): + zone = root / "nm-zone" + print(zone.read_text().strip() if zone.is_file() else "") + elif args == ["connection", "modify", "--temporary", "uuid", uuid, + "connection.zone", ""] and profile.is_file(): + (root / "nm-zone").write_text("") + elif args == ["connection", "delete", "uuid", uuid] and uuid: + identity.unlink() else: raise SystemExit(2) elif name in ("wg", "bridge", "flock", "sync", "ifup", "ifquery", "ifdown"): @@ -255,7 +296,8 @@ def _fixture(tmp_path: Path, mode: str): return plan, helper_path, base, runtime, roots -def _journal(plan, base: Path, mode: str, helper: Path, root: Path): +def _journal(plan, base: Path, mode: str, helper: Path, root: Path, *, + nft_phase: str = "installing"): journal = base / ".wg-fabric-txn" journal.mkdir(mode=0o700) # Read the rendered UUID; the fixture must not invent an independent plan. @@ -286,7 +328,7 @@ def _journal(plan, base: Path, mode: str, helper: Path, root: Path): f"{key}={value}\n" for key, value in metadata.items() )) _write(journal / "state", "ready\n") - _write(journal / "nft.phase", "installing\n") + _write(journal / "nft.phase", nft_phase + "\n") _write(journal / "nft.table", "") for name in ("conf", "owner", "helper", "unit", "repair", "timer", "gate_generator", "nm_profile", "ifupdown_fragment", @@ -298,6 +340,9 @@ def _journal(plan, base: Path, mode: str, helper: Path, root: Path): desired = _nft_rules_text(plan.digest) _write(journal / "nft_rules.desired", desired) _write(root / "tables" / "inet--virt_cloud_wg_fabric", desired) + if nft_phase == "refreshed": + _write(journal / "nft.installed", desired) + _write(journal / "nft.applied", desired) return journal @@ -339,6 +384,148 @@ def test_first_deploy_complete_entry_recovers_absent_generator_and_timer( ("start", "enable") for call in calls) +@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"]) +@pytest.mark.parametrize("helper_published", [False, True]) +def test_ready_refreshed_complete_entry_recovers_exact_first_deploy_snapshots( + tmp_path, mode, helper_published): + plan, helper, base, runtime, roots = _fixture(tmp_path, mode) + journal = _journal(plan, base, mode, helper, tmp_path, + nft_phase="refreshed") + if helper_published: + # The real first-deploy apply may publish its persistent helper before + # failing final readback. Recovery must run that helper's full entry, + # then use the cleanup_helper phase to safely remove its exact image. + content = helper.read_text(encoding="utf-8") + _write(journal / "helper.desired", content, 0o700) + helper = (roots["/usr/local/libexec"] / "virt-cloud-wg-fabric") + _write(helper, content, 0o700) + result = _run(helper, tmp_path, ( + "--recover-only" if helper_published else "--recover-current" + )) + assert result.returncode == 0, result.stderr + assert not journal.exists() + if helper_published: + assert not helper.exists() + assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists() + assert (base / "wg-fabric0.key").read_text(encoding="utf-8") == ( + "test-private-key-snapshot\n") + assert (runtime / "wg-fabric-emergency.owner").is_file() + for family in ("inet", "bridge"): + assert (tmp_path / "tables" / + f"{family}--virt_cloud_fabric_emergency").is_file() + calls = [json.loads(line) for line in + (tmp_path / "calls").read_text(encoding="utf-8").splitlines()] + assert any(call[0] == "nft" and call[1] == "-f" for call in calls) + assert not any(call[0] == "systemctl" and call[1] in + ("start", "enable") for call in calls) + assert not any(call[:5] == ["ip", "link", "set", "dev", "wg-fabric0"] + and call[-1] == "up" for call in calls) + + +@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"]) +def test_ready_refreshed_persistent_entry_recovers_created_trusted_link( + tmp_path, mode): + plan, helper, base, runtime, roots = _fixture(tmp_path, mode) + journal = _journal(plan, base, mode, helper, tmp_path, + nft_phase="refreshed") + content = helper.read_text(encoding="utf-8") + _write(journal / "helper.desired", content, 0o700) + helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric" + _write(helper, content, 0o700) + + def publish(path: Path, name: str, value: str, permissions=0o600): + _write(journal / (name + ".desired"), value, permissions) + _write(path, value, permissions) + + publish(base / "wg-fabric0.owner", "owner", "virt-cloud-wg-fabric-v1\n") + if mode == "networkmanager_nmcli": + uuid = re.search(r'^CONNECTION_UUID="([^"]+)"$', content, + re.MULTILINE).group(1) + profile = _nm_profile_template( + plan, plan.nodes[0], uuid, FIREWALL_MODE_FIREWALLD, + ) + publish(roots["/etc/NetworkManager"] / "system-connections" / + "virt-cloud-wg-fabric0.nmconnection", "nm_profile", profile) + _write(tmp_path / "nm-loaded-uuid", uuid + "\n") + _write(tmp_path / "nm-zone", "trusted\n") + else: + fragment = _ifupdown_fragment(plan, plan.nodes[0]).replace( + "/usr/local/libexec", str(roots["/usr/local/libexec"]), + ) + publish(roots["/etc/network"] / "interfaces.d" / + "virt-cloud-wg-fabric0", "ifupdown_fragment", fragment) + _write(tmp_path / "link-present", "") + _write(tmp_path / "link-up", "") + _write(tmp_path / "runtime-zone", "trusted\n") + rpf = roots["/proc/sys/net/ipv4"] / "conf" / "wg-fabric0" / "rp_filter" + _write(rpf, "2\n") + + result = _run(helper, tmp_path, "--recover-only") + assert result.returncode == 0, result.stderr + assert not journal.exists() + assert not helper.exists() + assert not (tmp_path / "link-present").exists() + assert not (tmp_path / "link-up").exists() + assert not (tmp_path / "runtime-zone").exists() + assert not rpf.exists() + assert not (base / "wg-fabric0.owner").exists() + assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists() + assert (runtime / "wg-fabric-emergency.owner").is_file() + for family in ("inet", "bridge"): + assert (tmp_path / "tables" / + f"{family}--virt_cloud_fabric_emergency").is_file() + calls = [json.loads(line) for line in + (tmp_path / "calls").read_text(encoding="utf-8").splitlines()] + assert ["ip", "link", "del", "dev", "wg-fabric0"] in calls + assert ["firewall-cmd", "--zone=trusted", + "--remove-interface=wg-fabric0"] in calls + if mode == "networkmanager_nmcli": + assert ["nmcli", "device", "set", "wg-fabric0", "managed", "no"] in calls + assert ["nmcli", "connection", "delete", "uuid", uuid] in calls + assert not any(call[0] == "systemctl" and call[1] in + ("start", "enable") for call in calls) + + +@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"]) +@pytest.mark.parametrize("failure", ["live_verdict", "applied_digest", "applied_mode"]) +def test_ready_refreshed_persistent_entry_rejects_unproved_post_images( + tmp_path, mode, failure): + plan, helper, base, runtime, roots = _fixture(tmp_path, mode) + journal = _journal(plan, base, mode, helper, tmp_path, + nft_phase="refreshed") + content = helper.read_text(encoding="utf-8") + _write(journal / "helper.desired", content, 0o700) + helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric" + _write(helper, content, 0o700) + live = tmp_path / "tables" / "inet--virt_cloud_wg_fabric" + if failure == "live_verdict": + _write(live, live.read_text(encoding="utf-8").replace( + "counter drop comment", "counter accept comment", 1, + )) + elif failure == "applied_digest": + applied = journal / "nft.applied" + _write(applied, applied.read_text(encoding="utf-8").replace( + plan.digest, "f" * 64, + )) + else: + (journal / "nft.applied").chmod(0o644) + before = live.read_bytes() + result = _run(helper, tmp_path, "--recover-only") + assert result.returncode == 79, result.stderr + assert journal.is_dir() + assert helper.read_text(encoding="utf-8") == content + assert live.read_bytes() == before + assert (runtime / "wg-fabric-emergency.owner").is_file() + for family in ("inet", "bridge"): + assert (tmp_path / "tables" / + f"{family}--virt_cloud_fabric_emergency").is_file() + calls = [json.loads(line) for line in + (tmp_path / "calls").read_text(encoding="utf-8").splitlines()] + assert not any(call[0] == "nft" and call[1] == "-f" for call in calls) + assert not any(call[0] == "systemctl" and call[1] in + ("start", "enable") for call in calls) + + def _transition_fixture(tmp_path: Path, gate_state: str): plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown") _write(base / "wg-fabric0.owner", "virt-cloud-wg-fabric-v1\n") diff --git a/engine/tests/test_fabric_route_readback.py b/engine/tests/test_fabric_route_readback.py index 7ac03125..e3e0f092 100644 --- a/engine/tests/test_fabric_route_readback.py +++ b/engine/tests/test_fabric_route_readback.py @@ -92,15 +92,19 @@ OWN_ROUTE = ("local 100.64.1.1 dev wg-fabric0 table local proto kernel " @pytest.mark.parametrize("variant", EXTRA_VARIANTS) @pytest.mark.parametrize("prefix", ["100.64.1.2", "100.64.1.2/32"]) -def test_real_host_route_and_owned_local_route_are_equivalent(variant, prefix): - routes = f"{prefix} dev wg-fabric0 scope link\n" + OWN_ROUTE +@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""]) +def test_real_host_route_and_owned_local_route_are_equivalent( + variant, prefix, device_field): + routes = f"{prefix}{device_field} scope link metric 50\n" + OWN_ROUTE.replace( + " dev wg-fabric0", device_field) assert _run_route(variant, routes) == "" @pytest.mark.parametrize("variant", ["apply_seen", "boot_seen"]) @pytest.mark.parametrize("prefix", ["100.64.1.2", "100.64.1.2/32"]) -def test_real_host_route_positive_readback(variant, prefix): - assert _run_route(variant, f"{prefix} dev wg-fabric0 scope link\n") == "1" +@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""]) +def test_real_host_route_positive_readback(variant, prefix, device_field): + assert _run_route(variant, f"{prefix}{device_field} scope link\n") == "1" assert _run_route(variant, OWN_ROUTE) == "" @@ -111,11 +115,28 @@ def test_real_host_route_positive_readback(variant, prefix): "100.64.1.02 dev wg-fabric0 scope link", "100.64.1.256 dev wg-fabric0 scope link", "100.64.1.2 dev br0 scope link", + "100.64.1.2 scope link dev", + "100.64.1.2 dev br0 dev wg-fabric0 scope link", + "100.64.1.2 dev wg-fabric0 dev wg-fabric0 scope link", "100.64.1.2 dev wg-fabric0 table 100 scope link", "local 100.64.1.1 dev br0 table local proto kernel scope host", "local 100.64.1.1 dev wg-fabric0 table 100 proto kernel scope host", "default dev wg-fabric0", "100.64.1.2 dev wg-fabric0\n100.64.1.2/32 dev wg-fabric0", + "blackhole 100.64.1.2", + "unreachable 100.64.1.2", + "prohibit 100.64.1.2", + "throw 100.64.1.2", + "broadcast 100.64.1.2 table local proto kernel scope link", + "100.64.1.2 nhid 123 scope link metric 50", + "100.64.1.2 nexthop dev wg-fabric0 weight 1", + "100.64.1.2 encap mpls 100 dev wg-fabric0", + "local 100.64.1.1 table main proto kernel scope host src 100.64.1.1", + "local 100.64.1.1 table local proto static scope host src 100.64.1.1", + "local 100.64.1.1 table local proto kernel scope link src 100.64.1.1", + "local 100.64.1.1 table local proto kernel scope host src 100.64.1.3", + "local 100.64.1.1 table local proto kernel scope host", + "local 100.64.1.1 table local scope host src 100.64.1.1", ]) def test_host_canonicalization_keeps_foreign_and_duplicate_routes_blocked( variant, foreign): @@ -123,13 +144,16 @@ def test_host_canonicalization_keeps_foreign_and_duplicate_routes_blocked( @pytest.mark.parametrize("variant", EXTRA_VARIANTS) -def test_vpc_host_route_requires_exact_owned_tuple_and_protocol(tmp_path, variant): +@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""]) +def test_vpc_host_route_requires_exact_owned_tuple_and_protocol( + tmp_path, variant, device_field): vpc_file = tmp_path / "routes" vpc_file.write_text( "10.1.0.3/32|wg-fabric0|100|100.64.1.2|100\n", encoding="utf-8") normal = "100.64.1.2 dev wg-fabric0 scope link\n" + OWN_ROUTE vpc = ("10.1.0.3 via 100.64.1.2 dev wg-fabric0 table 100 " - "proto 186 metric 100 scope link\n") + "proto 186 metric 100 scope link\n").replace( + " dev wg-fabric0", device_field) assert _run_route(variant, normal + vpc, vpc_file) == "" for changed in (vpc.replace("proto 186", "proto static"), vpc.replace("metric 100", "metric 101"), @@ -181,19 +205,25 @@ def test_boot_nft_whitespace_fix_keeps_complete_ownership_validation(drift): assert result.returncode != 0 -def test_observe_route_awk_runs_begin_outside_record_action(tmp_path): +def _observe_route_program(): script = _observe_script(("100.64.1.2",), "100.64.1.1", endpoint_ips=("203.0.113.2",), deployment_mode="ifupdown", firewall_mode=FIREWALL_MODE_FIREWALLD) start = script.index('awk -v vpc_file="${VPC_ROUTE_FILE:-}"') program = script[start:script.index("\n }'", start) + len("\n }'")] - program = program.replace('"${VPC_ROUTE_FILE:-}"', '"$VPC_ROUTE_FILE"') + return program.replace('"${VPC_ROUTE_FILE:-}"', '"$VPC_ROUTE_FILE"') + + +@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""]) +def test_observe_route_awk_runs_begin_outside_record_action(tmp_path, device_field): + program = _observe_route_program() vpc_file = tmp_path / "routes" vpc_file.write_text("10.1.0.3/32|wg-fabric0|100|100.64.1.2|100\n", encoding="utf-8") routes = ("100.64.1.2 dev wg-fabric0 scope link\n" + OWN_ROUTE - + "10.1.0.3 via 100.64.1.2 dev wg-fabric0 table 100 proto 186 metric 100\n") + + "10.1.0.3 via 100.64.1.2 dev wg-fabric0 table 100 proto 186 metric 100\n").replace( + " dev wg-fabric0", device_field) result = subprocess.run(["/bin/sh", "-eu", "-c", program], input=routes, capture_output=True, text=True, check=False, env={"VPC_ROUTE_FILE": str(vpc_file), "VPC_STATE_STATUS": "valid"}) @@ -204,6 +234,100 @@ def test_observe_route_awk_runs_begin_outside_record_action(tmp_path): ] +@pytest.mark.parametrize("device_field", [" dev br0", " dev", " dev br0 dev wg-fabric0"]) +def test_observe_explicit_device_drift_remains_visible_to_readiness(device_field): + result = subprocess.run( + ["/bin/sh", "-eu", "-c", _observe_route_program()], + input="100.64.1.2" + device_field + "\n", capture_output=True, + text=True, check=False, env={"VPC_ROUTE_FILE": "/dev/null"}, + ) + assert result.returncode == 0, result.stderr + assert result.stdout.strip() == ( + "__VC_FABRIC_ROUTE__=100.64.1.2/32|main|__invalid_device__") + + +@pytest.mark.parametrize("route", [ + "blackhole 100.64.1.2", + "unreachable 100.64.1.2", + "prohibit 100.64.1.2", + "throw 100.64.1.2", + "100.64.1.2 nhid 123", + "100.64.1.2 nexthop dev wg-fabric0 weight 1", + "100.64.1.2 encap mpls 100 dev wg-fabric0", +]) +def test_observe_no_oif_route_forms_cannot_bypass_readiness(route): + result = subprocess.run( + ["/bin/sh", "-eu", "-c", _observe_route_program()], + input=route + "\n", capture_output=True, text=True, check=False, + env={"VPC_ROUTE_FILE": "/dev/null"}, + ) + assert result.returncode == 0, result.stderr + assert result.stdout.strip() == ( + "__VC_FABRIC_ROUTE__=100.64.1.2/32|main|__invalid_device__") + + +@pytest.mark.parametrize("variant", ["apply_seen", "boot_seen"]) +@pytest.mark.parametrize("device_field", [" dev br0", " dev", " dev br0 dev wg-fabric0"]) +def test_positive_filtered_readback_rejects_explicit_device_drift(variant, device_field): + assert _run_route(variant, "100.64.1.2" + device_field + "\n") == "" + + +def test_unfiltered_exact_and_overlap_parsers_do_not_infer_device(): + boot_start = _BOOT_HELPER.index( + 'route_conflict=$(printf \'%s\\n\' "$existing_route" | awk') + exact = _assignment(_BOOT_HELPER, "route_conflict", boot_start) + assert "filtered_route_device" not in exact + cases = [("100.64.1.2 scope link\n", "foreign"), + ("100.64.1.2 dev br0 scope link\n", "foreign"), + ("100.64.1.2 dev wg-fabric0 scope link\n", "")] + for routes, expected in cases: + result = subprocess.run( + ["/bin/sh", "-eu", "-c", exact + '\nprintf "%s" "$route_conflict"'], + env={"existing_route": routes, "IFACE": "wg-fabric0"}, + capture_output=True, text=True, check=False, + ) + assert result.returncode == 0, result.stderr + assert result.stdout == expected + + apply = _render_apply() + start = apply.index("route_overlaps_target() {") + end = apply.index("\nfor route in $MANAGED_ROUTES", start) + overlap = apply[start:end] + assert "filtered_route_device" not in overlap + capture = 'mock_ip() { printf "%s\\n" "$VC_ROUTES"; }; IP=mock_ip\n' + for routes, expected in cases: + result = subprocess.run( + ["/bin/sh", "-eu", "-c", capture + overlap + "\nroute_overlaps_target 100.64.1.2/32"], + env={"VC_ROUTES": routes, "IFACE": "wg-fabric0", + "OLD_MANAGED_ROUTES": "100.64.1.2/32"}, + capture_output=True, text=True, check=False, + ) + assert result.returncode == 0, result.stderr + assert result.stdout.strip() == ("conflict" if expected else "") + + +@pytest.mark.parametrize("route,expected", [ + ("10.1.0.3 table 100 proto 186 metric 100", 0), + ("10.1.0.3 dev vcv-01234567890 table 100 proto 186 metric 100", 0), + ("10.1.0.3 dev br0 table 100 proto 186 metric 100", 1), + ("10.1.0.3 table 101 proto 186 metric 100", 1), + ("10.1.0.3 table 100 proto static metric 100", 1), + ("blackhole 10.1.0.3 table 100 proto 186 metric 100", 1), + ("10.1.0.3 nhid 123 table 100 proto 186 metric 100", 1), +]) +def test_boot_vpc_bridge_filtered_inventory_keeps_table_proto_device_fences(route, expected): + start = _BOOT_HELPER.index('printf \'%s\\n\' "$vpc_bridge_routes" | awk') + end = _BOOT_HELPER.index("' || exit 71", start) + 1 + program = _BOOT_HELPER[start:end] + result = subprocess.run( + ["/bin/sh", "-eu", "-c", program], + env={"vpc_bridge_routes": route, "vpc_table": "100", + "vpc_bridge": "vcv-01234567890"}, + capture_output=True, text=True, check=False, + ) + assert result.returncode == expected, result.stderr + + @pytest.mark.parametrize("mode", ["ifupdown", "networkmanager_nmcli"]) def test_apply_and_boot_replace_the_same_route_priority_as_selected_manager(mode): script = _render_apply(mode) diff --git a/engine/virsh_panel/fabric.py b/engine/virsh_panel/fabric.py index 64811ed2..eb3c5b58 100644 --- a/engine/virsh_panel/fabric.py +++ b/engine/virsh_panel/fabric.py @@ -1974,6 +1974,54 @@ function canonical_host_prefix(value, octet, count, pos) { } return value "/32" } +function route_ipv4_prefix(value, parts, octet, count, pos) { + count=split(value, parts, "/") + if (count > 2 || (count == 2 && + (parts[2] !~ /^(0|[1-9][0-9]*)$/ || parts[2] > 32))) return 0 + if (split(parts[1], octet, ".") != 4) return 0 + for (pos=1; pos<=4; pos++) { + if (octet[pos] !~ /^(0|[1-9][0-9]*)$/ || octet[pos] > 255) return 0 + } + return 1 +} +function filtered_route_device(iface, own, pos, seen, local_route, local_table, + local_proto, local_scope, local_src) { + # ONLY for `ip route show ... dev IFACE`: iproute2 deliberately suppresses + # the already-selected output device. Never use this inference for an + # unfiltered inventory or route-get result. Its filter does not guarantee + # an OIF on special routes, nhid objects or multipath records: reject those + # before inference. Only ordinary IPv4 unicast and the exact own kernel + # local-address record may use the selected device. + local_route=($1 == "local") + if (local_route) { + if (own == "" || !route_ipv4_prefix($2) || + canonical_host_prefix($2) != canonical_host_prefix(own)) + return "__invalid_device__" + } else if (!route_ipv4_prefix($1)) return "__invalid_device__" + for (pos=1; pos<=NF; pos++) { + if ($pos == "nhid" || $pos == "nexthop" || $pos == "encap") + return "__invalid_device__" + if ($pos == "dev") { + if (++seen != 1 || pos == NF || $(pos+1) != iface) + return "__invalid_device__" + } + if (local_route) { + if ($pos == "via") return "__invalid_device__" + if ($pos == "table" && (++local_table != 1 || + ($(pos+1) != "local" && $(pos+1) != "255"))) + return "__invalid_device__" + if ($pos == "proto" && (++local_proto != 1 || $(pos+1) != "kernel")) + return "__invalid_device__" + if ($pos == "scope" && (++local_scope != 1 || $(pos+1) != "host")) + return "__invalid_device__" + if ($pos == "src" && (++local_src != 1 || $(pos+1) != own)) + return "__invalid_device__" + } + } + if (local_route && (local_table != 1 || local_proto != 1 || + local_scope != 1 || local_src != 1)) return "__invalid_device__" + return iface +} '''.strip() @@ -5698,7 +5746,7 @@ if "$IP" link show dev "$IFACE" >/dev/null 2>&1; then BEGIN {while ((getline line < vpc_file) > 0) {n=split(line, parts, "|"); if (n>=5) vpc[parts[1] "|" parts[2] "|" parts[3] "|" parts[4] "|" parts[5]]=1} close(vpc_file)} NF {prefix=$1; if ($1=="local" || $1=="broadcast" || $1=="unreachable" || $1=="blackhole" || $1=="prohibit" || $1=="throw") prefix=$2 prefix=canonical_host_prefix(prefix) - table="main"; device=""; via="-"; metric="100"; proto=""; for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="dev") device=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)} + table="main"; device=filtered_route_device(iface, own); via="-"; metric="100"; proto=""; for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)} if (table=="254") table="main"; if (table=="255") table="local" if (prefix==canonical_host_prefix(own) && (table=="local" || table=="main") && device==iface) next if (table!="main" && proto=="186" && vpc[prefix "|" device "|" table "|" via "|" metric]) next @@ -6380,11 +6428,13 @@ recover_vpc_dataplanes() { $vpc_bridge_ports EOF vpc_bridge_routes=$("$IP" -o -4 route show table all dev "$vpc_bridge" 2>/dev/null) || exit 71 - printf '%s\n' "$vpc_bridge_routes" | awk -v wanted_table="$vpc_table" ' + printf '%s\n' "$vpc_bridge_routes" | awk -v wanted_table="$vpc_table" -v iface="$vpc_bridge" ' + __VC_FABRIC_ROUTE_VALIDATION_AWK__ NF {table="main"; proto=""; prefix=$1 for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="proto") proto=$(i+1)} if (table=="254") table="main" - if (prefix=="default" || table!=wanted_table || proto!="186") bad=1} + if (prefix=="default" || table!=wanted_table || proto!="186" || + filtered_route_device(iface)!=iface) bad=1} END {exit bad ? 1 : 0}' || exit 71 for vpc_sysctl_key in rp_filter proxy_arp proxy_arp_pvlan; do [ -e "/proc/sys/net/ipv4/conf/$vpc_bridge/$vpc_sysctl_key" ] || exit 71 @@ -6499,8 +6549,8 @@ actual_fabric_routes=$("$IP" -o route show table all dev "$IFACE" 2>/dev/null) | prefix=$1 if ($1=="local" || $1=="broadcast" || $1=="unreachable" || $1=="blackhole" || $1=="prohibit" || $1=="throw") prefix=$2 prefix=canonical_host_prefix(prefix) - table="main"; device=""; via="-"; metric="100"; proto="" - for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="dev") device=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)} + table="main"; device=filtered_route_device("wg-fabric0", own); via="-"; metric="100"; proto="" + for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)} if (table=="254") table="main"; if (table=="255") table="local" if (prefix==canonical_host_prefix(own) && (table=="local" || table=="main") && device=="wg-fabric0") next if (table!="main" && proto=="186" && vpc[prefix "|" device "|" table "|" via "|" metric]) next @@ -6511,7 +6561,7 @@ actual_fabric_routes=$("$IP" -o route show table all dev "$IFACE" 2>/dev/null) | for expected_route in $allowed_routes; do route_seen=$(printf '%s\n' "$actual_fabric_routes" | awk -v wanted="$expected_route" ' __VC_FABRIC_ROUTE_VALIDATION_AWK__ - NF {prefix=canonical_host_prefix($1); table="main"; device=""; for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1); if ($i=="dev") device=$(i+1)} if (prefix==wanted && table=="main" && device=="wg-fabric0") {print 1; exit}}') + NF {prefix=canonical_host_prefix($1); table="main"; device=filtered_route_device("wg-fabric0"); for (i=1;i<=NF;i++) {if ($i=="table") table=$(i+1)} if (prefix==wanted && table=="main" && device=="wg-fabric0") {print 1; exit}}') [ "$route_seen" = 1 ] || exit 78 done for route in $allowed_routes; do @@ -8396,7 +8446,7 @@ if "$IP" link show dev "$IFACE" >/dev/null 2>&1; then BEGIN {{while ((getline line < vpc_file) > 0) {{n=split(line, parts, "|"); if (n>=5) vpc[parts[1] "|" parts[2] "|" parts[3] "|" parts[4] "|" parts[5]]=1}} close(vpc_file)}} NF {{prefix=$1; if ($1=="local" || $1=="broadcast" || $1=="unreachable" || $1=="blackhole" || $1=="prohibit" || $1=="throw") prefix=$2 prefix=canonical_host_prefix(prefix) - table="main"; device=""; via="-"; metric="100"; proto=""; for (i=1;i<=NF;i++) {{if ($i=="table") table=$(i+1); if ($i=="dev") device=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)}} + table="main"; device=filtered_route_device(iface, own); via="-"; metric="100"; proto=""; for (i=1;i<=NF;i++) {{if ($i=="table") table=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)}} if (table=="254") table="main"; if (table=="255") table="local" if (prefix==canonical_host_prefix(own) && (table=="local" || table=="main") && device==iface) next if (table!="main" && proto=="186" && vpc[prefix "|" device "|" table "|" via "|" metric]) next @@ -9184,10 +9234,9 @@ if [ "$FIREWALL_MODE" = firewalld_nft ] || if ($1=="local" || $1=="broadcast" || $1=="unreachable" || $1=="blackhole" || $1=="prohibit" || $1=="throw") prefix=$2 prefix=canonical_host_prefix(prefix) - table="main"; device=""; via="-"; metric="100"; proto="" + table="main"; device=filtered_route_device("wg-fabric0", own); via="-"; metric="100"; proto="" for (i=1;i<=NF;i++) {{ if ($i=="table") table=$(i+1) - if ($i=="dev") device=$(i+1) if ($i=="via") via=$(i+1) if ($i=="metric") metric=$(i+1) if ($i=="proto") proto=$(i+1) @@ -9209,10 +9258,9 @@ if [ "$FIREWALL_MODE" = firewalld_nft ] || final_route_seen=$(printf '%s\n' "$final_fabric_routes" | awk -v wanted="$final_expected_route" ' {_FABRIC_ROUTE_VALIDATION_AWK} NF {{ - prefix=canonical_host_prefix($1); table="main"; device="" + prefix=canonical_host_prefix($1); table="main"; device=filtered_route_device("wg-fabric0") for (i=1;i<=NF;i++) {{ if ($i=="table") table=$(i+1) - if ($i=="dev") device=$(i+1) }} if (table=="254") table="main" if (prefix==wanted && table=="main" && device=="wg-fabric0") {{print 1; exit}} @@ -9814,7 +9862,7 @@ if "$IP" link show dev "$IFACE" >/dev/null 2>&1 && "$WG" show "$IFACE" >/dev/nul printf '%s\\n' "__VC_FABRIC_PEER__=$peer|$endpoint|$allowed" printf '%s\\n' "__VC_FABRIC_HANDSHAKE__=$peer|$handshake" done - $IP -o route show table all dev "$IFACE" 2>/dev/null | awk -v vpc_file="${{VPC_ROUTE_FILE:-}}" -v vpc_state="${{VPC_STATE_STATUS:-invalid}}" ' + $IP -o route show table all dev "$IFACE" 2>/dev/null | awk -v vpc_file="${{VPC_ROUTE_FILE:-}}" -v vpc_state="${{VPC_STATE_STATUS:-invalid}}" -v own={_q(overlay_ip or "")} ' {_FABRIC_ROUTE_VALIDATION_AWK} BEGIN {{ if (vpc_state != "invalid" && vpc_file != "") {{ @@ -9826,10 +9874,10 @@ if "$IP" link show dev "$IFACE" >/dev/null 2>&1 && "$WG" show "$IFACE" >/dev/nul }} }} NF {{ - table="main"; device=""; via="-"; metric="100"; proto=""; prefix=$1 + table="main"; device=filtered_route_device("wg-fabric0", own); via="-"; metric="100"; proto=""; prefix=$1 if ($1=="local" || $1=="broadcast" || $1=="unreachable" || $1=="blackhole" || $1=="prohibit" || $1=="throw") prefix=$2 prefix=canonical_host_prefix(prefix) - for (i=1;i<=NF;i++) {{if ($i=="table") table=$(i+1); if ($i=="dev") device=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)}} + for (i=1;i<=NF;i++) {{if ($i=="table") table=$(i+1); if ($i=="via") via=$(i+1); if ($i=="metric") metric=$(i+1); if ($i=="proto") proto=$(i+1)}} if (table=="254") table="main" if (table=="255") table="local" if (table!="main" && table!="local" && proto=="186" && vpc_state != "invalid" &&