Skip to content

Commit 8cee696

Browse files
committed
net: log bytes recv/sent per command
1 parent 754400e commit 8cee696

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

src/net.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#endif
6262
#endif
6363

64+
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
6465

6566
//
6667
// Global state variables
@@ -678,7 +679,9 @@ void CNode::copyStats(CNodeStats& stats)
678679
X(fInbound);
679680
X(nStartingHeight);
680681
X(nSendBytes);
682+
X(mapSendBytesPerMsgCmd);
681683
X(nRecvBytes);
684+
X(mapRecvBytesPerMsgCmd);
682685
X(fWhitelisted);
683686

684687
// It is common for nodes with good ping times to suddenly become lagged,
@@ -732,6 +735,15 @@ bool CNode::ReceiveMsgBytes(const char* pch, unsigned int nBytes, bool& complete
732735
nBytes -= handled;
733736

734737
if (msg.complete()) {
738+
739+
// Store received bytes per message command
740+
// to prevent a memory DOS, only allow valid commands
741+
mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
742+
if (i == mapRecvBytesPerMsgCmd.end())
743+
i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
744+
assert(i != mapRecvBytesPerMsgCmd.end());
745+
i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
746+
735747
msg.nTime = GetTimeMicros();
736748
complete = true;
737749
}
@@ -2478,6 +2490,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
24782490
nOptimisticBytesWritten = 0;
24792491
nLocalServices = nLocalServicesIn;
24802492

2493+
for (const std::string &msg : getAllNetMessageTypes())
2494+
mapRecvBytesPerMsgCmd[msg] = 0;
2495+
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
2496+
24812497
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
24822498
nMyStartingHeight = nMyStartingHeightIn;
24832499

@@ -2546,7 +2562,7 @@ void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
25462562
LogPrint(BCLog::NET, "(aborted)\n");
25472563
}
25482564

2549-
void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
2565+
void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
25502566
{
25512567
// The -*messagestest options are intentionally not documented in the help message,
25522568
// since they are only used during development to debug the networking code and are
@@ -2568,6 +2584,9 @@ void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
25682584
unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
25692585
WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
25702586

2587+
//log total amount of bytes per command
2588+
mapSendBytesPerMsgCmd[std::string(pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;
2589+
25712590
// Set the checksum
25722591
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
25732592
unsigned int nChecksum = 0;

src/net.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ struct LocalServiceInfo {
465465

466466
extern RecursiveMutex cs_mapLocalHost;
467467
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
468+
typedef std::map<std::string, uint64_t> mapMsgCmdSize; //command, total bytes
468469

469470
class CNodeStats
470471
{
@@ -481,7 +482,9 @@ class CNodeStats
481482
bool fInbound;
482483
int nStartingHeight;
483484
uint64_t nSendBytes;
485+
mapMsgCmdSize mapSendBytesPerMsgCmd;
484486
uint64_t nRecvBytes;
487+
mapMsgCmdSize mapRecvBytesPerMsgCmd;
485488
bool fWhitelisted;
486489
double dPingTime;
487490
double dPingWait;
@@ -587,6 +590,9 @@ class CNode
587590

588591
const uint64_t nKeyedNetGroup;
589592
protected:
593+
mapMsgCmdSize mapSendBytesPerMsgCmd;
594+
mapMsgCmdSize mapRecvBytesPerMsgCmd;
595+
590596
std::vector<std::string> vecRequestsFulfilled; //keep track of what client has asked for
591597

592598
// Basic fuzz-testing
@@ -732,7 +738,7 @@ class CNode
732738
void AbortMessage() UNLOCK_FUNCTION(cs_vSend);
733739

734740
// TODO: Document the precondition of this function. Is cs_vSend locked?
735-
void EndMessage() UNLOCK_FUNCTION(cs_vSend);
741+
void EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend);
736742

737743
void PushVersion();
738744

@@ -741,7 +747,7 @@ class CNode
741747
{
742748
try {
743749
BeginMessage(pszCommand);
744-
EndMessage();
750+
EndMessage(pszCommand);
745751
} catch (...) {
746752
AbortMessage();
747753
throw;
@@ -754,7 +760,7 @@ class CNode
754760
try {
755761
BeginMessage(pszCommand);
756762
ssSend << a1;
757-
EndMessage();
763+
EndMessage(pszCommand);
758764
} catch (...) {
759765
AbortMessage();
760766
throw;
@@ -767,7 +773,7 @@ class CNode
767773
try {
768774
BeginMessage(pszCommand);
769775
ssSend << a1 << a2;
770-
EndMessage();
776+
EndMessage(pszCommand);
771777
} catch (...) {
772778
AbortMessage();
773779
throw;
@@ -780,7 +786,7 @@ class CNode
780786
try {
781787
BeginMessage(pszCommand);
782788
ssSend << a1 << a2 << a3;
783-
EndMessage();
789+
EndMessage(pszCommand);
784790
} catch (...) {
785791
AbortMessage();
786792
throw;
@@ -793,7 +799,7 @@ class CNode
793799
try {
794800
BeginMessage(pszCommand);
795801
ssSend << a1 << a2 << a3 << a4;
796-
EndMessage();
802+
EndMessage(pszCommand);
797803
} catch (...) {
798804
AbortMessage();
799805
throw;
@@ -806,7 +812,7 @@ class CNode
806812
try {
807813
BeginMessage(pszCommand);
808814
ssSend << a1 << a2 << a3 << a4 << a5;
809-
EndMessage();
815+
EndMessage(pszCommand);
810816
} catch (...) {
811817
AbortMessage();
812818
throw;
@@ -819,7 +825,7 @@ class CNode
819825
try {
820826
BeginMessage(pszCommand);
821827
ssSend << a1 << a2 << a3 << a4 << a5 << a6;
822-
EndMessage();
828+
EndMessage(pszCommand);
823829
} catch (...) {
824830
AbortMessage();
825831
throw;
@@ -832,7 +838,7 @@ class CNode
832838
try {
833839
BeginMessage(pszCommand);
834840
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
835-
EndMessage();
841+
EndMessage(pszCommand);
836842
} catch (...) {
837843
AbortMessage();
838844
throw;
@@ -845,7 +851,7 @@ class CNode
845851
try {
846852
BeginMessage(pszCommand);
847853
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
848-
EndMessage();
854+
EndMessage(pszCommand);
849855
} catch (...) {
850856
AbortMessage();
851857
throw;
@@ -858,7 +864,7 @@ class CNode
858864
try {
859865
BeginMessage(pszCommand);
860866
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
861-
EndMessage();
867+
EndMessage(pszCommand);
862868
} catch (...) {
863869
AbortMessage();
864870
throw;
@@ -871,7 +877,7 @@ class CNode
871877
try {
872878
BeginMessage(pszCommand);
873879
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10;
874-
EndMessage();
880+
EndMessage(pszCommand);
875881
} catch (...) {
876882
AbortMessage();
877883
throw;
@@ -884,7 +890,7 @@ class CNode
884890
try {
885891
BeginMessage(pszCommand);
886892
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10 << a11;
887-
EndMessage();
893+
EndMessage(pszCommand);
888894
} catch (...) {
889895
AbortMessage();
890896
throw;
@@ -897,7 +903,7 @@ class CNode
897903
try {
898904
BeginMessage(pszCommand);
899905
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10 << a11 << a12;
900-
EndMessage();
906+
EndMessage(pszCommand);
901907
} catch (...) {
902908
AbortMessage();
903909
throw;

src/rpc/net.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
9595
" n, (numeric) The heights of blocks we're currently asking from this peer\n"
9696
" ...\n"
9797
" ]\n"
98+
" \"bytessent_per_msg\": {\n"
99+
" \"addr\": n, (numeric) The total bytes sent aggregated by message type\n"
100+
" ...\n"
101+
" }\n"
102+
" \"bytesrecv_per_msg\": {\n"
103+
" \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
104+
" ...\n"
105+
" }\n"
98106
" }\n"
99107
" ,...\n"
100108
"]\n"
@@ -147,6 +155,20 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
147155
}
148156
obj.push_back(Pair("whitelisted", stats.fWhitelisted));
149157

158+
UniValue sendPerMsgCmd(UniValue::VOBJ);
159+
for (const mapMsgCmdSize::value_type &i : stats.mapSendBytesPerMsgCmd) {
160+
if (i.second > 0)
161+
sendPerMsgCmd.pushKV(i.first, i.second);
162+
}
163+
obj.pushKV("bytessent_per_msg", sendPerMsgCmd);
164+
165+
UniValue recvPerMsgCmd(UniValue::VOBJ);
166+
for (const mapMsgCmdSize::value_type &i : stats.mapRecvBytesPerMsgCmd) {
167+
if (i.second > 0)
168+
recvPerMsgCmd.pushKV(i.first, i.second);
169+
}
170+
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);
171+
150172
ret.push_back(obj);
151173
}
152174

0 commit comments

Comments
 (0)