[routing-manager] add Publish()/Unpublish() in Nat64PrefixManager (#11838)

This commit introduces `Publish()` and `Unpublish()` private helper
methods in `Nat64PrefixManager` to encapsulate and centralize the
logic for managing the published NAT64 prefix in the Network Data.

- `Publish(aPrefix, aPreference)` adds the given prefix to the Network
  Data. It handles removing any previously published prefix if the
  new prefix or preference differs.
- `Unpublish()` removes the currently published NAT64 prefix from the
  Network Data, if one exists.

These new helpers simplify the logic within the `Evaluate()` and
`Stop()` methods, reduce code duplication, and improve overall
clarity.
This commit is contained in:
Abtin Keshavarzian
2025-08-21 13:52:57 -07:00
committed by GitHub
parent e4f6008a57
commit c5f66c9e02
2 changed files with 41 additions and 18 deletions
+38 -15
View File
@@ -4055,12 +4055,7 @@ void RoutingManager::Nat64PrefixManager::Stop(void)
{
LogInfo("Stopping Nat64PrefixManager");
if (mPublishedPrefix.IsValidNat64())
{
IgnoreError(Get<NetworkData::Publisher>().UnpublishPrefix(mPublishedPrefix));
}
mPublishedPrefix.Clear();
Unpublish();
mInfraIfPrefix.Clear();
mTimer.Stop();
@@ -4129,17 +4124,13 @@ void RoutingManager::Nat64PrefixManager::Evaluate(void)
((error == kErrorNotFound) || (netdataPrefixConfig.mPreference < preference) ||
(netdataPrefixConfig.GetPrefix() == mPublishedPrefix) || (netdataPrefixConfig.GetPrefix() == mInfraIfPrefix));
if (mPublishedPrefix.IsValidNat64() && (!shouldPublish || (prefix != mPublishedPrefix)))
if (shouldPublish)
{
IgnoreError(Get<NetworkData::Publisher>().UnpublishPrefix(mPublishedPrefix));
mPublishedPrefix.Clear();
Publish(prefix, preference);
}
if (shouldPublish && ((prefix != mPublishedPrefix) || (preference != mPublishedPreference)))
else
{
mPublishedPrefix = prefix;
mPublishedPreference = preference;
Publish();
Unpublish();
}
#if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
@@ -4162,10 +4153,25 @@ exit:
return;
}
void RoutingManager::Nat64PrefixManager::Publish(void)
void RoutingManager::Nat64PrefixManager::Publish(const Ip6::Prefix &aPrefix, RoutePreference aPreference)
{
// Publishes the given prefix and preference in the Network Data
// if they differ from the currently published ones. This method
// updates `mPublishedPrefix` and `mPublishedPreference` to track
// the new values.
//
// If there is any change, this method ensures that the previous
// prefix is unpublished before the new one is published.
NetworkData::ExternalRouteConfig routeConfig;
VerifyOrExit((aPrefix != mPublishedPrefix) || (aPreference != mPublishedPreference));
Unpublish();
mPublishedPrefix = aPrefix;
mPublishedPreference = aPreference;
routeConfig.Clear();
routeConfig.SetPrefix(mPublishedPrefix);
routeConfig.mPreference = mPublishedPreference;
@@ -4174,6 +4180,23 @@ void RoutingManager::Nat64PrefixManager::Publish(void)
SuccessOrAssert(
Get<NetworkData::Publisher>().PublishExternalRoute(routeConfig, NetworkData::Publisher::kFromRoutingManager));
exit:
return;
}
void RoutingManager::Nat64PrefixManager::Unpublish(void)
{
// Unpublishes the previously published prefix (if any) and clears
// the `mPublishedPrefix`.
VerifyOrExit(mPublishedPrefix.IsValidNat64());
IgnoreError(Get<NetworkData::Publisher>().UnpublishPrefix(mPublishedPrefix));
mPublishedPrefix.Clear();
exit:
return;
}
void RoutingManager::Nat64PrefixManager::HandleTimer(void)
+3 -3
View File
@@ -1556,12 +1556,12 @@ private:
private:
void Discover(void);
void Publish(void);
void Publish(const Ip6::Prefix &aPrefix, RoutePreference aPreference);
void Unpublish(void);
using Nat64Timer = TimerMilliIn<RoutingManager, &RoutingManager::HandleNat64PrefixManagerTimer>;
bool mEnabled;
bool mEnabled;
Ip6::Prefix mInfraIfPrefix; // The latest NAT64 prefix discovered on the infrastructure interface.
Ip6::Prefix mLocalPrefix; // The local prefix (from BR ULA prefix).
Ip6::Prefix mPublishedPrefix; // The prefix to publish in Net Data (empty or local or from infra-if).