Spaces:
Running
Running
Download server/sim/rib.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 5.38 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/rib.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/sim/rib.py
-
curl -L -o rib.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/rib.py
5.38 kB
| """Routing Information Base — merges connected, static, OSPF, and BGP routes. | |
| Produces FRR-compatible 'show ip route json' dicts and provides longest-prefix | |
| match forwarding lookup. | |
| """ | |
| import ipaddress | |
| PREFERENCE = {"connected": 0, "kernel": 0, "static": 1, "ospf": 110, "ebgp": 20, "ibgp": 200, "bgp": 20} | |
| class RIBEngine: | |
| def __init__(self, engine): | |
| self._engine = engine | |
| def compute_rib(self, router_name: str) -> dict: | |
| """Merged routing table. Returns FRR-compatible dict.""" | |
| topo = self._engine.topo | |
| router = topo.routers.get(router_name) | |
| if not router: | |
| return {} | |
| entries: dict[str, list[dict]] = {} | |
| for intf in router.interfaces.values(): | |
| if intf.ip and intf.up: | |
| prefix = intf.subnet | |
| self._add_route(entries, prefix, "connected", 0, [ | |
| {"directlyConnected": True, "interfaceName": intf.name, "active": True} | |
| ]) | |
| if router.loopback: | |
| self._add_route(entries, f"{router.loopback}/32", "connected", 0, [ | |
| {"directlyConnected": True, "interfaceName": "lo", "active": True} | |
| ]) | |
| for prefix, nexthop in router.static_routes.items(): | |
| if nexthop == "blackhole": | |
| self._add_route(entries, prefix, "static", 1, [ | |
| {"blackhole": True, "active": True} | |
| ]) | |
| else: | |
| out_intf = self._resolve_nexthop_intf(router, nexthop) | |
| self._add_route(entries, prefix, "static", 1, [ | |
| {"ip": nexthop, "interfaceName": out_intf or "", "active": True} | |
| ]) | |
| if router.ospf_daemon_alive: | |
| ospf_routes = self._engine.ospf.run_spf(router_name) | |
| for prefix, (nh, cost, out_intf) in ospf_routes.items(): | |
| self._add_route(entries, prefix, "ospf", cost, [ | |
| {"ip": nh, "interfaceName": out_intf, "active": True} | |
| ]) | |
| if router.bgp_daemon_alive: | |
| best = self._engine.bgp.best_path(router_name) | |
| for prefix, route in best.items(): | |
| peer_node = self._engine.topo.ip_to_node(route.next_hop) | |
| peer_router = self._engine.topo.routers.get(peer_node) if peer_node else None | |
| is_ebgp = peer_router.asn != router.asn if peer_router else True | |
| proto = "bgp" | |
| pref_key = "ebgp" if is_ebgp else "ibgp" | |
| out_intf = self._resolve_nexthop_intf(router, route.next_hop) | |
| self._add_route(entries, prefix, proto, 0, [ | |
| {"ip": route.next_hop, "interfaceName": out_intf or "", "active": True} | |
| ], pref_key=pref_key) | |
| result = {} | |
| for prefix, route_list in entries.items(): | |
| route_list.sort(key=lambda r: PREFERENCE.get(r.get("_pref_key", r["protocol"]), 999)) | |
| for i, r in enumerate(route_list): | |
| r["selected"] = (i == 0) | |
| r["installed"] = (i == 0) | |
| result[prefix] = route_list | |
| return result | |
| def _add_route(self, entries, prefix, protocol, metric, nexthops, pref_key=None): | |
| entry = { | |
| "protocol": protocol, | |
| "metric": metric, | |
| "selected": False, | |
| "installed": False, | |
| "nexthops": nexthops, | |
| "_pref_key": pref_key or protocol, | |
| } | |
| entries.setdefault(prefix, []).append(entry) | |
| def _resolve_nexthop_intf(self, router, nexthop_ip: str) -> str | None: | |
| try: | |
| nh = ipaddress.ip_address(nexthop_ip) | |
| except ValueError: | |
| return None | |
| for intf in router.interfaces.values(): | |
| if intf.ip: | |
| net = ipaddress.ip_network(f"{intf.ip}/{intf.mask}", strict=False) | |
| if nh in net: | |
| return intf.name | |
| return None | |
| def lookup(self, router_name: str, dst_ip: str) -> tuple[str | None, str | None]: | |
| """Longest prefix match. Returns (next_hop_ip_or_None, out_interface_or_None). | |
| Returns ("blackhole", None) for blackhole routes.""" | |
| rib = self.compute_rib(router_name) | |
| try: | |
| dst = ipaddress.ip_address(dst_ip) | |
| except ValueError: | |
| return None, None | |
| best_match = None | |
| best_prefix_len = -1 | |
| for prefix_str, route_list in rib.items(): | |
| try: | |
| net = ipaddress.ip_network(prefix_str, strict=False) | |
| except ValueError: | |
| continue | |
| if dst in net and net.prefixlen > best_prefix_len: | |
| for r in route_list: | |
| if r.get("selected"): | |
| nhs = r.get("nexthops", []) | |
| if nhs: | |
| nh = nhs[0] | |
| if nh.get("blackhole"): | |
| best_match = ("blackhole", None) | |
| elif nh.get("directlyConnected"): | |
| best_match = (dst_ip, nh.get("interfaceName")) | |
| else: | |
| best_match = (nh.get("ip"), nh.get("interfaceName")) | |
| best_prefix_len = net.prefixlen | |
| break | |
| return best_match if best_match else (None, None) | |