hibernate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  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. #define HIBERNATE_STATE_LIVE 1
  22. #define HIBERNATE_STATE_EXITING 2
  23. #define HIBERNATE_STATE_LOWBANDWIDTH 3
  24. #define HIBERNATE_STATE_DORMANT 4
  25. #define SHUTDOWN_WAIT_LENGTH 30 /* seconds */
  26. static int hibernate_state = HIBERNATE_STATE_LIVE;
  27. /** If are hibernating, when do we plan to wake up? Set to 0 if we
  28. * aren't hibernating. */
  29. static time_t hibernate_end_time = 0;
  30. /* Fields for accounting logic. Accounting overview:
  31. *
  32. * Accounting is designed to ensure that no more than N bytes are sent
  33. * in either direction over a given interval (currently, one month,
  34. * starting at 0:00 GMT an arbitrary day within the month). We could
  35. * try to do this by choking our bandwidth to a trickle, but that
  36. * would make our streams useless. Instead, we estimate what our
  37. * bandwidth usage will be, and guess how long we'll be able to
  38. * provide that much bandwidth before hitting our limit. We then
  39. * choose a random time within the accounting interval to come up (so
  40. * that we don't get 50 Tors running on the 1st of the month and none
  41. * on the 30th).
  42. *
  43. * Each interval runs as follows:
  44. *
  45. * 1. We guess our bandwidth usage, based on how much we used
  46. * last time. We choose a "wakeup time" within the interval to come up.
  47. * 2. Until the chosen wakeup time, we hibernate.
  48. * 3. We come up at the wakeup time, and provide bandwidth until we are
  49. * "very close" to running out.
  50. * 4. Then we go into low-bandwidth mode, and stop accepting new
  51. * connections, but provide bandwidth until we run out.
  52. * 5. Then we hibernate until the end of the interval.
  53. *
  54. * If the interval ends before we run out of bandwdith, we go back to
  55. * step one.
  56. */
  57. /** How many bytes have we read/written in this accounting interval? */
  58. static uint64_t n_bytes_read_in_interval = 0;
  59. static uint64_t n_bytes_written_in_interval = 0;
  60. /** How many seconds have we been running this interval? */
  61. static uint32_t n_seconds_active_in_interval = 0;
  62. /** When did this accounting interval start? */
  63. static time_t interval_start_time = 0;
  64. /** When will this accounting interval end? */
  65. static time_t interval_end_time = 0;
  66. /** How far into the accounting interval should we hibernate? */
  67. static time_t interval_wakeup_time = 0;
  68. /** How much bandwidth do we 'expect' to use per minute? */
  69. static uint32_t expected_bandwidth_usage = 0;
  70. static void reset_accounting(time_t now);
  71. static int read_bandwidth_usage(void);
  72. static int record_bandwidth_usage(time_t now);
  73. static time_t start_of_accounting_period_after(time_t now);
  74. static time_t start_of_accounting_period_containing(time_t now);
  75. static void accounting_set_wakeup_time(void);
  76. /* ************
  77. * Functions for bandwidth accounting.
  78. * ************/
  79. /** Called from main.c to tell us that <b>seconds</b> seconds have
  80. * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
  81. * bytes have been written. */
  82. void
  83. accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
  84. {
  85. n_bytes_read_in_interval += n_read;
  86. n_bytes_written_in_interval += n_written;
  87. /* If we haven't been called in 10 seconds, we're probably jumping
  88. * around in time. */
  89. n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
  90. }
  91. /** Increment the month field of <b>tm</b> by <b>delta</b> months. */
  92. static INLINE void
  93. incr_month(struct tm *tm, unsigned int delta)
  94. {
  95. tm->tm_mon += delta;
  96. /* officially, we don't have to do this, but some platforms are rumored
  97. * to have broken implementations. */
  98. while (tm->tm_mon > 11) {
  99. ++tm->tm_year;
  100. tm->tm_mon -= 12;
  101. }
  102. }
  103. /** Decrement the month field of <b>tm</b> by <b>delta</b> months. */
  104. static INLINE void
  105. decr_month(struct tm *tm, unsigned int delta)
  106. {
  107. tm->tm_mon -= delta;
  108. while (tm->tm_mon < 0) {
  109. --tm->tm_year;
  110. tm->tm_mon += 12;
  111. }
  112. }
  113. /** Return the start of the accounting period that contains the time
  114. * <b>now</b> */
  115. static time_t
  116. start_of_accounting_period_containing(time_t now)
  117. {
  118. struct tm *tm;
  119. /* Only months are supported. */
  120. tm = gmtime(&now);
  121. /* If this is before the Nth, we want the Nth of last month. */
  122. if (tm->tm_mday < get_options()->AccountingStart) {
  123. decr_month(tm, 1);
  124. }
  125. /* Otherwise, the month and year are correct.*/
  126. tm->tm_mday = get_options()->AccountingStart;
  127. tm->tm_hour = 0;
  128. tm->tm_min = 0;
  129. tm->tm_sec = 0;
  130. return tor_timegm(tm);
  131. }
  132. /** Return the start of the accounting period that comes after the one
  133. * containing the time <b>now</b>. */
  134. static time_t
  135. start_of_accounting_period_after(time_t now)
  136. {
  137. time_t start;
  138. struct tm *tm;
  139. start = start_of_accounting_period_containing(now);
  140. tm = gmtime(&start);
  141. incr_month(tm, 1);
  142. return tor_timegm(tm);
  143. }
  144. /** Initialize the accounting subsystem. */
  145. void
  146. configure_accounting(time_t now)
  147. {
  148. /* Try to remember our recorded usage. */
  149. if (!interval_start_time)
  150. read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
  151. * reset below.*/
  152. if (!interval_start_time ||
  153. start_of_accounting_period_after(interval_start_time) <= now) {
  154. /* We didn't have recorded usage, or we don't have recorded usage
  155. * for this interval. Start a new interval. */
  156. log_fn(LOG_INFO, "Starting new accounting interval.");
  157. reset_accounting(now);
  158. } if (interval_start_time ==
  159. start_of_accounting_period_containing(interval_start_time)) {
  160. log_fn(LOG_INFO, "Continuing accounting interval.");
  161. /* We are in the interval we thought we were in. Do nothing.*/
  162. } else {
  163. log_fn(LOG_WARN, "Mismatched accounting interval; starting a fresh one.");
  164. reset_accounting(now);
  165. }
  166. accounting_set_wakeup_time();
  167. }
  168. /** Set expected_bandwidth_usage based on how much we sent/received
  169. * per minute last interval (if we were up for at least 30 minutes),
  170. * or based on our declared bandwidth otherwise. */
  171. static void
  172. update_expected_bandwidth(void)
  173. {
  174. uint64_t used;
  175. uint32_t max_configured = (get_options()->BandwidthRateBytes * 60);
  176. /* XXX max_configured will be false if it exceeds
  177. * get_options()->AccountingMaxKB*1000, right? -RD
  178. */
  179. if (n_seconds_active_in_interval < 1800) {
  180. expected_bandwidth_usage = max_configured;
  181. } else {
  182. used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
  183. n_bytes_read_in_interval : n_bytes_written_in_interval;
  184. expected_bandwidth_usage = (uint32_t)
  185. (used / (n_seconds_active_in_interval / 60));
  186. if (expected_bandwidth_usage > max_configured)
  187. expected_bandwidth_usage = max_configured;
  188. }
  189. }
  190. /** Called at the start of a new accounting interval: reset our
  191. * expected bandwidth usage based on what happened last time, set up
  192. * the start and end of the interval, and clear byte/time totals.
  193. */
  194. static void
  195. reset_accounting(time_t now) {
  196. log_fn(LOG_INFO, "Starting new accounting interval.");
  197. update_expected_bandwidth();
  198. interval_start_time = start_of_accounting_period_containing(now);
  199. interval_end_time = start_of_accounting_period_after(interval_start_time);
  200. n_bytes_read_in_interval = 0;
  201. n_bytes_written_in_interval = 0;
  202. n_seconds_active_in_interval = 0;
  203. }
  204. /** Return true iff we should save our bandwidth usage to disk. */
  205. static INLINE int
  206. time_to_record_bandwidth_usage(time_t now)
  207. {
  208. /* Note every 5 minutes */
  209. #define NOTE_INTERVAL (5*60)
  210. /* Or every 20 megabytes */
  211. #define NOTE_BYTES 20*(1024*1024)
  212. static uint64_t last_read_bytes_noted = 0;
  213. static uint64_t last_written_bytes_noted = 0;
  214. static time_t last_time_noted = 0;
  215. if (last_time_noted + NOTE_INTERVAL <= now ||
  216. last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
  217. last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
  218. (interval_end_time && interval_end_time <= now)) {
  219. last_time_noted = now;
  220. last_read_bytes_noted = n_bytes_read_in_interval;
  221. last_written_bytes_noted = n_bytes_written_in_interval;
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. void
  227. accounting_run_housekeeping(time_t now)
  228. {
  229. if (now >= interval_end_time) {
  230. configure_accounting(now);
  231. }
  232. if (time_to_record_bandwidth_usage(now)) {
  233. if (record_bandwidth_usage(now)) {
  234. log_fn(LOG_ERR, "Couldn't record bandwidth usage; exiting.");
  235. exit(1);
  236. }
  237. }
  238. }
  239. /** Based on our interval and our estimated bandwidth, choose a
  240. * deterministic (but random-ish) time to wake up. */
  241. static void
  242. accounting_set_wakeup_time(void)
  243. {
  244. struct tm *tm;
  245. char buf[ISO_TIME_LEN+1];
  246. char digest[DIGEST_LEN];
  247. crypto_digest_env_t *d;
  248. int n_days_in_interval;
  249. int n_days_to_exhaust_bw;
  250. int n_days_to_consider;
  251. format_iso_time(buf, interval_start_time);
  252. crypto_pk_get_digest(get_identity_key(), digest);
  253. d = crypto_new_digest_env();
  254. crypto_digest_add_bytes(d, buf, ISO_TIME_LEN);
  255. crypto_digest_add_bytes(d, digest, DIGEST_LEN);
  256. crypto_digest_get_digest(d, digest, DIGEST_LEN);
  257. crypto_free_digest_env(d);
  258. n_days_to_exhaust_bw =
  259. (get_options()->AccountingMaxKB/expected_bandwidth_usage)/(24*60);
  260. tm = gmtime(&interval_start_time);
  261. if (++tm->tm_mon > 11) { tm->tm_mon = 0; ++tm->tm_year; }
  262. n_days_in_interval = (tor_timegm(tm)-interval_start_time+1)/(24*60*60);
  263. n_days_to_consider = n_days_in_interval - n_days_to_exhaust_bw;
  264. /* XXX can we simplify this just by picking a random (non-deterministic)
  265. * time to be up? If we go down and come up, then we pick a new one. Is
  266. * that good enough? -RD */
  267. while (((unsigned char)digest[0]) > n_days_to_consider)
  268. crypto_digest(digest, digest, DIGEST_LEN);
  269. interval_wakeup_time = interval_start_time +
  270. 24*60*60 * (unsigned char)digest[0];
  271. }
  272. #define BW_ACCOUNTING_VERSION 1
  273. /** Save all our bandwidth tracking information to disk. Return 0 on
  274. * success, -1 on failure*/
  275. static int
  276. record_bandwidth_usage(time_t now)
  277. {
  278. char buf[128];
  279. char fname[512];
  280. char time1[ISO_TIME_LEN+1];
  281. char time2[ISO_TIME_LEN+1];
  282. char *cp = buf;
  283. /* Format is:
  284. Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
  285. format_iso_time(time1, interval_start_time);
  286. format_iso_time(time2, now);
  287. tor_snprintf(cp, sizeof(buf),
  288. "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
  289. BW_ACCOUNTING_VERSION,
  290. time1,
  291. time2,
  292. U64_PRINTF_ARG(n_bytes_read_in_interval),
  293. U64_PRINTF_ARG(n_bytes_written_in_interval),
  294. (unsigned long)n_seconds_active_in_interval,
  295. (unsigned long)expected_bandwidth_usage);
  296. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  297. get_options()->DataDirectory);
  298. return write_str_to_file(fname, buf, 0);
  299. }
  300. /** Read stored accounting information from disk. Return 0 on success;
  301. * return -1 and change nothing on failure. */
  302. static int
  303. read_bandwidth_usage(void)
  304. {
  305. char *s = NULL;
  306. char fname[512];
  307. time_t t1, t2;
  308. uint64_t n_read, n_written;
  309. uint32_t expected_bw, n_seconds;
  310. smartlist_t *elts;
  311. int ok;
  312. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  313. get_options()->DataDirectory);
  314. if (!(s = read_file_to_str(fname, 0))) {
  315. return 0;
  316. }
  317. elts = smartlist_create();
  318. smartlist_split_string(elts, s, "\n", SPLIT_SKIP_SPACE, SPLIT_IGNORE_BLANK);
  319. tor_free(s);
  320. if (smartlist_len(elts)<1 ||
  321. atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
  322. log_fn(LOG_WARN, "Unrecognized bw_accounting file version: %s",
  323. (const char*)smartlist_get(elts,0));
  324. goto err;
  325. }
  326. if (smartlist_len(elts) < 7) {
  327. log_fn(LOG_WARN, "Corrupted bw_accounting file: %d lines",
  328. smartlist_len(elts));
  329. goto err;
  330. }
  331. if (parse_iso_time(smartlist_get(elts,1), &t1)) {
  332. log_fn(LOG_WARN, "Error parsing bandwidth usage start time.");
  333. goto err;
  334. }
  335. if (parse_iso_time(smartlist_get(elts,2), &t2)) {
  336. log_fn(LOG_WARN, "Error parsing bandwidth usage last-written time");
  337. goto err;
  338. }
  339. n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
  340. &ok, NULL);
  341. if (!ok) {
  342. log_fn(LOG_WARN, "Error parsing number of bytes read");
  343. goto err;
  344. }
  345. n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
  346. &ok, NULL);
  347. if (!ok) {
  348. log_fn(LOG_WARN, "Error parsing number of bytes read");
  349. goto err;
  350. }
  351. n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
  352. &ok, NULL);
  353. if (!ok) {
  354. log_fn(LOG_WARN, "Error parsing number of seconds live");
  355. goto err;
  356. }
  357. expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
  358. &ok, NULL);
  359. if (!ok) {
  360. log_fn(LOG_WARN, "Error parsing expected bandwidth");
  361. goto err;
  362. }
  363. n_bytes_read_in_interval = n_read;
  364. n_bytes_written_in_interval = n_written;
  365. n_seconds_active_in_interval = n_seconds;
  366. interval_start_time = t1;
  367. expected_bandwidth_usage = expected_bw;
  368. accounting_set_wakeup_time();
  369. return 0;
  370. err:
  371. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  372. smartlist_free(elts);
  373. return -1;
  374. }
  375. /** Return true iff we have sent/received all the bytes we are willing
  376. * to send/receive this interval. */
  377. static int
  378. hibernate_hard_limit_reached(void)
  379. {
  380. uint64_t hard_limit = get_options()->AccountingMaxKB<<10;
  381. if (!hard_limit)
  382. return 0;
  383. return n_bytes_read_in_interval >= hard_limit
  384. || n_bytes_written_in_interval >= hard_limit;
  385. }
  386. /** Return true iff we have sent/received almost all the bytes we are willing
  387. * to send/receive this interval. */
  388. static int hibernate_soft_limit_reached(void)
  389. {
  390. uint64_t soft_limit = (uint64_t) ((get_options()->AccountingMaxKB<<10) * .99);
  391. if (!soft_limit)
  392. return 0;
  393. return n_bytes_read_in_interval >= soft_limit
  394. || n_bytes_written_in_interval >= soft_limit;
  395. }
  396. /** Called when we get a SIGINT, or when bandwidth soft limit is
  397. * reached. Puts us into "loose hibernation": we don't accept new
  398. * connections, but we continue handling old ones. */
  399. static void hibernate_begin(int new_state, time_t now) {
  400. connection_t *conn;
  401. if(hibernate_state == HIBERNATE_STATE_EXITING) {
  402. /* we've been called twice now. close immediately. */
  403. log(LOG_NOTICE,"Second sigint received; exiting now.");
  404. tor_cleanup();
  405. exit(0);
  406. }
  407. /* close listeners. leave control listener(s). */
  408. while((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
  409. (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
  410. (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
  411. log_fn(LOG_INFO,"Closing listener type %d", conn->type);
  412. connection_mark_for_close(conn);
  413. }
  414. /* XXX kill intro point circs */
  415. /* XXX upload rendezvous service descriptors with no intro points */
  416. if(new_state == HIBERNATE_STATE_EXITING) {
  417. log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH);
  418. hibernate_end_time = time(NULL) + SHUTDOWN_WAIT_LENGTH;
  419. } else { /* soft limit reached */
  420. hibernate_end_time = interval_end_time;
  421. }
  422. hibernate_state = new_state;
  423. }
  424. /** Called when we've been hibernating and our timeout is reached. */
  425. static void
  426. hibernate_end(int new_state) {
  427. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  428. hibernate_state == HIBERNATE_STATE_DORMANT);
  429. /* listeners will be relaunched in run_scheduled_events() in main.c */
  430. log_fn(LOG_NOTICE,"Hibernation period ended. Resuming normal activity.");
  431. hibernate_state = new_state;
  432. hibernate_end_time = 0; /* no longer hibernating */
  433. }
  434. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  435. void
  436. hibernate_begin_shutdown(void) {
  437. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  438. }
  439. /** Return true iff we are currently hibernating. */
  440. int
  441. we_are_hibernating(void) {
  442. return hibernate_state != HIBERNATE_STATE_LIVE;
  443. }
  444. /** If we aren't currently dormant, close all connections and become
  445. * dormant. */
  446. static void
  447. hibernate_go_dormant(void) {
  448. connection_t *conn;
  449. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  450. return;
  451. hibernate_state = HIBERNATE_STATE_DORMANT;
  452. log_fn(LOG_NOTICE,"Going dormant. Blowing away remaining connections.");
  453. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  454. * to be able to upload server descriptors so people know we're still
  455. * running, and download directories so we can detect if we're obsolete.
  456. * Leave control conns because we still want to be controllable.
  457. */
  458. while((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  459. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  460. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  461. log_fn(LOG_INFO,"Closing conn type %d", conn->type);
  462. connection_mark_for_close(conn);
  463. }
  464. }
  465. /** Called when hibernate_end_time has arrived. */
  466. static void
  467. hibernate_end_time_elapsed(time_t now)
  468. {
  469. /* The interval has ended, or it is wakeup time. Find out which. */
  470. accounting_run_housekeeping(now);
  471. if (interval_wakeup_time <= now) {
  472. /* The interval hasn't changed, but interval_wakeup_time has passed.
  473. * It's time to wake up and start being a server. */
  474. hibernate_end(HIBERNATE_STATE_LIVE);
  475. return;
  476. } else {
  477. /* The interval has changed, and it isn't time to wake up yet. */
  478. hibernate_end_time = interval_wakeup_time;
  479. if (hibernate_state != HIBERNATE_STATE_DORMANT)
  480. /* We weren't sleeping before; we should sleep now. */
  481. hibernate_go_dormant();
  482. }
  483. }
  484. /** The big function. Consider our environment and decide if it's time
  485. * to start/stop hibernating.
  486. */
  487. void consider_hibernation(time_t now) {
  488. /* If we're in 'exiting' mode, then we just shut down after the interval
  489. * elapses. */
  490. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  491. tor_assert(hibernate_end_time);
  492. if(hibernate_end_time <= now) {
  493. log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
  494. tor_cleanup();
  495. exit(0);
  496. }
  497. return; /* if exiting soon, don't worry about bandwidth limits */
  498. }
  499. if(hibernate_state == HIBERNATE_STATE_DORMANT) {
  500. /* We've been hibernating because of bandwidth accounting. */
  501. tor_assert(hibernate_end_time);
  502. if (hibernate_end_time > now) {
  503. /* If we're hibernating, don't wake up until it's time, regardless of
  504. * whether we're in a new interval. */
  505. return ;
  506. } else {
  507. hibernate_end_time_elapsed(now);
  508. }
  509. }
  510. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  511. * go dormant. */
  512. if (hibernate_state == HIBERNATE_STATE_LIVE &&
  513. hibernate_soft_limit_reached()) {
  514. log_fn(LOG_NOTICE,"Bandwidth soft limit reached; commencing hibernation.");
  515. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  516. }
  517. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  518. if (hibernate_hard_limit_reached()) {
  519. hibernate_go_dormant();
  520. } else if (hibernate_end_time <= now) {
  521. /* The hibernation period ended while we were still in lowbandwidth.*/
  522. hibernate_end_time_elapsed(now);
  523. }
  524. }
  525. }