consdiffmgr.c 23 KB

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