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