cpuworker.c 11.9 KB
Newer Older
1
/* Copyright 2003-2004 Roger Dingledine. */
Roger Dingledine's avatar
Roger Dingledine committed
2
3
4
/* See LICENSE for licensing information */
/* $Id$ */

Roger Dingledine's avatar
Roger Dingledine committed
5
6
7
/**
 * \file cpuworker.c
 * \brief Run computation-intensive tasks (generally for crypto) in
8
9
10
 * a separate execution context. [OR only.]
 *
 * Right now, we only use this for processing onionskins.
Roger Dingledine's avatar
Roger Dingledine committed
11
 **/
12

Roger Dingledine's avatar
Roger Dingledine committed
13
#include "or.h"
14
extern or_options_t options; /* command-line and config-file options */
Roger Dingledine's avatar
Roger Dingledine committed
15

16
/** The maximum number of cpuworker processes we will keep around. */
Roger Dingledine's avatar
Roger Dingledine committed
17
#define MAX_CPUWORKERS 16
18
/** The minimum number of cpuworker processes we will keep around. */
19
#define MIN_CPUWORKERS 1
Roger Dingledine's avatar
Roger Dingledine committed
20

21
/** The tag specifies which circuit this onionskin was from. */
22
#define TAG_LEN 8
Roger Dingledine's avatar
Roger Dingledine committed
23
/** How many bytes are sent from tor to the cpuworker? */
24
#define LEN_ONION_QUESTION (1+TAG_LEN+ONIONSKIN_CHALLENGE_LEN)
Roger Dingledine's avatar
Roger Dingledine committed
25
/** How many bytes are sent from the cpuworker back to tor? */
26
#define LEN_ONION_RESPONSE (1+TAG_LEN+ONIONSKIN_REPLY_LEN+40+32)
Roger Dingledine's avatar
Roger Dingledine committed
27

28
/** How many cpuworkers we have running right now. */
29
static int num_cpuworkers=0;
30
/** How many of the running cpuworkers have an assigned task right now. */
31
static int num_cpuworkers_busy=0;
Roger Dingledine's avatar
Roger Dingledine committed
32
/** We need to spawn new cpuworkers whenever we rotate the onion keys
33
 * on platforms where execution contexts==processes.  This variable stores
34
 * the last time we got a key rotation event. */
35
static time_t last_rotation_time=0;
Roger Dingledine's avatar
Roger Dingledine committed
36
37
38
39

int cpuworker_main(void *data);
static int spawn_cpuworker(void);
static void spawn_enough_cpuworkers(void);
40
static void process_pending_task(connection_t *cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
41

Roger Dingledine's avatar
Roger Dingledine committed
42
/** Initialize the cpuworker subsystem.
43
 */
Roger Dingledine's avatar
Roger Dingledine committed
44
void cpu_init(void) {
45
  last_rotation_time=time(NULL);
Roger Dingledine's avatar
Roger Dingledine committed
46
47
48
  spawn_enough_cpuworkers();
}

Roger Dingledine's avatar
Roger Dingledine committed
49
/** Called when we're done sending a request to a cpuworker. */
Roger Dingledine's avatar
Roger Dingledine committed
50
int connection_cpu_finished_flushing(connection_t *conn) {
Roger Dingledine's avatar
Roger Dingledine committed
51
  tor_assert(conn && conn->type == CONN_TYPE_CPUWORKER);
Roger Dingledine's avatar
Roger Dingledine committed
52
53
54
55
  connection_stop_writing(conn);
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
56
/** Pack addr,port,and circ_id; set *tag to the result. (See note on
57
 * cpuworker_main for wire format.) */
58
59
static void tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id) {
  *(uint32_t *)tag     = addr;
60
  *(uint16_t *)(tag+4) = port;
61
  *(uint16_t *)(tag+6) = circ_id;
62
63
}

Nick Mathewson's avatar
Nick Mathewson committed
64
/** Unpack <b>tag</b> into addr, port, and circ_id.
65
66
 */
static void tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id) {
67
68
  struct in_addr in;

69
70
71
  *addr    = *(const uint32_t *)tag;
  *port    = *(const uint16_t *)(tag+4);
  *circ_id = *(const uint16_t *)(tag+6);
72
73

  in.s_addr = htonl(*addr);
74
  log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", inet_ntoa(in), *port, *circ_id);
75
76
}

Roger Dingledine's avatar
Roger Dingledine committed
77
/** Called when the onion key has changed and we need to spawn new
78
79
80
 * cpuworkers.  Close all currently idle cpuworkers, and mark the last
 * rotation time as now.
 */
81
82
83
84
85
86
87
88
89
90
91
92
void cpuworkers_rotate(void)
{
  connection_t *cpuworker;
  while ((cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
                                                   CPUWORKER_STATE_IDLE))) {
    connection_mark_for_close(cpuworker,0);
    --num_cpuworkers;
  }
  last_rotation_time = time(NULL);
  spawn_enough_cpuworkers();
}

Roger Dingledine's avatar
Roger Dingledine committed
93
/** Called when we get data from a cpuworker.  If the answer is not complete,
94
95
96
97
 * 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.
 */
Roger Dingledine's avatar
Roger Dingledine committed
98
int connection_cpu_process_inbuf(connection_t *conn) {
99
  char success;
100
101
102
  unsigned char buf[LEN_ONION_RESPONSE];
  uint32_t addr;
  uint16_t port;
103
  uint16_t circ_id;
104
105
  connection_t *p_conn;
  circuit_t *circ;
Roger Dingledine's avatar
Roger Dingledine committed
106

Roger Dingledine's avatar
Roger Dingledine committed
107
  tor_assert(conn && conn->type == CONN_TYPE_CPUWORKER);
Roger Dingledine's avatar
Roger Dingledine committed
108
109

  if(conn->inbuf_reached_eof) {
110
    log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
Roger Dingledine's avatar
Roger Dingledine committed
111
    if(conn->state != CPUWORKER_STATE_IDLE) {
Roger Dingledine's avatar
Roger Dingledine committed
112
113
114
      /* 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. */
115
      log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
Roger Dingledine's avatar
Roger Dingledine committed
116
117
118
      num_cpuworkers_busy--;
    }
    num_cpuworkers--;
Roger Dingledine's avatar
Roger Dingledine committed
119
    spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
120
121
    connection_mark_for_close(conn,0);
    return 0;
Roger Dingledine's avatar
Roger Dingledine committed
122
123
124
  }

  if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
125
    if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
Roger Dingledine's avatar
Roger Dingledine committed
126
      return 0; /* not yet */
Roger Dingledine's avatar
Roger Dingledine committed
127
    tor_assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE);
Roger Dingledine's avatar
Roger Dingledine committed
128

129
130
    connection_fetch_from_buf(&success,1,conn);
    connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);
Roger Dingledine's avatar
Roger Dingledine committed
131

132
    /* parse out the circ it was talking about */
133
    tag_unpack(buf, &addr, &port, &circ_id);
134
135
136
    circ = NULL;
    p_conn = connection_exact_get_by_addr_port(addr,port);
    if(p_conn)
137
      circ = circuit_get_by_circ_id_conn(circ_id, p_conn);
138

139
140
141
142
143
144
    if(success == 0) {
      log_fn(LOG_WARN,"decoding onionskin failed. Closing.");
      if(circ)
        circuit_mark_for_close(circ);
      goto done_processing;
    }
145
    if(!circ) {
146
      log_fn(LOG_INFO,"processed onion for a circ that's gone. Dropping.");
147
      goto done_processing;
Roger Dingledine's avatar
Roger Dingledine committed
148
    }
Roger Dingledine's avatar
Roger Dingledine committed
149
    tor_assert(circ->p_conn);
150
    if(onionskin_answer(circ, buf+TAG_LEN, buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
151
      log_fn(LOG_WARN,"onionskin_answer failed. Closing.");
152
      circuit_mark_for_close(circ);
153
154
155
      goto done_processing;
    }
    log_fn(LOG_DEBUG,"onionskin_answer succeeded. Yay.");
Roger Dingledine's avatar
Roger Dingledine committed
156
  } else {
Roger Dingledine's avatar
Roger Dingledine committed
157
    tor_assert(0); /* don't ask me to do handshakes yet */
Roger Dingledine's avatar
Roger Dingledine committed
158
159
  }

160
done_processing:
Roger Dingledine's avatar
Roger Dingledine committed
161
162
  conn->state = CPUWORKER_STATE_IDLE;
  num_cpuworkers_busy--;
163
164
165
166
167
168
169
  if (conn->timestamp_created < last_rotation_time) {
    connection_mark_for_close(conn,0);
    num_cpuworkers--;
    spawn_enough_cpuworkers();
  } else {
    process_pending_task(conn);
  }
Roger Dingledine's avatar
Roger Dingledine committed
170
171
172
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
173
/** Implement a cpuworker.  'data' is an fdarray as returned by socketpair.
174
175
176
 * Read and writes from fdarray[1].  Reads requests, writes answers.
 *
 *   Request format:
177
 *          Task type           [1 byte, always CPUWORKER_TASK_ONION]
178
179
180
181
182
183
184
185
 *          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
 */
Roger Dingledine's avatar
Roger Dingledine committed
186
int cpuworker_main(void *data) {
187
  unsigned char question[ONIONSKIN_CHALLENGE_LEN];
Roger Dingledine's avatar
Roger Dingledine committed
188
189
190
191
192
  unsigned char question_type;
  int *fdarray = data;
  int fd;

  /* variables for onion processing */
193
  unsigned char keys[40+32];
194
  unsigned char reply_to_proxy[ONIONSKIN_REPLY_LEN];
195
196
  unsigned char buf[LEN_ONION_RESPONSE];
  char tag[TAG_LEN];
197
  crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
198

199
  tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
Roger Dingledine's avatar
Roger Dingledine committed
200
  fd = fdarray[1]; /* this side is ours */
201
#ifndef MS_WINDOWS
202
  connection_free_all(); /* so the child doesn't hold the parent's fd's open */
203
#endif
Roger Dingledine's avatar
Roger Dingledine committed
204

205
206
207
208
209
  /* XXXX WINDOWS lock here. */
  onion_key = crypto_pk_dup_key(get_onion_key());
  if (get_previous_onion_key())
    last_onion_key = crypto_pk_dup_key(get_previous_onion_key());

Roger Dingledine's avatar
Roger Dingledine committed
210
211
  for(;;) {

212
    if(recv(fd, &question_type, 1, 0) != 1) {
213
//      log_fn(LOG_ERR,"read type failed. Exiting.");
214
      log_fn(LOG_INFO,"cpuworker exiting because tor process closed connection (either rotated keys or died).");
215
      goto end;
Roger Dingledine's avatar
Roger Dingledine committed
216
    }
Roger Dingledine's avatar
Roger Dingledine committed
217
    tor_assert(question_type == CPUWORKER_TASK_ONION);
Roger Dingledine's avatar
Roger Dingledine committed
218

219
    if(read_all(fd, tag, TAG_LEN, 1) != TAG_LEN) {
220
      log_fn(LOG_ERR,"read tag failed. Exiting.");
221
      goto end;
222
    }
Roger Dingledine's avatar
Roger Dingledine committed
223

224
    if(read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) != ONIONSKIN_CHALLENGE_LEN) {
225
      log_fn(LOG_ERR,"read question failed. Exiting.");
226
      goto end;
Roger Dingledine's avatar
Roger Dingledine committed
227
228
229
    }

    if(question_type == CPUWORKER_TASK_ONION) {
230
      if(onion_skin_server_handshake(question, onion_key, last_onion_key,
231
        reply_to_proxy, keys, 40+32) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
232
        /* failure */
Roger Dingledine's avatar
Roger Dingledine committed
233
        log_fn(LOG_WARN,"onion_skin_server_handshake failed.");
Roger Dingledine's avatar
Roger Dingledine committed
234
235
236
        memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
      } else {
        /* success */
237
        log_fn(LOG_INFO,"onion_skin_server_handshake succeeded.");
Roger Dingledine's avatar
Roger Dingledine committed
238
        buf[0] = 1; /* 1 means success */
239
        memcpy(buf+1,tag,TAG_LEN);
240
        memcpy(buf+1+TAG_LEN,reply_to_proxy,ONIONSKIN_REPLY_LEN);
241
        memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,40+32);
Roger Dingledine's avatar
Roger Dingledine committed
242
      }
243
      if(write_all(fd, buf, LEN_ONION_RESPONSE, 1) != LEN_ONION_RESPONSE) {
244
        log_fn(LOG_ERR,"writing response buf failed. Exiting.");
Roger Dingledine's avatar
Roger Dingledine committed
245
246
        spawn_exit();
      }
247
      log_fn(LOG_DEBUG,"finished writing response.");
Roger Dingledine's avatar
Roger Dingledine committed
248
249
    }
  }
250
251
252
253
254
255
 end:
  if (onion_key)
    crypto_free_pk_env(onion_key);
  if (last_onion_key)
    crypto_free_pk_env(last_onion_key);
  spawn_exit();
Roger Dingledine's avatar
Roger Dingledine committed
256
257
258
  return 0; /* windows wants this function to return an int */
}

Roger Dingledine's avatar
Roger Dingledine committed
259
/** Launch a new cpuworker.
260
 */
Roger Dingledine's avatar
Roger Dingledine committed
261
262
263
264
265
static int spawn_cpuworker(void) {
  int fd[2];
  connection_t *conn;

  if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
266
267
    log(LOG_ERR, "Couldn't construct socketpair: %s",
        tor_socket_strerror(tor_socket_errno(-1)));
Roger Dingledine's avatar
Roger Dingledine committed
268
269
270
271
272
    exit(1);
  }

  spawn_func(cpuworker_main, (void*)fd);
  log_fn(LOG_DEBUG,"just spawned a worker.");
273
  tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
Roger Dingledine's avatar
Roger Dingledine committed
274
275
276
277
278
279
280

  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];
281
  conn->address = tor_strdup("localhost");
Roger Dingledine's avatar
Roger Dingledine committed
282
283

  if(connection_add(conn) < 0) { /* no space, forget it */
Roger Dingledine's avatar
Roger Dingledine committed
284
    log_fn(LOG_WARN,"connection_add failed. Giving up.");
Roger Dingledine's avatar
Roger Dingledine committed
285
286
287
288
289
290
291
292
293
294
    connection_free(conn); /* this closes fd[0] */
    return -1;
  }

  conn->state = CPUWORKER_STATE_IDLE;
  connection_start_reading(conn);

  return 0; /* success */
}

Roger Dingledine's avatar
Roger Dingledine committed
295
/** If we have too few or too many active cpuworkers, try to spawn new ones
296
297
 * or kill idle ones.
 */
Roger Dingledine's avatar
Roger Dingledine committed
298
static void spawn_enough_cpuworkers(void) {
299
  int num_cpuworkers_needed = options.NumCpus;
Roger Dingledine's avatar
Roger Dingledine committed
300
301
302
303
304
305
306
307

  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) {
Roger Dingledine's avatar
Roger Dingledine committed
308
      log_fn(LOG_WARN,"spawn failed!");
Roger Dingledine's avatar
Roger Dingledine committed
309
310
311
312
313
314
      return;
    }
    num_cpuworkers++;
  }
}

315
/** Take a pending task from the queue and assign it to 'cpuworker'. */
316
static void process_pending_task(connection_t *cpuworker) {
Roger Dingledine's avatar
Roger Dingledine committed
317
318
  circuit_t *circ;

Roger Dingledine's avatar
Roger Dingledine committed
319
  tor_assert(cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
320
321
322
323
324

  /* for now only process onion tasks */

  circ = onion_next_task();
  if(!circ)
325
326
    return;
  if(assign_to_cpuworker(cpuworker, CPUWORKER_TASK_ONION, circ) < 0)
Roger Dingledine's avatar
Roger Dingledine committed
327
    log_fn(LOG_WARN,"assign_to_cpuworker failed. Ignoring.");
Roger Dingledine's avatar
Roger Dingledine committed
328
329
}

Roger Dingledine's avatar
Roger Dingledine committed
330
/** if cpuworker is defined, assert that he's idle, and use him. else,
Roger Dingledine's avatar
Roger Dingledine committed
331
332
 * look for an idle cpuworker and use him. if none idle, queue task onto
 * the pending onion list and return.
Roger Dingledine's avatar
Roger Dingledine committed
333
334
 * If question_type is CPUWORKER_TASK_ONION then task is a circ.
 * No other question_types are allowed.
Roger Dingledine's avatar
Roger Dingledine committed
335
336
337
338
 */
int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
                        void *task) {
  circuit_t *circ;
339
340
  char tag[TAG_LEN];

Roger Dingledine's avatar
Roger Dingledine committed
341
  tor_assert(question_type == CPUWORKER_TASK_ONION);
Roger Dingledine's avatar
Roger Dingledine committed
342
343
344
345
346
347
348
349
350
351
352

  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;
    }

353
    if (!cpuworker)
Roger Dingledine's avatar
Roger Dingledine committed
354
355
      cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER, CPUWORKER_STATE_IDLE);

Roger Dingledine's avatar
Roger Dingledine committed
356
    tor_assert(cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
357

358
    if(!circ->p_conn) {
359
      log_fn(LOG_INFO,"circ->p_conn gone. Failing circ.");
360
361
      return -1;
    }
362
    tag_pack(tag, circ->p_conn->addr, circ->p_conn->port, circ->p_circ_id);
363

Roger Dingledine's avatar
Roger Dingledine committed
364
365
366
    cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
    num_cpuworkers_busy++;

367
368
    connection_write_to_buf(&question_type, 1, cpuworker);
    connection_write_to_buf(tag, sizeof(tag), cpuworker);
369
    connection_write_to_buf(circ->onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
370
  }
Roger Dingledine's avatar
Roger Dingledine committed
371
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
372
373
374
375
376
377
378
379
380
}

/*
  Local Variables:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/