2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2004-10-31 21:29:25 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file hibernate.c
|
|
|
|
* \brief Functions to close listeners, stop allowing new circuits,
|
2004-11-05 00:39:57 +01:00
|
|
|
* etc in preparation for closing down or going dormant; and to track
|
|
|
|
* bandwidth and time intervals to know when to hibernate and when to
|
|
|
|
* stop hibernating.
|
2016-10-15 02:08:51 +02:00
|
|
|
*
|
|
|
|
* Ordinarily a Tor relay is "Live".
|
|
|
|
*
|
|
|
|
* A live relay can stop accepting connections for one of two reasons: either
|
|
|
|
* it is trying to conserve bandwidth because of bandwidth accounting rules
|
|
|
|
* ("soft hibernation"), or it is about to shut down ("exiting").
|
2004-10-31 21:29:25 +01:00
|
|
|
**/
|
|
|
|
|
|
|
|
/*
|
|
|
|
hibernating, phase 1:
|
|
|
|
- send destroy in response to create cells
|
|
|
|
- send end (policy failed) in response to begin cells
|
|
|
|
- close an OR conn when it has no circuits
|
|
|
|
|
|
|
|
hibernating, phase 2:
|
|
|
|
(entered when bandwidth hard limit reached)
|
|
|
|
- close all OR/AP/exit conns)
|
|
|
|
*/
|
|
|
|
|
2011-09-07 23:48:21 +02:00
|
|
|
#define HIBERNATE_PRIVATE
|
2004-10-31 21:29:25 +01:00
|
|
|
#include "or.h"
|
2012-11-13 22:45:00 +01:00
|
|
|
#include "channel.h"
|
|
|
|
#include "channeltls.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2017-09-19 06:17:35 +02:00
|
|
|
#include "connection_or.h"
|
2016-03-30 22:19:11 +02:00
|
|
|
#include "control.h"
|
2010-07-22 12:30:46 +02:00
|
|
|
#include "hibernate.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2012-09-12 23:58:33 +02:00
|
|
|
#include "statefile.h"
|
2004-10-31 21:29:25 +01:00
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Are we currently awake, asleep, running out of bandwidth, or shutting
|
|
|
|
* down? */
|
2011-09-01 05:21:50 +02:00
|
|
|
static hibernate_state_t hibernate_state = HIBERNATE_STATE_INITIAL;
|
2004-11-08 00:14:47 +01:00
|
|
|
/** If are hibernating, when do we plan to wake up? Set to 0 if we
|
|
|
|
* aren't hibernating. */
|
|
|
|
static time_t hibernate_end_time = 0;
|
2007-02-07 07:54:33 +01:00
|
|
|
/** If we are shutting down, when do we plan finally exit? Set to 0 if
|
|
|
|
* we aren't shutting down. */
|
|
|
|
static time_t shutdown_time = 0;
|
2004-11-08 00:14:47 +01:00
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Possible accounting periods. */
|
2004-11-12 17:39:03 +01:00
|
|
|
typedef enum {
|
2004-11-22 22:56:51 +01:00
|
|
|
UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
|
2004-11-12 17:39:03 +01:00
|
|
|
} time_unit_t;
|
|
|
|
|
2016-10-15 02:08:51 +02:00
|
|
|
/*
|
|
|
|
* @file hibernate.c
|
2004-11-08 00:14:47 +01:00
|
|
|
*
|
2016-10-15 02:08:51 +02:00
|
|
|
* <h4>Accounting</h4>
|
2007-02-07 07:54:33 +01:00
|
|
|
* Accounting is designed to ensure that no more than N bytes are sent in
|
|
|
|
* either direction over a given interval (currently, one month, one week, or
|
|
|
|
* one day) We could
|
2004-11-08 00:14:47 +01:00
|
|
|
* try to do this by choking our bandwidth to a trickle, but that
|
|
|
|
* would make our streams useless. Instead, we estimate what our
|
|
|
|
* bandwidth usage will be, and guess how long we'll be able to
|
|
|
|
* provide that much bandwidth before hitting our limit. We then
|
|
|
|
* choose a random time within the accounting interval to come up (so
|
|
|
|
* that we don't get 50 Tors running on the 1st of the month and none
|
|
|
|
* on the 30th).
|
|
|
|
*
|
|
|
|
* Each interval runs as follows:
|
|
|
|
*
|
2016-10-15 02:08:51 +02:00
|
|
|
* <ol>
|
|
|
|
* <li>We guess our bandwidth usage, based on how much we used
|
2004-11-08 00:14:47 +01:00
|
|
|
* last time. We choose a "wakeup time" within the interval to come up.
|
2016-10-15 02:08:51 +02:00
|
|
|
* <li>Until the chosen wakeup time, we hibernate.
|
|
|
|
* <li> We come up at the wakeup time, and provide bandwidth until we are
|
2004-11-08 00:14:47 +01:00
|
|
|
* "very close" to running out.
|
2016-10-15 02:08:51 +02:00
|
|
|
* <li> Then we go into low-bandwidth mode, and stop accepting new
|
2004-11-08 00:14:47 +01:00
|
|
|
* connections, but provide bandwidth until we run out.
|
2016-10-15 02:08:51 +02:00
|
|
|
* <li> Then we hibernate until the end of the interval.
|
2004-11-08 00:14:47 +01:00
|
|
|
*
|
2004-12-01 04:48:14 +01:00
|
|
|
* If the interval ends before we run out of bandwidth, we go back to
|
2004-11-08 00:14:47 +01:00
|
|
|
* step one.
|
2016-10-15 02:08:51 +02:00
|
|
|
*
|
|
|
|
* Accounting is controlled by the AccountingMax, AccountingRule, and
|
|
|
|
* AccountingStart options.
|
2004-11-08 00:14:47 +01:00
|
|
|
*/
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** How many bytes have we read in this accounting interval? */
|
2004-11-04 23:33:06 +01:00
|
|
|
static uint64_t n_bytes_read_in_interval = 0;
|
2007-02-02 21:06:43 +01:00
|
|
|
/** How many bytes have we written in this accounting interval? */
|
2004-11-04 23:33:06 +01:00
|
|
|
static uint64_t n_bytes_written_in_interval = 0;
|
2004-11-05 18:55:34 +01:00
|
|
|
/** How many seconds have we been running this interval? */
|
2004-11-04 23:33:06 +01:00
|
|
|
static uint32_t n_seconds_active_in_interval = 0;
|
2010-09-03 20:29:17 +02:00
|
|
|
/** How many seconds were we active in this interval before we hit our soft
|
|
|
|
* limit? */
|
|
|
|
static int n_seconds_to_hit_soft_limit = 0;
|
|
|
|
/** When in this interval was the soft limit hit. */
|
|
|
|
static time_t soft_limit_hit_at = 0;
|
|
|
|
/** How many bytes had we read/written when we hit the soft limit? */
|
|
|
|
static uint64_t n_bytes_at_soft_limit = 0;
|
2004-11-04 23:33:06 +01:00
|
|
|
/** When did this accounting interval start? */
|
|
|
|
static time_t interval_start_time = 0;
|
|
|
|
/** When will this accounting interval end? */
|
|
|
|
static time_t interval_end_time = 0;
|
|
|
|
/** How far into the accounting interval should we hibernate? */
|
|
|
|
static time_t interval_wakeup_time = 0;
|
2004-11-23 23:34:23 +01:00
|
|
|
/** How much bandwidth do we 'expect' to use per minute? (0 if we have no
|
|
|
|
* info from the last period.) */
|
2006-12-17 17:37:46 +01:00
|
|
|
static uint64_t expected_bandwidth_usage = 0;
|
2004-11-22 22:56:51 +01:00
|
|
|
/** What unit are we using for our accounting? */
|
|
|
|
static time_unit_t cfg_unit = UNIT_MONTH;
|
2008-12-23 18:56:31 +01:00
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
/** How many days,hours,minutes into each unit does our accounting interval
|
|
|
|
* start? */
|
2012-06-05 01:51:00 +02:00
|
|
|
/** @{ */
|
2008-12-23 18:56:31 +01:00
|
|
|
static int cfg_start_day = 0,
|
|
|
|
cfg_start_hour = 0,
|
|
|
|
cfg_start_min = 0;
|
2012-06-05 01:51:00 +02:00
|
|
|
/** @} */
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2016-03-30 22:19:11 +02:00
|
|
|
static const char *hibernate_state_to_string(hibernate_state_t state);
|
2004-11-05 18:55:34 +01:00
|
|
|
static void reset_accounting(time_t now);
|
2004-11-04 23:33:06 +01:00
|
|
|
static int read_bandwidth_usage(void);
|
|
|
|
static time_t start_of_accounting_period_after(time_t now);
|
|
|
|
static time_t start_of_accounting_period_containing(time_t now);
|
|
|
|
static void accounting_set_wakeup_time(void);
|
2016-03-30 22:19:11 +02:00
|
|
|
static void on_hibernate_state_change(hibernate_state_t prev_state);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the human-readable name for the hibernation state <b>state</b>
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
hibernate_state_to_string(hibernate_state_t state)
|
|
|
|
{
|
|
|
|
static char buf[64];
|
|
|
|
switch (state) {
|
|
|
|
case HIBERNATE_STATE_EXITING: return "EXITING";
|
|
|
|
case HIBERNATE_STATE_LOWBANDWIDTH: return "SOFT";
|
|
|
|
case HIBERNATE_STATE_DORMANT: return "HARD";
|
|
|
|
case HIBERNATE_STATE_INITIAL:
|
|
|
|
case HIBERNATE_STATE_LIVE:
|
|
|
|
return "AWAKE";
|
|
|
|
default:
|
|
|
|
log_warn(LD_BUG, "unknown hibernate state %d", state);
|
|
|
|
tor_snprintf(buf, sizeof(buf), "unknown [%d]", state);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
2004-11-04 23:33:06 +01:00
|
|
|
|
|
|
|
/* ************
|
|
|
|
* Functions for bandwidth accounting.
|
|
|
|
* ************/
|
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
/** Configure accounting start/end time settings based on
|
|
|
|
* options->AccountingStart. Return 0 on success, -1 on failure. If
|
|
|
|
* <b>validate_only</b> is true, do not change the current settings. */
|
|
|
|
int
|
2011-06-14 19:01:38 +02:00
|
|
|
accounting_parse_options(const or_options_t *options, int validate_only)
|
2004-11-22 22:56:51 +01:00
|
|
|
{
|
|
|
|
time_unit_t unit;
|
|
|
|
int ok, idx;
|
|
|
|
long d,h,m;
|
|
|
|
smartlist_t *items;
|
|
|
|
const char *v = options->AccountingStart;
|
|
|
|
const char *s;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
if (!validate_only) {
|
|
|
|
cfg_unit = UNIT_MONTH;
|
|
|
|
cfg_start_day = 1;
|
|
|
|
cfg_start_hour = 0;
|
|
|
|
cfg_start_min = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
items = smartlist_new();
|
2004-12-16 22:10:51 +01:00
|
|
|
smartlist_split_string(items, v, NULL,
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
|
2004-11-22 22:56:51 +01:00
|
|
|
if (smartlist_len(items)<2) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s = smartlist_get(items,0);
|
|
|
|
if (0==strcasecmp(s, "month")) {
|
|
|
|
unit = UNIT_MONTH;
|
|
|
|
} else if (0==strcasecmp(s, "week")) {
|
|
|
|
unit = UNIT_WEEK;
|
|
|
|
} else if (0==strcasecmp(s, "day")) {
|
|
|
|
unit = UNIT_DAY;
|
|
|
|
} else {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Unrecognized accounting unit '%s': only 'month', 'week',"
|
|
|
|
" and 'day' are supported.", s);
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case UNIT_WEEK:
|
|
|
|
d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
|
|
|
|
if (!ok) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
|
|
|
|
"1 (Monday) and 7 (Sunday)");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UNIT_MONTH:
|
|
|
|
d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
|
|
|
|
if (!ok) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
|
|
|
|
"1 and 28");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UNIT_DAY:
|
|
|
|
d = 0;
|
|
|
|
break;
|
2009-10-27 03:35:29 +01:00
|
|
|
/* Coverity dislikes unreachable default cases; some compilers warn on
|
|
|
|
* switch statements missing a case. Tell Coverity not to worry. */
|
|
|
|
/* coverity[dead_error_begin] */
|
2004-11-22 22:56:51 +01:00
|
|
|
default:
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = unit==UNIT_DAY?1:2;
|
|
|
|
if (smartlist_len(items) != (idx+1)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
|
|
|
|
s, idx, (idx>1)?"s":"");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s = smartlist_get(items, idx);
|
|
|
|
h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
|
|
|
|
if (!ok) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!cp || *cp!=':') {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Accounting start time not parseable: not in HH:MM format");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
|
|
|
|
if (!ok) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!cp || *cp!='\0') {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Accounting start time not parseable: not in HH:MM format");
|
2004-11-22 22:56:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validate_only) {
|
|
|
|
cfg_unit = unit;
|
|
|
|
cfg_start_day = (int)d;
|
|
|
|
cfg_start_hour = (int)h;
|
|
|
|
cfg_start_min = (int)m;
|
|
|
|
}
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(items, char *, item, tor_free(item));
|
2004-11-22 22:56:51 +01:00
|
|
|
smartlist_free(items);
|
|
|
|
return 0;
|
|
|
|
err:
|
2007-05-17 00:15:14 +02:00
|
|
|
SMARTLIST_FOREACH(items, char *, item, tor_free(item));
|
2004-11-22 22:56:51 +01:00
|
|
|
smartlist_free(items);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-15 05:01:31 +01:00
|
|
|
/** If we want to manage the accounting system and potentially
|
|
|
|
* hibernate, return 1, else return 0.
|
|
|
|
*/
|
2014-04-15 14:20:34 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
accounting_is_enabled,(const or_options_t *options))
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-11-20 01:37:00 +01:00
|
|
|
if (options->AccountingMax)
|
2004-11-15 05:01:31 +01:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 16:45:27 +01:00
|
|
|
/** If accounting is enabled, return how long (in seconds) this
|
|
|
|
* interval lasts. */
|
|
|
|
int
|
|
|
|
accounting_get_interval_length(void)
|
|
|
|
{
|
|
|
|
return (int)(interval_end_time - interval_start_time);
|
|
|
|
}
|
|
|
|
|
2013-08-21 19:41:15 +02:00
|
|
|
/** Return the time at which the current accounting interval will end. */
|
2014-04-15 14:20:34 +02:00
|
|
|
MOCK_IMPL(time_t,
|
|
|
|
accounting_get_end_time,(void))
|
2013-06-12 10:51:39 +02:00
|
|
|
{
|
|
|
|
return interval_end_time;
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Called from main.c to tell us that <b>seconds</b> seconds have
|
|
|
|
* passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
|
|
|
|
* bytes have been written. */
|
2004-11-05 18:55:34 +01:00
|
|
|
void
|
|
|
|
accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
|
|
|
n_bytes_read_in_interval += n_read;
|
|
|
|
n_bytes_written_in_interval += n_written;
|
|
|
|
/* If we haven't been called in 10 seconds, we're probably jumping
|
|
|
|
* around in time. */
|
|
|
|
n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
|
|
|
|
}
|
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
/** If get_end, return the end of the accounting period that contains
|
|
|
|
* the time <b>now</b>. Else, return the start of the accounting
|
|
|
|
* period that contains the time <b>now</b> */
|
|
|
|
static time_t
|
|
|
|
edge_of_accounting_period_containing(time_t now, int get_end)
|
2004-11-05 18:55:34 +01:00
|
|
|
{
|
2004-11-22 22:56:51 +01:00
|
|
|
int before;
|
2005-02-22 08:03:03 +01:00
|
|
|
struct tm tm;
|
|
|
|
tor_localtime_r(&now, &tm);
|
2004-11-22 22:56:51 +01:00
|
|
|
|
|
|
|
/* Set 'before' to true iff the current time is before the hh:mm
|
|
|
|
* changeover time for today. */
|
2005-02-22 08:03:03 +01:00
|
|
|
before = tm.tm_hour < cfg_start_hour ||
|
|
|
|
(tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
|
2004-11-22 22:56:51 +01:00
|
|
|
|
|
|
|
/* Dispatch by unit. First, find the start day of the given period;
|
|
|
|
* then, if get_end is true, increment to the end day. */
|
|
|
|
switch (cfg_unit)
|
|
|
|
{
|
|
|
|
case UNIT_MONTH: {
|
|
|
|
/* If this is before the Nth, we want the Nth of last month. */
|
2005-02-22 08:03:03 +01:00
|
|
|
if (tm.tm_mday < cfg_start_day ||
|
2017-05-15 17:43:18 +02:00
|
|
|
(tm.tm_mday == cfg_start_day && before)) {
|
2005-02-22 08:03:03 +01:00
|
|
|
--tm.tm_mon;
|
2004-11-22 22:56:51 +01:00
|
|
|
}
|
|
|
|
/* Otherwise, the month is correct. */
|
2005-02-22 08:03:03 +01:00
|
|
|
tm.tm_mday = cfg_start_day;
|
2004-11-22 22:56:51 +01:00
|
|
|
if (get_end)
|
2005-02-22 08:03:03 +01:00
|
|
|
++tm.tm_mon;
|
2004-11-22 22:56:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNIT_WEEK: {
|
|
|
|
/* What is the 'target' day of the week in struct tm format? (We
|
|
|
|
say Sunday==7; struct tm says Sunday==0.) */
|
|
|
|
int wday = cfg_start_day % 7;
|
|
|
|
/* How many days do we subtract from today to get to the right day? */
|
2005-02-22 08:03:03 +01:00
|
|
|
int delta = (7+tm.tm_wday-wday)%7;
|
2004-11-22 22:56:51 +01:00
|
|
|
/* If we are on the right day, but the changeover hasn't happened yet,
|
|
|
|
* then subtract a whole week. */
|
|
|
|
if (delta == 0 && before)
|
|
|
|
delta = 7;
|
2005-02-22 08:03:03 +01:00
|
|
|
tm.tm_mday -= delta;
|
2004-11-22 22:56:51 +01:00
|
|
|
if (get_end)
|
2005-02-22 08:03:03 +01:00
|
|
|
tm.tm_mday += 7;
|
2004-11-22 22:56:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNIT_DAY:
|
|
|
|
if (before)
|
2005-02-22 08:03:03 +01:00
|
|
|
--tm.tm_mday;
|
2004-11-22 22:56:51 +01:00
|
|
|
if (get_end)
|
2005-02-22 08:03:03 +01:00
|
|
|
++tm.tm_mday;
|
2004-11-22 22:56:51 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_assert(0);
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2005-02-22 08:03:03 +01:00
|
|
|
tm.tm_hour = cfg_start_hour;
|
|
|
|
tm.tm_min = cfg_start_min;
|
|
|
|
tm.tm_sec = 0;
|
|
|
|
tm.tm_isdst = -1; /* Autodetect DST */
|
|
|
|
return mktime(&tm);
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
/** Return the start of the accounting period containing the time
|
|
|
|
* <b>now</b>. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static time_t
|
|
|
|
start_of_accounting_period_containing(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2004-11-22 22:56:51 +01:00
|
|
|
return edge_of_accounting_period_containing(now, 0);
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
2004-11-08 00:14:47 +01:00
|
|
|
|
|
|
|
/** Return the start of the accounting period that comes after the one
|
|
|
|
* containing the time <b>now</b>. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static time_t
|
|
|
|
start_of_accounting_period_after(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2004-11-22 22:56:51 +01:00
|
|
|
return edge_of_accounting_period_containing(now, 1);
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2010-09-03 18:06:04 +02:00
|
|
|
/** Return the length of the accounting period containing the time
|
|
|
|
* <b>now</b>. */
|
|
|
|
static long
|
|
|
|
length_of_accounting_period_containing(time_t now)
|
|
|
|
{
|
|
|
|
return edge_of_accounting_period_containing(now, 1) -
|
|
|
|
edge_of_accounting_period_containing(now, 0);
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Initialize the accounting subsystem. */
|
2004-11-05 18:55:34 +01:00
|
|
|
void
|
|
|
|
configure_accounting(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2010-09-21 20:59:43 +02:00
|
|
|
time_t s_now;
|
2004-11-08 00:14:47 +01:00
|
|
|
/* Try to remember our recorded usage. */
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!interval_start_time)
|
2004-11-05 18:55:34 +01:00
|
|
|
read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
|
|
|
|
* reset below.*/
|
2010-09-21 20:59:43 +02:00
|
|
|
|
|
|
|
s_now = start_of_accounting_period_containing(now);
|
|
|
|
|
|
|
|
if (!interval_start_time) {
|
|
|
|
/* We didn't have recorded usage; Start a new interval. */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_ACCT, "Starting new accounting interval.");
|
2004-11-05 18:55:34 +01:00
|
|
|
reset_accounting(now);
|
2010-09-21 20:59:43 +02:00
|
|
|
} else if (s_now == interval_start_time) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_ACCT, "Continuing accounting interval.");
|
2004-11-04 23:33:06 +01:00
|
|
|
/* We are in the interval we thought we were in. Do nothing.*/
|
2004-11-10 05:19:53 +01:00
|
|
|
interval_end_time = start_of_accounting_period_after(interval_start_time);
|
2004-11-04 23:33:06 +01:00
|
|
|
} else {
|
2011-04-29 01:18:32 +02:00
|
|
|
long duration =
|
|
|
|
length_of_accounting_period_containing(interval_start_time);
|
2010-09-03 18:06:04 +02:00
|
|
|
double delta = ((double)(s_now - interval_start_time)) / duration;
|
|
|
|
if (-0.50 <= delta && delta <= 0.50) {
|
2010-09-21 20:59:43 +02:00
|
|
|
/* The start of the period is now a little later or earlier than we
|
|
|
|
* remembered. That's fine; we might lose some bytes we could otherwise
|
2016-12-10 01:13:11 +01:00
|
|
|
* have written, but better to err on the side of obeying accounting
|
|
|
|
* settings. */
|
2010-09-03 18:06:04 +02:00
|
|
|
log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
|
|
|
|
"that's fine.", delta*100);
|
2010-09-21 20:59:43 +02:00
|
|
|
interval_end_time = start_of_accounting_period_after(now);
|
|
|
|
} else if (delta >= 0.99) {
|
|
|
|
/* This is the regular time-moved-forward case; don't be too noisy
|
|
|
|
* about it or people will complain */
|
|
|
|
log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
|
|
|
|
reset_accounting(now);
|
2010-09-03 18:06:04 +02:00
|
|
|
} else {
|
|
|
|
log_warn(LD_ACCT,
|
|
|
|
"Mismatched accounting interval: moved by %.02f%%. "
|
|
|
|
"Starting a fresh one.", delta*100);
|
|
|
|
reset_accounting(now);
|
|
|
|
}
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
accounting_set_wakeup_time();
|
|
|
|
}
|
|
|
|
|
2014-09-23 14:34:22 +02:00
|
|
|
/** Return the relevant number of bytes sent/received this interval
|
|
|
|
* based on the set AccountingRule */
|
2016-01-04 05:02:44 +01:00
|
|
|
uint64_t
|
2014-09-23 14:34:22 +02:00
|
|
|
get_accounting_bytes(void)
|
|
|
|
{
|
2014-09-23 14:46:35 +02:00
|
|
|
if (get_options()->AccountingRule == ACCT_SUM)
|
2014-09-23 14:34:22 +02:00
|
|
|
return n_bytes_read_in_interval+n_bytes_written_in_interval;
|
2016-01-04 05:02:44 +01:00
|
|
|
else if (get_options()->AccountingRule == ACCT_IN)
|
|
|
|
return n_bytes_read_in_interval;
|
|
|
|
else if (get_options()->AccountingRule == ACCT_OUT)
|
|
|
|
return n_bytes_written_in_interval;
|
2014-09-23 14:46:35 +02:00
|
|
|
else
|
|
|
|
return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval);
|
2014-09-23 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Set expected_bandwidth_usage based on how much we sent/received
|
|
|
|
* per minute last interval (if we were up for at least 30 minutes),
|
|
|
|
* or based on our declared bandwidth otherwise. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static void
|
|
|
|
update_expected_bandwidth(void)
|
|
|
|
{
|
2010-09-15 21:41:32 +02:00
|
|
|
uint64_t expected;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options= get_options();
|
2010-09-21 20:02:04 +02:00
|
|
|
uint64_t max_configured = (options->RelayBandwidthRate > 0 ?
|
|
|
|
options->RelayBandwidthRate :
|
|
|
|
options->BandwidthRate) * 60;
|
2014-09-23 14:34:22 +02:00
|
|
|
/* max_configured is the larger of bytes read and bytes written
|
|
|
|
* If we are accounting based on sum, worst case is both are
|
|
|
|
* at max, doubling the expected sum of bandwidth */
|
2014-09-23 14:46:35 +02:00
|
|
|
if (get_options()->AccountingRule == ACCT_SUM)
|
2014-09-23 14:34:22 +02:00
|
|
|
max_configured *= 2;
|
2004-11-05 18:55:34 +01:00
|
|
|
|
2010-09-03 20:29:17 +02:00
|
|
|
#define MIN_TIME_FOR_MEASUREMENT (1800)
|
|
|
|
|
|
|
|
if (soft_limit_hit_at > interval_start_time && n_bytes_at_soft_limit &&
|
|
|
|
(soft_limit_hit_at - interval_start_time) > MIN_TIME_FOR_MEASUREMENT) {
|
|
|
|
/* If we hit our soft limit last time, only count the bytes up to that
|
|
|
|
* time. This is a better predictor of our actual bandwidth than
|
|
|
|
* considering the entirety of the last interval, since we likely started
|
|
|
|
* using bytes very slowly once we hit our soft limit. */
|
|
|
|
expected = n_bytes_at_soft_limit /
|
|
|
|
(soft_limit_hit_at - interval_start_time);
|
|
|
|
expected /= 60;
|
|
|
|
} else if (n_seconds_active_in_interval >= MIN_TIME_FOR_MEASUREMENT) {
|
|
|
|
/* Otherwise, we either measured enough time in the last interval but
|
|
|
|
* never hit our soft limit, or we're using a state file from a Tor that
|
2010-09-15 21:41:32 +02:00
|
|
|
* doesn't know to store soft-limit info. Just take rate at which
|
|
|
|
* we were reading/writing in the last interval as our expected rate.
|
2010-09-03 20:29:17 +02:00
|
|
|
*/
|
2014-09-23 14:34:22 +02:00
|
|
|
uint64_t used = get_accounting_bytes();
|
2010-09-03 20:29:17 +02:00
|
|
|
expected = used / (n_seconds_active_in_interval / 60);
|
|
|
|
} else {
|
2004-11-23 23:34:23 +01:00
|
|
|
/* If we haven't gotten enough data last interval, set 'expected'
|
|
|
|
* to 0. This will set our wakeup to the start of the interval.
|
|
|
|
* Next interval, we'll choose our starting time based on how much
|
|
|
|
* we sent this interval.
|
2004-11-16 04:32:01 +01:00
|
|
|
*/
|
2004-11-23 23:34:23 +01:00
|
|
|
expected = 0;
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
2010-09-03 20:29:17 +02:00
|
|
|
if (expected > max_configured)
|
|
|
|
expected = max_configured;
|
2006-12-17 17:37:46 +01:00
|
|
|
expected_bandwidth_usage = expected;
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Called at the start of a new accounting interval: reset our
|
|
|
|
* expected bandwidth usage based on what happened last time, set up
|
|
|
|
* the start and end of the interval, and clear byte/time totals.
|
|
|
|
*/
|
2004-11-05 18:55:34 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
reset_accounting(time_t now)
|
|
|
|
{
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_ACCT, "Starting new accounting interval.");
|
2004-11-05 18:55:34 +01:00
|
|
|
update_expected_bandwidth();
|
|
|
|
interval_start_time = start_of_accounting_period_containing(now);
|
|
|
|
interval_end_time = start_of_accounting_period_after(interval_start_time);
|
|
|
|
n_bytes_read_in_interval = 0;
|
|
|
|
n_bytes_written_in_interval = 0;
|
|
|
|
n_seconds_active_in_interval = 0;
|
2010-09-03 20:29:17 +02:00
|
|
|
n_bytes_at_soft_limit = 0;
|
|
|
|
soft_limit_hit_at = 0;
|
|
|
|
n_seconds_to_hit_soft_limit = 0;
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return true iff we should save our bandwidth usage to disk. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2004-11-08 00:14:47 +01:00
|
|
|
time_to_record_bandwidth_usage(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2006-12-07 21:11:30 +01:00
|
|
|
/* Note every 600 sec */
|
|
|
|
#define NOTE_INTERVAL (600)
|
2004-11-04 23:33:06 +01:00
|
|
|
/* Or every 20 megabytes */
|
|
|
|
#define NOTE_BYTES 20*(1024*1024)
|
|
|
|
static uint64_t last_read_bytes_noted = 0;
|
|
|
|
static uint64_t last_written_bytes_noted = 0;
|
|
|
|
static time_t last_time_noted = 0;
|
|
|
|
|
2004-11-05 18:55:34 +01:00
|
|
|
if (last_time_noted + NOTE_INTERVAL <= now ||
|
|
|
|
last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
|
|
|
|
last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
|
|
|
|
(interval_end_time && interval_end_time <= now)) {
|
2004-11-04 23:33:06 +01:00
|
|
|
last_time_noted = now;
|
|
|
|
last_read_bytes_noted = n_bytes_read_in_interval;
|
|
|
|
last_written_bytes_noted = n_bytes_written_in_interval;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Invoked once per second. Checks whether it is time to hibernate,
|
|
|
|
* record bandwidth used, etc. */
|
2004-11-05 18:55:34 +01:00
|
|
|
void
|
|
|
|
accounting_run_housekeeping(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
|
|
|
if (now >= interval_end_time) {
|
|
|
|
configure_accounting(now);
|
|
|
|
}
|
|
|
|
if (time_to_record_bandwidth_usage(now)) {
|
2006-12-07 19:57:37 +01:00
|
|
|
if (accounting_record_bandwidth_usage(now, get_or_state())) {
|
|
|
|
log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Based on our interval and our estimated bandwidth, choose a
|
|
|
|
* deterministic (but random-ish) time to wake up. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static void
|
|
|
|
accounting_set_wakeup_time(void)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN];
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *d_env;
|
2006-03-13 02:06:55 +01:00
|
|
|
uint64_t time_to_exhaust_bw;
|
2004-11-22 22:56:51 +01:00
|
|
|
int time_to_consider;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2010-10-04 07:38:53 +02:00
|
|
|
if (! server_identity_key_is_set()) {
|
2004-11-21 05:19:04 +01:00
|
|
|
if (init_keys() < 0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_err(LD_BUG, "Error initializing keys");
|
2004-11-21 05:19:04 +01:00
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 19:37:13 +01:00
|
|
|
if (server_identity_key_is_set()) {
|
Fix a bug in calculating wakeup time on 64-bit machines.
If you had TIME_MAX > INT_MAX, and your "time_to_exhaust_bw =
accountingmax/expected_bandwidth_usage * 60" calculation managed to
overflow INT_MAX, then your time_to_consider value could underflow and
wind up being rediculously low or high. "Low" was no problem;
negative values got caught by the (time_to_consider <= 0) check.
"High", however, would get you a wakeup time somewhere in the distant
future.
The fix is to check for time_to_exhaust_bw overflowing INT_MAX, not
TIME_MAX: We don't allow any accounting interval longer than a month,
so if time_to_exhaust_bw is significantly larger than 31*24*60*60, we
can just clip it.
This is a bugfix on 0.0.9pre6, when accounting was first introduced.
It fixes bug 2146, unless there are other causes there too. The fix
is from boboper. (I tweaked it slightly by removing an assignment
that boboper marked as dead, and lowering a variable that no longer
needed to be function-scoped.)
2010-12-06 18:01:32 +01:00
|
|
|
char buf[ISO_TIME_LEN+1];
|
2010-12-03 19:37:13 +01:00
|
|
|
format_iso_time(buf, interval_start_time);
|
|
|
|
|
2017-08-09 15:24:16 +02:00
|
|
|
if (crypto_pk_get_digest(get_server_identity_key(), digest) < 0) {
|
|
|
|
log_err(LD_BUG, "Error getting our key's digest.");
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
2010-12-03 19:37:13 +01:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
d_env = crypto_digest_new();
|
2010-12-03 19:37:13 +01:00
|
|
|
crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
|
|
|
|
crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
|
|
|
|
crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_free(d_env);
|
2010-12-03 19:37:13 +01:00
|
|
|
} else {
|
|
|
|
crypto_rand(digest, DIGEST_LEN);
|
|
|
|
}
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2004-11-23 23:34:23 +01:00
|
|
|
if (!expected_bandwidth_usage) {
|
|
|
|
char buf1[ISO_TIME_LEN+1];
|
|
|
|
char buf2[ISO_TIME_LEN+1];
|
|
|
|
format_local_iso_time(buf1, interval_start_time);
|
|
|
|
format_local_iso_time(buf2, interval_end_time);
|
|
|
|
interval_wakeup_time = interval_start_time;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
2011-01-25 16:45:27 +01:00
|
|
|
"Configured hibernation. This interval begins at %s "
|
|
|
|
"and ends at %s. We have no prior estimate for bandwidth, so "
|
2005-12-10 10:36:26 +01:00
|
|
|
"we will start out awake and hibernate when we exhaust our quota.",
|
|
|
|
buf1, buf2);
|
2004-11-23 23:34:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-13 02:06:55 +01:00
|
|
|
time_to_exhaust_bw =
|
|
|
|
(get_options()->AccountingMax/expected_bandwidth_usage)*60;
|
Fix a bug in calculating wakeup time on 64-bit machines.
If you had TIME_MAX > INT_MAX, and your "time_to_exhaust_bw =
accountingmax/expected_bandwidth_usage * 60" calculation managed to
overflow INT_MAX, then your time_to_consider value could underflow and
wind up being rediculously low or high. "Low" was no problem;
negative values got caught by the (time_to_consider <= 0) check.
"High", however, would get you a wakeup time somewhere in the distant
future.
The fix is to check for time_to_exhaust_bw overflowing INT_MAX, not
TIME_MAX: We don't allow any accounting interval longer than a month,
so if time_to_exhaust_bw is significantly larger than 31*24*60*60, we
can just clip it.
This is a bugfix on 0.0.9pre6, when accounting was first introduced.
It fixes bug 2146, unless there are other causes there too. The fix
is from boboper. (I tweaked it slightly by removing an assignment
that boboper marked as dead, and lowering a variable that no longer
needed to be function-scoped.)
2010-12-06 18:01:32 +01:00
|
|
|
if (time_to_exhaust_bw > INT_MAX) {
|
|
|
|
time_to_exhaust_bw = INT_MAX;
|
2006-03-13 02:06:55 +01:00
|
|
|
time_to_consider = 0;
|
|
|
|
} else {
|
2011-01-25 16:45:27 +01:00
|
|
|
time_to_consider = accounting_get_interval_length() -
|
|
|
|
(int)time_to_exhaust_bw;
|
2006-03-13 02:06:55 +01:00
|
|
|
}
|
2004-11-23 23:34:23 +01:00
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
if (time_to_consider<=0) {
|
|
|
|
interval_wakeup_time = interval_start_time;
|
|
|
|
} else {
|
|
|
|
/* XXX can we simplify this just by picking a random (non-deterministic)
|
|
|
|
* time to be up? If we go down and come up, then we pick a new one. Is
|
|
|
|
* that good enough? -RD */
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
/* This is not a perfectly unbiased conversion, but it is good enough:
|
|
|
|
* in the worst case, the first half of the day is 0.06 percent likelier
|
|
|
|
* to be chosen than the last half. */
|
|
|
|
interval_wakeup_time = interval_start_time +
|
|
|
|
(get_uint32(digest) % time_to_consider);
|
|
|
|
}
|
2004-11-23 23:34:23 +01:00
|
|
|
|
2004-11-22 22:56:51 +01:00
|
|
|
{
|
|
|
|
char buf1[ISO_TIME_LEN+1];
|
|
|
|
char buf2[ISO_TIME_LEN+1];
|
|
|
|
char buf3[ISO_TIME_LEN+1];
|
|
|
|
char buf4[ISO_TIME_LEN+1];
|
2006-03-13 02:06:55 +01:00
|
|
|
time_t down_time;
|
|
|
|
if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
|
|
|
|
down_time = TIME_MAX;
|
|
|
|
else
|
|
|
|
down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
|
2004-11-22 22:56:51 +01:00
|
|
|
if (down_time>interval_end_time)
|
|
|
|
down_time = interval_end_time;
|
|
|
|
format_local_iso_time(buf1, interval_start_time);
|
|
|
|
format_local_iso_time(buf2, interval_wakeup_time);
|
2006-03-13 02:06:55 +01:00
|
|
|
format_local_iso_time(buf3, down_time);
|
2004-11-22 22:56:51 +01:00
|
|
|
format_local_iso_time(buf4, interval_end_time);
|
|
|
|
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
|
|
|
"Configured hibernation. This interval began at %s; "
|
2004-11-23 23:34:23 +01:00
|
|
|
"the scheduled wake-up time %s %s; "
|
2004-12-05 13:26:02 +01:00
|
|
|
"we expect%s to exhaust our quota for this interval around %s; "
|
2004-11-23 23:34:23 +01:00
|
|
|
"the next interval begins at %s (all times local)",
|
|
|
|
buf1,
|
|
|
|
time(NULL)<interval_wakeup_time?"is":"was", buf2,
|
|
|
|
time(NULL)<down_time?"":"ed", buf3,
|
|
|
|
buf4);
|
2004-11-22 22:56:51 +01:00
|
|
|
}
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2006-12-10 09:04:50 +01:00
|
|
|
/* This rounds 0 up to 1000, but that's actually a feature. */
|
2006-12-07 21:11:36 +01:00
|
|
|
#define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Save all our bandwidth tracking information to disk. Return 0 on
|
2006-06-24 06:57:59 +02:00
|
|
|
* success, -1 on failure. */
|
2004-11-14 23:21:23 +01:00
|
|
|
int
|
2006-12-07 19:57:37 +01:00
|
|
|
accounting_record_bandwidth_usage(time_t now, or_state_t *state)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2007-10-14 02:13:04 +02:00
|
|
|
/* Just update the state */
|
2006-12-07 19:57:37 +01:00
|
|
|
state->AccountingIntervalStart = interval_start_time;
|
2006-12-07 21:11:36 +01:00
|
|
|
state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
|
|
|
|
state->AccountingBytesWrittenInInterval =
|
|
|
|
ROUND_UP(n_bytes_written_in_interval);
|
2006-12-07 19:57:37 +01:00
|
|
|
state->AccountingSecondsActive = n_seconds_active_in_interval;
|
|
|
|
state->AccountingExpectedUsage = expected_bandwidth_usage;
|
2006-12-24 03:45:46 +01:00
|
|
|
|
2010-09-03 20:29:17 +02:00
|
|
|
state->AccountingSecondsToReachSoftLimit = n_seconds_to_hit_soft_limit;
|
|
|
|
state->AccountingSoftLimitHitAt = soft_limit_hit_at;
|
|
|
|
state->AccountingBytesAtSoftLimit = n_bytes_at_soft_limit;
|
|
|
|
|
2006-12-24 03:45:46 +01:00
|
|
|
or_state_mark_dirty(state,
|
|
|
|
now+(get_options()->AvoidDiskWrites ? 7200 : 60));
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2007-10-14 02:13:04 +02:00
|
|
|
return 0;
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
2006-12-07 21:11:36 +01:00
|
|
|
#undef ROUND_UP
|
2004-10-31 21:29:25 +01:00
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Read stored accounting information from disk. Return 0 on success;
|
|
|
|
* return -1 and change nothing on failure. */
|
|
|
|
static int
|
|
|
|
read_bandwidth_usage(void)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2006-12-07 19:57:37 +01:00
|
|
|
or_state_t *state = get_or_state();
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2007-10-14 02:13:04 +02:00
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname = get_datadir_fname("bw_accounting");
|
2014-03-19 01:52:31 +01:00
|
|
|
int res;
|
|
|
|
|
|
|
|
res = unlink(fname);
|
2016-08-31 16:57:09 +02:00
|
|
|
if (res != 0 && errno != ENOENT) {
|
2014-03-19 01:52:31 +01:00
|
|
|
log_warn(LD_FS,
|
|
|
|
"Failed to unlink %s: %s",
|
|
|
|
fname, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2006-12-07 19:57:37 +01:00
|
|
|
}
|
|
|
|
|
2007-10-14 02:13:04 +02:00
|
|
|
if (!state)
|
2009-04-24 00:04:10 +02:00
|
|
|
return -1;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
log_info(LD_ACCT, "Reading bandwidth accounting data from state file");
|
2007-10-14 02:13:04 +02:00
|
|
|
n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
|
|
|
|
n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
|
|
|
|
n_seconds_active_in_interval = state->AccountingSecondsActive;
|
|
|
|
interval_start_time = state->AccountingIntervalStart;
|
|
|
|
expected_bandwidth_usage = state->AccountingExpectedUsage;
|
2007-08-18 20:20:42 +02:00
|
|
|
|
2010-09-21 20:02:04 +02:00
|
|
|
/* Older versions of Tor (before 0.2.2.17-alpha or so) didn't generate these
|
2010-09-03 20:29:17 +02:00
|
|
|
* fields. If you switch back and forth, you might get an
|
|
|
|
* AccountingSoftLimitHitAt value from long before the most recent
|
|
|
|
* interval_start_time. If that's so, then ignore the softlimit-related
|
|
|
|
* values. */
|
|
|
|
if (state->AccountingSoftLimitHitAt > interval_start_time) {
|
|
|
|
soft_limit_hit_at = state->AccountingSoftLimitHitAt;
|
|
|
|
n_bytes_at_soft_limit = state->AccountingBytesAtSoftLimit;
|
2010-09-15 21:13:17 +02:00
|
|
|
n_seconds_to_hit_soft_limit = state->AccountingSecondsToReachSoftLimit;
|
2010-09-03 20:29:17 +02:00
|
|
|
} else {
|
|
|
|
soft_limit_hit_at = 0;
|
|
|
|
n_bytes_at_soft_limit = 0;
|
|
|
|
n_seconds_to_hit_soft_limit = 0;
|
|
|
|
}
|
|
|
|
|
2007-10-14 02:13:04 +02:00
|
|
|
{
|
|
|
|
char tbuf1[ISO_TIME_LEN+1];
|
|
|
|
char tbuf2[ISO_TIME_LEN+1];
|
|
|
|
format_iso_time(tbuf1, state->LastWritten);
|
|
|
|
format_iso_time(tbuf2, state->AccountingIntervalStart);
|
2004-11-05 18:55:34 +01:00
|
|
|
|
2007-10-14 02:13:04 +02:00
|
|
|
log_info(LD_ACCT,
|
|
|
|
"Successfully read bandwidth accounting info from state written at %s "
|
2005-12-09 06:37:26 +01:00
|
|
|
"for interval starting at %s. We have been active for %lu seconds in "
|
|
|
|
"this interval. At the start of the interval, we expected to use "
|
|
|
|
"about %lu KB per second. ("U64_FORMAT" bytes read so far, "
|
|
|
|
U64_FORMAT" bytes written so far)",
|
2007-10-14 02:13:04 +02:00
|
|
|
tbuf1, tbuf2,
|
2005-12-10 10:36:26 +01:00
|
|
|
(unsigned long)n_seconds_active_in_interval,
|
2006-12-17 17:37:46 +01:00
|
|
|
(unsigned long)(expected_bandwidth_usage*1024/60),
|
2005-12-10 10:36:26 +01:00
|
|
|
U64_PRINTF_ARG(n_bytes_read_in_interval),
|
|
|
|
U64_PRINTF_ARG(n_bytes_written_in_interval));
|
2006-12-07 19:57:37 +01:00
|
|
|
}
|
2007-10-14 02:13:04 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return true iff we have sent/received all the bytes we are willing
|
|
|
|
* to send/receive this interval. */
|
|
|
|
static int
|
|
|
|
hibernate_hard_limit_reached(void)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2004-11-20 01:37:00 +01:00
|
|
|
uint64_t hard_limit = get_options()->AccountingMax;
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!hard_limit)
|
|
|
|
return 0;
|
2014-09-23 14:34:22 +02:00
|
|
|
return get_accounting_bytes() >= hard_limit;
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return true iff we have sent/received almost all the bytes we are willing
|
|
|
|
* to send/receive this interval. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
hibernate_soft_limit_reached(void)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2010-09-03 19:43:59 +02:00
|
|
|
const uint64_t acct_max = get_options()->AccountingMax;
|
|
|
|
#define SOFT_LIM_PCT (.95)
|
|
|
|
#define SOFT_LIM_BYTES (500*1024*1024)
|
|
|
|
#define SOFT_LIM_MINUTES (3*60)
|
|
|
|
/* The 'soft limit' is a fair bit more complicated now than once it was.
|
|
|
|
* We want to stop accepting connections when ALL of the following are true:
|
|
|
|
* - We expect to use up the remaining bytes in under 3 hours
|
|
|
|
* - We have used up 95% of our bytes.
|
|
|
|
* - We have less than 500MB of bytes left.
|
|
|
|
*/
|
|
|
|
uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(acct_max) * SOFT_LIM_PCT);
|
|
|
|
if (acct_max > SOFT_LIM_BYTES && acct_max - SOFT_LIM_BYTES > soft_limit) {
|
|
|
|
soft_limit = acct_max - SOFT_LIM_BYTES;
|
|
|
|
}
|
|
|
|
if (expected_bandwidth_usage) {
|
|
|
|
const uint64_t expected_usage =
|
|
|
|
expected_bandwidth_usage * SOFT_LIM_MINUTES;
|
|
|
|
if (acct_max > expected_usage && acct_max - expected_usage > soft_limit)
|
|
|
|
soft_limit = acct_max - expected_usage;
|
|
|
|
}
|
|
|
|
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!soft_limit)
|
|
|
|
return 0;
|
2014-09-23 14:34:22 +02:00
|
|
|
return get_accounting_bytes() >= soft_limit;
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Called when we get a SIGINT, or when bandwidth soft limit is
|
|
|
|
* reached. Puts us into "loose hibernation": we don't accept new
|
|
|
|
* connections, but we continue handling old ones. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2007-05-25 01:51:08 +02:00
|
|
|
hibernate_begin(hibernate_state_t new_state, time_t now)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2004-10-31 21:29:25 +01:00
|
|
|
|
2005-02-10 08:34:19 +01:00
|
|
|
if (new_state == HIBERNATE_STATE_EXITING &&
|
|
|
|
hibernate_state != HIBERNATE_STATE_LIVE) {
|
2009-05-27 23:55:51 +02:00
|
|
|
log_notice(LD_GENERAL,"SIGINT received %s; exiting now.",
|
2006-02-13 10:37:53 +01:00
|
|
|
hibernate_state == HIBERNATE_STATE_EXITING ?
|
|
|
|
"a second time" : "while hibernating");
|
2017-10-20 16:25:55 +02:00
|
|
|
tor_shutdown_event_loop_and_exit(0);
|
|
|
|
return;
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2010-09-03 20:29:17 +02:00
|
|
|
if (new_state == HIBERNATE_STATE_LOWBANDWIDTH &&
|
|
|
|
hibernate_state == HIBERNATE_STATE_LIVE) {
|
|
|
|
soft_limit_hit_at = now;
|
|
|
|
n_seconds_to_hit_soft_limit = n_seconds_active_in_interval;
|
2014-09-23 14:34:22 +02:00
|
|
|
n_bytes_at_soft_limit = get_accounting_bytes();
|
2010-09-03 20:29:17 +02:00
|
|
|
}
|
|
|
|
|
2004-11-09 03:12:41 +01:00
|
|
|
/* close listeners. leave control listener(s). */
|
2011-11-28 21:44:10 +01:00
|
|
|
connection_mark_all_noncontrol_listeners();
|
2004-10-31 21:29:25 +01:00
|
|
|
|
|
|
|
/* XXX kill intro point circs */
|
|
|
|
/* XXX upload rendezvous service descriptors with no intro points */
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (new_state == HIBERNATE_STATE_EXITING) {
|
2011-01-26 00:45:13 +01:00
|
|
|
log_notice(LD_GENERAL,"Interrupt: we have stopped accepting new "
|
|
|
|
"connections, and will shut down in %d seconds. Interrupt "
|
2006-02-13 10:37:53 +01:00
|
|
|
"again to exit now.", options->ShutdownWaitLength);
|
2007-02-07 07:54:33 +01:00
|
|
|
shutdown_time = time(NULL) + options->ShutdownWaitLength;
|
2004-10-31 21:29:25 +01:00
|
|
|
} else { /* soft limit reached */
|
2004-11-08 00:14:47 +01:00
|
|
|
hibernate_end_time = interval_end_time;
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hibernate_state = new_state;
|
2006-12-07 19:57:37 +01:00
|
|
|
accounting_record_bandwidth_usage(now, get_or_state());
|
2006-12-24 03:45:46 +01:00
|
|
|
|
|
|
|
or_state_mark_dirty(get_or_state(),
|
|
|
|
get_options()->AvoidDiskWrites ? now+600 : 0);
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when we've been hibernating and our timeout is reached. */
|
2004-11-08 00:14:47 +01:00
|
|
|
static void
|
2007-05-25 01:51:08 +02:00
|
|
|
hibernate_end(hibernate_state_t new_state)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-31 21:29:25 +01:00
|
|
|
tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
|
2011-09-01 05:21:50 +02:00
|
|
|
hibernate_state == HIBERNATE_STATE_DORMANT ||
|
|
|
|
hibernate_state == HIBERNATE_STATE_INITIAL);
|
2004-10-31 21:29:25 +01:00
|
|
|
|
|
|
|
/* listeners will be relaunched in run_scheduled_events() in main.c */
|
2011-09-01 05:21:50 +02:00
|
|
|
if (hibernate_state != HIBERNATE_STATE_INITIAL)
|
|
|
|
log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
|
2004-10-31 21:29:25 +01:00
|
|
|
|
|
|
|
hibernate_state = new_state;
|
2004-11-08 00:14:47 +01:00
|
|
|
hibernate_end_time = 0; /* no longer hibernating */
|
2018-01-31 02:36:38 +01:00
|
|
|
reset_uptime(); /* reset published uptime */
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A wrapper around hibernate_begin, for when we get SIGINT. */
|
2004-11-08 00:14:47 +01:00
|
|
|
void
|
2005-06-11 20:52:12 +02:00
|
|
|
hibernate_begin_shutdown(void)
|
|
|
|
{
|
2004-11-05 18:55:34 +01:00
|
|
|
hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return true iff we are currently hibernating. */
|
2014-04-15 14:20:34 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
we_are_hibernating,(void))
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-31 21:29:25 +01:00
|
|
|
return hibernate_state != HIBERNATE_STATE_LIVE;
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** If we aren't currently dormant, close all connections and become
|
|
|
|
* dormant. */
|
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
hibernate_go_dormant(time_t now)
|
|
|
|
{
|
2004-11-08 00:14:47 +01:00
|
|
|
connection_t *conn;
|
|
|
|
|
|
|
|
if (hibernate_state == HIBERNATE_STATE_DORMANT)
|
|
|
|
return;
|
2004-11-14 22:11:06 +01:00
|
|
|
else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
|
|
|
|
hibernate_state = HIBERNATE_STATE_DORMANT;
|
|
|
|
else
|
|
|
|
hibernate_begin(HIBERNATE_STATE_DORMANT, now);
|
2004-11-08 00:14:47 +01:00
|
|
|
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
|
2004-11-08 00:14:47 +01:00
|
|
|
|
2004-11-09 03:12:41 +01:00
|
|
|
/* Close all OR/AP/exit conns. Leave dir conns because we still want
|
2016-12-10 01:13:11 +01:00
|
|
|
* to be able to upload server descriptors so clients know we're still
|
2004-11-09 03:12:41 +01:00
|
|
|
* running, and download directories so we can detect if we're obsolete.
|
|
|
|
* Leave control conns because we still want to be controllable.
|
|
|
|
*/
|
2004-11-28 10:05:49 +01:00
|
|
|
while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
|
|
|
|
(conn = connection_get_by_type(CONN_TYPE_AP)) ||
|
|
|
|
(conn = connection_get_by_type(CONN_TYPE_EXIT))) {
|
2017-09-19 06:18:25 +02:00
|
|
|
if (CONN_IS_EDGE(conn)) {
|
2007-03-24 16:57:51 +01:00
|
|
|
connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
|
2017-09-19 06:18:25 +02:00
|
|
|
}
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_NET,"Closing conn type %d", conn->type);
|
2017-09-19 06:18:25 +02:00
|
|
|
if (conn->type == CONN_TYPE_AP) {
|
|
|
|
/* send socks failure if needed */
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
|
2006-07-26 21:07:26 +02:00
|
|
|
END_STREAM_REASON_HIBERNATING);
|
2017-09-19 06:18:25 +02:00
|
|
|
} else if (conn->type == CONN_TYPE_OR) {
|
2012-11-13 22:45:00 +01:00
|
|
|
if (TO_OR_CONN(conn)->chan) {
|
2017-09-19 06:17:35 +02:00
|
|
|
connection_or_close_normally(TO_OR_CONN(conn), 0);
|
2012-11-13 22:45:00 +01:00
|
|
|
} else {
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
2017-09-19 06:18:25 +02:00
|
|
|
} else {
|
2005-03-27 06:55:13 +02:00
|
|
|
connection_mark_for_close(conn);
|
2017-09-19 06:18:25 +02:00
|
|
|
}
|
2004-11-08 00:14:47 +01:00
|
|
|
}
|
2004-11-10 05:19:53 +01:00
|
|
|
|
2007-02-07 07:54:33 +01:00
|
|
|
if (now < interval_wakeup_time)
|
|
|
|
hibernate_end_time = interval_wakeup_time;
|
|
|
|
else
|
|
|
|
hibernate_end_time = interval_end_time;
|
|
|
|
|
2006-12-07 19:57:37 +01:00
|
|
|
accounting_record_bandwidth_usage(now, get_or_state());
|
2006-12-24 03:45:46 +01:00
|
|
|
|
|
|
|
or_state_mark_dirty(get_or_state(),
|
|
|
|
get_options()->AvoidDiskWrites ? now+600 : 0);
|
2004-11-08 00:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when hibernate_end_time has arrived. */
|
|
|
|
static void
|
|
|
|
hibernate_end_time_elapsed(time_t now)
|
|
|
|
{
|
2004-11-14 22:11:06 +01:00
|
|
|
char buf[ISO_TIME_LEN+1];
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/* The interval has ended, or it is wakeup time. Find out which. */
|
|
|
|
accounting_run_housekeeping(now);
|
|
|
|
if (interval_wakeup_time <= now) {
|
|
|
|
/* The interval hasn't changed, but interval_wakeup_time has passed.
|
|
|
|
* It's time to wake up and start being a server. */
|
|
|
|
hibernate_end(HIBERNATE_STATE_LIVE);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* The interval has changed, and it isn't time to wake up yet. */
|
|
|
|
hibernate_end_time = interval_wakeup_time;
|
2004-11-14 22:11:06 +01:00
|
|
|
format_iso_time(buf,interval_wakeup_time);
|
|
|
|
if (hibernate_state != HIBERNATE_STATE_DORMANT) {
|
2004-11-08 00:14:47 +01:00
|
|
|
/* We weren't sleeping before; we should sleep now. */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
|
|
|
"Accounting period ended. Commencing hibernation until "
|
2012-11-23 16:05:16 +01:00
|
|
|
"%s UTC", buf);
|
2004-11-14 22:11:06 +01:00
|
|
|
hibernate_go_dormant(now);
|
|
|
|
} else {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
|
|
|
"Accounting period ended. This period, we will hibernate"
|
2012-11-23 16:05:16 +01:00
|
|
|
" until %s UTC",buf);
|
2004-11-14 22:11:06 +01:00
|
|
|
}
|
2004-11-08 00:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-13 01:44:39 +01:00
|
|
|
/** Consider our environment and decide if it's time
|
2004-11-08 00:14:47 +01:00
|
|
|
* to start/stop hibernating.
|
2004-10-31 21:29:25 +01:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
consider_hibernation(time_t now)
|
|
|
|
{
|
2004-11-20 01:37:00 +01:00
|
|
|
int accounting_enabled = get_options()->AccountingMax != 0;
|
2004-11-14 22:11:06 +01:00
|
|
|
char buf[ISO_TIME_LEN+1];
|
2016-03-30 22:19:11 +02:00
|
|
|
hibernate_state_t prev_state = hibernate_state;
|
2004-10-31 21:29:25 +01:00
|
|
|
|
2004-11-09 03:12:41 +01:00
|
|
|
/* If we're in 'exiting' mode, then we just shut down after the interval
|
2004-11-08 00:14:47 +01:00
|
|
|
* elapses. */
|
2004-10-31 21:29:25 +01:00
|
|
|
if (hibernate_state == HIBERNATE_STATE_EXITING) {
|
2007-02-07 07:54:33 +01:00
|
|
|
tor_assert(shutdown_time);
|
|
|
|
if (shutdown_time <= now) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
|
2017-10-20 16:25:55 +02:00
|
|
|
tor_shutdown_event_loop_and_exit(0);
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
return; /* if exiting soon, don't worry about bandwidth limits */
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (hibernate_state == HIBERNATE_STATE_DORMANT) {
|
2004-11-05 18:55:34 +01:00
|
|
|
/* We've been hibernating because of bandwidth accounting. */
|
2004-11-08 00:14:47 +01:00
|
|
|
tor_assert(hibernate_end_time);
|
2004-11-14 22:11:06 +01:00
|
|
|
if (hibernate_end_time > now && accounting_enabled) {
|
2004-11-05 18:55:34 +01:00
|
|
|
/* If we're hibernating, don't wake up until it's time, regardless of
|
2004-11-08 00:14:47 +01:00
|
|
|
* whether we're in a new interval. */
|
2004-11-05 18:55:34 +01:00
|
|
|
return ;
|
|
|
|
} else {
|
2004-11-08 00:14:47 +01:00
|
|
|
hibernate_end_time_elapsed(now);
|
2004-11-05 18:55:34 +01:00
|
|
|
}
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/* Else, we aren't hibernating. See if it's time to start hibernating, or to
|
|
|
|
* go dormant. */
|
2011-09-01 05:21:50 +02:00
|
|
|
if (hibernate_state == HIBERNATE_STATE_LIVE ||
|
|
|
|
hibernate_state == HIBERNATE_STATE_INITIAL) {
|
2004-11-14 22:11:06 +01:00
|
|
|
if (hibernate_soft_limit_reached()) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
2011-01-26 00:45:13 +01:00
|
|
|
"Bandwidth soft limit reached; commencing hibernation. "
|
2011-10-28 11:19:16 +02:00
|
|
|
"No new connections will be accepted");
|
2004-11-14 22:11:06 +01:00
|
|
|
hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
|
|
|
|
} else if (accounting_enabled && now < interval_wakeup_time) {
|
2006-04-24 01:05:34 +02:00
|
|
|
format_local_iso_time(buf,interval_wakeup_time);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_ACCT,
|
2006-04-24 01:05:34 +02:00
|
|
|
"Commencing hibernation. We will wake up at %s local time.",
|
|
|
|
buf);
|
2004-11-14 22:11:06 +01:00
|
|
|
hibernate_go_dormant(now);
|
2011-09-01 05:21:50 +02:00
|
|
|
} else if (hibernate_state == HIBERNATE_STATE_INITIAL) {
|
|
|
|
hibernate_end(HIBERNATE_STATE_LIVE);
|
2004-11-14 22:11:06 +01:00
|
|
|
}
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
|
2004-11-14 22:11:06 +01:00
|
|
|
if (!accounting_enabled) {
|
|
|
|
hibernate_end_time_elapsed(now);
|
|
|
|
} else if (hibernate_hard_limit_reached()) {
|
|
|
|
hibernate_go_dormant(now);
|
2004-11-08 00:14:47 +01:00
|
|
|
} else if (hibernate_end_time <= now) {
|
|
|
|
/* The hibernation period ended while we were still in lowbandwidth.*/
|
|
|
|
hibernate_end_time_elapsed(now);
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 22:19:11 +02:00
|
|
|
|
|
|
|
/* Dispatch a controller event if the hibernation state changed. */
|
|
|
|
if (hibernate_state != prev_state)
|
|
|
|
on_hibernate_state_change(prev_state);
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Helper function: called when we get a GETINFO request for an
|
|
|
|
* accounting-related key on the control connection <b>conn</b>. If we can
|
|
|
|
* answer the request for <b>question</b>, then set *<b>answer</b> to a newly
|
|
|
|
* allocated string holding the result. Otherwise, set *<b>answer</b> to
|
|
|
|
* NULL. */
|
2005-07-22 16:55:09 +02:00
|
|
|
int
|
2006-12-08 05:39:13 +01:00
|
|
|
getinfo_helper_accounting(control_connection_t *conn,
|
2010-07-18 17:05:58 +02:00
|
|
|
const char *question, char **answer,
|
|
|
|
const char **errmsg)
|
2005-07-22 16:55:09 +02:00
|
|
|
{
|
2006-12-08 05:39:13 +01:00
|
|
|
(void) conn;
|
2010-07-18 17:05:58 +02:00
|
|
|
(void) errmsg;
|
2005-07-22 16:55:09 +02:00
|
|
|
if (!strcmp(question, "accounting/enabled")) {
|
2006-12-16 12:34:12 +01:00
|
|
|
*answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
|
2005-07-22 16:55:09 +02:00
|
|
|
} else if (!strcmp(question, "accounting/hibernating")) {
|
2016-03-30 22:19:11 +02:00
|
|
|
*answer = tor_strdup(hibernate_state_to_string(hibernate_state));
|
|
|
|
tor_strlower(*answer);
|
2005-07-22 16:55:09 +02:00
|
|
|
} else if (!strcmp(question, "accounting/bytes")) {
|
2016-01-04 05:02:44 +01:00
|
|
|
tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
|
2005-07-22 16:55:09 +02:00
|
|
|
U64_PRINTF_ARG(n_bytes_read_in_interval),
|
|
|
|
U64_PRINTF_ARG(n_bytes_written_in_interval));
|
|
|
|
} else if (!strcmp(question, "accounting/bytes-left")) {
|
|
|
|
uint64_t limit = get_options()->AccountingMax;
|
2014-09-23 14:46:35 +02:00
|
|
|
if (get_options()->AccountingRule == ACCT_SUM) {
|
2014-09-23 14:34:22 +02:00
|
|
|
uint64_t total_left = 0;
|
|
|
|
uint64_t total_bytes = get_accounting_bytes();
|
|
|
|
if (total_bytes < limit)
|
|
|
|
total_left = limit - total_bytes;
|
|
|
|
tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
|
|
|
|
U64_PRINTF_ARG(total_left), U64_PRINTF_ARG(total_left));
|
2016-01-04 05:02:44 +01:00
|
|
|
} else if (get_options()->AccountingRule == ACCT_IN) {
|
|
|
|
uint64_t read_left = 0;
|
|
|
|
if (n_bytes_read_in_interval < limit)
|
|
|
|
read_left = limit - n_bytes_read_in_interval;
|
|
|
|
tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
|
|
|
|
U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(limit));
|
|
|
|
} else if (get_options()->AccountingRule == ACCT_OUT) {
|
|
|
|
uint64_t write_left = 0;
|
|
|
|
if (n_bytes_written_in_interval < limit)
|
|
|
|
write_left = limit - n_bytes_written_in_interval;
|
|
|
|
tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
|
|
|
|
U64_PRINTF_ARG(limit), U64_PRINTF_ARG(write_left));
|
2014-09-23 14:34:22 +02:00
|
|
|
} else {
|
|
|
|
uint64_t read_left = 0, write_left = 0;
|
|
|
|
if (n_bytes_read_in_interval < limit)
|
|
|
|
read_left = limit - n_bytes_read_in_interval;
|
|
|
|
if (n_bytes_written_in_interval < limit)
|
|
|
|
write_left = limit - n_bytes_written_in_interval;
|
|
|
|
tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
|
|
|
|
U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
|
|
|
|
}
|
2005-07-22 16:55:09 +02:00
|
|
|
} else if (!strcmp(question, "accounting/interval-start")) {
|
|
|
|
*answer = tor_malloc(ISO_TIME_LEN+1);
|
|
|
|
format_iso_time(*answer, interval_start_time);
|
|
|
|
} else if (!strcmp(question, "accounting/interval-wake")) {
|
|
|
|
*answer = tor_malloc(ISO_TIME_LEN+1);
|
|
|
|
format_iso_time(*answer, interval_wakeup_time);
|
|
|
|
} else if (!strcmp(question, "accounting/interval-end")) {
|
|
|
|
*answer = tor_malloc(ISO_TIME_LEN+1);
|
|
|
|
format_iso_time(*answer, interval_end_time);
|
|
|
|
} else {
|
|
|
|
*answer = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-22 23:12:10 +02:00
|
|
|
|
2016-03-30 22:19:11 +02:00
|
|
|
/**
|
|
|
|
* Helper function: called when the hibernation state changes, and sends a
|
|
|
|
* SERVER_STATUS event to notify interested controllers of the accounting
|
|
|
|
* state change.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
on_hibernate_state_change(hibernate_state_t prev_state)
|
|
|
|
{
|
|
|
|
(void)prev_state; /* Should we do something with this? */
|
|
|
|
control_event_server_status(LOG_NOTICE,
|
|
|
|
"HIBERNATION_STATUS STATUS=%s",
|
|
|
|
hibernate_state_to_string(hibernate_state));
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:58:28 +02:00
|
|
|
#ifdef TOR_UNIT_TESTS
|
2011-09-07 23:48:21 +02:00
|
|
|
/**
|
|
|
|
* Manually change the hibernation state. Private; used only by the unit
|
|
|
|
* tests.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
hibernate_set_state_for_testing_(hibernate_state_t newstate)
|
|
|
|
{
|
|
|
|
hibernate_state = newstate;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(TOR_UNIT_TESTS) */
|
2011-10-11 17:30:01 +02:00
|
|
|
|