]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Fix: bw cache entry spikes (Issue: #31524)
authorexcurso <w.zimpel@dev.utilizer.de>
Thu, 27 Mar 2025 14:51:16 +0000 (14:51 +0000)
committerDavid Goulet <dgoulet@torproject.org>
Thu, 27 Mar 2025 14:51:16 +0000 (14:51 +0000)
changes/ticket31524 [new file with mode: 0644]
src/feature/control/control.c
src/feature/control/control_auth.c
src/feature/control/control_auth.h
src/feature/control/control_events.c
src/feature/control/control_events.h

diff --git a/changes/ticket31524 b/changes/ticket31524
new file mode 100644 (file)
index 0000000..b629fce
--- /dev/null
@@ -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.
index ac373578181d208ba8563119550fff36e2e2837f..619bd1d725d1d5f0c704d0b142db0bc409211bea 100644 (file)
@@ -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
index 2af6517493e1b8f363bfde65d197bf6f25813631..07dcf4417e0d4eb00fa507cb1d8f332854a76da5 100644 (file)
@@ -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"
 
 #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);
 }
index 3d2d300b5a16d54bf70fd42b61b8fc2416491a79..396ca2661ffb7fad91f46fcd70c1e7d792b80f4f 100644 (file)
@@ -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;
 
index 4c8cf9a425dc4f57a7d420e4f440e2f82d826db7..245df459ea93e46494e33935af3844c5e9a47f26 100644 (file)
@@ -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
index 901d2701cf8b242f2d8fc85166b3dce0be1011db..f1c68f81b1e819176379c7e3f3fff8bef882fea3 100644 (file)
@@ -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