scheduler_kist.c 25 KB

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