[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Calculate the guardfraction bandwidth of a guard.



commit 14a3c17ce658c47ae9a0b8e689970efe37ca3b22
Author: George Kadianakis <desnacked@xxxxxxxxxx>
Date:   Thu Jan 29 15:05:30 2015 +0000

    Calculate the guardfraction bandwidth of a guard.
---
 src/or/entrynodes.c |   36 ++++++++++++++++++++++++++++++++++++
 src/or/entrynodes.h |   14 ++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/src/or/entrynodes.c b/src/or/entrynodes.c
index a766f6c..93b23f6 100644
--- a/src/or/entrynodes.c
+++ b/src/or/entrynodes.c
@@ -1715,6 +1715,42 @@ should_apply_guardfraction(const networkstatus_t *ns)
 
   return options->UseGuardFraction;
 }
+
+/* Given the original bandwidth of a guard and its guardfraction,
+ * calculate how much bandwidth the guard should have as a guard and
+ * as a non-guard.
+ *
+ * Quoting from proposal236:
+ *
+ *   Let Wpf denote the weight from the 'bandwidth-weights' line a
+ *   client would apply to N for position p if it had the guard
+ *   flag, Wpn the weight if it did not have the guard flag, and B the
+ *   measured bandwidth of N in the consensus.  Then instead of choosing
+ *   N for position p proportionally to Wpf*B or Wpn*B, clients should
+ *   choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
+ *
+ * This function fills the <b>guardfraction_bw</b> structure. It sets
+ * <b>guard_bw</b> to F*B and <b>non_guard_bw</b> to (1-F)*B.
+ */
+void
+guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
+                                  int orig_bandwidth,
+                                  uint32_t guardfraction_percentage)
+{
+  double guardfraction_fraction;
+
+  /* Turn the percentage into a fraction. */
+  tor_assert(guardfraction_percentage <= 100);
+  guardfraction_fraction = guardfraction_percentage / 100.0;
+
+  long guard_bw = tor_lround(guardfraction_fraction * orig_bandwidth);
+  tor_assert(guard_bw <= INT_MAX);
+
+  guardfraction_bw->guard_bw = (int) guard_bw;
+
+  guardfraction_bw->non_guard_bw = orig_bandwidth - guard_bw;
+}
+
 /** A list of configured bridges. Whenever we actually get a descriptor
  * for one, we add it as an entry guard.  Note that the order of bridges
  * in this list does not necessarily correspond to the order of bridges
diff --git a/src/or/entrynodes.h b/src/or/entrynodes.h
index 96d832e..35bd748 100644
--- a/src/or/entrynodes.h
+++ b/src/or/entrynodes.h
@@ -160,7 +160,21 @@ int validate_pluggable_transports_config(void);
 double pathbias_get_close_success_count(entry_guard_t *guard);
 double pathbias_get_use_success_count(entry_guard_t *guard);
 
+/** Contains the bandwidth of a relay as a guard and as a non-guard
+ *  after the guardfraction has been considered.  */
+typedef struct guardfraction_bandwidth_t {
+  /* Bandwidth as a guard after guardfraction has been considered. */
+  int guard_bw;
+  /* Bandwidth as a non-guard after guardfraction has been considered. */
+  int non_guard_bw;
+} guardfraction_bandwidth_t;
+
 int should_apply_guardfraction(const networkstatus_t *ns);
 
+void
+guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
+                                  int orig_bandwidth,
+                                  uint32_t guardfraction_percentage);
+
 #endif
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits