[router-table] add new helper methods FindNextHopOf() and renames (#8437)

This commit contains smaller enhancements in `RouterTable` class:

- New method `FindNextHopOf()` is added to find the next hop of a
  given router.
- Methods which find router are renamed: `FindRouterById()` to search
  for a given Router ID, `FindRouterByRloc16()` to search for a given
  RLOC16.
- The methods which find a neighbor router are declared as `private`
  and only used by `friend` class `NeighborTable`. Other modules use
  `NeighborTable` methods to find neighbors.
This commit is contained in:
Abtin Keshavarzian
2022-11-28 21:12:21 -08:00
committed by Jonathan Hui
parent d88ea9a463
commit 43a6430083
4 changed files with 108 additions and 82 deletions
+30 -31
View File
@@ -663,7 +663,7 @@ void MleRouter::HandleLinkRequest(RxInfo &aRxInfo)
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr);
neighbor = mRouterTable.GetRouter(RouterIdFromRloc16(sourceAddress));
neighbor = mRouterTable.FindRouterByRloc16(sourceAddress);
VerifyOrExit(neighbor != nullptr, error = kErrorParse);
VerifyOrExit(!neighbor->IsStateLinkRequest(), error = kErrorAlready);
@@ -860,7 +860,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
VerifyOrExit(IsActiveRouter(sourceAddress), error = kErrorParse);
routerId = RouterIdFromRloc16(sourceAddress);
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
neighborState = (router != nullptr) ? router->GetState() : Neighbor::kStateInvalid;
// Response
@@ -927,7 +927,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
// Route
mRouterTable.Clear();
SuccessOrExit(error = ProcessRouteTlv(aRxInfo));
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit(router != nullptr);
if (mLeaderData.GetLeaderRouterId() == RouterIdFromRloc16(GetRloc16()))
@@ -972,7 +972,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
case kErrorNone:
UpdateRoutes(routeTlv, routerId);
// Need to update router after ProcessRouteTlv
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
OT_ASSERT(router != nullptr);
break;
@@ -1074,7 +1074,7 @@ uint8_t MleRouter::GetLinkCost(uint8_t aRouterId)
uint8_t rval = kMaxRouteCost;
Router *router;
router = mRouterTable.GetRouter(aRouterId);
router = mRouterTable.FindRouterById(aRouterId);
// `nullptr` aRouterId indicates non-existing next hop, hence return kMaxRouteCost for it.
VerifyOrExit(router != nullptr);
@@ -1141,7 +1141,7 @@ Error MleRouter::ProcessRouteTlv(RxInfo &aRxInfo, RouteTlv &aRouteTlv)
if (neighborRloc16 != Mac::kShortAddrInvalid)
{
aRxInfo.mNeighbor = Get<RouterTable>().GetNeighbor(neighborRloc16);
aRxInfo.mNeighbor = Get<NeighborTable>().FindNeighbor(neighborRloc16);
}
exit:
@@ -1315,7 +1315,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo)
}
else
{
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
if (router != nullptr && router->IsStateValid())
{
@@ -1402,7 +1402,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo)
else
{
// MLE Advertisement not from parent, but from some other neighboring router
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit(router != nullptr);
if (IsFullThreadDevice() && !router->IsStateValid() && !router->IsStateLinkRequest() &&
@@ -1430,7 +1430,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo)
IgnoreError(SendLinkRequest(nullptr));
}
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit(router != nullptr);
// check current active router number
@@ -1458,7 +1458,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo)
OT_FALL_THROUGH;
case kRoleLeader:
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit(router != nullptr);
// Send unicast link request if no link to router and no unicast/multicast link request in progress
@@ -1497,7 +1497,7 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
bool resetAdvInterval = false;
bool changed = false;
neighbor = mRouterTable.GetRouter(aRouterId);
neighbor = mRouterTable.FindRouterById(aRouterId);
VerifyOrExit(neighbor != nullptr);
// update link quality out to neighbor
@@ -1508,7 +1508,6 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
{
Router *router;
Router *nextHop;
uint8_t oldNextHop;
uint8_t cost;
if (!aRoute.IsRouterIdSet(routerId))
@@ -1516,7 +1515,7 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
continue;
}
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
if (router == nullptr || router->GetRloc16() == GetRloc16() || router == neighbor)
{
@@ -1524,8 +1523,7 @@ void MleRouter::UpdateRoutes(const RouteTlv &aRoute, uint8_t aRouterId)
continue;
}
oldNextHop = router->GetNextHop();
nextHop = mRouterTable.GetRouter(oldNextHop);
nextHop = mRouterTable.FindNextHopOf(*router);
cost = aRoute.GetRouteCost(routeCount);
@@ -1624,7 +1622,7 @@ bool MleRouter::UpdateLinkQualityOut(const RouteTlv &aRoute, Router &aNeighbor,
oldLinkCost = mRouterTable.GetLinkCost(aNeighbor);
aNeighbor.SetLinkQualityOut(linkQuality);
nextHop = mRouterTable.GetRouter(aNeighbor.GetNextHop());
nextHop = mRouterTable.FindNextHopOf(aNeighbor);
// reset MLE advertisement timer if neighbor route cost changed to or from infinite
if (nextHop == nullptr && (oldLinkCost >= kMaxRouteCost) != (mRouterTable.GetLinkCost(aNeighbor) >= kMaxRouteCost))
@@ -1995,8 +1993,8 @@ void MleRouter::HandleTimeTick(void)
if (IsLeader())
{
if (mRouterTable.GetRouter(router.GetNextHop()) == nullptr &&
mRouterTable.GetLinkCost(router) >= kMaxRouteCost && age >= Time::SecToMsec(kMaxLeaderToRouterTimeout))
if (mRouterTable.FindNextHopOf(router) == nullptr && mRouterTable.GetLinkCost(router) >= kMaxRouteCost &&
age >= Time::SecToMsec(kMaxLeaderToRouterTimeout))
{
LogInfo("Router ID timeout expired (no route)");
IgnoreError(mRouterTable.Release(router.GetRouterId()));
@@ -2402,7 +2400,8 @@ void MleRouter::HandleChildIdRequest(RxInfo &aRxInfo)
}
// Remove from router table
router = mRouterTable.GetRouter(extAddr);
router = mRouterTable.FindRouter(extAddr);
if (router != nullptr)
{
// The `router` here can be invalid
@@ -3549,7 +3548,7 @@ uint16_t MleRouter::GetNextHop(uint16_t aDestination)
ExitNow(rval = aDestination);
}
router = mRouterTable.GetRouter(destinationId);
router = mRouterTable.FindRouterById(destinationId);
VerifyOrExit(router != nullptr);
linkCost = GetLinkCost(destinationId);
@@ -3557,7 +3556,7 @@ uint16_t MleRouter::GetNextHop(uint16_t aDestination)
if ((routeCost + GetLinkCost(router->GetNextHop())) < linkCost)
{
nextHop = mRouterTable.GetRouter(router->GetNextHop());
nextHop = mRouterTable.FindNextHopOf(*router);
VerifyOrExit(nextHop != nullptr && !nextHop->IsStateInvalid());
rval = Rloc16FromRouterId(router->GetNextHop());
@@ -3575,10 +3574,10 @@ uint8_t MleRouter::GetCost(uint16_t aRloc16)
{
uint8_t routerId = RouterIdFromRloc16(aRloc16);
uint8_t cost = GetLinkCost(routerId);
Router *router = mRouterTable.GetRouter(routerId);
Router *router = mRouterTable.FindRouterById(routerId);
uint8_t routeCost;
VerifyOrExit(router != nullptr && mRouterTable.GetRouter(router->GetNextHop()) != nullptr);
VerifyOrExit(router != nullptr && mRouterTable.FindNextHopOf(*router) != nullptr);
routeCost = GetRouteCost(aRloc16) + GetLinkCost(router->GetNextHop());
@@ -3596,8 +3595,8 @@ uint8_t MleRouter::GetRouteCost(uint16_t aRloc16) const
uint8_t rval = kMaxRouteCost;
const Router *router;
router = mRouterTable.GetRouter(RouterIdFromRloc16(aRloc16));
VerifyOrExit(router != nullptr && mRouterTable.GetRouter(router->GetNextHop()) != nullptr);
router = mRouterTable.FindRouterByRloc16(aRloc16);
VerifyOrExit(router != nullptr && mRouterTable.FindNextHopOf(*router) != nullptr);
rval = router->GetCost();
@@ -3633,7 +3632,7 @@ void MleRouter::ResolveRoutingLoops(uint16_t aSourceMac, uint16_t aDestRloc16)
}
// loop exists
router = mRouterTable.GetRouter(RouterIdFromRloc16(aDestRloc16));
router = mRouterTable.FindRouterByRloc16(aDestRloc16);
VerifyOrExit(router != nullptr);
// invalidate next hop
@@ -3810,13 +3809,13 @@ void MleRouter::HandleAddressSolicitResponse(Coap::Message * aMessage,
mRouterTable.Clear();
mRouterTable.UpdateRouterIdSet(routerMaskTlv.GetIdSequence(), routerMaskTlv.GetAssignedRouterIdMask());
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit(router != nullptr);
router->SetExtAddress(Get<Mac::Mac>().GetExtAddress());
router->SetCost(0);
router = mRouterTable.GetRouter(mParent.GetRouterId());
router = mRouterTable.FindRouterById(mParent.GetRouterId());
VerifyOrExit(router != nullptr);
// Keep link to the parent in order to respond to Parent Requests before new link is established.
@@ -3905,7 +3904,7 @@ template <> void MleRouter::HandleTmf<kUriAddressSolicit>(Coap::Message &aMessag
#endif
// Check if allocation already exists
router = mRouterTable.GetRouter(extAddress);
router = mRouterTable.FindRouter(extAddress);
if (router != nullptr)
{
@@ -4015,7 +4014,7 @@ template <> void MleRouter::HandleTmf<kUriAddressRelease>(Coap::Message &aMessag
SuccessOrExit(Tlv::Find<ThreadExtMacAddressTlv>(aMessage, extAddress));
routerId = RouterIdFromRloc16(rloc16);
router = mRouterTable.GetRouter(routerId);
router = mRouterTable.FindRouterById(routerId);
VerifyOrExit((router != nullptr) && (router->GetExtAddress() == extAddress));
@@ -4221,7 +4220,7 @@ void MleRouter::FillRouteTlv(RouteTlv &aTlv, Neighbor *aNeighbor)
uint8_t routeCost;
linkCost = mRouterTable.GetLinkCost(router);
nextHop = mRouterTable.GetRouter(router.GetNextHop());
nextHop = mRouterTable.FindNextHopOf(router);
if (nextHop == nullptr)
{
+2 -2
View File
@@ -172,7 +172,7 @@ Neighbor *NeighborTable::FindRxOnlyNeighborRouter(const Mac::Address &aMacAddres
Neighbor *neighbor = nullptr;
VerifyOrExit(Get<Mle::Mle>().IsChild());
neighbor = Get<RouterTable>().GetNeighbor(aMacAddress);
neighbor = Get<RouterTable>().FindNeighbor(aMacAddress);
exit:
return neighbor;
@@ -213,7 +213,7 @@ Error NeighborTable::GetNextNeighborInfo(otNeighborInfoIterator &aIterator, Neig
for (index = -aIterator; index <= Mle::kMaxRouterId; index++)
{
Router *router = Get<RouterTable>().GetRouter(static_cast<uint8_t>(index));
Router *router = Get<RouterTable>().FindRouterById(static_cast<uint8_t>(index));
if (router != nullptr && router->IsStateValid())
{
+21 -11
View File
@@ -253,7 +253,7 @@ Router *RouterTable::Allocate(uint8_t aRouterId)
mAllocatedRouterIds.Add(aRouterId);
UpdateAllocation();
rval = GetRouter(aRouterId);
rval = FindRouterById(aRouterId);
rval->SetLastHeard(TimerMilli::GetNow());
mRouterIdSequence++;
@@ -277,7 +277,7 @@ Error RouterTable::Release(uint8_t aRouterId)
VerifyOrExit(Get<Mle::MleRouter>().IsLeader(), error = kErrorInvalidState);
VerifyOrExit(IsAllocated(aRouterId), error = kErrorNotFound);
router = GetNeighbor(rloc16);
router = FindNeighbor(rloc16);
if (router != nullptr)
{
@@ -357,7 +357,7 @@ const Router *RouterTable::FindRouter(const Router::AddressMatcher &aMatcher) co
return router;
}
Router *RouterTable::GetNeighbor(uint16_t aRloc16)
Router *RouterTable::FindNeighbor(uint16_t aRloc16)
{
Router *router = nullptr;
@@ -368,17 +368,17 @@ exit:
return router;
}
Router *RouterTable::GetNeighbor(const Mac::ExtAddress &aExtAddress)
Router *RouterTable::FindNeighbor(const Mac::ExtAddress &aExtAddress)
{
return FindRouter(Router::AddressMatcher(aExtAddress, Router::kInStateValid));
}
Router *RouterTable::GetNeighbor(const Mac::Address &aMacAddress)
Router *RouterTable::FindNeighbor(const Mac::Address &aMacAddress)
{
return FindRouter(Router::AddressMatcher(aMacAddress, Router::kInStateValid));
}
const Router *RouterTable::GetRouter(uint8_t aRouterId) const
const Router *RouterTable::FindRouterById(uint8_t aRouterId) const
{
const Router *router = nullptr;
uint16_t rloc16;
@@ -393,7 +393,17 @@ exit:
return router;
}
Router *RouterTable::GetRouter(const Mac::ExtAddress &aExtAddress)
const Router *RouterTable::FindRouterByRloc16(uint16_t aRloc16) const
{
return FindRouterById(Mle::RouterIdFromRloc16(aRloc16));
}
const Router *RouterTable::FindNextHopOf(const Router &aRouter) const
{
return FindRouterById(aRouter.GetNextHop());
}
Router *RouterTable::FindRouter(const Mac::ExtAddress &aExtAddress)
{
return FindRouter(Router::AddressMatcher(aExtAddress, Router::kInStateAny));
}
@@ -415,7 +425,7 @@ Error RouterTable::GetRouterInfo(uint16_t aRouterId, Router::Info &aRouterInfo)
VerifyOrExit(routerId <= Mle::kMaxRouterId, error = kErrorInvalidArgs);
}
router = GetRouter(routerId);
router = FindRouterById(routerId);
VerifyOrExit(router != nullptr, error = kErrorNotFound);
aRouterInfo.SetFrom(*router);
@@ -426,7 +436,7 @@ exit:
Router *RouterTable::GetLeader(void)
{
return GetRouter(Get<Mle::MleRouter>().GetLeaderId());
return FindRouterById(Get<Mle::MleRouter>().GetLeaderId());
}
uint32_t RouterTable::GetLeaderAge(void) const
@@ -449,7 +459,7 @@ uint8_t RouterTable::GetNeighborCount(void) const
return count;
}
uint8_t RouterTable::GetLinkCost(Router &aRouter)
uint8_t RouterTable::GetLinkCost(const Router &aRouter) const
{
uint8_t rval = Mle::kMaxRouteCost;
@@ -473,7 +483,7 @@ void RouterTable::UpdateRouterIdSet(uint8_t aRouterIdSequence, const Mle::Router
// If was allocated but removed in new Router Id Set
if (IsAllocated(routerId) && !aRouterIdSet.Contains(routerId))
{
Router *router = GetRouter(routerId);
Router *router = FindRouterForId(routerId);
OT_ASSERT(router != nullptr);
router->SetNextHop(Mle::kInvalidRouterId);
+55 -38
View File
@@ -104,29 +104,31 @@ public:
void ClearNeighbors(void);
/**
* This method allocates a router with a random router id.
* This method allocates a router with a random Router ID.
*
* @returns A pointer to the allocated router or `nullptr` if a router ID is not available.
* @returns A pointer to the allocated router or `nullptr` if a Router ID is not available.
*
*/
Router *Allocate(void);
/**
* This method allocates a router with a specified router id.
* This method allocates a router with a specified Router ID.
*
* @returns A pointer to the allocated router or `nullptr` if the router id could not be allocated.
* @param[in] aRouterId The Router ID to try to allocate.
*
* @returns A pointer to the allocated router or `nullptr` if the ID @p aRouterId could not be allocated.
*
*/
Router *Allocate(uint8_t aRouterId);
/**
* This method releases a router id.
* This method releases a Router ID.
*
* @param[in] aRouterId The router id.
* @param[in] aRouterId The Router ID.
*
* @retval kErrorNone Successfully released the router id.
* @retval kErrorNone Successfully released the Router ID @p aRouterId.
* @retval kErrorInvalidState The device is not currently operating as a leader.
* @retval kErrorNotFound The router id is not currently allocated.
* @retval kErrorNotFound The Router ID @p aRouterId is not currently allocated.
*
*/
Error Release(uint8_t aRouterId);
@@ -156,9 +158,9 @@ public:
Router *GetLeader(void);
/**
* This method returns the time in seconds since the last Router ID Sequence update.
* This method returns the leader's age in seconds, i.e., seconds since the last Router ID Sequence update.
*
* @returns The time in seconds since the last Router ID Sequence update.
* @returns The leader's age.
*
*/
uint32_t GetLeaderAge(void) const;
@@ -166,81 +168,91 @@ public:
/**
* This method returns the link cost for a neighboring router.
*
* @param[in] aRouter A reference to the router.
* @param[in] aRouter A router.
*
* @returns The link cost.
* @returns The link cost to @p aRouter.
*
*/
uint8_t GetLinkCost(Router &aRouter);
uint8_t GetLinkCost(const Router &aRouter) const;
/**
* This method returns the neighbor for a given RLOC16.
* This method finds the router for a given Router ID.
*
* @param[in] aRloc16 The RLOC16 value.
* @param[in] aRouterId The Router ID to search for.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *GetNeighbor(uint16_t aRloc16);
Router *FindRouterById(uint8_t aRouterId) { return AsNonConst(AsConst(this)->FindRouterById(aRouterId)); }
/**
* This method returns the neighbor for a given IEEE Extended Address.
* This method finds the router for a given Router ID.
*
* @param[in] aExtAddress A reference to the IEEE Extended Address.
* @param[in] aRouterId The Router ID to search for.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *GetNeighbor(const Mac::ExtAddress &aExtAddress);
const Router *FindRouterById(uint8_t aRouterId) const;
/**
* This method returns the neighbor for a given MAC address.
* This method finds the router for a given RLOC16.
*
* @param[in] aMacAddress A MAC address
* @param[in] aRloc16 The RLOC16 to search for.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *GetNeighbor(const Mac::Address &aMacAddress);
Router *FindRouterByRloc16(uint16_t aRloc16) { return AsNonConst(AsConst(this)->FindRouterByRloc16(aRloc16)); }
/**
* This method returns the router for a given router id.
* This method finds the router for a given RLOC16.
*
* @param[in] aRouterId The router id.
* @param[in] aRloc16 The RLOC16 to search for.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *GetRouter(uint8_t aRouterId) { return AsNonConst(AsConst(this)->GetRouter(aRouterId)); }
const Router *FindRouterByRloc16(uint16_t aRloc16) const;
/**
* This method returns the router for a given router id.
* This method finds the router that is the next hop of a given router.
*
* @param[in] aRouterId The router id.
* @param[in] aRouter The router to find next hop of.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
const Router *GetRouter(uint8_t aRouterId) const;
Router *FindNextHopOf(const Router &aRouter) { return AsNonConst(AsConst(this)->FindNextHopOf(aRouter)); }
/**
* This method returns the router for a given IEEE Extended Address.
* This method finds the router that is the next hop of a given router.
*
* @param[in] aExtAddress A reference to the IEEE Extended Address.
* @param[in] aRouter The router to find next hop of.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *GetRouter(const Mac::ExtAddress &aExtAddress);
const Router *FindNextHopOf(const Router &aRouter) const;
/**
* This method returns if the router table contains a given `Neighbor` instance.
* This method find the router for a given MAC Extended Address.
*
* @param[in] aExtAddress A reference to the MAC Extended Address.
*
* @returns A pointer to the router or `nullptr` if the router could not be found.
*
*/
Router *FindRouter(const Mac::ExtAddress &aExtAddress);
/**
* This method indicates whether the router table contains a given `Neighbor` instance.
*
* @param[in] aNeighbor A reference to a `Neighbor`.
*
* @retval TRUE if @p aNeighbor is a `Router` in the router table.
* @retval FALSE if @p aNeighbor is not a `Router` in the router table
* (i.e. mParent, mParentCandidate, a `Child` of the child table).
* (i.e. it can be the parent or parent candidate, or a `Child` of the child table).
*
*/
bool Contains(const Neighbor &aNeighbor) const
@@ -287,19 +299,21 @@ public:
uint8_t GetNeighborCount(void) const;
/**
* This method indicates whether or not @p aRouterId is allocated.
* This method indicates whether or not a Router ID is allocated.
*
* @retval TRUE if @p aRouterId is allocated.
* @param[in] aRouterId The Router ID.
*
* @retval TRUE if @p aRouterId is allocated.
* @retval FALSE if @p aRouterId is not allocated.
*
*/
bool IsAllocated(uint8_t aRouterId) const;
/**
* This method updates the Router ID allocation.
* This method updates the Router ID allocation set.
*
* @param[in] aRouterIdSequence The Router Id Sequence.
* @param[in] aRouterIdSet A reference to the Router Id Set.
* @param[in] aRouterIdSequence The Router ID Sequence.
* @param[in] aRouterIdSet The Router ID Set.
*
*/
void UpdateRouterIdSet(uint8_t aRouterIdSequence, const Mle::RouterIdSet &aRouterIdSet);
@@ -365,6 +379,9 @@ private:
Router * GetFirstEntry(void) { return AsNonConst(AsConst(this)->GetFirstEntry()); }
Router * GetNextEntry(Router *aRouter) { return AsNonConst(AsConst(this)->GetNextEntry(aRouter)); }
Router * FindNeighbor(uint16_t aRloc16);
Router * FindNeighbor(const Mac::ExtAddress &aExtAddress);
Router * FindNeighbor(const Mac::Address &aMacAddress);
const Router *FindRouter(const Router::AddressMatcher &aMatcher) const;
Router * FindRouter(const Router::AddressMatcher &aMatcher)
{