]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
hs_pow: faster hs_circuitmap lookup for rend in pow_worker_job_t
authorMicah Elizabeth Scott <beth@torproject.org>
Thu, 6 Apr 2023 21:27:18 +0000 (14:27 -0700)
committerMicah Elizabeth Scott <beth@torproject.org>
Wed, 10 May 2023 14:41:37 +0000 (07:41 -0700)
The worker job queue for hs_pow needs what's effectively a weak pointer
to two circuits, but there's not a generic mechanism for this in c-tor.
The previous approach of circuit_get_by_global_id() is straightforward
but not efficient. These global IDs are normally only used by the
control port protocol. To reduce the number of O(N) lookups we have over
the whole circuit list, we can use hs_circuitmap to look up the rend
circuit by its auth cookie.

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
src/feature/hs/hs_client.c
src/feature/hs/hs_pow.c
src/feature/hs/hs_pow.h

index e77e1a3fbda30c391039264f61c988979d1c9cd4..d8df1d0772d6645d6e2083819bfa94fa0195a2fa 100644 (file)
@@ -797,7 +797,7 @@ consider_sending_introduce1(origin_circuit_t *intro_circ,
       /* send it to the client-side pow cpuworker for solving. */
       intro_circ->hs_currently_solving_pow = 1;
       if (hs_pow_queue_work(intro_circ->global_identifier,
-                            rend_circ->global_identifier,
+                            rend_circ->hs_ident->rendezvous_cookie,
                             &pow_inputs) != 0) {
         log_warn(LD_REND, "Failed to enqueue PoW request");
       }
index 3cd14c524676cbe922083a4b8cf349ead58a7ebf..15152bc6490aa80f525889ae7a1ef770c0f706f9 100644 (file)
@@ -16,6 +16,7 @@
 #include "ext/equix/include/equix.h"
 #include "feature/hs/hs_cache.h"
 #include "feature/hs/hs_descriptor.h"
+#include "feature/hs/hs_circuitmap.h"
 #include "feature/hs/hs_client.h"
 #include "feature/hs/hs_pow.h"
 #include "lib/crypt_ops/crypto_rand.h"
@@ -356,7 +357,7 @@ typedef struct pow_worker_job_t {
 
   /** State: we'll look these up to figure out how to proceed after. */
   uint32_t intro_circ_identifier;
-  uint32_t rend_circ_identifier;
+  uint8_t rend_circ_cookie[HS_REND_COOKIE_LEN];
 
   /** Output: The worker thread will malloc and write its answer here,
    * or set it to NULL if it produced no useful answer. */
@@ -411,11 +412,17 @@ pow_worker_replyfn(void *work_)
 
   pow_worker_job_t *job = work_;
 
-  // look up the circuits that we're going to use this pow in
+  /* Look up the circuits that we're going to use this pow in.
+   * There's room for improvement here. We already had a fast mapping to
+   * rend circuits from some kind of identifier that we can keep in a
+   * pow_worker_job_t, but we don't have that index for intro circs at this
+   * time. If the linear search in circuit_get_by_global_id() is ever a
+   * noticeable bottleneck we should add another map.
+   */
   origin_circuit_t *intro_circ =
     circuit_get_by_global_id(job->intro_circ_identifier);
   origin_circuit_t *rend_circ =
-    circuit_get_by_global_id(job->rend_circ_identifier);
+    hs_circuitmap_get_established_rend_circ_client_side(job->rend_circ_cookie);
 
   /* try to re-create desc and ip */
   const ed25519_public_key_t *service_identity_pk = NULL;
@@ -466,14 +473,17 @@ pow_worker_replyfn(void *work_)
  */
 int
 hs_pow_queue_work(uint32_t intro_circ_identifier,
-                  uint32_t rend_circ_identifier,
+                  const uint8_t *rend_circ_cookie,
                   const hs_pow_solver_inputs_t *pow_inputs)
 {
   tor_assert(in_main_thread());
+  tor_assert(rend_circ_cookie);
+  tor_assert(pow_inputs);
 
   pow_worker_job_t *job = tor_malloc_zero(sizeof(*job));
   job->intro_circ_identifier = intro_circ_identifier;
-  job->rend_circ_identifier = rend_circ_identifier;
+  memcpy(&job->rend_circ_cookie, rend_circ_cookie,
+         sizeof job->rend_circ_cookie);
   memcpy(&job->pow_inputs, pow_inputs, sizeof job->pow_inputs);
 
   workqueue_entry_t *work;
index 6e3611be69fa8b944b5b3e864ace055660acddac..357f527c343b492411057f3d41004045a093dea1 100644 (file)
@@ -145,7 +145,7 @@ void hs_pow_remove_seed_from_cache(const uint8_t *seed_head);
 void hs_pow_free_service_state(hs_pow_service_state_t *state);
 
 int hs_pow_queue_work(uint32_t intro_circ_identifier,
-                      uint32_t rend_circ_identifier,
+                      const uint8_t *rend_circ_cookie,
                       const hs_pow_solver_inputs_t *pow_inputs);
 
 #else /* !defined(HAVE_MODULE_POW) */
@@ -183,11 +183,11 @@ hs_pow_free_service_state(hs_pow_service_state_t *state)
 
 static inline int
 hs_pow_queue_work(uint32_t intro_circ_identifier,
-                  uint32_t rend_circ_identifier,
+                  const uint8_t *rend_circ_cookie,
                   const hs_pow_solver_inputs_t *pow_inputs)
 {
   (void)intro_circ_identifier;
-  (void)rend_circ_identifier;
+  (void)rend_circ_cookie;
   (void)pow_inputs;
   return -1;
 }