circuitmux.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /* * Copyright (c) 2012-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file circuitmux.c
  5. * \brief Circuit mux/cell selection abstraction
  6. *
  7. * A circuitmux is responsible for <b>MU</b>ltiple<b>X</b>ing all of the
  8. * circuits that are writing on a single channel. It keeps track of which of
  9. * these circuits has something to write (aka, "active" circuits), and which
  10. * one should write next. A circuitmux corresponds 1:1 with a channel.
  11. *
  12. * There can be different implementations of the circuitmux's rules (which
  13. * decide which circuit is next to write).
  14. *
  15. * A circuitmux exposes three distinct
  16. * interfaces to other components:
  17. *
  18. * To channels, which each have a circuitmux_t, the supported operations
  19. * (invoked from relay.c) are:
  20. *
  21. * circuitmux_get_first_active_circuit():
  22. *
  23. * Pick one of the circuitmux's active circuits to send cells from.
  24. *
  25. * circuitmux_notify_xmit_cells():
  26. *
  27. * Notify the circuitmux that cells have been sent on a circuit.
  28. *
  29. * To circuits, the exposed operations are:
  30. *
  31. * circuitmux_attach_circuit():
  32. *
  33. * Attach a circuit to the circuitmux; this will allocate any policy-
  34. * specific data wanted for this circuit and add it to the active
  35. * circuits list if it has queued cells.
  36. *
  37. * circuitmux_detach_circuit():
  38. *
  39. * Detach a circuit from the circuitmux, freeing associated structures.
  40. *
  41. * circuitmux_clear_num_cells():
  42. *
  43. * Clear the circuitmux's cell counter for this circuit.
  44. *
  45. * circuitmux_set_num_cells():
  46. *
  47. * Set the circuitmux's cell counter for this circuit. One of
  48. * circuitmuc_clear_num_cells() or circuitmux_set_num_cells() MUST be
  49. * called when the number of cells queued on a circuit changes.
  50. *
  51. * See circuitmux.h for the circuitmux_policy_t data structure, which contains
  52. * a table of function pointers implementing a circuit selection policy, and
  53. * circuitmux_ewma.c for an example of a circuitmux policy. Circuitmux
  54. * policies can be manipulated with:
  55. *
  56. * circuitmux_get_policy():
  57. *
  58. * Return the current policy for a circuitmux_t, if any.
  59. *
  60. * circuitmux_clear_policy():
  61. *
  62. * Remove a policy installed on a circuitmux_t, freeing all associated
  63. * data. The circuitmux will revert to the built-in round-robin behavior.
  64. *
  65. * circuitmux_set_policy():
  66. *
  67. * Install a policy on a circuitmux_t; the appropriate callbacks will be
  68. * made to attach all existing circuits to the new policy.
  69. **/
  70. #include "core/or/or.h"
  71. #include "core/or/channel.h"
  72. #include "core/or/circuitlist.h"
  73. #include "core/or/circuitmux.h"
  74. #include "core/or/relay.h"
  75. #include "core/or/cell_queue_st.h"
  76. #include "core/or/destroy_cell_queue_st.h"
  77. #include "core/or/or_circuit_st.h"
  78. /*
  79. * Private typedefs for circuitmux.c
  80. */
  81. /*
  82. * Map of muxinfos for circuitmux_t to use; struct is defined below (name
  83. * of struct must match HT_HEAD line).
  84. */
  85. typedef struct chanid_circid_muxinfo_map chanid_circid_muxinfo_map_t;
  86. /*
  87. * Hash table entry (yeah, calling it chanid_circid_muxinfo_s seems to
  88. * break the hash table code).
  89. */
  90. typedef struct chanid_circid_muxinfo_t chanid_circid_muxinfo_t;
  91. /*
  92. * Anything the mux wants to store per-circuit in the map; right now just
  93. * a count of queued cells.
  94. */
  95. typedef struct circuit_muxinfo_s circuit_muxinfo_t;
  96. /*
  97. * Structures for circuitmux.c
  98. */
  99. struct circuitmux_s {
  100. /* Keep count of attached, active circuits */
  101. unsigned int n_circuits, n_active_circuits;
  102. /* Total number of queued cells on all circuits */
  103. unsigned int n_cells;
  104. /*
  105. * Map from (channel ID, circuit ID) pairs to circuit_muxinfo_t
  106. */
  107. chanid_circid_muxinfo_map_t *chanid_circid_map;
  108. /** List of queued destroy cells */
  109. destroy_cell_queue_t destroy_cell_queue;
  110. /** Boolean: True iff the last cell to circuitmux_get_first_active_circuit
  111. * returned the destroy queue. Used to force alternation between
  112. * destroy/non-destroy cells.
  113. *
  114. * XXXX There is no reason to think that alternating is a particularly good
  115. * approach -- it's just designed to prevent destroys from starving other
  116. * cells completely.
  117. */
  118. unsigned int last_cell_was_destroy : 1;
  119. /** Destroy counter: increment this when a destroy gets queued, decrement
  120. * when we unqueue it, so we can test to make sure they don't starve.
  121. */
  122. int64_t destroy_ctr;
  123. /*
  124. * Circuitmux policy; if this is non-NULL, it can override the built-
  125. * in round-robin active circuits behavior. This is how EWMA works in
  126. * the new circuitmux_t world.
  127. */
  128. const circuitmux_policy_t *policy;
  129. /* Policy-specific data */
  130. circuitmux_policy_data_t *policy_data;
  131. };
  132. /*
  133. * This struct holds whatever we want to store per attached circuit on a
  134. * circuitmux_t; right now, just the count of queued cells and the direction.
  135. */
  136. struct circuit_muxinfo_s {
  137. /* Count of cells on this circuit at last update */
  138. unsigned int cell_count;
  139. /* Direction of flow */
  140. cell_direction_t direction;
  141. /* Policy-specific data */
  142. circuitmux_policy_circ_data_t *policy_data;
  143. /* Mark bit for consistency checker */
  144. unsigned int mark:1;
  145. };
  146. /*
  147. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  148. * circuit.
  149. */
  150. struct chanid_circid_muxinfo_t {
  151. HT_ENTRY(chanid_circid_muxinfo_t) node;
  152. uint64_t chan_id;
  153. circid_t circ_id;
  154. circuit_muxinfo_t muxinfo;
  155. };
  156. /*
  157. * Static function declarations
  158. */
  159. static inline int
  160. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  161. chanid_circid_muxinfo_t *b);
  162. static inline unsigned int
  163. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  164. static chanid_circid_muxinfo_t *
  165. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
  166. static void
  167. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ);
  168. static void
  169. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ);
  170. /* Static global variables */
  171. /** Count the destroy balance to debug destroy queue logic */
  172. static int64_t global_destroy_ctr = 0;
  173. /* Function definitions */
  174. /**
  175. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  176. * ID and circuit ID for a and b, and return less than, equal to, or greater
  177. * than zero appropriately.
  178. */
  179. static inline int
  180. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  181. chanid_circid_muxinfo_t *b)
  182. {
  183. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  184. }
  185. /**
  186. * Helper: return a hash based on circuit ID and channel ID in a.
  187. */
  188. static inline unsigned int
  189. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  190. {
  191. return (((unsigned int)(a->circ_id) << 8) ^
  192. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  193. ((unsigned int)(a->chan_id & 0xffffffff)));
  194. }
  195. /* Declare the struct chanid_circid_muxinfo_map type */
  196. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  197. /* Emit a bunch of hash table stuff */
  198. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  199. chanid_circid_entry_hash, chanid_circid_entries_eq)
  200. HT_GENERATE2(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  201. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  202. tor_reallocarray_, tor_free_)
  203. /*
  204. * Circuitmux alloc/free functions
  205. */
  206. /**
  207. * Allocate a new circuitmux_t
  208. */
  209. circuitmux_t *
  210. circuitmux_alloc(void)
  211. {
  212. circuitmux_t *rv = NULL;
  213. rv = tor_malloc_zero(sizeof(*rv));
  214. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  215. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  216. destroy_cell_queue_init(&rv->destroy_cell_queue);
  217. return rv;
  218. }
  219. /**
  220. * Detach all circuits from a circuitmux (use before circuitmux_free())
  221. *
  222. * If <b>detached_out</b> is non-NULL, add every detached circuit_t to
  223. * detached_out.
  224. */
  225. void
  226. circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out)
  227. {
  228. chanid_circid_muxinfo_t **i = NULL, *to_remove;
  229. channel_t *chan = NULL;
  230. circuit_t *circ = NULL;
  231. tor_assert(cmux);
  232. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  233. while (i) {
  234. to_remove = *i;
  235. if (! to_remove) {
  236. log_warn(LD_BUG, "Somehow, an HT iterator gave us a NULL pointer.");
  237. break;
  238. } else {
  239. /* Find a channel and circuit */
  240. chan = channel_find_by_global_id(to_remove->chan_id);
  241. if (chan) {
  242. circ =
  243. circuit_get_by_circid_channel_even_if_marked(to_remove->circ_id,
  244. chan);
  245. if (circ) {
  246. /* Clear the circuit's mux for this direction */
  247. if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
  248. /*
  249. * Update active_circuits et al.; this does policy notifies, so
  250. * comes before freeing policy data
  251. */
  252. if (to_remove->muxinfo.cell_count > 0) {
  253. circuitmux_make_circuit_inactive(cmux, circ);
  254. }
  255. /* Clear n_mux */
  256. circ->n_mux = NULL;
  257. if (detached_out)
  258. smartlist_add(detached_out, circ);
  259. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  260. /*
  261. * Update active_circuits et al.; this does policy notifies, so
  262. * comes before freeing policy data
  263. */
  264. if (to_remove->muxinfo.cell_count > 0) {
  265. circuitmux_make_circuit_inactive(cmux, circ);
  266. }
  267. /*
  268. * It has a sensible p_chan and direction == CELL_DIRECTION_IN,
  269. * so clear p_mux.
  270. */
  271. TO_OR_CIRCUIT(circ)->p_mux = NULL;
  272. if (detached_out)
  273. smartlist_add(detached_out, circ);
  274. } else {
  275. /* Complain and move on */
  276. log_warn(LD_CIRC,
  277. "Circuit %u/channel %"PRIu64 " had direction == "
  278. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  279. (unsigned)to_remove->circ_id,
  280. (to_remove->chan_id));
  281. }
  282. /* Free policy-specific data if we have it */
  283. if (to_remove->muxinfo.policy_data) {
  284. /*
  285. * If we have policy data, assert that we have the means to
  286. * free it
  287. */
  288. tor_assert(cmux->policy);
  289. tor_assert(cmux->policy->free_circ_data);
  290. /* Call free_circ_data() */
  291. cmux->policy->free_circ_data(cmux,
  292. cmux->policy_data,
  293. circ,
  294. to_remove->muxinfo.policy_data);
  295. to_remove->muxinfo.policy_data = NULL;
  296. }
  297. } else {
  298. /* Complain and move on */
  299. log_warn(LD_CIRC,
  300. "Couldn't find circuit %u (for channel %"PRIu64 ")",
  301. (unsigned)to_remove->circ_id,
  302. (to_remove->chan_id));
  303. }
  304. } else {
  305. /* Complain and move on */
  306. log_warn(LD_CIRC,
  307. "Couldn't find channel %"PRIu64 " (for circuit id %u)",
  308. (to_remove->chan_id),
  309. (unsigned)to_remove->circ_id);
  310. }
  311. /* Assert that we don't have un-freed policy data for this circuit */
  312. tor_assert(to_remove->muxinfo.policy_data == NULL);
  313. }
  314. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  315. /* Free it */
  316. tor_free(to_remove);
  317. }
  318. cmux->n_circuits = 0;
  319. cmux->n_active_circuits = 0;
  320. cmux->n_cells = 0;
  321. }
  322. /** Reclaim all circuit IDs currently marked as unusable on <b>chan</b> because
  323. * of pending destroy cells in <b>cmux</b>.
  324. *
  325. * This function must be called AFTER circuits are unlinked from the (channel,
  326. * circuid-id) map with circuit_unlink_all_from_channel(), but before calling
  327. * circuitmux_free().
  328. */
  329. void
  330. circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
  331. {
  332. destroy_cell_t *cell;
  333. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  334. channel_mark_circid_usable(chan, cell->circid);
  335. }
  336. }
  337. /**
  338. * Free a circuitmux_t; the circuits must be detached first with
  339. * circuitmux_detach_all_circuits().
  340. */
  341. void
  342. circuitmux_free_(circuitmux_t *cmux)
  343. {
  344. if (!cmux) return;
  345. tor_assert(cmux->n_circuits == 0);
  346. tor_assert(cmux->n_active_circuits == 0);
  347. /*
  348. * Free policy-specific data if we have any; we don't
  349. * need to do circuitmux_set_policy(cmux, NULL) to cover
  350. * the circuits because they would have been handled in
  351. * circuitmux_detach_all_circuits() before this was
  352. * called.
  353. */
  354. if (cmux->policy && cmux->policy->free_cmux_data) {
  355. if (cmux->policy_data) {
  356. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  357. cmux->policy_data = NULL;
  358. }
  359. } else tor_assert(cmux->policy_data == NULL);
  360. if (cmux->chanid_circid_map) {
  361. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  362. tor_free(cmux->chanid_circid_map);
  363. }
  364. /*
  365. * We're throwing away some destroys; log the counter and
  366. * adjust the global counter by the queue size.
  367. */
  368. if (cmux->destroy_cell_queue.n > 0) {
  369. cmux->destroy_ctr -= cmux->destroy_cell_queue.n;
  370. global_destroy_ctr -= cmux->destroy_cell_queue.n;
  371. log_debug(LD_CIRC,
  372. "Freeing cmux at %p with %u queued destroys; the last cmux "
  373. "destroy balance was %"PRId64", global is %"PRId64,
  374. cmux, cmux->destroy_cell_queue.n,
  375. (cmux->destroy_ctr),
  376. (global_destroy_ctr));
  377. } else {
  378. log_debug(LD_CIRC,
  379. "Freeing cmux at %p with no queued destroys, the cmux destroy "
  380. "balance was %"PRId64", global is %"PRId64,
  381. cmux,
  382. (cmux->destroy_ctr),
  383. (global_destroy_ctr));
  384. }
  385. destroy_cell_queue_clear(&cmux->destroy_cell_queue);
  386. tor_free(cmux);
  387. }
  388. /*
  389. * Circuitmux policy control functions
  390. */
  391. /**
  392. * Remove any policy installed on cmux; all policy data will be freed and
  393. * cmux behavior will revert to the built-in round-robin active_circuits
  394. * mechanism.
  395. */
  396. void
  397. circuitmux_clear_policy(circuitmux_t *cmux)
  398. {
  399. tor_assert(cmux);
  400. /* Internally, this is just setting policy to NULL */
  401. circuitmux_set_policy(cmux, NULL);
  402. }
  403. /**
  404. * Return the policy currently installed on a circuitmux_t
  405. */
  406. MOCK_IMPL(const circuitmux_policy_t *,
  407. circuitmux_get_policy, (circuitmux_t *cmux))
  408. {
  409. tor_assert(cmux);
  410. return cmux->policy;
  411. }
  412. /**
  413. * Set policy; allocate for new policy, detach all circuits from old policy
  414. * if any, attach them to new policy, and free old policy data.
  415. */
  416. void
  417. circuitmux_set_policy(circuitmux_t *cmux,
  418. const circuitmux_policy_t *pol)
  419. {
  420. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  421. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  422. chanid_circid_muxinfo_t **i = NULL;
  423. channel_t *chan = NULL;
  424. uint64_t last_chan_id_searched = 0;
  425. circuit_t *circ = NULL;
  426. tor_assert(cmux);
  427. /* Set up variables */
  428. old_pol = cmux->policy;
  429. old_pol_data = cmux->policy_data;
  430. new_pol = pol;
  431. /* Check if this is the trivial case */
  432. if (old_pol == new_pol) return;
  433. /* Allocate data for new policy, if any */
  434. if (new_pol && new_pol->alloc_cmux_data) {
  435. /*
  436. * If alloc_cmux_data is not null, then we expect to get some policy
  437. * data. Assert that we also have free_cmux_data so we can free it
  438. * when the time comes, and allocate it.
  439. */
  440. tor_assert(new_pol->free_cmux_data);
  441. new_pol_data = new_pol->alloc_cmux_data(cmux);
  442. tor_assert(new_pol_data);
  443. }
  444. /* Install new policy and new policy data on cmux */
  445. cmux->policy = new_pol;
  446. cmux->policy_data = new_pol_data;
  447. /* Iterate over all circuits, attaching/detaching each one */
  448. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  449. while (i) {
  450. /* Assert that this entry isn't NULL */
  451. tor_assert(*i);
  452. /*
  453. * Get the channel; since normal case is all circuits on the mux share a
  454. * channel, we cache last_chan_id_searched
  455. */
  456. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  457. chan = channel_find_by_global_id((*i)->chan_id);
  458. last_chan_id_searched = (*i)->chan_id;
  459. }
  460. tor_assert(chan);
  461. /* Get the circuit */
  462. circ = circuit_get_by_circid_channel_even_if_marked((*i)->circ_id, chan);
  463. tor_assert(circ);
  464. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  465. if (old_pol && old_pol->notify_circ_inactive &&
  466. (*i)->muxinfo.cell_count > 0) {
  467. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  468. (*i)->muxinfo.policy_data);
  469. }
  470. /* Need to free old policy data? */
  471. if ((*i)->muxinfo.policy_data) {
  472. /* Assert that we have the means to free it if we have policy data */
  473. tor_assert(old_pol);
  474. tor_assert(old_pol->free_circ_data);
  475. /* Free it */
  476. old_pol->free_circ_data(cmux, old_pol_data, circ,
  477. (*i)->muxinfo.policy_data);
  478. (*i)->muxinfo.policy_data = NULL;
  479. }
  480. /* Need to allocate new policy data? */
  481. if (new_pol && new_pol->alloc_circ_data) {
  482. /*
  483. * If alloc_circ_data is not null, we expect to get some per-circuit
  484. * policy data. Assert that we also have free_circ_data so we can
  485. * free it when the time comes, and allocate it.
  486. */
  487. tor_assert(new_pol->free_circ_data);
  488. (*i)->muxinfo.policy_data =
  489. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  490. (*i)->muxinfo.direction,
  491. (*i)->muxinfo.cell_count);
  492. }
  493. /* Need to make active on new policy? */
  494. if (new_pol && new_pol->notify_circ_active &&
  495. (*i)->muxinfo.cell_count > 0) {
  496. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  497. (*i)->muxinfo.policy_data);
  498. }
  499. /* Advance to next circuit map entry */
  500. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  501. }
  502. /* Free data for old policy, if any */
  503. if (old_pol_data) {
  504. /*
  505. * If we had old policy data, we should have an old policy and a free
  506. * function for it.
  507. */
  508. tor_assert(old_pol);
  509. tor_assert(old_pol->free_cmux_data);
  510. old_pol->free_cmux_data(cmux, old_pol_data);
  511. old_pol_data = NULL;
  512. }
  513. }
  514. /*
  515. * Circuitmux/circuit attachment status inquiry functions
  516. */
  517. /**
  518. * Query the direction of an attached circuit
  519. */
  520. cell_direction_t
  521. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  522. {
  523. chanid_circid_muxinfo_t *hashent = NULL;
  524. /* Try to find a map entry */
  525. hashent = circuitmux_find_map_entry(cmux, circ);
  526. /*
  527. * This function should only be called on attached circuits; assert that
  528. * we had a map entry.
  529. */
  530. tor_assert(hashent);
  531. /* Return the direction from the map entry */
  532. return hashent->muxinfo.direction;
  533. }
  534. /**
  535. * Find an entry in the cmux's map for this circuit or return NULL if there
  536. * is none.
  537. */
  538. static chanid_circid_muxinfo_t *
  539. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  540. {
  541. chanid_circid_muxinfo_t search, *hashent = NULL;
  542. /* Sanity-check parameters */
  543. tor_assert(cmux);
  544. tor_assert(cmux->chanid_circid_map);
  545. tor_assert(circ);
  546. /* Check if we have n_chan */
  547. if (circ->n_chan) {
  548. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  549. search.chan_id = circ->n_chan->global_identifier;
  550. search.circ_id = circ->n_circ_id;
  551. /* Query */
  552. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  553. &search);
  554. }
  555. /* Found something? */
  556. if (hashent) {
  557. /*
  558. * Assert that the direction makes sense for a hashent we found by
  559. * n_chan/n_circ_id before we return it.
  560. */
  561. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  562. } else {
  563. /* Not there, have we got a p_chan/p_circ_id to try? */
  564. if (circ->magic == OR_CIRCUIT_MAGIC) {
  565. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  566. /* Check for p_chan */
  567. if (TO_OR_CIRCUIT(circ)->p_chan) {
  568. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  569. /* Okay, search for that */
  570. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  571. &search);
  572. /* Find anything? */
  573. if (hashent) {
  574. /* Assert that the direction makes sense before we return it */
  575. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  576. }
  577. }
  578. }
  579. }
  580. /* Okay, hashent is it if it was there */
  581. return hashent;
  582. }
  583. /**
  584. * Query whether a circuit is attached to a circuitmux
  585. */
  586. int
  587. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  588. {
  589. chanid_circid_muxinfo_t *hashent = NULL;
  590. /* Look if it's in the circuit map */
  591. hashent = circuitmux_find_map_entry(cmux, circ);
  592. return (hashent != NULL);
  593. }
  594. /**
  595. * Query whether a circuit is active on a circuitmux
  596. */
  597. int
  598. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  599. {
  600. chanid_circid_muxinfo_t *hashent = NULL;
  601. int is_active = 0;
  602. tor_assert(cmux);
  603. tor_assert(circ);
  604. /* Look if it's in the circuit map */
  605. hashent = circuitmux_find_map_entry(cmux, circ);
  606. if (hashent) {
  607. /* Check the number of cells on this circuit */
  608. is_active = (hashent->muxinfo.cell_count > 0);
  609. }
  610. /* else not attached, so not active */
  611. return is_active;
  612. }
  613. /**
  614. * Query number of available cells for a circuit on a circuitmux
  615. */
  616. unsigned int
  617. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  618. {
  619. chanid_circid_muxinfo_t *hashent = NULL;
  620. unsigned int n_cells = 0;
  621. tor_assert(cmux);
  622. tor_assert(circ);
  623. /* Look if it's in the circuit map */
  624. hashent = circuitmux_find_map_entry(cmux, circ);
  625. if (hashent) {
  626. /* Just get the cell count for this circuit */
  627. n_cells = hashent->muxinfo.cell_count;
  628. }
  629. /* else not attached, so 0 cells */
  630. return n_cells;
  631. }
  632. /**
  633. * Query total number of available cells on a circuitmux
  634. */
  635. MOCK_IMPL(unsigned int,
  636. circuitmux_num_cells, (circuitmux_t *cmux))
  637. {
  638. tor_assert(cmux);
  639. return cmux->n_cells + cmux->destroy_cell_queue.n;
  640. }
  641. /**
  642. * Query total number of circuits active on a circuitmux
  643. */
  644. unsigned int
  645. circuitmux_num_active_circuits(circuitmux_t *cmux)
  646. {
  647. tor_assert(cmux);
  648. return cmux->n_active_circuits;
  649. }
  650. /**
  651. * Query total number of circuits attached to a circuitmux
  652. */
  653. unsigned int
  654. circuitmux_num_circuits(circuitmux_t *cmux)
  655. {
  656. tor_assert(cmux);
  657. return cmux->n_circuits;
  658. }
  659. /*
  660. * Functions for circuit code to call to update circuit status
  661. */
  662. /**
  663. * Attach a circuit to a circuitmux, for the specified direction.
  664. */
  665. MOCK_IMPL(void,
  666. circuitmux_attach_circuit,(circuitmux_t *cmux, circuit_t *circ,
  667. cell_direction_t direction))
  668. {
  669. channel_t *chan = NULL;
  670. uint64_t channel_id;
  671. circid_t circ_id;
  672. chanid_circid_muxinfo_t search, *hashent = NULL;
  673. unsigned int cell_count;
  674. tor_assert(cmux);
  675. tor_assert(circ);
  676. tor_assert(direction == CELL_DIRECTION_IN ||
  677. direction == CELL_DIRECTION_OUT);
  678. /*
  679. * Figure out which channel we're using, and get the circuit's current
  680. * cell count and circuit ID; assert that the circuit is not already
  681. * attached to another mux.
  682. */
  683. if (direction == CELL_DIRECTION_OUT) {
  684. /* It's n_chan */
  685. chan = circ->n_chan;
  686. cell_count = circ->n_chan_cells.n;
  687. circ_id = circ->n_circ_id;
  688. } else {
  689. /* We want p_chan */
  690. chan = TO_OR_CIRCUIT(circ)->p_chan;
  691. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  692. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  693. }
  694. /* Assert that we did get a channel */
  695. tor_assert(chan);
  696. /* Assert that the circuit ID is sensible */
  697. tor_assert(circ_id != 0);
  698. /* Get the channel ID */
  699. channel_id = chan->global_identifier;
  700. /* See if we already have this one */
  701. search.chan_id = channel_id;
  702. search.circ_id = circ_id;
  703. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  704. &search);
  705. if (hashent) {
  706. /*
  707. * This circuit was already attached to this cmux; make sure the
  708. * directions match and update the cell count and active circuit count.
  709. */
  710. log_info(LD_CIRC,
  711. "Circuit %u on channel %"PRIu64 " was already attached to "
  712. "cmux %p (trying to attach to %p)",
  713. (unsigned)circ_id, (channel_id),
  714. ((direction == CELL_DIRECTION_OUT) ?
  715. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  716. cmux);
  717. /*
  718. * The mux pointer on this circuit and the direction in result should
  719. * match; otherwise assert.
  720. */
  721. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  722. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  723. tor_assert(hashent->muxinfo.direction == direction);
  724. /*
  725. * Looks okay; just update the cell count and active circuits if we must
  726. */
  727. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  728. --(cmux->n_active_circuits);
  729. circuitmux_make_circuit_inactive(cmux, circ);
  730. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  731. ++(cmux->n_active_circuits);
  732. circuitmux_make_circuit_active(cmux, circ);
  733. }
  734. cmux->n_cells -= hashent->muxinfo.cell_count;
  735. cmux->n_cells += cell_count;
  736. hashent->muxinfo.cell_count = cell_count;
  737. } else {
  738. /*
  739. * New circuit; add an entry and update the circuit/active circuit
  740. * counts.
  741. */
  742. log_debug(LD_CIRC,
  743. "Attaching circuit %u on channel %"PRIu64 " to cmux %p",
  744. (unsigned)circ_id, (channel_id), cmux);
  745. /*
  746. * Assert that the circuit doesn't already have a mux for this
  747. * direction.
  748. */
  749. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  750. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  751. /* Insert it in the map */
  752. hashent = tor_malloc_zero(sizeof(*hashent));
  753. hashent->chan_id = channel_id;
  754. hashent->circ_id = circ_id;
  755. hashent->muxinfo.cell_count = cell_count;
  756. hashent->muxinfo.direction = direction;
  757. /* Allocate policy specific circuit data if we need it */
  758. if (cmux->policy->alloc_circ_data) {
  759. /* Assert that we have the means to free policy-specific data */
  760. tor_assert(cmux->policy->free_circ_data);
  761. /* Allocate it */
  762. hashent->muxinfo.policy_data =
  763. cmux->policy->alloc_circ_data(cmux,
  764. cmux->policy_data,
  765. circ,
  766. direction,
  767. cell_count);
  768. /* If we wanted policy data, it's an error not to get any */
  769. tor_assert(hashent->muxinfo.policy_data);
  770. }
  771. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  772. hashent);
  773. /* Set the circuit's mux for this direction */
  774. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  775. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  776. /* Update counters */
  777. ++(cmux->n_circuits);
  778. if (cell_count > 0) {
  779. ++(cmux->n_active_circuits);
  780. circuitmux_make_circuit_active(cmux, circ);
  781. }
  782. cmux->n_cells += cell_count;
  783. }
  784. }
  785. /**
  786. * Detach a circuit from a circuitmux and update all counters as needed;
  787. * no-op if not attached.
  788. */
  789. MOCK_IMPL(void,
  790. circuitmux_detach_circuit,(circuitmux_t *cmux, circuit_t *circ))
  791. {
  792. chanid_circid_muxinfo_t search, *hashent = NULL;
  793. /*
  794. * Use this to keep track of whether we found it for n_chan or
  795. * p_chan for consistency checking.
  796. *
  797. * The 0 initializer is not a valid cell_direction_t value.
  798. * We assert that it has been replaced with a valid value before it is used.
  799. */
  800. cell_direction_t last_searched_direction = 0;
  801. tor_assert(cmux);
  802. tor_assert(cmux->chanid_circid_map);
  803. tor_assert(circ);
  804. /* See if we have it for n_chan/n_circ_id */
  805. if (circ->n_chan) {
  806. search.chan_id = circ->n_chan->global_identifier;
  807. search.circ_id = circ->n_circ_id;
  808. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  809. &search);
  810. last_searched_direction = CELL_DIRECTION_OUT;
  811. }
  812. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  813. if (!hashent) {
  814. if (circ->magic == OR_CIRCUIT_MAGIC) {
  815. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  816. if (TO_OR_CIRCUIT(circ)->p_chan) {
  817. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  818. hashent = HT_FIND(chanid_circid_muxinfo_map,
  819. cmux->chanid_circid_map,
  820. &search);
  821. last_searched_direction = CELL_DIRECTION_IN;
  822. }
  823. }
  824. }
  825. tor_assert(last_searched_direction == CELL_DIRECTION_OUT
  826. || last_searched_direction == CELL_DIRECTION_IN);
  827. /*
  828. * If hashent isn't NULL, we have a circuit to detach; don't remove it from
  829. * the map until later of circuitmux_make_circuit_inactive() breaks.
  830. */
  831. if (hashent) {
  832. /* Update counters */
  833. --(cmux->n_circuits);
  834. if (hashent->muxinfo.cell_count > 0) {
  835. --(cmux->n_active_circuits);
  836. /* This does policy notifies, so comes before freeing policy data */
  837. circuitmux_make_circuit_inactive(cmux, circ);
  838. }
  839. cmux->n_cells -= hashent->muxinfo.cell_count;
  840. /* Free policy-specific data if we have it */
  841. if (hashent->muxinfo.policy_data) {
  842. /* If we have policy data, assert that we have the means to free it */
  843. tor_assert(cmux->policy);
  844. tor_assert(cmux->policy->free_circ_data);
  845. /* Call free_circ_data() */
  846. cmux->policy->free_circ_data(cmux,
  847. cmux->policy_data,
  848. circ,
  849. hashent->muxinfo.policy_data);
  850. hashent->muxinfo.policy_data = NULL;
  851. }
  852. /* Consistency check: the direction must match the direction searched */
  853. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  854. /* Clear the circuit's mux for this direction */
  855. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  856. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  857. /* Now remove it from the map */
  858. HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map, hashent);
  859. /* Free the hash entry */
  860. tor_free(hashent);
  861. }
  862. }
  863. /**
  864. * Make a circuit active; update active list and policy-specific info, but
  865. * we don't mess with the counters or hash table here.
  866. */
  867. static void
  868. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  869. {
  870. tor_assert(cmux);
  871. tor_assert(cmux->policy);
  872. tor_assert(circ);
  873. /* Policy-specific notification */
  874. if (cmux->policy->notify_circ_active) {
  875. /* Okay, we need to check the circuit for policy data now */
  876. chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
  877. /* We should have found something */
  878. tor_assert(hashent);
  879. /* Notify */
  880. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  881. circ, hashent->muxinfo.policy_data);
  882. }
  883. }
  884. /**
  885. * Make a circuit inactive; update active list and policy-specific info, but
  886. * we don't mess with the counters or hash table here.
  887. */
  888. static void
  889. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ)
  890. {
  891. tor_assert(cmux);
  892. tor_assert(cmux->policy);
  893. tor_assert(circ);
  894. /* Policy-specific notification */
  895. if (cmux->policy->notify_circ_inactive) {
  896. /* Okay, we need to check the circuit for policy data now */
  897. chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
  898. /* We should have found something */
  899. tor_assert(hashent);
  900. /* Notify */
  901. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  902. circ, hashent->muxinfo.policy_data);
  903. }
  904. }
  905. /**
  906. * Clear the cell counter for a circuit on a circuitmux
  907. */
  908. void
  909. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  910. {
  911. /* This is the same as setting the cell count to zero */
  912. circuitmux_set_num_cells(cmux, circ, 0);
  913. }
  914. /**
  915. * Set the cell counter for a circuit on a circuitmux
  916. */
  917. void
  918. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  919. unsigned int n_cells)
  920. {
  921. chanid_circid_muxinfo_t *hashent = NULL;
  922. tor_assert(cmux);
  923. tor_assert(circ);
  924. /* Search for this circuit's entry */
  925. hashent = circuitmux_find_map_entry(cmux, circ);
  926. /* Assert that we found one */
  927. tor_assert(hashent);
  928. /* Update cmux cell counter */
  929. cmux->n_cells -= hashent->muxinfo.cell_count;
  930. cmux->n_cells += n_cells;
  931. /* Do we need to notify a cmux policy? */
  932. if (cmux->policy->notify_set_n_cells) {
  933. /* Call notify_set_n_cells */
  934. cmux->policy->notify_set_n_cells(cmux,
  935. cmux->policy_data,
  936. circ,
  937. hashent->muxinfo.policy_data,
  938. n_cells);
  939. }
  940. /*
  941. * Update cmux active circuit counter: is the old cell count > 0 and the
  942. * new cell count == 0 ?
  943. */
  944. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  945. --(cmux->n_active_circuits);
  946. hashent->muxinfo.cell_count = n_cells;
  947. circuitmux_make_circuit_inactive(cmux, circ);
  948. /* Is the old cell count == 0 and the new cell count > 0 ? */
  949. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  950. ++(cmux->n_active_circuits);
  951. hashent->muxinfo.cell_count = n_cells;
  952. circuitmux_make_circuit_active(cmux, circ);
  953. } else {
  954. hashent->muxinfo.cell_count = n_cells;
  955. }
  956. }
  957. /*
  958. * Functions for channel code to call to get a circuit to transmit from or
  959. * notify that cells have been transmitted.
  960. */
  961. /**
  962. * Pick a circuit to send from, using the active circuits list or a
  963. * circuitmux policy if one is available. This is called from channel.c.
  964. *
  965. * If we would rather send a destroy cell, return NULL and set
  966. * *<b>destroy_queue_out</b> to the destroy queue.
  967. *
  968. * If we have nothing to send, set *<b>destroy_queue_out</b> to NULL and
  969. * return NULL.
  970. */
  971. circuit_t *
  972. circuitmux_get_first_active_circuit(circuitmux_t *cmux,
  973. destroy_cell_queue_t **destroy_queue_out)
  974. {
  975. circuit_t *circ = NULL;
  976. tor_assert(cmux);
  977. tor_assert(cmux->policy);
  978. /* This callback is mandatory. */
  979. tor_assert(cmux->policy->pick_active_circuit);
  980. tor_assert(destroy_queue_out);
  981. *destroy_queue_out = NULL;
  982. if (cmux->destroy_cell_queue.n &&
  983. (!cmux->last_cell_was_destroy || cmux->n_active_circuits == 0)) {
  984. /* We have destroy cells to send, and either we just sent a relay cell,
  985. * or we have no relay cells to send. */
  986. /* XXXX We should let the cmux policy have some say in this eventually. */
  987. /* XXXX Alternating is not a terribly brilliant approach here. */
  988. *destroy_queue_out = &cmux->destroy_cell_queue;
  989. cmux->last_cell_was_destroy = 1;
  990. } else if (cmux->n_active_circuits > 0) {
  991. /* We also must have a cell available for this to be the case */
  992. tor_assert(cmux->n_cells > 0);
  993. /* Do we have a policy-provided circuit selector? */
  994. circ = cmux->policy->pick_active_circuit(cmux, cmux->policy_data);
  995. cmux->last_cell_was_destroy = 0;
  996. } else {
  997. tor_assert(cmux->n_cells == 0);
  998. tor_assert(cmux->destroy_cell_queue.n == 0);
  999. }
  1000. return circ;
  1001. }
  1002. /**
  1003. * Notify the circuitmux that cells have been sent on a circuit; this
  1004. * is called from channel.c.
  1005. */
  1006. void
  1007. circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  1008. unsigned int n_cells)
  1009. {
  1010. chanid_circid_muxinfo_t *hashent = NULL;
  1011. int becomes_inactive = 0;
  1012. tor_assert(cmux);
  1013. tor_assert(circ);
  1014. if (n_cells == 0) return;
  1015. /*
  1016. * To handle this, we have to:
  1017. *
  1018. * 1.) Adjust the circuit's cell counter in the cmux hash table
  1019. * 2.) Move the circuit to the tail of the active_circuits linked list
  1020. * for this cmux, or make the circuit inactive if the cell count
  1021. * went to zero.
  1022. * 3.) Call cmux->policy->notify_xmit_cells(), if any
  1023. */
  1024. /* Find the hash entry */
  1025. hashent = circuitmux_find_map_entry(cmux, circ);
  1026. /* Assert that we found one */
  1027. tor_assert(hashent);
  1028. /* Adjust the cell counter and assert that we had that many cells to send */
  1029. tor_assert(n_cells <= hashent->muxinfo.cell_count);
  1030. hashent->muxinfo.cell_count -= n_cells;
  1031. /* Do we need to make the circuit inactive? */
  1032. if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
  1033. /* Adjust the mux cell counter */
  1034. cmux->n_cells -= n_cells;
  1035. /*
  1036. * We call notify_xmit_cells() before making the circuit inactive if needed,
  1037. * so the policy can always count on this coming in on an active circuit.
  1038. */
  1039. if (cmux->policy->notify_xmit_cells) {
  1040. cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
  1041. hashent->muxinfo.policy_data,
  1042. n_cells);
  1043. }
  1044. /*
  1045. * Now make the circuit inactive if needed; this will call the policy's
  1046. * notify_circ_inactive() if present.
  1047. */
  1048. if (becomes_inactive) {
  1049. --(cmux->n_active_circuits);
  1050. circuitmux_make_circuit_inactive(cmux, circ);
  1051. }
  1052. }
  1053. /**
  1054. * Notify the circuitmux that a destroy was sent, so we can update
  1055. * the counter.
  1056. */
  1057. void
  1058. circuitmux_notify_xmit_destroy(circuitmux_t *cmux)
  1059. {
  1060. tor_assert(cmux);
  1061. --(cmux->destroy_ctr);
  1062. --(global_destroy_ctr);
  1063. log_debug(LD_CIRC,
  1064. "Cmux at %p sent a destroy, cmux counter is now %"PRId64", "
  1065. "global counter is now %"PRId64,
  1066. cmux,
  1067. (cmux->destroy_ctr),
  1068. (global_destroy_ctr));
  1069. }
  1070. /*DOCDOC */
  1071. void
  1072. circuitmux_append_destroy_cell(channel_t *chan,
  1073. circuitmux_t *cmux,
  1074. circid_t circ_id,
  1075. uint8_t reason)
  1076. {
  1077. destroy_cell_queue_append(&cmux->destroy_cell_queue, circ_id, reason);
  1078. /* Destroy entering the queue, update counters */
  1079. ++(cmux->destroy_ctr);
  1080. ++global_destroy_ctr;
  1081. log_debug(LD_CIRC,
  1082. "Cmux at %p queued a destroy for circ %u, cmux counter is now "
  1083. "%"PRId64", global counter is now %"PRId64,
  1084. cmux, circ_id,
  1085. (cmux->destroy_ctr),
  1086. (global_destroy_ctr));
  1087. /* XXXX Duplicate code from append_cell_to_circuit_queue */
  1088. if (!channel_has_queued_writes(chan)) {
  1089. /* There is no data at all waiting to be sent on the outbuf. Add a
  1090. * cell, so that we can notice when it gets flushed, flushed_some can
  1091. * get called, and we can start putting more data onto the buffer then.
  1092. */
  1093. log_debug(LD_GENERAL, "Primed a buffer.");
  1094. channel_flush_from_first_active_circuit(chan, 1);
  1095. }
  1096. }
  1097. /*DOCDOC; for debugging 12184. This runs slowly. */
  1098. int64_t
  1099. circuitmux_count_queued_destroy_cells(const channel_t *chan,
  1100. const circuitmux_t *cmux)
  1101. {
  1102. int64_t n_destroy_cells = cmux->destroy_ctr;
  1103. int64_t destroy_queue_size = cmux->destroy_cell_queue.n;
  1104. int64_t manual_total = 0;
  1105. int64_t manual_total_in_map = 0;
  1106. destroy_cell_t *cell;
  1107. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  1108. circid_t id;
  1109. ++manual_total;
  1110. id = cell->circid;
  1111. if (circuit_id_in_use_on_channel(id, (channel_t*)chan))
  1112. ++manual_total_in_map;
  1113. }
  1114. if (n_destroy_cells != destroy_queue_size ||
  1115. n_destroy_cells != manual_total ||
  1116. n_destroy_cells != manual_total_in_map) {
  1117. log_warn(LD_BUG, " Discrepancy in counts for queued destroy cells on "
  1118. "circuitmux. n=%"PRId64". queue_size=%"PRId64". "
  1119. "manual_total=%"PRId64". manual_total_in_map=%"PRId64".",
  1120. (n_destroy_cells),
  1121. (destroy_queue_size),
  1122. (manual_total),
  1123. (manual_total_in_map));
  1124. }
  1125. return n_destroy_cells;
  1126. }
  1127. /**
  1128. * Compare cmuxes to see which is more preferred; return < 0 if
  1129. * cmux_1 has higher priority (i.e., cmux_1 < cmux_2 in the scheduler's
  1130. * sort order), > 0 if cmux_2 has higher priority, or 0 if they are
  1131. * equally preferred.
  1132. *
  1133. * If the cmuxes have different cmux policies or the policy does not
  1134. * support the cmp_cmux method, return 0.
  1135. */
  1136. MOCK_IMPL(int,
  1137. circuitmux_compare_muxes, (circuitmux_t *cmux_1, circuitmux_t *cmux_2))
  1138. {
  1139. const circuitmux_policy_t *policy;
  1140. tor_assert(cmux_1);
  1141. tor_assert(cmux_2);
  1142. if (cmux_1 == cmux_2) {
  1143. /* Equivalent because they're the same cmux */
  1144. return 0;
  1145. }
  1146. if (cmux_1->policy && cmux_2->policy) {
  1147. if (cmux_1->policy == cmux_2->policy) {
  1148. policy = cmux_1->policy;
  1149. if (policy->cmp_cmux) {
  1150. /* Okay, we can compare! */
  1151. return policy->cmp_cmux(cmux_1, cmux_1->policy_data,
  1152. cmux_2, cmux_2->policy_data);
  1153. } else {
  1154. /*
  1155. * Equivalent because the policy doesn't know how to compare between
  1156. * muxes.
  1157. */
  1158. return 0;
  1159. }
  1160. } else {
  1161. /* Equivalent because they have different policies */
  1162. return 0;
  1163. }
  1164. } else {
  1165. /* Equivalent because one or both are missing a policy */
  1166. return 0;
  1167. }
  1168. }