From 132df25505604a0e9793c26d61002ca428e8634f Mon Sep 17 00:00:00 2001 From: Rongli Sun Date: Mon, 30 Mar 2020 14:13:43 +0800 Subject: [PATCH] [bbr] (Un)Subscribe AllNetworkBBRs Multicast address (#4755) --- src/core/backbone_router/leader.cpp | 4 +- src/core/backbone_router/local.cpp | 2 +- src/core/backbone_router/local.hpp | 25 ++++-- src/core/thread/mle.cpp | 86 ++++++++++++++----- src/core/thread/mle.hpp | 16 +++- .../v1_2_test_backbone_router_service.py | 2 +- 6 files changed, 100 insertions(+), 35 deletions(-) diff --git a/src/core/backbone_router/leader.cpp b/src/core/backbone_router/leader.cpp index 3f9b52b59..71760eb53 100644 --- a/src/core/backbone_router/leader.cpp +++ b/src/core/backbone_router/leader.cpp @@ -156,13 +156,13 @@ void Leader::Update(void) } else { - // Short Address of PBBR changes + // Short Address of PBBR changes. state = kStateToTriggerRereg; } } else if (config.mServer16 == Mac::kShortAddrInvalid) { - // If no primary all the time + // If no Primary all the time. state = kStateNone; } else if (config.mSequenceNumber != mConfig.mSequenceNumber) diff --git a/src/core/backbone_router/local.cpp b/src/core/backbone_router/local.cpp index c657e4565..0b4373684 100644 --- a/src/core/backbone_router/local.cpp +++ b/src/core/backbone_router/local.cpp @@ -86,7 +86,7 @@ void Local::Reset(void) if (mState == OT_BACKBONE_ROUTER_STATE_PRIMARY) { - // Increase sequence number when changing from primary to secondary. + // Increase sequence number when changing from Primary to Secondary. mSequenceNumber++; Get().Signal(OT_CHANGED_THREAD_BACKBONE_ROUTER_LOCAL); SetState(OT_BACKBONE_ROUTER_STATE_SECONDARY); diff --git a/src/core/backbone_router/local.hpp b/src/core/backbone_router/local.hpp index 3c251fc95..3d56f7c9c 100644 --- a/src/core/backbone_router/local.hpp +++ b/src/core/backbone_router/local.hpp @@ -120,14 +120,23 @@ public: otError AddService(bool aForce = false); /** - * This method indicates whether or not the Backbone Router is primary. + * This method indicates whether or not the Backbone Router is Primary. * - * @retval True if the backbone router is primary. - * @retval False if the backbone router is not primary. + * @retval True if the Backbone Router is Primary. + * @retval False if the Backbone Router is not Primary. * */ bool IsPrimary(void) const { return mState == OT_BACKBONE_ROUTER_STATE_PRIMARY; } + /** + * This method indicates whether or not the Backbone Router is enabled. + * + * @retval True if the Backbone Router is enabled. + * @retval False if the Backbone Router is not enabled. + * + */ + bool IsEnabled(void) const { return mState != OT_BACKBONE_ROUTER_STATE_DISABLED; } + /** * This method sets the Backbone Router registration jitter value. * @@ -145,10 +154,10 @@ public: uint8_t GetRegistrationJitter(void) const { return mRegistrationJitter; } /** - * This method notifies primary backbone router status. + * This method notifies Primary Backbone Router status. * - * @param[in] aState The state or state change of primary backbone router. - * @param[in] aConfig The primary backbone router service. + * @param[in] aState The state or state change of Primary Backbone Router. + * @param[in] aConfig The Primary Backbone Router service. * */ void UpdateBackboneRouterPrimary(Leader::State aState, const BackboneRouterConfig &aConfig); @@ -164,8 +173,8 @@ private: uint8_t mRegistrationJitter; // Indicates whether or not already add Backbone Router Service to local server data. - // Used to check whether or not in restore stage after reset or whether to remove bbr - // service for secondary bbr if it is added by force. + // Used to check whether or not in restore stage after reset or whether to remove + // Backbone Router service for Secondary Backbone Router if it was added by force. bool mIsServiceAdded; }; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 27539e010..9b325c4e7 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -160,7 +160,9 @@ Mle::Mle(Instance &aInstance) } #endif + #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + // Primary Backbone Router Aloc mBackboneRouterPrimaryAloc.Clear(); @@ -170,6 +172,16 @@ Mle::Mle(Instance &aInstance) mBackboneRouterPrimaryAloc.mScopeOverride = Ip6::Address::kRealmLocalScope; mBackboneRouterPrimaryAloc.mScopeOverrideValid = true; mBackboneRouterPrimaryAloc.GetAddress().SetLocator(kAloc16BackboneRouterPrimary); + + // All Network Backbone Routers Multicast Address. + mAllNetworkBackboneRouters.Clear(); + + mAllNetworkBackboneRouters.GetAddress().mFields.m8[0] = 0xff; // Multicast + mAllNetworkBackboneRouters.GetAddress().mFields.m8[1] = 0x32; // Flags = 3, Scope = 2 + mAllNetworkBackboneRouters.GetAddress().mFields.m8[2] = 0; // Reserved + mAllNetworkBackboneRouters.GetAddress().mFields.m8[3] = 64; // Prefix Length = 64 + mAllNetworkBackboneRouters.GetAddress().mFields.m8[15] = 3; // Group ID = 3 + #endif // initialize Mesh Local Prefix @@ -945,10 +957,17 @@ void Mle::SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix) Get().UnsubscribeMulticast(mRealmLocalAllThreadNodes); #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + if (Get().IsPrimary()) { Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); } + + if (Get().IsEnabled()) + { + Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); + } + #endif } @@ -957,7 +976,7 @@ void Mle::SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix) mLeaderAloc.GetAddress().SetPrefix(aMeshLocalPrefix); #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(GetMeshLocalPrefix()); + mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(aMeshLocalPrefix); #endif // Just keep mesh local prefix if network interface is down @@ -971,27 +990,6 @@ exit: void Mle::ApplyMeshLocalPrefix(void) { -#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE - - for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mServiceAlocs); i++) - { - if (mServiceAlocs[i].GetAddress().GetLocator() != Mac::kShortAddrInvalid) - { - Get().RemoveUnicastAddress(mServiceAlocs[i]); - mServiceAlocs[i].GetAddress().SetPrefix(GetMeshLocalPrefix()); - Get().AddUnicastAddress(mServiceAlocs[i]); - } - } - -#endif - -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - if (Get().IsPrimary()) - { - Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); - } -#endif - mLinkLocalAllThreadNodes.GetAddress().mFields.m8[3] = 64; memcpy(mLinkLocalAllThreadNodes.GetAddress().mFields.m8 + 4, mMeshLocal64.GetAddress().mFields.m8, 8); @@ -1016,6 +1014,37 @@ void Mle::ApplyMeshLocalPrefix(void) Get().AddUnicastAddress(mLeaderAloc); } +#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE + + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mServiceAlocs); i++) + { + if (mServiceAlocs[i].GetAddress().GetLocator() != Mac::kShortAddrInvalid) + { + Get().RemoveUnicastAddress(mServiceAlocs[i]); + mServiceAlocs[i].GetAddress().SetPrefix(GetMeshLocalPrefix()); + Get().AddUnicastAddress(mServiceAlocs[i]); + } + } + +#endif + +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + + if (Get().IsPrimary()) + { + Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); + } + + memcpy(mAllNetworkBackboneRouters.GetAddress().mFields.m8 + 4, mMeshLocal64.GetAddress().mFields.m8, 8); + + if (Get().IsEnabled()) + { + // Subscribe All Network Backbone Routers Multicast Address for both Seconday and Primary state. + Get().SubscribeMulticast(mAllNetworkBackboneRouters); + } + +#endif + exit: // Changing the prefix also causes the mesh local address to be different. Get().Signal(OT_CHANGED_THREAD_ML_ADDR); @@ -1626,17 +1655,30 @@ void Mle::HandleStateChanged(otChangedFlags aFlags) } #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + if (aFlags & OT_CHANGED_THREAD_BACKBONE_ROUTER_STATE) { if (Get().IsPrimary()) { + // Add Primary Backbone Router Aloc for Primary Backbone Router. Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); } else { Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); } + + if (Get().IsEnabled()) + { + // Subscribe All Network Backbone Routers Multicast Address for both Seconday and Primary state. + Get().SubscribeMulticast(mAllNetworkBackboneRouters); + } + else + { + Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); + } } + #endif exit: diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index e5084031e..709597829 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -601,6 +601,19 @@ public: return mRealmLocalAllThreadNodes.GetAddress(); } +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + /** + * This method returns a reference to the All Network Backbone Routers Multicast Address. + * + * @returns A reference to the All Network Backbone Routers Multicast Address. + * + */ + const Ip6::Address &GetAllNetworkBackboneRoutersAddress(void) const + { + return mAllNetworkBackboneRouters.GetAddress(); + } +#endif + /** * This method gets the parent when operating in End Device mode. * @@ -1771,7 +1784,8 @@ private: #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - Ip6::NetifUnicastAddress mBackboneRouterPrimaryAloc; + Ip6::NetifUnicastAddress mBackboneRouterPrimaryAloc; + Ip6::NetifMulticastAddress mAllNetworkBackboneRouters; #endif otMleCounters mCounters; diff --git a/tests/scripts/thread-cert/v1_2_test_backbone_router_service.py b/tests/scripts/thread-cert/v1_2_test_backbone_router_service.py index f6d8da7df..f17cecc6b 100755 --- a/tests/scripts/thread-cert/v1_2_test_backbone_router_service.py +++ b/tests/scripts/thread-cert/v1_2_test_backbone_router_service.py @@ -206,7 +206,7 @@ class TestBackboneRouterService(thread_cert.TestCase): self.nodes[BBR_2].reset() self.nodes[LEADER_1_1].release_router_id( self.nodes[BBR_2].get_router_id()) - # Wait for the dissemination of Network Data without backbone router service + # Wait for the dissemination of Network Data without Backbone Router service self.simulator.go(10) # BBR_1 becomes Primary.