Skip to content

Commit 8f77784

Browse files
authored
Rmp log source destination for txrx messages (project-chip#35672)
* [Logging] Add destination to RX logs * [Logging] Add source to TX logs * Update the readme about RX/TX logs formatting
1 parent be3c70a commit 8f77784

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

.github/.wordlist.txt

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ DV
462462
DVK
463463
dynload
464464
eabi
465+
EAF
465466
EB
466467
ECC
467468
ECD
@@ -552,6 +553,7 @@ feff
552553
ffaa
553554
ffeebaefa
554555
FFF
556+
FFFFFFFFFFFF0102
555557
fffe
556558
fffff
557559
Fi

src/messaging/ExchangeMgr.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
195195
auto * protocolName = Protocols::GetProtocolName(payloadHeader.GetProtocolID());
196196
auto * msgTypeName = Protocols::GetMessageTypeName(payloadHeader.GetProtocolID(), payloadHeader.GetMessageType());
197197

198+
auto destination = kUndefinedNodeId;
199+
if (packetHeader.GetDestinationNodeId().HasValue())
200+
{
201+
destination = packetHeader.GetDestinationNodeId().Value();
202+
}
203+
else if (session->IsSecureSession())
204+
{
205+
destination = session->AsSecureSession()->GetLocalNodeId();
206+
}
207+
198208
//
199209
// 32-bit value maximum = 10 chars + text preamble (6) + trailer (1) + null (1) + 2 buffer = 20
200210
//
@@ -220,18 +230,25 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
220230
char typeStr[4 + 1 + 2 + 1];
221231
snprintf(typeStr, sizeof(typeStr), "%04X:%02X", payloadHeader.GetProtocolID().GetProtocolId(), payloadHeader.GetMessageType());
222232

233+
// More work around pigweed not allowing more than 14 format args in a log
234+
// message when using tokenized logs.
235+
// text(5) + fabricIndex (uint16_t, at most 5 chars) + text (1) + source (16) + text (2) + compressed fabric id (4) + text (5) +
236+
// destination + null-terminator
237+
char sourceDestinationStr[5 + 5 + 1 + 16 + 2 + 4 + 5 + 16 + 1];
238+
snprintf(sourceDestinationStr, sizeof(sourceDestinationStr), "from %u:" ChipLogFormatX64 " [%04X] to " ChipLogFormatX64,
239+
session->GetFabricIndex(), ChipLogValueX64(session->GetPeer().GetNodeId()), static_cast<uint16_t>(compressedFabricId),
240+
ChipLogValueX64(destination));
241+
223242
//
224243
// Legend that can be used to decode this log line can be found in README.md
225244
//
226-
ChipLogProgress(ExchangeManager,
227-
">>> [E:" ChipLogFormatExchangeId " S:%u M:" ChipLogFormatMessageCounter
228-
"%s] (%s) Msg RX from %u:" ChipLogFormatX64 " [%04X] --- Type %s (%s:%s) (B:%u)",
229-
ChipLogValueExchangeIdFromReceivedHeader(payloadHeader), session->SessionIdForLogging(),
230-
packetHeader.GetMessageCounter(), ackBuf, Transport::GetSessionTypeString(session), session->GetFabricIndex(),
231-
ChipLogValueX64(session->GetPeer().GetNodeId()), static_cast<uint16_t>(compressedFabricId), typeStr,
232-
protocolName, msgTypeName,
233-
static_cast<unsigned>(msgBuf->TotalLength() + packetHeader.EncodeSizeBytes() + packetHeader.MICTagLength() +
234-
payloadHeader.EncodeSizeBytes()));
245+
ChipLogProgress(
246+
ExchangeManager,
247+
">>> [E:" ChipLogFormatExchangeId " S:%u M:" ChipLogFormatMessageCounter "%s] (%s) Msg RX %s --- Type %s (%s:%s) (B:%u)",
248+
ChipLogValueExchangeIdFromReceivedHeader(payloadHeader), session->SessionIdForLogging(), packetHeader.GetMessageCounter(),
249+
ackBuf, Transport::GetSessionTypeString(session), sourceDestinationStr, typeStr, protocolName, msgTypeName,
250+
static_cast<unsigned>(msgBuf->TotalLength() + packetHeader.EncodeSizeBytes() + packetHeader.MICTagLength() +
251+
payloadHeader.EncodeSizeBytes()));
235252
#endif
236253

237254
MessageFlags msgFlags;

src/messaging/README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ will be expanded are denoted with `$` .
1616
Unless specified, numerical values are represented in decimal notation.
1717

1818
```
19-
<<< [E:$exchange_id S:$session_id M:$msg_id (Ack: $ack_msg_id)] ($msg_category) Msg TX to $fabric_index:$destination [$compressed_fabric_id] [$peer_address] --- Type $protocol_id:$msg_type ($protocol_name:$msg_type_name) (B:$size)
19+
<<< [E:$exchange_id S:$session_id M:$msg_id (Ack: $ack_msg_id)] ($msg_category) Msg TX from $source to $fabric_index:$destination [$compressed_fabric_id] [$peer_address] --- Type $protocol_id:$msg_type ($protocol_name:$msg_type_name) (B:$size)
2020
```
2121

2222
| Field | Description |
@@ -27,6 +27,7 @@ Unless specified, numerical values are represented in decimal notation.
2727
| ack_msg_id | If present, the ACK message counter. Otherwise, this entire field is omitted from display |
2828
| msg_category | U: Un-secure Unicast, S: Secure Unicast, G: Secure Groupcast |
2929
| fabric_index | Fabric index on the sending side |
30+
| source | 64-bit Node Identifier that can represent both group, operational and temporary node identifiers depending on `$msg_category` (in hex) |
3031
| destination | 64-bit Node Identifier that can represent both group, operational and temporary node identifiers depending on `$msg_category` (in hex) |
3132
| compressed_fabric_id | If present and valid, lower 16-bits of the compressed fabric ID (in hex). Otherwise, it will be set to 0000. |
3233
| peer_address | The peer address (IP and port) for the session |
@@ -41,19 +42,19 @@ Unless specified, numerical values are represented in decimal notation.
4142
_Unencrypted Unicast:_
4243

4344
```
44-
<<< [E:26341i S:0 M:264589322] (U) Msg TX to 0:0000000000000000 [0000] --- Type 0000:30 (SecureChannel:CASE_Sigma1)
45+
<<< [E:26341i S:0 M:264589322] (U) Msg TX from 0FDE2AE2EAF5D74D to 0:0000000000000000 [0000] --- Type 0000:30 (SecureChannel:CASE_Sigma1)
4546
```
4647

4748
_Secure Unicast:_
4849

4950
```
50-
<<< [E:26347i S:5110 M:30642895 (Ack: 9203233)] (S) Msg TX to 1:0000000012344321 [1667] --- Type 0001:06 (IM:WriteRequest)
51+
<<< [E:26347i S:5110 M:30642895 (Ack: 9203233)] (S) Msg TX from 000000000001B669 to 1:0000000012344321 [1667] --- Type 0001:06 (IM:WriteRequest)
5152
```
5253

5354
_Secure Groupcast:_
5455

5556
```
56-
<<< [E:26349i S:17850 M:2000] (G) Msg TX to 1:FFFFFFFFFFFF0102 [1667] --- Type 0001:06 (IM:WriteRequest)
57+
<<< [E:26349i S:17850 M:2000] (G) Msg TX from 000000000001B669 to 1:FFFFFFFFFFFF0102 [1667] --- Type 0001:06 (IM:WriteRequest)
5758
```
5859

5960
### Message Reception
@@ -66,7 +67,7 @@ will be expanded are denoted with `$` .
6667
Unless specified, numerical values are represented in decimal notation.
6768

6869
```
69-
>>> [E:$exchange_id M: $msg_id (Ack: $ack_msg_id)] ($msg_category) Msg RX from $fabric_index:$source [$compressed_fabric_id] --- Type $protocol_id:$msg_type ($protocol_name:$msg_type_name) (B:$size)
70+
>>> [E:$exchange_id M: $msg_id (Ack: $ack_msg_id)] ($msg_category) Msg RX from $fabric_index:$source to $destination [$compressed_fabric_id] --- Type $protocol_id:$msg_type ($protocol_name:$msg_type_name) (B:$size)
7071
```
7172

7273
This has a similar legend to that for transmission except `$source` denotes the
@@ -77,17 +78,17 @@ source's node identifier and `$fabric_index` is the index on the recipient.
7778
_Unencrypted Unicast:_
7879

7980
```
80-
>>> [E:26341i S:0 M:264589322] (U) Msg RX from 0:0FDE2AE2EAF5D74D [0000] --- Type 0000:30 (SecureChannel:CASE_Sigma1)
81+
>>> [E:26341i S:0 M:264589322] (U) Msg RX from 0:0FDE2AE2EAF5D74D to 0000000000000000 [0000] --- Type 0000:30 (SecureChannel:CASE_Sigma1)
8182
```
8283

8384
_Secure Unicast:_
8485

8586
```
86-
>>> [E:26342i S:62311 M:30642885] (S) Msg RX from 1:000000000001B669 [1667] --- Type 0001:08 (IM:InvokeCommandRequest)
87+
>>> [E:26342i S:62311 M:30642885] (S) Msg RX from 1:000000000001B669 to 0000000012344321 [1667] --- Type 0001:08 (IM:InvokeCommandRequest)
8788
```
8889

8990
_Secure Groupcast:_
9091

9192
```
92-
>>> [E:26349i S:1720 M:2000] (G) Msg RX from 1:000000000001B669 [0000] --- Type 0001:06 (IM:WriteRequest)
93+
>>> [E:26349i S:1720 M:2000] (G) Msg RX from 1:000000000001B669 to FFFFFFFFFFFF0102 [0000] --- Type 0001:06 (IM:WriteRequest)
9394
```

src/transport/SessionManager.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
220220
FabricIndex fabricIndex;
221221
#endif // CHIP_PROGRESS_LOGGING
222222

223+
NodeId sourceNodeId = kUndefinedNodeId;
223224
PeerAddress destination_address;
224225

225226
switch (sessionHandle->GetSessionType())
@@ -236,7 +237,7 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
236237
packetHeader.SetMessageCounter(mGroupClientCounter.GetCounter(isControlMsg));
237238
mGroupClientCounter.IncrementCounter(isControlMsg);
238239
packetHeader.SetSessionType(Header::SessionType::kGroupSession);
239-
NodeId sourceNodeId = fabric->GetNodeId();
240+
sourceNodeId = fabric->GetNodeId();
240241
packetHeader.SetSourceNodeId(sourceNodeId);
241242

242243
if (!packetHeader.IsValidGroupMsg())
@@ -292,7 +293,7 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
292293
CHIP_TRACE_MESSAGE_SENT(payloadHeader, packetHeader, destination_address, message->Start(), message->TotalLength());
293294

294295
CryptoContext::NonceStorage nonce;
295-
NodeId sourceNodeId = session->GetLocalScopedNodeId().GetNodeId();
296+
sourceNodeId = session->GetLocalScopedNodeId().GetNodeId();
296297
CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), messageCounter, sourceNodeId);
297298

298299
ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message));
@@ -336,6 +337,10 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
336337
{
337338
destination = session->GetEphemeralInitiatorNodeID();
338339
}
340+
else if (session->GetSessionRole() == Transport::UnauthenticatedSession::SessionRole::kInitiator)
341+
{
342+
sourceNodeId = session->GetEphemeralInitiatorNodeID();
343+
}
339344
#endif // CHIP_PROGRESS_LOGGING
340345
}
341346
break;
@@ -386,16 +391,22 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
386391
char exchangeStr[5 + 1 + 1];
387392
snprintf(exchangeStr, sizeof(exchangeStr), ChipLogFormatExchangeId, ChipLogValueExchangeIdFromSentHeader(payloadHeader));
388393

394+
// More work around pigweed not allowing more than 14 format args in a log
395+
// message when using tokenized logs.
396+
// text(5) + source(16) + text(4) + fabricIndex(uint16_t, at most 5 chars) + text(1) + destination(16) + text(2) + compressed
397+
// fabric id(4) + text(1) + null-terminator
398+
char sourceDestinationStr[5 + 16 + 4 + 5 + 1 + 16 + 2 + 4 + 1 + 1];
399+
snprintf(sourceDestinationStr, sizeof(sourceDestinationStr), "from " ChipLogFormatX64 " to %u:" ChipLogFormatX64 " [%04X]",
400+
ChipLogValueX64(sourceNodeId), fabricIndex, ChipLogValueX64(destination), static_cast<uint16_t>(compressedFabricId));
401+
389402
//
390403
// Legend that can be used to decode this log line can be found in messaging/README.md
391404
//
392405
ChipLogProgress(ExchangeManager,
393-
"<<< [E:%s S:%u M:" ChipLogFormatMessageCounter "%s] (%s) Msg TX to %u:" ChipLogFormatX64
394-
" [%04X] [%s] --- Type %s (%s:%s) (B:%u)",
406+
"<<< [E:%s S:%u M:" ChipLogFormatMessageCounter "%s] (%s) Msg TX %s [%s] --- Type %s (%s:%s) (B:%u)",
395407
exchangeStr, sessionHandle->SessionIdForLogging(), packetHeader.GetMessageCounter(), ackBuf,
396-
Transport::GetSessionTypeString(sessionHandle), fabricIndex, ChipLogValueX64(destination),
397-
static_cast<uint16_t>(compressedFabricId), addressStr, typeStr, protocolName, msgTypeName,
398-
static_cast<unsigned>(message->TotalLength()));
408+
Transport::GetSessionTypeString(sessionHandle), sourceDestinationStr, addressStr, typeStr, protocolName,
409+
msgTypeName, static_cast<unsigned>(message->TotalLength()));
399410
#endif
400411

401412
preparedMessage = EncryptedPacketBufferHandle::MarkEncrypted(std::move(message));

0 commit comments

Comments
 (0)