circuitmux.c 57 KB

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