Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: reduce instructions generated by net:message tracepoints #23809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions contrib/tracing/log_p2p_traffic.bt
Expand Up @@ -2,14 +2,15 @@

BEGIN
{
printf("Logging P2P traffic\n")
printf("Logging P2P traffic\n");
}

usdt:./src/bitcoind:net:inbound_message
{
$peer_id = (int64) arg0;
$peer_addr = str(arg1);
$peer_type = str(arg2);
$ct = (uint32)arg2;
$peer_type = ($ct == 0 ? "inbound":($ct == 1 ? "outbound-full-relay":($ct == 2 ? "manual":($ct == 3 ? "feeler":($ct == 4 ? "block-relay":($ct == 5 ? "addr-fetch":"unknown"))))));
$msg_type = str(arg3);
$msg_len = arg4;
printf("inbound '%s' msg from peer %d (%s, %s) with %d bytes\n", $msg_type, $peer_id, $peer_type, $peer_addr, $msg_len);
Expand All @@ -19,7 +20,8 @@ usdt:./src/bitcoind:net:outbound_message
{
$peer_id = (int64) arg0;
$peer_addr = str(arg1);
$peer_type = str(arg2);
$ct = (uint32)arg2;
$peer_type = ($ct == 0 ? "inbound":($ct == 1 ? "outbound-full-relay":($ct == 2 ? "manual":($ct == 3 ? "feeler":($ct == 4 ? "block-relay":($ct == 5 ? "addr-fetch":"unknown"))))));
$msg_type = str(arg3);
$msg_len = arg4;

Expand Down
12 changes: 6 additions & 6 deletions contrib/tracing/log_raw_p2p_msgs.py
Expand Up @@ -33,6 +33,8 @@
import sys
from bcc import BPF, USDT

from net_common import describe_conn_type

# BCC: The C program to be compiled to an eBPF program (by BCC) and loaded into
# a sandboxed Linux kernel VM.
program = """
Expand All @@ -46,15 +48,14 @@

// Tor v3 addresses are 62 chars + 6 chars for the port (':12345').
#define MAX_PEER_ADDR_LENGTH 62 + 6
#define MAX_PEER_CONN_TYPE_LENGTH 20
#define MAX_MSG_TYPE_LENGTH 20
#define MAX_MSG_DATA_LENGTH PCPU_MIN_UNIT_SIZE - 200

struct p2p_message
{
u64 peer_id;
char peer_addr[MAX_PEER_ADDR_LENGTH];
char peer_conn_type[MAX_PEER_CONN_TYPE_LENGTH];
u32 peer_conn_type;
char msg_type[MAX_MSG_TYPE_LENGTH];
u64 msg_size;
u8 msg[MAX_MSG_DATA_LENGTH];
Expand Down Expand Up @@ -82,7 +83,7 @@

bpf_usdt_readarg(1, ctx, &msg->peer_id);
bpf_usdt_readarg_p(2, ctx, &msg->peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg->peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg(3, ctx, &msg->peer_conn_type);
bpf_usdt_readarg_p(4, ctx, &msg->msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(5, ctx, &msg->msg_size);
bpf_usdt_readarg_p(6, ctx, &msg->msg, MIN(msg->msg_size, MAX_MSG_DATA_LENGTH));
Expand All @@ -102,7 +103,7 @@

bpf_usdt_readarg(1, ctx, &msg->peer_id);
bpf_usdt_readarg_p(2, ctx, &msg->peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg->peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg(3, ctx, &msg->peer_conn_type);
bpf_usdt_readarg_p(4, ctx, &msg->msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(5, ctx, &msg->msg_size);
bpf_usdt_readarg_p(6, ctx, &msg->msg, MIN(msg->msg_size, MAX_MSG_DATA_LENGTH));
Expand All @@ -112,7 +113,6 @@
};
"""


def print_message(event, inbound):
print(f"%s %s msg '%s' from peer %d (%s, %s) with %d bytes: %s" %
(
Expand All @@ -121,7 +121,7 @@ def print_message(event, inbound):
"inbound" if inbound else "outbound",
event.msg_type.decode("utf-8"),
event.peer_id,
event.peer_conn_type.decode("utf-8"),
describe_conn_type(event.peer_conn_type),
event.peer_addr.decode("utf-8"),
event.msg_size,
bytes(event.msg[:event.msg_size]).hex(),
Expand Down
15 changes: 15 additions & 0 deletions contrib/tracing/net_common.py
@@ -0,0 +1,15 @@

# see ConnectionType enum in net.h
CONN_TYPES = [
'inbound',
'outbound-full-relay',
'manual',
'feeler',
'block-relay',
'addr-fetch'
]

def describe_conn_type(conn_type):
if conn_type < len(CONN_TYPES):
return CONN_TYPES[conn_type]
return 'unknown'
14 changes: 7 additions & 7 deletions contrib/tracing/p2p_monitor.py
Expand Up @@ -16,6 +16,8 @@
from curses import wrapper, panel
from bcc import BPF, USDT

from net_common import describe_conn_type

# BCC: The C program to be compiled to an eBPF program (by BCC) and loaded into
# a sandboxed Linux kernel VM.
program = """
Expand All @@ -24,14 +26,13 @@
// Tor v3 addresses are 62 chars + 6 chars for the port (':12345').
// I2P addresses are 60 chars + 6 chars for the port (':12345').
#define MAX_PEER_ADDR_LENGTH 62 + 6
#define MAX_PEER_CONN_TYPE_LENGTH 20
#define MAX_MSG_TYPE_LENGTH 20

struct p2p_message
{
u64 peer_id;
char peer_addr[MAX_PEER_ADDR_LENGTH];
char peer_conn_type[MAX_PEER_CONN_TYPE_LENGTH];
u32 peer_conn_type;
char msg_type[MAX_MSG_TYPE_LENGTH];
u64 msg_size;
};
Expand All @@ -46,7 +47,7 @@

bpf_usdt_readarg(1, ctx, &msg.peer_id);
bpf_usdt_readarg_p(2, ctx, &msg.peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg.peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg(3, ctx, &msg.peer_conn_type);
bpf_usdt_readarg_p(4, ctx, &msg.msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(5, ctx, &msg.msg_size);

Expand All @@ -59,7 +60,7 @@

bpf_usdt_readarg(1, ctx, &msg.peer_id);
bpf_usdt_readarg_p(2, ctx, &msg.peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg.peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg(3, ctx, &msg.peer_conn_type);
bpf_usdt_readarg_p(4, ctx, &msg.msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(5, ctx, &msg.msg_size);

Expand All @@ -68,7 +69,6 @@
};
"""


class Message:
""" A P2P network message. """
msg_type = ""
Expand Down Expand Up @@ -132,7 +132,7 @@ def handle_inbound(_, data, size):
event = bpf["inbound_messages"].event(data)
if event.peer_id not in peers:
peer = Peer(event.peer_id, event.peer_addr.decode(
"utf-8"), event.peer_conn_type.decode("utf-8"))
"utf-8"), describe_conn_type(event.peer_conn_type))
peers[peer.id] = peer
peers[event.peer_id].add_message(
Message(event.msg_type.decode("utf-8"), event.msg_size, True))
Expand All @@ -145,7 +145,7 @@ def handle_outbound(_, data, size):
event = bpf["outbound_messages"].event(data)
if event.peer_id not in peers:
peer = Peer(event.peer_id, event.peer_addr.decode(
"utf-8"), event.peer_conn_type.decode("utf-8"))
"utf-8"), describe_conn_type(event.peer_conn_type))
peers[peer.id] = peer
peers[event.peer_id].add_message(
Message(event.msg_type.decode("utf-8"), event.msg_size, False))
Expand Down
6 changes: 3 additions & 3 deletions doc/tracing.md
Expand Up @@ -63,7 +63,7 @@ information about our peer, the connection and the message as arguments.
Arguments passed:
1. Peer ID as `int64`
2. Peer Address and Port (IPv4, IPv6, Tor v3, I2P, ...) as `pointer to C-style String` (max. length 68 characters)
3. Connection Type (inbound, feeler, outbound-full-relay, ...) as `pointer to C-style String` (max. length 20 characters)
3. Connection Type (`0` inbound, `1` outbound-full-relay, `2` manual, `3` feeler, `4` block-relay, `5` addr-fetch) as `uint32`
4. Message Type (inv, ping, getdata, addrv2, ...) as `pointer to C-style String` (max. length 20 characters)
5. Message Size in bytes as `uint64`
6. Message Bytes as `pointer to unsigned chars` (i.e. bytes)
Expand All @@ -82,7 +82,7 @@ information about our peer, the connection and the message as arguments.
Arguments passed:
1. Peer ID as `int64`
2. Peer Address and Port (IPv4, IPv6, Tor v3, I2P, ...) as `pointer to C-style String` (max. length 68 characters)
3. Connection Type (inbound, feeler, outbound-full-relay, ...) as `pointer to C-style String` (max. length 20 characters)
3. Connection Type (`0` inbound, `1` outbound-full-relay, `2` manual, `3` feeler, `4` block-relay, `5` addr-fetch) as `uint32`
4. Message Type (inv, ping, getdata, addrv2, ...) as `pointer to C-style String` (max. length 20 characters)
5. Message Size in bytes as `uint64`
6. Message Bytes as `pointer to unsigned chars` (i.e. bytes)
Expand Down Expand Up @@ -190,7 +190,7 @@ For example:
TRACE6(net, inbound_message,
pnode->GetId(),
pnode->m_addr_name.c_str(),
pnode->ConnectionTypeAsString().c_str(),
pnode->GetConnectionType(),
sanitizedType.c_str(),
msg.data.size(),
msg.data.data()
Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Expand Up @@ -3029,7 +3029,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
TRACE6(net, outbound_message,
pnode->GetId(),
pnode->m_addr_name.c_str(),
pnode->ConnectionTypeAsString().c_str(),
(uint32_t)pnode->GetConnectionType(),
msg.m_type.c_str(),
msg.data.size(),
msg.data.data()
Expand Down
2 changes: 2 additions & 0 deletions src/net.h
Expand Up @@ -660,6 +660,8 @@ class CNode

std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }

ConnectionType GetConnectionType() const { return m_conn_type; }

/** A ping-pong round trip has completed successfully. Update latest and minimum ping times. */
void PongReceived(std::chrono::microseconds ping_time) {
m_last_ping_time = ping_time;
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Expand Up @@ -4146,7 +4146,7 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
TRACE6(net, inbound_message,
pfrom->GetId(),
pfrom->m_addr_name.c_str(),
pfrom->ConnectionTypeAsString().c_str(),
(uint32_t)pfrom->GetConnectionType(),
msg.m_command.c_str(),
msg.m_recv.size(),
msg.m_recv.data()
Expand Down