consdiffmgr.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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. // Deconfuse coverity about the possibility of sha3_received being
  599. // uninitialized
  600. tor_assert(r <= 0);
  601. if (fast_memneq(sha3_received, sha3_expected, DIGEST256_LEN)) {
  602. problems = 1;
  603. consensus_cache_entry_mark_for_removal(obj);
  604. continue;
  605. }
  606. } SMARTLIST_FOREACH_END(obj);
  607. smartlist_free(objects);
  608. return problems;
  609. }
  610. /**
  611. * Helper: build new diffs of <b>flavor</b> as needed
  612. */
  613. static void
  614. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  615. {
  616. smartlist_t *matches = NULL;
  617. smartlist_t *diffs = NULL;
  618. smartlist_t *compute_diffs_from = NULL;
  619. strmap_t *have_diff_from = NULL;
  620. // look for the most recent consensus, and for all previous in-range
  621. // consensuses. Do they all have diffs to it?
  622. const char *flavname = networkstatus_get_flavor_name(flavor);
  623. // 1. find the most recent consensus, and the ones that we might want
  624. // to diff to it.
  625. matches = smartlist_new();
  626. consensus_cache_find_all(matches, cdm_cache_get(),
  627. LABEL_FLAVOR, flavname);
  628. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  629. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  630. if (!most_recent) {
  631. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  632. "not making diffs", flavname);
  633. goto done;
  634. }
  635. tor_assert(smartlist_len(matches));
  636. smartlist_del(matches, smartlist_len(matches) - 1);
  637. const char *most_recent_valid_after =
  638. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  639. if (BUG(most_recent_valid_after == NULL))
  640. goto done; //LCOV_EXCL_LINE
  641. uint8_t most_recent_sha3[DIGEST256_LEN];
  642. if (BUG(cdm_entry_get_sha3_value(most_recent_sha3, most_recent,
  643. LABEL_SHA3_DIGEST) < 0))
  644. goto done; //LCOV_EXCL_LINE
  645. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  646. // that we don't need to compute.
  647. diffs = smartlist_new();
  648. consensus_cache_find_all(diffs, cdm_cache_get(),
  649. LABEL_VALID_AFTER, most_recent_valid_after);
  650. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  651. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  652. have_diff_from = strmap_new();
  653. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  654. const char *va = consensus_cache_entry_get_value(diff,
  655. LABEL_FROM_VALID_AFTER);
  656. if (BUG(va == NULL))
  657. continue; // LCOV_EXCL_LINE
  658. strmap_set(have_diff_from, va, diff);
  659. } SMARTLIST_FOREACH_END(diff);
  660. // 3. See which consensuses in 'matches' don't have diffs yet.
  661. smartlist_reverse(matches); // from newest to oldest.
  662. compute_diffs_from = smartlist_new();
  663. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  664. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  665. if (BUG(va == NULL))
  666. continue; // LCOV_EXCL_LINE
  667. if (strmap_get(have_diff_from, va) != NULL)
  668. continue; /* we already have this one. */
  669. smartlist_add(compute_diffs_from, ent);
  670. } SMARTLIST_FOREACH_END(ent);
  671. log_info(LD_DIRSERV,
  672. "The most recent %s consensus is valid-after %s. We have diffs to "
  673. "this consensus for %d/%d older %s consensuses. Generating diffs "
  674. "for the other %d.",
  675. flavname,
  676. most_recent_valid_after,
  677. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  678. smartlist_len(matches),
  679. flavname,
  680. smartlist_len(compute_diffs_from));
  681. // 4. Update the hashtable; remove entries in this flavor to other
  682. // target consensuses.
  683. cdm_diff_ht_purge(flavor, most_recent_sha3);
  684. // 5. Actually launch the requests.
  685. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  686. if (BUG(c == most_recent))
  687. continue; // LCOV_EXCL_LINE
  688. uint8_t this_sha3[DIGEST256_LEN];
  689. if (BUG(cdm_entry_get_sha3_value(this_sha3, c, LABEL_SHA3_DIGEST)<0))
  690. continue; // LCOV_EXCL_LINE
  691. if (cdm_diff_ht_check_and_note_pending(flavor,
  692. this_sha3, most_recent_sha3)) {
  693. // This is already pending, or we encountered an error.
  694. continue;
  695. }
  696. consensus_diff_queue_diff_work(c, most_recent);
  697. } SMARTLIST_FOREACH_END(c);
  698. done:
  699. smartlist_free(matches);
  700. smartlist_free(diffs);
  701. smartlist_free(compute_diffs_from);
  702. strmap_free(have_diff_from, NULL);
  703. }
  704. /**
  705. * Scan the cache for diffs, and add them to the hashtable.
  706. */
  707. static void
  708. consdiffmgr_diffs_load(void)
  709. {
  710. smartlist_t *diffs = smartlist_new();
  711. consensus_cache_find_all(diffs, cdm_cache_get(),
  712. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  713. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  714. const char *lv_flavor =
  715. consensus_cache_entry_get_value(diff, LABEL_FLAVOR);
  716. if (!lv_flavor)
  717. continue;
  718. int flavor = networkstatus_parse_flavor_name(lv_flavor);
  719. if (flavor < 0)
  720. continue;
  721. uint8_t from_sha3[DIGEST256_LEN];
  722. uint8_t to_sha3[DIGEST256_LEN];
  723. if (cdm_entry_get_sha3_value(from_sha3, diff, LABEL_FROM_SHA3_DIGEST)<0)
  724. continue;
  725. if (cdm_entry_get_sha3_value(to_sha3, diff, LABEL_TARGET_SHA3_DIGEST)<0)
  726. continue;
  727. cdm_diff_ht_set_status(flavor, from_sha3, to_sha3,
  728. CDM_DIFF_PRESENT,
  729. consensus_cache_entry_handle_new(diff));
  730. } SMARTLIST_FOREACH_END(diff);
  731. smartlist_free(diffs);
  732. }
  733. /**
  734. * Build new diffs as needed.
  735. */
  736. void
  737. consdiffmgr_rescan(void)
  738. {
  739. if (cdm_cache_dirty == 0)
  740. return;
  741. // Clean up here to make room for new diffs, and to ensure that older
  742. // consensuses do not have any entries.
  743. consdiffmgr_cleanup();
  744. if (cdm_cache_loaded == 0) {
  745. consdiffmgr_diffs_load();
  746. cdm_cache_loaded = 1;
  747. }
  748. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  749. consdiffmgr_rescan_flavor_((consensus_flavor_t) flav);
  750. }
  751. cdm_cache_dirty = 0;
  752. }
  753. /**
  754. * Set consensus cache flags on the objects in this consdiffmgr.
  755. */
  756. static void
  757. consdiffmgr_set_cache_flags(void)
  758. {
  759. /* Right now, we just mark the consensus objects for aggressive release,
  760. * so that they get mmapped for as little time as possible. */
  761. smartlist_t *objects = smartlist_new();
  762. consensus_cache_find_all(objects, cdm_cache_get(), LABEL_DOCTYPE,
  763. DOCTYPE_CONSENSUS);
  764. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  765. consensus_cache_entry_mark_for_aggressive_release(ent);
  766. } SMARTLIST_FOREACH_END(ent);
  767. smartlist_free(objects);
  768. }
  769. /**
  770. * Called before shutdown: drop all storage held by the consdiffmgr.c module.
  771. */
  772. void
  773. consdiffmgr_free_all(void)
  774. {
  775. cdm_diff_t **diff, **next;
  776. for (diff = HT_START(cdm_diff_ht, &cdm_diff_ht); diff; diff = next) {
  777. cdm_diff_t *this = *diff;
  778. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  779. cdm_diff_free(this);
  780. }
  781. consensus_cache_free(cons_diff_cache);
  782. cons_diff_cache = NULL;
  783. }
  784. /* =====
  785. Thread workers
  786. =====*/
  787. /**
  788. * An object passed to a worker thread that will try to produce a consensus
  789. * diff.
  790. */
  791. typedef struct consensus_diff_worker_job_t {
  792. /**
  793. * Input: The consensus to compute the diff from. Holds a reference to the
  794. * cache entry, which must not be released until the job is passed back to
  795. * the main thread. The body must be mapped into memory in the main thread.
  796. */
  797. consensus_cache_entry_t *diff_from;
  798. /**
  799. * Input: The consensus to compute the diff to. Holds a reference to the
  800. * cache entry, which must not be released until the job is passed back to
  801. * the main thread. The body must be mapped into memory in the main thread.
  802. */
  803. consensus_cache_entry_t *diff_to;
  804. /**
  805. * Output: Labels to store in the cache associated with this diff.
  806. */
  807. config_line_t *labels_out;
  808. /**
  809. * Output: Body of the diff
  810. */
  811. uint8_t *body_out;
  812. /**
  813. * Output: length of body_out
  814. */
  815. size_t bodylen_out;
  816. } consensus_diff_worker_job_t;
  817. /**
  818. * Worker function. This function runs inside a worker thread and receives
  819. * a consensus_diff_worker_job_t as its input.
  820. */
  821. static workqueue_reply_t
  822. consensus_diff_worker_threadfn(void *state_, void *work_)
  823. {
  824. (void)state_;
  825. consensus_diff_worker_job_t *job = work_;
  826. const uint8_t *diff_from, *diff_to;
  827. size_t len_from, len_to;
  828. int r;
  829. /* We need to have the body already mapped into RAM here.
  830. */
  831. r = consensus_cache_entry_get_body(job->diff_from, &diff_from, &len_from);
  832. if (BUG(r < 0))
  833. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  834. r = consensus_cache_entry_get_body(job->diff_to, &diff_to, &len_to);
  835. if (BUG(r < 0))
  836. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  837. const char *lv_to_valid_after =
  838. consensus_cache_entry_get_value(job->diff_to, LABEL_VALID_AFTER);
  839. const char *lv_from_valid_after =
  840. consensus_cache_entry_get_value(job->diff_from, LABEL_VALID_AFTER);
  841. const char *lv_from_digest =
  842. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  843. const char *lv_from_flavor =
  844. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  845. const char *lv_to_flavor =
  846. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  847. const char *lv_to_digest =
  848. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  849. /* All these values are mandatory on the input */
  850. if (BUG(!lv_to_valid_after) ||
  851. BUG(!lv_from_valid_after) ||
  852. BUG(!lv_from_digest) ||
  853. BUG(!lv_from_flavor) ||
  854. BUG(!lv_to_flavor)) {
  855. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  856. }
  857. /* The flavors need to match */
  858. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  859. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  860. }
  861. char *consensus_diff;
  862. {
  863. // XXXX the input might not be nul-terminated. And also we wanted to
  864. // XXXX support compression later I guess. So, we need to copy here.
  865. char *diff_from_nt, *diff_to_nt;
  866. diff_from_nt = tor_memdup_nulterm(diff_from, len_from);
  867. diff_to_nt = tor_memdup_nulterm(diff_to, len_to);
  868. // XXXX ugh; this is going to calculate the SHA3 of both its
  869. // XXXX inputs again, even though we already have that. Maybe it's time
  870. // XXXX to change the API here?
  871. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  872. tor_free(diff_from_nt);
  873. tor_free(diff_to_nt);
  874. }
  875. if (!consensus_diff) {
  876. /* Couldn't generate consensus; we'll leave the reply blank. */
  877. return WQ_RPL_REPLY;
  878. }
  879. /* Send the reply */
  880. job->body_out = (uint8_t *) consensus_diff;
  881. job->bodylen_out = strlen(consensus_diff);
  882. cdm_labels_prepend_sha3(&job->labels_out, job->body_out, job->bodylen_out);
  883. config_line_prepend(&job->labels_out, LABEL_FROM_VALID_AFTER,
  884. lv_from_valid_after);
  885. config_line_prepend(&job->labels_out, LABEL_VALID_AFTER, lv_to_valid_after);
  886. config_line_prepend(&job->labels_out, LABEL_FLAVOR, lv_from_flavor);
  887. config_line_prepend(&job->labels_out, LABEL_FROM_SHA3_DIGEST,
  888. lv_from_digest);
  889. config_line_prepend(&job->labels_out, LABEL_TARGET_SHA3_DIGEST,
  890. lv_to_digest);
  891. config_line_prepend(&job->labels_out, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  892. return WQ_RPL_REPLY;
  893. }
  894. /**
  895. * Helper: release all storage held in <b>job</b>.
  896. */
  897. static void
  898. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  899. {
  900. if (!job)
  901. return;
  902. tor_free(job->body_out);
  903. config_free_lines(job->labels_out);
  904. consensus_cache_entry_decref(job->diff_from);
  905. consensus_cache_entry_decref(job->diff_to);
  906. tor_free(job);
  907. }
  908. /**
  909. * Worker function: This function runs in the main thread, and receives
  910. * a consensus_diff_worker_job_t that the worker thread has already
  911. * processed.
  912. */
  913. static void
  914. consensus_diff_worker_replyfn(void *work_)
  915. {
  916. tor_assert(in_main_thread());
  917. tor_assert(work_);
  918. consensus_diff_worker_job_t *job = work_;
  919. const char *lv_from_digest =
  920. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  921. const char *lv_to_digest =
  922. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  923. const char *lv_flavor =
  924. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  925. if (BUG(lv_from_digest == NULL))
  926. lv_from_digest = "???"; // LCOV_EXCL_LINE
  927. if (BUG(lv_to_digest == NULL))
  928. lv_to_digest = "???"; // LCOV_EXCL_LINE
  929. uint8_t from_sha3[DIGEST256_LEN];
  930. uint8_t to_sha3[DIGEST256_LEN];
  931. int flav = -1;
  932. int cache = 1;
  933. if (BUG(cdm_entry_get_sha3_value(from_sha3, job->diff_from,
  934. LABEL_SHA3_DIGEST) < 0))
  935. cache = 0;
  936. if (BUG(cdm_entry_get_sha3_value(to_sha3, job->diff_to,
  937. LABEL_SHA3_DIGEST) < 0))
  938. cache = 0;
  939. if (BUG(lv_flavor == NULL)) {
  940. cache = 0;
  941. } else if ((flav = networkstatus_parse_flavor_name(lv_flavor)) < 0) {
  942. cache = 0;
  943. }
  944. int status;
  945. consensus_cache_entry_handle_t *handle = NULL;
  946. if (job->body_out && job->bodylen_out && job->labels_out) {
  947. /* Success! Store the results */
  948. log_info(LD_DIRSERV, "Adding consensus diff from %s to %s",
  949. lv_from_digest, lv_to_digest);
  950. consensus_cache_entry_t *ent =
  951. consensus_cache_add(cdm_cache_get(), job->labels_out,
  952. job->body_out,
  953. job->bodylen_out);
  954. status = CDM_DIFF_PRESENT;
  955. handle = consensus_cache_entry_handle_new(ent);
  956. consensus_cache_entry_decref(ent);
  957. } else {
  958. /* Failure! Nothing to do but complain */
  959. log_warn(LD_DIRSERV,
  960. "Worker was unable to compute consensus diff "
  961. "from %s to %s", lv_from_digest, lv_to_digest);
  962. /* Cache this error so we don't try to compute this one again. */
  963. status = CDM_DIFF_ERROR;
  964. }
  965. if (cache)
  966. cdm_diff_ht_set_status(flav, from_sha3, to_sha3, status, handle);
  967. else
  968. consensus_cache_entry_handle_free(handle);
  969. consensus_diff_worker_job_free(job);
  970. }
  971. /**
  972. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  973. * in a worker thread.
  974. */
  975. static int
  976. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  977. consensus_cache_entry_t *diff_to)
  978. {
  979. tor_assert(in_main_thread());
  980. consensus_cache_entry_incref(diff_from);
  981. consensus_cache_entry_incref(diff_to);
  982. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  983. job->diff_from = diff_from;
  984. job->diff_to = diff_to;
  985. /* Make sure body is mapped. */
  986. const uint8_t *body;
  987. size_t bodylen;
  988. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  989. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  990. if (r1 < 0 || r2 < 0)
  991. goto err;
  992. workqueue_entry_t *work;
  993. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  994. consensus_diff_worker_replyfn,
  995. job);
  996. if (!work)
  997. goto err;
  998. return 0;
  999. err:
  1000. consensus_diff_worker_job_free(job); // includes decrefs.
  1001. return -1;
  1002. }