scheduler_kist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <event2/event.h>
  4. #include <netinet/tcp.h>
  5. #include "or.h"
  6. #include "buffers.h"
  7. #include "config.h"
  8. #include "connection.h"
  9. #include "networkstatus.h"
  10. #define TOR_CHANNEL_INTERNAL_
  11. #include "channel.h"
  12. #include "channeltls.h"
  13. #define SCHEDULER_PRIVATE_
  14. #include "scheduler.h"
  15. #define TLS_PER_CELL_OVERHEAD 29
  16. #ifdef HAVE_KIST_SUPPORT
  17. /* Kernel interface needed for KIST. */
  18. #include <linux/sockios.h>
  19. #endif /* HAVE_KIST_SUPPORT */
  20. /*****************************************************************************
  21. * Data structures and supporting functions
  22. *****************************************************************************/
  23. /* Indicate if we don't have the kernel support. This can happen if the kernel
  24. * changed and it doesn't recognized the values passed to the syscalls needed
  25. * by KIST. In that case, fallback to the naive approach. */
  26. #ifdef HAVE_KIST_SUPPORT
  27. static unsigned int kist_no_kernel_support = 0;
  28. #endif /* HAVE_KIST_SUPPORT */
  29. /* Socket_table hash table stuff. The socket_table keeps track of per-socket
  30. * limit information imposed by kist and used by kist. */
  31. static uint32_t
  32. socket_table_ent_hash(const socket_table_ent_t *ent)
  33. {
  34. return (uint32_t)ent->chan->global_identifier;
  35. }
  36. static unsigned
  37. socket_table_ent_eq(const socket_table_ent_t *a, const socket_table_ent_t *b)
  38. {
  39. return a->chan->global_identifier == b->chan->global_identifier;
  40. }
  41. typedef HT_HEAD(socket_table_s, socket_table_ent_s) socket_table_t;
  42. static socket_table_t socket_table = HT_INITIALIZER();
  43. HT_PROTOTYPE(socket_table_s, socket_table_ent_s, node, socket_table_ent_hash,
  44. socket_table_ent_eq)
  45. HT_GENERATE2(socket_table_s, socket_table_ent_s, node, socket_table_ent_hash,
  46. socket_table_ent_eq, 0.6, tor_reallocarray, tor_free_)
  47. /* outbuf_table hash table stuff. The outbuf_table keeps track of which
  48. * channels have data sitting in their outbuf so the kist scheduler can force
  49. * a write from outbuf to kernel periodically during a run and at the end of a
  50. * run. */
  51. typedef struct outbuf_table_ent_s {
  52. HT_ENTRY(outbuf_table_ent_s) node;
  53. channel_t *chan;
  54. } outbuf_table_ent_t;
  55. static uint32_t
  56. outbuf_table_ent_hash(const outbuf_table_ent_t *ent)
  57. {
  58. return (uint32_t)ent->chan->global_identifier;
  59. }
  60. static unsigned
  61. outbuf_table_ent_eq(const outbuf_table_ent_t *a, const outbuf_table_ent_t *b)
  62. {
  63. return a->chan->global_identifier == b->chan->global_identifier;
  64. }
  65. static outbuf_table_t outbuf_table = HT_INITIALIZER();
  66. HT_PROTOTYPE(outbuf_table_s, outbuf_table_ent_s, node, outbuf_table_ent_hash,
  67. outbuf_table_ent_eq)
  68. HT_GENERATE2(outbuf_table_s, outbuf_table_ent_s, node, outbuf_table_ent_hash,
  69. outbuf_table_ent_eq, 0.6, tor_reallocarray, tor_free_)
  70. /*****************************************************************************
  71. * Other internal data
  72. *****************************************************************************/
  73. /* Store the last time the scheduler was run so we can decide when to next run
  74. * the scheduler based on it. */
  75. static monotime_t scheduler_last_run;
  76. /* This is a factor for the extra_space calculation in kist per-socket limits.
  77. * It is the number of extra congestion windows we want to write to the kernel.
  78. */
  79. static double sock_buf_size_factor = 1.0;
  80. /* How often the scheduler runs. */
  81. STATIC int32_t sched_run_interval = 10;
  82. /* Stores the kist scheduler function pointers. */
  83. static scheduler_t *kist_scheduler = NULL;
  84. /*****************************************************************************
  85. * Internally called function implementations
  86. *****************************************************************************/
  87. /* Little helper function to get the length of a channel's output buffer */
  88. static inline size_t
  89. channel_outbuf_length(channel_t *chan)
  90. {
  91. return buf_datalen(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn)->outbuf);
  92. }
  93. /* Little helper function for HT_FOREACH_FN. */
  94. static int
  95. each_channel_write_to_kernel(outbuf_table_ent_t *ent, void *data)
  96. {
  97. (void) data; /* Make compiler happy. */
  98. channel_write_to_kernel(ent->chan);
  99. return 0; /* Returning non-zero removes the element from the table. */
  100. }
  101. /* Free the given outbuf table entry ent. */
  102. static int
  103. free_outbuf_info_by_ent(outbuf_table_ent_t *ent, void *data)
  104. {
  105. (void) data; /* Make compiler happy. */
  106. log_debug(LD_SCHED, "Freeing outbuf table entry from chan=%" PRIu64,
  107. ent->chan->global_identifier);
  108. tor_free(ent);
  109. return 1; /* So HT_FOREACH_FN will remove the element */
  110. }
  111. /* Clean up outbuf_table. Probably because the KIST sched impl is going away */
  112. static void
  113. free_all_outbuf_info(void)
  114. {
  115. HT_FOREACH_FN(outbuf_table_s, &outbuf_table, free_outbuf_info_by_ent, NULL);
  116. }
  117. /* Free the given socket table entry ent. */
  118. static int
  119. free_socket_info_by_ent(socket_table_ent_t *ent, void *data)
  120. {
  121. (void) data; /* Make compiler happy. */
  122. log_debug(LD_SCHED, "Freeing socket table entry from chan=%" PRIu64,
  123. ent->chan->global_identifier);
  124. tor_free(ent);
  125. return 1; /* So HT_FOREACH_FN will remove the element */
  126. }
  127. /* Clean up socket_table. Probably because the KIST sched impl is going away */
  128. static void
  129. free_all_socket_info(void)
  130. {
  131. HT_FOREACH_FN(socket_table_s, &socket_table, free_socket_info_by_ent, NULL);
  132. }
  133. static socket_table_ent_t *
  134. socket_table_search(socket_table_t *table, const channel_t *chan)
  135. {
  136. socket_table_ent_t search, *ent = NULL;
  137. search.chan = chan;
  138. ent = HT_FIND(socket_table_s, table, &search);
  139. return ent;
  140. }
  141. /* Free a socket entry in table for the given chan. */
  142. static void
  143. free_socket_info_by_chan(socket_table_t *table, const channel_t *chan)
  144. {
  145. socket_table_ent_t *ent = NULL;
  146. ent = socket_table_search(table, chan);
  147. if (!ent)
  148. return;
  149. log_debug(LD_SCHED, "scheduler free socket info for chan=%" PRIu64,
  150. chan->global_identifier);
  151. HT_REMOVE(socket_table_s, table, ent);
  152. free_socket_info_by_ent(ent, NULL);
  153. }
  154. /* Perform system calls for the given socket in order to calculate kist's
  155. * per-socket limit as documented in the function body. */
  156. MOCK_IMPL(void,
  157. update_socket_info_impl, (socket_table_ent_t *ent))
  158. {
  159. #ifdef HAVE_KIST_SUPPORT
  160. int64_t tcp_space, extra_space;
  161. const tor_socket_t sock =
  162. TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
  163. struct tcp_info tcp;
  164. socklen_t tcp_info_len = sizeof(tcp);
  165. if (kist_no_kernel_support) {
  166. goto fallback;
  167. }
  168. /* Gather information */
  169. if (getsockopt(sock, SOL_TCP, TCP_INFO, (void *)&(tcp), &tcp_info_len) < 0) {
  170. if (errno == EINVAL) {
  171. /* Oops, this option is not provided by the kernel, we'll have to
  172. * disable KIST entirely. This can happen if tor was built on a machine
  173. * with the support previously or if the kernel was updated and lost the
  174. * support. */
  175. log_notice(LD_SCHED, "Looks like our kernel doesn't have the support "
  176. "for KIST anymore. We will fallback to the naive "
  177. "approach. Set KISTSchedRunInterval=-1 to disable "
  178. "KIST.");
  179. kist_no_kernel_support = 1;
  180. }
  181. goto fallback;
  182. }
  183. if (ioctl(sock, SIOCOUTQNSD, &(ent->notsent)) < 0) {
  184. if (errno == EINVAL) {
  185. log_notice(LD_SCHED, "Looks like our kernel doesn't have the support "
  186. "for KIST anymore. We will fallback to the naive "
  187. "approach. Set KISTSchedRunInterval=-1 to disable "
  188. "KIST.");
  189. /* Same reason as the above. */
  190. kist_no_kernel_support = 1;
  191. }
  192. goto fallback;
  193. }
  194. ent->cwnd = tcp.tcpi_snd_cwnd;
  195. ent->unacked = tcp.tcpi_unacked;
  196. ent->mss = tcp.tcpi_snd_mss;
  197. /* TCP space is the number of bytes would could give to the kernel and it
  198. * would be able to immediately push them to the network. */
  199. tcp_space = (ent->cwnd - ent->unacked) * ent->mss;
  200. if (tcp_space < 0) {
  201. tcp_space = 0;
  202. }
  203. /* Imagine we have filled up tcp_space already for a socket and the scheduler
  204. * isn't going to run again for a while. We should write a little extra to the
  205. * kernel so it has some data to send between scheduling runs if it gets ACKs
  206. * back so it doesn't sit idle. With the suggested sock_buf_size_factor of
  207. * 1.0, a socket can have at most 2*cwnd data in the kernel: 1 cwnd on the
  208. * wire waiting for ACKs and 1 cwnd ready and waiting to be sent when those
  209. * ACKs come. */
  210. extra_space =
  211. clamp_double_to_int64((ent->cwnd * ent->mss) * sock_buf_size_factor) -
  212. ent->notsent;
  213. if (extra_space < 0) {
  214. extra_space = 0;
  215. }
  216. ent->limit = tcp_space + extra_space;
  217. return;
  218. #else /* HAVE_KIST_SUPPORT */
  219. goto fallback;
  220. #endif /* HAVE_KIST_SUPPORT */
  221. fallback:
  222. /* If all of a sudden we don't have kist support, we just zero out all the
  223. * variables for this socket since we don't know what they should be.
  224. * We also effectively allow the socket write as much as it wants to the
  225. * kernel, effectively returning it to vanilla scheduler behavior. Writes
  226. * are still limited by the lower layers of Tor: socket blocking, full
  227. * outbuf, etc. */
  228. ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
  229. ent->limit = INT_MAX;
  230. }
  231. /* Given a socket that isn't in the table, add it.
  232. * Given a socket that is in the table, reinit values that need init-ing
  233. * every scheduling run
  234. */
  235. static void
  236. init_socket_info(socket_table_t *table, const channel_t *chan)
  237. {
  238. socket_table_ent_t *ent = NULL;
  239. ent = socket_table_search(table, chan);
  240. if (!ent) {
  241. log_debug(LD_SCHED, "scheduler init socket info for chan=%" PRIu64,
  242. chan->global_identifier);
  243. ent = tor_malloc_zero(sizeof(*ent));
  244. ent->chan = chan;
  245. HT_INSERT(socket_table_s, table, ent);
  246. }
  247. ent->written = 0;
  248. }
  249. /* Add chan to the outbuf table if it isn't already in it. If it is, then don't
  250. * do anything */
  251. static void
  252. outbuf_table_add(outbuf_table_t *table, channel_t *chan)
  253. {
  254. outbuf_table_ent_t search, *ent;
  255. search.chan = chan;
  256. ent = HT_FIND(outbuf_table_s, table, &search);
  257. if (!ent) {
  258. log_debug(LD_SCHED, "scheduler init outbuf info for chan=%" PRIu64,
  259. chan->global_identifier);
  260. ent = tor_malloc_zero(sizeof(*ent));
  261. ent->chan = chan;
  262. HT_INSERT(outbuf_table_s, table, ent);
  263. }
  264. }
  265. static void
  266. outbuf_table_remove(outbuf_table_t *table, channel_t *chan)
  267. {
  268. outbuf_table_ent_t search, *ent;
  269. search.chan = chan;
  270. ent = HT_FIND(outbuf_table_s, table, &search);
  271. if (ent) {
  272. HT_REMOVE(outbuf_table_s, table, ent);
  273. free_outbuf_info_by_ent(ent, NULL);
  274. }
  275. }
  276. /* Set the scheduler running interval. */
  277. static void
  278. set_scheduler_run_interval(const networkstatus_t *ns)
  279. {
  280. int32_t old_sched_run_interval = sched_run_interval;
  281. sched_run_interval = kist_scheduler_run_interval(ns);
  282. if (old_sched_run_interval != sched_run_interval) {
  283. log_info(LD_SCHED, "Scheduler KIST changing its running interval "
  284. "from %" PRId32 " to %" PRId32,
  285. old_sched_run_interval, sched_run_interval);
  286. }
  287. }
  288. /* Return true iff the channel associated socket can write to the kernel that
  289. * is hasn't reach the limit. */
  290. static int
  291. socket_can_write(socket_table_t *table, const channel_t *chan)
  292. {
  293. socket_table_ent_t *ent = NULL;
  294. ent = socket_table_search(table, chan);
  295. tor_assert(ent);
  296. int64_t kist_limit_space =
  297. (int64_t) (ent->limit - ent->written) /
  298. (CELL_MAX_NETWORK_SIZE + TLS_PER_CELL_OVERHEAD);
  299. return kist_limit_space > 0;
  300. }
  301. /* Update the channel's socket kernel information. */
  302. static void
  303. update_socket_info(socket_table_t *table, const channel_t *chan)
  304. {
  305. socket_table_ent_t *ent = NULL;
  306. ent = socket_table_search(table, chan);
  307. tor_assert(ent);
  308. update_socket_info_impl(ent);
  309. }
  310. /* Increament the channel's socket written value by the number of bytes. */
  311. static void
  312. update_socket_written(socket_table_t *table, channel_t *chan, size_t bytes)
  313. {
  314. socket_table_ent_t *ent = NULL;
  315. ent = socket_table_search(table, chan);
  316. tor_assert(ent);
  317. log_debug(LD_SCHED, "chan=%" PRIu64 " wrote %lu bytes, old was %" PRIi64,
  318. chan->global_identifier, bytes, ent->written);
  319. ent->written += bytes;
  320. }
  321. /*
  322. * A naive KIST impl would write every single cell all the way to the kernel.
  323. * That would take a lot of system calls. A less bad KIST impl would write a
  324. * channel's outbuf to the kernel only when we are switching to a different
  325. * channel. But if we have two channels with equal priority, we end up writing
  326. * one cell for each and bouncing back and forth. This KIST impl avoids that
  327. * by only writing a channel's outbuf to the kernel if it has 8 cells or more
  328. * in it.
  329. */
  330. MOCK_IMPL(int, channel_should_write_to_kernel,
  331. (outbuf_table_t *table, channel_t *chan))
  332. {
  333. outbuf_table_add(table, chan);
  334. /* CELL_MAX_NETWORK_SIZE * 8 because we only want to write the outbuf to the
  335. * kernel if there's 8 or more cells waiting */
  336. return channel_outbuf_length(chan) > (CELL_MAX_NETWORK_SIZE * 8);
  337. }
  338. /* Little helper function to write a channel's outbuf all the way to the
  339. * kernel */
  340. MOCK_IMPL(void, channel_write_to_kernel, (channel_t *chan))
  341. {
  342. log_debug(LD_SCHED, "Writing %lu bytes to kernel for chan %" PRIu64,
  343. channel_outbuf_length(chan), chan->global_identifier);
  344. connection_handle_write(TO_CONN(BASE_CHAN_TO_TLS(chan)->conn), 0);
  345. }
  346. /* Return true iff the scheduler has work to perform. */
  347. static int
  348. have_work(void)
  349. {
  350. smartlist_t *cp = get_channels_pending();
  351. tor_assert(cp);
  352. return smartlist_len(cp) > 0;
  353. }
  354. /* Function of the scheduler interface: free_all() */
  355. static void
  356. kist_free_all(void)
  357. {
  358. free_all_outbuf_info();
  359. free_all_socket_info();
  360. }
  361. /* Function of the scheduler interface: on_channel_free() */
  362. static void
  363. kist_on_channel_free(const channel_t *chan)
  364. {
  365. free_socket_info_by_chan(&socket_table, chan);
  366. }
  367. /* Function of the scheduler interface: on_new_consensus() */
  368. static void
  369. kist_scheduler_on_new_consensus(const networkstatus_t *old_c,
  370. const networkstatus_t *new_c)
  371. {
  372. (void) old_c;
  373. (void) new_c;
  374. set_scheduler_run_interval(new_c);
  375. }
  376. /* Function of the scheduler interface: on_new_options() */
  377. static void
  378. kist_scheduler_on_new_options(void)
  379. {
  380. sock_buf_size_factor = get_options()->KISTSockBufSizeFactor;
  381. /* Calls kist_scheduler_run_interval which calls get_options(). */
  382. set_scheduler_run_interval(NULL);
  383. }
  384. /* Function of the scheduler interface: init() */
  385. static void
  386. kist_scheduler_init(void)
  387. {
  388. kist_scheduler_on_new_options();
  389. tor_assert(sched_run_interval > 0);
  390. }
  391. /* Function of the scheduler interface: schedule() */
  392. static void
  393. kist_scheduler_schedule(void)
  394. {
  395. struct monotime_t now;
  396. struct timeval next_run;
  397. int32_t diff;
  398. struct event *ev = get_run_sched_ev();
  399. tor_assert(ev);
  400. if (!have_work()) {
  401. return;
  402. }
  403. monotime_get(&now);
  404. diff = (int32_t) monotime_diff_msec(&scheduler_last_run, &now);
  405. if (diff < sched_run_interval) {
  406. next_run.tv_sec = 0;
  407. /* 1000 for ms -> us */
  408. next_run.tv_usec = (sched_run_interval - diff) * 1000;
  409. /* Readding an event reschedules it. It does not duplicate it. */
  410. event_add(ev, &next_run);
  411. } else {
  412. event_active(ev, EV_TIMEOUT, 1);
  413. }
  414. }
  415. /* Function of the scheduler interface: run() */
  416. static void
  417. kist_scheduler_run(void)
  418. {
  419. /* Define variables */
  420. channel_t *chan = NULL; // current working channel
  421. /* The last distinct chan served in a sched loop. */
  422. channel_t *prev_chan = NULL;
  423. int flush_result; // temporarily store results from flush calls
  424. /* Channels to be readding to pending at the end */
  425. smartlist_t *to_readd = NULL;
  426. smartlist_t *cp = get_channels_pending();
  427. /* For each pending channel, collect new kernel information */
  428. SMARTLIST_FOREACH_BEGIN(cp, const channel_t *, pchan) {
  429. init_socket_info(&socket_table, pchan);
  430. update_socket_info(&socket_table, pchan);
  431. } SMARTLIST_FOREACH_END(pchan);
  432. log_debug(LD_SCHED, "Running the scheduler. %d channels pending",
  433. smartlist_len(cp));
  434. /* The main scheduling loop. Loop until there are no more pending channels */
  435. while (smartlist_len(cp) > 0) {
  436. /* get best channel */
  437. chan = smartlist_pqueue_pop(cp, scheduler_compare_channels,
  438. offsetof(channel_t, sched_heap_idx));
  439. tor_assert(chan);
  440. outbuf_table_add(&outbuf_table, chan);
  441. /* if we have switched to a new channel, consider writing the previous
  442. * channel's outbuf to the kernel. */
  443. if (!prev_chan) {
  444. prev_chan = chan;
  445. }
  446. if (prev_chan != chan) {
  447. if (channel_should_write_to_kernel(&outbuf_table, prev_chan)) {
  448. channel_write_to_kernel(prev_chan);
  449. outbuf_table_remove(&outbuf_table, prev_chan);
  450. }
  451. prev_chan = chan;
  452. }
  453. /* Only flush and write if the per-socket limit hasn't been hit */
  454. if (socket_can_write(&socket_table, chan)) {
  455. /* flush to channel queue/outbuf */
  456. flush_result = (int)channel_flush_some_cells(chan, 1); // 1 for num cells
  457. /* flush_result has the # cells flushed */
  458. if (flush_result > 0) {
  459. update_socket_written(&socket_table, chan, flush_result *
  460. (CELL_MAX_NETWORK_SIZE + TLS_PER_CELL_OVERHEAD));
  461. }
  462. /* XXX What if we didn't flush? */
  463. }
  464. /* Decide what to do with the channel now */
  465. if (!channel_more_to_flush(chan) &&
  466. !socket_can_write(&socket_table, chan)) {
  467. /* Case 1: no more cells to send, and cannot write */
  468. /*
  469. * You might think we should put the channel in SCHED_CHAN_IDLE. And
  470. * you're probably correct. While implementing KIST, we found that the
  471. * scheduling system would sometimes lose track of channels when we did
  472. * that. We suspect it has to do with the difference between "can't
  473. * write because socket/outbuf is full" and KIST's "can't write because
  474. * we've arbitrarily decided that that's enough for now." Sometimes
  475. * channels run out of cells at the same time they hit their
  476. * kist-imposed write limit and maybe the rest of Tor doesn't put the
  477. * channel back in pending when it is supposed to.
  478. *
  479. * This should be investigated again. It is as simple as changing
  480. * SCHED_CHAN_WAITING_FOR_CELLS to SCHED_CHAN_IDLE and seeing if Tor
  481. * starts having serious throughput issues. Best done in shadow/chutney.
  482. */
  483. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  484. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_for_cells",
  485. chan->global_identifier);
  486. } else if (!channel_more_to_flush(chan)) {
  487. /* Case 2: no more cells to send, but still open for writes */
  488. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  489. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_for_cells",
  490. chan->global_identifier);
  491. } else if (!socket_can_write(&socket_table, chan)) {
  492. /* Case 3: cells to send, but cannot write */
  493. /*
  494. * We want to write, but can't. If we left the channel in
  495. * channels_pending, we would never exit the scheduling loop. We need to
  496. * add it to a temporary list of channels to be added to channels_pending
  497. * after the scheduling loop is over. They can hopefully be taken care of
  498. * in the next scheduling round.
  499. */
  500. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  501. if (!to_readd) {
  502. to_readd = smartlist_new();
  503. }
  504. smartlist_add(to_readd, chan);
  505. log_debug(LD_SCHED, "chan=%" PRIu64 " now waiting_to_write",
  506. chan->global_identifier);
  507. } else {
  508. /* Case 4: cells to send, and still open for writes */
  509. chan->scheduler_state = SCHED_CHAN_PENDING;
  510. smartlist_pqueue_add(cp, scheduler_compare_channels,
  511. offsetof(channel_t, sched_heap_idx), chan);
  512. }
  513. } /* End of main scheduling loop */
  514. /* Write the outbuf of any channels that still have data */
  515. HT_FOREACH_FN(outbuf_table_s, &outbuf_table, each_channel_write_to_kernel,
  516. NULL);
  517. free_all_outbuf_info();
  518. HT_CLEAR(outbuf_table_s, &outbuf_table);
  519. log_debug(LD_SCHED, "len pending=%d, len to_readd=%d",
  520. smartlist_len(cp),
  521. (to_readd ? smartlist_len(to_readd) : -1));
  522. /* Readd any channels we need to */
  523. if (to_readd) {
  524. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, readd_chan) {
  525. readd_chan->scheduler_state = SCHED_CHAN_PENDING;
  526. if (!smartlist_contains(cp, readd_chan)) {
  527. smartlist_pqueue_add(cp, scheduler_compare_channels,
  528. offsetof(channel_t, sched_heap_idx), readd_chan);
  529. }
  530. } SMARTLIST_FOREACH_END(readd_chan);
  531. smartlist_free(to_readd);
  532. }
  533. monotime_get(&scheduler_last_run);
  534. }
  535. /*****************************************************************************
  536. * Externally called function implementations not called through scheduler_t
  537. *****************************************************************************/
  538. /* Return the KIST scheduler object. If it didn't exists, return a newly
  539. * allocated one but init() is not called. */
  540. scheduler_t *
  541. get_kist_scheduler(void)
  542. {
  543. if (!kist_scheduler) {
  544. log_debug(LD_SCHED, "Allocating kist scheduler struct");
  545. kist_scheduler = tor_malloc_zero(sizeof(*kist_scheduler));
  546. kist_scheduler->free_all = kist_free_all;
  547. kist_scheduler->on_channel_free = kist_on_channel_free;
  548. kist_scheduler->init = kist_scheduler_init;
  549. kist_scheduler->on_new_consensus = kist_scheduler_on_new_consensus;
  550. kist_scheduler->schedule = kist_scheduler_schedule;
  551. kist_scheduler->run = kist_scheduler_run;
  552. kist_scheduler->on_new_options = kist_scheduler_on_new_options;
  553. }
  554. return kist_scheduler;
  555. }
  556. /* Check the torrc for the configured KIST scheduler run interval.
  557. * - If torrc < 0, then return the negative torrc value (shouldn't even be
  558. * using KIST)
  559. * - If torrc > 0, then return the positive torrc value (should use KIST, and
  560. * should use the set value)
  561. * - If torrc == 0, then look in the consensus for what the value should be.
  562. * - If == 0, then return -1 (don't use KIST)
  563. * - If > 0, then return the positive consensus value
  564. * - If consensus doesn't say anything, return 10 milliseconds
  565. */
  566. int32_t
  567. kist_scheduler_run_interval(const networkstatus_t *ns)
  568. {
  569. int32_t run_interval = (int32_t)get_options()->KISTSchedRunInterval;
  570. if (run_interval != 0) {
  571. log_debug(LD_SCHED, "Found KISTSchedRunInterval in torrc. Using that.");
  572. return run_interval;
  573. }
  574. log_debug(LD_SCHED, "Turning to the consensus for KISTSchedRunInterval");
  575. run_interval = networkstatus_get_param(ns, "KISTSchedRunInterval",
  576. KIST_SCHED_RUN_INTERVAL_DEFAULT,
  577. KIST_SCHED_RUN_INTERVAL_MIN,
  578. KIST_SCHED_RUN_INTERVAL_MAX);
  579. if (run_interval <= 0)
  580. return -1;
  581. return run_interval;
  582. }
  583. #ifdef HAVE_KIST_SUPPORT
  584. /* Return true iff the scheduler subsystem should use KIST. */
  585. int
  586. scheduler_should_use_kist(void)
  587. {
  588. int64_t run_interval = kist_scheduler_run_interval(NULL);
  589. log_info(LD_SCHED, "Determined sched_run_interval should be %" PRId64 ". "
  590. "Will%s use KIST.",
  591. run_interval, (run_interval > 0 ? "" : " not"));
  592. return run_interval > 0;
  593. }
  594. #else /* HAVE_KIST_SUPPORT */
  595. int
  596. scheduler_should_use_kist(void)
  597. {
  598. return 0;
  599. }
  600. #endif /* HAVE_KIST_SUPPORT */