2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine.
|
|
|
|
* Copyright 2004 Roger Dingledine, Nick Mathewson. */
|
2003-08-21 01:05:22 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file cpuworker.c
|
|
|
|
* \brief Run computation-intensive tasks (generally for crypto) in
|
2004-05-05 23:32:43 +02:00
|
|
|
* a separate execution context. [OR only.]
|
|
|
|
*
|
|
|
|
* Right now, we only use this for processing onionskins.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2003-08-21 01:05:22 +02:00
|
|
|
#include "or.h"
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** The maximum number of cpuworker processes we will keep around. */
|
2003-11-16 18:00:02 +01:00
|
|
|
#define MAX_CPUWORKERS 16
|
2004-05-10 12:27:54 +02:00
|
|
|
/** The minimum number of cpuworker processes we will keep around. */
|
2003-09-14 04:58:50 +02:00
|
|
|
#define MIN_CPUWORKERS 1
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** The tag specifies which circuit this onionskin was from. */
|
2004-07-16 23:47:18 +02:00
|
|
|
#define TAG_LEN 8
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How many bytes are sent from tor to the cpuworker? */
|
2003-12-16 09:21:58 +01:00
|
|
|
#define LEN_ONION_QUESTION (1+TAG_LEN+ONIONSKIN_CHALLENGE_LEN)
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How many bytes are sent from the cpuworker back to tor? */
|
2003-12-17 06:58:30 +01:00
|
|
|
#define LEN_ONION_RESPONSE (1+TAG_LEN+ONIONSKIN_REPLY_LEN+40+32)
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** How many cpuworkers we have running right now. */
|
2004-04-25 00:17:50 +02:00
|
|
|
static int num_cpuworkers=0;
|
2004-05-10 12:27:54 +02:00
|
|
|
/** How many of the running cpuworkers have an assigned task right now. */
|
2004-04-25 00:17:50 +02:00
|
|
|
static int num_cpuworkers_busy=0;
|
2004-05-09 18:47:25 +02:00
|
|
|
/** We need to spawn new cpuworkers whenever we rotate the onion keys
|
2004-05-05 23:32:43 +02:00
|
|
|
* on platforms where execution contexts==processes. This variable stores
|
2004-05-10 12:27:54 +02:00
|
|
|
* the last time we got a key rotation event. */
|
2004-04-25 00:17:50 +02:00
|
|
|
static time_t last_rotation_time=0;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-05-12 22:58:27 +02:00
|
|
|
static int cpuworker_main(void *data);
|
2003-08-21 01:05:22 +02:00
|
|
|
static int spawn_cpuworker(void);
|
|
|
|
static void spawn_enough_cpuworkers(void);
|
2003-09-14 04:58:50 +02:00
|
|
|
static void process_pending_task(connection_t *cpuworker);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Initialize the cpuworker subsystem.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2003-08-21 01:05:22 +02:00
|
|
|
void cpu_init(void) {
|
2004-04-25 00:17:50 +02:00
|
|
|
last_rotation_time=time(NULL);
|
2003-08-21 01:05:22 +02:00
|
|
|
spawn_enough_cpuworkers();
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we're done sending a request to a cpuworker. */
|
2003-08-21 01:05:22 +02:00
|
|
|
int connection_cpu_finished_flushing(connection_t *conn) {
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_CPUWORKER);
|
2003-08-21 01:05:22 +02:00
|
|
|
connection_stop_writing(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Pack addr,port,and circ_id; set *tag to the result. (See note on
|
2004-05-05 23:32:43 +02:00
|
|
|
* cpuworker_main for wire format.) */
|
2004-07-16 23:47:18 +02:00
|
|
|
static void tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id) {
|
|
|
|
*(uint32_t *)tag = addr;
|
|
|
|
*(uint16_t *)(tag+4) = port;
|
|
|
|
*(uint16_t *)(tag+6) = circ_id;
|
2003-09-14 04:58:50 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Unpack <b>tag</b> into addr, port, and circ_id.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2004-07-16 23:47:18 +02:00
|
|
|
static void tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id) {
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
*addr = *(const uint32_t *)tag;
|
|
|
|
*port = *(const uint16_t *)(tag+4);
|
|
|
|
*circ_id = *(const uint16_t *)(tag+6);
|
|
|
|
|
|
|
|
in.s_addr = htonl(*addr);
|
|
|
|
log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", inet_ntoa(in), *port, *circ_id);
|
2003-09-14 04:58:50 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when the onion key has changed and we need to spawn new
|
2004-05-05 23:32:43 +02:00
|
|
|
* cpuworkers. Close all currently idle cpuworkers, and mark the last
|
|
|
|
* rotation time as now.
|
|
|
|
*/
|
2004-04-25 00:17:50 +02:00
|
|
|
void cpuworkers_rotate(void)
|
|
|
|
{
|
|
|
|
connection_t *cpuworker;
|
|
|
|
while ((cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
|
|
|
|
CPUWORKER_STATE_IDLE))) {
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(cpuworker);
|
2004-04-25 00:17:50 +02:00
|
|
|
--num_cpuworkers;
|
|
|
|
}
|
|
|
|
last_rotation_time = time(NULL);
|
|
|
|
spawn_enough_cpuworkers();
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we get data from a cpuworker. If the answer is not complete,
|
2004-05-05 23:32:43 +02:00
|
|
|
* wait for a complete answer. If the cpuworker closes the connection,
|
|
|
|
* mark it as closed and spawn a new one as needed. If the answer is complete,
|
|
|
|
* process it as appropriate.
|
|
|
|
*/
|
2003-08-21 01:05:22 +02:00
|
|
|
int connection_cpu_process_inbuf(connection_t *conn) {
|
2004-03-20 10:30:30 +01:00
|
|
|
char success;
|
2003-09-14 04:58:50 +02:00
|
|
|
unsigned char buf[LEN_ONION_RESPONSE];
|
2004-07-16 23:47:18 +02:00
|
|
|
uint32_t addr;
|
|
|
|
uint16_t port;
|
2003-12-19 20:55:02 +01:00
|
|
|
uint16_t circ_id;
|
2003-09-14 04:58:50 +02:00
|
|
|
connection_t *p_conn;
|
|
|
|
circuit_t *circ;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_CPUWORKER);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
if(conn->inbuf_reached_eof) {
|
2004-03-06 02:43:37 +01:00
|
|
|
log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
|
2003-08-21 01:05:22 +02:00
|
|
|
if(conn->state != CPUWORKER_STATE_IDLE) {
|
2003-11-16 18:00:02 +01:00
|
|
|
/* the circ associated with this cpuworker will have to wait until
|
|
|
|
* it gets culled in run_connection_housekeeping(), since we have
|
|
|
|
* no way to find out which circ it was. */
|
2004-03-06 02:43:37 +01:00
|
|
|
log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
|
2003-08-21 01:05:22 +02:00
|
|
|
num_cpuworkers_busy--;
|
|
|
|
}
|
|
|
|
num_cpuworkers--;
|
2003-11-16 18:00:02 +01:00
|
|
|
spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(conn);
|
2004-02-28 05:11:53 +01:00
|
|
|
return 0;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
|
2003-09-25 07:17:11 +02:00
|
|
|
if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
|
2003-08-21 01:05:22 +02:00
|
|
|
return 0; /* not yet */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-03-20 10:30:30 +01:00
|
|
|
connection_fetch_from_buf(&success,1,conn);
|
|
|
|
connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2003-09-14 04:58:50 +02:00
|
|
|
/* parse out the circ it was talking about */
|
2004-07-16 23:47:18 +02:00
|
|
|
tag_unpack(buf, &addr, &port, &circ_id);
|
2003-09-14 04:58:50 +02:00
|
|
|
circ = NULL;
|
2004-11-16 04:12:53 +01:00
|
|
|
/* (Here we use connection_exact_get_by_addr_port rather than
|
|
|
|
* get_by_identity_digest: we want a specific port here in
|
2004-11-12 17:39:03 +01:00
|
|
|
* case there are multiple connections.) */
|
2004-07-16 23:47:18 +02:00
|
|
|
p_conn = connection_exact_get_by_addr_port(addr,port);
|
2003-09-14 04:58:50 +02:00
|
|
|
if(p_conn)
|
2003-11-11 04:01:48 +01:00
|
|
|
circ = circuit_get_by_circ_id_conn(circ_id, p_conn);
|
2003-09-14 04:58:50 +02:00
|
|
|
|
2004-04-25 06:49:11 +02:00
|
|
|
if(success == 0) {
|
|
|
|
log_fn(LOG_WARN,"decoding onionskin failed. Closing.");
|
|
|
|
if(circ)
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
goto done_processing;
|
|
|
|
}
|
2003-09-14 04:58:50 +02:00
|
|
|
if(!circ) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO,"processed onion for a circ that's gone. Dropping.");
|
2003-09-14 04:58:50 +02:00
|
|
|
goto done_processing;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->p_conn);
|
2004-03-20 10:30:30 +01:00
|
|
|
if(onionskin_answer(circ, buf+TAG_LEN, buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"onionskin_answer failed. Closing.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2003-09-14 04:58:50 +02:00
|
|
|
goto done_processing;
|
|
|
|
}
|
|
|
|
log_fn(LOG_DEBUG,"onionskin_answer succeeded. Yay.");
|
2003-08-21 01:05:22 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0); /* don't ask me to do handshakes yet */
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
2003-09-14 04:58:50 +02:00
|
|
|
done_processing:
|
2003-08-21 01:05:22 +02:00
|
|
|
conn->state = CPUWORKER_STATE_IDLE;
|
|
|
|
num_cpuworkers_busy--;
|
2004-04-25 00:17:50 +02:00
|
|
|
if (conn->timestamp_created < last_rotation_time) {
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(conn);
|
2004-04-25 00:17:50 +02:00
|
|
|
num_cpuworkers--;
|
|
|
|
spawn_enough_cpuworkers();
|
|
|
|
} else {
|
|
|
|
process_pending_task(conn);
|
|
|
|
}
|
2003-08-21 01:05:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Implement a cpuworker. 'data' is an fdarray as returned by socketpair.
|
2004-05-05 23:32:43 +02:00
|
|
|
* Read and writes from fdarray[1]. Reads requests, writes answers.
|
|
|
|
*
|
|
|
|
* Request format:
|
2004-05-06 13:08:04 +02:00
|
|
|
* Task type [1 byte, always CPUWORKER_TASK_ONION]
|
2004-05-05 23:32:43 +02:00
|
|
|
* Opaque tag TAG_LEN
|
|
|
|
* Onionskin challenge ONIONSKIN_CHALLENGE_LEN
|
|
|
|
* Response format:
|
|
|
|
* Success/failure [1 byte, boolean.]
|
|
|
|
* Opaque tag TAG_LEN
|
|
|
|
* Onionskin challenge ONIONSKIN_REPLY_LEN
|
|
|
|
* Negotiated keys KEY_LEN*2+DIGEST_LEN*2
|
2004-07-16 23:47:18 +02:00
|
|
|
*
|
|
|
|
* (Note: this _should_ be by addr/port, since we're concerned with specific
|
2004-10-17 23:51:20 +02:00
|
|
|
* connections, not with routers (where we'd use identity).)
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2004-05-12 22:58:27 +02:00
|
|
|
static int cpuworker_main(void *data) {
|
2003-12-16 09:21:58 +01:00
|
|
|
unsigned char question[ONIONSKIN_CHALLENGE_LEN];
|
2003-08-21 01:05:22 +02:00
|
|
|
unsigned char question_type;
|
|
|
|
int *fdarray = data;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* variables for onion processing */
|
2003-12-17 06:58:30 +01:00
|
|
|
unsigned char keys[40+32];
|
2003-12-16 09:21:58 +01:00
|
|
|
unsigned char reply_to_proxy[ONIONSKIN_REPLY_LEN];
|
2003-09-14 04:58:50 +02:00
|
|
|
unsigned char buf[LEN_ONION_RESPONSE];
|
|
|
|
char tag[TAG_LEN];
|
2004-04-25 00:17:50 +02:00
|
|
|
crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
|
2003-08-21 01:05:22 +02:00
|
|
|
fd = fdarray[1]; /* this side is ours */
|
2004-03-12 22:52:15 +01:00
|
|
|
#ifndef MS_WINDOWS
|
2004-01-06 08:53:40 +01:00
|
|
|
connection_free_all(); /* so the child doesn't hold the parent's fd's open */
|
2004-03-12 22:52:15 +01:00
|
|
|
#endif
|
2004-08-08 09:25:45 +02:00
|
|
|
handle_signals(0); /* ignore interrupts from the keyboard, etc */
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-06-05 03:50:35 +02:00
|
|
|
dup_onion_keys(&onion_key, &last_onion_key);
|
2004-04-25 00:17:50 +02:00
|
|
|
|
2003-08-21 01:05:22 +02:00
|
|
|
for(;;) {
|
|
|
|
|
2004-03-11 07:35:03 +01:00
|
|
|
if(recv(fd, &question_type, 1, 0) != 1) {
|
2004-03-04 22:26:23 +01:00
|
|
|
// log_fn(LOG_ERR,"read type failed. Exiting.");
|
2004-05-09 18:33:04 +02:00
|
|
|
log_fn(LOG_INFO,"cpuworker exiting because tor process closed connection (either rotated keys or died).");
|
2004-04-25 00:17:50 +02:00
|
|
|
goto end;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(question_type == CPUWORKER_TASK_ONION);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-03-11 07:35:03 +01:00
|
|
|
if(read_all(fd, tag, TAG_LEN, 1) != TAG_LEN) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_ERR,"read tag failed. Exiting.");
|
2004-04-25 00:17:50 +02:00
|
|
|
goto end;
|
2003-09-14 04:58:50 +02:00
|
|
|
}
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-03-11 07:35:03 +01:00
|
|
|
if(read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) != ONIONSKIN_CHALLENGE_LEN) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_ERR,"read question failed. Exiting.");
|
2004-04-25 00:17:50 +02:00
|
|
|
goto end;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(question_type == CPUWORKER_TASK_ONION) {
|
2004-04-25 00:17:50 +02:00
|
|
|
if(onion_skin_server_handshake(question, onion_key, last_onion_key,
|
2003-12-17 06:58:30 +01:00
|
|
|
reply_to_proxy, keys, 40+32) < 0) {
|
2003-08-21 01:05:22 +02:00
|
|
|
/* failure */
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_server_handshake failed.");
|
2003-08-21 01:05:22 +02:00
|
|
|
memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
|
|
|
|
} else {
|
|
|
|
/* success */
|
2004-06-03 07:57:27 +02:00
|
|
|
log_fn(LOG_DEBUG,"onion_skin_server_handshake succeeded.");
|
2003-08-21 01:05:22 +02:00
|
|
|
buf[0] = 1; /* 1 means success */
|
2003-09-14 04:58:50 +02:00
|
|
|
memcpy(buf+1,tag,TAG_LEN);
|
2003-12-16 09:21:58 +01:00
|
|
|
memcpy(buf+1+TAG_LEN,reply_to_proxy,ONIONSKIN_REPLY_LEN);
|
2003-12-17 06:58:30 +01:00
|
|
|
memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,40+32);
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2004-03-11 07:35:03 +01:00
|
|
|
if(write_all(fd, buf, LEN_ONION_RESPONSE, 1) != LEN_ONION_RESPONSE) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_ERR,"writing response buf failed. Exiting.");
|
2003-08-21 01:05:22 +02:00
|
|
|
spawn_exit();
|
|
|
|
}
|
2003-09-14 04:58:50 +02:00
|
|
|
log_fn(LOG_DEBUG,"finished writing response.");
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
}
|
2004-04-25 00:17:50 +02:00
|
|
|
end:
|
|
|
|
if (onion_key)
|
|
|
|
crypto_free_pk_env(onion_key);
|
|
|
|
if (last_onion_key)
|
|
|
|
crypto_free_pk_env(last_onion_key);
|
|
|
|
spawn_exit();
|
2003-08-21 01:05:22 +02:00
|
|
|
return 0; /* windows wants this function to return an int */
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Launch a new cpuworker.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2003-08-21 01:05:22 +02:00
|
|
|
static int spawn_cpuworker(void) {
|
|
|
|
int fd[2];
|
|
|
|
connection_t *conn;
|
|
|
|
|
|
|
|
if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
|
2004-05-02 22:18:21 +02:00
|
|
|
log(LOG_ERR, "Couldn't construct socketpair: %s",
|
|
|
|
tor_socket_strerror(tor_socket_errno(-1)));
|
2004-07-23 00:15:36 +02:00
|
|
|
tor_cleanup();
|
2003-08-21 01:05:22 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn_func(cpuworker_main, (void*)fd);
|
|
|
|
log_fn(LOG_DEBUG,"just spawned a worker.");
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
conn = connection_new(CONN_TYPE_CPUWORKER);
|
|
|
|
|
|
|
|
set_socket_nonblocking(fd[0]);
|
|
|
|
|
|
|
|
/* set up conn so it's got all the data we need to remember */
|
|
|
|
conn->s = fd[0];
|
2003-10-04 05:29:09 +02:00
|
|
|
conn->address = tor_strdup("localhost");
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
if(connection_add(conn) < 0) { /* no space, forget it */
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"connection_add failed. Giving up.");
|
2003-08-21 01:05:22 +02:00
|
|
|
connection_free(conn); /* this closes fd[0] */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->state = CPUWORKER_STATE_IDLE;
|
|
|
|
connection_start_reading(conn);
|
|
|
|
|
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If we have too few or too many active cpuworkers, try to spawn new ones
|
2004-05-05 23:32:43 +02:00
|
|
|
* or kill idle ones.
|
|
|
|
*/
|
2003-08-21 01:05:22 +02:00
|
|
|
static void spawn_enough_cpuworkers(void) {
|
2004-11-06 06:18:11 +01:00
|
|
|
int num_cpuworkers_needed = get_options()->NumCpus;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
if(num_cpuworkers_needed < MIN_CPUWORKERS)
|
|
|
|
num_cpuworkers_needed = MIN_CPUWORKERS;
|
|
|
|
if(num_cpuworkers_needed > MAX_CPUWORKERS)
|
|
|
|
num_cpuworkers_needed = MAX_CPUWORKERS;
|
|
|
|
|
|
|
|
while(num_cpuworkers < num_cpuworkers_needed) {
|
|
|
|
if(spawn_cpuworker() < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"spawn failed!");
|
2003-08-21 01:05:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
num_cpuworkers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Take a pending task from the queue and assign it to 'cpuworker'. */
|
2003-09-14 04:58:50 +02:00
|
|
|
static void process_pending_task(connection_t *cpuworker) {
|
2003-08-21 01:05:22 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cpuworker);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
/* for now only process onion tasks */
|
|
|
|
|
|
|
|
circ = onion_next_task();
|
|
|
|
if(!circ)
|
2003-09-14 04:58:50 +02:00
|
|
|
return;
|
|
|
|
if(assign_to_cpuworker(cpuworker, CPUWORKER_TASK_ONION, circ) < 0)
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"assign_to_cpuworker failed. Ignoring.");
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** if cpuworker is defined, assert that he's idle, and use him. else,
|
2003-08-21 01:05:22 +02:00
|
|
|
* look for an idle cpuworker and use him. if none idle, queue task onto
|
|
|
|
* the pending onion list and return.
|
2003-11-16 18:00:02 +01:00
|
|
|
* If question_type is CPUWORKER_TASK_ONION then task is a circ.
|
|
|
|
* No other question_types are allowed.
|
2003-08-21 01:05:22 +02:00
|
|
|
*/
|
|
|
|
int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
|
|
|
|
void *task) {
|
|
|
|
circuit_t *circ;
|
2003-09-14 04:58:50 +02:00
|
|
|
char tag[TAG_LEN];
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(question_type == CPUWORKER_TASK_ONION);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
if(question_type == CPUWORKER_TASK_ONION) {
|
|
|
|
circ = task;
|
|
|
|
|
|
|
|
if(num_cpuworkers_busy == num_cpuworkers) {
|
|
|
|
log_fn(LOG_DEBUG,"No idle cpuworkers. Queuing.");
|
|
|
|
if(onion_pending_add(circ) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 00:17:50 +02:00
|
|
|
if (!cpuworker)
|
2003-08-21 01:05:22 +02:00
|
|
|
cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER, CPUWORKER_STATE_IDLE);
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cpuworker);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2003-09-14 04:58:50 +02:00
|
|
|
if(!circ->p_conn) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO,"circ->p_conn gone. Failing circ.");
|
2003-09-14 04:58:50 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-07-16 23:47:18 +02:00
|
|
|
tag_pack(tag, circ->p_conn->addr, circ->p_conn->port, circ->p_circ_id);
|
2003-09-14 04:58:50 +02:00
|
|
|
|
2003-08-21 01:05:22 +02:00
|
|
|
cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
|
|
|
|
num_cpuworkers_busy++;
|
|
|
|
|
2003-10-04 04:38:18 +02:00
|
|
|
connection_write_to_buf(&question_type, 1, cpuworker);
|
|
|
|
connection_write_to_buf(tag, sizeof(tag), cpuworker);
|
2003-12-16 09:21:58 +01:00
|
|
|
connection_write_to_buf(circ->onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2003-11-16 18:00:02 +01:00
|
|
|
return 0;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|