scheduler_kist.c 25 KB

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