From e3a716224d3d6d0217f57e345cbf28caf724c867 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 14 Jan 2025 18:17:53 +0100 Subject: [PATCH] Implemented MTU autoconfiguration on interfaces --- RNS/Interfaces/Interface.py | 29 ++++++++++++++++++++++++++++- RNS/Interfaces/LocalInterface.py | 4 +++- RNS/Interfaces/TCPInterface.py | 5 ++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/RNS/Interfaces/Interface.py b/RNS/Interfaces/Interface.py index 580bca4..f025fe1 100755 --- a/RNS/Interfaces/Interface.py +++ b/RNS/Interfaces/Interface.py @@ -64,12 +64,14 @@ class Interface: IC_BURST_PENALTY = 5*60 IC_HELD_RELEASE_INTERVAL = 30 + AUTOCONFIGURE_MTU = False + def __init__(self): self.rxb = 0 self.txb = 0 self.created = time.time() self.online = False - self.bitrate = 1e6 + self.bitrate = 62500 self.HW_MTU = None self.ingress_control = True @@ -118,6 +120,31 @@ class Interface: else: return False + def optimise_mtu(self): + if self.AUTOCONFIGURE_MTU: + if self.bitrate > 16_000_000: + self.HW_MTU = 262144 + elif self.bitrate > 8_000_000: + self.HW_MTU = 131072 + elif self.bitrate > 4_000_000: + self.HW_MTU = 65536 + elif self.bitrate > 2_000_000: + self.HW_MTU = 32768 + elif self.bitrate > 1_000_000: + self.HW_MTU = 16384 + elif self.bitrate > 500_000: + self.HW_MTU = 8192 + elif self.bitrate > 250_000: + self.HW_MTU = 4096 + elif self.bitrate > 125_000: + self.HW_MTU = 2048 + elif self.bitrate > 62_500: + self.HW_MTU = 1024 + else: + self.HW_MTU = None + + RNS.log(f"{self} hardware MTU set to {self.HW_MTU}", RNS.LOG_DEBUG) # TODO: Remove debug + def age(self): return time.time()-self.created diff --git a/RNS/Interfaces/LocalInterface.py b/RNS/Interfaces/LocalInterface.py index b06a6b3..31195c5 100644 --- a/RNS/Interfaces/LocalInterface.py +++ b/RNS/Interfaces/LocalInterface.py @@ -52,6 +52,7 @@ class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): class LocalClientInterface(Interface): RECONNECT_WAIT = 8 + AUTOCONFIGURE_MTU = True def __init__(self, owner, name, target_port = None, connected_socket=None): super().__init__() @@ -86,7 +87,7 @@ class LocalClientInterface(Interface): self.connect() self.owner = owner - self.bitrate = 1000*1000*1000 + self.bitrate = 1_000_000_000 self.online = True self.writing = False @@ -288,6 +289,7 @@ class LocalClientInterface(Interface): class LocalServerInterface(Interface): + AUTOCONFIGURE_MTU = True def __init__(self, owner, bindport=None): super().__init__() diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index abaecba..ad7e5d3 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -67,6 +67,7 @@ class ThreadingTCP6Server(socketserver.ThreadingMixIn, socketserver.TCPServer): class TCPClientInterface(Interface): BITRATE_GUESS = 10*1000*1000 DEFAULT_IFAC_SIZE = 16 + AUTOCONFIGURE_MTU = True RECONNECT_WAIT = 5 RECONNECT_MAX_TRIES = None @@ -406,8 +407,9 @@ class TCPClientInterface(Interface): class TCPServerInterface(Interface): - BITRATE_GUESS = 10*1000*1000 + BITRATE_GUESS = 10_000_000 DEFAULT_IFAC_SIZE = 16 + AUTOCONFIGURE_MTU = True @staticmethod def get_address_for_if(name, bind_port, prefer_ipv6=False): @@ -528,6 +530,7 @@ class TCPServerInterface(Interface): spawned_interface.target_port = str(handler.client_address[1]) spawned_interface.parent_interface = self spawned_interface.bitrate = self.bitrate + spawned_interface.optimise_mtu() spawned_interface.ifac_size = self.ifac_size spawned_interface.ifac_netname = self.ifac_netname