Spaces:
Running
Running
Download tests/phase0_validate.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 8.83 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/phase0_validate.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/tests/phase0_validate.py
-
curl -L -o phase0_validate.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/phase0_validate.py
8.83 kB
| """Phase 0 validation: build a minimal 2-AS topology with eBGP + OSPF, | |
| read telemetry, inject a fault, measure timings.""" | |
| import json | |
| import time | |
| import sys | |
| from ipmininet.iptopo import IPTopo | |
| from ipmininet.ipnet import IPNet | |
| from ipmininet.router.config import BGP, OSPF, RouterConfig, AF_INET, set_rr | |
| from ipmininet.router.config.bgp import bgp_peering, ebgp_session | |
| class TwoASTopo(IPTopo): | |
| """2 ASes, 4 routers, 2 hosts, eBGP between AS border routers, OSPF within each AS.""" | |
| def build(self, *args, **kwargs): | |
| # AS 64501 | |
| r1 = self.addRouter("r1", config=RouterConfig, lo_addresses=["10.0.1.1/32"]) | |
| r2 = self.addRouter("r2", config=RouterConfig, lo_addresses=["10.0.1.2/32"]) | |
| h1 = self.addHost("h1") | |
| # AS 64502 | |
| r3 = self.addRouter("r3", config=RouterConfig, lo_addresses=["10.0.2.1/32"]) | |
| r4 = self.addRouter("r4", config=RouterConfig, lo_addresses=["10.0.2.2/32"]) | |
| h2 = self.addHost("h2") | |
| # Intra-AS links | |
| self.addLink(r1, r2) | |
| self.addLink(r2, h1) | |
| self.addLink(r3, r4) | |
| self.addLink(r4, h2) | |
| # Inter-AS link (eBGP peering) | |
| self.addLink(r1, r3) | |
| # OSPF within each AS | |
| r1.addDaemon(OSPF) | |
| r2.addDaemon(OSPF) | |
| r3.addDaemon(OSPF) | |
| r4.addDaemon(OSPF) | |
| # BGP | |
| r1.addDaemon(BGP, address_families=[AF_INET(redistribute=["connected", "ospf"])]) | |
| r2.addDaemon(BGP, address_families=[AF_INET(redistribute=["connected", "ospf"])]) | |
| r3.addDaemon(BGP, address_families=[AF_INET(redistribute=["connected", "ospf"])]) | |
| r4.addDaemon(BGP, address_families=[AF_INET(redistribute=["connected", "ospf"])]) | |
| # iBGP within AS 64501 | |
| bgp_peering(self, r1, r2) | |
| # iBGP within AS 64502 | |
| bgp_peering(self, r3, r4) | |
| # eBGP between ASes | |
| ebgp_session(self, r1, r3, link_type=None) | |
| # AS numbers | |
| self.addAS(64501, (r1, r2)) | |
| self.addAS(64502, (r3, r4)) | |
| # Subnets for hosts | |
| self.addSubnet(nodes=[r2, h1], subnets=["192.168.1.0/24"]) | |
| self.addSubnet(nodes=[r4, h2], subnets=["192.168.2.0/24"]) | |
| super().build(*args, **kwargs) | |
| def wait_for_bgp(net, timeout=90): | |
| """Wait until all BGP sessions are Established.""" | |
| routers = [n for n in net.routers] | |
| start = time.time() | |
| while time.time() - start < timeout: | |
| all_up = True | |
| for r in routers: | |
| try: | |
| out = r.cmd('vtysh -c "show bgp summary json"') | |
| bgp = json.loads(out) | |
| peers = bgp.get("ipv4Unicast", {}).get("peers", {}) | |
| for peer, info in peers.items(): | |
| if info.get("state") != "Established": | |
| all_up = False | |
| break | |
| except (json.JSONDecodeError, KeyError): | |
| all_up = False | |
| if not all_up: | |
| break | |
| if all_up: | |
| return time.time() - start | |
| time.sleep(2) | |
| return -1 | |
| def read_telemetry(router): | |
| """Read all telemetry streams from a router. Returns dict of results.""" | |
| results = {} | |
| # BGP summary | |
| try: | |
| out = router.cmd('vtysh -c "show bgp summary json"') | |
| results["bgp"] = json.loads(out) | |
| except Exception as e: | |
| results["bgp_error"] = str(e) | |
| # OSPF neighbors | |
| try: | |
| out = router.cmd('vtysh -c "show ip ospf neighbor json"') | |
| results["ospf"] = json.loads(out) | |
| except Exception as e: | |
| results["ospf_error"] = str(e) | |
| # Routing table | |
| try: | |
| out = router.cmd('vtysh -c "show ip route json"') | |
| results["routes"] = json.loads(out) | |
| except Exception as e: | |
| results["routes_error"] = str(e) | |
| # Interface stats | |
| try: | |
| out = router.cmd('ip -j -s link') | |
| results["interfaces"] = json.loads(out) | |
| except Exception as e: | |
| results["interfaces_error"] = str(e) | |
| # /proc/net/snmp | |
| try: | |
| out = router.cmd('cat /proc/net/snmp') | |
| results["snmp"] = out[:500] | |
| except Exception as e: | |
| results["snmp_error"] = str(e) | |
| return results | |
| def main(): | |
| print("=" * 60) | |
| print("PHASE 0 VALIDATION: 2-AS Topology with eBGP + OSPF") | |
| print("=" * 60) | |
| # 1. Build topology | |
| print("\n[1] Building topology...") | |
| t0 = time.time() | |
| net = IPNet(topo=TwoASTopo(), allocate_IPs=True) | |
| build_time = time.time() - t0 | |
| print(f" Topology built in {build_time:.2f}s") | |
| # 2. Start network | |
| print("\n[2] Starting network...") | |
| t0 = time.time() | |
| net.start() | |
| start_time = time.time() - t0 | |
| print(f" Network started in {start_time:.2f}s") | |
| # 3. Wait for BGP convergence | |
| print("\n[3] Waiting for BGP convergence (timeout 90s)...") | |
| conv_time = wait_for_bgp(net, timeout=90) | |
| if conv_time < 0: | |
| print(" WARNING: BGP did not converge within 90s!") | |
| else: | |
| print(f" BGP converged in {conv_time:.2f}s") | |
| # 4. Read telemetry from all routers | |
| print("\n[4] Reading telemetry from all routers...") | |
| for r in net.routers: | |
| telem = read_telemetry(r) | |
| bgp_peers = len(telem.get("bgp", {}).get("ipv4Unicast", {}).get("peers", {})) | |
| ospf_nbrs = len(telem.get("ospf", {}).get("neighbors", [])) | |
| route_count = len(telem.get("routes", {})) | |
| intf_count = len(telem.get("interfaces", [])) | |
| print(f" {r.name}: BGP peers={bgp_peers}, OSPF neighbors={ospf_nbrs}, " | |
| f"routes={route_count}, interfaces={intf_count}") | |
| if "bgp_error" in telem: | |
| print(f" BGP ERROR: {telem['bgp_error']}") | |
| if "ospf_error" in telem: | |
| print(f" OSPF ERROR: {telem['ospf_error']}") | |
| # 5. Connectivity test | |
| print("\n[5] Ping test (h1 -> h2)...") | |
| h1 = net["h1"] | |
| h2 = net["h2"] | |
| result = h1.cmd(f"ping -c 3 -W 2 {h2.IP()}") | |
| print(f" {result.strip()}") | |
| # 6. Fault injection: bring down link r1-r3 | |
| print("\n[6] Injecting fault: link r1-r3 DOWN...") | |
| r1 = net["r1"] | |
| r3 = net["r3"] | |
| r1_r3_intf = None | |
| for intf in r1.intfList(): | |
| if intf.link: | |
| other = intf.link.intf1 if intf.link.intf2 == intf else intf.link.intf2 | |
| if other.node == r3: | |
| r1_r3_intf = intf | |
| break | |
| if r1_r3_intf: | |
| r1.cmd(f"ip link set {r1_r3_intf.name} down") | |
| print(f" Brought down {r1_r3_intf.name} on r1") | |
| # Wait a bit and check BGP | |
| time.sleep(5) | |
| out = r1.cmd('vtysh -c "show bgp summary json"') | |
| try: | |
| bgp = json.loads(out) | |
| peers = bgp.get("ipv4Unicast", {}).get("peers", {}) | |
| for peer, info in peers.items(): | |
| print(f" r1 BGP peer {peer}: state={info.get('state', 'unknown')}") | |
| except json.JSONDecodeError: | |
| print(f" BGP output: {out[:200]}") | |
| # Ping should fail now (no alternate path between ASes) | |
| result = h1.cmd(f"ping -c 2 -W 2 {h2.IP()}") | |
| loss = "100% packet loss" in result | |
| print(f" Ping after fault: {'FAILED (expected)' if loss else 'STILL UP (unexpected)'}") | |
| # Restore | |
| r1.cmd(f"ip link set {r1_r3_intf.name} up") | |
| print(f" Restored {r1_r3_intf.name}") | |
| else: | |
| print(" Could not find r1-r3 interface!") | |
| # 7. OVS stats | |
| print("\n[7] OVS switch stats...") | |
| for sw in net.switches: | |
| try: | |
| out = sw.cmd(f"ovs-ofctl dump-ports {sw.name}") | |
| lines = [l for l in out.split('\n') if l.strip()][:5] | |
| print(f" {sw.name}: {len(lines)} port stat lines") | |
| except Exception as e: | |
| print(f" {sw.name} ERROR: {e}") | |
| # 8. Stop network | |
| print("\n[8] Stopping network...") | |
| t0 = time.time() | |
| net.stop() | |
| stop_time = time.time() - t0 | |
| print(f" Network stopped in {stop_time:.2f}s") | |
| # 9. Check for stale state | |
| print("\n[9] Checking for stale state...") | |
| import subprocess | |
| ns_out = subprocess.run(["ip", "netns", "list"], capture_output=True, text=True) | |
| stale_ns = [l for l in ns_out.stdout.strip().split('\n') if l] | |
| print(f" Network namespaces remaining: {len(stale_ns)}") | |
| if stale_ns: | |
| print(f" WARNING stale namespaces: {stale_ns[:5]}") | |
| # Summary | |
| print("\n" + "=" * 60) | |
| print("PHASE 0 SUMMARY") | |
| print("=" * 60) | |
| print(f" Build time: {build_time:.2f}s") | |
| print(f" Start time: {start_time:.2f}s") | |
| print(f" BGP convergence: {conv_time:.2f}s" if conv_time > 0 else " BGP convergence: FAILED") | |
| print(f" Stop time: {stop_time:.2f}s") | |
| print(f" Stale namespaces: {len(stale_ns)}") | |
| print(f" Stack: Mininet + FRR + OVS + IPMininet") | |
| if __name__ == "__main__": | |
| main() | |