scheduler_kist.c 24 KB

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