hibernate.c 35 KB

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