Skip to content

Commit

Permalink
[routing-manager] add CalculateExpirationTime() (openthread#10207)
Browse files Browse the repository at this point in the history
This commit adds `CalculateExpirationTime()`, which simplifies the
calculation of expiration times of discovered prefix entry by
clamping a given lifetime to the maximum value (in seconds) first
before converting to milliseconds.
  • Loading branch information
abtink authored May 7, 2024
1 parent 83af14a commit 383d0d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
22 changes: 8 additions & 14 deletions src/core/border_router/routing_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ bool RoutingManager::DiscoveredPrefixTable::Entry::Matches(const ExpirationCheck

TimeMilli RoutingManager::DiscoveredPrefixTable::Entry::GetExpireTime(void) const
{
return mLastUpdateTime + CalculateExpireDelay(mValidLifetime);
return CalculateExpirationTime(mValidLifetime);
}

TimeMilli RoutingManager::DiscoveredPrefixTable::Entry::GetStaleTime(void) const
Expand All @@ -1752,14 +1752,14 @@ TimeMilli RoutingManager::DiscoveredPrefixTable::Entry::GetStaleTime(void) const

TimeMilli RoutingManager::DiscoveredPrefixTable::Entry::GetStaleTimeFromPreferredLifetime(void) const
{
return mLastUpdateTime + CalculateExpireDelay(GetPreferredLifetime());
return CalculateExpirationTime(GetPreferredLifetime());
}

bool RoutingManager::DiscoveredPrefixTable::Entry::IsDeprecated(void) const
{
OT_ASSERT(IsOnLinkPrefix());

return mLastUpdateTime + CalculateExpireDelay(GetPreferredLifetime()) <= TimerMilli::GetNow();
return CalculateExpirationTime(GetPreferredLifetime()) <= TimerMilli::GetNow();
}

RoutingManager::RoutePreference RoutingManager::DiscoveredPrefixTable::Entry::GetPreference(void) const
Expand Down Expand Up @@ -1798,20 +1798,14 @@ void RoutingManager::DiscoveredPrefixTable::Entry::AdoptValidAndPreferredLifetim
mLastUpdateTime = aEntry.GetLastUpdateTime();
}

uint32_t RoutingManager::DiscoveredPrefixTable::Entry::CalculateExpireDelay(uint32_t aValidLifetime)
TimeMilli RoutingManager::DiscoveredPrefixTable::Entry::CalculateExpirationTime(uint32_t aLifetime) const
{
uint32_t delay;
// `aLifetime` is in unit of seconds. We clamp the lifetime to max
// interval supported by `Timer` (`2^31` msec or ~24.8 days).

if (aValidLifetime * static_cast<uint64_t>(1000) > Timer::kMaxDelay)
{
delay = Timer::kMaxDelay;
}
else
{
delay = aValidLifetime * 1000;
}
static constexpr uint32_t kMaxLifetime = Time::MsecToSec(Timer::kMaxDelay);

return delay;
return mLastUpdateTime + Time::SecToMsec(Min(aLifetime, kMaxLifetime));
}

//---------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/core/border_router/routing_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ class RoutingManager : public InstanceLocator
RoutePreference GetRoutePreference(void) const { return mShared.mRoutePreference; }

private:
static uint32_t CalculateExpireDelay(uint32_t aValidLifetime);
TimeMilli CalculateExpirationTime(uint32_t aLifetime) const;

Entry *mNext;
Ip6::Prefix mPrefix;
Expand Down

0 comments on commit 383d0d2

Please sign in to comment.