From 4cba902db117910de22680a396b5d1c05154c12b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 10 May 2023 17:59:32 -0700 Subject: [PATCH] [ip6] simplify and update `NewMessageFromData()` (#9037) This commit simplifies and updates the `NewMessageFromData()` method to always determine the priority from the IPv6 header, even if a `Message::Settings` is provided. This change aligns the behavior of `NewMessageFromData()` with the documented and intended behavior of `otIp6NewMessageFromBuffer()`. --- src/core/api/ip6_api.cpp | 5 ++--- src/core/net/ip6.cpp | 45 +++++++++++++--------------------------- src/core/net/ip6.hpp | 22 +++++--------------- 3 files changed, 21 insertions(+), 51 deletions(-) diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 98da9489e..4ff7d229c 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -146,9 +146,8 @@ otMessage *otIp6NewMessageFromBuffer(otInstance *aInstance, uint16_t aDataLength, const otMessageSettings *aSettings) { - return (aSettings != nullptr) - ? AsCoreType(aInstance).Get().NewMessage(aData, aDataLength, AsCoreType(aSettings)) - : AsCoreType(aInstance).Get().NewMessage(aData, aDataLength); + return AsCoreType(aInstance).Get().NewMessageFromData(aData, aDataLength, + Message::Settings::From(aSettings)); } otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort) diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index b47bf9ef7..d3f1bb99a 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -87,9 +87,21 @@ Message *Ip6::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(MplOption) + aReserved, aSettings); } -Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings) +Message *Ip6::NewMessageFromData(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings) { - Message *message = Get().Allocate(Message::kTypeIp6, /* aReserveHeader */ 0, aSettings); + Message *message = nullptr; + Message::Settings settings = aSettings; + const Header *header; + + VerifyOrExit((aData != nullptr) && (aDataLength >= sizeof(Header))); + + // Determine priority from IPv6 header + header = reinterpret_cast(aData); + VerifyOrExit(header->IsValid()); + VerifyOrExit(sizeof(Header) + header->GetPayloadLength() == aDataLength); + settings.mPriority = DscpToPriority(header->GetDscp()); + + message = Get().Allocate(Message::kTypeIp6, /* aReserveHeader */ 0, settings); VerifyOrExit(message != nullptr); @@ -103,18 +115,6 @@ exit: return message; } -Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength) -{ - Message *message = nullptr; - Message::Priority priority; - - SuccessOrExit(GetDatagramPriority(aData, aDataLength, priority)); - message = NewMessage(aData, aDataLength, Message::Settings(Message::kWithLinkSecurity, priority)); - -exit: - return message; -} - Message::Priority Ip6::DscpToPriority(uint8_t aDscp) { Message::Priority priority; @@ -170,23 +170,6 @@ uint8_t Ip6::PriorityToDscp(Message::Priority aPriority) return dscp; } -Error Ip6::GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, Message::Priority &aPriority) -{ - Error error = kErrorNone; - const Header *header; - - VerifyOrExit((aData != nullptr) && (aDataLen >= sizeof(Header)), error = kErrorInvalidArgs); - - header = reinterpret_cast(aData); - VerifyOrExit(header->IsValid(), error = kErrorParse); - VerifyOrExit(sizeof(Header) + header->GetPayloadLength() == aDataLen, error = kErrorParse); - - aPriority = DscpToPriority(header->GetDscp()); - -exit: - return error; -} - Error Ip6::AddMplOption(Message &aMessage, Header &aHeader) { Error error = kErrorNone; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 3c3092495..8bfd38082 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -149,29 +149,18 @@ public: /** * This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message. * + * The message priority is always determined from IPv6 message itself (@p aData) and the priority included in + * @p aSetting is ignored. + * * @param[in] aData A pointer to the IPv6 datagram buffer. * @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData. * @param[in] aSettings The message settings. * * @returns A pointer to the message or `nullptr` if malformed IPv6 header or insufficient message buffers are - * available. + * available. * */ - Message *NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings); - - /** - * This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message. - * - * @note The link layer security is enabled and the message priority is obtained from IPv6 message itself. - * - * @param[in] aData A pointer to the IPv6 datagram buffer. - * @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData. - * - * @returns A pointer to the message or `nullptr` if malformed IPv6 header or insufficient message buffers are - * available. - * - */ - Message *NewMessage(const uint8_t *aData, uint16_t aDataLength); + Message *NewMessageFromData(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings); /** * This method converts the IPv6 DSCP value to message priority level. @@ -375,7 +364,6 @@ private: void HandleSendQueue(void); static uint8_t PriorityToDscp(Message::Priority aPriority); - static Error GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, Message::Priority &aPriority); void EnqueueDatagram(Message &aMessage); Error PassToHost(Message &aMessage,