Skip to content

Commit dc0305d

Browse files
committed
Merge pull request #6589
ca188c6 log bytes recv/sent per command (Jonas Schnelli)
2 parents 82bcf40 + ca188c6 commit dc0305d

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/net.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ namespace {
6767
};
6868
}
6969

70+
//immutable thread safe array of allowed commands for logging inbound traffic
71+
const static std::string logAllowIncomingMsgCmds[] = {
72+
"version", "addr", "inv", "getdata", "merkleblock",
73+
"getblocks", "getheaders", "tx", "headers", "block",
74+
"getaddr", "mempool", "ping", "pong", "alert", "notfound",
75+
"filterload", "filteradd", "filterclear", "reject"};
76+
77+
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
78+
7079
//
7180
// Global state variables
7281
//
@@ -627,7 +636,9 @@ void CNode::copyStats(CNodeStats &stats)
627636
X(fInbound);
628637
X(nStartingHeight);
629638
X(nSendBytes);
639+
X(mapSendBytesPerMsgCmd);
630640
X(nRecvBytes);
641+
X(mapRecvBytesPerMsgCmd);
631642
X(fWhitelisted);
632643

633644
// It is common for nodes with good ping times to suddenly become lagged,
@@ -682,6 +693,15 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
682693
nBytes -= handled;
683694

684695
if (msg.complete()) {
696+
697+
//store received bytes per message command
698+
//to prevent a memory DOS, only allow valid commands
699+
mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
700+
if (i == mapRecvBytesPerMsgCmd.end())
701+
i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
702+
assert(i != mapRecvBytesPerMsgCmd.end());
703+
i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
704+
685705
msg.nTime = GetTimeMicros();
686706
messageHandlerCondition.notify_one();
687707
}
@@ -2378,6 +2398,9 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
23782398
nPingUsecTime = 0;
23792399
fPingQueued = false;
23802400
nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2401+
for (unsigned int i = 0; i < sizeof(logAllowIncomingMsgCmds)/sizeof(logAllowIncomingMsgCmds[0]); i++)
2402+
mapRecvBytesPerMsgCmd[logAllowIncomingMsgCmds[i]] = 0;
2403+
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
23812404

23822405
{
23832406
LOCK(cs_nLastNodeId);
@@ -2457,7 +2480,7 @@ void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
24572480
LogPrint("net", "(aborted)\n");
24582481
}
24592482

2460-
void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
2483+
void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
24612484
{
24622485
// The -*messagestest options are intentionally not documented in the help message,
24632486
// since they are only used during development to debug the networking code and are
@@ -2480,6 +2503,9 @@ void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
24802503
unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
24812504
WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
24822505

2506+
//log total amount of bytes per command
2507+
mapSendBytesPerMsgCmd[std::string(pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;
2508+
24832509
// Set the checksum
24842510
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
24852511
unsigned int nChecksum = 0;

src/net.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct LocalServiceInfo {
182182

183183
extern CCriticalSection cs_mapLocalHost;
184184
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
185+
typedef std::map<std::string, uint64_t> mapMsgCmdSize; //command, total bytes
185186

186187
class CNodeStats
187188
{
@@ -199,7 +200,9 @@ class CNodeStats
199200
bool fInbound;
200201
int nStartingHeight;
201202
uint64_t nSendBytes;
203+
mapMsgCmdSize mapSendBytesPerMsgCmd;
202204
uint64_t nRecvBytes;
205+
mapMsgCmdSize mapRecvBytesPerMsgCmd;
203206
bool fWhitelisted;
204207
double dPingTime;
205208
double dPingWait;
@@ -373,6 +376,9 @@ class CNode
373376
static std::vector<CSubNet> vWhitelistedRange;
374377
static CCriticalSection cs_vWhitelistedRange;
375378

379+
mapMsgCmdSize mapSendBytesPerMsgCmd;
380+
mapMsgCmdSize mapRecvBytesPerMsgCmd;
381+
376382
// Basic fuzz-testing
377383
void Fuzz(int nChance); // modifies ssSend
378384

@@ -525,7 +531,7 @@ class CNode
525531
void AbortMessage() UNLOCK_FUNCTION(cs_vSend);
526532

527533
// TODO: Document the precondition of this function. Is cs_vSend locked?
528-
void EndMessage() UNLOCK_FUNCTION(cs_vSend);
534+
void EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend);
529535

530536
void PushVersion();
531537

@@ -535,7 +541,7 @@ class CNode
535541
try
536542
{
537543
BeginMessage(pszCommand);
538-
EndMessage();
544+
EndMessage(pszCommand);
539545
}
540546
catch (...)
541547
{
@@ -551,7 +557,7 @@ class CNode
551557
{
552558
BeginMessage(pszCommand);
553559
ssSend << a1;
554-
EndMessage();
560+
EndMessage(pszCommand);
555561
}
556562
catch (...)
557563
{
@@ -567,7 +573,7 @@ class CNode
567573
{
568574
BeginMessage(pszCommand);
569575
ssSend << a1 << a2;
570-
EndMessage();
576+
EndMessage(pszCommand);
571577
}
572578
catch (...)
573579
{
@@ -583,7 +589,7 @@ class CNode
583589
{
584590
BeginMessage(pszCommand);
585591
ssSend << a1 << a2 << a3;
586-
EndMessage();
592+
EndMessage(pszCommand);
587593
}
588594
catch (...)
589595
{
@@ -599,7 +605,7 @@ class CNode
599605
{
600606
BeginMessage(pszCommand);
601607
ssSend << a1 << a2 << a3 << a4;
602-
EndMessage();
608+
EndMessage(pszCommand);
603609
}
604610
catch (...)
605611
{
@@ -615,7 +621,7 @@ class CNode
615621
{
616622
BeginMessage(pszCommand);
617623
ssSend << a1 << a2 << a3 << a4 << a5;
618-
EndMessage();
624+
EndMessage(pszCommand);
619625
}
620626
catch (...)
621627
{
@@ -631,7 +637,7 @@ class CNode
631637
{
632638
BeginMessage(pszCommand);
633639
ssSend << a1 << a2 << a3 << a4 << a5 << a6;
634-
EndMessage();
640+
EndMessage(pszCommand);
635641
}
636642
catch (...)
637643
{
@@ -647,7 +653,7 @@ class CNode
647653
{
648654
BeginMessage(pszCommand);
649655
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
650-
EndMessage();
656+
EndMessage(pszCommand);
651657
}
652658
catch (...)
653659
{
@@ -663,7 +669,7 @@ class CNode
663669
{
664670
BeginMessage(pszCommand);
665671
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
666-
EndMessage();
672+
EndMessage(pszCommand);
667673
}
668674
catch (...)
669675
{
@@ -679,7 +685,7 @@ class CNode
679685
{
680686
BeginMessage(pszCommand);
681687
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
682-
EndMessage();
688+
EndMessage(pszCommand);
683689
}
684690
catch (...)
685691
{

src/rpcnet.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ UniValue getpeerinfo(const UniValue& params, bool fHelp)
111111
" n, (numeric) The heights of blocks we're currently asking from this peer\n"
112112
" ...\n"
113113
" ]\n"
114+
" \"bytessent_per_msg\": {\n"
115+
" \"addr\": n, (numeric) The total bytes sent aggregated by message type\n"
116+
" ...\n"
117+
" }\n"
118+
" \"bytesrecv_per_msg\": {\n"
119+
" \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
120+
" ...\n"
121+
" }\n"
114122
" }\n"
115123
" ,...\n"
116124
"]\n"
@@ -165,6 +173,20 @@ UniValue getpeerinfo(const UniValue& params, bool fHelp)
165173
}
166174
obj.push_back(Pair("whitelisted", stats.fWhitelisted));
167175

176+
UniValue sendPerMsgCmd(UniValue::VOBJ);
177+
BOOST_FOREACH(const mapMsgCmdSize::value_type &i, stats.mapSendBytesPerMsgCmd) {
178+
if (i.second > 0)
179+
sendPerMsgCmd.push_back(Pair(i.first, i.second));
180+
}
181+
obj.push_back(Pair("bytessent_per_msg", sendPerMsgCmd));
182+
183+
UniValue recvPerMsgCmd(UniValue::VOBJ);
184+
BOOST_FOREACH(const mapMsgCmdSize::value_type &i, stats.mapRecvBytesPerMsgCmd) {
185+
if (i.second > 0)
186+
recvPerMsgCmd.push_back(Pair(i.first, i.second));
187+
}
188+
obj.push_back(Pair("bytesrecv_per_msg", recvPerMsgCmd));
189+
168190
ret.push_back(obj);
169191
}
170192

0 commit comments

Comments
 (0)