test_scheduler.c 28 KB

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