circuitmux.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. if (detached_out)
  256. smartlist_add(detached_out, circ);
  257. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  258. /*
  259. * Update active_circuits et al.; this does policy notifies, so
  260. * comes before freeing policy data
  261. */
  262. if (to_remove->muxinfo.cell_count > 0) {
  263. circuitmux_make_circuit_inactive(cmux, circ);
  264. }
  265. if (detached_out)
  266. smartlist_add(detached_out, circ);
  267. } else {
  268. /* Complain and move on */
  269. log_warn(LD_CIRC,
  270. "Circuit %u/channel %"PRIu64 " had direction == "
  271. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  272. (unsigned)to_remove->circ_id,
  273. (to_remove->chan_id));
  274. }
  275. /* Free policy-specific data if we have it */
  276. if (to_remove->muxinfo.policy_data) {
  277. /*
  278. * If we have policy data, assert that we have the means to
  279. * free it
  280. */
  281. tor_assert(cmux->policy);
  282. tor_assert(cmux->policy->free_circ_data);
  283. /* Call free_circ_data() */
  284. cmux->policy->free_circ_data(cmux,
  285. cmux->policy_data,
  286. circ,
  287. to_remove->muxinfo.policy_data);
  288. to_remove->muxinfo.policy_data = NULL;
  289. }
  290. } else {
  291. /* Complain and move on */
  292. log_warn(LD_CIRC,
  293. "Couldn't find circuit %u (for channel %"PRIu64 ")",
  294. (unsigned)to_remove->circ_id,
  295. (to_remove->chan_id));
  296. }
  297. } else {
  298. /* Complain and move on */
  299. log_warn(LD_CIRC,
  300. "Couldn't find channel %"PRIu64 " (for circuit id %u)",
  301. (to_remove->chan_id),
  302. (unsigned)to_remove->circ_id);
  303. }
  304. /* Assert that we don't have un-freed policy data for this circuit */
  305. tor_assert(to_remove->muxinfo.policy_data == NULL);
  306. }
  307. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  308. /* Free it */
  309. tor_free(to_remove);
  310. }
  311. cmux->n_circuits = 0;
  312. cmux->n_active_circuits = 0;
  313. cmux->n_cells = 0;
  314. }
  315. /** Reclaim all circuit IDs currently marked as unusable on <b>chan</b> because
  316. * of pending destroy cells in <b>cmux</b>.
  317. *
  318. * This function must be called AFTER circuits are unlinked from the (channel,
  319. * circuid-id) map with circuit_unlink_all_from_channel(), but before calling
  320. * circuitmux_free().
  321. */
  322. void
  323. circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
  324. {
  325. destroy_cell_t *cell;
  326. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  327. channel_mark_circid_usable(chan, cell->circid);
  328. }
  329. }
  330. /**
  331. * Free a circuitmux_t; the circuits must be detached first with
  332. * circuitmux_detach_all_circuits().
  333. */
  334. void
  335. circuitmux_free_(circuitmux_t *cmux)
  336. {
  337. if (!cmux) return;
  338. tor_assert(cmux->n_circuits == 0);
  339. tor_assert(cmux->n_active_circuits == 0);
  340. /*
  341. * Free policy-specific data if we have any; we don't
  342. * need to do circuitmux_set_policy(cmux, NULL) to cover
  343. * the circuits because they would have been handled in
  344. * circuitmux_detach_all_circuits() before this was
  345. * called.
  346. */
  347. if (cmux->policy && cmux->policy->free_cmux_data) {
  348. if (cmux->policy_data) {
  349. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  350. cmux->policy_data = NULL;
  351. }
  352. } else tor_assert(cmux->policy_data == NULL);
  353. if (cmux->chanid_circid_map) {
  354. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  355. tor_free(cmux->chanid_circid_map);
  356. }
  357. /*
  358. * We're throwing away some destroys; log the counter and
  359. * adjust the global counter by the queue size.
  360. */
  361. if (cmux->destroy_cell_queue.n > 0) {
  362. cmux->destroy_ctr -= cmux->destroy_cell_queue.n;
  363. global_destroy_ctr -= cmux->destroy_cell_queue.n;
  364. log_debug(LD_CIRC,
  365. "Freeing cmux at %p with %u queued destroys; the last cmux "
  366. "destroy balance was %"PRId64", global is %"PRId64,
  367. cmux, cmux->destroy_cell_queue.n,
  368. (cmux->destroy_ctr),
  369. (global_destroy_ctr));
  370. } else {
  371. log_debug(LD_CIRC,
  372. "Freeing cmux at %p with no queued destroys, the cmux destroy "
  373. "balance was %"PRId64", global is %"PRId64,
  374. cmux,
  375. (cmux->destroy_ctr),
  376. (global_destroy_ctr));
  377. }
  378. destroy_cell_queue_clear(&cmux->destroy_cell_queue);
  379. tor_free(cmux);
  380. }
  381. /*
  382. * Circuitmux policy control functions
  383. */
  384. /**
  385. * Remove any policy installed on cmux; all policy data will be freed and
  386. * cmux behavior will revert to the built-in round-robin active_circuits
  387. * mechanism.
  388. */
  389. void
  390. circuitmux_clear_policy(circuitmux_t *cmux)
  391. {
  392. tor_assert(cmux);
  393. /* Internally, this is just setting policy to NULL */
  394. circuitmux_set_policy(cmux, NULL);
  395. }
  396. /**
  397. * Return the policy currently installed on a circuitmux_t
  398. */
  399. MOCK_IMPL(const circuitmux_policy_t *,
  400. circuitmux_get_policy, (circuitmux_t *cmux))
  401. {
  402. tor_assert(cmux);
  403. return cmux->policy;
  404. }
  405. /**
  406. * Set policy; allocate for new policy, detach all circuits from old policy
  407. * if any, attach them to new policy, and free old policy data.
  408. */
  409. void
  410. circuitmux_set_policy(circuitmux_t *cmux,
  411. const circuitmux_policy_t *pol)
  412. {
  413. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  414. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  415. chanid_circid_muxinfo_t **i = NULL;
  416. channel_t *chan = NULL;
  417. uint64_t last_chan_id_searched = 0;
  418. circuit_t *circ = NULL;
  419. tor_assert(cmux);
  420. /* Set up variables */
  421. old_pol = cmux->policy;
  422. old_pol_data = cmux->policy_data;
  423. new_pol = pol;
  424. /* Check if this is the trivial case */
  425. if (old_pol == new_pol) return;
  426. /* Allocate data for new policy, if any */
  427. if (new_pol && new_pol->alloc_cmux_data) {
  428. /*
  429. * If alloc_cmux_data is not null, then we expect to get some policy
  430. * data. Assert that we also have free_cmux_data so we can free it
  431. * when the time comes, and allocate it.
  432. */
  433. tor_assert(new_pol->free_cmux_data);
  434. new_pol_data = new_pol->alloc_cmux_data(cmux);
  435. tor_assert(new_pol_data);
  436. }
  437. /* Install new policy and new policy data on cmux */
  438. cmux->policy = new_pol;
  439. cmux->policy_data = new_pol_data;
  440. /* Iterate over all circuits, attaching/detaching each one */
  441. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  442. while (i) {
  443. /* Assert that this entry isn't NULL */
  444. tor_assert(*i);
  445. /*
  446. * Get the channel; since normal case is all circuits on the mux share a
  447. * channel, we cache last_chan_id_searched
  448. */
  449. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  450. chan = channel_find_by_global_id((*i)->chan_id);
  451. last_chan_id_searched = (*i)->chan_id;
  452. }
  453. tor_assert(chan);
  454. /* Get the circuit */
  455. circ = circuit_get_by_circid_channel_even_if_marked((*i)->circ_id, chan);
  456. tor_assert(circ);
  457. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  458. if (old_pol && old_pol->notify_circ_inactive &&
  459. (*i)->muxinfo.cell_count > 0) {
  460. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  461. (*i)->muxinfo.policy_data);
  462. }
  463. /* Need to free old policy data? */
  464. if ((*i)->muxinfo.policy_data) {
  465. /* Assert that we have the means to free it if we have policy data */
  466. tor_assert(old_pol);
  467. tor_assert(old_pol->free_circ_data);
  468. /* Free it */
  469. old_pol->free_circ_data(cmux, old_pol_data, circ,
  470. (*i)->muxinfo.policy_data);
  471. (*i)->muxinfo.policy_data = NULL;
  472. }
  473. /* Need to allocate new policy data? */
  474. if (new_pol && new_pol->alloc_circ_data) {
  475. /*
  476. * If alloc_circ_data is not null, we expect to get some per-circuit
  477. * policy data. Assert that we also have free_circ_data so we can
  478. * free it when the time comes, and allocate it.
  479. */
  480. tor_assert(new_pol->free_circ_data);
  481. (*i)->muxinfo.policy_data =
  482. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  483. (*i)->muxinfo.direction,
  484. (*i)->muxinfo.cell_count);
  485. }
  486. /* Need to make active on new policy? */
  487. if (new_pol && new_pol->notify_circ_active &&
  488. (*i)->muxinfo.cell_count > 0) {
  489. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  490. (*i)->muxinfo.policy_data);
  491. }
  492. /* Advance to next circuit map entry */
  493. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  494. }
  495. /* Free data for old policy, if any */
  496. if (old_pol_data) {
  497. /*
  498. * If we had old policy data, we should have an old policy and a free
  499. * function for it.
  500. */
  501. tor_assert(old_pol);
  502. tor_assert(old_pol->free_cmux_data);
  503. old_pol->free_cmux_data(cmux, old_pol_data);
  504. old_pol_data = NULL;
  505. }
  506. }
  507. /*
  508. * Circuitmux/circuit attachment status inquiry functions
  509. */
  510. /**
  511. * Query the direction of an attached circuit
  512. */
  513. cell_direction_t
  514. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  515. {
  516. chanid_circid_muxinfo_t *hashent = NULL;
  517. /* Try to find a map entry */
  518. hashent = circuitmux_find_map_entry(cmux, circ);
  519. /*
  520. * This function should only be called on attached circuits; assert that
  521. * we had a map entry.
  522. */
  523. tor_assert(hashent);
  524. /* Return the direction from the map entry */
  525. return hashent->muxinfo.direction;
  526. }
  527. /**
  528. * Find an entry in the cmux's map for this circuit or return NULL if there
  529. * is none.
  530. */
  531. static chanid_circid_muxinfo_t *
  532. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  533. {
  534. chanid_circid_muxinfo_t search, *hashent = NULL;
  535. /* Sanity-check parameters */
  536. tor_assert(cmux);
  537. tor_assert(cmux->chanid_circid_map);
  538. tor_assert(circ);
  539. /* Check if we have n_chan */
  540. if (circ->n_chan) {
  541. /* Okay, let's see if it's attached for n_chan/n_circ_id */
  542. search.chan_id = circ->n_chan->global_identifier;
  543. search.circ_id = circ->n_circ_id;
  544. /* Query */
  545. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  546. &search);
  547. }
  548. /* Found something? */
  549. if (hashent) {
  550. /*
  551. * Assert that the direction makes sense for a hashent we found by
  552. * n_chan/n_circ_id before we return it.
  553. */
  554. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  555. } else {
  556. /* Not there, have we got a p_chan/p_circ_id to try? */
  557. if (circ->magic == OR_CIRCUIT_MAGIC) {
  558. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  559. /* Check for p_chan */
  560. if (TO_OR_CIRCUIT(circ)->p_chan) {
  561. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  562. /* Okay, search for that */
  563. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  564. &search);
  565. /* Find anything? */
  566. if (hashent) {
  567. /* Assert that the direction makes sense before we return it */
  568. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  569. }
  570. }
  571. }
  572. }
  573. /* Okay, hashent is it if it was there */
  574. return hashent;
  575. }
  576. /**
  577. * Query whether a circuit is attached to a circuitmux
  578. */
  579. int
  580. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  581. {
  582. chanid_circid_muxinfo_t *hashent = NULL;
  583. /* Look if it's in the circuit map */
  584. hashent = circuitmux_find_map_entry(cmux, circ);
  585. return (hashent != NULL);
  586. }
  587. /**
  588. * Query whether a circuit is active on a circuitmux
  589. */
  590. int
  591. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  592. {
  593. chanid_circid_muxinfo_t *hashent = NULL;
  594. int is_active = 0;
  595. tor_assert(cmux);
  596. tor_assert(circ);
  597. /* Look if it's in the circuit map */
  598. hashent = circuitmux_find_map_entry(cmux, circ);
  599. if (hashent) {
  600. /* Check the number of cells on this circuit */
  601. is_active = (hashent->muxinfo.cell_count > 0);
  602. }
  603. /* else not attached, so not active */
  604. return is_active;
  605. }
  606. /**
  607. * Query number of available cells for a circuit on a circuitmux
  608. */
  609. unsigned int
  610. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  611. {
  612. chanid_circid_muxinfo_t *hashent = NULL;
  613. unsigned int n_cells = 0;
  614. tor_assert(cmux);
  615. tor_assert(circ);
  616. /* Look if it's in the circuit map */
  617. hashent = circuitmux_find_map_entry(cmux, circ);
  618. if (hashent) {
  619. /* Just get the cell count for this circuit */
  620. n_cells = hashent->muxinfo.cell_count;
  621. }
  622. /* else not attached, so 0 cells */
  623. return n_cells;
  624. }
  625. /**
  626. * Query total number of available cells on a circuitmux
  627. */
  628. MOCK_IMPL(unsigned int,
  629. circuitmux_num_cells, (circuitmux_t *cmux))
  630. {
  631. tor_assert(cmux);
  632. return cmux->n_cells + cmux->destroy_cell_queue.n;
  633. }
  634. /**
  635. * Query total number of circuits active on a circuitmux
  636. */
  637. unsigned int
  638. circuitmux_num_active_circuits(circuitmux_t *cmux)
  639. {
  640. tor_assert(cmux);
  641. return cmux->n_active_circuits;
  642. }
  643. /**
  644. * Query total number of circuits attached to a circuitmux
  645. */
  646. unsigned int
  647. circuitmux_num_circuits(circuitmux_t *cmux)
  648. {
  649. tor_assert(cmux);
  650. return cmux->n_circuits;
  651. }
  652. /*
  653. * Functions for circuit code to call to update circuit status
  654. */
  655. /**
  656. * Attach a circuit to a circuitmux, for the specified direction.
  657. */
  658. MOCK_IMPL(void,
  659. circuitmux_attach_circuit,(circuitmux_t *cmux, circuit_t *circ,
  660. cell_direction_t direction))
  661. {
  662. channel_t *chan = NULL;
  663. uint64_t channel_id;
  664. circid_t circ_id;
  665. chanid_circid_muxinfo_t search, *hashent = NULL;
  666. unsigned int cell_count;
  667. tor_assert(cmux);
  668. tor_assert(circ);
  669. tor_assert(direction == CELL_DIRECTION_IN ||
  670. direction == CELL_DIRECTION_OUT);
  671. /*
  672. * Figure out which channel we're using, and get the circuit's current
  673. * cell count and circuit ID; assert that the circuit is not already
  674. * attached to another mux.
  675. */
  676. if (direction == CELL_DIRECTION_OUT) {
  677. /* It's n_chan */
  678. chan = circ->n_chan;
  679. cell_count = circ->n_chan_cells.n;
  680. circ_id = circ->n_circ_id;
  681. } else {
  682. /* We want p_chan */
  683. chan = TO_OR_CIRCUIT(circ)->p_chan;
  684. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  685. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  686. }
  687. /* Assert that we did get a channel */
  688. tor_assert(chan);
  689. /* Assert that the circuit ID is sensible */
  690. tor_assert(circ_id != 0);
  691. /* Get the channel ID */
  692. channel_id = chan->global_identifier;
  693. /* See if we already have this one */
  694. search.chan_id = channel_id;
  695. search.circ_id = circ_id;
  696. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  697. &search);
  698. if (hashent) {
  699. /*
  700. * This circuit was already attached to this cmux; make sure the
  701. * directions match and update the cell count and active circuit count.
  702. */
  703. log_info(LD_CIRC,
  704. "Circuit %u on channel %"PRIu64 " was already attached to "
  705. "(trying to attach to %p)",
  706. (unsigned)circ_id, (channel_id),
  707. cmux);
  708. /*
  709. * The mux pointer on this circuit and the direction in result should
  710. * match; otherwise assert.
  711. */
  712. tor_assert(hashent->muxinfo.direction == direction);
  713. /*
  714. * Looks okay; just update the cell count and active circuits if we must
  715. */
  716. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  717. --(cmux->n_active_circuits);
  718. circuitmux_make_circuit_inactive(cmux, circ);
  719. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  720. ++(cmux->n_active_circuits);
  721. circuitmux_make_circuit_active(cmux, circ);
  722. }
  723. cmux->n_cells -= hashent->muxinfo.cell_count;
  724. cmux->n_cells += cell_count;
  725. hashent->muxinfo.cell_count = cell_count;
  726. } else {
  727. /*
  728. * New circuit; add an entry and update the circuit/active circuit
  729. * counts.
  730. */
  731. log_debug(LD_CIRC,
  732. "Attaching circuit %u on channel %"PRIu64 " to cmux %p",
  733. (unsigned)circ_id, (channel_id), cmux);
  734. /* Insert it in the map */
  735. hashent = tor_malloc_zero(sizeof(*hashent));
  736. hashent->chan_id = channel_id;
  737. hashent->circ_id = circ_id;
  738. hashent->muxinfo.cell_count = cell_count;
  739. hashent->muxinfo.direction = direction;
  740. /* Allocate policy specific circuit data if we need it */
  741. if (cmux->policy->alloc_circ_data) {
  742. /* Assert that we have the means to free policy-specific data */
  743. tor_assert(cmux->policy->free_circ_data);
  744. /* Allocate it */
  745. hashent->muxinfo.policy_data =
  746. cmux->policy->alloc_circ_data(cmux,
  747. cmux->policy_data,
  748. circ,
  749. direction,
  750. cell_count);
  751. /* If we wanted policy data, it's an error not to get any */
  752. tor_assert(hashent->muxinfo.policy_data);
  753. }
  754. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  755. hashent);
  756. /* Update counters */
  757. ++(cmux->n_circuits);
  758. if (cell_count > 0) {
  759. ++(cmux->n_active_circuits);
  760. circuitmux_make_circuit_active(cmux, circ);
  761. }
  762. cmux->n_cells += cell_count;
  763. }
  764. }
  765. /**
  766. * Detach a circuit from a circuitmux and update all counters as needed;
  767. * no-op if not attached.
  768. */
  769. MOCK_IMPL(void,
  770. circuitmux_detach_circuit,(circuitmux_t *cmux, circuit_t *circ))
  771. {
  772. chanid_circid_muxinfo_t search, *hashent = NULL;
  773. /*
  774. * Use this to keep track of whether we found it for n_chan or
  775. * p_chan for consistency checking.
  776. *
  777. * The 0 initializer is not a valid cell_direction_t value.
  778. * We assert that it has been replaced with a valid value before it is used.
  779. */
  780. cell_direction_t last_searched_direction = 0;
  781. tor_assert(cmux);
  782. tor_assert(cmux->chanid_circid_map);
  783. tor_assert(circ);
  784. /* See if we have it for n_chan/n_circ_id */
  785. if (circ->n_chan) {
  786. search.chan_id = circ->n_chan->global_identifier;
  787. search.circ_id = circ->n_circ_id;
  788. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  789. &search);
  790. last_searched_direction = CELL_DIRECTION_OUT;
  791. }
  792. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  793. if (!hashent) {
  794. if (circ->magic == OR_CIRCUIT_MAGIC) {
  795. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  796. if (TO_OR_CIRCUIT(circ)->p_chan) {
  797. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  798. hashent = HT_FIND(chanid_circid_muxinfo_map,
  799. cmux->chanid_circid_map,
  800. &search);
  801. last_searched_direction = CELL_DIRECTION_IN;
  802. }
  803. }
  804. }
  805. tor_assert(last_searched_direction == CELL_DIRECTION_OUT
  806. || last_searched_direction == CELL_DIRECTION_IN);
  807. /*
  808. * If hashent isn't NULL, we have a circuit to detach; don't remove it from
  809. * the map until later of circuitmux_make_circuit_inactive() breaks.
  810. */
  811. if (hashent) {
  812. /* Update counters */
  813. --(cmux->n_circuits);
  814. if (hashent->muxinfo.cell_count > 0) {
  815. --(cmux->n_active_circuits);
  816. /* This does policy notifies, so comes before freeing policy data */
  817. circuitmux_make_circuit_inactive(cmux, circ);
  818. }
  819. cmux->n_cells -= hashent->muxinfo.cell_count;
  820. /* Free policy-specific data if we have it */
  821. if (hashent->muxinfo.policy_data) {
  822. /* If we have policy data, assert that we have the means to free it */
  823. tor_assert(cmux->policy);
  824. tor_assert(cmux->policy->free_circ_data);
  825. /* Call free_circ_data() */
  826. cmux->policy->free_circ_data(cmux,
  827. cmux->policy_data,
  828. circ,
  829. hashent->muxinfo.policy_data);
  830. hashent->muxinfo.policy_data = NULL;
  831. }
  832. /* Consistency check: the direction must match the direction searched */
  833. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  834. /* Now remove it from the map */
  835. HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map, hashent);
  836. /* Free the hash entry */
  837. tor_free(hashent);
  838. }
  839. }
  840. /**
  841. * Make a circuit active; update active list and policy-specific info, but
  842. * we don't mess with the counters or hash table here.
  843. */
  844. static void
  845. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  846. {
  847. tor_assert(cmux);
  848. tor_assert(cmux->policy);
  849. tor_assert(circ);
  850. /* Policy-specific notification */
  851. if (cmux->policy->notify_circ_active) {
  852. /* Okay, we need to check the circuit for policy data now */
  853. chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
  854. /* We should have found something */
  855. tor_assert(hashent);
  856. /* Notify */
  857. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  858. circ, hashent->muxinfo.policy_data);
  859. }
  860. }
  861. /**
  862. * Make a circuit inactive; update active list and policy-specific info, but
  863. * we don't mess with the counters or hash table here.
  864. */
  865. static void
  866. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ)
  867. {
  868. tor_assert(cmux);
  869. tor_assert(cmux->policy);
  870. tor_assert(circ);
  871. /* Policy-specific notification */
  872. if (cmux->policy->notify_circ_inactive) {
  873. /* Okay, we need to check the circuit for policy data now */
  874. chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
  875. /* We should have found something */
  876. tor_assert(hashent);
  877. /* Notify */
  878. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  879. circ, hashent->muxinfo.policy_data);
  880. }
  881. }
  882. /**
  883. * Clear the cell counter for a circuit on a circuitmux
  884. */
  885. void
  886. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  887. {
  888. /* This is the same as setting the cell count to zero */
  889. circuitmux_set_num_cells(cmux, circ, 0);
  890. }
  891. /**
  892. * Set the cell counter for a circuit on a circuitmux
  893. */
  894. void
  895. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  896. unsigned int n_cells)
  897. {
  898. chanid_circid_muxinfo_t *hashent = NULL;
  899. tor_assert(cmux);
  900. tor_assert(circ);
  901. /* Search for this circuit's entry */
  902. hashent = circuitmux_find_map_entry(cmux, circ);
  903. /* Assert that we found one */
  904. tor_assert(hashent);
  905. /* Update cmux cell counter */
  906. cmux->n_cells -= hashent->muxinfo.cell_count;
  907. cmux->n_cells += n_cells;
  908. /* Do we need to notify a cmux policy? */
  909. if (cmux->policy->notify_set_n_cells) {
  910. /* Call notify_set_n_cells */
  911. cmux->policy->notify_set_n_cells(cmux,
  912. cmux->policy_data,
  913. circ,
  914. hashent->muxinfo.policy_data,
  915. n_cells);
  916. }
  917. /*
  918. * Update cmux active circuit counter: is the old cell count > 0 and the
  919. * new cell count == 0 ?
  920. */
  921. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  922. --(cmux->n_active_circuits);
  923. hashent->muxinfo.cell_count = n_cells;
  924. circuitmux_make_circuit_inactive(cmux, circ);
  925. /* Is the old cell count == 0 and the new cell count > 0 ? */
  926. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  927. ++(cmux->n_active_circuits);
  928. hashent->muxinfo.cell_count = n_cells;
  929. circuitmux_make_circuit_active(cmux, circ);
  930. } else {
  931. hashent->muxinfo.cell_count = n_cells;
  932. }
  933. }
  934. /*
  935. * Functions for channel code to call to get a circuit to transmit from or
  936. * notify that cells have been transmitted.
  937. */
  938. /**
  939. * Pick a circuit to send from, using the active circuits list or a
  940. * circuitmux policy if one is available. This is called from channel.c.
  941. *
  942. * If we would rather send a destroy cell, return NULL and set
  943. * *<b>destroy_queue_out</b> to the destroy queue.
  944. *
  945. * If we have nothing to send, set *<b>destroy_queue_out</b> to NULL and
  946. * return NULL.
  947. */
  948. circuit_t *
  949. circuitmux_get_first_active_circuit(circuitmux_t *cmux,
  950. destroy_cell_queue_t **destroy_queue_out)
  951. {
  952. circuit_t *circ = NULL;
  953. tor_assert(cmux);
  954. tor_assert(cmux->policy);
  955. /* This callback is mandatory. */
  956. tor_assert(cmux->policy->pick_active_circuit);
  957. tor_assert(destroy_queue_out);
  958. *destroy_queue_out = NULL;
  959. if (cmux->destroy_cell_queue.n &&
  960. (!cmux->last_cell_was_destroy || cmux->n_active_circuits == 0)) {
  961. /* We have destroy cells to send, and either we just sent a relay cell,
  962. * or we have no relay cells to send. */
  963. /* XXXX We should let the cmux policy have some say in this eventually. */
  964. /* XXXX Alternating is not a terribly brilliant approach here. */
  965. *destroy_queue_out = &cmux->destroy_cell_queue;
  966. cmux->last_cell_was_destroy = 1;
  967. } else if (cmux->n_active_circuits > 0) {
  968. /* We also must have a cell available for this to be the case */
  969. tor_assert(cmux->n_cells > 0);
  970. /* Do we have a policy-provided circuit selector? */
  971. circ = cmux->policy->pick_active_circuit(cmux, cmux->policy_data);
  972. cmux->last_cell_was_destroy = 0;
  973. } else {
  974. tor_assert(cmux->n_cells == 0);
  975. tor_assert(cmux->destroy_cell_queue.n == 0);
  976. }
  977. return circ;
  978. }
  979. /**
  980. * Notify the circuitmux that cells have been sent on a circuit; this
  981. * is called from channel.c.
  982. */
  983. void
  984. circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  985. unsigned int n_cells)
  986. {
  987. chanid_circid_muxinfo_t *hashent = NULL;
  988. int becomes_inactive = 0;
  989. tor_assert(cmux);
  990. tor_assert(circ);
  991. if (n_cells == 0) return;
  992. /*
  993. * To handle this, we have to:
  994. *
  995. * 1.) Adjust the circuit's cell counter in the cmux hash table
  996. * 2.) Move the circuit to the tail of the active_circuits linked list
  997. * for this cmux, or make the circuit inactive if the cell count
  998. * went to zero.
  999. * 3.) Call cmux->policy->notify_xmit_cells(), if any
  1000. */
  1001. /* Find the hash entry */
  1002. hashent = circuitmux_find_map_entry(cmux, circ);
  1003. /* Assert that we found one */
  1004. tor_assert(hashent);
  1005. /* Adjust the cell counter and assert that we had that many cells to send */
  1006. tor_assert(n_cells <= hashent->muxinfo.cell_count);
  1007. hashent->muxinfo.cell_count -= n_cells;
  1008. /* Do we need to make the circuit inactive? */
  1009. if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
  1010. /* Adjust the mux cell counter */
  1011. cmux->n_cells -= n_cells;
  1012. /*
  1013. * We call notify_xmit_cells() before making the circuit inactive if needed,
  1014. * so the policy can always count on this coming in on an active circuit.
  1015. */
  1016. if (cmux->policy->notify_xmit_cells) {
  1017. cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
  1018. hashent->muxinfo.policy_data,
  1019. n_cells);
  1020. }
  1021. /*
  1022. * Now make the circuit inactive if needed; this will call the policy's
  1023. * notify_circ_inactive() if present.
  1024. */
  1025. if (becomes_inactive) {
  1026. --(cmux->n_active_circuits);
  1027. circuitmux_make_circuit_inactive(cmux, circ);
  1028. }
  1029. }
  1030. /**
  1031. * Notify the circuitmux that a destroy was sent, so we can update
  1032. * the counter.
  1033. */
  1034. void
  1035. circuitmux_notify_xmit_destroy(circuitmux_t *cmux)
  1036. {
  1037. tor_assert(cmux);
  1038. --(cmux->destroy_ctr);
  1039. --(global_destroy_ctr);
  1040. log_debug(LD_CIRC,
  1041. "Cmux at %p sent a destroy, cmux counter is now %"PRId64", "
  1042. "global counter is now %"PRId64,
  1043. cmux,
  1044. (cmux->destroy_ctr),
  1045. (global_destroy_ctr));
  1046. }
  1047. /*DOCDOC */
  1048. void
  1049. circuitmux_append_destroy_cell(channel_t *chan,
  1050. circuitmux_t *cmux,
  1051. circid_t circ_id,
  1052. uint8_t reason)
  1053. {
  1054. destroy_cell_queue_append(&cmux->destroy_cell_queue, circ_id, reason);
  1055. /* Destroy entering the queue, update counters */
  1056. ++(cmux->destroy_ctr);
  1057. ++global_destroy_ctr;
  1058. log_debug(LD_CIRC,
  1059. "Cmux at %p queued a destroy for circ %u, cmux counter is now "
  1060. "%"PRId64", global counter is now %"PRId64,
  1061. cmux, circ_id,
  1062. (cmux->destroy_ctr),
  1063. (global_destroy_ctr));
  1064. /* XXXX Duplicate code from append_cell_to_circuit_queue */
  1065. if (!channel_has_queued_writes(chan)) {
  1066. /* There is no data at all waiting to be sent on the outbuf. Add a
  1067. * cell, so that we can notice when it gets flushed, flushed_some can
  1068. * get called, and we can start putting more data onto the buffer then.
  1069. */
  1070. log_debug(LD_GENERAL, "Primed a buffer.");
  1071. channel_flush_from_first_active_circuit(chan, 1);
  1072. }
  1073. }
  1074. /*DOCDOC; for debugging 12184. This runs slowly. */
  1075. int64_t
  1076. circuitmux_count_queued_destroy_cells(const channel_t *chan,
  1077. const circuitmux_t *cmux)
  1078. {
  1079. int64_t n_destroy_cells = cmux->destroy_ctr;
  1080. int64_t destroy_queue_size = cmux->destroy_cell_queue.n;
  1081. int64_t manual_total = 0;
  1082. int64_t manual_total_in_map = 0;
  1083. destroy_cell_t *cell;
  1084. TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
  1085. circid_t id;
  1086. ++manual_total;
  1087. id = cell->circid;
  1088. if (circuit_id_in_use_on_channel(id, (channel_t*)chan))
  1089. ++manual_total_in_map;
  1090. }
  1091. if (n_destroy_cells != destroy_queue_size ||
  1092. n_destroy_cells != manual_total ||
  1093. n_destroy_cells != manual_total_in_map) {
  1094. log_warn(LD_BUG, " Discrepancy in counts for queued destroy cells on "
  1095. "circuitmux. n=%"PRId64". queue_size=%"PRId64". "
  1096. "manual_total=%"PRId64". manual_total_in_map=%"PRId64".",
  1097. (n_destroy_cells),
  1098. (destroy_queue_size),
  1099. (manual_total),
  1100. (manual_total_in_map));
  1101. }
  1102. return n_destroy_cells;
  1103. }
  1104. /**
  1105. * Compare cmuxes to see which is more preferred; return < 0 if
  1106. * cmux_1 has higher priority (i.e., cmux_1 < cmux_2 in the scheduler's
  1107. * sort order), > 0 if cmux_2 has higher priority, or 0 if they are
  1108. * equally preferred.
  1109. *
  1110. * If the cmuxes have different cmux policies or the policy does not
  1111. * support the cmp_cmux method, return 0.
  1112. */
  1113. MOCK_IMPL(int,
  1114. circuitmux_compare_muxes, (circuitmux_t *cmux_1, circuitmux_t *cmux_2))
  1115. {
  1116. const circuitmux_policy_t *policy;
  1117. tor_assert(cmux_1);
  1118. tor_assert(cmux_2);
  1119. if (cmux_1 == cmux_2) {
  1120. /* Equivalent because they're the same cmux */
  1121. return 0;
  1122. }
  1123. if (cmux_1->policy && cmux_2->policy) {
  1124. if (cmux_1->policy == cmux_2->policy) {
  1125. policy = cmux_1->policy;
  1126. if (policy->cmp_cmux) {
  1127. /* Okay, we can compare! */
  1128. return policy->cmp_cmux(cmux_1, cmux_1->policy_data,
  1129. cmux_2, cmux_2->policy_data);
  1130. } else {
  1131. /*
  1132. * Equivalent because the policy doesn't know how to compare between
  1133. * muxes.
  1134. */
  1135. return 0;
  1136. }
  1137. } else {
  1138. /* Equivalent because they have different policies */
  1139. return 0;
  1140. }
  1141. } else {
  1142. /* Equivalent because one or both are missing a policy */
  1143. return 0;
  1144. }
  1145. }