From 4d61d52f4387622701cdad4bb0fb115127021106 Mon Sep 17 00:00:00 2001 From: 0xb10c <0xb10c@gmail.com> Date: Mon, 2 May 2022 16:27:13 +0200 Subject: [PATCH] tracing: add outbound connection tracepoint --- doc/tracing.md | 12 ++++++ src/net.cpp | 8 ++++ test/functional/interface_usdt_net.py | 57 +++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/doc/tracing.md b/doc/tracing.md index 80edea1e646..a0720b68cf4 100644 --- a/doc/tracing.md +++ b/doc/tracing.md @@ -105,6 +105,18 @@ Arguments passed: 4. Network the peer connects from as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`. 5. Number of existing inbound connections as `uint64` including the newly opened inbound connection. +#### Tracepoint `net:outbound_connection` + +Is called when a new outbound connection is opened by us. Passes information about +the peer and the number of outbound connections including the newly opened connection. + +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) +4. Network of the peer as `uint32` (1 = IPv4, 2 = IPv6, 3 = Onion, 4 = I2P, 5 = CJDNS). See `Network` enum in `netaddress.h`. +5. Number of existing outbound connections as `uint64` including the newly opened outbound connection. + ### Context `validation` #### Tracepoint `validation:block_connected` diff --git a/src/net.cpp b/src/net.cpp index d2056412c9f..623901d9593 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -54,6 +54,7 @@ #include TRACEPOINT_SEMAPHORE(net, inbound_connection); +TRACEPOINT_SEMAPHORE(net, outbound_connection); TRACEPOINT_SEMAPHORE(net, outbound_message); /** Maximum number of block-relay-only anchor connections */ @@ -3002,6 +3003,13 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai // update connection count by network if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()]; } + + TRACEPOINT(net, outbound_connection, + pnode->GetId(), + pnode->m_addr_name.c_str(), + pnode->ConnectionTypeAsString().c_str(), + pnode->ConnectedThroughNetwork(), + GetNodeCount(ConnectionDirection::Out)); } Mutex NetEventsInterface::g_msgproc_mutex; diff --git a/test/functional/interface_usdt_net.py b/test/functional/interface_usdt_net.py index 6849a210f08..f4f77ab25bc 100755 --- a/test/functional/interface_usdt_net.py +++ b/test/functional/interface_usdt_net.py @@ -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()