Spaces:
Running
Running
Download server/faults/compound.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 7.42 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/compound.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/faults/compound.py
-
curl -L -o compound.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/compound.py
7.42 kB
| """Compound and expert faults (F35-F37 + expert): multi-fault, cascading, red herrings.""" | |
| from __future__ import annotations | |
| import random | |
| from server.constants import GroundTruth, AS_CONFIGS | |
| from server.faults.physical import inject_link_failure, inject_latency, inject_packet_loss, inject_packet_corruption, inject_interface_flapping | |
| from server.faults.ospf import inject_ospf_cost_change, inject_ospf_timer_mismatch | |
| from server.faults.bgp_control import inject_bgp_session_kill, inject_bgp_route_withdrawal | |
| from server.faults.bgp_policy import inject_bgp_route_leak, inject_bgp_local_pref | |
| from server.faults.routing_logic import inject_bgp_blackhole | |
| from server.faults.system import inject_cpu_exhaustion, inject_stale_routes_after_crash | |
| from server.faults.firewall import inject_control_plane_isolation | |
| _INJECT_FNS = { | |
| "inject_link_failure": inject_link_failure, | |
| "inject_latency": inject_latency, | |
| "inject_packet_loss": inject_packet_loss, | |
| "inject_packet_corruption": inject_packet_corruption, | |
| "inject_interface_flapping": inject_interface_flapping, | |
| "inject_ospf_cost_change": inject_ospf_cost_change, | |
| "inject_ospf_timer_mismatch": inject_ospf_timer_mismatch, | |
| "inject_bgp_session_kill": inject_bgp_session_kill, | |
| "inject_bgp_route_withdrawal": inject_bgp_route_withdrawal, | |
| "inject_bgp_route_leak": inject_bgp_route_leak, | |
| "inject_bgp_local_pref": inject_bgp_local_pref, | |
| "inject_bgp_blackhole": inject_bgp_blackhole, | |
| "inject_cpu_exhaustion": inject_cpu_exhaustion, | |
| "inject_stale_routes_after_crash": inject_stale_routes_after_crash, | |
| "inject_control_plane_isolation": inject_control_plane_isolation, | |
| } | |
| def inject_multi_fault(injector, faults: list[tuple[str, dict]]) -> list[GroundTruth]: | |
| """F35/F36 — Inject multiple faults. Each entry is (method_name, kwargs). | |
| Example: inject_multi_fault(injector, [ | |
| ("inject_link_failure", {"router_a": "r1", "router_b": "r4"}), | |
| ("inject_latency", {"router": "r7", "peer": "r8", "delay_ms": 200}), | |
| ]) | |
| """ | |
| results = [] | |
| for method_name, kwargs in faults: | |
| fn = _INJECT_FNS[method_name] | |
| results.append(fn(injector, **kwargs)) | |
| return results | |
| def _inject_multi_as_single(injector, fault_type: str, faults: list) -> GroundTruth: | |
| """Wrap inject_multi_fault to return a single GroundTruth.""" | |
| gts = inject_multi_fault(injector, faults) | |
| components = [gt.component for gt in gts] | |
| return GroundTruth( | |
| fault_type=fault_type, | |
| component="+".join(components), | |
| details={"sub_faults": [gt.fault_type for gt in gts]}, | |
| ) | |
| def _inject_stale_routes_random(injector, all_links) -> GroundTruth: | |
| """Pick two routers in the same AS for stale routes fault.""" | |
| for ra, rb in all_links: | |
| asn_a = injector._router_asn(ra) | |
| asn_b = injector._router_asn(rb) | |
| if asn_a == asn_b: | |
| return inject_stale_routes_after_crash(injector, ra, rb) | |
| return inject_ospf_cost_change(injector, all_links[0][0], all_links[0][1], 1000) | |
| def _inject_silent_blackhole_random(injector, router: str) -> GroundTruth: | |
| """Pick a suitable host interface on a host-connected router for F31.""" | |
| from server.faults.routing_logic import inject_silent_blackhole_aggregate | |
| for asn, cfg in AS_CONFIGS.items(): | |
| if router in cfg["routers"] and router == cfg["host_router"]: | |
| host_subnet = cfg["host_subnet"] | |
| summary = host_subnet.rsplit(".", 1)[0] + ".0/16" | |
| intf_name = injector._nm.find_link_interface(router, cfg["hosts"][0]) | |
| if intf_name: | |
| intf_str = intf_name.name if hasattr(intf_name, "name") else str(intf_name) | |
| return inject_silent_blackhole_aggregate( | |
| injector, router, summary, intf_str, host_subnet) | |
| return inject_bgp_blackhole(injector, router, "192.168.99.0/24") | |
| def inject_red_herring_cpu(injector, a, b, br, all_routers) -> GroundTruth: | |
| """Real fault: BGP route leak. Red herring: CPU spike on unrelated router.""" | |
| decoy = random.choice([r for r in all_routers if r not in (a, b, br)]) | |
| inject_cpu_exhaustion(injector, decoy) | |
| gt = inject_bgp_route_leak(injector, br, "192.168.1.0/24") | |
| return GroundTruth( | |
| fault_type="red_herring_cpu", | |
| component=gt.component, | |
| details={"real_fault": "bgp_route_leak", "decoy": decoy, | |
| "sub_faults": ["bgp_route_leak", "cpu_exhaustion"]}, | |
| ) | |
| def inject_red_herring_flap(injector, a, b, all_links) -> GroundTruth: | |
| """Real fault: packet loss on a-b. Red herring: interface flapping elsewhere.""" | |
| other_links = [(x, y) for x, y in all_links if x != a and y != b] | |
| decoy_a, decoy_b = random.choice(other_links) if other_links else (a, b) | |
| inject_interface_flapping(injector, decoy_a, decoy_b) | |
| gt = inject_packet_loss(injector, a, b, random.choice([10, 20])) | |
| return GroundTruth( | |
| fault_type="red_herring_flap", | |
| component=gt.component, | |
| details={"real_fault": "packet_loss", "decoy_link": f"{decoy_a}-{decoy_b}", | |
| "sub_faults": ["packet_loss", "interface_flapping"]}, | |
| ) | |
| def inject_triple_fault(injector, a, b, br, all_links, all_routers) -> GroundTruth: | |
| """Three simultaneous faults across different layers.""" | |
| other_links = [(x, y) for x, y in all_links if x != a and y != b] | |
| a2, b2 = random.choice(other_links) if other_links else (a, b) | |
| inject_latency(injector, a, b, random.choice([200, 500])) | |
| inject_ospf_timer_mismatch(injector, a2, b2) | |
| inject_bgp_route_withdrawal(injector, br) | |
| return GroundTruth( | |
| fault_type="triple_fault", | |
| component=f"{a}-{b}+{a2}-{b2}+{br}", | |
| details={"sub_faults": ["latency_spike", "ospf_timer_mismatch", "bgp_route_withdrawal"]}, | |
| ) | |
| def inject_cross_layer_fault(injector, a, b, br) -> GroundTruth: | |
| """L2 packet corruption + BGP policy fault on same path.""" | |
| inject_packet_corruption(injector, a, b, random.choice([5, 10])) | |
| inject_bgp_local_pref( | |
| injector, br, random.choice([x for x in injector._border_routers() if x != br]) | |
| ) | |
| return GroundTruth( | |
| fault_type="cross_layer_fault", | |
| component=f"{a}-{b}+{br}", | |
| details={"sub_faults": ["packet_corruption", "bgp_local_pref"]}, | |
| ) | |
| def inject_cascading_reveal(injector, a, b, br) -> GroundTruth: | |
| """Link failure masks an underlying BGP blackhole. Fixing the link reveals the blackhole.""" | |
| inject_link_failure(injector, a, b) | |
| inject_bgp_blackhole(injector, br, "192.168.99.0/24") | |
| return GroundTruth( | |
| fault_type="cascading_reveal", | |
| component=f"{a}-{b}+{br}", | |
| details={"sub_faults": ["link_failure", "bgp_blackhole"]}, | |
| ) | |
| FAULTS = { | |
| "multi_fault_easy": {"fn": None, "difficulty": "hard", "needs": "link"}, | |
| "multi_fault_hard": {"fn": None, "difficulty": "hard", "needs": "border"}, | |
| "cascading_failure": {"fn": None, "difficulty": "hard", "needs": "border"}, | |
| "red_herring_cpu": {"fn": None, "difficulty": "expert", "needs": "border"}, | |
| "red_herring_flap": {"fn": None, "difficulty": "expert", "needs": "link"}, | |
| "triple_fault": {"fn": None, "difficulty": "expert", "needs": "link"}, | |
| "cross_layer_fault": {"fn": None, "difficulty": "expert", "needs": "border"}, | |
| "cascading_reveal": {"fn": None, "difficulty": "expert", "needs": "border"}, | |
| } | |