circuitmux.c 61 KB

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