circuitmux.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* * Copyright (c) 2012-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file circuitmux.h
  5. * \brief Header file for circuitmux.c
  6. **/
  7. #ifndef TOR_CIRCUITMUX_H
  8. #define TOR_CIRCUITMUX_H
  9. #include "or.h"
  10. #include "testsupport.h"
  11. typedef struct circuitmux_policy_s circuitmux_policy_t;
  12. typedef struct circuitmux_policy_data_s circuitmux_policy_data_t;
  13. typedef struct circuitmux_policy_circ_data_s circuitmux_policy_circ_data_t;
  14. struct circuitmux_policy_s {
  15. /* Allocate cmux-wide policy-specific data */
  16. circuitmux_policy_data_t * (*alloc_cmux_data)(circuitmux_t *cmux);
  17. /* Free cmux-wide policy-specific data */
  18. void (*free_cmux_data)(circuitmux_t *cmux,
  19. circuitmux_policy_data_t *pol_data);
  20. /* Allocate circuit policy-specific data for a newly attached circuit */
  21. circuitmux_policy_circ_data_t *
  22. (*alloc_circ_data)(circuitmux_t *cmux,
  23. circuitmux_policy_data_t *pol_data,
  24. circuit_t *circ,
  25. cell_direction_t direction,
  26. unsigned int cell_count);
  27. /* Free circuit policy-specific data */
  28. void (*free_circ_data)(circuitmux_t *cmux,
  29. circuitmux_policy_data_t *pol_data,
  30. circuit_t *circ,
  31. circuitmux_policy_circ_data_t *pol_circ_data);
  32. /* Notify that a circuit has become active/inactive */
  33. void (*notify_circ_active)(circuitmux_t *cmux,
  34. circuitmux_policy_data_t *pol_data,
  35. circuit_t *circ,
  36. circuitmux_policy_circ_data_t *pol_circ_data);
  37. void (*notify_circ_inactive)(circuitmux_t *cmux,
  38. circuitmux_policy_data_t *pol_data,
  39. circuit_t *circ,
  40. circuitmux_policy_circ_data_t *pol_circ_data);
  41. /* Notify of arriving/transmitted cells on a circuit */
  42. void (*notify_set_n_cells)(circuitmux_t *cmux,
  43. circuitmux_policy_data_t *pol_data,
  44. circuit_t *circ,
  45. circuitmux_policy_circ_data_t *pol_circ_data,
  46. unsigned int n_cells);
  47. void (*notify_xmit_cells)(circuitmux_t *cmux,
  48. circuitmux_policy_data_t *pol_data,
  49. circuit_t *circ,
  50. circuitmux_policy_circ_data_t *pol_circ_data,
  51. unsigned int n_cells);
  52. /* Choose a circuit */
  53. circuit_t * (*pick_active_circuit)(circuitmux_t *cmux,
  54. circuitmux_policy_data_t *pol_data);
  55. /* Optional: channel comparator for use by the scheduler */
  56. int (*cmp_cmux)(circuitmux_t *cmux_1, circuitmux_policy_data_t *pol_data_1,
  57. circuitmux_t *cmux_2, circuitmux_policy_data_t *pol_data_2);
  58. };
  59. /*
  60. * Circuitmux policy implementations can subclass this to store circuitmux-
  61. * wide data; it just has the magic number in the base struct.
  62. */
  63. struct circuitmux_policy_data_s {
  64. uint32_t magic;
  65. };
  66. /*
  67. * Circuitmux policy implementations can subclass this to store circuit-
  68. * specific data; it just has the magic number in the base struct.
  69. */
  70. struct circuitmux_policy_circ_data_s {
  71. uint32_t magic;
  72. };
  73. /*
  74. * Upcast #defines for the above types
  75. */
  76. /**
  77. * Convert a circuitmux_policy_data_t subtype to a circuitmux_policy_data_t.
  78. */
  79. #define TO_CMUX_POL_DATA(x) (&((x)->base_))
  80. /**
  81. * Convert a circuitmux_policy_circ_data_t subtype to a
  82. * circuitmux_policy_circ_data_t.
  83. */
  84. #define TO_CMUX_POL_CIRC_DATA(x) (&((x)->base_))
  85. /* Consistency check */
  86. void circuitmux_assert_okay(circuitmux_t *cmux);
  87. /* Create/destroy */
  88. circuitmux_t * circuitmux_alloc(void);
  89. void circuitmux_detach_all_circuits(circuitmux_t *cmux,
  90. smartlist_t *detached_out);
  91. void circuitmux_free(circuitmux_t *cmux);
  92. /* Policy control */
  93. void circuitmux_clear_policy(circuitmux_t *cmux);
  94. MOCK_DECL(const circuitmux_policy_t *,
  95. circuitmux_get_policy, (circuitmux_t *cmux));
  96. void circuitmux_set_policy(circuitmux_t *cmux,
  97. const circuitmux_policy_t *pol);
  98. /* Status inquiries */
  99. cell_direction_t circuitmux_attached_circuit_direction(
  100. circuitmux_t *cmux,
  101. circuit_t *circ);
  102. int circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ);
  103. int circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ);
  104. unsigned int circuitmux_num_cells_for_circuit(circuitmux_t *cmux,
  105. circuit_t *circ);
  106. MOCK_DECL(unsigned int, circuitmux_num_cells, (circuitmux_t *cmux));
  107. unsigned int circuitmux_num_circuits(circuitmux_t *cmux);
  108. unsigned int circuitmux_num_active_circuits(circuitmux_t *cmux);
  109. /* Debuging interface - slow. */
  110. int64_t circuitmux_count_queued_destroy_cells(const channel_t *chan,
  111. const circuitmux_t *cmux);
  112. /* Channel interface */
  113. circuit_t * circuitmux_get_first_active_circuit(circuitmux_t *cmux,
  114. cell_queue_t **destroy_queue_out);
  115. void circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
  116. unsigned int n_cells);
  117. void circuitmux_notify_xmit_destroy(circuitmux_t *cmux);
  118. /* Circuit interface */
  119. MOCK_DECL(void, circuitmux_attach_circuit, (circuitmux_t *cmux,
  120. circuit_t *circ,
  121. cell_direction_t direction));
  122. MOCK_DECL(void, circuitmux_detach_circuit,
  123. (circuitmux_t *cmux, circuit_t *circ));
  124. void circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ);
  125. void circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
  126. unsigned int n_cells);
  127. void circuitmux_append_destroy_cell(channel_t *chan,
  128. circuitmux_t *cmux, circid_t circ_id,
  129. uint8_t reason);
  130. void circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux,
  131. channel_t *chan);
  132. /* Optional interchannel comparisons for scheduling */
  133. MOCK_DECL(int, circuitmux_compare_muxes,
  134. (circuitmux_t *cmux_1, circuitmux_t *cmux_2));
  135. #endif /* TOR_CIRCUITMUX_H */