1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2015, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
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
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
20 (entered when bandwidth hard limit reached)
21 - close all OR/AP/exit conns)
24 #define HIBERNATE_PRIVATE
27 #include "channeltls.h"
29 #include "connection.h"
30 #include "connection_edge.h"
31 #include "hibernate.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
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. */
50 UNIT_MONTH
=1, UNIT_WEEK
=2, UNIT_DAY
=3,
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
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
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
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
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
109 static int cfg_start_day
= 0,
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);
121 * Functions for bandwidth accounting.
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
)
134 const char *v
= options
->AccountingStart
;
139 if (!validate_only
) {
140 cfg_unit
= UNIT_MONTH
;
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");
155 s
= smartlist_get(items
,0);
156 if (0==strcasecmp(s
, "month")) {
158 } else if (0==strcasecmp(s
, "week")) {
160 } else if (0==strcasecmp(s
, "day")) {
164 "Unrecognized accounting unit '%s': only 'month', 'week',"
165 " and 'day' are supported.", s
);
171 d
= tor_parse_long(smartlist_get(items
,1), 10, 1, 7, &ok
, NULL
);
173 log_warn(LD_CONFIG
, "Weekly accounting must begin on a day between "
174 "1 (Monday) and 7 (Sunday)");
179 d
= tor_parse_long(smartlist_get(items
,1), 10, 1, 28, &ok
, NULL
);
181 log_warn(LD_CONFIG
, "Monthly accounting must begin on a day between "
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] */
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":"");
202 s
= smartlist_get(items
, idx
);
203 h
= tor_parse_long(s
, 10, 0, 23, &ok
, &cp
);
205 log_warn(LD_CONFIG
,"Accounting start time not parseable: bad hour.");
208 if (!cp
|| *cp
!=':') {
210 "Accounting start time not parseable: not in HH:MM format");
213 m
= tor_parse_long(cp
+1, 10, 0, 59, &ok
, &cp
);
215 log_warn(LD_CONFIG
, "Accounting start time not parseable: bad minute");
218 if (!cp
|| *cp
!='\0') {
220 "Accounting start time not parseable: not in HH:MM format");
224 if (!validate_only
) {
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
);
234 SMARTLIST_FOREACH(items
, char *, item
, tor_free(item
));
235 smartlist_free(items
);
239 /** If we want to manage the accounting system and potentially
240 * hibernate, return 1, else return 0.
243 accounting_is_enabled
,(const or_options_t
*options
))
245 if (options
->AccountingMax
)
250 /** If accounting is enabled, return how long (in seconds) this
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. */
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. */
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
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> */
282 edge_of_accounting_period_containing(time_t now
, int get_end
)
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. */
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
)) {
303 /* Otherwise, the month is correct. */
304 tm
.tm_mday
= cfg_start_day
;
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
)
334 tm
.tm_hour
= cfg_start_hour
;
335 tm
.tm_min
= cfg_start_min
;
337 tm
.tm_isdst
= -1; /* Autodetect DST */
341 /** Return the start of the accounting period containing the time
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>. */
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
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. */
368 configure_accounting(time_t 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
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
);
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
);
405 "Mismatched accounting interval: moved by %.02f%%. "
406 "Starting a fresh one.", delta
*100);
407 reset_accounting(now
);
410 accounting_set_wakeup_time();
413 /** Return the relevant number of bytes sent/received this interval
414 * based on the set AccountingRule */
416 get_accounting_bytes(void)
418 if (get_options()->AccountingRule
== ACCT_SUM
)
419 return n_bytes_read_in_interval
+n_bytes_written_in_interval
;
421 return MAX(n_bytes_read_in_interval
, n_bytes_written_in_interval
);
424 /** Set expected_bandwidth_usage based on how much we sent/received
425 * per minute last interval (if we were up for at least 30 minutes),
426 * or based on our declared bandwidth otherwise. */
428 update_expected_bandwidth(void)
431 const or_options_t
*options
= get_options();
432 uint64_t max_configured
= (options
->RelayBandwidthRate
> 0 ?
433 options
->RelayBandwidthRate
:
434 options
->BandwidthRate
) * 60;
435 /* max_configured is the larger of bytes read and bytes written
436 * If we are accounting based on sum, worst case is both are
437 * at max, doubling the expected sum of bandwidth */
438 if (get_options()->AccountingRule
== ACCT_SUM
)
441 #define MIN_TIME_FOR_MEASUREMENT (1800)
443 if (soft_limit_hit_at
> interval_start_time
&& n_bytes_at_soft_limit
&&
444 (soft_limit_hit_at
- interval_start_time
) > MIN_TIME_FOR_MEASUREMENT
) {
445 /* If we hit our soft limit last time, only count the bytes up to that
446 * time. This is a better predictor of our actual bandwidth than
447 * considering the entirety of the last interval, since we likely started
448 * using bytes very slowly once we hit our soft limit. */
449 expected
= n_bytes_at_soft_limit
/
450 (soft_limit_hit_at
- interval_start_time
);
452 } else if (n_seconds_active_in_interval
>= MIN_TIME_FOR_MEASUREMENT
) {
453 /* Otherwise, we either measured enough time in the last interval but
454 * never hit our soft limit, or we're using a state file from a Tor that
455 * doesn't know to store soft-limit info. Just take rate at which
456 * we were reading/writing in the last interval as our expected rate.
458 uint64_t used
= get_accounting_bytes();
459 expected
= used
/ (n_seconds_active_in_interval
/ 60);
461 /* If we haven't gotten enough data last interval, set 'expected'
462 * to 0. This will set our wakeup to the start of the interval.
463 * Next interval, we'll choose our starting time based on how much
464 * we sent this interval.
468 if (expected
> max_configured
)
469 expected
= max_configured
;
470 expected_bandwidth_usage
= expected
;
473 /** Called at the start of a new accounting interval: reset our
474 * expected bandwidth usage based on what happened last time, set up
475 * the start and end of the interval, and clear byte/time totals.
478 reset_accounting(time_t now
)
480 log_info(LD_ACCT
, "Starting new accounting interval.");
481 update_expected_bandwidth();
482 interval_start_time
= start_of_accounting_period_containing(now
);
483 interval_end_time
= start_of_accounting_period_after(interval_start_time
);
484 n_bytes_read_in_interval
= 0;
485 n_bytes_written_in_interval
= 0;
486 n_seconds_active_in_interval
= 0;
487 n_bytes_at_soft_limit
= 0;
488 soft_limit_hit_at
= 0;
489 n_seconds_to_hit_soft_limit
= 0;
492 /** Return true iff we should save our bandwidth usage to disk. */
494 time_to_record_bandwidth_usage(time_t now
)
496 /* Note every 600 sec */
497 #define NOTE_INTERVAL (600)
498 /* Or every 20 megabytes */
499 #define NOTE_BYTES 20*(1024*1024)
500 static uint64_t last_read_bytes_noted
= 0;
501 static uint64_t last_written_bytes_noted
= 0;
502 static time_t last_time_noted
= 0;
504 if (last_time_noted
+ NOTE_INTERVAL
<= now
||
505 last_read_bytes_noted
+ NOTE_BYTES
<= n_bytes_read_in_interval
||
506 last_written_bytes_noted
+ NOTE_BYTES
<= n_bytes_written_in_interval
||
507 (interval_end_time
&& interval_end_time
<= now
)) {
508 last_time_noted
= now
;
509 last_read_bytes_noted
= n_bytes_read_in_interval
;
510 last_written_bytes_noted
= n_bytes_written_in_interval
;
516 /** Invoked once per second. Checks whether it is time to hibernate,
517 * record bandwidth used, etc. */
519 accounting_run_housekeeping(time_t now
)
521 if (now
>= interval_end_time
) {
522 configure_accounting(now
);
524 if (time_to_record_bandwidth_usage(now
)) {
525 if (accounting_record_bandwidth_usage(now
, get_or_state())) {
526 log_warn(LD_FS
, "Couldn't record bandwidth usage to disk.");
531 /** Based on our interval and our estimated bandwidth, choose a
532 * deterministic (but random-ish) time to wake up. */
534 accounting_set_wakeup_time(void)
536 char digest
[DIGEST_LEN
];
537 crypto_digest_t
*d_env
;
538 uint64_t time_to_exhaust_bw
;
539 int time_to_consider
;
541 if (! server_identity_key_is_set()) {
542 if (init_keys() < 0) {
543 log_err(LD_BUG
, "Error initializing keys");
548 if (server_identity_key_is_set()) {
549 char buf
[ISO_TIME_LEN
+1];
550 format_iso_time(buf
, interval_start_time
);
552 crypto_pk_get_digest(get_server_identity_key(), digest
);
554 d_env
= crypto_digest_new();
555 crypto_digest_add_bytes(d_env
, buf
, ISO_TIME_LEN
);
556 crypto_digest_add_bytes(d_env
, digest
, DIGEST_LEN
);
557 crypto_digest_get_digest(d_env
, digest
, DIGEST_LEN
);
558 crypto_digest_free(d_env
);
560 crypto_rand(digest
, DIGEST_LEN
);
563 if (!expected_bandwidth_usage
) {
564 char buf1
[ISO_TIME_LEN
+1];
565 char buf2
[ISO_TIME_LEN
+1];
566 format_local_iso_time(buf1
, interval_start_time
);
567 format_local_iso_time(buf2
, interval_end_time
);
568 interval_wakeup_time
= interval_start_time
;
571 "Configured hibernation. This interval begins at %s "
572 "and ends at %s. We have no prior estimate for bandwidth, so "
573 "we will start out awake and hibernate when we exhaust our quota.",
579 (get_options()->AccountingMax
/expected_bandwidth_usage
)*60;
580 if (time_to_exhaust_bw
> INT_MAX
) {
581 time_to_exhaust_bw
= INT_MAX
;
582 time_to_consider
= 0;
584 time_to_consider
= accounting_get_interval_length() -
585 (int)time_to_exhaust_bw
;
588 if (time_to_consider
<=0) {
589 interval_wakeup_time
= interval_start_time
;
591 /* XXX can we simplify this just by picking a random (non-deterministic)
592 * time to be up? If we go down and come up, then we pick a new one. Is
593 * that good enough? -RD */
595 /* This is not a perfectly unbiased conversion, but it is good enough:
596 * in the worst case, the first half of the day is 0.06 percent likelier
597 * to be chosen than the last half. */
598 interval_wakeup_time
= interval_start_time
+
599 (get_uint32(digest
) % time_to_consider
);
603 char buf1
[ISO_TIME_LEN
+1];
604 char buf2
[ISO_TIME_LEN
+1];
605 char buf3
[ISO_TIME_LEN
+1];
606 char buf4
[ISO_TIME_LEN
+1];
608 if (interval_wakeup_time
+time_to_exhaust_bw
> TIME_MAX
)
609 down_time
= TIME_MAX
;
611 down_time
= (time_t)(interval_wakeup_time
+time_to_exhaust_bw
);
612 if (down_time
>interval_end_time
)
613 down_time
= interval_end_time
;
614 format_local_iso_time(buf1
, interval_start_time
);
615 format_local_iso_time(buf2
, interval_wakeup_time
);
616 format_local_iso_time(buf3
, down_time
);
617 format_local_iso_time(buf4
, interval_end_time
);
620 "Configured hibernation. This interval began at %s; "
621 "the scheduled wake-up time %s %s; "
622 "we expect%s to exhaust our quota for this interval around %s; "
623 "the next interval begins at %s (all times local)",
625 time(NULL
)<interval_wakeup_time
?"is":"was", buf2
,
626 time(NULL
)<down_time
?"":"ed", buf3
,
631 /* This rounds 0 up to 1000, but that's actually a feature. */
632 #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
633 /** Save all our bandwidth tracking information to disk. Return 0 on
634 * success, -1 on failure. */
636 accounting_record_bandwidth_usage(time_t now
, or_state_t
*state
)
638 /* Just update the state */
639 state
->AccountingIntervalStart
= interval_start_time
;
640 state
->AccountingBytesReadInInterval
= ROUND_UP(n_bytes_read_in_interval
);
641 state
->AccountingBytesWrittenInInterval
=
642 ROUND_UP(n_bytes_written_in_interval
);
643 state
->AccountingSecondsActive
= n_seconds_active_in_interval
;
644 state
->AccountingExpectedUsage
= expected_bandwidth_usage
;
646 state
->AccountingSecondsToReachSoftLimit
= n_seconds_to_hit_soft_limit
;
647 state
->AccountingSoftLimitHitAt
= soft_limit_hit_at
;
648 state
->AccountingBytesAtSoftLimit
= n_bytes_at_soft_limit
;
650 or_state_mark_dirty(state
,
651 now
+(get_options()->AvoidDiskWrites
? 7200 : 60));
657 /** Read stored accounting information from disk. Return 0 on success;
658 * return -1 and change nothing on failure. */
660 read_bandwidth_usage(void)
662 or_state_t
*state
= get_or_state();
665 char *fname
= get_datadir_fname("bw_accounting");
671 "Failed to unlink %s: %s",
672 fname
, strerror(errno
));
681 log_info(LD_ACCT
, "Reading bandwidth accounting data from state file");
682 n_bytes_read_in_interval
= state
->AccountingBytesReadInInterval
;
683 n_bytes_written_in_interval
= state
->AccountingBytesWrittenInInterval
;
684 n_seconds_active_in_interval
= state
->AccountingSecondsActive
;
685 interval_start_time
= state
->AccountingIntervalStart
;
686 expected_bandwidth_usage
= state
->AccountingExpectedUsage
;
688 /* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
689 * fields. If you switch back and forth, you might get an
690 * AccountingSoftLimitHitAt value from long before the most recent
691 * interval_start_time. If that's so, then ignore the softlimit-related
693 if (state
->AccountingSoftLimitHitAt
> interval_start_time
) {
694 soft_limit_hit_at
= state
->AccountingSoftLimitHitAt
;
695 n_bytes_at_soft_limit
= state
->AccountingBytesAtSoftLimit
;
696 n_seconds_to_hit_soft_limit
= state
->AccountingSecondsToReachSoftLimit
;
698 soft_limit_hit_at
= 0;
699 n_bytes_at_soft_limit
= 0;
700 n_seconds_to_hit_soft_limit
= 0;
704 char tbuf1
[ISO_TIME_LEN
+1];
705 char tbuf2
[ISO_TIME_LEN
+1];
706 format_iso_time(tbuf1
, state
->LastWritten
);
707 format_iso_time(tbuf2
, state
->AccountingIntervalStart
);
710 "Successfully read bandwidth accounting info from state written at %s "
711 "for interval starting at %s. We have been active for %lu seconds in "
712 "this interval. At the start of the interval, we expected to use "
713 "about %lu KB per second. ("U64_FORMAT
" bytes read so far, "
714 U64_FORMAT
" bytes written so far)",
716 (unsigned long)n_seconds_active_in_interval
,
717 (unsigned long)(expected_bandwidth_usage
*1024/60),
718 U64_PRINTF_ARG(n_bytes_read_in_interval
),
719 U64_PRINTF_ARG(n_bytes_written_in_interval
));
725 /** Return true iff we have sent/received all the bytes we are willing
726 * to send/receive this interval. */
728 hibernate_hard_limit_reached(void)
730 uint64_t hard_limit
= get_options()->AccountingMax
;
733 return get_accounting_bytes() >= hard_limit
;
736 /** Return true iff we have sent/received almost all the bytes we are willing
737 * to send/receive this interval. */
739 hibernate_soft_limit_reached(void)
741 const uint64_t acct_max
= get_options()->AccountingMax
;
742 #define SOFT_LIM_PCT (.95)
743 #define SOFT_LIM_BYTES (500*1024*1024)
744 #define SOFT_LIM_MINUTES (3*60)
745 /* The 'soft limit' is a fair bit more complicated now than once it was.
746 * We want to stop accepting connections when ALL of the following are true:
747 * - We expect to use up the remaining bytes in under 3 hours
748 * - We have used up 95% of our bytes.
749 * - We have less than 500MB of bytes left.
751 uint64_t soft_limit
= DBL_TO_U64(U64_TO_DBL(acct_max
) * SOFT_LIM_PCT
);
752 if (acct_max
> SOFT_LIM_BYTES
&& acct_max
- SOFT_LIM_BYTES
> soft_limit
) {
753 soft_limit
= acct_max
- SOFT_LIM_BYTES
;
755 if (expected_bandwidth_usage
) {
756 const uint64_t expected_usage
=
757 expected_bandwidth_usage
* SOFT_LIM_MINUTES
;
758 if (acct_max
> expected_usage
&& acct_max
- expected_usage
> soft_limit
)
759 soft_limit
= acct_max
- expected_usage
;
764 return get_accounting_bytes() >= soft_limit
;
767 /** Called when we get a SIGINT, or when bandwidth soft limit is
768 * reached. Puts us into "loose hibernation": we don't accept new
769 * connections, but we continue handling old ones. */
771 hibernate_begin(hibernate_state_t new_state
, time_t now
)
773 const or_options_t
*options
= get_options();
775 if (new_state
== HIBERNATE_STATE_EXITING
&&
776 hibernate_state
!= HIBERNATE_STATE_LIVE
) {
777 log_notice(LD_GENERAL
,"SIGINT received %s; exiting now.",
778 hibernate_state
== HIBERNATE_STATE_EXITING
?
779 "a second time" : "while hibernating");
784 if (new_state
== HIBERNATE_STATE_LOWBANDWIDTH
&&
785 hibernate_state
== HIBERNATE_STATE_LIVE
) {
786 soft_limit_hit_at
= now
;
787 n_seconds_to_hit_soft_limit
= n_seconds_active_in_interval
;
788 n_bytes_at_soft_limit
= get_accounting_bytes();
791 /* close listeners. leave control listener(s). */
792 connection_mark_all_noncontrol_listeners();
794 /* XXX kill intro point circs */
795 /* XXX upload rendezvous service descriptors with no intro points */
797 if (new_state
== HIBERNATE_STATE_EXITING
) {
798 log_notice(LD_GENERAL
,"Interrupt: we have stopped accepting new "
799 "connections, and will shut down in %d seconds. Interrupt "
800 "again to exit now.", options
->ShutdownWaitLength
);
801 shutdown_time
= time(NULL
) + options
->ShutdownWaitLength
;
802 } else { /* soft limit reached */
803 hibernate_end_time
= interval_end_time
;
806 hibernate_state
= new_state
;
807 accounting_record_bandwidth_usage(now
, get_or_state());
809 or_state_mark_dirty(get_or_state(),
810 get_options()->AvoidDiskWrites
? now
+600 : 0);
813 /** Called when we've been hibernating and our timeout is reached. */
815 hibernate_end(hibernate_state_t new_state
)
817 tor_assert(hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
||
818 hibernate_state
== HIBERNATE_STATE_DORMANT
||
819 hibernate_state
== HIBERNATE_STATE_INITIAL
);
821 /* listeners will be relaunched in run_scheduled_events() in main.c */
822 if (hibernate_state
!= HIBERNATE_STATE_INITIAL
)
823 log_notice(LD_ACCT
,"Hibernation period ended. Resuming normal activity.");
825 hibernate_state
= new_state
;
826 hibernate_end_time
= 0; /* no longer hibernating */
827 stats_n_seconds_working
= 0; /* reset published uptime */
830 /** A wrapper around hibernate_begin, for when we get SIGINT. */
832 hibernate_begin_shutdown(void)
834 hibernate_begin(HIBERNATE_STATE_EXITING
, time(NULL
));
837 /** Return true iff we are currently hibernating. */
839 we_are_hibernating
,(void))
841 return hibernate_state
!= HIBERNATE_STATE_LIVE
;
844 /** If we aren't currently dormant, close all connections and become
847 hibernate_go_dormant(time_t now
)
851 if (hibernate_state
== HIBERNATE_STATE_DORMANT
)
853 else if (hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
)
854 hibernate_state
= HIBERNATE_STATE_DORMANT
;
856 hibernate_begin(HIBERNATE_STATE_DORMANT
, now
);
858 log_notice(LD_ACCT
,"Going dormant. Blowing away remaining connections.");
860 /* Close all OR/AP/exit conns. Leave dir conns because we still want
861 * to be able to upload server descriptors so people know we're still
862 * running, and download directories so we can detect if we're obsolete.
863 * Leave control conns because we still want to be controllable.
865 while ((conn
= connection_get_by_type(CONN_TYPE_OR
)) ||
866 (conn
= connection_get_by_type(CONN_TYPE_AP
)) ||
867 (conn
= connection_get_by_type(CONN_TYPE_EXIT
))) {
868 if (CONN_IS_EDGE(conn
))
869 connection_edge_end(TO_EDGE_CONN(conn
), END_STREAM_REASON_HIBERNATING
);
870 log_info(LD_NET
,"Closing conn type %d", conn
->type
);
871 if (conn
->type
== CONN_TYPE_AP
) /* send socks failure if needed */
872 connection_mark_unattached_ap(TO_ENTRY_CONN(conn
),
873 END_STREAM_REASON_HIBERNATING
);
874 else if (conn
->type
== CONN_TYPE_OR
) {
875 if (TO_OR_CONN(conn
)->chan
) {
876 channel_mark_for_close(TLS_CHAN_TO_BASE(TO_OR_CONN(conn
)->chan
));
878 connection_mark_for_close(conn
);
881 connection_mark_for_close(conn
);
884 if (now
< interval_wakeup_time
)
885 hibernate_end_time
= interval_wakeup_time
;
887 hibernate_end_time
= interval_end_time
;
889 accounting_record_bandwidth_usage(now
, get_or_state());
891 or_state_mark_dirty(get_or_state(),
892 get_options()->AvoidDiskWrites
? now
+600 : 0);
895 /** Called when hibernate_end_time has arrived. */
897 hibernate_end_time_elapsed(time_t now
)
899 char buf
[ISO_TIME_LEN
+1];
901 /* The interval has ended, or it is wakeup time. Find out which. */
902 accounting_run_housekeeping(now
);
903 if (interval_wakeup_time
<= now
) {
904 /* The interval hasn't changed, but interval_wakeup_time has passed.
905 * It's time to wake up and start being a server. */
906 hibernate_end(HIBERNATE_STATE_LIVE
);
909 /* The interval has changed, and it isn't time to wake up yet. */
910 hibernate_end_time
= interval_wakeup_time
;
911 format_iso_time(buf
,interval_wakeup_time
);
912 if (hibernate_state
!= HIBERNATE_STATE_DORMANT
) {
913 /* We weren't sleeping before; we should sleep now. */
915 "Accounting period ended. Commencing hibernation until "
917 hibernate_go_dormant(now
);
920 "Accounting period ended. This period, we will hibernate"
921 " until %s UTC",buf
);
926 /** Consider our environment and decide if it's time
927 * to start/stop hibernating.
930 consider_hibernation(time_t now
)
932 int accounting_enabled
= get_options()->AccountingMax
!= 0;
933 char buf
[ISO_TIME_LEN
+1];
935 /* If we're in 'exiting' mode, then we just shut down after the interval
937 if (hibernate_state
== HIBERNATE_STATE_EXITING
) {
938 tor_assert(shutdown_time
);
939 if (shutdown_time
<= now
) {
940 log_notice(LD_GENERAL
, "Clean shutdown finished. Exiting.");
944 return; /* if exiting soon, don't worry about bandwidth limits */
947 if (hibernate_state
== HIBERNATE_STATE_DORMANT
) {
948 /* We've been hibernating because of bandwidth accounting. */
949 tor_assert(hibernate_end_time
);
950 if (hibernate_end_time
> now
&& accounting_enabled
) {
951 /* If we're hibernating, don't wake up until it's time, regardless of
952 * whether we're in a new interval. */
955 hibernate_end_time_elapsed(now
);
959 /* Else, we aren't hibernating. See if it's time to start hibernating, or to
961 if (hibernate_state
== HIBERNATE_STATE_LIVE
||
962 hibernate_state
== HIBERNATE_STATE_INITIAL
) {
963 if (hibernate_soft_limit_reached()) {
965 "Bandwidth soft limit reached; commencing hibernation. "
966 "No new connections will be accepted");
967 hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH
, now
);
968 } else if (accounting_enabled
&& now
< interval_wakeup_time
) {
969 format_local_iso_time(buf
,interval_wakeup_time
);
971 "Commencing hibernation. We will wake up at %s local time.",
973 hibernate_go_dormant(now
);
974 } else if (hibernate_state
== HIBERNATE_STATE_INITIAL
) {
975 hibernate_end(HIBERNATE_STATE_LIVE
);
979 if (hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
) {
980 if (!accounting_enabled
) {
981 hibernate_end_time_elapsed(now
);
982 } else if (hibernate_hard_limit_reached()) {
983 hibernate_go_dormant(now
);
984 } else if (hibernate_end_time
<= now
) {
985 /* The hibernation period ended while we were still in lowbandwidth.*/
986 hibernate_end_time_elapsed(now
);
991 /** Helper function: called when we get a GETINFO request for an
992 * accounting-related key on the control connection <b>conn</b>. If we can
993 * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
994 * allocated string holding the result. Otherwise, set *<b>answer</b> to
997 getinfo_helper_accounting(control_connection_t
*conn
,
998 const char *question
, char **answer
,
1003 if (!strcmp(question
, "accounting/enabled")) {
1004 *answer
= tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
1005 } else if (!strcmp(question
, "accounting/hibernating")) {
1006 if (hibernate_state
== HIBERNATE_STATE_DORMANT
)
1007 *answer
= tor_strdup("hard");
1008 else if (hibernate_state
== HIBERNATE_STATE_LOWBANDWIDTH
)
1009 *answer
= tor_strdup("soft");
1011 *answer
= tor_strdup("awake");
1012 } else if (!strcmp(question
, "accounting/bytes")) {
1013 tor_asprintf(answer
, U64_FORMAT
" "U64_FORMAT
,
1014 U64_PRINTF_ARG(n_bytes_read_in_interval
),
1015 U64_PRINTF_ARG(n_bytes_written_in_interval
));
1016 } else if (!strcmp(question
, "accounting/bytes-left")) {
1017 uint64_t limit
= get_options()->AccountingMax
;
1018 if (get_options()->AccountingRule
== ACCT_SUM
) {
1019 uint64_t total_left
= 0;
1020 uint64_t total_bytes
= get_accounting_bytes();
1021 if (total_bytes
< limit
)
1022 total_left
= limit
- total_bytes
;
1023 tor_asprintf(answer
, U64_FORMAT
" "U64_FORMAT
,
1024 U64_PRINTF_ARG(total_left
), U64_PRINTF_ARG(total_left
));
1026 uint64_t read_left
= 0, write_left
= 0;
1027 if (n_bytes_read_in_interval
< limit
)
1028 read_left
= limit
- n_bytes_read_in_interval
;
1029 if (n_bytes_written_in_interval
< limit
)
1030 write_left
= limit
- n_bytes_written_in_interval
;
1031 tor_asprintf(answer
, U64_FORMAT
" "U64_FORMAT
,
1032 U64_PRINTF_ARG(read_left
), U64_PRINTF_ARG(write_left
));
1034 } else if (!strcmp(question
, "accounting/interval-start")) {
1035 *answer
= tor_malloc(ISO_TIME_LEN
+1);
1036 format_iso_time(*answer
, interval_start_time
);
1037 } else if (!strcmp(question
, "accounting/interval-wake")) {
1038 *answer
= tor_malloc(ISO_TIME_LEN
+1);
1039 format_iso_time(*answer
, interval_wakeup_time
);
1040 } else if (!strcmp(question
, "accounting/interval-end")) {
1041 *answer
= tor_malloc(ISO_TIME_LEN
+1);
1042 format_iso_time(*answer
, interval_end_time
);
1049 #ifdef TOR_UNIT_TESTS
1051 * Manually change the hibernation state. Private; used only by the unit
1055 hibernate_set_state_for_testing_(hibernate_state_t newstate
)
1057 hibernate_state
= newstate
;