test_scheduler.c 30 KB

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