scheduler.c 26 KB

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