consdiffmgr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file consdiffmsr.c
  5. *
  6. * \brief consensus diff manager functions
  7. *
  8. * This module is run by directory authorities and caches in order
  9. * to remember a number of past consensus documents, and to generate
  10. * and serve the diffs from those documents to the latest consensus.
  11. */
  12. #define CONSDIFFMGR_PRIVATE
  13. #include "or.h"
  14. #include "conscache.h"
  15. #include "consdiff.h"
  16. #include "consdiffmgr.h"
  17. #include "cpuworker.h"
  18. #include "networkstatus.h"
  19. #include "workqueue.h"
  20. /* XXXX support compression */
  21. /**
  22. * Labels to apply to items in the conscache object.
  23. *
  24. * @{
  25. */
  26. /* One of DOCTYPE_CONSENSUS or DOCTYPE_CONSENSUS_DIFF */
  27. #define LABEL_DOCTYPE "document-type"
  28. /* The valid-after time for a consensus (or for the target consensus of a
  29. * diff), encoded as ISO UTC. */
  30. #define LABEL_VALID_AFTER "consensus-valid-after"
  31. /* A hex encoded SHA3 digest of the object after decompression. */
  32. #define LABEL_SHA3_DIGEST "sha3-digest"
  33. /* The flavor of the consensus or consensuses diff */
  34. #define LABEL_FLAVOR "consensus-flavor"
  35. /* Diff only: the SHA3 digest of the source consensus. */
  36. #define LABEL_FROM_SHA3_DIGEST "from-sha3-digest"
  37. /* Diff only: the SHA3 digest of the target consensus. */
  38. #define LABEL_TARGET_SHA3_DIGEST "target-sha3-digest"
  39. /* Diff only: the valid-after date of the source consensus. */
  40. #define LABEL_FROM_VALID_AFTER "from-valid-after"
  41. /** @} */
  42. #define DOCTYPE_CONSENSUS "consensus"
  43. #define DOCTYPE_CONSENSUS_DIFF "consensus-diff"
  44. /**
  45. * Underlying directory that stores consensuses and consensus diffs. Don't
  46. * use this directly: use cdm_cache_get() instead.
  47. */
  48. static consensus_cache_t *cons_diff_cache = NULL;
  49. /**
  50. * If true, we have learned at least one new consensus since the
  51. * consensus cache was last up-to-date.
  52. */
  53. static int cdm_cache_dirty = 0;
  54. /**
  55. * Configuration for this module
  56. */
  57. static consdiff_cfg_t consdiff_cfg = {
  58. /* .cache_max_age_hours = */ 24 * 90,
  59. /* .cache_max_num = */ 1440
  60. };
  61. static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  62. consensus_cache_entry_t *diff_to);
  63. static void consdiffmgr_set_cache_flags(void);
  64. /**
  65. * Helper: initialize <b>cons_diff_cache</b>.
  66. */
  67. static void
  68. cdm_cache_init(void)
  69. {
  70. unsigned n_entries = consdiff_cfg.cache_max_num * 2;
  71. tor_assert(cons_diff_cache == NULL);
  72. cons_diff_cache = consensus_cache_open("diff-cache", n_entries);
  73. if (cons_diff_cache == NULL) {
  74. // LCOV_EXCL_START
  75. log_err(LD_FS, "Error: Couldn't open storage for consensus diffs.");
  76. tor_assert_unreached();
  77. // LCOV_EXCL_STOP
  78. } else {
  79. consdiffmgr_set_cache_flags();
  80. }
  81. cdm_cache_dirty = 1;
  82. }
  83. /**
  84. * Helper: return the consensus_cache_t * that backs this manager,
  85. * initializing it if needed.
  86. */
  87. STATIC consensus_cache_t *
  88. cdm_cache_get(void)
  89. {
  90. if (PREDICT_UNLIKELY(cons_diff_cache == NULL)) {
  91. cdm_cache_init();
  92. }
  93. return cons_diff_cache;
  94. }
  95. /**
  96. * Helper: given a list of labels, prepend the hex-encoded SHA3 digest
  97. * of the <b>bodylen</b>-byte object at <b>body</b> to those labels,
  98. * with LABEL_SHA3_DIGEST as its label.
  99. */
  100. static void
  101. cdm_labels_prepend_sha3(config_line_t **labels,
  102. const uint8_t *body,
  103. size_t bodylen)
  104. {
  105. uint8_t sha3_digest[DIGEST256_LEN];
  106. char hexdigest[HEX_DIGEST256_LEN+1];
  107. crypto_digest256((char *)sha3_digest,
  108. (const char *)body, bodylen, DIGEST_SHA3_256);
  109. base16_encode(hexdigest, sizeof(hexdigest),
  110. (const char *)sha3_digest, sizeof(sha3_digest));
  111. config_line_prepend(labels, LABEL_SHA3_DIGEST, hexdigest);
  112. }
  113. /**
  114. * Helper: look for a consensus with the given <b>flavor</b> and
  115. * <b>valid_after</b> time in the cache. Return that consensus if it's
  116. * present, or NULL if it's missing.
  117. */
  118. STATIC consensus_cache_entry_t *
  119. cdm_cache_lookup_consensus(consensus_flavor_t flavor, time_t valid_after)
  120. {
  121. char formatted_time[ISO_TIME_LEN+1];
  122. format_iso_time_nospace(formatted_time, valid_after);
  123. const char *flavname = networkstatus_get_flavor_name(flavor);
  124. /* We'll filter by valid-after time first, since that should
  125. * match the fewest documents. */
  126. // XXXX This is stupid and it should be a hash table.
  127. smartlist_t *matches = smartlist_new();
  128. consensus_cache_find_all(matches, cdm_cache_get(),
  129. LABEL_VALID_AFTER, formatted_time);
  130. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  131. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  132. consensus_cache_entry_t *result = NULL;
  133. if (smartlist_len(matches) > 1) {
  134. log_warn(LD_BUG, "How odd; there appear to be two matching consensuses "
  135. "with flavor %s published at %s.",
  136. flavname, formatted_time);
  137. }
  138. if (smartlist_len(matches)) {
  139. result = smartlist_get(matches, 0);
  140. }
  141. smartlist_free(matches);
  142. return result;
  143. }
  144. /**
  145. * Given a string containing a networkstatus consensus, and the results of
  146. * having parsed that consensus, add that consensus to the cache if it is not
  147. * already present and not too old. Create new consensus diffs from or to
  148. * that consensus as appropriate.
  149. *
  150. * Return 0 on success and -1 on failure.
  151. */
  152. int
  153. consdiffmgr_add_consensus(const char *consensus,
  154. const networkstatus_t *as_parsed)
  155. {
  156. if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
  157. return -1; // LCOV_EXCL_LINE
  158. if (BUG(as_parsed->type != NS_TYPE_CONSENSUS))
  159. return -1; // LCOV_EXCL_LINE
  160. const consensus_flavor_t flavor = as_parsed->flavor;
  161. const time_t valid_after = as_parsed->valid_after;
  162. if (valid_after < approx_time() - 3600 * consdiff_cfg.cache_max_age_hours) {
  163. log_info(LD_DIRSERV, "We don't care about this consensus document; it's "
  164. "too old.");
  165. return -1;
  166. }
  167. /* Do we already have this one? */
  168. consensus_cache_entry_t *entry =
  169. cdm_cache_lookup_consensus(flavor, valid_after);
  170. if (entry) {
  171. log_info(LD_DIRSERV, "We already have a copy of that consensus");
  172. return -1;
  173. }
  174. /* We don't have it. Add it to the cache. */
  175. {
  176. size_t bodylen = strlen(consensus);
  177. config_line_t *labels = NULL;
  178. char formatted_time[ISO_TIME_LEN+1];
  179. format_iso_time_nospace(formatted_time, valid_after);
  180. const char *flavname = networkstatus_get_flavor_name(flavor);
  181. cdm_labels_prepend_sha3(&labels, (const uint8_t *)consensus, bodylen);
  182. config_line_prepend(&labels, LABEL_FLAVOR, flavname);
  183. config_line_prepend(&labels, LABEL_VALID_AFTER, formatted_time);
  184. config_line_prepend(&labels, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  185. entry = consensus_cache_add(cdm_cache_get(),
  186. labels,
  187. (const uint8_t *)consensus,
  188. bodylen);
  189. config_free_lines(labels);
  190. }
  191. if (entry) {
  192. consensus_cache_entry_mark_for_aggressive_release(entry);
  193. consensus_cache_entry_decref(entry);
  194. }
  195. cdm_cache_dirty = 1;
  196. return entry ? 0 : -1;
  197. }
  198. /**
  199. * Helper: used to sort two smartlists of consensus_cache_entry_t by their
  200. * LABEL_VALID_AFTER labels.
  201. */
  202. static int
  203. compare_by_valid_after_(const void **a, const void **b)
  204. {
  205. const consensus_cache_entry_t *e1 = *a;
  206. const consensus_cache_entry_t *e2 = *b;
  207. /* We're in luck here: sorting UTC iso-encoded values lexically will work
  208. * fine (until 9999). */
  209. return strcmp_opt(consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER),
  210. consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER));
  211. }
  212. /**
  213. * Helper: Sort <b>lst</b> by LABEL_VALID_AFTER and return the most recent
  214. * entry.
  215. */
  216. static consensus_cache_entry_t *
  217. sort_and_find_most_recent(smartlist_t *lst)
  218. {
  219. smartlist_sort(lst, compare_by_valid_after_);
  220. if (smartlist_len(lst)) {
  221. return smartlist_get(lst, smartlist_len(lst) - 1);
  222. } else {
  223. return NULL;
  224. }
  225. }
  226. /**
  227. * Look up consensus_cache_entry_t for the consensus of type <b>flavor</b>,
  228. * from the source consensus with the specified digest (which must be SHA3).
  229. *
  230. * If the diff is present, store it into *<b>entry_out</b> and return
  231. * CONSDIFF_AVAILABLE. Otherwise return CONSDIFF_NOT_FOUND or
  232. * CONSDIFF_IN_PROGRESS.
  233. */
  234. consdiff_status_t
  235. consdiffmgr_find_diff_from(consensus_cache_entry_t **entry_out,
  236. consensus_flavor_t flavor,
  237. int digest_type,
  238. const uint8_t *digest,
  239. size_t digestlen)
  240. {
  241. // XXXX actually return IN_PROGRESS some times?
  242. if (BUG(digest_type != DIGEST_SHA3_256) ||
  243. BUG(digestlen != DIGEST256_LEN)) {
  244. return CONSDIFF_NOT_FOUND; // LCOV_EXCL_LINE
  245. }
  246. char hex[HEX_DIGEST256_LEN+1];
  247. base16_encode(hex, sizeof(hex), (const char *)digest, digestlen);
  248. const char *flavname = networkstatus_get_flavor_name(flavor);
  249. smartlist_t *matches = smartlist_new();
  250. consensus_cache_find_all(matches, cdm_cache_get(),
  251. LABEL_FROM_SHA3_DIGEST, hex);
  252. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  253. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  254. *entry_out = sort_and_find_most_recent(matches);
  255. consdiff_status_t result =
  256. (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  257. smartlist_free(matches);
  258. return result;
  259. }
  260. /**
  261. * Perform periodic cleanup tasks on the consensus diff cache. Return
  262. * the number of objects marked for deletion.
  263. */
  264. int
  265. consdiffmgr_cleanup(void)
  266. {
  267. smartlist_t *objects = smartlist_new();
  268. smartlist_t *consensuses = smartlist_new();
  269. smartlist_t *diffs = smartlist_new();
  270. int n_to_delete = 0;
  271. log_debug(LD_DIRSERV, "Looking for consdiffmgr entries to remove");
  272. // 1. Delete any consensus or diff or anything whose valid_after is too old.
  273. const time_t valid_after_cutoff =
  274. approx_time() - 3600 * consdiff_cfg.cache_max_age_hours;
  275. consensus_cache_find_all(objects, cdm_cache_get(),
  276. NULL, NULL);
  277. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  278. const char *lv_valid_after =
  279. consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  280. if (! lv_valid_after) {
  281. log_debug(LD_DIRSERV, "Ignoring entry because it had no %s label",
  282. LABEL_VALID_AFTER);
  283. continue;
  284. }
  285. time_t valid_after = 0;
  286. if (parse_iso_time_nospace(lv_valid_after, &valid_after) < 0) {
  287. log_debug(LD_DIRSERV, "Ignoring entry because its %s value (%s) was "
  288. "unparseable", LABEL_VALID_AFTER, escaped(lv_valid_after));
  289. continue;
  290. }
  291. if (valid_after < valid_after_cutoff) {
  292. log_debug(LD_DIRSERV, "Deleting entry because its %s value (%s) was "
  293. "too old", LABEL_VALID_AFTER, lv_valid_after);
  294. consensus_cache_entry_mark_for_removal(ent);
  295. ++n_to_delete;
  296. }
  297. } SMARTLIST_FOREACH_END(ent);
  298. // 2. Delete all diffs that lead to a consensus whose valid-after is not the
  299. // latest.
  300. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  301. const char *flavname = networkstatus_get_flavor_name(flav);
  302. /* Determine the most recent consensus of this flavor */
  303. consensus_cache_find_all(consensuses, cdm_cache_get(),
  304. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  305. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  306. consensus_cache_entry_t *most_recent =
  307. sort_and_find_most_recent(consensuses);
  308. if (most_recent == NULL)
  309. continue;
  310. const char *most_recent_sha3 =
  311. consensus_cache_entry_get_value(most_recent, LABEL_SHA3_DIGEST);
  312. if (BUG(most_recent_sha3 == NULL))
  313. continue; // LCOV_EXCL_LINE
  314. /* consider all such-flavored diffs, and look to see if they match. */
  315. consensus_cache_find_all(diffs, cdm_cache_get(),
  316. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  317. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  318. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  319. const char *this_diff_target_sha3 =
  320. consensus_cache_entry_get_value(diff, LABEL_TARGET_SHA3_DIGEST);
  321. if (!this_diff_target_sha3)
  322. continue;
  323. if (strcmp(this_diff_target_sha3, most_recent_sha3)) {
  324. consensus_cache_entry_mark_for_removal(diff);
  325. ++n_to_delete;
  326. }
  327. } SMARTLIST_FOREACH_END(diff);
  328. smartlist_clear(consensuses);
  329. smartlist_clear(diffs);
  330. }
  331. smartlist_free(objects);
  332. smartlist_free(consensuses);
  333. smartlist_free(diffs);
  334. // XXXX for anything where the sha3 doesn't match -- delete it. But not
  335. // XXXX here. Somewhere else?
  336. // Actually remove files, if they're not used.
  337. consensus_cache_delete_pending(cdm_cache_get());
  338. return n_to_delete;
  339. }
  340. /**
  341. * Initialize the consensus diff manager and its cache, and configure
  342. * its parameters based on the latest torrc and networkstatus parameters.
  343. */
  344. void
  345. consdiffmgr_configure(const consdiff_cfg_t *cfg)
  346. {
  347. memcpy(&consdiff_cfg, cfg, sizeof(consdiff_cfg));
  348. (void) cdm_cache_get();
  349. }
  350. /**
  351. * Helper: build new diffs of <b>flavor</b> as needed
  352. */
  353. static void
  354. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  355. {
  356. smartlist_t *matches = NULL;
  357. smartlist_t *diffs = NULL;
  358. smartlist_t *compute_diffs_from = NULL;
  359. strmap_t *have_diff_from = NULL;
  360. // look for the most recent consensus, and for all previous in-range
  361. // consensuses. Do they all have diffs to it?
  362. const char *flavname = networkstatus_get_flavor_name(flavor);
  363. // 1. find the most recent consensus, and the ones that we might want
  364. // to diff to it.
  365. matches = smartlist_new();
  366. consensus_cache_find_all(matches, cdm_cache_get(),
  367. LABEL_FLAVOR, flavname);
  368. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  369. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  370. if (!most_recent) {
  371. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  372. "not making diffs", flavname);
  373. goto done;
  374. }
  375. tor_assert(smartlist_len(matches));
  376. smartlist_del(matches, smartlist_len(matches) - 1);
  377. const char *most_recent_valid_after =
  378. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  379. if (BUG(most_recent_valid_after == NULL))
  380. goto done; //LCOV_EXCL_LINE
  381. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  382. // that we don't need to compute.
  383. diffs = smartlist_new();
  384. consensus_cache_find_all(diffs, cdm_cache_get(),
  385. LABEL_VALID_AFTER, most_recent_valid_after);
  386. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  387. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  388. have_diff_from = strmap_new();
  389. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  390. const char *va = consensus_cache_entry_get_value(diff,
  391. LABEL_FROM_VALID_AFTER);
  392. if (BUG(va == NULL))
  393. continue; // LCOV_EXCL_LINE
  394. strmap_set(have_diff_from, va, diff);
  395. } SMARTLIST_FOREACH_END(diff);
  396. // 3. See which consensuses in 'matches' don't have diffs yet.
  397. smartlist_reverse(matches); // from newest to oldest.
  398. compute_diffs_from = smartlist_new();
  399. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  400. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  401. if (BUG(va == NULL))
  402. continue; // LCOV_EXCL_LINE
  403. if (strmap_get(have_diff_from, va) != NULL)
  404. continue; /* we already have this one. */
  405. smartlist_add(compute_diffs_from, ent);
  406. } SMARTLIST_FOREACH_END(ent);
  407. log_info(LD_DIRSERV,
  408. "The most recent %s consensus is valid-after %s. We have diffs to "
  409. "this consensus for %d/%d older %s consensuses. Generating diffs "
  410. "for the other %d.",
  411. flavname,
  412. most_recent_valid_after,
  413. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  414. smartlist_len(matches),
  415. flavname,
  416. smartlist_len(compute_diffs_from));
  417. // 4. Actually launch the requests.
  418. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  419. if (BUG(c == most_recent))
  420. continue; // LCOV_EXCL_LINE
  421. // XXXX how do we know that we are not already computing this?????
  422. // XXXX DO NOT MERGE UNTIL THAT ISSUE IS SOLVED.
  423. consensus_diff_queue_diff_work(c, most_recent);
  424. } SMARTLIST_FOREACH_END(c);
  425. done:
  426. smartlist_free(matches);
  427. smartlist_free(diffs);
  428. smartlist_free(compute_diffs_from);
  429. strmap_free(have_diff_from, NULL);
  430. }
  431. /**
  432. * Build new diffs as needed.
  433. */
  434. void
  435. consdiffmgr_rescan(void)
  436. {
  437. if (cdm_cache_dirty == 0)
  438. return;
  439. // Clean up here to make room for new diffs, and to ensure that older
  440. // consensuses do not have any entries.
  441. consdiffmgr_cleanup();
  442. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  443. consdiffmgr_rescan_flavor_((consensus_flavor_t) flav);
  444. }
  445. cdm_cache_dirty = 0;
  446. }
  447. /**
  448. * Set consensus cache flags on the objects in this consdiffmgr.
  449. */
  450. static void
  451. consdiffmgr_set_cache_flags(void)
  452. {
  453. /* Right now, we just mark the consensus objects for aggressive release,
  454. * so that they get mmapped for as little time as possible. */
  455. smartlist_t *objects = smartlist_new();
  456. consensus_cache_find_all(objects, cdm_cache_get(), LABEL_DOCTYPE,
  457. DOCTYPE_CONSENSUS);
  458. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  459. consensus_cache_entry_mark_for_aggressive_release(ent);
  460. } SMARTLIST_FOREACH_END(ent);
  461. smartlist_free(objects);
  462. }
  463. /**
  464. * Called before shutdown: drop all storage held by the consdiffmgr.c module.
  465. */
  466. void
  467. consdiffmgr_free_all(void)
  468. {
  469. consensus_cache_free(cons_diff_cache);
  470. cons_diff_cache = NULL;
  471. }
  472. /* =====
  473. Thread workers
  474. =====*/
  475. /**
  476. * An object passed to a worker thread that will try to produce a consensus
  477. * diff.
  478. */
  479. typedef struct consensus_diff_worker_job_t {
  480. /**
  481. * Input: The consensus to compute the diff from. Holds a reference to the
  482. * cache entry, which must not be released until the job is passed back to
  483. * the main thread. The body must be mapped into memory in the main thread.
  484. */
  485. consensus_cache_entry_t *diff_from;
  486. /**
  487. * Input: The consensus to compute the diff to. Holds a reference to the
  488. * cache entry, which must not be released until the job is passed back to
  489. * the main thread. The body must be mapped into memory in the main thread.
  490. */
  491. consensus_cache_entry_t *diff_to;
  492. /**
  493. * Output: Labels to store in the cache associated with this diff.
  494. */
  495. config_line_t *labels_out;
  496. /**
  497. * Output: Body of the diff
  498. */
  499. uint8_t *body_out;
  500. /**
  501. * Output: length of body_out
  502. */
  503. size_t bodylen_out;
  504. } consensus_diff_worker_job_t;
  505. /**
  506. * Worker function. This function runs inside a worker thread and receives
  507. * a consensus_diff_worker_job_t as its input.
  508. */
  509. static workqueue_reply_t
  510. consensus_diff_worker_threadfn(void *state_, void *work_)
  511. {
  512. (void)state_;
  513. consensus_diff_worker_job_t *job = work_;
  514. const uint8_t *diff_from, *diff_to;
  515. size_t len_from, len_to;
  516. int r;
  517. /* We need to have the body already mapped into RAM here.
  518. */
  519. r = consensus_cache_entry_get_body(job->diff_from, &diff_from, &len_from);
  520. if (BUG(r < 0))
  521. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  522. r = consensus_cache_entry_get_body(job->diff_to, &diff_to, &len_to);
  523. if (BUG(r < 0))
  524. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  525. const char *lv_to_valid_after =
  526. consensus_cache_entry_get_value(job->diff_to, LABEL_VALID_AFTER);
  527. const char *lv_from_valid_after =
  528. consensus_cache_entry_get_value(job->diff_from, LABEL_VALID_AFTER);
  529. const char *lv_from_digest =
  530. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  531. const char *lv_from_flavor =
  532. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  533. const char *lv_to_flavor =
  534. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  535. const char *lv_to_digest =
  536. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  537. /* All these values are mandatory on the input */
  538. if (BUG(!lv_to_valid_after) ||
  539. BUG(!lv_from_valid_after) ||
  540. BUG(!lv_from_digest) ||
  541. BUG(!lv_from_flavor) ||
  542. BUG(!lv_to_flavor)) {
  543. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  544. }
  545. /* The flavors need to match */
  546. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  547. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  548. }
  549. char *consensus_diff;
  550. {
  551. // XXXX the input might not be nul-terminated. And also we wanted to
  552. // XXXX support compression later I guess. So, we need to copy here.
  553. char *diff_from_nt, *diff_to_nt;
  554. diff_from_nt = tor_memdup_nulterm(diff_from, len_from);
  555. diff_to_nt = tor_memdup_nulterm(diff_to, len_to);
  556. // XXXX ugh; this is going to calculate the SHA3 of both its
  557. // XXXX inputs again, even though we already have that. Maybe it's time
  558. // XXXX to change the API here?
  559. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  560. tor_free(diff_from_nt);
  561. tor_free(diff_to_nt);
  562. }
  563. if (!consensus_diff) {
  564. /* Couldn't generate consensus; we'll leave the reply blank. */
  565. return WQ_RPL_REPLY;
  566. }
  567. /* Send the reply */
  568. job->body_out = (uint8_t *) consensus_diff;
  569. job->bodylen_out = strlen(consensus_diff);
  570. cdm_labels_prepend_sha3(&job->labels_out, job->body_out, job->bodylen_out);
  571. config_line_prepend(&job->labels_out, LABEL_FROM_VALID_AFTER,
  572. lv_from_valid_after);
  573. config_line_prepend(&job->labels_out, LABEL_VALID_AFTER, lv_to_valid_after);
  574. config_line_prepend(&job->labels_out, LABEL_FLAVOR, lv_from_flavor);
  575. config_line_prepend(&job->labels_out, LABEL_FROM_SHA3_DIGEST,
  576. lv_from_digest);
  577. config_line_prepend(&job->labels_out, LABEL_TARGET_SHA3_DIGEST,
  578. lv_to_digest);
  579. config_line_prepend(&job->labels_out, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  580. return WQ_RPL_REPLY;
  581. }
  582. /**
  583. * Helper: release all storage held in <b>job</b>.
  584. */
  585. static void
  586. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  587. {
  588. if (!job)
  589. return;
  590. tor_free(job->body_out);
  591. config_free_lines(job->labels_out);
  592. consensus_cache_entry_decref(job->diff_from);
  593. consensus_cache_entry_decref(job->diff_to);
  594. tor_free(job);
  595. }
  596. /**
  597. * Worker function: This function runs in the main thread, and receives
  598. * a consensus_diff_worker_job_t that the worker thread has already
  599. * processed.
  600. */
  601. static void
  602. consensus_diff_worker_replyfn(void *work_)
  603. {
  604. tor_assert(in_main_thread());
  605. tor_assert(work_);
  606. consensus_diff_worker_job_t *job = work_;
  607. const char *lv_from_digest =
  608. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  609. const char *lv_to_digest =
  610. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  611. if (BUG(lv_from_digest == NULL))
  612. lv_from_digest = "???"; // LCOV_EXCL_LINE
  613. if (BUG(lv_to_digest == NULL))
  614. lv_to_digest = "???"; // LCOV_EXCL_LINE
  615. if (job->body_out && job->bodylen_out && job->labels_out) {
  616. /* Success! Store the results */
  617. log_info(LD_DIRSERV, "Adding consensus diff from %s to %s",
  618. lv_from_digest, lv_to_digest);
  619. consensus_cache_add(cdm_cache_get(), job->labels_out,
  620. job->body_out,
  621. job->bodylen_out);
  622. } else {
  623. /* Failure! Nothing to do but complain */
  624. log_warn(LD_DIRSERV,
  625. "Worker was unable to compute consensus diff "
  626. "from %s to %s", lv_from_digest, lv_to_digest);
  627. /* XXXX Actually, we should cache this failure and not repeat the
  628. * attempt over and over */
  629. }
  630. consensus_diff_worker_job_free(job);
  631. }
  632. /**
  633. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  634. * in a worker thread.
  635. */
  636. static int
  637. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  638. consensus_cache_entry_t *diff_to)
  639. {
  640. tor_assert(in_main_thread());
  641. consensus_cache_entry_incref(diff_from);
  642. consensus_cache_entry_incref(diff_to);
  643. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  644. job->diff_from = diff_from;
  645. job->diff_to = diff_to;
  646. /* Make sure body is mapped. */
  647. const uint8_t *body;
  648. size_t bodylen;
  649. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  650. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  651. if (r1 < 0 || r2 < 0)
  652. goto err;
  653. workqueue_entry_t *work;
  654. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  655. consensus_diff_worker_replyfn,
  656. job);
  657. if (!work)
  658. goto err;
  659. return 0;
  660. err:
  661. consensus_diff_worker_job_free(job); // includes decrefs.
  662. return -1;
  663. }