Skip to content

Commit

Permalink
[backbone-router] use TimeTicker directly to delay registration (#9483
Browse files Browse the repository at this point in the history
)

This commit update `BackboneRouter::Local` to be a `TimeTicker`
receiver directly instead of using the `MleRouter for this. The
`TimeTicker` callback is used to delay registration by counting down
`mRegistrationTimeout`.
  • Loading branch information
abtink authored Oct 4, 2023
1 parent 3043f2e commit 4ab8334
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
33 changes: 28 additions & 5 deletions src/core/backbone_router/bbr_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Local::Local(Instance &aInstance)
, mState(kStateDisabled)
, mMlrTimeout(kDefaultMlrTimeout)
, mReregistrationDelay(kDefaultRegistrationDelay)
, mRegistrationTimeout(0)
, mSequenceNumber(Random::NonCrypto::GetUint8() % 127)
, mRegistrationJitter(kDefaultRegistrationJitter)
, mIsServiceAdded(false)
Expand Down Expand Up @@ -252,16 +253,15 @@ void Local::HandleBackboneRouterPrimaryUpdate(Leader::State aState, const Config
// Wait some jitter before trying to Register.
if (aConfig.mServer16 == Mac::kShortAddrInvalid)
{
uint8_t delay = 1;
mRegistrationTimeout = 1;

if (!Get<Mle::MleRouter>().IsLeader())
{
delay += Random::NonCrypto::GetUint8InRange(0, mRegistrationJitter < 255 ? mRegistrationJitter + 1
: mRegistrationJitter);
mRegistrationTimeout +=
Random::NonCrypto::GetUint16InRange(0, static_cast<uint16_t>(mRegistrationJitter) + 1);
}

// Here uses the timer resource in Mle.
Get<Mle::MleRouter>().SetBackboneRouterRegistrationDelay(delay);
Get<TimeTicker>().RegisterReceiver(TimeTicker::kBbrLocal);
}
else if (aConfig.mServer16 != Get<Mle::MleRouter>().GetRloc16())
{
Expand All @@ -287,6 +287,29 @@ void Local::HandleBackboneRouterPrimaryUpdate(Leader::State aState, const Config
return;
}

void Local::HandleTimeTick(void)
{
// Delay registration when `GetRouterSelectionJitterTimeout()` is non-zero,
// which indicates device may soon switch its role (e.g., REED to router).
VerifyOrExit(Get<Mle::MleRouter>().GetRouterSelectionJitterTimeout() == 0);

if (mRegistrationTimeout > 0)
{
mRegistrationTimeout--;

if (mRegistrationTimeout == 0)
{
IgnoreError(AddService(kDecideBasedOnState));
}
}

exit:
if (mRegistrationTimeout == 0)
{
Get<TimeTicker>().UnregisterReceiver(TimeTicker::kBbrLocal);
}
}

Error Local::GetDomainPrefix(NetworkData::OnMeshPrefixConfig &aConfig)
{
Error error = kErrorNone;
Expand Down
5 changes: 5 additions & 0 deletions src/core/backbone_router/bbr_local.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "common/locator.hpp"
#include "common/log.hpp"
#include "common/non_copyable.hpp"
#include "common/time_ticker.hpp"
#include "net/netif.hpp"
#include "thread/network_data.hpp"

Expand All @@ -72,6 +73,8 @@ namespace BackboneRouter {
*/
class Local : public InstanceLocator, private NonCopyable
{
friend class ot::TimeTicker;

public:
/**
* Represents Backbone Router state.
Expand Down Expand Up @@ -278,6 +281,7 @@ class Local : public InstanceLocator, private NonCopyable
private:
void SetState(State aState);
void RemoveService(void);
void HandleTimeTick(void);
void AddDomainPrefixToNetworkData(void);
void RemoveDomainPrefixFromNetworkData(void);
void SequenceNumberIncrease(void);
Expand All @@ -292,6 +296,7 @@ class Local : public InstanceLocator, private NonCopyable
State mState;
uint32_t mMlrTimeout;
uint16_t mReregistrationDelay;
uint16_t mRegistrationTimeout;
uint8_t mSequenceNumber;
uint8_t mRegistrationJitter;

Expand Down
7 changes: 7 additions & 0 deletions src/core/common/time_ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ void TimeTicker::HandleTimer(void)
{
Get<Ip6::Mpl>().HandleTimeTick();
}

#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
if (mReceivers & Mask(kBbrLocal))
{
Get<BackboneRouter::Local>().HandleTimeTick();
}
#endif
}

} // namespace ot
1 change: 1 addition & 0 deletions src/core/common/time_ticker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class TimeTicker : public InstanceLocator, private NonCopyable
kMlrManager, ///< `MlrManager`
kNetworkDataNotifier, ///< `NetworkData::Notifier`
kIp6Mpl, ///< `Ip6::Mpl`
kBbrLocal, ///< `BackboneRouter::Local`

kNumReceivers, ///< Number of receivers.
};
Expand Down
16 changes: 0 additions & 16 deletions src/core/thread/mle_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ MleRouter::MleRouter(Instance &aInstance)
, mRouterSelectionJitterTimeout(0)
, mChildRouterLinks(kChildRouterLinks)
, mParentPriority(kParentPriorityUnspecified)
#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
, mBackboneRouterRegistrationDelay(0)
#endif
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
, mMaxChildIpAddresses(0)
#endif
Expand Down Expand Up @@ -1541,19 +1538,6 @@ void MleRouter::HandleTimeTick(void)
routerStateUpdate = true;
}
}
#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
// Delay register only when `mRouterSelectionJitterTimeout` is 0,
// that is, when the device has decided to stay as REED or Router.
else if (mBackboneRouterRegistrationDelay > 0)
{
mBackboneRouterRegistrationDelay--;

if (mBackboneRouterRegistrationDelay == 0)
{
IgnoreError(Get<BackboneRouter::Local>().AddService(BackboneRouter::Local::kDecideBasedOnState));
}
}
#endif

switch (mRole)
{
Expand Down
13 changes: 0 additions & 13 deletions src/core/thread/mle_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,16 +522,6 @@ class MleRouter : public Mle
Error SendTimeSync(void);
#endif

#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
/**
* Sets the delay before registering Backbone Router service.
*
* @param[in] aDelay The delay before registering Backbone Router service.
*
*/
void SetBackboneRouterRegistrationDelay(uint8_t aDelay) { mBackboneRouterRegistrationDelay = aDelay; }
#endif

/**
* Gets the maximum number of IP addresses that each MTD child may register with this device as parent.
*
Expand Down Expand Up @@ -743,9 +733,6 @@ class MleRouter : public Mle
uint8_t mChildRouterLinks;

int8_t mParentPriority; ///< The assigned parent priority value, -2 means not assigned.
#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
uint8_t mBackboneRouterRegistrationDelay; ///< Delay before registering Backbone Router service.
#endif
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
uint8_t mMaxChildIpAddresses;
#endif
Expand Down

0 comments on commit 4ab8334

Please sign in to comment.