consdiffmgr.c 37 KB

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