circuitmux.c 51 KB

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