test_scheduler.c 30 KB

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