Spaces:
Running
Running
Download server/sim/reachability.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 7.36 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/reachability.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/sim/reachability.py
-
curl -L -o reachability.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/reachability.py
7.36 kB
| """Ping and traceroute simulation — probe packets through the forwarding path. | |
| Probes traverse hop-by-hop via RIB lookups. At each hop, netem effects on the | |
| egress interface are applied probabilistically (same as real tc netem). | |
| """ | |
| import random | |
| MAX_TTL = 30 | |
| class ReachabilityEngine: | |
| def __init__(self, engine): | |
| self._engine = engine | |
| def ping(self, src_name: str, dst_name: str, count: int = 3, timeout: int = 2) -> dict: | |
| topo = self._engine.topo | |
| dst_ip = self._resolve_ip(dst_name) | |
| src_ip = self._resolve_ip(src_name) | |
| if not dst_ip or not src_ip: | |
| return {"raw": f"ping: unknown host", "loss_pct": 100} | |
| received = 0 | |
| rtts = [] | |
| for seq in range(1, count + 1): | |
| ok, rtt = self._send_probe(src_name, src_ip, dst_ip) | |
| if ok: | |
| ok_return, rtt_return = self._send_probe(dst_name, dst_ip, src_ip) | |
| if ok_return: | |
| received += 1 | |
| rtts.append(rtt + rtt_return) | |
| loss_pct = ((count - received) / count) * 100 if count > 0 else 100 | |
| if rtts: | |
| avg_rtt = sum(rtts) / len(rtts) | |
| min_rtt = min(rtts) | |
| max_rtt = max(rtts) | |
| else: | |
| avg_rtt = min_rtt = max_rtt = 0.0 | |
| lines = [f"PING {dst_ip} ({dst_ip}) 56(84) bytes of data."] | |
| for i, rtt in enumerate(rtts): | |
| lines.append(f"64 bytes from {dst_ip}: icmp_seq={i+1} ttl={64} time={rtt:.3f} ms") | |
| if received < count: | |
| for i in range(count - received): | |
| pass | |
| lines.append(f"") | |
| lines.append(f"--- {dst_ip} ping statistics ---") | |
| lines.append(f"{count} packets transmitted, {received} received, {loss_pct:.0f}% packet loss") | |
| if rtts: | |
| lines.append(f"rtt min/avg/max = {min_rtt:.3f}/{avg_rtt:.3f}/{max_rtt:.3f} ms") | |
| return { | |
| "raw": "\n".join(lines), "loss_pct": loss_pct, | |
| "rtt_avg": avg_rtt, "rtt_min": min_rtt, "rtt_max": max_rtt, | |
| } | |
| def _send_probe(self, src_name: str, src_ip: str, dst_ip: str) -> tuple[bool, float]: | |
| """Send one probe packet. Returns (arrived, total_delay_ms).""" | |
| topo = self._engine.topo | |
| current_node = src_name | |
| total_delay = 0.0 | |
| visited = set() | |
| for _ in range(MAX_TTL): | |
| if current_node in visited: | |
| return False, total_delay | |
| visited.add(current_node) | |
| if current_node in topo.hosts: | |
| host = topo.hosts[current_node] | |
| intf = list(host.interfaces.values())[0] if host.interfaces else None | |
| if intf and intf.ip == dst_ip: | |
| return True, total_delay | |
| nh_ip = host.gateway | |
| peer_intf = topo.get_peer_interface(intf.name) if intf else None | |
| if peer_intf: | |
| current_node = peer_intf.node | |
| total_delay += intf.netem_delay_ms | |
| if intf.netem_loss_pct > 0 and random.random() < intf.netem_loss_pct / 100: | |
| return False, total_delay | |
| else: | |
| return False, total_delay | |
| continue | |
| router = topo.routers.get(current_node) | |
| if not router: | |
| return False, total_delay | |
| for intf in router.interfaces.values(): | |
| if intf.ip == dst_ip: | |
| return True, total_delay | |
| nh_ip, out_intf = self._engine.rib.lookup(current_node, dst_ip) | |
| if not nh_ip: | |
| return False, total_delay | |
| if nh_ip == "blackhole": | |
| return False, total_delay | |
| intf = topo.get_interface(out_intf) if out_intf else None | |
| if intf: | |
| if not intf.up: | |
| return False, total_delay | |
| if intf.iptables_output_drop: | |
| return False, total_delay | |
| total_delay += intf.netem_delay_ms + intf.netem_jitter_ms * (random.random() - 0.5) | |
| if intf.netem_loss_pct > 0 and random.random() < intf.netem_loss_pct / 100: | |
| return False, total_delay | |
| peer_intf = topo.get_peer_interface(out_intf) if out_intf else None | |
| if not peer_intf or not peer_intf.up: | |
| return False, total_delay | |
| for rule in peer_intf.iptables_input_rules: | |
| if "DROP" in str(rule): | |
| return False, total_delay | |
| current_node = peer_intf.node | |
| return False, total_delay | |
| def traceroute(self, src_name: str, dst_name: str) -> str: | |
| topo = self._engine.topo | |
| dst_ip = self._resolve_ip(dst_name) | |
| if not dst_ip: | |
| return f"traceroute: unknown host {dst_name}" | |
| lines = [f"traceroute to {dst_ip}, {MAX_TTL} hops max"] | |
| current_node = src_name | |
| visited = set() | |
| for ttl in range(1, MAX_TTL + 1): | |
| if current_node in visited: | |
| lines.append(f" {ttl} * * * (loop detected)") | |
| break | |
| visited.add(current_node) | |
| if current_node in topo.hosts: | |
| host = topo.hosts[current_node] | |
| intf = list(host.interfaces.values())[0] if host.interfaces else None | |
| if intf and intf.ip == dst_ip: | |
| lines.append(f" {ttl} {dst_ip} 0.000 ms") | |
| break | |
| peer_intf = topo.get_peer_interface(intf.name) if intf else None | |
| if peer_intf: | |
| current_node = peer_intf.node | |
| else: | |
| lines.append(f" {ttl} * * *") | |
| break | |
| continue | |
| router = topo.routers.get(current_node) | |
| if not router: | |
| lines.append(f" {ttl} * * *") | |
| break | |
| for intf in router.interfaces.values(): | |
| if intf.ip == dst_ip: | |
| lines.append(f" {ttl} {dst_ip} 0.000 ms") | |
| return "\n".join(lines) | |
| nh_ip, out_intf = self._engine.rib.lookup(current_node, dst_ip) | |
| if not nh_ip or nh_ip == "blackhole": | |
| lines.append(f" {ttl} * * *") | |
| break | |
| intf = topo.get_interface(out_intf) if out_intf else None | |
| delay = intf.netem_delay_ms if intf else 0 | |
| hop_ip = router.loopback or (intf.ip if intf else "?") | |
| lines.append(f" {ttl} {hop_ip} {delay:.3f} ms") | |
| if not intf or not intf.up: | |
| lines.append(f" {ttl+1} * * *") | |
| break | |
| peer_intf = topo.get_peer_interface(out_intf) if out_intf else None | |
| if not peer_intf or not peer_intf.up: | |
| lines.append(f" {ttl+1} * * *") | |
| break | |
| current_node = peer_intf.node | |
| return "\n".join(lines) | |
| def _resolve_ip(self, name: str) -> str | None: | |
| topo = self._engine.topo | |
| if name in topo.hosts: | |
| h = topo.hosts[name] | |
| intf = list(h.interfaces.values())[0] if h.interfaces else None | |
| return intf.ip if intf else None | |
| if name in topo.routers: | |
| r = topo.routers[name] | |
| return r.loopback or None | |
| return None | |