From bed71fa3f89505e7c048e07c47474f5291e47472 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 24 Oct 2023 01:41:12 +0200 Subject: [PATCH] Added physical layer link stats to link and packet classes --- RNS/Link.py | 26 ++++++++++++++++++++++++++ RNS/Packet.py | 1 + RNS/Transport.py | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/RNS/Link.py b/RNS/Link.py index afc7411..3f77e25 100644 --- a/RNS/Link.py +++ b/RNS/Link.py @@ -153,6 +153,9 @@ class Link: self.rx = 0 self.txbytes = 0 self.rxbytes = 0 + self.rssi = None + self.snr = None + self.q = None self.traffic_timeout_factor = Link.TRAFFIC_TIMEOUT_FACTOR self.keepalive_timeout_factor = Link.KEEPALIVE_TIMEOUT_FACTOR self.keepalive = Link.KEEPALIVE @@ -472,6 +475,7 @@ class Link: self.teardown_reason = Link.DESTINATION_CLOSED else: self.teardown_reason = Link.INITIATOR_CLOSED + self.__update_phy_stats(packet) self.link_closed() except Exception as e: pass @@ -577,6 +581,14 @@ class Link: sleep(sleep_time) + def __update_phy_stats(self, packet): + if packet.rssi != None: + self.rssi = packet.rssi + if packet.snr != None: + self.snr = packet.snr + if packet.q != None: + self.q = packet.q + def send_keepalive(self): keepalive_packet = RNS.Packet(self, bytes([0xFF]), context=RNS.Packet.KEEPALIVE) keepalive_packet.send() @@ -707,6 +719,8 @@ class Link: except Exception as e: RNS.log("Error while executing proof request callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) + self.__update_phy_stats(packet) + elif packet.context == RNS.Packet.LINKIDENTIFY: plaintext = self.decrypt(packet.data) @@ -724,6 +738,8 @@ class Link: self.callbacks.remote_identified(self, self.__remote_identity) except Exception as e: RNS.log("Error while executing remote identified callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) + + self.__update_phy_stats(packet) elif packet.context == RNS.Packet.REQUEST: try: @@ -731,6 +747,7 @@ class Link: packed_request = self.decrypt(packet.data) unpacked_request = umsgpack.unpackb(packed_request) self.handle_request(request_id, unpacked_request) + self.__update_phy_stats(packet) except Exception as e: RNS.log("Error occurred while handling request. The contained exception was: "+str(e), RNS.LOG_ERROR) @@ -742,18 +759,21 @@ class Link: response_data = unpacked_response[1] transfer_size = len(umsgpack.packb(response_data))-2 self.handle_response(request_id, response_data, transfer_size, transfer_size) + self.__update_phy_stats(packet) except Exception as e: RNS.log("Error occurred while handling response. The contained exception was: "+str(e), RNS.LOG_ERROR) elif packet.context == RNS.Packet.LRRTT: if not self.initiator: self.rtt_packet(packet) + self.__update_phy_stats(packet) elif packet.context == RNS.Packet.LINKCLOSE: self.teardown_packet(packet) elif packet.context == RNS.Packet.RESOURCE_ADV: packet.plaintext = self.decrypt(packet.data) + self.__update_phy_stats(packet) if RNS.ResourceAdvertisement.is_request(packet): RNS.Resource.accept(packet, callback=self.request_resource_concluded) @@ -781,6 +801,7 @@ class Link: elif packet.context == RNS.Packet.RESOURCE_REQ: plaintext = self.decrypt(packet.data) + self.__update_phy_stats(packet) if ord(plaintext[:1]) == RNS.Resource.HASHMAP_IS_EXHAUSTED: resource_hash = plaintext[1+RNS.Resource.MAPHASH_LEN:RNS.Identity.HASHLENGTH//8+1+RNS.Resource.MAPHASH_LEN] else: @@ -796,6 +817,7 @@ class Link: elif packet.context == RNS.Packet.RESOURCE_HMU: plaintext = self.decrypt(packet.data) + self.__update_phy_stats(packet) resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8] for resource in self.incoming_resources: if resource_hash == resource.hash: @@ -803,6 +825,7 @@ class Link: elif packet.context == RNS.Packet.RESOURCE_ICL: plaintext = self.decrypt(packet.data) + self.__update_phy_stats(packet) resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8] for resource in self.incoming_resources: if resource_hash == resource.hash: @@ -822,6 +845,7 @@ class Link: elif packet.context == RNS.Packet.RESOURCE: for resource in self.incoming_resources: resource.receive_part(packet) + self.__update_phy_stats(packet) elif packet.context == RNS.Packet.CHANNEL: if not self._channel: @@ -842,6 +866,7 @@ class Link: packet.prove() plaintext = self.decrypt(packet.data) + self.__update_phy_stats(packet) self._channel._receive(plaintext) elif packet.packet_type == RNS.Packet.PROOF: @@ -850,6 +875,7 @@ class Link: for resource in self.outgoing_resources: if resource_hash == resource.hash: resource.validate_proof(packet.data) + self.__update_phy_stats(packet) self.watchdog_lock = False diff --git a/RNS/Packet.py b/RNS/Packet.py index 5dc8ea3..3d62451 100755 --- a/RNS/Packet.py +++ b/RNS/Packet.py @@ -138,6 +138,7 @@ class Packet: self.receiving_interface = None self.rssi = None self.snr = None + self.q = None def get_packed_flags(self): if self.context == Packet.LRPROOF: diff --git a/RNS/Transport.py b/RNS/Transport.py index afd2710..d4681a7 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -116,6 +116,7 @@ class Transport: local_client_rssi_cache = [] local_client_snr_cache = [] + local_client_q_cache = [] LOCAL_CLIENT_CACHE_MAXSIZE = 512 pending_local_path_requests = {} @@ -1096,6 +1097,15 @@ class Transport: while len(Transport.local_client_snr_cache) > Transport.LOCAL_CLIENT_CACHE_MAXSIZE: Transport.local_client_snr_cache.pop(0) + if hasattr(interface, "r_stat_q"): + if interface.r_stat_q != None: + packet.q = interface.r_stat_q + if len(Transport.local_client_interfaces) > 0: + Transport.local_client_q_cache.append([packet.packet_hash, packet.q]) + + while len(Transport.local_client_q_cache) > Transport.LOCAL_CLIENT_CACHE_MAXSIZE: + Transport.local_client_q_cache.pop(0) + if len(Transport.local_client_interfaces) > 0: if Transport.is_local_client_interface(interface): packet.hops -= 1