mirror of
https://github.com/espressif/openthread.git
synced 2026-09-13 04:30:08 +00:00
[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:
@@ -255,6 +255,11 @@ void OmrPrefix::SetPrefix(const Ip6::Prefix &aPrefix, RoutePreference aPreferenc
|
|||||||
mPreference = aPreference;
|
mPreference = aPreference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool OmrPrefix::Matches(const Ip6::Prefix &aPrefix, RoutePreference aPreference) const
|
||||||
|
{
|
||||||
|
return (mPreference == aPreference) && (mPrefix == aPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
// FavoredOmrPrefix
|
// FavoredOmrPrefix
|
||||||
|
|
||||||
|
|||||||
@@ -582,6 +582,17 @@ public:
|
|||||||
*/
|
*/
|
||||||
void SetPrefix(const Ip6::Prefix &aPrefix, RoutePreference aPreference);
|
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:
|
protected:
|
||||||
Ip6::Prefix mPrefix;
|
Ip6::Prefix mPrefix;
|
||||||
RoutePreference mPreference;
|
RoutePreference mPreference;
|
||||||
|
|||||||
@@ -857,8 +857,13 @@ void RoutingManager::OmrPrefixManager::DetermineFavoredPrefixInNetData(FavoredOm
|
|||||||
|
|
||||||
void RoutingManager::OmrPrefixManager::UpdateLocalPrefix(void)
|
void RoutingManager::OmrPrefixManager::UpdateLocalPrefix(void)
|
||||||
{
|
{
|
||||||
// Determine the local prefix and remove any outdated previous
|
// Determine the local prefix, its origin and remove any outdated
|
||||||
// local prefix which may have been added in the Network Data.
|
// previous local prefix which may have been added in the Network
|
||||||
|
// Data.
|
||||||
|
|
||||||
|
const Ip6::Prefix *prefix = nullptr;
|
||||||
|
RoutePreference preference;
|
||||||
|
PrefixOrigin origin;
|
||||||
|
|
||||||
switch (mConfig)
|
switch (mConfig)
|
||||||
{
|
{
|
||||||
@@ -866,45 +871,46 @@ void RoutingManager::OmrPrefixManager::UpdateLocalPrefix(void)
|
|||||||
#if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
|
#if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
|
||||||
if (Get<RoutingManager>().mPdPrefixManager.HasPrefix() && !Get<RoutingManager>().mPdPrefixManager.HasConflict())
|
if (Get<RoutingManager>().mPdPrefixManager.HasPrefix() && !Get<RoutingManager>().mPdPrefixManager.HasConflict())
|
||||||
{
|
{
|
||||||
if (mLocalPrefix.GetPrefix() != Get<RoutingManager>().mPdPrefixManager.GetPrefix())
|
prefix = &Get<RoutingManager>().mPdPrefixManager.GetPrefix();
|
||||||
{
|
preference = PdPrefixManager::kPdRoutePreference;
|
||||||
RemoveLocalFromNetData();
|
origin = kDhcp6Pd;
|
||||||
mLocalPrefix.SetPrefix(Get<RoutingManager>().mPdPrefixManager.GetPrefix(),
|
break;
|
||||||
PdPrefixManager::kPdRoutePreference);
|
|
||||||
LogInfo("Setting local OMR prefix to PD prefix: %s", mLocalPrefix.GetPrefix().ToString().AsCString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
#endif
|
#endif
|
||||||
|
prefix = &mGeneratedPrefix;
|
||||||
if (mLocalPrefix.GetPrefix() != mGeneratedPrefix)
|
preference = RoutePreference::kRoutePreferenceLow;
|
||||||
{
|
origin = kSelfGenerated;
|
||||||
RemoveLocalFromNetData();
|
|
||||||
mLocalPrefix.SetPrefix(mGeneratedPrefix, RoutePreference::kRoutePreferenceLow);
|
|
||||||
LogInfo("Setting local OMR prefix to generated prefix: %s",
|
|
||||||
mLocalPrefix.GetPrefix().ToString().AsCString());
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kOmrConfigCustom:
|
case kOmrConfigCustom:
|
||||||
if (mLocalPrefix != mCustomPrefix)
|
prefix = &mCustomPrefix.GetPrefix();
|
||||||
{
|
preference = mCustomPrefix.GetPreference();
|
||||||
RemoveLocalFromNetData();
|
origin = kCustom;
|
||||||
mLocalPrefix = mCustomPrefix;
|
|
||||||
LogInfo("Setting local OMR prefix to custom prefix: %s", mLocalPrefix.GetPrefix().ToString().AsCString());
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kOmrConfigDisabled:
|
case kOmrConfigDisabled:
|
||||||
if (!mLocalPrefix.IsEmpty())
|
|
||||||
{
|
|
||||||
RemoveLocalFromNetData();
|
|
||||||
mLocalPrefix.Clear();
|
|
||||||
}
|
|
||||||
break;
|
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)
|
void RoutingManager::OmrPrefixManager::Evaluate(void)
|
||||||
@@ -1040,8 +1046,11 @@ RoutingManager::OmrPrefixManager::InfoString RoutingManager::OmrPrefixManager::L
|
|||||||
{
|
{
|
||||||
InfoString string;
|
InfoString string;
|
||||||
|
|
||||||
string.Append("local OMR prefix %s (def-route:%s)", mLocalPrefix.GetPrefix().ToString().AsCString(),
|
string.Append("local OMR prefix %s (prf:%s, def-route:%s, origin:%s)",
|
||||||
ToYesNo(mDefaultRoute));
|
mLocalPrefix.GetPrefix().ToString().AsCString(),
|
||||||
|
RoutePreferenceToString(mLocalPrefix.GetPreference()), ToYesNo(mDefaultRoute),
|
||||||
|
PrefixOriginToString(mLocalPrefixOrigin));
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1087,6 +1096,18 @@ const char *RoutingManager::OmrPrefixManager::OmrConfigToString(OmrConfig aConfi
|
|||||||
return kStrings[aConfig];
|
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
|
// OnLinkPrefixManager
|
||||||
|
|
||||||
|
|||||||
@@ -640,6 +640,13 @@ private:
|
|||||||
|
|
||||||
typedef String<kInfoStringSize> InfoString;
|
typedef String<kInfoStringSize> InfoString;
|
||||||
|
|
||||||
|
enum PrefixOrigin : uint8_t // of `mLocalPrefix`
|
||||||
|
{
|
||||||
|
kSelfGenerated,
|
||||||
|
kCustom,
|
||||||
|
kDhcp6Pd,
|
||||||
|
};
|
||||||
|
|
||||||
void SetFavoredPrefix(const OmrPrefix &aOmrPrefix);
|
void SetFavoredPrefix(const OmrPrefix &aOmrPrefix);
|
||||||
void ClearFavoredPrefix(void) { SetFavoredPrefix(OmrPrefix()); }
|
void ClearFavoredPrefix(void) { SetFavoredPrefix(OmrPrefix()); }
|
||||||
void DetermineFavoredPrefixInNetData(FavoredOmrPrefix &aFavoredPrefix);
|
void DetermineFavoredPrefixInNetData(FavoredOmrPrefix &aFavoredPrefix);
|
||||||
@@ -651,12 +658,14 @@ private:
|
|||||||
InfoString FavoredToString(const FavoredOmrPrefix &aFavoredPrefix) const;
|
InfoString FavoredToString(const FavoredOmrPrefix &aFavoredPrefix) const;
|
||||||
|
|
||||||
static const char *OmrConfigToString(OmrConfig aConfig);
|
static const char *OmrConfigToString(OmrConfig aConfig);
|
||||||
|
static const char *PrefixOriginToString(PrefixOrigin aOrigin);
|
||||||
|
|
||||||
OmrConfig mConfig;
|
OmrConfig mConfig;
|
||||||
OmrPrefix mLocalPrefix;
|
OmrPrefix mLocalPrefix;
|
||||||
OmrPrefix mCustomPrefix;
|
OmrPrefix mCustomPrefix;
|
||||||
Ip6::Prefix mGeneratedPrefix;
|
Ip6::Prefix mGeneratedPrefix;
|
||||||
FavoredOmrPrefix mFavoredPrefix;
|
FavoredOmrPrefix mFavoredPrefix;
|
||||||
|
PrefixOrigin mLocalPrefixOrigin;
|
||||||
bool mIsLocalAddedInNetData;
|
bool mIsLocalAddedInNetData;
|
||||||
bool mDefaultRoute;
|
bool mDefaultRoute;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user