cpuworker.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright 2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. #define MAX_QUESTIONLEN 256
  7. #define MAX_CPUWORKERS 17
  8. #define MIN_CPUWORKERS 2
  9. #define LEN_ONION_RESPONSE (1+DH_KEY_LEN+32)
  10. #define LEN_HANDSHAKE_RESPONSE (somethingelse)
  11. int num_cpuworkers=0;
  12. int num_cpuworkers_busy=0;
  13. int cpuworker_main(void *data);
  14. static int spawn_cpuworker(void);
  15. static void spawn_enough_cpuworkers(void);
  16. static int process_pending_task(connection_t *cpuworker);
  17. void cpu_init(void) {
  18. spawn_enough_cpuworkers();
  19. }
  20. int connection_cpu_finished_flushing(connection_t *conn) {
  21. assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  22. connection_stop_writing(conn);
  23. return 0;
  24. }
  25. int connection_cpu_process_inbuf(connection_t *conn) {
  26. unsigned char buf[MAX_QUESTIONLEN];
  27. assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  28. if(conn->inbuf_reached_eof) {
  29. log_fn(LOG_ERR,"Read eof. Worker dying.");
  30. if(conn->state != CPUWORKER_STATE_IDLE) {
  31. circuit_close(conn->circ);
  32. conn->circ = NULL;
  33. num_cpuworkers_busy--;
  34. }
  35. num_cpuworkers--;
  36. return -1;
  37. }
  38. if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
  39. assert(conn->circ);
  40. if(conn->inbuf_datalen < LEN_ONION_RESPONSE) /* entire answer available? */
  41. return 0; /* not yet */
  42. assert(conn->inbuf_datalen == LEN_ONION_RESPONSE);
  43. connection_fetch_from_buf(buf,LEN_ONION_RESPONSE,conn);
  44. /* XXX conn->circ might already have been closed. Serious bug. Suck. */
  45. if(*buf == 0 || conn->circ->p_conn == NULL ||
  46. onionskin_process(conn->circ, buf+1, buf+1+DH_KEY_LEN) < 0) {
  47. log_fn(LOG_DEBUG,"decoding onion, onionskin_process, or p_conn failed. Closing.");
  48. circuit_close(conn->circ);
  49. } else {
  50. log_fn(LOG_DEBUG,"onionskin_process succeeded. Yay.");
  51. }
  52. conn->circ = NULL;
  53. } else {
  54. assert(conn->state == CPUWORKER_STATE_BUSY_HANDSHAKE);
  55. assert(0); /* don't ask me to do handshakes yet */
  56. }
  57. conn->state = CPUWORKER_STATE_IDLE;
  58. num_cpuworkers_busy--;
  59. process_pending_task(conn); /* discard return value */
  60. return 0;
  61. }
  62. int cpuworker_main(void *data) {
  63. unsigned char question[MAX_QUESTIONLEN];
  64. unsigned char question_type;
  65. int *fdarray = data;
  66. int fd;
  67. int len;
  68. /* variables for onion processing */
  69. unsigned char keys[32];
  70. unsigned char response[DH_KEY_LEN];
  71. unsigned char buf[MAX_QUESTIONLEN];
  72. close(fdarray[0]); /* this is the side of the socketpair the parent uses */
  73. fd = fdarray[1]; /* this side is ours */
  74. for(;;) {
  75. if(read(fd, &question_type, 1) != 1) {
  76. log_fn(LOG_INFO,"read type failed. Exiting.");
  77. spawn_exit();
  78. }
  79. assert(question_type == CPUWORKER_TASK_ONION ||
  80. question_type == CPUWORKER_TASK_HANDSHAKE);
  81. if(question_type == CPUWORKER_TASK_ONION)
  82. len = DH_ONIONSKIN_LEN;
  83. else
  84. len = 0; /* XXX */
  85. if(read(fd, question, len) != len) {
  86. log(LOG_INFO,"cpuworker_main(): read question failed. Exiting.");
  87. spawn_exit();
  88. }
  89. if(question_type == CPUWORKER_TASK_ONION) {
  90. if(onion_skin_server_handshake(question, get_privatekey(),
  91. response, keys, 32) < 0) {
  92. /* failure */
  93. log_fn(LOG_ERR,"onion_skin_server_handshake failed.");
  94. memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
  95. } else {
  96. /* success */
  97. log_fn(LOG_DEBUG,"onion_skin_server_handshake succeeded.");
  98. buf[0] = 1; /* 1 means success */
  99. memcpy(buf+1,response,DH_KEY_LEN);
  100. memcpy(buf+1+DH_KEY_LEN,keys,32);
  101. }
  102. if(write_all(fd, buf, LEN_ONION_RESPONSE) != LEN_ONION_RESPONSE) {
  103. log_fn(LOG_INFO,"writing response buf failed. Exiting.");
  104. spawn_exit();
  105. }
  106. log_fn(LOG_DEBUG,"finished writing response/keys.");
  107. } else { /* we've been asked to do a handshake. not implemented yet. */
  108. spawn_exit();
  109. }
  110. }
  111. return 0; /* windows wants this function to return an int */
  112. }
  113. static int spawn_cpuworker(void) {
  114. int fd[2];
  115. connection_t *conn;
  116. if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
  117. perror("socketpair");
  118. exit(1);
  119. }
  120. spawn_func(cpuworker_main, (void*)fd);
  121. log_fn(LOG_DEBUG,"just spawned a worker.");
  122. close(fd[1]); /* we don't need the worker's side of the pipe */
  123. conn = connection_new(CONN_TYPE_CPUWORKER);
  124. if(!conn) {
  125. close(fd[0]);
  126. return -1;
  127. }
  128. set_socket_nonblocking(fd[0]);
  129. /* set up conn so it's got all the data we need to remember */
  130. conn->receiver_bucket = -1; /* non-cell connections don't do receiver buckets */
  131. conn->bandwidth = -1;
  132. conn->s = fd[0];
  133. if(connection_add(conn) < 0) { /* no space, forget it */
  134. log_fn(LOG_INFO,"connection_add failed. Giving up.");
  135. connection_free(conn); /* this closes fd[0] */
  136. return -1;
  137. }
  138. conn->state = CPUWORKER_STATE_IDLE;
  139. connection_start_reading(conn);
  140. return 0; /* success */
  141. }
  142. static void spawn_enough_cpuworkers(void) {
  143. int num_cpuworkers_needed = options.NumCpus + 1;
  144. if(num_cpuworkers_needed < MIN_CPUWORKERS)
  145. num_cpuworkers_needed = MIN_CPUWORKERS;
  146. if(num_cpuworkers_needed > MAX_CPUWORKERS)
  147. num_cpuworkers_needed = MAX_CPUWORKERS;
  148. while(num_cpuworkers < num_cpuworkers_needed) {
  149. if(spawn_cpuworker() < 0) {
  150. log_fn(LOG_ERR,"spawn failed!");
  151. return;
  152. }
  153. num_cpuworkers++;
  154. }
  155. }
  156. static int process_pending_task(connection_t *cpuworker) {
  157. circuit_t *circ;
  158. assert(cpuworker);
  159. /* for now only process onion tasks */
  160. circ = onion_next_task();
  161. if(!circ)
  162. return 0;
  163. return assign_to_cpuworker(cpuworker, CPUWORKER_TASK_ONION, circ);
  164. }
  165. /* if cpuworker is defined, assert that he's idle, and use him. else,
  166. * look for an idle cpuworker and use him. if none idle, queue task onto
  167. * the pending onion list and return.
  168. * If question_type is CPUWORKER_TASK_ONION then task is a circ, else
  169. * (something else)
  170. */
  171. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  172. void *task) {
  173. circuit_t *circ;
  174. if(question_type == CPUWORKER_TASK_ONION) {
  175. circ = task;
  176. if(num_cpuworkers_busy == num_cpuworkers) {
  177. log_fn(LOG_DEBUG,"No idle cpuworkers. Queuing.");
  178. if(onion_pending_add(circ) < 0)
  179. return -1;
  180. return 0;
  181. }
  182. if(!cpuworker)
  183. cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER, CPUWORKER_STATE_IDLE);
  184. assert(cpuworker);
  185. cpuworker->circ = circ;
  186. cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
  187. num_cpuworkers_busy++;
  188. if(connection_write_to_buf(&question_type, 1, cpuworker) < 0 ||
  189. connection_write_to_buf(circ->onionskin, DH_ONIONSKIN_LEN, cpuworker) < 0) {
  190. log_fn(LOG_NOTICE,"Write failed. Closing worker and failing circ.");
  191. cpuworker->marked_for_close = 1;
  192. return -1;
  193. }
  194. }
  195. return 0;
  196. }
  197. /*
  198. Local Variables:
  199. mode:c
  200. indent-tabs-mode:nil
  201. c-basic-offset:2
  202. End:
  203. */