From 0d91ffc5cad45bfa844254ffa897ee3ec96375f4 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 10 Jul 2025 12:11:24 -0700 Subject: [PATCH] [ip6] move `Message::Ownership` enum to `Ip6` class (#11696) This commit moves the `Message::Ownership` enum definition to the `Ip6` class. This enum is exclusively used by the `Ip6` class to determine whether to clone a message or take direct custody. This model is not intended for use by other components. `OwnerPtr<>` is the recommended approach for conveying ownership transfers. --- src/core/common/message.hpp | 19 ------------------- src/core/net/ip6.cpp | 18 +++++++++--------- src/core/net/ip6.hpp | 12 +++++++++--- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index f886fdf0b..0a9e46b54 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -326,25 +326,6 @@ public: static constexpr uint8_t kNumPriorities = 4; ///< Number of priority levels. - /** - * Represents the message ownership model when a `Message` instance is passed to a method/function. - */ - enum Ownership : uint8_t - { - /** - * This value indicates that the method/function receiving a `Message` instance should take custody of the - * message (e.g., the method should `Free()` the message if no longer needed). - */ - kTakeCustody, - - /** - * This value indicates that the method/function receiving a `Message` instance does not own the message (e.g., - * it should not `Free()` or `Enqueue()` it in a queue). The receiving method/function should create a - * copy/clone of the message to keep (if/when needed). - */ - kCopyToUse, - }; - /** * Represents an IPv6 message origin. */ diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 54aa594a6..c8b75c8a7 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -823,7 +823,7 @@ Error Ip6::HandleExtensionHeaders(OwnedPtr &aMessagePtr, break; case kProtoFragment: - IgnoreError(PassToHost(aMessagePtr, aHeader, aNextHeader, aReceive, Message::kCopyToUse)); + IgnoreError(PassToHost(aMessagePtr, aHeader, aNextHeader, aReceive, kCopyMessageToUse)); SuccessOrExit(error = HandleFragment(*aMessagePtr)); break; @@ -847,15 +847,15 @@ exit: Error Ip6::TakeOrCopyMessagePtr(OwnedPtr &aTargetPtr, OwnedPtr &aMessagePtr, - Message::Ownership aMessageOwnership) + MessageOwnership aMessageOwnership) { switch (aMessageOwnership) { - case Message::kTakeCustody: + case kTakeMessageCustody: aTargetPtr = aMessagePtr.PassOwnership(); break; - case Message::kCopyToUse: + case kCopyMessageToUse: aTargetPtr.Reset(aMessagePtr->Clone()); break; } @@ -866,7 +866,7 @@ Error Ip6::TakeOrCopyMessagePtr(OwnedPtr &aTargetPtr, Error Ip6::Receive(Header &aIp6Header, OwnedPtr &aMessagePtr, uint8_t aIpProto, - Message::Ownership aMessageOwnership) + MessageOwnership aMessageOwnership) { Error error = kErrorNone; OwnedPtr messagePtr; @@ -921,7 +921,7 @@ Error Ip6::PassToHost(OwnedPtr &aMessagePtr, const Header &aHeader, uint8_t aIpProto, bool aReceive, - Message::Ownership aMessageOwnership) + MessageOwnership aMessageOwnership) { // This method passes the message to host by invoking the // registered IPv6 receive callback. When NAT64 is enabled, it @@ -1208,7 +1208,7 @@ Error Ip6::HandleDatagram(OwnedPtr aMessagePtr, bool aIsReassembled) bool multicastLoop = aMessagePtr->GetMulticastLoop(); SuccessOrExit(error = TakeOrCopyMessagePtr(messagePtr, aMessagePtr, - forwardThread ? Message::kCopyToUse : Message::kTakeCustody)); + forwardThread ? kCopyMessageToUse : kTakeMessageCustody)); messagePtr->SetMulticastLoop(multicastLoop); messagePtr->RemoveHeader(messagePtr->GetOffset()); @@ -1223,12 +1223,12 @@ Error Ip6::HandleDatagram(OwnedPtr aMessagePtr, bool aIsReassembled) if ((forwardHost || receive) && !aIsReassembled) { error = PassToHost(aMessagePtr, header, nextHeader, receive, - (receive || forwardThread) ? Message::kCopyToUse : Message::kTakeCustody); + (receive || forwardThread) ? kCopyMessageToUse : kTakeMessageCustody); } if (receive) { - error = Receive(header, aMessagePtr, nextHeader, forwardThread ? Message::kCopyToUse : Message::kTakeCustody); + error = Receive(header, aMessagePtr, nextHeader, forwardThread ? kCopyMessageToUse : kTakeMessageCustody); } if (forwardThread) diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 30a9569f8..9980fd8b4 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -349,10 +349,16 @@ private: static constexpr uint16_t kMinimalMtu = 1280; + enum MessageOwnership : uint8_t + { + kTakeMessageCustody, + kCopyMessageToUse, + }; + static uint8_t PriorityToDscp(Message::Priority aPriority); static Error TakeOrCopyMessagePtr(OwnedPtr &aTargetPtr, OwnedPtr &aMessagePtr, - Message::Ownership aMessageOwnership); + MessageOwnership aMessageOwnership); void EnqueueDatagram(Message &aMessage); void HandleSendQueue(void); @@ -365,7 +371,7 @@ private: const Header &aHeader, uint8_t aIpProto, bool aReceive, - Message::Ownership aMessageOwnership); + MessageOwnership aMessageOwnership); Error HandleExtensionHeaders(OwnedPtr &aMessagePtr, const Header &aHeader, uint8_t &aNextHeader, @@ -387,7 +393,7 @@ private: Error Receive(Header &aIp6Header, OwnedPtr &aMessagePtr, uint8_t aIpProto, - Message::Ownership aMessageOwnership); + MessageOwnership aMessageOwnership); #if OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE void UpdateBorderRoutingCounters(const Header &aHeader, uint16_t aMessageLength, bool aIsInbound); #endif