Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update buffer allocation strategy and maximum buffer size when using TCP as transport #30404

Closed
wants to merge 13 commits into from
20 changes: 20 additions & 0 deletions src/system/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,26 @@
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 15
#endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE */

/**
* @def CHIP_CONFIG_TCP_SUPPORT_CLIENT
*
* @brief
* Indicates whether the current node supports TCP Client
*/
#ifndef CHIP_CONFIG_TCP_SUPPORT_CLIENT
#define CHIP_CONFIG_TCP_SUPPORT_CLIENT 0
#endif /* CHIP_CONFIG_TCP_SUPPORT_CLIENT */

/**
* @def CHIP_CONFIG_TCP_SUPPORT_SERVER
*
* @brief
* Indicates whether the current node supports TCP Server
*/
#ifndef CHIP_CONFIG_TCP_SUPPORT_SERVER
#define CHIP_CONFIG_TCP_SUPPORT_SERVER 0
#endif /* CHIP_CONFIG_TCP_SUPPORT_SERVER */

/**
* @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_LWIP_PBUF_RAM
*
Expand Down
29 changes: 20 additions & 9 deletions src/transport/raw/MessageHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,49 @@

namespace chip {

static constexpr size_t kMaxTagLen = 16;

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 kMaxUdpIPPacketSizeBytes = 1280;
static constexpr size_t kMaxTcpIPPacketSizeBytes = 12800;
static constexpr size_t kMaxUDPAndIPHeaderSizeBytes = 48;

static_assert(kMaxIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE,
static_assert(kMaxUdpIPPacketSizeBytes >= 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;
kMaxUdpIPPacketSizeBytes - 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 =
static constexpr size_t kMaxApplicationUdpPayloadAndMICSizeBytes =
min(kMaxPerSpecApplicationPayloadAndMICSizeBytes, kMaxPacketBufferApplicationPayloadAndMICSizeBytes);

} // namespace detail
static constexpr size_t kMaxApplicationTcpPayloadAndMICSizeBytes =
kMaxTcpIPPacketSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;

static constexpr size_t kMaxTagLen = 16;

static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
static_assert(detail::kMaxApplicationUdpPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
static_assert(detail::kMaxApplicationTcpPayloadAndMICSizeBytes > 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 sizes.
static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen;
static constexpr size_t kMaxUdpAppMessageLen = kMaxApplicationUdpPayloadAndMICSizeBytes - kMaxTagLen;
static constexpr size_t kMaxTcpAppMessageLen = kMaxApplicationTcpPayloadAndMICSizeBytes - kMaxTagLen;
} // namespace detail

static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;

#if CHIP_CONFIG_TCP_SUPPORT_SERVER || CHIP_CONFIG_TCP_SUPPORT_CLIENT
static constexpr size_t kMaxAppMessageLen = detail::kMaxTcpAppMessageLen;
#else
static constexpr size_t kMaxAppMessageLen = detail::kMaxUdpAppMessageLen;
#endif

typedef int PacketHeaderFlags;

namespace Header {
Expand Down