Spaces:
Sleeping
Sleeping
Download server/sim/counters.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 6.58 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/counters.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/sim/counters.py
-
curl -L -o counters.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/sim/counters.py
6.58 kB
| """Interface and protocol counter simulation. | |
| Counters accumulate from a baseline traffic rate, with fault effects (loss, | |
| corruption, duplication) applied to the baseline. Not fabricated — derived | |
| deterministically from fault state and elapsed time. | |
| """ | |
| BASELINE_PKT_RATE = 100 | |
| BASELINE_BYTE_PER_PKT = 500 | |
| class CounterEngine: | |
| def __init__(self, engine): | |
| self._engine = engine | |
| def accumulate(self, engine, seconds: float): | |
| """Called by engine.advance() — accumulate counters for elapsed time.""" | |
| topo = engine.topo | |
| for link in topo.links: | |
| a = topo.get_interface(link.intf_a) | |
| b = topo.get_interface(link.intf_b) | |
| if not a or not b: | |
| continue | |
| if not a.up or not b.up: | |
| continue | |
| a_router = topo.routers.get(a.node) | |
| b_router = topo.routers.get(b.node) | |
| traffic_mult = 1 | |
| if (a_router and a_router.bg_iperf) or (b_router and b_router.bg_iperf): | |
| traffic_mult = 10 | |
| pkts = int(BASELINE_PKT_RATE * seconds * traffic_mult) | |
| byts = pkts * BASELINE_BYTE_PER_PKT | |
| a.tx_pkts += pkts | |
| a.tx_bytes += byts | |
| b.tx_pkts += pkts | |
| b.tx_bytes += byts | |
| a_loss = a.netem_loss_pct / 100.0 | |
| b_loss = b.netem_loss_pct / 100.0 | |
| b.rx_pkts += int(pkts * (1 - a_loss)) | |
| b.rx_bytes += int(byts * (1 - a_loss)) | |
| a_drops = int(pkts * a_loss) | |
| b_drops = int(pkts * b_loss) | |
| b.rx_dropped += a_drops | |
| a.tx_dropped += a_drops | |
| a.rx_pkts += int(pkts * (1 - b_loss)) | |
| a.rx_bytes += int(byts * (1 - b_loss)) | |
| a.rx_dropped += b_drops | |
| b.tx_dropped += b_drops | |
| a_corrupt = int(pkts * a.netem_corrupt_pct / 100.0) | |
| b_corrupt = int(pkts * b.netem_corrupt_pct / 100.0) | |
| b.rx_errors += a_corrupt | |
| a.rx_errors += b_corrupt | |
| b.rx_pkts += int(pkts * a.netem_dup_pct / 100.0) | |
| a.rx_pkts += int(pkts * b.netem_dup_pct / 100.0) | |
| if a.tbf_rate_kbit is not None: | |
| offered_kbit = (byts * 8) / (seconds * 1000) if seconds > 0 else 0 | |
| if offered_kbit > a.tbf_rate_kbit: | |
| drop_ratio = 1.0 - (a.tbf_rate_kbit / offered_kbit) | |
| a.tx_dropped += int(pkts * drop_ratio) | |
| if b.tbf_rate_kbit is not None: | |
| offered_kbit = (byts * 8) / (seconds * 1000) if seconds > 0 else 0 | |
| if offered_kbit > b.tbf_rate_kbit: | |
| drop_ratio = 1.0 - (b.tbf_rate_kbit / offered_kbit) | |
| b.tx_dropped += int(pkts * drop_ratio) | |
| def interface_stats(self, router_name: str) -> list[dict]: | |
| """Produce 'ip -j -s link' compatible output.""" | |
| topo = self._engine.topo | |
| router = topo.routers.get(router_name) | |
| if not router: | |
| return [] | |
| result = [] | |
| lo = { | |
| "ifname": "lo", | |
| "flags": ["LOOPBACK", "UP", "LOWER_UP"], | |
| "mtu": 65536, | |
| "stats64": { | |
| "rx": {"bytes": 0, "packets": 0, "errors": 0, "dropped": 0}, | |
| "tx": {"bytes": 0, "packets": 0, "errors": 0, "dropped": 0}, | |
| }, | |
| } | |
| result.append(lo) | |
| for intf_name in sorted(router.interfaces): | |
| intf = router.interfaces[intf_name] | |
| flags = [] | |
| if intf.up: | |
| flags.extend(["BROADCAST", "MULTICAST", "UP", "LOWER_UP"]) | |
| else: | |
| flags.extend(["BROADCAST", "MULTICAST"]) | |
| result.append({ | |
| "ifname": intf.name, | |
| "flags": flags, | |
| "mtu": intf.mtu, | |
| "stats64": { | |
| "rx": { | |
| "bytes": intf.rx_bytes, | |
| "packets": intf.rx_pkts, | |
| "errors": intf.rx_errors, | |
| "dropped": intf.rx_dropped, | |
| }, | |
| "tx": { | |
| "bytes": intf.tx_bytes, | |
| "packets": intf.tx_pkts, | |
| "errors": 0, | |
| "dropped": intf.tx_dropped, | |
| }, | |
| }, | |
| }) | |
| return result | |
| def queue_stats(self, router_name: str, intf_name: str) -> dict: | |
| """Produce tc qdisc stats dict.""" | |
| topo = self._engine.topo | |
| router = topo.routers.get(router_name) | |
| if not router: | |
| return {"raw": "", "qdiscs": []} | |
| intf = router.interfaces.get(intf_name) | |
| if not intf: | |
| return {"raw": "", "qdiscs": []} | |
| if intf.has_netem: | |
| qtype = "netem" | |
| elif intf.has_tbf: | |
| qtype = "tbf" | |
| else: | |
| qtype = "pfifo_fast" | |
| qdisc = { | |
| "type": qtype, | |
| "raw": "", | |
| "sent_bytes": intf.tx_bytes, | |
| "sent_pkts": intf.tx_pkts, | |
| "dropped": intf.tx_dropped + intf.rx_dropped, | |
| "overlimits": intf.tx_dropped, | |
| "backlog_bytes": "0", | |
| "backlog_pkts": 0, | |
| } | |
| return {"raw": "", "qdiscs": [qdisc]} | |
| def protocol_counters(self, router_name: str) -> dict: | |
| """Produce /proc/net/snmp + netstat compatible dict.""" | |
| topo = self._engine.topo | |
| router = topo.routers.get(router_name) | |
| if not router: | |
| return {"snmp": {}, "netstat": {}} | |
| total_tx = sum(i.tx_pkts for i in router.interfaces.values()) | |
| total_retrans = sum(i.rx_dropped for i in router.interfaces.values()) | |
| total_errors = sum(i.rx_errors for i in router.interfaces.values()) | |
| total_reorder = sum( | |
| int(i.tx_pkts * i.netem_reorder_pct / 100) | |
| for i in router.interfaces.values() | |
| ) | |
| has_loop = any( | |
| intf.name in str(router.static_routes) for intf in router.interfaces.values() | |
| ) | |
| ttl_exceeded = total_tx // 10 if has_loop else 0 | |
| return { | |
| "snmp": { | |
| "Tcp": { | |
| "OutSegs": total_tx, | |
| "RetransSegs": total_retrans, | |
| "InErrs": total_errors, | |
| }, | |
| "Icmp": { | |
| "OutTimeExcds": ttl_exceeded, | |
| }, | |
| }, | |
| "netstat": { | |
| "TcpExt": { | |
| "TCPSACKReorder": total_reorder, | |
| }, | |
| }, | |
| } | |