hibernate.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file hibernate.c
  6. * \brief Functions to close listeners, stop allowing new circuits,
  7. * etc in preparation for closing down or going dormant; and to track
  8. * bandwidth and time intervals to know when to hibernate and when to
  9. * stop hibernating.
  10. **/
  11. /*
  12. hibernating, phase 1:
  13. - send destroy in response to create cells
  14. - send end (policy failed) in response to begin cells
  15. - close an OR conn when it has no circuits
  16. hibernating, phase 2:
  17. (entered when bandwidth hard limit reached)
  18. - close all OR/AP/exit conns)
  19. */
  20. #define HIBERNATE_PRIVATE
  21. #include "or.h"
  22. #include "channel.h"
  23. #include "channeltls.h"
  24. #include "config.h"
  25. #include "connection.h"
  26. #include "connection_edge.h"
  27. #include "control.h"
  28. #include "hibernate.h"
  29. #include "main.h"
  30. #include "router.h"
  31. #include "statefile.h"
  32. extern long stats_n_seconds_working; /* published uptime */
  33. /** Are we currently awake, asleep, running out of bandwidth, or shutting
  34. * down? */
  35. static hibernate_state_t hibernate_state = HIBERNATE_STATE_INITIAL;
  36. /** If are hibernating, when do we plan to wake up? Set to 0 if we
  37. * aren't hibernating. */
  38. static time_t hibernate_end_time = 0;
  39. /** If we are shutting down, when do we plan finally exit? Set to 0 if
  40. * we aren't shutting down. */
  41. static time_t shutdown_time = 0;
  42. /** Possible accounting periods. */
  43. typedef enum {
  44. UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
  45. } time_unit_t;
  46. /* Fields for accounting logic. Accounting overview:
  47. *
  48. * Accounting is designed to ensure that no more than N bytes are sent in
  49. * either direction over a given interval (currently, one month, one week, or
  50. * one day) We could
  51. * try to do this by choking our bandwidth to a trickle, but that
  52. * would make our streams useless. Instead, we estimate what our
  53. * bandwidth usage will be, and guess how long we'll be able to
  54. * provide that much bandwidth before hitting our limit. We then
  55. * choose a random time within the accounting interval to come up (so
  56. * that we don't get 50 Tors running on the 1st of the month and none
  57. * on the 30th).
  58. *
  59. * Each interval runs as follows:
  60. *
  61. * 1. We guess our bandwidth usage, based on how much we used
  62. * last time. We choose a "wakeup time" within the interval to come up.
  63. * 2. Until the chosen wakeup time, we hibernate.
  64. * 3. We come up at the wakeup time, and provide bandwidth until we are
  65. * "very close" to running out.
  66. * 4. Then we go into low-bandwidth mode, and stop accepting new
  67. * connections, but provide bandwidth until we run out.
  68. * 5. Then we hibernate until the end of the interval.
  69. *
  70. * If the interval ends before we run out of bandwidth, we go back to
  71. * step one.
  72. */
  73. /** How many bytes have we read in this accounting interval? */
  74. static uint64_t n_bytes_read_in_interval = 0;
  75. /** How many bytes have we written in this accounting interval? */
  76. static uint64_t n_bytes_written_in_interval = 0;
  77. /** How many seconds have we been running this interval? */
  78. static uint32_t n_seconds_active_in_interval = 0;
  79. /** How many seconds were we active in this interval before we hit our soft
  80. * limit? */
  81. static int n_seconds_to_hit_soft_limit = 0;
  82. /** When in this interval was the soft limit hit. */
  83. static time_t soft_limit_hit_at = 0;
  84. /** How many bytes had we read/written when we hit the soft limit? */
  85. static uint64_t n_bytes_at_soft_limit = 0;
  86. /** When did this accounting interval start? */
  87. static time_t interval_start_time = 0;
  88. /** When will this accounting interval end? */
  89. static time_t interval_end_time = 0;
  90. /** How far into the accounting interval should we hibernate? */
  91. static time_t interval_wakeup_time = 0;
  92. /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
  93. * info from the last period.) */
  94. static uint64_t expected_bandwidth_usage = 0;
  95. /** What unit are we using for our accounting? */
  96. static time_unit_t cfg_unit = UNIT_MONTH;
  97. /** How many days,hours,minutes into each unit does our accounting interval
  98. * start? */
  99. /** @{ */
  100. static int cfg_start_day = 0,
  101. cfg_start_hour = 0,
  102. cfg_start_min = 0;
  103. /** @} */
  104. static const char *hibernate_state_to_string(hibernate_state_t state);
  105. static void reset_accounting(time_t now);
  106. static int read_bandwidth_usage(void);
  107. static time_t start_of_accounting_period_after(time_t now);
  108. static time_t start_of_accounting_period_containing(time_t now);
  109. static void accounting_set_wakeup_time(void);
  110. static void on_hibernate_state_change(hibernate_state_t prev_state);
  111. /**
  112. * Return the human-readable name for the hibernation state <b>state</b>
  113. */
  114. static const char *
  115. hibernate_state_to_string(hibernate_state_t state)
  116. {
  117. static char buf[64];
  118. switch (state) {
  119. case HIBERNATE_STATE_EXITING: return "EXITING";
  120. case HIBERNATE_STATE_LOWBANDWIDTH: return "SOFT";
  121. case HIBERNATE_STATE_DORMANT: return "HARD";
  122. case HIBERNATE_STATE_INITIAL:
  123. case HIBERNATE_STATE_LIVE:
  124. return "AWAKE";
  125. default:
  126. log_warn(LD_BUG, "unknown hibernate state %d", state);
  127. tor_snprintf(buf, sizeof(buf), "unknown [%d]", state);
  128. return buf;
  129. }
  130. }
  131. /* ************
  132. * Functions for bandwidth accounting.
  133. * ************/
  134. /** Configure accounting start/end time settings based on
  135. * options->AccountingStart. Return 0 on success, -1 on failure. If
  136. * <b>validate_only</b> is true, do not change the current settings. */
  137. int
  138. accounting_parse_options(const or_options_t *options, int validate_only)
  139. {
  140. time_unit_t unit;
  141. int ok, idx;
  142. long d,h,m;
  143. smartlist_t *items;
  144. const char *v = options->AccountingStart;
  145. const char *s;
  146. char *cp;
  147. if (!v) {
  148. if (!validate_only) {
  149. cfg_unit = UNIT_MONTH;
  150. cfg_start_day = 1;
  151. cfg_start_hour = 0;
  152. cfg_start_min = 0;
  153. }
  154. return 0;
  155. }
  156. items = smartlist_new();
  157. smartlist_split_string(items, v, NULL,
  158. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  159. if (smartlist_len(items)<2) {
  160. log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
  161. goto err;
  162. }
  163. s = smartlist_get(items,0);
  164. if (0==strcasecmp(s, "month")) {
  165. unit = UNIT_MONTH;
  166. } else if (0==strcasecmp(s, "week")) {
  167. unit = UNIT_WEEK;
  168. } else if (0==strcasecmp(s, "day")) {
  169. unit = UNIT_DAY;
  170. } else {
  171. log_warn(LD_CONFIG,
  172. "Unrecognized accounting unit '%s': only 'month', 'week',"
  173. " and 'day' are supported.", s);
  174. goto err;
  175. }
  176. switch (unit) {
  177. case UNIT_WEEK:
  178. d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
  179. if (!ok) {
  180. log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
  181. "1 (Monday) and 7 (Sunday)");
  182. goto err;
  183. }
  184. break;
  185. case UNIT_MONTH:
  186. d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
  187. if (!ok) {
  188. log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
  189. "1 and 28");
  190. goto err;
  191. }
  192. break;
  193. case UNIT_DAY:
  194. d = 0;
  195. break;
  196. /* Coverity dislikes unreachable default cases; some compilers warn on
  197. * switch statements missing a case. Tell Coverity not to worry. */
  198. /* coverity[dead_error_begin] */
  199. default:
  200. tor_assert(0);
  201. }
  202. idx = unit==UNIT_DAY?1:2;
  203. if (smartlist_len(items) != (idx+1)) {
  204. log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
  205. s, idx, (idx>1)?"s":"");
  206. goto err;
  207. }
  208. s = smartlist_get(items, idx);
  209. h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
  210. if (!ok) {
  211. log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
  212. goto err;
  213. }
  214. if (!cp || *cp!=':') {
  215. log_warn(LD_CONFIG,
  216. "Accounting start time not parseable: not in HH:MM format");
  217. goto err;
  218. }
  219. m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
  220. if (!ok) {
  221. log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
  222. goto err;
  223. }
  224. if (!cp || *cp!='\0') {
  225. log_warn(LD_CONFIG,
  226. "Accounting start time not parseable: not in HH:MM format");
  227. goto err;
  228. }
  229. if (!validate_only) {
  230. cfg_unit = unit;
  231. cfg_start_day = (int)d;
  232. cfg_start_hour = (int)h;
  233. cfg_start_min = (int)m;
  234. }
  235. SMARTLIST_FOREACH(items, char *, item, tor_free(item));
  236. smartlist_free(items);
  237. return 0;
  238. err:
  239. SMARTLIST_FOREACH(items, char *, item, tor_free(item));
  240. smartlist_free(items);
  241. return -1;
  242. }
  243. /** If we want to manage the accounting system and potentially
  244. * hibernate, return 1, else return 0.
  245. */
  246. MOCK_IMPL(int,
  247. accounting_is_enabled,(const or_options_t *options))
  248. {
  249. if (options->AccountingMax)
  250. return 1;
  251. return 0;
  252. }
  253. /** If accounting is enabled, return how long (in seconds) this
  254. * interval lasts. */
  255. int
  256. accounting_get_interval_length(void)
  257. {
  258. return (int)(interval_end_time - interval_start_time);
  259. }
  260. /** Return the time at which the current accounting interval will end. */
  261. MOCK_IMPL(time_t,
  262. accounting_get_end_time,(void))
  263. {
  264. return interval_end_time;
  265. }
  266. /** Called from main.c to tell us that <b>seconds</b> seconds have
  267. * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
  268. * bytes have been written. */
  269. void
  270. accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
  271. {
  272. n_bytes_read_in_interval += n_read;
  273. n_bytes_written_in_interval += n_written;
  274. /* If we haven't been called in 10 seconds, we're probably jumping
  275. * around in time. */
  276. n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
  277. }
  278. /** If get_end, return the end of the accounting period that contains
  279. * the time <b>now</b>. Else, return the start of the accounting
  280. * period that contains the time <b>now</b> */
  281. static time_t
  282. edge_of_accounting_period_containing(time_t now, int get_end)
  283. {
  284. int before;
  285. struct tm tm;
  286. tor_localtime_r(&now, &tm);
  287. /* Set 'before' to true iff the current time is before the hh:mm
  288. * changeover time for today. */
  289. before = tm.tm_hour < cfg_start_hour ||
  290. (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
  291. /* Dispatch by unit. First, find the start day of the given period;
  292. * then, if get_end is true, increment to the end day. */
  293. switch (cfg_unit)
  294. {
  295. case UNIT_MONTH: {
  296. /* If this is before the Nth, we want the Nth of last month. */
  297. if (tm.tm_mday < cfg_start_day ||
  298. (tm.tm_mday < cfg_start_day && before)) {
  299. --tm.tm_mon;
  300. }
  301. /* Otherwise, the month is correct. */
  302. tm.tm_mday = cfg_start_day;
  303. if (get_end)
  304. ++tm.tm_mon;
  305. break;
  306. }
  307. case UNIT_WEEK: {
  308. /* What is the 'target' day of the week in struct tm format? (We
  309. say Sunday==7; struct tm says Sunday==0.) */
  310. int wday = cfg_start_day % 7;
  311. /* How many days do we subtract from today to get to the right day? */
  312. int delta = (7+tm.tm_wday-wday)%7;
  313. /* If we are on the right day, but the changeover hasn't happened yet,
  314. * then subtract a whole week. */
  315. if (delta == 0 && before)
  316. delta = 7;
  317. tm.tm_mday -= delta;
  318. if (get_end)
  319. tm.tm_mday += 7;
  320. break;
  321. }
  322. case UNIT_DAY:
  323. if (before)
  324. --tm.tm_mday;
  325. if (get_end)
  326. ++tm.tm_mday;
  327. break;
  328. default:
  329. tor_assert(0);
  330. }
  331. tm.tm_hour = cfg_start_hour;
  332. tm.tm_min = cfg_start_min;
  333. tm.tm_sec = 0;
  334. tm.tm_isdst = -1; /* Autodetect DST */
  335. return mktime(&tm);
  336. }
  337. /** Return the start of the accounting period containing the time
  338. * <b>now</b>. */
  339. static time_t
  340. start_of_accounting_period_containing(time_t now)
  341. {
  342. return edge_of_accounting_period_containing(now, 0);
  343. }
  344. /** Return the start of the accounting period that comes after the one
  345. * containing the time <b>now</b>. */
  346. static time_t
  347. start_of_accounting_period_after(time_t now)
  348. {
  349. return edge_of_accounting_period_containing(now, 1);
  350. }
  351. /** Return the length of the accounting period containing the time
  352. * <b>now</b>. */
  353. static long
  354. length_of_accounting_period_containing(time_t now)
  355. {
  356. return edge_of_accounting_period_containing(now, 1) -
  357. edge_of_accounting_period_containing(now, 0);
  358. }
  359. /** Initialize the accounting subsystem. */
  360. void
  361. configure_accounting(time_t now)
  362. {
  363. time_t s_now;
  364. /* Try to remember our recorded usage. */
  365. if (!interval_start_time)
  366. read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
  367. * reset below.*/
  368. s_now = start_of_accounting_period_containing(now);
  369. if (!interval_start_time) {
  370. /* We didn't have recorded usage; Start a new interval. */
  371. log_info(LD_ACCT, "Starting new accounting interval.");
  372. reset_accounting(now);
  373. } else if (s_now == interval_start_time) {
  374. log_info(LD_ACCT, "Continuing accounting interval.");
  375. /* We are in the interval we thought we were in. Do nothing.*/
  376. interval_end_time = start_of_accounting_period_after(interval_start_time);
  377. } else {
  378. long duration =
  379. length_of_accounting_period_containing(interval_start_time);
  380. double delta = ((double)(s_now - interval_start_time)) / duration;
  381. if (-0.50 <= delta && delta <= 0.50) {
  382. /* The start of the period is now a little later or earlier than we
  383. * remembered. That's fine; we might lose some bytes we could otherwise
  384. * have written, but better to err on the side of obeying people's
  385. * accounting settings. */
  386. log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
  387. "that's fine.", delta*100);
  388. interval_end_time = start_of_accounting_period_after(now);
  389. } else if (delta >= 0.99) {
  390. /* This is the regular time-moved-forward case; don't be too noisy
  391. * about it or people will complain */
  392. log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
  393. reset_accounting(now);
  394. } else {
  395. log_warn(LD_ACCT,
  396. "Mismatched accounting interval: moved by %.02f%%. "
  397. "Starting a fresh one.", delta*100);
  398. reset_accounting(now);
  399. }
  400. }
  401. accounting_set_wakeup_time();
  402. }
  403. /** Return the relevant number of bytes sent/received this interval
  404. * based on the set AccountingRule */
  405. uint64_t
  406. get_accounting_bytes(void)
  407. {
  408. if (get_options()->AccountingRule == ACCT_SUM)
  409. return n_bytes_read_in_interval+n_bytes_written_in_interval;
  410. else if (get_options()->AccountingRule == ACCT_IN)
  411. return n_bytes_read_in_interval;
  412. else if (get_options()->AccountingRule == ACCT_OUT)
  413. return n_bytes_written_in_interval;
  414. else
  415. return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
  416. }
  417. /** Set expected_bandwidth_usage based on how much we sent/received
  418. * per minute last interval (if we were up for at least 30 minutes),
  419. * or based on our declared bandwidth otherwise. */
  420. static void
  421. update_expected_bandwidth(void)
  422. {
  423. uint64_t expected;
  424. const or_options_t *options= get_options();
  425. uint64_t max_configured = (options->RelayBandwidthRate > 0 ?
  426. options->RelayBandwidthRate :
  427. options->BandwidthRate) * 60;
  428. /* max_configured is the larger of bytes read and bytes written
  429. * If we are accounting based on sum, worst case is both are
  430. * at max, doubling the expected sum of bandwidth */
  431. if (get_options()->AccountingRule == ACCT_SUM)
  432. max_configured *= 2;
  433. #define MIN_TIME_FOR_MEASUREMENT (1800)
  434. if (soft_limit_hit_at > interval_start_time && n_bytes_at_soft_limit &&
  435. (soft_limit_hit_at - interval_start_time) > MIN_TIME_FOR_MEASUREMENT) {
  436. /* If we hit our soft limit last time, only count the bytes up to that
  437. * time. This is a better predictor of our actual bandwidth than
  438. * considering the entirety of the last interval, since we likely started
  439. * using bytes very slowly once we hit our soft limit. */
  440. expected = n_bytes_at_soft_limit /
  441. (soft_limit_hit_at - interval_start_time);
  442. expected /= 60;
  443. } else if (n_seconds_active_in_interval >= MIN_TIME_FOR_MEASUREMENT) {
  444. /* Otherwise, we either measured enough time in the last interval but
  445. * never hit our soft limit, or we're using a state file from a Tor that
  446. * doesn't know to store soft-limit info. Just take rate at which
  447. * we were reading/writing in the last interval as our expected rate.
  448. */
  449. uint64_t used = get_accounting_bytes();
  450. expected = used / (n_seconds_active_in_interval / 60);
  451. } else {
  452. /* If we haven't gotten enough data last interval, set 'expected'
  453. * to 0. This will set our wakeup to the start of the interval.
  454. * Next interval, we'll choose our starting time based on how much
  455. * we sent this interval.
  456. */
  457. expected = 0;
  458. }
  459. if (expected > max_configured)
  460. expected = max_configured;
  461. expected_bandwidth_usage = expected;
  462. }
  463. /** Called at the start of a new accounting interval: reset our
  464. * expected bandwidth usage based on what happened last time, set up
  465. * the start and end of the interval, and clear byte/time totals.
  466. */
  467. static void
  468. reset_accounting(time_t now)
  469. {
  470. log_info(LD_ACCT, "Starting new accounting interval.");
  471. update_expected_bandwidth();
  472. interval_start_time = start_of_accounting_period_containing(now);
  473. interval_end_time = start_of_accounting_period_after(interval_start_time);
  474. n_bytes_read_in_interval = 0;
  475. n_bytes_written_in_interval = 0;
  476. n_seconds_active_in_interval = 0;
  477. n_bytes_at_soft_limit = 0;
  478. soft_limit_hit_at = 0;
  479. n_seconds_to_hit_soft_limit = 0;
  480. }
  481. /** Return true iff we should save our bandwidth usage to disk. */
  482. static inline int
  483. time_to_record_bandwidth_usage(time_t now)
  484. {
  485. /* Note every 600 sec */
  486. #define NOTE_INTERVAL (600)
  487. /* Or every 20 megabytes */
  488. #define NOTE_BYTES 20*(1024*1024)
  489. static uint64_t last_read_bytes_noted = 0;
  490. static uint64_t last_written_bytes_noted = 0;
  491. static time_t last_time_noted = 0;
  492. if (last_time_noted + NOTE_INTERVAL <= now ||
  493. last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
  494. last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
  495. (interval_end_time && interval_end_time <= now)) {
  496. last_time_noted = now;
  497. last_read_bytes_noted = n_bytes_read_in_interval;
  498. last_written_bytes_noted = n_bytes_written_in_interval;
  499. return 1;
  500. }
  501. return 0;
  502. }
  503. /** Invoked once per second. Checks whether it is time to hibernate,
  504. * record bandwidth used, etc. */
  505. void
  506. accounting_run_housekeeping(time_t now)
  507. {
  508. if (now >= interval_end_time) {
  509. configure_accounting(now);
  510. }
  511. if (time_to_record_bandwidth_usage(now)) {
  512. if (accounting_record_bandwidth_usage(now, get_or_state())) {
  513. log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
  514. }
  515. }
  516. }
  517. /** Based on our interval and our estimated bandwidth, choose a
  518. * deterministic (but random-ish) time to wake up. */
  519. static void
  520. accounting_set_wakeup_time(void)
  521. {
  522. char digest[DIGEST_LEN];
  523. crypto_digest_t *d_env;
  524. uint64_t time_to_exhaust_bw;
  525. int time_to_consider;
  526. if (! server_identity_key_is_set()) {
  527. if (init_keys() < 0) {
  528. log_err(LD_BUG, "Error initializing keys");
  529. tor_assert(0);
  530. }
  531. }
  532. if (server_identity_key_is_set()) {
  533. char buf[ISO_TIME_LEN+1];
  534. format_iso_time(buf, interval_start_time);
  535. crypto_pk_get_digest(get_server_identity_key(), digest);
  536. d_env = crypto_digest_new();
  537. crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
  538. crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
  539. crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
  540. crypto_digest_free(d_env);
  541. } else {
  542. crypto_rand(digest, DIGEST_LEN);
  543. }
  544. if (!expected_bandwidth_usage) {
  545. char buf1[ISO_TIME_LEN+1];
  546. char buf2[ISO_TIME_LEN+1];
  547. format_local_iso_time(buf1, interval_start_time);
  548. format_local_iso_time(buf2, interval_end_time);
  549. interval_wakeup_time = interval_start_time;
  550. log_notice(LD_ACCT,
  551. "Configured hibernation. This interval begins at %s "
  552. "and ends at %s. We have no prior estimate for bandwidth, so "
  553. "we will start out awake and hibernate when we exhaust our quota.",
  554. buf1, buf2);
  555. return;
  556. }
  557. time_to_exhaust_bw =
  558. (get_options()->AccountingMax/expected_bandwidth_usage)*60;
  559. if (time_to_exhaust_bw > INT_MAX) {
  560. time_to_exhaust_bw = INT_MAX;
  561. time_to_consider = 0;
  562. } else {
  563. time_to_consider = accounting_get_interval_length() -
  564. (int)time_to_exhaust_bw;
  565. }
  566. if (time_to_consider<=0) {
  567. interval_wakeup_time = interval_start_time;
  568. } else {
  569. /* XXX can we simplify this just by picking a random (non-deterministic)
  570. * time to be up? If we go down and come up, then we pick a new one. Is
  571. * that good enough? -RD */
  572. /* This is not a perfectly unbiased conversion, but it is good enough:
  573. * in the worst case, the first half of the day is 0.06 percent likelier
  574. * to be chosen than the last half. */
  575. interval_wakeup_time = interval_start_time +
  576. (get_uint32(digest) % time_to_consider);
  577. }
  578. {
  579. char buf1[ISO_TIME_LEN+1];
  580. char buf2[ISO_TIME_LEN+1];
  581. char buf3[ISO_TIME_LEN+1];
  582. char buf4[ISO_TIME_LEN+1];
  583. time_t down_time;
  584. if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
  585. down_time = TIME_MAX;
  586. else
  587. down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
  588. if (down_time>interval_end_time)
  589. down_time = interval_end_time;
  590. format_local_iso_time(buf1, interval_start_time);
  591. format_local_iso_time(buf2, interval_wakeup_time);
  592. format_local_iso_time(buf3, down_time);
  593. format_local_iso_time(buf4, interval_end_time);
  594. log_notice(LD_ACCT,
  595. "Configured hibernation. This interval began at %s; "
  596. "the scheduled wake-up time %s %s; "
  597. "we expect%s to exhaust our quota for this interval around %s; "
  598. "the next interval begins at %s (all times local)",
  599. buf1,
  600. time(NULL)<interval_wakeup_time?"is":"was", buf2,
  601. time(NULL)<down_time?"":"ed", buf3,
  602. buf4);
  603. }
  604. }
  605. /* This rounds 0 up to 1000, but that's actually a feature. */
  606. #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
  607. /** Save all our bandwidth tracking information to disk. Return 0 on
  608. * success, -1 on failure. */
  609. int
  610. accounting_record_bandwidth_usage(time_t now, or_state_t *state)
  611. {
  612. /* Just update the state */
  613. state->AccountingIntervalStart = interval_start_time;
  614. state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
  615. state->AccountingBytesWrittenInInterval =
  616. ROUND_UP(n_bytes_written_in_interval);
  617. state->AccountingSecondsActive = n_seconds_active_in_interval;
  618. state->AccountingExpectedUsage = expected_bandwidth_usage;
  619. state->AccountingSecondsToReachSoftLimit = n_seconds_to_hit_soft_limit;
  620. state->AccountingSoftLimitHitAt = soft_limit_hit_at;
  621. state->AccountingBytesAtSoftLimit = n_bytes_at_soft_limit;
  622. or_state_mark_dirty(state,
  623. now+(get_options()->AvoidDiskWrites ? 7200 : 60));
  624. return 0;
  625. }
  626. #undef ROUND_UP
  627. /** Read stored accounting information from disk. Return 0 on success;
  628. * return -1 and change nothing on failure. */
  629. static int
  630. read_bandwidth_usage(void)
  631. {
  632. or_state_t *state = get_or_state();
  633. {
  634. char *fname = get_datadir_fname("bw_accounting");
  635. int res;
  636. res = unlink(fname);
  637. if (res != 0) {
  638. log_warn(LD_FS,
  639. "Failed to unlink %s: %s",
  640. fname, strerror(errno));
  641. }
  642. tor_free(fname);
  643. }
  644. if (!state)
  645. return -1;
  646. log_info(LD_ACCT, "Reading bandwidth accounting data from state file");
  647. n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
  648. n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
  649. n_seconds_active_in_interval = state->AccountingSecondsActive;
  650. interval_start_time = state->AccountingIntervalStart;
  651. expected_bandwidth_usage = state->AccountingExpectedUsage;
  652. /* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
  653. * fields. If you switch back and forth, you might get an
  654. * AccountingSoftLimitHitAt value from long before the most recent
  655. * interval_start_time. If that's so, then ignore the softlimit-related
  656. * values. */
  657. if (state->AccountingSoftLimitHitAt > interval_start_time) {
  658. soft_limit_hit_at = state->AccountingSoftLimitHitAt;
  659. n_bytes_at_soft_limit = state->AccountingBytesAtSoftLimit;
  660. n_seconds_to_hit_soft_limit = state->AccountingSecondsToReachSoftLimit;
  661. } else {
  662. soft_limit_hit_at = 0;
  663. n_bytes_at_soft_limit = 0;
  664. n_seconds_to_hit_soft_limit = 0;
  665. }
  666. {
  667. char tbuf1[ISO_TIME_LEN+1];
  668. char tbuf2[ISO_TIME_LEN+1];
  669. format_iso_time(tbuf1, state->LastWritten);
  670. format_iso_time(tbuf2, state->AccountingIntervalStart);
  671. log_info(LD_ACCT,
  672. "Successfully read bandwidth accounting info from state written at %s "
  673. "for interval starting at %s. We have been active for %lu seconds in "
  674. "this interval. At the start of the interval, we expected to use "
  675. "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
  676. U64_FORMAT" bytes written so far)",
  677. tbuf1, tbuf2,
  678. (unsigned long)n_seconds_active_in_interval,
  679. (unsigned long)(expected_bandwidth_usage*1024/60),
  680. U64_PRINTF_ARG(n_bytes_read_in_interval),
  681. U64_PRINTF_ARG(n_bytes_written_in_interval));
  682. }
  683. return 0;
  684. }
  685. /** Return true iff we have sent/received all the bytes we are willing
  686. * to send/receive this interval. */
  687. static int
  688. hibernate_hard_limit_reached(void)
  689. {
  690. uint64_t hard_limit = get_options()->AccountingMax;
  691. if (!hard_limit)
  692. return 0;
  693. return get_accounting_bytes() >= hard_limit;
  694. }
  695. /** Return true iff we have sent/received almost all the bytes we are willing
  696. * to send/receive this interval. */
  697. static int
  698. hibernate_soft_limit_reached(void)
  699. {
  700. const uint64_t acct_max = get_options()->AccountingMax;
  701. #define SOFT_LIM_PCT (.95)
  702. #define SOFT_LIM_BYTES (500*1024*1024)
  703. #define SOFT_LIM_MINUTES (3*60)
  704. /* The 'soft limit' is a fair bit more complicated now than once it was.
  705. * We want to stop accepting connections when ALL of the following are true:
  706. * - We expect to use up the remaining bytes in under 3 hours
  707. * - We have used up 95% of our bytes.
  708. * - We have less than 500MB of bytes left.
  709. */
  710. uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(acct_max) * SOFT_LIM_PCT);
  711. if (acct_max > SOFT_LIM_BYTES && acct_max - SOFT_LIM_BYTES > soft_limit) {
  712. soft_limit = acct_max - SOFT_LIM_BYTES;
  713. }
  714. if (expected_bandwidth_usage) {
  715. const uint64_t expected_usage =
  716. expected_bandwidth_usage * SOFT_LIM_MINUTES;
  717. if (acct_max > expected_usage && acct_max - expected_usage > soft_limit)
  718. soft_limit = acct_max - expected_usage;
  719. }
  720. if (!soft_limit)
  721. return 0;
  722. return get_accounting_bytes() >= soft_limit;
  723. }
  724. /** Called when we get a SIGINT, or when bandwidth soft limit is
  725. * reached. Puts us into "loose hibernation": we don't accept new
  726. * connections, but we continue handling old ones. */
  727. static void
  728. hibernate_begin(hibernate_state_t new_state, time_t now)
  729. {
  730. const or_options_t *options = get_options();
  731. if (new_state == HIBERNATE_STATE_EXITING &&
  732. hibernate_state != HIBERNATE_STATE_LIVE) {
  733. log_notice(LD_GENERAL,"SIGINT received %s; exiting now.",
  734. hibernate_state == HIBERNATE_STATE_EXITING ?
  735. "a second time" : "while hibernating");
  736. tor_cleanup();
  737. exit(0);
  738. }
  739. if (new_state == HIBERNATE_STATE_LOWBANDWIDTH &&
  740. hibernate_state == HIBERNATE_STATE_LIVE) {
  741. soft_limit_hit_at = now;
  742. n_seconds_to_hit_soft_limit = n_seconds_active_in_interval;
  743. n_bytes_at_soft_limit = get_accounting_bytes();
  744. }
  745. /* close listeners. leave control listener(s). */
  746. connection_mark_all_noncontrol_listeners();
  747. /* XXX kill intro point circs */
  748. /* XXX upload rendezvous service descriptors with no intro points */
  749. if (new_state == HIBERNATE_STATE_EXITING) {
  750. log_notice(LD_GENERAL,"Interrupt: we have stopped accepting new "
  751. "connections, and will shut down in %d seconds. Interrupt "
  752. "again to exit now.", options->ShutdownWaitLength);
  753. shutdown_time = time(NULL) + options->ShutdownWaitLength;
  754. } else { /* soft limit reached */
  755. hibernate_end_time = interval_end_time;
  756. }
  757. hibernate_state = new_state;
  758. accounting_record_bandwidth_usage(now, get_or_state());
  759. or_state_mark_dirty(get_or_state(),
  760. get_options()->AvoidDiskWrites ? now+600 : 0);
  761. }
  762. /** Called when we've been hibernating and our timeout is reached. */
  763. static void
  764. hibernate_end(hibernate_state_t new_state)
  765. {
  766. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  767. hibernate_state == HIBERNATE_STATE_DORMANT ||
  768. hibernate_state == HIBERNATE_STATE_INITIAL);
  769. /* listeners will be relaunched in run_scheduled_events() in main.c */
  770. if (hibernate_state != HIBERNATE_STATE_INITIAL)
  771. log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
  772. hibernate_state = new_state;
  773. hibernate_end_time = 0; /* no longer hibernating */
  774. stats_n_seconds_working = 0; /* reset published uptime */
  775. }
  776. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  777. void
  778. hibernate_begin_shutdown(void)
  779. {
  780. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  781. }
  782. /** Return true iff we are currently hibernating. */
  783. MOCK_IMPL(int,
  784. we_are_hibernating,(void))
  785. {
  786. return hibernate_state != HIBERNATE_STATE_LIVE;
  787. }
  788. /** If we aren't currently dormant, close all connections and become
  789. * dormant. */
  790. static void
  791. hibernate_go_dormant(time_t now)
  792. {
  793. connection_t *conn;
  794. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  795. return;
  796. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  797. hibernate_state = HIBERNATE_STATE_DORMANT;
  798. else
  799. hibernate_begin(HIBERNATE_STATE_DORMANT, now);
  800. log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
  801. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  802. * to be able to upload server descriptors so people know we're still
  803. * running, and download directories so we can detect if we're obsolete.
  804. * Leave control conns because we still want to be controllable.
  805. */
  806. while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  807. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  808. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  809. if (CONN_IS_EDGE(conn))
  810. connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
  811. log_info(LD_NET,"Closing conn type %d", conn->type);
  812. if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
  813. connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
  814. END_STREAM_REASON_HIBERNATING);
  815. else if (conn->type == CONN_TYPE_OR) {
  816. if (TO_OR_CONN(conn)->chan) {
  817. channel_mark_for_close(TLS_CHAN_TO_BASE(TO_OR_CONN(conn)->chan));
  818. } else {
  819. connection_mark_for_close(conn);
  820. }
  821. } else
  822. connection_mark_for_close(conn);
  823. }
  824. if (now < interval_wakeup_time)
  825. hibernate_end_time = interval_wakeup_time;
  826. else
  827. hibernate_end_time = interval_end_time;
  828. accounting_record_bandwidth_usage(now, get_or_state());
  829. or_state_mark_dirty(get_or_state(),
  830. get_options()->AvoidDiskWrites ? now+600 : 0);
  831. }
  832. /** Called when hibernate_end_time has arrived. */
  833. static void
  834. hibernate_end_time_elapsed(time_t now)
  835. {
  836. char buf[ISO_TIME_LEN+1];
  837. /* The interval has ended, or it is wakeup time. Find out which. */
  838. accounting_run_housekeeping(now);
  839. if (interval_wakeup_time <= now) {
  840. /* The interval hasn't changed, but interval_wakeup_time has passed.
  841. * It's time to wake up and start being a server. */
  842. hibernate_end(HIBERNATE_STATE_LIVE);
  843. return;
  844. } else {
  845. /* The interval has changed, and it isn't time to wake up yet. */
  846. hibernate_end_time = interval_wakeup_time;
  847. format_iso_time(buf,interval_wakeup_time);
  848. if (hibernate_state != HIBERNATE_STATE_DORMANT) {
  849. /* We weren't sleeping before; we should sleep now. */
  850. log_notice(LD_ACCT,
  851. "Accounting period ended. Commencing hibernation until "
  852. "%s UTC", buf);
  853. hibernate_go_dormant(now);
  854. } else {
  855. log_notice(LD_ACCT,
  856. "Accounting period ended. This period, we will hibernate"
  857. " until %s UTC",buf);
  858. }
  859. }
  860. }
  861. /** Consider our environment and decide if it's time
  862. * to start/stop hibernating.
  863. */
  864. void
  865. consider_hibernation(time_t now)
  866. {
  867. int accounting_enabled = get_options()->AccountingMax != 0;
  868. char buf[ISO_TIME_LEN+1];
  869. hibernate_state_t prev_state = hibernate_state;
  870. /* If we're in 'exiting' mode, then we just shut down after the interval
  871. * elapses. */
  872. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  873. tor_assert(shutdown_time);
  874. if (shutdown_time <= now) {
  875. log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
  876. tor_cleanup();
  877. exit(0);
  878. }
  879. return; /* if exiting soon, don't worry about bandwidth limits */
  880. }
  881. if (hibernate_state == HIBERNATE_STATE_DORMANT) {
  882. /* We've been hibernating because of bandwidth accounting. */
  883. tor_assert(hibernate_end_time);
  884. if (hibernate_end_time > now && accounting_enabled) {
  885. /* If we're hibernating, don't wake up until it's time, regardless of
  886. * whether we're in a new interval. */
  887. return ;
  888. } else {
  889. hibernate_end_time_elapsed(now);
  890. }
  891. }
  892. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  893. * go dormant. */
  894. if (hibernate_state == HIBERNATE_STATE_LIVE ||
  895. hibernate_state == HIBERNATE_STATE_INITIAL) {
  896. if (hibernate_soft_limit_reached()) {
  897. log_notice(LD_ACCT,
  898. "Bandwidth soft limit reached; commencing hibernation. "
  899. "No new connections will be accepted");
  900. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  901. } else if (accounting_enabled && now < interval_wakeup_time) {
  902. format_local_iso_time(buf,interval_wakeup_time);
  903. log_notice(LD_ACCT,
  904. "Commencing hibernation. We will wake up at %s local time.",
  905. buf);
  906. hibernate_go_dormant(now);
  907. } else if (hibernate_state == HIBERNATE_STATE_INITIAL) {
  908. hibernate_end(HIBERNATE_STATE_LIVE);
  909. }
  910. }
  911. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  912. if (!accounting_enabled) {
  913. hibernate_end_time_elapsed(now);
  914. } else if (hibernate_hard_limit_reached()) {
  915. hibernate_go_dormant(now);
  916. } else if (hibernate_end_time <= now) {
  917. /* The hibernation period ended while we were still in lowbandwidth.*/
  918. hibernate_end_time_elapsed(now);
  919. }
  920. }
  921. /* Dispatch a controller event if the hibernation state changed. */
  922. if (hibernate_state != prev_state)
  923. on_hibernate_state_change(prev_state);
  924. }
  925. /** Helper function: called when we get a GETINFO request for an
  926. * accounting-related key on the control connection <b>conn</b>. If we can
  927. * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
  928. * allocated string holding the result. Otherwise, set *<b>answer</b> to
  929. * NULL. */
  930. int
  931. getinfo_helper_accounting(control_connection_t *conn,
  932. const char *question, char **answer,
  933. const char **errmsg)
  934. {
  935. (void) conn;
  936. (void) errmsg;
  937. if (!strcmp(question, "accounting/enabled")) {
  938. *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
  939. } else if (!strcmp(question, "accounting/hibernating")) {
  940. *answer = tor_strdup(hibernate_state_to_string(hibernate_state));
  941. tor_strlower(*answer);
  942. } else if (!strcmp(question, "accounting/bytes")) {
  943. tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
  944. U64_PRINTF_ARG(n_bytes_read_in_interval),
  945. U64_PRINTF_ARG(n_bytes_written_in_interval));
  946. } else if (!strcmp(question, "accounting/bytes-left")) {
  947. uint64_t limit = get_options()->AccountingMax;
  948. if (get_options()->AccountingRule == ACCT_SUM) {
  949. uint64_t total_left = 0;
  950. uint64_t total_bytes = get_accounting_bytes();
  951. if (total_bytes < limit)
  952. total_left = limit - total_bytes;
  953. tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
  954. U64_PRINTF_ARG(total_left), U64_PRINTF_ARG(total_left));
  955. } else if (get_options()->AccountingRule == ACCT_IN) {
  956. uint64_t read_left = 0;
  957. if (n_bytes_read_in_interval < limit)
  958. read_left = limit - n_bytes_read_in_interval;
  959. tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
  960. U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(limit));
  961. } else if (get_options()->AccountingRule == ACCT_OUT) {
  962. uint64_t write_left = 0;
  963. if (n_bytes_written_in_interval < limit)
  964. write_left = limit - n_bytes_written_in_interval;
  965. tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
  966. U64_PRINTF_ARG(limit), U64_PRINTF_ARG(write_left));
  967. } else {
  968. uint64_t read_left = 0, write_left = 0;
  969. if (n_bytes_read_in_interval < limit)
  970. read_left = limit - n_bytes_read_in_interval;
  971. if (n_bytes_written_in_interval < limit)
  972. write_left = limit - n_bytes_written_in_interval;
  973. tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
  974. U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
  975. }
  976. } else if (!strcmp(question, "accounting/interval-start")) {
  977. *answer = tor_malloc(ISO_TIME_LEN+1);
  978. format_iso_time(*answer, interval_start_time);
  979. } else if (!strcmp(question, "accounting/interval-wake")) {
  980. *answer = tor_malloc(ISO_TIME_LEN+1);
  981. format_iso_time(*answer, interval_wakeup_time);
  982. } else if (!strcmp(question, "accounting/interval-end")) {
  983. *answer = tor_malloc(ISO_TIME_LEN+1);
  984. format_iso_time(*answer, interval_end_time);
  985. } else {
  986. *answer = NULL;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * Helper function: called when the hibernation state changes, and sends a
  992. * SERVER_STATUS event to notify interested controllers of the accounting
  993. * state change.
  994. */
  995. static void
  996. on_hibernate_state_change(hibernate_state_t prev_state)
  997. {
  998. (void)prev_state; /* Should we do something with this? */
  999. control_event_server_status(LOG_NOTICE,
  1000. "HIBERNATION_STATUS STATUS=%s",
  1001. hibernate_state_to_string(hibernate_state));
  1002. }
  1003. #ifdef TOR_UNIT_TESTS
  1004. /**
  1005. * Manually change the hibernation state. Private; used only by the unit
  1006. * tests.
  1007. */
  1008. void
  1009. hibernate_set_state_for_testing_(hibernate_state_t newstate)
  1010. {
  1011. hibernate_state = newstate;
  1012. }
  1013. #endif