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