From 6b469962b885f6685fee68aa6147dc4023759617 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 7 Aug 2023 16:08:48 -0400 Subject: [PATCH] Simplify the size computation logic. --- src/transport/raw/MessageHeader.h | 39 ++++++++++++++++--------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h index e4644bc44deaf6..aa7185d8ff7412 100644 --- a/src/transport/raw/MessageHeader.h +++ b/src/transport/raw/MessageHeader.h @@ -44,32 +44,33 @@ namespace chip { namespace detail { // Figure out the max size of a packet we can allocate, including all headers. -static constexpr size_t kMaxIPPacketSizeBytes = 1280; -static constexpr size_t kMaxPacketBufferSizeBytes = System::PacketBuffer::kMaxSizeWithoutReserve; -static constexpr size_t kMaxPacketSizeBytes = min(kMaxIPPacketSizeBytes, kMaxPacketBufferSizeBytes); - -// Figure out the max size of our headers. -// System::PacketBuffer::kDefaultHeaderReserve may or may not include space for -// UDP headers, depending on the situation. Make sure we always have enough -// space for UDP headers plus Matter headers. -static constexpr size_t kMaxUDPHeaderSizeBytes = 48; -static constexpr size_t kMaxPacketbufferHeaderSizeBytes = System::PacketBuffer::kDefaultHeaderReserve; -static constexpr size_t kMaxHeaderSizeBytes = - max(kMaxUDPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE, kMaxPacketbufferHeaderSizeBytes); - -static_assert(kMaxPacketSizeBytes > kMaxHeaderSizeBytes, "Need to be able to fit our headers in a packet."); - -static constexpr size_t kMaxMessageSizeBytes = detail::kMaxPacketSizeBytes - detail::kMaxHeaderSizeBytes; +static constexpr size_t kMaxIPPacketSizeBytes = 1280; +static constexpr size_t kMaxUDPAndIPHeaderSizeBytes = 48; + +static_assert(kMaxIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE, + "Matter headers and IP headers must fit in an MTU."); + +// Max space we have for our Application Payload and MIC, per spec. +static constexpr size_t kMaxPerSpecApplicationPayloadAndMICSizeBytes = + kMaxIPPacketSizeBytes - kMaxUDPAndIPHeaderSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE; + +// Max space we have for our Application Payload and MIC in our actual packet +// buffers. This is the size _excluding_ the header reserve. +static constexpr size_t kMaxPacketBufferApplicationPayloadAndMICSizeBytes = System::PacketBuffer::kMaxSize; + +static constexpr size_t kMaxApplicationPayloadAndMICSizeBytes = + min(kMaxPerSpecApplicationPayloadAndMICSizeBytes, kMaxPacketBufferApplicationPayloadAndMICSizeBytes); + } // namespace detail static constexpr size_t kMaxTagLen = 16; -static_assert(detail::kMaxMessageSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message"); +static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message"); // This is somewhat of an under-estimate, because in practice any time we have a // tag we will not have source/destination node IDs, but above we are including -// those in the header size. -static constexpr size_t kMaxAppMessageLen = detail::kMaxMessageSizeBytes - kMaxTagLen; +// those in the header sizes. +static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen; static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;