hibernation: Rescan the event list on state change
[tor.git] / src / or / hibernate.c
blobc4ae63acc49df42ad144dc4c8c76eba58ea41158
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2017, 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.
12 * Ordinarily a Tor relay is "Live".
14 * A live relay can stop accepting connections for one of two reasons: either
15 * it is trying to conserve bandwidth because of bandwidth accounting rules
16 * ("soft hibernation"), or it is about to shut down ("exiting").
17 **/
20 hibernating, phase 1:
21 - send destroy in response to create cells
22 - send end (policy failed) in response to begin cells
23 - close an OR conn when it has no circuits
25 hibernating, phase 2:
26 (entered when bandwidth hard limit reached)
27 - close all OR/AP/exit conns)
30 #define HIBERNATE_PRIVATE
31 #include "or.h"
32 #include "channel.h"
33 #include "channeltls.h"
34 #include "config.h"
35 #include "connection.h"
36 #include "connection_edge.h"
37 #include "connection_or.h"
38 #include "control.h"
39 #include "hibernate.h"
40 #include "main.h"
41 #include "router.h"
42 #include "statefile.h"
44 /** Are we currently awake, asleep, running out of bandwidth, or shutting
45 * down? */
46 static hibernate_state_t hibernate_state = HIBERNATE_STATE_INITIAL;
47 /** If are hibernating, when do we plan to wake up? Set to 0 if we
48 * aren't hibernating. */
49 static time_t hibernate_end_time = 0;
50 /** If we are shutting down, when do we plan finally exit? Set to 0 if
51 * we aren't shutting down. */
52 static time_t shutdown_time = 0;
54 /** Possible accounting periods. */
55 typedef enum {
56 UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
57 } time_unit_t;
60 * @file hibernate.c
62 * <h4>Accounting</h4>
63 * Accounting is designed to ensure that no more than N bytes are sent in
64 * either direction over a given interval (currently, one month, one week, or
65 * one day) We could
66 * try to do this by choking our bandwidth to a trickle, but that
67 * would make our streams useless. Instead, we estimate what our
68 * bandwidth usage will be, and guess how long we'll be able to
69 * provide that much bandwidth before hitting our limit. We then
70 * choose a random time within the accounting interval to come up (so
71 * that we don't get 50 Tors running on the 1st of the month and none
72 * on the 30th).
74 * Each interval runs as follows:
76 * <ol>
77 * <li>We guess our bandwidth usage, based on how much we used
78 * last time. We choose a "wakeup time" within the interval to come up.
79 * <li>Until the chosen wakeup time, we hibernate.
80 * <li> We come up at the wakeup time, and provide bandwidth until we are
81 * "very close" to running out.
82 * <li> Then we go into low-bandwidth mode, and stop accepting new
83 * connections, but provide bandwidth until we run out.
84 * <li> Then we hibernate until the end of the interval.
86 * If the interval ends before we run out of bandwidth, we go back to
87 * step one.
89 * Accounting is controlled by the AccountingMax, AccountingRule, and
90 * AccountingStart options.
93 /** How many bytes have we read in this accounting interval? */
94 static uint64_t n_bytes_read_in_interval = 0;
95 /** How many bytes have we written in this accounting interval? */
96 static uint64_t n_bytes_written_in_interval = 0;
97 /** How many seconds have we been running this interval? */
98 static uint32_t n_seconds_active_in_interval = 0;
99 /** How many seconds were we active in this interval before we hit our soft
100 * limit? */
101 static int n_seconds_to_hit_soft_limit = 0;
102 /** When in this interval was the soft limit hit. */
103 static time_t soft_limit_hit_at = 0;
104 /** How many bytes had we read/written when we hit the soft limit? */
105 static uint64_t n_bytes_at_soft_limit = 0;
106 /** When did this accounting interval start? */
107 static time_t interval_start_time = 0;
108 /** When will this accounting interval end? */
109 static time_t interval_end_time = 0;
110 /** How far into the accounting interval should we hibernate? */
111 static time_t interval_wakeup_time = 0;
112 /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
113 * info from the last period.) */
114 static uint64_t expected_bandwidth_usage = 0;
115 /** What unit are we using for our accounting? */
116 static time_unit_t cfg_unit = UNIT_MONTH;
118 /** How many days,hours,minutes into each unit does our accounting interval
119 * start? */
120 /** @{ */
121 static int cfg_start_day = 0,
122 cfg_start_hour = 0,
123 cfg_start_min = 0;
124 /** @} */
126 static const char *hibernate_state_to_string(hibernate_state_t state);
127 static void reset_accounting(time_t now);
128 static int read_bandwidth_usage(void);
129 static time_t start_of_accounting_period_after(time_t now);
130 static time_t start_of_accounting_period_containing(time_t now);
131 static void accounting_set_wakeup_time(void);
132 static void on_hibernate_state_change(hibernate_state_t prev_state);
135 * Return the human-readable name for the hibernation state <b>state</b>
137 static const char *
138 hibernate_state_to_string(hibernate_state_t state)
140 static char buf[64];
141 switch (state) {
142 case HIBERNATE_STATE_EXITING: return "EXITING";
143 case HIBERNATE_STATE_LOWBANDWIDTH: return "SOFT";
144 case HIBERNATE_STATE_DORMANT: return "HARD";
145 case HIBERNATE_STATE_INITIAL:
146 case HIBERNATE_STATE_LIVE:
147 return "AWAKE";
148 default:
149 log_warn(LD_BUG, "unknown hibernate state %d", state);
150 tor_snprintf(buf, sizeof(buf), "unknown [%d]", state);
151 return buf;
155 /* ************
156 * Functions for bandwidth accounting.
157 * ************/
159 /** Configure accounting start/end time settings based on
160 * options->AccountingStart. Return 0 on success, -1 on failure. If
161 * <b>validate_only</b> is true, do not change the current settings. */
163 accounting_parse_options(const or_options_t *options, int validate_only)
165 time_unit_t unit;
166 int ok, idx;
167 long d,h,m;
168 smartlist_t *items;
169 const char *v = options->AccountingStart;
170 const char *s;
171 char *cp;
173 if (!v) {
174 if (!validate_only) {
175 cfg_unit = UNIT_MONTH;
176 cfg_start_day = 1;
177 cfg_start_hour = 0;
178 cfg_start_min = 0;
180 return 0;
183 items = smartlist_new();
184 smartlist_split_string(items, v, NULL,
185 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
186 if (smartlist_len(items)<2) {
187 log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
188 goto err;
190 s = smartlist_get(items,0);
191 if (0==strcasecmp(s, "month")) {
192 unit = UNIT_MONTH;
193 } else if (0==strcasecmp(s, "week")) {
194 unit = UNIT_WEEK;
195 } else if (0==strcasecmp(s, "day")) {
196 unit = UNIT_DAY;
197 } else {
198 log_warn(LD_CONFIG,
199 "Unrecognized accounting unit '%s': only 'month', 'week',"
200 " and 'day' are supported.", s);
201 goto err;
204 switch (unit) {
205 case UNIT_WEEK:
206 d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
207 if (!ok) {
208 log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
209 "1 (Monday) and 7 (Sunday)");
210 goto err;
212 break;
213 case UNIT_MONTH:
214 d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
215 if (!ok) {
216 log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
217 "1 and 28");
218 goto err;
220 break;
221 case UNIT_DAY:
222 d = 0;
223 break;
224 /* Coverity dislikes unreachable default cases; some compilers warn on
225 * switch statements missing a case. Tell Coverity not to worry. */
226 /* coverity[dead_error_begin] */
227 default:
228 tor_assert(0);
231 idx = unit==UNIT_DAY?1:2;
232 if (smartlist_len(items) != (idx+1)) {
233 log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
234 s, idx, (idx>1)?"s":"");
235 goto err;
237 s = smartlist_get(items, idx);
238 h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
239 if (!ok) {
240 log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
241 goto err;
243 if (!cp || *cp!=':') {
244 log_warn(LD_CONFIG,
245 "Accounting start time not parseable: not in HH:MM format");
246 goto err;
248 m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
249 if (!ok) {
250 log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
251 goto err;
253 if (!cp || *cp!='\0') {
254 log_warn(LD_CONFIG,
255 "Accounting start time not parseable: not in HH:MM format");
256 goto err;
259 if (!validate_only) {
260 cfg_unit = unit;
261 cfg_start_day = (int)d;
262 cfg_start_hour = (int)h;
263 cfg_start_min = (int)m;
265 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
266 smartlist_free(items);
267 return 0;
268 err:
269 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
270 smartlist_free(items);
271 return -1;
274 /** If we want to manage the accounting system and potentially
275 * hibernate, return 1, else return 0.
277 MOCK_IMPL(int,
278 accounting_is_enabled,(const or_options_t *options))
280 if (options->AccountingMax)
281 return 1;
282 return 0;
285 /** If accounting is enabled, return how long (in seconds) this
286 * interval lasts. */
288 accounting_get_interval_length(void)
290 return (int)(interval_end_time - interval_start_time);
293 /** Return the time at which the current accounting interval will end. */
294 MOCK_IMPL(time_t,
295 accounting_get_end_time,(void))
297 return interval_end_time;
300 /** Called from connection.c to tell us that <b>seconds</b> seconds have
301 * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
302 * bytes have been written. */
303 void
304 accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
306 n_bytes_read_in_interval += n_read;
307 n_bytes_written_in_interval += n_written;
308 /* If we haven't been called in 10 seconds, we're probably jumping
309 * around in time. */
310 n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
313 /** If get_end, return the end of the accounting period that contains
314 * the time <b>now</b>. Else, return the start of the accounting
315 * period that contains the time <b>now</b> */
316 static time_t
317 edge_of_accounting_period_containing(time_t now, int get_end)
319 int before;
320 struct tm tm;
321 tor_localtime_r(&now, &tm);
323 /* Set 'before' to true iff the current time is before the hh:mm
324 * changeover time for today. */
325 before = tm.tm_hour < cfg_start_hour ||
326 (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
328 /* Dispatch by unit. First, find the start day of the given period;
329 * then, if get_end is true, increment to the end day. */
330 switch (cfg_unit)
332 case UNIT_MONTH: {
333 /* If this is before the Nth, we want the Nth of last month. */
334 if (tm.tm_mday < cfg_start_day ||
335 (tm.tm_mday == cfg_start_day && before)) {
336 --tm.tm_mon;
338 /* Otherwise, the month is correct. */
339 tm.tm_mday = cfg_start_day;
340 if (get_end)
341 ++tm.tm_mon;
342 break;
344 case UNIT_WEEK: {
345 /* What is the 'target' day of the week in struct tm format? (We
346 say Sunday==7; struct tm says Sunday==0.) */
347 int wday = cfg_start_day % 7;
348 /* How many days do we subtract from today to get to the right day? */
349 int delta = (7+tm.tm_wday-wday)%7;
350 /* If we are on the right day, but the changeover hasn't happened yet,
351 * then subtract a whole week. */
352 if (delta == 0 && before)
353 delta = 7;
354 tm.tm_mday -= delta;
355 if (get_end)
356 tm.tm_mday += 7;
357 break;
359 case UNIT_DAY:
360 if (before)
361 --tm.tm_mday;
362 if (get_end)
363 ++tm.tm_mday;
364 break;
365 default:
366 tor_assert(0);
369 tm.tm_hour = cfg_start_hour;
370 tm.tm_min = cfg_start_min;
371 tm.tm_sec = 0;
372 tm.tm_isdst = -1; /* Autodetect DST */
373 return mktime(&tm);
376 /** Return the start of the accounting period containing the time
377 * <b>now</b>. */
378 static time_t
379 start_of_accounting_period_containing(time_t now)
381 return edge_of_accounting_period_containing(now, 0);
384 /** Return the start of the accounting period that comes after the one
385 * containing the time <b>now</b>. */
386 static time_t
387 start_of_accounting_period_after(time_t now)
389 return edge_of_accounting_period_containing(now, 1);
392 /** Return the length of the accounting period containing the time
393 * <b>now</b>. */
394 static long
395 length_of_accounting_period_containing(time_t now)
397 return edge_of_accounting_period_containing(now, 1) -
398 edge_of_accounting_period_containing(now, 0);
401 /** Initialize the accounting subsystem. */
402 void
403 configure_accounting(time_t now)
405 time_t s_now;
406 /* Try to remember our recorded usage. */
407 if (!interval_start_time)
408 read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
409 * reset below.*/
411 s_now = start_of_accounting_period_containing(now);
413 if (!interval_start_time) {
414 /* We didn't have recorded usage; Start a new interval. */
415 log_info(LD_ACCT, "Starting new accounting interval.");
416 reset_accounting(now);
417 } else if (s_now == interval_start_time) {
418 log_info(LD_ACCT, "Continuing accounting interval.");
419 /* We are in the interval we thought we were in. Do nothing.*/
420 interval_end_time = start_of_accounting_period_after(interval_start_time);
421 } else {
422 long duration =
423 length_of_accounting_period_containing(interval_start_time);
424 double delta = ((double)(s_now - interval_start_time)) / duration;
425 if (-0.50 <= delta && delta <= 0.50) {
426 /* The start of the period is now a little later or earlier than we
427 * remembered. That's fine; we might lose some bytes we could otherwise
428 * have written, but better to err on the side of obeying accounting
429 * settings. */
430 log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
431 "that's fine.", delta*100);
432 interval_end_time = start_of_accounting_period_after(now);
433 } else if (delta >= 0.99) {
434 /* This is the regular time-moved-forward case; don't be too noisy
435 * about it or people will complain */
436 log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
437 reset_accounting(now);
438 } else {
439 log_warn(LD_ACCT,
440 "Mismatched accounting interval: moved by %.02f%%. "
441 "Starting a fresh one.", delta*100);
442 reset_accounting(now);
445 accounting_set_wakeup_time();
448 /** Return the relevant number of bytes sent/received this interval
449 * based on the set AccountingRule */
450 uint64_t
451 get_accounting_bytes(void)
453 if (get_options()->AccountingRule == ACCT_SUM)
454 return n_bytes_read_in_interval+n_bytes_written_in_interval;
455 else if (get_options()->AccountingRule == ACCT_IN)
456 return n_bytes_read_in_interval;
457 else if (get_options()->AccountingRule == ACCT_OUT)
458 return n_bytes_written_in_interval;
459 else
460 return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
463 /** Set expected_bandwidth_usage based on how much we sent/received
464 * per minute last interval (if we were up for at least 30 minutes),
465 * or based on our declared bandwidth otherwise. */
466 static void
467 update_expected_bandwidth(void)
469 uint64_t expected;
470 const or_options_t *options= get_options();
471 uint64_t max_configured = (options->RelayBandwidthRate > 0 ?
472 options->RelayBandwidthRate :
473 options->BandwidthRate) * 60;
474 /* max_configured is the larger of bytes read and bytes written
475 * If we are accounting based on sum, worst case is both are
476 * at max, doubling the expected sum of bandwidth */
477 if (get_options()->AccountingRule == ACCT_SUM)
478 max_configured *= 2;
480 #define MIN_TIME_FOR_MEASUREMENT (1800)
482 if (soft_limit_hit_at > interval_start_time && n_bytes_at_soft_limit &&
483 (soft_limit_hit_at - interval_start_time) > MIN_TIME_FOR_MEASUREMENT) {
484 /* If we hit our soft limit last time, only count the bytes up to that
485 * time. This is a better predictor of our actual bandwidth than
486 * considering the entirety of the last interval, since we likely started
487 * using bytes very slowly once we hit our soft limit. */
488 expected = n_bytes_at_soft_limit /
489 (soft_limit_hit_at - interval_start_time);
490 expected /= 60;
491 } else if (n_seconds_active_in_interval >= MIN_TIME_FOR_MEASUREMENT) {
492 /* Otherwise, we either measured enough time in the last interval but
493 * never hit our soft limit, or we're using a state file from a Tor that
494 * doesn't know to store soft-limit info. Just take rate at which
495 * we were reading/writing in the last interval as our expected rate.
497 uint64_t used = get_accounting_bytes();
498 expected = used / (n_seconds_active_in_interval / 60);
499 } else {
500 /* If we haven't gotten enough data last interval, set 'expected'
501 * to 0. This will set our wakeup to the start of the interval.
502 * Next interval, we'll choose our starting time based on how much
503 * we sent this interval.
505 expected = 0;
507 if (expected > max_configured)
508 expected = max_configured;
509 expected_bandwidth_usage = expected;
512 /** Called at the start of a new accounting interval: reset our
513 * expected bandwidth usage based on what happened last time, set up
514 * the start and end of the interval, and clear byte/time totals.
516 static void
517 reset_accounting(time_t now)
519 log_info(LD_ACCT, "Starting new accounting interval.");
520 update_expected_bandwidth();
521 interval_start_time = start_of_accounting_period_containing(now);
522 interval_end_time = start_of_accounting_period_after(interval_start_time);
523 n_bytes_read_in_interval = 0;
524 n_bytes_written_in_interval = 0;
525 n_seconds_active_in_interval = 0;
526 n_bytes_at_soft_limit = 0;
527 soft_limit_hit_at = 0;
528 n_seconds_to_hit_soft_limit = 0;
531 /** Return true iff we should save our bandwidth usage to disk. */
532 static inline int
533 time_to_record_bandwidth_usage(time_t now)
535 /* Note every 600 sec */
536 #define NOTE_INTERVAL (600)
537 /* Or every 20 megabytes */
538 #define NOTE_BYTES 20*(1024*1024)
539 static uint64_t last_read_bytes_noted = 0;
540 static uint64_t last_written_bytes_noted = 0;
541 static time_t last_time_noted = 0;
543 if (last_time_noted + NOTE_INTERVAL <= now ||
544 last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
545 last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
546 (interval_end_time && interval_end_time <= now)) {
547 last_time_noted = now;
548 last_read_bytes_noted = n_bytes_read_in_interval;
549 last_written_bytes_noted = n_bytes_written_in_interval;
550 return 1;
552 return 0;
555 /** Invoked once per second. Checks whether it is time to hibernate,
556 * record bandwidth used, etc. */
557 void
558 accounting_run_housekeeping(time_t now)
560 if (now >= interval_end_time) {
561 configure_accounting(now);
563 if (time_to_record_bandwidth_usage(now)) {
564 if (accounting_record_bandwidth_usage(now, get_or_state())) {
565 log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
570 /** Based on our interval and our estimated bandwidth, choose a
571 * deterministic (but random-ish) time to wake up. */
572 static void
573 accounting_set_wakeup_time(void)
575 char digest[DIGEST_LEN];
576 crypto_digest_t *d_env;
577 uint64_t time_to_exhaust_bw;
578 int time_to_consider;
580 if (! server_identity_key_is_set()) {
581 if (init_keys() < 0) {
582 log_err(LD_BUG, "Error initializing keys");
583 tor_assert(0);
587 if (server_identity_key_is_set()) {
588 char buf[ISO_TIME_LEN+1];
589 format_iso_time(buf, interval_start_time);
591 if (crypto_pk_get_digest(get_server_identity_key(), digest) < 0) {
592 log_err(LD_BUG, "Error getting our key's digest.");
593 tor_assert(0);
596 d_env = crypto_digest_new();
597 crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
598 crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
599 crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
600 crypto_digest_free(d_env);
601 } else {
602 crypto_rand(digest, DIGEST_LEN);
605 if (!expected_bandwidth_usage) {
606 char buf1[ISO_TIME_LEN+1];
607 char buf2[ISO_TIME_LEN+1];
608 format_local_iso_time(buf1, interval_start_time);
609 format_local_iso_time(buf2, interval_end_time);
610 interval_wakeup_time = interval_start_time;
612 log_notice(LD_ACCT,
613 "Configured hibernation. This interval begins at %s "
614 "and ends at %s. We have no prior estimate for bandwidth, so "
615 "we will start out awake and hibernate when we exhaust our quota.",
616 buf1, buf2);
617 return;
620 time_to_exhaust_bw =
621 (get_options()->AccountingMax/expected_bandwidth_usage)*60;
622 if (time_to_exhaust_bw > INT_MAX) {
623 time_to_exhaust_bw = INT_MAX;
624 time_to_consider = 0;
625 } else {
626 time_to_consider = accounting_get_interval_length() -
627 (int)time_to_exhaust_bw;
630 if (time_to_consider<=0) {
631 interval_wakeup_time = interval_start_time;
632 } else {
633 /* XXX can we simplify this just by picking a random (non-deterministic)
634 * time to be up? If we go down and come up, then we pick a new one. Is
635 * that good enough? -RD */
637 /* This is not a perfectly unbiased conversion, but it is good enough:
638 * in the worst case, the first half of the day is 0.06 percent likelier
639 * to be chosen than the last half. */
640 interval_wakeup_time = interval_start_time +
641 (get_uint32(digest) % time_to_consider);
645 char buf1[ISO_TIME_LEN+1];
646 char buf2[ISO_TIME_LEN+1];
647 char buf3[ISO_TIME_LEN+1];
648 char buf4[ISO_TIME_LEN+1];
649 time_t down_time;
650 if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
651 down_time = TIME_MAX;
652 else
653 down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
654 if (down_time>interval_end_time)
655 down_time = interval_end_time;
656 format_local_iso_time(buf1, interval_start_time);
657 format_local_iso_time(buf2, interval_wakeup_time);
658 format_local_iso_time(buf3, down_time);
659 format_local_iso_time(buf4, interval_end_time);
661 log_notice(LD_ACCT,
662 "Configured hibernation. This interval began at %s; "
663 "the scheduled wake-up time %s %s; "
664 "we expect%s to exhaust our quota for this interval around %s; "
665 "the next interval begins at %s (all times local)",
666 buf1,
667 time(NULL)<interval_wakeup_time?"is":"was", buf2,
668 time(NULL)<down_time?"":"ed", buf3,
669 buf4);
673 /* This rounds 0 up to 1000, but that's actually a feature. */
674 #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
675 /** Save all our bandwidth tracking information to disk. Return 0 on
676 * success, -1 on failure. */
678 accounting_record_bandwidth_usage(time_t now, or_state_t *state)
680 /* Just update the state */
681 state->AccountingIntervalStart = interval_start_time;
682 state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
683 state->AccountingBytesWrittenInInterval =
684 ROUND_UP(n_bytes_written_in_interval);
685 state->AccountingSecondsActive = n_seconds_active_in_interval;
686 state->AccountingExpectedUsage = expected_bandwidth_usage;
688 state->AccountingSecondsToReachSoftLimit = n_seconds_to_hit_soft_limit;
689 state->AccountingSoftLimitHitAt = soft_limit_hit_at;
690 state->AccountingBytesAtSoftLimit = n_bytes_at_soft_limit;
692 or_state_mark_dirty(state,
693 now+(get_options()->AvoidDiskWrites ? 7200 : 60));
695 return 0;
697 #undef ROUND_UP
699 /** Read stored accounting information from disk. Return 0 on success;
700 * return -1 and change nothing on failure. */
701 static int
702 read_bandwidth_usage(void)
704 or_state_t *state = get_or_state();
707 char *fname = get_datadir_fname("bw_accounting");
708 int res;
710 res = unlink(fname);
711 if (res != 0 && errno != ENOENT) {
712 log_warn(LD_FS,
713 "Failed to unlink %s: %s",
714 fname, strerror(errno));
717 tor_free(fname);
720 if (!state)
721 return -1;
723 log_info(LD_ACCT, "Reading bandwidth accounting data from state file");
724 n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
725 n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
726 n_seconds_active_in_interval = state->AccountingSecondsActive;
727 interval_start_time = state->AccountingIntervalStart;
728 expected_bandwidth_usage = state->AccountingExpectedUsage;
730 /* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
731 * fields. If you switch back and forth, you might get an
732 * AccountingSoftLimitHitAt value from long before the most recent
733 * interval_start_time. If that's so, then ignore the softlimit-related
734 * values. */
735 if (state->AccountingSoftLimitHitAt > interval_start_time) {
736 soft_limit_hit_at = state->AccountingSoftLimitHitAt;
737 n_bytes_at_soft_limit = state->AccountingBytesAtSoftLimit;
738 n_seconds_to_hit_soft_limit = state->AccountingSecondsToReachSoftLimit;
739 } else {
740 soft_limit_hit_at = 0;
741 n_bytes_at_soft_limit = 0;
742 n_seconds_to_hit_soft_limit = 0;
746 char tbuf1[ISO_TIME_LEN+1];
747 char tbuf2[ISO_TIME_LEN+1];
748 format_iso_time(tbuf1, state->LastWritten);
749 format_iso_time(tbuf2, state->AccountingIntervalStart);
751 log_info(LD_ACCT,
752 "Successfully read bandwidth accounting info from state written at %s "
753 "for interval starting at %s. We have been active for %lu seconds in "
754 "this interval. At the start of the interval, we expected to use "
755 "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
756 U64_FORMAT" bytes written so far)",
757 tbuf1, tbuf2,
758 (unsigned long)n_seconds_active_in_interval,
759 (unsigned long)(expected_bandwidth_usage*1024/60),
760 U64_PRINTF_ARG(n_bytes_read_in_interval),
761 U64_PRINTF_ARG(n_bytes_written_in_interval));
764 return 0;
767 /** Return true iff we have sent/received all the bytes we are willing
768 * to send/receive this interval. */
769 static int
770 hibernate_hard_limit_reached(void)
772 uint64_t hard_limit = get_options()->AccountingMax;
773 if (!hard_limit)
774 return 0;
775 return get_accounting_bytes() >= hard_limit;
778 /** Return true iff we have sent/received almost all the bytes we are willing
779 * to send/receive this interval. */
780 static int
781 hibernate_soft_limit_reached(void)
783 const uint64_t acct_max = get_options()->AccountingMax;
784 #define SOFT_LIM_PCT (.95)
785 #define SOFT_LIM_BYTES (500*1024*1024)
786 #define SOFT_LIM_MINUTES (3*60)
787 /* The 'soft limit' is a fair bit more complicated now than once it was.
788 * We want to stop accepting connections when ALL of the following are true:
789 * - We expect to use up the remaining bytes in under 3 hours
790 * - We have used up 95% of our bytes.
791 * - We have less than 500MB of bytes left.
793 uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(acct_max) * SOFT_LIM_PCT);
794 if (acct_max > SOFT_LIM_BYTES && acct_max - SOFT_LIM_BYTES > soft_limit) {
795 soft_limit = acct_max - SOFT_LIM_BYTES;
797 if (expected_bandwidth_usage) {
798 const uint64_t expected_usage =
799 expected_bandwidth_usage * SOFT_LIM_MINUTES;
800 if (acct_max > expected_usage && acct_max - expected_usage > soft_limit)
801 soft_limit = acct_max - expected_usage;
804 if (!soft_limit)
805 return 0;
806 return get_accounting_bytes() >= soft_limit;
809 /** Called when we get a SIGINT, or when bandwidth soft limit is
810 * reached. Puts us into "loose hibernation": we don't accept new
811 * connections, but we continue handling old ones. */
812 static void
813 hibernate_begin(hibernate_state_t new_state, time_t now)
815 const or_options_t *options = get_options();
817 if (new_state == HIBERNATE_STATE_EXITING &&
818 hibernate_state != HIBERNATE_STATE_LIVE) {
819 log_notice(LD_GENERAL,"SIGINT received %s; exiting now.",
820 hibernate_state == HIBERNATE_STATE_EXITING ?
821 "a second time" : "while hibernating");
822 tor_shutdown_event_loop_and_exit(0);
823 return;
826 if (new_state == HIBERNATE_STATE_LOWBANDWIDTH &&
827 hibernate_state == HIBERNATE_STATE_LIVE) {
828 soft_limit_hit_at = now;
829 n_seconds_to_hit_soft_limit = n_seconds_active_in_interval;
830 n_bytes_at_soft_limit = get_accounting_bytes();
833 /* close listeners. leave control listener(s). */
834 connection_mark_all_noncontrol_listeners();
836 /* XXX kill intro point circs */
837 /* XXX upload rendezvous service descriptors with no intro points */
839 if (new_state == HIBERNATE_STATE_EXITING) {
840 log_notice(LD_GENERAL,"Interrupt: we have stopped accepting new "
841 "connections, and will shut down in %d seconds. Interrupt "
842 "again to exit now.", options->ShutdownWaitLength);
843 shutdown_time = time(NULL) + options->ShutdownWaitLength;
844 } else { /* soft limit reached */
845 hibernate_end_time = interval_end_time;
848 hibernate_state = new_state;
849 accounting_record_bandwidth_usage(now, get_or_state());
851 or_state_mark_dirty(get_or_state(),
852 get_options()->AvoidDiskWrites ? now+600 : 0);
855 /** Called when we've been hibernating and our timeout is reached. */
856 static void
857 hibernate_end(hibernate_state_t new_state)
859 tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
860 hibernate_state == HIBERNATE_STATE_DORMANT ||
861 hibernate_state == HIBERNATE_STATE_INITIAL);
863 /* listeners will be relaunched in run_scheduled_events() in main.c */
864 if (hibernate_state != HIBERNATE_STATE_INITIAL)
865 log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
867 hibernate_state = new_state;
868 hibernate_end_time = 0; /* no longer hibernating */
869 reset_uptime(); /* reset published uptime */
872 /** A wrapper around hibernate_begin, for when we get SIGINT. */
873 void
874 hibernate_begin_shutdown(void)
876 hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
879 /** Return true iff we are currently hibernating. */
880 MOCK_IMPL(int,
881 we_are_hibernating,(void))
883 return hibernate_state != HIBERNATE_STATE_LIVE;
886 /** If we aren't currently dormant, close all connections and become
887 * dormant. */
888 static void
889 hibernate_go_dormant(time_t now)
891 connection_t *conn;
893 if (hibernate_state == HIBERNATE_STATE_DORMANT)
894 return;
895 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
896 hibernate_state = HIBERNATE_STATE_DORMANT;
897 else
898 hibernate_begin(HIBERNATE_STATE_DORMANT, now);
900 log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
902 /* Close all OR/AP/exit conns. Leave dir conns because we still want
903 * to be able to upload server descriptors so clients know we're still
904 * running, and download directories so we can detect if we're obsolete.
905 * Leave control conns because we still want to be controllable.
907 while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
908 (conn = connection_get_by_type(CONN_TYPE_AP)) ||
909 (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
910 if (CONN_IS_EDGE(conn)) {
911 connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
913 log_info(LD_NET,"Closing conn type %d", conn->type);
914 if (conn->type == CONN_TYPE_AP) {
915 /* send socks failure if needed */
916 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
917 END_STREAM_REASON_HIBERNATING);
918 } else if (conn->type == CONN_TYPE_OR) {
919 if (TO_OR_CONN(conn)->chan) {
920 connection_or_close_normally(TO_OR_CONN(conn), 0);
921 } else {
922 connection_mark_for_close(conn);
924 } else {
925 connection_mark_for_close(conn);
929 if (now < interval_wakeup_time)
930 hibernate_end_time = interval_wakeup_time;
931 else
932 hibernate_end_time = interval_end_time;
934 accounting_record_bandwidth_usage(now, get_or_state());
936 or_state_mark_dirty(get_or_state(),
937 get_options()->AvoidDiskWrites ? now+600 : 0);
940 /** Called when hibernate_end_time has arrived. */
941 static void
942 hibernate_end_time_elapsed(time_t now)
944 char buf[ISO_TIME_LEN+1];
946 /* The interval has ended, or it is wakeup time. Find out which. */
947 accounting_run_housekeeping(now);
948 if (interval_wakeup_time <= now) {
949 /* The interval hasn't changed, but interval_wakeup_time has passed.
950 * It's time to wake up and start being a server. */
951 hibernate_end(HIBERNATE_STATE_LIVE);
952 return;
953 } else {
954 /* The interval has changed, and it isn't time to wake up yet. */
955 hibernate_end_time = interval_wakeup_time;
956 format_iso_time(buf,interval_wakeup_time);
957 if (hibernate_state != HIBERNATE_STATE_DORMANT) {
958 /* We weren't sleeping before; we should sleep now. */
959 log_notice(LD_ACCT,
960 "Accounting period ended. Commencing hibernation until "
961 "%s UTC", buf);
962 hibernate_go_dormant(now);
963 } else {
964 log_notice(LD_ACCT,
965 "Accounting period ended. This period, we will hibernate"
966 " until %s UTC",buf);
971 /** Consider our environment and decide if it's time
972 * to start/stop hibernating.
974 void
975 consider_hibernation(time_t now)
977 int accounting_enabled = get_options()->AccountingMax != 0;
978 char buf[ISO_TIME_LEN+1];
979 hibernate_state_t prev_state = hibernate_state;
981 /* If we're in 'exiting' mode, then we just shut down after the interval
982 * elapses. */
983 if (hibernate_state == HIBERNATE_STATE_EXITING) {
984 tor_assert(shutdown_time);
985 if (shutdown_time <= now) {
986 log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
987 tor_shutdown_event_loop_and_exit(0);
989 return; /* if exiting soon, don't worry about bandwidth limits */
992 if (hibernate_state == HIBERNATE_STATE_DORMANT) {
993 /* We've been hibernating because of bandwidth accounting. */
994 tor_assert(hibernate_end_time);
995 if (hibernate_end_time > now && accounting_enabled) {
996 /* If we're hibernating, don't wake up until it's time, regardless of
997 * whether we're in a new interval. */
998 return ;
999 } else {
1000 hibernate_end_time_elapsed(now);
1004 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
1005 * go dormant. */
1006 if (hibernate_state == HIBERNATE_STATE_LIVE ||
1007 hibernate_state == HIBERNATE_STATE_INITIAL) {
1008 if (hibernate_soft_limit_reached()) {
1009 log_notice(LD_ACCT,
1010 "Bandwidth soft limit reached; commencing hibernation. "
1011 "No new connections will be accepted");
1012 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
1013 } else if (accounting_enabled && now < interval_wakeup_time) {
1014 format_local_iso_time(buf,interval_wakeup_time);
1015 log_notice(LD_ACCT,
1016 "Commencing hibernation. We will wake up at %s local time.",
1017 buf);
1018 hibernate_go_dormant(now);
1019 } else if (hibernate_state == HIBERNATE_STATE_INITIAL) {
1020 hibernate_end(HIBERNATE_STATE_LIVE);
1024 if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
1025 if (!accounting_enabled) {
1026 hibernate_end_time_elapsed(now);
1027 } else if (hibernate_hard_limit_reached()) {
1028 hibernate_go_dormant(now);
1029 } else if (hibernate_end_time <= now) {
1030 /* The hibernation period ended while we were still in lowbandwidth.*/
1031 hibernate_end_time_elapsed(now);
1035 /* Dispatch a controller event if the hibernation state changed. */
1036 if (hibernate_state != prev_state)
1037 on_hibernate_state_change(prev_state);
1040 /** Helper function: called when we get a GETINFO request for an
1041 * accounting-related key on the control connection <b>conn</b>. If we can
1042 * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
1043 * allocated string holding the result. Otherwise, set *<b>answer</b> to
1044 * NULL. */
1046 getinfo_helper_accounting(control_connection_t *conn,
1047 const char *question, char **answer,
1048 const char **errmsg)
1050 (void) conn;
1051 (void) errmsg;
1052 if (!strcmp(question, "accounting/enabled")) {
1053 *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
1054 } else if (!strcmp(question, "accounting/hibernating")) {
1055 *answer = tor_strdup(hibernate_state_to_string(hibernate_state));
1056 tor_strlower(*answer);
1057 } else if (!strcmp(question, "accounting/bytes")) {
1058 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1059 U64_PRINTF_ARG(n_bytes_read_in_interval),
1060 U64_PRINTF_ARG(n_bytes_written_in_interval));
1061 } else if (!strcmp(question, "accounting/bytes-left")) {
1062 uint64_t limit = get_options()->AccountingMax;
1063 if (get_options()->AccountingRule == ACCT_SUM) {
1064 uint64_t total_left = 0;
1065 uint64_t total_bytes = get_accounting_bytes();
1066 if (total_bytes < limit)
1067 total_left = limit - total_bytes;
1068 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1069 U64_PRINTF_ARG(total_left), U64_PRINTF_ARG(total_left));
1070 } else if (get_options()->AccountingRule == ACCT_IN) {
1071 uint64_t read_left = 0;
1072 if (n_bytes_read_in_interval < limit)
1073 read_left = limit - n_bytes_read_in_interval;
1074 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1075 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(limit));
1076 } else if (get_options()->AccountingRule == ACCT_OUT) {
1077 uint64_t write_left = 0;
1078 if (n_bytes_written_in_interval < limit)
1079 write_left = limit - n_bytes_written_in_interval;
1080 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1081 U64_PRINTF_ARG(limit), U64_PRINTF_ARG(write_left));
1082 } else {
1083 uint64_t read_left = 0, write_left = 0;
1084 if (n_bytes_read_in_interval < limit)
1085 read_left = limit - n_bytes_read_in_interval;
1086 if (n_bytes_written_in_interval < limit)
1087 write_left = limit - n_bytes_written_in_interval;
1088 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1089 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
1091 } else if (!strcmp(question, "accounting/interval-start")) {
1092 *answer = tor_malloc(ISO_TIME_LEN+1);
1093 format_iso_time(*answer, interval_start_time);
1094 } else if (!strcmp(question, "accounting/interval-wake")) {
1095 *answer = tor_malloc(ISO_TIME_LEN+1);
1096 format_iso_time(*answer, interval_wakeup_time);
1097 } else if (!strcmp(question, "accounting/interval-end")) {
1098 *answer = tor_malloc(ISO_TIME_LEN+1);
1099 format_iso_time(*answer, interval_end_time);
1100 } else {
1101 *answer = NULL;
1103 return 0;
1107 * Helper function: called when the hibernation state changes, and sends a
1108 * SERVER_STATUS event to notify interested controllers of the accounting
1109 * state change.
1111 static void
1112 on_hibernate_state_change(hibernate_state_t prev_state)
1114 control_event_server_status(LOG_NOTICE,
1115 "HIBERNATION_STATUS STATUS=%s",
1116 hibernate_state_to_string(hibernate_state));
1118 /* We are changing hibernation state, this can affect the main loop event
1119 * list. Rescan it to update the events state. We do this whatever the new
1120 * hibernation state because they can each possibly affect an event. The
1121 * initial state means we are booting up so we shouldn't scan here because
1122 * at this point the events in the list haven't been initialized. */
1123 if (prev_state != HIBERNATE_STATE_INITIAL) {
1124 rescan_periodic_events(get_options());
1128 #ifdef TOR_UNIT_TESTS
1130 * Manually change the hibernation state. Private; used only by the unit
1131 * tests.
1133 void
1134 hibernate_set_state_for_testing_(hibernate_state_t newstate)
1136 hibernate_state = newstate;
1138 #endif /* defined(TOR_UNIT_TESTS) */