[router-table] move FillRouteTlv() to RouterTable (#8437)

This commit moves `FillRouteTlv()` method to `RouterTable` class.
This commit is contained in:
Abtin Keshavarzian
2022-11-23 13:08:32 -08:00
committed by Jonathan Hui
parent 43a6430083
commit d2e1c1e14d
6 changed files with 115 additions and 112 deletions
+1 -1
View File
@@ -4937,7 +4937,7 @@ Error Mle::TxMessage::AppendRouteTlv(Neighbor *aNeighbor)
RouteTlv tlv;
tlv.Init();
Get<MleRouter>().FillRouteTlv(tlv, aNeighbor);
Get<RouterTable>().FillRouteTlv(tlv, aNeighbor);
return tlv.AppendTo(*this);
}
-101
View File
@@ -4151,107 +4151,6 @@ void MleRouter::FillConnectivityTlv(ConnectivityTlv &aTlv)
aTlv.SetSedDatagramCount(OPENTHREAD_CONFIG_DEFAULT_SED_DATAGRAM_COUNT);
}
void MleRouter::FillRouteTlv(RouteTlv &aTlv, Neighbor *aNeighbor)
{
uint8_t routerIdSequence = mRouterTable.GetRouterIdSequence();
RouterIdSet routerIdSet = mRouterTable.GetRouterIdSet();
uint8_t routerCount;
if (aNeighbor && IsActiveRouter(aNeighbor->GetRloc16()))
{
// Sending a Link Accept message that may require truncation
// of Route64 TLV
routerCount = mRouterTable.GetActiveRouterCount();
if (routerCount > kLinkAcceptMaxRouters)
{
for (uint8_t routerId = 0; routerId <= kMaxRouterId; routerId++)
{
if (routerCount <= kLinkAcceptMaxRouters)
{
break;
}
if ((routerId == RouterIdFromRloc16(GetRloc16())) || (routerId == aNeighbor->GetRouterId()) ||
(routerId == GetLeaderId()))
{
// Route64 TLV must contain this device and the
// neighboring router to ensure that at least this
// link can be established.
continue;
}
if (routerIdSet.Contains(routerId))
{
routerIdSet.Remove(routerId);
routerCount--;
}
}
// Ensure that the neighbor will process the current
// Route64 TLV in a subsequent message exchange
routerIdSequence -= kLinkAcceptSequenceRollback;
}
}
aTlv.SetRouterIdSequence(routerIdSequence);
aTlv.SetRouterIdMask(routerIdSet);
routerCount = 0;
for (Router &router : Get<RouterTable>().Iterate())
{
if (!routerIdSet.Contains(router.GetRouterId()))
{
continue;
}
if (router.GetRloc16() == GetRloc16())
{
aTlv.SetLinkQualityIn(routerCount, kLinkQuality0);
aTlv.SetLinkQualityOut(routerCount, kLinkQuality0);
aTlv.SetRouteCost(routerCount, 1);
}
else
{
Router *nextHop;
uint8_t linkCost;
uint8_t routeCost;
linkCost = mRouterTable.GetLinkCost(router);
nextHop = mRouterTable.FindNextHopOf(router);
if (nextHop == nullptr)
{
routeCost = linkCost;
}
else
{
routeCost = router.GetCost() + mRouterTable.GetLinkCost(*nextHop);
if (linkCost < routeCost)
{
routeCost = linkCost;
}
}
if (routeCost >= kMaxRouteCost)
{
routeCost = 0;
}
aTlv.SetRouteCost(routerCount, routeCost);
aTlv.SetLinkQualityOut(routerCount, router.GetLinkQualityOut());
aTlv.SetLinkQualityIn(routerCount, router.GetLinkQualityIn());
}
routerCount++;
}
aTlv.SetRouteDataLength(routerCount);
}
bool MleRouter::HasMinDowngradeNeighborRouters(void)
{
uint8_t routerCount = 0;
-8
View File
@@ -419,14 +419,6 @@ public:
*/
void FillConnectivityTlv(ConnectivityTlv &aTlv);
/**
* This method fills an RouteTlv.
*
* @param[out] aTlv A reference to the tlv to be filled.
*
*/
void FillRouteTlv(RouteTlv &aTlv, Neighbor *aNeighbor = nullptr);
/**
* This method generates an MLE Child Update Request message to be sent to the parent.
*
+1 -1
View File
@@ -343,7 +343,7 @@ Error NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest,
RouteTlv tlv;
tlv.Init();
Get<Mle::MleRouter>().FillRouteTlv(tlv);
Get<RouterTable>().FillRouteTlv(tlv);
SuccessOrExit(error = tlv.AppendTo(aResponse));
break;
}
+99 -1
View File
@@ -483,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 = FindRouterForId(routerId);
Router *router = FindRouterById(routerId);
OT_ASSERT(router != nullptr);
router->SetNextHop(Mle::kInvalidRouterId);
@@ -501,6 +501,104 @@ exit:
return;
}
void RouterTable::FillRouteTlv(Mle::RouteTlv &aRouteTlv, const Neighbor *aNeighbor) const
{
uint8_t routerIdSequence = mRouterIdSequence;
Mle::RouterIdSet routerIdSet = mAllocatedRouterIds;
uint8_t routerCount;
if ((aNeighbor != nullptr) && Mle::IsActiveRouter(aNeighbor->GetRloc16()))
{
// Sending a Link Accept message that may require truncation
// of Route64 TLV.
routerCount = mActiveRouterCount;
if (routerCount > Mle::kLinkAcceptMaxRouters)
{
for (uint8_t routerId = 0; routerId <= Mle::kMaxRouterId; routerId++)
{
if (routerCount <= Mle::kLinkAcceptMaxRouters)
{
break;
}
if ((routerId == Mle::RouterIdFromRloc16(Get<Mle::Mle>().GetRloc16())) ||
(routerId == aNeighbor->GetRouterId()) || (routerId == Get<Mle::Mle>().GetLeaderId()))
{
// Route64 TLV must contain this device and the
// neighboring router to ensure that at least this
// link can be established.
continue;
}
if (routerIdSet.Contains(routerId))
{
routerIdSet.Remove(routerId);
routerCount--;
}
}
// Ensure that the neighbor will process the current
// Route64 TLV in a subsequent message exchange
routerIdSequence -= Mle::kLinkAcceptSequenceRollback;
}
}
aRouteTlv.SetRouterIdSequence(routerIdSequence);
aRouteTlv.SetRouterIdMask(routerIdSet);
routerCount = 0;
for (const Router *router = GetFirstEntry(); router != nullptr; router = GetNextEntry(router))
{
if (!routerIdSet.Contains(router->GetRouterId()))
{
continue;
}
if (router->GetRloc16() == Get<Mle::Mle>().GetRloc16())
{
aRouteTlv.SetLinkQualityIn(routerCount, kLinkQuality0);
aRouteTlv.SetLinkQualityOut(routerCount, kLinkQuality0);
aRouteTlv.SetRouteCost(routerCount, 1);
}
else
{
const Router *nextHop = FindNextHopOf(*router);
uint8_t linkCost = GetLinkCost(*router);
uint8_t routeCost;
if (nextHop == nullptr)
{
routeCost = linkCost;
}
else
{
routeCost = router->GetCost() + GetLinkCost(*nextHop);
if (linkCost < routeCost)
{
routeCost = linkCost;
}
}
if (routeCost >= Mle::kMaxRouteCost)
{
routeCost = 0;
}
aRouteTlv.SetRouteCost(routerCount, routeCost);
aRouteTlv.SetLinkQualityOut(routerCount, router->GetLinkQualityOut());
aRouteTlv.SetLinkQualityIn(routerCount, router->GetLinkQualityIn());
}
routerCount++;
}
aRouteTlv.SetRouteDataLength(routerCount);
}
void RouterTable::HandleTimeTick(void)
{
Mle::MleRouter &mle = Get<Mle::MleRouter>();
+14
View File
@@ -39,6 +39,7 @@
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "mac/mac_types.hpp"
#include "thread/mle_tlvs.hpp"
#include "thread/mle_types.hpp"
#include "thread/thread_tlvs.hpp"
#include "thread/topology.hpp"
@@ -326,6 +327,19 @@ public:
*/
const Mle::RouterIdSet &GetRouterIdSet(void) const { return mAllocatedRouterIds; }
/**
* This method fills a Route TLV.
*
* When @p aNeighbor is not `nullptr`, we limit the number of router entries to `Mle::kLinkAcceptMaxRouters` when
* populating `aRouteTlv`, so that the TLV can be appended in a Link Accept message. In this case, we ensure to
* include router entries associated with @p aNeighbor, leader, and this device itself.
*
* @param[out] aRouteTlv A Route TLV to be filled.
* @param[in] aNeighbor A pointer to the receiver (in case TLV is for a Link Accept message).
*
*/
void FillRouteTlv(Mle::RouteTlv &aRouteTlv, const Neighbor *aNeighbor = nullptr) const;
/**
* This method updates the router table and must be called with a one second period.
*