circuitmux.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. /* Mark bit for consistency checker */
  97. unsigned int mark:1;
  98. };
  99. /*
  100. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  101. * circuit.
  102. */
  103. struct chanid_circid_muxinfo_t {
  104. HT_ENTRY(chanid_circid_muxinfo_t) node;
  105. uint64_t chan_id;
  106. circid_t circ_id;
  107. circuit_muxinfo_t muxinfo;
  108. };
  109. /*
  110. * Internal-use #defines
  111. */
  112. #ifdef CMUX_PARANOIA
  113. #define circuitmux_assert_okay_paranoid(cmux) \
  114. circuitmux_assert_okay(cmux)
  115. #else
  116. #define circuitmux_assert_okay_paranoid(cmux)
  117. #endif
  118. /*
  119. * Static function declarations
  120. */
  121. static INLINE int
  122. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  123. chanid_circid_muxinfo_t *b);
  124. static INLINE unsigned int
  125. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  126. static chanid_circid_muxinfo_t *
  127. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
  128. static void
  129. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  130. cell_direction_t direction);
  131. static void
  132. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  133. cell_direction_t direction);
  134. static INLINE void
  135. circuitmux_move_active_circ_to_tail(circuitmux_t *cmux, circuit_t *circ,
  136. cell_direction_t direction);
  137. static INLINE circuit_t **
  138. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  139. static INLINE circuit_t **
  140. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  141. static void circuitmux_assert_okay_pass_one(circuitmux_t *cmux);
  142. static void circuitmux_assert_okay_pass_two(circuitmux_t *cmux);
  143. static void circuitmux_assert_okay_pass_three(circuitmux_t *cmux);
  144. /* Function definitions */
  145. /**
  146. * Linked list helpers
  147. */
  148. /**
  149. * Move an active circuit to the tail of the cmux's active circuits list;
  150. * used by circuitmux_notify_xmit_cells().
  151. */
  152. static INLINE void
  153. circuitmux_move_active_circ_to_tail(circuitmux_t *cmux, circuit_t *circ,
  154. cell_direction_t direction)
  155. {
  156. circuit_t **next_p = NULL, **prev_p = NULL;
  157. circuit_t **next_prev = NULL, **prev_next = NULL;
  158. circuit_t **tail_next = NULL;
  159. or_circuit_t *or_circ = NULL;
  160. tor_assert(cmux);
  161. tor_assert(circ);
  162. circuitmux_assert_okay_paranoid(cmux);
  163. /* Figure out our next_p and prev_p for this cmux/direction */
  164. if (direction) {
  165. if (direction == CELL_DIRECTION_OUT) {
  166. tor_assert(circ->n_mux == cmux);
  167. next_p = &(circ->next_active_on_n_chan);
  168. prev_p = &(circ->prev_active_on_n_chan);
  169. } else {
  170. or_circ = TO_OR_CIRCUIT(circ);
  171. tor_assert(or_circ->p_mux == cmux);
  172. next_p = &(or_circ->next_active_on_p_chan);
  173. prev_p = &(or_circ->prev_active_on_p_chan);
  174. }
  175. } else {
  176. if (circ->n_mux == cmux) {
  177. next_p = &(circ->next_active_on_n_chan);
  178. prev_p = &(circ->prev_active_on_n_chan);
  179. direction = CELL_DIRECTION_OUT;
  180. } else {
  181. or_circ = TO_OR_CIRCUIT(circ);
  182. tor_assert(or_circ->p_mux == cmux);
  183. next_p = &(or_circ->next_active_on_p_chan);
  184. prev_p = &(or_circ->prev_active_on_p_chan);
  185. direction = CELL_DIRECTION_IN;
  186. }
  187. }
  188. tor_assert(next_p);
  189. tor_assert(prev_p);
  190. /* Check if this really is an active circuit */
  191. if ((*next_p == NULL && *prev_p == NULL) &&
  192. !(circ == cmux->active_circuits_head ||
  193. circ == cmux->active_circuits_tail)) {
  194. /* Not active, no-op */
  195. return;
  196. }
  197. /* Check if this is already the tail */
  198. if (circ == cmux->active_circuits_tail) return;
  199. /* Okay, we have to move it; figure out next_prev and prev_next */
  200. if (*next_p) next_prev = circuitmux_prev_active_circ_p(cmux, *next_p);
  201. if (*prev_p) prev_next = circuitmux_next_active_circ_p(cmux, *prev_p);
  202. /* Adjust the previous node's next pointer, if any */
  203. if (prev_next) *prev_next = *next_p;
  204. /* Otherwise, we were the head */
  205. else cmux->active_circuits_head = *next_p;
  206. /* Adjust the next node's previous pointer, if any */
  207. if (next_prev) *next_prev = *prev_p;
  208. /* We're out of the list; now re-attach at the tail */
  209. /* Adjust our next and prev pointers */
  210. *next_p = NULL;
  211. *prev_p = cmux->active_circuits_tail;
  212. /* Set the next pointer of the tail, or the head if none */
  213. if (cmux->active_circuits_tail) {
  214. tail_next = circuitmux_next_active_circ_p(cmux,
  215. cmux->active_circuits_tail);
  216. *tail_next = circ;
  217. } else {
  218. cmux->active_circuits_head = circ;
  219. }
  220. /* Set the tail to this circuit */
  221. cmux->active_circuits_tail = circ;
  222. circuitmux_assert_okay_paranoid(cmux);
  223. }
  224. static INLINE circuit_t **
  225. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  226. {
  227. tor_assert(cmux);
  228. tor_assert(circ);
  229. if (circ->n_mux == cmux) return &(circ->next_active_on_n_chan);
  230. else {
  231. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  232. return &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  233. }
  234. }
  235. static INLINE circuit_t **
  236. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  237. {
  238. tor_assert(cmux);
  239. tor_assert(circ);
  240. if (circ->n_mux == cmux) return &(circ->prev_active_on_n_chan);
  241. else {
  242. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  243. return &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  244. }
  245. }
  246. /**
  247. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  248. * ID and circuit ID for a and b, and return less than, equal to, or greater
  249. * than zero appropriately.
  250. */
  251. static INLINE int
  252. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  253. chanid_circid_muxinfo_t *b)
  254. {
  255. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  256. }
  257. /**
  258. * Helper: return a hash based on circuit ID and channel ID in a.
  259. */
  260. static INLINE unsigned int
  261. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  262. {
  263. return (((unsigned int)(a->circ_id) << 8) ^
  264. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  265. ((unsigned int)(a->chan_id & 0xffffffff)));
  266. }
  267. /* Declare the struct chanid_circid_muxinfo_map type */
  268. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  269. /* Emit a bunch of hash table stuff */
  270. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  271. chanid_circid_entry_hash, chanid_circid_entries_eq);
  272. HT_GENERATE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  273. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  274. malloc, realloc, free);
  275. /*
  276. * Circuitmux alloc/free functions
  277. */
  278. /**
  279. * Allocate a new circuitmux_t
  280. */
  281. circuitmux_t *
  282. circuitmux_alloc(void)
  283. {
  284. circuitmux_t *rv = NULL;
  285. rv = tor_malloc_zero(sizeof(*rv));
  286. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  287. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  288. return rv;
  289. }
  290. /**
  291. * Detach all circuits from a circuitmux (use before circuitmux_free())
  292. */
  293. void
  294. circuitmux_detach_all_circuits(circuitmux_t *cmux)
  295. {
  296. chanid_circid_muxinfo_t **i = NULL, *to_remove;
  297. channel_t *chan = NULL;
  298. circuit_t *circ = NULL;
  299. tor_assert(cmux);
  300. /*
  301. * Don't circuitmux_assert_okay_paranoid() here; this gets called when
  302. * channels are being freed and have already been unregistered, so
  303. * the channel ID lookups it does will fail.
  304. */
  305. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  306. while (i) {
  307. to_remove = *i;
  308. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  309. if (to_remove) {
  310. /* Find a channel and circuit */
  311. chan = channel_find_by_global_id(to_remove->chan_id);
  312. if (chan) {
  313. circ =
  314. circuit_get_by_circid_channel_even_if_marked(to_remove->circ_id,
  315. chan);
  316. if (circ) {
  317. /* Clear the circuit's mux for this direction */
  318. if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
  319. /* Clear n_mux */
  320. circ->n_mux = NULL;
  321. /*
  322. * Update active_circuits et al.; this does policy notifies, so
  323. * comes before freeing policy data
  324. */
  325. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_OUT);
  326. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  327. /*
  328. * It has a sensible p_chan and direction == CELL_DIRECTION_IN,
  329. * so clear p_mux.
  330. */
  331. TO_OR_CIRCUIT(circ)->p_mux = NULL;
  332. /*
  333. * Update active_circuits et al.; this does policy notifies, so
  334. * comes before freeing policy data
  335. */
  336. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_IN);
  337. } else {
  338. /* Complain and move on */
  339. log_warn(LD_CIRC,
  340. "Circuit %d/channel " U64_FORMAT " had direction == "
  341. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  342. to_remove->circ_id,
  343. U64_PRINTF_ARG(to_remove->chan_id));
  344. }
  345. /* Free policy-specific data if we have it */
  346. if (to_remove->muxinfo.policy_data) {
  347. /*
  348. * If we have policy data, assert that we have the means to
  349. * free it
  350. */
  351. tor_assert(cmux->policy);
  352. tor_assert(cmux->policy->free_circ_data);
  353. /* Call free_circ_data() */
  354. cmux->policy->free_circ_data(cmux,
  355. cmux->policy_data,
  356. circ,
  357. to_remove->muxinfo.policy_data);
  358. to_remove->muxinfo.policy_data = NULL;
  359. }
  360. } else {
  361. /* Complain and move on */
  362. log_warn(LD_CIRC,
  363. "Couldn't find circuit %d (for channel " U64_FORMAT ")",
  364. to_remove->circ_id,
  365. U64_PRINTF_ARG(to_remove->chan_id));
  366. }
  367. } else {
  368. /* Complain and move on */
  369. log_warn(LD_CIRC,
  370. "Couldn't find channel " U64_FORMAT " (for circuit id %d)",
  371. U64_PRINTF_ARG(to_remove->chan_id),
  372. to_remove->circ_id);
  373. }
  374. /* Assert that we don't have un-freed policy data for this circuit */
  375. tor_assert(to_remove->muxinfo.policy_data == NULL);
  376. /* Free it */
  377. tor_free(to_remove);
  378. }
  379. }
  380. cmux->n_circuits = 0;
  381. cmux->n_active_circuits = 0;
  382. cmux->n_cells = 0;
  383. }
  384. /**
  385. * Free a circuitmux_t; the circuits must be detached first with
  386. * circuitmux_detach_all_circuits().
  387. */
  388. void
  389. circuitmux_free(circuitmux_t *cmux)
  390. {
  391. if (!cmux) return;
  392. tor_assert(cmux->n_circuits == 0);
  393. tor_assert(cmux->n_active_circuits == 0);
  394. /*
  395. * Free policy-specific data if we have any; we don't
  396. * need to do circuitmux_set_policy(cmux, NULL) to cover
  397. * the circuits because they would have been handled in
  398. * circuitmux_detach_all_circuits() before this was
  399. * called.
  400. */
  401. if (cmux->policy && cmux->policy->free_cmux_data) {
  402. if (cmux->policy_data) {
  403. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  404. cmux->policy_data = NULL;
  405. }
  406. } else tor_assert(cmux->policy_data == NULL);
  407. if (cmux->chanid_circid_map) {
  408. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  409. tor_free(cmux->chanid_circid_map);
  410. }
  411. tor_free(cmux);
  412. }
  413. /*
  414. * Circuitmux policy control functions
  415. */
  416. /**
  417. * Remove any policy installed on cmux; all policy data will be freed and
  418. * cmux behavior will revert to the built-in round-robin active_circuits
  419. * mechanism.
  420. */
  421. void
  422. circuitmux_clear_policy(circuitmux_t *cmux)
  423. {
  424. tor_assert(cmux);
  425. /* Internally, this is just setting policy to NULL */
  426. if (cmux->policy) {
  427. circuitmux_set_policy(cmux, NULL);
  428. }
  429. }
  430. /**
  431. * Return the policy currently installed on a circuitmux_t
  432. */
  433. const circuitmux_policy_t *
  434. circuitmux_get_policy(circuitmux_t *cmux)
  435. {
  436. tor_assert(cmux);
  437. return cmux->policy;
  438. }
  439. /**
  440. * Set policy; allocate for new policy, detach all circuits from old policy
  441. * if any, attach them to new policy, and free old policy data.
  442. */
  443. void
  444. circuitmux_set_policy(circuitmux_t *cmux,
  445. const circuitmux_policy_t *pol)
  446. {
  447. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  448. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  449. chanid_circid_muxinfo_t **i = NULL;
  450. channel_t *chan = NULL;
  451. uint64_t last_chan_id_searched = 0;
  452. circuit_t *circ = NULL;
  453. tor_assert(cmux);
  454. /* Set up variables */
  455. old_pol = cmux->policy;
  456. old_pol_data = cmux->policy_data;
  457. new_pol = pol;
  458. /* Check if this is the trivial case */
  459. if (old_pol == new_pol) return;
  460. /* Allocate data for new policy, if any */
  461. if (new_pol && new_pol->alloc_cmux_data) {
  462. /*
  463. * If alloc_cmux_data is not null, then we expect to get some policy
  464. * data. Assert that we also have free_cmux_data so we can free it
  465. * when the time comes, and allocate it.
  466. */
  467. tor_assert(new_pol->free_cmux_data);
  468. new_pol_data = new_pol->alloc_cmux_data(cmux);
  469. tor_assert(new_pol_data);
  470. }
  471. /* Install new policy and new policy data on cmux */
  472. cmux->policy = new_pol;
  473. cmux->policy_data = new_pol_data;
  474. /* Iterate over all circuits, attaching/detaching each one */
  475. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  476. while (i) {
  477. /* Assert that this entry isn't NULL */
  478. tor_assert(*i);
  479. /*
  480. * Get the channel; since normal case is all circuits on the mux share a
  481. * channel, we cache last_chan_id_searched
  482. */
  483. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  484. chan = channel_find_by_global_id((*i)->chan_id);
  485. last_chan_id_searched = (*i)->chan_id;
  486. }
  487. tor_assert(chan);
  488. /* Get the circuit */
  489. circ = circuit_get_by_circid_channel_even_if_marked((*i)->circ_id, chan);
  490. tor_assert(circ);
  491. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  492. if (old_pol && old_pol->notify_circ_inactive &&
  493. (*i)->muxinfo.cell_count > 0) {
  494. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  495. (*i)->muxinfo.policy_data);
  496. }
  497. /* Need to free old policy data? */
  498. if ((*i)->muxinfo.policy_data) {
  499. /* Assert that we have the means to free it if we have policy data */
  500. tor_assert(old_pol);
  501. tor_assert(old_pol->free_circ_data);
  502. /* Free it */
  503. old_pol->free_circ_data(cmux, old_pol_data, circ,
  504. (*i)->muxinfo.policy_data);
  505. (*i)->muxinfo.policy_data = NULL;
  506. }
  507. /* Need to allocate new policy data? */
  508. if (new_pol && new_pol->alloc_circ_data) {
  509. /*
  510. * If alloc_circ_data is not null, we expect to get some per-circuit
  511. * policy data. Assert that we also have free_circ_data so we can
  512. * free it when the time comes, and allocate it.
  513. */
  514. tor_assert(new_pol->free_circ_data);
  515. (*i)->muxinfo.policy_data =
  516. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  517. (*i)->muxinfo.direction,
  518. (*i)->muxinfo.cell_count);
  519. }
  520. /* Need to make active on new policy? */
  521. if (new_pol && new_pol->notify_circ_active &&
  522. (*i)->muxinfo.cell_count > 0) {
  523. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  524. (*i)->muxinfo.policy_data);
  525. }
  526. /* Advance to next circuit map entry */
  527. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  528. }
  529. /* Free data for old policy, if any */
  530. if (old_pol_data) {
  531. /*
  532. * If we had old policy data, we should have an old policy and a free
  533. * function for it.
  534. */
  535. tor_assert(old_pol);
  536. tor_assert(old_pol->free_cmux_data);
  537. old_pol->free_cmux_data(cmux, old_pol_data);
  538. old_pol_data = NULL;
  539. }
  540. }
  541. /*
  542. * Circuitmux/circuit attachment status inquiry functions
  543. */
  544. /**
  545. * Query the direction of an attached circuit
  546. */
  547. cell_direction_t
  548. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  549. {
  550. chanid_circid_muxinfo_t *hashent = NULL;
  551. /* Try to find a map entry */
  552. hashent = circuitmux_find_map_entry(cmux, circ);
  553. /*
  554. * This function should only be called on attached circuits; assert that
  555. * we had a map entry.
  556. */
  557. tor_assert(hashent);
  558. /* Return the direction from the map entry */
  559. return hashent->muxinfo.direction;
  560. }
  561. /**
  562. * Find an entry in the cmux's map for this circuit or return NULL if there
  563. * is none.
  564. */
  565. static chanid_circid_muxinfo_t *
  566. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  567. {
  568. chanid_circid_muxinfo_t search, *hashent = NULL;
  569. /* Sanity-check parameters */
  570. tor_assert(cmux);
  571. tor_assert(cmux->chanid_circid_map);
  572. tor_assert(circ);
  573. /* Check if we have n_chan */
  574. if (circ->n_chan) {
  575. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  576. search.chan_id = circ->n_chan->global_identifier;
  577. search.circ_id = circ->n_circ_id;
  578. /* Query */
  579. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  580. &search);
  581. }
  582. /* Found something? */
  583. if (hashent) {
  584. /*
  585. * Assert that the direction makes sense for a hashent we found by
  586. * n_chan/n_circ_id before we return it.
  587. */
  588. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  589. } else {
  590. /* Not there, have we got a p_chan/p_circ_id to try? */
  591. if (circ->magic == OR_CIRCUIT_MAGIC) {
  592. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  593. /* Check for p_chan */
  594. if (TO_OR_CIRCUIT(circ)->p_chan) {
  595. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  596. /* Okay, search for that */
  597. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  598. &search);
  599. /* Find anything? */
  600. if (hashent) {
  601. /* Assert that the direction makes sense before we return it */
  602. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  603. }
  604. }
  605. }
  606. }
  607. /* Okay, hashent is it if it was there */
  608. return hashent;
  609. }
  610. /**
  611. * Query whether a circuit is attached to a circuitmux
  612. */
  613. int
  614. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  615. {
  616. chanid_circid_muxinfo_t *hashent = NULL;
  617. /* Look if it's in the circuit map */
  618. hashent = circuitmux_find_map_entry(cmux, circ);
  619. return (hashent != NULL);
  620. }
  621. /**
  622. * Query whether a circuit is active on a circuitmux
  623. */
  624. int
  625. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  626. {
  627. chanid_circid_muxinfo_t *hashent = NULL;
  628. int is_active = 0;
  629. tor_assert(cmux);
  630. tor_assert(circ);
  631. /* Look if it's in the circuit map */
  632. hashent = circuitmux_find_map_entry(cmux, circ);
  633. if (hashent) {
  634. /* Check the number of cells on this circuit */
  635. is_active = (hashent->muxinfo.cell_count > 0);
  636. }
  637. /* else not attached, so not active */
  638. return is_active;
  639. }
  640. /**
  641. * Query number of available cells for a circuit on a circuitmux
  642. */
  643. unsigned int
  644. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  645. {
  646. chanid_circid_muxinfo_t *hashent = NULL;
  647. unsigned int n_cells = 0;
  648. tor_assert(cmux);
  649. tor_assert(circ);
  650. /* Look if it's in the circuit map */
  651. hashent = circuitmux_find_map_entry(cmux, circ);
  652. if (hashent) {
  653. /* Just get the cell count for this circuit */
  654. n_cells = hashent->muxinfo.cell_count;
  655. }
  656. /* else not attached, so 0 cells */
  657. return n_cells;
  658. }
  659. /**
  660. * Query total number of available cells on a circuitmux
  661. */
  662. unsigned int
  663. circuitmux_num_cells(circuitmux_t *cmux)
  664. {
  665. tor_assert(cmux);
  666. return cmux->n_cells;
  667. }
  668. /**
  669. * Query total number of circuits active on a circuitmux
  670. */
  671. unsigned int
  672. circuitmux_num_active_circuits(circuitmux_t *cmux)
  673. {
  674. tor_assert(cmux);
  675. return cmux->n_active_circuits;
  676. }
  677. /**
  678. * Query total number of circuits attached to a circuitmux
  679. */
  680. unsigned int
  681. circuitmux_num_circuits(circuitmux_t *cmux)
  682. {
  683. tor_assert(cmux);
  684. return cmux->n_circuits;
  685. }
  686. /*
  687. * Functions for circuit code to call to update circuit status
  688. */
  689. /**
  690. * Attach a circuit to a circuitmux, for the specified direction.
  691. */
  692. void
  693. circuitmux_attach_circuit(circuitmux_t *cmux, circuit_t *circ,
  694. cell_direction_t direction)
  695. {
  696. channel_t *chan = NULL;
  697. uint64_t channel_id;
  698. circid_t circ_id;
  699. chanid_circid_muxinfo_t search, *hashent = NULL;
  700. unsigned int cell_count;
  701. tor_assert(cmux);
  702. tor_assert(circ);
  703. tor_assert(direction == CELL_DIRECTION_IN ||
  704. direction == CELL_DIRECTION_OUT);
  705. circuitmux_assert_okay_paranoid(cmux);
  706. /*
  707. * Figure out which channel we're using, and get the circuit's current
  708. * cell count and circuit ID; assert that the circuit is not already
  709. * attached to another mux.
  710. */
  711. if (direction == CELL_DIRECTION_OUT) {
  712. /* It's n_chan */
  713. chan = circ->n_chan;
  714. cell_count = circ->n_chan_cells.n;
  715. circ_id = circ->n_circ_id;
  716. } else {
  717. /* We want p_chan */
  718. chan = TO_OR_CIRCUIT(circ)->p_chan;
  719. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  720. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  721. }
  722. /* Assert that we did get a channel */
  723. tor_assert(chan);
  724. /* Assert that the circuit ID is sensible */
  725. tor_assert(circ_id != 0);
  726. /* Get the channel ID */
  727. channel_id = chan->global_identifier;
  728. /* See if we already have this one */
  729. search.chan_id = channel_id;
  730. search.circ_id = circ_id;
  731. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  732. &search);
  733. if (hashent) {
  734. /*
  735. * This circuit was already attached to this cmux; make sure the
  736. * directions match and update the cell count and active circuit count.
  737. */
  738. log_info(LD_CIRC,
  739. "Circuit %u on channel " U64_FORMAT " was already attached to "
  740. "cmux %p (trying to attach to %p)",
  741. circ_id, U64_PRINTF_ARG(channel_id),
  742. ((direction == CELL_DIRECTION_OUT) ?
  743. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  744. cmux);
  745. /*
  746. * The mux pointer on this circuit and the direction in result should
  747. * match; otherwise assert.
  748. */
  749. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  750. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  751. tor_assert(hashent->muxinfo.direction == direction);
  752. /*
  753. * Looks okay; just update the cell count and active circuits if we must
  754. */
  755. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  756. --(cmux->n_active_circuits);
  757. circuitmux_make_circuit_inactive(cmux, circ, direction);
  758. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  759. ++(cmux->n_active_circuits);
  760. circuitmux_make_circuit_active(cmux, circ, direction);
  761. }
  762. cmux->n_cells -= hashent->muxinfo.cell_count;
  763. cmux->n_cells += cell_count;
  764. hashent->muxinfo.cell_count = cell_count;
  765. } else {
  766. /*
  767. * New circuit; add an entry and update the circuit/active circuit
  768. * counts.
  769. */
  770. log_debug(LD_CIRC,
  771. "Attaching circuit %u on channel " U64_FORMAT " to cmux %p",
  772. circ_id, U64_PRINTF_ARG(channel_id), cmux);
  773. /*
  774. * Assert that the circuit doesn't already have a mux for this
  775. * direction.
  776. */
  777. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  778. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  779. /* Insert it in the map */
  780. hashent = tor_malloc_zero(sizeof(*hashent));
  781. hashent->chan_id = channel_id;
  782. hashent->circ_id = circ_id;
  783. hashent->muxinfo.cell_count = cell_count;
  784. hashent->muxinfo.direction = direction;
  785. /* Allocate policy specific circuit data if we need it */
  786. if (cmux->policy && cmux->policy->alloc_circ_data) {
  787. /* Assert that we have the means to free policy-specific data */
  788. tor_assert(cmux->policy->free_circ_data);
  789. /* Allocate it */
  790. hashent->muxinfo.policy_data =
  791. cmux->policy->alloc_circ_data(cmux,
  792. cmux->policy_data,
  793. circ,
  794. direction,
  795. cell_count);
  796. /* If we wanted policy data, it's an error not to get any */
  797. tor_assert(hashent->muxinfo.policy_data);
  798. }
  799. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  800. hashent);
  801. /* Set the circuit's mux for this direction */
  802. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  803. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  804. /* Make sure the next/prev pointers are NULL */
  805. if (direction == CELL_DIRECTION_OUT) {
  806. circ->next_active_on_n_chan = NULL;
  807. circ->prev_active_on_n_chan = NULL;
  808. } else {
  809. TO_OR_CIRCUIT(circ)->next_active_on_p_chan = NULL;
  810. TO_OR_CIRCUIT(circ)->prev_active_on_p_chan = NULL;
  811. }
  812. /* Update counters */
  813. ++(cmux->n_circuits);
  814. if (cell_count > 0) {
  815. ++(cmux->n_active_circuits);
  816. circuitmux_make_circuit_active(cmux, circ, direction);
  817. }
  818. cmux->n_cells += cell_count;
  819. }
  820. circuitmux_assert_okay_paranoid(cmux);
  821. }
  822. /**
  823. * Detach a circuit from a circuitmux and update all counters as needed;
  824. * no-op if not attached.
  825. */
  826. void
  827. circuitmux_detach_circuit(circuitmux_t *cmux, circuit_t *circ)
  828. {
  829. chanid_circid_muxinfo_t search, *hashent = NULL;
  830. /*
  831. * Use this to keep track of whether we found it for n_chan or
  832. * p_chan for consistency checking.
  833. */
  834. cell_direction_t last_searched_direction;
  835. tor_assert(cmux);
  836. tor_assert(cmux->chanid_circid_map);
  837. tor_assert(circ);
  838. circuitmux_assert_okay_paranoid(cmux);
  839. /* See if we have it for n_chan/n_circ_id */
  840. if (circ->n_chan) {
  841. search.chan_id = circ->n_chan->global_identifier;
  842. search.circ_id = circ->n_circ_id;
  843. hashent = HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  844. &search);
  845. last_searched_direction = CELL_DIRECTION_OUT;
  846. }
  847. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  848. if (!hashent) {
  849. if (circ->magic == OR_CIRCUIT_MAGIC) {
  850. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  851. if (TO_OR_CIRCUIT(circ)->p_chan) {
  852. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  853. hashent = HT_REMOVE(chanid_circid_muxinfo_map,
  854. cmux->chanid_circid_map,
  855. &search);
  856. last_searched_direction = CELL_DIRECTION_IN;
  857. }
  858. }
  859. }
  860. /* If hashent isn't NULL, we just removed it from the map */
  861. if (hashent) {
  862. /* Update counters */
  863. --(cmux->n_circuits);
  864. if (hashent->muxinfo.cell_count > 0) {
  865. --(cmux->n_active_circuits);
  866. /* This does policy notifies, so comes before freeing policy data */
  867. circuitmux_make_circuit_inactive(cmux, circ, last_searched_direction);
  868. }
  869. cmux->n_cells -= hashent->muxinfo.cell_count;
  870. /* Free policy-specific data if we have it */
  871. if (hashent->muxinfo.policy_data) {
  872. /* If we have policy data, assert that we have the means to free it */
  873. tor_assert(cmux->policy);
  874. tor_assert(cmux->policy->free_circ_data);
  875. /* Call free_circ_data() */
  876. cmux->policy->free_circ_data(cmux,
  877. cmux->policy_data,
  878. circ,
  879. hashent->muxinfo.policy_data);
  880. hashent->muxinfo.policy_data = NULL;
  881. }
  882. /* Consistency check: the direction must match the direction searched */
  883. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  884. /* Clear the circuit's mux for this direction */
  885. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  886. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  887. /* Free the hash entry */
  888. tor_free(hashent);
  889. }
  890. circuitmux_assert_okay_paranoid(cmux);
  891. }
  892. /**
  893. * Make a circuit active; update active list and policy-specific info, but
  894. * we don't mess with the counters or hash table here.
  895. */
  896. static void
  897. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  898. cell_direction_t direction)
  899. {
  900. circuit_t **next_active = NULL, **prev_active = NULL, **next_prev = NULL;
  901. circuitmux_t *circuit_cmux = NULL;
  902. chanid_circid_muxinfo_t *hashent = NULL;
  903. channel_t *chan = NULL;
  904. circid_t circ_id;
  905. int already_active;
  906. tor_assert(cmux);
  907. tor_assert(circ);
  908. tor_assert(direction == CELL_DIRECTION_OUT ||
  909. direction == CELL_DIRECTION_IN);
  910. /*
  911. * Don't circuitmux_assert_okay_paranoid(cmux) here because the cell count
  912. * already got changed and we have to update the list for it to be consistent
  913. * again.
  914. */
  915. /* Get the right set of active list links for this direction */
  916. if (direction == CELL_DIRECTION_OUT) {
  917. next_active = &(circ->next_active_on_n_chan);
  918. prev_active = &(circ->prev_active_on_n_chan);
  919. circuit_cmux = circ->n_mux;
  920. chan = circ->n_chan;
  921. circ_id = circ->n_circ_id;
  922. } else {
  923. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  924. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  925. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  926. chan = TO_OR_CIRCUIT(circ)->p_chan;
  927. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  928. }
  929. /* Assert that it is attached to this mux and a channel */
  930. tor_assert(cmux == circuit_cmux);
  931. tor_assert(chan != NULL);
  932. /*
  933. * Check if the circuit really was inactive; if it's active, at least one
  934. * of the next_active and prev_active pointers will not be NULL, or this
  935. * circuit will be either the head or tail of the list for this cmux.
  936. */
  937. already_active = (*prev_active != NULL || *next_active != NULL ||
  938. cmux->active_circuits_head == circ ||
  939. cmux->active_circuits_tail == circ);
  940. /* If we're already active, log a warning and finish */
  941. if (already_active) {
  942. log_warn(LD_CIRC,
  943. "Circuit %d on channel " U64_FORMAT " was already active",
  944. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  945. return;
  946. }
  947. /*
  948. * This is going at the head of the list; if the old head is not NULL,
  949. * then its prev pointer should point to this.
  950. */
  951. *next_active = cmux->active_circuits_head; /* Next is old head */
  952. *prev_active = NULL; /* Prev is NULL (this will be the head) */
  953. if (cmux->active_circuits_head) {
  954. /* The list had an old head; update its prev pointer */
  955. next_prev =
  956. circuitmux_prev_active_circ_p(cmux, cmux->active_circuits_head);
  957. tor_assert(next_prev);
  958. *next_prev = circ;
  959. } else {
  960. /* The list was empty; this becomes the tail as well */
  961. cmux->active_circuits_tail = circ;
  962. }
  963. /* This becomes the new head of the list */
  964. cmux->active_circuits_head = circ;
  965. /* Policy-specific notification */
  966. if (cmux->policy &&
  967. cmux->policy->notify_circ_active) {
  968. /* Okay, we need to check the circuit for policy data now */
  969. hashent = circuitmux_find_map_entry(cmux, circ);
  970. /* We should have found something */
  971. tor_assert(hashent);
  972. /* Notify */
  973. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  974. circ, hashent->muxinfo.policy_data);
  975. }
  976. circuitmux_assert_okay_paranoid(cmux);
  977. }
  978. /**
  979. * Make a circuit inactive; update active list and policy-specific info, but
  980. * we don't mess with the counters or hash table here.
  981. */
  982. static void
  983. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  984. cell_direction_t direction)
  985. {
  986. circuit_t **next_active = NULL, **prev_active = NULL;
  987. circuit_t **next_prev = NULL, **prev_next = NULL;
  988. circuitmux_t *circuit_cmux = NULL;
  989. chanid_circid_muxinfo_t *hashent = NULL;
  990. channel_t *chan = NULL;
  991. circid_t circ_id;
  992. int already_inactive;
  993. tor_assert(cmux);
  994. tor_assert(circ);
  995. tor_assert(direction == CELL_DIRECTION_OUT ||
  996. direction == CELL_DIRECTION_IN);
  997. /*
  998. * Don't circuitmux_assert_okay_paranoid(cmux) here because the cell count
  999. * already got changed and we have to update the list for it to be consistent
  1000. * again.
  1001. */
  1002. /* Get the right set of active list links for this direction */
  1003. if (direction == CELL_DIRECTION_OUT) {
  1004. next_active = &(circ->next_active_on_n_chan);
  1005. prev_active = &(circ->prev_active_on_n_chan);
  1006. circuit_cmux = circ->n_mux;
  1007. chan = circ->n_chan;
  1008. circ_id = circ->n_circ_id;
  1009. } else {
  1010. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  1011. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  1012. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  1013. chan = TO_OR_CIRCUIT(circ)->p_chan;
  1014. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  1015. }
  1016. /* Assert that it is attached to this mux and a channel */
  1017. tor_assert(cmux == circuit_cmux);
  1018. tor_assert(chan != NULL);
  1019. /*
  1020. * Check if the circuit really was active; if it's inactive, the
  1021. * next_active and prev_active pointers will be NULL and this circuit
  1022. * will not be the head or tail of the list for this cmux.
  1023. */
  1024. already_inactive = (*prev_active == NULL && *next_active == NULL &&
  1025. cmux->active_circuits_head != circ &&
  1026. cmux->active_circuits_tail != circ);
  1027. /* If we're already inactive, log a warning and finish */
  1028. if (already_inactive) {
  1029. log_warn(LD_CIRC,
  1030. "Circuit %d on channel " U64_FORMAT " was already inactive",
  1031. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  1032. return;
  1033. }
  1034. /* Remove from the list; first get next_prev and prev_next */
  1035. if (*next_active) {
  1036. /*
  1037. * If there's a next circuit, its previous circuit becomes this
  1038. * circuit's previous circuit.
  1039. */
  1040. next_prev = circuitmux_prev_active_circ_p(cmux, *next_active);
  1041. } else {
  1042. /* Else, the tail becomes this circuit's previous circuit */
  1043. next_prev = &(cmux->active_circuits_tail);
  1044. }
  1045. /* Got next_prev, now prev_next */
  1046. if (*prev_active) {
  1047. /*
  1048. * If there's a previous circuit, its next circuit becomes this circuit's
  1049. * next circuit.
  1050. */
  1051. prev_next = circuitmux_next_active_circ_p(cmux, *prev_active);
  1052. } else {
  1053. /* Else, the head becomes this circuit's next circuit */
  1054. prev_next = &(cmux->active_circuits_head);
  1055. }
  1056. /* Assert that we got sensible values for the next/prev pointers */
  1057. tor_assert(next_prev != NULL);
  1058. tor_assert(prev_next != NULL);
  1059. /* Update the next/prev pointers - this removes circ from the list */
  1060. *next_prev = *prev_active;
  1061. *prev_next = *next_active;
  1062. /* Now null out prev_active/next_active */
  1063. *prev_active = NULL;
  1064. *next_active = NULL;
  1065. /* Policy-specific notification */
  1066. if (cmux->policy &&
  1067. cmux->policy->notify_circ_inactive) {
  1068. /* Okay, we need to check the circuit for policy data now */
  1069. hashent = circuitmux_find_map_entry(cmux, circ);
  1070. /* We should have found something */
  1071. tor_assert(hashent);
  1072. /* Notify */
  1073. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  1074. circ, hashent->muxinfo.policy_data);
  1075. }
  1076. circuitmux_assert_okay_paranoid(cmux);
  1077. }
  1078. /**
  1079. * Clear the cell counter for a circuit on a circuitmux
  1080. */
  1081. void
  1082. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  1083. {
  1084. /* This is the same as setting the cell count to zero */
  1085. circuitmux_set_num_cells(cmux, circ, 0);
  1086. }
  1087. /**
  1088. * Set the cell counter for a circuit on a circuitmux
  1089. */
  1090. void
  1091. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  1092. unsigned int n_cells)
  1093. {
  1094. chanid_circid_muxinfo_t *hashent = NULL;
  1095. tor_assert(cmux);
  1096. tor_assert(circ);
  1097. circuitmux_assert_okay_paranoid(cmux);
  1098. /* Search for this circuit's entry */
  1099. hashent = circuitmux_find_map_entry(cmux, circ);
  1100. /* Assert that we found one */
  1101. tor_assert(hashent);
  1102. /* Update cmux cell counter */
  1103. cmux->n_cells -= hashent->muxinfo.cell_count;
  1104. cmux->n_cells += n_cells;
  1105. /* Do we need to notify a cmux policy? */
  1106. if (cmux->policy && cmux->policy->notify_set_n_cells) {
  1107. /* Call notify_set_n_cells */
  1108. cmux->policy->notify_set_n_cells(cmux,
  1109. cmux->policy_data,
  1110. circ,
  1111. hashent->muxinfo.policy_data,
  1112. n_cells);
  1113. }
  1114. /*
  1115. * Update cmux active circuit counter: is the old cell count > 0 and the
  1116. * new cell count == 0 ?
  1117. */
  1118. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  1119. --(cmux->n_active_circuits);
  1120. hashent->muxinfo.cell_count = n_cells;
  1121. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1122. /* Is the old cell count == 0 and the new cell count > 0 ? */
  1123. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  1124. ++(cmux->n_active_circuits);
  1125. hashent->muxinfo.cell_count = n_cells;
  1126. circuitmux_make_circuit_active(cmux, circ, hashent->muxinfo.direction);
  1127. } else {
  1128. /*
  1129. * Update the entry cell count like this so we can put a
  1130. * circuitmux_assert_okay_paranoid inside make_circuit_(in)active() too.
  1131. */
  1132. hashent->muxinfo.cell_count = n_cells;
  1133. }
  1134. circuitmux_assert_okay_paranoid(cmux);
  1135. }
  1136. /*
  1137. * Functions for channel code to call to get a circuit to transmit from or
  1138. * notify that cells have been transmitted.
  1139. */
  1140. /**
  1141. * Pick a circuit to send from, using the active circuits list or a
  1142. * circuitmux policy if one is available. This is called from channel.c.
  1143. */
  1144. circuit_t *
  1145. circuitmux_get_first_active_circuit(circuitmux_t *cmux)
  1146. {
  1147. circuit_t *circ = NULL;
  1148. tor_assert(cmux);
  1149. if (cmux->n_active_circuits > 0) {
  1150. /* We also must have a cell available for this to be the case */
  1151. tor_assert(cmux->n_cells > 0);
  1152. /* Do we have a policy-provided circuit selector? */
  1153. if (cmux->policy && cmux->policy->pick_active_circuit) {
  1154. circ = cmux->policy->pick_active_circuit(cmux, cmux->policy_data);
  1155. }
  1156. /* Fall back on the head of the active circuits list */
  1157. if (!circ) {
  1158. tor_assert(cmux->active_circuits_head);
  1159. circ = cmux->active_circuits_head;
  1160. }
  1161. } else tor_assert(cmux->n_cells == 0);
  1162. return circ;
  1163. }
  1164. /**
  1165. * Notify the circuitmux that cells have been sent on a circuit; this
  1166. * is called from channel.c.
  1167. */
  1168. void
  1169. circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  1170. unsigned int n_cells)
  1171. {
  1172. chanid_circid_muxinfo_t *hashent = NULL;
  1173. int becomes_inactive = 0;
  1174. tor_assert(cmux);
  1175. tor_assert(circ);
  1176. circuitmux_assert_okay_paranoid(cmux);
  1177. if (n_cells == 0) return;
  1178. /*
  1179. * To handle this, we have to:
  1180. *
  1181. * 1.) Adjust the circuit's cell counter in the cmux hash table
  1182. * 2.) Move the circuit to the tail of the active_circuits linked list
  1183. * for this cmux, or make the circuit inactive if the cell count
  1184. * went to zero.
  1185. * 3.) Call cmux->policy->notify_xmit_cells(), if any
  1186. */
  1187. /* Find the hash entry */
  1188. hashent = circuitmux_find_map_entry(cmux, circ);
  1189. /* Assert that we found one */
  1190. tor_assert(hashent);
  1191. /* Adjust the cell counter and assert that we had that many cells to send */
  1192. tor_assert(n_cells <= hashent->muxinfo.cell_count);
  1193. hashent->muxinfo.cell_count -= n_cells;
  1194. /* Do we need to make the circuit inactive? */
  1195. if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
  1196. /* Adjust the mux cell counter */
  1197. cmux->n_cells -= n_cells;
  1198. /* If we aren't making it inactive later, move it to the tail of the list */
  1199. if (!becomes_inactive) {
  1200. circuitmux_move_active_circ_to_tail(cmux, circ,
  1201. hashent->muxinfo.direction);
  1202. }
  1203. /*
  1204. * We call notify_xmit_cells() before making the circuit inactive if needed,
  1205. * so the policy can always count on this coming in on an active circuit.
  1206. */
  1207. if (cmux->policy && cmux->policy->notify_xmit_cells) {
  1208. cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
  1209. hashent->muxinfo.policy_data,
  1210. n_cells);
  1211. }
  1212. /*
  1213. * Now make the circuit inactive if needed; this will call the policy's
  1214. * notify_circ_inactive() if present.
  1215. */
  1216. if (becomes_inactive) {
  1217. --(cmux->n_active_circuits);
  1218. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1219. }
  1220. circuitmux_assert_okay_paranoid(cmux);
  1221. }
  1222. /*
  1223. * Circuitmux consistency checking assertions
  1224. */
  1225. /**
  1226. * Check that circuitmux data structures are consistent and fail with an
  1227. * assert if not.
  1228. */
  1229. void
  1230. circuitmux_assert_okay(circuitmux_t *cmux)
  1231. {
  1232. tor_assert(cmux);
  1233. /*
  1234. * Pass 1: iterate the hash table; for each entry:
  1235. * a) Check that the circuit has this cmux for n_mux or p_mux
  1236. * b) If the cell_count is > 0, set the mark bit; otherwise clear it
  1237. * c) Also check activeness (cell_count > 0 should be active)
  1238. * d) Count the number of circuits, active circuits and queued cells
  1239. * and at the end check that they match the counters in the cmux.
  1240. *
  1241. * Pass 2: iterate the active circuits list; for each entry,
  1242. * make sure the circuit is attached to this mux and appears
  1243. * in the hash table. Make sure the mark bit is 1, and clear
  1244. * it in the hash table entry. Consistency-check the linked
  1245. * list pointers.
  1246. *
  1247. * Pass 3: iterate the hash table again; assert if any active circuits
  1248. * (mark bit set to 1) are discovered that weren't cleared in pass 2
  1249. * (don't appear in the linked list).
  1250. */
  1251. circuitmux_assert_okay_pass_one(cmux);
  1252. circuitmux_assert_okay_pass_two(cmux);
  1253. circuitmux_assert_okay_pass_three(cmux);
  1254. }
  1255. /**
  1256. * Do the first pass of circuitmux_assert_okay(); see the comment in that
  1257. * function.
  1258. */
  1259. static void
  1260. circuitmux_assert_okay_pass_one(circuitmux_t *cmux)
  1261. {
  1262. chanid_circid_muxinfo_t **i = NULL;
  1263. uint64_t chan_id;
  1264. channel_t *chan;
  1265. circid_t circ_id;
  1266. circuit_t *circ;
  1267. or_circuit_t *or_circ;
  1268. unsigned int circ_is_active;
  1269. circuit_t **next_p, **prev_p;
  1270. unsigned int n_circuits, n_active_circuits, n_cells;
  1271. tor_assert(cmux);
  1272. tor_assert(cmux->chanid_circid_map);
  1273. /* Reset the counters */
  1274. n_circuits = n_active_circuits = n_cells = 0;
  1275. /* Start iterating the hash table */
  1276. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  1277. while (i) {
  1278. /* Assert that the hash table entry isn't null */
  1279. tor_assert(*i);
  1280. /* Get the channel and circuit id */
  1281. chan_id = (*i)->chan_id;
  1282. circ_id = (*i)->circ_id;
  1283. /* Find the channel and circuit, assert that they exist */
  1284. chan = channel_find_by_global_id(chan_id);
  1285. tor_assert(chan);
  1286. circ = circuit_get_by_circid_channel_even_if_marked(circ_id, chan);
  1287. tor_assert(circ);
  1288. /* Clear the circ_is_active bit to start */
  1289. circ_is_active = 0;
  1290. /* Assert that we know which direction this is going */
  1291. tor_assert((*i)->muxinfo.direction == CELL_DIRECTION_OUT ||
  1292. (*i)->muxinfo.direction == CELL_DIRECTION_IN);
  1293. if ((*i)->muxinfo.direction == CELL_DIRECTION_OUT) {
  1294. /* We should be n_mux on this circuit */
  1295. tor_assert(cmux == circ->n_mux);
  1296. tor_assert(chan == circ->n_chan);
  1297. /* Get next and prev for next test */
  1298. next_p = &(circ->next_active_on_n_chan);
  1299. prev_p = &(circ->prev_active_on_n_chan);
  1300. } else {
  1301. /* This should be an or_circuit_t and we should be p_mux */
  1302. or_circ = TO_OR_CIRCUIT(circ);
  1303. tor_assert(cmux == or_circ->p_mux);
  1304. tor_assert(chan == or_circ->p_chan);
  1305. /* Get next and prev for next test */
  1306. next_p = &(or_circ->next_active_on_p_chan);
  1307. prev_p = &(or_circ->prev_active_on_p_chan);
  1308. }
  1309. /*
  1310. * Should this circuit be active? I.e., does the mux know about > 0
  1311. * cells on it?
  1312. */
  1313. circ_is_active = ((*i)->muxinfo.cell_count > 0);
  1314. /* It should be in the linked list iff it's active */
  1315. if (circ_is_active) {
  1316. /* Either we have a next link or we are the tail */
  1317. tor_assert(*next_p || (circ == cmux->active_circuits_tail));
  1318. /* Either we have a prev link or we are the head */
  1319. tor_assert(*prev_p || (circ == cmux->active_circuits_head));
  1320. /* Increment the active circuits counter */
  1321. ++n_active_circuits;
  1322. } else {
  1323. /* Shouldn't be in list, so no next or prev link */
  1324. tor_assert(!(*next_p));
  1325. tor_assert(!(*prev_p));
  1326. /* And can't be head or tail */
  1327. tor_assert(circ != cmux->active_circuits_head);
  1328. tor_assert(circ != cmux->active_circuits_tail);
  1329. }
  1330. /* Increment the circuits counter */
  1331. ++n_circuits;
  1332. /* Adjust the cell counter */
  1333. n_cells += (*i)->muxinfo.cell_count;
  1334. /* Set the mark bit to circ_is_active */
  1335. (*i)->muxinfo.mark = circ_is_active;
  1336. /* Advance to the next entry */
  1337. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  1338. }
  1339. /* Now check the counters */
  1340. tor_assert(n_cells == cmux->n_cells);
  1341. tor_assert(n_circuits == cmux->n_circuits);
  1342. tor_assert(n_active_circuits == cmux->n_active_circuits);
  1343. }
  1344. /**
  1345. * Do the second pass of circuitmux_assert_okay(); see the comment in that
  1346. * function.
  1347. */
  1348. static void
  1349. circuitmux_assert_okay_pass_two(circuitmux_t *cmux)
  1350. {
  1351. circuit_t *curr_circ, *prev_circ = NULL, *next_circ;
  1352. or_circuit_t *curr_or_circ;
  1353. uint64_t curr_chan_id;
  1354. circid_t curr_circ_id;
  1355. circuit_t **next_p, **prev_p;
  1356. channel_t *chan;
  1357. unsigned int n_active_circuits = 0;
  1358. cell_direction_t direction;
  1359. chanid_circid_muxinfo_t search, *hashent = NULL;
  1360. tor_assert(cmux);
  1361. tor_assert(cmux->chanid_circid_map);
  1362. /*
  1363. * Walk the linked list of active circuits in cmux; keep track of the
  1364. * previous circuit seen for consistency checking purposes. Count them
  1365. * to make sure the number in the linked list matches
  1366. * cmux->n_active_circuits.
  1367. */
  1368. curr_circ = cmux->active_circuits_head;
  1369. while (curr_circ) {
  1370. /* Reset some things */
  1371. chan = NULL;
  1372. curr_or_circ = NULL;
  1373. next_circ = NULL;
  1374. next_p = prev_p = NULL;
  1375. direction = 0;
  1376. /* Figure out if this is n_mux or p_mux */
  1377. if (cmux == curr_circ->n_mux) {
  1378. /* Get next_p and prev_p */
  1379. next_p = &(curr_circ->next_active_on_n_chan);
  1380. prev_p = &(curr_circ->prev_active_on_n_chan);
  1381. /* Get the channel */
  1382. chan = curr_circ->n_chan;
  1383. /* Get the circuit id */
  1384. curr_circ_id = curr_circ->n_circ_id;
  1385. /* Remember the direction */
  1386. direction = CELL_DIRECTION_OUT;
  1387. } else {
  1388. /* We must be p_mux and this must be an or_circuit_t */
  1389. curr_or_circ = TO_OR_CIRCUIT(curr_circ);
  1390. tor_assert(cmux == curr_or_circ->p_mux);
  1391. /* Get next_p and prev_p */
  1392. next_p = &(curr_or_circ->next_active_on_p_chan);
  1393. prev_p = &(curr_or_circ->prev_active_on_p_chan);
  1394. /* Get the channel */
  1395. chan = curr_or_circ->p_chan;
  1396. /* Get the circuit id */
  1397. curr_circ_id = curr_or_circ->p_circ_id;
  1398. /* Remember the direction */
  1399. direction = CELL_DIRECTION_IN;
  1400. }
  1401. /* Assert that we got a channel and get the channel ID */
  1402. tor_assert(chan);
  1403. curr_chan_id = chan->global_identifier;
  1404. /* Assert that prev_p points to last circuit we saw */
  1405. tor_assert(*prev_p == prev_circ);
  1406. /* If that's NULL, assert that we are the head */
  1407. if (!(*prev_p)) tor_assert(curr_circ == cmux->active_circuits_head);
  1408. /* Get the next circuit */
  1409. next_circ = *next_p;
  1410. /* If it's NULL, assert that we are the tail */
  1411. if (!(*next_p)) tor_assert(curr_circ == cmux->active_circuits_tail);
  1412. /* Now find the hash table entry for this circuit */
  1413. search.chan_id = curr_chan_id;
  1414. search.circ_id = curr_circ_id;
  1415. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  1416. &search);
  1417. /* Assert that we have one */
  1418. tor_assert(hashent);
  1419. /* Assert that the direction matches */
  1420. tor_assert(direction == hashent->muxinfo.direction);
  1421. /* Assert that the hash entry got marked in pass one */
  1422. tor_assert(hashent->muxinfo.mark);
  1423. /* Clear the mark */
  1424. hashent->muxinfo.mark = 0;
  1425. /* Increment the counter */
  1426. ++n_active_circuits;
  1427. /* Advance to the next active circuit and update prev_circ */
  1428. prev_circ = curr_circ;
  1429. curr_circ = next_circ;
  1430. }
  1431. /* Assert that the counter matches the cmux */
  1432. tor_assert(n_active_circuits == cmux->n_active_circuits);
  1433. }
  1434. /**
  1435. * Do the third pass of circuitmux_assert_okay(); see the comment in that
  1436. * function.
  1437. */
  1438. static void
  1439. circuitmux_assert_okay_pass_three(circuitmux_t *cmux)
  1440. {
  1441. chanid_circid_muxinfo_t **i = NULL;
  1442. tor_assert(cmux);
  1443. tor_assert(cmux->chanid_circid_map);
  1444. /* Start iterating the hash table */
  1445. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  1446. /* Advance through each entry */
  1447. while (i) {
  1448. /* Assert that it isn't null */
  1449. tor_assert(*i);
  1450. /*
  1451. * Assert that this entry is not marked - i.e., that either we didn't
  1452. * think it should be active in pass one or we saw it in the active
  1453. * circuits linked list.
  1454. */
  1455. tor_assert(!((*i)->muxinfo.mark));
  1456. /* Advance to the next entry */
  1457. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  1458. }
  1459. }