2006-02-09 05:46:49 +00:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 21:09:01 +00:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2012-06-04 20:58:17 -04:00
|
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 01:12:15 +00:00
|
|
|
/* See LICENSE for licensing information */
|
2002-06-26 22:45:49 +00:00
|
|
|
|
2004-05-10 02:36:04 +00:00
|
|
|
/**
|
|
|
|
* \file onion.c
|
2004-05-13 07:24:49 +00:00
|
|
|
* \brief Functions to queue create cells, and handle onionskin
|
|
|
|
* parsing and creation.
|
2004-05-10 02:36:04 +00:00
|
|
|
**/
|
|
|
|
|
2002-06-26 22:45:49 +00:00
|
|
|
#include "or.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-23 20:38:25 +02:00
|
|
|
#include "onion.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2002-06-26 22:45:49 +00:00
|
|
|
|
2005-10-06 04:33:40 +00:00
|
|
|
/** Type for a linked list of circuits that are waiting for a free CPU worker
|
|
|
|
* to process a waiting onion handshake. */
|
2005-07-22 21:12:10 +00:00
|
|
|
typedef struct onion_queue_t {
|
2006-07-23 07:37:35 +00:00
|
|
|
or_circuit_t *circ;
|
2008-02-05 23:20:49 +00:00
|
|
|
char *onionskin;
|
2005-02-20 09:27:48 +00:00
|
|
|
time_t when_added;
|
2003-09-16 20:57:09 +00:00
|
|
|
struct onion_queue_t *next;
|
2005-07-22 21:12:10 +00:00
|
|
|
} onion_queue_t;
|
2003-09-16 20:57:09 +00:00
|
|
|
|
2005-02-20 09:27:48 +00:00
|
|
|
/** 5 seconds on the onion queue til we just send back a destroy */
|
|
|
|
#define ONIONQUEUE_WAIT_CUTOFF 5
|
|
|
|
|
2007-02-24 07:50:38 +00:00
|
|
|
/** First and last elements in the linked list of circuits waiting for CPU
|
2012-06-04 19:51:00 -04:00
|
|
|
* workers, or NULL if the list is empty.
|
|
|
|
* @{ */
|
2005-07-22 21:12:10 +00:00
|
|
|
static onion_queue_t *ol_list=NULL;
|
|
|
|
static onion_queue_t *ol_tail=NULL;
|
2012-06-04 19:51:00 -04:00
|
|
|
/**@}*/
|
2005-02-20 09:27:48 +00:00
|
|
|
/** Length of ol_list */
|
2002-11-27 04:08:20 +00:00
|
|
|
static int ol_length=0;
|
|
|
|
|
2004-05-10 02:36:04 +00:00
|
|
|
/** Add <b>circ</b> to the end of ol_list and return 0, except
|
|
|
|
* if ol_list is too long, in which case do nothing and return -1.
|
|
|
|
*/
|
2005-06-11 18:52:12 +00:00
|
|
|
int
|
2008-02-05 23:20:49 +00:00
|
|
|
onion_pending_add(or_circuit_t *circ, char *onionskin)
|
2005-06-11 18:52:12 +00:00
|
|
|
{
|
2005-07-22 21:12:10 +00:00
|
|
|
onion_queue_t *tmp;
|
2005-02-20 09:27:48 +00:00
|
|
|
time_t now = time(NULL);
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2005-07-22 21:12:10 +00:00
|
|
|
tmp = tor_malloc_zero(sizeof(onion_queue_t));
|
2002-11-27 04:08:20 +00:00
|
|
|
tmp->circ = circ;
|
2008-02-05 23:20:49 +00:00
|
|
|
tmp->onionskin = onionskin;
|
2005-02-20 09:27:48 +00:00
|
|
|
tmp->when_added = now;
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2004-11-28 09:05:49 +00:00
|
|
|
if (!ol_tail) {
|
2004-04-25 20:37:37 +00:00
|
|
|
tor_assert(!ol_list);
|
|
|
|
tor_assert(!ol_length);
|
2002-11-27 04:08:20 +00:00
|
|
|
ol_list = tmp;
|
|
|
|
ol_tail = tmp;
|
|
|
|
ol_length++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 20:37:37 +00:00
|
|
|
tor_assert(ol_list);
|
|
|
|
tor_assert(!ol_tail->next);
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2004-11-28 09:05:49 +00:00
|
|
|
if (ol_length >= get_options()->MaxOnionsPending) {
|
2009-10-26 22:49:43 -07:00
|
|
|
#define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
|
2010-08-18 15:55:49 -04:00
|
|
|
static ratelim_t last_warned =
|
|
|
|
RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
|
|
|
|
char *m;
|
|
|
|
if ((m = rate_limit_log(&last_warned, approx_time()))) {
|
2009-10-26 22:49:43 -07:00
|
|
|
log_warn(LD_GENERAL,
|
|
|
|
"Your computer is too slow to handle this many circuit "
|
|
|
|
"creation requests! Please consider using the "
|
|
|
|
"MaxAdvertisedBandwidth config option or choosing a more "
|
2010-08-18 15:55:49 -04:00
|
|
|
"restricted exit policy.%s",m);
|
|
|
|
tor_free(m);
|
2009-10-26 22:49:43 -07:00
|
|
|
}
|
2004-09-29 06:52:36 +00:00
|
|
|
tor_free(tmp);
|
2002-11-27 04:08:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ol_length++;
|
|
|
|
ol_tail->next = tmp;
|
|
|
|
ol_tail = tmp;
|
2005-02-20 09:27:48 +00:00
|
|
|
while ((int)(now - ol_list->when_added) >= ONIONQUEUE_WAIT_CUTOFF) {
|
|
|
|
/* cull elderly requests. */
|
|
|
|
circ = ol_list->circ;
|
|
|
|
onion_pending_remove(ol_list->circ);
|
2006-02-13 09:37:53 +00:00
|
|
|
log_info(LD_CIRC,
|
2009-05-27 17:55:51 -04:00
|
|
|
"Circuit create request is too old; canceling due to overload.");
|
2006-07-23 07:37:35 +00:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
|
2005-02-20 09:27:48 +00:00
|
|
|
}
|
2002-11-27 04:08:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-10 02:36:04 +00:00
|
|
|
/** Remove the first item from ol_list and return it, or return
|
|
|
|
* NULL if the list is empty.
|
|
|
|
*/
|
2006-07-23 07:37:35 +00:00
|
|
|
or_circuit_t *
|
2008-02-05 23:20:49 +00:00
|
|
|
onion_next_task(char **onionskin_out)
|
2005-06-11 18:52:12 +00:00
|
|
|
{
|
2006-07-23 07:37:35 +00:00
|
|
|
or_circuit_t *circ;
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2004-11-28 09:05:49 +00:00
|
|
|
if (!ol_list)
|
2003-08-20 23:05:22 +00:00
|
|
|
return NULL; /* no onions pending, we're done */
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2004-04-25 20:37:37 +00:00
|
|
|
tor_assert(ol_list->circ);
|
2012-09-02 21:13:25 -07:00
|
|
|
tor_assert(ol_list->circ->p_chan); /* make sure it's still valid */
|
2004-04-25 20:37:37 +00:00
|
|
|
tor_assert(ol_length > 0);
|
2003-09-14 08:17:14 +00:00
|
|
|
circ = ol_list->circ;
|
2008-02-05 23:20:49 +00:00
|
|
|
*onionskin_out = ol_list->onionskin;
|
|
|
|
ol_list->onionskin = NULL; /* prevent free. */
|
2003-09-14 08:17:14 +00:00
|
|
|
onion_pending_remove(ol_list->circ);
|
|
|
|
return circ;
|
2002-11-27 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2004-05-10 02:36:04 +00:00
|
|
|
/** Go through ol_list, find the onion_queue_t element which points to
|
|
|
|
* circ, remove and free that element. Leave circ itself alone.
|
2002-11-27 04:08:20 +00:00
|
|
|
*/
|
2005-06-11 18:52:12 +00:00
|
|
|
void
|
2006-07-23 07:37:35 +00:00
|
|
|
onion_pending_remove(or_circuit_t *circ)
|
2005-06-11 18:52:12 +00:00
|
|
|
{
|
2005-07-22 21:12:10 +00:00
|
|
|
onion_queue_t *tmpo, *victim;
|
2002-11-27 04:08:20 +00:00
|
|
|
|
2004-11-28 09:05:49 +00:00
|
|
|
if (!ol_list)
|
2002-11-27 04:08:20 +00:00
|
|
|
return; /* nothing here. */
|
|
|
|
|
|
|
|
/* first check to see if it's the first entry */
|
|
|
|
tmpo = ol_list;
|
2004-11-28 09:05:49 +00:00
|
|
|
if (tmpo->circ == circ) {
|
2002-11-27 04:08:20 +00:00
|
|
|
/* it's the first one. remove it from the list. */
|
|
|
|
ol_list = tmpo->next;
|
2004-11-28 09:05:49 +00:00
|
|
|
if (!ol_list)
|
2002-11-27 04:08:20 +00:00
|
|
|
ol_tail = NULL;
|
|
|
|
ol_length--;
|
|
|
|
victim = tmpo;
|
|
|
|
} else { /* we need to hunt through the rest of the list */
|
2004-11-28 09:05:49 +00:00
|
|
|
for ( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
|
|
|
|
if (!tmpo->next) {
|
2006-02-13 09:37:53 +00:00
|
|
|
log_debug(LD_GENERAL,
|
|
|
|
"circ (p_circ_id %d) not in list, probably at cpuworker.",
|
|
|
|
circ->p_circ_id);
|
2002-11-27 04:08:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* now we know tmpo->next->circ == circ */
|
|
|
|
victim = tmpo->next;
|
|
|
|
tmpo->next = victim->next;
|
2004-11-28 09:05:49 +00:00
|
|
|
if (ol_tail == victim)
|
2002-11-27 04:08:20 +00:00
|
|
|
ol_tail = tmpo;
|
|
|
|
ol_length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now victim points to the element that needs to be removed */
|
|
|
|
|
2008-02-05 23:20:49 +00:00
|
|
|
tor_free(victim->onionskin);
|
2004-09-29 06:52:36 +00:00
|
|
|
tor_free(victim);
|
2002-11-27 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2005-02-11 01:26:47 +00:00
|
|
|
/** Remove all circuits from the pending list. Called from tor_free_all. */
|
|
|
|
void
|
|
|
|
clear_pending_onions(void)
|
|
|
|
{
|
|
|
|
while (ol_list) {
|
2005-07-22 21:12:10 +00:00
|
|
|
onion_queue_t *victim = ol_list;
|
2005-02-11 01:26:47 +00:00
|
|
|
ol_list = victim->next;
|
2008-02-05 23:20:49 +00:00
|
|
|
tor_free(victim->onionskin);
|
2005-02-11 01:26:47 +00:00
|
|
|
tor_free(victim);
|
|
|
|
}
|
|
|
|
ol_list = ol_tail = NULL;
|
|
|
|
ol_length = 0;
|
|
|
|
}
|
2005-06-09 19:03:31 +00:00
|
|
|
|