From ea25f0954b9d01bb6cd24bd9b2479750e848b7e0 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 24 Jun 2024 05:43:17 -0700 Subject: [PATCH] [mesh-forwarder] move `CheckReachability()` to `MeshForwarder` (#10421) This commit moves the `CheckReachability()` & `ResolveRoutingLoops()` methods from the `MleRouter` class to the `MeshForwarder` class now as `private` methods. This consolidates all `CheckReachability()` overloads within the `MeshForwarder` class. --- src/core/thread/mesh_forwarder.hpp | 2 + src/core/thread/mesh_forwarder_ftd.cpp | 67 ++++++++++++++++++++++++-- src/core/thread/mle_router.cpp | 56 --------------------- src/core/thread/mle_router.hpp | 21 -------- 4 files changed, 65 insertions(+), 81 deletions(-) diff --git a/src/core/thread/mesh_forwarder.hpp b/src/core/thread/mesh_forwarder.hpp index 459481b83..eba8e74ba 100644 --- a/src/core/thread/mesh_forwarder.hpp +++ b/src/core/thread/mesh_forwarder.hpp @@ -492,6 +492,7 @@ private: void SendIcmpErrorIfDstUnreach(const Message &aMessage, const Mac::Addresses &aMacAddrs); Error CheckReachability(const FrameData &aFrameData, const Mac::Addresses &aMeshAddrs); + Error CheckReachability(uint16_t aMeshDest, const Ip6::Header &aIp6Header); void UpdateRoutes(const FrameData &aFrameData, const Mac::Addresses &aMeshAddrs); Error FrameToMessage(const FrameData &aFrameData, uint16_t aDatagramSize, @@ -501,6 +502,7 @@ private: void GetMacSourceAddress(const Ip6::Address &aIp6Addr, Mac::Address &aMacAddr); Message *PrepareNextDirectTransmission(void); void HandleMesh(FrameData &aFrameData, const Mac::Address &aMacSource, const ThreadLinkInfo &aLinkInfo); + void ResolveRoutingLoops(uint16_t aSourceRloc16, uint16_t aDestRloc16); void HandleFragment(FrameData &aFrameData, const Mac::Addresses &aMacAddrs, const ThreadLinkInfo &aLinkInfo); void HandleLowpanHC(const FrameData &aFrameData, const Mac::Addresses &aMacAddrs, const ThreadLinkInfo &aLinkInfo); diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index 810d2c62f..bd36abf9f 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -575,7 +575,7 @@ Error MeshForwarder::UpdateIp6RouteFtd(const Ip6::Header &aIp6Header, Message &a mMeshSource = Get().GetShortAddress(); - SuccessOrExit(error = mle.CheckReachability(mMeshDest, aIp6Header)); + SuccessOrExit(error = CheckReachability(mMeshDest, aIp6Header)); aMessage.SetMeshDest(mMeshDest); mMacAddrs.mDestination.SetShort(Get().GetNextHop(mMeshDest)); @@ -609,7 +609,7 @@ void MeshForwarder::SendIcmpErrorIfDstUnreach(const Message &aMessage, const Mac VerifyOrExit(!ip6Headers.GetDestinationAddress().IsMulticast() && Get().IsOnMesh(ip6Headers.GetDestinationAddress())); - error = Get().CheckReachability(aMacAddrs.mDestination.GetShort(), ip6Headers.GetIp6Header()); + error = CheckReachability(aMacAddrs.mDestination.GetShort(), ip6Headers.GetIp6Header()); if (error == kErrorNoRoute) { @@ -639,7 +639,7 @@ Error MeshForwarder::CheckReachability(const FrameData &aFrameData, const Mac::A ExitNow(); } - error = Get().CheckReachability(aMeshAddrs.mDestination.GetShort(), ip6Headers.GetIp6Header()); + error = CheckReachability(aMeshAddrs.mDestination.GetShort(), ip6Headers.GetIp6Header()); if (error == kErrorNoRoute) { @@ -650,6 +650,44 @@ exit: return error; } +Error MeshForwarder::CheckReachability(uint16_t aMeshDest, const Ip6::Header &aIp6Header) +{ + bool isReachable = false; + uint16_t deviceRloc16 = Get().GetRloc16(); + + if (Get().IsChild()) + { + if (aMeshDest == deviceRloc16) + { + isReachable = Get().HasUnicastAddress(aIp6Header.GetDestination()); + } + else + { + isReachable = true; + } + + ExitNow(); + } + + if (aMeshDest == deviceRloc16) + { + isReachable = Get().HasUnicastAddress(aIp6Header.GetDestination()) || + (Get().FindNeighbor(aIp6Header.GetDestination()) != nullptr); + ExitNow(); + } + + if (Mle::RouterIdMatch(aMeshDest, deviceRloc16)) + { + isReachable = (Get().FindChild(aMeshDest, Child::kInStateValidOrRestoring) != nullptr); + ExitNow(); + } + + isReachable = (Get().GetNextHop(aMeshDest) != Mac::kShortAddrInvalid); + +exit: + return isReachable ? kErrorNone : kErrorNoRoute; +} + void MeshForwarder::SendDestinationUnreachable(uint16_t aMeshSource, const Ip6::Headers &aIp6Headers) { Ip6::MessageInfo messageInfo; @@ -697,7 +735,7 @@ void MeshForwarder::HandleMesh(FrameData &aFrameData, const Mac::Address &aMacSo OwnedPtr messagePtr; Message::Priority priority = Message::kPriorityNormal; - Get().ResolveRoutingLoops(aMacSource.GetShort(), meshAddrs.mDestination.GetShort()); + ResolveRoutingLoops(aMacSource.GetShort(), meshAddrs.mDestination.GetShort()); SuccessOrExit(error = CheckReachability(aFrameData, meshAddrs)); @@ -735,6 +773,27 @@ exit: } } +void MeshForwarder::ResolveRoutingLoops(uint16_t aSourceRloc16, uint16_t aDestRloc16) +{ + // Resolves 2-hop routing loops. + + Router *router; + + if (aSourceRloc16 != Get().GetNextHop(aDestRloc16)) + { + ExitNow(); + } + + router = Get().FindRouterByRloc16(aDestRloc16); + VerifyOrExit(router != nullptr); + + router->SetNextHopToInvalid(); + Get().ResetAdvertiseInterval(); + +exit: + return; +} + void MeshForwarder::UpdateRoutes(const FrameData &aFrameData, const Mac::Addresses &aMeshAddrs) { Ip6::Headers ip6Headers; diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 2edd75f44..ca3476fe5 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -3197,62 +3197,6 @@ void MleRouter::SetRouterId(uint8_t aRouterId) mPreviousRouterId = mRouterId; } -void MleRouter::ResolveRoutingLoops(uint16_t aSourceMac, uint16_t aDestRloc16) -{ - Router *router; - - if (aSourceMac != mRouterTable.GetNextHop(aDestRloc16)) - { - ExitNow(); - } - - router = mRouterTable.FindRouterByRloc16(aDestRloc16); - VerifyOrExit(router != nullptr); - - router->SetNextHopToInvalid(); - ResetAdvertiseInterval(); - -exit: - return; -} - -Error MleRouter::CheckReachability(uint16_t aMeshDest, const Ip6::Header &aIp6Header) -{ - bool isReachable = false; - - if (IsChild()) - { - if (aMeshDest == GetRloc16()) - { - isReachable = Get().HasUnicastAddress(aIp6Header.GetDestination()); - } - else - { - isReachable = true; - } - - ExitNow(); - } - - if (aMeshDest == GetRloc16()) - { - isReachable = Get().HasUnicastAddress(aIp6Header.GetDestination()) || - (mNeighborTable.FindNeighbor(aIp6Header.GetDestination()) != nullptr); - ExitNow(); - } - - if (RouterIdFromRloc16(aMeshDest) == mRouterId) - { - isReachable = (mChildTable.FindChild(aMeshDest, Child::kInStateValidOrRestoring) != nullptr); - ExitNow(); - } - - isReachable = (mRouterTable.GetNextHop(aMeshDest) != Mac::kShortAddrInvalid); - -exit: - return isReachable ? kErrorNone : kErrorNoRoute; -} - Error MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus) { Error error = kErrorNone; diff --git a/src/core/thread/mle_router.hpp b/src/core/thread/mle_router.hpp index 8dbcb0807..3cf3f406b 100644 --- a/src/core/thread/mle_router.hpp +++ b/src/core/thread/mle_router.hpp @@ -379,27 +379,6 @@ public: bool aSingletonB, const LeaderData &aLeaderDataB); - /** - * Checks if the destination is reachable. - * - * @param[in] aMeshDest The RLOC16 of the destination. - * @param[in] aIp6Header A reference to the IPv6 header of the message. - * - * @retval kErrorNone The destination is reachable. - * @retval kErrorNoRoute The destination is not reachable and the message should be dropped. - * - */ - Error CheckReachability(uint16_t aMeshDest, const Ip6::Header &aIp6Header); - - /** - * Resolves 2-hop routing loops. - * - * @param[in] aSourceMac The RLOC16 of the previous hop. - * @param[in] aDestRloc16 The RLOC16 of the final destination. - * - */ - void ResolveRoutingLoops(uint16_t aSourceMac, uint16_t aDestRloc16); - /** * Fills an ConnectivityTlv. *