Skip to content

Commit

Permalink
net: log bytes recv/sent per command
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Sep 2, 2020
1 parent 754400e commit 8cee696
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
21 changes: 20 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#endif
#endif

const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";

//
// Global state variables
Expand Down Expand Up @@ -678,7 +679,9 @@ void CNode::copyStats(CNodeStats& stats)
X(fInbound);
X(nStartingHeight);
X(nSendBytes);
X(mapSendBytesPerMsgCmd);
X(nRecvBytes);
X(mapRecvBytesPerMsgCmd);
X(fWhitelisted);

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

if (msg.complete()) {

// Store received bytes per message command
// to prevent a memory DOS, only allow valid commands
mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
if (i == mapRecvBytesPerMsgCmd.end())
i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
assert(i != mapRecvBytesPerMsgCmd.end());
i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;

msg.nTime = GetTimeMicros();
complete = true;
}
Expand Down Expand Up @@ -2478,6 +2490,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
nOptimisticBytesWritten = 0;
nLocalServices = nLocalServicesIn;

for (const std::string &msg : getAllNetMessageTypes())
mapRecvBytesPerMsgCmd[msg] = 0;
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;

GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
nMyStartingHeight = nMyStartingHeightIn;

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

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

//log total amount of bytes per command
mapSendBytesPerMsgCmd[std::string(pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;

// Set the checksum
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
unsigned int nChecksum = 0;
Expand Down
34 changes: 20 additions & 14 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ struct LocalServiceInfo {

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

class CNodeStats
{
Expand All @@ -481,7 +482,9 @@ class CNodeStats
bool fInbound;
int nStartingHeight;
uint64_t nSendBytes;
mapMsgCmdSize mapSendBytesPerMsgCmd;
uint64_t nRecvBytes;
mapMsgCmdSize mapRecvBytesPerMsgCmd;
bool fWhitelisted;
double dPingTime;
double dPingWait;
Expand Down Expand Up @@ -587,6 +590,9 @@ class CNode

const uint64_t nKeyedNetGroup;
protected:
mapMsgCmdSize mapSendBytesPerMsgCmd;
mapMsgCmdSize mapRecvBytesPerMsgCmd;

std::vector<std::string> vecRequestsFulfilled; //keep track of what client has asked for

// Basic fuzz-testing
Expand Down Expand Up @@ -732,7 +738,7 @@ class CNode
void AbortMessage() UNLOCK_FUNCTION(cs_vSend);

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

void PushVersion();

Expand All @@ -741,7 +747,7 @@ class CNode
{
try {
BeginMessage(pszCommand);
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -754,7 +760,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -767,7 +773,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -780,7 +786,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -793,7 +799,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -806,7 +812,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -819,7 +825,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -832,7 +838,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -845,7 +851,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -858,7 +864,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -871,7 +877,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -884,7 +890,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10 << a11;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand All @@ -897,7 +903,7 @@ class CNode
try {
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9 << a10 << a11 << a12;
EndMessage();
EndMessage(pszCommand);
} catch (...) {
AbortMessage();
throw;
Expand Down
22 changes: 22 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
" n, (numeric) The heights of blocks we're currently asking from this peer\n"
" ...\n"
" ]\n"
" \"bytessent_per_msg\": {\n"
" \"addr\": n, (numeric) The total bytes sent aggregated by message type\n"
" ...\n"
" }\n"
" \"bytesrecv_per_msg\": {\n"
" \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
" ...\n"
" }\n"
" }\n"
" ,...\n"
"]\n"
Expand Down Expand Up @@ -147,6 +155,20 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
}
obj.push_back(Pair("whitelisted", stats.fWhitelisted));

UniValue sendPerMsgCmd(UniValue::VOBJ);
for (const mapMsgCmdSize::value_type &i : stats.mapSendBytesPerMsgCmd) {
if (i.second > 0)
sendPerMsgCmd.pushKV(i.first, i.second);
}
obj.pushKV("bytessent_per_msg", sendPerMsgCmd);

UniValue recvPerMsgCmd(UniValue::VOBJ);
for (const mapMsgCmdSize::value_type &i : stats.mapRecvBytesPerMsgCmd) {
if (i.second > 0)
recvPerMsgCmd.pushKV(i.first, i.second);
}
obj.pushKV("bytesrecv_per_msg", recvPerMsgCmd);

ret.push_back(obj);
}

Expand Down

0 comments on commit 8cee696

Please sign in to comment.