scheduler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /* Copyright (c) 2013-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "core/or/or.h"
  4. #include "app/config/config.h"
  5. #include "core/or/scheduler_sys.h"
  6. #include "lib/evloop/compat_libevent.h"
  7. #define SCHEDULER_PRIVATE_
  8. #define SCHEDULER_KIST_PRIVATE
  9. #include "core/or/scheduler.h"
  10. #include "core/mainloop/mainloop.h"
  11. #include "lib/buf/buffers.h"
  12. #define TOR_CHANNEL_INTERNAL_
  13. #include "core/or/channeltls.h"
  14. #include "lib/evloop/compat_libevent.h"
  15. #include "core/or/or_connection_st.h"
  16. /**
  17. * \file scheduler.c
  18. * \brief Channel scheduling system: decides which channels should send and
  19. * receive when.
  20. *
  21. * This module is the global/common parts of the scheduling system. This system
  22. * is what decides what channels get to send cells on their circuits and when.
  23. *
  24. * Terms:
  25. * - "Scheduling system": the collection of scheduler*.{h,c} files and their
  26. * aggregate behavior.
  27. * - "Scheduler implementation": a scheduler_t. The scheduling system has one
  28. * active scheduling implementation at a time.
  29. *
  30. * In this file you will find state that any scheduler implementation can have
  31. * access to as well as the functions the rest of Tor uses to interact with the
  32. * scheduling system.
  33. *
  34. * The earliest versions of Tor approximated a kind of round-robin system
  35. * among active connections, but only approximated it. It would only consider
  36. * one connection (roughly equal to a channel in today's terms) at a time, and
  37. * thus could only prioritize circuits against others on the same connection.
  38. *
  39. * Then in response to the KIST paper[0], Tor implemented a global
  40. * circuit scheduler. It was supposed to prioritize circuits across many
  41. * channels, but wasn't effective. It is preserved in scheduler_vanilla.c.
  42. *
  43. * [0]: http://www.robgjansen.com/publications/kist-sec2014.pdf
  44. *
  45. * Then we actually got around to implementing KIST for real. We decided to
  46. * modularize the scheduler so new ones can be implemented. You can find KIST
  47. * in scheduler_kist.c.
  48. *
  49. * Channels have one of four scheduling states based on whether or not they
  50. * have cells to send and whether or not they are able to send.
  51. *
  52. * <ol>
  53. * <li>
  54. * Not open for writes, no cells to send.
  55. * <ul><li> Not much to do here, and the channel will have scheduler_state
  56. * == SCHED_CHAN_IDLE
  57. * <li> Transitions from:
  58. * <ul>
  59. * <li>Open for writes/has cells by simultaneously draining all circuit
  60. * queues and filling the output buffer.
  61. * </ul>
  62. * <li> Transitions to:
  63. * <ul>
  64. * <li> Not open for writes/has cells by arrival of cells on an attached
  65. * circuit (this would be driven from append_cell_to_circuit_queue())
  66. * <li> Open for writes/no cells by a channel type specific path;
  67. * driven from connection_or_flushed_some() for channel_tls_t.
  68. * </ul>
  69. * </ul>
  70. *
  71. * <li> Open for writes, no cells to send
  72. * <ul>
  73. * <li>Not much here either; this will be the state an idle but open
  74. * channel can be expected to settle in. It will have scheduler_state
  75. * == SCHED_CHAN_WAITING_FOR_CELLS
  76. * <li> Transitions from:
  77. * <ul>
  78. * <li>Not open for writes/no cells by flushing some of the output
  79. * buffer.
  80. * <li>Open for writes/has cells by the scheduler moving cells from
  81. * circuit queues to channel output queue, but not having enough
  82. * to fill the output queue.
  83. * </ul>
  84. * <li> Transitions to:
  85. * <ul>
  86. * <li>Open for writes/has cells by arrival of new cells on an attached
  87. * circuit, in append_cell_to_circuit_queue()
  88. * </ul>
  89. * </ul>
  90. *
  91. * <li>Not open for writes, cells to send
  92. * <ul>
  93. * <li>This is the state of a busy circuit limited by output bandwidth;
  94. * cells have piled up in the circuit queues waiting to be relayed.
  95. * The channel will have scheduler_state == SCHED_CHAN_WAITING_TO_WRITE.
  96. * <li> Transitions from:
  97. * <ul>
  98. * <li>Not open for writes/no cells by arrival of cells on an attached
  99. * circuit
  100. * <li>Open for writes/has cells by filling an output buffer without
  101. * draining all cells from attached circuits
  102. * </ul>
  103. * <li> Transitions to:
  104. * <ul>
  105. * <li>Opens for writes/has cells by draining some of the output buffer
  106. * via the connection_or_flushed_some() path (for channel_tls_t).
  107. * </ul>
  108. * </ul>
  109. *
  110. * <li>Open for writes, cells to send
  111. * <ul>
  112. * <li>This connection is ready to relay some cells and waiting for
  113. * the scheduler to choose it. The channel will have scheduler_state ==
  114. * SCHED_CHAN_PENDING.
  115. * <li>Transitions from:
  116. * <ul>
  117. * <li>Not open for writes/has cells by the connection_or_flushed_some()
  118. * path
  119. * <li>Open for writes/no cells by the append_cell_to_circuit_queue()
  120. * path
  121. * </ul>
  122. * <li> Transitions to:
  123. * <ul>
  124. * <li>Not open for writes/no cells by draining all circuit queues and
  125. * simultaneously filling the output buffer.
  126. * <li>Not open for writes/has cells by writing enough cells to fill the
  127. * output buffer
  128. * <li>Open for writes/no cells by draining all attached circuit queues
  129. * without also filling the output buffer
  130. * </ul>
  131. * </ul>
  132. * </ol>
  133. *
  134. * Other event-driven parts of the code move channels between these scheduling
  135. * states by calling scheduler functions. The scheduling system builds up a
  136. * list of channels in the SCHED_CHAN_PENDING state that the scheduler
  137. * implementation should then use when it runs. Scheduling implementations need
  138. * to properly update channel states during their scheduler_t->run() function
  139. * as that is the only opportunity for channels to move from SCHED_CHAN_PENDING
  140. * to any other state.
  141. *
  142. * The remainder of this file is a small amount of state that any scheduler
  143. * implementation should have access to, and the functions the rest of Tor uses
  144. * to interact with the scheduling system.
  145. */
  146. /*****************************************************************************
  147. * Scheduling system state
  148. *
  149. * State that can be accessed from any scheduler implementation (but not
  150. * outside the scheduling system)
  151. *****************************************************************************/
  152. /** DOCDOC */
  153. STATIC const scheduler_t *the_scheduler = NULL;
  154. /**
  155. * We keep a list of channels that are pending - i.e, have cells to write
  156. * and can accept them to send. The enum scheduler_state in channel_t
  157. * is reserved for our use.
  158. *
  159. * Priority queue of channels that can write and have cells (pending work)
  160. */
  161. STATIC smartlist_t *channels_pending = NULL;
  162. /**
  163. * This event runs the scheduler from its callback, and is manually
  164. * activated whenever a channel enters open for writes/cells to send.
  165. */
  166. STATIC struct mainloop_event_t *run_sched_ev = NULL;
  167. static int have_logged_kist_suddenly_disabled = 0;
  168. /*****************************************************************************
  169. * Scheduling system static function definitions
  170. *
  171. * Functions that can only be accessed from this file.
  172. *****************************************************************************/
  173. /** Return a human readable string for the given scheduler type. */
  174. static const char *
  175. get_scheduler_type_string(scheduler_types_t type)
  176. {
  177. switch (type) {
  178. case SCHEDULER_VANILLA:
  179. return "Vanilla";
  180. case SCHEDULER_KIST:
  181. return "KIST";
  182. case SCHEDULER_KIST_LITE:
  183. return "KISTLite";
  184. case SCHEDULER_NONE:
  185. /* fallthrough */
  186. default:
  187. tor_assert_unreached();
  188. return "(N/A)";
  189. }
  190. }
  191. /**
  192. * Scheduler event callback; this should get triggered once per event loop
  193. * if any scheduling work was created during the event loop.
  194. */
  195. static void
  196. scheduler_evt_callback(mainloop_event_t *event, void *arg)
  197. {
  198. (void) event;
  199. (void) arg;
  200. log_debug(LD_SCHED, "Scheduler event callback called");
  201. /* Run the scheduler. This is a mandatory function. */
  202. /* We might as well assert on this. If this function doesn't exist, no cells
  203. * are getting scheduled. Things are very broken. scheduler_t says the run()
  204. * function is mandatory. */
  205. tor_assert(the_scheduler->run);
  206. the_scheduler->run();
  207. /* Schedule itself back in if it has more work. */
  208. /* Again, might as well assert on this mandatory scheduler_t function. If it
  209. * doesn't exist, there's no way to tell libevent to run the scheduler again
  210. * in the future. */
  211. tor_assert(the_scheduler->schedule);
  212. the_scheduler->schedule();
  213. }
  214. /** Using the global options, select the scheduler we should be using. */
  215. static void
  216. select_scheduler(void)
  217. {
  218. scheduler_t *new_scheduler = NULL;
  219. #ifdef TOR_UNIT_TESTS
  220. /* This is hella annoying to set in the options for every test that passes
  221. * through the scheduler and there are many so if we don't explicitly have
  222. * a list of types set, just put the vanilla one. */
  223. if (get_options()->SchedulerTypes_ == NULL) {
  224. the_scheduler = get_vanilla_scheduler();
  225. return;
  226. }
  227. #endif /* defined(TOR_UNIT_TESTS) */
  228. /* This list is ordered that is first entry has the first priority. Thus, as
  229. * soon as we find a scheduler type that we can use, we use it and stop. */
  230. SMARTLIST_FOREACH_BEGIN(get_options()->SchedulerTypes_, int *, type) {
  231. switch (*type) {
  232. case SCHEDULER_VANILLA:
  233. new_scheduler = get_vanilla_scheduler();
  234. goto end;
  235. case SCHEDULER_KIST:
  236. if (!scheduler_can_use_kist()) {
  237. #ifdef HAVE_KIST_SUPPORT
  238. if (!have_logged_kist_suddenly_disabled) {
  239. /* We should only log this once in most cases. If it was the kernel
  240. * losing support for kist that caused scheduler_can_use_kist() to
  241. * return false, then this flag makes sure we only log this message
  242. * once. If it was the consensus that switched from "yes use kist"
  243. * to "no don't use kist", then we still set the flag so we log
  244. * once, but we unset the flag elsewhere if we ever can_use_kist()
  245. * again.
  246. */
  247. have_logged_kist_suddenly_disabled = 1;
  248. log_notice(LD_SCHED, "Scheduler type KIST has been disabled by "
  249. "the consensus or no kernel support.");
  250. }
  251. #else /* !defined(HAVE_KIST_SUPPORT) */
  252. log_info(LD_SCHED, "Scheduler type KIST not built in");
  253. #endif /* defined(HAVE_KIST_SUPPORT) */
  254. continue;
  255. }
  256. /* This flag will only get set in one of two cases:
  257. * 1 - the kernel lost support for kist. In that case, we don't expect to
  258. * ever end up here
  259. * 2 - the consensus went from "yes use kist" to "no don't use kist".
  260. * We might end up here if the consensus changes back to "yes", in which
  261. * case we might want to warn the user again if it goes back to "no"
  262. * yet again. Thus we unset the flag */
  263. have_logged_kist_suddenly_disabled = 0;
  264. new_scheduler = get_kist_scheduler();
  265. scheduler_kist_set_full_mode();
  266. goto end;
  267. case SCHEDULER_KIST_LITE:
  268. new_scheduler = get_kist_scheduler();
  269. scheduler_kist_set_lite_mode();
  270. goto end;
  271. case SCHEDULER_NONE:
  272. /* fallthrough */
  273. default:
  274. /* Our option validation should have caught this. */
  275. tor_assert_unreached();
  276. }
  277. } SMARTLIST_FOREACH_END(type);
  278. end:
  279. if (new_scheduler == NULL) {
  280. log_err(LD_SCHED, "Tor was unable to select a scheduler type. Please "
  281. "make sure Schedulers is correctly configured with "
  282. "what Tor does support.");
  283. /* We weren't able to choose a scheduler which means that none of the ones
  284. * set in Schedulers are supported or usable. We will respect the user
  285. * wishes of using what it has been configured and don't do a sneaky
  286. * fallback. Because this can be changed at runtime, we have to stop tor
  287. * right now. */
  288. exit(1); // XXXX bad exit
  289. }
  290. /* Set the chosen scheduler. */
  291. the_scheduler = new_scheduler;
  292. }
  293. /**
  294. * Helper function called from a few different places. It changes the
  295. * scheduler implementation, if necessary. And if it did, it then tells the
  296. * old one to free its state and the new one to initialize.
  297. */
  298. static void
  299. set_scheduler(void)
  300. {
  301. const scheduler_t *old_scheduler = the_scheduler;
  302. scheduler_types_t old_scheduler_type = SCHEDULER_NONE;
  303. /* We keep track of the type in order to log only if the type switched. We
  304. * can't just use the scheduler pointers because KIST and KISTLite share the
  305. * same object. */
  306. if (the_scheduler) {
  307. old_scheduler_type = the_scheduler->type;
  308. }
  309. /* From the options, select the scheduler type to set. */
  310. select_scheduler();
  311. tor_assert(the_scheduler);
  312. /* We look at the pointer difference in case the old sched and new sched
  313. * share the same scheduler object, as is the case with KIST and KISTLite. */
  314. if (old_scheduler != the_scheduler) {
  315. /* Allow the old scheduler to clean up, if needed. */
  316. if (old_scheduler && old_scheduler->free_all) {
  317. old_scheduler->free_all();
  318. }
  319. /* Initialize the new scheduler. */
  320. if (the_scheduler->init) {
  321. the_scheduler->init();
  322. }
  323. }
  324. /* Finally we notice log if we switched schedulers. We use the type in case
  325. * two schedulers share a scheduler object. */
  326. if (old_scheduler_type != the_scheduler->type) {
  327. log_info(LD_CONFIG, "Scheduler type %s has been enabled.",
  328. get_scheduler_type_string(the_scheduler->type));
  329. }
  330. }
  331. /*****************************************************************************
  332. * Scheduling system private function definitions
  333. *
  334. * Functions that can only be accessed from scheduler*.c
  335. *****************************************************************************/
  336. /** Returns human readable string for the given channel scheduler state. */
  337. const char *
  338. get_scheduler_state_string(int scheduler_state)
  339. {
  340. switch (scheduler_state) {
  341. case SCHED_CHAN_IDLE:
  342. return "IDLE";
  343. case SCHED_CHAN_WAITING_FOR_CELLS:
  344. return "WAITING_FOR_CELLS";
  345. case SCHED_CHAN_WAITING_TO_WRITE:
  346. return "WAITING_TO_WRITE";
  347. case SCHED_CHAN_PENDING:
  348. return "PENDING";
  349. default:
  350. return "(invalid)";
  351. }
  352. }
  353. /** Helper that logs channel scheduler_state changes. Use this instead of
  354. * setting scheduler_state directly. */
  355. void
  356. scheduler_set_channel_state(channel_t *chan, int new_state)
  357. {
  358. log_debug(LD_SCHED, "chan %" PRIu64 " changed from scheduler state %s to %s",
  359. chan->global_identifier,
  360. get_scheduler_state_string(chan->scheduler_state),
  361. get_scheduler_state_string(new_state));
  362. chan->scheduler_state = new_state;
  363. }
  364. /** Return the pending channel list. */
  365. smartlist_t *
  366. get_channels_pending(void)
  367. {
  368. return channels_pending;
  369. }
  370. /** Comparison function to use when sorting pending channels. */
  371. MOCK_IMPL(int,
  372. scheduler_compare_channels, (const void *c1_v, const void *c2_v))
  373. {
  374. const channel_t *c1 = NULL, *c2 = NULL;
  375. /* These are a workaround for -Wbad-function-cast throwing a fit */
  376. const circuitmux_policy_t *p1, *p2;
  377. uintptr_t p1_i, p2_i;
  378. tor_assert(c1_v);
  379. tor_assert(c2_v);
  380. c1 = (const channel_t *)(c1_v);
  381. c2 = (const channel_t *)(c2_v);
  382. if (c1 != c2) {
  383. if (circuitmux_get_policy(c1->cmux) ==
  384. circuitmux_get_policy(c2->cmux)) {
  385. /* Same cmux policy, so use the mux comparison */
  386. return circuitmux_compare_muxes(c1->cmux, c2->cmux);
  387. } else {
  388. /*
  389. * Different policies; not important to get this edge case perfect
  390. * because the current code never actually gives different channels
  391. * different cmux policies anyway. Just use this arbitrary but
  392. * definite choice.
  393. */
  394. p1 = circuitmux_get_policy(c1->cmux);
  395. p2 = circuitmux_get_policy(c2->cmux);
  396. p1_i = (uintptr_t)p1;
  397. p2_i = (uintptr_t)p2;
  398. return (p1_i < p2_i) ? -1 : 1;
  399. }
  400. } else {
  401. /* c1 == c2, so always equal */
  402. return 0;
  403. }
  404. }
  405. /*****************************************************************************
  406. * Scheduling system global functions
  407. *
  408. * Functions that can be accessed from anywhere in Tor.
  409. *****************************************************************************/
  410. /**
  411. * This is how the scheduling system is notified of Tor's configuration
  412. * changing. For example: a SIGHUP was issued.
  413. */
  414. void
  415. scheduler_conf_changed(void)
  416. {
  417. /* Let the scheduler decide what it should do. */
  418. set_scheduler();
  419. /* Then tell the (possibly new) scheduler that we have new options. */
  420. if (the_scheduler->on_new_options) {
  421. the_scheduler->on_new_options();
  422. }
  423. }
  424. /**
  425. * Whenever we get a new consensus, this function is called.
  426. */
  427. void
  428. scheduler_notify_networkstatus_changed(void)
  429. {
  430. /* Maybe the consensus param made us change the scheduler. */
  431. set_scheduler();
  432. /* Then tell the (possibly new) scheduler that we have a new consensus */
  433. if (the_scheduler->on_new_consensus) {
  434. the_scheduler->on_new_consensus();
  435. }
  436. }
  437. /**
  438. * Free everything scheduling-related from main.c. Note this is only called
  439. * when Tor is shutting down, while scheduler_t->free_all() is called both when
  440. * Tor is shutting down and when we are switching schedulers.
  441. */
  442. void
  443. scheduler_free_all(void)
  444. {
  445. log_debug(LD_SCHED, "Shutting down scheduler");
  446. if (run_sched_ev) {
  447. mainloop_event_free(run_sched_ev);
  448. run_sched_ev = NULL;
  449. }
  450. if (channels_pending) {
  451. /* We don't have ownership of the objects in this list. */
  452. smartlist_free(channels_pending);
  453. channels_pending = NULL;
  454. }
  455. if (the_scheduler && the_scheduler->free_all) {
  456. the_scheduler->free_all();
  457. }
  458. the_scheduler = NULL;
  459. }
  460. /** Mark a channel as no longer ready to accept writes.
  461. *
  462. * Possible state changes:
  463. * - SCHED_CHAN_PENDING -> SCHED_CHAN_WAITING_TO_WRITE
  464. * - SCHED_CHAN_WAITING_FOR_CELLS -> SCHED_CHAN_IDLE
  465. */
  466. MOCK_IMPL(void,
  467. scheduler_channel_doesnt_want_writes,(channel_t *chan))
  468. {
  469. IF_BUG_ONCE(!chan) {
  470. return;
  471. }
  472. IF_BUG_ONCE(!channels_pending) {
  473. return;
  474. }
  475. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  476. /*
  477. * It has cells but no longer can write, so it becomes
  478. * SCHED_CHAN_WAITING_TO_WRITE. It's in channels_pending, so we
  479. * should remove it from the list.
  480. */
  481. smartlist_pqueue_remove(channels_pending,
  482. scheduler_compare_channels,
  483. offsetof(channel_t, sched_heap_idx),
  484. chan);
  485. scheduler_set_channel_state(chan, SCHED_CHAN_WAITING_TO_WRITE);
  486. } else if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  487. /*
  488. * It does not have cells and no longer can write, so it becomes
  489. * SCHED_CHAN_IDLE.
  490. */
  491. scheduler_set_channel_state(chan, SCHED_CHAN_IDLE);
  492. }
  493. }
  494. /** Mark a channel as having waiting cells.
  495. *
  496. * Possible state changes:
  497. * - SCHED_CHAN_WAITING_FOR_CELLS -> SCHED_CHAN_PENDING
  498. * - SCHED_CHAN_IDLE -> SCHED_CHAN_WAITING_TO_WRITE
  499. */
  500. MOCK_IMPL(void,
  501. scheduler_channel_has_waiting_cells,(channel_t *chan))
  502. {
  503. IF_BUG_ONCE(!chan) {
  504. return;
  505. }
  506. IF_BUG_ONCE(!channels_pending) {
  507. return;
  508. }
  509. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  510. /*
  511. * It is able to write and now has cells, so it becomes
  512. * SCHED_CHAN_PENDING. It must be added to the channels_pending
  513. * list.
  514. */
  515. scheduler_set_channel_state(chan, SCHED_CHAN_PENDING);
  516. if (!SCHED_BUG(chan->sched_heap_idx != -1, chan)) {
  517. smartlist_pqueue_add(channels_pending,
  518. scheduler_compare_channels,
  519. offsetof(channel_t, sched_heap_idx),
  520. chan);
  521. }
  522. /* If we made a channel pending, we potentially have scheduling work to
  523. * do. */
  524. the_scheduler->schedule();
  525. } else if (chan->scheduler_state == SCHED_CHAN_IDLE) {
  526. /*
  527. * It is not able to write but now has cells, so it becomes
  528. * SCHED_CHAN_WAITING_TO_WRITE.
  529. */
  530. scheduler_set_channel_state(chan, SCHED_CHAN_WAITING_TO_WRITE);
  531. }
  532. }
  533. /** Add the scheduler event to the set of pending events with next_run being
  534. * the longest time libevent should wait before triggering the event. */
  535. void
  536. scheduler_ev_add(const struct timeval *next_run)
  537. {
  538. tor_assert(run_sched_ev);
  539. tor_assert(next_run);
  540. if (BUG(mainloop_event_schedule(run_sched_ev, next_run) < 0)) {
  541. log_warn(LD_SCHED, "Adding to libevent failed. Next run time was set to: "
  542. "%ld.%06ld", next_run->tv_sec, (long)next_run->tv_usec);
  543. return;
  544. }
  545. }
  546. /** Make the scheduler event active with the given flags. */
  547. void
  548. scheduler_ev_active(void)
  549. {
  550. tor_assert(run_sched_ev);
  551. mainloop_event_activate(run_sched_ev);
  552. }
  553. /*
  554. * Initialize any global memory needed by the scheduler. In order to use the
  555. * scheduler, you must still tell it when the configuration from config.c is
  556. * ready with scheduler_conf_changed(), and attach the mainloop event with
  557. * scheduler_attach_mainloop(). Note this is only called when Tor is starting
  558. * up, while scheduler_t->init() is called when we are switching schedulers.
  559. */
  560. void
  561. scheduler_init(void)
  562. {
  563. log_debug(LD_SCHED, "Initting scheduler memory");
  564. channels_pending = smartlist_new();
  565. }
  566. /*
  567. * Create and attach a new mainloop event.
  568. */
  569. void
  570. scheduler_attach_mainloop(void)
  571. {
  572. // Two '!' because we really do want to check if the pointer is non-NULL
  573. IF_BUG_ONCE(!!run_sched_ev) {
  574. log_warn(LD_SCHED, "We should not already have a libevent scheduler event."
  575. "I'll clean the old one up, but this is odd.");
  576. mainloop_event_free(run_sched_ev);
  577. run_sched_ev = NULL;
  578. }
  579. run_sched_ev = mainloop_event_new(scheduler_evt_callback, NULL);
  580. }
  581. /*
  582. * If a channel is going away, this is how the scheduling system is informed
  583. * so it can do any freeing necessary. This ultimately calls
  584. * scheduler_t->on_channel_free() so the current scheduler can release any
  585. * state specific to this channel.
  586. */
  587. MOCK_IMPL(void,
  588. scheduler_release_channel,(channel_t *chan))
  589. {
  590. IF_BUG_ONCE(!chan) {
  591. return;
  592. }
  593. IF_BUG_ONCE(!channels_pending) {
  594. return;
  595. }
  596. /* Try to remove the channel from the pending list regardless of its
  597. * scheduler state. We can release a channel in many places in the tor code
  598. * so we can't rely on the channel state (PENDING) to remove it from the
  599. * list.
  600. *
  601. * For instance, the channel can change state from OPEN to CLOSING while
  602. * being handled in the scheduler loop leading to the channel being in
  603. * PENDING state but not in the pending list. Furthermore, we release the
  604. * channel when it changes state to close and a second time when we free it.
  605. * Not ideal at all but for now that is the way it is. */
  606. if (chan->sched_heap_idx != -1) {
  607. smartlist_pqueue_remove(channels_pending,
  608. scheduler_compare_channels,
  609. offsetof(channel_t, sched_heap_idx),
  610. chan);
  611. }
  612. if (the_scheduler->on_channel_free) {
  613. the_scheduler->on_channel_free(chan);
  614. }
  615. scheduler_set_channel_state(chan, SCHED_CHAN_IDLE);
  616. }
  617. /** Mark a channel as ready to accept writes.
  618. * Possible state changes:
  619. *
  620. * - SCHED_CHAN_WAITING_TO_WRITE -> SCHED_CHAN_PENDING
  621. * - SCHED_CHAN_IDLE -> SCHED_CHAN_WAITING_FOR_CELLS
  622. */
  623. void
  624. scheduler_channel_wants_writes(channel_t *chan)
  625. {
  626. IF_BUG_ONCE(!chan) {
  627. return;
  628. }
  629. IF_BUG_ONCE(!channels_pending) {
  630. return;
  631. }
  632. if (chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE) {
  633. /*
  634. * It has cells and can now write, so it becomes
  635. * SCHED_CHAN_PENDING. It must be added to the channels_pending
  636. * list.
  637. */
  638. scheduler_set_channel_state(chan, SCHED_CHAN_PENDING);
  639. if (!SCHED_BUG(chan->sched_heap_idx != -1, chan)) {
  640. smartlist_pqueue_add(channels_pending,
  641. scheduler_compare_channels,
  642. offsetof(channel_t, sched_heap_idx),
  643. chan);
  644. }
  645. /* We just made a channel pending, we have scheduling work to do. */
  646. the_scheduler->schedule();
  647. } else if (chan->scheduler_state == SCHED_CHAN_IDLE) {
  648. /*
  649. * It does not have cells but can now write, so it becomes
  650. * SCHED_CHAN_WAITING_FOR_CELLS.
  651. */
  652. scheduler_set_channel_state(chan, SCHED_CHAN_WAITING_FOR_CELLS);
  653. }
  654. }
  655. /* Log warn the given channel and extra scheduler context as well. This is
  656. * used by SCHED_BUG() in order to be able to extract as much information as
  657. * we can when we hit a bug. Channel chan can be NULL. */
  658. void
  659. scheduler_bug_occurred(const channel_t *chan)
  660. {
  661. char buf[128];
  662. if (chan != NULL) {
  663. const size_t outbuf_len =
  664. buf_datalen(TO_CONN(BASE_CHAN_TO_TLS((channel_t *) chan)->conn)->outbuf);
  665. tor_snprintf(buf, sizeof(buf),
  666. "Channel %" PRIu64 " in state %s and scheduler state %s."
  667. " Num cells on cmux: %d. Connection outbuf len: %lu.",
  668. chan->global_identifier,
  669. channel_state_to_string(chan->state),
  670. get_scheduler_state_string(chan->scheduler_state),
  671. circuitmux_num_cells(chan->cmux),
  672. (unsigned long)outbuf_len);
  673. }
  674. {
  675. char *msg;
  676. /* Rate limit every 60 seconds. If we start seeing this every 60 sec, we
  677. * know something is stuck/wrong. It *should* be loud but not too much. */
  678. static ratelim_t rlimit = RATELIM_INIT(60);
  679. if ((msg = rate_limit_log(&rlimit, approx_time()))) {
  680. log_warn(LD_BUG, "%s Num pending channels: %d. "
  681. "Channel in pending list: %s.%s",
  682. (chan != NULL) ? buf : "No channel in bug context.",
  683. smartlist_len(channels_pending),
  684. (smartlist_pos(channels_pending, chan) == -1) ? "no" : "yes",
  685. msg);
  686. tor_free(msg);
  687. }
  688. }
  689. }
  690. #ifdef TOR_UNIT_TESTS
  691. /*
  692. * Notify scheduler that a channel's queue position may have changed.
  693. */
  694. void
  695. scheduler_touch_channel(channel_t *chan)
  696. {
  697. IF_BUG_ONCE(!chan) {
  698. return;
  699. }
  700. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  701. /* Remove and re-add it */
  702. smartlist_pqueue_remove(channels_pending,
  703. scheduler_compare_channels,
  704. offsetof(channel_t, sched_heap_idx),
  705. chan);
  706. smartlist_pqueue_add(channels_pending,
  707. scheduler_compare_channels,
  708. offsetof(channel_t, sched_heap_idx),
  709. chan);
  710. }
  711. /* else no-op, since it isn't in the queue */
  712. }
  713. #endif /* defined(TOR_UNIT_TESTS) */
  714. static int
  715. subsys_scheduler_initialize(void)
  716. {
  717. scheduler_init();
  718. return 0;
  719. }
  720. static void
  721. subsys_scheduler_shutdown(void)
  722. {
  723. scheduler_free_all();
  724. }
  725. const struct subsys_fns_t sys_scheduler = {
  726. .name = "scheduler",
  727. .supported = true,
  728. .level = 10,
  729. .initialize = subsys_scheduler_initialize,
  730. .shutdown = subsys_scheduler_shutdown,
  731. };