hibernate.c 35 KB

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