circuitmux.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 "circuitlist.h"
  10. #include "circuitmux.h"
  11. /*
  12. * Private typedefs for circuitmux.c
  13. */
  14. /*
  15. * Map of muxinfos for circuitmux_t to use; struct is defined below (name
  16. * of struct must match HT_HEAD line).
  17. */
  18. typedef struct chanid_circid_muxinfo_map chanid_circid_muxinfo_map_t;
  19. /*
  20. * Hash table entry (yeah, calling it chanid_circid_muxinfo_s seems to
  21. * break the hash table code).
  22. */
  23. typedef struct chanid_circid_muxinfo_t chanid_circid_muxinfo_t;
  24. /*
  25. * Anything the mux wants to store per-circuit in the map; right now just
  26. * a count of queued cells.
  27. */
  28. typedef struct circuit_muxinfo_s circuit_muxinfo_t;
  29. /*
  30. * Structures for circuitmux.c
  31. */
  32. /*
  33. * A circuitmux is a collection of circuits; it tracks which subset
  34. * of the attached circuits are 'active' (i.e., have cells available
  35. * to transmit) and how many cells on each. It expoes three distinct
  36. * interfaces to other components:
  37. *
  38. * To channels, which each have a circuitmux_t, the supported operations
  39. * are:
  40. *
  41. * circuitmux_flush_cells():
  42. *
  43. * Retrieve a cell from one of the active circuits, chosen according to
  44. * the circuitmux_t's cell selection policy.
  45. *
  46. * circuitmux_unlink_all():
  47. *
  48. * The channel is closing down, all circuits must be detached.
  49. *
  50. * To circuits, the exposed operations are:
  51. *
  52. * TODO
  53. *
  54. * To circuit selection policies, the exposed operations are:
  55. *
  56. * TODO
  57. *
  58. * General status inquiries?
  59. *
  60. */
  61. struct circuitmux_s {
  62. /* Keep count of attached, active circuits */
  63. unsigned int n_circuits, n_active_circuits;
  64. /* Total number of queued cells on all circuits */
  65. unsigned int n_cells;
  66. /*
  67. * Map from (channel ID, circuit ID) pairs to circuit_muxinfo_t
  68. */
  69. chanid_circid_muxinfo_map_t *chanid_circid_map;
  70. /*
  71. * Double-linked ring of circuits with queued cells waiting for room to
  72. * free up on this connection's outbuf. Every time we pull cells from
  73. * a circuit, we advance this pointer to the next circuit in the ring.
  74. */
  75. struct circuit_t *active_circuits_head, *active_circuits_tail;
  76. /*
  77. * Circuitmux policy; if this is non-NULL, it can override the built-
  78. * in round-robin active circuits behavior. This is how EWMA works in
  79. * the new circuitmux_t world.
  80. */
  81. const circuitmux_policy_t *policy;
  82. /* Policy-specific data */
  83. circuitmux_policy_data_t *policy_data;
  84. };
  85. /*
  86. * This struct holds whatever we want to store per attached circuit on a
  87. * circuitmux_t; right now, just the count of queued cells and the direction.
  88. */
  89. struct circuit_muxinfo_s {
  90. /* Count of cells on this circuit at last update */
  91. unsigned int cell_count;
  92. /* Direction of flow */
  93. cell_direction_t direction;
  94. /* Policy-specific data */
  95. circuitmux_policy_circ_data_t *policy_data;
  96. };
  97. /*
  98. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  99. * circuit.
  100. */
  101. struct chanid_circid_muxinfo_t {
  102. HT_ENTRY(chanid_circid_muxinfo_t) node;
  103. uint64_t chan_id;
  104. circid_t circ_id;
  105. circuit_muxinfo_t muxinfo;
  106. };
  107. /*
  108. * Static function declarations
  109. */
  110. static INLINE int
  111. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  112. chanid_circid_muxinfo_t *b);
  113. static INLINE unsigned int
  114. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  115. static chanid_circid_muxinfo_t *
  116. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
  117. static void
  118. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  119. cell_direction_t direction);
  120. static void
  121. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  122. cell_direction_t direction);
  123. static INLINE circuit_t **
  124. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  125. static INLINE circuit_t **
  126. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  127. /* Function definitions */
  128. /**
  129. * Linked list helpers
  130. */
  131. static INLINE circuit_t **
  132. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  133. {
  134. tor_assert(cmux);
  135. tor_assert(circ);
  136. if (circ->n_mux == cmux) return &(circ->next_active_on_n_chan);
  137. else {
  138. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  139. return &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  140. }
  141. }
  142. static INLINE circuit_t **
  143. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  144. {
  145. tor_assert(cmux);
  146. tor_assert(circ);
  147. if (circ->n_mux == cmux) return &(circ->prev_active_on_n_chan);
  148. else {
  149. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  150. return &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  151. }
  152. }
  153. /**
  154. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  155. * ID and circuit ID for a and b, and return less than, equal to, or greater
  156. * than zero appropriately.
  157. */
  158. static INLINE int
  159. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  160. chanid_circid_muxinfo_t *b)
  161. {
  162. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  163. }
  164. /**
  165. * Helper: return a hash based on circuit ID and channel ID in a.
  166. */
  167. static INLINE unsigned int
  168. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  169. {
  170. return (((unsigned int)(a->circ_id) << 8) ^
  171. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  172. ((unsigned int)(a->chan_id & 0xffffffff)));
  173. }
  174. /* Declare the struct chanid_circid_muxinfo_map type */
  175. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  176. /* Emit a bunch of hash table stuff */
  177. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  178. chanid_circid_entry_hash, chanid_circid_entries_eq);
  179. HT_GENERATE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  180. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  181. malloc, realloc, free);
  182. /*
  183. * Circuitmux alloc/free functions
  184. */
  185. /**
  186. * Allocate a new circuitmux_t
  187. */
  188. circuitmux_t *
  189. circuitmux_alloc(void)
  190. {
  191. circuitmux_t *rv = NULL;
  192. rv = tor_malloc_zero(sizeof(*rv));
  193. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  194. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  195. return rv;
  196. }
  197. /**
  198. * Detach all circuits from a circuitmux (use before circuitmux_free())
  199. */
  200. void
  201. circuitmux_detach_all_circuits(circuitmux_t *cmux)
  202. {
  203. chanid_circid_muxinfo_t **i = NULL, *to_remove;
  204. channel_t *chan = NULL;
  205. circuit_t *circ = NULL;
  206. tor_assert(cmux);
  207. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  208. while (i) {
  209. to_remove = *i;
  210. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  211. if (to_remove) {
  212. /* Find a channel and circuit */
  213. chan = channel_find_by_global_id(to_remove->chan_id);
  214. if (chan) {
  215. circ = circuit_get_by_circid_channel(to_remove->circ_id, chan);
  216. if (circ) {
  217. /* Clear the circuit's mux for this direction */
  218. if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
  219. /* Clear n_mux */
  220. circ->n_mux = NULL;
  221. /*
  222. * Update active_circuits et al.; this does policy notifies, so
  223. * comes before freeing policy data
  224. */
  225. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_OUT);
  226. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  227. /*
  228. * It has a sensible p_chan and direction == CELL_DIRECTION_IN,
  229. * so clear p_mux.
  230. */
  231. TO_OR_CIRCUIT(circ)->p_mux = NULL;
  232. /*
  233. * Update active_circuits et al.; this does policy notifies, so
  234. * comes before freeing policy data
  235. */
  236. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_IN);
  237. } else {
  238. /* Complain and move on */
  239. log_warn(LD_CIRC,
  240. "Circuit %d/channel " U64_FORMAT " had direction == "
  241. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  242. to_remove->circ_id,
  243. U64_PRINTF_ARG(to_remove->chan_id));
  244. }
  245. /* Free policy-specific data if we have it */
  246. if (to_remove->muxinfo.policy_data) {
  247. /*
  248. * If we have policy data, assert that we have the means to
  249. * free it
  250. */
  251. tor_assert(cmux->policy);
  252. tor_assert(cmux->policy->free_circ_data);
  253. /* Call free_circ_data() */
  254. cmux->policy->free_circ_data(cmux,
  255. cmux->policy_data,
  256. circ,
  257. to_remove->muxinfo.policy_data);
  258. to_remove->muxinfo.policy_data = NULL;
  259. }
  260. } else {
  261. /* Complain and move on */
  262. log_warn(LD_CIRC,
  263. "Couldn't find circuit %d (for channel " U64_FORMAT ")",
  264. to_remove->circ_id,
  265. U64_PRINTF_ARG(to_remove->chan_id));
  266. }
  267. } else {
  268. /* Complain and move on */
  269. log_warn(LD_CIRC,
  270. "Couldn't find channel " U64_FORMAT " (for circuit id %d)",
  271. U64_PRINTF_ARG(to_remove->chan_id),
  272. to_remove->circ_id);
  273. }
  274. /* Assert that we don't have un-freed policy data for this circuit */
  275. tor_assert(to_remove->muxinfo.policy_data == NULL);
  276. /* Free it */
  277. tor_free(to_remove);
  278. }
  279. }
  280. cmux->n_circuits = 0;
  281. cmux->n_active_circuits = 0;
  282. cmux->n_cells = 0;
  283. }
  284. /**
  285. * Free a circuitmux_t; the circuits must be detached first with
  286. * circuitmux_detach_all_circuits().
  287. */
  288. void
  289. circuitmux_free(circuitmux_t *cmux)
  290. {
  291. if (!cmux) return;
  292. tor_assert(cmux->n_circuits == 0);
  293. tor_assert(cmux->n_active_circuits == 0);
  294. /*
  295. * Free policy-specific data if we have any; we don't
  296. * need to do circuitmux_set_policy(cmux, NULL) to cover
  297. * the circuits because they would have been handled in
  298. * circuitmux_detach_all_circuits() before this was
  299. * called.
  300. */
  301. if (cmux->policy && cmux->policy->free_cmux_data) {
  302. if (cmux->policy_data) {
  303. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  304. cmux->policy_data = NULL;
  305. }
  306. } else tor_assert(cmux->policy_data == NULL);
  307. if (cmux->chanid_circid_map) {
  308. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  309. tor_free(cmux->chanid_circid_map);
  310. }
  311. tor_free(cmux);
  312. }
  313. /*
  314. * Circuitmux policy control functions
  315. */
  316. /**
  317. * Remove any policy installed on cmux; all policy data will be freed and
  318. * cmux behavior will revert to the built-in round-robin active_circuits
  319. * mechanism.
  320. */
  321. void
  322. circuitmux_clear_policy(circuitmux_t *cmux)
  323. {
  324. tor_assert(cmux);
  325. /* Internally, this is just setting policy to NULL */
  326. if (cmux->policy) {
  327. circuitmux_set_policy(cmux, NULL);
  328. }
  329. }
  330. /**
  331. * Return the policy currently installed on a circuitmux_t
  332. */
  333. const circuitmux_policy_t *
  334. circuitmux_get_policy(circuitmux_t *cmux)
  335. {
  336. tor_assert(cmux);
  337. return cmux->policy;
  338. }
  339. /**
  340. * Set policy; allocate for new policy, detach all circuits from old policy
  341. * if any, attach them to new policy, and free old policy data.
  342. */
  343. void
  344. circuitmux_set_policy(circuitmux_t *cmux,
  345. const circuitmux_policy_t *pol)
  346. {
  347. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  348. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  349. chanid_circid_muxinfo_t **i = NULL;
  350. channel_t *chan = NULL;
  351. uint64_t last_chan_id_searched = 0;
  352. circuit_t *circ = NULL;
  353. tor_assert(cmux);
  354. /* Set up variables */
  355. old_pol = cmux->policy;
  356. old_pol_data = cmux->policy_data;
  357. new_pol = pol;
  358. /* Check if this is the trivial case */
  359. if (old_pol == new_pol) return;
  360. /* Allocate data for new policy, if any */
  361. if (new_pol && new_pol->alloc_cmux_data) {
  362. /*
  363. * If alloc_cmux_data is not null, then we expect to get some policy
  364. * data. Assert that we also have free_cmux_data so we can free it
  365. * when the time comes, and allocate it.
  366. */
  367. tor_assert(new_pol->free_cmux_data);
  368. new_pol_data = new_pol->alloc_cmux_data(cmux);
  369. tor_assert(new_pol_data);
  370. }
  371. /* Install new policy and new policy data on cmux */
  372. cmux->policy = new_pol;
  373. cmux->policy_data = new_pol_data;
  374. /* Iterate over all circuits, attaching/detaching each one */
  375. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  376. while (i) {
  377. /* Assert that this entry isn't NULL */
  378. tor_assert(*i);
  379. /*
  380. * Get the channel; since normal case is all circuits on the mux share a
  381. * channel, we cache last_chan_id_searched
  382. */
  383. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  384. chan = channel_find_by_global_id((*i)->chan_id);
  385. last_chan_id_searched = (*i)->chan_id;
  386. }
  387. tor_assert(chan);
  388. /* Get the circuit */
  389. circ = circuit_get_by_circid_channel((*i)->circ_id, chan);
  390. tor_assert(circ);
  391. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  392. if (old_pol && old_pol->notify_circ_inactive &&
  393. (*i)->muxinfo.cell_count > 0) {
  394. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  395. (*i)->muxinfo.policy_data);
  396. }
  397. /* Need to free old policy data? */
  398. if ((*i)->muxinfo.policy_data) {
  399. /* Assert that we have the means to free it if we have policy data */
  400. tor_assert(old_pol);
  401. tor_assert(old_pol->free_circ_data);
  402. /* Free it */
  403. old_pol->free_circ_data(cmux, old_pol_data, circ,
  404. (*i)->muxinfo.policy_data);
  405. (*i)->muxinfo.policy_data = NULL;
  406. }
  407. /* Need to allocate new policy data? */
  408. if (new_pol && new_pol->alloc_circ_data) {
  409. /*
  410. * If alloc_circ_data is not null, we expect to get some per-circuit
  411. * policy data. Assert that we also have free_circ_data so we can
  412. * free it when the time comes, and allocate it.
  413. */
  414. tor_assert(new_pol->free_circ_data);
  415. (*i)->muxinfo.policy_data =
  416. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  417. (*i)->muxinfo.direction,
  418. (*i)->muxinfo.cell_count);
  419. }
  420. /* Need to make active on new policy? */
  421. if (new_pol && new_pol->notify_circ_active &&
  422. (*i)->muxinfo.cell_count > 0) {
  423. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  424. (*i)->muxinfo.policy_data);
  425. }
  426. /* Advance to next circuit map entry */
  427. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  428. }
  429. /* Free data for old policy, if any */
  430. if (old_pol_data) {
  431. /*
  432. * If we had old policy data, we should have an old policy and a free
  433. * function for it.
  434. */
  435. tor_assert(old_pol);
  436. tor_assert(old_pol->free_cmux_data);
  437. old_pol->free_cmux_data(cmux, old_pol_data);
  438. old_pol_data = NULL;
  439. }
  440. }
  441. /*
  442. * Circuitmux/circuit attachment status inquiry functions
  443. */
  444. /**
  445. * Query the direction of an attached circuit
  446. */
  447. cell_direction_t
  448. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  449. {
  450. chanid_circid_muxinfo_t *hashent = NULL;
  451. /* Try to find a map entry */
  452. hashent = circuitmux_find_map_entry(cmux, circ);
  453. /*
  454. * This function should only be called on attached circuits; assert that
  455. * we had a map entry.
  456. */
  457. tor_assert(hashent);
  458. /* Return the direction from the map entry */
  459. return hashent->muxinfo.direction;
  460. }
  461. /**
  462. * Find an entry in the cmux's map for this circuit or return NULL if there
  463. * is none.
  464. */
  465. static chanid_circid_muxinfo_t *
  466. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  467. {
  468. chanid_circid_muxinfo_t search, *hashent = NULL;
  469. /* Sanity-check parameters */
  470. tor_assert(cmux);
  471. tor_assert(cmux->chanid_circid_map);
  472. tor_assert(circ);
  473. tor_assert(circ->n_chan);
  474. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  475. search.chan_id = circ->n_chan->global_identifier;
  476. search.circ_id = circ->n_circ_id;
  477. /* Query */
  478. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  479. &search);
  480. /* Found something? */
  481. if (hashent) {
  482. /*
  483. * Assert that the direction makes sense for a hashent we found by
  484. * n_chan/n_circ_id before we return it.
  485. */
  486. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  487. } else {
  488. /* Not there, have we got a p_chan/p_circ_id to try? */
  489. if (circ->magic == OR_CIRCUIT_MAGIC) {
  490. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  491. /* Check for p_chan */
  492. if (TO_OR_CIRCUIT(circ)->p_chan) {
  493. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  494. /* Okay, search for that */
  495. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  496. &search);
  497. /* Find anything? */
  498. if (hashent) {
  499. /* Assert that the direction makes sense before we return it */
  500. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  501. }
  502. }
  503. }
  504. }
  505. /* Okay, hashent is it if it was there */
  506. return hashent;
  507. }
  508. /**
  509. * Query whether a circuit is attached to a circuitmux
  510. */
  511. int
  512. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  513. {
  514. chanid_circid_muxinfo_t *hashent = NULL;
  515. /* Look if it's in the circuit map */
  516. hashent = circuitmux_find_map_entry(cmux, circ);
  517. return (hashent != NULL);
  518. }
  519. /**
  520. * Query whether a circuit is active on a circuitmux
  521. */
  522. int
  523. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  524. {
  525. chanid_circid_muxinfo_t *hashent = NULL;
  526. int is_active = 0;
  527. tor_assert(cmux);
  528. tor_assert(circ);
  529. /* Look if it's in the circuit map */
  530. hashent = circuitmux_find_map_entry(cmux, circ);
  531. if (hashent) {
  532. /* Check the number of cells on this circuit */
  533. is_active = (hashent->muxinfo.cell_count > 0);
  534. }
  535. /* else not attached, so not active */
  536. return is_active;
  537. }
  538. /**
  539. * Query number of available cells for a circuit on a circuitmux
  540. */
  541. unsigned int
  542. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  543. {
  544. chanid_circid_muxinfo_t *hashent = NULL;
  545. unsigned int n_cells = 0;
  546. tor_assert(cmux);
  547. tor_assert(circ);
  548. /* Look if it's in the circuit map */
  549. hashent = circuitmux_find_map_entry(cmux, circ);
  550. if (hashent) {
  551. /* Just get the cell count for this circuit */
  552. n_cells = hashent->muxinfo.cell_count;
  553. }
  554. /* else not attached, so 0 cells */
  555. return n_cells;
  556. }
  557. /**
  558. * Query total number of available cells on a circuitmux
  559. */
  560. unsigned int
  561. circuitmux_num_cells(circuitmux_t *cmux)
  562. {
  563. tor_assert(cmux);
  564. return cmux->n_cells;
  565. }
  566. /**
  567. * Query total number of circuits active on a circuitmux
  568. */
  569. unsigned int
  570. circuitmux_num_active_circuits(circuitmux_t *cmux)
  571. {
  572. tor_assert(cmux);
  573. return cmux->n_active_circuits;
  574. }
  575. /**
  576. * Query total number of circuits attached to a circuitmux
  577. */
  578. unsigned int
  579. circuitmux_num_circuits(circuitmux_t *cmux)
  580. {
  581. tor_assert(cmux);
  582. return cmux->n_circuits;
  583. }
  584. /*
  585. * Functions for circuit code to call to update circuit status
  586. */
  587. /**
  588. * Attach a circuit to a circuitmux, for the specified direction.
  589. */
  590. void
  591. circuitmux_attach_circuit(circuitmux_t *cmux, circuit_t *circ,
  592. cell_direction_t direction)
  593. {
  594. channel_t *chan = NULL;
  595. uint64_t channel_id;
  596. circid_t circ_id;
  597. chanid_circid_muxinfo_t search, *hashent = NULL;
  598. unsigned int cell_count;
  599. tor_assert(cmux);
  600. tor_assert(circ);
  601. tor_assert(direction == CELL_DIRECTION_IN ||
  602. direction == CELL_DIRECTION_OUT);
  603. /*
  604. * Figure out which channel we're using, and get the circuit's current
  605. * cell count and circuit ID; assert that the circuit is not already
  606. * attached to another mux.
  607. */
  608. if (direction == CELL_DIRECTION_OUT) {
  609. /* It's n_chan */
  610. chan = circ->n_chan;
  611. cell_count = circ->n_chan_cells.n;
  612. circ_id = circ->n_circ_id;
  613. } else {
  614. /* We want p_chan */
  615. chan = TO_OR_CIRCUIT(circ)->p_chan;
  616. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  617. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  618. }
  619. /* Assert that we did get a channel */
  620. tor_assert(chan);
  621. /* Assert that the circuit ID is sensible */
  622. tor_assert(circ_id != 0);
  623. /* Get the channel ID */
  624. channel_id = chan->global_identifier;
  625. /* See if we already have this one */
  626. search.chan_id = channel_id;
  627. search.circ_id = circ_id;
  628. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  629. &search);
  630. if (hashent) {
  631. /*
  632. * This circuit was already attached to this cmux; make sure the
  633. * directions match and update the cell count and active circuit count.
  634. */
  635. log_info(LD_CIRC,
  636. "Circuit %u on channel " U64_FORMAT " was already attached to "
  637. "cmux %p (trying to attach to %p)",
  638. circ_id, U64_PRINTF_ARG(channel_id),
  639. ((direction == CELL_DIRECTION_OUT) ?
  640. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  641. cmux);
  642. /*
  643. * The mux pointer on this circuit and the direction in result should
  644. * match; otherwise assert.
  645. */
  646. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  647. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  648. tor_assert(hashent->muxinfo.direction == direction);
  649. /*
  650. * Looks okay; just update the cell count and active circuits if we must
  651. */
  652. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  653. --(cmux->n_active_circuits);
  654. circuitmux_make_circuit_inactive(cmux, circ, direction);
  655. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  656. ++(cmux->n_active_circuits);
  657. circuitmux_make_circuit_active(cmux, circ, direction);
  658. }
  659. cmux->n_cells -= hashent->muxinfo.cell_count;
  660. cmux->n_cells += cell_count;
  661. hashent->muxinfo.cell_count = cell_count;
  662. } else {
  663. /*
  664. * New circuit; add an entry and update the circuit/active circuit
  665. * counts.
  666. */
  667. log_debug(LD_CIRC,
  668. "Attaching circuit %u on channel " U64_FORMAT " to cmux %p",
  669. circ_id, U64_PRINTF_ARG(channel_id), cmux);
  670. /*
  671. * Assert that the circuit doesn't already have a mux for this
  672. * direction.
  673. */
  674. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  675. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  676. /* Insert it in the map */
  677. hashent = tor_malloc_zero(sizeof(*hashent));
  678. hashent->chan_id = channel_id;
  679. hashent->circ_id = circ_id;
  680. hashent->muxinfo.cell_count = cell_count;
  681. hashent->muxinfo.direction = direction;
  682. /* Allocate policy specific circuit data if we need it */
  683. if (cmux->policy && cmux->policy->alloc_circ_data) {
  684. /* Assert that we have the means to free policy-specific data */
  685. tor_assert(cmux->policy->free_circ_data);
  686. /* Allocate it */
  687. hashent->muxinfo.policy_data =
  688. cmux->policy->alloc_circ_data(cmux,
  689. cmux->policy_data,
  690. circ,
  691. direction,
  692. cell_count);
  693. /* If we wanted policy data, it's an error not to get any */
  694. tor_assert(hashent->muxinfo.policy_data);
  695. }
  696. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  697. hashent);
  698. /* Set the circuit's mux for this direction */
  699. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  700. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  701. /* Make sure the next/prev pointers are NULL */
  702. if (direction == CELL_DIRECTION_OUT) {
  703. circ->next_active_on_n_chan = NULL;
  704. circ->prev_active_on_n_chan = NULL;
  705. } else {
  706. TO_OR_CIRCUIT(circ)->next_active_on_p_chan = NULL;
  707. TO_OR_CIRCUIT(circ)->prev_active_on_p_chan = NULL;
  708. }
  709. /* Update counters */
  710. ++(cmux->n_circuits);
  711. if (cell_count > 0) {
  712. ++(cmux->n_active_circuits);
  713. circuitmux_make_circuit_active(cmux, circ, direction);
  714. }
  715. cmux->n_cells += cell_count;
  716. }
  717. }
  718. /**
  719. * Detach a circuit from a circuitmux and update all counters as needed;
  720. * no-op if not attached.
  721. */
  722. void
  723. circuitmux_detach_circuit(circuitmux_t *cmux, circuit_t *circ)
  724. {
  725. chanid_circid_muxinfo_t search, *hashent = NULL;
  726. /*
  727. * Use this to keep track of whether we found it for n_chan or
  728. * p_chan for consistency checking.
  729. */
  730. cell_direction_t last_searched_direction;
  731. tor_assert(cmux);
  732. tor_assert(cmux->chanid_circid_map);
  733. tor_assert(circ);
  734. tor_assert(circ->n_chan);
  735. /* See if we have it for n_chan/n_circ_id */
  736. search.chan_id = circ->n_chan->global_identifier;
  737. search.circ_id = circ->n_circ_id;
  738. hashent = HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  739. &search);
  740. last_searched_direction = CELL_DIRECTION_OUT;
  741. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  742. if (!hashent) {
  743. if (circ->magic == OR_CIRCUIT_MAGIC) {
  744. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  745. if (TO_OR_CIRCUIT(circ)->p_chan) {
  746. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  747. hashent = HT_REMOVE(chanid_circid_muxinfo_map,
  748. cmux->chanid_circid_map,
  749. &search);
  750. last_searched_direction = CELL_DIRECTION_IN;
  751. }
  752. }
  753. }
  754. /* If hashent isn't NULL, we just removed it from the map */
  755. if (hashent) {
  756. /* Update counters */
  757. --(cmux->n_circuits);
  758. if (hashent->muxinfo.cell_count > 0) {
  759. --(cmux->n_active_circuits);
  760. /* This does policy notifies, so comes before freeing policy data */
  761. circuitmux_make_circuit_inactive(cmux, circ, last_searched_direction);
  762. }
  763. cmux->n_cells -= hashent->muxinfo.cell_count;
  764. /* Free policy-specific data if we have it */
  765. if (hashent->muxinfo.policy_data) {
  766. /* If we have policy data, assert that we have the means to free it */
  767. tor_assert(cmux->policy);
  768. tor_assert(cmux->policy->free_circ_data);
  769. /* Call free_circ_data() */
  770. cmux->policy->free_circ_data(cmux,
  771. cmux->policy_data,
  772. circ,
  773. hashent->muxinfo.policy_data);
  774. hashent->muxinfo.policy_data = NULL;
  775. }
  776. /* Consistency check: the direction must match the direction searched */
  777. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  778. /* Clear the circuit's mux for this direction */
  779. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  780. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  781. /* Free the hash entry */
  782. tor_free(hashent);
  783. }
  784. }
  785. /**
  786. * Make a circuit active; update active list and policy-specific info, but
  787. * we don't mess with the counters or hash table here.
  788. */
  789. static void
  790. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  791. cell_direction_t direction)
  792. {
  793. circuit_t **next_active = NULL, **prev_active = NULL, **next_prev = NULL;
  794. circuitmux_t *circuit_cmux = NULL;
  795. chanid_circid_muxinfo_t *hashent = NULL;
  796. channel_t *chan = NULL;
  797. circid_t circ_id;
  798. int already_active;
  799. tor_assert(cmux);
  800. tor_assert(circ);
  801. tor_assert(direction == CELL_DIRECTION_OUT ||
  802. direction == CELL_DIRECTION_IN);
  803. /* Get the right set of active list links for this direction */
  804. if (direction == CELL_DIRECTION_OUT) {
  805. next_active = &(circ->next_active_on_n_chan);
  806. prev_active = &(circ->prev_active_on_n_chan);
  807. circuit_cmux = circ->n_mux;
  808. chan = circ->n_chan;
  809. circ_id = circ->n_circ_id;
  810. } else {
  811. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  812. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  813. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  814. chan = TO_OR_CIRCUIT(circ)->p_chan;
  815. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  816. }
  817. /* Assert that it is attached to this mux and a channel */
  818. tor_assert(cmux == circuit_cmux);
  819. tor_assert(chan != NULL);
  820. /*
  821. * Check if the circuit really was inactive; if it's active, at least one
  822. * of the next_active and prev_active pointers will not be NULL, or this
  823. * circuit will be either the head or tail of the list for this cmux.
  824. */
  825. already_active = (*prev_active != NULL || *next_active != NULL ||
  826. cmux->active_circuits_head == circ ||
  827. cmux->active_circuits_tail == circ);
  828. /* If we're already active, log a warning and finish */
  829. if (already_active) {
  830. log_warn(LD_CIRC,
  831. "Circuit %d on channel " U64_FORMAT " was already active",
  832. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  833. return;
  834. }
  835. /*
  836. * This is going at the head of the list; if the old head is not NULL,
  837. * then its prev pointer should point to this.
  838. */
  839. *next_active = cmux->active_circuits_head; /* Next is old head */
  840. *prev_active = NULL; /* Prev is NULL (this will be the head) */
  841. if (cmux->active_circuits_head) {
  842. /* The list had an old head; update its prev pointer */
  843. next_prev =
  844. circuitmux_prev_active_circ_p(cmux, cmux->active_circuits_head);
  845. tor_assert(next_prev);
  846. *next_prev = circ;
  847. } else {
  848. /* The list was empty; this becomes the tail as well */
  849. cmux->active_circuits_tail = circ;
  850. }
  851. /* This becomes the new head of the list */
  852. cmux->active_circuits_head = circ;
  853. /* Policy-specific notification */
  854. if (cmux->policy &&
  855. cmux->policy->notify_circ_active) {
  856. /* Okay, we need to check the circuit for policy data now */
  857. hashent = circuitmux_find_map_entry(cmux, circ);
  858. /* We should have found something */
  859. tor_assert(hashent);
  860. /* Notify */
  861. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  862. circ, hashent->muxinfo.policy_data);
  863. }
  864. }
  865. /**
  866. * Make a circuit inactive; update active list and policy-specific info, but
  867. * we don't mess with the counters or hash table here.
  868. */
  869. static void
  870. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  871. cell_direction_t direction)
  872. {
  873. circuit_t **next_active = NULL, **prev_active = NULL;
  874. circuit_t **next_prev = NULL, **prev_next = NULL;
  875. circuitmux_t *circuit_cmux = NULL;
  876. chanid_circid_muxinfo_t *hashent = NULL;
  877. channel_t *chan = NULL;
  878. circid_t circ_id;
  879. int already_inactive;
  880. tor_assert(cmux);
  881. tor_assert(circ);
  882. tor_assert(direction == CELL_DIRECTION_OUT ||
  883. direction == CELL_DIRECTION_IN);
  884. /* Get the right set of active list links for this direction */
  885. if (direction == CELL_DIRECTION_OUT) {
  886. next_active = &(circ->next_active_on_n_chan);
  887. prev_active = &(circ->prev_active_on_n_chan);
  888. circuit_cmux = circ->n_mux;
  889. chan = circ->n_chan;
  890. circ_id = circ->n_circ_id;
  891. } else {
  892. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  893. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  894. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  895. chan = TO_OR_CIRCUIT(circ)->p_chan;
  896. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  897. }
  898. /* Assert that it is attached to this mux and a channel */
  899. tor_assert(cmux == circuit_cmux);
  900. tor_assert(chan != NULL);
  901. /*
  902. * Check if the circuit really was active; if it's inactive, the
  903. * next_active and prev_active pointers will be NULL and this circuit
  904. * will not be the head or tail of the list for this cmux.
  905. */
  906. already_inactive = (*prev_active == NULL && *next_active == NULL &&
  907. cmux->active_circuits_head != circ &&
  908. cmux->active_circuits_tail != circ);
  909. /* If we're already inactive, log a warning and finish */
  910. if (already_inactive) {
  911. log_warn(LD_CIRC,
  912. "Circuit %d on channel " U64_FORMAT " was already inactive",
  913. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  914. return;
  915. }
  916. /* Remove from the list; first get next_prev and prev_next */
  917. if (*next_active) {
  918. /*
  919. * If there's a next circuit, its previous circuit becomes this
  920. * circuit's previous circuit.
  921. */
  922. next_prev = circuitmux_prev_active_circ_p(cmux, *next_active);
  923. } else {
  924. /* Else, the tail becomes this circuit's previous circuit */
  925. next_prev = &(cmux->active_circuits_tail);
  926. }
  927. /* Got next_prev, now prev_next */
  928. if (*prev_active) {
  929. /*
  930. * If there's a previous circuit, its next circuit becomes this circuit's
  931. * next circuit.
  932. */
  933. prev_next = circuitmux_next_active_circ_p(cmux, *prev_active);
  934. } else {
  935. /* Else, the head becomes this circuit's next circuit */
  936. prev_next = &(cmux->active_circuits_head);
  937. }
  938. /* Assert that we got sensible values for the next/prev pointers */
  939. tor_assert(next_prev != NULL);
  940. tor_assert(prev_next != NULL);
  941. /* Update the next/prev pointers - this removes circ from the list */
  942. *next_prev = *prev_active;
  943. *prev_next = *next_active;
  944. /* Now null out prev_active/next_active */
  945. *prev_active = NULL;
  946. *next_active = NULL;
  947. /* Policy-specific notification */
  948. if (cmux->policy &&
  949. cmux->policy->notify_circ_inactive) {
  950. /* Okay, we need to check the circuit for policy data now */
  951. hashent = circuitmux_find_map_entry(cmux, circ);
  952. /* We should have found something */
  953. tor_assert(hashent);
  954. /* Notify */
  955. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  956. circ, hashent->muxinfo.policy_data);
  957. }
  958. }
  959. /**
  960. * Clear the cell counter for a circuit on a circuitmux
  961. */
  962. void
  963. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  964. {
  965. /* This is the same as setting the cell count to zero */
  966. circuitmux_set_num_cells(cmux, circ, 0);
  967. }
  968. /**
  969. * Set the cell counter for a circuit on a circuitmux
  970. */
  971. void
  972. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  973. unsigned int n_cells)
  974. {
  975. chanid_circid_muxinfo_t *hashent = NULL;
  976. tor_assert(cmux);
  977. tor_assert(circ);
  978. /* Search for this circuit's entry */
  979. hashent = circuitmux_find_map_entry(cmux, circ);
  980. /* Assert that we found one */
  981. tor_assert(hashent);
  982. /* Update cmux cell counter */
  983. cmux->n_cells -= hashent->muxinfo.cell_count;
  984. cmux->n_cells += n_cells;
  985. /* Do we need to notify a cmux policy? */
  986. if (cmux->policy && cmux->policy->notify_set_n_cells) {
  987. /* Call notify_set_n_cells */
  988. cmux->policy->notify_set_n_cells(cmux,
  989. cmux->policy_data,
  990. circ,
  991. hashent->muxinfo.policy_data,
  992. n_cells);
  993. }
  994. /*
  995. * Update cmux active circuit counter: is the old cell count > 0 and the
  996. * new cell count == 0 ?
  997. */
  998. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  999. --(cmux->n_active_circuits);
  1000. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1001. /* Is the old cell count == 0 and the new cell count > 0 ? */
  1002. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  1003. ++(cmux->n_active_circuits);
  1004. circuitmux_make_circuit_active(cmux, circ, hashent->muxinfo.direction);
  1005. }
  1006. /* Update hash entry cell counter */
  1007. hashent->muxinfo.cell_count = n_cells;
  1008. }