Skip to content

Commit 4d61d52

Browse files
committed
tracing: add outbound connection tracepoint
1 parent 85b2603 commit 4d61d52

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

doc/tracing.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ Arguments passed:
105105
4. Network the peer connects from as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`.
106106
5. Number of existing inbound connections as `uint64` including the newly opened inbound connection.
107107

108+
#### Tracepoint `net:outbound_connection`
109+
110+
Is called when a new outbound connection is opened by us. Passes information about
111+
the peer and the number of outbound connections including the newly opened connection.
112+
113+
Arguments passed:
114+
1. Peer ID as `int64`
115+
2. Peer address and port (IPv4, IPv6, Tor v3, I2P, ...) as `pointer to C-style String` (max. length 68 characters)
116+
3. Connection Type (inbound, feeler, outbound-full-relay, ...) as `pointer to C-style String` (max. length 20 characters)
117+
4. Network of the peer as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`.
118+
5. Number of existing outbound connections as `uint64` including the newly opened outbound connection.
119+
108120
### Context `validation`
109121

110122
#### Tracepoint `validation:block_connected`

src/net.cpp

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

5656
TRACEPOINT_SEMAPHORE(net, inbound_connection);
57+
TRACEPOINT_SEMAPHORE(net, outbound_connection);
5758
TRACEPOINT_SEMAPHORE(net, outbound_message);
5859

5960
/** Maximum number of block-relay-only anchor connections */
@@ -3002,6 +3003,13 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
30023003
// update connection count by network
30033004
if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()];
30043005
}
3006+
3007+
TRACEPOINT(net, outbound_connection,
3008+
pnode->GetId(),
3009+
pnode->m_addr_name.c_str(),
3010+
pnode->ConnectionTypeAsString().c_str(),
3011+
pnode->ConnectedThroughNetwork(),
3012+
GetNodeCount(ConnectionDirection::Out));
30053013
}
30063014

30073015
Mutex NetEventsInterface::g_msgproc_mutex;

test/functional/interface_usdt_net.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@
111111
return 0;
112112
};
113113
114+
BPF_PERF_OUTPUT(outbound_connections);
115+
int trace_outbound_connection(struct pt_regs *ctx) {
116+
struct NewConnection outbound = {};
117+
void *conn_type_pointer = NULL, *address_pointer = NULL;
118+
bpf_usdt_readarg(1, ctx, &outbound.conn.id);
119+
bpf_usdt_readarg(2, ctx, &address_pointer);
120+
bpf_usdt_readarg(3, ctx, &conn_type_pointer);
121+
bpf_usdt_readarg(4, ctx, &outbound.conn.network);
122+
bpf_usdt_readarg(5, ctx, &outbound.existing);
123+
bpf_probe_read_user_str(&outbound.conn.addr, sizeof(outbound.conn.addr), address_pointer);
124+
bpf_probe_read_user_str(&outbound.conn.type, sizeof(outbound.conn.type), conn_type_pointer);
125+
outbound_connections.perf_submit(ctx, &outbound, sizeof(outbound));
126+
return 0;
127+
};
128+
114129
"""
115130

116131

@@ -148,6 +163,7 @@ def skip_test_if_missing_module(self):
148163
def run_test(self):
149164
self.p2p_message_tracepoint_test()
150165
self.inbound_conn_tracepoint_test()
166+
self.outbound_conn_tracepoint_test()
151167

152168
def p2p_message_tracepoint_test(self):
153169
# Tests the net:inbound_message and net:outbound_message tracepoints
@@ -263,5 +279,46 @@ def handle_inbound_connection(_, data, __):
263279
for node in testnodes:
264280
node.peer_disconnect()
265281

282+
def outbound_conn_tracepoint_test(self):
283+
self.log.info("hook into the net:outbound_connection tracepoint")
284+
ctx = USDT(pid=self.nodes[0].process.pid)
285+
ctx.enable_probe(probe="net:outbound_connection",
286+
fn_name="trace_outbound_connection")
287+
bpf = BPF(text=net_tracepoints_program, usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
288+
289+
# that the handle_* function succeeds.
290+
EXPECTED_OUTBOUND_CONNECTIONS = 2
291+
EXPECTED_CONNECTION_TYPE = "feeler"
292+
outbound_connections = []
293+
294+
def handle_outbound_connection(_, data, __):
295+
event = ctypes.cast(data, ctypes.POINTER(NewConnection)).contents
296+
self.log.info(f"handle_outbound_connection(): {event}")
297+
outbound_connections.append(event)
298+
299+
bpf["outbound_connections"].open_perf_buffer(
300+
handle_outbound_connection)
301+
302+
self.log.info(
303+
f"connect {EXPECTED_OUTBOUND_CONNECTIONS} P2P test nodes to our bitcoind node")
304+
testnodes = list()
305+
for p2p_idx in range(EXPECTED_OUTBOUND_CONNECTIONS):
306+
testnode = P2PInterface()
307+
self.nodes[0].add_outbound_p2p_connection(
308+
testnode, p2p_idx=p2p_idx, connection_type=EXPECTED_CONNECTION_TYPE)
309+
testnodes.append(testnode)
310+
bpf.perf_buffer_poll(timeout=200)
311+
312+
assert_equal(EXPECTED_OUTBOUND_CONNECTIONS, len(outbound_connections))
313+
for outbound_connection in outbound_connections:
314+
assert outbound_connection.conn.id > 0
315+
assert outbound_connection.existing > 0
316+
assert_equal(EXPECTED_CONNECTION_TYPE, outbound_connection.conn.conn_type.decode('utf-8'))
317+
assert_equal(NETWORK_TYPE_UNROUTABLE, outbound_connection.conn.network)
318+
319+
bpf.cleanup()
320+
for node in testnodes:
321+
node.peer_disconnect()
322+
266323
if __name__ == '__main__':
267324
NetTracepointTest(__file__).main()

0 commit comments

Comments
 (0)