[netdata] simplify checking if network data contains certain entries (#7309)

This commit adds new methods in `NetworkData` to indicate whether or
not it contains a given on mesh prefix, external route, or service
entry. It also adds a new method to check whether it contains all
entries as in another given Network Data all associated with a given
RLOC16. This is then used to simplify comparison between `Local` and
`Leader` network data.
This commit is contained in:
Abtin Keshavarzian
2022-01-14 12:40:59 -08:00
committed by GitHub
parent 7a1c843ffd
commit 612d235cd1
4 changed files with 98 additions and 117 deletions
+60 -66
View File
@@ -284,112 +284,106 @@ exit:
return error; return error;
} }
bool NetworkData::ContainsOnMeshPrefixes(const NetworkData &aCompare, uint16_t aRloc16) const bool NetworkData::ContainsOnMeshPrefix(const OnMeshPrefixConfig &aPrefix) const
{ {
Iterator outerIterator = kIteratorInit; bool contains = false;
OnMeshPrefixConfig outerConfig; Iterator iterator = kIteratorInit;
bool rval = true; OnMeshPrefixConfig prefix;
while (aCompare.GetNextOnMeshPrefix(outerIterator, aRloc16, outerConfig) == kErrorNone) while (GetNextOnMeshPrefix(iterator, aPrefix.mRloc16, prefix) == kErrorNone)
{ {
Iterator innerIterator = kIteratorInit; if (prefix == aPrefix)
OnMeshPrefixConfig innerConfig;
Error error;
while ((error = GetNextOnMeshPrefix(innerIterator, aRloc16, innerConfig)) == kErrorNone)
{ {
if (outerConfig == innerConfig) contains = true;
{ break;
break;
}
}
if (error != kErrorNone)
{
ExitNow(rval = false);
} }
} }
exit: return contains;
return rval;
} }
bool NetworkData::ContainsExternalRoutes(const NetworkData &aCompare, uint16_t aRloc16) const bool NetworkData::ContainsExternalRoute(const ExternalRouteConfig &aRoute) const
{ {
Iterator outerIterator = kIteratorInit; bool contains = false;
ExternalRouteConfig outerConfig; Iterator iterator = kIteratorInit;
bool rval = true; ExternalRouteConfig route;
while (aCompare.GetNextExternalRoute(outerIterator, aRloc16, outerConfig) == kErrorNone) while (GetNextExternalRoute(iterator, aRoute.mRloc16, route) == kErrorNone)
{ {
Iterator innerIterator = kIteratorInit; if (route == aRoute)
ExternalRouteConfig innerConfig;
Error error;
while ((error = GetNextExternalRoute(innerIterator, aRloc16, innerConfig)) == kErrorNone)
{ {
if (outerConfig == innerConfig) contains = true;
{ break;
break;
}
}
if (error != kErrorNone)
{
ExitNow(rval = false);
} }
} }
exit: return contains;
return rval;
} }
bool NetworkData::ContainsServices(const NetworkData &aCompare, uint16_t aRloc16) const bool NetworkData::ContainsService(const ServiceConfig &aService) const
{ {
Iterator outerIterator = kIteratorInit; bool contains = false;
ServiceConfig outerConfig; Iterator iterator = kIteratorInit;
bool rval = true; ServiceConfig service;
while (aCompare.GetNextService(outerIterator, aRloc16, outerConfig) == kErrorNone) while (GetNextService(iterator, aService.GetServerConfig().mRloc16, service) == kErrorNone)
{ {
Iterator innerIterator = kIteratorInit; if (service == aService)
ServiceConfig innerConfig;
Error error;
while ((error = GetNextService(innerIterator, aRloc16, innerConfig)) == kErrorNone)
{ {
if (outerConfig == innerConfig) contains = true;
{ break;
break;
}
}
if (error != kErrorNone)
{
ExitNow(rval = false);
} }
} }
exit: return contains;
return rval;
} }
bool NetworkData::ContainsService(uint8_t aServiceId, uint16_t aRloc16) const bool NetworkData::ContainsService(uint8_t aServiceId, uint16_t aRloc16) const
{ {
bool contains = false;
Iterator iterator = kIteratorInit; Iterator iterator = kIteratorInit;
uint8_t serviceId; uint8_t serviceId;
bool rval = false;
while (GetNextServiceId(iterator, aRloc16, serviceId) == kErrorNone) while (GetNextServiceId(iterator, aRloc16, serviceId) == kErrorNone)
{ {
if (serviceId == aServiceId) if (serviceId == aServiceId)
{ {
ExitNow(rval = true); contains = true;
break;
}
}
return contains;
}
bool NetworkData::ContainsEntriesFrom(const NetworkData &aComapre, uint16_t aRloc16) const
{
bool contains = true;
Iterator iterator = kIteratorInit;
while (true)
{
Config config;
OnMeshPrefixConfig prefix;
ExternalRouteConfig route;
ServiceConfig service;
config.mOnMeshPrefix = &prefix;
config.mExternalRoute = &route;
config.mService = &service;
SuccessOrExit(aComapre.Iterate(iterator, aRloc16, config));
if (((config.mOnMeshPrefix != nullptr) && !ContainsOnMeshPrefix(*config.mOnMeshPrefix)) ||
((config.mExternalRoute != nullptr) && !ContainsExternalRoute(*config.mExternalRoute)) ||
((config.mService != nullptr) && !ContainsService(*config.mService)))
{
ExitNow(contains = false);
} }
} }
exit: exit:
return rval; return contains;
} }
void MutableNetworkData::RemoveTemporaryData(void) void MutableNetworkData::RemoveTemporaryData(void)
+31 -24
View File
@@ -280,57 +280,64 @@ public:
Error GetNextServiceId(Iterator &aIterator, uint16_t aRloc16, uint8_t &aServiceId) const; Error GetNextServiceId(Iterator &aIterator, uint16_t aRloc16, uint8_t &aServiceId) const;
/** /**
* This method indicates whether or not the Thread Network Data contains all of the on mesh prefix information * This method indicates whether or not the Thread Network Data contains a given on mesh prefix entry.
* in @p aCompare associated with @p aRloc16.
* *
* @param[in] aCompare The Network Data to use for the query. * @param[in] aPrefix The on mesh prefix config to check.
* @param[in] aRloc16 The RLOC16 to consider.
* *
* @returns TRUE if this object contains all on mesh prefix information in @p aCompare associated with @p aRloc16, * @retval TRUE if Network Data contains an on mesh prefix matching @p aPrefix.
* FALSE otherwise. * @retval FALSE if Network Data does not contain an on mesh prefix matching @p aPrefix.
* *
*/ */
bool ContainsOnMeshPrefixes(const NetworkData &aCompare, uint16_t aRloc16) const; bool ContainsOnMeshPrefix(const OnMeshPrefixConfig &aPrefix) const;
/** /**
* This method indicates whether or not the Thread Network Data contains all of the external route information * This method indicates whether or not the Thread Network Data contains a given external route entry.
* in @p aCompare associated with @p aRloc16.
* *
* @param[in] aCompare The Network Data to use for the query. * @param[in] aRoute The external route config to check.
* @param[in] aRloc16 The RLOC16 to consider.
* *
* @returns TRUE if this object contains all external route information in @p aCompare associated with @p aRloc16, * @retval TRUE if Network Data contains an external route matching @p aRoute.
* FALSE otherwise. * @retval FALSE if Network Data does not contain an external route matching @p aRoute.
* *
*/ */
bool ContainsExternalRoutes(const NetworkData &aCompare, uint16_t aRloc16) const; bool ContainsExternalRoute(const ExternalRouteConfig &aRoute) const;
/** /**
* This method indicates whether or not the Thread Network Data contains all of the service information * This method indicates whether or not the Thread Network Data contains a given service entry.
* in @p aCompare associated with @p aRloc16.
* *
* @param[in] aCompare The Network Data to use for the query. * @param[in] aService The service config to check.
* @param[in] aRloc16 The RLOC16 to consider.
* *
* @returns TRUE if this object contains all service information in @p aCompare associated with @p aRloc16, * @retval TRUE if Network Data contains a service matching @p aService.
* FALSE otherwise. * @retval FALSE if Network Data does not contain a service matching @p aService.
* *
*/ */
bool ContainsServices(const NetworkData &aCompare, uint16_t aRloc16) const; bool ContainsService(const ServiceConfig &aService) const;
/** /**
* This method indicates whether or not the Thread Network Data contains the service with given Service ID * This method indicates whether or not the Thread Network Data contains the service with a given Service ID
* associated with @p aRloc16. * associated with @p aRloc16.
* *
* @param[in] aServiceId The Service ID to search for. * @param[in] aServiceId The Service ID to search for.
* @param[in] aRloc16 The RLOC16 to consider. * @param[in] aRloc16 The RLOC16 to consider.
* *
* @returns TRUE if this object contains the service with given ID associated with @p aRloc16, * @retval TRUE if Network Data contains a service matching @p aServiceId for @p aRloc16.
* FALSE otherwise. * @retval FALSE if Network Data does not contain a service matching @p aServiceId for @p aRloc16.
* *
*/ */
bool ContainsService(uint8_t aServiceId, uint16_t aRloc16) const; bool ContainsService(uint8_t aServiceId, uint16_t aRloc16) const;
/**
* This method indicates whether or not the Thread Network Data contains all the on mesh prefixes, external
* routes, and service entries as in another given Network Data associated with a given RLOC16.
*
* @param[in] aCompare The Network Data to compare with.
* @param[in] aRloc16 The RLOC16 to consider.
*
* @retval TRUE if Network Data contains all the same entries as in @p aCompare for @p aRloc16.
* @retval FALSE if Network Data does not contains all the same entries as in @p aCompare for @p aRloc16.
*
*/
bool ContainsEntriesFrom(const NetworkData &aComapre, uint16_t aRloc16) const;
/** /**
* This method provides the next server RLOC16 in the Thread Network Data. * This method provides the next server RLOC16 in the Thread Network Data.
* *
+6 -24
View File
@@ -168,16 +168,10 @@ void Local::UpdateRloc(PrefixTlv &aPrefixTlv)
} }
} }
bool Local::IsOnMeshPrefixConsistent(void) const bool Local::IsConsistent(void) const
{ {
return (Get<Leader>().ContainsOnMeshPrefixes(*this, Get<Mle::MleRouter>().GetRloc16()) && return Get<Leader>().ContainsEntriesFrom(*this, Get<Mle::MleRouter>().GetRloc16()) &&
ContainsOnMeshPrefixes(Get<Leader>(), Get<Mle::MleRouter>().GetRloc16())); ContainsEntriesFrom(Get<Leader>(), Get<Mle::MleRouter>().GetRloc16());
}
bool Local::IsExternalRouteConsistent(void) const
{
return (Get<Leader>().ContainsExternalRoutes(*this, Get<Mle::MleRouter>().GetRloc16()) &&
ContainsExternalRoutes(Get<Leader>(), Get<Mle::MleRouter>().GetRloc16()));
} }
#endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
@@ -255,12 +249,6 @@ void Local::UpdateRloc(ServiceTlv &aService)
} }
} }
bool Local::IsServiceConsistent(void) const
{
return (Get<Leader>().ContainsServices(*this, Get<Mle::MleRouter>().GetRloc16()) &&
ContainsServices(Get<Leader>(), Get<Mle::MleRouter>().GetRloc16()));
}
#endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
void Local::UpdateRloc(void) void Local::UpdateRloc(void)
@@ -291,9 +279,8 @@ void Local::UpdateRloc(void)
Error Local::UpdateInconsistentServerData(Coap::ResponseHandler aHandler, void *aContext) Error Local::UpdateInconsistentServerData(Coap::ResponseHandler aHandler, void *aContext)
{ {
Error error = kErrorNone; Error error = kErrorNone;
uint16_t rloc = Get<Mle::MleRouter>().GetRloc16(); uint16_t rloc = Get<Mle::MleRouter>().GetRloc16();
bool isConsistent = true;
#if OPENTHREAD_FTD #if OPENTHREAD_FTD
// Don't send this Server Data Notification if the device is going to upgrade to Router // Don't send this Server Data Notification if the device is going to upgrade to Router
@@ -306,13 +293,8 @@ Error Local::UpdateInconsistentServerData(Coap::ResponseHandler aHandler, void *
UpdateRloc(); UpdateRloc();
#if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
isConsistent = isConsistent && IsOnMeshPrefixConsistent() && IsExternalRouteConsistent(); VerifyOrExit(!IsConsistent(), error = kErrorNotFound);
#endif #endif
#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
isConsistent = isConsistent && IsServiceConsistent();
#endif
VerifyOrExit(!isConsistent, error = kErrorNotFound);
if (mOldRloc == rloc) if (mOldRloc == rloc)
{ {
+1 -3
View File
@@ -172,13 +172,11 @@ private:
Error AddPrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType, uint16_t aFlags, bool aStable); Error AddPrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType, uint16_t aFlags, bool aStable);
Error RemovePrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType); Error RemovePrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType);
void UpdateRloc(PrefixTlv &aPrefixTlv); void UpdateRloc(PrefixTlv &aPrefixTlv);
bool IsOnMeshPrefixConsistent(void) const; bool IsConsistent(void) const;
bool IsExternalRouteConsistent(void) const;
#endif #endif
#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
void UpdateRloc(ServiceTlv &aService); void UpdateRloc(ServiceTlv &aService);
bool IsServiceConsistent(void) const;
#endif #endif
uint8_t mTlvBuffer[kMaxSize]; uint8_t mTlvBuffer[kMaxSize];