tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

commit 71d87706ef9c2fc1248c2b22a40142427843c55b
parent a789ab32f2191f191370f614cccd4f80bfc7e59b
Author: excurso <w.zimpel@dev.utilizer.de>
Date:   Thu, 27 Mar 2025 14:51:16 +0000

Fix: bw cache entry spikes (Issue: #31524)

Diffstat:
Achanges/ticket31524 | 3+++
Msrc/feature/control/control.c | 4+++-
Msrc/feature/control/control_auth.c | 39++++++++++++++++++++++++++++++++++++++-
Msrc/feature/control/control_auth.h | 4+++-
Msrc/feature/control/control_events.c | 17++++++++++++++++-
Msrc/feature/control/control_events.h | 5++++-
6 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/changes/ticket31524 b/changes/ticket31524 @@ -0,0 +1,3 @@ + o Major bugfix (control-events, bw-cache): + - Fixes spikes occurring in bandwidth cache on control connection. + Fixes bug 31524; bugfix on 0.4.8.12-dev. diff --git a/src/feature/control/control.c b/src/feature/control/control.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -252,6 +252,8 @@ connection_control_closed(control_connection_t *conn) if (conn->is_owning_control_connection) { lost_owning_controller("connection", "closed"); } + + control_remove_authenticated_connection(conn); } /** Return true iff <b>cmd</b> is allowable (or at least forgivable) at this diff --git a/src/feature/control/control_auth.c b/src/feature/control/control_auth.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -11,6 +11,7 @@ #include "app/config/config.h" #include "core/mainloop/connection.h" #include "feature/control/control.h" +#include "feature/control/control_events.h" #include "feature/control/control_cmd.h" #include "feature/control/control_auth.h" #include "feature/control/control_cmd_args_st.h" @@ -24,6 +25,36 @@ #include "lib/crypt_ops/crypto_s2k.h" +/* List of authenticated control connections */ +static smartlist_t *control_auth_conns = NULL; + +static void +control_add_authenticated_connection(control_connection_t *conn) +{ + if (!control_auth_conns) + control_auth_conns = smartlist_new(); + + smartlist_add(control_auth_conns, conn); + + if (smartlist_len(control_auth_conns) == 1) + stats_init(); +} + +void +control_remove_authenticated_connection(const control_connection_t *conn) +{ + if (!control_auth_conns) + return; + + smartlist_remove(control_auth_conns, conn); + + if (smartlist_len(control_auth_conns) == 0) { + smartlist_free(control_auth_conns); + control_auth_conns = NULL; + stats_clear(); + } +} + /** If we're using cookie-type authentication, how long should our cookies be? */ #define AUTHENTICATION_COOKIE_LEN 32 @@ -429,6 +460,9 @@ handle_control_authenticate(control_connection_t *conn, SMARTLIST_FOREACH(sl, char *, str, tor_free(str)); smartlist_free(sl); } + + control_add_authenticated_connection(conn); + return 0; } @@ -438,4 +472,7 @@ control_auth_free_all(void) if (authentication_cookie) /* Free the auth cookie */ tor_free(authentication_cookie); authentication_cookie_is_set = 0; + + if (control_auth_conns) + smartlist_free(control_auth_conns); } diff --git a/src/feature/control/control_auth.h b/src/feature/control/control_auth.h @@ -1,7 +1,7 @@ /* Copyright (c) 2001 Matej Pfajfar. * Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -12,6 +12,8 @@ #ifndef TOR_CONTROL_AUTH_H #define TOR_CONTROL_AUTH_H +void control_remove_authenticated_connection(const control_connection_t *conn); + struct control_cmd_args_t; struct control_cmd_syntax_t; diff --git a/src/feature/control/control_events.c b/src/feature/control/control_events.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -1295,6 +1295,21 @@ static struct cached_bw_event_t { uint32_t n_written; } cached_bw_events[N_BW_EVENTS_TO_CACHE]; +void +stats_init(void) +{ + stats_prev_n_read = get_bytes_read(); + stats_prev_n_written = get_bytes_written(); +} + +void +stats_clear(void) +{ + memset(cached_bw_events, 0, sizeof cached_bw_events); + stats_prev_n_read = stats_prev_n_written = 0; + n_measurements = next_measurement_idx = 0; +} + /** A second or more has elapsed: tell any interested control * connections how much bandwidth we used. */ int diff --git a/src/feature/control/control_events.h b/src/feature/control/control_events.h @@ -1,7 +1,7 @@ /* Copyright (c) 2001 Matej Pfajfar. * Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2021, The Tor Project, Inc. */ + * Copyright (c) 2007-2024, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -226,6 +226,9 @@ void cbt_control_event_buildtimeout_set(const circuit_build_times_t *cbt, int control_event_enter_controller_wait(void); +void stats_init(void); +void stats_clear(void); + void control_events_free_all(void); #ifdef CONTROL_MODULE_PRIVATE