test_scheduler.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. channel_t *ch3 = new_fake_channel();
  670. /* setup options so we're sure about what sched we are running */
  671. MOCK(get_options, mock_get_options);
  672. MOCK(channel_flush_some_cells, channel_flush_some_cells_mock);
  673. MOCK(channel_more_to_flush, channel_more_to_flush_mock);
  674. MOCK(channel_write_to_kernel, channel_write_to_kernel_mock);
  675. MOCK(channel_should_write_to_kernel, channel_should_write_to_kernel_mock);
  676. MOCK(update_socket_info_impl, update_socket_info_impl_mock);
  677. clear_options();
  678. mocked_options.KISTSchedRunInterval = 11;
  679. set_scheduler_options(SCHEDULER_KIST);
  680. scheduler_init();
  681. tt_assert(ch1);
  682. ch1->magic = TLS_CHAN_MAGIC;
  683. ch1->state = CHANNEL_STATE_OPENING;
  684. channel_register(ch1);
  685. tt_assert(ch1->registered);
  686. channel_change_state_open(ch1);
  687. scheduler_channel_has_waiting_cells(ch1);
  688. scheduler_channel_wants_writes(ch1);
  689. channel_flush_some_cells_mock_set(ch1, 5);
  690. tt_assert(ch2);
  691. ch2->magic = TLS_CHAN_MAGIC;
  692. ch2->state = CHANNEL_STATE_OPENING;
  693. channel_register(ch2);
  694. tt_assert(ch2->registered);
  695. channel_change_state_open(ch2);
  696. scheduler_channel_has_waiting_cells(ch2);
  697. scheduler_channel_wants_writes(ch2);
  698. channel_flush_some_cells_mock_set(ch2, 5);
  699. the_scheduler->run();
  700. scheduler_channel_has_waiting_cells(ch1);
  701. channel_flush_some_cells_mock_set(ch1, 5);
  702. the_scheduler->run();
  703. scheduler_channel_has_waiting_cells(ch1);
  704. channel_flush_some_cells_mock_set(ch1, 5);
  705. scheduler_channel_has_waiting_cells(ch2);
  706. channel_flush_some_cells_mock_set(ch2, 5);
  707. the_scheduler->run();
  708. channel_flush_some_cells_mock_free_all();
  709. /* We'll try to run this closed channel threw the scheduler loop and make
  710. * sure it ends up in the right state. */
  711. tt_assert(ch3);
  712. ch3->magic = TLS_CHAN_MAGIC;
  713. ch3->state = CHANNEL_STATE_OPEN;
  714. ch3->cmux = circuitmux_alloc();
  715. channel_register(ch3);
  716. tt_assert(ch3->registered);
  717. ch3->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  718. scheduler_channel_has_waiting_cells(ch3);
  719. /* Should be in the pending list now waiting to be handled. */
  720. tt_int_op(ch3->scheduler_state, OP_EQ, SCHED_CHAN_PENDING);
  721. tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 1);
  722. /* By running the scheduler on a closed channel, it should end up in the
  723. * IDLE state and not in the pending channel list. */
  724. ch3->state = CHANNEL_STATE_CLOSED;
  725. the_scheduler->run();
  726. tt_int_op(ch3->scheduler_state, OP_EQ, SCHED_CHAN_IDLE);
  727. tt_int_op(smartlist_len(get_channels_pending()), OP_EQ, 0);
  728. done:
  729. /* Prep the channel so the free() function doesn't explode. */
  730. ch1->state = ch2->state = ch3->state = CHANNEL_STATE_CLOSED;
  731. ch1->registered = ch2->registered = ch3->registered = 0;
  732. channel_free(ch1);
  733. channel_free(ch2);
  734. channel_free(ch3);
  735. UNMOCK(update_socket_info_impl);
  736. UNMOCK(channel_should_write_to_kernel);
  737. UNMOCK(channel_write_to_kernel);
  738. UNMOCK(channel_more_to_flush);
  739. UNMOCK(channel_flush_some_cells);
  740. UNMOCK(get_options);
  741. scheduler_free_all();
  742. return;
  743. }
  744. static void
  745. test_scheduler_channel_states(void *arg)
  746. {
  747. (void)arg;
  748. perform_channel_state_tests(-1, SCHEDULER_VANILLA);
  749. perform_channel_state_tests(11, SCHEDULER_KIST_LITE);
  750. #ifdef HAVE_KIST_SUPPORT
  751. perform_channel_state_tests(11, SCHEDULER_KIST);
  752. #endif
  753. }
  754. static void
  755. test_scheduler_initfree(void *arg)
  756. {
  757. (void)arg;
  758. tt_ptr_op(channels_pending, ==, NULL);
  759. tt_ptr_op(run_sched_ev, ==, NULL);
  760. mock_event_init();
  761. MOCK(tor_libevent_get_base, tor_libevent_get_base_mock);
  762. MOCK(get_options, mock_get_options);
  763. set_scheduler_options(SCHEDULER_KIST);
  764. set_scheduler_options(SCHEDULER_KIST_LITE);
  765. set_scheduler_options(SCHEDULER_VANILLA);
  766. scheduler_init();
  767. tt_ptr_op(channels_pending, !=, NULL);
  768. tt_ptr_op(run_sched_ev, !=, NULL);
  769. /* We have specified nothing in the torrc and there's no consensus so the
  770. * KIST scheduler is what should be in use */
  771. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  772. tt_int_op(sched_run_interval, ==, 10);
  773. scheduler_free_all();
  774. UNMOCK(tor_libevent_get_base);
  775. mock_event_free_all();
  776. tt_ptr_op(channels_pending, ==, NULL);
  777. tt_ptr_op(run_sched_ev, ==, NULL);
  778. done:
  779. UNMOCK(get_options);
  780. cleanup_scheduler_options();
  781. return;
  782. }
  783. static void
  784. test_scheduler_can_use_kist(void *arg)
  785. {
  786. (void)arg;
  787. int res_should, res_freq;
  788. MOCK(get_options, mock_get_options);
  789. /* Test force enabling of KIST */
  790. clear_options();
  791. mocked_options.KISTSchedRunInterval = 1234;
  792. res_should = scheduler_can_use_kist();
  793. res_freq = kist_scheduler_run_interval(NULL);
  794. #ifdef HAVE_KIST_SUPPORT
  795. tt_int_op(res_should, ==, 1);
  796. #else /* HAVE_KIST_SUPPORT */
  797. tt_int_op(res_should, ==, 0);
  798. #endif /* HAVE_KIST_SUPPORT */
  799. tt_int_op(res_freq, ==, 1234);
  800. /* Test defer to consensus, but no consensus available */
  801. clear_options();
  802. mocked_options.KISTSchedRunInterval = 0;
  803. res_should = scheduler_can_use_kist();
  804. res_freq = kist_scheduler_run_interval(NULL);
  805. #ifdef HAVE_KIST_SUPPORT
  806. tt_int_op(res_should, ==, 1);
  807. #else /* HAVE_KIST_SUPPORT */
  808. tt_int_op(res_should, ==, 0);
  809. #endif /* HAVE_KIST_SUPPORT */
  810. tt_int_op(res_freq, ==, 10);
  811. /* Test defer to consensus, and kist consensus available */
  812. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  813. clear_options();
  814. mocked_options.KISTSchedRunInterval = 0;
  815. res_should = scheduler_can_use_kist();
  816. res_freq = kist_scheduler_run_interval(NULL);
  817. #ifdef HAVE_KIST_SUPPORT
  818. tt_int_op(res_should, ==, 1);
  819. #else /* HAVE_KIST_SUPPORT */
  820. tt_int_op(res_should, ==, 0);
  821. #endif /* HAVE_KIST_SUPPORT */
  822. tt_int_op(res_freq, ==, 12);
  823. UNMOCK(networkstatus_get_param);
  824. /* Test defer to consensus, and vanilla consensus available */
  825. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  826. clear_options();
  827. mocked_options.KISTSchedRunInterval = 0;
  828. res_should = scheduler_can_use_kist();
  829. res_freq = kist_scheduler_run_interval(NULL);
  830. tt_int_op(res_should, ==, 0);
  831. tt_int_op(res_freq, ==, 0);
  832. UNMOCK(networkstatus_get_param);
  833. done:
  834. UNMOCK(get_options);
  835. return;
  836. }
  837. static void
  838. test_scheduler_ns_changed(void *arg)
  839. {
  840. (void) arg;
  841. /*
  842. * Currently no scheduler implementations use the old/new consensuses passed
  843. * in scheduler_notify_networkstatus_changed, so it is okay to pass NULL.
  844. *
  845. * "But then what does test actually exercise???" It tests that
  846. * scheduler_notify_networkstatus_changed fetches the correct value from the
  847. * consensus, and then switches the scheduler if necessasry.
  848. */
  849. MOCK(get_options, mock_get_options);
  850. clear_options();
  851. set_scheduler_options(SCHEDULER_KIST);
  852. set_scheduler_options(SCHEDULER_VANILLA);
  853. tt_ptr_op(the_scheduler, ==, NULL);
  854. /* Change from vanilla to kist via consensus */
  855. the_scheduler = get_vanilla_scheduler();
  856. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  857. scheduler_notify_networkstatus_changed(NULL, NULL);
  858. UNMOCK(networkstatus_get_param);
  859. #ifdef HAVE_KIST_SUPPORT
  860. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  861. #else
  862. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  863. #endif
  864. /* Change from kist to vanilla via consensus */
  865. the_scheduler = get_kist_scheduler();
  866. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  867. scheduler_notify_networkstatus_changed(NULL, NULL);
  868. UNMOCK(networkstatus_get_param);
  869. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  870. /* Doesn't change when using KIST */
  871. the_scheduler = get_kist_scheduler();
  872. MOCK(networkstatus_get_param, mock_kist_networkstatus_get_param);
  873. scheduler_notify_networkstatus_changed(NULL, NULL);
  874. UNMOCK(networkstatus_get_param);
  875. #ifdef HAVE_KIST_SUPPORT
  876. tt_ptr_op(the_scheduler, ==, get_kist_scheduler());
  877. #else
  878. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  879. #endif
  880. /* Doesn't change when using vanilla */
  881. the_scheduler = get_vanilla_scheduler();
  882. MOCK(networkstatus_get_param, mock_vanilla_networkstatus_get_param);
  883. scheduler_notify_networkstatus_changed(NULL, NULL);
  884. UNMOCK(networkstatus_get_param);
  885. tt_ptr_op(the_scheduler, ==, get_vanilla_scheduler());
  886. done:
  887. UNMOCK(get_options);
  888. cleanup_scheduler_options();
  889. return;
  890. }
  891. struct testcase_t scheduler_tests[] = {
  892. { "compare_channels", test_scheduler_compare_channels,
  893. TT_FORK, NULL, NULL },
  894. { "channel_states", test_scheduler_channel_states, TT_FORK, NULL, NULL },
  895. { "initfree", test_scheduler_initfree, TT_FORK, NULL, NULL },
  896. { "loop_vanilla", test_scheduler_loop_vanilla, TT_FORK, NULL, NULL },
  897. { "loop_kist", test_scheduler_loop_kist, TT_FORK, NULL, NULL },
  898. { "ns_changed", test_scheduler_ns_changed, TT_FORK, NULL, NULL},
  899. { "should_use_kist", test_scheduler_can_use_kist, TT_FORK, NULL, NULL },
  900. END_OF_TESTCASES
  901. };