hibernate.c 34 KB

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