Skip to content

Commit

Permalink
[routing-manager] add SetState() in OnLinkPrefixManager (#9273)
Browse files Browse the repository at this point in the history
This commit adds `SetState()` method to the `OnLinkPrefixManager`
class. This method allows the state of the on-link prefix manager to
be changed in a single place, which simplifies the logging and makes
it easier to take additional actions on state change. For example,
the `SetState()` can be used to update the Advertising PIO (AP) flag
in the published route in Network Data.
  • Loading branch information
abtink authored Jul 12, 2023
1 parent fd62216 commit 34e9b92
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
75 changes: 47 additions & 28 deletions src/core/border_router/routing_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,18 @@ RoutingManager::OnLinkPrefixManager::OnLinkPrefixManager(Instance &aInstance)
mOldLocalPrefixes.Clear();
}

void RoutingManager::OnLinkPrefixManager::SetState(State aState)
{
VerifyOrExit(mState != aState);

LogInfo("Local on-link prefix state: %s -> %s (%s)", StateToString(mState), StateToString(aState),
mLocalPrefix.ToString().AsCString());
mState = aState;

exit:
return;
}

void RoutingManager::OnLinkPrefixManager::Init(void)
{
TimeMilli now = TimerMilli::GetNow();
Expand Down Expand Up @@ -2249,19 +2261,19 @@ void RoutingManager::OnLinkPrefixManager::GenerateLocalPrefix(void)
LogNote("Local on-link prefix: %s", mLocalPrefix.ToString().AsCString());

// Check if the new local prefix happens to be in `mOldLocalPrefixes` array.
// If so, we remove it from the array and set `mState` accordingly.
// If so, we remove it from the array and update the state accordingly.

entry = mOldLocalPrefixes.FindMatching(mLocalPrefix);

if (entry != nullptr)
{
mState = kDeprecating;
SetState(kDeprecating);
mExpireTime = entry->mExpireTime;
mOldLocalPrefixes.Remove(*entry);
}
else
{
mState = kIdle;
SetState(kIdle);
}

exit:
Expand All @@ -2274,15 +2286,15 @@ void RoutingManager::OnLinkPrefixManager::Stop(void)
{
mFavoredDiscoveredPrefix.Clear();

switch (mState)
switch (GetState())
{
case kIdle:
break;

case kPublishing:
case kAdvertising:
case kDeprecating:
mState = kDeprecating;
SetState(kDeprecating);
break;
}
}
Expand Down Expand Up @@ -2372,7 +2384,7 @@ void RoutingManager::OnLinkPrefixManager::PublishAndAdvertise(void)
// Start publishing and advertising the local on-link prefix if
// not already.

switch (mState)
switch (GetState())
{
case kIdle:
case kDeprecating:
Expand All @@ -2383,11 +2395,9 @@ void RoutingManager::OnLinkPrefixManager::PublishAndAdvertise(void)
ExitNow();
}

mState = kPublishing;
SetState(kPublishing);
ResetExpireTime(TimerMilli::GetNow());

LogInfo("Publishing route for local on-link prefix %s", mLocalPrefix.ToString().AsCString());

// We wait for the ULA `fc00::/7` route or a sub-prefix of it (e.g.,
// default route) to be added in Network Data before
// starting to advertise the local on-link prefix in RAs.
Expand All @@ -2397,7 +2407,7 @@ void RoutingManager::OnLinkPrefixManager::PublishAndAdvertise(void)

if (Get<RoutingManager>().NetworkDataContainsUlaRoute())
{
EnterAdvertisingState();
SetState(kAdvertising);
}

exit:
Expand All @@ -2413,12 +2423,11 @@ void RoutingManager::OnLinkPrefixManager::Deprecate(void)
// with zero preferred lifetime and the remaining valid lifetime
// until the timer expires.

switch (mState)
switch (GetState())
{
case kPublishing:
case kAdvertising:
mState = kDeprecating;
LogInfo("Deprecating local on-link prefix %s", mLocalPrefix.ToString().AsCString());
SetState(kDeprecating);
break;

case kIdle:
Expand All @@ -2434,7 +2443,7 @@ bool RoutingManager::OnLinkPrefixManager::ShouldPublishUlaRoute(void) const
// or `kDeprecating` states, or if there is at least one old local
// prefix being deprecated.

return (mState != kIdle) || !mOldLocalPrefixes.IsEmpty();
return (GetState() != kIdle) || !mOldLocalPrefixes.IsEmpty();
}

void RoutingManager::OnLinkPrefixManager::ResetExpireTime(TimeMilli aNow)
Expand All @@ -2444,15 +2453,9 @@ void RoutingManager::OnLinkPrefixManager::ResetExpireTime(TimeMilli aNow)
SavePrefix(mLocalPrefix, mExpireTime);
}

void RoutingManager::OnLinkPrefixManager::EnterAdvertisingState(void)
{
mState = kAdvertising;
LogInfo("Advertising local on-link prefix %s", mLocalPrefix.ToString().AsCString());
}

bool RoutingManager::OnLinkPrefixManager::IsPublishingOrAdvertising(void) const
{
return (mState == kPublishing) || (mState == kAdvertising);
return (GetState() == kPublishing) || (GetState() == kAdvertising);
}

void RoutingManager::OnLinkPrefixManager::AppendAsPiosTo(Ip6::Nd::RouterAdvertMessage &aRaMessage)
Expand All @@ -2474,7 +2477,7 @@ void RoutingManager::OnLinkPrefixManager::AppendCurPrefix(Ip6::Nd::RouterAdvertM
uint32_t preferredLifetime = kDefaultOnLinkPrefixLifetime;
TimeMilli now = TimerMilli::GetNow();

switch (mState)
switch (GetState())
{
case kAdvertising:
ResetExpireTime(now);
Expand Down Expand Up @@ -2520,11 +2523,11 @@ void RoutingManager::OnLinkPrefixManager::AppendOldPrefixes(Ip6::Nd::RouterAdver

void RoutingManager::OnLinkPrefixManager::HandleNetDataChange(void)
{
VerifyOrExit(mState == kPublishing);
VerifyOrExit(GetState() == kPublishing);

if (Get<RoutingManager>().NetworkDataContainsUlaRoute())
{
EnterAdvertisingState();
SetState(kAdvertising);
Get<RoutingManager>().ScheduleRoutingPolicyEvaluation(kAfterRandomDelay);
}

Expand All @@ -2541,7 +2544,7 @@ void RoutingManager::OnLinkPrefixManager::HandleExtPanIdChange(void)
// so to allow Thread nodes to continue to communicate with `InfraIf`
// device using addresses based on this prefix.

uint16_t oldState = mState;
uint16_t oldState = GetState();
Ip6::Prefix oldPrefix = mLocalPrefix;

GenerateLocalPrefix();
Expand Down Expand Up @@ -2630,7 +2633,7 @@ void RoutingManager::OnLinkPrefixManager::HandleTimer(void)
TimeMilli nextExpireTime = now.GetDistantFuture();
Array<Ip6::Prefix, kMaxOldPrefixes> expiredPrefixes;

switch (mState)
switch (GetState())
{
case kIdle:
break;
Expand All @@ -2639,9 +2642,8 @@ void RoutingManager::OnLinkPrefixManager::HandleTimer(void)
case kDeprecating:
if (now >= mExpireTime)
{
LogInfo("Removing expired local on-link prefix %s", mLocalPrefix.ToString().AsCString());
IgnoreError(Get<Settings>().RemoveBrOnLinkPrefix(mLocalPrefix));
mState = kIdle;
SetState(kIdle);
}
else
{
Expand Down Expand Up @@ -2677,6 +2679,23 @@ void RoutingManager::OnLinkPrefixManager::HandleTimer(void)
Get<RoutingManager>().mRoutePublisher.Evaluate();
}

const char *RoutingManager::OnLinkPrefixManager::StateToString(State aState)
{
static const char *const kStateStrings[] = {
"Removed", // (0) kIdle
"Publishing", // (1) kPublishing
"Advertising", // (2) kAdvertising
"Deprecating", // (3) kDeprecating
};

static_assert(0 == kIdle, "kIdle value is incorrect");
static_assert(1 == kPublishing, "kPublishing value is incorrect");
static_assert(2 == kAdvertising, "kAdvertising value is incorrect");
static_assert(3 == kDeprecating, "kDeprecating value is incorrect");

return kStateStrings[aState];
}

//---------------------------------------------------------------------------------------------------------------------
// OnMeshPrefixArray

Expand Down
21 changes: 12 additions & 9 deletions src/core/border_router/routing_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,15 +899,18 @@ class RoutingManager : public InstanceLocator
TimeMilli mExpireTime;
};

void GenerateLocalPrefix(void);
void PublishAndAdvertise(void);
void Deprecate(void);
void ResetExpireTime(TimeMilli aNow);
void EnterAdvertisingState(void);
void AppendCurPrefix(Ip6::Nd::RouterAdvertMessage &aRaMessage);
void AppendOldPrefixes(Ip6::Nd::RouterAdvertMessage &aRaMessage);
void DeprecateOldPrefix(const Ip6::Prefix &aPrefix, TimeMilli aExpireTime);
void SavePrefix(const Ip6::Prefix &aPrefix, TimeMilli aExpireTime);
State GetState(void) const { return mState; }
void SetState(State aState);
void GenerateLocalPrefix(void);
void PublishAndAdvertise(void);
void Deprecate(void);
void ResetExpireTime(TimeMilli aNow);
void AppendCurPrefix(Ip6::Nd::RouterAdvertMessage &aRaMessage);
void AppendOldPrefixes(Ip6::Nd::RouterAdvertMessage &aRaMessage);
void DeprecateOldPrefix(const Ip6::Prefix &aPrefix, TimeMilli aExpireTime);
void SavePrefix(const Ip6::Prefix &aPrefix, TimeMilli aExpireTime);

static const char *StateToString(State aState);

using ExpireTimer = TimerMilliIn<RoutingManager, &RoutingManager::HandleOnLinkPrefixManagerTimer>;

Expand Down

0 comments on commit 34e9b92

Please sign in to comment.