Skip to content

Commit

Permalink
Improve protocol overhead handling for MTU tlm
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Aug 12, 2024
1 parent ca7c600 commit 7746da0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions runtimepy/net/arbiter/struct/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# internal
from runtimepy.net.arbiter.info import AppInfo, RuntimeStruct
from runtimepy.net.mtu import UDP_DEFAULT_MTU
from runtimepy.net.mtu import UDP_DEFAULT_MTU, UDP_HEADER_SIZE
from runtimepy.net.udp.connection import UdpConnection
from runtimepy.primitives import Uint16, Uint64
from runtimepy.primitives.serializable.framer import SerializableFramer
Expand Down Expand Up @@ -128,7 +128,9 @@ def get_payload(probe_size: int) -> bytes:

# This actually sends data over this connection.
self.framer_tx.set_mtu(
self.mtu(probe_create=get_payload), logger=self.logger
self.mtu(probe_create=get_payload),
logger=self.logger,
protocol_overhead=UDP_HEADER_SIZE,
)

def assign_app_tx(self, pattern: str, app: AppInfo) -> Optional[T]:
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/net/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from runtimepy.net import IpHost as _IpHost
from runtimepy.net import normalize_host as _normalize_host
from runtimepy.net.connection import BinaryMessage as _BinaryMessage
from runtimepy.net.mtu import UDP_DEFAULT_MTU, host_discover_mtu
from runtimepy.net.mtu import ETHERNET_MTU, UDP_DEFAULT_MTU, host_discover_mtu


class BinaryMessageQueueMixin:
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self, transport: _asyncio.BaseTransport) -> None:
def mtu(
self,
probe_size: int = UDP_DEFAULT_MTU,
fallback: int = UDP_DEFAULT_MTU,
fallback: int = ETHERNET_MTU,
probe_create: Callable[[int], bytes] = bytes,
) -> int:
"""
Expand Down
8 changes: 4 additions & 4 deletions runtimepy/net/mtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class SocketConstants(IntEnum):
IP_PMTUDISC_DO = 2


IP_HEADER_SIZE = 60
UDP_HEADER_SIZE = 8
ETHERNET_MTU = 1500
UDP_DEFAULT_MTU = ETHERNET_MTU - (IP_HEADER_SIZE + UDP_HEADER_SIZE)
IP_HEADER_SIZE = 60
UDP_HEADER_SIZE = IP_HEADER_SIZE + 8
UDP_DEFAULT_MTU = ETHERNET_MTU - UDP_HEADER_SIZE


def socket_discover_mtu(
Expand Down Expand Up @@ -115,7 +115,7 @@ def discover_mtu(
*destination: IpHostlike,
local: IpHost = None,
probe_size: int = UDP_DEFAULT_MTU,
fallback: int = UDP_DEFAULT_MTU,
fallback: int = ETHERNET_MTU,
kind: int = socket.SOCK_DGRAM,
probe_create: Callable[[int], bytes] = bytes,
) -> int:
Expand Down
11 changes: 7 additions & 4 deletions runtimepy/primitives/serializable/framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ def __init__(self, instance: Serializable, mtu: int) -> None:
self.instance = instance
self.set_mtu(mtu)

def set_mtu(self, mtu: int, logger: LoggerType = None) -> int:
def set_mtu(
self, mtu: int, logger: LoggerType = None, protocol_overhead: int = 0
) -> int:
"""Set a new maximum transmission unit for this framer."""

raw_length = self.instance.length()
assert raw_length > 0

self.length = mtu // raw_length
self.length = (mtu - protocol_overhead) // raw_length
assert self.length > 0

self.reset()

if logger is not None:
logger.info(
"Set MTU to %d (%d %d-byte elements).",
mtu,
"Set MTU to %d (%d %d-byte elements, %d bytes overhead).",
mtu - protocol_overhead,
self.length,
raw_length,
protocol_overhead,
)

return self.length
Expand Down

0 comments on commit 7746da0

Please sign in to comment.