cpuworker.c 12 KB

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