circuitstats.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitstats.h
  8. * \brief Header file for circuitstats.c
  9. **/
  10. #ifndef TOR_CIRCUITSTATS_H
  11. #define TOR_CIRCUITSTATS_H
  12. const circuit_build_times_t *get_circuit_build_times(void);
  13. circuit_build_times_t *get_circuit_build_times_mutable(void);
  14. double get_circuit_build_close_time_ms(void);
  15. double get_circuit_build_timeout_ms(void);
  16. int circuit_build_times_disabled(const or_options_t *options);
  17. int circuit_build_times_disabled_(const or_options_t *options,
  18. int ignore_consensus);
  19. /** A build_time_t is milliseconds */
  20. typedef uint32_t build_time_t;
  21. int circuit_build_times_enough_to_compute(const circuit_build_times_t *cbt);
  22. void circuit_build_times_update_state(const circuit_build_times_t *cbt,
  23. or_state_t *state);
  24. int circuit_build_times_parse_state(circuit_build_times_t *cbt,
  25. or_state_t *state);
  26. void circuit_build_times_count_timeout(circuit_build_times_t *cbt,
  27. int did_onehop);
  28. int circuit_build_times_count_close(circuit_build_times_t *cbt,
  29. int did_onehop, time_t start_time);
  30. void circuit_build_times_set_timeout(circuit_build_times_t *cbt);
  31. int circuit_build_times_add_time(circuit_build_times_t *cbt,
  32. build_time_t time);
  33. int circuit_build_times_needs_circuits(const circuit_build_times_t *cbt);
  34. void circuit_build_times_handle_completed_hop(origin_circuit_t *circ);
  35. int circuit_build_times_needs_circuits_now(const circuit_build_times_t *cbt);
  36. void circuit_build_times_init(circuit_build_times_t *cbt);
  37. void circuit_build_times_free_timeouts(circuit_build_times_t *cbt);
  38. void circuit_build_times_new_consensus_params(circuit_build_times_t *cbt,
  39. networkstatus_t *ns);
  40. double circuit_build_times_timeout_rate(const circuit_build_times_t *cbt);
  41. double circuit_build_times_close_rate(const circuit_build_times_t *cbt);
  42. void circuit_build_times_update_last_circ(circuit_build_times_t *cbt);
  43. void circuit_build_times_mark_circ_as_measurement_only(origin_circuit_t *circ);
  44. /** Total size of the circuit timeout history to accumulate.
  45. * 1000 is approx 2.5 days worth of continual-use circuits. */
  46. #define CBT_NCIRCUITS_TO_OBSERVE 1000
  47. /** Width of the histogram bins in milliseconds */
  48. #define CBT_BIN_WIDTH ((build_time_t)50)
  49. /** Number of modes to use in the weighted-avg computation of Xm */
  50. #define CBT_DEFAULT_NUM_XM_MODES 3
  51. #define CBT_MIN_NUM_XM_MODES 1
  52. #define CBT_MAX_NUM_XM_MODES 20
  53. /**
  54. * CBT_BUILD_ABANDONED is our flag value to represent a force-closed
  55. * circuit (Aka a 'right-censored' pareto value).
  56. */
  57. #define CBT_BUILD_ABANDONED ((build_time_t)(INT32_MAX-1))
  58. #define CBT_BUILD_TIME_MAX ((build_time_t)(INT32_MAX))
  59. /** Save state every 10 circuits */
  60. #define CBT_SAVE_STATE_EVERY 10
  61. /* Circuit build times consensus parameters */
  62. /**
  63. * How long to wait before actually closing circuits that take too long to
  64. * build in terms of CDF quantile.
  65. */
  66. #define CBT_DEFAULT_CLOSE_QUANTILE 95
  67. #define CBT_MIN_CLOSE_QUANTILE CBT_MIN_QUANTILE_CUTOFF
  68. #define CBT_MAX_CLOSE_QUANTILE CBT_MAX_QUANTILE_CUTOFF
  69. /**
  70. * How many circuits count as recent when considering if the
  71. * connection has gone gimpy or changed.
  72. */
  73. #define CBT_DEFAULT_RECENT_CIRCUITS 20
  74. #define CBT_MIN_RECENT_CIRCUITS 3
  75. #define CBT_MAX_RECENT_CIRCUITS 1000
  76. /**
  77. * Maximum count of timeouts that finish the first hop in the past
  78. * RECENT_CIRCUITS before calculating a new timeout.
  79. *
  80. * This tells us whether to abandon timeout history and set
  81. * the timeout back to whatever circuit_build_times_get_initial_timeout()
  82. * gives us.
  83. */
  84. #define CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT (CBT_DEFAULT_RECENT_CIRCUITS*9/10)
  85. #define CBT_MIN_MAX_RECENT_TIMEOUT_COUNT 3
  86. #define CBT_MAX_MAX_RECENT_TIMEOUT_COUNT 10000
  87. /** Minimum circuits before estimating a timeout */
  88. #define CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE 100
  89. #define CBT_MIN_MIN_CIRCUITS_TO_OBSERVE 1
  90. #define CBT_MAX_MIN_CIRCUITS_TO_OBSERVE 10000
  91. /** Cutoff percentile on the CDF for our timeout estimation. */
  92. #define CBT_DEFAULT_QUANTILE_CUTOFF 80
  93. #define CBT_MIN_QUANTILE_CUTOFF 10
  94. #define CBT_MAX_QUANTILE_CUTOFF 99
  95. double circuit_build_times_quantile_cutoff(void);
  96. /** How often in seconds should we build a test circuit */
  97. #define CBT_DEFAULT_TEST_FREQUENCY 10
  98. #define CBT_MIN_TEST_FREQUENCY 1
  99. #define CBT_MAX_TEST_FREQUENCY INT32_MAX
  100. /** Lowest allowable value for CircuitBuildTimeout in milliseconds */
  101. #define CBT_DEFAULT_TIMEOUT_MIN_VALUE (1500)
  102. #define CBT_MIN_TIMEOUT_MIN_VALUE 500
  103. #define CBT_MAX_TIMEOUT_MIN_VALUE INT32_MAX
  104. /** Initial circuit build timeout in milliseconds */
  105. #define CBT_DEFAULT_TIMEOUT_INITIAL_VALUE (60*1000)
  106. #define CBT_MIN_TIMEOUT_INITIAL_VALUE CBT_MIN_TIMEOUT_MIN_VALUE
  107. #define CBT_MAX_TIMEOUT_INITIAL_VALUE INT32_MAX
  108. int32_t circuit_build_times_initial_timeout(void);
  109. #if CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT < CBT_MIN_MAX_RECENT_TIMEOUT_COUNT
  110. #error "RECENT_CIRCUITS is set too low."
  111. #endif
  112. #ifdef CIRCUITSTATS_PRIVATE
  113. STATIC double circuit_build_times_calculate_timeout(circuit_build_times_t *cbt,
  114. double quantile);
  115. STATIC int circuit_build_times_update_alpha(circuit_build_times_t *cbt);
  116. STATIC void circuit_build_times_reset(circuit_build_times_t *cbt);
  117. /* Network liveness functions */
  118. STATIC int circuit_build_times_network_check_changed(
  119. circuit_build_times_t *cbt);
  120. #endif /* defined(CIRCUITSTATS_PRIVATE) */
  121. #ifdef TOR_UNIT_TESTS
  122. build_time_t circuit_build_times_generate_sample(circuit_build_times_t *cbt,
  123. double q_lo, double q_hi);
  124. double circuit_build_times_cdf(circuit_build_times_t *cbt, double x);
  125. void circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
  126. double quantile, double time_ms);
  127. void circuitbuild_running_unit_tests(void);
  128. #endif /* defined(TOR_UNIT_TESTS) */
  129. /* Network liveness functions */
  130. void circuit_build_times_network_is_live(circuit_build_times_t *cbt);
  131. int circuit_build_times_network_check_live(const circuit_build_times_t *cbt);
  132. void circuit_build_times_network_circ_success(circuit_build_times_t *cbt);
  133. #ifdef CIRCUITSTATS_PRIVATE
  134. /** Information about the state of our local network connection */
  135. typedef struct {
  136. /** The timestamp we last completed a TLS handshake or received a cell */
  137. time_t network_last_live;
  138. /** If the network is not live, how many timeouts has this caused? */
  139. int nonlive_timeouts;
  140. /** Circular array of circuits that have made it to the first hop. Slot is
  141. * 1 if circuit timed out, 0 if circuit succeeded */
  142. int8_t *timeouts_after_firsthop;
  143. /** Number of elements allocated for the above array */
  144. int num_recent_circs;
  145. /** Index into circular array. */
  146. int after_firsthop_idx;
  147. } network_liveness_t;
  148. /** Structure for circuit build times history */
  149. struct circuit_build_times_s {
  150. /** The circular array of recorded build times in milliseconds */
  151. build_time_t circuit_build_times[CBT_NCIRCUITS_TO_OBSERVE];
  152. /** Current index in the circuit_build_times circular array */
  153. int build_times_idx;
  154. /** Total number of build times accumulated. Max CBT_NCIRCUITS_TO_OBSERVE */
  155. int total_build_times;
  156. /** Information about the state of our local network connection */
  157. network_liveness_t liveness;
  158. /** Last time we built a circuit. Used to decide to build new test circs */
  159. time_t last_circ_at;
  160. /** "Minimum" value of our pareto distribution (actually mode) */
  161. build_time_t Xm;
  162. /** alpha exponent for pareto dist. */
  163. double alpha;
  164. /** Have we computed a timeout? */
  165. int have_computed_timeout;
  166. /** The exact value for that timeout in milliseconds. Stored as a double
  167. * to maintain precision from calculations to and from quantile value. */
  168. double timeout_ms;
  169. /** How long we wait before actually closing the circuit. */
  170. double close_ms;
  171. /** Total succeeded counts. Old measurements may be scaled downward if
  172. * we've seen a lot of circuits. */
  173. uint32_t num_circ_succeeded;
  174. /** Total timeout counts. Old measurements may be scaled downward if
  175. * we've seen a lot of circuits. */
  176. uint32_t num_circ_timeouts;
  177. /** Total closed counts. Old measurements may be scaled downward if
  178. * we've seen a lot of circuits.*/
  179. uint32_t num_circ_closed;
  180. };
  181. #endif /* defined(CIRCUITSTATS_PRIVATE) */
  182. #endif /* !defined(TOR_CIRCUITSTATS_H) */