scheduler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* * Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file scheduler.h
  5. * \brief Header file for scheduler*.c
  6. **/
  7. #ifndef TOR_SCHEDULER_H
  8. #define TOR_SCHEDULER_H
  9. #include "or.h"
  10. #include "channel.h"
  11. #include "testsupport.h"
  12. /*
  13. * A scheduler implementation is a collection of function pointers. If you
  14. * would like to add a new scheduler called foo, create scheduler_foo.c,
  15. * implement at least the mandatory ones, and implement get_foo_scheduler()
  16. * that returns a complete scheduler_t for your foo scheduler. See
  17. * scheduler_kist.c for an example.
  18. *
  19. * These function pointers SHOULD NOT be used anywhere outside of the
  20. * scheduling source files. The rest of Tor should communicate with the
  21. * scheduling system through the functions near the bottom of this file, and
  22. * those functions will call into the current scheduler implementation as
  23. * necessary.
  24. *
  25. * If your scheduler doesn't need to implement something (for example: it
  26. * doesn't create any state for itself, thus it has nothing to free when Tor
  27. * is shutting down), then set that function pointer to NULL.
  28. */
  29. typedef struct scheduler_s {
  30. /* (Optional) To be called when we want to prepare a scheduler for use.
  31. * Perhaps Tor just started and we are the lucky chosen scheduler, or
  32. * perhaps Tor is switching to this scheduler. No matter the case, this is
  33. * where we would prepare any state and initialize parameters. You might
  34. * think of this as the opposite of free_all(). */
  35. void (*init)(void);
  36. /* (Optional) To be called when we want to tell the scheduler to delete all
  37. * of its state (if any). Perhaps Tor is shutting down or perhaps we are
  38. * switching schedulers. */
  39. void (*free_all)(void);
  40. /* (Mandatory) Libevent controls the main event loop in Tor, and this is
  41. * where we register with libevent the next execution of run_sched_ev [which
  42. * ultimately calls run()]. */
  43. void (*schedule)(void);
  44. /* (Mandatory) This is the heart of a scheduler! This is where the
  45. * excitement happens! Here libevent has given us the chance to execute, and
  46. * we should do whatever we need to do in order to move some cells from
  47. * their circuit queues to output buffers in an intelligent manner. We
  48. * should do this quickly. When we are done, we'll try to schedule() ourself
  49. * if more work needs to be done to setup the next scehduling run. */
  50. void (*run)(void);
  51. /*
  52. * External event not related to the scheduler but that can influence it.
  53. */
  54. /* (Optional) To be called whenever Tor finds out about a new consensus.
  55. * First the scheduling system as a whole will react to the new consensus
  56. * and change the scheduler if needed. After that, whatever is the (possibly
  57. * new) scheduler will call this so it has the chance to react to the new
  58. * consensus too. If there's a consensus parameter that your scheduler wants
  59. * to keep an eye on, this is where you should check for it. */
  60. void (*on_new_consensus)(const networkstatus_t *old_c,
  61. const networkstatus_t *new_c);
  62. /* (Optional) To be called when a channel is being freed. Sometimes channels
  63. * go away (for example: the relay on the other end is shutting down). If
  64. * the scheduler keeps any channel-specific state and has memory to free
  65. * when channels go away, implement this and free it here. */
  66. void (*on_channel_free)(const channel_t *);
  67. /* (Optional) To be called whenever Tor is reloading configuration options.
  68. * For example: SIGHUP was issued and Tor is rereading its torrc. A
  69. * scheduler should use this as an opportunity to parse and cache torrc
  70. * options so that it doesn't have to call get_options() all the time. */
  71. void (*on_new_options)(void);
  72. } scheduler_t;
  73. /*****************************************************************************
  74. * Globally visible scheduler functions
  75. *
  76. * These functions are how the rest of Tor communicates with the scheduling
  77. * system.
  78. *****************************************************************************/
  79. void scheduler_init(void);
  80. void scheduler_free_all(void);
  81. void scheduler_conf_changed(void);
  82. void scheduler_notify_networkstatus_changed(const networkstatus_t *old_c,
  83. const networkstatus_t *new_c);
  84. MOCK_DECL(void, scheduler_release_channel, (channel_t *chan));
  85. /*
  86. * Ways for a channel to interact with the scheduling system. A channel only
  87. * really knows (i) whether or not it has cells it wants to send, and
  88. * (ii) whether or not it would like to write.
  89. */
  90. void scheduler_channel_wants_writes(channel_t *chan);
  91. MOCK_DECL(void, scheduler_channel_doesnt_want_writes, (channel_t *chan));
  92. MOCK_DECL(void, scheduler_channel_has_waiting_cells, (channel_t *chan));
  93. /*****************************************************************************
  94. * Private scheduler functions
  95. *
  96. * These functions are only visible to the scheduling system, the current
  97. * scheduler implementation, and tests.
  98. *****************************************************************************/
  99. #ifdef SCHEDULER_PRIVATE_
  100. /*********************************
  101. * Defined in scheduler.c
  102. *********************************/
  103. int scheduler_should_use_kist(void);
  104. smartlist_t *get_channels_pending(void);
  105. struct event *get_run_sched_ev(void);
  106. MOCK_DECL(int, scheduler_compare_channels,
  107. (const void *c1_v, const void *c2_v));
  108. #ifdef TOR_UNIT_TESTS
  109. extern smartlist_t *channels_pending;
  110. extern struct event *run_sched_ev;
  111. extern scheduler_t *scheduler;
  112. void scheduler_touch_channel(channel_t *chan);
  113. #endif /* TOR_UNIT_TESTS */
  114. /*********************************
  115. * Defined in scheduler_kist.c
  116. *********************************/
  117. /* Socke table entry which holds information of a channel's socket and kernel
  118. * TCP information. Only used by KIST. */
  119. typedef struct socket_table_ent_s {
  120. HT_ENTRY(socket_table_ent_s) node;
  121. const channel_t *chan;
  122. /* Amount written this scheduling run */
  123. uint64_t written;
  124. /* Amount that can be written this scheduling run */
  125. uint64_t limit;
  126. /* TCP info from the kernel */
  127. uint32_t cwnd;
  128. uint32_t unacked;
  129. uint32_t mss;
  130. uint32_t notsent;
  131. } socket_table_ent_t;
  132. typedef HT_HEAD(outbuf_table_s, outbuf_table_ent_s) outbuf_table_t;
  133. MOCK_DECL(int, channel_should_write_to_kernel,
  134. (outbuf_table_t *table, channel_t *chan));
  135. MOCK_DECL(void, channel_write_to_kernel, (channel_t *chan));
  136. MOCK_DECL(void, update_socket_info_impl, (socket_table_ent_t *ent));
  137. scheduler_t *get_kist_scheduler(void);
  138. int32_t kist_scheduler_run_interval(const networkstatus_t *ns);
  139. #ifdef TOR_UNIT_TESTS
  140. extern int32_t sched_run_interval;
  141. #endif /* TOR_UNIT_TESTS */
  142. /*********************************
  143. * Defined in scheduler_vanilla.c
  144. *********************************/
  145. scheduler_t *get_vanilla_scheduler(void);
  146. #endif /* SCHEDULER_PRIVATE_ */
  147. #endif /* TOR_SCHEDULER_H */