47
47
#include < system/SystemClock.h>
48
48
#include < system/TLVPacketBufferBackingStore.h>
49
49
#include < tracing/macros.h>
50
+ #include < tracing/metric_event.h>
50
51
#include < transport/SessionManager.h>
51
52
52
53
namespace {
@@ -101,6 +102,7 @@ using namespace Credentials;
101
102
using namespace Messaging ;
102
103
using namespace Encoding ;
103
104
using namespace Protocols ::SecureChannel;
105
+ using namespace Tracing ;
104
106
105
107
constexpr uint8_t kKDFSR2Info [] = { 0x53 , 0x69 , 0x67 , 0x6d , 0x61 , 0x32 };
106
108
constexpr uint8_t kKDFSR3Info [] = { 0x53 , 0x69 , 0x67 , 0x6d , 0x61 , 0x33 };
@@ -521,14 +523,14 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
521
523
CHIP_ERROR err = CHIP_NO_ERROR;
522
524
523
525
// Return early on error here, as we have not initialized any state yet
524
- ReturnErrorCodeIf ( exchangeCtxt == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
525
- ReturnErrorCodeIf ( fabricTable == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
526
+ ReturnErrorCodeWithMetricIf ( kMetricDeviceCASESession , exchangeCtxt == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
527
+ ReturnErrorCodeWithMetricIf ( kMetricDeviceCASESession , fabricTable == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
526
528
527
529
// Use FabricTable directly to avoid situation of dangling index from stale FabricInfo
528
530
// until we factor-out any FabricInfo direct usage.
529
- ReturnErrorCodeIf ( peerScopedNodeId.GetFabricIndex () == kUndefinedFabricIndex , CHIP_ERROR_INVALID_ARGUMENT);
531
+ ReturnErrorCodeWithMetricIf ( kMetricDeviceCASESession , peerScopedNodeId.GetFabricIndex () == kUndefinedFabricIndex , CHIP_ERROR_INVALID_ARGUMENT);
530
532
const auto * fabricInfo = fabricTable->FindFabricWithIndex (peerScopedNodeId.GetFabricIndex ());
531
- ReturnErrorCodeIf ( fabricInfo == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
533
+ ReturnErrorCodeWithMetricIf ( kMetricDeviceCASESession , fabricInfo == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
532
534
533
535
err = Init (sessionManager, policy, delegate, peerScopedNodeId);
534
536
@@ -542,9 +544,11 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
542
544
543
545
// From here onwards, let's go to exit on error, as some state might have already
544
546
// been initialized
545
- SuccessOrExit ( err);
547
+ SuccessOrExitWithMetric ( kMetricDeviceCASESession , err);
546
548
547
- SuccessOrExit (err = fabricTable->AddFabricDelegate (this ));
549
+ SuccessOrExitWithMetric (kMetricDeviceCASESession , err = fabricTable->AddFabricDelegate (this ));
550
+
551
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESession );
548
552
549
553
// Set the PeerAddress in the secure session up front to indicate the
550
554
// Transport Type of the session that is being set up.
@@ -571,13 +575,16 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
571
575
}
572
576
else
573
577
{
578
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma1 );
574
579
err = SendSigma1 ();
575
580
SuccessOrExit (err);
576
581
}
577
582
578
583
exit :
579
584
if (err != CHIP_NO_ERROR)
580
585
{
586
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
587
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESession , err);
581
588
Clear ();
582
589
}
583
590
return err;
@@ -601,6 +608,7 @@ void CASESession::OnResponseTimeout(ExchangeContext * ec)
601
608
602
609
void CASESession::AbortPendingEstablish (CHIP_ERROR err)
603
610
{
611
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESession , err);
604
612
MATTER_TRACE_SCOPE (" AbortPendingEstablish" , " CASESession" );
605
613
// This needs to come before Clear() which will reset mState.
606
614
SessionEstablishmentStage state = MapCASEStateToSessionEstablishmentStage (mState );
@@ -851,7 +859,17 @@ CHIP_ERROR CASESession::SendSigma1()
851
859
ReturnErrorOnFailure (mExchangeCtxt .Value ()->SendMessage (Protocols::SecureChannel::MsgType::CASE_Sigma1, std::move (msg_R1),
852
860
SendFlags (SendMessageFlags::kExpectResponse )));
853
861
854
- mState = resuming ? State::kSentSigma1Resume : State::kSentSigma1 ;
862
+ if (resuming)
863
+ {
864
+ mState = State::kSentSigma1Resume ;
865
+
866
+ // Flags that Resume is being attempted
867
+ MATTER_LOG_METRIC (kMetricDeviceCASESessionSigma1Resume );
868
+ }
869
+ else
870
+ {
871
+ mState = State::kSentSigma1 ;
872
+ }
855
873
856
874
ChipLogProgress (SecureChannel, " Sent Sigma1 msg" );
857
875
@@ -984,7 +1002,13 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg)
984
1002
std::copy (resumptionId.begin (), resumptionId.end (), mResumeResumptionId .begin ());
985
1003
986
1004
// Send Sigma2Resume message to the initiator
987
- SuccessOrExit (err = SendSigma2Resume ());
1005
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma2Resume );
1006
+ err = SendSigma2Resume ();
1007
+ if (CHIP_NO_ERROR != err)
1008
+ {
1009
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma2Resume , err);
1010
+ }
1011
+ SuccessOrExit (err);
988
1012
989
1013
mDelegate ->OnSessionEstablishmentStarted ();
990
1014
@@ -1013,7 +1037,14 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg)
1013
1037
// mRemotePubKey.Length() == initiatorPubKey.size() == kP256_PublicKey_Length.
1014
1038
memcpy (mRemotePubKey .Bytes (), initiatorPubKey.data (), mRemotePubKey .Length ());
1015
1039
1016
- SuccessOrExit (err = SendSigma2 ());
1040
+
1041
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma2 );
1042
+ err = SendSigma2 ();
1043
+ if (CHIP_NO_ERROR != err)
1044
+ {
1045
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma2 , err);
1046
+ }
1047
+ SuccessOrExit (err);
1017
1048
1018
1049
mDelegate ->OnSessionEstablishmentStarted ();
1019
1050
@@ -1236,6 +1267,7 @@ CHIP_ERROR CASESession::HandleSigma2Resume(System::PacketBufferHandle && msg)
1236
1267
1237
1268
ChipLogDetail (SecureChannel, " Received Sigma2Resume msg" );
1238
1269
MATTER_TRACE_COUNTER (" Sigma2Resume" );
1270
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
1239
1271
1240
1272
uint8_t sigma2ResumeMIC[CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES];
1241
1273
@@ -1278,6 +1310,7 @@ CHIP_ERROR CASESession::HandleSigma2Resume(System::PacketBufferHandle && msg)
1278
1310
ChipLogError (SecureChannel, " Unable to save session resumption state: %" CHIP_ERROR_FORMAT, err2.Format ());
1279
1311
}
1280
1312
1313
+ MATTER_LOG_METRIC (kMetricDeviceCASESessionSigmaFinished );
1281
1314
SendStatusReport (mExchangeCtxt , kProtocolCodeSuccess );
1282
1315
1283
1316
mState = State::kFinishedViaResume ;
@@ -1294,10 +1327,17 @@ CHIP_ERROR CASESession::HandleSigma2Resume(System::PacketBufferHandle && msg)
1294
1327
CHIP_ERROR CASESession::HandleSigma2_and_SendSigma3 (System::PacketBufferHandle && msg)
1295
1328
{
1296
1329
MATTER_TRACE_SCOPE (" HandleSigma2_and_SendSigma3" , " CASESession" );
1297
- ReturnErrorOnFailure (HandleSigma2 (std::move (msg)));
1298
- ReturnErrorOnFailure (SendSigma3a ());
1330
+ auto err = HandleSigma2 (std::move (msg));
1331
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
1332
+ ReturnErrorOnFailure (err);
1299
1333
1300
- return CHIP_NO_ERROR;
1334
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma3 );
1335
+ err = SendSigma3a ();
1336
+ if (CHIP_NO_ERROR != err)
1337
+ {
1338
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma3 , err);
1339
+ }
1340
+ return err;
1301
1341
}
1302
1342
1303
1343
CHIP_ERROR CASESession::HandleSigma2 (System::PacketBufferHandle && msg)
@@ -1708,6 +1748,7 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
1708
1748
1709
1749
ChipLogProgress (SecureChannel, " Received Sigma3 msg" );
1710
1750
MATTER_TRACE_COUNTER (" Sigma3" );
1751
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma2 , err);
1711
1752
1712
1753
auto helper = WorkHelper<HandleSigma3Data>::Create (*this , &HandleSigma3b, &CASESession::HandleSigma3c);
1713
1754
VerifyOrExit (helper, err = CHIP_ERROR_NO_MEMORY);
@@ -1888,6 +1929,7 @@ CHIP_ERROR CASESession::HandleSigma3c(HandleSigma3Data & data, CHIP_ERROR status
1888
1929
}
1889
1930
}
1890
1931
1932
+ MATTER_LOG_METRIC (kMetricDeviceCASESessionSigmaFinished );
1891
1933
SendStatusReport (mExchangeCtxt , kProtocolCodeSuccess );
1892
1934
1893
1935
mState = State::kFinished ;
@@ -2288,6 +2330,7 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
2288
2330
2289
2331
case MsgType::StatusReport:
2290
2332
err = HandleStatusReport (std::move (msg), /* successExpected*/ false );
2333
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
2291
2334
break ;
2292
2335
2293
2336
default :
@@ -2308,6 +2351,7 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
2308
2351
2309
2352
case MsgType::StatusReport:
2310
2353
err = HandleStatusReport (std::move (msg), /* successExpected*/ false );
2354
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
2311
2355
break ;
2312
2356
2313
2357
default :
@@ -2324,6 +2368,7 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
2324
2368
2325
2369
case MsgType::StatusReport:
2326
2370
err = HandleStatusReport (std::move (msg), /* successExpected*/ false );
2371
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma2 , err);
2327
2372
break ;
2328
2373
2329
2374
default :
@@ -2335,7 +2380,10 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
2335
2380
case State::kSentSigma2Resume :
2336
2381
if (msgType == Protocols::SecureChannel::MsgType::StatusReport)
2337
2382
{
2383
+ // Need to capture before invoking status report since 'this' might be deallocated on successful completion of sigma3
2384
+ auto key = (mState == State::kSentSigma3 ) ? kMetricDeviceCASESessionSigma3 : kMetricDeviceCASESessionSigma2Resume ;
2338
2385
err = HandleStatusReport (std::move (msg), /* successExpected*/ true );
2386
+ MATTER_LOG_METRIC_END (key, err);
2339
2387
}
2340
2388
break ;
2341
2389
default :
0 commit comments