test_scheduler.c 31 KB

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