mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-26 14:59:16 +02:00
tracing: add outbound connection tracepoint
This commit is contained in:
@@ -111,6 +111,21 @@ int trace_inbound_connection(struct pt_regs *ctx) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
BPF_PERF_OUTPUT(outbound_connections);
|
||||
int trace_outbound_connection(struct pt_regs *ctx) {
|
||||
struct NewConnection outbound = {};
|
||||
void *conn_type_pointer = NULL, *address_pointer = NULL;
|
||||
bpf_usdt_readarg(1, ctx, &outbound.conn.id);
|
||||
bpf_usdt_readarg(2, ctx, &address_pointer);
|
||||
bpf_usdt_readarg(3, ctx, &conn_type_pointer);
|
||||
bpf_usdt_readarg(4, ctx, &outbound.conn.network);
|
||||
bpf_usdt_readarg(5, ctx, &outbound.existing);
|
||||
bpf_probe_read_user_str(&outbound.conn.addr, sizeof(outbound.conn.addr), address_pointer);
|
||||
bpf_probe_read_user_str(&outbound.conn.type, sizeof(outbound.conn.type), conn_type_pointer);
|
||||
outbound_connections.perf_submit(ctx, &outbound, sizeof(outbound));
|
||||
return 0;
|
||||
};
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@@ -148,6 +163,7 @@ class NetTracepointTest(BitcoinTestFramework):
|
||||
def run_test(self):
|
||||
self.p2p_message_tracepoint_test()
|
||||
self.inbound_conn_tracepoint_test()
|
||||
self.outbound_conn_tracepoint_test()
|
||||
|
||||
def p2p_message_tracepoint_test(self):
|
||||
# Tests the net:inbound_message and net:outbound_message tracepoints
|
||||
@@ -263,5 +279,46 @@ class NetTracepointTest(BitcoinTestFramework):
|
||||
for node in testnodes:
|
||||
node.peer_disconnect()
|
||||
|
||||
def outbound_conn_tracepoint_test(self):
|
||||
self.log.info("hook into the net:outbound_connection tracepoint")
|
||||
ctx = USDT(pid=self.nodes[0].process.pid)
|
||||
ctx.enable_probe(probe="net:outbound_connection",
|
||||
fn_name="trace_outbound_connection")
|
||||
bpf = BPF(text=net_tracepoints_program, usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
|
||||
|
||||
# that the handle_* function succeeds.
|
||||
EXPECTED_OUTBOUND_CONNECTIONS = 2
|
||||
EXPECTED_CONNECTION_TYPE = "feeler"
|
||||
outbound_connections = []
|
||||
|
||||
def handle_outbound_connection(_, data, __):
|
||||
event = ctypes.cast(data, ctypes.POINTER(NewConnection)).contents
|
||||
self.log.info(f"handle_outbound_connection(): {event}")
|
||||
outbound_connections.append(event)
|
||||
|
||||
bpf["outbound_connections"].open_perf_buffer(
|
||||
handle_outbound_connection)
|
||||
|
||||
self.log.info(
|
||||
f"connect {EXPECTED_OUTBOUND_CONNECTIONS} P2P test nodes to our bitcoind node")
|
||||
testnodes = list()
|
||||
for p2p_idx in range(EXPECTED_OUTBOUND_CONNECTIONS):
|
||||
testnode = P2PInterface()
|
||||
self.nodes[0].add_outbound_p2p_connection(
|
||||
testnode, p2p_idx=p2p_idx, connection_type=EXPECTED_CONNECTION_TYPE)
|
||||
testnodes.append(testnode)
|
||||
bpf.perf_buffer_poll(timeout=200)
|
||||
|
||||
assert_equal(EXPECTED_OUTBOUND_CONNECTIONS, len(outbound_connections))
|
||||
for outbound_connection in outbound_connections:
|
||||
assert outbound_connection.conn.id > 0
|
||||
assert outbound_connection.existing > 0
|
||||
assert_equal(EXPECTED_CONNECTION_TYPE, outbound_connection.conn.conn_type.decode('utf-8'))
|
||||
assert_equal(NETWORK_TYPE_UNROUTABLE, outbound_connection.conn.network)
|
||||
|
||||
bpf.cleanup()
|
||||
for node in testnodes:
|
||||
node.peer_disconnect()
|
||||
|
||||
if __name__ == '__main__':
|
||||
NetTracepointTest(__file__).main()
|
||||
|
||||
Reference in New Issue
Block a user