scheduler_kist.c 25 KB

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