hibernate.c 38 KB

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