circuitmux.c 58 KB

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