consdiffmgr.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. * If true, we have scanned the cache to update our hashtable of diffs.
  56. */
  57. static int cdm_cache_loaded = 0;
  58. /**
  59. * Possible status values for cdm_diff_t.cdm_diff_status
  60. **/
  61. typedef enum cdm_diff_status_t {
  62. CDM_DIFF_PRESENT=1,
  63. CDM_DIFF_IN_PROGRESS=2,
  64. CDM_DIFF_ERROR=3,
  65. } cdm_diff_status_t;
  66. /** Hashtable node used to remember the current status of the diff
  67. * from a given sha3 digest to the current consensus. */
  68. typedef struct cdm_diff_t {
  69. HT_ENTRY(cdm_diff_t) node;
  70. /** Consensus flavor for this diff (part of ht key) */
  71. consensus_flavor_t flavor;
  72. /** SHA3-256 digest of the consensus that this diff is _from_. (part of the
  73. * ht key) */
  74. uint8_t from_sha3[DIGEST256_LEN];
  75. /** One of the CDM_DIFF_* values, depending on whether this diff
  76. * is available, in progress, or impossible to compute. */
  77. cdm_diff_status_t cdm_diff_status;
  78. /** SHA3-256 digest of the consensus that this diff is _to. */
  79. uint8_t target_sha3[DIGEST256_LEN];
  80. /** Handle to the cache entry for this diff, if any. We use a handle here
  81. * to avoid thinking too hard about cache entry lifetime issues. */
  82. consensus_cache_entry_handle_t *entry;
  83. } cdm_diff_t;
  84. /** Hashtable mapping flavor and source consensus digest to status. */
  85. static HT_HEAD(cdm_diff_ht, cdm_diff_t) cdm_diff_ht = HT_INITIALIZER();
  86. /**
  87. * Configuration for this module
  88. */
  89. static consdiff_cfg_t consdiff_cfg = {
  90. /* .cache_max_age_hours = */ 24 * 90,
  91. /* .cache_max_num = */ 1440
  92. };
  93. static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  94. consensus_cache_entry_t *diff_to);
  95. static void consdiffmgr_set_cache_flags(void);
  96. /* =====
  97. * Hashtable setup
  98. * ===== */
  99. /** Helper: hash the key of a cdm_diff_t. */
  100. static unsigned
  101. cdm_diff_hash(const cdm_diff_t *diff)
  102. {
  103. uint8_t tmp[DIGEST256_LEN + 1];
  104. memcpy(tmp, diff->from_sha3, DIGEST256_LEN);
  105. tmp[DIGEST256_LEN] = (uint8_t) diff->flavor;
  106. return (unsigned) siphash24g(tmp, sizeof(tmp));
  107. }
  108. /** Helper: compare two cdm_diff_t objects for key equality */
  109. static int
  110. cdm_diff_eq(const cdm_diff_t *diff1, const cdm_diff_t *diff2)
  111. {
  112. return fast_memeq(diff1->from_sha3, diff2->from_sha3, DIGEST256_LEN) &&
  113. diff1->flavor == diff2->flavor;
  114. }
  115. HT_PROTOTYPE(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq)
  116. HT_GENERATE2(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq,
  117. 0.6, tor_reallocarray, tor_free_)
  118. /** Release all storage held in <b>diff</b>. */
  119. static void
  120. cdm_diff_free(cdm_diff_t *diff)
  121. {
  122. if (!diff)
  123. return;
  124. consensus_cache_entry_handle_free(diff->entry);
  125. tor_free(diff);
  126. }
  127. /** Create and return a new cdm_diff_t with the given values. Does not
  128. * add it to the hashtable. */
  129. static cdm_diff_t *
  130. cdm_diff_new(consensus_flavor_t flav,
  131. const uint8_t *from_sha3,
  132. const uint8_t *target_sha3)
  133. {
  134. cdm_diff_t *ent;
  135. ent = tor_malloc_zero(sizeof(cdm_diff_t));
  136. ent->flavor = flav;
  137. memcpy(ent->from_sha3, from_sha3, DIGEST256_LEN);
  138. memcpy(ent->target_sha3, target_sha3, DIGEST256_LEN);
  139. return ent;
  140. }
  141. /**
  142. * Examine the diff hashtable to see whether we know anything about computing
  143. * a diff of type <b>flav</b> between consensuses with the two provided
  144. * SHA3-256 digests. If a computation is in progress, or if the computation
  145. * has already been tried and failed, return 1. Otherwise, note the
  146. * computation as "in progress" so that we don't reattempt it later, and
  147. * return 0.
  148. */
  149. static int
  150. cdm_diff_ht_check_and_note_pending(consensus_flavor_t flav,
  151. const uint8_t *from_sha3,
  152. const uint8_t *target_sha3)
  153. {
  154. struct cdm_diff_t search, *ent;
  155. memset(&search, 0, sizeof(cdm_diff_t));
  156. search.flavor = flav;
  157. memcpy(search.from_sha3, from_sha3, DIGEST256_LEN);
  158. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  159. if (ent) {
  160. tor_assert_nonfatal(ent->cdm_diff_status != CDM_DIFF_PRESENT);
  161. return 1;
  162. }
  163. ent = cdm_diff_new(flav, from_sha3, target_sha3);
  164. ent->cdm_diff_status = CDM_DIFF_IN_PROGRESS;
  165. HT_INSERT(cdm_diff_ht, &cdm_diff_ht, ent);
  166. return 0;
  167. }
  168. /**
  169. * Update the status of the diff of type <b>flav</b> between consensuses with
  170. * the two provided SHA3-256 digests, so that its status becomes
  171. * <b>status</b>, and its value becomes the <b>handle</b>. If <b>handle</b>
  172. * is NULL, then the old handle (if any) is freed, and replaced with NULL.
  173. */
  174. static void
  175. cdm_diff_ht_set_status(consensus_flavor_t flav,
  176. const uint8_t *from_sha3,
  177. const uint8_t *to_sha3,
  178. int status,
  179. consensus_cache_entry_handle_t *handle)
  180. {
  181. struct cdm_diff_t search, *ent;
  182. memset(&search, 0, sizeof(cdm_diff_t));
  183. search.flavor = flav;
  184. memcpy(search.from_sha3, from_sha3, DIGEST256_LEN);
  185. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  186. if (!ent) {
  187. ent = cdm_diff_new(flav, from_sha3, to_sha3);
  188. ent->cdm_diff_status = CDM_DIFF_IN_PROGRESS;
  189. HT_INSERT(cdm_diff_ht, &cdm_diff_ht, ent);
  190. } else if (fast_memneq(ent->target_sha3, to_sha3, DIGEST256_LEN)) {
  191. // This can happen under certain really pathological conditions
  192. // if we decide we don't care about a diff before it is actually
  193. // done computing.
  194. return;
  195. }
  196. tor_assert_nonfatal(ent->cdm_diff_status == CDM_DIFF_IN_PROGRESS);
  197. ent->cdm_diff_status = status;
  198. consensus_cache_entry_handle_free(ent->entry);
  199. ent->entry = handle;
  200. }
  201. /**
  202. * Helper: Remove from the hash table every present (actually computed) diff
  203. * of type <b>flav</b> whose target digest does not match
  204. * <b>unless_target_sha3_matches</b>.
  205. *
  206. * This function is used for the hash table to throw away references to diffs
  207. * that do not lead to the most given consensus of a given flavor.
  208. */
  209. static void
  210. cdm_diff_ht_purge(consensus_flavor_t flav,
  211. const uint8_t *unless_target_sha3_matches)
  212. {
  213. cdm_diff_t **diff, **next;
  214. for (diff = HT_START(cdm_diff_ht, &cdm_diff_ht); diff; diff = next) {
  215. cdm_diff_t *this = *diff;
  216. if ((*diff)->cdm_diff_status == CDM_DIFF_PRESENT &&
  217. flav == (*diff)->flavor) {
  218. if (consensus_cache_entry_handle_get((*diff)->entry) == NULL) {
  219. /* the underlying entry has gone away; drop this. */
  220. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  221. cdm_diff_free(this);
  222. continue;
  223. }
  224. if (unless_target_sha3_matches &&
  225. fast_memneq(unless_target_sha3_matches, (*diff)->target_sha3,
  226. DIGEST256_LEN)) {
  227. /* target hash doesn't match; drop this. */
  228. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  229. cdm_diff_free(this);
  230. continue;
  231. }
  232. }
  233. next = HT_NEXT(cdm_diff_ht, &cdm_diff_ht, diff);
  234. }
  235. }
  236. /**
  237. * Helper: initialize <b>cons_diff_cache</b>.
  238. */
  239. static void
  240. cdm_cache_init(void)
  241. {
  242. unsigned n_entries = consdiff_cfg.cache_max_num * 2;
  243. tor_assert(cons_diff_cache == NULL);
  244. cons_diff_cache = consensus_cache_open("diff-cache", n_entries);
  245. if (cons_diff_cache == NULL) {
  246. // LCOV_EXCL_START
  247. log_err(LD_FS, "Error: Couldn't open storage for consensus diffs.");
  248. tor_assert_unreached();
  249. // LCOV_EXCL_STOP
  250. } else {
  251. consdiffmgr_set_cache_flags();
  252. }
  253. cdm_cache_dirty = 1;
  254. cdm_cache_loaded = 0;
  255. }
  256. /**
  257. * Helper: return the consensus_cache_t * that backs this manager,
  258. * initializing it if needed.
  259. */
  260. STATIC consensus_cache_t *
  261. cdm_cache_get(void)
  262. {
  263. if (PREDICT_UNLIKELY(cons_diff_cache == NULL)) {
  264. cdm_cache_init();
  265. }
  266. return cons_diff_cache;
  267. }
  268. /**
  269. * Helper: given a list of labels, prepend the hex-encoded SHA3 digest
  270. * of the <b>bodylen</b>-byte object at <b>body</b> to those labels,
  271. * with LABEL_SHA3_DIGEST as its label.
  272. */
  273. static void
  274. cdm_labels_prepend_sha3(config_line_t **labels,
  275. const uint8_t *body,
  276. size_t bodylen)
  277. {
  278. uint8_t sha3_digest[DIGEST256_LEN];
  279. char hexdigest[HEX_DIGEST256_LEN+1];
  280. crypto_digest256((char *)sha3_digest,
  281. (const char *)body, bodylen, DIGEST_SHA3_256);
  282. base16_encode(hexdigest, sizeof(hexdigest),
  283. (const char *)sha3_digest, sizeof(sha3_digest));
  284. config_line_prepend(labels, LABEL_SHA3_DIGEST, hexdigest);
  285. }
  286. /** Helper: if there is a sha3-256 hex-encoded digest in <b>ent</b> with the
  287. * given label, set <b>digest_out</b> to that value (decoded), and return 0.
  288. *
  289. * Return -1 if there is no such label, and -2 if it is badly formatted. */
  290. STATIC int
  291. cdm_entry_get_sha3_value(uint8_t *digest_out,
  292. consensus_cache_entry_t *ent,
  293. const char *label)
  294. {
  295. if (ent == NULL)
  296. return -1;
  297. const char *hex = consensus_cache_entry_get_value(ent, label);
  298. if (hex == NULL)
  299. return -1;
  300. int n = base16_decode((char*)digest_out, DIGEST256_LEN, hex, strlen(hex));
  301. if (n != DIGEST256_LEN)
  302. return -2;
  303. else
  304. return 0;
  305. }
  306. /**
  307. * Helper: look for a consensus with the given <b>flavor</b> and
  308. * <b>valid_after</b> time in the cache. Return that consensus if it's
  309. * present, or NULL if it's missing.
  310. */
  311. STATIC consensus_cache_entry_t *
  312. cdm_cache_lookup_consensus(consensus_flavor_t flavor, time_t valid_after)
  313. {
  314. char formatted_time[ISO_TIME_LEN+1];
  315. format_iso_time_nospace(formatted_time, valid_after);
  316. const char *flavname = networkstatus_get_flavor_name(flavor);
  317. /* We'll filter by valid-after time first, since that should
  318. * match the fewest documents. */
  319. /* We could add an extra hashtable here, but since we only do this scan
  320. * when adding a new consensus, it probably doesn't matter much. */
  321. smartlist_t *matches = smartlist_new();
  322. consensus_cache_find_all(matches, cdm_cache_get(),
  323. LABEL_VALID_AFTER, formatted_time);
  324. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  325. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  326. consensus_cache_entry_t *result = NULL;
  327. if (smartlist_len(matches) > 1) {
  328. log_warn(LD_BUG, "How odd; there appear to be two matching consensuses "
  329. "with flavor %s published at %s.",
  330. flavname, formatted_time);
  331. }
  332. if (smartlist_len(matches)) {
  333. result = smartlist_get(matches, 0);
  334. }
  335. smartlist_free(matches);
  336. return result;
  337. }
  338. /**
  339. * Given a string containing a networkstatus consensus, and the results of
  340. * having parsed that consensus, add that consensus to the cache if it is not
  341. * already present and not too old. Create new consensus diffs from or to
  342. * that consensus as appropriate.
  343. *
  344. * Return 0 on success and -1 on failure.
  345. */
  346. int
  347. consdiffmgr_add_consensus(const char *consensus,
  348. const networkstatus_t *as_parsed)
  349. {
  350. if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
  351. return -1; // LCOV_EXCL_LINE
  352. if (BUG(as_parsed->type != NS_TYPE_CONSENSUS))
  353. return -1; // LCOV_EXCL_LINE
  354. const consensus_flavor_t flavor = as_parsed->flavor;
  355. const time_t valid_after = as_parsed->valid_after;
  356. if (valid_after < approx_time() - 3600 * consdiff_cfg.cache_max_age_hours) {
  357. log_info(LD_DIRSERV, "We don't care about this consensus document; it's "
  358. "too old.");
  359. return -1;
  360. }
  361. /* Do we already have this one? */
  362. consensus_cache_entry_t *entry =
  363. cdm_cache_lookup_consensus(flavor, valid_after);
  364. if (entry) {
  365. log_info(LD_DIRSERV, "We already have a copy of that consensus");
  366. return -1;
  367. }
  368. /* We don't have it. Add it to the cache. */
  369. {
  370. size_t bodylen = strlen(consensus);
  371. config_line_t *labels = NULL;
  372. char formatted_time[ISO_TIME_LEN+1];
  373. format_iso_time_nospace(formatted_time, valid_after);
  374. const char *flavname = networkstatus_get_flavor_name(flavor);
  375. cdm_labels_prepend_sha3(&labels, (const uint8_t *)consensus, bodylen);
  376. config_line_prepend(&labels, LABEL_FLAVOR, flavname);
  377. config_line_prepend(&labels, LABEL_VALID_AFTER, formatted_time);
  378. config_line_prepend(&labels, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  379. entry = consensus_cache_add(cdm_cache_get(),
  380. labels,
  381. (const uint8_t *)consensus,
  382. bodylen);
  383. config_free_lines(labels);
  384. }
  385. if (entry) {
  386. consensus_cache_entry_mark_for_aggressive_release(entry);
  387. consensus_cache_entry_decref(entry);
  388. }
  389. cdm_cache_dirty = 1;
  390. return entry ? 0 : -1;
  391. }
  392. /**
  393. * Helper: used to sort two smartlists of consensus_cache_entry_t by their
  394. * LABEL_VALID_AFTER labels.
  395. */
  396. static int
  397. compare_by_valid_after_(const void **a, const void **b)
  398. {
  399. const consensus_cache_entry_t *e1 = *a;
  400. const consensus_cache_entry_t *e2 = *b;
  401. /* We're in luck here: sorting UTC iso-encoded values lexically will work
  402. * fine (until 9999). */
  403. return strcmp_opt(consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER),
  404. consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER));
  405. }
  406. /**
  407. * Helper: Sort <b>lst</b> by LABEL_VALID_AFTER and return the most recent
  408. * entry.
  409. */
  410. static consensus_cache_entry_t *
  411. sort_and_find_most_recent(smartlist_t *lst)
  412. {
  413. smartlist_sort(lst, compare_by_valid_after_);
  414. if (smartlist_len(lst)) {
  415. return smartlist_get(lst, smartlist_len(lst) - 1);
  416. } else {
  417. return NULL;
  418. }
  419. }
  420. /**
  421. * Look up consensus_cache_entry_t for the consensus of type <b>flavor</b>,
  422. * from the source consensus with the specified digest (which must be SHA3).
  423. *
  424. * If the diff is present, store it into *<b>entry_out</b> and return
  425. * CONSDIFF_AVAILABLE. Otherwise return CONSDIFF_NOT_FOUND or
  426. * CONSDIFF_IN_PROGRESS.
  427. */
  428. consdiff_status_t
  429. consdiffmgr_find_diff_from(consensus_cache_entry_t **entry_out,
  430. consensus_flavor_t flavor,
  431. int digest_type,
  432. const uint8_t *digest,
  433. size_t digestlen)
  434. {
  435. if (BUG(digest_type != DIGEST_SHA3_256) ||
  436. BUG(digestlen != DIGEST256_LEN)) {
  437. return CONSDIFF_NOT_FOUND; // LCOV_EXCL_LINE
  438. }
  439. // Try to look up the entry in the hashtable.
  440. cdm_diff_t search, *ent;
  441. memset(&search, 0, sizeof(search));
  442. search.flavor = flavor;
  443. memcpy(search.from_sha3, digest, DIGEST256_LEN);
  444. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  445. if (ent == NULL ||
  446. ent->cdm_diff_status == CDM_DIFF_ERROR) {
  447. return CONSDIFF_NOT_FOUND;
  448. } else if (ent->cdm_diff_status == CDM_DIFF_IN_PROGRESS) {
  449. return CONSDIFF_IN_PROGRESS;
  450. } else if (BUG(ent->cdm_diff_status != CDM_DIFF_PRESENT)) {
  451. return CONSDIFF_IN_PROGRESS;
  452. }
  453. *entry_out = consensus_cache_entry_handle_get(ent->entry);
  454. return (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  455. #if 0
  456. // XXXX Remove this. I'm keeping it around for now in case we need to
  457. // XXXX debug issues in the hashtable.
  458. char hex[HEX_DIGEST256_LEN+1];
  459. base16_encode(hex, sizeof(hex), (const char *)digest, digestlen);
  460. const char *flavname = networkstatus_get_flavor_name(flavor);
  461. smartlist_t *matches = smartlist_new();
  462. consensus_cache_find_all(matches, cdm_cache_get(),
  463. LABEL_FROM_SHA3_DIGEST, hex);
  464. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  465. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  466. *entry_out = sort_and_find_most_recent(matches);
  467. consdiff_status_t result =
  468. (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  469. smartlist_free(matches);
  470. return result;
  471. #endif
  472. }
  473. /**
  474. * Perform periodic cleanup tasks on the consensus diff cache. Return
  475. * the number of objects marked for deletion.
  476. */
  477. int
  478. consdiffmgr_cleanup(void)
  479. {
  480. smartlist_t *objects = smartlist_new();
  481. smartlist_t *consensuses = smartlist_new();
  482. smartlist_t *diffs = smartlist_new();
  483. int n_to_delete = 0;
  484. log_debug(LD_DIRSERV, "Looking for consdiffmgr entries to remove");
  485. // 1. Delete any consensus or diff or anything whose valid_after is too old.
  486. const time_t valid_after_cutoff =
  487. approx_time() - 3600 * consdiff_cfg.cache_max_age_hours;
  488. consensus_cache_find_all(objects, cdm_cache_get(),
  489. NULL, NULL);
  490. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  491. const char *lv_valid_after =
  492. consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  493. if (! lv_valid_after) {
  494. log_debug(LD_DIRSERV, "Ignoring entry because it had no %s label",
  495. LABEL_VALID_AFTER);
  496. continue;
  497. }
  498. time_t valid_after = 0;
  499. if (parse_iso_time_nospace(lv_valid_after, &valid_after) < 0) {
  500. log_debug(LD_DIRSERV, "Ignoring entry because its %s value (%s) was "
  501. "unparseable", LABEL_VALID_AFTER, escaped(lv_valid_after));
  502. continue;
  503. }
  504. if (valid_after < valid_after_cutoff) {
  505. log_debug(LD_DIRSERV, "Deleting entry because its %s value (%s) was "
  506. "too old", LABEL_VALID_AFTER, lv_valid_after);
  507. consensus_cache_entry_mark_for_removal(ent);
  508. ++n_to_delete;
  509. }
  510. } SMARTLIST_FOREACH_END(ent);
  511. // 2. Delete all diffs that lead to a consensus whose valid-after is not the
  512. // latest.
  513. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  514. const char *flavname = networkstatus_get_flavor_name(flav);
  515. /* Determine the most recent consensus of this flavor */
  516. consensus_cache_find_all(consensuses, cdm_cache_get(),
  517. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  518. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  519. consensus_cache_entry_t *most_recent =
  520. sort_and_find_most_recent(consensuses);
  521. if (most_recent == NULL)
  522. continue;
  523. const char *most_recent_sha3 =
  524. consensus_cache_entry_get_value(most_recent, LABEL_SHA3_DIGEST);
  525. if (BUG(most_recent_sha3 == NULL))
  526. continue; // LCOV_EXCL_LINE
  527. /* consider all such-flavored diffs, and look to see if they match. */
  528. consensus_cache_find_all(diffs, cdm_cache_get(),
  529. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  530. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  531. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  532. const char *this_diff_target_sha3 =
  533. consensus_cache_entry_get_value(diff, LABEL_TARGET_SHA3_DIGEST);
  534. if (!this_diff_target_sha3)
  535. continue;
  536. if (strcmp(this_diff_target_sha3, most_recent_sha3)) {
  537. consensus_cache_entry_mark_for_removal(diff);
  538. ++n_to_delete;
  539. }
  540. } SMARTLIST_FOREACH_END(diff);
  541. smartlist_clear(consensuses);
  542. smartlist_clear(diffs);
  543. }
  544. smartlist_free(objects);
  545. smartlist_free(consensuses);
  546. smartlist_free(diffs);
  547. // Actually remove files, if they're not used.
  548. consensus_cache_delete_pending(cdm_cache_get());
  549. return n_to_delete;
  550. }
  551. /**
  552. * Initialize the consensus diff manager and its cache, and configure
  553. * its parameters based on the latest torrc and networkstatus parameters.
  554. */
  555. void
  556. consdiffmgr_configure(const consdiff_cfg_t *cfg)
  557. {
  558. memcpy(&consdiff_cfg, cfg, sizeof(consdiff_cfg));
  559. (void) cdm_cache_get();
  560. }
  561. /**
  562. * Scan the consensus diff manager's cache for any grossly malformed entries,
  563. * and mark them as deletable. Return 0 if no problems were found; 1
  564. * if problems were found and fixed.
  565. */
  566. int
  567. consdiffmgr_validate(void)
  568. {
  569. /* Right now, we only check for entries that have bad sha3 values */
  570. int problems = 0;
  571. smartlist_t *objects = smartlist_new();
  572. consensus_cache_find_all(objects, cdm_cache_get(),
  573. NULL, NULL);
  574. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, obj) {
  575. uint8_t sha3_expected[DIGEST256_LEN];
  576. uint8_t sha3_received[DIGEST256_LEN];
  577. int r = cdm_entry_get_sha3_value(sha3_expected, obj, LABEL_SHA3_DIGEST);
  578. if (r == -1) {
  579. /* digest isn't there; that's allowed */
  580. continue;
  581. } else if (r == -2) {
  582. /* digest is malformed; that's not allowed */
  583. problems = 1;
  584. consensus_cache_entry_mark_for_removal(obj);
  585. continue;
  586. }
  587. const uint8_t *body;
  588. size_t bodylen;
  589. consensus_cache_entry_incref(obj);
  590. r = consensus_cache_entry_get_body(obj, &body, &bodylen);
  591. if (r == 0) {
  592. crypto_digest256((char *)sha3_received, (const char *)body, bodylen,
  593. DIGEST_SHA3_256);
  594. }
  595. consensus_cache_entry_decref(obj);
  596. if (r < 0)
  597. continue;
  598. if (fast_memneq(sha3_received, sha3_expected, DIGEST256_LEN)) {
  599. problems = 1;
  600. consensus_cache_entry_mark_for_removal(obj);
  601. continue;
  602. }
  603. } SMARTLIST_FOREACH_END(obj);
  604. smartlist_free(objects);
  605. return problems;
  606. }
  607. /**
  608. * Helper: build new diffs of <b>flavor</b> as needed
  609. */
  610. static void
  611. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  612. {
  613. smartlist_t *matches = NULL;
  614. smartlist_t *diffs = NULL;
  615. smartlist_t *compute_diffs_from = NULL;
  616. strmap_t *have_diff_from = NULL;
  617. // look for the most recent consensus, and for all previous in-range
  618. // consensuses. Do they all have diffs to it?
  619. const char *flavname = networkstatus_get_flavor_name(flavor);
  620. // 1. find the most recent consensus, and the ones that we might want
  621. // to diff to it.
  622. matches = smartlist_new();
  623. consensus_cache_find_all(matches, cdm_cache_get(),
  624. LABEL_FLAVOR, flavname);
  625. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  626. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  627. if (!most_recent) {
  628. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  629. "not making diffs", flavname);
  630. goto done;
  631. }
  632. tor_assert(smartlist_len(matches));
  633. smartlist_del(matches, smartlist_len(matches) - 1);
  634. const char *most_recent_valid_after =
  635. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  636. if (BUG(most_recent_valid_after == NULL))
  637. goto done; //LCOV_EXCL_LINE
  638. uint8_t most_recent_sha3[DIGEST256_LEN];
  639. if (BUG(cdm_entry_get_sha3_value(most_recent_sha3, most_recent,
  640. LABEL_SHA3_DIGEST) < 0))
  641. goto done; //LCOV_EXCL_LINE
  642. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  643. // that we don't need to compute.
  644. diffs = smartlist_new();
  645. consensus_cache_find_all(diffs, cdm_cache_get(),
  646. LABEL_VALID_AFTER, most_recent_valid_after);
  647. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  648. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  649. have_diff_from = strmap_new();
  650. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  651. const char *va = consensus_cache_entry_get_value(diff,
  652. LABEL_FROM_VALID_AFTER);
  653. if (BUG(va == NULL))
  654. continue; // LCOV_EXCL_LINE
  655. strmap_set(have_diff_from, va, diff);
  656. } SMARTLIST_FOREACH_END(diff);
  657. // 3. See which consensuses in 'matches' don't have diffs yet.
  658. smartlist_reverse(matches); // from newest to oldest.
  659. compute_diffs_from = smartlist_new();
  660. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  661. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  662. if (BUG(va == NULL))
  663. continue; // LCOV_EXCL_LINE
  664. if (strmap_get(have_diff_from, va) != NULL)
  665. continue; /* we already have this one. */
  666. smartlist_add(compute_diffs_from, ent);
  667. } SMARTLIST_FOREACH_END(ent);
  668. log_info(LD_DIRSERV,
  669. "The most recent %s consensus is valid-after %s. We have diffs to "
  670. "this consensus for %d/%d older %s consensuses. Generating diffs "
  671. "for the other %d.",
  672. flavname,
  673. most_recent_valid_after,
  674. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  675. smartlist_len(matches),
  676. flavname,
  677. smartlist_len(compute_diffs_from));
  678. // 4. Update the hashtable; remove entries in this flavor to other
  679. // target consensuses.
  680. cdm_diff_ht_purge(flavor, most_recent_sha3);
  681. // 5. Actually launch the requests.
  682. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  683. if (BUG(c == most_recent))
  684. continue; // LCOV_EXCL_LINE
  685. uint8_t this_sha3[DIGEST256_LEN];
  686. if (BUG(cdm_entry_get_sha3_value(this_sha3, c, LABEL_SHA3_DIGEST)<0))
  687. continue; // LCOV_EXCL_LINE
  688. if (cdm_diff_ht_check_and_note_pending(flavor,
  689. this_sha3, most_recent_sha3)) {
  690. // This is already pending, or we encountered an error.
  691. continue;
  692. }
  693. consensus_diff_queue_diff_work(c, most_recent);
  694. } SMARTLIST_FOREACH_END(c);
  695. done:
  696. smartlist_free(matches);
  697. smartlist_free(diffs);
  698. smartlist_free(compute_diffs_from);
  699. strmap_free(have_diff_from, NULL);
  700. }
  701. /**
  702. * Scan the cache for diffs, and add them to the hashtable.
  703. */
  704. static void
  705. consdiffmgr_diffs_load(void)
  706. {
  707. smartlist_t *diffs = smartlist_new();
  708. consensus_cache_find_all(diffs, cdm_cache_get(),
  709. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  710. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  711. const char *lv_flavor =
  712. consensus_cache_entry_get_value(diff, LABEL_FLAVOR);
  713. if (!lv_flavor)
  714. continue;
  715. int flavor = networkstatus_parse_flavor_name(lv_flavor);
  716. if (flavor < 0)
  717. continue;
  718. uint8_t from_sha3[DIGEST256_LEN];
  719. uint8_t to_sha3[DIGEST256_LEN];
  720. if (cdm_entry_get_sha3_value(from_sha3, diff, LABEL_FROM_SHA3_DIGEST)<0)
  721. continue;
  722. if (cdm_entry_get_sha3_value(to_sha3, diff, LABEL_TARGET_SHA3_DIGEST)<0)
  723. continue;
  724. cdm_diff_ht_set_status(flavor, from_sha3, to_sha3,
  725. CDM_DIFF_PRESENT,
  726. consensus_cache_entry_handle_new(diff));
  727. } SMARTLIST_FOREACH_END(diff);
  728. smartlist_free(diffs);
  729. }
  730. /**
  731. * Build new diffs as needed.
  732. */
  733. void
  734. consdiffmgr_rescan(void)
  735. {
  736. if (cdm_cache_dirty == 0)
  737. return;
  738. // Clean up here to make room for new diffs, and to ensure that older
  739. // consensuses do not have any entries.
  740. consdiffmgr_cleanup();
  741. if (cdm_cache_loaded == 0) {
  742. consdiffmgr_diffs_load();
  743. cdm_cache_loaded = 1;
  744. }
  745. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  746. consdiffmgr_rescan_flavor_((consensus_flavor_t) flav);
  747. }
  748. cdm_cache_dirty = 0;
  749. }
  750. /**
  751. * Set consensus cache flags on the objects in this consdiffmgr.
  752. */
  753. static void
  754. consdiffmgr_set_cache_flags(void)
  755. {
  756. /* Right now, we just mark the consensus objects for aggressive release,
  757. * so that they get mmapped for as little time as possible. */
  758. smartlist_t *objects = smartlist_new();
  759. consensus_cache_find_all(objects, cdm_cache_get(), LABEL_DOCTYPE,
  760. DOCTYPE_CONSENSUS);
  761. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  762. consensus_cache_entry_mark_for_aggressive_release(ent);
  763. } SMARTLIST_FOREACH_END(ent);
  764. smartlist_free(objects);
  765. }
  766. /**
  767. * Called before shutdown: drop all storage held by the consdiffmgr.c module.
  768. */
  769. void
  770. consdiffmgr_free_all(void)
  771. {
  772. cdm_diff_t **diff, **next;
  773. for (diff = HT_START(cdm_diff_ht, &cdm_diff_ht); diff; diff = next) {
  774. cdm_diff_t *this = *diff;
  775. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  776. cdm_diff_free(this);
  777. }
  778. consensus_cache_free(cons_diff_cache);
  779. cons_diff_cache = NULL;
  780. }
  781. /* =====
  782. Thread workers
  783. =====*/
  784. /**
  785. * An object passed to a worker thread that will try to produce a consensus
  786. * diff.
  787. */
  788. typedef struct consensus_diff_worker_job_t {
  789. /**
  790. * Input: The consensus to compute the diff from. Holds a reference to the
  791. * cache entry, which must not be released until the job is passed back to
  792. * the main thread. The body must be mapped into memory in the main thread.
  793. */
  794. consensus_cache_entry_t *diff_from;
  795. /**
  796. * Input: The consensus to compute the diff to. Holds a reference to the
  797. * cache entry, which must not be released until the job is passed back to
  798. * the main thread. The body must be mapped into memory in the main thread.
  799. */
  800. consensus_cache_entry_t *diff_to;
  801. /**
  802. * Output: Labels to store in the cache associated with this diff.
  803. */
  804. config_line_t *labels_out;
  805. /**
  806. * Output: Body of the diff
  807. */
  808. uint8_t *body_out;
  809. /**
  810. * Output: length of body_out
  811. */
  812. size_t bodylen_out;
  813. } consensus_diff_worker_job_t;
  814. /**
  815. * Worker function. This function runs inside a worker thread and receives
  816. * a consensus_diff_worker_job_t as its input.
  817. */
  818. static workqueue_reply_t
  819. consensus_diff_worker_threadfn(void *state_, void *work_)
  820. {
  821. (void)state_;
  822. consensus_diff_worker_job_t *job = work_;
  823. const uint8_t *diff_from, *diff_to;
  824. size_t len_from, len_to;
  825. int r;
  826. /* We need to have the body already mapped into RAM here.
  827. */
  828. r = consensus_cache_entry_get_body(job->diff_from, &diff_from, &len_from);
  829. if (BUG(r < 0))
  830. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  831. r = consensus_cache_entry_get_body(job->diff_to, &diff_to, &len_to);
  832. if (BUG(r < 0))
  833. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  834. const char *lv_to_valid_after =
  835. consensus_cache_entry_get_value(job->diff_to, LABEL_VALID_AFTER);
  836. const char *lv_from_valid_after =
  837. consensus_cache_entry_get_value(job->diff_from, LABEL_VALID_AFTER);
  838. const char *lv_from_digest =
  839. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  840. const char *lv_from_flavor =
  841. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  842. const char *lv_to_flavor =
  843. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  844. const char *lv_to_digest =
  845. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  846. /* All these values are mandatory on the input */
  847. if (BUG(!lv_to_valid_after) ||
  848. BUG(!lv_from_valid_after) ||
  849. BUG(!lv_from_digest) ||
  850. BUG(!lv_from_flavor) ||
  851. BUG(!lv_to_flavor)) {
  852. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  853. }
  854. /* The flavors need to match */
  855. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  856. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  857. }
  858. char *consensus_diff;
  859. {
  860. // XXXX the input might not be nul-terminated. And also we wanted to
  861. // XXXX support compression later I guess. So, we need to copy here.
  862. char *diff_from_nt, *diff_to_nt;
  863. diff_from_nt = tor_memdup_nulterm(diff_from, len_from);
  864. diff_to_nt = tor_memdup_nulterm(diff_to, len_to);
  865. // XXXX ugh; this is going to calculate the SHA3 of both its
  866. // XXXX inputs again, even though we already have that. Maybe it's time
  867. // XXXX to change the API here?
  868. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  869. tor_free(diff_from_nt);
  870. tor_free(diff_to_nt);
  871. }
  872. if (!consensus_diff) {
  873. /* Couldn't generate consensus; we'll leave the reply blank. */
  874. return WQ_RPL_REPLY;
  875. }
  876. /* Send the reply */
  877. job->body_out = (uint8_t *) consensus_diff;
  878. job->bodylen_out = strlen(consensus_diff);
  879. cdm_labels_prepend_sha3(&job->labels_out, job->body_out, job->bodylen_out);
  880. config_line_prepend(&job->labels_out, LABEL_FROM_VALID_AFTER,
  881. lv_from_valid_after);
  882. config_line_prepend(&job->labels_out, LABEL_VALID_AFTER, lv_to_valid_after);
  883. config_line_prepend(&job->labels_out, LABEL_FLAVOR, lv_from_flavor);
  884. config_line_prepend(&job->labels_out, LABEL_FROM_SHA3_DIGEST,
  885. lv_from_digest);
  886. config_line_prepend(&job->labels_out, LABEL_TARGET_SHA3_DIGEST,
  887. lv_to_digest);
  888. config_line_prepend(&job->labels_out, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  889. return WQ_RPL_REPLY;
  890. }
  891. /**
  892. * Helper: release all storage held in <b>job</b>.
  893. */
  894. static void
  895. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  896. {
  897. if (!job)
  898. return;
  899. tor_free(job->body_out);
  900. config_free_lines(job->labels_out);
  901. consensus_cache_entry_decref(job->diff_from);
  902. consensus_cache_entry_decref(job->diff_to);
  903. tor_free(job);
  904. }
  905. /**
  906. * Worker function: This function runs in the main thread, and receives
  907. * a consensus_diff_worker_job_t that the worker thread has already
  908. * processed.
  909. */
  910. static void
  911. consensus_diff_worker_replyfn(void *work_)
  912. {
  913. tor_assert(in_main_thread());
  914. tor_assert(work_);
  915. consensus_diff_worker_job_t *job = work_;
  916. const char *lv_from_digest =
  917. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  918. const char *lv_to_digest =
  919. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  920. const char *lv_flavor =
  921. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  922. if (BUG(lv_from_digest == NULL))
  923. lv_from_digest = "???"; // LCOV_EXCL_LINE
  924. if (BUG(lv_to_digest == NULL))
  925. lv_to_digest = "???"; // LCOV_EXCL_LINE
  926. uint8_t from_sha3[DIGEST256_LEN];
  927. uint8_t to_sha3[DIGEST256_LEN];
  928. int flav = -1;
  929. int cache = 1;
  930. if (BUG(cdm_entry_get_sha3_value(from_sha3, job->diff_from,
  931. LABEL_SHA3_DIGEST) < 0))
  932. cache = 0;
  933. if (BUG(cdm_entry_get_sha3_value(to_sha3, job->diff_to,
  934. LABEL_SHA3_DIGEST) < 0))
  935. cache = 0;
  936. if (BUG(lv_flavor == NULL)) {
  937. cache = 0;
  938. } else if ((flav = networkstatus_parse_flavor_name(lv_flavor)) < 0) {
  939. cache = 0;
  940. }
  941. int status;
  942. consensus_cache_entry_handle_t *handle = NULL;
  943. if (job->body_out && job->bodylen_out && job->labels_out) {
  944. /* Success! Store the results */
  945. log_info(LD_DIRSERV, "Adding consensus diff from %s to %s",
  946. lv_from_digest, lv_to_digest);
  947. consensus_cache_entry_t *ent =
  948. consensus_cache_add(cdm_cache_get(), job->labels_out,
  949. job->body_out,
  950. job->bodylen_out);
  951. status = CDM_DIFF_PRESENT;
  952. handle = consensus_cache_entry_handle_new(ent);
  953. consensus_cache_entry_decref(ent);
  954. } else {
  955. /* Failure! Nothing to do but complain */
  956. log_warn(LD_DIRSERV,
  957. "Worker was unable to compute consensus diff "
  958. "from %s to %s", lv_from_digest, lv_to_digest);
  959. /* Cache this error so we don't try to compute this one again. */
  960. status = CDM_DIFF_ERROR;
  961. }
  962. if (cache)
  963. cdm_diff_ht_set_status(flav, from_sha3, to_sha3, status, handle);
  964. else
  965. consensus_cache_entry_handle_free(handle);
  966. consensus_diff_worker_job_free(job);
  967. }
  968. /**
  969. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  970. * in a worker thread.
  971. */
  972. static int
  973. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  974. consensus_cache_entry_t *diff_to)
  975. {
  976. tor_assert(in_main_thread());
  977. consensus_cache_entry_incref(diff_from);
  978. consensus_cache_entry_incref(diff_to);
  979. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  980. job->diff_from = diff_from;
  981. job->diff_to = diff_to;
  982. /* Make sure body is mapped. */
  983. const uint8_t *body;
  984. size_t bodylen;
  985. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  986. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  987. if (r1 < 0 || r2 < 0)
  988. goto err;
  989. workqueue_entry_t *work;
  990. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  991. consensus_diff_worker_replyfn,
  992. job);
  993. if (!work)
  994. goto err;
  995. return 0;
  996. err:
  997. consensus_diff_worker_job_free(job); // includes decrefs.
  998. return -1;
  999. }