consdiffmgr.c 36 KB

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