Skip to content

Commit b2ad6ed

Browse files
committed
tracing: add misbehaving conn tracepoint
1 parent 68c1ef4 commit b2ad6ed

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

doc/tracing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ Arguments passed:
128128
4. Network the peer connects from as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`.
129129
5. Connection established UNIX epoch timestamp in seconds as `uint64`.
130130

131+
#### Tracepoint `net:misbehaving_connection`
132+
133+
Is called when a connection is misbehaving. Passes the peer id and a
134+
reason for the peers misbehavior.
135+
136+
Arguments passed:
137+
1. Peer ID as `int64`.
138+
2. Reason why the peer is misbehaving as `pointer to C-style String` (max. length 128 characters).
139+
131140
### Context `validation`
132141

133142
#### Tracepoint `validation:block_connected`

src/net_processing.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
using namespace util::hex_literals;
5959

6060
TRACEPOINT_SEMAPHORE(net, inbound_message);
61+
TRACEPOINT_SEMAPHORE(net, misbehaving_connection);
6162

6263
/** Headers download timeout.
6364
* Timeout = base + per_header * (expected number of headers) */
@@ -1752,6 +1753,10 @@ void PeerManagerImpl::Misbehaving(Peer& peer, const std::string& message)
17521753
const std::string message_prefixed = message.empty() ? "" : (": " + message);
17531754
peer.m_should_discourage = true;
17541755
LogDebug(BCLog::NET, "Misbehaving: peer=%d%s\n", peer.m_id, message_prefixed);
1756+
TRACEPOINT(net, misbehaving_connection,
1757+
peer.m_id,
1758+
message.c_str()
1759+
);
17551760
}
17561761

17571762
void PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,

test/functional/interface_usdt_net.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from bcc import BPF, USDT # type: ignore[import]
1515
except ImportError:
1616
pass
17-
from test_framework.messages import msg_version
17+
from test_framework.messages import CBlockHeader, MAX_HEADERS_RESULTS, msg_headers, msg_version
1818
from test_framework.p2p import P2PInterface
1919
from test_framework.test_framework import BitcoinTestFramework
2020
from test_framework.util import assert_equal
@@ -23,6 +23,7 @@
2323
MAX_PEER_ADDR_LENGTH = 68
2424
MAX_PEER_CONN_TYPE_LENGTH = 20
2525
MAX_MSG_TYPE_LENGTH = 20
26+
MAX_MISBEHAVING_MESSAGE_LENGTH = 128
2627
# We won't process messages larger than 150 byte in this test. For reading
2728
# larger messanges see contrib/tracing/log_raw_p2p_msgs.py
2829
MAX_MSG_DATA_LENGTH = 150
@@ -40,11 +41,13 @@
4041
#define MAX_PEER_CONN_TYPE_LENGTH {}
4142
#define MAX_MSG_TYPE_LENGTH {}
4243
#define MAX_MSG_DATA_LENGTH {}
44+
#define MAX_MISBEHAVING_MESSAGE_LENGTH {}
4345
""".format(
4446
MAX_PEER_ADDR_LENGTH,
4547
MAX_PEER_CONN_TYPE_LENGTH,
4648
MAX_MSG_TYPE_LENGTH,
47-
MAX_MSG_DATA_LENGTH
49+
MAX_MSG_DATA_LENGTH,
50+
MAX_MISBEHAVING_MESSAGE_LENGTH,
4851
) + """
4952
// A min() macro. Prefixed with _TRACEPOINT_TEST to avoid collision with other MIN macros.
5053
#define _TRACEPOINT_TEST_MIN(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
@@ -79,6 +82,12 @@
7982
u64 time_established;
8083
};
8184
85+
struct MisbehavingConnection
86+
{
87+
u64 id;
88+
char message[MAX_MISBEHAVING_MESSAGE_LENGTH];
89+
};
90+
8291
BPF_PERF_OUTPUT(inbound_messages);
8392
int trace_inbound_message(struct pt_regs *ctx) {
8493
struct p2p_message msg = {};
@@ -150,6 +159,16 @@
150159
return 0;
151160
};
152161
162+
BPF_PERF_OUTPUT(misbehaving_connections);
163+
int trace_misbehaving_connection(struct pt_regs *ctx) {
164+
struct MisbehavingConnection misbehaving = {};
165+
void *message_pointer = NULL;
166+
bpf_usdt_readarg(1, ctx, &misbehaving.id);
167+
bpf_usdt_readarg(2, ctx, &message_pointer);
168+
bpf_probe_read_user_str(&misbehaving.message, sizeof(misbehaving.message), message_pointer);
169+
misbehaving_connections.perf_submit(ctx, &misbehaving, sizeof(misbehaving));
170+
return 0;
171+
};
153172
"""
154173

155174

@@ -183,6 +202,17 @@ class ClosedConnection(ctypes.Structure):
183202
def __repr__(self):
184203
return f"ClosedConnection(conn={self.conn}, time_established={self.time_established})"
185204

205+
206+
class MisbehavingConnection(ctypes.Structure):
207+
_fields_ = [
208+
("id", ctypes.c_uint64),
209+
("message", ctypes.c_char * MAX_MISBEHAVING_MESSAGE_LENGTH),
210+
]
211+
212+
def __repr__(self):
213+
return f"MisbehavingConnection(id={self.id}, message={self.message})"
214+
215+
186216
class NetTracepointTest(BitcoinTestFramework):
187217
def set_test_params(self):
188218
self.num_nodes = 1
@@ -199,6 +229,7 @@ def run_test(self):
199229
self.inbound_conn_tracepoint_test()
200230
self.outbound_conn_tracepoint_test()
201231
self.evicted_inbound_conn_tracepoint_test()
232+
self.misbehaving_conn_tracepoint_test()
202233

203234
def p2p_message_tracepoint_test(self):
204235
# Tests the net:inbound_message and net:outbound_message tracepoints
@@ -392,5 +423,39 @@ def handle_evicted_inbound_connection(_, data, __):
392423
for node in testnodes:
393424
node.peer_disconnect()
394425

426+
def misbehaving_conn_tracepoint_test(self):
427+
self.log.info("hook into the net:misbehaving_connection tracepoint")
428+
ctx = USDT(pid=self.nodes[0].process.pid)
429+
ctx.enable_probe(probe="net:misbehaving_connection",
430+
fn_name="trace_misbehaving_connection")
431+
bpf = BPF(text=net_tracepoints_program, usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
432+
433+
EXPECTED_MISBEHAVING_CONNECTIONS = 2
434+
misbehaving_connections = []
435+
436+
def handle_misbehaving_connection(_, data, __):
437+
event = ctypes.cast(data, ctypes.POINTER(MisbehavingConnection)).contents
438+
self.log.info(f"handle_misbehaving_connection(): {event}")
439+
misbehaving_connections.append(event)
440+
441+
bpf["misbehaving_connections"].open_perf_buffer(handle_misbehaving_connection)
442+
443+
self.log.info("connect a misbehaving P2P test nodes to our bitcoind node")
444+
msg = msg_headers([CBlockHeader()] * (MAX_HEADERS_RESULTS + 1))
445+
for _ in range(EXPECTED_MISBEHAVING_CONNECTIONS):
446+
testnode = P2PInterface()
447+
self.nodes[0].add_p2p_connection(testnode)
448+
testnode.send_message(msg)
449+
bpf.perf_buffer_poll(timeout=500)
450+
testnode.peer_disconnect()
451+
452+
assert_equal(EXPECTED_MISBEHAVING_CONNECTIONS, len(misbehaving_connections))
453+
for misbehaving_connection in misbehaving_connections:
454+
assert misbehaving_connection.id > 0
455+
assert len(misbehaving_connection.message) > 0
456+
assert misbehaving_connection.message == b"headers message size = 2001"
457+
458+
bpf.cleanup()
459+
395460
if __name__ == '__main__':
396461
NetTracepointTest(__file__).main()

0 commit comments

Comments
 (0)