Log number of inbound/outbound ipv4/6 conns as part of heartbeat

Closes #29113
This commit is contained in:
Nick Mathewson 2020-08-07 10:45:29 -04:00
parent 6fde1548f2
commit 24c721de37
5 changed files with 65 additions and 5 deletions

3
changes/ticket29113 Normal file
View File

@ -0,0 +1,3 @@
o Minor features (heartbeat):
- Include the total number of inbound and outbound IPv4 and IPv6
connections in the heartbeat message . Closes ticket 29113.

View File

@ -82,6 +82,7 @@
#include "core/or/policies.h" #include "core/or/policies.h"
#include "core/or/reasons.h" #include "core/or/reasons.h"
#include "core/or/relay.h" #include "core/or/relay.h"
#include "core/or/status.h"
#include "core/or/crypt_path.h" #include "core/or/crypt_path.h"
#include "core/proto/proto_haproxy.h" #include "core/proto/proto_haproxy.h"
#include "core/proto/proto_http.h" #include "core/proto/proto_http.h"
@ -2042,6 +2043,9 @@ connection_handle_listener_read(connection_t *conn, int new_type)
connection_mark_for_close(newconn); connection_mark_for_close(newconn);
return 0; return 0;
} }
note_connection(true /* inbound */, conn->socket_family);
return 0; return 0;
} }
@ -2213,6 +2217,8 @@ connection_connect_sockaddr,(connection_t *conn,
} }
} }
note_connection(false /* outbound */, conn->socket_family);
/* it succeeded. we're connected. */ /* it succeeded. we're connected. */
log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET, log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
"Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").", "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",

View File

@ -113,6 +113,41 @@ log_onion_service_stats(void)
hs_stats_get_n_rendezvous_launches()); hs_stats_get_n_rendezvous_launches());
} }
/**
* @name connection counts for heartbeat
*
* Tracks incoming and outgoing connections on IPv4/IPv6, for heartbeat
* logs.
**/
/**@{*/
static unsigned n_incoming_ipv4;
static unsigned n_incoming_ipv6;
static unsigned n_outgoing_ipv4;
static unsigned n_outgoing_ipv6;
/**@}*/
/**
* Note that a connection has arrived or has been made, for use in the
* heartbeat message.
**/
void
note_connection(bool inbound, int family)
{
if (family == AF_INET) {
if (inbound) {
++n_incoming_ipv4;
} else {
++n_outgoing_ipv4;
}
} else if (family == AF_INET6) {
if (inbound) {
++n_incoming_ipv6;
} else {
++n_outgoing_ipv6;
}
}
}
/** Log a "heartbeat" message describing Tor's status and history so that the /** Log a "heartbeat" message describing Tor's status and history so that the
* user can know that there is indeed a running Tor. Return 0 on success and * user can know that there is indeed a running Tor. Return 0 on success and
* -1 on failure. */ * -1 on failure. */
@ -143,8 +178,12 @@ log_heartbeat(time_t now)
bw_sent = bytes_to_usage(get_bytes_written()); bw_sent = bytes_to_usage(get_bytes_written());
log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d " log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
"circuits open. I've sent %s and received %s.%s", "circuits open. I've sent %s and received %s. I've received %u "
"connections on IPv4 and %u on IPv6. I've made %u connections "
"with IPv4 and %u with IPv6.%s",
uptime, count_circuits(), bw_sent, bw_rcvd, uptime, count_circuits(), bw_sent, bw_rcvd,
n_incoming_ipv4, n_incoming_ipv6,
n_outgoing_ipv4, n_outgoing_ipv6,
hibernating?" We are currently hibernating.":""); hibernating?" We are currently hibernating.":"");
dirclient_dump_total_dls(); dirclient_dump_total_dls();

View File

@ -11,6 +11,7 @@
#include "lib/testsupport/testsupport.h" #include "lib/testsupport/testsupport.h"
void note_connection(bool inbound, int family);
int log_heartbeat(time_t now); int log_heartbeat(time_t now);
#ifdef STATUS_PRIVATE #ifdef STATUS_PRIVATE

View File

@ -347,7 +347,9 @@ test_status_hb_not_in_consensus(void *arg)
"not in the cached consensus.\n"); "not in the cached consensus.\n");
expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, " expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
"with 0 circuits open. " "with 0 circuits open. "
"I've sent 0 kB and received 0 kB.\n"); "I've sent 0 kB and received 0 kB. "
"I've received 0 connections on IPv4 and 0 on IPv6. "
"I've made 0 connections with IPv4 and 0 with IPv6.\n");
expect_log_msg("Average packaged cell fullness: 100.000%. " expect_log_msg("Average packaged cell fullness: 100.000%. "
"TLS write overhead: 0%\n"); "TLS write overhead: 0%\n");
expect_log_msg("Circuit handshake stats since last time: 1/1 TAP, " expect_log_msg("Circuit handshake stats since last time: 1/1 TAP, "
@ -462,6 +464,8 @@ test_status_hb_simple(void *arg)
expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, " expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
"with 0 circuits open. " "with 0 circuits open. "
"I've sent 0 kB and received 0 kB. " "I've sent 0 kB and received 0 kB. "
"I've received 0 connections on IPv4 and 0 on IPv6. "
"I've made 0 connections with IPv4 and 0 with IPv6. "
"We are currently hibernating.\n"); "We are currently hibernating.\n");
done: done:
@ -581,7 +585,10 @@ test_status_hb_calls_log_accounting(void *arg)
expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, " expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
"with 0 circuits open. " "with 0 circuits open. "
"I've sent 0 kB and received 0 kB.\n"); "I've sent 0 kB and received 0 kB. "
"I've received 0 connections on IPv4 and 0 on IPv6. "
"I've made 0 connections with IPv4 and 0 with IPv6.\n");
expect_log_msg_containing("Heartbeat: Accounting enabled. Sent: 0 kB, " expect_log_msg_containing("Heartbeat: Accounting enabled. Sent: 0 kB, "
"Received: 0 kB, Used: 0 kB / 0 kB, Rule: max. " "Received: 0 kB, Used: 0 kB / 0 kB, Rule: max. "
"The current accounting interval ends on "); "The current accounting interval ends on ");
@ -727,7 +734,9 @@ test_status_hb_packaged_cell_fullness(void *arg)
tt_int_op(actual, OP_EQ, expected); tt_int_op(actual, OP_EQ, expected);
expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, " expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
"with 0 circuits open. " "with 0 circuits open. "
"I've sent 0 kB and received 0 kB.\n"); "I've sent 0 kB and received 0 kB. "
"I've received 0 connections on IPv4 and 0 on IPv6. "
"I've made 0 connections with IPv4 and 0 with IPv6.\n");
expect_log_msg("Average packaged cell fullness: 50.000%. " expect_log_msg("Average packaged cell fullness: 50.000%. "
"TLS write overhead: 0%\n"); "TLS write overhead: 0%\n");
@ -850,7 +859,9 @@ test_status_hb_tls_write_overhead(void *arg)
tt_int_op(actual, OP_EQ, expected); tt_int_op(actual, OP_EQ, expected);
expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, " expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
"with 0 circuits open. " "with 0 circuits open. "
"I've sent 0 kB and received 0 kB.\n"); "I've sent 0 kB and received 0 kB. "
"I've received 0 connections on IPv4 and 0 on IPv6. "
"I've made 0 connections with IPv4 and 0 with IPv6.\n");
expect_log_msg("Average packaged cell fullness: 100.000%. " expect_log_msg("Average packaged cell fullness: 100.000%. "
"TLS write overhead: 100%\n"); "TLS write overhead: 100%\n");