scheduler.c 23 KB

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