Skip to content

Commit 68c1ef4

Browse files
committed
tracing: add inbound connection eviction tracepoint
1 parent 4d61d52 commit 68c1ef4

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

doc/tracing.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ Arguments passed:
117117
4. Network of the peer as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`.
118118
5. Number of existing outbound connections as `uint64` including the newly opened outbound connection.
119119

120+
#### Tracepoint `net:evicted_inbound_connection`
121+
122+
Is called when a inbound connection is evicted by us. Passes information about the evicted peer and the time at connection establishment.
123+
124+
Arguments passed:
125+
1. Peer ID as `int64`
126+
2. Peer address and port (IPv4, IPv6, Tor v3, I2P, ...) as `pointer to C-style String` (max. length 68 characters)
127+
3. Connection Type (inbound, feeler, outbound-full-relay, ...) as `pointer to C-style String` (max. length 20 characters)
128+
4. Network the peer connects from as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`.
129+
5. Connection established UNIX epoch timestamp in seconds as `uint64`.
130+
120131
### Context `validation`
121132

122133
#### Tracepoint `validation:block_connected`

src/net.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <optional>
5454
#include <unordered_map>
5555

56+
TRACEPOINT_SEMAPHORE(net, evicted_inbound_connection);
5657
TRACEPOINT_SEMAPHORE(net, inbound_connection);
5758
TRACEPOINT_SEMAPHORE(net, outbound_connection);
5859
TRACEPOINT_SEMAPHORE(net, outbound_message);
@@ -1710,6 +1711,12 @@ bool CConnman::AttemptToEvictConnection()
17101711
for (CNode* pnode : m_nodes) {
17111712
if (pnode->GetId() == *node_id_to_evict) {
17121713
LogDebug(BCLog::NET, "selected %s connection for eviction, %s", pnode->ConnectionTypeAsString(), pnode->DisconnectMsg(fLogIPs));
1714+
TRACEPOINT(net, evicted_inbound_connection,
1715+
pnode->GetId(),
1716+
pnode->m_addr_name.c_str(),
1717+
pnode->ConnectionTypeAsString().c_str(),
1718+
pnode->ConnectedThroughNetwork(),
1719+
Ticks<std::chrono::seconds>(pnode->m_connected));
17131720
pnode->fDisconnect = true;
17141721
return true;
17151722
}

test/functional/interface_usdt_net.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
# from net_address.h
3131
NETWORK_TYPE_UNROUTABLE = 0
32+
# Use in -maxconnections. Results in a maximum of 21 inbound connections
33+
MAX_CONNECTIONS = 32
34+
MAX_INBOUND_CONNECTIONS = MAX_CONNECTIONS - 10 - 1 # 10 outbound and 1 feeler
3235

3336
net_tracepoints_program = """
3437
#include <uapi/linux/ptrace.h>
@@ -70,6 +73,12 @@
7073
u64 existing;
7174
};
7275
76+
struct ClosedConnection
77+
{
78+
struct Connection conn;
79+
u64 time_established;
80+
};
81+
7382
BPF_PERF_OUTPUT(inbound_messages);
7483
int trace_inbound_message(struct pt_regs *ctx) {
7584
struct p2p_message msg = {};
@@ -126,6 +135,21 @@
126135
return 0;
127136
};
128137
138+
BPF_PERF_OUTPUT(evicted_inbound_connections);
139+
int trace_evicted_inbound_connection(struct pt_regs *ctx) {
140+
void *conn_type_pointer = NULL, *address_pointer = NULL;
141+
void* address_pointer;
142+
bpf_usdt_readarg(1, ctx, &evicted.conn.id);
143+
bpf_usdt_readarg(2, ctx, &address_pointer);
144+
bpf_usdt_readarg(3, ctx, &conn_type_pointer);
145+
bpf_usdt_readarg(4, ctx, &evicted.conn.network);
146+
bpf_usdt_readarg(5, ctx, &evicted.time_established);
147+
bpf_probe_read_user_str(&evicted.conn.addr, sizeof(evicted.conn.addr), address_pointer);
148+
bpf_probe_read_user_str(&evicted.conn.type, sizeof(evicted.conn.type), conn_type_pointer);
149+
evicted_inbound_connections.perf_submit(ctx, &evicted, sizeof(evicted));
150+
return 0;
151+
};
152+
129153
"""
130154

131155

@@ -150,9 +174,19 @@ class NewConnection(ctypes.Structure):
150174
def __repr__(self):
151175
return f"NewConnection(conn={self.conn}, existing={self.existing})"
152176

177+
class ClosedConnection(ctypes.Structure):
178+
_fields_ = [
179+
("conn", Connection),
180+
("time_established", ctypes.c_uint64),
181+
]
182+
183+
def __repr__(self):
184+
return f"ClosedConnection(conn={self.conn}, time_established={self.time_established})"
185+
153186
class NetTracepointTest(BitcoinTestFramework):
154187
def set_test_params(self):
155188
self.num_nodes = 1
189+
self.extra_args = [[f'-maxconnections={MAX_CONNECTIONS}']]
156190

157191
def skip_test_if_missing_module(self):
158192
self.skip_if_platform_not_linux()
@@ -164,6 +198,7 @@ def run_test(self):
164198
self.p2p_message_tracepoint_test()
165199
self.inbound_conn_tracepoint_test()
166200
self.outbound_conn_tracepoint_test()
201+
self.evicted_inbound_conn_tracepoint_test()
167202

168203
def p2p_message_tracepoint_test(self):
169204
# Tests the net:inbound_message and net:outbound_message tracepoints
@@ -320,5 +355,42 @@ def handle_outbound_connection(_, data, __):
320355
for node in testnodes:
321356
node.peer_disconnect()
322357

358+
def evicted_inbound_conn_tracepoint_test(self):
359+
self.log.info("hook into the net:evicted_inbound_connection tracepoint")
360+
ctx = USDT(pid=self.nodes[0].process.pid)
361+
ctx.enable_probe(probe="net:evicted_inbound_connection",
362+
fn_name="trace_evicted_inbound_connection")
363+
bpf = BPF(text=net_tracepoints_program, usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
364+
365+
EXPECTED_EVICTED_CONNECTIONS = 2
366+
evicted_connections = []
367+
368+
def handle_evicted_inbound_connection(_, data, __):
369+
event = ctypes.cast(data, ctypes.POINTER(ClosedConnection)).contents
370+
self.log.info(f"handle_evicted_inbound_connection(): {event}")
371+
evicted_connections.append(event)
372+
373+
bpf["evicted_inbound_connections"].open_perf_buffer(handle_evicted_inbound_connection)
374+
375+
self.log.info(
376+
f"connect {MAX_INBOUND_CONNECTIONS + EXPECTED_EVICTED_CONNECTIONS} P2P test nodes to our bitcoind node and expect {EXPECTED_EVICTED_CONNECTIONS} evictions")
377+
testnodes = list()
378+
for p2p_idx in range(MAX_INBOUND_CONNECTIONS + EXPECTED_EVICTED_CONNECTIONS):
379+
testnode = P2PInterface()
380+
self.nodes[0].add_p2p_connection(testnode)
381+
testnodes.append(testnode)
382+
bpf.perf_buffer_poll(timeout=200)
383+
384+
assert_equal(EXPECTED_EVICTED_CONNECTIONS, len(evicted_connections))
385+
for evicted_connection in evicted_connections:
386+
assert evicted_connection.conn.id > 0
387+
assert evicted_connection.time_established > 0
388+
assert_equal("inbound", evicted_connection.conn.conn_type.decode('utf-8'))
389+
assert_equal(NETWORK_TYPE_UNROUTABLE, evicted_connection.conn.network)
390+
391+
bpf.cleanup()
392+
for node in testnodes:
393+
node.peer_disconnect()
394+
323395
if __name__ == '__main__':
324396
NetTracepointTest(__file__).main()

0 commit comments

Comments
 (0)