copy changelog to releasenotes
[tor.git] / src / or / hibernate.c
blobc2b3bbb839a5d8f6c73f5c80576e567e64556cec
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2016, 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 "control.h"
38 #include "hibernate.h"
39 #include "main.h"
40 #include "router.h"
41 #include "statefile.h"
43 /** Are we currently awake, asleep, running out of bandwidth, or shutting
44 * down? */
45 static hibernate_state_t hibernate_state = HIBERNATE_STATE_INITIAL;
46 /** If are hibernating, when do we plan to wake up? Set to 0 if we
47 * aren't hibernating. */
48 static time_t hibernate_end_time = 0;
49 /** If we are shutting down, when do we plan finally exit? Set to 0 if
50 * we aren't shutting down. */
51 static time_t shutdown_time = 0;
53 /** Possible accounting periods. */
54 typedef enum {
55 UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
56 } time_unit_t;
59 * @file hibernate.c
61 * <h4>Accounting</h4>
62 * Accounting is designed to ensure that no more than N bytes are sent in
63 * either direction over a given interval (currently, one month, one week, or
64 * one day) We could
65 * try to do this by choking our bandwidth to a trickle, but that
66 * would make our streams useless. Instead, we estimate what our
67 * bandwidth usage will be, and guess how long we'll be able to
68 * provide that much bandwidth before hitting our limit. We then
69 * choose a random time within the accounting interval to come up (so
70 * that we don't get 50 Tors running on the 1st of the month and none
71 * on the 30th).
73 * Each interval runs as follows:
75 * <ol>
76 * <li>We guess our bandwidth usage, based on how much we used
77 * last time. We choose a "wakeup time" within the interval to come up.
78 * <li>Until the chosen wakeup time, we hibernate.
79 * <li> We come up at the wakeup time, and provide bandwidth until we are
80 * "very close" to running out.
81 * <li> Then we go into low-bandwidth mode, and stop accepting new
82 * connections, but provide bandwidth until we run out.
83 * <li> Then we hibernate until the end of the interval.
85 * If the interval ends before we run out of bandwidth, we go back to
86 * step one.
88 * Accounting is controlled by the AccountingMax, AccountingRule, and
89 * AccountingStart options.
92 /** How many bytes have we read in this accounting interval? */
93 static uint64_t n_bytes_read_in_interval = 0;
94 /** How many bytes have we written in this accounting interval? */
95 static uint64_t n_bytes_written_in_interval = 0;
96 /** How many seconds have we been running this interval? */
97 static uint32_t n_seconds_active_in_interval = 0;
98 /** How many seconds were we active in this interval before we hit our soft
99 * limit? */
100 static int n_seconds_to_hit_soft_limit = 0;
101 /** When in this interval was the soft limit hit. */
102 static time_t soft_limit_hit_at = 0;
103 /** How many bytes had we read/written when we hit the soft limit? */
104 static uint64_t n_bytes_at_soft_limit = 0;
105 /** When did this accounting interval start? */
106 static time_t interval_start_time = 0;
107 /** When will this accounting interval end? */
108 static time_t interval_end_time = 0;
109 /** How far into the accounting interval should we hibernate? */
110 static time_t interval_wakeup_time = 0;
111 /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
112 * info from the last period.) */
113 static uint64_t expected_bandwidth_usage = 0;
114 /** What unit are we using for our accounting? */
115 static time_unit_t cfg_unit = UNIT_MONTH;
117 /** How many days,hours,minutes into each unit does our accounting interval
118 * start? */
119 /** @{ */
120 static int cfg_start_day = 0,
121 cfg_start_hour = 0,
122 cfg_start_min = 0;
123 /** @} */
125 static const char *hibernate_state_to_string(hibernate_state_t state);
126 static void reset_accounting(time_t now);
127 static int read_bandwidth_usage(void);
128 static time_t start_of_accounting_period_after(time_t now);
129 static time_t start_of_accounting_period_containing(time_t now);
130 static void accounting_set_wakeup_time(void);
131 static void on_hibernate_state_change(hibernate_state_t prev_state);
134 * Return the human-readable name for the hibernation state <b>state</b>
136 static const char *
137 hibernate_state_to_string(hibernate_state_t state)
139 static char buf[64];
140 switch (state) {
141 case HIBERNATE_STATE_EXITING: return "EXITING";
142 case HIBERNATE_STATE_LOWBANDWIDTH: return "SOFT";
143 case HIBERNATE_STATE_DORMANT: return "HARD";
144 case HIBERNATE_STATE_INITIAL:
145 case HIBERNATE_STATE_LIVE:
146 return "AWAKE";
147 default:
148 log_warn(LD_BUG, "unknown hibernate state %d", state);
149 tor_snprintf(buf, sizeof(buf), "unknown [%d]", state);
150 return buf;
154 /* ************
155 * Functions for bandwidth accounting.
156 * ************/
158 /** Configure accounting start/end time settings based on
159 * options->AccountingStart. Return 0 on success, -1 on failure. If
160 * <b>validate_only</b> is true, do not change the current settings. */
162 accounting_parse_options(const or_options_t *options, int validate_only)
164 time_unit_t unit;
165 int ok, idx;
166 long d,h,m;
167 smartlist_t *items;
168 const char *v = options->AccountingStart;
169 const char *s;
170 char *cp;
172 if (!v) {
173 if (!validate_only) {
174 cfg_unit = UNIT_MONTH;
175 cfg_start_day = 1;
176 cfg_start_hour = 0;
177 cfg_start_min = 0;
179 return 0;
182 items = smartlist_new();
183 smartlist_split_string(items, v, NULL,
184 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
185 if (smartlist_len(items)<2) {
186 log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
187 goto err;
189 s = smartlist_get(items,0);
190 if (0==strcasecmp(s, "month")) {
191 unit = UNIT_MONTH;
192 } else if (0==strcasecmp(s, "week")) {
193 unit = UNIT_WEEK;
194 } else if (0==strcasecmp(s, "day")) {
195 unit = UNIT_DAY;
196 } else {
197 log_warn(LD_CONFIG,
198 "Unrecognized accounting unit '%s': only 'month', 'week',"
199 " and 'day' are supported.", s);
200 goto err;
203 switch (unit) {
204 case UNIT_WEEK:
205 d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
206 if (!ok) {
207 log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
208 "1 (Monday) and 7 (Sunday)");
209 goto err;
211 break;
212 case UNIT_MONTH:
213 d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
214 if (!ok) {
215 log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
216 "1 and 28");
217 goto err;
219 break;
220 case UNIT_DAY:
221 d = 0;
222 break;
223 /* Coverity dislikes unreachable default cases; some compilers warn on
224 * switch statements missing a case. Tell Coverity not to worry. */
225 /* coverity[dead_error_begin] */
226 default:
227 tor_assert(0);
230 idx = unit==UNIT_DAY?1:2;
231 if (smartlist_len(items) != (idx+1)) {
232 log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
233 s, idx, (idx>1)?"s":"");
234 goto err;
236 s = smartlist_get(items, idx);
237 h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
238 if (!ok) {
239 log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
240 goto err;
242 if (!cp || *cp!=':') {
243 log_warn(LD_CONFIG,
244 "Accounting start time not parseable: not in HH:MM format");
245 goto err;
247 m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
248 if (!ok) {
249 log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
250 goto err;
252 if (!cp || *cp!='\0') {
253 log_warn(LD_CONFIG,
254 "Accounting start time not parseable: not in HH:MM format");
255 goto err;
258 if (!validate_only) {
259 cfg_unit = unit;
260 cfg_start_day = (int)d;
261 cfg_start_hour = (int)h;
262 cfg_start_min = (int)m;
264 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
265 smartlist_free(items);
266 return 0;
267 err:
268 SMARTLIST_FOREACH(items, char *, item, tor_free(item));
269 smartlist_free(items);
270 return -1;
273 /** If we want to manage the accounting system and potentially
274 * hibernate, return 1, else return 0.
276 MOCK_IMPL(int,
277 accounting_is_enabled,(const or_options_t *options))
279 if (options->AccountingMax)
280 return 1;
281 return 0;
284 /** If accounting is enabled, return how long (in seconds) this
285 * interval lasts. */
287 accounting_get_interval_length(void)
289 return (int)(interval_end_time - interval_start_time);
292 /** Return the time at which the current accounting interval will end. */
293 MOCK_IMPL(time_t,
294 accounting_get_end_time,(void))
296 return interval_end_time;
299 /** Called from main.c to tell us that <b>seconds</b> seconds have
300 * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
301 * bytes have been written. */
302 void
303 accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
305 n_bytes_read_in_interval += n_read;
306 n_bytes_written_in_interval += n_written;
307 /* If we haven't been called in 10 seconds, we're probably jumping
308 * around in time. */
309 n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
312 /** If get_end, return the end of the accounting period that contains
313 * the time <b>now</b>. Else, return the start of the accounting
314 * period that contains the time <b>now</b> */
315 static time_t
316 edge_of_accounting_period_containing(time_t now, int get_end)
318 int before;
319 struct tm tm;
320 tor_localtime_r(&now, &tm);
322 /* Set 'before' to true iff the current time is before the hh:mm
323 * changeover time for today. */
324 before = tm.tm_hour < cfg_start_hour ||
325 (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
327 /* Dispatch by unit. First, find the start day of the given period;
328 * then, if get_end is true, increment to the end day. */
329 switch (cfg_unit)
331 case UNIT_MONTH: {
332 /* If this is before the Nth, we want the Nth of last month. */
333 if (tm.tm_mday < cfg_start_day ||
334 (tm.tm_mday < cfg_start_day && before)) {
335 --tm.tm_mon;
337 /* Otherwise, the month is correct. */
338 tm.tm_mday = cfg_start_day;
339 if (get_end)
340 ++tm.tm_mon;
341 break;
343 case UNIT_WEEK: {
344 /* What is the 'target' day of the week in struct tm format? (We
345 say Sunday==7; struct tm says Sunday==0.) */
346 int wday = cfg_start_day % 7;
347 /* How many days do we subtract from today to get to the right day? */
348 int delta = (7+tm.tm_wday-wday)%7;
349 /* If we are on the right day, but the changeover hasn't happened yet,
350 * then subtract a whole week. */
351 if (delta == 0 && before)
352 delta = 7;
353 tm.tm_mday -= delta;
354 if (get_end)
355 tm.tm_mday += 7;
356 break;
358 case UNIT_DAY:
359 if (before)
360 --tm.tm_mday;
361 if (get_end)
362 ++tm.tm_mday;
363 break;
364 default:
365 tor_assert(0);
368 tm.tm_hour = cfg_start_hour;
369 tm.tm_min = cfg_start_min;
370 tm.tm_sec = 0;
371 tm.tm_isdst = -1; /* Autodetect DST */
372 return mktime(&tm);
375 /** Return the start of the accounting period containing the time
376 * <b>now</b>. */
377 static time_t
378 start_of_accounting_period_containing(time_t now)
380 return edge_of_accounting_period_containing(now, 0);
383 /** Return the start of the accounting period that comes after the one
384 * containing the time <b>now</b>. */
385 static time_t
386 start_of_accounting_period_after(time_t now)
388 return edge_of_accounting_period_containing(now, 1);
391 /** Return the length of the accounting period containing the time
392 * <b>now</b>. */
393 static long
394 length_of_accounting_period_containing(time_t now)
396 return edge_of_accounting_period_containing(now, 1) -
397 edge_of_accounting_period_containing(now, 0);
400 /** Initialize the accounting subsystem. */
401 void
402 configure_accounting(time_t now)
404 time_t s_now;
405 /* Try to remember our recorded usage. */
406 if (!interval_start_time)
407 read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
408 * reset below.*/
410 s_now = start_of_accounting_period_containing(now);
412 if (!interval_start_time) {
413 /* We didn't have recorded usage; Start a new interval. */
414 log_info(LD_ACCT, "Starting new accounting interval.");
415 reset_accounting(now);
416 } else if (s_now == interval_start_time) {
417 log_info(LD_ACCT, "Continuing accounting interval.");
418 /* We are in the interval we thought we were in. Do nothing.*/
419 interval_end_time = start_of_accounting_period_after(interval_start_time);
420 } else {
421 long duration =
422 length_of_accounting_period_containing(interval_start_time);
423 double delta = ((double)(s_now - interval_start_time)) / duration;
424 if (-0.50 <= delta && delta <= 0.50) {
425 /* The start of the period is now a little later or earlier than we
426 * remembered. That's fine; we might lose some bytes we could otherwise
427 * have written, but better to err on the side of obeying accounting
428 * settings. */
429 log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
430 "that's fine.", delta*100);
431 interval_end_time = start_of_accounting_period_after(now);
432 } else if (delta >= 0.99) {
433 /* This is the regular time-moved-forward case; don't be too noisy
434 * about it or people will complain */
435 log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
436 reset_accounting(now);
437 } else {
438 log_warn(LD_ACCT,
439 "Mismatched accounting interval: moved by %.02f%%. "
440 "Starting a fresh one.", delta*100);
441 reset_accounting(now);
444 accounting_set_wakeup_time();
447 /** Return the relevant number of bytes sent/received this interval
448 * based on the set AccountingRule */
449 uint64_t
450 get_accounting_bytes(void)
452 if (get_options()->AccountingRule == ACCT_SUM)
453 return n_bytes_read_in_interval+n_bytes_written_in_interval;
454 else if (get_options()->AccountingRule == ACCT_IN)
455 return n_bytes_read_in_interval;
456 else if (get_options()->AccountingRule == ACCT_OUT)
457 return n_bytes_written_in_interval;
458 else
459 return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
462 /** Set expected_bandwidth_usage based on how much we sent/received
463 * per minute last interval (if we were up for at least 30 minutes),
464 * or based on our declared bandwidth otherwise. */
465 static void
466 update_expected_bandwidth(void)
468 uint64_t expected;
469 const or_options_t *options= get_options();
470 uint64_t max_configured = (options->RelayBandwidthRate > 0 ?
471 options->RelayBandwidthRate :
472 options->BandwidthRate) * 60;
473 /* max_configured is the larger of bytes read and bytes written
474 * If we are accounting based on sum, worst case is both are
475 * at max, doubling the expected sum of bandwidth */
476 if (get_options()->AccountingRule == ACCT_SUM)
477 max_configured *= 2;
479 #define MIN_TIME_FOR_MEASUREMENT (1800)
481 if (soft_limit_hit_at > interval_start_time && n_bytes_at_soft_limit &&
482 (soft_limit_hit_at - interval_start_time) > MIN_TIME_FOR_MEASUREMENT) {
483 /* If we hit our soft limit last time, only count the bytes up to that
484 * time. This is a better predictor of our actual bandwidth than
485 * considering the entirety of the last interval, since we likely started
486 * using bytes very slowly once we hit our soft limit. */
487 expected = n_bytes_at_soft_limit /
488 (soft_limit_hit_at - interval_start_time);
489 expected /= 60;
490 } else if (n_seconds_active_in_interval >= MIN_TIME_FOR_MEASUREMENT) {
491 /* Otherwise, we either measured enough time in the last interval but
492 * never hit our soft limit, or we're using a state file from a Tor that
493 * doesn't know to store soft-limit info. Just take rate at which
494 * we were reading/writing in the last interval as our expected rate.
496 uint64_t used = get_accounting_bytes();
497 expected = used / (n_seconds_active_in_interval / 60);
498 } else {
499 /* If we haven't gotten enough data last interval, set 'expected'
500 * to 0. This will set our wakeup to the start of the interval.
501 * Next interval, we'll choose our starting time based on how much
502 * we sent this interval.
504 expected = 0;
506 if (expected > max_configured)
507 expected = max_configured;
508 expected_bandwidth_usage = expected;
511 /** Called at the start of a new accounting interval: reset our
512 * expected bandwidth usage based on what happened last time, set up
513 * the start and end of the interval, and clear byte/time totals.
515 static void
516 reset_accounting(time_t now)
518 log_info(LD_ACCT, "Starting new accounting interval.");
519 update_expected_bandwidth();
520 interval_start_time = start_of_accounting_period_containing(now);
521 interval_end_time = start_of_accounting_period_after(interval_start_time);
522 n_bytes_read_in_interval = 0;
523 n_bytes_written_in_interval = 0;
524 n_seconds_active_in_interval = 0;
525 n_bytes_at_soft_limit = 0;
526 soft_limit_hit_at = 0;
527 n_seconds_to_hit_soft_limit = 0;
530 /** Return true iff we should save our bandwidth usage to disk. */
531 static inline int
532 time_to_record_bandwidth_usage(time_t now)
534 /* Note every 600 sec */
535 #define NOTE_INTERVAL (600)
536 /* Or every 20 megabytes */
537 #define NOTE_BYTES 20*(1024*1024)
538 static uint64_t last_read_bytes_noted = 0;
539 static uint64_t last_written_bytes_noted = 0;
540 static time_t last_time_noted = 0;
542 if (last_time_noted + NOTE_INTERVAL <= now ||
543 last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
544 last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
545 (interval_end_time && interval_end_time <= now)) {
546 last_time_noted = now;
547 last_read_bytes_noted = n_bytes_read_in_interval;
548 last_written_bytes_noted = n_bytes_written_in_interval;
549 return 1;
551 return 0;
554 /** Invoked once per second. Checks whether it is time to hibernate,
555 * record bandwidth used, etc. */
556 void
557 accounting_run_housekeeping(time_t now)
559 if (now >= interval_end_time) {
560 configure_accounting(now);
562 if (time_to_record_bandwidth_usage(now)) {
563 if (accounting_record_bandwidth_usage(now, get_or_state())) {
564 log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
569 /** Based on our interval and our estimated bandwidth, choose a
570 * deterministic (but random-ish) time to wake up. */
571 static void
572 accounting_set_wakeup_time(void)
574 char digest[DIGEST_LEN];
575 crypto_digest_t *d_env;
576 uint64_t time_to_exhaust_bw;
577 int time_to_consider;
579 if (! server_identity_key_is_set()) {
580 if (init_keys() < 0) {
581 log_err(LD_BUG, "Error initializing keys");
582 tor_assert(0);
586 if (server_identity_key_is_set()) {
587 char buf[ISO_TIME_LEN+1];
588 format_iso_time(buf, interval_start_time);
590 crypto_pk_get_digest(get_server_identity_key(), digest);
592 d_env = crypto_digest_new();
593 crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
594 crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
595 crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
596 crypto_digest_free(d_env);
597 } else {
598 crypto_rand(digest, DIGEST_LEN);
601 if (!expected_bandwidth_usage) {
602 char buf1[ISO_TIME_LEN+1];
603 char buf2[ISO_TIME_LEN+1];
604 format_local_iso_time(buf1, interval_start_time);
605 format_local_iso_time(buf2, interval_end_time);
606 interval_wakeup_time = interval_start_time;
608 log_notice(LD_ACCT,
609 "Configured hibernation. This interval begins at %s "
610 "and ends at %s. We have no prior estimate for bandwidth, so "
611 "we will start out awake and hibernate when we exhaust our quota.",
612 buf1, buf2);
613 return;
616 time_to_exhaust_bw =
617 (get_options()->AccountingMax/expected_bandwidth_usage)*60;
618 if (time_to_exhaust_bw > INT_MAX) {
619 time_to_exhaust_bw = INT_MAX;
620 time_to_consider = 0;
621 } else {
622 time_to_consider = accounting_get_interval_length() -
623 (int)time_to_exhaust_bw;
626 if (time_to_consider<=0) {
627 interval_wakeup_time = interval_start_time;
628 } else {
629 /* XXX can we simplify this just by picking a random (non-deterministic)
630 * time to be up? If we go down and come up, then we pick a new one. Is
631 * that good enough? -RD */
633 /* This is not a perfectly unbiased conversion, but it is good enough:
634 * in the worst case, the first half of the day is 0.06 percent likelier
635 * to be chosen than the last half. */
636 interval_wakeup_time = interval_start_time +
637 (get_uint32(digest) % time_to_consider);
641 char buf1[ISO_TIME_LEN+1];
642 char buf2[ISO_TIME_LEN+1];
643 char buf3[ISO_TIME_LEN+1];
644 char buf4[ISO_TIME_LEN+1];
645 time_t down_time;
646 if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
647 down_time = TIME_MAX;
648 else
649 down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
650 if (down_time>interval_end_time)
651 down_time = interval_end_time;
652 format_local_iso_time(buf1, interval_start_time);
653 format_local_iso_time(buf2, interval_wakeup_time);
654 format_local_iso_time(buf3, down_time);
655 format_local_iso_time(buf4, interval_end_time);
657 log_notice(LD_ACCT,
658 "Configured hibernation. This interval began at %s; "
659 "the scheduled wake-up time %s %s; "
660 "we expect%s to exhaust our quota for this interval around %s; "
661 "the next interval begins at %s (all times local)",
662 buf1,
663 time(NULL)<interval_wakeup_time?"is":"was", buf2,
664 time(NULL)<down_time?"":"ed", buf3,
665 buf4);
669 /* This rounds 0 up to 1000, but that's actually a feature. */
670 #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
671 /** Save all our bandwidth tracking information to disk. Return 0 on
672 * success, -1 on failure. */
674 accounting_record_bandwidth_usage(time_t now, or_state_t *state)
676 /* Just update the state */
677 state->AccountingIntervalStart = interval_start_time;
678 state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
679 state->AccountingBytesWrittenInInterval =
680 ROUND_UP(n_bytes_written_in_interval);
681 state->AccountingSecondsActive = n_seconds_active_in_interval;
682 state->AccountingExpectedUsage = expected_bandwidth_usage;
684 state->AccountingSecondsToReachSoftLimit = n_seconds_to_hit_soft_limit;
685 state->AccountingSoftLimitHitAt = soft_limit_hit_at;
686 state->AccountingBytesAtSoftLimit = n_bytes_at_soft_limit;
688 or_state_mark_dirty(state,
689 now+(get_options()->AvoidDiskWrites ? 7200 : 60));
691 return 0;
693 #undef ROUND_UP
695 /** Read stored accounting information from disk. Return 0 on success;
696 * return -1 and change nothing on failure. */
697 static int
698 read_bandwidth_usage(void)
700 or_state_t *state = get_or_state();
703 char *fname = get_datadir_fname("bw_accounting");
704 int res;
706 res = unlink(fname);
707 if (res != 0 && errno != ENOENT) {
708 log_warn(LD_FS,
709 "Failed to unlink %s: %s",
710 fname, strerror(errno));
713 tor_free(fname);
716 if (!state)
717 return -1;
719 log_info(LD_ACCT, "Reading bandwidth accounting data from state file");
720 n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
721 n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
722 n_seconds_active_in_interval = state->AccountingSecondsActive;
723 interval_start_time = state->AccountingIntervalStart;
724 expected_bandwidth_usage = state->AccountingExpectedUsage;
726 /* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
727 * fields. If you switch back and forth, you might get an
728 * AccountingSoftLimitHitAt value from long before the most recent
729 * interval_start_time. If that's so, then ignore the softlimit-related
730 * values. */
731 if (state->AccountingSoftLimitHitAt > interval_start_time) {
732 soft_limit_hit_at = state->AccountingSoftLimitHitAt;
733 n_bytes_at_soft_limit = state->AccountingBytesAtSoftLimit;
734 n_seconds_to_hit_soft_limit = state->AccountingSecondsToReachSoftLimit;
735 } else {
736 soft_limit_hit_at = 0;
737 n_bytes_at_soft_limit = 0;
738 n_seconds_to_hit_soft_limit = 0;
742 char tbuf1[ISO_TIME_LEN+1];
743 char tbuf2[ISO_TIME_LEN+1];
744 format_iso_time(tbuf1, state->LastWritten);
745 format_iso_time(tbuf2, state->AccountingIntervalStart);
747 log_info(LD_ACCT,
748 "Successfully read bandwidth accounting info from state written at %s "
749 "for interval starting at %s. We have been active for %lu seconds in "
750 "this interval. At the start of the interval, we expected to use "
751 "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
752 U64_FORMAT" bytes written so far)",
753 tbuf1, tbuf2,
754 (unsigned long)n_seconds_active_in_interval,
755 (unsigned long)(expected_bandwidth_usage*1024/60),
756 U64_PRINTF_ARG(n_bytes_read_in_interval),
757 U64_PRINTF_ARG(n_bytes_written_in_interval));
760 return 0;
763 /** Return true iff we have sent/received all the bytes we are willing
764 * to send/receive this interval. */
765 static int
766 hibernate_hard_limit_reached(void)
768 uint64_t hard_limit = get_options()->AccountingMax;
769 if (!hard_limit)
770 return 0;
771 return get_accounting_bytes() >= hard_limit;
774 /** Return true iff we have sent/received almost all the bytes we are willing
775 * to send/receive this interval. */
776 static int
777 hibernate_soft_limit_reached(void)
779 const uint64_t acct_max = get_options()->AccountingMax;
780 #define SOFT_LIM_PCT (.95)
781 #define SOFT_LIM_BYTES (500*1024*1024)
782 #define SOFT_LIM_MINUTES (3*60)
783 /* The 'soft limit' is a fair bit more complicated now than once it was.
784 * We want to stop accepting connections when ALL of the following are true:
785 * - We expect to use up the remaining bytes in under 3 hours
786 * - We have used up 95% of our bytes.
787 * - We have less than 500MB of bytes left.
789 uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(acct_max) * SOFT_LIM_PCT);
790 if (acct_max > SOFT_LIM_BYTES && acct_max - SOFT_LIM_BYTES > soft_limit) {
791 soft_limit = acct_max - SOFT_LIM_BYTES;
793 if (expected_bandwidth_usage) {
794 const uint64_t expected_usage =
795 expected_bandwidth_usage * SOFT_LIM_MINUTES;
796 if (acct_max > expected_usage && acct_max - expected_usage > soft_limit)
797 soft_limit = acct_max - expected_usage;
800 if (!soft_limit)
801 return 0;
802 return get_accounting_bytes() >= soft_limit;
805 /** Called when we get a SIGINT, or when bandwidth soft limit is
806 * reached. Puts us into "loose hibernation": we don't accept new
807 * connections, but we continue handling old ones. */
808 static void
809 hibernate_begin(hibernate_state_t new_state, time_t now)
811 const or_options_t *options = get_options();
813 if (new_state == HIBERNATE_STATE_EXITING &&
814 hibernate_state != HIBERNATE_STATE_LIVE) {
815 log_notice(LD_GENERAL,"SIGINT received %s; exiting now.",
816 hibernate_state == HIBERNATE_STATE_EXITING ?
817 "a second time" : "while hibernating");
818 tor_cleanup();
819 exit(0);
822 if (new_state == HIBERNATE_STATE_LOWBANDWIDTH &&
823 hibernate_state == HIBERNATE_STATE_LIVE) {
824 soft_limit_hit_at = now;
825 n_seconds_to_hit_soft_limit = n_seconds_active_in_interval;
826 n_bytes_at_soft_limit = get_accounting_bytes();
829 /* close listeners. leave control listener(s). */
830 connection_mark_all_noncontrol_listeners();
832 /* XXX kill intro point circs */
833 /* XXX upload rendezvous service descriptors with no intro points */
835 if (new_state == HIBERNATE_STATE_EXITING) {
836 log_notice(LD_GENERAL,"Interrupt: we have stopped accepting new "
837 "connections, and will shut down in %d seconds. Interrupt "
838 "again to exit now.", options->ShutdownWaitLength);
839 shutdown_time = time(NULL) + options->ShutdownWaitLength;
840 } else { /* soft limit reached */
841 hibernate_end_time = interval_end_time;
844 hibernate_state = new_state;
845 accounting_record_bandwidth_usage(now, get_or_state());
847 or_state_mark_dirty(get_or_state(),
848 get_options()->AvoidDiskWrites ? now+600 : 0);
851 /** Called when we've been hibernating and our timeout is reached. */
852 static void
853 hibernate_end(hibernate_state_t new_state)
855 tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
856 hibernate_state == HIBERNATE_STATE_DORMANT ||
857 hibernate_state == HIBERNATE_STATE_INITIAL);
859 /* listeners will be relaunched in run_scheduled_events() in main.c */
860 if (hibernate_state != HIBERNATE_STATE_INITIAL)
861 log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
863 hibernate_state = new_state;
864 hibernate_end_time = 0; /* no longer hibernating */
865 stats_n_seconds_working = 0; /* reset published uptime */
868 /** A wrapper around hibernate_begin, for when we get SIGINT. */
869 void
870 hibernate_begin_shutdown(void)
872 hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
875 /** Return true iff we are currently hibernating. */
876 MOCK_IMPL(int,
877 we_are_hibernating,(void))
879 return hibernate_state != HIBERNATE_STATE_LIVE;
882 /** If we aren't currently dormant, close all connections and become
883 * dormant. */
884 static void
885 hibernate_go_dormant(time_t now)
887 connection_t *conn;
889 if (hibernate_state == HIBERNATE_STATE_DORMANT)
890 return;
891 else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
892 hibernate_state = HIBERNATE_STATE_DORMANT;
893 else
894 hibernate_begin(HIBERNATE_STATE_DORMANT, now);
896 log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
898 /* Close all OR/AP/exit conns. Leave dir conns because we still want
899 * to be able to upload server descriptors so clients know we're still
900 * running, and download directories so we can detect if we're obsolete.
901 * Leave control conns because we still want to be controllable.
903 while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
904 (conn = connection_get_by_type(CONN_TYPE_AP)) ||
905 (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
906 if (CONN_IS_EDGE(conn))
907 connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
908 log_info(LD_NET,"Closing conn type %d", conn->type);
909 if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
910 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
911 END_STREAM_REASON_HIBERNATING);
912 else if (conn->type == CONN_TYPE_OR) {
913 if (TO_OR_CONN(conn)->chan) {
914 channel_mark_for_close(TLS_CHAN_TO_BASE(TO_OR_CONN(conn)->chan));
915 } else {
916 connection_mark_for_close(conn);
918 } else
919 connection_mark_for_close(conn);
922 if (now < interval_wakeup_time)
923 hibernate_end_time = interval_wakeup_time;
924 else
925 hibernate_end_time = interval_end_time;
927 accounting_record_bandwidth_usage(now, get_or_state());
929 or_state_mark_dirty(get_or_state(),
930 get_options()->AvoidDiskWrites ? now+600 : 0);
933 /** Called when hibernate_end_time has arrived. */
934 static void
935 hibernate_end_time_elapsed(time_t now)
937 char buf[ISO_TIME_LEN+1];
939 /* The interval has ended, or it is wakeup time. Find out which. */
940 accounting_run_housekeeping(now);
941 if (interval_wakeup_time <= now) {
942 /* The interval hasn't changed, but interval_wakeup_time has passed.
943 * It's time to wake up and start being a server. */
944 hibernate_end(HIBERNATE_STATE_LIVE);
945 return;
946 } else {
947 /* The interval has changed, and it isn't time to wake up yet. */
948 hibernate_end_time = interval_wakeup_time;
949 format_iso_time(buf,interval_wakeup_time);
950 if (hibernate_state != HIBERNATE_STATE_DORMANT) {
951 /* We weren't sleeping before; we should sleep now. */
952 log_notice(LD_ACCT,
953 "Accounting period ended. Commencing hibernation until "
954 "%s UTC", buf);
955 hibernate_go_dormant(now);
956 } else {
957 log_notice(LD_ACCT,
958 "Accounting period ended. This period, we will hibernate"
959 " until %s UTC",buf);
964 /** Consider our environment and decide if it's time
965 * to start/stop hibernating.
967 void
968 consider_hibernation(time_t now)
970 int accounting_enabled = get_options()->AccountingMax != 0;
971 char buf[ISO_TIME_LEN+1];
972 hibernate_state_t prev_state = hibernate_state;
974 /* If we're in 'exiting' mode, then we just shut down after the interval
975 * elapses. */
976 if (hibernate_state == HIBERNATE_STATE_EXITING) {
977 tor_assert(shutdown_time);
978 if (shutdown_time <= now) {
979 log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
980 tor_cleanup();
981 exit(0);
983 return; /* if exiting soon, don't worry about bandwidth limits */
986 if (hibernate_state == HIBERNATE_STATE_DORMANT) {
987 /* We've been hibernating because of bandwidth accounting. */
988 tor_assert(hibernate_end_time);
989 if (hibernate_end_time > now && accounting_enabled) {
990 /* If we're hibernating, don't wake up until it's time, regardless of
991 * whether we're in a new interval. */
992 return ;
993 } else {
994 hibernate_end_time_elapsed(now);
998 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
999 * go dormant. */
1000 if (hibernate_state == HIBERNATE_STATE_LIVE ||
1001 hibernate_state == HIBERNATE_STATE_INITIAL) {
1002 if (hibernate_soft_limit_reached()) {
1003 log_notice(LD_ACCT,
1004 "Bandwidth soft limit reached; commencing hibernation. "
1005 "No new connections will be accepted");
1006 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
1007 } else if (accounting_enabled && now < interval_wakeup_time) {
1008 format_local_iso_time(buf,interval_wakeup_time);
1009 log_notice(LD_ACCT,
1010 "Commencing hibernation. We will wake up at %s local time.",
1011 buf);
1012 hibernate_go_dormant(now);
1013 } else if (hibernate_state == HIBERNATE_STATE_INITIAL) {
1014 hibernate_end(HIBERNATE_STATE_LIVE);
1018 if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
1019 if (!accounting_enabled) {
1020 hibernate_end_time_elapsed(now);
1021 } else if (hibernate_hard_limit_reached()) {
1022 hibernate_go_dormant(now);
1023 } else if (hibernate_end_time <= now) {
1024 /* The hibernation period ended while we were still in lowbandwidth.*/
1025 hibernate_end_time_elapsed(now);
1029 /* Dispatch a controller event if the hibernation state changed. */
1030 if (hibernate_state != prev_state)
1031 on_hibernate_state_change(prev_state);
1034 /** Helper function: called when we get a GETINFO request for an
1035 * accounting-related key on the control connection <b>conn</b>. If we can
1036 * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
1037 * allocated string holding the result. Otherwise, set *<b>answer</b> to
1038 * NULL. */
1040 getinfo_helper_accounting(control_connection_t *conn,
1041 const char *question, char **answer,
1042 const char **errmsg)
1044 (void) conn;
1045 (void) errmsg;
1046 if (!strcmp(question, "accounting/enabled")) {
1047 *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
1048 } else if (!strcmp(question, "accounting/hibernating")) {
1049 *answer = tor_strdup(hibernate_state_to_string(hibernate_state));
1050 tor_strlower(*answer);
1051 } else if (!strcmp(question, "accounting/bytes")) {
1052 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1053 U64_PRINTF_ARG(n_bytes_read_in_interval),
1054 U64_PRINTF_ARG(n_bytes_written_in_interval));
1055 } else if (!strcmp(question, "accounting/bytes-left")) {
1056 uint64_t limit = get_options()->AccountingMax;
1057 if (get_options()->AccountingRule == ACCT_SUM) {
1058 uint64_t total_left = 0;
1059 uint64_t total_bytes = get_accounting_bytes();
1060 if (total_bytes < limit)
1061 total_left = limit - total_bytes;
1062 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1063 U64_PRINTF_ARG(total_left), U64_PRINTF_ARG(total_left));
1064 } else if (get_options()->AccountingRule == ACCT_IN) {
1065 uint64_t read_left = 0;
1066 if (n_bytes_read_in_interval < limit)
1067 read_left = limit - n_bytes_read_in_interval;
1068 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1069 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(limit));
1070 } else if (get_options()->AccountingRule == ACCT_OUT) {
1071 uint64_t write_left = 0;
1072 if (n_bytes_written_in_interval < limit)
1073 write_left = limit - n_bytes_written_in_interval;
1074 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1075 U64_PRINTF_ARG(limit), U64_PRINTF_ARG(write_left));
1076 } else {
1077 uint64_t read_left = 0, write_left = 0;
1078 if (n_bytes_read_in_interval < limit)
1079 read_left = limit - n_bytes_read_in_interval;
1080 if (n_bytes_written_in_interval < limit)
1081 write_left = limit - n_bytes_written_in_interval;
1082 tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
1083 U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
1085 } else if (!strcmp(question, "accounting/interval-start")) {
1086 *answer = tor_malloc(ISO_TIME_LEN+1);
1087 format_iso_time(*answer, interval_start_time);
1088 } else if (!strcmp(question, "accounting/interval-wake")) {
1089 *answer = tor_malloc(ISO_TIME_LEN+1);
1090 format_iso_time(*answer, interval_wakeup_time);
1091 } else if (!strcmp(question, "accounting/interval-end")) {
1092 *answer = tor_malloc(ISO_TIME_LEN+1);
1093 format_iso_time(*answer, interval_end_time);
1094 } else {
1095 *answer = NULL;
1097 return 0;
1101 * Helper function: called when the hibernation state changes, and sends a
1102 * SERVER_STATUS event to notify interested controllers of the accounting
1103 * state change.
1105 static void
1106 on_hibernate_state_change(hibernate_state_t prev_state)
1108 (void)prev_state; /* Should we do something with this? */
1109 control_event_server_status(LOG_NOTICE,
1110 "HIBERNATION_STATUS STATUS=%s",
1111 hibernate_state_to_string(hibernate_state));
1114 #ifdef TOR_UNIT_TESTS
1116 * Manually change the hibernation state. Private; used only by the unit
1117 * tests.
1119 void
1120 hibernate_set_state_for_testing_(hibernate_state_t newstate)
1122 hibernate_state = newstate;
1124 #endif