test_scheduler.c 29 KB

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