Skip to content

Commit

Permalink
Fix build, add more tests for the PASE sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed May 27, 2022
1 parent 51729ba commit 95e2104
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 30 deletions.
28 changes: 16 additions & 12 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Protocols::InteractionModel::Status InteractionModelEngine::OnReadInitialRequest
reader.Init(aPayload.Retain());

ReadRequestMessage::Parser readRequestParser;
ReturnErrorOnFailure(readRequestParser.Init(reader));
VerifyOrReturnError(readRequestParser.Init(reader) == CHIP_NO_ERROR, Protocols::InteractionModel::Status::Failure);

{
size_t requestedAttributePathCount = 0;
Expand All @@ -405,8 +405,7 @@ Protocols::InteractionModel::Status InteractionModelEngine::OnReadInitialRequest
}
else if (err != CHIP_ERROR_END_OF_TLV)
{
aStatus = Protocols::InteractionModel::Status::InvalidAction;
return CHIP_NO_ERROR;
return Protocols::InteractionModel::Status::InvalidAction;
}
EventPathIBs::Parser eventpathListParser;
err = readRequestParser.GetEventRequests(&eventpathListParser);
Expand All @@ -418,17 +417,15 @@ Protocols::InteractionModel::Status InteractionModelEngine::OnReadInitialRequest
}
else if (err != CHIP_ERROR_END_OF_TLV)
{
aStatus = Protocols::InteractionModel::Status::InvalidAction;
return CHIP_NO_ERROR;
return Protocols::InteractionModel::Status::InvalidAction;
}

// The following cast is safe, since we can only hold a few tens of paths in one request.
Protocols::InteractionModel::Status checkResult = EnsureResourceForRead(
apExchangeContext->GetSessionHandle()->GetFabricIndex(), requestedAttributePathCount, requestedEventPathCount);
if (checkResult != Protocols::InteractionModel::Status::Success)
{
aStatus = checkResult;
return CHIP_NO_ERROR;
return checkResult;
}
}
}
Expand Down Expand Up @@ -836,9 +833,11 @@ bool InteractionModelEngine::TrimFabricForRead(FabricIndex aFabricIndex)
});

if (candidate != nullptr &&
(attributePathsUsedByCurrentFabric > minSupportedPathsPerFabricForRead ||
eventPathsUsedByCurrentFabric > minSupportedPathsPerFabricForRead ||
readTransactionsOnCurrentFabric > kMinSupportedReadRequestsPerFabric))
((attributePathsUsedByCurrentFabric > minSupportedPathsPerFabricForRead ||
eventPathsUsedByCurrentFabric > minSupportedPathsPerFabricForRead ||
readTransactionsOnCurrentFabric > kMinSupportedReadRequestsPerFabric) ||
// Always evict the transactions on PASE sessions if the fabric table is full.
(aFabricIndex == kUndefinedFabricIndex && mpFabricTable->FabricCount() == GetConfigMaxFabrics())))
{
candidate->Abort();
return true;
Expand Down Expand Up @@ -903,9 +902,9 @@ Protocols::InteractionModel::Status InteractionModelEngine::EnsureResourceForRea
return Protocols::InteractionModel::Status::PathsExhausted;
}

// If we have commissioned CHIP_CONFIG_MAX_FABRIC already, and this transaction don't have an associated fabric index, reject
// If we have commissioned CHIP_CONFIG_MAX_FABRICS already, and this transaction don't have an associated fabric index, reject
// the request if we don't have sufficient resources for this request.
if (mpFabricTable->FabricCount() == CHIP_CONFIG_MAX_FABRICS && aFabricIndex == kUndefinedFabricIndex)
if (mpFabricTable->FabricCount() == GetConfigMaxFabrics() && aFabricIndex == kUndefinedFabricIndex)
{
return Protocols::InteractionModel::Status::ResourceExhausted;
}
Expand Down Expand Up @@ -956,6 +955,11 @@ Protocols::InteractionModel::Status InteractionModelEngine::EnsureResourceForRea
while (didEvictHandler)
{
didEvictHandler = false;
didEvictHandler = didEvictHandler || evictAndUpdateResourceUsage(kUndefinedFabricIndex);
if (didEvictHandler)
{
continue;
}
for (const auto & fabric : *mpFabricTable)
{
// The resources are enough to serve this request, do not evict anything.
Expand Down
24 changes: 18 additions & 6 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,17 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
//
auto & GetReadHandlerPool() { return mReadHandlers; }

//
// Override the maximal capacity of the fabric table only for interaction model engine
//
// If -1 is passed in, no override is instituted and default behavior resumes.
//
void SetConfigMaxFabrics(int32_t sz) { mMaxNumFabricsOverride = sz; }

//
// Override the maximal capacity of the underlying read handler pool to mimic
// out of memory scenarios in unit-tests.
//
// This function did not considered the resources reserved for read handlers,
// SetHandlerCapacityForSubscriptions if there are subscriptions in the tests.
//
// If -1 is passed in, no override is instituted and default behavior resumes.
//
void SetHandlerCapacityForReads(int32_t sz) { mReadHandlerCapacityForReadsOverride = sz; }
Expand All @@ -284,9 +288,6 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
// Override the maximal capacity of the underlying attribute path pool and event path pool to mimic
// out of paths exhausted scenarios in unit-tests.
//
// This function did not considered the resources reserved for read handlers,
// SetPathPoolCapacityForSubscriptions if there are subscriptions in the tests.
//
// If -1 is passed in, no override is instituted and default behavior resumes.
//
void SetPathPoolCapacityForReads(int32_t sz) { mPathPoolCapacityForReadsOverride = sz; }
Expand Down Expand Up @@ -431,6 +432,15 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
#endif
}

inline uint8_t GetConfigMaxFabrics() const
{
#if CONFIG_IM_BUILD_FOR_UNIT_TEST
return (mMaxNumFabricsOverride == -1) ? CHIP_CONFIG_MAX_FABRICS : static_cast<uint8_t>(mMaxNumFabricsOverride);
#else
return CHIP_CONFIG_MAX_FABRICS;
#endif
}

/**
* Verify and ensure (by killing oldest read handlers that make the resources used by the current fabric exceed the fabric
* quota)
Expand Down Expand Up @@ -520,6 +530,8 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
int mReadHandlerCapacityForReadsOverride = -1;
int mPathPoolCapacityForReadsOverride = -1;

int mMaxNumFabricsOverride = -1;

// We won't limit the handler used per fabric on platforms that are using heap for memory pools, so we introduces a flag to
// enforce such check based on the configured size. This flag is used for unit tests only, there is another compare time flag
// CHIP_CONFIG_IM_FORCE_FABRIC_QUOTA_CHECK for stress tests.
Expand Down
Loading

0 comments on commit 95e2104

Please sign in to comment.