circuitmux_ewma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* * Copyright (c) 2012-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file circuitmux_ewma.c
  5. * \brief EWMA circuit selection as a circuitmux_t policy
  6. *
  7. * The "EWMA" in this module stands for the "exponentially weighted moving
  8. * average" of the number of cells sent on each circuit. The goal is to
  9. * prioritize cells on circuits that have been quiet recently, by looking at
  10. * those that have sent few cells over time, prioritizing recent times
  11. * more than older ones.
  12. *
  13. * Specifically, a cell sent at time "now" has weight 1, but a time X ticks
  14. * before now has weight ewma_scale_factor ^ X , where ewma_scale_factor is
  15. * between 0.0 and 1.0.
  16. *
  17. * For efficiency, we do not re-scale these averages every time we send a
  18. * cell: that would be horribly inefficient. Instead, we we keep the cell
  19. * count on all circuits on the same circuitmux scaled relative to a single
  20. * tick. When we add a new cell, we scale its weight depending on the time
  21. * that has elapsed since the tick. We do re-scale the circuits on the
  22. * circuitmux periodically, so that we don't overflow double.
  23. *
  24. *
  25. * This module should be used through the interfaces in circuitmux.c, which it
  26. * implements.
  27. *
  28. **/
  29. #define CIRCUITMUX_EWMA_PRIVATE
  30. #include "orconfig.h"
  31. #include <math.h>
  32. #include "or/or.h"
  33. #include "or/circuitmux.h"
  34. #include "or/circuitmux_ewma.h"
  35. #include "lib/crypt_ops/crypto_rand.h"
  36. #include "or/networkstatus.h"
  37. /*** EWMA parameter #defines ***/
  38. /** How long does a tick last (seconds)? */
  39. #define EWMA_TICK_LEN 10
  40. /** The default per-tick scale factor, if it hasn't been overridden by a
  41. * consensus or a configuration setting. zero means "disabled". */
  42. #define EWMA_DEFAULT_HALFLIFE 0.0
  43. /*** Some useful constant #defines ***/
  44. /** Any halflife smaller than this number of seconds is considered to be
  45. * "disabled". */
  46. #define EPSILON 0.00001
  47. /** The natural logarithm of 0.5. */
  48. #define LOG_ONEHALF -0.69314718055994529
  49. /*** EWMA structures ***/
  50. typedef struct cell_ewma_s cell_ewma_t;
  51. typedef struct ewma_policy_data_s ewma_policy_data_t;
  52. typedef struct ewma_policy_circ_data_s ewma_policy_circ_data_t;
  53. /**
  54. * The cell_ewma_t structure keeps track of how many cells a circuit has
  55. * transferred recently. It keeps an EWMA (exponentially weighted moving
  56. * average) of the number of cells flushed from the circuit queue onto a
  57. * connection in channel_flush_from_first_active_circuit().
  58. */
  59. struct cell_ewma_s {
  60. /** The last 'tick' at which we recalibrated cell_count.
  61. *
  62. * A cell sent at exactly the start of this tick has weight 1.0. Cells sent
  63. * since the start of this tick have weight greater than 1.0; ones sent
  64. * earlier have less weight. */
  65. unsigned int last_adjusted_tick;
  66. /** The EWMA of the cell count. */
  67. double cell_count;
  68. /** True iff this is the cell count for a circuit's previous
  69. * channel. */
  70. unsigned int is_for_p_chan : 1;
  71. /** The position of the circuit within the OR connection's priority
  72. * queue. */
  73. int heap_index;
  74. };
  75. struct ewma_policy_data_s {
  76. circuitmux_policy_data_t base_;
  77. /**
  78. * Priority queue of cell_ewma_t for circuits with queued cells waiting
  79. * for room to free up on the channel that owns this circuitmux. Kept
  80. * in heap order according to EWMA. This was formerly in channel_t, and
  81. * in or_connection_t before that.
  82. */
  83. smartlist_t *active_circuit_pqueue;
  84. /**
  85. * The tick on which the cell_ewma_ts in active_circuit_pqueue last had
  86. * their ewma values rescaled. This was formerly in channel_t, and in
  87. * or_connection_t before that.
  88. */
  89. unsigned int active_circuit_pqueue_last_recalibrated;
  90. };
  91. struct ewma_policy_circ_data_s {
  92. circuitmux_policy_circ_data_t base_;
  93. /**
  94. * The EWMA count for the number of cells flushed from this circuit
  95. * onto this circuitmux. Used to determine which circuit to flush
  96. * from next. This was formerly in circuit_t and or_circuit_t.
  97. */
  98. cell_ewma_t cell_ewma;
  99. /**
  100. * Pointer back to the circuit_t this is for; since we're separating
  101. * out circuit selection policy like this, we can't attach cell_ewma_t
  102. * to the circuit_t any more, so we can't use SUBTYPE_P directly to a
  103. * circuit_t like before; instead get it here.
  104. */
  105. circuit_t *circ;
  106. };
  107. #define EWMA_POL_DATA_MAGIC 0x2fd8b16aU
  108. #define EWMA_POL_CIRC_DATA_MAGIC 0x761e7747U
  109. /*** Downcasts for the above types ***/
  110. static ewma_policy_data_t *
  111. TO_EWMA_POL_DATA(circuitmux_policy_data_t *);
  112. static ewma_policy_circ_data_t *
  113. TO_EWMA_POL_CIRC_DATA(circuitmux_policy_circ_data_t *);
  114. /**
  115. * Downcast a circuitmux_policy_data_t to an ewma_policy_data_t and assert
  116. * if the cast is impossible.
  117. */
  118. static inline ewma_policy_data_t *
  119. TO_EWMA_POL_DATA(circuitmux_policy_data_t *pol)
  120. {
  121. if (!pol) return NULL;
  122. else {
  123. tor_assert(pol->magic == EWMA_POL_DATA_MAGIC);
  124. return DOWNCAST(ewma_policy_data_t, pol);
  125. }
  126. }
  127. /**
  128. * Downcast a circuitmux_policy_circ_data_t to an ewma_policy_circ_data_t
  129. * and assert if the cast is impossible.
  130. */
  131. static inline ewma_policy_circ_data_t *
  132. TO_EWMA_POL_CIRC_DATA(circuitmux_policy_circ_data_t *pol)
  133. {
  134. if (!pol) return NULL;
  135. else {
  136. tor_assert(pol->magic == EWMA_POL_CIRC_DATA_MAGIC);
  137. return DOWNCAST(ewma_policy_circ_data_t, pol);
  138. }
  139. }
  140. /*** Static declarations for circuitmux_ewma.c ***/
  141. static void add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma);
  142. static int compare_cell_ewma_counts(const void *p1, const void *p2);
  143. static circuit_t * cell_ewma_to_circuit(cell_ewma_t *ewma);
  144. static inline double get_scale_factor(unsigned from_tick, unsigned to_tick);
  145. static cell_ewma_t * pop_first_cell_ewma(ewma_policy_data_t *pol);
  146. static void remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma);
  147. static void scale_single_cell_ewma(cell_ewma_t *ewma, unsigned cur_tick);
  148. static void scale_active_circuits(ewma_policy_data_t *pol,
  149. unsigned cur_tick);
  150. /*** Circuitmux policy methods ***/
  151. static circuitmux_policy_data_t * ewma_alloc_cmux_data(circuitmux_t *cmux);
  152. static void ewma_free_cmux_data(circuitmux_t *cmux,
  153. circuitmux_policy_data_t *pol_data);
  154. static circuitmux_policy_circ_data_t *
  155. ewma_alloc_circ_data(circuitmux_t *cmux, circuitmux_policy_data_t *pol_data,
  156. circuit_t *circ, cell_direction_t direction,
  157. unsigned int cell_count);
  158. static void
  159. ewma_free_circ_data(circuitmux_t *cmux,
  160. circuitmux_policy_data_t *pol_data,
  161. circuit_t *circ,
  162. circuitmux_policy_circ_data_t *pol_circ_data);
  163. static void
  164. ewma_notify_circ_active(circuitmux_t *cmux,
  165. circuitmux_policy_data_t *pol_data,
  166. circuit_t *circ,
  167. circuitmux_policy_circ_data_t *pol_circ_data);
  168. static void
  169. ewma_notify_circ_inactive(circuitmux_t *cmux,
  170. circuitmux_policy_data_t *pol_data,
  171. circuit_t *circ,
  172. circuitmux_policy_circ_data_t *pol_circ_data);
  173. static void
  174. ewma_notify_xmit_cells(circuitmux_t *cmux,
  175. circuitmux_policy_data_t *pol_data,
  176. circuit_t *circ,
  177. circuitmux_policy_circ_data_t *pol_circ_data,
  178. unsigned int n_cells);
  179. static circuit_t *
  180. ewma_pick_active_circuit(circuitmux_t *cmux,
  181. circuitmux_policy_data_t *pol_data);
  182. static int
  183. ewma_cmp_cmux(circuitmux_t *cmux_1, circuitmux_policy_data_t *pol_data_1,
  184. circuitmux_t *cmux_2, circuitmux_policy_data_t *pol_data_2);
  185. /*** EWMA global variables ***/
  186. /** The per-tick scale factor to be used when computing cell-count EWMA
  187. * values. (A cell sent N ticks before the start of the current tick
  188. * has value ewma_scale_factor ** N.)
  189. */
  190. static double ewma_scale_factor = 0.1;
  191. /*** EWMA circuitmux_policy_t method table ***/
  192. circuitmux_policy_t ewma_policy = {
  193. /*.alloc_cmux_data =*/ ewma_alloc_cmux_data,
  194. /*.free_cmux_data =*/ ewma_free_cmux_data,
  195. /*.alloc_circ_data =*/ ewma_alloc_circ_data,
  196. /*.free_circ_data =*/ ewma_free_circ_data,
  197. /*.notify_circ_active =*/ ewma_notify_circ_active,
  198. /*.notify_circ_inactive =*/ ewma_notify_circ_inactive,
  199. /*.notify_set_n_cells =*/ NULL, /* EWMA doesn't need this */
  200. /*.notify_xmit_cells =*/ ewma_notify_xmit_cells,
  201. /*.pick_active_circuit =*/ ewma_pick_active_circuit,
  202. /*.cmp_cmux =*/ ewma_cmp_cmux
  203. };
  204. /** Have we initialized the ewma tick-counting logic? */
  205. static int ewma_ticks_initialized = 0;
  206. /** At what monotime_coarse_t did the current tick begin? */
  207. static monotime_coarse_t start_of_current_tick;
  208. /** What is the number of the current tick? */
  209. static unsigned current_tick_num;
  210. /*** EWMA method implementations using the below EWMA helper functions ***/
  211. /** Compute and return the current cell_ewma tick. */
  212. static inline unsigned int
  213. cell_ewma_get_tick(void)
  214. {
  215. monotime_coarse_t now;
  216. monotime_coarse_get(&now);
  217. int32_t msec_diff = monotime_coarse_diff_msec32(&start_of_current_tick,
  218. &now);
  219. return current_tick_num + msec_diff / (1000*EWMA_TICK_LEN);
  220. }
  221. /**
  222. * Allocate an ewma_policy_data_t and upcast it to a circuitmux_policy_data_t;
  223. * this is called when setting the policy on a circuitmux_t to ewma_policy.
  224. */
  225. static circuitmux_policy_data_t *
  226. ewma_alloc_cmux_data(circuitmux_t *cmux)
  227. {
  228. ewma_policy_data_t *pol = NULL;
  229. tor_assert(cmux);
  230. pol = tor_malloc_zero(sizeof(*pol));
  231. pol->base_.magic = EWMA_POL_DATA_MAGIC;
  232. pol->active_circuit_pqueue = smartlist_new();
  233. pol->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  234. return TO_CMUX_POL_DATA(pol);
  235. }
  236. /**
  237. * Free an ewma_policy_data_t allocated with ewma_alloc_cmux_data()
  238. */
  239. static void
  240. ewma_free_cmux_data(circuitmux_t *cmux,
  241. circuitmux_policy_data_t *pol_data)
  242. {
  243. ewma_policy_data_t *pol = NULL;
  244. tor_assert(cmux);
  245. if (!pol_data) return;
  246. pol = TO_EWMA_POL_DATA(pol_data);
  247. smartlist_free(pol->active_circuit_pqueue);
  248. tor_free(pol);
  249. }
  250. /**
  251. * Allocate an ewma_policy_circ_data_t and upcast it to a
  252. * circuitmux_policy_data_t; this is called when attaching a circuit to a
  253. * circuitmux_t with ewma_policy.
  254. */
  255. static circuitmux_policy_circ_data_t *
  256. ewma_alloc_circ_data(circuitmux_t *cmux,
  257. circuitmux_policy_data_t *pol_data,
  258. circuit_t *circ,
  259. cell_direction_t direction,
  260. unsigned int cell_count)
  261. {
  262. ewma_policy_circ_data_t *cdata = NULL;
  263. tor_assert(cmux);
  264. tor_assert(pol_data);
  265. tor_assert(circ);
  266. tor_assert(direction == CELL_DIRECTION_OUT ||
  267. direction == CELL_DIRECTION_IN);
  268. /* Shut the compiler up without triggering -Wtautological-compare */
  269. (void)cell_count;
  270. cdata = tor_malloc_zero(sizeof(*cdata));
  271. cdata->base_.magic = EWMA_POL_CIRC_DATA_MAGIC;
  272. cdata->circ = circ;
  273. /*
  274. * Initialize the cell_ewma_t structure (formerly in
  275. * init_circuit_base())
  276. */
  277. cdata->cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  278. cdata->cell_ewma.cell_count = 0.0;
  279. cdata->cell_ewma.heap_index = -1;
  280. if (direction == CELL_DIRECTION_IN) {
  281. cdata->cell_ewma.is_for_p_chan = 1;
  282. } else {
  283. cdata->cell_ewma.is_for_p_chan = 0;
  284. }
  285. return TO_CMUX_POL_CIRC_DATA(cdata);
  286. }
  287. /**
  288. * Free an ewma_policy_circ_data_t allocated with ewma_alloc_circ_data()
  289. */
  290. static void
  291. ewma_free_circ_data(circuitmux_t *cmux,
  292. circuitmux_policy_data_t *pol_data,
  293. circuit_t *circ,
  294. circuitmux_policy_circ_data_t *pol_circ_data)
  295. {
  296. ewma_policy_circ_data_t *cdata = NULL;
  297. tor_assert(cmux);
  298. tor_assert(circ);
  299. tor_assert(pol_data);
  300. if (!pol_circ_data) return;
  301. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  302. tor_free(cdata);
  303. }
  304. /**
  305. * Handle circuit activation; this inserts the circuit's cell_ewma into
  306. * the active_circuits_pqueue.
  307. */
  308. static void
  309. ewma_notify_circ_active(circuitmux_t *cmux,
  310. circuitmux_policy_data_t *pol_data,
  311. circuit_t *circ,
  312. circuitmux_policy_circ_data_t *pol_circ_data)
  313. {
  314. ewma_policy_data_t *pol = NULL;
  315. ewma_policy_circ_data_t *cdata = NULL;
  316. tor_assert(cmux);
  317. tor_assert(pol_data);
  318. tor_assert(circ);
  319. tor_assert(pol_circ_data);
  320. pol = TO_EWMA_POL_DATA(pol_data);
  321. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  322. add_cell_ewma(pol, &(cdata->cell_ewma));
  323. }
  324. /**
  325. * Handle circuit deactivation; this removes the circuit's cell_ewma from
  326. * the active_circuits_pqueue.
  327. */
  328. static void
  329. ewma_notify_circ_inactive(circuitmux_t *cmux,
  330. circuitmux_policy_data_t *pol_data,
  331. circuit_t *circ,
  332. circuitmux_policy_circ_data_t *pol_circ_data)
  333. {
  334. ewma_policy_data_t *pol = NULL;
  335. ewma_policy_circ_data_t *cdata = NULL;
  336. tor_assert(cmux);
  337. tor_assert(pol_data);
  338. tor_assert(circ);
  339. tor_assert(pol_circ_data);
  340. pol = TO_EWMA_POL_DATA(pol_data);
  341. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  342. remove_cell_ewma(pol, &(cdata->cell_ewma));
  343. }
  344. /**
  345. * Update cell_ewma for this circuit after we've sent some cells, and
  346. * remove/reinsert it in the queue. This used to be done (brokenly,
  347. * see bug 6816) in channel_flush_from_first_active_circuit().
  348. */
  349. static void
  350. ewma_notify_xmit_cells(circuitmux_t *cmux,
  351. circuitmux_policy_data_t *pol_data,
  352. circuit_t *circ,
  353. circuitmux_policy_circ_data_t *pol_circ_data,
  354. unsigned int n_cells)
  355. {
  356. ewma_policy_data_t *pol = NULL;
  357. ewma_policy_circ_data_t *cdata = NULL;
  358. unsigned int tick;
  359. double fractional_tick, ewma_increment;
  360. cell_ewma_t *cell_ewma, *tmp;
  361. tor_assert(cmux);
  362. tor_assert(pol_data);
  363. tor_assert(circ);
  364. tor_assert(pol_circ_data);
  365. tor_assert(n_cells > 0);
  366. pol = TO_EWMA_POL_DATA(pol_data);
  367. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  368. /* Rescale the EWMAs if needed */
  369. tick = cell_ewma_get_current_tick_and_fraction(&fractional_tick);
  370. if (tick != pol->active_circuit_pqueue_last_recalibrated) {
  371. scale_active_circuits(pol, tick);
  372. }
  373. /* How much do we adjust the cell count in cell_ewma by? */
  374. ewma_increment =
  375. ((double)(n_cells)) * pow(ewma_scale_factor, -fractional_tick);
  376. /* Do the adjustment */
  377. cell_ewma = &(cdata->cell_ewma);
  378. cell_ewma->cell_count += ewma_increment;
  379. /*
  380. * Since we just sent on this circuit, it should be at the head of
  381. * the queue. Pop the head, assert that it matches, then re-add.
  382. */
  383. tmp = pop_first_cell_ewma(pol);
  384. tor_assert(tmp == cell_ewma);
  385. add_cell_ewma(pol, cell_ewma);
  386. }
  387. /**
  388. * Pick the preferred circuit to send from; this will be the one with
  389. * the lowest EWMA value in the priority queue. This used to be done
  390. * in channel_flush_from_first_active_circuit().
  391. */
  392. static circuit_t *
  393. ewma_pick_active_circuit(circuitmux_t *cmux,
  394. circuitmux_policy_data_t *pol_data)
  395. {
  396. ewma_policy_data_t *pol = NULL;
  397. circuit_t *circ = NULL;
  398. cell_ewma_t *cell_ewma = NULL;
  399. tor_assert(cmux);
  400. tor_assert(pol_data);
  401. pol = TO_EWMA_POL_DATA(pol_data);
  402. if (smartlist_len(pol->active_circuit_pqueue) > 0) {
  403. /* Get the head of the queue */
  404. cell_ewma = smartlist_get(pol->active_circuit_pqueue, 0);
  405. circ = cell_ewma_to_circuit(cell_ewma);
  406. }
  407. return circ;
  408. }
  409. /**
  410. * Compare two EWMA cmuxes, and return -1, 0 or 1 to indicate which should
  411. * be more preferred - see circuitmux_compare_muxes() of circuitmux.c.
  412. */
  413. static int
  414. ewma_cmp_cmux(circuitmux_t *cmux_1, circuitmux_policy_data_t *pol_data_1,
  415. circuitmux_t *cmux_2, circuitmux_policy_data_t *pol_data_2)
  416. {
  417. ewma_policy_data_t *p1 = NULL, *p2 = NULL;
  418. cell_ewma_t *ce1 = NULL, *ce2 = NULL;
  419. tor_assert(cmux_1);
  420. tor_assert(pol_data_1);
  421. tor_assert(cmux_2);
  422. tor_assert(pol_data_2);
  423. p1 = TO_EWMA_POL_DATA(pol_data_1);
  424. p2 = TO_EWMA_POL_DATA(pol_data_2);
  425. if (p1 != p2) {
  426. /* Get the head cell_ewma_t from each queue */
  427. if (smartlist_len(p1->active_circuit_pqueue) > 0) {
  428. ce1 = smartlist_get(p1->active_circuit_pqueue, 0);
  429. }
  430. if (smartlist_len(p2->active_circuit_pqueue) > 0) {
  431. ce2 = smartlist_get(p2->active_circuit_pqueue, 0);
  432. }
  433. /* Got both of them? */
  434. if (ce1 != NULL && ce2 != NULL) {
  435. /* Pick whichever one has the better best circuit */
  436. return compare_cell_ewma_counts(ce1, ce2);
  437. } else {
  438. if (ce1 != NULL ) {
  439. /* We only have a circuit on cmux_1, so prefer it */
  440. return -1;
  441. } else if (ce2 != NULL) {
  442. /* We only have a circuit on cmux_2, so prefer it */
  443. return 1;
  444. } else {
  445. /* No circuits at all; no preference */
  446. return 0;
  447. }
  448. }
  449. } else {
  450. /* We got identical params */
  451. return 0;
  452. }
  453. }
  454. /** Helper for sorting cell_ewma_t values in their priority queue. */
  455. static int
  456. compare_cell_ewma_counts(const void *p1, const void *p2)
  457. {
  458. const cell_ewma_t *e1 = p1, *e2 = p2;
  459. if (e1->cell_count < e2->cell_count)
  460. return -1;
  461. else if (e1->cell_count > e2->cell_count)
  462. return 1;
  463. else
  464. return 0;
  465. }
  466. /** Given a cell_ewma_t, return a pointer to the circuit containing it. */
  467. static circuit_t *
  468. cell_ewma_to_circuit(cell_ewma_t *ewma)
  469. {
  470. ewma_policy_circ_data_t *cdata = NULL;
  471. tor_assert(ewma);
  472. cdata = SUBTYPE_P(ewma, ewma_policy_circ_data_t, cell_ewma);
  473. tor_assert(cdata);
  474. return cdata->circ;
  475. }
  476. /* ==== Functions for scaling cell_ewma_t ====
  477. When choosing which cells to relay first, we favor circuits that have been
  478. quiet recently. This gives better latency on connections that aren't
  479. pushing lots of data, and makes the network feel more interactive.
  480. Conceptually, we take an exponentially weighted mean average of the number
  481. of cells a circuit has sent, and allow active circuits (those with cells to
  482. relay) to send cells in reverse order of their exponentially-weighted mean
  483. average (EWMA) cell count. [That is, a cell sent N seconds ago 'counts'
  484. F^N times as much as a cell sent now, for 0<F<1.0, and we favor the
  485. circuit that has sent the fewest cells]
  486. If 'double' had infinite precision, we could do this simply by counting a
  487. cell sent at startup as having weight 1.0, and a cell sent N seconds later
  488. as having weight F^-N. This way, we would never need to re-scale
  489. any already-sent cells.
  490. To prevent double from overflowing, we could count a cell sent now as
  491. having weight 1.0 and a cell sent N seconds ago as having weight F^N.
  492. This, however, would mean we'd need to re-scale *ALL* old circuits every
  493. time we wanted to send a cell.
  494. So as a compromise, we divide time into 'ticks' (currently, 10-second
  495. increments) and say that a cell sent at the start of a current tick is
  496. worth 1.0, a cell sent N seconds before the start of the current tick is
  497. worth F^N, and a cell sent N seconds after the start of the current tick is
  498. worth F^-N. This way we don't overflow, and we don't need to constantly
  499. rescale.
  500. */
  501. /**
  502. * Initialize the system that tells which ewma tick we are in.
  503. */
  504. STATIC void
  505. cell_ewma_initialize_ticks(void)
  506. {
  507. if (ewma_ticks_initialized)
  508. return;
  509. monotime_coarse_get(&start_of_current_tick);
  510. crypto_rand((char*)&current_tick_num, sizeof(current_tick_num));
  511. ewma_ticks_initialized = 1;
  512. }
  513. /** Compute the current cell_ewma tick and the fraction of the tick that has
  514. * elapsed between the start of the tick and the current time. Return the
  515. * former and store the latter in *<b>remainder_out</b>.
  516. *
  517. * These tick values are not meant to be shared between Tor instances, or used
  518. * for other purposes. */
  519. STATIC unsigned
  520. cell_ewma_get_current_tick_and_fraction(double *remainder_out)
  521. {
  522. if (BUG(!ewma_ticks_initialized)) {
  523. cell_ewma_initialize_ticks(); // LCOV_EXCL_LINE
  524. }
  525. monotime_coarse_t now;
  526. monotime_coarse_get(&now);
  527. int32_t msec_diff = monotime_coarse_diff_msec32(&start_of_current_tick,
  528. &now);
  529. if (msec_diff > (1000*EWMA_TICK_LEN)) {
  530. unsigned ticks_difference = msec_diff / (1000*EWMA_TICK_LEN);
  531. monotime_coarse_add_msec(&start_of_current_tick,
  532. &start_of_current_tick,
  533. ticks_difference * 1000 * EWMA_TICK_LEN);
  534. current_tick_num += ticks_difference;
  535. msec_diff %= 1000*EWMA_TICK_LEN;
  536. }
  537. *remainder_out = ((double)msec_diff) / (1.0e3 * EWMA_TICK_LEN);
  538. return current_tick_num;
  539. }
  540. /* Default value for the CircuitPriorityHalflifeMsec consensus parameter in
  541. * msec. */
  542. #define CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT 30000
  543. /* Minimum and maximum value for the CircuitPriorityHalflifeMsec consensus
  544. * parameter. */
  545. #define CMUX_PRIORITY_HALFLIFE_MSEC_MIN 1
  546. #define CMUX_PRIORITY_HALFLIFE_MSEC_MAX INT32_MAX
  547. /* Return the value of the circuit priority halflife from the options if
  548. * available or else from the consensus (in that order). If none can be found,
  549. * a default value is returned.
  550. *
  551. * The source_msg points to a string describing from where the value was
  552. * picked so it can be used for logging. */
  553. static double
  554. get_circuit_priority_halflife(const or_options_t *options,
  555. const networkstatus_t *consensus,
  556. const char **source_msg)
  557. {
  558. int32_t halflife_ms;
  559. double halflife;
  560. /* Compute the default value now. We might need it. */
  561. double halflife_default =
  562. ((double) CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT) / 1000.0;
  563. /* Try to get it from configuration file first. */
  564. if (options && options->CircuitPriorityHalflife >= -EPSILON) {
  565. halflife = options->CircuitPriorityHalflife;
  566. *source_msg = "CircuitPriorityHalflife in configuration";
  567. goto end;
  568. }
  569. /* Try to get the msec value from the consensus. */
  570. halflife_ms = networkstatus_get_param(consensus,
  571. "CircuitPriorityHalflifeMsec",
  572. CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT,
  573. CMUX_PRIORITY_HALFLIFE_MSEC_MIN,
  574. CMUX_PRIORITY_HALFLIFE_MSEC_MAX);
  575. halflife = ((double) halflife_ms) / 1000.0;
  576. *source_msg = "CircuitPriorityHalflifeMsec in consensus";
  577. end:
  578. /* We should never go below the EPSILON else we would consider it disabled
  579. * and we can't have that. */
  580. if (halflife < EPSILON) {
  581. log_warn(LD_CONFIG, "CircuitPriorityHalflife is too small (%f). "
  582. "Adjusting to the smallest value allowed: %f.",
  583. halflife, halflife_default);
  584. halflife = halflife_default;
  585. }
  586. return halflife;
  587. }
  588. /** Adjust the global cell scale factor based on <b>options</b> */
  589. void
  590. cmux_ewma_set_options(const or_options_t *options,
  591. const networkstatus_t *consensus)
  592. {
  593. double halflife;
  594. const char *source;
  595. cell_ewma_initialize_ticks();
  596. /* Both options and consensus can be NULL. This assures us to either get a
  597. * valid configured value or the default one. */
  598. halflife = get_circuit_priority_halflife(options, consensus, &source);
  599. /* convert halflife into halflife-per-tick. */
  600. halflife /= EWMA_TICK_LEN;
  601. /* compute per-tick scale factor. */
  602. ewma_scale_factor = exp( LOG_ONEHALF / halflife );
  603. log_info(LD_OR,
  604. "Enabled cell_ewma algorithm because of value in %s; "
  605. "scale factor is %f per %d seconds",
  606. source, ewma_scale_factor, EWMA_TICK_LEN);
  607. }
  608. /** Return the multiplier necessary to convert the value of a cell sent in
  609. * 'from_tick' to one sent in 'to_tick'. */
  610. static inline double
  611. get_scale_factor(unsigned from_tick, unsigned to_tick)
  612. {
  613. /* This math can wrap around, but that's okay: unsigned overflow is
  614. well-defined */
  615. int diff = (int)(to_tick - from_tick);
  616. return pow(ewma_scale_factor, diff);
  617. }
  618. /** Adjust the cell count of <b>ewma</b> so that it is scaled with respect to
  619. * <b>cur_tick</b> */
  620. static void
  621. scale_single_cell_ewma(cell_ewma_t *ewma, unsigned cur_tick)
  622. {
  623. double factor = get_scale_factor(ewma->last_adjusted_tick, cur_tick);
  624. ewma->cell_count *= factor;
  625. ewma->last_adjusted_tick = cur_tick;
  626. }
  627. /** Adjust the cell count of every active circuit on <b>chan</b> so
  628. * that they are scaled with respect to <b>cur_tick</b> */
  629. static void
  630. scale_active_circuits(ewma_policy_data_t *pol, unsigned cur_tick)
  631. {
  632. double factor;
  633. tor_assert(pol);
  634. tor_assert(pol->active_circuit_pqueue);
  635. factor =
  636. get_scale_factor(
  637. pol->active_circuit_pqueue_last_recalibrated,
  638. cur_tick);
  639. /** Ordinarily it isn't okay to change the value of an element in a heap,
  640. * but it's okay here, since we are preserving the order. */
  641. SMARTLIST_FOREACH_BEGIN(
  642. pol->active_circuit_pqueue,
  643. cell_ewma_t *, e) {
  644. tor_assert(e->last_adjusted_tick ==
  645. pol->active_circuit_pqueue_last_recalibrated);
  646. e->cell_count *= factor;
  647. e->last_adjusted_tick = cur_tick;
  648. } SMARTLIST_FOREACH_END(e);
  649. pol->active_circuit_pqueue_last_recalibrated = cur_tick;
  650. }
  651. /** Rescale <b>ewma</b> to the same scale as <b>pol</b>, and add it to
  652. * <b>pol</b>'s priority queue of active circuits */
  653. static void
  654. add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  655. {
  656. tor_assert(pol);
  657. tor_assert(pol->active_circuit_pqueue);
  658. tor_assert(ewma);
  659. tor_assert(ewma->heap_index == -1);
  660. scale_single_cell_ewma(
  661. ewma,
  662. pol->active_circuit_pqueue_last_recalibrated);
  663. smartlist_pqueue_add(pol->active_circuit_pqueue,
  664. compare_cell_ewma_counts,
  665. offsetof(cell_ewma_t, heap_index),
  666. ewma);
  667. }
  668. /** Remove <b>ewma</b> from <b>pol</b>'s priority queue of active circuits */
  669. static void
  670. remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  671. {
  672. tor_assert(pol);
  673. tor_assert(pol->active_circuit_pqueue);
  674. tor_assert(ewma);
  675. tor_assert(ewma->heap_index != -1);
  676. smartlist_pqueue_remove(pol->active_circuit_pqueue,
  677. compare_cell_ewma_counts,
  678. offsetof(cell_ewma_t, heap_index),
  679. ewma);
  680. }
  681. /** Remove and return the first cell_ewma_t from pol's priority queue of
  682. * active circuits. Requires that the priority queue is nonempty. */
  683. static cell_ewma_t *
  684. pop_first_cell_ewma(ewma_policy_data_t *pol)
  685. {
  686. tor_assert(pol);
  687. tor_assert(pol->active_circuit_pqueue);
  688. return smartlist_pqueue_pop(pol->active_circuit_pqueue,
  689. compare_cell_ewma_counts,
  690. offsetof(cell_ewma_t, heap_index));
  691. }
  692. /**
  693. * Drop all resources held by circuitmux_ewma.c, and deinitialize the
  694. * module. */
  695. void
  696. circuitmux_ewma_free_all(void)
  697. {
  698. ewma_ticks_initialized = 0;
  699. }