[tor-commits] [tor/master] relay: Add the onionskins processing metrics

dgoulet at torproject.org dgoulet at torproject.org
Wed May 12 16:03:06 UTC 2021


commit 8bb1874f1e80f10c1f222db52471a458c4d6d5bc
Author: David Goulet <dgoulet at torproject.org>
Date:   Wed May 5 13:33:33 2021 -0400

    relay: Add the onionskins processing metrics
    
    With this commit, a relay now emits metrics event on the MetricsPort
    related to how many onionskins were handled (processed or dropped) for
    each handshake type.
    
    Related to #40367
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/feature/relay/onion_queue.c   |  1 +
 src/feature/relay/relay_metrics.c | 59 +++++++++++++++++++++++++++++++++++++++
 src/feature/relay/relay_metrics.h |  4 ++-
 src/feature/stats/rephist.c       | 20 +++++++++++++
 src/feature/stats/rephist.h       |  3 ++
 5 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/src/feature/relay/onion_queue.c b/src/feature/relay/onion_queue.c
index 85ec0dc74a..c09f4d5b9b 100644
--- a/src/feature/relay/onion_queue.c
+++ b/src/feature/relay/onion_queue.c
@@ -164,6 +164,7 @@ onion_pending_add(or_circuit_t *circ, create_cell_t *onionskin)
 #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
     static ratelim_t last_warned =
       RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
+    rep_hist_note_circuit_handshake_dropped(onionskin->handshake_type);
     if (onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR) {
       char *m;
       /* Note this ntor onionskin drop as an overload */
diff --git a/src/feature/relay/relay_metrics.c b/src/feature/relay/relay_metrics.c
index 2686249548..6816c25f43 100644
--- a/src/feature/relay/relay_metrics.c
+++ b/src/feature/relay/relay_metrics.c
@@ -19,9 +19,11 @@
 #include "lib/log/util_bug.h"
 
 #include "feature/relay/relay_metrics.h"
+#include "feature/stats/rephist.h"
 
 /** Declarations of each fill function for metrics defined in base_metrics. */
 static void fill_oom_values(void);
+static void fill_onionskins_values(void);
 
 /** The base metrics that is a static array of metrics added to the metrics
  * store.
@@ -36,12 +38,69 @@ static const relay_metrics_entry_t base_metrics[] =
     .help = "Total number of bytes the OOM has freed by subsystem",
     .fill_fn = fill_oom_values,
   },
+  {
+    .key = RELAY_METRICS_NUM_ONIONSKINS,
+    .type = METRICS_TYPE_COUNTER,
+    .name = METRICS_NAME(relay_load_onionskins_total),
+    .help = "Total number of onionskins handled",
+    .fill_fn = fill_onionskins_values,
+  },
 };
 static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics);
 
 /** The only and single store of all the relay metrics. */
 static metrics_store_t *the_store;
 
+/** Helper function to convert an handshake type into a string. */
+static inline const char *
+handshake_type_to_str(const uint16_t type)
+{
+  switch (type) {
+    case ONION_HANDSHAKE_TYPE_TAP:
+      return "tap";
+    case ONION_HANDSHAKE_TYPE_FAST:
+      return "fast";
+    case ONION_HANDSHAKE_TYPE_NTOR:
+      return "ntor";
+    default:
+      // LCOV_EXCL_START
+      tor_assert_unreached();
+      // LCOV_EXCL_STOP
+  }
+}
+
+/** Fill function for the RELAY_METRICS_NUM_ONIONSKINS metrics. */
+static void
+fill_onionskins_values(void)
+{
+  metrics_store_entry_t *sentry;
+  const relay_metrics_entry_t *rentry =
+    &base_metrics[RELAY_METRICS_NUM_ONIONSKINS];
+
+  for (uint16_t t = 0; t <= MAX_ONION_HANDSHAKE_TYPE; t++) {
+    /* Dup the label because metrics_format_label() returns a pointer to a
+     * string on the stack and we need that label for all metrics. */
+    char *type_label =
+      tor_strdup(metrics_format_label("type", handshake_type_to_str(t)));
+    sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                               rentry->help);
+    metrics_store_entry_add_label(sentry, type_label);
+    metrics_store_entry_add_label(sentry,
+                        metrics_format_label("action", "processed"));
+    metrics_store_entry_update(sentry,
+                               rep_hist_get_circuit_handshake_assigned(t));
+
+    sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                               rentry->help);
+    metrics_store_entry_add_label(sentry, type_label);
+    metrics_store_entry_add_label(sentry,
+                        metrics_format_label("action", "dropped"));
+    metrics_store_entry_update(sentry,
+                               rep_hist_get_circuit_handshake_dropped(t));
+    tor_free(type_label);
+  }
+}
+
 /** Fill function for the RELAY_METRICS_NUM_OOM_BYTES metrics. */
 static void
 fill_oom_values(void)
diff --git a/src/feature/relay/relay_metrics.h b/src/feature/relay/relay_metrics.h
index 7d644badd3..6868bd26df 100644
--- a/src/feature/relay/relay_metrics.h
+++ b/src/feature/relay/relay_metrics.h
@@ -16,7 +16,9 @@
  * the base_metrics array. */
 typedef enum {
   /** Number of OOM invocation. */
-  RELAY_METRICS_NUM_OOM_BYTES = 0,
+  RELAY_METRICS_NUM_OOM_BYTES  = 0,
+  /** Number of onionskines handled. */
+  RELAY_METRICS_NUM_ONIONSKINS = 1,
 } relay_metrics_key_t;
 
 /** The metadata of a relay metric. */
diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c
index e25c01331d..98907055ef 100644
--- a/src/feature/stats/rephist.c
+++ b/src/feature/stats/rephist.c
@@ -1790,6 +1790,7 @@ rep_hist_note_desc_served(const char * desc)
  * @{ */
 STATIC int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
 STATIC int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
+STATIC uint64_t onion_handshakes_dropped[MAX_ONION_HANDSHAKE_TYPE+1] = {0};
 /**@}*/
 
 /** A new onionskin (using the <b>type</b> handshake) has arrived. */
@@ -1809,6 +1810,15 @@ rep_hist_note_circuit_handshake_assigned(uint16_t type)
     onion_handshakes_assigned[type]++;
 }
 
+/** We've just drop an onionskin (using the <b>type</b> handshake) due to being
+ * overloaded. */
+void
+rep_hist_note_circuit_handshake_dropped(uint16_t type)
+{
+  if (type <= MAX_ONION_HANDSHAKE_TYPE)
+    onion_handshakes_dropped[type]++;
+}
+
 /** Get the circuit handshake value that is requested. */
 MOCK_IMPL(int,
 rep_hist_get_circuit_handshake_requested, (uint16_t type))
@@ -1829,6 +1839,16 @@ rep_hist_get_circuit_handshake_assigned, (uint16_t type))
   return onion_handshakes_assigned[type];
 }
 
+/** Get the circuit handshake value that is dropped. */
+MOCK_IMPL(uint64_t,
+rep_hist_get_circuit_handshake_dropped, (uint16_t type))
+{
+  if (BUG(type > MAX_ONION_HANDSHAKE_TYPE)) {
+    return 0;
+  }
+  return onion_handshakes_dropped[type];
+}
+
 /** Log our onionskin statistics since the last time we were called. */
 void
 rep_hist_log_circuit_handshake_stats(time_t now)
diff --git a/src/feature/stats/rephist.h b/src/feature/stats/rephist.h
index d4a2f301cf..0bb9dd4678 100644
--- a/src/feature/stats/rephist.h
+++ b/src/feature/stats/rephist.h
@@ -58,10 +58,12 @@ time_t rep_hist_desc_stats_write(time_t now);
 
 void rep_hist_note_circuit_handshake_requested(uint16_t type);
 void rep_hist_note_circuit_handshake_assigned(uint16_t type);
+void rep_hist_note_circuit_handshake_dropped(uint16_t type);
 void rep_hist_log_circuit_handshake_stats(time_t now);
 
 MOCK_DECL(int, rep_hist_get_circuit_handshake_requested, (uint16_t type));
 MOCK_DECL(int, rep_hist_get_circuit_handshake_assigned, (uint16_t type));
+MOCK_DECL(uint64_t, rep_hist_get_circuit_handshake_dropped, (uint16_t type));
 
 void rep_hist_hs_stats_init(time_t now);
 void rep_hist_hs_stats_term(void);
@@ -83,6 +85,7 @@ extern uint32_t rephist_total_num;
 #ifdef TOR_UNIT_TESTS
 extern int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1];
 extern int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1];
+extern uint64_t onion_handshakes_dropped[MAX_ONION_HANDSHAKE_TYPE+1];
 #endif
 
 #ifdef REPHIST_PRIVATE





More information about the tor-commits mailing list