onion_queue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion_queue.c
  8. * \brief Functions to queue create cells for processing.
  9. *
  10. * Relays invoke these functions when they receive a CREATE or EXTEND
  11. * cell in command.c or relay.c, in order to queue the pending request.
  12. * They also invoke them from cpuworker.c, which handles dispatching
  13. * onionskin requests to different worker threads.
  14. *
  15. * <br>
  16. *
  17. * This module also handles:
  18. * <ul>
  19. * <li> Queueing incoming onionskins on the relay side before passing
  20. * them to worker threads.
  21. * <li>Expiring onionskins on the relay side if they have waited for
  22. * too long.
  23. * </ul>
  24. **/
  25. #include "core/or/or.h"
  26. #include "feature/relay/onion_queue.h"
  27. #include "app/config/config.h"
  28. #include "core/mainloop/cpuworker.h"
  29. #include "core/or/circuitlist.h"
  30. #include "core/or/onion.h"
  31. #include "feature/nodelist/networkstatus.h"
  32. #include "core/or/or_circuit_st.h"
  33. /** Type for a linked list of circuits that are waiting for a free CPU worker
  34. * to process a waiting onion handshake. */
  35. typedef struct onion_queue_t {
  36. TOR_TAILQ_ENTRY(onion_queue_t) next;
  37. or_circuit_t *circ;
  38. uint16_t handshake_type;
  39. create_cell_t *onionskin;
  40. time_t when_added;
  41. } onion_queue_t;
  42. /** 5 seconds on the onion queue til we just send back a destroy */
  43. #define ONIONQUEUE_WAIT_CUTOFF 5
  44. /** Array of queues of circuits waiting for CPU workers. An element is NULL
  45. * if that queue is empty.*/
  46. static TOR_TAILQ_HEAD(onion_queue_head_t, onion_queue_t)
  47. ol_list[MAX_ONION_HANDSHAKE_TYPE+1] =
  48. { TOR_TAILQ_HEAD_INITIALIZER(ol_list[0]), /* tap */
  49. TOR_TAILQ_HEAD_INITIALIZER(ol_list[1]), /* fast */
  50. TOR_TAILQ_HEAD_INITIALIZER(ol_list[2]), /* ntor */
  51. };
  52. /** Number of entries of each type currently in each element of ol_list[]. */
  53. static int ol_entries[MAX_ONION_HANDSHAKE_TYPE+1];
  54. static int num_ntors_per_tap(void);
  55. static void onion_queue_entry_remove(onion_queue_t *victim);
  56. /* XXXX Check lengths vs MAX_ONIONSKIN_{CHALLENGE,REPLY}_LEN.
  57. *
  58. * (By which I think I meant, "make sure that no
  59. * X_ONIONSKIN_CHALLENGE/REPLY_LEN is greater than
  60. * MAX_ONIONSKIN_CHALLENGE/REPLY_LEN." Also, make sure that we can pass
  61. * over-large values via EXTEND2/EXTENDED2, for future-compatibility.*/
  62. /** Return true iff we have room to queue another onionskin of type
  63. * <b>type</b>. */
  64. static int
  65. have_room_for_onionskin(uint16_t type)
  66. {
  67. const or_options_t *options = get_options();
  68. int num_cpus;
  69. uint64_t tap_usec, ntor_usec;
  70. uint64_t ntor_during_tap_usec, tap_during_ntor_usec;
  71. /* If we've got fewer than 50 entries, we always have room for one more. */
  72. if (ol_entries[type] < 50)
  73. return 1;
  74. num_cpus = get_num_cpus(options);
  75. /* Compute how many microseconds we'd expect to need to clear all
  76. * onionskins in various combinations of the queues. */
  77. /* How long would it take to process all the TAP cells in the queue? */
  78. tap_usec = estimated_usec_for_onionskins(
  79. ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  80. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  81. /* How long would it take to process all the NTor cells in the queue? */
  82. ntor_usec = estimated_usec_for_onionskins(
  83. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  84. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  85. /* How long would it take to process the tap cells that we expect to
  86. * process while draining the ntor queue? */
  87. tap_during_ntor_usec = estimated_usec_for_onionskins(
  88. MIN(ol_entries[ONION_HANDSHAKE_TYPE_TAP],
  89. ol_entries[ONION_HANDSHAKE_TYPE_NTOR] / num_ntors_per_tap()),
  90. ONION_HANDSHAKE_TYPE_TAP) / num_cpus;
  91. /* How long would it take to process the ntor cells that we expect to
  92. * process while draining the tap queue? */
  93. ntor_during_tap_usec = estimated_usec_for_onionskins(
  94. MIN(ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  95. ol_entries[ONION_HANDSHAKE_TYPE_TAP] * num_ntors_per_tap()),
  96. ONION_HANDSHAKE_TYPE_NTOR) / num_cpus;
  97. /* See whether that exceeds MaxOnionQueueDelay. If so, we can't queue
  98. * this. */
  99. if (type == ONION_HANDSHAKE_TYPE_NTOR &&
  100. (ntor_usec + tap_during_ntor_usec) / 1000 >
  101. (uint64_t)options->MaxOnionQueueDelay)
  102. return 0;
  103. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  104. (tap_usec + ntor_during_tap_usec) / 1000 >
  105. (uint64_t)options->MaxOnionQueueDelay)
  106. return 0;
  107. /* If we support the ntor handshake, then don't let TAP handshakes use
  108. * more than 2/3 of the space on the queue. */
  109. if (type == ONION_HANDSHAKE_TYPE_TAP &&
  110. tap_usec / 1000 > (uint64_t)options->MaxOnionQueueDelay * 2 / 3)
  111. return 0;
  112. return 1;
  113. }
  114. /** Add <b>circ</b> to the end of ol_list and return 0, except
  115. * if ol_list is too long, in which case do nothing and return -1.
  116. */
  117. int
  118. onion_pending_add(or_circuit_t *circ, create_cell_t *onionskin)
  119. {
  120. onion_queue_t *tmp;
  121. time_t now = time(NULL);
  122. if (onionskin->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  123. /* LCOV_EXCL_START
  124. * We should have rejected this far before this point */
  125. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  126. onionskin->handshake_type);
  127. return -1;
  128. /* LCOV_EXCL_STOP */
  129. }
  130. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  131. tmp->circ = circ;
  132. tmp->handshake_type = onionskin->handshake_type;
  133. tmp->onionskin = onionskin;
  134. tmp->when_added = now;
  135. if (!have_room_for_onionskin(onionskin->handshake_type)) {
  136. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  137. static ratelim_t last_warned =
  138. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  139. char *m;
  140. if (onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR &&
  141. (m = rate_limit_log(&last_warned, approx_time()))) {
  142. log_warn(LD_GENERAL,
  143. "Your computer is too slow to handle this many circuit "
  144. "creation requests! Please consider using the "
  145. "MaxAdvertisedBandwidth config option or choosing a more "
  146. "restricted exit policy.%s",m);
  147. tor_free(m);
  148. }
  149. tor_free(tmp);
  150. return -1;
  151. }
  152. ++ol_entries[onionskin->handshake_type];
  153. log_info(LD_OR, "New create (%s). Queues now ntor=%d and tap=%d.",
  154. onionskin->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  155. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  156. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  157. circ->onionqueue_entry = tmp;
  158. TOR_TAILQ_INSERT_TAIL(&ol_list[onionskin->handshake_type], tmp, next);
  159. /* cull elderly requests. */
  160. while (1) {
  161. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[onionskin->handshake_type]);
  162. if (now - head->when_added < (time_t)ONIONQUEUE_WAIT_CUTOFF)
  163. break;
  164. circ = head->circ;
  165. circ->onionqueue_entry = NULL;
  166. onion_queue_entry_remove(head);
  167. log_info(LD_CIRC,
  168. "Circuit create request is too old; canceling due to overload.");
  169. if (! TO_CIRCUIT(circ)->marked_for_close) {
  170. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  171. }
  172. }
  173. return 0;
  174. }
  175. /** Return a fairness parameter, to prefer processing NTOR style
  176. * handshakes but still slowly drain the TAP queue so we don't starve
  177. * it entirely. */
  178. static int
  179. num_ntors_per_tap(void)
  180. {
  181. #define DEFAULT_NUM_NTORS_PER_TAP 10
  182. #define MIN_NUM_NTORS_PER_TAP 1
  183. #define MAX_NUM_NTORS_PER_TAP 100000
  184. return networkstatus_get_param(NULL, "NumNTorsPerTAP",
  185. DEFAULT_NUM_NTORS_PER_TAP,
  186. MIN_NUM_NTORS_PER_TAP,
  187. MAX_NUM_NTORS_PER_TAP);
  188. }
  189. /** Choose which onion queue we'll pull from next. If one is empty choose
  190. * the other; if they both have elements, load balance across them but
  191. * favoring NTOR. */
  192. static uint16_t
  193. decide_next_handshake_type(void)
  194. {
  195. /* The number of times we've chosen ntor lately when both were available. */
  196. static int recently_chosen_ntors = 0;
  197. if (!ol_entries[ONION_HANDSHAKE_TYPE_NTOR])
  198. return ONION_HANDSHAKE_TYPE_TAP; /* no ntors? try tap */
  199. if (!ol_entries[ONION_HANDSHAKE_TYPE_TAP]) {
  200. /* Nick wants us to prioritize new tap requests when there aren't
  201. * any in the queue and we've processed k ntor cells since the last
  202. * tap cell. This strategy is maybe a good idea, since it starves tap
  203. * less in the case where tap is rare, or maybe a poor idea, since it
  204. * makes the new tap cell unfairly jump in front of ntor cells that
  205. * got here first. In any case this edge case will only become relevant
  206. * once tap is rare. We should reevaluate whether we like this decision
  207. * once tap gets more rare. */
  208. if (ol_entries[ONION_HANDSHAKE_TYPE_NTOR] &&
  209. recently_chosen_ntors <= num_ntors_per_tap())
  210. ++recently_chosen_ntors;
  211. return ONION_HANDSHAKE_TYPE_NTOR; /* no taps? try ntor */
  212. }
  213. /* They both have something queued. Pick ntor if we haven't done that
  214. * too much lately. */
  215. if (++recently_chosen_ntors <= num_ntors_per_tap()) {
  216. return ONION_HANDSHAKE_TYPE_NTOR;
  217. }
  218. /* Else, it's time to let tap have its turn. */
  219. recently_chosen_ntors = 0;
  220. return ONION_HANDSHAKE_TYPE_TAP;
  221. }
  222. /** Remove the highest priority item from ol_list[] and return it, or
  223. * return NULL if the lists are empty.
  224. */
  225. or_circuit_t *
  226. onion_next_task(create_cell_t **onionskin_out)
  227. {
  228. or_circuit_t *circ;
  229. uint16_t handshake_to_choose = decide_next_handshake_type();
  230. onion_queue_t *head = TOR_TAILQ_FIRST(&ol_list[handshake_to_choose]);
  231. if (!head)
  232. return NULL; /* no onions pending, we're done */
  233. tor_assert(head->circ);
  234. tor_assert(head->handshake_type <= MAX_ONION_HANDSHAKE_TYPE);
  235. // tor_assert(head->circ->p_chan); /* make sure it's still valid */
  236. /* XXX I only commented out the above line to make the unit tests
  237. * more manageable. That's probably not good long-term. -RD */
  238. circ = head->circ;
  239. if (head->onionskin)
  240. --ol_entries[head->handshake_type];
  241. log_info(LD_OR, "Processing create (%s). Queues now ntor=%d and tap=%d.",
  242. head->handshake_type == ONION_HANDSHAKE_TYPE_NTOR ? "ntor" : "tap",
  243. ol_entries[ONION_HANDSHAKE_TYPE_NTOR],
  244. ol_entries[ONION_HANDSHAKE_TYPE_TAP]);
  245. *onionskin_out = head->onionskin;
  246. head->onionskin = NULL; /* prevent free. */
  247. circ->onionqueue_entry = NULL;
  248. onion_queue_entry_remove(head);
  249. return circ;
  250. }
  251. /** Return the number of <b>handshake_type</b>-style create requests pending.
  252. */
  253. int
  254. onion_num_pending(uint16_t handshake_type)
  255. {
  256. return ol_entries[handshake_type];
  257. }
  258. /** Go through ol_list, find the onion_queue_t element which points to
  259. * circ, remove and free that element. Leave circ itself alone.
  260. */
  261. void
  262. onion_pending_remove(or_circuit_t *circ)
  263. {
  264. onion_queue_t *victim;
  265. if (!circ)
  266. return;
  267. victim = circ->onionqueue_entry;
  268. if (victim)
  269. onion_queue_entry_remove(victim);
  270. cpuworker_cancel_circ_handshake(circ);
  271. }
  272. /** Remove a queue entry <b>victim</b> from the queue, unlinking it from
  273. * its circuit and freeing it and any structures it owns.*/
  274. static void
  275. onion_queue_entry_remove(onion_queue_t *victim)
  276. {
  277. if (victim->handshake_type > MAX_ONION_HANDSHAKE_TYPE) {
  278. /* LCOV_EXCL_START
  279. * We should have rejected this far before this point */
  280. log_warn(LD_BUG, "Handshake %d out of range! Dropping.",
  281. victim->handshake_type);
  282. /* XXX leaks */
  283. return;
  284. /* LCOV_EXCL_STOP */
  285. }
  286. TOR_TAILQ_REMOVE(&ol_list[victim->handshake_type], victim, next);
  287. if (victim->circ)
  288. victim->circ->onionqueue_entry = NULL;
  289. if (victim->onionskin)
  290. --ol_entries[victim->handshake_type];
  291. tor_free(victim->onionskin);
  292. tor_free(victim);
  293. }
  294. /** Remove all circuits from the pending list. Called from tor_free_all. */
  295. void
  296. clear_pending_onions(void)
  297. {
  298. onion_queue_t *victim, *next;
  299. int i;
  300. for (i=0; i<=MAX_ONION_HANDSHAKE_TYPE; i++) {
  301. for (victim = TOR_TAILQ_FIRST(&ol_list[i]); victim; victim = next) {
  302. next = TOR_TAILQ_NEXT(victim,next);
  303. onion_queue_entry_remove(victim);
  304. }
  305. tor_assert(TOR_TAILQ_EMPTY(&ol_list[i]));
  306. }
  307. memset(ol_entries, 0, sizeof(ol_entries));
  308. }