Collect IPv6 bidi connection statistics

This commit is contained in:
Nick Mathewson 2020-07-10 09:47:55 -04:00
parent 54141d66e2
commit dbdf8bebde
4 changed files with 47 additions and 25 deletions

View File

@ -3363,11 +3363,11 @@ record_num_bytes_transferred_impl(connection_t *conn,
if (!connection_is_rate_limited(conn)) if (!connection_is_rate_limited(conn))
return; return;
const bool is_ipv6 = (conn->socket_family == AF_INET6);
if (conn->type == CONN_TYPE_OR) if (conn->type == CONN_TYPE_OR)
conn_stats_note_or_conn_bytes(conn->global_identifier, num_read, conn_stats_note_or_conn_bytes(conn->global_identifier, num_read,
num_written, now); num_written, now, is_ipv6);
const bool is_ipv6 = (conn->socket_family == AF_INET6);
if (num_read > 0) { if (num_read > 0) {
bwhist_note_bytes_read(num_read, now, is_ipv6); bwhist_note_bytes_read(num_read, now, is_ipv6);
} }

View File

@ -70,6 +70,8 @@ typedef struct conn_counts_t {
/** A collection of connection counts, over all OR connections. */ /** A collection of connection counts, over all OR connections. */
static conn_counts_t counts; static conn_counts_t counts;
/** A collection of connection counts, over IPv6 OR connections only. */
static conn_counts_t counts_ipv6;
/** Entry in a map from connection ID to the number of read and written /** Entry in a map from connection ID to the number of read and written
* bytes on this connection in a BIDI_INTERVAL second interval. */ * bytes on this connection in a BIDI_INTERVAL second interval. */
@ -78,6 +80,7 @@ typedef struct bidi_map_entry_t {
uint64_t conn_id; /**< Connection ID */ uint64_t conn_id; /**< Connection ID */
size_t read; /**< Number of read bytes */ size_t read; /**< Number of read bytes */
size_t written; /**< Number of written bytes */ size_t written; /**< Number of written bytes */
bool is_ipv6; /**< True if this is an IPv6 connection */
} bidi_map_entry_t; } bidi_map_entry_t;
/** Map of OR connections together with the number of read and written /** Map of OR connections together with the number of read and written
@ -123,6 +126,7 @@ conn_stats_reset(time_t now)
{ {
start_of_conn_stats_interval = now; start_of_conn_stats_interval = now;
memset(&counts, 0, sizeof(counts)); memset(&counts, 0, sizeof(counts));
memset(&counts_ipv6, 0, sizeof(counts_ipv6));
conn_stats_free_all(); conn_stats_free_all();
} }
@ -159,17 +163,18 @@ static void
collect_period_statistics(void) collect_period_statistics(void)
{ {
bidi_map_entry_t **ptr, **next, *ent; bidi_map_entry_t **ptr, **next, *ent;
conn_counts_t *cnt = &counts;
for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) { for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
ent = *ptr; ent = *ptr;
add_entry_to_count(cnt, ent); add_entry_to_count(&counts, ent);
if (ent->is_ipv6)
add_entry_to_count(&counts_ipv6, ent);
next = HT_NEXT_RMV(bidimap, &bidi_map, ptr); next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
tor_free(ent); tor_free(ent);
} }
log_info(LD_GENERAL, "%d below threshold, %d mostly read, " log_info(LD_GENERAL, "%d below threshold, %d mostly read, "
"%d mostly written, %d both read and written.", "%d mostly written, %d both read and written.",
cnt->below_threshold, cnt->mostly_read, cnt->mostly_written, counts.below_threshold, counts.mostly_read, counts.mostly_written,
cnt->both_read_and_written); counts.both_read_and_written);
} }
/** We read <b>num_read</b> bytes and wrote <b>num_written</b> from/to OR /** We read <b>num_read</b> bytes and wrote <b>num_written</b> from/to OR
@ -178,7 +183,8 @@ collect_period_statistics(void)
* for this connection. */ * for this connection. */
void void
conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read, conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
size_t num_written, time_t when) size_t num_written, time_t when,
bool is_ipv6)
{ {
if (!start_of_conn_stats_interval) if (!start_of_conn_stats_interval)
return; return;
@ -199,11 +205,13 @@ conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
if (entry) { if (entry) {
entry->written += num_written; entry->written += num_written;
entry->read += num_read; entry->read += num_read;
entry->is_ipv6 |= is_ipv6;
} else { } else {
entry = tor_malloc_zero(sizeof(bidi_map_entry_t)); entry = tor_malloc_zero(sizeof(bidi_map_entry_t));
entry->conn_id = conn_id; entry->conn_id = conn_id;
entry->written = num_written; entry->written = num_written;
entry->read = num_read; entry->read = num_read;
entry->is_ipv6 = is_ipv6;
HT_INSERT(bidimap, &bidi_map, entry); HT_INSERT(bidimap, &bidi_map, entry);
} }
} }
@ -215,21 +223,30 @@ conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
char * char *
conn_stats_format(time_t now) conn_stats_format(time_t now)
{ {
char *result, written[ISO_TIME_LEN+1]; char *result, written_at[ISO_TIME_LEN+1];
if (!start_of_conn_stats_interval) if (!start_of_conn_stats_interval)
return NULL; /* Not initialized. */ return NULL; /* Not initialized. */
tor_assert(now >= start_of_conn_stats_interval); tor_assert(now >= start_of_conn_stats_interval);
format_iso_time(written, now); format_iso_time(written_at, now);
tor_asprintf(&result, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n", tor_asprintf(&result,
written, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n"
"ipv6-conn-bi-direct %s (%d s) %d,%d,%d,%d\n",
written_at,
(unsigned) (now - start_of_conn_stats_interval), (unsigned) (now - start_of_conn_stats_interval),
counts.below_threshold, counts.below_threshold,
counts.mostly_read, counts.mostly_read,
counts.mostly_written, counts.mostly_written,
counts.both_read_and_written); counts.both_read_and_written,
written_at,
(unsigned) (now - start_of_conn_stats_interval),
counts_ipv6.below_threshold,
counts_ipv6.mostly_read,
counts_ipv6.mostly_written,
counts_ipv6.both_read_and_written);
return result; return result;
} }

View File

@ -14,7 +14,8 @@
void conn_stats_init(time_t now); void conn_stats_init(time_t now);
void conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read, void conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
size_t num_written, time_t when); size_t num_written, time_t when,
bool is_ipv6);
void conn_stats_reset(time_t now); void conn_stats_reset(time_t now);
char *conn_stats_format(time_t now); char *conn_stats_format(time_t now);
time_t conn_stats_save(time_t now); time_t conn_stats_save(time_t now);

View File

@ -112,37 +112,41 @@ test_stats(void *arg)
/* Continue with testing connection statistics; we shouldn't collect /* Continue with testing connection statistics; we shouldn't collect
* conn stats without initializing them. */ * conn stats without initializing them. */
conn_stats_note_or_conn_bytes(1, 20, 400, now); conn_stats_note_or_conn_bytes(1, 20, 400, now, false);
s = conn_stats_format(now + 86400); s = conn_stats_format(now + 86400);
tt_ptr_op(s, OP_EQ, NULL); tt_ptr_op(s, OP_EQ, NULL);
/* Initialize stats, note bytes, and generate history string. */ /* Initialize stats, note bytes, and generate history string. */
conn_stats_init(now); conn_stats_init(now);
conn_stats_note_or_conn_bytes(1, 30000, 400000, now); conn_stats_note_or_conn_bytes(1, 30000, 400000, now, false);
conn_stats_note_or_conn_bytes(1, 30000, 400000, now + 5); conn_stats_note_or_conn_bytes(1, 30000, 400000, now + 5, false);
conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 10); conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 10, true);
conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15); conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15, true);
s = conn_stats_format(now + 86400); s = conn_stats_format(now + 86400);
tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n",OP_EQ, s); tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n"
"ipv6-conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",
OP_EQ, s);
tor_free(s); tor_free(s);
/* Stop collecting stats, add some bytes, and ensure we don't generate /* Stop collecting stats, add some bytes, and ensure we don't generate
* a history string. */ * a history string. */
conn_stats_terminate(); conn_stats_terminate();
conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15); conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15, true);
s = conn_stats_format(now + 86400); s = conn_stats_format(now + 86400);
tt_ptr_op(s, OP_EQ, NULL); tt_ptr_op(s, OP_EQ, NULL);
/* Re-start stats, add some bytes, reset stats, and see what history we /* Re-start stats, add some bytes, reset stats, and see what history we
* get when observing no bytes at all. */ * get when observing no bytes at all. */
conn_stats_init(now); conn_stats_init(now);
conn_stats_note_or_conn_bytes(1, 30000, 400000, now); conn_stats_note_or_conn_bytes(1, 30000, 400000, now, false);
conn_stats_note_or_conn_bytes(1, 30000, 400000, now + 5); conn_stats_note_or_conn_bytes(1, 30000, 400000, now + 5, false);
conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 10); conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 10, true);
conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15); conn_stats_note_or_conn_bytes(2, 400000, 30000, now + 15, true);
conn_stats_reset(now); conn_stats_reset(now);
s = conn_stats_format(now + 86400); s = conn_stats_format(now + 86400);
tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",OP_EQ, s); tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n"
"ipv6-conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",
OP_EQ, s);
tor_free(s); tor_free(s);
/* Continue with testing buffer statistics; we shouldn't collect buffer /* Continue with testing buffer statistics; we shouldn't collect buffer