test_scheduler.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <math.h>
  5. #include <event2/event.h>
  6. #define TOR_CHANNEL_INTERNAL_
  7. #define CHANNEL_PRIVATE_
  8. #include "or.h"
  9. #include "config.h"
  10. #include "compat_libevent.h"
  11. #include "channel.h"
  12. #include "channeltls.h"
  13. #include "connection.h"
  14. #include "networkstatus.h"
  15. #define SCHEDULER_PRIVATE_
  16. #include "scheduler.h"
  17. /* Test suite stuff */
  18. #include "test.h"
  19. #include "fakechans.h"
  20. /* Shamelessly stolen from compat_libevent.c */
  21. #define V(major, minor, patch) \
  22. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  23. /******************************************************************************
  24. * Statistical info
  25. *****************************************************************************/
  26. static int scheduler_compare_channels_mock_ctr = 0;
  27. static int scheduler_run_mock_ctr = 0;
  28. /******************************************************************************
  29. * Utility functions and things we need to mock
  30. *****************************************************************************/
  31. static or_options_t mocked_options;
  32. static const or_options_t *
  33. mock_get_options(void)
  34. {
  35. return &mocked_options;
  36. }
  37. static void
  38. clear_options(void)
  39. {
  40. memset(&mocked_options, 0, sizeof(mocked_options));
  41. }
  42. static int32_t
  43. mock_vanilla_networkstatus_get_param(
  44. const networkstatus_t *ns, const char *param_name, int32_t default_val,
  45. int32_t min_val, int32_t max_val)
  46. {
  47. (void)ns;
  48. (void)default_val;
  49. (void)min_val;
  50. (void)max_val;
  51. // only support KISTSchedRunInterval right now
  52. tor_assert(strcmp(param_name, "KISTSchedRunInterval")==0);
  53. return -1;
  54. }
  55. static int32_t
  56. mock_kist_networkstatus_get_param(
  57. const networkstatus_t *ns, const char *param_name, int32_t default_val,
  58. int32_t min_val, int32_t max_val)
  59. {
  60. (void)ns;
  61. (void)default_val;
  62. (void)min_val;
  63. (void)max_val;
  64. // only support KISTSchedRunInterval right now
  65. tor_assert(strcmp(param_name, "KISTSchedRunInterval")==0);
  66. return 12;
  67. }
  68. /* Event base for scheduelr tests */
  69. static struct event_base *mock_event_base = NULL;
  70. /* Setup for mock event stuff */
  71. static void mock_event_free_all(void);
  72. static void mock_event_init(void);
  73. static void
  74. mock_event_free_all(void)
  75. {
  76. tt_ptr_op(mock_event_base, OP_NE, NULL);
  77. if (mock_event_base) {
  78. event_base_free(mock_event_base);
  79. mock_event_base = NULL;
  80. }
  81. tt_ptr_op(mock_event_base, OP_EQ, NULL);
  82. done:
  83. return;
  84. }
  85. static void
  86. mock_event_init(void)
  87. {
  88. struct event_config *cfg = NULL;
  89. tt_ptr_op(mock_event_base, OP_EQ, NULL);
  90. /*
  91. * Really cut down from tor_libevent_initialize of
  92. * src/common/compat_libevent.c to kill config dependencies
  93. */
  94. if (!mock_event_base) {
  95. cfg = event_config_new();
  96. #if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
  97. /* We can enable changelist support with epoll, since we don't give
  98. * Libevent any dup'd fds. This lets us avoid some syscalls. */
  99. event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
  100. #endif
  101. mock_event_base = event_base_new_with_config(cfg);
  102. event_config_free(cfg);
  103. }
  104. tt_ptr_op(mock_event_base, OP_NE, NULL);
  105. done:
  106. return;
  107. }
  108. static struct event_base *
  109. tor_libevent_get_base_mock(void)
  110. {
  111. return mock_event_base;
  112. }
  113. static int
  114. scheduler_compare_channels_mock(const void *c1_v,
  115. const void *c2_v)
  116. {
  117. uintptr_t p1, p2;
  118. p1 = (uintptr_t)(c1_v);
  119. p2 = (uintptr_t)(c2_v);
  120. ++scheduler_compare_channels_mock_ctr;
  121. if (p1 == p2) return 0;
  122. else if (p1 < p2) return 1;
  123. else return -1;
  124. }
  125. static void
  126. scheduler_run_noop_mock(void)
  127. {
  128. ++scheduler_run_mock_ctr;
  129. }
  130. static circuitmux_t *mock_ccm_tgt_1 = NULL;
  131. static circuitmux_t *mock_ccm_tgt_2 = NULL;
  132. static circuitmux_t *mock_cgp_tgt_1 = NULL;
  133. static circuitmux_policy_t *mock_cgp_val_1 = NULL;
  134. static circuitmux_t *mock_cgp_tgt_2 = NULL;
  135. static circuitmux_policy_t *mock_cgp_val_2 = NULL;
  136. static const circuitmux_policy_t *
  137. circuitmux_get_policy_mock(circuitmux_t *cmux)
  138. {
  139. const circuitmux_policy_t *result = NULL;
  140. tt_assert(cmux != NULL);
  141. if (cmux) {
  142. if (cmux == mock_cgp_tgt_1) result = mock_cgp_val_1;
  143. else if (cmux == mock_cgp_tgt_2) result = mock_cgp_val_2;
  144. else result = circuitmux_get_policy__real(cmux);
  145. }
  146. done:
  147. return result;
  148. }
  149. static int
  150. circuitmux_compare_muxes_mock(circuitmux_t *cmux_1,
  151. circuitmux_t *cmux_2)
  152. {
  153. int result = 0;
  154. tt_assert(cmux_1 != NULL);
  155. tt_assert(cmux_2 != NULL);
  156. if (cmux_1 != cmux_2) {
  157. if (cmux_1 == mock_ccm_tgt_1 && cmux_2 == mock_ccm_tgt_2) result = -1;
  158. else if (cmux_1 == mock_ccm_tgt_2 && cmux_2 == mock_ccm_tgt_1) {
  159. result = 1;
  160. } else {
  161. if (cmux_1 == mock_ccm_tgt_1 || cmux_1 == mock_ccm_tgt_2) result = -1;
  162. else if (cmux_2 == mock_ccm_tgt_1 || cmux_2 == mock_ccm_tgt_2) {
  163. result = 1;
  164. } else {
  165. result = circuitmux_compare_muxes__real(cmux_1, cmux_2);
  166. }
  167. }
  168. }
  169. /* else result = 0 always */
  170. done:
  171. return result;
  172. }
  173. typedef struct {
  174. const channel_t *chan;
  175. ssize_t cells;
  176. } flush_mock_channel_t;
  177. static smartlist_t *chans_for_flush_mock = NULL;
  178. static void
  179. channel_flush_some_cells_mock_free_all(void)
  180. {
  181. if (chans_for_flush_mock) {
  182. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  183. flush_mock_channel_t *,
  184. flush_mock_ch) {
  185. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  186. tor_free(flush_mock_ch);
  187. } SMARTLIST_FOREACH_END(flush_mock_ch);
  188. smartlist_free(chans_for_flush_mock);
  189. chans_for_flush_mock = NULL;
  190. }
  191. }
  192. static void
  193. channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
  194. {
  195. int found = 0;
  196. if (!chan) return;
  197. if (num_cells <= 0) return;
  198. if (!chans_for_flush_mock) {
  199. chans_for_flush_mock = smartlist_new();
  200. }
  201. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  202. flush_mock_channel_t *,
  203. flush_mock_ch) {
  204. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  205. if (flush_mock_ch->chan == chan) {
  206. /* Found it */
  207. flush_mock_ch->cells = num_cells;
  208. found = 1;
  209. break;
  210. }
  211. } else {
  212. /* That shouldn't be there... */
  213. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  214. tor_free(flush_mock_ch);
  215. }
  216. } SMARTLIST_FOREACH_END(flush_mock_ch);
  217. if (! found) {
  218. /* The loop didn't find it */
  219. flush_mock_channel_t *flush_mock_ch;
  220. flush_mock_ch = tor_malloc_zero(sizeof(*flush_mock_ch));
  221. flush_mock_ch->chan = chan;
  222. flush_mock_ch->cells = num_cells;
  223. smartlist_add(chans_for_flush_mock, flush_mock_ch);
  224. }
  225. }
  226. static int
  227. channel_more_to_flush_mock(channel_t *chan)
  228. {
  229. tor_assert(chan);
  230. flush_mock_channel_t *found_mock_ch = NULL;
  231. /* Check if we have any queued */
  232. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  233. return 1;
  234. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  235. flush_mock_channel_t *,
  236. flush_mock_ch) {
  237. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  238. if (flush_mock_ch->chan == chan) {
  239. /* Found it */
  240. found_mock_ch = flush_mock_ch;
  241. break;
  242. }
  243. } else {
  244. /* That shouldn't be there... */
  245. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  246. tor_free(flush_mock_ch);
  247. }
  248. } SMARTLIST_FOREACH_END(flush_mock_ch);
  249. tor_assert(found_mock_ch);
  250. /* Check if any circuits would like to queue some */
  251. /* special for the mock: return the number of cells (instead of 1), or zero
  252. * if nothing to flush */
  253. return (found_mock_ch->cells > 0 ? (int)found_mock_ch->cells : 0 );
  254. }
  255. static void
  256. channel_write_to_kernel_mock(channel_t *chan)
  257. {
  258. (void)chan;
  259. //log_debug(LD_SCHED, "chan=%d writing to kernel",
  260. // (int)chan->global_identifier);
  261. }
  262. static int
  263. channel_should_write_to_kernel_mock(outbuf_table_t *ot, channel_t *chan)
  264. {
  265. (void)ot;
  266. (void)chan;
  267. return 1;
  268. /* We could make this more complicated if we wanted. But I don't think doing
  269. * so tests much of anything */
  270. //static int called_counter = 0;
  271. //if (++called_counter >= 3) {
  272. // called_counter -= 3;
  273. // log_debug(LD_SCHED, "chan=%d should write to kernel",
  274. // (int)chan->global_identifier);
  275. // return 1;
  276. //}
  277. //return 0;
  278. }
  279. static ssize_t
  280. channel_flush_some_cells_mock(channel_t *chan, ssize_t num_cells)
  281. {
  282. ssize_t flushed = 0, max;
  283. char unlimited = 0;
  284. flush_mock_channel_t *found = NULL;
  285. tt_ptr_op(chan, OP_NE, NULL);
  286. if (chan) {
  287. if (num_cells < 0) {
  288. num_cells = 0;
  289. unlimited = 1;
  290. }
  291. /* Check if we have it */
  292. if (chans_for_flush_mock != NULL) {
  293. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  294. flush_mock_channel_t *,
  295. flush_mock_ch) {
  296. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  297. if (flush_mock_ch->chan == chan) {
  298. /* Found it */
  299. found = flush_mock_ch;
  300. break;
  301. }
  302. } else {
  303. /* That shouldn't be there... */
  304. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  305. tor_free(flush_mock_ch);
  306. }
  307. } SMARTLIST_FOREACH_END(flush_mock_ch);
  308. if (found) {
  309. /* We found one */
  310. if (found->cells < 0) found->cells = 0;
  311. if (unlimited) max = found->cells;
  312. else max = MIN(found->cells, num_cells);
  313. flushed += max;
  314. found->cells -= max;
  315. }
  316. }
  317. }
  318. done:
  319. return flushed;
  320. }
  321. static void
  322. update_socket_info_impl_mock(socket_table_ent_t *ent)
  323. {
  324. ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
  325. ent->limit = INT_MAX;
  326. }
  327. static void
  328. perform_channel_state_tests(int KISTSchedRunInterval)
  329. {
  330. channel_t *ch1 = NULL, *ch2 = NULL;
  331. int old_count;
  332. /* setup options so we're sure about what sched we are running */
  333. MOCK(get_options, mock_get_options);
  334. clear_options();
  335. mocked_options.KISTSchedRunInterval = KISTSchedRunInterval;
  336. /* Set up libevent and scheduler */
  337. mock_event_init();
  338. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  339. scheduler_init();
  340. /*
  341. * Install the compare channels mock so we can test
  342. * scheduler_touch_channel().
  343. */
  344. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  345. /*
  346. * Disable scheduler_run so we can just check the state transitions
  347. * without having to make everything it might call work too.
  348. */
  349. the_scheduler->run = scheduler_run_noop_mock;
  350. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  351. /* Set up a fake channel */
  352. ch1 = new_fake_channel();
  353. tt_assert(ch1);
  354. /* Start it off in OPENING */
  355. ch1->state = CHANNEL_STATE_OPENING;
  356. /* We'll need a cmux */
  357. ch1->cmux = circuitmux_alloc();
  358. /* Try to register it */
  359. channel_register(ch1);
  360. tt_assert(ch1->registered);
  361. /* It should start off in SCHED_CHAN_IDLE */
  362. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  363. /* Now get another one */
  364. ch2 = new_fake_channel();
  365. tt_assert(ch2);
  366. ch2->state = CHANNEL_STATE_OPENING;
  367. ch2->cmux = circuitmux_alloc();
  368. channel_register(ch2);
  369. tt_assert(ch2->registered);
  370. /* Send ch1 to SCHED_CHAN_WAITING_TO_WRITE */
  371. scheduler_channel_has_waiting_cells(ch1);
  372. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  373. /* This should send it to SCHED_CHAN_PENDING */
  374. scheduler_channel_wants_writes(ch1);
  375. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  376. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  377. /* Now send ch2 to SCHED_CHAN_WAITING_FOR_CELLS */
  378. scheduler_channel_wants_writes(ch2);
  379. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  380. /* Drop ch2 back to idle */
  381. scheduler_channel_doesnt_want_writes(ch2);
  382. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  383. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  384. scheduler_channel_wants_writes(ch2);
  385. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  386. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  387. scheduler_channel_has_waiting_cells(ch2);
  388. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  389. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  390. /* This should send ch2 to SCHED_CHAN_WAITING_TO_WRITE */
  391. scheduler_channel_doesnt_want_writes(ch2);
  392. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  393. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  394. /* ...and back to SCHED_CHAN_PENDING */
  395. scheduler_channel_wants_writes(ch2);
  396. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  397. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  398. /* Now we exercise scheduler_touch_channel */
  399. old_count = scheduler_compare_channels_mock_ctr;
  400. scheduler_touch_channel(ch1);
  401. tt_assert(scheduler_compare_channels_mock_ctr > old_count);
  402. /* Close */
  403. channel_mark_for_close(ch1);
  404. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
  405. channel_mark_for_close(ch2);
  406. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  407. channel_closed(ch1);
  408. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
  409. ch1 = NULL;
  410. channel_closed(ch2);
  411. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
  412. ch2 = NULL;
  413. /* Shut things down */
  414. channel_free_all();
  415. scheduler_free_all();
  416. mock_event_free_all();
  417. done:
  418. tor_free(ch1);
  419. tor_free(ch2);
  420. UNMOCK(scheduler_compare_channels);
  421. UNMOCK(tor_libevent_get_base);
  422. UNMOCK(get_options);
  423. return;
  424. }
  425. static void
  426. test_scheduler_compare_channels(void *arg)
  427. {
  428. /* We don't actually need whole fake channels... */
  429. channel_t c1, c2;
  430. /* ...and some dummy circuitmuxes too */
  431. circuitmux_t *cm1 = NULL, *cm2 = NULL;
  432. int result;
  433. (void)arg;
  434. /* We can't actually see sizeof(circuitmux_t) from here */
  435. cm1 = tor_malloc_zero(sizeof(void *));
  436. cm2 = tor_malloc_zero(sizeof(void *));
  437. c1.cmux = cm1;
  438. c2.cmux = cm2;
  439. /* Configure circuitmux_get_policy() mock */
  440. mock_cgp_tgt_1 = cm1;
  441. mock_cgp_tgt_2 = cm2;
  442. /*
  443. * This is to test the different-policies case, which uses the policy
  444. * cast to an uintptr_t as an arbitrary but definite thing to compare.
  445. */
  446. mock_cgp_val_1 = tor_malloc_zero(16);
  447. mock_cgp_val_2 = tor_malloc_zero(16);
  448. if ( ((uintptr_t) mock_cgp_val_1) > ((uintptr_t) mock_cgp_val_2) ) {
  449. void *tmp = mock_cgp_val_1;
  450. mock_cgp_val_1 = mock_cgp_val_2;
  451. mock_cgp_val_2 = tmp;
  452. }
  453. MOCK(circuitmux_get_policy, circuitmux_get_policy_mock);
  454. /* Now set up circuitmux_compare_muxes() mock using cm1/cm2 */
  455. mock_ccm_tgt_1 = cm1;
  456. mock_ccm_tgt_2 = cm2;
  457. MOCK(circuitmux_compare_muxes, circuitmux_compare_muxes_mock);
  458. /* Equal-channel case */
  459. result = scheduler_compare_channels(&c1, &c1);
  460. tt_int_op(result, OP_EQ, 0);
  461. /* Distinct channels, distinct policies */
  462. result = scheduler_compare_channels(&c1, &c2);
  463. tt_int_op(result, OP_EQ, -1);
  464. result = scheduler_compare_channels(&c2, &c1);
  465. tt_int_op(result, OP_EQ, 1);
  466. /* Distinct channels, same policy */
  467. tor_free(mock_cgp_val_2);
  468. mock_cgp_val_2 = mock_cgp_val_1;
  469. result = scheduler_compare_channels(&c1, &c2);
  470. tt_int_op(result, OP_EQ, -1);
  471. result = scheduler_compare_channels(&c2, &c1);
  472. tt_int_op(result, OP_EQ, 1);
  473. done:
  474. UNMOCK(circuitmux_compare_muxes);
  475. mock_ccm_tgt_1 = NULL;
  476. mock_ccm_tgt_2 = NULL;
  477. UNMOCK(circuitmux_get_policy);
  478. mock_cgp_tgt_1 = NULL;
  479. mock_cgp_tgt_2 = NULL;
  480. tor_free(cm1);
  481. tor_free(cm2);
  482. if (mock_cgp_val_1 != mock_cgp_val_2)
  483. tor_free(mock_cgp_val_1);
  484. tor_free(mock_cgp_val_2);
  485. mock_cgp_val_1 = NULL;
  486. mock_cgp_val_2 = NULL;
  487. return;
  488. }
  489. /******************************************************************************
  490. * The actual tests!
  491. *****************************************************************************/
  492. static void
  493. test_scheduler_loop_vanilla(void *arg)
  494. {
  495. (void)arg;
  496. channel_t *ch1 = NULL, *ch2 = NULL;
  497. void (*run_func_ptr)(void);
  498. /* setup options so we're sure about what sched we are running */
  499. MOCK(get_options, mock_get_options);
  500. clear_options();
  501. mocked_options.KISTSchedRunInterval = -1;
  502. /* Set up libevent and scheduler */
  503. mock_event_init();
  504. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  505. scheduler_init();
  506. /*
  507. * Install the compare channels mock so we can test
  508. * scheduler_touch_channel().
  509. */
  510. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  511. /*
  512. * Disable scheduler_run so we can just check the state transitions
  513. * without having to make everything it might call work too.
  514. */
  515. run_func_ptr = the_scheduler->run;
  516. the_scheduler->run = scheduler_run_noop_mock;
  517. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  518. /* Set up a fake channel */
  519. ch1 = new_fake_channel();
  520. ch1->magic = TLS_CHAN_MAGIC;
  521. tt_assert(ch1);
  522. /* Start it off in OPENING */
  523. ch1->state = CHANNEL_STATE_OPENING;
  524. /* We'll need a cmux */
  525. ch1->cmux = circuitmux_alloc();
  526. /* Try to register it */
  527. channel_register(ch1);
  528. tt_assert(ch1->registered);
  529. /* Finish opening it */
  530. channel_change_state_open(ch1);
  531. /* It should start off in SCHED_CHAN_IDLE */
  532. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  533. /* Now get another one */
  534. ch2 = new_fake_channel();
  535. ch2->magic = TLS_CHAN_MAGIC;
  536. tt_assert(ch2);
  537. ch2->state = CHANNEL_STATE_OPENING;
  538. ch2->cmux = circuitmux_alloc();
  539. channel_register(ch2);
  540. tt_assert(ch2->registered);
  541. /*
  542. * Don't open ch2; then channel_num_cells_writeable() will return
  543. * zero and we'll get coverage of that exception case in scheduler_run()
  544. */
  545. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  546. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
  547. /* Send it to SCHED_CHAN_WAITING_TO_WRITE */
  548. scheduler_channel_has_waiting_cells(ch1);
  549. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  550. /* This should send it to SCHED_CHAN_PENDING */
  551. scheduler_channel_wants_writes(ch1);
  552. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  553. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  554. /* Now send ch2 to SCHED_CHAN_WAITING_FOR_CELLS */
  555. scheduler_channel_wants_writes(ch2);
  556. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  557. /* Drop ch2 back to idle */
  558. scheduler_channel_doesnt_want_writes(ch2);
  559. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  560. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  561. scheduler_channel_wants_writes(ch2);
  562. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  563. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  564. scheduler_channel_has_waiting_cells(ch2);
  565. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  566. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  567. /*
  568. * Now we've got two pending channels and need to fire off
  569. * the scheduler run() that we kept.
  570. */
  571. run_func_ptr();
  572. /*
  573. * Assert that they're still in the states we left and aren't still
  574. * pending
  575. */
  576. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  577. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
  578. tt_assert(ch1->scheduler_state != SCHED_CHAN_PENDING);
  579. tt_assert(ch2->scheduler_state != SCHED_CHAN_PENDING);
  580. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  581. /* Now, finish opening ch2, and get both back to pending */
  582. channel_change_state_open(ch2);
  583. scheduler_channel_wants_writes(ch1);
  584. scheduler_channel_wants_writes(ch2);
  585. scheduler_channel_has_waiting_cells(ch1);
  586. scheduler_channel_has_waiting_cells(ch2);
  587. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  588. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPEN);
  589. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  590. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  591. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  592. /* Now, set up the channel_flush_some_cells() mock */
  593. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  594. /*
  595. * 16 cells on ch1 means it'll completely drain into the 32 cells
  596. * fakechan's num_cells_writeable() returns.
  597. */
  598. channel_flush_some_cells_mock_set(ch1, 16);
  599. /*
  600. * This one should get sent back to pending, since num_cells_writeable()
  601. * will still return non-zero.
  602. */
  603. channel_flush_some_cells_mock_set(ch2, 48);
  604. /*
  605. * And re-run the scheduler run() loop with non-zero returns from
  606. * channel_flush_some_cells() this time.
  607. */
  608. run_func_ptr();
  609. /*
  610. * ch1 should have gone to SCHED_CHAN_WAITING_FOR_CELLS, with 16 flushed
  611. * and 32 writeable.
  612. */
  613. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  614. /*
  615. * ...ch2 should also have gone to SCHED_CHAN_WAITING_FOR_CELLS, with
  616. * channel_more_to_flush() returning false and channel_num_cells_writeable()
  617. * > 0/
  618. */
  619. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  620. /* Close */
  621. channel_mark_for_close(ch1);
  622. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
  623. channel_mark_for_close(ch2);
  624. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  625. channel_closed(ch1);
  626. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
  627. ch1 = NULL;
  628. channel_closed(ch2);
  629. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
  630. ch2 = NULL;
  631. /* Shut things down */
  632. channel_flush_some_cells_mock_free_all();
  633. channel_free_all();
  634. scheduler_free_all();
  635. mock_event_free_all();
  636. done:
  637. tor_free(ch1);
  638. tor_free(ch2);
  639. UNMOCK(channel_flush_some_cells);
  640. UNMOCK(scheduler_compare_channels);
  641. UNMOCK(tor_libevent_get_base);
  642. UNMOCK(get_options);
  643. }
  644. static void
  645. test_scheduler_loop_kist(void *arg)
  646. {
  647. (void) arg;
  648. channel_t *ch1 = new_fake_channel(), *ch2 = new_fake_channel();
  649. /* setup options so we're sure about what sched we are running */
  650. MOCK(get_options, mock_get_options);
  651. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  652. MOCK(channel_more_to_flush, channel_more_to_flush_mock);
  653. MOCK(channel_write_to_kernel, channel_write_to_kernel_mock);
  654. MOCK(channel_should_write_to_kernel, channel_should_write_to_kernel_mock);
  655. MOCK(update_socket_info_impl, update_socket_info_impl_mock);
  656. clear_options();
  657. mocked_options.KISTSchedRunInterval = 11;
  658. scheduler_init();
  659. tt_assert(ch1);
  660. ch1->magic = TLS_CHAN_MAGIC;
  661. ch1->state = CHANNEL_STATE_OPENING;
  662. ch1->cmux = circuitmux_alloc();
  663. channel_register(ch1);
  664. tt_assert(ch1->registered);
  665. channel_change_state_open(ch1);
  666. scheduler_channel_has_waiting_cells(ch1);
  667. scheduler_channel_wants_writes(ch1);
  668. channel_flush_some_cells_mock_set(ch1, 5);
  669. tt_assert(ch2);
  670. ch2->magic = TLS_CHAN_MAGIC;
  671. ch2->state = CHANNEL_STATE_OPENING;
  672. ch2->cmux = circuitmux_alloc();
  673. channel_register(ch2);
  674. tt_assert(ch2->registered);
  675. channel_change_state_open(ch2);
  676. scheduler_channel_has_waiting_cells(ch2);
  677. scheduler_channel_wants_writes(ch2);
  678. channel_flush_some_cells_mock_set(ch2, 5);
  679. the_scheduler->run();
  680. scheduler_channel_has_waiting_cells(ch1);
  681. channel_flush_some_cells_mock_set(ch1, 5);
  682. the_scheduler->run();
  683. scheduler_channel_has_waiting_cells(ch1);
  684. channel_flush_some_cells_mock_set(ch1, 5);
  685. scheduler_channel_has_waiting_cells(ch2);
  686. channel_flush_some_cells_mock_set(ch2, 5);
  687. the_scheduler->run();
  688. channel_flush_some_cells_mock_free_all();
  689. tt_int_op(1,==,1);
  690. done:
  691. /* Prep the channel so the free() function doesn't explode. */
  692. ch1->state = ch2->state = CHANNEL_STATE_CLOSED;
  693. ch1->registered = ch2->registered = 0;
  694. channel_free(ch1);
  695. channel_free(ch2);
  696. UNMOCK(update_socket_info_impl);
  697. UNMOCK(channel_should_write_to_kernel);
  698. UNMOCK(channel_write_to_kernel);
  699. UNMOCK(channel_more_to_flush);
  700. UNMOCK(channel_flush_some_cells);
  701. UNMOCK(get_options);
  702. scheduler_free_all();
  703. return;
  704. }
  705. static void
  706. test_scheduler_channel_states(void *arg)
  707. {
  708. (void)arg;
  709. perform_channel_state_tests(-1); // vanilla
  710. #ifdef HAVE_KIST_SUPPORT
  711. perform_channel_state_tests(11); // kist
  712. #endif
  713. }
  714. static void
  715. test_scheduler_initfree(void *arg)
  716. {
  717. (void)arg;
  718. tt_ptr_op(channels_pending, ==, NULL);
  719. tt_ptr_op(run_sched_ev, ==, NULL);
  720. mock_event_init();
  721. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  722. scheduler_init();
  723. tt_ptr_op(channels_pending, !=, NULL);
  724. tt_ptr_op(run_sched_ev, !=, NULL);
  725. /* We have specified nothing in the torrc and there's no consensus so the
  726. * KIST scheduler is what should be in use */
  727. #ifdef HAVE_KIST_SUPPORT
  728. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  729. #else
  730. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  731. #endif
  732. tt_int_op(sched_run_interval, ==, 10);
  733. scheduler_free_all();
  734. UNMOCK(tor_libevent_get_base);
  735. mock_event_free_all();
  736. tt_ptr_op(channels_pending, ==, NULL);
  737. tt_ptr_op(run_sched_ev, ==, NULL);
  738. done:
  739. return;
  740. }
  741. static void
  742. test_scheduler_should_use_kist(void *arg)
  743. {
  744. (void)arg;
  745. int res_should, res_freq;
  746. MOCK(get_options, mock_get_options);
  747. /* Test force disabling of KIST */
  748. clear_options();
  749. mocked_options.KISTSchedRunInterval = -1;
  750. res_should = scheduler_should_use_kist();
  751. res_freq = kist_scheduler_run_interval(NULL);
  752. tt_int_op(res_should, ==, 0);
  753. tt_int_op(res_freq, ==, -1);
  754. /* Test force enabling of KIST */
  755. clear_options();
  756. mocked_options.KISTSchedRunInterval = 1234;
  757. res_should = scheduler_should_use_kist();
  758. res_freq = kist_scheduler_run_interval(NULL);
  759. #ifdef HAVE_KIST_SUPPORT
  760. tt_int_op(res_should, ==, 1);
  761. #else /* HAVE_KIST_SUPPORT */
  762. tt_int_op(res_should, ==, 0);
  763. #endif /* HAVE_KIST_SUPPORT */
  764. tt_int_op(res_freq, ==, 1234);
  765. /* Test defer to consensus, but no consensus available */
  766. clear_options();
  767. mocked_options.KISTSchedRunInterval = 0;
  768. res_should = scheduler_should_use_kist();
  769. res_freq = kist_scheduler_run_interval(NULL);
  770. #ifdef HAVE_KIST_SUPPORT
  771. tt_int_op(res_should, ==, 1);
  772. #else /* HAVE_KIST_SUPPORT */
  773. tt_int_op(res_should, ==, 0);
  774. #endif /* HAVE_KIST_SUPPORT */
  775. tt_int_op(res_freq, ==, 10);
  776. /* Test defer to consensus, and kist consensus available */
  777. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  778. clear_options();
  779. mocked_options.KISTSchedRunInterval = 0;
  780. res_should = scheduler_should_use_kist();
  781. res_freq = kist_scheduler_run_interval(NULL);
  782. #ifdef HAVE_KIST_SUPPORT
  783. tt_int_op(res_should, ==, 1);
  784. #else /* HAVE_KIST_SUPPORT */
  785. tt_int_op(res_should, ==, 0);
  786. #endif /* HAVE_KIST_SUPPORT */
  787. tt_int_op(res_freq, ==, 12);
  788. UNMOCK(networkstatus_get_param);
  789. /* Test defer to consensus, and vanilla consensus available */
  790. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  791. clear_options();
  792. mocked_options.KISTSchedRunInterval = 0;
  793. res_should = scheduler_should_use_kist();
  794. res_freq = kist_scheduler_run_interval(NULL);
  795. tt_int_op(res_should, ==, 0);
  796. tt_int_op(res_freq, ==, -1);
  797. UNMOCK(networkstatus_get_param);
  798. done:
  799. UNMOCK(get_options);
  800. return;
  801. }
  802. static void
  803. test_scheduler_ns_changed(void *arg)
  804. {
  805. (void) arg;
  806. /*
  807. * Currently no scheduler implementations use the old/new consensuses passed
  808. * in scheduler_notify_networkstatus_changed, so it is okay to pass NULL.
  809. *
  810. * "But then what does test actually exercise???" It tests that
  811. * scheduler_notify_networkstatus_changed fetches the correct value from the
  812. * consensus, and then switches the scheduler if necessasry.
  813. */
  814. MOCK(get_options, mock_get_options);
  815. clear_options();
  816. tt_ptr_op(the_scheduler, ==, NULL);
  817. /* Change from vanilla to kist via consensus */
  818. the_scheduler = get_vanilla_scheduler();
  819. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  820. scheduler_notify_networkstatus_changed(NULL, NULL);
  821. UNMOCK(networkstatus_get_param);
  822. #ifdef HAVE_KIST_SUPPORT
  823. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  824. #else
  825. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  826. #endif
  827. /* Change from kist to vanilla via consensus */
  828. the_scheduler = get_kist_scheduler();
  829. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  830. scheduler_notify_networkstatus_changed(NULL, NULL);
  831. UNMOCK(networkstatus_get_param);
  832. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  833. /* Doesn't change when using KIST */
  834. the_scheduler = get_kist_scheduler();
  835. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  836. scheduler_notify_networkstatus_changed(NULL, NULL);
  837. UNMOCK(networkstatus_get_param);
  838. #ifdef HAVE_KIST_SUPPORT
  839. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  840. #else
  841. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  842. #endif
  843. /* Doesn't change when using vanilla */
  844. the_scheduler = get_vanilla_scheduler();
  845. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  846. scheduler_notify_networkstatus_changed(NULL, NULL);
  847. UNMOCK(networkstatus_get_param);
  848. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  849. done:
  850. UNMOCK(get_options);
  851. return;
  852. }
  853. struct testcase_t scheduler_tests[] = {
  854. { "compare_channels", test_scheduler_compare_channels,
  855. TT_FORK, NULL, NULL },
  856. { "channel_states", test_scheduler_channel_states, TT_FORK, NULL, NULL },
  857. { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
  858. { "loop_vanilla", test_scheduler_loop_vanilla, TT_FORK, NULL, NULL },
  859. { "loop_kist", test_scheduler_loop_kist, TT_FORK, NULL, NULL },
  860. { "ns_changed", test_scheduler_ns_changed, TT_FORK, NULL, NULL},
  861. { "should_use_kist", test_scheduler_should_use_kist, TT_FORK, NULL, NULL },
  862. END_OF_TESTCASES
  863. };