circuitmux.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "circuitmux.h"
  9. /*
  10. * Private typedefs for circuitmux.c
  11. */
  12. /*
  13. * Map of muxinfos for circuitmux_t to use; struct is defined below (name
  14. * of struct must match HT_HEAD line).
  15. */
  16. typedef struct chanid_circid_muxinfo_map chanid_circid_muxinfo_map_t;
  17. /*
  18. * Hash table entry (yeah, calling it chanid_circid_muxinfo_s seems to
  19. * break the hash table code).
  20. */
  21. typedef struct chanid_circid_muxinfo_t chanid_circid_muxinfo_t;
  22. /*
  23. * Anything the mux wants to store per-circuit in the map; right now just
  24. * a count of queued cells.
  25. */
  26. typedef struct circuit_muxinfo_s circuit_muxinfo_t;
  27. /*
  28. * Structures for circuitmux.c
  29. */
  30. /*
  31. * A circuitmux is a collection of circuits; it tracks which subset
  32. * of the attached circuits are 'active' (i.e., have cells available
  33. * to transmit) and how many cells on each. It expoes three distinct
  34. * interfaces to other components:
  35. *
  36. * To channels, which each have a circuitmux_t, the supported operations
  37. * are:
  38. *
  39. * circuitmux_flush_cells():
  40. *
  41. * Retrieve a cell from one of the active circuits, chosen according to
  42. * the circuitmux_t's cell selection policy.
  43. *
  44. * circuitmux_unlink_all():
  45. *
  46. * The channel is closing down, all circuits must be detached.
  47. *
  48. * To circuits, the exposed operations are:
  49. *
  50. * TODO
  51. *
  52. * To circuit selection policies, the exposed operations are:
  53. *
  54. * TODO
  55. *
  56. * General status inquiries?
  57. *
  58. */
  59. struct circuitmux_s {
  60. /* Keep count of attached, active circuits */
  61. unsigned int n_circuits, n_active_circuits;
  62. /*
  63. * Map from (channel ID, circuit ID) pairs to circuit_muxinfo_t
  64. */
  65. chanid_circid_muxinfo_map_t *chanid_circid_map;
  66. /*
  67. * Double-linked ring of circuits with queued cells waiting for room to
  68. * free up on this connection's outbuf. Every time we pull cells from
  69. * a circuit, we advance this pointer to the next circuit in the ring.
  70. */
  71. struct circuit_t *active_circuits;
  72. /*
  73. * Priority queue of cell_ewma_t for circuits with queued cells waiting
  74. * for room to free up on this connection's outbuf. Kept in heap order
  75. * according to EWMA.
  76. *
  77. * This is redundant with active_circuits; if we ever decide only to use
  78. * the cell_ewma algorithm for choosing circuits, we can remove
  79. * active_circuits.
  80. */
  81. smartlist_t *active_circuit_pqueue;
  82. /*
  83. * The tick on which the cell_ewma_ts in active_circuit_pqueue last had
  84. * their ewma values rescaled.
  85. */
  86. unsigned int active_circuit_pqueue_last_recalibrated;
  87. };
  88. /*
  89. * This struct holds whatever we want to store per attached circuit on a
  90. * circuitmux_t; right now, just the count of queued cells.
  91. */
  92. struct circuit_muxinfo_s {
  93. unsigned int cell_count;
  94. };
  95. /*
  96. * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
  97. * circuit.
  98. */
  99. struct chanid_circid_muxinfo_t {
  100. HT_ENTRY(chanid_circid_muxinfo_t) node;
  101. uint64_t chan_id;
  102. circid_t circ_id;
  103. circuit_muxinfo_t muxinfo;
  104. };
  105. /*
  106. * Static function declarations
  107. */
  108. static INLINE int
  109. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  110. chanid_circid_muxinfo_t *b);
  111. static INLINE unsigned int
  112. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
  113. /* Function definitions */
  114. /**
  115. * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
  116. * ID and circuit ID for a and b, and return less than, equal to, or greater
  117. * than zero appropriately.
  118. */
  119. static INLINE int
  120. chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
  121. chanid_circid_muxinfo_t *b)
  122. {
  123. return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
  124. }
  125. /**
  126. * Helper: return a hash based on circuit ID and channel ID in a.
  127. */
  128. static INLINE unsigned int
  129. chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
  130. {
  131. return (((unsigned int)(a->circ_id) << 8) ^
  132. ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
  133. ((unsigned int)(a->chan_id & 0xffffffff)));
  134. }
  135. /* Declare the struct chanid_circid_muxinfo_map type */
  136. HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);
  137. /* Emit a bunch of hash table stuff */
  138. HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  139. chanid_circid_entry_hash, chanid_circid_entries_eq);
  140. HT_GENERATE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
  141. chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
  142. malloc, realloc, free);
  143. /**
  144. * Allocate a new circuitmux_t
  145. */
  146. circuitmux_t *
  147. circuitmux_alloc(void)
  148. {
  149. circuitmux_t *rv = NULL;
  150. rv = tor_malloc_zero(sizeof(*rv));
  151. rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  152. HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
  153. return rv;
  154. }
  155. /**
  156. * Free a circuitmux_t; the circuits must be detached first with
  157. * circuitmux_detach_all_circuits().
  158. */
  159. void
  160. circuitmux_free(circuitmux_t *cmux)
  161. {
  162. if (!cmux) return;
  163. tor_assert(cmux->n_circuits == 0);
  164. tor_assert(cmux->n_active_circuits == 0);
  165. smartlist_free(cmux->active_circuit_pqueue);
  166. if (cmux->chanid_circid_map) {
  167. HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  168. tor_free(cmux->chanid_circid_map);
  169. }
  170. tor_free(cmux);
  171. }