Skip to content

Commit

Permalink
Simplify the size computation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Aug 7, 2023
1 parent eb00a31 commit 6b46996
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/transport/raw/MessageHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6b46996

Please sign in to comment.