hibernate.c 38 KB

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