2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine.
|
2005-04-01 22:15:56 +02:00
|
|
|
* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
|
2003-08-21 01:05:22 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2004-11-29 23:25:31 +01:00
|
|
|
const char cpuworker_c_id[] = "$Id$";
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file cpuworker.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Implements a farm of 'CPU worker' processes to perform
|
|
|
|
* CPU-intensive tasks in another thread or process, to not
|
|
|
|
* interrupt the main thread.
|
2004-05-05 23:32:43 +02:00
|
|
|
*
|
|
|
|
* 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? */
|
2005-05-03 00:35:18 +02:00
|
|
|
#define LEN_ONION_RESPONSE (1+TAG_LEN+ONIONSKIN_REPLY_LEN+CPATH_KEY_MATERIAL_LEN)
|
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
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
cpu_init(void)
|
|
|
|
{
|
2005-08-15 12:27:37 +02:00
|
|
|
cpuworkers_rotate();
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we're done sending a request to a cpuworker. */
|
2005-06-11 20:52:12 +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.) */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id)
|
|
|
|
{
|
2004-07-16 23:47:18 +02:00
|
|
|
*(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
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id)
|
|
|
|
{
|
2004-07-16 23:47:18 +02:00
|
|
|
struct in_addr in;
|
2005-02-22 09:18:36 +01:00
|
|
|
char addrbuf[INET_NTOA_BUF_LEN];
|
2004-07-16 23:47:18 +02:00
|
|
|
|
|
|
|
*addr = *(const uint32_t *)tag;
|
|
|
|
*port = *(const uint16_t *)(tag+4);
|
|
|
|
*circ_id = *(const uint16_t *)(tag+6);
|
|
|
|
|
|
|
|
in.s_addr = htonl(*addr);
|
2005-02-22 09:18:36 +01:00
|
|
|
tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
|
|
|
|
log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", addrbuf, *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.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
cpuworkers_rotate(void)
|
2004-04-25 00:17:50 +02:00
|
|
|
{
|
|
|
|
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);
|
2005-08-16 01:46:18 +02:00
|
|
|
if (server_mode(get_options()))
|
|
|
|
spawn_enough_cpuworkers();
|
2004-04-25 00:17:50 +02:00
|
|
|
}
|
|
|
|
|
2004-11-21 11:14:57 +01:00
|
|
|
/** If the cpuworker closes the connection,
|
|
|
|
* mark it as closed and spawn a new one as needed. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_cpu_reached_eof(connection_t *conn)
|
|
|
|
{
|
2004-11-21 11:14:57 +01:00
|
|
|
log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->state != CPUWORKER_STATE_IDLE) {
|
2004-11-21 11:14:57 +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. */
|
|
|
|
log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
|
|
|
|
num_cpuworkers_busy--;
|
|
|
|
}
|
|
|
|
num_cpuworkers--;
|
|
|
|
spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we get data from a cpuworker. If the answer is not complete,
|
2004-11-21 11:14:57 +01:00
|
|
|
* wait for a complete answer. If the answer is complete,
|
2004-05-05 23:32:43 +02:00
|
|
|
* process it as appropriate.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_cpu_process_inbuf(connection_t *conn)
|
|
|
|
{
|
2004-03-20 10:30:30 +01:00
|
|
|
char success;
|
2005-05-07 07:55:06 +02:00
|
|
|
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
|
|
|
|
2004-12-07 00:19:55 +01:00
|
|
|
if (!buf_datalen(conn->inbuf))
|
|
|
|
return 0;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->state == CPUWORKER_STATE_BUSY_ONION) {
|
|
|
|
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;
|
2005-04-06 17:19:32 +02:00
|
|
|
/* (Here we use connection_or_exact_get_by_addr_port rather than
|
2004-11-16 04:12:53 +01:00
|
|
|
* get_by_identity_digest: we want a specific port here in
|
2004-11-12 17:39:03 +01:00
|
|
|
* case there are multiple connections.) */
|
2005-04-06 17:19:32 +02:00
|
|
|
p_conn = connection_or_exact_get_by_addr_port(addr,port);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (p_conn)
|
2005-04-06 07:33:32 +02:00
|
|
|
circ = circuit_get_by_circid_orconn(circ_id, p_conn);
|
2003-09-14 04:58:50 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (success == 0) {
|
2005-10-17 18:21:42 +02:00
|
|
|
log_fn(LOG_DEBUG,"decoding onionskin failed. Closing.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ)
|
2004-04-25 06:49:11 +02:00
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
goto done_processing;
|
|
|
|
}
|
2004-11-28 10:05:49 +01: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);
|
2005-05-03 00:35:18 +02:00
|
|
|
if (onionskin_answer(circ, CELL_CREATED, 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
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
cpuworker_main(void *data)
|
|
|
|
{
|
2005-05-07 07:55:06 +02:00
|
|
|
char question[ONIONSKIN_CHALLENGE_LEN];
|
|
|
|
uint8_t question_type;
|
2003-08-21 01:05:22 +02:00
|
|
|
int *fdarray = data;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* variables for onion processing */
|
2005-05-07 07:55:06 +02:00
|
|
|
char keys[CPATH_KEY_MATERIAL_LEN];
|
|
|
|
char reply_to_proxy[ONIONSKIN_REPLY_LEN];
|
|
|
|
char buf[LEN_ONION_RESPONSE];
|
2003-09-14 04:58:50 +02:00
|
|
|
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
|
|
|
|
|
|
|
fd = fdarray[1]; /* this side is ours */
|
2005-01-03 19:06:51 +01:00
|
|
|
#ifndef TOR_IS_MULTITHREADED
|
2004-12-07 00:19:55 +01:00
|
|
|
tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
|
2005-05-03 05:51:20 +02:00
|
|
|
tor_free_all(1); /* so the child doesn't hold the parent's fd's open */
|
2004-08-08 09:25:45 +02:00
|
|
|
handle_signals(0); /* ignore interrupts from the keyboard, etc */
|
2005-01-03 20:07:25 +01:00
|
|
|
#endif
|
|
|
|
tor_free(data);
|
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
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (;;) {
|
2005-01-19 22:34:42 +01:00
|
|
|
int r;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2005-01-19 22:34:42 +01:00
|
|
|
if ((r = recv(fd, &question_type, 1, 0)) != 1) {
|
2004-03-04 22:26:23 +01:00
|
|
|
// log_fn(LOG_ERR,"read type failed. Exiting.");
|
2005-01-19 22:34:42 +01:00
|
|
|
if (r == 0) {
|
|
|
|
log_fn(LOG_INFO,"CPU worker exiting because Tor process closed connection (either rotated keys or died).");
|
|
|
|
} else {
|
2005-01-20 00:15:59 +01:00
|
|
|
log_fn(LOG_INFO,"CPU worker editing because of error on connection to Tor process.");
|
2005-01-19 22:34:42 +01:00
|
|
|
log_fn(LOG_INFO,"(Error on %d was %s)", fd, tor_socket_strerror(tor_socket_errno(fd)));
|
|
|
|
}
|
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-11-28 10:05:49 +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-11-28 10:05:49 +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
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (question_type == CPUWORKER_TASK_ONION) {
|
|
|
|
if (onion_skin_server_handshake(question, onion_key, last_onion_key,
|
2005-05-03 00:35:18 +02:00
|
|
|
reply_to_proxy, keys, CPATH_KEY_MATERIAL_LEN) < 0) {
|
2003-08-21 01:05:22 +02:00
|
|
|
/* failure */
|
2005-10-17 18:21:42 +02:00
|
|
|
log_fn(LOG_DEBUG,"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);
|
2005-05-03 00:35:18 +02:00
|
|
|
memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,CPATH_KEY_MATERIAL_LEN);
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +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.");
|
2005-04-04 23:46:08 +02:00
|
|
|
goto end;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
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);
|
2005-04-04 23:53:26 +02:00
|
|
|
tor_close_socket(fd);
|
2004-04-25 00:17:50 +02:00
|
|
|
spawn_exit();
|
2003-08-21 01:05:22 +02:00
|
|
|
return 0; /* windows wants this function to return an int */
|
|
|
|
}
|
|
|
|
|
2005-09-23 02:04:44 +02:00
|
|
|
/** Launch a new cpuworker. Return 0 if we're happy, -1 if we failed.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
spawn_cpuworker(void)
|
|
|
|
{
|
2005-01-03 20:07:25 +01:00
|
|
|
int *fdarray;
|
|
|
|
int fd;
|
2003-08-21 01:05:22 +02:00
|
|
|
connection_t *conn;
|
2005-06-30 09:17:38 +02:00
|
|
|
int err;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
fdarray = tor_malloc(sizeof(int)*2);
|
2005-06-30 09:17:38 +02:00
|
|
|
if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
|
2005-09-23 02:04:44 +02:00
|
|
|
log(LOG_WARN, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
|
2005-01-03 20:07:25 +01:00
|
|
|
tor_free(fdarray);
|
2005-09-23 02:04:44 +02:00
|
|
|
return -1;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
fd = fdarray[0];
|
|
|
|
spawn_func(cpuworker_main, (void*)fdarray);
|
2003-08-21 01:05:22 +02:00
|
|
|
log_fn(LOG_DEBUG,"just spawned a worker.");
|
2005-01-03 19:06:51 +01:00
|
|
|
#ifndef TOR_IS_MULTITHREADED
|
2005-01-03 20:07:25 +01:00
|
|
|
tor_close_socket(fdarray[1]); /* we don't need the worker's side of the pipe */
|
|
|
|
tor_free(fdarray);
|
2005-01-03 19:06:51 +01:00
|
|
|
#endif
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
conn = connection_new(CONN_TYPE_CPUWORKER);
|
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
set_socket_nonblocking(fd);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
|
|
|
/* set up conn so it's got all the data we need to remember */
|
2005-01-03 20:07:25 +01:00
|
|
|
conn->s = fd;
|
2003-10-04 05:29:09 +02:00
|
|
|
conn->address = tor_strdup("localhost");
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2004-11-28 10:05:49 +01: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.");
|
2005-01-03 20:07:25 +01:00
|
|
|
connection_free(conn); /* this closes fd */
|
2003-08-21 01:05:22 +02:00
|
|
|
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.
|
|
|
|
*/
|
2005-06-11 20:52:12 +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
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_cpuworkers_needed < MIN_CPUWORKERS)
|
2003-08-21 01:05:22 +02:00
|
|
|
num_cpuworkers_needed = MIN_CPUWORKERS;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_cpuworkers_needed > MAX_CPUWORKERS)
|
2003-08-21 01:05:22 +02:00
|
|
|
num_cpuworkers_needed = MAX_CPUWORKERS;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
while (num_cpuworkers < num_cpuworkers_needed) {
|
|
|
|
if (spawn_cpuworker() < 0) {
|
2005-09-23 02:04:44 +02:00
|
|
|
log_fn(LOG_WARN,"Spawn failed. Will try again later.");
|
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'. */
|
2005-06-11 20:52:12 +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();
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ)
|
2003-09-14 04:58:50 +02:00
|
|
|
return;
|
2004-11-28 10:05:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-04-23 22:58:44 +02:00
|
|
|
#define CPUWORKER_BUSY_TIMEOUT 3600 /* seconds */
|
2005-04-07 22:02:00 +02:00
|
|
|
|
2005-04-07 22:07:34 +02:00
|
|
|
/** We have a bug that I can't find. Sometimes, very rarely, cpuworkers
|
|
|
|
* get stuck in the 'busy' state, even though the cpuworker process
|
|
|
|
* thinks of itself as idle. I don't know why. But here's a workaround
|
2005-04-23 22:58:44 +02:00
|
|
|
* to kill any cpuworker that's been busy for more than 3600 seconds. */
|
2005-04-07 22:02:00 +02:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
cull_wedged_cpuworkers(void)
|
|
|
|
{
|
2005-04-07 22:02:00 +02:00
|
|
|
connection_t **carray;
|
|
|
|
connection_t *conn;
|
|
|
|
int n_conns, i;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
get_connection_array(&carray, &n_conns);
|
|
|
|
for (i = 0; i < n_conns; ++i) {
|
|
|
|
conn = carray[i];
|
|
|
|
if (!conn->marked_for_close &&
|
|
|
|
conn->type == CONN_TYPE_CPUWORKER &&
|
|
|
|
conn->state == CPUWORKER_STATE_BUSY_ONION &&
|
2005-04-08 06:59:34 +02:00
|
|
|
conn->timestamp_lastwritten + CPUWORKER_BUSY_TIMEOUT < now) {
|
2005-04-07 22:02:00 +02:00
|
|
|
log_fn(LOG_NOTICE,"Bug: closing wedged cpuworker. Can somebody find the bug?");
|
|
|
|
num_cpuworkers_busy--;
|
|
|
|
num_cpuworkers--;
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-06 17:19:32 +02:00
|
|
|
/** If cpuworker is defined, assert that he's idle, and use him. Else,
|
|
|
|
* look for an idle cpuworker and use him. If none idle, queue task onto
|
2003-08-21 01:05:22 +02:00
|
|
|
* 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
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
|
|
|
|
void *task)
|
|
|
|
{
|
2003-08-21 01:05:22 +02:00
|
|
|
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
|
|
|
|
2005-04-07 22:02:00 +02:00
|
|
|
cull_wedged_cpuworkers();
|
|
|
|
spawn_enough_cpuworkers();
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (question_type == CPUWORKER_TASK_ONION) {
|
2003-08-21 01:05:22 +02:00
|
|
|
circ = task;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_cpuworkers_busy == num_cpuworkers) {
|
2003-08-21 01:05:22 +02:00
|
|
|
log_fn(LOG_DEBUG,"No idle cpuworkers. Queuing.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (onion_pending_add(circ) < 0)
|
2003-08-21 01:05:22 +02:00
|
|
|
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
|
|
|
|
2004-11-28 10:05:49 +01: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++;
|
|
|
|
|
2005-05-07 07:55:06 +02:00
|
|
|
connection_write_to_buf((char*)&question_type, 1, cpuworker);
|
2003-10-04 04:38:18 +02:00
|
|
|
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
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|