circuitmux.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. };
  97. /*
  98. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  99. * circuit.
  100. */
  101. struct chanid_circid_muxinfo_t {
  102. HT_ENTRY(chanid_circid_muxinfo_t) node;
  103. uint64_t chan_id;
  104. circid_t circ_id;
  105. circuit_muxinfo_t muxinfo;
  106. };
  107. /*
  108. * Static function declarations
  109. */
  110. static INLINE int
  111. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  112. chanid_circid_muxinfo_t *b);
  113. static INLINE unsigned int
  114. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  115. static chanid_circid_muxinfo_t *
  116. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
  117. static void
  118. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  119. cell_direction_t direction);
  120. static void
  121. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  122. cell_direction_t direction);
  123. static INLINE void
  124. circuitmux_move_active_circ_to_tail(circuitmux_t *cmux, circuit_t *circ,
  125. cell_direction_t direction);
  126. static INLINE circuit_t **
  127. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  128. static INLINE circuit_t **
  129. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ);
  130. /* Function definitions */
  131. /**
  132. * Linked list helpers
  133. */
  134. /**
  135. * Move an active circuit to the tail of the cmux's active circuits list;
  136. * used by circuitmux_notify_xmit_cells().
  137. */
  138. static INLINE void
  139. circuitmux_move_active_circ_to_tail(circuitmux_t *cmux, circuit_t *circ,
  140. cell_direction_t direction)
  141. {
  142. circuit_t **next_p = NULL, **prev_p = NULL;
  143. circuit_t **next_prev = NULL, **prev_next = NULL;
  144. or_circuit_t *or_circ = NULL;
  145. tor_assert(cmux);
  146. tor_assert(circ);
  147. /* Figure out our next_p and prev_p for this cmux/direction */
  148. if (direction) {
  149. if (direction == CELL_DIRECTION_OUT) {
  150. tor_assert(circ->n_mux == cmux);
  151. next_p = &(circ->next_active_on_n_chan);
  152. prev_p = &(circ->prev_active_on_n_chan);
  153. } else {
  154. or_circ = TO_OR_CIRCUIT(circ);
  155. tor_assert(or_circ->p_mux == cmux);
  156. next_p = &(or_circ->next_active_on_p_chan);
  157. prev_p = &(or_circ->prev_active_on_p_chan);
  158. }
  159. } else {
  160. if (circ->n_mux == cmux) {
  161. next_p = &(circ->next_active_on_n_chan);
  162. prev_p = &(circ->prev_active_on_n_chan);
  163. direction = CELL_DIRECTION_OUT;
  164. } else {
  165. or_circ = TO_OR_CIRCUIT(circ);
  166. tor_assert(or_circ->p_mux == cmux);
  167. next_p = &(or_circ->next_active_on_p_chan);
  168. prev_p = &(or_circ->prev_active_on_p_chan);
  169. direction = CELL_DIRECTION_IN;
  170. }
  171. }
  172. tor_assert(next_p);
  173. tor_assert(prev_p);
  174. /* Check if this really is an active circuit */
  175. if ((*next_p == NULL && *prev_p == NULL) &&
  176. !(circ == cmux->active_circuits_head ||
  177. circ == cmux->active_circuits_tail)) {
  178. /* Not active, no-op */
  179. return;
  180. }
  181. /* Check if this is already the tail */
  182. if (circ == cmux->active_circuits_tail) return;
  183. /* Okay, we have to move it; figure out next_prev and prev_next */
  184. if (*next_p) next_prev = circuitmux_prev_active_circ_p(cmux, *next_p);
  185. if (*prev_p) prev_next = circuitmux_next_active_circ_p(cmux, *prev_p);
  186. /* Adjust the previous node's next pointer, if any */
  187. if (prev_next) *prev_next = *next_p;
  188. /* Otherwise, we were the head */
  189. else cmux->active_circuits_head = *next_p;
  190. /* Adjust the next node's previous pointer, if any */
  191. if (next_prev) *next_prev = *prev_p;
  192. /* Adjust our next and prev pointers */
  193. *next_p = NULL;
  194. *prev_p = cmux->active_circuits_tail;
  195. /* Set the tail to this circuit */
  196. cmux->active_circuits_tail = circ;
  197. }
  198. static INLINE circuit_t **
  199. circuitmux_next_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  200. {
  201. tor_assert(cmux);
  202. tor_assert(circ);
  203. if (circ->n_mux == cmux) return &(circ->next_active_on_n_chan);
  204. else {
  205. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  206. return &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  207. }
  208. }
  209. static INLINE circuit_t **
  210. circuitmux_prev_active_circ_p(circuitmux_t *cmux, circuit_t *circ)
  211. {
  212. tor_assert(cmux);
  213. tor_assert(circ);
  214. if (circ->n_mux == cmux) return &(circ->prev_active_on_n_chan);
  215. else {
  216. tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  217. return &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  218. }
  219. }
  220. /**
  221. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  222. * ID and circuit ID for a and b, and return less than, equal to, or greater
  223. * than zero appropriately.
  224. */
  225. static INLINE int
  226. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  227. chanid_circid_muxinfo_t *b)
  228. {
  229. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  230. }
  231. /**
  232. * Helper: return a hash based on circuit ID and channel ID in a.
  233. */
  234. static INLINE unsigned int
  235. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  236. {
  237. return (((unsigned int)(a->circ_id) << 8) ^
  238. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  239. ((unsigned int)(a->chan_id & 0xffffffff)));
  240. }
  241. /* Declare the struct chanid_circid_muxinfo_map type */
  242. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  243. /* Emit a bunch of hash table stuff */
  244. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  245. chanid_circid_entry_hash, chanid_circid_entries_eq);
  246. HT_GENERATE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  247. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  248. malloc, realloc, free);
  249. /*
  250. * Circuitmux alloc/free functions
  251. */
  252. /**
  253. * Allocate a new circuitmux_t
  254. */
  255. circuitmux_t *
  256. circuitmux_alloc(void)
  257. {
  258. circuitmux_t *rv = NULL;
  259. rv = tor_malloc_zero(sizeof(*rv));
  260. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  261. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  262. return rv;
  263. }
  264. /**
  265. * Detach all circuits from a circuitmux (use before circuitmux_free())
  266. */
  267. void
  268. circuitmux_detach_all_circuits(circuitmux_t *cmux)
  269. {
  270. chanid_circid_muxinfo_t **i = NULL, *to_remove;
  271. channel_t *chan = NULL;
  272. circuit_t *circ = NULL;
  273. tor_assert(cmux);
  274. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  275. while (i) {
  276. to_remove = *i;
  277. i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  278. if (to_remove) {
  279. /* Find a channel and circuit */
  280. chan = channel_find_by_global_id(to_remove->chan_id);
  281. if (chan) {
  282. circ = circuit_get_by_circid_channel(to_remove->circ_id, chan);
  283. if (circ) {
  284. /* Clear the circuit's mux for this direction */
  285. if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
  286. /* Clear n_mux */
  287. circ->n_mux = NULL;
  288. /*
  289. * Update active_circuits et al.; this does policy notifies, so
  290. * comes before freeing policy data
  291. */
  292. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_OUT);
  293. } else if (circ->magic == OR_CIRCUIT_MAGIC) {
  294. /*
  295. * It has a sensible p_chan and direction == CELL_DIRECTION_IN,
  296. * so clear p_mux.
  297. */
  298. TO_OR_CIRCUIT(circ)->p_mux = NULL;
  299. /*
  300. * Update active_circuits et al.; this does policy notifies, so
  301. * comes before freeing policy data
  302. */
  303. circuitmux_make_circuit_inactive(cmux, circ, CELL_DIRECTION_IN);
  304. } else {
  305. /* Complain and move on */
  306. log_warn(LD_CIRC,
  307. "Circuit %d/channel " U64_FORMAT " had direction == "
  308. "CELL_DIRECTION_IN, but isn't an or_circuit_t",
  309. to_remove->circ_id,
  310. U64_PRINTF_ARG(to_remove->chan_id));
  311. }
  312. /* Free policy-specific data if we have it */
  313. if (to_remove->muxinfo.policy_data) {
  314. /*
  315. * If we have policy data, assert that we have the means to
  316. * free it
  317. */
  318. tor_assert(cmux->policy);
  319. tor_assert(cmux->policy->free_circ_data);
  320. /* Call free_circ_data() */
  321. cmux->policy->free_circ_data(cmux,
  322. cmux->policy_data,
  323. circ,
  324. to_remove->muxinfo.policy_data);
  325. to_remove->muxinfo.policy_data = NULL;
  326. }
  327. } else {
  328. /* Complain and move on */
  329. log_warn(LD_CIRC,
  330. "Couldn't find circuit %d (for channel " U64_FORMAT ")",
  331. to_remove->circ_id,
  332. U64_PRINTF_ARG(to_remove->chan_id));
  333. }
  334. } else {
  335. /* Complain and move on */
  336. log_warn(LD_CIRC,
  337. "Couldn't find channel " U64_FORMAT " (for circuit id %d)",
  338. U64_PRINTF_ARG(to_remove->chan_id),
  339. to_remove->circ_id);
  340. }
  341. /* Assert that we don't have un-freed policy data for this circuit */
  342. tor_assert(to_remove->muxinfo.policy_data == NULL);
  343. /* Free it */
  344. tor_free(to_remove);
  345. }
  346. }
  347. cmux->n_circuits = 0;
  348. cmux->n_active_circuits = 0;
  349. cmux->n_cells = 0;
  350. }
  351. /**
  352. * Free a circuitmux_t; the circuits must be detached first with
  353. * circuitmux_detach_all_circuits().
  354. */
  355. void
  356. circuitmux_free(circuitmux_t *cmux)
  357. {
  358. if (!cmux) return;
  359. tor_assert(cmux->n_circuits == 0);
  360. tor_assert(cmux->n_active_circuits == 0);
  361. /*
  362. * Free policy-specific data if we have any; we don't
  363. * need to do circuitmux_set_policy(cmux, NULL) to cover
  364. * the circuits because they would have been handled in
  365. * circuitmux_detach_all_circuits() before this was
  366. * called.
  367. */
  368. if (cmux->policy && cmux->policy->free_cmux_data) {
  369. if (cmux->policy_data) {
  370. cmux->policy->free_cmux_data(cmux, cmux->policy_data);
  371. cmux->policy_data = NULL;
  372. }
  373. } else tor_assert(cmux->policy_data == NULL);
  374. if (cmux->chanid_circid_map) {
  375. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  376. tor_free(cmux->chanid_circid_map);
  377. }
  378. tor_free(cmux);
  379. }
  380. /*
  381. * Circuitmux policy control functions
  382. */
  383. /**
  384. * Remove any policy installed on cmux; all policy data will be freed and
  385. * cmux behavior will revert to the built-in round-robin active_circuits
  386. * mechanism.
  387. */
  388. void
  389. circuitmux_clear_policy(circuitmux_t *cmux)
  390. {
  391. tor_assert(cmux);
  392. /* Internally, this is just setting policy to NULL */
  393. if (cmux->policy) {
  394. circuitmux_set_policy(cmux, NULL);
  395. }
  396. }
  397. /**
  398. * Return the policy currently installed on a circuitmux_t
  399. */
  400. const circuitmux_policy_t *
  401. circuitmux_get_policy(circuitmux_t *cmux)
  402. {
  403. tor_assert(cmux);
  404. return cmux->policy;
  405. }
  406. /**
  407. * Set policy; allocate for new policy, detach all circuits from old policy
  408. * if any, attach them to new policy, and free old policy data.
  409. */
  410. void
  411. circuitmux_set_policy(circuitmux_t *cmux,
  412. const circuitmux_policy_t *pol)
  413. {
  414. const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  415. circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  416. chanid_circid_muxinfo_t **i = NULL;
  417. channel_t *chan = NULL;
  418. uint64_t last_chan_id_searched = 0;
  419. circuit_t *circ = NULL;
  420. tor_assert(cmux);
  421. /* Set up variables */
  422. old_pol = cmux->policy;
  423. old_pol_data = cmux->policy_data;
  424. new_pol = pol;
  425. /* Check if this is the trivial case */
  426. if (old_pol == new_pol) return;
  427. /* Allocate data for new policy, if any */
  428. if (new_pol && new_pol->alloc_cmux_data) {
  429. /*
  430. * If alloc_cmux_data is not null, then we expect to get some policy
  431. * data. Assert that we also have free_cmux_data so we can free it
  432. * when the time comes, and allocate it.
  433. */
  434. tor_assert(new_pol->free_cmux_data);
  435. new_pol_data = new_pol->alloc_cmux_data(cmux);
  436. tor_assert(new_pol_data);
  437. }
  438. /* Install new policy and new policy data on cmux */
  439. cmux->policy = new_pol;
  440. cmux->policy_data = new_pol_data;
  441. /* Iterate over all circuits, attaching/detaching each one */
  442. i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  443. while (i) {
  444. /* Assert that this entry isn't NULL */
  445. tor_assert(*i);
  446. /*
  447. * Get the channel; since normal case is all circuits on the mux share a
  448. * channel, we cache last_chan_id_searched
  449. */
  450. if (!chan || last_chan_id_searched != (*i)->chan_id) {
  451. chan = channel_find_by_global_id((*i)->chan_id);
  452. last_chan_id_searched = (*i)->chan_id;
  453. }
  454. tor_assert(chan);
  455. /* Get the circuit */
  456. circ = circuit_get_by_circid_channel((*i)->circ_id, chan);
  457. tor_assert(circ);
  458. /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
  459. if (old_pol && old_pol->notify_circ_inactive &&
  460. (*i)->muxinfo.cell_count > 0) {
  461. old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
  462. (*i)->muxinfo.policy_data);
  463. }
  464. /* Need to free old policy data? */
  465. if ((*i)->muxinfo.policy_data) {
  466. /* Assert that we have the means to free it if we have policy data */
  467. tor_assert(old_pol);
  468. tor_assert(old_pol->free_circ_data);
  469. /* Free it */
  470. old_pol->free_circ_data(cmux, old_pol_data, circ,
  471. (*i)->muxinfo.policy_data);
  472. (*i)->muxinfo.policy_data = NULL;
  473. }
  474. /* Need to allocate new policy data? */
  475. if (new_pol && new_pol->alloc_circ_data) {
  476. /*
  477. * If alloc_circ_data is not null, we expect to get some per-circuit
  478. * policy data. Assert that we also have free_circ_data so we can
  479. * free it when the time comes, and allocate it.
  480. */
  481. tor_assert(new_pol->free_circ_data);
  482. (*i)->muxinfo.policy_data =
  483. new_pol->alloc_circ_data(cmux, new_pol_data, circ,
  484. (*i)->muxinfo.direction,
  485. (*i)->muxinfo.cell_count);
  486. }
  487. /* Need to make active on new policy? */
  488. if (new_pol && new_pol->notify_circ_active &&
  489. (*i)->muxinfo.cell_count > 0) {
  490. new_pol->notify_circ_active(cmux, new_pol_data, circ,
  491. (*i)->muxinfo.policy_data);
  492. }
  493. /* Advance to next circuit map entry */
  494. i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  495. }
  496. /* Free data for old policy, if any */
  497. if (old_pol_data) {
  498. /*
  499. * If we had old policy data, we should have an old policy and a free
  500. * function for it.
  501. */
  502. tor_assert(old_pol);
  503. tor_assert(old_pol->free_cmux_data);
  504. old_pol->free_cmux_data(cmux, old_pol_data);
  505. old_pol_data = NULL;
  506. }
  507. }
  508. /*
  509. * Circuitmux/circuit attachment status inquiry functions
  510. */
  511. /**
  512. * Query the direction of an attached circuit
  513. */
  514. cell_direction_t
  515. circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
  516. {
  517. chanid_circid_muxinfo_t *hashent = NULL;
  518. /* Try to find a map entry */
  519. hashent = circuitmux_find_map_entry(cmux, circ);
  520. /*
  521. * This function should only be called on attached circuits; assert that
  522. * we had a map entry.
  523. */
  524. tor_assert(hashent);
  525. /* Return the direction from the map entry */
  526. return hashent->muxinfo.direction;
  527. }
  528. /**
  529. * Find an entry in the cmux's map for this circuit or return NULL if there
  530. * is none.
  531. */
  532. static chanid_circid_muxinfo_t *
  533. circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
  534. {
  535. chanid_circid_muxinfo_t search, *hashent = NULL;
  536. /* Sanity-check parameters */
  537. tor_assert(cmux);
  538. tor_assert(cmux->chanid_circid_map);
  539. tor_assert(circ);
  540. tor_assert(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. /* Found something? */
  548. if (hashent) {
  549. /*
  550. * Assert that the direction makes sense for a hashent we found by
  551. * n_chan/n_circ_id before we return it.
  552. */
  553. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  554. } else {
  555. /* Not there, have we got a p_chan/p_circ_id to try? */
  556. if (circ->magic == OR_CIRCUIT_MAGIC) {
  557. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  558. /* Check for p_chan */
  559. if (TO_OR_CIRCUIT(circ)->p_chan) {
  560. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  561. /* Okay, search for that */
  562. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  563. &search);
  564. /* Find anything? */
  565. if (hashent) {
  566. /* Assert that the direction makes sense before we return it */
  567. tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
  568. }
  569. }
  570. }
  571. }
  572. /* Okay, hashent is it if it was there */
  573. return hashent;
  574. }
  575. /**
  576. * Query whether a circuit is attached to a circuitmux
  577. */
  578. int
  579. circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
  580. {
  581. chanid_circid_muxinfo_t *hashent = NULL;
  582. /* Look if it's in the circuit map */
  583. hashent = circuitmux_find_map_entry(cmux, circ);
  584. return (hashent != NULL);
  585. }
  586. /**
  587. * Query whether a circuit is active on a circuitmux
  588. */
  589. int
  590. circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
  591. {
  592. chanid_circid_muxinfo_t *hashent = NULL;
  593. int is_active = 0;
  594. tor_assert(cmux);
  595. tor_assert(circ);
  596. /* Look if it's in the circuit map */
  597. hashent = circuitmux_find_map_entry(cmux, circ);
  598. if (hashent) {
  599. /* Check the number of cells on this circuit */
  600. is_active = (hashent->muxinfo.cell_count > 0);
  601. }
  602. /* else not attached, so not active */
  603. return is_active;
  604. }
  605. /**
  606. * Query number of available cells for a circuit on a circuitmux
  607. */
  608. unsigned int
  609. circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
  610. {
  611. chanid_circid_muxinfo_t *hashent = NULL;
  612. unsigned int n_cells = 0;
  613. tor_assert(cmux);
  614. tor_assert(circ);
  615. /* Look if it's in the circuit map */
  616. hashent = circuitmux_find_map_entry(cmux, circ);
  617. if (hashent) {
  618. /* Just get the cell count for this circuit */
  619. n_cells = hashent->muxinfo.cell_count;
  620. }
  621. /* else not attached, so 0 cells */
  622. return n_cells;
  623. }
  624. /**
  625. * Query total number of available cells on a circuitmux
  626. */
  627. unsigned int
  628. circuitmux_num_cells(circuitmux_t *cmux)
  629. {
  630. tor_assert(cmux);
  631. return cmux->n_cells;
  632. }
  633. /**
  634. * Query total number of circuits active on a circuitmux
  635. */
  636. unsigned int
  637. circuitmux_num_active_circuits(circuitmux_t *cmux)
  638. {
  639. tor_assert(cmux);
  640. return cmux->n_active_circuits;
  641. }
  642. /**
  643. * Query total number of circuits attached to a circuitmux
  644. */
  645. unsigned int
  646. circuitmux_num_circuits(circuitmux_t *cmux)
  647. {
  648. tor_assert(cmux);
  649. return cmux->n_circuits;
  650. }
  651. /*
  652. * Functions for circuit code to call to update circuit status
  653. */
  654. /**
  655. * Attach a circuit to a circuitmux, for the specified direction.
  656. */
  657. void
  658. circuitmux_attach_circuit(circuitmux_t *cmux, circuit_t *circ,
  659. cell_direction_t direction)
  660. {
  661. channel_t *chan = NULL;
  662. uint64_t channel_id;
  663. circid_t circ_id;
  664. chanid_circid_muxinfo_t search, *hashent = NULL;
  665. unsigned int cell_count;
  666. tor_assert(cmux);
  667. tor_assert(circ);
  668. tor_assert(direction == CELL_DIRECTION_IN ||
  669. direction == CELL_DIRECTION_OUT);
  670. /*
  671. * Figure out which channel we're using, and get the circuit's current
  672. * cell count and circuit ID; assert that the circuit is not already
  673. * attached to another mux.
  674. */
  675. if (direction == CELL_DIRECTION_OUT) {
  676. /* It's n_chan */
  677. chan = circ->n_chan;
  678. cell_count = circ->n_chan_cells.n;
  679. circ_id = circ->n_circ_id;
  680. } else {
  681. /* We want p_chan */
  682. chan = TO_OR_CIRCUIT(circ)->p_chan;
  683. cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
  684. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  685. }
  686. /* Assert that we did get a channel */
  687. tor_assert(chan);
  688. /* Assert that the circuit ID is sensible */
  689. tor_assert(circ_id != 0);
  690. /* Get the channel ID */
  691. channel_id = chan->global_identifier;
  692. /* See if we already have this one */
  693. search.chan_id = channel_id;
  694. search.circ_id = circ_id;
  695. hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  696. &search);
  697. if (hashent) {
  698. /*
  699. * This circuit was already attached to this cmux; make sure the
  700. * directions match and update the cell count and active circuit count.
  701. */
  702. log_info(LD_CIRC,
  703. "Circuit %u on channel " U64_FORMAT " was already attached to "
  704. "cmux %p (trying to attach to %p)",
  705. circ_id, U64_PRINTF_ARG(channel_id),
  706. ((direction == CELL_DIRECTION_OUT) ?
  707. circ->n_mux : TO_OR_CIRCUIT(circ)->p_mux),
  708. cmux);
  709. /*
  710. * The mux pointer on this circuit and the direction in result should
  711. * match; otherwise assert.
  712. */
  713. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == cmux);
  714. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == cmux);
  715. tor_assert(hashent->muxinfo.direction == direction);
  716. /*
  717. * Looks okay; just update the cell count and active circuits if we must
  718. */
  719. if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
  720. --(cmux->n_active_circuits);
  721. circuitmux_make_circuit_inactive(cmux, circ, direction);
  722. } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
  723. ++(cmux->n_active_circuits);
  724. circuitmux_make_circuit_active(cmux, circ, direction);
  725. }
  726. cmux->n_cells -= hashent->muxinfo.cell_count;
  727. cmux->n_cells += cell_count;
  728. hashent->muxinfo.cell_count = cell_count;
  729. } else {
  730. /*
  731. * New circuit; add an entry and update the circuit/active circuit
  732. * counts.
  733. */
  734. log_debug(LD_CIRC,
  735. "Attaching circuit %u on channel " U64_FORMAT " to cmux %p",
  736. circ_id, U64_PRINTF_ARG(channel_id), cmux);
  737. /*
  738. * Assert that the circuit doesn't already have a mux for this
  739. * direction.
  740. */
  741. if (direction == CELL_DIRECTION_OUT) tor_assert(circ->n_mux == NULL);
  742. else tor_assert(TO_OR_CIRCUIT(circ)->p_mux == NULL);
  743. /* Insert it in the map */
  744. hashent = tor_malloc_zero(sizeof(*hashent));
  745. hashent->chan_id = channel_id;
  746. hashent->circ_id = circ_id;
  747. hashent->muxinfo.cell_count = cell_count;
  748. hashent->muxinfo.direction = direction;
  749. /* Allocate policy specific circuit data if we need it */
  750. if (cmux->policy && cmux->policy->alloc_circ_data) {
  751. /* Assert that we have the means to free policy-specific data */
  752. tor_assert(cmux->policy->free_circ_data);
  753. /* Allocate it */
  754. hashent->muxinfo.policy_data =
  755. cmux->policy->alloc_circ_data(cmux,
  756. cmux->policy_data,
  757. circ,
  758. direction,
  759. cell_count);
  760. /* If we wanted policy data, it's an error not to get any */
  761. tor_assert(hashent->muxinfo.policy_data);
  762. }
  763. HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  764. hashent);
  765. /* Set the circuit's mux for this direction */
  766. if (direction == CELL_DIRECTION_OUT) circ->n_mux = cmux;
  767. else TO_OR_CIRCUIT(circ)->p_mux = cmux;
  768. /* Make sure the next/prev pointers are NULL */
  769. if (direction == CELL_DIRECTION_OUT) {
  770. circ->next_active_on_n_chan = NULL;
  771. circ->prev_active_on_n_chan = NULL;
  772. } else {
  773. TO_OR_CIRCUIT(circ)->next_active_on_p_chan = NULL;
  774. TO_OR_CIRCUIT(circ)->prev_active_on_p_chan = NULL;
  775. }
  776. /* Update counters */
  777. ++(cmux->n_circuits);
  778. if (cell_count > 0) {
  779. ++(cmux->n_active_circuits);
  780. circuitmux_make_circuit_active(cmux, circ, direction);
  781. }
  782. cmux->n_cells += cell_count;
  783. }
  784. }
  785. /**
  786. * Detach a circuit from a circuitmux and update all counters as needed;
  787. * no-op if not attached.
  788. */
  789. void
  790. circuitmux_detach_circuit(circuitmux_t *cmux, circuit_t *circ)
  791. {
  792. chanid_circid_muxinfo_t search, *hashent = NULL;
  793. /*
  794. * Use this to keep track of whether we found it for n_chan or
  795. * p_chan for consistency checking.
  796. */
  797. cell_direction_t last_searched_direction;
  798. tor_assert(cmux);
  799. tor_assert(cmux->chanid_circid_map);
  800. tor_assert(circ);
  801. tor_assert(circ->n_chan);
  802. /* See if we have it for n_chan/n_circ_id */
  803. search.chan_id = circ->n_chan->global_identifier;
  804. search.circ_id = circ->n_circ_id;
  805. hashent = HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
  806. &search);
  807. last_searched_direction = CELL_DIRECTION_OUT;
  808. /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  809. if (!hashent) {
  810. if (circ->magic == OR_CIRCUIT_MAGIC) {
  811. search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  812. if (TO_OR_CIRCUIT(circ)->p_chan) {
  813. search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
  814. hashent = HT_REMOVE(chanid_circid_muxinfo_map,
  815. cmux->chanid_circid_map,
  816. &search);
  817. last_searched_direction = CELL_DIRECTION_IN;
  818. }
  819. }
  820. }
  821. /* If hashent isn't NULL, we just removed it from the map */
  822. if (hashent) {
  823. /* Update counters */
  824. --(cmux->n_circuits);
  825. if (hashent->muxinfo.cell_count > 0) {
  826. --(cmux->n_active_circuits);
  827. /* This does policy notifies, so comes before freeing policy data */
  828. circuitmux_make_circuit_inactive(cmux, circ, last_searched_direction);
  829. }
  830. cmux->n_cells -= hashent->muxinfo.cell_count;
  831. /* Free policy-specific data if we have it */
  832. if (hashent->muxinfo.policy_data) {
  833. /* If we have policy data, assert that we have the means to free it */
  834. tor_assert(cmux->policy);
  835. tor_assert(cmux->policy->free_circ_data);
  836. /* Call free_circ_data() */
  837. cmux->policy->free_circ_data(cmux,
  838. cmux->policy_data,
  839. circ,
  840. hashent->muxinfo.policy_data);
  841. hashent->muxinfo.policy_data = NULL;
  842. }
  843. /* Consistency check: the direction must match the direction searched */
  844. tor_assert(last_searched_direction == hashent->muxinfo.direction);
  845. /* Clear the circuit's mux for this direction */
  846. if (last_searched_direction == CELL_DIRECTION_OUT) circ->n_mux = NULL;
  847. else TO_OR_CIRCUIT(circ)->p_mux = NULL;
  848. /* Free the hash entry */
  849. tor_free(hashent);
  850. }
  851. }
  852. /**
  853. * Make a circuit active; update active list and policy-specific info, but
  854. * we don't mess with the counters or hash table here.
  855. */
  856. static void
  857. circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ,
  858. cell_direction_t direction)
  859. {
  860. circuit_t **next_active = NULL, **prev_active = NULL, **next_prev = NULL;
  861. circuitmux_t *circuit_cmux = NULL;
  862. chanid_circid_muxinfo_t *hashent = NULL;
  863. channel_t *chan = NULL;
  864. circid_t circ_id;
  865. int already_active;
  866. tor_assert(cmux);
  867. tor_assert(circ);
  868. tor_assert(direction == CELL_DIRECTION_OUT ||
  869. direction == CELL_DIRECTION_IN);
  870. /* Get the right set of active list links for this direction */
  871. if (direction == CELL_DIRECTION_OUT) {
  872. next_active = &(circ->next_active_on_n_chan);
  873. prev_active = &(circ->prev_active_on_n_chan);
  874. circuit_cmux = circ->n_mux;
  875. chan = circ->n_chan;
  876. circ_id = circ->n_circ_id;
  877. } else {
  878. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  879. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  880. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  881. chan = TO_OR_CIRCUIT(circ)->p_chan;
  882. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  883. }
  884. /* Assert that it is attached to this mux and a channel */
  885. tor_assert(cmux == circuit_cmux);
  886. tor_assert(chan != NULL);
  887. /*
  888. * Check if the circuit really was inactive; if it's active, at least one
  889. * of the next_active and prev_active pointers will not be NULL, or this
  890. * circuit will be either the head or tail of the list for this cmux.
  891. */
  892. already_active = (*prev_active != NULL || *next_active != NULL ||
  893. cmux->active_circuits_head == circ ||
  894. cmux->active_circuits_tail == circ);
  895. /* If we're already active, log a warning and finish */
  896. if (already_active) {
  897. log_warn(LD_CIRC,
  898. "Circuit %d on channel " U64_FORMAT " was already active",
  899. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  900. return;
  901. }
  902. /*
  903. * This is going at the head of the list; if the old head is not NULL,
  904. * then its prev pointer should point to this.
  905. */
  906. *next_active = cmux->active_circuits_head; /* Next is old head */
  907. *prev_active = NULL; /* Prev is NULL (this will be the head) */
  908. if (cmux->active_circuits_head) {
  909. /* The list had an old head; update its prev pointer */
  910. next_prev =
  911. circuitmux_prev_active_circ_p(cmux, cmux->active_circuits_head);
  912. tor_assert(next_prev);
  913. *next_prev = circ;
  914. } else {
  915. /* The list was empty; this becomes the tail as well */
  916. cmux->active_circuits_tail = circ;
  917. }
  918. /* This becomes the new head of the list */
  919. cmux->active_circuits_head = circ;
  920. /* Policy-specific notification */
  921. if (cmux->policy &&
  922. cmux->policy->notify_circ_active) {
  923. /* Okay, we need to check the circuit for policy data now */
  924. hashent = circuitmux_find_map_entry(cmux, circ);
  925. /* We should have found something */
  926. tor_assert(hashent);
  927. /* Notify */
  928. cmux->policy->notify_circ_active(cmux, cmux->policy_data,
  929. circ, hashent->muxinfo.policy_data);
  930. }
  931. }
  932. /**
  933. * Make a circuit inactive; update active list and policy-specific info, but
  934. * we don't mess with the counters or hash table here.
  935. */
  936. static void
  937. circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ,
  938. cell_direction_t direction)
  939. {
  940. circuit_t **next_active = NULL, **prev_active = NULL;
  941. circuit_t **next_prev = NULL, **prev_next = NULL;
  942. circuitmux_t *circuit_cmux = NULL;
  943. chanid_circid_muxinfo_t *hashent = NULL;
  944. channel_t *chan = NULL;
  945. circid_t circ_id;
  946. int already_inactive;
  947. tor_assert(cmux);
  948. tor_assert(circ);
  949. tor_assert(direction == CELL_DIRECTION_OUT ||
  950. direction == CELL_DIRECTION_IN);
  951. /* Get the right set of active list links for this direction */
  952. if (direction == CELL_DIRECTION_OUT) {
  953. next_active = &(circ->next_active_on_n_chan);
  954. prev_active = &(circ->prev_active_on_n_chan);
  955. circuit_cmux = circ->n_mux;
  956. chan = circ->n_chan;
  957. circ_id = circ->n_circ_id;
  958. } else {
  959. next_active = &(TO_OR_CIRCUIT(circ)->next_active_on_p_chan);
  960. prev_active = &(TO_OR_CIRCUIT(circ)->prev_active_on_p_chan);
  961. circuit_cmux = TO_OR_CIRCUIT(circ)->p_mux;
  962. chan = TO_OR_CIRCUIT(circ)->p_chan;
  963. circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  964. }
  965. /* Assert that it is attached to this mux and a channel */
  966. tor_assert(cmux == circuit_cmux);
  967. tor_assert(chan != NULL);
  968. /*
  969. * Check if the circuit really was active; if it's inactive, the
  970. * next_active and prev_active pointers will be NULL and this circuit
  971. * will not be the head or tail of the list for this cmux.
  972. */
  973. already_inactive = (*prev_active == NULL && *next_active == NULL &&
  974. cmux->active_circuits_head != circ &&
  975. cmux->active_circuits_tail != circ);
  976. /* If we're already inactive, log a warning and finish */
  977. if (already_inactive) {
  978. log_warn(LD_CIRC,
  979. "Circuit %d on channel " U64_FORMAT " was already inactive",
  980. circ_id, U64_PRINTF_ARG(chan->global_identifier));
  981. return;
  982. }
  983. /* Remove from the list; first get next_prev and prev_next */
  984. if (*next_active) {
  985. /*
  986. * If there's a next circuit, its previous circuit becomes this
  987. * circuit's previous circuit.
  988. */
  989. next_prev = circuitmux_prev_active_circ_p(cmux, *next_active);
  990. } else {
  991. /* Else, the tail becomes this circuit's previous circuit */
  992. next_prev = &(cmux->active_circuits_tail);
  993. }
  994. /* Got next_prev, now prev_next */
  995. if (*prev_active) {
  996. /*
  997. * If there's a previous circuit, its next circuit becomes this circuit's
  998. * next circuit.
  999. */
  1000. prev_next = circuitmux_next_active_circ_p(cmux, *prev_active);
  1001. } else {
  1002. /* Else, the head becomes this circuit's next circuit */
  1003. prev_next = &(cmux->active_circuits_head);
  1004. }
  1005. /* Assert that we got sensible values for the next/prev pointers */
  1006. tor_assert(next_prev != NULL);
  1007. tor_assert(prev_next != NULL);
  1008. /* Update the next/prev pointers - this removes circ from the list */
  1009. *next_prev = *prev_active;
  1010. *prev_next = *next_active;
  1011. /* Now null out prev_active/next_active */
  1012. *prev_active = NULL;
  1013. *next_active = NULL;
  1014. /* Policy-specific notification */
  1015. if (cmux->policy &&
  1016. cmux->policy->notify_circ_inactive) {
  1017. /* Okay, we need to check the circuit for policy data now */
  1018. hashent = circuitmux_find_map_entry(cmux, circ);
  1019. /* We should have found something */
  1020. tor_assert(hashent);
  1021. /* Notify */
  1022. cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
  1023. circ, hashent->muxinfo.policy_data);
  1024. }
  1025. }
  1026. /**
  1027. * Clear the cell counter for a circuit on a circuitmux
  1028. */
  1029. void
  1030. circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
  1031. {
  1032. /* This is the same as setting the cell count to zero */
  1033. circuitmux_set_num_cells(cmux, circ, 0);
  1034. }
  1035. /**
  1036. * Set the cell counter for a circuit on a circuitmux
  1037. */
  1038. void
  1039. circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  1040. unsigned int n_cells)
  1041. {
  1042. chanid_circid_muxinfo_t *hashent = NULL;
  1043. tor_assert(cmux);
  1044. tor_assert(circ);
  1045. /* Search for this circuit's entry */
  1046. hashent = circuitmux_find_map_entry(cmux, circ);
  1047. /* Assert that we found one */
  1048. tor_assert(hashent);
  1049. /* Update cmux cell counter */
  1050. cmux->n_cells -= hashent->muxinfo.cell_count;
  1051. cmux->n_cells += n_cells;
  1052. /* Do we need to notify a cmux policy? */
  1053. if (cmux->policy && cmux->policy->notify_set_n_cells) {
  1054. /* Call notify_set_n_cells */
  1055. cmux->policy->notify_set_n_cells(cmux,
  1056. cmux->policy_data,
  1057. circ,
  1058. hashent->muxinfo.policy_data,
  1059. n_cells);
  1060. }
  1061. /*
  1062. * Update cmux active circuit counter: is the old cell count > 0 and the
  1063. * new cell count == 0 ?
  1064. */
  1065. if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
  1066. --(cmux->n_active_circuits);
  1067. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1068. /* Is the old cell count == 0 and the new cell count > 0 ? */
  1069. } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
  1070. ++(cmux->n_active_circuits);
  1071. circuitmux_make_circuit_active(cmux, circ, hashent->muxinfo.direction);
  1072. }
  1073. /* Update hash entry cell counter */
  1074. hashent->muxinfo.cell_count = n_cells;
  1075. }
  1076. /*
  1077. * Functions for channel code to call to get a circuit to transmit from or
  1078. * notify that cells have been transmitted.
  1079. */
  1080. void
  1081. circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  1082. unsigned int n_cells)
  1083. {
  1084. chanid_circid_muxinfo_t *hashent = NULL;
  1085. int becomes_inactive = 0;
  1086. tor_assert(cmux);
  1087. tor_assert(circ);
  1088. if (n_cells == 0) return;
  1089. /*
  1090. * To handle this, we have to:
  1091. *
  1092. * 1.) Adjust the circuit's cell counter in the cmux hash table
  1093. * 2.) Move the circuit to the tail of the active_circuits linked list
  1094. * for this cmux, or make the circuit inactive if the cell count
  1095. * went to zero.
  1096. * 3.) Call cmux->policy->notify_xmit_cells(), if any
  1097. */
  1098. /* Find the hash entry */
  1099. hashent = circuitmux_find_map_entry(cmux, circ);
  1100. /* Assert that we found one */
  1101. tor_assert(hashent);
  1102. /* Adjust the cell counter and assert that we had that many cells to send */
  1103. tor_assert(n_cells <= hashent->muxinfo.cell_count);
  1104. hashent->muxinfo.cell_count -= n_cells;
  1105. /* Do we need to make the circuit inactive? */
  1106. if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
  1107. /* If we aren't making it inactive later, move it to the tail of the list */
  1108. if (!becomes_inactive) {
  1109. circuitmux_move_active_circ_to_tail(cmux, circ,
  1110. hashent->muxinfo.direction);
  1111. }
  1112. /*
  1113. * We call notify_xmit_cells() before making the circuit inactive if needed,
  1114. * so the policy can always count on this coming in on an active circuit.
  1115. */
  1116. if (cmux->policy && cmux->policy->notify_xmit_cells) {
  1117. cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
  1118. hashent->muxinfo.policy_data,
  1119. n_cells);
  1120. }
  1121. /*
  1122. * Now make the circuit inactive if needed; this will call the policy's
  1123. * notify_circ_inactive() if present.
  1124. */
  1125. if (becomes_inactive) {
  1126. --(cmux->n_active_circuits);
  1127. circuitmux_make_circuit_inactive(cmux, circ, hashent->muxinfo.direction);
  1128. }
  1129. }