From 05abdf5d0be574c268ef07f321c5a9b155abbb30 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Thu, 30 Jan 2025 13:24:06 +0200 Subject: [PATCH] net: lwm2m: Deprecate LWM2M_ENGINE_MESSAGE_HEADER_SIZE Kconfig value LWM2M_ENGINE_MESSAGE_HEADER_SIZE added an extra headroom for CoAP packet sizes so that if CoAP Block-Wise transfer block-size is configured to be same as LWM2M_COAP_MAX_MSG_SIZE, the full payload block would usually fit to the datagram. This causes too much confusion to be usable. CoAP headers and options vary on sizes, and there is no runtime limitations that we should check the header size against. Only real limitation is the CoAP packet size, which must fit into the UDP datagram with typical DTLS headers. Only limitation for CoAP block-size then is that it must fit into the CoAP message with all the headers and options. But as the option sizes, like path, vary, it must be checked runtime. Signed-off-by: Seppo Takalo --- doc/releases/release-notes-4.2.rst | 6 +++++ subsys/net/lib/lwm2m/Kconfig | 22 +++++++++---------- subsys/net/lib/lwm2m/lwm2m_message_handling.c | 13 ++++++----- subsys/net/lib/lwm2m/lwm2m_object.h | 17 ++++++-------- tests/net/lib/lwm2m/block_transfer/prj.conf | 1 + .../net/lib/lwm2m/lwm2m_engine/CMakeLists.txt | 1 - .../lib/lwm2m/lwm2m_rd_client/CMakeLists.txt | 1 - 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/doc/releases/release-notes-4.2.rst b/doc/releases/release-notes-4.2.rst index 7b09b489bbc..989d3c6cb3f 100644 --- a/doc/releases/release-notes-4.2.rst +++ b/doc/releases/release-notes-4.2.rst @@ -67,6 +67,12 @@ Deprecated APIs and options renamed and deprecated. Use :kconfig:option:`CONFIG_SCHED_SIMPLE` and :kconfig:option:`CONFIG_WAITQ_SIMPLE` instead. +* The :kconfig:option:`CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE` Kconfig option has been removed. + The required header size should be included in the message size, configured using + :kconfig:option:`CONFIG_LWM2M_COAP_MAX_MSG_SIZE`. Special care should be taken to ensure that + used CoAP block size :kconfig:option:`CONFIG_LWM2M_COAP_BLOCK_SIZE` can fit given message size + with headers. Previous headroom was 48 bytes. + * TLS credential type ``TLS_CREDENTIAL_SERVER_CERTIFICATE`` was renamed and deprecated, use :c:enumerator:`TLS_CREDENTIAL_PUBLIC_CERTIFICATE` instead. diff --git a/subsys/net/lib/lwm2m/Kconfig b/subsys/net/lib/lwm2m/Kconfig index 3878e906691..206b6d4c380 100644 --- a/subsys/net/lib/lwm2m/Kconfig +++ b/subsys/net/lib/lwm2m/Kconfig @@ -273,25 +273,25 @@ config LWM2M_ENGINE_MAX_MESSAGES config LWM2M_COAP_BLOCK_SIZE int "LWM2M CoAP block-wise transfer size" - default 256 + default 512 range 64 1024 help CoAP block size used by LwM2M when performing block-wise transfers. Possible values: 64, 128, 256, 512 and 1024. - -config LWM2M_ENGINE_MESSAGE_HEADER_SIZE - int "Room for CoAP header data" - default 48 - range 24 128 - help - Extra room allocated to handle CoAP header data + When adjusting the value, ensure that LWM2M_COAP_MAX_MSG_SIZE is large enough + to fit all CoAP headers and full block size. config LWM2M_COAP_MAX_MSG_SIZE int "LWM2M CoAP maximum message size" - default LWM2M_COAP_BLOCK_SIZE + default 1232 if !LWM2M_DTLS_SUPPORT + default 1195 if LWM2M_DTLS_SUPPORT && !LWM2M_DTLS_CID + default 1187 if LWM2M_DTLS_SUPPORT && LWM2M_DTLS_CID help - CoAP message size used by LWM2M. Minimum is the block size used - in blockwise transfers. + CoAP message size used by LWM2M. Default is the maximum + packet size for IPv6 network (MTU(1280)-IPv6(40)-UDP(8)-DTLS(29..53)). + If the network has a smaller MTU, this value should be adjusted accordingly. + If CoAP block-wise transfer is enabled, this value should be larger than + the block size and estimated CoAP header sizes. config LWM2M_ENGINE_VALIDATION_BUFFER_SIZE int "Size of the validation buffer for the incoming data" diff --git a/subsys/net/lib/lwm2m/lwm2m_message_handling.c b/subsys/net/lib/lwm2m/lwm2m_message_handling.c index 17499e86edc..1725661fe17 100644 --- a/subsys/net/lib/lwm2m/lwm2m_message_handling.c +++ b/subsys/net/lib/lwm2m/lwm2m_message_handling.c @@ -383,6 +383,7 @@ STATIC int build_msg_block_for_send(struct lwm2m_message *msg, uint16_t block_nu ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), complete_payload + (block_num * block_size_bytes), payload_size); if (ret < 0) { + LOG_ERR("CoAP message size overflow"); return ret; } @@ -392,19 +393,16 @@ STATIC int build_msg_block_for_send(struct lwm2m_message *msg, uint16_t block_nu STATIC int prepare_msg_for_send(struct lwm2m_message *msg) { int ret; - uint16_t len; - const uint8_t *payload; - /* save the big buffer for later use (splitting blocks) */ msg->body_encode_buffer = msg->cpkt; /* set the default (small) buffer for sending blocks */ msg->cpkt.data = msg->msg_data; msg->cpkt.offset = 0; - msg->cpkt.max_len = MAX_PACKET_SIZE; + msg->cpkt.max_len = sizeof(msg->msg_data); - payload = coap_packet_get_payload(&msg->body_encode_buffer, &len); - if (len <= CONFIG_LWM2M_COAP_MAX_MSG_SIZE) { + /* Can we fit a whole message into one frame */ + if (msg->body_encode_buffer.offset <= msg->cpkt.max_len) { /* copy the packet */ ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), msg->body_encode_buffer.data, @@ -422,6 +420,9 @@ STATIC int prepare_msg_for_send(struct lwm2m_message *msg) NET_ASSERT(msg->out.block_ctx == NULL, "Expecting to have no context to release"); } else { + uint16_t len; + const uint8_t *payload = coap_packet_get_payload(&msg->body_encode_buffer, &len); + /* Before splitting the content, append Etag option to protect the integrity of * the payload. */ diff --git a/subsys/net/lib/lwm2m/lwm2m_object.h b/subsys/net/lib/lwm2m/lwm2m_object.h index e8a9d71825e..79a63f43ce8 100644 --- a/subsys/net/lib/lwm2m/lwm2m_object.h +++ b/subsys/net/lib/lwm2m/lwm2m_object.h @@ -131,15 +131,12 @@ BUILD_ASSERT(CONFIG_LWM2M_COAP_BLOCK_SIZE <= CONFIG_LWM2M_COAP_MAX_MSG_SIZE, "CoAP block size can't exceed maximum message size"); - -#define MAX_PACKET_SIZE (CONFIG_LWM2M_COAP_MAX_MSG_SIZE + \ - CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE) - -#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER) -BUILD_ASSERT(CONFIG_LWM2M_COAP_ENCODE_BUFFER_SIZE > - (CONFIG_LWM2M_COAP_BLOCK_SIZE + CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE), - "The buffer for serializing message needs to be bigger than a message with one block"); -#endif +BUILD_ASSERT(CONFIG_LWM2M_COAP_BLOCK_SIZE == 64 || + CONFIG_LWM2M_COAP_BLOCK_SIZE == 128 || + CONFIG_LWM2M_COAP_BLOCK_SIZE == 256 || + CONFIG_LWM2M_COAP_BLOCK_SIZE == 512 || + CONFIG_LWM2M_COAP_BLOCK_SIZE == 1024, + "CoAP block must be 64, 128, 256, 512 or 1024"); /* buffer util macros */ #define CPKT_BUF_WRITE(cpkt) (cpkt)->data, &(cpkt)->offset, (cpkt)->max_len @@ -494,7 +491,7 @@ struct lwm2m_message { struct coap_packet cpkt; /** Buffer data related outgoing message */ - uint8_t msg_data[MAX_PACKET_SIZE]; + uint8_t msg_data[CONFIG_LWM2M_COAP_MAX_MSG_SIZE]; #if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER) /** Buffer data containing complete message */ diff --git a/tests/net/lib/lwm2m/block_transfer/prj.conf b/tests/net/lib/lwm2m/block_transfer/prj.conf index 2b036cf0c8e..8ba1ef71a8b 100644 --- a/tests/net/lib/lwm2m/block_transfer/prj.conf +++ b/tests/net/lib/lwm2m/block_transfer/prj.conf @@ -14,3 +14,4 @@ CONFIG_LWM2M=y CONFIG_LWM2M_COAP_BLOCK_TRANSFER=y CONFIG_LWM2M_COAP_BLOCK_SIZE=64 CONFIG_LWM2M_COAP_ENCODE_BUFFER_SIZE=256 +CONFIG_LWM2M_COAP_MAX_MSG_SIZE=96 diff --git a/tests/net/lib/lwm2m/lwm2m_engine/CMakeLists.txt b/tests/net/lib/lwm2m/lwm2m_engine/CMakeLists.txt index 2538d32a4cd..42887cdd49c 100644 --- a/tests/net/lib/lwm2m/lwm2m_engine/CMakeLists.txt +++ b/tests/net/lib/lwm2m/lwm2m_engine/CMakeLists.txt @@ -21,7 +21,6 @@ target_include_directories(app PRIVATE ${ZEPHYR_BASE}/../modules/crypto/mbedtls/ add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_PENDING=2) add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_REPLIES=2) add_compile_definitions(CONFIG_LWM2M_ENGINE_VALIDATION_BUFFER_SIZE=512) -add_compile_definitions(CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE=512) add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_OBSERVER=10) add_compile_definitions(CONFIG_LWM2M_ENGINE_STACK_SIZE=2048) add_compile_definitions(CONFIG_LWM2M_NUM_BLOCK1_CONTEXT=3) diff --git a/tests/net/lib/lwm2m/lwm2m_rd_client/CMakeLists.txt b/tests/net/lib/lwm2m/lwm2m_rd_client/CMakeLists.txt index 2a8e8959c25..a3f70cd7198 100644 --- a/tests/net/lib/lwm2m/lwm2m_rd_client/CMakeLists.txt +++ b/tests/net/lib/lwm2m/lwm2m_rd_client/CMakeLists.txt @@ -18,7 +18,6 @@ target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/lib/lwm2m/) add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_PENDING=2) add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_REPLIES=2) add_compile_definitions(CONFIG_LWM2M_ENGINE_VALIDATION_BUFFER_SIZE=512) -add_compile_definitions(CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE=512) add_compile_definitions(CONFIG_LWM2M_RD_CLIENT_ENDPOINT_NAME_MAX_LENGTH=32) add_compile_definitions(CONFIG_LWM2M_RD_CLIENT_MAX_RETRIES=2) add_compile_definitions(CONFIG_LWM2M_COAP_BLOCK_SIZE=256)