circuitmux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* * Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file circuitmux.c
  5. * \brief Circuit mux/cell selection abstraction
  6. **/
  7. #include "or.h"
  8. #include "channel.h"
  9. #include "circuitmux.h"
  10. /*
  11. * Private typedefs for circuitmux.c
  12. */
  13. /*
  14. * Map of muxinfos for circuitmux_t to use; struct is defined below (name
  15. * of struct must match HT_HEAD line).
  16. */
  17. typedef struct chanid_circid_muxinfo_map chanid_circid_muxinfo_map_t;
  18. /*
  19. * Hash table entry (yeah, calling it chanid_circid_muxinfo_s seems to
  20. * break the hash table code).
  21. */
  22. typedef struct chanid_circid_muxinfo_t chanid_circid_muxinfo_t;
  23. /*
  24. * Anything the mux wants to store per-circuit in the map; right now just
  25. * a count of queued cells.
  26. */
  27. typedef struct circuit_muxinfo_s circuit_muxinfo_t;
  28. /*
  29. * Structures for circuitmux.c
  30. */
  31. /*
  32. * A circuitmux is a collection of circuits; it tracks which subset
  33. * of the attached circuits are 'active' (i.e., have cells available
  34. * to transmit) and how many cells on each. It expoes three distinct
  35. * interfaces to other components:
  36. *
  37. * To channels, which each have a circuitmux_t, the supported operations
  38. * are:
  39. *
  40. * circuitmux_flush_cells():
  41. *
  42. * Retrieve a cell from one of the active circuits, chosen according to
  43. * the circuitmux_t's cell selection policy.
  44. *
  45. * circuitmux_unlink_all():
  46. *
  47. * The channel is closing down, all circuits must be detached.
  48. *
  49. * To circuits, the exposed operations are:
  50. *
  51. * TODO
  52. *
  53. * To circuit selection policies, the exposed operations are:
  54. *
  55. * TODO
  56. *
  57. * General status inquiries?
  58. *
  59. */
  60. struct circuitmux_s {
  61. /* Keep count of attached, active circuits */
  62. unsigned int n_circuits, n_active_circuits;
  63. /* Total number of queued cells on all circuits */
  64. unsigned int n_cells;
  65. /*
  66. * Map from (channel ID, circuit ID) pairs to circuit_muxinfo_t
  67. */
  68. chanid_circid_muxinfo_map_t *chanid_circid_map;
  69. /*
  70. * Double-linked ring of circuits with queued cells waiting for room to
  71. * free up on this connection's outbuf. Every time we pull cells from
  72. * a circuit, we advance this pointer to the next circuit in the ring.
  73. */
  74. struct circuit_t *active_circuits;
  75. /*
  76. * Priority queue of cell_ewma_t for circuits with queued cells waiting
  77. * for room to free up on this connection's outbuf. Kept in heap order
  78. * according to EWMA.
  79. *
  80. * This is redundant with active_circuits; if we ever decide only to use
  81. * the cell_ewma algorithm for choosing circuits, we can remove
  82. * active_circuits.
  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.
  88. */
  89. unsigned int active_circuit_pqueue_last_recalibrated;
  90. };
  91. /*
  92. * This struct holds whatever we want to store per attached circuit on a
  93. * circuitmux_t; right now, just the count of queued cells and the direction.
  94. */
  95. struct circuit_muxinfo_s {
  96. /* Count of cells on this circuit at last update */
  97. unsigned int cell_count;
  98. /* Direction of flow */
  99. cell_direction_t direction;
  100. };
  101. /*
  102. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  103. * circuit.
  104. */
  105. struct chanid_circid_muxinfo_t {
  106. HT_ENTRY(chanid_circid_muxinfo_t) node;
  107. uint64_t chan_id;
  108. circid_t circ_id;
  109. circuit_muxinfo_t muxinfo;
  110. };
  111. /*
  112. * Static function declarations
  113. */
  114. static INLINE int
  115. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  116. chanid_circid_muxinfo_t *b);
  117. static INLINE unsigned int
  118. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  119. static chanid_circid_muxinfo_t *
  120. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
  121. /* Function definitions */
  122. /**
  123. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  124. * ID and circuit ID for a and b, and return less than, equal to, or greater
  125. * than zero appropriately.
  126. */
  127. static INLINE int
  128. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  129. chanid_circid_muxinfo_t *b)
  130. {
  131. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  132. }
  133. /**
  134. * Helper: return a hash based on circuit ID and channel ID in a.
  135. */
  136. static INLINE unsigned int
  137. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  138. {
  139. return (((unsigned int)(a->circ_id) << 8) ^
  140. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  141. ((unsigned int)(a->chan_id & 0xffffffff)));
  142. }
  143. /* Declare the struct chanid_circid_muxinfo_map type */
  144. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  145. /* Emit a bunch of hash table stuff */
  146. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  147. chanid_circid_entry_hash, chanid_circid_entries_eq);
  148. HT_GENERATE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  149. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  150. malloc, realloc, free);
  151. /*
  152. * Circuitmux alloc/free functions
  153. */
  154. /**
  155. * Allocate a new circuitmux_t
  156. */
  157. circuitmux_t *
  158. circuitmux_alloc(void)
  159. {
  160. circuitmux_t *rv = NULL;
  161. rv = tor_malloc_zero(sizeof(*rv));
  162. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  163. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  164. return rv;
  165. }
  166. /**
  167. * Free a circuitmux_t; the circuits must be detached first with
  168. * circuitmux_detach_all_circuits().
  169. */
  170. void
  171. circuitmux_free(circuitmux_t *cmux)
  172. {
  173. if (!cmux) return;
  174. tor_assert(cmux->n_circuits == 0);
  175. tor_assert(cmux->n_active_circuits == 0);
  176. smartlist_free(cmux->active_circuit_pqueue);
  177. if (cmux->chanid_circid_map) {
  178. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  179. tor_free(cmux->chanid_circid_map);
  180. }
  181. tor_free(cmux);
  182. }
  183. /*
  184. * Circuitmux/circuit attachment status inquiry functions
  185. */
  186. /**
  187. * Query the direction of an attached circuit
  188. */
  189. cell_direction_t
  190. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  191. {
  192. chanid_circid_muxinfo_t *hashent = NULL;
  193. /* Try to find a map entry */
  194. hashent = circuitmux_find_map_entry(cmux, circ);
  195. /*
  196. * This function should only be called on attached circuits; assert that
  197. * we had a map entry.
  198. */
  199. tor_assert(hashent);
  200. /* Return the direction from the map entry */
  201. return hashent->muxinfo.direction;
  202. }
  203. /**
  204. * Find an entry in the cmux's map for this circuit or return NULL if there
  205. * is none.
  206. */
  207. static chanid_circid_muxinfo_t *
  208. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  209. {
  210. chanid_circid_muxinfo_t search, *hashent = NULL;
  211. /* Sanity-check parameters */
  212. tor_assert(cmux);
  213. tor_assert(cmux->chanid_circid_map);
  214. tor_assert(circ);
  215. tor_assert(circ->n_chan);
  216. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  217. search.chan_id = circ->n_chan->global_identifier;
  218. search.circ_id = circ->n_circ_id;
  219. /* Query */
  220. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  221. &search);
  222. /* Found something? */
  223. if (hashent) {
  224. /*
  225. * Assert that the direction makes sense for a hashent we found by
  226. * n_chan/n_circ_id before we return it.
  227. */
  228. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  229. } else {
  230. /* Not there, have we got a p_chan/p_circ_id to try? */
  231. if (circ->magic == OR_CIRCUIT_MAGIC) {
  232. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  233. /* Check for p_chan */
  234. if (TO_OR_CIRCUIT(circ)->p_chan) {
  235. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  236. /* Okay, search for that */
  237. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  238. &search);
  239. /* Find anything? */
  240. if (hashent) {
  241. /* Assert that the direction makes sense before we return it */
  242. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  243. }
  244. }
  245. }
  246. }
  247. /* Okay, hashent is it if it was there */
  248. return hashent;
  249. }
  250. /**
  251. * Query whether a circuit is attached to a circuitmux
  252. */
  253. int
  254. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  255. {
  256. chanid_circid_muxinfo_t *hashent = NULL;
  257. /* Look if it's in the circuit map */
  258. hashent = circuitmux_find_map_entry(cmux, circ);
  259. return (hashent != NULL);
  260. }
  261. /*
  262. * Functions for circuit code to call to update circuit status
  263. */
  264. /**
  265. * Attach a circuit to a circuitmux, for the specified direction.
  266. */
  267. void
  268. circuitmux_attach_circuit(circuitmux_t *cmux, circuit_t *circ,
  269. cell_direction_t direction)
  270. {
  271. channel_t *chan = NULL;
  272. uint64_t channel_id;
  273. circid_t circ_id;
  274. chanid_circid_muxinfo_t search, *hashent = NULL;
  275. unsigned int cell_count;
  276. tor_assert(cmux);
  277. tor_assert(circ);
  278. tor_assert(direction == CELL_DIRECTION_IN ||
  279. direction == CELL_DIRECTION_OUT);
  280. /*
  281. * Figure out which channel we're using, and get the circuit's current
  282. * cell count and circuit ID; assert that the circuit is not already
  283. * attached to another mux.
  284. */
  285. if (direction == CELL_DIRECTION_OUT) {
  286. /* It's n_chan */
  287. chan = circ->n_chan;
  288. cell_count = circ->n_chan_cells.n;
  289. circ_id = circ->n_circ_id;
  290. } else {
  291. /* We want p_chan */
  292. chan = TO_OR_CIRCUIT(circ)->p_chan;
  293. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  294. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  295. }
  296. /* Assert that we did get a channel */
  297. tor_assert(chan);
  298. /* Assert that the circuit ID is sensible */
  299. tor_assert(circ_id != 0);
  300. /* Get the channel ID */
  301. channel_id = chan->global_identifier;
  302. /* See if we already have this one */
  303. search.chan_id = channel_id;
  304. search.circ_id = circ_id;
  305. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  306. &search);
  307. if (hashent) {
  308. /*
  309. * This circuit was already attached to this cmux; make sure the
  310. * directions match and update the cell count and active circuit count.
  311. */
  312. log_info(LD_CIRC,
  313. "Circuit %u on channel " U64_FORMAT " was already attached to "
  314. "cmux %p (trying to attach to %p)",
  315. circ_id, U64_PRINTF_ARG(channel_id),
  316. ((direction == CELL_DIRECTION_OUT) ?
  317. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  318. cmux);
  319. /*
  320. * The mux pointer on this circuit and the direction in result should
  321. * match; otherwise assert.
  322. */
  323. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  324. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  325. tor_assert(hashent->muxinfo.direction == direction);
  326. /*
  327. * Looks okay; just update the cell count and active circuits if we must
  328. */
  329. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  330. --(cmux->n_active_circuits);
  331. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  332. ++(cmux->n_active_circuits);
  333. }
  334. cmux->n_cells -= hashent->muxinfo.cell_count;
  335. cmux->n_cells += cell_count;
  336. hashent->muxinfo.cell_count = cell_count;
  337. /* TODO update active_circuits / active_circuit_pqueue */
  338. } else {
  339. /*
  340. * New circuit; add an entry and update the circuit/active circuit
  341. * counts.
  342. */
  343. log_debug(LD_CIRC,
  344. "Attaching circuit %u on channel " U64_FORMAT " to cmux %p",
  345. circ_id, U64_PRINTF_ARG(channel_id), cmux);
  346. /*
  347. * Assert that the circuit doesn't already have a mux for this
  348. * direction.
  349. */
  350. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  351. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  352. /* Insert it in the map */
  353. hashent = tor_malloc_zero(sizeof(*hashent));
  354. hashent->chan_id = channel_id;
  355. hashent->circ_id = circ_id;
  356. hashent->muxinfo.cell_count = cell_count;
  357. hashent->muxinfo.direction = direction;
  358. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  359. hashent);
  360. /* Set the circuit's mux for this direction */
  361. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  362. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  363. /* Make sure the next/prev pointers are NULL */
  364. if (direction == CELL_DIRECTION_OUT) {
  365. circ->next_active_on_n_chan = NULL;
  366. circ->prev_active_on_n_chan = NULL;
  367. } else {
  368. TO_OR_CIRCUIT(circ)->next_active_on_p_chan = NULL;
  369. TO_OR_CIRCUIT(circ)->prev_active_on_p_chan = NULL;
  370. }
  371. /* Update counters */
  372. ++(cmux->n_circuits);
  373. if (cell_count > 0) {
  374. ++(cmux->n_active_circuits);
  375. circuitmux_make_circuit_active(cmux, circ, direction);
  376. }
  377. cmux->n_cells += cell_count;
  378. /* TODO update active_circuits / active_circuit_pqueue */
  379. }
  380. }
  381. /**
  382. * Detach a circuit from a circuitmux and update all counters as needed;
  383. * no-op if not attached.
  384. */
  385. void
  386. circuitmux_detach_circuit(circuitmux_t *cmux, circuit_t *circ)
  387. {
  388. chanid_circid_muxinfo_t search, *hashent = NULL;
  389. /*
  390. * Use this to keep track of whether we found it for n_chan or
  391. * p_chan for consistency checking.
  392. */
  393. cell_direction_t last_searched_direction;
  394. tor_assert(cmux);
  395. tor_assert(cmux->chanid_circid_map);
  396. tor_assert(circ);
  397. tor_assert(circ->n_chan);
  398. /* See if we have it for n_chan/n_circ_id */
  399. search.chan_id = circ->n_chan->global_identifier;
  400. search.circ_id = circ->n_circ_id;
  401. hashent = HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  402. &search);
  403. last_searched_direction = CELL_DIRECTION_OUT;
  404. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  405. if (!hashent) {
  406. if (circ->magic == OR_CIRCUIT_MAGIC) {
  407. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  408. if (TO_OR_CIRCUIT(circ)->p_chan) {
  409. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  410. hashent = HT_REMOVE(chanid_circid_muxinfo_map,
  411. cmux->chanid_circid_map,
  412. &search);
  413. last_searched_direction = CELL_DIRECTION_IN;
  414. }
  415. }
  416. }
  417. /* If hashent isn't NULL, we just removed it from the map */
  418. if (hashent) {
  419. /* Update counters */
  420. --(cmux->n_circuits);
  421. if (hashent->muxinfo.cell_count > 0) --(cmux->n_active_circuits);
  422. cmux->n_cells -= hashent->muxinfo.cell_count;
  423. /* TODO update active_circuits / active_circuit_pqueue */
  424. /* Consistency check: the direction must match the direction searched */
  425. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  426. /* Clear the circuit's mux for this direction */
  427. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  428. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  429. /* Free the hash entry */
  430. tor_free(hashent);
  431. }
  432. }