test_scheduler.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. /* Check if we have any queued */
  254. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  255. return 1;
  256. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  257. flush_mock_channel_t *,
  258. flush_mock_ch) {
  259. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  260. if (flush_mock_ch->chan == chan) {
  261. /* Found it */
  262. found_mock_ch = flush_mock_ch;
  263. break;
  264. }
  265. } else {
  266. /* That shouldn't be there... */
  267. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  268. tor_free(flush_mock_ch);
  269. }
  270. } SMARTLIST_FOREACH_END(flush_mock_ch);
  271. tor_assert(found_mock_ch);
  272. /* Check if any circuits would like to queue some */
  273. /* special for the mock: return the number of cells (instead of 1), or zero
  274. * if nothing to flush */
  275. return (found_mock_ch->cells > 0 ? (int)found_mock_ch->cells : 0 );
  276. }
  277. static void
  278. channel_write_to_kernel_mock(channel_t *chan)
  279. {
  280. (void)chan;
  281. //log_debug(LD_SCHED, "chan=%d writing to kernel",
  282. // (int)chan->global_identifier);
  283. }
  284. static int
  285. channel_should_write_to_kernel_mock(outbuf_table_t *ot, channel_t *chan)
  286. {
  287. (void)ot;
  288. (void)chan;
  289. return 1;
  290. /* We could make this more complicated if we wanted. But I don't think doing
  291. * so tests much of anything */
  292. //static int called_counter = 0;
  293. //if (++called_counter >= 3) {
  294. // called_counter -= 3;
  295. // log_debug(LD_SCHED, "chan=%d should write to kernel",
  296. // (int)chan->global_identifier);
  297. // return 1;
  298. //}
  299. //return 0;
  300. }
  301. static ssize_t
  302. channel_flush_some_cells_mock(channel_t *chan, ssize_t num_cells)
  303. {
  304. ssize_t flushed = 0, max;
  305. char unlimited = 0;
  306. flush_mock_channel_t *found = NULL;
  307. tt_ptr_op(chan, OP_NE, NULL);
  308. if (chan) {
  309. if (num_cells < 0) {
  310. num_cells = 0;
  311. unlimited = 1;
  312. }
  313. /* Check if we have it */
  314. if (chans_for_flush_mock != NULL) {
  315. SMARTLIST_FOREACH_BEGIN(chans_for_flush_mock,
  316. flush_mock_channel_t *,
  317. flush_mock_ch) {
  318. if (flush_mock_ch != NULL && flush_mock_ch->chan != NULL) {
  319. if (flush_mock_ch->chan == chan) {
  320. /* Found it */
  321. found = flush_mock_ch;
  322. break;
  323. }
  324. } else {
  325. /* That shouldn't be there... */
  326. SMARTLIST_DEL_CURRENT(chans_for_flush_mock, flush_mock_ch);
  327. tor_free(flush_mock_ch);
  328. }
  329. } SMARTLIST_FOREACH_END(flush_mock_ch);
  330. if (found) {
  331. /* We found one */
  332. if (found->cells < 0) found->cells = 0;
  333. if (unlimited) max = found->cells;
  334. else max = MIN(found->cells, num_cells);
  335. flushed += max;
  336. found->cells -= max;
  337. }
  338. }
  339. }
  340. done:
  341. return flushed;
  342. }
  343. static void
  344. update_socket_info_impl_mock(socket_table_ent_t *ent)
  345. {
  346. ent->cwnd = ent->unacked = ent->mss = ent->notsent = 0;
  347. ent->limit = INT_MAX;
  348. }
  349. static void
  350. perform_channel_state_tests(int KISTSchedRunInterval, int sched_type)
  351. {
  352. channel_t *ch1 = NULL, *ch2 = NULL;
  353. int old_count;
  354. /* setup options so we're sure about what sched we are running */
  355. MOCK(get_options, mock_get_options);
  356. clear_options();
  357. mocked_options.KISTSchedRunInterval = KISTSchedRunInterval;
  358. set_scheduler_options(sched_type);
  359. /* Set up libevent and scheduler */
  360. mock_event_init();
  361. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  362. scheduler_init();
  363. /*
  364. * Install the compare channels mock so we can test
  365. * scheduler_touch_channel().
  366. */
  367. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  368. /*
  369. * Disable scheduler_run so we can just check the state transitions
  370. * without having to make everything it might call work too.
  371. */
  372. ((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
  373. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  374. /* Set up a fake channel */
  375. ch1 = new_fake_channel();
  376. tt_assert(ch1);
  377. /* Start it off in OPENING */
  378. ch1->state = CHANNEL_STATE_OPENING;
  379. /* We'll need a cmux */
  380. ch1->cmux = circuitmux_alloc();
  381. /* Try to register it */
  382. channel_register(ch1);
  383. tt_assert(ch1->registered);
  384. /* It should start off in SCHED_CHAN_IDLE */
  385. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  386. /* Now get another one */
  387. ch2 = new_fake_channel();
  388. tt_assert(ch2);
  389. ch2->state = CHANNEL_STATE_OPENING;
  390. ch2->cmux = circuitmux_alloc();
  391. channel_register(ch2);
  392. tt_assert(ch2->registered);
  393. /* Send ch1 to SCHED_CHAN_WAITING_TO_WRITE */
  394. scheduler_channel_has_waiting_cells(ch1);
  395. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  396. /* This should send it to SCHED_CHAN_PENDING */
  397. scheduler_channel_wants_writes(ch1);
  398. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  399. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  400. /* Now send ch2 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. /* Drop ch2 back to idle */
  404. scheduler_channel_doesnt_want_writes(ch2);
  405. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  406. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  407. scheduler_channel_wants_writes(ch2);
  408. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  409. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  410. scheduler_channel_has_waiting_cells(ch2);
  411. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  412. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  413. /* This should send ch2 to SCHED_CHAN_WAITING_TO_WRITE */
  414. scheduler_channel_doesnt_want_writes(ch2);
  415. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  416. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  417. /* ...and back to SCHED_CHAN_PENDING */
  418. scheduler_channel_wants_writes(ch2);
  419. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  420. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  421. /* Now we exercise scheduler_touch_channel */
  422. old_count = scheduler_compare_channels_mock_ctr;
  423. scheduler_touch_channel(ch1);
  424. tt_assert(scheduler_compare_channels_mock_ctr > old_count);
  425. /* Close */
  426. channel_mark_for_close(ch1);
  427. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
  428. channel_mark_for_close(ch2);
  429. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  430. channel_closed(ch1);
  431. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
  432. ch1 = NULL;
  433. channel_closed(ch2);
  434. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
  435. ch2 = NULL;
  436. /* Shut things down */
  437. channel_free_all();
  438. scheduler_free_all();
  439. mock_event_free_all();
  440. done:
  441. tor_free(ch1);
  442. tor_free(ch2);
  443. UNMOCK(scheduler_compare_channels);
  444. UNMOCK(tor_libevent_get_base);
  445. UNMOCK(get_options);
  446. cleanup_scheduler_options();
  447. return;
  448. }
  449. static void
  450. test_scheduler_compare_channels(void *arg)
  451. {
  452. /* We don't actually need whole fake channels... */
  453. channel_t c1, c2;
  454. /* ...and some dummy circuitmuxes too */
  455. circuitmux_t *cm1 = NULL, *cm2 = NULL;
  456. int result;
  457. (void)arg;
  458. /* We can't actually see sizeof(circuitmux_t) from here */
  459. cm1 = tor_malloc_zero(sizeof(void *));
  460. cm2 = tor_malloc_zero(sizeof(void *));
  461. c1.cmux = cm1;
  462. c2.cmux = cm2;
  463. /* Configure circuitmux_get_policy() mock */
  464. mock_cgp_tgt_1 = cm1;
  465. mock_cgp_tgt_2 = cm2;
  466. /*
  467. * This is to test the different-policies case, which uses the policy
  468. * cast to an uintptr_t as an arbitrary but definite thing to compare.
  469. */
  470. mock_cgp_val_1 = tor_malloc_zero(16);
  471. mock_cgp_val_2 = tor_malloc_zero(16);
  472. if ( ((uintptr_t) mock_cgp_val_1) > ((uintptr_t) mock_cgp_val_2) ) {
  473. void *tmp = mock_cgp_val_1;
  474. mock_cgp_val_1 = mock_cgp_val_2;
  475. mock_cgp_val_2 = tmp;
  476. }
  477. MOCK(circuitmux_get_policy, circuitmux_get_policy_mock);
  478. /* Now set up circuitmux_compare_muxes() mock using cm1/cm2 */
  479. mock_ccm_tgt_1 = cm1;
  480. mock_ccm_tgt_2 = cm2;
  481. MOCK(circuitmux_compare_muxes, circuitmux_compare_muxes_mock);
  482. /* Equal-channel case */
  483. result = scheduler_compare_channels(&c1, &c1);
  484. tt_int_op(result, OP_EQ, 0);
  485. /* Distinct channels, distinct policies */
  486. result = scheduler_compare_channels(&c1, &c2);
  487. tt_int_op(result, OP_EQ, -1);
  488. result = scheduler_compare_channels(&c2, &c1);
  489. tt_int_op(result, OP_EQ, 1);
  490. /* Distinct channels, same policy */
  491. tor_free(mock_cgp_val_2);
  492. mock_cgp_val_2 = mock_cgp_val_1;
  493. result = scheduler_compare_channels(&c1, &c2);
  494. tt_int_op(result, OP_EQ, -1);
  495. result = scheduler_compare_channels(&c2, &c1);
  496. tt_int_op(result, OP_EQ, 1);
  497. done:
  498. UNMOCK(circuitmux_compare_muxes);
  499. mock_ccm_tgt_1 = NULL;
  500. mock_ccm_tgt_2 = NULL;
  501. UNMOCK(circuitmux_get_policy);
  502. mock_cgp_tgt_1 = NULL;
  503. mock_cgp_tgt_2 = NULL;
  504. tor_free(cm1);
  505. tor_free(cm2);
  506. if (mock_cgp_val_1 != mock_cgp_val_2)
  507. tor_free(mock_cgp_val_1);
  508. tor_free(mock_cgp_val_2);
  509. mock_cgp_val_1 = NULL;
  510. mock_cgp_val_2 = NULL;
  511. return;
  512. }
  513. /******************************************************************************
  514. * The actual tests!
  515. *****************************************************************************/
  516. static void
  517. test_scheduler_loop_vanilla(void *arg)
  518. {
  519. (void)arg;
  520. channel_t *ch1 = NULL, *ch2 = NULL;
  521. void (*run_func_ptr)(void);
  522. /* setup options so we're sure about what sched we are running */
  523. MOCK(get_options, mock_get_options);
  524. clear_options();
  525. set_scheduler_options(SCHEDULER_VANILLA);
  526. mocked_options.KISTSchedRunInterval = 0;
  527. /* Set up libevent and scheduler */
  528. mock_event_init();
  529. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  530. scheduler_init();
  531. /*
  532. * Install the compare channels mock so we can test
  533. * scheduler_touch_channel().
  534. */
  535. MOCK(scheduler_compare_channels, scheduler_compare_channels_mock);
  536. /*
  537. * Disable scheduler_run so we can just check the state transitions
  538. * without having to make everything it might call work too.
  539. */
  540. run_func_ptr = the_scheduler->run;
  541. ((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
  542. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  543. /* Set up a fake channel */
  544. ch1 = new_fake_channel();
  545. ch1->magic = TLS_CHAN_MAGIC;
  546. tt_assert(ch1);
  547. /* Start it off in OPENING */
  548. ch1->state = CHANNEL_STATE_OPENING;
  549. /* We'll need a cmux */
  550. ch1->cmux = circuitmux_alloc();
  551. /* Try to register it */
  552. channel_register(ch1);
  553. tt_assert(ch1->registered);
  554. /* Finish opening it */
  555. channel_change_state_open(ch1);
  556. /* It should start off in SCHED_CHAN_IDLE */
  557. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  558. /* Now get another one */
  559. ch2 = new_fake_channel();
  560. ch2->magic = TLS_CHAN_MAGIC;
  561. tt_assert(ch2);
  562. ch2->state = CHANNEL_STATE_OPENING;
  563. ch2->cmux = circuitmux_alloc();
  564. channel_register(ch2);
  565. tt_assert(ch2->registered);
  566. /*
  567. * Don't open ch2; then channel_num_cells_writeable() will return
  568. * zero and we'll get coverage of that exception case in scheduler_run()
  569. */
  570. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  571. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
  572. /* Send it to SCHED_CHAN_WAITING_TO_WRITE */
  573. scheduler_channel_has_waiting_cells(ch1);
  574. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_TO_WRITE);
  575. /* This should send it to SCHED_CHAN_PENDING */
  576. scheduler_channel_wants_writes(ch1);
  577. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  578. tt_int_op(smartlist_len(channels_pending), OP_EQ, 1);
  579. /* Now send ch2 to SCHED_CHAN_WAITING_FOR_CELLS */
  580. scheduler_channel_wants_writes(ch2);
  581. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  582. /* Drop ch2 back to idle */
  583. scheduler_channel_doesnt_want_writes(ch2);
  584. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  585. /* ...and back to SCHED_CHAN_WAITING_FOR_CELLS */
  586. scheduler_channel_wants_writes(ch2);
  587. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  588. /* ...and this should kick ch2 into SCHED_CHAN_PENDING */
  589. scheduler_channel_has_waiting_cells(ch2);
  590. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  591. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  592. /*
  593. * Now we've got two pending channels and need to fire off
  594. * the scheduler run() that we kept.
  595. */
  596. run_func_ptr();
  597. /*
  598. * Assert that they're still in the states we left and aren't still
  599. * pending
  600. */
  601. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  602. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPENING);
  603. tt_assert(ch1->scheduler_state != SCHED_CHAN_PENDING);
  604. tt_assert(ch2->scheduler_state != SCHED_CHAN_PENDING);
  605. tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
  606. /* Now, finish opening ch2, and get both back to pending */
  607. channel_change_state_open(ch2);
  608. scheduler_channel_wants_writes(ch1);
  609. scheduler_channel_wants_writes(ch2);
  610. scheduler_channel_has_waiting_cells(ch1);
  611. scheduler_channel_has_waiting_cells(ch2);
  612. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_OPEN);
  613. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_OPEN);
  614. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  615. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  616. tt_int_op(smartlist_len(channels_pending), OP_EQ, 2);
  617. /* Now, set up the channel_flush_some_cells() mock */
  618. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  619. /*
  620. * 16 cells on ch1 means it'll completely drain into the 32 cells
  621. * fakechan's num_cells_writeable() returns.
  622. */
  623. channel_flush_some_cells_mock_set(ch1, 16);
  624. /*
  625. * This one should get sent back to pending, since num_cells_writeable()
  626. * will still return non-zero.
  627. */
  628. channel_flush_some_cells_mock_set(ch2, 48);
  629. /*
  630. * And re-run the scheduler run() loop with non-zero returns from
  631. * channel_flush_some_cells() this time.
  632. */
  633. run_func_ptr();
  634. /*
  635. * ch1 should have gone to SCHED_CHAN_WAITING_FOR_CELLS, with 16 flushed
  636. * and 32 writeable.
  637. */
  638. tt_int_op(ch1->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  639. /*
  640. * ...ch2 should also have gone to SCHED_CHAN_WAITING_FOR_CELLS, with
  641. * channel_more_to_flush() returning false and channel_num_cells_writeable()
  642. * > 0/
  643. */
  644. tt_int_op(ch2->scheduler_state, OP_EQ, SCHED_CHAN_WAITING_FOR_CELLS);
  645. /* Close */
  646. channel_mark_for_close(ch1);
  647. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSING);
  648. channel_mark_for_close(ch2);
  649. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  650. channel_closed(ch1);
  651. tt_int_op(ch1->state, OP_EQ, CHANNEL_STATE_CLOSED);
  652. ch1 = NULL;
  653. channel_closed(ch2);
  654. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSED);
  655. ch2 = NULL;
  656. /* Shut things down */
  657. channel_flush_some_cells_mock_free_all();
  658. channel_free_all();
  659. scheduler_free_all();
  660. mock_event_free_all();
  661. done:
  662. tor_free(ch1);
  663. tor_free(ch2);
  664. cleanup_scheduler_options();
  665. UNMOCK(channel_flush_some_cells);
  666. UNMOCK(scheduler_compare_channels);
  667. UNMOCK(tor_libevent_get_base);
  668. UNMOCK(get_options);
  669. }
  670. static void
  671. test_scheduler_loop_kist(void *arg)
  672. {
  673. (void) arg;
  674. #ifndef HAVE_KIST_SUPPORT
  675. return;
  676. #endif
  677. channel_t *ch1 = new_fake_channel(), *ch2 = new_fake_channel();
  678. /* setup options so we're sure about what sched we are running */
  679. MOCK(get_options, mock_get_options);
  680. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  681. MOCK(channel_more_to_flush, channel_more_to_flush_mock);
  682. MOCK(channel_write_to_kernel, channel_write_to_kernel_mock);
  683. MOCK(channel_should_write_to_kernel, channel_should_write_to_kernel_mock);
  684. MOCK(update_socket_info_impl, update_socket_info_impl_mock);
  685. clear_options();
  686. mocked_options.KISTSchedRunInterval = 11;
  687. set_scheduler_options(SCHEDULER_KIST);
  688. scheduler_init();
  689. tt_assert(ch1);
  690. ch1->magic = TLS_CHAN_MAGIC;
  691. ch1->state = CHANNEL_STATE_OPENING;
  692. ch1->cmux = circuitmux_alloc();
  693. channel_register(ch1);
  694. tt_assert(ch1->registered);
  695. channel_change_state_open(ch1);
  696. scheduler_channel_has_waiting_cells(ch1);
  697. scheduler_channel_wants_writes(ch1);
  698. channel_flush_some_cells_mock_set(ch1, 5);
  699. tt_assert(ch2);
  700. ch2->magic = TLS_CHAN_MAGIC;
  701. ch2->state = CHANNEL_STATE_OPENING;
  702. ch2->cmux = circuitmux_alloc();
  703. channel_register(ch2);
  704. tt_assert(ch2->registered);
  705. channel_change_state_open(ch2);
  706. scheduler_channel_has_waiting_cells(ch2);
  707. scheduler_channel_wants_writes(ch2);
  708. channel_flush_some_cells_mock_set(ch2, 5);
  709. the_scheduler->run();
  710. scheduler_channel_has_waiting_cells(ch1);
  711. channel_flush_some_cells_mock_set(ch1, 5);
  712. the_scheduler->run();
  713. scheduler_channel_has_waiting_cells(ch1);
  714. channel_flush_some_cells_mock_set(ch1, 5);
  715. scheduler_channel_has_waiting_cells(ch2);
  716. channel_flush_some_cells_mock_set(ch2, 5);
  717. the_scheduler->run();
  718. channel_flush_some_cells_mock_free_all();
  719. tt_int_op(1,==,1);
  720. done:
  721. /* Prep the channel so the free() function doesn't explode. */
  722. ch1->state = ch2->state = CHANNEL_STATE_CLOSED;
  723. ch1->registered = ch2->registered = 0;
  724. channel_free(ch1);
  725. channel_free(ch2);
  726. UNMOCK(update_socket_info_impl);
  727. UNMOCK(channel_should_write_to_kernel);
  728. UNMOCK(channel_write_to_kernel);
  729. UNMOCK(channel_more_to_flush);
  730. UNMOCK(channel_flush_some_cells);
  731. UNMOCK(get_options);
  732. scheduler_free_all();
  733. return;
  734. }
  735. static void
  736. test_scheduler_channel_states(void *arg)
  737. {
  738. (void)arg;
  739. perform_channel_state_tests(-1, SCHEDULER_VANILLA);
  740. perform_channel_state_tests(11, SCHEDULER_KIST_LITE);
  741. #ifdef HAVE_KIST_SUPPORT
  742. perform_channel_state_tests(11, SCHEDULER_KIST);
  743. #endif
  744. }
  745. static void
  746. test_scheduler_initfree(void *arg)
  747. {
  748. (void)arg;
  749. tt_ptr_op(channels_pending, ==, NULL);
  750. tt_ptr_op(run_sched_ev, ==, NULL);
  751. mock_event_init();
  752. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  753. MOCK(get_options, mock_get_options);
  754. set_scheduler_options(SCHEDULER_KIST);
  755. set_scheduler_options(SCHEDULER_KIST_LITE);
  756. set_scheduler_options(SCHEDULER_VANILLA);
  757. scheduler_init();
  758. tt_ptr_op(channels_pending, !=, NULL);
  759. tt_ptr_op(run_sched_ev, !=, NULL);
  760. /* We have specified nothing in the torrc and there's no consensus so the
  761. * KIST scheduler is what should be in use */
  762. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  763. tt_int_op(sched_run_interval, ==, 10);
  764. scheduler_free_all();
  765. UNMOCK(tor_libevent_get_base);
  766. mock_event_free_all();
  767. tt_ptr_op(channels_pending, ==, NULL);
  768. tt_ptr_op(run_sched_ev, ==, NULL);
  769. done:
  770. UNMOCK(get_options);
  771. cleanup_scheduler_options();
  772. return;
  773. }
  774. static void
  775. test_scheduler_can_use_kist(void *arg)
  776. {
  777. (void)arg;
  778. int res_should, res_freq;
  779. MOCK(get_options, mock_get_options);
  780. /* Test force enabling of KIST */
  781. clear_options();
  782. mocked_options.KISTSchedRunInterval = 1234;
  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, ==, 1234);
  791. /* Test defer to consensus, but no consensus available */
  792. clear_options();
  793. mocked_options.KISTSchedRunInterval = 0;
  794. res_should = scheduler_can_use_kist();
  795. res_freq = kist_scheduler_run_interval(NULL);
  796. #ifdef HAVE_KIST_SUPPORT
  797. tt_int_op(res_should, ==, 1);
  798. #else /* HAVE_KIST_SUPPORT */
  799. tt_int_op(res_should, ==, 0);
  800. #endif /* HAVE_KIST_SUPPORT */
  801. tt_int_op(res_freq, ==, 10);
  802. /* Test defer to consensus, and kist consensus available */
  803. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  804. clear_options();
  805. mocked_options.KISTSchedRunInterval = 0;
  806. res_should = scheduler_can_use_kist();
  807. res_freq = kist_scheduler_run_interval(NULL);
  808. #ifdef HAVE_KIST_SUPPORT
  809. tt_int_op(res_should, ==, 1);
  810. #else /* HAVE_KIST_SUPPORT */
  811. tt_int_op(res_should, ==, 0);
  812. #endif /* HAVE_KIST_SUPPORT */
  813. tt_int_op(res_freq, ==, 12);
  814. UNMOCK(networkstatus_get_param);
  815. /* Test defer to consensus, and vanilla consensus available */
  816. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  817. clear_options();
  818. mocked_options.KISTSchedRunInterval = 0;
  819. res_should = scheduler_can_use_kist();
  820. res_freq = kist_scheduler_run_interval(NULL);
  821. tt_int_op(res_should, ==, 0);
  822. tt_int_op(res_freq, ==, 0);
  823. UNMOCK(networkstatus_get_param);
  824. done:
  825. UNMOCK(get_options);
  826. return;
  827. }
  828. static void
  829. test_scheduler_ns_changed(void *arg)
  830. {
  831. (void) arg;
  832. /*
  833. * Currently no scheduler implementations use the old/new consensuses passed
  834. * in scheduler_notify_networkstatus_changed, so it is okay to pass NULL.
  835. *
  836. * "But then what does test actually exercise???" It tests that
  837. * scheduler_notify_networkstatus_changed fetches the correct value from the
  838. * consensus, and then switches the scheduler if necessasry.
  839. */
  840. MOCK(get_options, mock_get_options);
  841. clear_options();
  842. set_scheduler_options(SCHEDULER_KIST);
  843. set_scheduler_options(SCHEDULER_VANILLA);
  844. tt_ptr_op(the_scheduler, ==, NULL);
  845. /* Change from vanilla to kist via consensus */
  846. the_scheduler = get_vanilla_scheduler();
  847. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  848. scheduler_notify_networkstatus_changed(NULL, NULL);
  849. UNMOCK(networkstatus_get_param);
  850. #ifdef HAVE_KIST_SUPPORT
  851. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  852. #else
  853. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  854. #endif
  855. /* Change from kist to vanilla via consensus */
  856. the_scheduler = get_kist_scheduler();
  857. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  858. scheduler_notify_networkstatus_changed(NULL, NULL);
  859. UNMOCK(networkstatus_get_param);
  860. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  861. /* Doesn't change when using KIST */
  862. the_scheduler = get_kist_scheduler();
  863. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  864. scheduler_notify_networkstatus_changed(NULL, NULL);
  865. UNMOCK(networkstatus_get_param);
  866. #ifdef HAVE_KIST_SUPPORT
  867. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  868. #else
  869. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  870. #endif
  871. /* Doesn't change when using vanilla */
  872. the_scheduler = get_vanilla_scheduler();
  873. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  874. scheduler_notify_networkstatus_changed(NULL, NULL);
  875. UNMOCK(networkstatus_get_param);
  876. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  877. done:
  878. UNMOCK(get_options);
  879. cleanup_scheduler_options();
  880. return;
  881. }
  882. struct testcase_t scheduler_tests[] = {
  883. { "compare_channels", test_scheduler_compare_channels,
  884. TT_FORK, NULL, NULL },
  885. { "channel_states", test_scheduler_channel_states, TT_FORK, NULL, NULL },
  886. { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
  887. { "loop_vanilla", test_scheduler_loop_vanilla, TT_FORK, NULL, NULL },
  888. { "loop_kist", test_scheduler_loop_kist, TT_FORK, NULL, NULL },
  889. { "ns_changed", test_scheduler_ns_changed, TT_FORK, NULL, NULL},
  890. { "should_use_kist", test_scheduler_can_use_kist, TT_FORK, NULL, NULL },
  891. END_OF_TESTCASES
  892. };