cpuworker.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright 2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /*****
  5. * cpuworker.c: Run computation-intensive tasks (generally for crypto) in
  6. * a separate execution context. [OR only.]
  7. *
  8. * Right now, we only use this for processing onionskins.
  9. *****/
  10. #include "or.h"
  11. extern or_options_t options; /* command-line and config-file options */
  12. #define MAX_CPUWORKERS 16
  13. #define MIN_CPUWORKERS 1
  14. #define TAG_LEN 8
  15. #define LEN_ONION_QUESTION (1+TAG_LEN+ONIONSKIN_CHALLENGE_LEN)
  16. #define LEN_ONION_RESPONSE (1+TAG_LEN+ONIONSKIN_REPLY_LEN+40+32)
  17. static int num_cpuworkers=0;
  18. static int num_cpuworkers_busy=0;
  19. /* We need to spawn new cpuworkers whenever we rotate the onion keys
  20. * on platforms where execution contexts==processes. This variable stores
  21. * the last time we got a key rotation event.*/
  22. static time_t last_rotation_time=0;
  23. int cpuworker_main(void *data);
  24. static int spawn_cpuworker(void);
  25. static void spawn_enough_cpuworkers(void);
  26. static void process_pending_task(connection_t *cpuworker);
  27. /* Initialize the cpuworker subsystem.
  28. */
  29. void cpu_init(void) {
  30. last_rotation_time=time(NULL);
  31. spawn_enough_cpuworkers();
  32. }
  33. /* Called when we're done sending a request to a cpuworker. */
  34. int connection_cpu_finished_flushing(connection_t *conn) {
  35. tor_assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  36. connection_stop_writing(conn);
  37. return 0;
  38. }
  39. /* Pack addr,port,and circ_id; set *tag to the result. (See note on
  40. * cpuworker_main for wire format.) */
  41. static void tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id) {
  42. *(uint32_t *)tag = addr;
  43. *(uint16_t *)(tag+4) = port;
  44. *(uint16_t *)(tag+6) = circ_id;
  45. }
  46. /* Unpack 'tag' into addr, port, and circ_id.
  47. */
  48. static void tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id) {
  49. struct in_addr in;
  50. *addr = *(const uint32_t *)tag;
  51. *port = *(const uint16_t *)(tag+4);
  52. *circ_id = *(const uint16_t *)(tag+6);
  53. in.s_addr = htonl(*addr);
  54. log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", inet_ntoa(in), *port, *circ_id);
  55. }
  56. /* Called when the onion key has changed and we need to spawn new
  57. * cpuworkers. Close all currently idle cpuworkers, and mark the last
  58. * rotation time as now.
  59. */
  60. void cpuworkers_rotate(void)
  61. {
  62. connection_t *cpuworker;
  63. while ((cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER,
  64. CPUWORKER_STATE_IDLE))) {
  65. connection_mark_for_close(cpuworker,0);
  66. --num_cpuworkers;
  67. }
  68. last_rotation_time = time(NULL);
  69. spawn_enough_cpuworkers();
  70. }
  71. /* Called when we get data from a cpuworker. If the answer is not complete,
  72. * wait for a complete answer. If the cpuworker closes the connection,
  73. * mark it as closed and spawn a new one as needed. If the answer is complete,
  74. * process it as appropriate.
  75. */
  76. int connection_cpu_process_inbuf(connection_t *conn) {
  77. char success;
  78. unsigned char buf[LEN_ONION_RESPONSE];
  79. uint32_t addr;
  80. uint16_t port;
  81. uint16_t circ_id;
  82. connection_t *p_conn;
  83. circuit_t *circ;
  84. tor_assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  85. if(conn->inbuf_reached_eof) {
  86. log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
  87. if(conn->state != CPUWORKER_STATE_IDLE) {
  88. /* the circ associated with this cpuworker will have to wait until
  89. * it gets culled in run_connection_housekeeping(), since we have
  90. * no way to find out which circ it was. */
  91. log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
  92. num_cpuworkers_busy--;
  93. }
  94. num_cpuworkers--;
  95. spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
  96. connection_mark_for_close(conn,0);
  97. return 0;
  98. }
  99. if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
  100. if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
  101. return 0; /* not yet */
  102. tor_assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE);
  103. connection_fetch_from_buf(&success,1,conn);
  104. connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);
  105. /* parse out the circ it was talking about */
  106. tag_unpack(buf, &addr, &port, &circ_id);
  107. circ = NULL;
  108. p_conn = connection_exact_get_by_addr_port(addr,port);
  109. if(p_conn)
  110. circ = circuit_get_by_circ_id_conn(circ_id, p_conn);
  111. if(success == 0) {
  112. log_fn(LOG_WARN,"decoding onionskin failed. Closing.");
  113. if(circ)
  114. circuit_mark_for_close(circ);
  115. goto done_processing;
  116. }
  117. if(!circ) {
  118. log_fn(LOG_INFO,"processed onion for a circ that's gone. Dropping.");
  119. goto done_processing;
  120. }
  121. tor_assert(circ->p_conn);
  122. if(onionskin_answer(circ, buf+TAG_LEN, buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
  123. log_fn(LOG_WARN,"onionskin_answer failed. Closing.");
  124. circuit_mark_for_close(circ);
  125. goto done_processing;
  126. }
  127. log_fn(LOG_DEBUG,"onionskin_answer succeeded. Yay.");
  128. } else {
  129. tor_assert(0); /* don't ask me to do handshakes yet */
  130. }
  131. done_processing:
  132. conn->state = CPUWORKER_STATE_IDLE;
  133. num_cpuworkers_busy--;
  134. if (conn->timestamp_created < last_rotation_time) {
  135. connection_mark_for_close(conn,0);
  136. num_cpuworkers--;
  137. spawn_enough_cpuworkers();
  138. } else {
  139. process_pending_task(conn);
  140. }
  141. return 0;
  142. }
  143. /* Implement a cpuworker. 'data' is an fdarray as returned by socketpair.
  144. * Read and writes from fdarray[1]. Reads requests, writes answers.
  145. *
  146. * Request format:
  147. * Task type [1 byte, always CPUWORKER_TASK_ONION]
  148. * Opaque tag TAG_LEN
  149. * Onionskin challenge ONIONSKIN_CHALLENGE_LEN
  150. * Response format:
  151. * Success/failure [1 byte, boolean.]
  152. * Opaque tag TAG_LEN
  153. * Onionskin challenge ONIONSKIN_REPLY_LEN
  154. * Negotiated keys KEY_LEN*2+DIGEST_LEN*2
  155. */
  156. int cpuworker_main(void *data) {
  157. unsigned char question[ONIONSKIN_CHALLENGE_LEN];
  158. unsigned char question_type;
  159. int *fdarray = data;
  160. int fd;
  161. /* variables for onion processing */
  162. unsigned char keys[40+32];
  163. unsigned char reply_to_proxy[ONIONSKIN_REPLY_LEN];
  164. unsigned char buf[LEN_ONION_RESPONSE];
  165. char tag[TAG_LEN];
  166. crypto_pk_env_t *onion_key = NULL, *last_onion_key = NULL;
  167. tor_close_socket(fdarray[0]); /* this is the side of the socketpair the parent uses */
  168. fd = fdarray[1]; /* this side is ours */
  169. #ifndef MS_WINDOWS
  170. connection_free_all(); /* so the child doesn't hold the parent's fd's open */
  171. #endif
  172. /* XXXX WINDOWS lock here. */
  173. onion_key = crypto_pk_dup_key(get_onion_key());
  174. if (get_previous_onion_key())
  175. last_onion_key = crypto_pk_dup_key(get_previous_onion_key());
  176. for(;;) {
  177. if(recv(fd, &question_type, 1, 0) != 1) {
  178. // log_fn(LOG_ERR,"read type failed. Exiting.");
  179. log_fn(LOG_INFO,"cpuworker exiting because tor process died.");
  180. goto end;
  181. }
  182. tor_assert(question_type == CPUWORKER_TASK_ONION);
  183. if(read_all(fd, tag, TAG_LEN, 1) != TAG_LEN) {
  184. log_fn(LOG_ERR,"read tag failed. Exiting.");
  185. goto end;
  186. }
  187. if(read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) != ONIONSKIN_CHALLENGE_LEN) {
  188. log_fn(LOG_ERR,"read question failed. Exiting.");
  189. goto end;
  190. }
  191. if(question_type == CPUWORKER_TASK_ONION) {
  192. if(onion_skin_server_handshake(question, onion_key, last_onion_key,
  193. reply_to_proxy, keys, 40+32) < 0) {
  194. /* failure */
  195. log_fn(LOG_WARN,"onion_skin_server_handshake failed.");
  196. memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
  197. } else {
  198. /* success */
  199. log_fn(LOG_INFO,"onion_skin_server_handshake succeeded.");
  200. buf[0] = 1; /* 1 means success */
  201. memcpy(buf+1,tag,TAG_LEN);
  202. memcpy(buf+1+TAG_LEN,reply_to_proxy,ONIONSKIN_REPLY_LEN);
  203. memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,40+32);
  204. }
  205. if(write_all(fd, buf, LEN_ONION_RESPONSE, 1) != LEN_ONION_RESPONSE) {
  206. log_fn(LOG_ERR,"writing response buf failed. Exiting.");
  207. spawn_exit();
  208. }
  209. log_fn(LOG_DEBUG,"finished writing response.");
  210. }
  211. }
  212. end:
  213. if (onion_key)
  214. crypto_free_pk_env(onion_key);
  215. if (last_onion_key)
  216. crypto_free_pk_env(last_onion_key);
  217. spawn_exit();
  218. return 0; /* windows wants this function to return an int */
  219. }
  220. /* Launch a new cpuworker.
  221. */
  222. static int spawn_cpuworker(void) {
  223. int fd[2];
  224. connection_t *conn;
  225. if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
  226. log(LOG_ERR, "Couldn't construct socketpair: %s",
  227. tor_socket_strerror(tor_socket_errno(-1)));
  228. exit(1);
  229. }
  230. spawn_func(cpuworker_main, (void*)fd);
  231. log_fn(LOG_DEBUG,"just spawned a worker.");
  232. tor_close_socket(fd[1]); /* we don't need the worker's side of the pipe */
  233. conn = connection_new(CONN_TYPE_CPUWORKER);
  234. set_socket_nonblocking(fd[0]);
  235. /* set up conn so it's got all the data we need to remember */
  236. conn->s = fd[0];
  237. conn->address = tor_strdup("localhost");
  238. if(connection_add(conn) < 0) { /* no space, forget it */
  239. log_fn(LOG_WARN,"connection_add failed. Giving up.");
  240. connection_free(conn); /* this closes fd[0] */
  241. return -1;
  242. }
  243. conn->state = CPUWORKER_STATE_IDLE;
  244. connection_start_reading(conn);
  245. return 0; /* success */
  246. }
  247. /* If we have too few or too many active cpuworkers, try to spawn new ones
  248. * or kill idle ones.
  249. */
  250. static void spawn_enough_cpuworkers(void) {
  251. int num_cpuworkers_needed = options.NumCpus;
  252. if(num_cpuworkers_needed < MIN_CPUWORKERS)
  253. num_cpuworkers_needed = MIN_CPUWORKERS;
  254. if(num_cpuworkers_needed > MAX_CPUWORKERS)
  255. num_cpuworkers_needed = MAX_CPUWORKERS;
  256. while(num_cpuworkers < num_cpuworkers_needed) {
  257. if(spawn_cpuworker() < 0) {
  258. log_fn(LOG_WARN,"spawn failed!");
  259. return;
  260. }
  261. num_cpuworkers++;
  262. }
  263. }
  264. /* Take a pending task from the queue and assign it to 'cpuworker' */
  265. static void process_pending_task(connection_t *cpuworker) {
  266. circuit_t *circ;
  267. tor_assert(cpuworker);
  268. /* for now only process onion tasks */
  269. circ = onion_next_task();
  270. if(!circ)
  271. return;
  272. if(assign_to_cpuworker(cpuworker, CPUWORKER_TASK_ONION, circ) < 0)
  273. log_fn(LOG_WARN,"assign_to_cpuworker failed. Ignoring.");
  274. }
  275. /* if cpuworker is defined, assert that he's idle, and use him. else,
  276. * look for an idle cpuworker and use him. if none idle, queue task onto
  277. * the pending onion list and return.
  278. * If question_type is CPUWORKER_TASK_ONION then task is a circ.
  279. * No other question_types are allowed.
  280. */
  281. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  282. void *task) {
  283. circuit_t *circ;
  284. char tag[TAG_LEN];
  285. tor_assert(question_type == CPUWORKER_TASK_ONION);
  286. if(question_type == CPUWORKER_TASK_ONION) {
  287. circ = task;
  288. if(num_cpuworkers_busy == num_cpuworkers) {
  289. log_fn(LOG_DEBUG,"No idle cpuworkers. Queuing.");
  290. if(onion_pending_add(circ) < 0)
  291. return -1;
  292. return 0;
  293. }
  294. if (!cpuworker)
  295. cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER, CPUWORKER_STATE_IDLE);
  296. tor_assert(cpuworker);
  297. if(!circ->p_conn) {
  298. log_fn(LOG_INFO,"circ->p_conn gone. Failing circ.");
  299. return -1;
  300. }
  301. tag_pack(tag, circ->p_conn->addr, circ->p_conn->port, circ->p_circ_id);
  302. cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
  303. num_cpuworkers_busy++;
  304. connection_write_to_buf(&question_type, 1, cpuworker);
  305. connection_write_to_buf(tag, sizeof(tag), cpuworker);
  306. connection_write_to_buf(circ->onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
  307. }
  308. return 0;
  309. }
  310. /*
  311. Local Variables:
  312. mode:c
  313. indent-tabs-mode:nil
  314. c-basic-offset:2
  315. End:
  316. */