scheduler_kist.c 24 KB

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