|
@@ -18,6 +18,10 @@
|
|
|
#undef log
|
|
|
#include <math.h>
|
|
|
|
|
|
+static void cbt_control_event_buildtimeout_set(
|
|
|
+ const circuit_build_times_t *cbt,
|
|
|
+ buildtimeout_set_event_t type);
|
|
|
+
|
|
|
#define CBT_BIN_TO_MS(bin) ((bin)*CBT_BIN_WIDTH + (CBT_BIN_WIDTH/2))
|
|
|
|
|
|
/** Global list of circuit build times */
|
|
@@ -26,8 +30,7 @@
|
|
|
// vary in their own latency. The downside of this is that guards
|
|
|
// can change frequently, so we'd be building a lot more circuits
|
|
|
// most likely.
|
|
|
-/* XXXX024 Make this static; add accessor functions. */
|
|
|
-circuit_build_times_t circ_times;
|
|
|
+static circuit_build_times_t circ_times;
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
/** If set, we're running the unit tests: we should avoid clobbering
|
|
@@ -37,6 +40,37 @@ static int unit_tests = 0;
|
|
|
#define unit_tests 0
|
|
|
#endif
|
|
|
|
|
|
+/** Return a pointer to the data structure describing our current circuit
|
|
|
+ * build time history and computations. */
|
|
|
+const circuit_build_times_t *
|
|
|
+get_circuit_build_times(void)
|
|
|
+{
|
|
|
+ return &circ_times;
|
|
|
+}
|
|
|
+
|
|
|
+/** As get_circuit_build_times, but return a mutable pointer. */
|
|
|
+circuit_build_times_t *
|
|
|
+get_circuit_build_times_mutable(void)
|
|
|
+{
|
|
|
+ return &circ_times;
|
|
|
+}
|
|
|
+
|
|
|
+/** Return the time to wait before actually closing an under-construction, in
|
|
|
+ * milliseconds. */
|
|
|
+double
|
|
|
+get_circuit_build_close_time_ms(void)
|
|
|
+{
|
|
|
+ return circ_times.close_ms;
|
|
|
+}
|
|
|
+
|
|
|
+/** Return the time to wait before giving up on an under-construction circuit,
|
|
|
+ * in milliseconds. */
|
|
|
+double
|
|
|
+get_circuit_build_timeout_ms(void)
|
|
|
+{
|
|
|
+ return circ_times.timeout_ms;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* This function decides if CBT learning should be disabled. It returns
|
|
|
* true if one or more of the following four conditions are met:
|
|
@@ -158,7 +192,7 @@ circuit_build_times_min_circs_to_observe(void)
|
|
|
/** Return true iff <b>cbt</b> has recorded enough build times that we
|
|
|
* want to start acting on the timeout it implies. */
|
|
|
int
|
|
|
-circuit_build_times_enough_to_compute(circuit_build_times_t *cbt)
|
|
|
+circuit_build_times_enough_to_compute(const circuit_build_times_t *cbt)
|
|
|
{
|
|
|
return cbt->total_build_times >= circuit_build_times_min_circs_to_observe();
|
|
|
}
|
|
@@ -475,7 +509,7 @@ circuit_build_times_init(circuit_build_times_t *cbt)
|
|
|
cbt->liveness.timeouts_after_firsthop = NULL;
|
|
|
}
|
|
|
cbt->close_ms = cbt->timeout_ms = circuit_build_times_get_initial_timeout();
|
|
|
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
|
|
|
+ cbt_control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -561,7 +595,7 @@ circuit_build_times_add_time(circuit_build_times_t *cbt, build_time_t time)
|
|
|
* Return maximum circuit build time
|
|
|
*/
|
|
|
static build_time_t
|
|
|
-circuit_build_times_max(circuit_build_times_t *cbt)
|
|
|
+circuit_build_times_max(const circuit_build_times_t *cbt)
|
|
|
{
|
|
|
int i = 0;
|
|
|
build_time_t max_build_time = 0;
|
|
@@ -602,7 +636,7 @@ circuit_build_times_min(circuit_build_times_t *cbt)
|
|
|
* The return value must be freed by the caller.
|
|
|
*/
|
|
|
static uint32_t *
|
|
|
-circuit_build_times_create_histogram(circuit_build_times_t *cbt,
|
|
|
+circuit_build_times_create_histogram(const circuit_build_times_t *cbt,
|
|
|
build_time_t *nbins)
|
|
|
{
|
|
|
uint32_t *histogram;
|
|
@@ -692,7 +726,7 @@ circuit_build_times_get_xm(circuit_build_times_t *cbt)
|
|
|
* the or_state_t state structure.
|
|
|
*/
|
|
|
void
|
|
|
-circuit_build_times_update_state(circuit_build_times_t *cbt,
|
|
|
+circuit_build_times_update_state(const circuit_build_times_t *cbt,
|
|
|
or_state_t *state)
|
|
|
{
|
|
|
uint32_t *histogram;
|
|
@@ -1129,7 +1163,7 @@ circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
|
|
|
* Returns true if we need circuits to be built
|
|
|
*/
|
|
|
int
|
|
|
-circuit_build_times_needs_circuits(circuit_build_times_t *cbt)
|
|
|
+circuit_build_times_needs_circuits(const circuit_build_times_t *cbt)
|
|
|
{
|
|
|
/* Return true if < MIN_CIRCUITS_TO_OBSERVE */
|
|
|
return !circuit_build_times_enough_to_compute(cbt);
|
|
@@ -1140,7 +1174,7 @@ circuit_build_times_needs_circuits(circuit_build_times_t *cbt)
|
|
|
* right now.
|
|
|
*/
|
|
|
int
|
|
|
-circuit_build_times_needs_circuits_now(circuit_build_times_t *cbt)
|
|
|
+circuit_build_times_needs_circuits_now(const circuit_build_times_t *cbt)
|
|
|
{
|
|
|
return circuit_build_times_needs_circuits(cbt) &&
|
|
|
approx_time()-cbt->last_circ_at > circuit_build_times_test_frequency();
|
|
@@ -1273,7 +1307,7 @@ circuit_build_times_network_close(circuit_build_times_t *cbt,
|
|
|
* in the case of recent liveness changes.
|
|
|
*/
|
|
|
int
|
|
|
-circuit_build_times_network_check_live(circuit_build_times_t *cbt)
|
|
|
+circuit_build_times_network_check_live(const circuit_build_times_t *cbt)
|
|
|
{
|
|
|
if (cbt->liveness.nonlive_timeouts > 0) {
|
|
|
return 0;
|
|
@@ -1339,7 +1373,7 @@ circuit_build_times_network_check_changed(circuit_build_times_t *cbt)
|
|
|
= circuit_build_times_get_initial_timeout();
|
|
|
}
|
|
|
|
|
|
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
|
|
|
+ cbt_control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
|
|
|
|
|
|
log_notice(LD_CIRC,
|
|
|
"Your network connection speed appears to have changed. Resetting "
|
|
@@ -1521,7 +1555,7 @@ circuit_build_times_set_timeout(circuit_build_times_t *cbt)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_COMPUTED);
|
|
|
+ cbt_control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_COMPUTED);
|
|
|
|
|
|
timeout_rate = circuit_build_times_timeout_rate(cbt);
|
|
|
|
|
@@ -1567,3 +1601,44 @@ circuitbuild_running_unit_tests(void)
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+void
|
|
|
+circuit_build_times_update_last_circ(circuit_build_times_t *cbt)
|
|
|
+{
|
|
|
+ cbt->last_circ_at = approx_time();
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+cbt_control_event_buildtimeout_set(const circuit_build_times_t *cbt,
|
|
|
+ buildtimeout_set_event_t type)
|
|
|
+{
|
|
|
+ char *args = NULL;
|
|
|
+ double qnt;
|
|
|
+
|
|
|
+ switch(type) {
|
|
|
+ case BUILDTIMEOUT_SET_EVENT_RESET:
|
|
|
+ case BUILDTIMEOUT_SET_EVENT_SUSPENDED:
|
|
|
+ case BUILDTIMEOUT_SET_EVENT_DISCARD:
|
|
|
+ qnt = 1.0;
|
|
|
+ break;
|
|
|
+ case BUILDTIMEOUT_SET_EVENT_COMPUTED:
|
|
|
+ case BUILDTIMEOUT_SET_EVENT_RESUME:
|
|
|
+ default:
|
|
|
+ qnt = circuit_build_times_quantile_cutoff();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ tor_asprintf(&args, "TOTAL_TIMES=%lu "
|
|
|
+ "TIMEOUT_MS=%lu XM=%lu ALPHA=%f CUTOFF_QUANTILE=%f "
|
|
|
+ "TIMEOUT_RATE=%f CLOSE_MS=%lu CLOSE_RATE=%f",
|
|
|
+ (unsigned long)cbt->total_build_times,
|
|
|
+ (unsigned long)cbt->timeout_ms,
|
|
|
+ (unsigned long)cbt->Xm, cbt->alpha, qnt,
|
|
|
+ circuit_build_times_timeout_rate(cbt),
|
|
|
+ (unsigned long)cbt->close_ms,
|
|
|
+ circuit_build_times_close_rate(cbt));
|
|
|
+
|
|
|
+ control_event_buildtimeout_set(type, args);
|
|
|
+
|
|
|
+ tor_free(args);
|
|
|
+
|
|
|
+}
|