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 03:12:15 +02:00
|
|
|
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
2002-11-23 09:49:03 +01:00
|
|
|
extern or_options_t options; /* command-line and config-file options */
|
|
|
|
|
2002-11-24 09:45:54 +01:00
|
|
|
void command_time_process_cell(cell_t *cell, connection_t *conn,
|
|
|
|
int *num, int *time,
|
|
|
|
void (*func)(cell_t *, connection_t *)) {
|
|
|
|
struct timeval start, end;
|
2003-04-17 01:21:44 +02:00
|
|
|
long time_passed;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
*num += 1;
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
my_gettimeofday(&start);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
(*func)(cell, conn);
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
my_gettimeofday(&end);
|
|
|
|
time_passed = tv_udiff(&start, &end) ;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
if (time_passed > 5000) { /* more than 5ms */
|
2002-11-24 09:45:54 +01:00
|
|
|
log(LOG_INFO,"command_time_process_cell(): That call just took %d ms.",time_passed/1000);
|
|
|
|
}
|
|
|
|
*time += time_passed;
|
|
|
|
}
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
void command_process_cell(cell_t *cell, connection_t *conn) {
|
2003-05-20 08:41:23 +02:00
|
|
|
static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
|
|
|
|
static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
|
2002-11-24 09:45:54 +01:00
|
|
|
static long current_second = 0; /* from previous calls to gettimeofday */
|
|
|
|
struct timeval now;
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
my_gettimeofday(&now);
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
if(now.tv_sec > current_second) { /* the second has rolled over */
|
|
|
|
/* print stats */
|
|
|
|
log(LOG_INFO,"At end of second:");
|
|
|
|
log(LOG_INFO,"Create: %d (%d ms)", num_create, create_time/1000);
|
2003-05-06 01:24:46 +02:00
|
|
|
log(LOG_INFO,"Created: %d (%d ms)", num_created, created_time/1000);
|
2003-05-01 08:42:29 +02:00
|
|
|
log(LOG_INFO,"Relay: %d (%d ms)", num_relay, relay_time/1000);
|
2002-11-24 09:45:54 +01:00
|
|
|
log(LOG_INFO,"Destroy: %d (%d ms)", num_destroy, destroy_time/1000);
|
|
|
|
|
|
|
|
/* zero out stats */
|
2003-05-20 08:41:23 +02:00
|
|
|
num_create = num_created = num_relay = num_destroy = 0;
|
|
|
|
create_time = created_time = relay_time = destroy_time = 0;
|
2002-11-24 09:45:54 +01:00
|
|
|
|
|
|
|
/* remember which second it is, for next time */
|
|
|
|
current_second = now.tv_sec;
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
switch(cell->command) {
|
|
|
|
case CELL_PADDING:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
case CELL_CREATE:
|
2002-11-24 09:45:54 +01:00
|
|
|
command_time_process_cell(cell, conn, &num_create, &create_time,
|
|
|
|
command_process_create_cell);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
2003-05-06 01:24:46 +02:00
|
|
|
case CELL_CREATED:
|
|
|
|
command_time_process_cell(cell, conn, &num_created, &created_time,
|
|
|
|
command_process_created_cell);
|
|
|
|
break;
|
2003-05-01 08:42:29 +02:00
|
|
|
case CELL_RELAY:
|
|
|
|
command_time_process_cell(cell, conn, &num_relay, &relay_time,
|
|
|
|
command_process_relay_cell);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
|
|
|
case CELL_DESTROY:
|
2002-11-24 09:45:54 +01:00
|
|
|
command_time_process_cell(cell, conn, &num_destroy, &destroy_time,
|
|
|
|
command_process_destroy_cell);
|
2002-06-27 00:45:49 +02:00
|
|
|
break;
|
2002-07-18 08:37:58 +02:00
|
|
|
default:
|
|
|
|
log(LOG_DEBUG,"Cell of unknown type (%d) received. Dropping.", cell->command);
|
|
|
|
break;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_process_create_cell(cell_t *cell, connection_t *conn) {
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
circ = circuit_get_by_aci_conn(cell->aci, conn);
|
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circ) {
|
|
|
|
log(LOG_DEBUG,"command_process_create_cell(): received CREATE cell for known circ. Dropping.");
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
circ = circuit_new(cell->aci, conn);
|
|
|
|
circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
|
2003-05-06 07:54:42 +02:00
|
|
|
if(cell->length != DH_ONIONSKIN_LEN) {
|
2003-05-06 01:24:46 +02:00
|
|
|
log(LOG_DEBUG,"command_process_create_cell(): Bad cell length %d. Dropping.", cell->length);
|
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(circ->onionskin,cell->payload,cell->length);
|
|
|
|
|
|
|
|
/* add it to the pending onions queue, and then return */
|
|
|
|
if(onion_pending_add(circ) < 0) {
|
|
|
|
log(LOG_DEBUG,"command_process_create_cell(): Failed to queue onionskin. Closing.");
|
|
|
|
circuit_close(circ);
|
|
|
|
}
|
|
|
|
log(LOG_DEBUG,"command_process_create_cell(): success: queued onionskin.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_process_created_cell(cell_t *cell, connection_t *conn) {
|
|
|
|
circuit_t *circ;
|
|
|
|
cell_t newcell;
|
|
|
|
|
|
|
|
circ = circuit_get_by_aci_conn(cell->aci, conn);
|
|
|
|
|
|
|
|
if(!circ) {
|
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): received CREATED cell for unknown circ. Dropping.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(circ->n_aci != cell->aci) {
|
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): got created cell from OPward? Dropping.");
|
|
|
|
return;
|
|
|
|
}
|
2003-05-06 07:54:42 +02:00
|
|
|
assert(cell->length == DH_KEY_LEN);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
|
|
|
if(circ->cpath) { /* we're the OP. Handshake this. */
|
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): at OP. Finishing handshake.");
|
|
|
|
if(circuit_finish_handshake(circ, cell->payload) < 0) {
|
|
|
|
log(LOG_INFO,"command_process_created_cell(): circuit_finish_handshake failed.");
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
|
|
|
}
|
2003-05-06 01:24:46 +02:00
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): Moving to next skin.");
|
|
|
|
if(circuit_send_next_onion_skin(circ) < 0) {
|
|
|
|
log(LOG_INFO,"command_process_created_cell(): circuit_send_next_onion_skin failed.");
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
|
|
|
}
|
2003-05-06 01:24:46 +02:00
|
|
|
} else { /* pack it into an extended relay cell, and send it. */
|
|
|
|
memset(&newcell, 0, sizeof(cell_t));
|
|
|
|
newcell.command = CELL_RELAY;
|
|
|
|
newcell.aci = circ->p_aci;
|
|
|
|
SET_CELL_RELAY_COMMAND(newcell, RELAY_COMMAND_EXTENDED);
|
|
|
|
SET_CELL_STREAM_ID(newcell, ZERO_STREAM);
|
|
|
|
|
|
|
|
newcell.length = RELAY_HEADER_SIZE + cell->length;
|
2003-05-06 07:54:42 +02:00
|
|
|
memcpy(newcell.payload+RELAY_HEADER_SIZE, cell->payload, DH_KEY_LEN);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): Sending extended relay cell.");
|
2003-06-12 12:16:33 +02:00
|
|
|
if(circuit_deliver_relay_cell(&newcell, circ, CELL_DIRECTION_IN, NULL) < 0) {
|
2003-05-06 01:24:46 +02:00
|
|
|
log(LOG_DEBUG,"command_process_created_cell(): failed to deliver extended cell. Closing.");
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
|
|
|
}
|
2002-11-27 05:08:20 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-05-01 08:42:29 +02:00
|
|
|
void command_process_relay_cell(cell_t *cell, connection_t *conn) {
|
2002-07-18 08:37:58 +02:00
|
|
|
circuit_t *circ;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
circ = circuit_get_by_aci_conn(cell->aci, conn);
|
|
|
|
|
|
|
|
if(!circ) {
|
2003-05-01 08:42:29 +02:00
|
|
|
log(LOG_DEBUG,"command_process_relay_cell(): unknown circuit %d. Dropping.", cell->aci);
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2003-05-01 08:42:29 +02:00
|
|
|
log(LOG_DEBUG,"command_process_relay_cell(): circuit in create_wait. Queueing relay cell.");
|
|
|
|
onion_pending_relay_add(circ, cell);
|
2002-11-27 05:08:20 +01:00
|
|
|
return;
|
|
|
|
}
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-02-18 02:35:55 +01:00
|
|
|
if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
|
|
|
|
cell->aci = circ->n_aci; /* switch it */
|
2003-05-02 23:29:25 +02:00
|
|
|
if(circuit_deliver_relay_cell(cell, circ, CELL_DIRECTION_OUT, conn->cpath_layer) < 0) {
|
2003-05-01 08:42:29 +02:00
|
|
|
log(LOG_INFO,"command_process_relay_cell(): circuit_deliver_relay_cell (forward) failed. Closing.");
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else { /* it's an ingoing cell */
|
|
|
|
cell->aci = circ->p_aci; /* switch it */
|
2003-05-02 23:29:25 +02:00
|
|
|
if(circuit_deliver_relay_cell(cell, circ, CELL_DIRECTION_IN, NULL) < 0) {
|
2003-05-01 08:42:29 +02:00
|
|
|
log(LOG_DEBUG,"command_process_relay_cell(): circuit_deliver_relay_cell (backward) failed. Closing.");
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
circuit_close(circ);
|
|
|
|
return;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
circ = circuit_get_by_aci_conn(cell->aci, conn);
|
|
|
|
|
|
|
|
if(!circ) {
|
2002-07-18 08:37:58 +02:00
|
|
|
log(LOG_DEBUG,"command_process_destroy_cell(): unknown circuit %d. Dropping.", cell->aci);
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(LOG_DEBUG,"command_process_destroy_cell(): Received for aci %d.",cell->aci);
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2002-11-27 05:08:20 +01:00
|
|
|
onion_pending_remove(circ);
|
|
|
|
}
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-06-13 11:20:23 +02:00
|
|
|
if(cell->aci == circ->p_aci || circ->cpath) {
|
|
|
|
/* either the destroy came from behind, or we're the AP */
|
2003-04-20 21:47:33 +02:00
|
|
|
circ->p_conn = NULL;
|
2003-06-13 11:20:23 +02:00
|
|
|
circuit_close(circ);
|
|
|
|
} else { /* the destroy came from ahead */
|
2003-04-20 21:47:33 +02:00
|
|
|
circ->n_conn = NULL;
|
2003-06-13 11:20:23 +02:00
|
|
|
log(LOG_DEBUG, "command_process_destroy_cell(): Delivering 'truncated' back.");
|
|
|
|
connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED);
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2003-04-07 04:12:02 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|