[border-router] simplify local OMR prefix update logic (#12750)

This commit simplifies the logic for updating the local OMR prefix
within `RoutingManager::OmrPrefixManager`.

The process of updating the prefix in `UpdateLocalPrefix()` is
consolidated into a single flow. Instead of multiple paths clearing
the old prefix from `NetworkData` and logging changes, the method now
determines the appropriate `prefix`, `preference`, and `origin`
(`kSelfGenerated`, `kCustom`, `kDhcp6Pd`), and delegates the change
to a shared sequence at the end of the method.

It also adds a `Matches()` method to `OmrPrefix` to efficiently check
if a given `Ip6::Prefix` and `RoutePreference` match the current OMR
prefix, avoiding unnecessary copies during updates.

Additionally, this change standardizes the log output format for local
OMR prefix updates by utilizing `LocalToString()` and ensures
the prefix's route preference is consistently included.
This commit is contained in:
Abtin Keshavarzian
2026-03-24 14:10:02 -05:00
committed by GitHub
parent 5b38208dbf
commit 52551d8ff0
4 changed files with 79 additions and 33 deletions
+5
View File
@@ -255,6 +255,11 @@ void OmrPrefix::SetPrefix(const Ip6::Prefix &aPrefix, RoutePreference aPreferenc
mPreference = aPreference;
}
bool OmrPrefix::Matches(const Ip6::Prefix &aPrefix, RoutePreference aPreference) const
{
return (mPreference == aPreference) && (mPrefix == aPrefix);
}
//---------------------------------------------------------------------------------------------------------------------
// FavoredOmrPrefix
+11
View File
@@ -582,6 +582,17 @@ public:
*/
void SetPrefix(const Ip6::Prefix &aPrefix, RoutePreference aPreference);
/**
* Indicates whether the OMR prefix matches a given prefix and preference.
*
* @param[in] aPrefix The prefix to compare with.
* @param[in] aPreference The preference to compare with.
*
* @retval TRUE The OMR prefix matches the given prefix and preference.
* @retval FALSE The OMR prefix does not match the given prefix and preference.
*/
bool Matches(const Ip6::Prefix &aPrefix, RoutePreference aPreference) const;
protected:
Ip6::Prefix mPrefix;
RoutePreference mPreference;
+54 -33
View File
@@ -857,8 +857,13 @@ void RoutingManager::OmrPrefixManager::DetermineFavoredPrefixInNetData(FavoredOm
void RoutingManager::OmrPrefixManager::UpdateLocalPrefix(void)
{
// Determine the local prefix and remove any outdated previous
// local prefix which may have been added in the Network Data.
// Determine the local prefix, its origin and remove any outdated
// previous local prefix which may have been added in the Network
// Data.
const Ip6::Prefix *prefix = nullptr;
RoutePreference preference;
PrefixOrigin origin;
switch (mConfig)
{
@@ -866,45 +871,46 @@ void RoutingManager::OmrPrefixManager::UpdateLocalPrefix(void)
#if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
if (Get<RoutingManager>().mPdPrefixManager.HasPrefix() && !Get<RoutingManager>().mPdPrefixManager.HasConflict())
{
if (mLocalPrefix.GetPrefix() != Get<RoutingManager>().mPdPrefixManager.GetPrefix())
{
RemoveLocalFromNetData();
mLocalPrefix.SetPrefix(Get<RoutingManager>().mPdPrefixManager.GetPrefix(),
PdPrefixManager::kPdRoutePreference);
LogInfo("Setting local OMR prefix to PD prefix: %s", mLocalPrefix.GetPrefix().ToString().AsCString());
}
prefix = &Get<RoutingManager>().mPdPrefixManager.GetPrefix();
preference = PdPrefixManager::kPdRoutePreference;
origin = kDhcp6Pd;
break;
}
else
#endif
if (mLocalPrefix.GetPrefix() != mGeneratedPrefix)
{
RemoveLocalFromNetData();
mLocalPrefix.SetPrefix(mGeneratedPrefix, RoutePreference::kRoutePreferenceLow);
LogInfo("Setting local OMR prefix to generated prefix: %s",
mLocalPrefix.GetPrefix().ToString().AsCString());
}
prefix = &mGeneratedPrefix;
preference = RoutePreference::kRoutePreferenceLow;
origin = kSelfGenerated;
break;
case kOmrConfigCustom:
if (mLocalPrefix != mCustomPrefix)
{
RemoveLocalFromNetData();
mLocalPrefix = mCustomPrefix;
LogInfo("Setting local OMR prefix to custom prefix: %s", mLocalPrefix.GetPrefix().ToString().AsCString());
}
prefix = &mCustomPrefix.GetPrefix();
preference = mCustomPrefix.GetPreference();
origin = kCustom;
break;
case kOmrConfigDisabled:
if (!mLocalPrefix.IsEmpty())
{
RemoveLocalFromNetData();
mLocalPrefix.Clear();
}
break;
}
if (prefix == nullptr)
{
VerifyOrExit(!mLocalPrefix.IsEmpty());
RemoveLocalFromNetData();
mLocalPrefix.Clear();
LogInfo("Cleared local OMR prefix");
ExitNow();
}
VerifyOrExit(!mLocalPrefix.Matches(*prefix, preference) || (origin != mLocalPrefixOrigin));
RemoveLocalFromNetData();
mLocalPrefix.SetPrefix(*prefix, preference);
mLocalPrefixOrigin = origin;
LogInfo("Set %s", LocalToString().AsCString());
exit:
return;
}
void RoutingManager::OmrPrefixManager::Evaluate(void)
@@ -1040,8 +1046,11 @@ RoutingManager::OmrPrefixManager::InfoString RoutingManager::OmrPrefixManager::L
{
InfoString string;
string.Append("local OMR prefix %s (def-route:%s)", mLocalPrefix.GetPrefix().ToString().AsCString(),
ToYesNo(mDefaultRoute));
string.Append("local OMR prefix %s (prf:%s, def-route:%s, origin:%s)",
mLocalPrefix.GetPrefix().ToString().AsCString(),
RoutePreferenceToString(mLocalPrefix.GetPreference()), ToYesNo(mDefaultRoute),
PrefixOriginToString(mLocalPrefixOrigin));
return string;
}
@@ -1087,6 +1096,18 @@ const char *RoutingManager::OmrPrefixManager::OmrConfigToString(OmrConfig aConfi
return kStrings[aConfig];
}
const char *RoutingManager::OmrPrefixManager::PrefixOriginToString(PrefixOrigin aOrigin)
{
#define PrefixOriginMapList(_) \
_(kSelfGenerated, "self-gen") \
_(kCustom, "custom") \
_(kDhcp6Pd, "dhcp6-pd")
DefineEnumStringArray(PrefixOriginMapList);
return kStrings[aOrigin];
}
//---------------------------------------------------------------------------------------------------------------------
// OnLinkPrefixManager
@@ -640,6 +640,13 @@ private:
typedef String<kInfoStringSize> InfoString;
enum PrefixOrigin : uint8_t // of `mLocalPrefix`
{
kSelfGenerated,
kCustom,
kDhcp6Pd,
};
void SetFavoredPrefix(const OmrPrefix &aOmrPrefix);
void ClearFavoredPrefix(void) { SetFavoredPrefix(OmrPrefix()); }
void DetermineFavoredPrefixInNetData(FavoredOmrPrefix &aFavoredPrefix);
@@ -651,12 +658,14 @@ private:
InfoString FavoredToString(const FavoredOmrPrefix &aFavoredPrefix) const;
static const char *OmrConfigToString(OmrConfig aConfig);
static const char *PrefixOriginToString(PrefixOrigin aOrigin);
OmrConfig mConfig;
OmrPrefix mLocalPrefix;
OmrPrefix mCustomPrefix;
Ip6::Prefix mGeneratedPrefix;
FavoredOmrPrefix mFavoredPrefix;
PrefixOrigin mLocalPrefixOrigin;
bool mIsLocalAddedInNetData;
bool mDefaultRoute;
};