Skip to content

Commit

Permalink
[mesh-forwarder] introduce FinalizeAndRemoveMessage() (openthread#1…
Browse files Browse the repository at this point in the history
…0830)

This commit adds a new helper method, `FinalizeAndRemoveMessage()`,
which finalizes all direct and indirect transmissions of a message
before removing it from the send queue. This helper is used by
`EvictMessage()` and `RemoveDataResponseMessages()`, simplifying
the code.
  • Loading branch information
abtink authored Oct 14, 2024
1 parent ef8f170 commit 7ec2c31
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
38 changes: 12 additions & 26 deletions src/core/thread/mesh_forwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,32 +184,6 @@ void MeshForwarder::PrepareEmptyFrame(Mac::TxFrame &aFrame, const Mac::Address &
aFrame.SetPayloadLength(0);
}

void MeshForwarder::EvictMessage(Message &aMessage)
{
PriorityQueue *queue = aMessage.GetPriorityQueue();

OT_ASSERT(queue != nullptr);

LogMessage(kMessageEvict, aMessage, kErrorNoBufs);

if (queue == &mSendQueue)
{
#if OPENTHREAD_FTD
for (Child &child : Get<ChildTable>().Iterate(Child::kInStateAnyExceptInvalid))
{
IgnoreError(mIndirectSender.RemoveMessageFromSleepyChild(aMessage, child));
}
#endif

FinalizeMessageDirectTx(aMessage, kErrorNoBufs);
RemoveMessageIfNoPendingTx(aMessage);
}
else
{
queue->DequeueAndFree(aMessage);
}
}

void MeshForwarder::ResumeMessageTransmissions(void)
{
if (mTxPaused)
Expand Down Expand Up @@ -1361,6 +1335,18 @@ void MeshForwarder::FinalizeMessageDirectTx(Message &aMessage, Error aError)
return;
}

void MeshForwarder::FinalizeAndRemoveMessage(Message &aMessage, Error aError, MessageAction aAction)
{
LogMessage(aAction, aMessage, aError);

#if OPENTHREAD_FTD
FinalizeMessageIndirectTxs(aMessage);
#endif

FinalizeMessageDirectTx(aMessage, aError);
RemoveMessageIfNoPendingTx(aMessage);
}

bool MeshForwarder::RemoveMessageIfNoPendingTx(Message &aMessage)
{
bool didRemove = false;
Expand Down
4 changes: 3 additions & 1 deletion src/core/thread/mesh_forwarder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ class MeshForwarder : public InstanceLocator, private NonCopyable
Message::Priority aPriority);
Error HandleDatagram(Message &aMessage, const Mac::Address &aMacSource);
void ClearReassemblyList(void);
void EvictMessage(Message &aMessage);
void HandleDiscoverComplete(void);

void HandleReceivedFrame(Mac::RxFrame &aFrame);
Expand All @@ -543,6 +542,7 @@ class MeshForwarder : public InstanceLocator, private NonCopyable
void HandleSentFrame(Mac::TxFrame &aFrame, Error aError);
void UpdateSendMessage(Error aFrameTxError, Mac::Address &aMacDest, Neighbor *aNeighbor);
void FinalizeMessageDirectTx(Message &aMessage, Error aError);
void FinalizeAndRemoveMessage(Message &aMessage, Error aError, MessageAction aAction);
bool RemoveMessageIfNoPendingTx(Message &aMessage);

void HandleTimeTick(void);
Expand All @@ -551,13 +551,15 @@ class MeshForwarder : public InstanceLocator, private NonCopyable
Error GetFramePriority(RxInfo &aRxInfo, Message::Priority &aPriority);

#if OPENTHREAD_FTD
void FinalizeMessageIndirectTxs(Message &aMessage);
FwdFrameInfo *FindFwdFrameInfoEntry(uint16_t aSrcRloc16, uint16_t aDatagramTag);
bool UpdateFwdFrameInfoArrayOnTimeTick(void);

Error GetFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader,
uint16_t aSrcRloc16,
Message::Priority &aPriority);
void GetForwardFramePriority(RxInfo &aRxInfo, Message::Priority &aPriority);

#endif

void PauseMessageTransmissions(void) { mTxPaused = true; }
Expand Down
34 changes: 16 additions & 18 deletions src/core/thread/mesh_forwarder_ftd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ Error MeshForwarder::EvictMessage(Message::Priority aPriority)
exit:
if ((error == kErrorNone) && (evict != nullptr))
{
EvictMessage(*evict);
FinalizeAndRemoveMessage(*evict, kErrorNoBufs, kMessageEvict);
}

return error;
Expand Down Expand Up @@ -299,30 +299,28 @@ void MeshForwarder::RemoveMessagesForChild(Child &aChild, MessageChecker &aMessa
}
}

void MeshForwarder::RemoveDataResponseMessages(void)
void MeshForwarder::FinalizeMessageIndirectTxs(Message &aMessage)
{
Ip6::Header ip6Header;
VerifyOrExit(!aMessage.GetIndirectTxChildMask().IsEmpty());

for (Message &message : mSendQueue)
for (Child &child : Get<ChildTable>().Iterate(Child::kInStateAnyExceptInvalid))
{
if (!message.IsMleCommand(Mle::kCommandDataResponse))
{
continue;
}
IgnoreError(mIndirectSender.RemoveMessageFromSleepyChild(aMessage, child));
VerifyOrExit(!aMessage.GetIndirectTxChildMask().IsEmpty());
}

IgnoreError(message.Read(0, ip6Header));
exit:
return;
}

if (!(ip6Header.GetDestination().IsMulticast()))
void MeshForwarder::RemoveDataResponseMessages(void)
{
for (Message &message : mSendQueue)
{
if (message.IsMleCommand(Mle::kCommandDataResponse))
{
for (Child &child : Get<ChildTable>().Iterate(Child::kInStateAnyExceptInvalid))
{
IgnoreError(mIndirectSender.RemoveMessageFromSleepyChild(message, child));
}
FinalizeAndRemoveMessage(message, kErrorDrop, kMessageDrop);
}

LogMessage(kMessageDrop, message);
FinalizeMessageDirectTx(message, kErrorDrop);
RemoveMessageIfNoPendingTx(message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/mesh_forwarder_mtd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Error MeshForwarder::EvictMessage(Message::Priority aPriority)

if (message->GetPriority() < static_cast<uint8_t>(aPriority))
{
EvictMessage(*message);
FinalizeAndRemoveMessage(*message, kErrorNoBufs, kMessageEvict);
ExitNow(error = kErrorNone);
}

Expand Down

0 comments on commit 7ec2c31

Please sign in to comment.