From 05f16b63c743e91c96bd0e15accd34407e238016 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 12 Feb 2025 10:28:27 -0800 Subject: [PATCH] [routing-manager] track and log favored OMR prefix changes (#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. --- src/core/border_router/routing_manager.cpp | 98 ++++++++++++++++------ src/core/border_router/routing_manager.hpp | 8 +- 2 files changed, 78 insertions(+), 28 deletions(-) diff --git a/src/core/border_router/routing_manager.cpp b/src/core/border_router/routing_manager.cpp index 5f7916ba4..5c5c9713c 100644 --- a/src/core/border_router/routing_manager.cpp +++ b/src/core/border_router/routing_manager.cpp @@ -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(); @@ -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().GetNextOnMeshPrefix(iterator, prefixConfig) == kErrorNone) { @@ -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().IsRunning()); - DetermineFavoredPrefix(); + DetermineFavoredPrefixInNetData(favoredPrefix); // Determine the local prefix and remove outdated prefix published by us. #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE @@ -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 (favoredPrefix.IsEmpty() || favoredPrefix.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. 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(); } @@ -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 diff --git a/src/core/border_router/routing_manager.hpp b/src/core/border_router/routing_manager.hpp index bfc138957..4d275cda5 100644 --- a/src/core/border_router/routing_manager.hpp +++ b/src/core/border_router/routing_manager.hpp @@ -1094,12 +1094,13 @@ private: //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - class FavoredOmrPrefix : public OmrPrefix + class FavoredOmrPrefix : public OmrPrefix, public Unequatable { friend class OmrPrefixManager; public: bool IsInfrastructureDerived(void) const; + bool operator==(const FavoredOmrPrefix &aOther) const; private: void SetFrom(const NetworkData::OnMeshPrefixConfig &aOnMeshPrefixConfig); @@ -1129,11 +1130,14 @@ private: typedef String 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;