circuitmux.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  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. destroy_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. destroy_cell_queue_init(&rv->destroy_cell_queue);
  336. return rv;
  337. }
  338. /**
  339. * Detach all circuits from a circuitmux (use before circuitmux_free())
  340. *
  341. * If <b>detached_out</b> is non-NULL, add every detached circuit_t to
  342. * detached_out.
  343. */
  344. void
  345. circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out)
  346. {
  347. chanid_circid_muxinfo_t **i = NULL, *to_remove;
  348. channel_t *chan = NULL;
  349. circuit_t *circ = NULL;
  350. tor_assert(cmux);
  351. /*
  352. * Don't circuitmux_assert_okay_paranoid() here; this gets called when
  353. * channels are being freed and have already been unregistered, so
  354. * the channel ID lookups it does will fail.
  355. */
  356. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  357. while (i) {
  358. to_remove = *i;
  359. if (! to_remove) {
  360. log_warn(LD_BUG, "Somehow, an HT iterator gave us a NULL pointer.");
  361. break;
  362. } else {
  363. /* Find a channel and circuit */
  364. chan = channel_find_by_global_id(to_remove->chan_id);
  365. if (chan) {
  366. circ =
  367. circuit_get_by_circid_channel_even_if_marked(to_remove->circ_id,
  368. chan);
  369. if (circ) {
  370. /* Clear the circuit's mux for this direction */
  371. if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
  372. /*
  373. * Update active_circuits et al.; this does policy notifies, so
  374. * comes before freeing policy data
  375. */
  376. if (to_remove->muxinfo.cell_count > 0) {
  377. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_OUT);
  378. }
  379. /* Clear n_mux */
  380. circ->n_mux = NULL;
  381. if (detached_out)
  382. smartlist_add(detached_out, circ);
  383. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  384. /*
  385. * Update active_circuits et al.; this does policy notifies, so
  386. * comes before freeing policy data
  387. */
  388. if (to_remove->muxinfo.cell_count > 0) {
  389. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_IN);
  390. }
  391. /*
  392. * It has a sensible p_chan and direction == CELL_DIRECTION_IN,
  393. * so clear p_mux.
  394. */
  395. TO_OR_CIRCUIT(circ)->p_mux = NULL;
  396. if (detached_out)
  397. smartlist_add(detached_out, circ);
  398. } else {
  399. /* Complain and move on */
  400. log_warn(LD_CIRC,
  401. "Circuit %u/channel " U64_FORMAT " had direction == "
  402. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  403. (unsigned)to_remove->circ_id,
  404. U64_PRINTF_ARG(to_remove->chan_id));
  405. }
  406. /* Free policy-specific data if we have it */
  407. if (to_remove->muxinfo.policy_data) {
  408. /*
  409. * If we have policy data, assert that we have the means to
  410. * free it
  411. */
  412. tor_assert(cmux->policy);
  413. tor_assert(cmux->policy->free_circ_data);
  414. /* Call free_circ_data() */
  415. cmux->policy->free_circ_data(cmux,
  416. cmux->policy_data,
  417. circ,
  418. to_remove->muxinfo.policy_data);
  419. to_remove->muxinfo.policy_data = NULL;
  420. }
  421. } else {
  422. /* Complain and move on */
  423. log_warn(LD_CIRC,
  424. "Couldn't find circuit %u (for channel " U64_FORMAT ")",
  425. (unsigned)to_remove->circ_id,
  426. U64_PRINTF_ARG(to_remove->chan_id));
  427. }
  428. } else {
  429. /* Complain and move on */
  430. log_warn(LD_CIRC,
  431. "Couldn't find channel " U64_FORMAT " (for circuit id %u)",
  432. U64_PRINTF_ARG(to_remove->chan_id),
  433. (unsigned)to_remove->circ_id);
  434. }
  435. /* Assert that we don't have un-freed policy data for this circuit */
  436. tor_assert(to_remove->muxinfo.policy_data == NULL);
  437. }
  438. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  439. /* Free it */
  440. tor_free(to_remove);
  441. }
  442. cmux->n_circuits = 0;
  443. cmux->n_active_circuits = 0;
  444. cmux->n_cells = 0;
  445. }
  446. /** Reclaim all circuit IDs currently marked as unusable on <b>chan</b> because
  447. * of pending destroy cells in <b>cmux</b>.
  448. *
  449. * This function must be called AFTER circuits are unlinked from the (channel,
  450. * circuid-id) map with circuit_unlink_all_from_channel(), but before calling
  451. * circuitmux_free().
  452. */
  453. void
  454. circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
  455. {
  456. destroy_cell_t *cell;
  457. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  458. channel_mark_circid_usable(chan, cell->circid);
  459. }
  460. }
  461. /**
  462. * Free a circuitmux_t; the circuits must be detached first with
  463. * circuitmux_detach_all_circuits().
  464. */
  465. void
  466. circuitmux_free(circuitmux_t *cmux)
  467. {
  468. if (!cmux) return;
  469. tor_assert(cmux->n_circuits == 0);
  470. tor_assert(cmux->n_active_circuits == 0);
  471. /*
  472. * Free policy-specific data if we have any; we don't
  473. * need to do circuitmux_set_policy(cmux, NULL) to cover
  474. * the circuits because they would have been handled in
  475. * circuitmux_detach_all_circuits() before this was
  476. * called.
  477. */
  478. if (cmux->policy && cmux->policy->free_cmux_data) {
  479. if (cmux->policy_data) {
  480. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  481. cmux->policy_data = NULL;
  482. }
  483. } else tor_assert(cmux->policy_data == NULL);
  484. if (cmux->chanid_circid_map) {
  485. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  486. tor_free(cmux->chanid_circid_map);
  487. }
  488. /*
  489. * We're throwing away some destroys; log the counter and
  490. * adjust the global counter by the queue size.
  491. */
  492. if (cmux->destroy_cell_queue.n > 0) {
  493. cmux->destroy_ctr -= cmux->destroy_cell_queue.n;
  494. global_destroy_ctr -= cmux->destroy_cell_queue.n;
  495. log_debug(LD_CIRC,
  496. "Freeing cmux at %p with %u queued destroys; the last cmux "
  497. "destroy balance was "I64_FORMAT", global is "I64_FORMAT,
  498. cmux, cmux->destroy_cell_queue.n,
  499. I64_PRINTF_ARG(cmux->destroy_ctr),
  500. I64_PRINTF_ARG(global_destroy_ctr));
  501. } else {
  502. log_debug(LD_CIRC,
  503. "Freeing cmux at %p with no queued destroys, the cmux destroy "
  504. "balance was "I64_FORMAT", global is "I64_FORMAT,
  505. cmux,
  506. I64_PRINTF_ARG(cmux->destroy_ctr),
  507. I64_PRINTF_ARG(global_destroy_ctr));
  508. }
  509. destroy_cell_queue_clear(&cmux->destroy_cell_queue);
  510. tor_free(cmux);
  511. }
  512. /*
  513. * Circuitmux policy control functions
  514. */
  515. /**
  516. * Remove any policy installed on cmux; all policy data will be freed and
  517. * cmux behavior will revert to the built-in round-robin active_circuits
  518. * mechanism.
  519. */
  520. void
  521. circuitmux_clear_policy(circuitmux_t *cmux)
  522. {
  523. tor_assert(cmux);
  524. /* Internally, this is just setting policy to NULL */
  525. if (cmux->policy) {
  526. circuitmux_set_policy(cmux, NULL);
  527. }
  528. }
  529. /**
  530. * Return the policy currently installed on a circuitmux_t
  531. */
  532. const circuitmux_policy_t *
  533. circuitmux_get_policy(circuitmux_t *cmux)
  534. {
  535. tor_assert(cmux);
  536. return cmux->policy;
  537. }
  538. /**
  539. * Set policy; allocate for new policy, detach all circuits from old policy
  540. * if any, attach them to new policy, and free old policy data.
  541. */
  542. void
  543. circuitmux_set_policy(circuitmux_t *cmux,
  544. const circuitmux_policy_t *pol)
  545. {
  546. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  547. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  548. chanid_circid_muxinfo_t **i = NULL;
  549. channel_t *chan = NULL;
  550. uint64_t last_chan_id_searched = 0;
  551. circuit_t *circ = NULL;
  552. tor_assert(cmux);
  553. /* Set up variables */
  554. old_pol = cmux->policy;
  555. old_pol_data = cmux->policy_data;
  556. new_pol = pol;
  557. /* Check if this is the trivial case */
  558. if (old_pol == new_pol) return;
  559. /* Allocate data for new policy, if any */
  560. if (new_pol && new_pol->alloc_cmux_data) {
  561. /*
  562. * If alloc_cmux_data is not null, then we expect to get some policy
  563. * data. Assert that we also have free_cmux_data so we can free it
  564. * when the time comes, and allocate it.
  565. */
  566. tor_assert(new_pol->free_cmux_data);
  567. new_pol_data = new_pol->alloc_cmux_data(cmux);
  568. tor_assert(new_pol_data);
  569. }
  570. /* Install new policy and new policy data on cmux */
  571. cmux->policy = new_pol;
  572. cmux->policy_data = new_pol_data;
  573. /* Iterate over all circuits, attaching/detaching each one */
  574. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  575. while (i) {
  576. /* Assert that this entry isn't NULL */
  577. tor_assert(*i);
  578. /*
  579. * Get the channel; since normal case is all circuits on the mux share a
  580. * channel, we cache last_chan_id_searched
  581. */
  582. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  583. chan = channel_find_by_global_id((*i)->chan_id);
  584. last_chan_id_searched = (*i)->chan_id;
  585. }
  586. tor_assert(chan);
  587. /* Get the circuit */
  588. circ = circuit_get_by_circid_channel_even_if_marked((*i)->circ_id, chan);
  589. tor_assert(circ);
  590. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  591. if (old_pol && old_pol->notify_circ_inactive &&
  592. (*i)->muxinfo.cell_count > 0) {
  593. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  594. (*i)->muxinfo.policy_data);
  595. }
  596. /* Need to free old policy data? */
  597. if ((*i)->muxinfo.policy_data) {
  598. /* Assert that we have the means to free it if we have policy data */
  599. tor_assert(old_pol);
  600. tor_assert(old_pol->free_circ_data);
  601. /* Free it */
  602. old_pol->free_circ_data(cmux, old_pol_data, circ,
  603. (*i)->muxinfo.policy_data);
  604. (*i)->muxinfo.policy_data = NULL;
  605. }
  606. /* Need to allocate new policy data? */
  607. if (new_pol && new_pol->alloc_circ_data) {
  608. /*
  609. * If alloc_circ_data is not null, we expect to get some per-circuit
  610. * policy data. Assert that we also have free_circ_data so we can
  611. * free it when the time comes, and allocate it.
  612. */
  613. tor_assert(new_pol->free_circ_data);
  614. (*i)->muxinfo.policy_data =
  615. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  616. (*i)->muxinfo.direction,
  617. (*i)->muxinfo.cell_count);
  618. }
  619. /* Need to make active on new policy? */
  620. if (new_pol && new_pol->notify_circ_active &&
  621. (*i)->muxinfo.cell_count > 0) {
  622. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  623. (*i)->muxinfo.policy_data);
  624. }
  625. /* Advance to next circuit map entry */
  626. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  627. }
  628. /* Free data for old policy, if any */
  629. if (old_pol_data) {
  630. /*
  631. * If we had old policy data, we should have an old policy and a free
  632. * function for it.
  633. */
  634. tor_assert(old_pol);
  635. tor_assert(old_pol->free_cmux_data);
  636. old_pol->free_cmux_data(cmux, old_pol_data);
  637. old_pol_data = NULL;
  638. }
  639. }
  640. /*
  641. * Circuitmux/circuit attachment status inquiry functions
  642. */
  643. /**
  644. * Query the direction of an attached circuit
  645. */
  646. cell_direction_t
  647. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  648. {
  649. chanid_circid_muxinfo_t *hashent = NULL;
  650. /* Try to find a map entry */
  651. hashent = circuitmux_find_map_entry(cmux, circ);
  652. /*
  653. * This function should only be called on attached circuits; assert that
  654. * we had a map entry.
  655. */
  656. tor_assert(hashent);
  657. /* Return the direction from the map entry */
  658. return hashent->muxinfo.direction;
  659. }
  660. /**
  661. * Find an entry in the cmux's map for this circuit or return NULL if there
  662. * is none.
  663. */
  664. static chanid_circid_muxinfo_t *
  665. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  666. {
  667. chanid_circid_muxinfo_t search, *hashent = NULL;
  668. /* Sanity-check parameters */
  669. tor_assert(cmux);
  670. tor_assert(cmux->chanid_circid_map);
  671. tor_assert(circ);
  672. /* Check if we have n_chan */
  673. if (circ->n_chan) {
  674. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  675. search.chan_id = circ->n_chan->global_identifier;
  676. search.circ_id = circ->n_circ_id;
  677. /* Query */
  678. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  679. &search);
  680. }
  681. /* Found something? */
  682. if (hashent) {
  683. /*
  684. * Assert that the direction makes sense for a hashent we found by
  685. * n_chan/n_circ_id before we return it.
  686. */
  687. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  688. } else {
  689. /* Not there, have we got a p_chan/p_circ_id to try? */
  690. if (circ->magic == OR_CIRCUIT_MAGIC) {
  691. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  692. /* Check for p_chan */
  693. if (TO_OR_CIRCUIT(circ)->p_chan) {
  694. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  695. /* Okay, search for that */
  696. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  697. &search);
  698. /* Find anything? */
  699. if (hashent) {
  700. /* Assert that the direction makes sense before we return it */
  701. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  702. }
  703. }
  704. }
  705. }
  706. /* Okay, hashent is it if it was there */
  707. return hashent;
  708. }
  709. /**
  710. * Query whether a circuit is attached to a circuitmux
  711. */
  712. int
  713. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  714. {
  715. chanid_circid_muxinfo_t *hashent = NULL;
  716. /* Look if it's in the circuit map */
  717. hashent = circuitmux_find_map_entry(cmux, circ);
  718. return (hashent != NULL);
  719. }
  720. /**
  721. * Query whether a circuit is active on a circuitmux
  722. */
  723. int
  724. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  725. {
  726. chanid_circid_muxinfo_t *hashent = NULL;
  727. int is_active = 0;
  728. tor_assert(cmux);
  729. tor_assert(circ);
  730. /* Look if it's in the circuit map */
  731. hashent = circuitmux_find_map_entry(cmux, circ);
  732. if (hashent) {
  733. /* Check the number of cells on this circuit */
  734. is_active = (hashent->muxinfo.cell_count > 0);
  735. }
  736. /* else not attached, so not active */
  737. return is_active;
  738. }
  739. /**
  740. * Query number of available cells for a circuit on a circuitmux
  741. */
  742. unsigned int
  743. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  744. {
  745. chanid_circid_muxinfo_t *hashent = NULL;
  746. unsigned int n_cells = 0;
  747. tor_assert(cmux);
  748. tor_assert(circ);
  749. /* Look if it's in the circuit map */
  750. hashent = circuitmux_find_map_entry(cmux, circ);
  751. if (hashent) {
  752. /* Just get the cell count for this circuit */
  753. n_cells = hashent->muxinfo.cell_count;
  754. }
  755. /* else not attached, so 0 cells */
  756. return n_cells;
  757. }
  758. /**
  759. * Query total number of available cells on a circuitmux
  760. */
  761. unsigned int
  762. circuitmux_num_cells(circuitmux_t *cmux)
  763. {
  764. tor_assert(cmux);
  765. return cmux->n_cells + cmux->destroy_cell_queue.n;
  766. }
  767. /**
  768. * Query total number of circuits active on a circuitmux
  769. */
  770. unsigned int
  771. circuitmux_num_active_circuits(circuitmux_t *cmux)
  772. {
  773. tor_assert(cmux);
  774. return cmux->n_active_circuits;
  775. }
  776. /**
  777. * Query total number of circuits attached to a circuitmux
  778. */
  779. unsigned int
  780. circuitmux_num_circuits(circuitmux_t *cmux)
  781. {
  782. tor_assert(cmux);
  783. return cmux->n_circuits;
  784. }
  785. /*
  786. * Functions for circuit code to call to update circuit status
  787. */
  788. /**
  789. * Attach a circuit to a circuitmux, for the specified direction.
  790. */
  791. MOCK_IMPL(void,
  792. circuitmux_attach_circuit,(circuitmux_t *cmux, circuit_t *circ,
  793. cell_direction_t direction))
  794. {
  795. channel_t *chan = NULL;
  796. uint64_t channel_id;
  797. circid_t circ_id;
  798. chanid_circid_muxinfo_t search, *hashent = NULL;
  799. unsigned int cell_count;
  800. tor_assert(cmux);
  801. tor_assert(circ);
  802. tor_assert(direction == CELL_DIRECTION_IN ||
  803. direction == CELL_DIRECTION_OUT);
  804. circuitmux_assert_okay_paranoid(cmux);
  805. /*
  806. * Figure out which channel we're using, and get the circuit's current
  807. * cell count and circuit ID; assert that the circuit is not already
  808. * attached to another mux.
  809. */
  810. if (direction == CELL_DIRECTION_OUT) {
  811. /* It's n_chan */
  812. chan = circ->n_chan;
  813. cell_count = circ->n_chan_cells.n;
  814. circ_id = circ->n_circ_id;
  815. } else {
  816. /* We want p_chan */
  817. chan = TO_OR_CIRCUIT(circ)->p_chan;
  818. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  819. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  820. }
  821. /* Assert that we did get a channel */
  822. tor_assert(chan);
  823. /* Assert that the circuit ID is sensible */
  824. tor_assert(circ_id != 0);
  825. /* Get the channel ID */
  826. channel_id = chan->global_identifier;
  827. /* See if we already have this one */
  828. search.chan_id = channel_id;
  829. search.circ_id = circ_id;
  830. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  831. &search);
  832. if (hashent) {
  833. /*
  834. * This circuit was already attached to this cmux; make sure the
  835. * directions match and update the cell count and active circuit count.
  836. */
  837. log_info(LD_CIRC,
  838. "Circuit %u on channel " U64_FORMAT " was already attached to "
  839. "cmux %p (trying to attach to %p)",
  840. (unsigned)circ_id, U64_PRINTF_ARG(channel_id),
  841. ((direction == CELL_DIRECTION_OUT) ?
  842. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  843. cmux);
  844. /*
  845. * The mux pointer on this circuit and the direction in result should
  846. * match; otherwise assert.
  847. */
  848. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  849. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  850. tor_assert(hashent->muxinfo.direction == direction);
  851. /*
  852. * Looks okay; just update the cell count and active circuits if we must
  853. */
  854. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  855. --(cmux->n_active_circuits);
  856. circuitmux_make_circuit_inactive(cmux, circ, direction);
  857. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  858. ++(cmux->n_active_circuits);
  859. circuitmux_make_circuit_active(cmux, circ, direction);
  860. }
  861. cmux->n_cells -= hashent->muxinfo.cell_count;
  862. cmux->n_cells += cell_count;
  863. hashent->muxinfo.cell_count = cell_count;
  864. } else {
  865. /*
  866. * New circuit; add an entry and update the circuit/active circuit
  867. * counts.
  868. */
  869. log_debug(LD_CIRC,
  870. "Attaching circuit %u on channel " U64_FORMAT " to cmux %p",
  871. (unsigned)circ_id, U64_PRINTF_ARG(channel_id), cmux);
  872. /*
  873. * Assert that the circuit doesn't already have a mux for this
  874. * direction.
  875. */
  876. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  877. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  878. /* Insert it in the map */
  879. hashent = tor_malloc_zero(sizeof(*hashent));
  880. hashent->chan_id = channel_id;
  881. hashent->circ_id = circ_id;
  882. hashent->muxinfo.cell_count = cell_count;
  883. hashent->muxinfo.direction = direction;
  884. /* Allocate policy specific circuit data if we need it */
  885. if (cmux->policy && cmux->policy->alloc_circ_data) {
  886. /* Assert that we have the means to free policy-specific data */
  887. tor_assert(cmux->policy->free_circ_data);
  888. /* Allocate it */
  889. hashent->muxinfo.policy_data =
  890. cmux->policy->alloc_circ_data(cmux,
  891. cmux->policy_data,
  892. circ,
  893. direction,
  894. cell_count);
  895. /* If we wanted policy data, it's an error not to get any */
  896. tor_assert(hashent->muxinfo.policy_data);
  897. }
  898. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  899. hashent);
  900. /* Set the circuit's mux for this direction */
  901. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  902. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  903. /* Make sure the next/prev pointers are NULL */
  904. if (direction == CELL_DIRECTION_OUT) {
  905. circ->next_active_on_n_chan = NULL;
  906. circ->prev_active_on_n_chan = NULL;
  907. } else {
  908. TO_OR_CIRCUIT(circ)->next_active_on_p_chan = NULL;
  909. TO_OR_CIRCUIT(circ)->prev_active_on_p_chan = NULL;
  910. }
  911. /* Update counters */
  912. ++(cmux->n_circuits);
  913. if (cell_count > 0) {
  914. ++(cmux->n_active_circuits);
  915. circuitmux_make_circuit_active(cmux, circ, direction);
  916. }
  917. cmux->n_cells += cell_count;
  918. }
  919. circuitmux_assert_okay_paranoid(cmux);
  920. }
  921. /**
  922. * Detach a circuit from a circuitmux and update all counters as needed;
  923. * no-op if not attached.
  924. */
  925. MOCK_IMPL(void,
  926. circuitmux_detach_circuit,(circuitmux_t *cmux, circuit_t *circ))
  927. {
  928. chanid_circid_muxinfo_t search, *hashent = NULL;
  929. /*
  930. * Use this to keep track of whether we found it for n_chan or
  931. * p_chan for consistency checking.
  932. *
  933. * The 0 initializer is not a valid cell_direction_t value.
  934. * We assert that it has been replaced with a valid value before it is used.
  935. */
  936. cell_direction_t last_searched_direction = 0;
  937. tor_assert(cmux);
  938. tor_assert(cmux->chanid_circid_map);
  939. tor_assert(circ);
  940. circuitmux_assert_okay_paranoid(cmux);
  941. /* See if we have it for n_chan/n_circ_id */
  942. if (circ->n_chan) {
  943. search.chan_id = circ->n_chan->global_identifier;
  944. search.circ_id = circ->n_circ_id;
  945. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  946. &search);
  947. last_searched_direction = CELL_DIRECTION_OUT;
  948. }
  949. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  950. if (!hashent) {
  951. if (circ->magic == OR_CIRCUIT_MAGIC) {
  952. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  953. if (TO_OR_CIRCUIT(circ)->p_chan) {
  954. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  955. hashent = HT_FIND(chanid_circid_muxinfo_map,
  956. cmux->chanid_circid_map,
  957. &search);
  958. last_searched_direction = CELL_DIRECTION_IN;
  959. }
  960. }
  961. }
  962. tor_assert(last_searched_direction == CELL_DIRECTION_OUT
  963. || last_searched_direction == CELL_DIRECTION_IN);
  964. /*
  965. * If hashent isn't NULL, we have a circuit to detach; don't remove it from
  966. * the map until later of circuitmux_make_circuit_inactive() breaks.
  967. */
  968. if (hashent) {
  969. /* Update counters */
  970. --(cmux->n_circuits);
  971. if (hashent->muxinfo.cell_count > 0) {
  972. --(cmux->n_active_circuits);
  973. /* This does policy notifies, so comes before freeing policy data */
  974. circuitmux_make_circuit_inactive(cmux, circ, last_searched_direction);
  975. }
  976. cmux->n_cells -= hashent->muxinfo.cell_count;
  977. /* Free policy-specific data if we have it */
  978. if (hashent->muxinfo.policy_data) {
  979. /* If we have policy data, assert that we have the means to free it */
  980. tor_assert(cmux->policy);
  981. tor_assert(cmux->policy->free_circ_data);
  982. /* Call free_circ_data() */
  983. cmux->policy->free_circ_data(cmux,
  984. cmux->policy_data,
  985. circ,
  986. hashent->muxinfo.policy_data);
  987. hashent->muxinfo.policy_data = NULL;
  988. }
  989. /* Consistency check: the direction must match the direction searched */
  990. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  991. /* Clear the circuit's mux for this direction */
  992. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  993. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  994. /* Now remove it from the map */
  995. HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map, hashent);
  996. /* Free the hash entry */
  997. tor_free(hashent);
  998. }
  999. circuitmux_assert_okay_paranoid(cmux);
  1000. }
  1001. /**
  1002. * Make a circuit active; update active list and policy-specific info, but
  1003. * we don't mess with the counters or hash table here.
  1004. */
  1005. static void
  1006. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  1007. cell_direction_t direction)
  1008. {
  1009. circuit_t **next_active = NULL, **prev_active = NULL, **next_prev = NULL;
  1010. circuitmux_t *circuit_cmux = NULL;
  1011. chanid_circid_muxinfo_t *hashent = NULL;
  1012. channel_t *chan = NULL;
  1013. circid_t circ_id;
  1014. int already_active;
  1015. tor_assert(cmux);
  1016. tor_assert(circ);
  1017. tor_assert(direction == CELL_DIRECTION_OUT ||
  1018. direction == CELL_DIRECTION_IN);
  1019. /*
  1020. * Don't circuitmux_assert_okay_paranoid(cmux) here because the cell count
  1021. * already got changed and we have to update the list for it to be consistent
  1022. * again.
  1023. */
  1024. /* Get the right set of active list links for this direction */
  1025. if (direction == CELL_DIRECTION_OUT) {
  1026. next_active = &(circ->next_active_on_n_chan);
  1027. prev_active = &(circ->prev_active_on_n_chan);
  1028. circuit_cmux = circ->n_mux;
  1029. chan = circ->n_chan;
  1030. circ_id = circ->n_circ_id;
  1031. } else {
  1032. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  1033. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  1034. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  1035. chan = TO_OR_CIRCUIT(circ)->p_chan;
  1036. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  1037. }
  1038. /* Assert that it is attached to this mux and a channel */
  1039. tor_assert(cmux == circuit_cmux);
  1040. tor_assert(chan != NULL);
  1041. /*
  1042. * Check if the circuit really was inactive; if it's active, at least one
  1043. * of the next_active and prev_active pointers will not be NULL, or this
  1044. * circuit will be either the head or tail of the list for this cmux.
  1045. */
  1046. already_active = (*prev_active != NULL || *next_active != NULL ||
  1047. cmux->active_circuits_head == circ ||
  1048. cmux->active_circuits_tail == circ);
  1049. /* If we're already active, log a warning and finish */
  1050. if (already_active) {
  1051. log_warn(LD_CIRC,
  1052. "Circuit %u on channel " U64_FORMAT " was already active",
  1053. (unsigned)circ_id, U64_PRINTF_ARG(chan->global_identifier));
  1054. return;
  1055. }
  1056. /*
  1057. * This is going at the head of the list; if the old head is not NULL,
  1058. * then its prev pointer should point to this.
  1059. */
  1060. *next_active = cmux->active_circuits_head; /* Next is old head */
  1061. *prev_active = NULL; /* Prev is NULL (this will be the head) */
  1062. if (cmux->active_circuits_head) {
  1063. /* The list had an old head; update its prev pointer */
  1064. next_prev =
  1065. circuitmux_prev_active_circ_p(cmux, cmux->active_circuits_head);
  1066. tor_assert(next_prev);
  1067. *next_prev = circ;
  1068. } else {
  1069. /* The list was empty; this becomes the tail as well */
  1070. cmux->active_circuits_tail = circ;
  1071. }
  1072. /* This becomes the new head of the list */
  1073. cmux->active_circuits_head = circ;
  1074. /* Policy-specific notification */
  1075. if (cmux->policy &&
  1076. cmux->policy->notify_circ_active) {
  1077. /* Okay, we need to check the circuit for policy data now */
  1078. hashent = circuitmux_find_map_entry(cmux, circ);
  1079. /* We should have found something */
  1080. tor_assert(hashent);
  1081. /* Notify */
  1082. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  1083. circ, hashent->muxinfo.policy_data);
  1084. }
  1085. circuitmux_assert_okay_paranoid(cmux);
  1086. }
  1087. /**
  1088. * Make a circuit inactive; update active list and policy-specific info, but
  1089. * we don't mess with the counters or hash table here.
  1090. */
  1091. static void
  1092. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  1093. cell_direction_t direction)
  1094. {
  1095. circuit_t **next_active = NULL, **prev_active = NULL;
  1096. circuit_t **next_prev = NULL, **prev_next = NULL;
  1097. circuitmux_t *circuit_cmux = NULL;
  1098. chanid_circid_muxinfo_t *hashent = NULL;
  1099. channel_t *chan = NULL;
  1100. circid_t circ_id;
  1101. int already_inactive;
  1102. tor_assert(cmux);
  1103. tor_assert(circ);
  1104. tor_assert(direction == CELL_DIRECTION_OUT ||
  1105. direction == CELL_DIRECTION_IN);
  1106. /*
  1107. * Don't circuitmux_assert_okay_paranoid(cmux) here because the cell count
  1108. * already got changed and we have to update the list for it to be consistent
  1109. * again.
  1110. */
  1111. /* Get the right set of active list links for this direction */
  1112. if (direction == CELL_DIRECTION_OUT) {
  1113. next_active = &(circ->next_active_on_n_chan);
  1114. prev_active = &(circ->prev_active_on_n_chan);
  1115. circuit_cmux = circ->n_mux;
  1116. chan = circ->n_chan;
  1117. circ_id = circ->n_circ_id;
  1118. } else {
  1119. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  1120. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  1121. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  1122. chan = TO_OR_CIRCUIT(circ)->p_chan;
  1123. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  1124. }
  1125. /* Assert that it is attached to this mux and a channel */
  1126. tor_assert(cmux == circuit_cmux);
  1127. tor_assert(chan != NULL);
  1128. /*
  1129. * Check if the circuit really was active; if it's inactive, the
  1130. * next_active and prev_active pointers will be NULL and this circuit
  1131. * will not be the head or tail of the list for this cmux.
  1132. */
  1133. already_inactive = (*prev_active == NULL && *next_active == NULL &&
  1134. cmux->active_circuits_head != circ &&
  1135. cmux->active_circuits_tail != circ);
  1136. /* If we're already inactive, log a warning and finish */
  1137. if (already_inactive) {
  1138. log_warn(LD_CIRC,
  1139. "Circuit %d on channel " U64_FORMAT " was already inactive",
  1140. (unsigned)circ_id, U64_PRINTF_ARG(chan->global_identifier));
  1141. return;
  1142. }
  1143. /* Remove from the list; first get next_prev and prev_next */
  1144. if (*next_active) {
  1145. /*
  1146. * If there's a next circuit, its previous circuit becomes this
  1147. * circuit's previous circuit.
  1148. */
  1149. next_prev = circuitmux_prev_active_circ_p(cmux, *next_active);
  1150. } else {
  1151. /* Else, the tail becomes this circuit's previous circuit */
  1152. next_prev = &(cmux->active_circuits_tail);
  1153. }
  1154. /* Got next_prev, now prev_next */
  1155. if (*prev_active) {
  1156. /*
  1157. * If there's a previous circuit, its next circuit becomes this circuit's
  1158. * next circuit.
  1159. */
  1160. prev_next = circuitmux_next_active_circ_p(cmux, *prev_active);
  1161. } else {
  1162. /* Else, the head becomes this circuit's next circuit */
  1163. prev_next = &(cmux->active_circuits_head);
  1164. }
  1165. /* Assert that we got sensible values for the next/prev pointers */
  1166. tor_assert(next_prev != NULL);
  1167. tor_assert(prev_next != NULL);
  1168. /* Update the next/prev pointers - this removes circ from the list */
  1169. *next_prev = *prev_active;
  1170. *prev_next = *next_active;
  1171. /* Now null out prev_active/next_active */
  1172. *prev_active = NULL;
  1173. *next_active = NULL;
  1174. /* Policy-specific notification */
  1175. if (cmux->policy &&
  1176. cmux->policy->notify_circ_inactive) {
  1177. /* Okay, we need to check the circuit for policy data now */
  1178. hashent = circuitmux_find_map_entry(cmux, circ);
  1179. /* We should have found something */
  1180. tor_assert(hashent);
  1181. /* Notify */
  1182. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  1183. circ, hashent->muxinfo.policy_data);
  1184. }
  1185. circuitmux_assert_okay_paranoid(cmux);
  1186. }
  1187. /**
  1188. * Clear the cell counter for a circuit on a circuitmux
  1189. */
  1190. void
  1191. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  1192. {
  1193. /* This is the same as setting the cell count to zero */
  1194. circuitmux_set_num_cells(cmux, circ, 0);
  1195. }
  1196. /**
  1197. * Set the cell counter for a circuit on a circuitmux
  1198. */
  1199. void
  1200. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  1201. unsigned int n_cells)
  1202. {
  1203. chanid_circid_muxinfo_t *hashent = NULL;
  1204. tor_assert(cmux);
  1205. tor_assert(circ);
  1206. circuitmux_assert_okay_paranoid(cmux);
  1207. /* Search for this circuit's entry */
  1208. hashent = circuitmux_find_map_entry(cmux, circ);
  1209. /* Assert that we found one */
  1210. tor_assert(hashent);
  1211. /* Update cmux cell counter */
  1212. cmux->n_cells -= hashent->muxinfo.cell_count;
  1213. cmux->n_cells += n_cells;
  1214. /* Do we need to notify a cmux policy? */
  1215. if (cmux->policy && cmux->policy->notify_set_n_cells) {
  1216. /* Call notify_set_n_cells */
  1217. cmux->policy->notify_set_n_cells(cmux,
  1218. cmux->policy_data,
  1219. circ,
  1220. hashent->muxinfo.policy_data,
  1221. n_cells);
  1222. }
  1223. /*
  1224. * Update cmux active circuit counter: is the old cell count > 0 and the
  1225. * new cell count == 0 ?
  1226. */
  1227. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  1228. --(cmux->n_active_circuits);
  1229. hashent->muxinfo.cell_count = n_cells;
  1230. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1231. /* Is the old cell count == 0 and the new cell count > 0 ? */
  1232. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  1233. ++(cmux->n_active_circuits);
  1234. hashent->muxinfo.cell_count = n_cells;
  1235. circuitmux_make_circuit_active(cmux, circ, hashent->muxinfo.direction);
  1236. } else {
  1237. /*
  1238. * Update the entry cell count like this so we can put a
  1239. * circuitmux_assert_okay_paranoid inside make_circuit_(in)active() too.
  1240. */
  1241. hashent->muxinfo.cell_count = n_cells;
  1242. }
  1243. circuitmux_assert_okay_paranoid(cmux);
  1244. }
  1245. /*
  1246. * Functions for channel code to call to get a circuit to transmit from or
  1247. * notify that cells have been transmitted.
  1248. */
  1249. /**
  1250. * Pick a circuit to send from, using the active circuits list or a
  1251. * circuitmux policy if one is available. This is called from channel.c.
  1252. *
  1253. * If we would rather send a destroy cell, return NULL and set
  1254. * *<b>destroy_queue_out</b> to the destroy queue.
  1255. *
  1256. * If we have nothing to send, set *<b>destroy_queue_out</b> to NULL and
  1257. * return NULL.
  1258. */
  1259. circuit_t *
  1260. circuitmux_get_first_active_circuit(circuitmux_t *cmux,
  1261. destroy_cell_queue_t **destroy_queue_out)
  1262. {
  1263. circuit_t *circ = NULL;
  1264. tor_assert(cmux);
  1265. tor_assert(destroy_queue_out);
  1266. *destroy_queue_out = NULL;
  1267. if (cmux->destroy_cell_queue.n &&
  1268. (!cmux->last_cell_was_destroy || cmux->n_active_circuits == 0)) {
  1269. /* We have destroy cells to send, and either we just sent a relay cell,
  1270. * or we have no relay cells to send. */
  1271. /* XXXX We should let the cmux policy have some say in this eventually. */
  1272. /* XXXX Alternating is not a terribly brilliant approach here. */
  1273. *destroy_queue_out = &cmux->destroy_cell_queue;
  1274. cmux->last_cell_was_destroy = 1;
  1275. } else if (cmux->n_active_circuits > 0) {
  1276. /* We also must have a cell available for this to be the case */
  1277. tor_assert(cmux->n_cells > 0);
  1278. /* Do we have a policy-provided circuit selector? */
  1279. if (cmux->policy && cmux->policy->pick_active_circuit) {
  1280. circ = cmux->policy->pick_active_circuit(cmux, cmux->policy_data);
  1281. }
  1282. /* Fall back on the head of the active circuits list */
  1283. if (!circ) {
  1284. tor_assert(cmux->active_circuits_head);
  1285. circ = cmux->active_circuits_head;
  1286. }
  1287. cmux->last_cell_was_destroy = 0;
  1288. } else {
  1289. tor_assert(cmux->n_cells == 0);
  1290. tor_assert(cmux->destroy_cell_queue.n == 0);
  1291. }
  1292. return circ;
  1293. }
  1294. /**
  1295. * Notify the circuitmux that cells have been sent on a circuit; this
  1296. * is called from channel.c.
  1297. */
  1298. void
  1299. circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  1300. unsigned int n_cells)
  1301. {
  1302. chanid_circid_muxinfo_t *hashent = NULL;
  1303. int becomes_inactive = 0;
  1304. tor_assert(cmux);
  1305. tor_assert(circ);
  1306. circuitmux_assert_okay_paranoid(cmux);
  1307. if (n_cells == 0) return;
  1308. /*
  1309. * To handle this, we have to:
  1310. *
  1311. * 1.) Adjust the circuit's cell counter in the cmux hash table
  1312. * 2.) Move the circuit to the tail of the active_circuits linked list
  1313. * for this cmux, or make the circuit inactive if the cell count
  1314. * went to zero.
  1315. * 3.) Call cmux->policy->notify_xmit_cells(), if any
  1316. */
  1317. /* Find the hash entry */
  1318. hashent = circuitmux_find_map_entry(cmux, circ);
  1319. /* Assert that we found one */
  1320. tor_assert(hashent);
  1321. /* Adjust the cell counter and assert that we had that many cells to send */
  1322. tor_assert(n_cells <= hashent->muxinfo.cell_count);
  1323. hashent->muxinfo.cell_count -= n_cells;
  1324. /* Do we need to make the circuit inactive? */
  1325. if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
  1326. /* Adjust the mux cell counter */
  1327. cmux->n_cells -= n_cells;
  1328. /* If we aren't making it inactive later, move it to the tail of the list */
  1329. if (!becomes_inactive) {
  1330. circuitmux_move_active_circ_to_tail(cmux, circ,
  1331. hashent->muxinfo.direction);
  1332. }
  1333. /*
  1334. * We call notify_xmit_cells() before making the circuit inactive if needed,
  1335. * so the policy can always count on this coming in on an active circuit.
  1336. */
  1337. if (cmux->policy && cmux->policy->notify_xmit_cells) {
  1338. cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
  1339. hashent->muxinfo.policy_data,
  1340. n_cells);
  1341. }
  1342. /*
  1343. * Now make the circuit inactive if needed; this will call the policy's
  1344. * notify_circ_inactive() if present.
  1345. */
  1346. if (becomes_inactive) {
  1347. --(cmux->n_active_circuits);
  1348. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1349. }
  1350. circuitmux_assert_okay_paranoid(cmux);
  1351. }
  1352. /**
  1353. * Notify the circuitmux that a destroy was sent, so we can update
  1354. * the counter.
  1355. */
  1356. void
  1357. circuitmux_notify_xmit_destroy(circuitmux_t *cmux)
  1358. {
  1359. tor_assert(cmux);
  1360. --(cmux->destroy_ctr);
  1361. --(global_destroy_ctr);
  1362. log_debug(LD_CIRC,
  1363. "Cmux at %p sent a destroy, cmux counter is now "I64_FORMAT", "
  1364. "global counter is now "I64_FORMAT,
  1365. cmux,
  1366. I64_PRINTF_ARG(cmux->destroy_ctr),
  1367. I64_PRINTF_ARG(global_destroy_ctr));
  1368. }
  1369. /*
  1370. * Circuitmux consistency checking assertions
  1371. */
  1372. /**
  1373. * Check that circuitmux data structures are consistent and fail with an
  1374. * assert if not.
  1375. */
  1376. void
  1377. circuitmux_assert_okay(circuitmux_t *cmux)
  1378. {
  1379. tor_assert(cmux);
  1380. /*
  1381. * Pass 1: iterate the hash table; for each entry:
  1382. * a) Check that the circuit has this cmux for n_mux or p_mux
  1383. * b) If the cell_count is > 0, set the mark bit; otherwise clear it
  1384. * c) Also check activeness (cell_count > 0 should be active)
  1385. * d) Count the number of circuits, active circuits and queued cells
  1386. * and at the end check that they match the counters in the cmux.
  1387. *
  1388. * Pass 2: iterate the active circuits list; for each entry,
  1389. * make sure the circuit is attached to this mux and appears
  1390. * in the hash table. Make sure the mark bit is 1, and clear
  1391. * it in the hash table entry. Consistency-check the linked
  1392. * list pointers.
  1393. *
  1394. * Pass 3: iterate the hash table again; assert if any active circuits
  1395. * (mark bit set to 1) are discovered that weren't cleared in pass 2
  1396. * (don't appear in the linked list).
  1397. */
  1398. circuitmux_assert_okay_pass_one(cmux);
  1399. circuitmux_assert_okay_pass_two(cmux);
  1400. circuitmux_assert_okay_pass_three(cmux);
  1401. }
  1402. /**
  1403. * Do the first pass of circuitmux_assert_okay(); see the comment in that
  1404. * function.
  1405. */
  1406. static void
  1407. circuitmux_assert_okay_pass_one(circuitmux_t *cmux)
  1408. {
  1409. chanid_circid_muxinfo_t **i = NULL;
  1410. uint64_t chan_id;
  1411. channel_t *chan;
  1412. circid_t circ_id;
  1413. circuit_t *circ;
  1414. or_circuit_t *or_circ;
  1415. unsigned int circ_is_active;
  1416. circuit_t **next_p, **prev_p;
  1417. unsigned int n_circuits, n_active_circuits, n_cells;
  1418. tor_assert(cmux);
  1419. tor_assert(cmux->chanid_circid_map);
  1420. /* Reset the counters */
  1421. n_circuits = n_active_circuits = n_cells = 0;
  1422. /* Start iterating the hash table */
  1423. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  1424. while (i) {
  1425. /* Assert that the hash table entry isn't null */
  1426. tor_assert(*i);
  1427. /* Get the channel and circuit id */
  1428. chan_id = (*i)->chan_id;
  1429. circ_id = (*i)->circ_id;
  1430. /* Find the channel and circuit, assert that they exist */
  1431. chan = channel_find_by_global_id(chan_id);
  1432. tor_assert(chan);
  1433. circ = circuit_get_by_circid_channel_even_if_marked(circ_id, chan);
  1434. tor_assert(circ);
  1435. /* Clear the circ_is_active bit to start */
  1436. circ_is_active = 0;
  1437. /* Assert that we know which direction this is going */
  1438. tor_assert((*i)->muxinfo.direction == CELL_DIRECTION_OUT ||
  1439. (*i)->muxinfo.direction == CELL_DIRECTION_IN);
  1440. if ((*i)->muxinfo.direction == CELL_DIRECTION_OUT) {
  1441. /* We should be n_mux on this circuit */
  1442. tor_assert(cmux == circ->n_mux);
  1443. tor_assert(chan == circ->n_chan);
  1444. /* Get next and prev for next test */
  1445. next_p = &(circ->next_active_on_n_chan);
  1446. prev_p = &(circ->prev_active_on_n_chan);
  1447. } else {
  1448. /* This should be an or_circuit_t and we should be p_mux */
  1449. or_circ = TO_OR_CIRCUIT(circ);
  1450. tor_assert(cmux == or_circ->p_mux);
  1451. tor_assert(chan == or_circ->p_chan);
  1452. /* Get next and prev for next test */
  1453. next_p = &(or_circ->next_active_on_p_chan);
  1454. prev_p = &(or_circ->prev_active_on_p_chan);
  1455. }
  1456. /*
  1457. * Should this circuit be active? I.e., does the mux know about > 0
  1458. * cells on it?
  1459. */
  1460. circ_is_active = ((*i)->muxinfo.cell_count > 0);
  1461. /* It should be in the linked list iff it's active */
  1462. if (circ_is_active) {
  1463. /* Either we have a next link or we are the tail */
  1464. tor_assert(*next_p || (circ == cmux->active_circuits_tail));
  1465. /* Either we have a prev link or we are the head */
  1466. tor_assert(*prev_p || (circ == cmux->active_circuits_head));
  1467. /* Increment the active circuits counter */
  1468. ++n_active_circuits;
  1469. } else {
  1470. /* Shouldn't be in list, so no next or prev link */
  1471. tor_assert(!(*next_p));
  1472. tor_assert(!(*prev_p));
  1473. /* And can't be head or tail */
  1474. tor_assert(circ != cmux->active_circuits_head);
  1475. tor_assert(circ != cmux->active_circuits_tail);
  1476. }
  1477. /* Increment the circuits counter */
  1478. ++n_circuits;
  1479. /* Adjust the cell counter */
  1480. n_cells += (*i)->muxinfo.cell_count;
  1481. /* Set the mark bit to circ_is_active */
  1482. (*i)->muxinfo.mark = circ_is_active;
  1483. /* Advance to the next entry */
  1484. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  1485. }
  1486. /* Now check the counters */
  1487. tor_assert(n_cells == cmux->n_cells);
  1488. tor_assert(n_circuits == cmux->n_circuits);
  1489. tor_assert(n_active_circuits == cmux->n_active_circuits);
  1490. }
  1491. /**
  1492. * Do the second pass of circuitmux_assert_okay(); see the comment in that
  1493. * function.
  1494. */
  1495. static void
  1496. circuitmux_assert_okay_pass_two(circuitmux_t *cmux)
  1497. {
  1498. circuit_t *curr_circ, *prev_circ = NULL, *next_circ;
  1499. or_circuit_t *curr_or_circ;
  1500. uint64_t curr_chan_id;
  1501. circid_t curr_circ_id;
  1502. circuit_t **next_p, **prev_p;
  1503. channel_t *chan;
  1504. unsigned int n_active_circuits = 0;
  1505. cell_direction_t direction;
  1506. chanid_circid_muxinfo_t search, *hashent = NULL;
  1507. tor_assert(cmux);
  1508. tor_assert(cmux->chanid_circid_map);
  1509. /*
  1510. * Walk the linked list of active circuits in cmux; keep track of the
  1511. * previous circuit seen for consistency checking purposes. Count them
  1512. * to make sure the number in the linked list matches
  1513. * cmux->n_active_circuits.
  1514. */
  1515. curr_circ = cmux->active_circuits_head;
  1516. while (curr_circ) {
  1517. /* Reset some things */
  1518. chan = NULL;
  1519. curr_or_circ = NULL;
  1520. next_circ = NULL;
  1521. next_p = prev_p = NULL;
  1522. direction = 0;
  1523. /* Figure out if this is n_mux or p_mux */
  1524. if (cmux == curr_circ->n_mux) {
  1525. /* Get next_p and prev_p */
  1526. next_p = &(curr_circ->next_active_on_n_chan);
  1527. prev_p = &(curr_circ->prev_active_on_n_chan);
  1528. /* Get the channel */
  1529. chan = curr_circ->n_chan;
  1530. /* Get the circuit id */
  1531. curr_circ_id = curr_circ->n_circ_id;
  1532. /* Remember the direction */
  1533. direction = CELL_DIRECTION_OUT;
  1534. } else {
  1535. /* We must be p_mux and this must be an or_circuit_t */
  1536. curr_or_circ = TO_OR_CIRCUIT(curr_circ);
  1537. tor_assert(cmux == curr_or_circ->p_mux);
  1538. /* Get next_p and prev_p */
  1539. next_p = &(curr_or_circ->next_active_on_p_chan);
  1540. prev_p = &(curr_or_circ->prev_active_on_p_chan);
  1541. /* Get the channel */
  1542. chan = curr_or_circ->p_chan;
  1543. /* Get the circuit id */
  1544. curr_circ_id = curr_or_circ->p_circ_id;
  1545. /* Remember the direction */
  1546. direction = CELL_DIRECTION_IN;
  1547. }
  1548. /* Assert that we got a channel and get the channel ID */
  1549. tor_assert(chan);
  1550. curr_chan_id = chan->global_identifier;
  1551. /* Assert that prev_p points to last circuit we saw */
  1552. tor_assert(*prev_p == prev_circ);
  1553. /* If that's NULL, assert that we are the head */
  1554. if (!(*prev_p)) tor_assert(curr_circ == cmux->active_circuits_head);
  1555. /* Get the next circuit */
  1556. next_circ = *next_p;
  1557. /* If it's NULL, assert that we are the tail */
  1558. if (!(*next_p)) tor_assert(curr_circ == cmux->active_circuits_tail);
  1559. /* Now find the hash table entry for this circuit */
  1560. search.chan_id = curr_chan_id;
  1561. search.circ_id = curr_circ_id;
  1562. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  1563. &search);
  1564. /* Assert that we have one */
  1565. tor_assert(hashent);
  1566. /* Assert that the direction matches */
  1567. tor_assert(direction == hashent->muxinfo.direction);
  1568. /* Assert that the hash entry got marked in pass one */
  1569. tor_assert(hashent->muxinfo.mark);
  1570. /* Clear the mark */
  1571. hashent->muxinfo.mark = 0;
  1572. /* Increment the counter */
  1573. ++n_active_circuits;
  1574. /* Advance to the next active circuit and update prev_circ */
  1575. prev_circ = curr_circ;
  1576. curr_circ = next_circ;
  1577. }
  1578. /* Assert that the counter matches the cmux */
  1579. tor_assert(n_active_circuits == cmux->n_active_circuits);
  1580. }
  1581. /**
  1582. * Do the third pass of circuitmux_assert_okay(); see the comment in that
  1583. * function.
  1584. */
  1585. static void
  1586. circuitmux_assert_okay_pass_three(circuitmux_t *cmux)
  1587. {
  1588. chanid_circid_muxinfo_t **i = NULL;
  1589. tor_assert(cmux);
  1590. tor_assert(cmux->chanid_circid_map);
  1591. /* Start iterating the hash table */
  1592. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  1593. /* Advance through each entry */
  1594. while (i) {
  1595. /* Assert that it isn't null */
  1596. tor_assert(*i);
  1597. /*
  1598. * Assert that this entry is not marked - i.e., that either we didn't
  1599. * think it should be active in pass one or we saw it in the active
  1600. * circuits linked list.
  1601. */
  1602. tor_assert(!((*i)->muxinfo.mark));
  1603. /* Advance to the next entry */
  1604. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  1605. }
  1606. }
  1607. /*DOCDOC */
  1608. void
  1609. circuitmux_append_destroy_cell(channel_t *chan,
  1610. circuitmux_t *cmux,
  1611. circid_t circ_id,
  1612. uint8_t reason)
  1613. {
  1614. destroy_cell_queue_append(&cmux->destroy_cell_queue, circ_id, reason);
  1615. /* Destroy entering the queue, update counters */
  1616. ++(cmux->destroy_ctr);
  1617. ++global_destroy_ctr;
  1618. log_debug(LD_CIRC,
  1619. "Cmux at %p queued a destroy for circ %u, cmux counter is now "
  1620. I64_FORMAT", global counter is now "I64_FORMAT,
  1621. cmux, circ_id,
  1622. I64_PRINTF_ARG(cmux->destroy_ctr),
  1623. I64_PRINTF_ARG(global_destroy_ctr));
  1624. /* XXXX Duplicate code from append_cell_to_circuit_queue */
  1625. if (!channel_has_queued_writes(chan)) {
  1626. /* There is no data at all waiting to be sent on the outbuf. Add a
  1627. * cell, so that we can notice when it gets flushed, flushed_some can
  1628. * get called, and we can start putting more data onto the buffer then.
  1629. */
  1630. log_debug(LD_GENERAL, "Primed a buffer.");
  1631. channel_flush_from_first_active_circuit(chan, 1);
  1632. }
  1633. }
  1634. /*DOCDOC; for debugging 12184. This runs slowly. */
  1635. int64_t
  1636. circuitmux_count_queued_destroy_cells(const channel_t *chan,
  1637. const circuitmux_t *cmux)
  1638. {
  1639. int64_t n_destroy_cells = cmux->destroy_ctr;
  1640. int64_t destroy_queue_size = cmux->destroy_cell_queue.n;
  1641. int64_t manual_total = 0;
  1642. int64_t manual_total_in_map = 0;
  1643. destroy_cell_t *cell;
  1644. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  1645. circid_t id;
  1646. ++manual_total;
  1647. id = cell->circid;
  1648. if (circuit_id_in_use_on_channel(id, (channel_t*)chan))
  1649. ++manual_total_in_map;
  1650. }
  1651. if (n_destroy_cells != destroy_queue_size ||
  1652. n_destroy_cells != manual_total ||
  1653. n_destroy_cells != manual_total_in_map) {
  1654. log_warn(LD_BUG, " Discrepancy in counts for queued destroy cells on "
  1655. "circuitmux. n="I64_FORMAT". queue_size="I64_FORMAT". "
  1656. "manual_total="I64_FORMAT". manual_total_in_map="I64_FORMAT".",
  1657. I64_PRINTF_ARG(n_destroy_cells),
  1658. I64_PRINTF_ARG(destroy_queue_size),
  1659. I64_PRINTF_ARG(manual_total),
  1660. I64_PRINTF_ARG(manual_total_in_map));
  1661. }
  1662. return n_destroy_cells;
  1663. }