Spaces:
Sleeping
Sleeping
Download server/network.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 6.32 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/network.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/network.py
-
curl -L -o network.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/network.py
6.32 kB
| """Mininet/IPMininet topology lifecycle: build, start, converge, stop.""" | |
| import json | |
| import time | |
| import subprocess | |
| from ipmininet.iptopo import IPTopo | |
| from ipmininet.ipnet import IPNet | |
| from ipmininet.router.config import BGP, OSPF, RouterConfig, AF_INET | |
| from ipmininet.router.config.bgp import bgp_peering, ebgp_session | |
| from server.constants import ( | |
| AS_CONFIGS, | |
| INTRA_AS_LINKS, | |
| EBGP_PEERS, | |
| BGP_CONVERGENCE_TIMEOUT, | |
| BGP_POLL_INTERVAL, | |
| POST_CONVERGENCE_DELAY, | |
| ) | |
| class NetOpsTopo(IPTopo): | |
| """Multi-AS ISP topology: eBGP between ASes, OSPF within. Scale set via constants.""" | |
| def build(self, *args, **kwargs): | |
| routers = {} | |
| hosts = {} | |
| for asn, cfg in AS_CONFIGS.items(): | |
| for rname in cfg["routers"]: | |
| routers[rname] = self.addRouter( | |
| rname, | |
| config=RouterConfig, | |
| lo_addresses=[cfg["loopbacks"][rname]], | |
| ) | |
| for hname in cfg["hosts"]: | |
| hosts[hname] = self.addHost(hname) | |
| for asn, links in INTRA_AS_LINKS.items(): | |
| for a, b in links: | |
| self.addLink(routers[a], routers[b]) | |
| for a, b in EBGP_PEERS: | |
| self.addLink(routers[a], routers[b]) | |
| for asn, cfg in AS_CONFIGS.items(): | |
| host_router = cfg["host_router"] | |
| for hname in cfg["hosts"]: | |
| self.addLink(routers[host_router], hosts[hname]) | |
| for asn, cfg in AS_CONFIGS.items(): | |
| for rname in cfg["routers"]: | |
| r = routers[rname] | |
| r.addDaemon(OSPF) | |
| r.addDaemon( | |
| BGP, | |
| address_families=[AF_INET(redistribute=["connected", "ospf"])], | |
| ) | |
| for asn, cfg in AS_CONFIGS.items(): | |
| router_nodes = [routers[r] for r in cfg["routers"]] | |
| for i in range(len(router_nodes)): | |
| for j in range(i + 1, len(router_nodes)): | |
| bgp_peering(self, router_nodes[i], router_nodes[j]) | |
| for a, b in EBGP_PEERS: | |
| ebgp_session(self, routers[a], routers[b], link_type=None) | |
| for asn, cfg in AS_CONFIGS.items(): | |
| router_nodes = tuple(routers[r] for r in cfg["routers"]) | |
| self.addAS(asn, router_nodes) | |
| super().build(*args, **kwargs) | |
| class NetworkManager: | |
| """Owns the Mininet network lifecycle.""" | |
| def __init__(self): | |
| self._net: IPNet | None = None | |
| def net(self) -> IPNet: | |
| if self._net is None: | |
| raise RuntimeError("Network not started") | |
| return self._net | |
| def running(self) -> bool: | |
| return self._net is not None | |
| def start(self) -> float: | |
| """Build and start the network. Returns total time (build + start + convergence).""" | |
| if self._net is not None: | |
| self.stop() | |
| t0 = time.time() | |
| self._net = IPNet(topo=NetOpsTopo(), allocate_IPs=True) | |
| self._net.start() | |
| conv = self.wait_for_bgp() | |
| if conv < 0: | |
| raise RuntimeError("BGP did not converge within timeout") | |
| time.sleep(POST_CONVERGENCE_DELAY) | |
| return time.time() - t0 | |
| def stop(self) -> None: | |
| if self._net is not None: | |
| self._net.stop() | |
| self._net = None | |
| self._cleanup_stale() | |
| def restart(self) -> float: | |
| self.stop() | |
| return self.start() | |
| def wait_for_bgp(self, timeout: int = BGP_CONVERGENCE_TIMEOUT) -> float: | |
| """Wait until all BGP sessions are Established. Returns convergence time or -1.""" | |
| start = time.time() | |
| while time.time() - start < timeout: | |
| if self._all_bgp_established(): | |
| return time.time() - start | |
| time.sleep(BGP_POLL_INTERVAL) | |
| return -1 | |
| def _all_bgp_established(self) -> bool: | |
| for r in self.net.routers: | |
| try: | |
| out = r.cmd('vtysh -c "show bgp summary json"') | |
| bgp = json.loads(out) | |
| peers = bgp.get("ipv4Unicast", {}).get("peers", {}) | |
| if not peers: | |
| return False | |
| for info in peers.values(): | |
| if info.get("state") != "Established": | |
| return False | |
| except (json.JSONDecodeError, KeyError): | |
| return False | |
| return True | |
| def get_router(self, name: str): | |
| return self.net[name] | |
| def get_host(self, name: str): | |
| return self.net[name] | |
| def get_all_routers(self) -> list: | |
| return list(self.net.routers) | |
| def get_all_hosts(self) -> list: | |
| return [self.net[h] for h in self._all_host_names()] | |
| def get_all_switches(self) -> list: | |
| return list(self.net.switches) | |
| def find_link_interface(self, router_name: str, peer_name: str): | |
| """Find the interface on router_name that connects to peer_name.""" | |
| router = self.net[router_name] | |
| peer = self.net[peer_name] | |
| for intf in router.intfList(): | |
| if intf.link: | |
| other = intf.link.intf1 if intf.link.intf2 == intf else intf.link.intf2 | |
| if other.node == peer: | |
| return intf | |
| return None | |
| def ping(self, src_name: str, dst_name: str, count: int = 3, timeout: int = 2) -> dict: | |
| src = self.net[src_name] | |
| dst = self.net[dst_name] | |
| result = src.cmd(f"ping -c {count} -W {timeout} {dst.IP()}") | |
| loss = 0.0 | |
| for line in result.split("\n"): | |
| if "packet loss" in line: | |
| try: | |
| loss = float(line.split("%")[0].split()[-1]) | |
| except (ValueError, IndexError): | |
| pass | |
| return {"raw": result.strip(), "loss_pct": loss} | |
| def traceroute(self, src_name: str, dst_name: str) -> str: | |
| src = self.net[src_name] | |
| dst = self.net[dst_name] | |
| return src.cmd(f"traceroute -n -w 2 {dst.IP()}").strip() | |
| def _all_host_names() -> list[str]: | |
| names = [] | |
| for cfg in AS_CONFIGS.values(): | |
| names.extend(cfg["hosts"]) | |
| return names | |
| def _cleanup_stale(): | |
| subprocess.run(["mn", "-c"], capture_output=True, timeout=10) | |