consdiffmgr.c 23 KB

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