circuitmux.c 58 KB

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