2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2010-02-27 23:13:37 +01:00
|
|
|
* Copyright (c) 2007-2010, The Tor Project, Inc. */
|
2003-08-21 01:05:22 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
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"
|
2010-07-22 00:46:18 +02:00
|
|
|
#include "buffers.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.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-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "cpuworker.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.h"
|
2010-07-23 20:38:25 +02:00
|
|
|
#include "onion.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2003-08-21 01:05:22 +02:00
|
|
|
|
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. */
|
2008-08-15 15:55:01 +02:00
|
|
|
#define TAG_LEN 10
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How many bytes are sent from the cpuworker back to tor? */
|
2005-12-14 21:40:40 +01: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
|
|
|
|
2007-05-15 23:17:48 +02:00
|
|
|
static void cpuworker_main(void *data) ATTR_NORETURN;
|
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;
|
|
|
|
}
|
|
|
|
|
2008-08-15 15:55:01 +02:00
|
|
|
/** Pack global_id 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
|
2008-08-15 15:55:01 +02:00
|
|
|
tag_pack(char *tag, uint64_t conn_id, circid_t circ_id)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2008-08-05 22:08:19 +02:00
|
|
|
/*XXXX RETHINK THIS WHOLE MESS !!!! !NM NM NM NM*/
|
2009-03-02 20:15:05 +01:00
|
|
|
set_uint64(tag, conn_id);
|
|
|
|
set_uint16(tag+8, 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
|
2008-08-15 15:55:01 +02:00
|
|
|
tag_unpack(const char *tag, uint64_t *conn_id, circid_t *circ_id)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2009-03-02 20:15:05 +01:00
|
|
|
*conn_id = get_uint64(tag);
|
|
|
|
*circ_id = get_uint16(tag+8);
|
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)
|
|
|
|
{
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_GENERAL,"Read eof. CPU 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. */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_GENERAL,"...and it left a circuit queued; abandoning circ.");
|
2004-11-21 11:14:57 +01:00
|
|
|
num_cpuworkers_busy--;
|
|
|
|
}
|
|
|
|
num_cpuworkers--;
|
2005-12-14 21:40:40 +01:00
|
|
|
spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up
|
|
|
|
spinning. */
|
2004-11-21 11:14:57 +01:00
|
|
|
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];
|
2008-08-15 15:55:01 +02:00
|
|
|
uint64_t conn_id;
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t circ_id;
|
2008-08-15 15:55:01 +02:00
|
|
|
connection_t *tmp_conn;
|
|
|
|
or_connection_t *p_conn = NULL;
|
2003-09-14 04:58:50 +02:00
|
|
|
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) {
|
2005-12-14 21:40:40 +01:00
|
|
|
if (buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* 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 */
|
2008-08-15 15:55:01 +02:00
|
|
|
tag_unpack(buf, &conn_id, &circ_id);
|
2003-09-14 04:58:50 +02:00
|
|
|
circ = NULL;
|
2008-08-15 15:55:01 +02:00
|
|
|
tmp_conn = connection_get_by_global_id(conn_id);
|
|
|
|
if (tmp_conn && !tmp_conn->marked_for_close &&
|
|
|
|
tmp_conn->type == CONN_TYPE_OR)
|
|
|
|
p_conn = TO_OR_CONN(tmp_conn);
|
|
|
|
|
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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,
|
|
|
|
"decoding onionskin failed. "
|
|
|
|
"(Old key or bad software.) Closing.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ)
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
|
2004-04-25 06:49:11 +02:00
|
|
|
goto done_processing;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2005-11-01 07:13:12 +01:00
|
|
|
/* This happens because somebody sends us a destroy cell and the
|
|
|
|
* circuit goes away, while the cpuworker is working. This is also
|
|
|
|
* why our tag doesn't include a pointer to the circ, because we'd
|
|
|
|
* never know if it's still valid.
|
|
|
|
*/
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"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
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(! CIRCUIT_IS_ORIGIN(circ));
|
|
|
|
if (onionskin_answer(TO_OR_CIRCUIT(circ), CELL_CREATED, buf+TAG_LEN,
|
2005-12-14 21:40:40 +01:00
|
|
|
buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_OR,"onionskin_answer failed. Closing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
|
2003-09-14 04:58:50 +02:00
|
|
|
goto done_processing;
|
|
|
|
}
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"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
|
|
|
}
|
|
|
|
|
2010-08-16 00:29:27 +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
|
|
|
*/
|
2006-09-06 10:42:20 +02:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
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
|
2005-12-14 21:40:40 +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 (;;) {
|
2008-02-22 20:09:45 +01:00
|
|
|
ssize_t 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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"CPU worker exiting because Tor process closed connection "
|
|
|
|
"(either rotated keys or died).");
|
2005-01-19 22:34:42 +01:00
|
|
|
} else {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_OR,
|
2006-02-15 04:47:38 +01:00
|
|
|
"CPU worker exiting because of error on connection to Tor "
|
2006-02-13 10:02:35 +01:00
|
|
|
"process.");
|
|
|
|
log_info(LD_OR,"(Error on %d was %s)",
|
|
|
|
fd, tor_socket_strerror(tor_socket_errno(fd)));
|
2005-01-19 22:34:42 +01:00
|
|
|
}
|
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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_err(LD_BUG,"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
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
if (read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) !=
|
|
|
|
ONIONSKIN_CHALLENGE_LEN) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_err(LD_BUG,"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 */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"onion_skin_server_handshake failed.");
|
2009-01-06 20:03:15 +01:00
|
|
|
*buf = 0; /* indicate failure in first byte */
|
|
|
|
memcpy(buf+1,tag,TAG_LEN);
|
|
|
|
/* send all zeros as answer */
|
|
|
|
memset(buf+1+TAG_LEN, 0, LEN_ONION_RESPONSE-(1+TAG_LEN));
|
2003-08-21 01:05:22 +02:00
|
|
|
} else {
|
|
|
|
/* success */
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_err(LD_BUG,"writing response buf failed. Exiting.");
|
2005-04-04 23:46:08 +02:00
|
|
|
goto end;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"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);
|
2005-10-25 21:01:48 +02:00
|
|
|
crypto_thread_cleanup();
|
2004-04-25 00:17:50 +02:00
|
|
|
spawn_exit();
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_NET, "Couldn't construct socketpair for cpuworker: %s",
|
2006-02-13 10:02:35 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-06-06 02:06:52 +02:00
|
|
|
tor_assert(fdarray[0] >= 0);
|
|
|
|
tor_assert(fdarray[1] >= 0);
|
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
fd = fdarray[0];
|
2006-09-06 10:42:20 +02:00
|
|
|
spawn_func(cpuworker_main, (void*)fdarray);
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"just spawned a cpu worker.");
|
2005-01-03 19:06:51 +01:00
|
|
|
#ifndef TOR_IS_MULTITHREADED
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_close_socket(fdarray[1]); /* don't need the worker's side of the pipe */
|
2005-01-03 20:07:25 +01:00
|
|
|
tor_free(fdarray);
|
2005-01-03 19:06:51 +01:00
|
|
|
#endif
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2007-06-05 22:54:49 +02:00
|
|
|
conn = connection_new(CONN_TYPE_CPUWORKER, AF_UNIX);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
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 */
|
2006-09-30 00:33:34 +02:00
|
|
|
log_warn(LD_NET,"connection_add for cpuworker 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)
|
|
|
|
{
|
2010-10-29 19:41:24 +02: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) {
|
2006-09-30 00:33:34 +02:00
|
|
|
log_warn(LD_GENERAL,"Cpuworker 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)
|
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *circ;
|
2008-02-06 00:20:49 +01:00
|
|
|
char *onionskin = NULL;
|
2003-08-21 01:05:22 +02:00
|
|
|
|
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 */
|
|
|
|
|
2008-02-06 00:20:49 +01:00
|
|
|
circ = onion_next_task(&onionskin);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ)
|
2003-09-14 04:58:50 +02:00
|
|
|
return;
|
2008-02-06 00:20:49 +01:00
|
|
|
if (assign_onionskin_to_cpuworker(cpuworker, circ, onionskin))
|
2006-02-13 10:02:35 +01:00
|
|
|
log_warn(LD_OR,"assign_to_cpuworker failed. Ignoring.");
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
|
2006-04-18 21:48:06 +02:00
|
|
|
/** How long should we let a cpuworker stay busy before we give
|
|
|
|
* up on it and decide that we have a bug or infinite loop?
|
|
|
|
* This value is high because some servers with low memory/cpu
|
|
|
|
* sometimes spend an hour or more swapping, and Tor starves. */
|
|
|
|
#define CPUWORKER_BUSY_TIMEOUT (60*60*12)
|
2005-04-07 22:02:00 +02:00
|
|
|
|
2006-03-12 23:48:18 +01: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 to kill any
|
|
|
|
* cpuworker that's been busy for more than CPUWORKER_BUSY_TIMEOUT.
|
|
|
|
*/
|
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
|
|
|
time_t now = time(NULL);
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, conn,
|
|
|
|
{
|
2005-04-07 22:02:00 +02:00
|
|
|
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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_notice(LD_BUG,
|
2007-03-04 21:11:46 +01:00
|
|
|
"closing wedged cpuworker. Can somebody find the bug?");
|
2005-04-07 22:02:00 +02:00
|
|
|
num_cpuworkers_busy--;
|
|
|
|
num_cpuworkers--;
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2005-04-07 22:02:00 +02:00
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Try to tell a cpuworker to perform the public key operations necessary to
|
|
|
|
* respond to <b>onionskin</b> for the circuit <b>circ</b>.
|
|
|
|
*
|
|
|
|
* If <b>cpuworker</b> 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 the
|
|
|
|
* pending onion list and return. Return 0 if we successfully assign the
|
|
|
|
* task, or -1 on failure.
|
2003-08-21 01:05:22 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2008-02-06 00:20:49 +01:00
|
|
|
assign_onionskin_to_cpuworker(connection_t *cpuworker,
|
|
|
|
or_circuit_t *circ, char *onionskin)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2008-02-06 00:20:49 +01:00
|
|
|
char qbuf[1];
|
2003-09-14 04:58:50 +02:00
|
|
|
char tag[TAG_LEN];
|
|
|
|
|
2005-04-07 22:02:00 +02:00
|
|
|
cull_wedged_cpuworkers();
|
|
|
|
spawn_enough_cpuworkers();
|
|
|
|
|
2008-02-06 00:20:49 +01:00
|
|
|
if (1) {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_cpuworkers_busy == num_cpuworkers) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_debug(LD_OR,"No idle cpuworkers. Queuing.");
|
2009-07-28 04:38:09 +02:00
|
|
|
if (onion_pending_add(circ, onionskin) < 0) {
|
|
|
|
tor_free(onionskin);
|
2003-08-21 01:05:22 +02:00
|
|
|
return -1;
|
2009-07-28 04:38:09 +02:00
|
|
|
}
|
2003-08-21 01:05:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 00:17:50 +02:00
|
|
|
if (!cpuworker)
|
2005-12-14 21:40:40 +01:00
|
|
|
cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
|
|
|
|
CPUWORKER_STATE_IDLE);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
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) {
|
2006-02-13 10:02:35 +01:00
|
|
|
log_info(LD_OR,"circ->p_conn gone. Failing circ.");
|
2008-02-06 00:20:49 +01:00
|
|
|
tor_free(onionskin);
|
2003-09-14 04:58:50 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-08-15 15:55:01 +02:00
|
|
|
tag_pack(tag, circ->p_conn->_base.global_identifier,
|
2006-07-26 21:07:26 +02:00
|
|
|
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;
|
2006-06-12 13:59:19 +02:00
|
|
|
/* touch the lastwritten timestamp, since that's how we check to
|
|
|
|
* see how long it's been since we asked the question, and sometimes
|
|
|
|
* we check before the first call to connection_handle_write(). */
|
|
|
|
cpuworker->timestamp_lastwritten = time(NULL);
|
2003-08-21 01:05:22 +02:00
|
|
|
num_cpuworkers_busy++;
|
|
|
|
|
2008-02-06 00:20:49 +01:00
|
|
|
qbuf[0] = CPUWORKER_TASK_ONION;
|
|
|
|
connection_write_to_buf(qbuf, 1, cpuworker);
|
2003-10-04 04:38:18 +02:00
|
|
|
connection_write_to_buf(tag, sizeof(tag), cpuworker);
|
2008-02-06 00:20:49 +01:00
|
|
|
connection_write_to_buf(onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
|
|
|
|
tor_free(onionskin);
|
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
|
|
|
|