test_scheduler.c 29 KB

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