2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2004 Roger Dingledine, Nick Mathewson. */
|
2004-10-31 21:29:25 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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.
|
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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
|
|
|
#define HIBERNATE_STATE_LIVE 1
|
|
|
|
#define HIBERNATE_STATE_EXITING 2
|
|
|
|
#define HIBERNATE_STATE_LOWBANDWIDTH 3
|
|
|
|
#define HIBERNATE_STATE_DORMANT 4
|
|
|
|
|
|
|
|
#define SHUTDOWN_WAIT_LENGTH 30 /* seconds */
|
|
|
|
|
2004-11-04 23:33:06 +01:00
|
|
|
static int hibernate_state = HIBERNATE_STATE_LIVE;
|
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;
|
|
|
|
|
2004-11-12 17:39:03 +01:00
|
|
|
typedef enum {
|
|
|
|
UNIT_MONTH, UNIT_WEEK, UNIT_DAY,
|
|
|
|
} time_unit_t;
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/* Fields for accounting logic. Accounting overview:
|
|
|
|
*
|
2004-11-09 03:12:41 +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,
|
|
|
|
* starting at 0:00 GMT an arbitrary day within the month). 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:
|
|
|
|
*
|
|
|
|
* 1. We guess our bandwidth usage, based on how much we used
|
|
|
|
* last time. We choose a "wakeup time" within the interval to come up.
|
|
|
|
* 2. Until the chosen wakeup time, we hibernate.
|
|
|
|
* 3. We come up at the wakeup time, and provide bandwidth until we are
|
|
|
|
* "very close" to running out.
|
|
|
|
* 4. Then we go into low-bandwidth mode, and stop accepting new
|
|
|
|
* connections, but provide bandwidth until we run out.
|
|
|
|
* 5. Then we hibernate until the end of the interval.
|
|
|
|
*
|
|
|
|
* If the interval ends before we run out of bandwdith, we go back to
|
|
|
|
* step one.
|
|
|
|
*/
|
2004-11-04 23:33:06 +01:00
|
|
|
|
|
|
|
/** How many bytes have we read/written in this accounting interval? */
|
|
|
|
static uint64_t n_bytes_read_in_interval = 0;
|
|
|
|
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;
|
|
|
|
/** 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-05 18:55:34 +01:00
|
|
|
/** How much bandwidth do we 'expect' to use per minute? */
|
|
|
|
static uint32_t expected_bandwidth_usage = 0;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* ************
|
|
|
|
* Functions for bandwidth accounting.
|
|
|
|
* ************/
|
|
|
|
|
2004-11-15 05:01:31 +01:00
|
|
|
/** If we want to manage the accounting system and potentially
|
|
|
|
* hibernate, return 1, else return 0.
|
|
|
|
*/
|
|
|
|
int accounting_is_enabled(or_options_t *options) {
|
2004-11-20 01:37:00 +01:00
|
|
|
if (options->AccountingMax)
|
2004-11-15 05:01:31 +01:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-08 00:14:47 +01:00
|
|
|
/** Increment the month field of <b>tm</b> by <b>delta</b> months. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static INLINE void
|
|
|
|
incr_month(struct tm *tm, unsigned int delta)
|
|
|
|
{
|
|
|
|
tm->tm_mon += delta;
|
2004-11-08 00:14:47 +01:00
|
|
|
/* officially, we don't have to do this, but some platforms are rumored
|
|
|
|
* to have broken implementations. */
|
2004-11-05 18:55:34 +01:00
|
|
|
while (tm->tm_mon > 11) {
|
|
|
|
++tm->tm_year;
|
|
|
|
tm->tm_mon -= 12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Decrement the month field of <b>tm</b> by <b>delta</b> months. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static INLINE void
|
|
|
|
decr_month(struct tm *tm, unsigned int delta)
|
|
|
|
{
|
|
|
|
tm->tm_mon -= delta;
|
|
|
|
while (tm->tm_mon < 0) {
|
|
|
|
--tm->tm_year;
|
|
|
|
tm->tm_mon += 12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return the start of the accounting period that contains 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
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
/* Only months are supported. */
|
|
|
|
tm = gmtime(&now);
|
|
|
|
/* If this is before the Nth, we want the Nth of last month. */
|
2004-11-06 06:18:11 +01:00
|
|
|
if (tm->tm_mday < get_options()->AccountingStart) {
|
2004-11-05 18:55:34 +01:00
|
|
|
decr_month(tm, 1);
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
/* Otherwise, the month and year are correct.*/
|
|
|
|
|
2004-11-06 06:18:11 +01:00
|
|
|
tm->tm_mday = get_options()->AccountingStart;
|
2004-11-04 23:33:06 +01:00
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_min = 0;
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
return tor_timegm(tm);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
time_t start;
|
|
|
|
struct tm *tm;
|
|
|
|
start = start_of_accounting_period_containing(now);
|
|
|
|
|
|
|
|
tm = gmtime(&start);
|
2004-11-05 18:55:34 +01:00
|
|
|
incr_month(tm, 1);
|
2004-11-04 23:33:06 +01:00
|
|
|
return tor_timegm(tm);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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.*/
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!interval_start_time ||
|
|
|
|
start_of_accounting_period_after(interval_start_time) <= now) {
|
2004-11-08 00:14:47 +01:00
|
|
|
/* We didn't have recorded usage, or we don't have recorded usage
|
|
|
|
* for this interval. Start a new interval. */
|
2004-11-04 23:33:06 +01:00
|
|
|
log_fn(LOG_INFO, "Starting new accounting interval.");
|
2004-11-05 18:55:34 +01:00
|
|
|
reset_accounting(now);
|
2004-11-04 23:33:06 +01:00
|
|
|
} if (interval_start_time ==
|
|
|
|
start_of_accounting_period_containing(interval_start_time)) {
|
|
|
|
log_fn(LOG_INFO, "Continuing accounting interval.");
|
|
|
|
/* 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 {
|
2004-11-05 18:55:34 +01:00
|
|
|
log_fn(LOG_WARN, "Mismatched accounting interval; starting a fresh one.");
|
|
|
|
reset_accounting(now);
|
2004-11-04 23:33:06 +01:00
|
|
|
}
|
|
|
|
accounting_set_wakeup_time();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
uint64_t used;
|
2004-11-20 01:37:00 +01:00
|
|
|
uint32_t max_configured = (get_options()->BandwidthRate * 60);
|
2004-11-05 18:55:34 +01:00
|
|
|
|
|
|
|
if (n_seconds_active_in_interval < 1800) {
|
2004-11-16 04:32:01 +01:00
|
|
|
/* If we haven't gotten enough data last interval, guess that
|
|
|
|
* we'll be used at our maximum capacity. This is unlikely to be
|
|
|
|
* so, but it will give us an okay first estimate, and we'll stay
|
2004-11-20 08:33:55 +01:00
|
|
|
* up until we send Max bytes. Next interval, we'll choose
|
2004-11-16 04:32:01 +01:00
|
|
|
* our starting time based on how much we sent this interval.
|
|
|
|
*/
|
2004-11-05 18:55:34 +01:00
|
|
|
expected_bandwidth_usage = max_configured;
|
|
|
|
} else {
|
|
|
|
used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
|
|
|
|
n_bytes_read_in_interval : n_bytes_written_in_interval;
|
|
|
|
expected_bandwidth_usage = (uint32_t)
|
|
|
|
(used / (n_seconds_active_in_interval / 60));
|
|
|
|
if (expected_bandwidth_usage > max_configured)
|
|
|
|
expected_bandwidth_usage = max_configured;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
reset_accounting(time_t now) {
|
|
|
|
log_fn(LOG_INFO, "Starting new accounting interval.");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Return true iff we should save our bandwidth usage to disk. */
|
|
|
|
static INLINE int
|
|
|
|
time_to_record_bandwidth_usage(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
2004-11-10 05:19:53 +01:00
|
|
|
/* Note every 60 sec */
|
|
|
|
#define NOTE_INTERVAL (60)
|
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;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2004-11-14 23:53:51 +01:00
|
|
|
if (accounting_record_bandwidth_usage(now)) {
|
2004-11-05 18:55:34 +01:00
|
|
|
log_fn(LOG_ERR, "Couldn't record bandwidth usage; exiting.");
|
|
|
|
exit(1);
|
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
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
char buf[ISO_TIME_LEN+1];
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
crypto_digest_env_t *d;
|
|
|
|
int n_days_in_interval;
|
2004-11-05 18:55:34 +01:00
|
|
|
int n_days_to_exhaust_bw;
|
|
|
|
int n_days_to_consider;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
2004-11-21 05:19:04 +01:00
|
|
|
if (! identity_key_is_set()) {
|
|
|
|
if (init_keys() < 0) {
|
|
|
|
log_fn(LOG_ERR, "Error initializing keys");
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-04 23:33:06 +01:00
|
|
|
format_iso_time(buf, interval_start_time);
|
|
|
|
crypto_pk_get_digest(get_identity_key(), digest);
|
|
|
|
|
|
|
|
d = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(d, buf, ISO_TIME_LEN);
|
|
|
|
crypto_digest_add_bytes(d, digest, DIGEST_LEN);
|
|
|
|
crypto_digest_get_digest(d, digest, DIGEST_LEN);
|
|
|
|
crypto_free_digest_env(d);
|
|
|
|
|
2004-11-10 20:32:44 +01:00
|
|
|
if (expected_bandwidth_usage)
|
|
|
|
n_days_to_exhaust_bw =
|
2004-11-20 01:37:00 +01:00
|
|
|
(get_options()->AccountingMax/expected_bandwidth_usage)/(24*60);
|
2004-11-10 20:32:44 +01:00
|
|
|
else
|
|
|
|
n_days_to_exhaust_bw = 1;
|
2004-11-05 18:55:34 +01:00
|
|
|
|
2004-11-04 23:33:06 +01:00
|
|
|
tm = gmtime(&interval_start_time);
|
|
|
|
if (++tm->tm_mon > 11) { tm->tm_mon = 0; ++tm->tm_year; }
|
|
|
|
n_days_in_interval = (tor_timegm(tm)-interval_start_time+1)/(24*60*60);
|
|
|
|
|
2004-11-05 18:55:34 +01:00
|
|
|
n_days_to_consider = n_days_in_interval - n_days_to_exhaust_bw;
|
|
|
|
|
2004-11-06 07:56:28 +01:00
|
|
|
/* 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-05 18:55:34 +01:00
|
|
|
while (((unsigned char)digest[0]) > n_days_to_consider)
|
2004-11-04 23:33:06 +01:00
|
|
|
crypto_digest(digest, digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
interval_wakeup_time = interval_start_time +
|
|
|
|
24*60*60 * (unsigned char)digest[0];
|
2004-11-14 22:11:06 +01:00
|
|
|
|
|
|
|
format_iso_time(buf, interval_wakeup_time);
|
|
|
|
log_fn(LOG_INFO, "Configured hibernation interval: Decided to wake up %d days into the interval, at %s GMT",
|
|
|
|
(int)(unsigned char)digest[0], buf);
|
2004-10-31 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2004-11-05 18:55:34 +01:00
|
|
|
#define BW_ACCOUNTING_VERSION 1
|
2004-11-08 00:14:47 +01:00
|
|
|
/** Save all our bandwidth tracking information to disk. Return 0 on
|
|
|
|
* success, -1 on failure*/
|
2004-11-14 23:21:23 +01:00
|
|
|
int
|
|
|
|
accounting_record_bandwidth_usage(time_t now)
|
2004-11-04 23:33:06 +01:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char fname[512];
|
2004-11-05 18:55:34 +01:00
|
|
|
char time1[ISO_TIME_LEN+1];
|
|
|
|
char time2[ISO_TIME_LEN+1];
|
2004-11-04 23:33:06 +01:00
|
|
|
char *cp = buf;
|
2004-11-05 18:55:34 +01:00
|
|
|
/* Format is:
|
|
|
|
Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
|
|
|
|
|
|
|
|
format_iso_time(time1, interval_start_time);
|
|
|
|
format_iso_time(time2, now);
|
|
|
|
tor_snprintf(cp, sizeof(buf),
|
|
|
|
"%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
|
|
|
|
BW_ACCOUNTING_VERSION,
|
|
|
|
time1,
|
|
|
|
time2,
|
2004-11-04 23:33:06 +01:00
|
|
|
U64_PRINTF_ARG(n_bytes_read_in_interval),
|
|
|
|
U64_PRINTF_ARG(n_bytes_written_in_interval),
|
2004-11-05 18:55:34 +01:00
|
|
|
(unsigned long)n_seconds_active_in_interval,
|
|
|
|
(unsigned long)expected_bandwidth_usage);
|
2004-11-04 23:33:06 +01:00
|
|
|
tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
|
2004-11-09 08:05:53 +01:00
|
|
|
get_options()->DataDirectory);
|
2004-11-04 23:33:06 +01:00
|
|
|
|
|
|
|
return write_str_to_file(fname, buf, 0);
|
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
|
|
|
{
|
|
|
|
char *s = NULL;
|
|
|
|
char fname[512];
|
2004-11-05 18:55:34 +01:00
|
|
|
time_t t1, t2;
|
|
|
|
uint64_t n_read, n_written;
|
|
|
|
uint32_t expected_bw, n_seconds;
|
|
|
|
smartlist_t *elts;
|
|
|
|
int ok;
|
2004-11-04 23:33:06 +01:00
|
|
|
|
|
|
|
tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
|
2004-11-09 08:05:53 +01:00
|
|
|
get_options()->DataDirectory);
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!(s = read_file_to_str(fname, 0))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
elts = smartlist_create();
|
2004-11-10 05:19:53 +01:00
|
|
|
smartlist_split_string(elts, s, "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
|
2004-11-05 18:55:34 +01:00
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
if (smartlist_len(elts)<1 ||
|
|
|
|
atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
|
|
|
|
log_fn(LOG_WARN, "Unrecognized bw_accounting file version: %s",
|
|
|
|
(const char*)smartlist_get(elts,0));
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
if (smartlist_len(elts) < 7) {
|
|
|
|
log_fn(LOG_WARN, "Corrupted bw_accounting file: %d lines",
|
|
|
|
smartlist_len(elts));
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
if (parse_iso_time(smartlist_get(elts,1), &t1)) {
|
2004-11-04 23:33:06 +01:00
|
|
|
log_fn(LOG_WARN, "Error parsing bandwidth usage start time.");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
if (parse_iso_time(smartlist_get(elts,2), &t2)) {
|
|
|
|
log_fn(LOG_WARN, "Error parsing bandwidth usage last-written time");
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
|
|
|
|
&ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_fn(LOG_WARN, "Error parsing number of bytes read");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
|
|
|
|
&ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_fn(LOG_WARN, "Error parsing number of bytes read");
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
|
|
|
|
&ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_fn(LOG_WARN, "Error parsing number of seconds live");
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-05 18:55:34 +01:00
|
|
|
expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
|
|
|
|
&ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_fn(LOG_WARN, "Error parsing expected bandwidth");
|
2004-11-04 23:33:06 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2004-11-05 18:55:34 +01:00
|
|
|
n_bytes_read_in_interval = n_read;
|
|
|
|
n_bytes_written_in_interval = n_written;
|
|
|
|
n_seconds_active_in_interval = n_seconds;
|
|
|
|
interval_start_time = t1;
|
|
|
|
expected_bandwidth_usage = expected_bw;
|
|
|
|
|
2004-11-04 23:33:06 +01:00
|
|
|
accounting_set_wakeup_time();
|
|
|
|
return 0;
|
|
|
|
err:
|
2004-11-05 18:55:34 +01:00
|
|
|
SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elts);
|
2004-11-04 23:33:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return n_bytes_read_in_interval >= hard_limit
|
|
|
|
|| n_bytes_written_in_interval >= hard_limit;
|
|
|
|
}
|
|
|
|
|
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. */
|
2004-11-04 23:33:06 +01:00
|
|
|
static int hibernate_soft_limit_reached(void)
|
|
|
|
{
|
2004-11-20 01:37:00 +01:00
|
|
|
uint64_t soft_limit = (uint64_t) ((get_options()->AccountingMax) * .99);
|
2004-11-04 23:33:06 +01:00
|
|
|
if (!soft_limit)
|
|
|
|
return 0;
|
|
|
|
return n_bytes_read_in_interval >= soft_limit
|
|
|
|
|| n_bytes_written_in_interval >= soft_limit;
|
|
|
|
}
|
|
|
|
|
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. */
|
2004-11-05 18:55:34 +01:00
|
|
|
static void hibernate_begin(int new_state, time_t now) {
|
2004-10-31 21:29:25 +01:00
|
|
|
connection_t *conn;
|
|
|
|
|
|
|
|
if(hibernate_state == HIBERNATE_STATE_EXITING) {
|
|
|
|
/* we've been called twice now. close immediately. */
|
|
|
|
log(LOG_NOTICE,"Second sigint received; exiting now.");
|
|
|
|
tor_cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2004-11-09 03:12:41 +01:00
|
|
|
/* close listeners. leave control listener(s). */
|
2004-10-31 21:29:25 +01:00
|
|
|
while((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
|
|
|
|
(conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
|
|
|
|
(conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
|
|
|
|
log_fn(LOG_INFO,"Closing listener type %d", conn->type);
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX kill intro point circs */
|
|
|
|
/* XXX upload rendezvous service descriptors with no intro points */
|
|
|
|
|
|
|
|
if(new_state == HIBERNATE_STATE_EXITING) {
|
|
|
|
log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH);
|
2004-11-08 00:14:47 +01:00
|
|
|
hibernate_end_time = time(NULL) + SHUTDOWN_WAIT_LENGTH;
|
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;
|
2004-11-14 23:21:23 +01:00
|
|
|
accounting_record_bandwidth_usage(now);
|
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
|
|
|
|
hibernate_end(int new_state) {
|
2004-10-31 21:29:25 +01:00
|
|
|
|
|
|
|
tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
|
|
|
|
hibernate_state == HIBERNATE_STATE_DORMANT);
|
|
|
|
|
|
|
|
/* listeners will be relaunched in run_scheduled_events() in main.c */
|
|
|
|
log_fn(LOG_NOTICE,"Hibernation period ended. Resuming normal activity.");
|
|
|
|
|
|
|
|
hibernate_state = new_state;
|
2004-11-08 00:14:47 +01:00
|
|
|
hibernate_end_time = 0; /* no longer hibernating */
|
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
|
|
|
|
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. */
|
|
|
|
int
|
|
|
|
we_are_hibernating(void) {
|
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
|
2004-11-14 22:11:06 +01: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
|
|
|
|
|
|
|
log_fn(LOG_NOTICE,"Going dormant. Blowing away remaining connections.");
|
|
|
|
|
2004-11-09 03:12:41 +01:00
|
|
|
/* Close all OR/AP/exit conns. Leave dir conns because we still want
|
|
|
|
* to be able to upload server descriptors so people know we're still
|
|
|
|
* running, and download directories so we can detect if we're obsolete.
|
|
|
|
* Leave control conns because we still want to be controllable.
|
|
|
|
*/
|
2004-11-08 00:14:47 +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))) {
|
|
|
|
log_fn(LOG_INFO,"Closing conn type %d", conn->type);
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
2004-11-10 05:19:53 +01:00
|
|
|
|
2004-11-14 23:21:23 +01:00
|
|
|
accounting_record_bandwidth_usage(now);
|
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. */
|
2004-11-14 22:11:06 +01:00
|
|
|
log_fn(LOG_NOTICE, "Accounting period ended. Commencing hibernation until %s GMT",buf);
|
|
|
|
hibernate_go_dormant(now);
|
|
|
|
} else {
|
|
|
|
log_fn(LOG_NOTICE, "Accounting period ended. This period, we will hibernate until %s GMT",buf);
|
|
|
|
}
|
2004-11-08 00:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The big function. Consider our environment and decide if it's time
|
|
|
|
* to start/stop hibernating.
|
2004-10-31 21:29:25 +01: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];
|
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) {
|
2004-11-08 00:14:47 +01:00
|
|
|
tor_assert(hibernate_end_time);
|
|
|
|
if(hibernate_end_time <= now) {
|
2004-10-31 21:29:25 +01:00
|
|
|
log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
|
|
|
|
tor_cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return; /* if exiting soon, don't worry about bandwidth limits */
|
|
|
|
}
|
|
|
|
|
2004-11-08 00:14:47 +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. */
|
2004-11-14 22:11:06 +01:00
|
|
|
if (hibernate_state == HIBERNATE_STATE_LIVE) {
|
|
|
|
if (hibernate_soft_limit_reached()) {
|
|
|
|
log_fn(LOG_NOTICE,"Bandwidth soft limit reached; commencing hibernation.");
|
|
|
|
hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
|
|
|
|
} else if (accounting_enabled && now < interval_wakeup_time) {
|
|
|
|
format_iso_time(buf,interval_wakeup_time);
|
|
|
|
log_fn(LOG_NOTICE, "Commencing hibernation. We will wake up at %s GMT",buf);
|
|
|
|
hibernate_go_dormant(now);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|