Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send piggybacked acks when they are not needed. #7958

Merged
merged 1 commit into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, Secu

SetDropAckDebug(false);
SetAckPending(false);
SetPeerRequestedAck(false);
SetMsgRcvdFromPeer(false);
SetAutoRequestAck(true);

Expand Down
15 changes: 3 additions & 12 deletions src/messaging/ExchangeMessageDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,14 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(SecureSessionHandle session, uin
payloadHeader.SetExchangeID(exchangeId).SetMessageType(protocol, type).SetInitiator(isInitiator);

// If there is a pending acknowledgment piggyback it on this message.
if (reliableMessageContext->HasPeerRequestedAck())
if (reliableMessageContext->IsAckPending())
{
payloadHeader.SetAckId(reliableMessageContext->GetPendingPeerAckId());

// Set AckPending flag to false since current outgoing message is going to serve as the ack on this exchange.
reliableMessageContext->SetAckPending(false);
payloadHeader.SetAckId(reliableMessageContext->TakePendingPeerAckId());

#if !defined(NDEBUG)
if (!payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::StandaloneAck))
{
ChipLogDetail(ExchangeManager, "Piggybacking Ack for MsgId:%08" PRIX32 " with msg",
reliableMessageContext->GetPendingPeerAckId());
ChipLogDetail(ExchangeManager, "Piggybacking Ack for MsgId:%08" PRIX32 " with msg", payloadHeader.GetAckId().Value());
}
#endif
}
Expand Down Expand Up @@ -115,11 +111,6 @@ CHIP_ERROR ExchangeMessageDispatch::OnMessageReceived(const PayloadHeader & payl
if (payloadHeader.NeedsAck())
{
// An acknowledgment needs to be sent back to the peer for this message on this exchange,
// Set the flag in message header indicating an ack requested by peer;
msgFlags.Set(MessageFlagValues::kPeerRequestedAck);

// Also set the flag in the exchange context indicating an ack requested;
reliableMessageContext->SetPeerRequestedAck(true);

ReturnErrorOnFailure(reliableMessageContext->HandleNeedsAck(messageId, msgFlags));
}
Expand Down
10 changes: 4 additions & 6 deletions src/messaging/Flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ enum class MessageFlagValues : uint32_t
kMessageEncoded = 0x00001000,
/**< Indicates that default IPv6 source address selection should be used when sending IPv6 multicast messages. */
kDefaultMulticastSourceAddress = 0x00002000,
/**< Indicates that the sender of the message requested an acknowledgment. */
kPeerRequestedAck = 0x00004000,
/**< Indicates that the message is a duplicate of a previously received message. */
kDuplicateMessage = 0x00008000,
kDuplicateMessage = 0x00004000,
/**< Indicates that the peer's group key message counter is not synchronized. */
kPeerGroupMsgIdNotSynchronized = 0x00010000,
kPeerGroupMsgIdNotSynchronized = 0x00008000,
/**< Indicates that the source of the message is the initiator of the CHIP exchange. */
kFromInitiator = 0x00020000,
kFromInitiator = 0x00010000,
/**< Indicates that message is being sent/received via the local ephemeral UDP port. */
kViaEphemeralUDPPort = 0x00040000,
kViaEphemeralUDPPort = 0x00020000,
};

using MessageFlags = BitFlags<MessageFlagValues>;
Expand Down
27 changes: 11 additions & 16 deletions src/messaging/ReliableMessageContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ bool ReliableMessageContext::IsAckPending() const
return mFlags.Has(Flags::kFlagAckPending);
}

bool ReliableMessageContext::HasPeerRequestedAck() const
{
return mFlags.Has(Flags::kFlagPeerRequestedAck);
}

bool ReliableMessageContext::HasRcvdMsgFromPeer() const
{
return mFlags.Has(Flags::kFlagMsgRcvdFromPeer);
Expand All @@ -87,11 +82,6 @@ void ReliableMessageContext::SetAckPending(bool inAckPending)
mFlags.Set(Flags::kFlagAckPending, inAckPending);
}

void ReliableMessageContext::SetPeerRequestedAck(bool inPeerRequestedAck)
{
mFlags.Set(Flags::kFlagPeerRequestedAck, inPeerRequestedAck);
}

void ReliableMessageContext::SetDropAckDebug(bool inDropAckDebug)
{
mFlags.Set(Flags::kFlagDropAckDebug, inDropAckDebug);
Expand Down Expand Up @@ -200,7 +190,8 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t MessageId, BitFlags<M
// Expire any virtual ticks that have expired so all wakeup sources reflect the current time
GetReliableMessageMgr()->ExpireTicks();

// If the message IS a duplicate.
// If the message IS a duplicate there will never be a response to it, so we
// should not wait for one and just immediately send a standalone ack.
if (MsgFlags.Has(MessageFlagValues::kDuplicateMessage))
{
#if !defined(NDEBUG)
Expand All @@ -213,7 +204,7 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t MessageId, BitFlags<M
uint32_t tempAckId = mPendingPeerAckId;

// Set the pending ack id.
mPendingPeerAckId = MessageId;
SetPendingPeerAckId(MessageId);

// Send the Ack for the duplication message in a SecureChannel::StandaloneAck message.
err = SendStandaloneAckMessage();
Expand All @@ -222,8 +213,7 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t MessageId, BitFlags<M
if (wasAckPending)
{
// Restore previously pending ack id.
mPendingPeerAckId = tempAckId;
SetAckPending(true);
SetPendingPeerAckId(tempAckId);
}

SuccessOrExit(err);
Expand All @@ -243,11 +233,10 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t MessageId, BitFlags<M
}

// Replace the Pending ack id.
mPendingPeerAckId = MessageId;
SetPendingPeerAckId(MessageId);
mNextAckTimeTick =
static_cast<uint16_t>(CHIP_CONFIG_RMP_DEFAULT_ACK_TIMEOUT_TICK +
GetReliableMessageMgr()->GetTickCounterFromTimeDelta(System::Timer::GetCurrentEpoch()));
SetAckPending(true);
}

exit:
Expand Down Expand Up @@ -286,5 +275,11 @@ CHIP_ERROR ReliableMessageContext::SendStandaloneAckMessage()
return err;
}

void ReliableMessageContext::SetPendingPeerAckId(uint32_t aPeerAckId)
{
mPendingPeerAckId = aPeerAckId;
SetAckPending(true);
}

} // namespace Messaging
} // namespace chip
58 changes: 24 additions & 34 deletions src/messaging/ReliableMessageContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ class ReliableMessageContext
*/
CHIP_ERROR FlushAcks();

uint32_t GetPendingPeerAckId() { return mPendingPeerAckId; }
/**
* Take the pending peer ack id from the context. This must only be called
* when IsAckPending() is true. After this call, IsAckPending() will be
* false; it's the caller's responsibility to send the ack.
*/
uint32_t TakePendingPeerAckId()
{
SetAckPending(false);
return mPendingPeerAckId;
}

/**
* Get the initial retransmission interval. It would be the time to wait before
Expand Down Expand Up @@ -131,33 +140,6 @@ class ReliableMessageContext
*/
bool IsAckPending() const;

/**
* Set if an acknowledgment needs to be sent back to the peer on this exchange.
*
* @param[in] inAckPending A Boolean indicating whether (true) or not
* (false) an acknowledgment should be sent back
* in response to a received message.
*/
void SetAckPending(bool inAckPending);

/**
* Determine whether peer requested acknowledgment for at least one message
* on this exchange.
*
* @return Returns 'true' if acknowledgment requested, else 'false'.
*/
bool HasPeerRequestedAck() const;

/**
* Set if an acknowledgment was requested in the last message received
* on this exchange.
*
* @param[in] inPeerRequestedAck A Boolean indicating whether (true) or not
* (false) an acknowledgment was requested
* in the last received message.
*/
void SetPeerRequestedAck(bool inPeerRequestedAck);

/**
* Determine whether at least one message has been received
* on this exchange from peer.
Expand Down Expand Up @@ -218,13 +200,8 @@ class ReliableMessageContext
/// When set, signifies that there is an acknowledgment pending to be sent back.
kFlagAckPending = 0x0020,

/// When set, signifies that at least one message received on this exchange requested an acknowledgment.
/// This flag is read by the application to decide if it needs to request an acknowledgment for the
/// response message it is about to send. This flag can also indicate whether peer is using ReliableMessageProtocol.
kFlagPeerRequestedAck = 0x0040,

/// When set, signifies that at least one message has been received from peer on this exchange context.
kFlagMsgRcvdFromPeer = 0x0080,
kFlagMsgRcvdFromPeer = 0x0040,
};

BitFlags<Flags> mFlags; // Internal state flags
Expand All @@ -236,6 +213,19 @@ class ReliableMessageContext
CHIP_ERROR HandleNeedsAck(uint32_t MessageId, BitFlags<MessageFlagValues> Flags);
ExchangeContext * GetExchangeContext();

/**
* Set if an acknowledgment needs to be sent back to the peer on this exchange.
*
* @param[in] inAckPending A Boolean indicating whether (true) or not
* (false) an acknowledgment should be sent back
* in response to a received message.
*/
void SetAckPending(bool inAckPending);

// Set our pending peer ack id and any other state needed to ensure that we
// will send that ack at some point.
void SetPendingPeerAckId(uint32_t aPeerAckId);

private:
friend class ReliableMessageMgr;
friend class ExchangeContext;
Expand Down
1 change: 0 additions & 1 deletion src/messaging/ReliableMessageMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ void ReliableMessageMgr::ExecuteActions()
#endif
// Send the Ack in a SecureChannel::StandaloneAck message
rc->SendStandaloneAckMessage();
rc->SetAckPending(false);
}
}
});
Expand Down
Loading