cpuworker.c 15.1 KB
Newer Older
1
2
/* Copyright (c) 2003-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3
 * Copyright (c) 2007-2008, The Tor Project, Inc. */
Roger Dingledine's avatar
Roger Dingledine committed
4
5
/* See LICENSE for licensing information */
/* $Id$ */
6
7
const char cpuworker_c_id[] =
  "$Id$";
Roger Dingledine's avatar
Roger Dingledine committed
8

Roger Dingledine's avatar
Roger Dingledine committed
9
10
/**
 * \file cpuworker.c
11
12
13
 * \brief Implements a farm of 'CPU worker' processes to perform
 * CPU-intensive tasks in another thread or process, to not
 * interrupt the main thread.
14
15
 *
 * Right now, we only use this for processing onionskins.
Roger Dingledine's avatar
Roger Dingledine committed
16
 **/
17

Roger Dingledine's avatar
Roger Dingledine committed
18
19
#include "or.h"

20
/** The maximum number of cpuworker processes we will keep around. */
Roger Dingledine's avatar
Roger Dingledine committed
21
#define MAX_CPUWORKERS 16
22
/** The minimum number of cpuworker processes we will keep around. */
23
#define MIN_CPUWORKERS 1
Roger Dingledine's avatar
Roger Dingledine committed
24

25
/** The tag specifies which circuit this onionskin was from. */
26
#define TAG_LEN 8
Roger Dingledine's avatar
Roger Dingledine committed
27
/** How many bytes are sent from the cpuworker back to tor? */
28
29
#define LEN_ONION_RESPONSE \
  (1+TAG_LEN+ONIONSKIN_REPLY_LEN+CPATH_KEY_MATERIAL_LEN)
Roger Dingledine's avatar
Roger Dingledine committed
30

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

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

Roger Dingledine's avatar
Roger Dingledine committed
45
/** Initialize the cpuworker subsystem.
46
 */
47
48
49
void
cpu_init(void)
{
50
  cpuworkers_rotate();
Roger Dingledine's avatar
Roger Dingledine committed
51
52
}

Roger Dingledine's avatar
Roger Dingledine committed
53
/** Called when we're done sending a request to a cpuworker. */
54
55
56
int
connection_cpu_finished_flushing(connection_t *conn)
{
57
58
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CPUWORKER);
Roger Dingledine's avatar
Roger Dingledine committed
59
60
61
62
  connection_stop_writing(conn);
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
63
/** Pack addr,port,and circ_id; set *tag to the result. (See note on
64
 * cpuworker_main for wire format.) */
65
66
67
static void
tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id)
{
68
69
70
  *(uint32_t *)tag     = addr;
  *(uint16_t *)(tag+4) = port;
  *(uint16_t *)(tag+6) = circ_id;
71
72
}

Nick Mathewson's avatar
Nick Mathewson committed
73
/** Unpack <b>tag</b> into addr, port, and circ_id.
74
 */
75
76
77
static void
tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id)
{
78
  struct in_addr in;
79
  char addrbuf[INET_NTOA_BUF_LEN];
80
81
82
83
84
85

  *addr    = *(const uint32_t *)tag;
  *port    = *(const uint16_t *)(tag+4);
  *circ_id = *(const uint16_t *)(tag+6);

  in.s_addr = htonl(*addr);
86
  tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
Roger Dingledine's avatar
Roger Dingledine committed
87
88
  log_debug(LD_OR,
            "onion was from %s:%d, circ_id %d.", addrbuf, *port, *circ_id);
89
90
}

Roger Dingledine's avatar
Roger Dingledine committed
91
/** Called when the onion key has changed and we need to spawn new
92
93
94
 * cpuworkers.  Close all currently idle cpuworkers, and mark the last
 * rotation time as now.
 */
95
96
void
cpuworkers_rotate(void)
97
98
99
100
{
  connection_t *cpuworker;
  while ((cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
                                                   CPUWORKER_STATE_IDLE))) {
101
    connection_mark_for_close(cpuworker);
102
103
104
    --num_cpuworkers;
  }
  last_rotation_time = time(NULL);
105
106
  if (server_mode(get_options()))
    spawn_enough_cpuworkers();
107
108
}

109
110
/** If the cpuworker closes the connection,
 * mark it as closed and spawn a new one as needed. */
111
112
113
int
connection_cpu_reached_eof(connection_t *conn)
{
114
  log_warn(LD_GENERAL,"Read eof. CPU worker died unexpectedly.");
115
  if (conn->state != CPUWORKER_STATE_IDLE) {
116
117
118
    /* 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. */
Roger Dingledine's avatar
Roger Dingledine committed
119
    log_warn(LD_GENERAL,"...and it left a circuit queued; abandoning circ.");
120
121
122
    num_cpuworkers_busy--;
  }
  num_cpuworkers--;
123
124
  spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up
                                spinning. */
125
126
127
128
  connection_mark_for_close(conn);
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
129
/** Called when we get data from a cpuworker.  If the answer is not complete,
130
 * wait for a complete answer. If the answer is complete,
131
132
 * process it as appropriate.
 */
133
134
135
int
connection_cpu_process_inbuf(connection_t *conn)
{
136
  char success;
137
  char buf[LEN_ONION_RESPONSE];
138
139
  uint32_t addr;
  uint16_t port;
140
  uint16_t circ_id;
141
  or_connection_t *p_conn;
142
  circuit_t *circ;
Roger Dingledine's avatar
Roger Dingledine committed
143

144
145
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CPUWORKER);
Roger Dingledine's avatar
Roger Dingledine committed
146

147
148
149
  if (!buf_datalen(conn->inbuf))
    return 0;

150
  if (conn->state == CPUWORKER_STATE_BUSY_ONION) {
151
    if (buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* answer available? */
Roger Dingledine's avatar
Roger Dingledine committed
152
      return 0; /* not yet */
Roger Dingledine's avatar
Roger Dingledine committed
153
    tor_assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE);
Roger Dingledine's avatar
Roger Dingledine committed
154

155
156
    connection_fetch_from_buf(&success,1,conn);
    connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);
Roger Dingledine's avatar
Roger Dingledine committed
157

158
    /* parse out the circ it was talking about */
159
    tag_unpack(buf, &addr, &port, &circ_id);
160
    circ = NULL;
161
    /* (Here we use connection_or_exact_get_by_addr_port rather than
Roger Dingledine's avatar
Roger Dingledine committed
162
     * get_by_identity_digest: we want a specific port here in
163
     * case there are multiple connections.) */
164
    p_conn = connection_or_exact_get_by_addr_port(addr,port);
165
    if (p_conn)
166
      circ = circuit_get_by_circid_orconn(circ_id, p_conn);
167

168
    if (success == 0) {
Roger Dingledine's avatar
Roger Dingledine committed
169
170
171
      log_debug(LD_OR,
                "decoding onionskin failed. "
                "(Old key or bad software.) Closing.");
172
      if (circ)
173
        circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
174
175
      goto done_processing;
    }
176
    if (!circ) {
177
178
179
180
181
      /* 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.
       */
Roger Dingledine's avatar
Roger Dingledine committed
182
      log_debug(LD_OR,"processed onion for a circ that's gone. Dropping.");
183
      goto done_processing;
Roger Dingledine's avatar
Roger Dingledine committed
184
    }
185
186
    tor_assert(! CIRCUIT_IS_ORIGIN(circ));
    if (onionskin_answer(TO_OR_CIRCUIT(circ), CELL_CREATED, buf+TAG_LEN,
187
                         buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
188
      log_warn(LD_OR,"onionskin_answer failed. Closing.");
189
      circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
190
191
      goto done_processing;
    }
Roger Dingledine's avatar
Roger Dingledine committed
192
    log_debug(LD_OR,"onionskin_answer succeeded. Yay.");
Roger Dingledine's avatar
Roger Dingledine committed
193
  } else {
Roger Dingledine's avatar
Roger Dingledine committed
194
    tor_assert(0); /* don't ask me to do handshakes yet */
Roger Dingledine's avatar
Roger Dingledine committed
195
196
  }

197
done_processing:
Roger Dingledine's avatar
Roger Dingledine committed
198
199
  conn->state = CPUWORKER_STATE_IDLE;
  num_cpuworkers_busy--;
200
  if (conn->timestamp_created < last_rotation_time) {
201
    connection_mark_for_close(conn);
202
203
204
205
206
    num_cpuworkers--;
    spawn_enough_cpuworkers();
  } else {
    process_pending_task(conn);
  }
Roger Dingledine's avatar
Roger Dingledine committed
207
208
209
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
210
/** Implement a cpuworker.  'data' is an fdarray as returned by socketpair.
211
212
213
 * Read and writes from fdarray[1].  Reads requests, writes answers.
 *
 *   Request format:
214
 *          Task type           [1 byte, always CPUWORKER_TASK_ONION]
215
216
217
218
219
220
221
 *          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
222
223
 *
 *  (Note: this _should_ be by addr/port, since we're concerned with specific
224
 * connections, not with routers (where we'd use identity).)
225
 */
226
static void
227
228
cpuworker_main(void *data)
{
229
230
  char question[ONIONSKIN_CHALLENGE_LEN];
  uint8_t question_type;
Roger Dingledine's avatar
Roger Dingledine committed
231
232
233
234
  int *fdarray = data;
  int fd;

  /* variables for onion processing */
235
236
237
  char keys[CPATH_KEY_MATERIAL_LEN];
  char reply_to_proxy[ONIONSKIN_REPLY_LEN];
  char buf[LEN_ONION_RESPONSE];
238
  char tag[TAG_LEN];
239
  crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
240
241

  fd = fdarray[1]; /* this side is ours */
242
#ifndef TOR_IS_MULTITHREADED
243
244
  tor_close_socket(fdarray[0]); /* this is the side of the socketpair the
                                 * parent uses */
245
  tor_free_all(1); /* so the child doesn't hold the parent's fd's open */
246
  handle_signals(0); /* ignore interrupts from the keyboard, etc */
247
248
#endif
  tor_free(data);
Roger Dingledine's avatar
Roger Dingledine committed
249

250
  dup_onion_keys(&onion_key, &last_onion_key);
251

252
  for (;;) {
253
    ssize_t r;
Roger Dingledine's avatar
Roger Dingledine committed
254

255
    if ((r = recv(fd, &question_type, 1, 0)) != 1) {
256
//      log_fn(LOG_ERR,"read type failed. Exiting.");
257
      if (r == 0) {
Roger Dingledine's avatar
Roger Dingledine committed
258
259
260
        log_info(LD_OR,
                 "CPU worker exiting because Tor process closed connection "
                 "(either rotated keys or died).");
261
      } else {
Roger Dingledine's avatar
Roger Dingledine committed
262
        log_info(LD_OR,
263
                 "CPU worker exiting because of error on connection to Tor "
Roger Dingledine's avatar
Roger Dingledine committed
264
265
266
                 "process.");
        log_info(LD_OR,"(Error on %d was %s)",
                 fd, tor_socket_strerror(tor_socket_errno(fd)));
267
      }
268
      goto end;
Roger Dingledine's avatar
Roger Dingledine committed
269
    }
Roger Dingledine's avatar
Roger Dingledine committed
270
    tor_assert(question_type == CPUWORKER_TASK_ONION);
Roger Dingledine's avatar
Roger Dingledine committed
271

272
    if (read_all(fd, tag, TAG_LEN, 1) != TAG_LEN) {
Roger Dingledine's avatar
Roger Dingledine committed
273
      log_err(LD_BUG,"read tag failed. Exiting.");
274
      goto end;
275
    }
Roger Dingledine's avatar
Roger Dingledine committed
276

277
278
    if (read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) !=
        ONIONSKIN_CHALLENGE_LEN) {
Roger Dingledine's avatar
Roger Dingledine committed
279
      log_err(LD_BUG,"read question failed. Exiting.");
280
      goto end;
Roger Dingledine's avatar
Roger Dingledine committed
281
282
    }

283
284
    if (question_type == CPUWORKER_TASK_ONION) {
      if (onion_skin_server_handshake(question, onion_key, last_onion_key,
285
          reply_to_proxy, keys, CPATH_KEY_MATERIAL_LEN) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
286
        /* failure */
Roger Dingledine's avatar
Roger Dingledine committed
287
        log_debug(LD_OR,"onion_skin_server_handshake failed.");
Roger Dingledine's avatar
Roger Dingledine committed
288
289
290
        memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
      } else {
        /* success */
Roger Dingledine's avatar
Roger Dingledine committed
291
        log_debug(LD_OR,"onion_skin_server_handshake succeeded.");
Roger Dingledine's avatar
Roger Dingledine committed
292
        buf[0] = 1; /* 1 means success */
293
        memcpy(buf+1,tag,TAG_LEN);
294
        memcpy(buf+1+TAG_LEN,reply_to_proxy,ONIONSKIN_REPLY_LEN);
295
        memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,CPATH_KEY_MATERIAL_LEN);
Roger Dingledine's avatar
Roger Dingledine committed
296
      }
297
      if (write_all(fd, buf, LEN_ONION_RESPONSE, 1) != LEN_ONION_RESPONSE) {
Roger Dingledine's avatar
Roger Dingledine committed
298
        log_err(LD_BUG,"writing response buf failed. Exiting.");
299
        goto end;
Roger Dingledine's avatar
Roger Dingledine committed
300
      }
Roger Dingledine's avatar
Roger Dingledine committed
301
      log_debug(LD_OR,"finished writing response.");
Roger Dingledine's avatar
Roger Dingledine committed
302
303
    }
  }
304
305
306
307
308
 end:
  if (onion_key)
    crypto_free_pk_env(onion_key);
  if (last_onion_key)
    crypto_free_pk_env(last_onion_key);
309
  tor_close_socket(fd);
310
  crypto_thread_cleanup();
311
  spawn_exit();
Roger Dingledine's avatar
Roger Dingledine committed
312
313
}

314
/** Launch a new cpuworker. Return 0 if we're happy, -1 if we failed.
315
 */
316
317
318
static int
spawn_cpuworker(void)
{
319
320
  int *fdarray;
  int fd;
Roger Dingledine's avatar
Roger Dingledine committed
321
  connection_t *conn;
322
  int err;
Roger Dingledine's avatar
Roger Dingledine committed
323

324
  fdarray = tor_malloc(sizeof(int)*2);
325
  if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
326
    log_warn(LD_NET, "Couldn't construct socketpair for cpuworker: %s",
Roger Dingledine's avatar
Roger Dingledine committed
327
             tor_socket_strerror(-err));
328
    tor_free(fdarray);
329
    return -1;
Roger Dingledine's avatar
Roger Dingledine committed
330
331
  }

332
333
334
  tor_assert(fdarray[0] >= 0);
  tor_assert(fdarray[1] >= 0);

335
  fd = fdarray[0];
336
  spawn_func(cpuworker_main, (void*)fdarray);
Roger Dingledine's avatar
Roger Dingledine committed
337
  log_debug(LD_OR,"just spawned a cpu worker.");
338
#ifndef TOR_IS_MULTITHREADED
339
  tor_close_socket(fdarray[1]); /* don't need the worker's side of the pipe */
340
  tor_free(fdarray);
341
#endif
Roger Dingledine's avatar
Roger Dingledine committed
342

343
  conn = connection_new(CONN_TYPE_CPUWORKER, AF_UNIX);
Roger Dingledine's avatar
Roger Dingledine committed
344

345
  set_socket_nonblocking(fd);
Roger Dingledine's avatar
Roger Dingledine committed
346
347

  /* set up conn so it's got all the data we need to remember */
348
  conn->s = fd;
349
  conn->address = tor_strdup("localhost");
Roger Dingledine's avatar
Roger Dingledine committed
350

351
  if (connection_add(conn) < 0) { /* no space, forget it */
352
    log_warn(LD_NET,"connection_add for cpuworker failed. Giving up.");
353
    connection_free(conn); /* this closes fd */
Roger Dingledine's avatar
Roger Dingledine committed
354
355
356
357
358
359
360
361
362
    return -1;
  }

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

  return 0; /* success */
}

Roger Dingledine's avatar
Roger Dingledine committed
363
/** If we have too few or too many active cpuworkers, try to spawn new ones
364
365
 * or kill idle ones.
 */
366
367
368
static void
spawn_enough_cpuworkers(void)
{
369
  int num_cpuworkers_needed = get_options()->NumCpus;
Roger Dingledine's avatar
Roger Dingledine committed
370

371
  if (num_cpuworkers_needed < MIN_CPUWORKERS)
Roger Dingledine's avatar
Roger Dingledine committed
372
    num_cpuworkers_needed = MIN_CPUWORKERS;
373
  if (num_cpuworkers_needed > MAX_CPUWORKERS)
Roger Dingledine's avatar
Roger Dingledine committed
374
375
    num_cpuworkers_needed = MAX_CPUWORKERS;

376
377
  while (num_cpuworkers < num_cpuworkers_needed) {
    if (spawn_cpuworker() < 0) {
378
      log_warn(LD_GENERAL,"Cpuworker spawn failed. Will try again later.");
Roger Dingledine's avatar
Roger Dingledine committed
379
380
381
382
383
384
      return;
    }
    num_cpuworkers++;
  }
}

385
/** Take a pending task from the queue and assign it to 'cpuworker'. */
386
387
388
static void
process_pending_task(connection_t *cpuworker)
{
389
  or_circuit_t *circ;
390
  char *onionskin = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
391

Roger Dingledine's avatar
Roger Dingledine committed
392
  tor_assert(cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
393
394
395

  /* for now only process onion tasks */

396
  circ = onion_next_task(&onionskin);
397
  if (!circ)
398
    return;
399
  if (assign_onionskin_to_cpuworker(cpuworker, circ, onionskin))
Roger Dingledine's avatar
Roger Dingledine committed
400
    log_warn(LD_OR,"assign_to_cpuworker failed. Ignoring.");
Roger Dingledine's avatar
Roger Dingledine committed
401
402
}

403
404
405
406
407
/** 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)
408

409
410
411
412
413
/** 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.
 */
414
static void
415
416
cull_wedged_cpuworkers(void)
{
417
  time_t now = time(NULL);
418
419
420
  smartlist_t *conns = get_connection_array();
  SMARTLIST_FOREACH(conns, connection_t *, conn,
  {
421
422
423
    if (!conn->marked_for_close &&
        conn->type == CONN_TYPE_CPUWORKER &&
        conn->state == CPUWORKER_STATE_BUSY_ONION &&
424
        conn->timestamp_lastwritten + CPUWORKER_BUSY_TIMEOUT < now) {
Roger Dingledine's avatar
Roger Dingledine committed
425
      log_notice(LD_BUG,
426
                 "closing wedged cpuworker. Can somebody find the bug?");
427
428
429
430
      num_cpuworkers_busy--;
      num_cpuworkers--;
      connection_mark_for_close(conn);
    }
431
  });
432
433
}

434
435
436
437
438
439
440
/** 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.
Roger Dingledine's avatar
Roger Dingledine committed
441
 */
442
int
443
444
assign_onionskin_to_cpuworker(connection_t *cpuworker,
                              or_circuit_t *circ, char *onionskin)
445
{
446
  char qbuf[1];
447
448
  char tag[TAG_LEN];

449
450
451
  cull_wedged_cpuworkers();
  spawn_enough_cpuworkers();

452
  if (1) {
453
    if (num_cpuworkers_busy == num_cpuworkers) {
Roger Dingledine's avatar
Roger Dingledine committed
454
      log_debug(LD_OR,"No idle cpuworkers. Queuing.");
455
      if (onion_pending_add(circ, onionskin) < 0)
Roger Dingledine's avatar
Roger Dingledine committed
456
457
458
459
        return -1;
      return 0;
    }

460
    if (!cpuworker)
461
462
      cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
                                               CPUWORKER_STATE_IDLE);
Roger Dingledine's avatar
Roger Dingledine committed
463

Roger Dingledine's avatar
Roger Dingledine committed
464
    tor_assert(cpuworker);
Roger Dingledine's avatar
Roger Dingledine committed
465

466
    if (!circ->p_conn) {
Roger Dingledine's avatar
Roger Dingledine committed
467
      log_info(LD_OR,"circ->p_conn gone. Failing circ.");
468
      tor_free(onionskin);
469
470
      return -1;
    }
471
472
    tag_pack(tag, circ->p_conn->_base.addr, circ->p_conn->_base.port,
             circ->p_circ_id);
473

Roger Dingledine's avatar
Roger Dingledine committed
474
    cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
475
476
477
478
    /* 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);
Roger Dingledine's avatar
Roger Dingledine committed
479
480
    num_cpuworkers_busy++;

481
482
    qbuf[0] = CPUWORKER_TASK_ONION;
    connection_write_to_buf(qbuf, 1, cpuworker);
483
    connection_write_to_buf(tag, sizeof(tag), cpuworker);
484
485
    connection_write_to_buf(onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
    tor_free(onionskin);
Roger Dingledine's avatar
Roger Dingledine committed
486
  }
Roger Dingledine's avatar
Roger Dingledine committed
487
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
488
}
489