From 134dc6777c0dd29404e61b36d4094f4e35b88b3e Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 13 Nov 2026 01:07:12 +0100 Subject: [PATCH] Added filtered traffic stats and display in rnstatus --- RNS/Interfaces/Interface.py | 2 ++ RNS/Reticulum.py | 16 +++++++++++++++ RNS/Transport.py | 41 ++++++++++++++++++++++++++++++++++--- RNS/Utilities/rnstatus.py | 33 ++++++++++++++++++++++++++--- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/RNS/Interfaces/Interface.py b/RNS/Interfaces/Interface.py index 66b7df15..c5afc2d3 100755 --- a/RNS/Interfaces/Interface.py +++ b/RNS/Interfaces/Interface.py @@ -98,6 +98,8 @@ class Interface: def __init__(self): self.rxb = 0 self.txb = 0 + self.frxb = 0 + self.ftxb = 0 self.arxb = 0 self.atxb = 0 self.arxc = 0 diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index aecc0dbe..6a6961c6 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -1727,6 +1727,16 @@ class Reticulum: else: ifstats["txs"] = 0 else: ifstats["txs"] = 0 + if hasattr(interface, "current_frx_speed"): + if interface.current_frx_speed != None: ifstats["frxs"] = interface.current_frx_speed + else: ifstats["frxs"] = 0 + else: ifstats["frxs"] = 0 + + if hasattr(interface, "current_ftx_speed"): + if interface.current_ftx_speed != None: ifstats["ftxs"] = interface.current_ftx_speed + else: ifstats["ftxs"] = 0 + else: ifstats["ftxs"] = 0 + if hasattr(interface, "current_arx_speed"): if interface.current_arx_speed != None: ifstats["arxs"] = interface.current_arx_speed else: ifstats["arxs"] = 0 @@ -1781,6 +1791,8 @@ class Reticulum: ifstats["mtu"] = interface.HW_MTU ifstats["rxb"] = interface.rxb ifstats["txb"] = interface.txb + ifstats["frxb"] = interface.frxb + ifstats["ftxb"] = interface.ftxb ifstats["arxb"] = interface.arxb ifstats["atxb"] = interface.atxb ifstats["arxc"] = interface.arxc @@ -1831,6 +1843,10 @@ class Reticulum: stats["txb"] = RNS.Transport.traffic_txb stats["rxs"] = RNS.Transport.speed_rx stats["txs"] = RNS.Transport.speed_tx + stats["frxb"] = RNS.Transport.filtered_rxb + stats["ftxb"] = RNS.Transport.filtered_txb + stats["frxs"] = RNS.Transport.filtered_speed_rx + stats["ftxs"] = RNS.Transport.filtered_speed_tx stats["arxb"] = RNS.Transport.announce_rxb stats["atxb"] = RNS.Transport.announce_txb stats["arxs"] = RNS.Transport.announce_speed_rx diff --git a/RNS/Transport.py b/RNS/Transport.py index b3e78c5e..6cc74700 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -379,6 +379,8 @@ class Transport: traffic_rxb = 0 traffic_txb = 0 + filtered_rxb = 0 + filtered_txb = 0 announce_rxc = 0 announce_txc = 0 announce_rxb = 0 @@ -389,6 +391,8 @@ class Transport: pr_txb = 0 speed_rx = 0 speed_tx = 0 + filtered_speed_rx = 0 + filtered_speed_tx = 0 announce_speed_rx = 0 announce_speed_tx = 0 announce_freq_rx = 0 @@ -749,6 +753,7 @@ class Transport: rxb = 0; txb = 0; rxs = 0; txs = 0; arxb = 0; atxb = 0; arxs = 0; atxs = 0; prxb = 0; ptxb = 0; prxs = 0; ptxs = 0; + frxb = 0; ftxb = 0; frxs = 0; ftxs = 0; iafreq = 0; oafreq = 0; ipfreq = 0; opfreq = 0 for interface in Transport.interfaces: @@ -760,6 +765,7 @@ class Transport: irxb = interface.rxb; itxb = interface.txb iarxb = interface.arxb; iatxb = interface.atxb iprxb = interface.prxb; iptxb = interface.ptxb + ifrxb = interface.frxb; iftxb = interface.ftxb tc = interface.transport_traffic_counter ts_diff = now - tc["ts"] @@ -767,6 +773,7 @@ class Transport: rx_diff = irxb - tc["rxb"]; tx_diff = itxb - tc["txb"] arx_diff = iarxb - tc["arxb"]; atx_diff = iatxb - tc["atxb"] prx_diff = iprxb - tc["prxb"]; ptx_diff = iptxb - tc["ptxb"] + frx_diff = ifrxb - tc["frxb"]; ftx_diff = iftxb - tc["ftxb"] rxb += rx_diff; crxs = (rx_diff*8)/ts_diff txb += tx_diff; ctxs = (tx_diff*8)/ts_diff @@ -774,6 +781,8 @@ class Transport: atxb += atx_diff; catxs = (atx_diff*8)/ts_diff prxb += prx_diff; cprxs = (prx_diff*8)/ts_diff ptxb += ptx_diff; cptxs = (ptx_diff*8)/ts_diff + frxb += frx_diff; cfrxs = (frx_diff*8)/ts_diff + ftxb += ftx_diff; cftxs = (ftx_diff*8)/ts_diff iafreq += interface.incoming_announce_frequency() oafreq += interface.outgoing_announce_frequency() @@ -786,16 +795,20 @@ class Transport: interface.current_atx_speed = catxs; atxs += catxs interface.current_prx_speed = cprxs; prxs += cprxs interface.current_ptx_speed = cptxs; ptxs += cptxs + interface.current_frx_speed = cfrxs; frxs += cfrxs + interface.current_ftx_speed = cftxs; ftxs += cftxs tc["ts"] = now tc["rxb"] = irxb; tc["txb"] = itxb tc["arxb"] = iarxb; tc["atxb"] = iatxb tc["prxb"] = iprxb; tc["ptxb"] = iptxb + tc["frxb"] = ifrxb; tc["ftxb"] = iftxb else: interface.transport_traffic_counter = { "ts": time.time(), "rxb": interface.rxb, "txb": interface.txb, "arxb": interface.arxb, "atxb": interface.atxb, - "prxb": interface.prxb, "ptxb": interface.ptxb } + "prxb": interface.prxb, "ptxb": interface.ptxb, + "frxb": interface.frxb, "ftxb": interface.ftxb } if not Transport.start_time: rpps = 0; tpps = 0 else: if not cts: ts = Transport.start_time @@ -812,6 +825,10 @@ class Transport: Transport.traffic_txb += txb Transport.speed_rx = rxs Transport.speed_tx = txs + Transport.filtered_rxb += frxb + Transport.filtered_txb += ftxb + Transport.filtered_speed_rx = frxs + Transport.filtered_speed_tx = ftxs Transport.announce_rxb += arxb Transport.announce_txb += atxb Transport.announce_speed_rx = arxs @@ -1919,12 +1936,24 @@ class Transport: RNS.log("Filtered packet with hash "+RNS.prettyhexrep(packet.packet_hash), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None return False + @staticmethod + def filtered_inbound(raw=None, interface=None): + if interface: + raw_len = len(raw) if raw else 0 + interface.rxb -= raw_len; interface.frxb += raw_len + if interface.parent_interface: + interface.parent_interface.rxb -= raw_len + interface.parent_interface.frxb += raw_len + @staticmethod def inbound(raw, interface=None, tc=None, ifac_handled=False): - try: Transport.preprocess_inbound(raw=raw, interface=interface, tc=tc, ifac_handled=ifac_handled) + try: + if not Transport.preprocess_inbound(raw=raw, interface=interface, tc=tc, ifac_handled=ifac_handled): + Transport.filtered_inbound(raw=raw, interface=interface) except Exception as e: RNS.log(f"Error while processing inbound on {interface}: {e}", RNS.LOG_ERROR) RNS.trace_exception(e) + Transport.filtered_inbound(raw=raw, interface=interface) @staticmethod def handle_ifac(raw, interface): @@ -1992,6 +2021,10 @@ class Transport: @staticmethod def preprocess_inbound(raw, interface=None, tc=None, ifac_handled=False): + if not interface: + RNS.log(f"No interface set on inbound packet, dropping", RNS.LOG_WARNING) + return None + if not Transport.ready: wait_start = time.time() while not Transport.ready: @@ -2037,7 +2070,6 @@ class Transport: if not Transport.packet_filter(packet): return interface.packet_filter_hit() traffic_class = tc or Transport.TC_DATA - Transport.rx_packets += 1 packet.receiving_interface = interface packet.hops += 1 @@ -2177,6 +2209,7 @@ class Transport: # locally on this system. Might need adding a local_client_dest_map, # but this will need to be kept *tightly* in-sync. + Transport.rx_packets += 1 if not Transport.USE_INBOUND_QUEUE: return Transport._inbound(packet) else: if traffic_class == Transport.TC_INGRESS_LIMITED: packet.traffic_class = Transport.TC_INGRESS_LIMITED @@ -2184,6 +2217,8 @@ class Transport: except Full: RNS.log(f"Dropping inbound packet, queue is full (tc={traffic_class})", RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None except Exception as e: RNS.log(f"Error while inserting into inbound queue: {e}", RNS.LOG_ERROR) + return True + @staticmethod def inbound_job(): while Transport._should_run: diff --git a/RNS/Utilities/rnstatus.py b/RNS/Utilities/rnstatus.py index 0e72c82b..5037bdd1 100644 --- a/RNS/Utilities/rnstatus.py +++ b/RNS/Utilities/rnstatus.py @@ -160,7 +160,7 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json= sorting=None, sort_reverse=False, remote=None, management_identity=None, must_exit=True, rns_instance=None, traffic_totals=False, discovered_interfaces=False, config_entries=False, burst_filter=False, blocked_ips=False, queue_stats=False, pps=False, profiling=False, remote_timeout=RNS.Transport.PATH_REQUEST_TIMEOUT, - attach=None, detach=None, reload=None, show_stale=False, show_unknown=False): + attach=None, detach=None, reload=None, show_stale=False, show_unknown=False, fstats=False): if remote: require_shared = False else: require_shared = True @@ -432,8 +432,12 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json= if sorting == "rate" or sorting == "bitrate": interfaces.sort(key=lambda i: i["bitrate"], reverse=not sort_reverse) if sorting == "rx": interfaces.sort(key=lambda i: i["rxb"], reverse=not sort_reverse) if sorting == "tx": interfaces.sort(key=lambda i: i["txb"], reverse=not sort_reverse) + if sorting == "frx": interfaces.sort(key=lambda i: i["frxb"], reverse=not sort_reverse) + if sorting == "ftx": interfaces.sort(key=lambda i: i["ftxb"], reverse=not sort_reverse) if sorting == "rxs": interfaces.sort(key=lambda i: i["rxs"], reverse=not sort_reverse) if sorting == "txs": interfaces.sort(key=lambda i: i["txs"], reverse=not sort_reverse) + if sorting == "frxs": interfaces.sort(key=lambda i: i["frxs"], reverse=not sort_reverse) + if sorting == "ftxs": interfaces.sort(key=lambda i: i["ftxs"], reverse=not sort_reverse) if sorting == "traffic": interfaces.sort(key=lambda i: i["rxb"]+i["txb"], reverse=not sort_reverse) if sorting == "anns" or sorting == "announces": interfaces.sort(key=lambda i: i["incoming_announce_frequency"]+i["outgoing_announce_frequency"], reverse=not sort_reverse) if sorting == "arx": interfaces.sort(key=lambda i: i["incoming_announce_frequency"], reverse=not sort_reverse) @@ -760,6 +764,14 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json= print(f" Traffic : {txstat}\n {rxstat}") + if fstats: + frxstat = "↓"+RNS.prettysize(ifstat["frxb"]) + ftxstat = "↑"+RNS.prettysize(ifstat["ftxb"]) + if "frxs" in ifstat and "ftxs" in ifstat: + frxstat += " "+RNS.prettyspeed(ifstat["frxs"]) + ftxstat += " "+RNS.prettyspeed(ifstat["ftxs"]) + print(f" Filtered : {ftxstat}\n {frxstat}") + lstr = "" if link_count != None and lstats: ms = "y" if link_count == 1 else "ies" @@ -800,6 +812,19 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json= print(f"\n Totals : {txstat}\n {rxstat}") + if fstats: + frxstat = "↓"+RNS.prettysize(stats["frxb"]) + ftxstat = "↑"+RNS.prettysize(stats["ftxb"]) + + strdiff = len(frxstat)-len(ftxstat) + if strdiff > 0: ftxstat += " "*strdiff + elif strdiff < 0: frxstat += " "*-strdiff + + if "frxs" in ifstat and "ftxs" in ifstat: + frxstat += " "+RNS.prettyspeed(stats["frxs"]) + ftxstat += " "+RNS.prettyspeed(stats["ftxs"]) + print(f"\n Filtered : {ftxstat}\n {frxstat}") + if pstats: if "prxb" in stats and "ptxb" in stats and "prxs" in stats and "ptxs" in stats: prxb_str = "↓"+RNS.prettysize(stats["prxb"]) @@ -895,6 +920,7 @@ def main(must_exit=True, rns_instance=None): parser.add_argument("-a", "--all", action="store_true", help="show all interfaces", default=False) parser.add_argument("-A", "--announce-stats", action="store_true", help="show announce stats", default=False) parser.add_argument("-P", "--pr-stats", action="store_true", help="show path request stats", default=False) + parser.add_argument("-F", "--filter-stats", action="store_true", help="show filtered traffic stats", default=False) parser.add_argument("-l", "--link-stats", action="store_true", help="show link stats", default=False) parser.add_argument("-B", "--burst", action="store_true", help="only show interfaces with active bursts", default=False) parser.add_argument("-b", "--blocked-ips", action="store_true", help="show blocked IPs per interface", default=False) @@ -945,7 +971,7 @@ def main(must_exit=True, rns_instance=None): remote=args.R, management_identity=args.i, remote_timeout=args.w, must_exit=False, rns_instance=reticulum, traffic_totals=args.totals, discovered_interfaces=args.discovered, config_entries=args.D, burst_filter=args.burst, blocked_ips=args.blocked_ips, queue_stats=args.queues, pps=args.pps, profiling=args.profiling, - show_stale=args.show_stale, show_unknown=args.show_unknown) + show_stale=args.show_stale, show_unknown=args.show_unknown, fstats=args.filter_stats) finally: sys.stdout = old_stdout @@ -964,7 +990,8 @@ def main(must_exit=True, rns_instance=None): remote=args.R, management_identity=args.i, remote_timeout=args.w, must_exit=must_exit, rns_instance=rns_instance, traffic_totals=args.totals, discovered_interfaces=args.discovered, config_entries=args.D, burst_filter=args.burst, blocked_ips=args.blocked_ips, queue_stats=args.queues, pps=args.pps, profiling=args.profiling, - attach=args.attach, detach=args.detach, reload=args.reload, show_stale=args.show_stale, show_unknown=args.show_unknown) + attach=args.attach, detach=args.detach, reload=args.reload, show_stale=args.show_stale, + show_unknown=args.show_unknown, fstats=args.filter_stats) except KeyboardInterrupt: print("")