Skip to content

Commit

Permalink
[routing-manager] track and log favored OMR prefix changes (openthrea…
Browse files Browse the repository at this point in the history
…d#11236)

This commit enhances the `OmrPrefixManager` to track and log changes
to the favored OMR prefix. The new log includes the old favored
prefix (if any) along with the new one, and provides additional
information such as its preference, whether it is a domain prefix, or
whether it matches the local OMR prefix. This replaces and enhances
the previous logs.

This new mechanism can also be used to signal (to other modules) when
the favored OMR prefix changes.
  • Loading branch information
abtink authored Feb 12, 2025
1 parent c1c5db8 commit 05f16b6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 28 deletions.
98 changes: 72 additions & 26 deletions src/core/border_router/routing_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,12 @@ bool RoutingManager::FavoredOmrPrefix::IsInfrastructureDerived(void) const
return !IsEmpty() && (mPreference >= NetworkData::kRoutePreferenceMedium);
}

bool RoutingManager::FavoredOmrPrefix::operator==(const FavoredOmrPrefix &aOther) const
{
return (mPreference == aOther.mPreference) && (mIsDomainPrefix == aOther.mIsDomainPrefix) &&
(mPrefix == aOther.mPrefix);
}

void RoutingManager::FavoredOmrPrefix::SetFrom(const NetworkData::OnMeshPrefixConfig &aOnMeshPrefixConfig)
{
mPrefix = aOnMeshPrefixConfig.GetPrefix();
Expand Down Expand Up @@ -2273,22 +2279,41 @@ void RoutingManager::OmrPrefixManager::Init(const Ip6::Prefix &aBrUlaPrefix)
LogInfo("Generated local OMR prefix: %s", mGeneratedPrefix.ToString().AsCString());
}

void RoutingManager::OmrPrefixManager::Start(void) { DetermineFavoredPrefix(); }
void RoutingManager::OmrPrefixManager::Start(void)
{
FavoredOmrPrefix favoredPrefix;

DetermineFavoredPrefixInNetData(favoredPrefix);
SetFavordPrefix(favoredPrefix);
}

void RoutingManager::OmrPrefixManager::Stop(void)
{
RemoveLocalFromNetData();
mFavoredPrefix.Clear();
ClearFavoredPrefix();
}

void RoutingManager::OmrPrefixManager::DetermineFavoredPrefix(void)
void RoutingManager::OmrPrefixManager::SetFavordPrefix(const OmrPrefix &aOmrPrefix)
{
FavoredOmrPrefix oldFavoredPrefix = mFavoredPrefix;

mFavoredPrefix.SetFrom(aOmrPrefix);

if (oldFavoredPrefix != mFavoredPrefix)
{
LogInfo("Favored OMR prefix: %s -> %s", FavoredToString(oldFavoredPrefix).AsCString(),
FavoredToString(mFavoredPrefix).AsCString());
}
}

void RoutingManager::OmrPrefixManager::DetermineFavoredPrefixInNetData(FavoredOmrPrefix &aFavoredPrefix)
{
// Determine the favored OMR prefix present in Network Data.

NetworkData::Iterator iterator = NetworkData::kIteratorInit;
NetworkData::OnMeshPrefixConfig prefixConfig;

mFavoredPrefix.Clear();
aFavoredPrefix.Clear();

while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, prefixConfig) == kErrorNone)
{
Expand All @@ -2297,18 +2322,20 @@ void RoutingManager::OmrPrefixManager::DetermineFavoredPrefix(void)
continue;
}

if (mFavoredPrefix.IsEmpty() || !mFavoredPrefix.IsFavoredOver(prefixConfig))
if (aFavoredPrefix.IsEmpty() || !aFavoredPrefix.IsFavoredOver(prefixConfig))
{
mFavoredPrefix.SetFrom(prefixConfig);
aFavoredPrefix.SetFrom(prefixConfig);
}
}
}

void RoutingManager::OmrPrefixManager::Evaluate(void)
{
FavoredOmrPrefix favoredPrefix;

OT_ASSERT(Get<RoutingManager>().IsRunning());

DetermineFavoredPrefix();
DetermineFavoredPrefixInNetData(favoredPrefix);

// Determine the local prefix and remove outdated prefix published by us.
#if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
Expand All @@ -2335,33 +2362,22 @@ void RoutingManager::OmrPrefixManager::Evaluate(void)
}

// Decide if we need to add or remove our local OMR prefix.
if (mFavoredPrefix.IsEmpty() || mFavoredPrefix.GetPreference() < mLocalPrefix.GetPreference())
{
if (mFavoredPrefix.IsEmpty())
{
LogInfo("No favored OMR prefix found in Thread network.");
}
else
{
LogInfo("Replacing favored OMR prefix %s with higher preference local prefix %s.",
mFavoredPrefix.GetPrefix().ToString().AsCString(), mLocalPrefix.GetPrefix().ToString().AsCString());
}

// The `mFavoredPrefix` remains empty if we fail to publish
// the local OMR prefix.
if (favoredPrefix.IsEmpty() || favoredPrefix.GetPreference() < mLocalPrefix.GetPreference())
{
SuccessOrExit(AddLocalToNetData());

mFavoredPrefix.SetFrom(mLocalPrefix);
SetFavordPrefix(mLocalPrefix);
ExitNow();
}
else if (mFavoredPrefix.GetPrefix() == mLocalPrefix.GetPrefix())

SetFavordPrefix(favoredPrefix);

if (favoredPrefix.GetPrefix() == mLocalPrefix.GetPrefix())
{
IgnoreError(AddLocalToNetData());
}
else if (mIsLocalAddedInNetData)
{
LogInfo("There is already a favored OMR prefix %s in the Thread network",
mFavoredPrefix.GetPrefix().ToString().AsCString());

RemoveLocalFromNetData();
}

Expand Down Expand Up @@ -2477,6 +2493,36 @@ RoutingManager::OmrPrefixManager::InfoString RoutingManager::OmrPrefixManager::L
return string;
}

RoutingManager::OmrPrefixManager::InfoString RoutingManager::OmrPrefixManager::FavoredToString(
const FavoredOmrPrefix &aFavoredPrefix) const
{
InfoString string;

if (aFavoredPrefix.IsEmpty())
{
string.Append("(none)");
}
else
{
string.Append("%s (prf:%s", aFavoredPrefix.GetPrefix().ToString().AsCString(),
RoutePreferenceToString(aFavoredPrefix.GetPreference()));

if (aFavoredPrefix.IsDomainPrefix())
{
string.Append(", domain");
}

if (aFavoredPrefix.GetPrefix() == mLocalPrefix.GetPrefix())
{
string.Append(", local");
}

string.Append(")");
}

return string;
}

//---------------------------------------------------------------------------------------------------------------------
// OnLinkPrefixManager

Expand Down
8 changes: 6 additions & 2 deletions src/core/border_router/routing_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,13 @@ class RoutingManager : public InstanceLocator

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

class FavoredOmrPrefix : public OmrPrefix
class FavoredOmrPrefix : public OmrPrefix, public Unequatable<FavoredOmrPrefix>
{
friend class OmrPrefixManager;

public:
bool IsInfrastructureDerived(void) const;
bool operator==(const FavoredOmrPrefix &aOther) const;

private:
void SetFrom(const NetworkData::OnMeshPrefixConfig &aOnMeshPrefixConfig);
Expand Down Expand Up @@ -1129,11 +1130,14 @@ class RoutingManager : public InstanceLocator

typedef String<kInfoStringSize> InfoString;

void DetermineFavoredPrefix(void);
void SetFavordPrefix(const OmrPrefix &aOmrPrefix);
void ClearFavoredPrefix(void) { SetFavordPrefix(OmrPrefix()); }
void DetermineFavoredPrefixInNetData(FavoredOmrPrefix &aFavoredPrefix);
Error AddLocalToNetData(void);
Error AddOrUpdateLocalInNetData(void);
void RemoveLocalFromNetData(void);
InfoString LocalToString(void) const;
InfoString FavoredToString(const FavoredOmrPrefix &aFavoredPrefix) const;

OmrPrefix mLocalPrefix;
Ip6::Prefix mGeneratedPrefix;
Expand Down

0 comments on commit 05f16b6

Please sign in to comment.