consdiffmgr.c 23 KB

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