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,15 @@ 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 ,
532
+ CHIP_ERROR_INVALID_ARGUMENT);
530
533
const auto * fabricInfo = fabricTable->FindFabricWithIndex (peerScopedNodeId.GetFabricIndex ());
531
- ReturnErrorCodeIf ( fabricInfo == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
534
+ ReturnErrorCodeWithMetricIf ( kMetricDeviceCASESession , fabricInfo == nullptr , CHIP_ERROR_INVALID_ARGUMENT);
532
535
533
536
err = Init (sessionManager, policy, delegate, peerScopedNodeId);
534
537
@@ -542,9 +545,11 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
542
545
543
546
// From here onwards, let's go to exit on error, as some state might have already
544
547
// been initialized
545
- SuccessOrExit ( err);
548
+ SuccessOrExitWithMetric ( kMetricDeviceCASESession , err);
546
549
547
- SuccessOrExit (err = fabricTable->AddFabricDelegate (this ));
550
+ SuccessOrExitWithMetric (kMetricDeviceCASESession , err = fabricTable->AddFabricDelegate (this ));
551
+
552
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESession );
548
553
549
554
// Set the PeerAddress in the secure session up front to indicate the
550
555
// Transport Type of the session that is being set up.
@@ -571,13 +576,16 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
571
576
}
572
577
else
573
578
{
579
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma1 );
574
580
err = SendSigma1 ();
575
581
SuccessOrExit (err);
576
582
}
577
583
578
584
exit :
579
585
if (err != CHIP_NO_ERROR)
580
586
{
587
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma1 , err);
588
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESession , err);
581
589
Clear ();
582
590
}
583
591
return err;
@@ -601,6 +609,7 @@ void CASESession::OnResponseTimeout(ExchangeContext * ec)
601
609
602
610
void CASESession::AbortPendingEstablish (CHIP_ERROR err)
603
611
{
612
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESession , err);
604
613
MATTER_TRACE_SCOPE (" AbortPendingEstablish" , " CASESession" );
605
614
// This needs to come before Clear() which will reset mState.
606
615
SessionEstablishmentStage state = MapCASEStateToSessionEstablishmentStage (mState );
@@ -851,7 +860,17 @@ CHIP_ERROR CASESession::SendSigma1()
851
860
ReturnErrorOnFailure (mExchangeCtxt .Value ()->SendMessage (Protocols::SecureChannel::MsgType::CASE_Sigma1, std::move (msg_R1),
852
861
SendFlags (SendMessageFlags::kExpectResponse )));
853
862
854
- mState = resuming ? State::kSentSigma1Resume : State::kSentSigma1 ;
863
+ if (resuming)
864
+ {
865
+ mState = State::kSentSigma1Resume ;
866
+
867
+ // Flags that Resume is being attempted
868
+ MATTER_LOG_METRIC (kMetricDeviceCASESessionSigma1Resume );
869
+ }
870
+ else
871
+ {
872
+ mState = State::kSentSigma1 ;
873
+ }
855
874
856
875
ChipLogProgress (SecureChannel, " Sent Sigma1 msg" );
857
876
@@ -984,7 +1003,13 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg)
984
1003
std::copy (resumptionId.begin (), resumptionId.end (), mResumeResumptionId .begin ());
985
1004
986
1005
// Send Sigma2Resume message to the initiator
987
- SuccessOrExit (err = SendSigma2Resume ());
1006
+ MATTER_LOG_METRIC_BEGIN (kMetricDeviceCASESessionSigma2Resume );
1007
+ err = SendSigma2Resume ();
1008
+ if (CHIP_NO_ERROR != err)
1009
+ {
1010
+ MATTER_LOG_METRIC_END (kMetricDeviceCASESessionSigma2Resume , err);
1011
+ }
1012
+ SuccessOrExit (err);
988
1013
989
1014
mDelegate ->OnSessionEstablishmentStarted ();
990
1015
@@ -1013,7 +1038,13 @@ CHIP_ERROR CASESession::HandleSigma1(System::PacketBufferHandle && msg)
1013
1038
// mRemotePubKey.Length() == initiatorPubKey.size() == kP256_PublicKey_Length.
1014
1039
memcpy (mRemotePubKey .Bytes (), initiatorPubKey.data (), mRemotePubKey .Length ());
1015
1040
1016
- SuccessOrExit (err = SendSigma2 ());
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
+ CHIP_ERROR 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,11 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
2335
2380
case State::kSentSigma2Resume :
2336
2381
if (msgType == Protocols::SecureChannel::MsgType::StatusReport)
2337
2382
{
2338
- err = HandleStatusReport (std::move (msg), /* successExpected*/ true );
2383
+ // Need to capture before invoking status report since 'this' might be deallocated on successful completion of sigma3
2384
+ MetricKey key = (mState == State::kSentSigma3 ) ? kMetricDeviceCASESessionSigma3 : kMetricDeviceCASESessionSigma2Resume ;
2385
+ err = HandleStatusReport (std::move (msg), /* successExpected*/ true );
2386
+ MATTER_LOG_METRIC_END (key, err);
2387
+ IgnoreUnusedVariable (key);
2339
2388
}
2340
2389
break ;
2341
2390
default :
0 commit comments