test_scheduler.c 31 KB

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