0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-21 12:22:50 -05:00

tracing: add outbound connection tracepoint

This commit is contained in:
0xb10c 2022-05-02 16:27:13 +02:00 committed by 0xb10c
parent 85b2603eec
commit 4d61d52f43
No known key found for this signature in database
GPG key ID: E2FFD5B1D88CA97D
3 changed files with 77 additions and 0 deletions

View file

@ -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`. 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. 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` ### Context `validation`
#### Tracepoint `validation:block_connected` #### Tracepoint `validation:block_connected`

View file

@ -54,6 +54,7 @@
#include <unordered_map> #include <unordered_map>
TRACEPOINT_SEMAPHORE(net, inbound_connection); TRACEPOINT_SEMAPHORE(net, inbound_connection);
TRACEPOINT_SEMAPHORE(net, outbound_connection);
TRACEPOINT_SEMAPHORE(net, outbound_message); TRACEPOINT_SEMAPHORE(net, outbound_message);
/** Maximum number of block-relay-only anchor connections */ /** 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 // update connection count by network
if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()]; 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; Mutex NetEventsInterface::g_msgproc_mutex;

View file

@ -111,6 +111,21 @@ int trace_inbound_connection(struct pt_regs *ctx) {
return 0; 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): def run_test(self):
self.p2p_message_tracepoint_test() self.p2p_message_tracepoint_test()
self.inbound_conn_tracepoint_test() self.inbound_conn_tracepoint_test()
self.outbound_conn_tracepoint_test()
def p2p_message_tracepoint_test(self): def p2p_message_tracepoint_test(self):
# Tests the net:inbound_message and net:outbound_message tracepoints # Tests the net:inbound_message and net:outbound_message tracepoints
@ -263,5 +279,46 @@ class NetTracepointTest(BitcoinTestFramework):
for node in testnodes: for node in testnodes:
node.peer_disconnect() 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__': if __name__ == '__main__':
NetTracepointTest(__file__).main() NetTracepointTest(__file__).main()