Spaces:
Sleeping
Sleeping
Download server/sim/node_proxy.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 29.3 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/node_proxy.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/sim/node_proxy.py
-
curl -L -o node_proxy.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/node_proxy.py
29.3 kB
| """SimNode — proxy object that intercepts cmd() calls from telemetry.py and fault_injector.py. | |
| READ commands dispatch to simulation engines and return JSON strings. | |
| WRITE commands mutate topology state. | |
| """ | |
| import json | |
| import re | |
| class SimNode: | |
| """Drop-in for a Mininet node. Routes cmd() to the simulation engine.""" | |
| def __init__(self, name: str, engine): | |
| self._name = name | |
| self._engine = engine | |
| self.pid = 99999 | |
| def name(self) -> str: | |
| return self._name | |
| def IP(self) -> str: | |
| topo = self._engine.topo | |
| if self._name in topo.routers: | |
| return topo.routers[self._name].loopback | |
| if self._name in topo.hosts: | |
| h = topo.hosts[self._name] | |
| intf = list(h.interfaces.values())[0] if h.interfaces else None | |
| return intf.ip if intf else "" | |
| return "" | |
| def intfList(self): | |
| topo = self._engine.topo | |
| if self._name in topo.routers: | |
| return [SimIntfProxy(intf) for intf in topo.routers[self._name].interfaces.values()] | |
| if self._name in topo.hosts: | |
| return [SimIntfProxy(intf) for intf in topo.hosts[self._name].interfaces.values()] | |
| return [] | |
| def cmd(self, command: str) -> str: | |
| command = command.strip() | |
| # READ dispatchers | |
| for pattern, handler in self._read_handlers(): | |
| m = re.search(pattern, command) | |
| if m: | |
| return handler(m) | |
| # WRITE dispatchers | |
| for pattern, handler in self._write_handlers(): | |
| m = re.search(pattern, command) | |
| if m: | |
| return handler(m) | |
| return "" | |
| def _read_handlers(self): | |
| return [ | |
| (r'vtysh.*"show bgp summary json"', self._read_bgp_summary), | |
| (r'vtysh.*"show bgp neighbors?.*json"', self._read_bgp_summary), | |
| (r'vtysh.*"show ip ospf neighbor json"', self._read_ospf_neighbors), | |
| (r'vtysh.*"show ip ospf interface\s*(\S*)\s*json"', self._read_ospf_interface), | |
| (r'vtysh.*"show ip route json"', self._read_routes), | |
| (r'vtysh.*"show version"', self._read_version), | |
| (r'ip -j -s link', self._read_interfaces), | |
| (r'tc -?s qdisc show dev (\S+)', self._read_tc_qdisc), | |
| (r'tc qdisc show dev (\S+)', self._read_tc_qdisc_raw), | |
| (r'cat /proc/loadavg', self._read_loadavg), | |
| (r'cat /proc/net/snmp', self._read_proc_snmp), | |
| (r'cat /proc/net/netstat', self._read_proc_netstat), | |
| (r'ip -4 addr show (\S+)', self._read_ip_addr), | |
| (r'ip route show type blackhole', self._read_route_blackholes), | |
| (r'ip route show (\S+)', self._read_ip_route), | |
| (r'iptables -L (\S+) -v -n', self._read_iptables), | |
| (r'iptables -L -n', self._read_iptables_all), | |
| (r'pgrep', self._read_pgrep), | |
| (r'cat /sys/class/net/(\S+)/mtu', self._read_sys_mtu), | |
| (r'vtysh.*"show running-config"', self._read_running_config), | |
| (r'vtysh.*"show ip ospf interface (\S+)"', self._read_ospf_intf_detail), | |
| (r'vtysh.*"show bgp ipv4 unicast json"', self._read_bgp_unicast), | |
| (r'vtysh.*"clear bgp', self._read_noop), | |
| ] | |
| def _write_handlers(self): | |
| return [ | |
| (r"bash -c 'while true.*ip link set (\S+)", self._write_flapping), | |
| (r'ip link set (?:dev )?(\S+) mtu (\d+)', self._write_mtu), | |
| (r'ip link set (\S+) (up|down)', self._write_link_state), | |
| (r'tc qdisc (add|replace) dev (\S+) root netem (.+)', self._write_tc_netem), | |
| (r'tc qdisc (add|replace) dev (\S+) root tbf (.+)', self._write_tc_tbf), | |
| (r'tc qdisc del dev (\S+) root', self._write_tc_del), | |
| (r'iptables -A (INPUT|OUTPUT|FORWARD) (.+)', self._write_iptables_add), | |
| (r'iptables -D (INPUT|OUTPUT|FORWARD) (.+)', self._write_iptables_del), | |
| (r'iptables -F', self._write_iptables_flush), | |
| (r'ip route add blackhole (\S+)', self._write_route_blackhole), | |
| (r'ip route add (\S+) via (\S+)', self._write_route_add), | |
| (r'ip route del blackhole (\S+)', self._write_route_del_blackhole), | |
| (r'ip route del (\S+) dev (\S+)', self._write_route_del_dev), | |
| (r'ip route add (\S+) dev (\S+)', self._write_route_add_dev), | |
| (r'ip route del (\S+)', self._write_route_del), | |
| (r'vtysh.*"conf t"(.+)', self._write_vtysh_conf), | |
| (r'/usr/lib/frr/(ospfd|bgpd)', self._write_start_daemon), | |
| (r'kill (\d+)', self._write_kill), | |
| (r'pkill -f (.+)', self._write_pkill), | |
| (r'iperf', self._write_iperf), | |
| (r'dd if=/dev/(urandom|zero)', self._write_cpu_hog), | |
| ] | |
| # ---- READ handlers ---- | |
| def _read_bgp_summary(self, m): | |
| return json.dumps(self._engine.bgp.bgp_summary_dict(self._name)) | |
| def _read_ospf_neighbors(self, m): | |
| return json.dumps(self._engine.ospf.ospf_neighbors_dict(self._name)) | |
| def _read_ospf_interface(self, m): | |
| return json.dumps(self._engine.ospf.ospf_interface_dict(self._name)) | |
| def _read_routes(self, m): | |
| return json.dumps(self._engine.rib.compute_rib(self._name)) | |
| def _read_version(self, m): | |
| return "FRRouting 8.5 (simulated)" | |
| def _read_interfaces(self, m): | |
| return json.dumps(self._engine.counters.interface_stats(self._name)) | |
| def _read_tc_qdisc(self, m): | |
| intf_name = m.group(1) | |
| return json.dumps(self._engine.counters.queue_stats(self._name, intf_name)) | |
| def _read_tc_qdisc_raw(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| intf = router.interfaces.get(intf_name) | |
| if not intf: | |
| return "" | |
| parts = [] | |
| if intf.has_netem: | |
| s = f"qdisc netem 8001: root refcnt 2" | |
| if intf.netem_delay_ms > 0: | |
| s += f" delay {intf.netem_delay_ms}ms" | |
| if intf.netem_jitter_ms > 0: | |
| s += f" {intf.netem_jitter_ms}ms" | |
| if intf.netem_loss_pct > 0: | |
| s += f" loss {intf.netem_loss_pct}%" | |
| if intf.netem_corrupt_pct > 0: | |
| s += f" corrupt {intf.netem_corrupt_pct}%" | |
| if intf.netem_reorder_pct > 0: | |
| s += f" reorder {intf.netem_reorder_pct}%" | |
| if intf.netem_dup_pct > 0: | |
| s += f" duplicate {intf.netem_dup_pct}%" | |
| parts.append(s) | |
| elif intf.has_tbf: | |
| parts.append(f"qdisc tbf 8001: root refcnt 2 rate {intf.tbf_rate_kbit}Kbit") | |
| else: | |
| parts.append(f"qdisc pfifo_fast 0: root refcnt 2") | |
| return "\n".join(parts) | |
| def _read_proc_snmp(self, m): | |
| c = self._engine.counters.protocol_counters(self._name) | |
| tcp = c.get("snmp", {}).get("Tcp", {}) | |
| icmp = c.get("snmp", {}).get("Icmp", {}) | |
| lines = [ | |
| "Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors", | |
| f"Tcp: 1 200 120000 -1 0 0 0 0 0 0 {tcp.get('OutSegs', 0)} {tcp.get('RetransSegs', 0)} {tcp.get('InErrs', 0)} 0 0", | |
| "Icmp: InMsgs InErrors InCsumErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps", | |
| f"Icmp: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 {icmp.get('OutTimeExcds', 0)} 0 0 0 0 0 0 0 0 0", | |
| ] | |
| return "\n".join(lines) | |
| def _read_proc_netstat(self, m): | |
| c = self._engine.counters.protocol_counters(self._name) | |
| reorder = c.get("netstat", {}).get("TcpExt", {}).get("TCPSACKReorder", 0) | |
| return f"TcpExt: TCPSACKReorder {reorder}\nTcpExt: {reorder}" | |
| def _read_loadavg(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router and router.cpu_exhausted: | |
| return "99.50 99.20 98.80 4/150 12345" | |
| return "0.05 0.08 0.10 1/150 12345" | |
| def _read_ip_addr(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| intf = router.interfaces.get(intf_name) | |
| if not intf: | |
| return "" | |
| return f" inet {intf.ip}/{intf.mask} scope global {intf.name}" | |
| def _read_ip_route(self, m): | |
| prefix = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| for p, nh in router.static_routes.items(): | |
| if prefix in p: | |
| if nh == "blackhole": | |
| return f"blackhole {p}" | |
| if "@" in p: | |
| actual_prefix, dev = p.split("@", 1) | |
| return f"{actual_prefix} dev {dev}" | |
| return f"{p} via {nh}" | |
| # Check connected routes | |
| for intf in router.interfaces.values(): | |
| if intf.ip and prefix in intf.subnet: | |
| return f"{intf.subnet} dev {intf.name}" | |
| return "" | |
| def _read_iptables(self, m): | |
| chain = m.group(1) | |
| return self._format_iptables(chain) | |
| def _read_iptables_all(self, m): | |
| lines = [] | |
| for chain in ["INPUT", "OUTPUT", "FORWARD"]: | |
| lines.append(self._format_iptables(chain)) | |
| return "\n".join(lines) | |
| def _format_iptables(self, chain): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return f"Chain {chain} (policy ACCEPT)" | |
| lines = [f"Chain {chain} (policy ACCEPT)"] | |
| seen = set() | |
| for intf in router.interfaces.values(): | |
| rules = [] | |
| if chain == "OUTPUT" and intf.iptables_output_drop: | |
| rules.append(f"DROP all -- 0.0.0.0/0 0.0.0.0/0 -o {intf.name}") | |
| if chain == "INPUT": | |
| rules.extend(intf.iptables_input_rules) | |
| elif chain == "FORWARD": | |
| rules.extend(intf.iptables_forward_rules) | |
| for r in rules: | |
| if r in seen: | |
| continue | |
| seen.add(r) | |
| formatted = self._format_iptables_rule(r) | |
| lines.append(f" 0 0 {formatted}") | |
| if chain == "OUTPUT" and hasattr(router, '_iptables_output_rules'): | |
| for r in router._iptables_output_rules: | |
| if r in seen: | |
| continue | |
| seen.add(r) | |
| formatted = self._format_iptables_rule(r) | |
| lines.append(f" 0 0 {formatted}") | |
| return "\n".join(lines) | |
| def _format_iptables_rule(self, rule): | |
| formatted = rule | |
| dport_m = re.search(r'--dport (\d+)', rule) | |
| if dport_m: | |
| formatted = formatted.replace(f"--dport {dport_m.group(1)}", f"dpt:{dport_m.group(1)}") | |
| proto_m = re.search(r'-p (\w+)', rule) | |
| if proto_m: | |
| formatted = formatted.replace(f"-p {proto_m.group(1)}", proto_m.group(1)) | |
| return formatted | |
| def _read_pgrep(self, m): | |
| return "" | |
| def _read_noop(self, m): | |
| return "" | |
| def _read_route_blackholes(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| lines = [] | |
| for prefix, nh in router.static_routes.items(): | |
| if nh == "blackhole": | |
| lines.append(f"blackhole {prefix}") | |
| return "\n".join(lines) | |
| def _read_bgp_unicast(self, m): | |
| d = self._engine.bgp.bgp_summary_dict(self._name) | |
| best = self._engine.bgp.best_path(self._name) | |
| routes = {} | |
| for prefix, route in best.items(): | |
| routes[prefix] = { | |
| "paths": [{ | |
| "aspath": {"string": " ".join(str(a) for a in route.as_path)}, | |
| "locPrf": route.local_pref, | |
| "community": {"list": route.communities} if route.communities else {}, | |
| "nexthops": [{"ip": route.next_hop}], | |
| }] | |
| } | |
| d["ipv4Unicast"]["routes"] = routes | |
| return json.dumps(d) | |
| def _read_sys_mtu(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router and intf_name in router.interfaces: | |
| return str(router.interfaces[intf_name].mtu) | |
| return "1500" | |
| def _read_running_config(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| lines = [f"hostname {self._name}", "!"] | |
| for intf_name, intf in router.interfaces.items(): | |
| lines.append(f"interface {intf_name}") | |
| hello = router.ospf_hello.get(intf_name) | |
| dead = router.ospf_dead.get(intf_name) | |
| if hello and hello != 10: | |
| lines.append(f" ip ospf hello-interval {hello}") | |
| if dead and dead != 40: | |
| lines.append(f" ip ospf dead-interval {dead}") | |
| cost = router.ospf_costs.get(intf_name, 10) | |
| if cost != 10: | |
| lines.append(f" ip ospf cost {cost}") | |
| lines.append("!") | |
| lines.append("router ospf") | |
| for intf_name in router.interfaces: | |
| area = router.ospf_area.get(intf_name, "0.0.0.0") | |
| intf = router.interfaces[intf_name] | |
| if intf.ip: | |
| lines.append(f" network {intf.subnet} area {area}") | |
| lines.append("!") | |
| return "\n".join(lines) | |
| def _read_ospf_intf_detail(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| area = router.ospf_area.get(intf_name, "0.0.0.0") | |
| cost = router.ospf_costs.get(intf_name, 10) | |
| intf = router.interfaces.get(intf_name) | |
| ip = intf.ip if intf else "" | |
| return f" {intf_name} is up\n Internet Address {ip}, Area {area}\n Router ID {router.loopback}, Cost {cost}" | |
| # ---- WRITE handlers ---- | |
| def _write_link_state(self, m): | |
| intf_name = m.group(1) | |
| state = m.group(2) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router and intf_name in router.interfaces: | |
| router.interfaces[intf_name].up = (state == "up") | |
| return "" | |
| def _write_mtu(self, m): | |
| intf_name = m.group(1) | |
| mtu = int(m.group(2)) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router and intf_name in router.interfaces: | |
| router.interfaces[intf_name].mtu = mtu | |
| return "" | |
| def _write_tc_netem(self, m): | |
| intf_name = m.group(2) | |
| params = m.group(3) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router or intf_name not in router.interfaces: | |
| return "" | |
| intf = router.interfaces[intf_name] | |
| intf.clear_netem() | |
| delay_m = re.search(r'delay (\d+)ms', params) | |
| if delay_m: | |
| intf.netem_delay_ms = int(delay_m.group(1)) | |
| jitter_m = re.search(r'delay \d+ms (\d+)ms', params) | |
| if jitter_m: | |
| intf.netem_jitter_ms = int(jitter_m.group(1)) | |
| loss_m = re.search(r'loss (\d+(?:\.\d+)?)%', params) | |
| if loss_m: | |
| intf.netem_loss_pct = float(loss_m.group(1)) | |
| corrupt_m = re.search(r'corrupt (\d+(?:\.\d+)?)%', params) | |
| if corrupt_m: | |
| intf.netem_corrupt_pct = float(corrupt_m.group(1)) | |
| reorder_m = re.search(r'reorder (\d+(?:\.\d+)?)%', params) | |
| if reorder_m: | |
| intf.netem_reorder_pct = float(reorder_m.group(1)) | |
| dup_m = re.search(r'duplicate (\d+(?:\.\d+)?)%', params) | |
| if dup_m: | |
| intf.netem_dup_pct = float(dup_m.group(1)) | |
| return "" | |
| def _write_tc_tbf(self, m): | |
| intf_name = m.group(2) | |
| params = m.group(3) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router or intf_name not in router.interfaces: | |
| return "" | |
| intf = router.interfaces[intf_name] | |
| intf.clear_netem() | |
| rate_m = re.search(r'rate (\d+)kbit', params) | |
| if rate_m: | |
| intf.tbf_rate_kbit = int(rate_m.group(1)) | |
| return "" | |
| def _write_tc_del(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router and intf_name in router.interfaces: | |
| router.interfaces[intf_name].clear_netem() | |
| return "" | |
| def _write_iptables_add(self, m): | |
| chain = m.group(1) | |
| rule = m.group(2).strip() | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| intf_match = re.search(r'-o (\S+)', rule) | |
| if chain == "OUTPUT" and intf_match and "-j DROP" in rule and "-p" not in rule: | |
| intf_name = intf_match.group(1) | |
| if intf_name in router.interfaces: | |
| router.interfaces[intf_name].iptables_output_drop = True | |
| return "" | |
| if not hasattr(router, '_iptables_output_rules'): | |
| router._iptables_output_rules = [] | |
| if chain == "OUTPUT": | |
| router._iptables_output_rules.append(rule) | |
| for intf in router.interfaces.values(): | |
| if chain == "INPUT": | |
| intf.iptables_input_rules.append(rule) | |
| elif chain == "FORWARD": | |
| intf.iptables_forward_rules.append(rule) | |
| return "" | |
| def _write_iptables_del(self, m): | |
| chain = m.group(1) | |
| rule = m.group(2).strip() | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| intf_match = re.search(r'-o (\S+)', rule) | |
| if chain == "OUTPUT" and intf_match and "-j DROP" in rule and "-p" not in rule: | |
| intf_name = intf_match.group(1) | |
| if intf_name in router.interfaces: | |
| router.interfaces[intf_name].iptables_output_drop = False | |
| return "" | |
| if chain == "OUTPUT" and hasattr(router, '_iptables_output_rules'): | |
| router._iptables_output_rules = [r for r in router._iptables_output_rules if r != rule] | |
| for intf in router.interfaces.values(): | |
| if chain == "INPUT": | |
| intf.iptables_input_rules = [r for r in intf.iptables_input_rules if r != rule] | |
| elif chain == "FORWARD": | |
| intf.iptables_forward_rules = [r for r in intf.iptables_forward_rules if r != rule] | |
| return "" | |
| def _write_iptables_flush(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| for intf in router.interfaces.values(): | |
| intf.iptables_output_drop = False | |
| intf.iptables_input_rules.clear() | |
| intf.iptables_forward_rules.clear() | |
| if hasattr(router, '_iptables_output_rules'): | |
| router._iptables_output_rules.clear() | |
| return "" | |
| def _write_route_blackhole(self, m): | |
| prefix = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.static_routes[prefix] = "blackhole" | |
| return "" | |
| def _write_route_add(self, m): | |
| prefix = m.group(1) | |
| nexthop = m.group(2) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.static_routes[prefix] = nexthop | |
| return "" | |
| def _write_route_del_dev(self, m): | |
| prefix = m.group(1) | |
| dev = m.group(2) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.static_routes.pop(prefix, None) | |
| key = f"{prefix}@{dev}" | |
| router.static_routes.pop(key, None) | |
| return "" | |
| def _write_route_add_dev(self, m): | |
| prefix = m.group(1) | |
| dev = m.group(2) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.static_routes[f"{prefix}@{dev}"] = dev | |
| return "" | |
| def _write_route_del_blackhole(self, m): | |
| prefix = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| if prefix in router.static_routes and router.static_routes[prefix] == "blackhole": | |
| del router.static_routes[prefix] | |
| return "" | |
| def _write_route_del(self, m): | |
| prefix = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.static_routes.pop(prefix, None) | |
| for p in list(router.static_routes): | |
| if prefix in p: | |
| router.static_routes.pop(p, None) | |
| return "" | |
| def _write_vtysh_conf(self, m): | |
| conf_str = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| # OSPF cost | |
| cost_m = re.search(r'"interface (\S+)".*"ip ospf cost (\d+)"', conf_str) | |
| if cost_m: | |
| router.ospf_costs[cost_m.group(1)] = int(cost_m.group(2)) | |
| return "" | |
| # OSPF hello/dead timers | |
| hello_m = re.search(r'"interface (\S+)".*"ip ospf hello-interval (\d+)"', conf_str) | |
| dead_m = re.search(r'"interface (\S+)".*"ip ospf dead-interval (\d+)"', conf_str) | |
| if hello_m: | |
| router.ospf_hello[hello_m.group(1)] = int(hello_m.group(2)) | |
| if dead_m: | |
| router.ospf_dead[dead_m.group(1)] = int(dead_m.group(2)) | |
| if hello_m or dead_m: | |
| return "" | |
| # Remove OSPF timers (no ip ospf hello-interval) | |
| rm_hello = re.search(r'"interface (\S+)".*"no ip ospf hello-interval"', conf_str) | |
| rm_dead = re.search(r'"interface (\S+)".*"no ip ospf dead-interval"', conf_str) | |
| if rm_hello: | |
| router.ospf_hello[rm_hello.group(1)] = 10 | |
| if rm_dead: | |
| router.ospf_dead[rm_dead.group(1)] = 40 | |
| if rm_hello or rm_dead: | |
| return "" | |
| # OSPF area | |
| area_m = re.search(r'"router ospf".*"network (\S+) area (\S+)"', conf_str) | |
| if area_m: | |
| subnet = area_m.group(1) | |
| area = area_m.group(2) | |
| for intf in router.interfaces.values(): | |
| if subnet in intf.subnet or intf.ip and subnet.startswith(intf.ip.rsplit('.', 1)[0]): | |
| router.ospf_area[intf.name] = area | |
| return "" | |
| # Remove OSPF area | |
| rm_area = re.search(r'"router ospf".*"no network (\S+) area (\S+)"', conf_str) | |
| if rm_area: | |
| for intf_name in list(router.ospf_area): | |
| if router.ospf_area[intf_name] == rm_area.group(2): | |
| router.ospf_area[intf_name] = "0.0.0.0" | |
| return "" | |
| # BGP redistribute | |
| if '"no redistribute connected"' in conf_str: | |
| router.bgp_redistribute_connected = False | |
| return "" | |
| if '"no redistribute ospf"' in conf_str: | |
| router.bgp_redistribute_ospf = False | |
| return "" | |
| if '"redistribute connected"' in conf_str: | |
| router.bgp_redistribute_connected = True | |
| return "" | |
| if '"redistribute ospf"' in conf_str: | |
| router.bgp_redistribute_ospf = True | |
| return "" | |
| if '"redistribute static"' in conf_str: | |
| router.bgp_redistribute_static = True | |
| return "" | |
| if '"no redistribute static"' in conf_str: | |
| router.bgp_redistribute_static = False | |
| return "" | |
| if '"redistribute bgp"' in conf_str: | |
| return "" | |
| # BGP network | |
| net_m = re.search(r'"network (\S+)"', conf_str) | |
| if net_m and "no network" not in conf_str: | |
| router.bgp_networks.add(net_m.group(1)) | |
| return "" | |
| no_net_m = re.search(r'"no network (\S+)"', conf_str) | |
| if no_net_m: | |
| router.bgp_networks.discard(no_net_m.group(1)) | |
| return "" | |
| # BGP prefix-list | |
| if '"ip prefix-list BLOCK_ALL deny any"' in conf_str: | |
| peer_m = re.search(r'"neighbor (\S+) prefix-list BLOCK_ALL in"', conf_str) | |
| if peer_m: | |
| peer_ip = peer_m.group(1) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip]["prefix_list_in"] = "BLOCK_ALL" | |
| return "" | |
| if '"no ip prefix-list BLOCK_ALL"' in conf_str: | |
| for cfg in router.bgp_peer_config.values(): | |
| cfg.pop("prefix_list_in", None) | |
| return "" | |
| no_pl = re.search(r'"no neighbor (\S+) prefix-list', conf_str) | |
| if no_pl: | |
| peer_ip = no_pl.group(1) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip].pop("prefix_list_in", None) | |
| return "" | |
| # BGP route-map set local-preference | |
| lp_m = re.search(r'"route-map \S+ permit \d+".*"set local-preference (\d+)"', conf_str) | |
| peer_rm = re.search(r'"neighbor (\S+) route-map \S+ in"', conf_str) | |
| if lp_m and peer_rm: | |
| peer_ip = peer_rm.group(1) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip]["local_pref_in"] = int(lp_m.group(1)) | |
| return "" | |
| # BGP route-map set as-path prepend | |
| prepend_m = re.search(r'"set as-path prepend (.+?)"', conf_str) | |
| peer_out = re.search(r'"neighbor (\S+) route-map \S+ out"', conf_str) | |
| if prepend_m and peer_out: | |
| peer_ip = peer_out.group(1) | |
| count = len(prepend_m.group(1).split()) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip]["prepend_out"] = count | |
| return "" | |
| # BGP route-map set community | |
| comm_m = re.search(r'"set community (\S+)"', conf_str) | |
| if comm_m and peer_out: | |
| peer_ip = peer_out.group(1) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip]["community_out"] = comm_m.group(1) | |
| return "" | |
| # Remove route-map | |
| peer_rm_del = re.search(r'"no neighbor (\S+) route-map', conf_str) | |
| if peer_rm_del: | |
| peer_ip = peer_rm_del.group(1) | |
| if peer_ip in router.bgp_peer_config: | |
| router.bgp_peer_config[peer_ip].pop("local_pref_in", None) | |
| router.bgp_peer_config[peer_ip].pop("prepend_out", None) | |
| router.bgp_peer_config[peer_ip].pop("community_out", None) | |
| return "" | |
| return "" | |
| def _write_start_daemon(self, m): | |
| daemon = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| if daemon == "ospfd": | |
| router.ospf_daemon_alive = True | |
| elif daemon == "bgpd": | |
| router.bgp_daemon_alive = True | |
| for key in list(self._engine.bgp._session_down_until): | |
| if key[0] == self._name: | |
| del self._engine.bgp._session_down_until[key] | |
| return "" | |
| def _write_kill(self, m): | |
| return "" | |
| def _write_pkill(self, m): | |
| pattern = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if not router: | |
| return "" | |
| if "iperf" in pattern: | |
| router.bg_iperf = False | |
| if "dd if=/dev" in pattern or "gzip" in pattern or "yes" in pattern: | |
| router.bg_cpu_hog = False | |
| router.cpu_exhausted = False | |
| if "ip link set" in pattern: | |
| for k in list(router.bg_flapping): | |
| router.bg_flapping[k] = False | |
| return "" | |
| def _write_iperf(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.bg_iperf = True | |
| return "" | |
| def _write_cpu_hog(self, m): | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.bg_cpu_hog = True | |
| router.cpu_exhausted = True | |
| return "" | |
| def _write_flapping(self, m): | |
| intf_name = m.group(1) | |
| topo = self._engine.topo | |
| router = topo.routers.get(self._name) | |
| if router: | |
| router.bg_flapping[intf_name] = True | |
| return "" | |
| class SimIntfProxy: | |
| """Mimics a Mininet interface.""" | |
| def __init__(self, intf): | |
| self._intf = intf | |
| self.link = None | |
| def name(self) -> str: | |
| return self._intf.name | |
| def IP(self) -> str: | |
| return self._intf.ip | |
| def prefixLen(self) -> int: | |
| return self._intf.mask | |