consdiffmgr.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. /**
  21. * Labels to apply to items in the conscache object.
  22. *
  23. * @{
  24. */
  25. /* One of DOCTYPE_CONSENSUS or DOCTYPE_CONSENSUS_DIFF */
  26. #define LABEL_DOCTYPE "document-type"
  27. /* The valid-after time for a consensus (or for the target consensus of a
  28. * diff), encoded as ISO UTC. */
  29. #define LABEL_VALID_AFTER "consensus-valid-after"
  30. /* A hex encoded SHA3 digest of the object, as compressed (if any) */
  31. #define LABEL_SHA3_DIGEST "sha3-digest"
  32. /* A hex encoded SHA3 digest of the object before compression. */
  33. #define LABEL_SHA3_DIGEST_UNCOMPRESSED "sha3-digest-uncompressed"
  34. /* The flavor of the consensus or consensuses diff */
  35. #define LABEL_FLAVOR "consensus-flavor"
  36. /* Diff only: the SHA3 digest of the source consensus. */
  37. #define LABEL_FROM_SHA3_DIGEST "from-sha3-digest"
  38. /* Diff only: the SHA3 digest of the target consensus. */
  39. #define LABEL_TARGET_SHA3_DIGEST "target-sha3-digest"
  40. /* Diff only: the valid-after date of the source consensus. */
  41. #define LABEL_FROM_VALID_AFTER "from-valid-after"
  42. /* What kind of compression was used? */
  43. #define LABEL_COMPRESSION_TYPE "compression"
  44. /** @} */
  45. #define DOCTYPE_CONSENSUS "consensus"
  46. #define DOCTYPE_CONSENSUS_DIFF "consensus-diff"
  47. /**
  48. * Underlying directory that stores consensuses and consensus diffs. Don't
  49. * use this directly: use cdm_cache_get() instead.
  50. */
  51. static consensus_cache_t *cons_diff_cache = NULL;
  52. /**
  53. * If true, we have learned at least one new consensus since the
  54. * consensus cache was last up-to-date.
  55. */
  56. static int cdm_cache_dirty = 0;
  57. /**
  58. * If true, we have scanned the cache to update our hashtable of diffs.
  59. */
  60. static int cdm_cache_loaded = 0;
  61. /**
  62. * Possible status values for cdm_diff_t.cdm_diff_status
  63. **/
  64. typedef enum cdm_diff_status_t {
  65. CDM_DIFF_PRESENT=1,
  66. CDM_DIFF_IN_PROGRESS=2,
  67. CDM_DIFF_ERROR=3,
  68. } cdm_diff_status_t;
  69. /** Hashtable node used to remember the current status of the diff
  70. * from a given sha3 digest to the current consensus. */
  71. typedef struct cdm_diff_t {
  72. HT_ENTRY(cdm_diff_t) node;
  73. /** Consensus flavor for this diff (part of ht key) */
  74. consensus_flavor_t flavor;
  75. /** SHA3-256 digest of the consensus that this diff is _from_. (part of the
  76. * ht key) */
  77. uint8_t from_sha3[DIGEST256_LEN];
  78. /** One of the CDM_DIFF_* values, depending on whether this diff
  79. * is available, in progress, or impossible to compute. */
  80. cdm_diff_status_t cdm_diff_status;
  81. /** SHA3-256 digest of the consensus that this diff is _to. */
  82. uint8_t target_sha3[DIGEST256_LEN];
  83. /** Handle to the cache entry for this diff, if any. We use a handle here
  84. * to avoid thinking too hard about cache entry lifetime issues. */
  85. consensus_cache_entry_handle_t *entry;
  86. } cdm_diff_t;
  87. /** Hashtable mapping flavor and source consensus digest to status. */
  88. static HT_HEAD(cdm_diff_ht, cdm_diff_t) cdm_diff_ht = HT_INITIALIZER();
  89. /**
  90. * Configuration for this module
  91. */
  92. static consdiff_cfg_t consdiff_cfg = {
  93. /* .cache_max_age_hours = */ 24 * 90,
  94. /* .cache_max_num = */ 1440
  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 <b>label</b> as its label.
  275. */
  276. static void
  277. cdm_labels_prepend_sha3(config_line_t **labels,
  278. const char *label,
  279. const uint8_t *body,
  280. size_t bodylen)
  281. {
  282. uint8_t sha3_digest[DIGEST256_LEN];
  283. char hexdigest[HEX_DIGEST256_LEN+1];
  284. crypto_digest256((char *)sha3_digest,
  285. (const char *)body, bodylen, DIGEST_SHA3_256);
  286. base16_encode(hexdigest, sizeof(hexdigest),
  287. (const char *)sha3_digest, sizeof(sha3_digest));
  288. config_line_prepend(labels, label, hexdigest);
  289. }
  290. /** Helper: if there is a sha3-256 hex-encoded digest in <b>ent</b> with the
  291. * given label, set <b>digest_out</b> to that value (decoded), and return 0.
  292. *
  293. * Return -1 if there is no such label, and -2 if it is badly formatted. */
  294. STATIC int
  295. cdm_entry_get_sha3_value(uint8_t *digest_out,
  296. consensus_cache_entry_t *ent,
  297. const char *label)
  298. {
  299. if (ent == NULL)
  300. return -1;
  301. const char *hex = consensus_cache_entry_get_value(ent, label);
  302. if (hex == NULL)
  303. return -1;
  304. int n = base16_decode((char*)digest_out, DIGEST256_LEN, hex, strlen(hex));
  305. if (n != DIGEST256_LEN)
  306. return -2;
  307. else
  308. return 0;
  309. }
  310. /**
  311. * Helper: look for a consensus with the given <b>flavor</b> and
  312. * <b>valid_after</b> time in the cache. Return that consensus if it's
  313. * present, or NULL if it's missing.
  314. */
  315. STATIC consensus_cache_entry_t *
  316. cdm_cache_lookup_consensus(consensus_flavor_t flavor, time_t valid_after)
  317. {
  318. char formatted_time[ISO_TIME_LEN+1];
  319. format_iso_time_nospace(formatted_time, valid_after);
  320. const char *flavname = networkstatus_get_flavor_name(flavor);
  321. /* We'll filter by valid-after time first, since that should
  322. * match the fewest documents. */
  323. /* We could add an extra hashtable here, but since we only do this scan
  324. * when adding a new consensus, it probably doesn't matter much. */
  325. smartlist_t *matches = smartlist_new();
  326. consensus_cache_find_all(matches, cdm_cache_get(),
  327. LABEL_VALID_AFTER, formatted_time);
  328. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  329. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  330. consensus_cache_entry_t *result = NULL;
  331. if (smartlist_len(matches) > 1) {
  332. log_warn(LD_BUG, "How odd; there appear to be two matching consensuses "
  333. "with flavor %s published at %s.",
  334. flavname, formatted_time);
  335. }
  336. if (smartlist_len(matches)) {
  337. result = smartlist_get(matches, 0);
  338. }
  339. smartlist_free(matches);
  340. return result;
  341. }
  342. /**
  343. * Given a string containing a networkstatus consensus, and the results of
  344. * having parsed that consensus, add that consensus to the cache if it is not
  345. * already present and not too old. Create new consensus diffs from or to
  346. * that consensus as appropriate.
  347. *
  348. * Return 0 on success and -1 on failure.
  349. */
  350. int
  351. consdiffmgr_add_consensus(const char *consensus,
  352. const networkstatus_t *as_parsed)
  353. {
  354. if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
  355. return -1; // LCOV_EXCL_LINE
  356. if (BUG(as_parsed->type != NS_TYPE_CONSENSUS))
  357. return -1; // LCOV_EXCL_LINE
  358. const consensus_flavor_t flavor = as_parsed->flavor;
  359. const time_t valid_after = as_parsed->valid_after;
  360. if (valid_after < approx_time() - 3600 * consdiff_cfg.cache_max_age_hours) {
  361. log_info(LD_DIRSERV, "We don't care about this consensus document; it's "
  362. "too old.");
  363. return -1;
  364. }
  365. /* Do we already have this one? */
  366. consensus_cache_entry_t *entry =
  367. cdm_cache_lookup_consensus(flavor, valid_after);
  368. if (entry) {
  369. log_info(LD_DIRSERV, "We already have a copy of that consensus");
  370. return -1;
  371. }
  372. /* We don't have it. Add it to the cache. */
  373. {
  374. size_t bodylen = strlen(consensus);
  375. config_line_t *labels = NULL;
  376. char formatted_time[ISO_TIME_LEN+1];
  377. format_iso_time_nospace(formatted_time, valid_after);
  378. const char *flavname = networkstatus_get_flavor_name(flavor);
  379. cdm_labels_prepend_sha3(&labels, LABEL_SHA3_DIGEST,
  380. (const uint8_t *)consensus, bodylen);
  381. cdm_labels_prepend_sha3(&labels, LABEL_SHA3_DIGEST_UNCOMPRESSED,
  382. (const uint8_t *)consensus, bodylen);
  383. config_line_prepend(&labels, LABEL_FLAVOR, flavname);
  384. config_line_prepend(&labels, LABEL_VALID_AFTER, formatted_time);
  385. config_line_prepend(&labels, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  386. entry = consensus_cache_add(cdm_cache_get(),
  387. labels,
  388. (const uint8_t *)consensus,
  389. bodylen);
  390. config_free_lines(labels);
  391. }
  392. if (entry) {
  393. consensus_cache_entry_mark_for_aggressive_release(entry);
  394. consensus_cache_entry_decref(entry);
  395. }
  396. cdm_cache_dirty = 1;
  397. return entry ? 0 : -1;
  398. }
  399. /**
  400. * Helper: used to sort two smartlists of consensus_cache_entry_t by their
  401. * LABEL_VALID_AFTER labels.
  402. */
  403. static int
  404. compare_by_valid_after_(const void **a, const void **b)
  405. {
  406. const consensus_cache_entry_t *e1 = *a;
  407. const consensus_cache_entry_t *e2 = *b;
  408. /* We're in luck here: sorting UTC iso-encoded values lexically will work
  409. * fine (until 9999). */
  410. return strcmp_opt(consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER),
  411. consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER));
  412. }
  413. /**
  414. * Helper: Sort <b>lst</b> by LABEL_VALID_AFTER and return the most recent
  415. * entry.
  416. */
  417. static consensus_cache_entry_t *
  418. sort_and_find_most_recent(smartlist_t *lst)
  419. {
  420. smartlist_sort(lst, compare_by_valid_after_);
  421. if (smartlist_len(lst)) {
  422. return smartlist_get(lst, smartlist_len(lst) - 1);
  423. } else {
  424. return NULL;
  425. }
  426. }
  427. /**
  428. * Look up consensus_cache_entry_t for the consensus of type <b>flavor</b>,
  429. * from the source consensus with the specified digest (which must be SHA3).
  430. *
  431. * If the diff is present, store it into *<b>entry_out</b> and return
  432. * CONSDIFF_AVAILABLE. Otherwise return CONSDIFF_NOT_FOUND or
  433. * CONSDIFF_IN_PROGRESS.
  434. */
  435. consdiff_status_t
  436. consdiffmgr_find_diff_from(consensus_cache_entry_t **entry_out,
  437. consensus_flavor_t flavor,
  438. int digest_type,
  439. const uint8_t *digest,
  440. size_t digestlen)
  441. {
  442. if (BUG(digest_type != DIGEST_SHA3_256) ||
  443. BUG(digestlen != DIGEST256_LEN)) {
  444. return CONSDIFF_NOT_FOUND; // LCOV_EXCL_LINE
  445. }
  446. // Try to look up the entry in the hashtable.
  447. cdm_diff_t search, *ent;
  448. memset(&search, 0, sizeof(search));
  449. search.flavor = flavor;
  450. memcpy(search.from_sha3, digest, DIGEST256_LEN);
  451. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  452. if (ent == NULL ||
  453. ent->cdm_diff_status == CDM_DIFF_ERROR) {
  454. return CONSDIFF_NOT_FOUND;
  455. } else if (ent->cdm_diff_status == CDM_DIFF_IN_PROGRESS) {
  456. return CONSDIFF_IN_PROGRESS;
  457. } else if (BUG(ent->cdm_diff_status != CDM_DIFF_PRESENT)) {
  458. return CONSDIFF_IN_PROGRESS;
  459. }
  460. *entry_out = consensus_cache_entry_handle_get(ent->entry);
  461. return (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  462. #if 0
  463. // XXXX Remove this. I'm keeping it around for now in case we need to
  464. // XXXX debug issues in the hashtable.
  465. char hex[HEX_DIGEST256_LEN+1];
  466. base16_encode(hex, sizeof(hex), (const char *)digest, digestlen);
  467. const char *flavname = networkstatus_get_flavor_name(flavor);
  468. smartlist_t *matches = smartlist_new();
  469. consensus_cache_find_all(matches, cdm_cache_get(),
  470. LABEL_FROM_SHA3_DIGEST, hex);
  471. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  472. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  473. *entry_out = sort_and_find_most_recent(matches);
  474. consdiff_status_t result =
  475. (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  476. smartlist_free(matches);
  477. return result;
  478. #endif
  479. }
  480. /**
  481. * Perform periodic cleanup tasks on the consensus diff cache. Return
  482. * the number of objects marked for deletion.
  483. */
  484. int
  485. consdiffmgr_cleanup(void)
  486. {
  487. smartlist_t *objects = smartlist_new();
  488. smartlist_t *consensuses = smartlist_new();
  489. smartlist_t *diffs = smartlist_new();
  490. int n_to_delete = 0;
  491. log_debug(LD_DIRSERV, "Looking for consdiffmgr entries to remove");
  492. // 1. Delete any consensus or diff or anything whose valid_after is too old.
  493. const time_t valid_after_cutoff =
  494. approx_time() - 3600 * consdiff_cfg.cache_max_age_hours;
  495. consensus_cache_find_all(objects, cdm_cache_get(),
  496. NULL, NULL);
  497. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  498. const char *lv_valid_after =
  499. consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  500. if (! lv_valid_after) {
  501. log_debug(LD_DIRSERV, "Ignoring entry because it had no %s label",
  502. LABEL_VALID_AFTER);
  503. continue;
  504. }
  505. time_t valid_after = 0;
  506. if (parse_iso_time_nospace(lv_valid_after, &valid_after) < 0) {
  507. log_debug(LD_DIRSERV, "Ignoring entry because its %s value (%s) was "
  508. "unparseable", LABEL_VALID_AFTER, escaped(lv_valid_after));
  509. continue;
  510. }
  511. if (valid_after < valid_after_cutoff) {
  512. log_debug(LD_DIRSERV, "Deleting entry because its %s value (%s) was "
  513. "too old", LABEL_VALID_AFTER, lv_valid_after);
  514. consensus_cache_entry_mark_for_removal(ent);
  515. ++n_to_delete;
  516. }
  517. } SMARTLIST_FOREACH_END(ent);
  518. // 2. Delete all diffs that lead to a consensus whose valid-after is not the
  519. // latest.
  520. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  521. const char *flavname = networkstatus_get_flavor_name(flav);
  522. /* Determine the most recent consensus of this flavor */
  523. consensus_cache_find_all(consensuses, cdm_cache_get(),
  524. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  525. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  526. consensus_cache_entry_t *most_recent =
  527. sort_and_find_most_recent(consensuses);
  528. if (most_recent == NULL)
  529. continue;
  530. const char *most_recent_sha3 =
  531. consensus_cache_entry_get_value(most_recent,
  532. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  533. if (BUG(most_recent_sha3 == NULL))
  534. continue; // LCOV_EXCL_LINE
  535. /* consider all such-flavored diffs, and look to see if they match. */
  536. consensus_cache_find_all(diffs, cdm_cache_get(),
  537. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  538. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  539. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  540. const char *this_diff_target_sha3 =
  541. consensus_cache_entry_get_value(diff, LABEL_TARGET_SHA3_DIGEST);
  542. if (!this_diff_target_sha3)
  543. continue;
  544. if (strcmp(this_diff_target_sha3, most_recent_sha3)) {
  545. consensus_cache_entry_mark_for_removal(diff);
  546. ++n_to_delete;
  547. }
  548. } SMARTLIST_FOREACH_END(diff);
  549. smartlist_clear(consensuses);
  550. smartlist_clear(diffs);
  551. }
  552. smartlist_free(objects);
  553. smartlist_free(consensuses);
  554. smartlist_free(diffs);
  555. // Actually remove files, if they're not used.
  556. consensus_cache_delete_pending(cdm_cache_get());
  557. return n_to_delete;
  558. }
  559. /**
  560. * Initialize the consensus diff manager and its cache, and configure
  561. * its parameters based on the latest torrc and networkstatus parameters.
  562. */
  563. void
  564. consdiffmgr_configure(const consdiff_cfg_t *cfg)
  565. {
  566. memcpy(&consdiff_cfg, cfg, sizeof(consdiff_cfg));
  567. (void) cdm_cache_get();
  568. }
  569. /**
  570. * Scan the consensus diff manager's cache for any grossly malformed entries,
  571. * and mark them as deletable. Return 0 if no problems were found; 1
  572. * if problems were found and fixed.
  573. */
  574. int
  575. consdiffmgr_validate(void)
  576. {
  577. /* Right now, we only check for entries that have bad sha3 values */
  578. int problems = 0;
  579. smartlist_t *objects = smartlist_new();
  580. consensus_cache_find_all(objects, cdm_cache_get(),
  581. NULL, NULL);
  582. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, obj) {
  583. uint8_t sha3_expected[DIGEST256_LEN];
  584. uint8_t sha3_received[DIGEST256_LEN];
  585. int r = cdm_entry_get_sha3_value(sha3_expected, obj, LABEL_SHA3_DIGEST);
  586. if (r == -1) {
  587. /* digest isn't there; that's allowed */
  588. continue;
  589. } else if (r == -2) {
  590. /* digest is malformed; that's not allowed */
  591. problems = 1;
  592. consensus_cache_entry_mark_for_removal(obj);
  593. continue;
  594. }
  595. const uint8_t *body;
  596. size_t bodylen;
  597. consensus_cache_entry_incref(obj);
  598. r = consensus_cache_entry_get_body(obj, &body, &bodylen);
  599. if (r == 0) {
  600. crypto_digest256((char *)sha3_received, (const char *)body, bodylen,
  601. DIGEST_SHA3_256);
  602. }
  603. consensus_cache_entry_decref(obj);
  604. if (r < 0)
  605. continue;
  606. if (fast_memneq(sha3_received, sha3_expected, DIGEST256_LEN)) {
  607. problems = 1;
  608. consensus_cache_entry_mark_for_removal(obj);
  609. continue;
  610. }
  611. } SMARTLIST_FOREACH_END(obj);
  612. smartlist_free(objects);
  613. return problems;
  614. }
  615. /**
  616. * Helper: build new diffs of <b>flavor</b> as needed
  617. */
  618. static void
  619. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  620. {
  621. smartlist_t *matches = NULL;
  622. smartlist_t *diffs = NULL;
  623. smartlist_t *compute_diffs_from = NULL;
  624. strmap_t *have_diff_from = NULL;
  625. // look for the most recent consensus, and for all previous in-range
  626. // consensuses. Do they all have diffs to it?
  627. const char *flavname = networkstatus_get_flavor_name(flavor);
  628. // 1. find the most recent consensus, and the ones that we might want
  629. // to diff to it.
  630. matches = smartlist_new();
  631. consensus_cache_find_all(matches, cdm_cache_get(),
  632. LABEL_FLAVOR, flavname);
  633. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  634. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  635. if (!most_recent) {
  636. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  637. "not making diffs", flavname);
  638. goto done;
  639. }
  640. tor_assert(smartlist_len(matches));
  641. smartlist_del(matches, smartlist_len(matches) - 1);
  642. const char *most_recent_valid_after =
  643. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  644. if (BUG(most_recent_valid_after == NULL))
  645. goto done; //LCOV_EXCL_LINE
  646. uint8_t most_recent_sha3[DIGEST256_LEN];
  647. if (BUG(cdm_entry_get_sha3_value(most_recent_sha3, most_recent,
  648. LABEL_SHA3_DIGEST_UNCOMPRESSED) < 0))
  649. goto done; //LCOV_EXCL_LINE
  650. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  651. // that we don't need to compute.
  652. diffs = smartlist_new();
  653. consensus_cache_find_all(diffs, cdm_cache_get(),
  654. LABEL_VALID_AFTER, most_recent_valid_after);
  655. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  656. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  657. have_diff_from = strmap_new();
  658. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  659. const char *va = consensus_cache_entry_get_value(diff,
  660. LABEL_FROM_VALID_AFTER);
  661. if (BUG(va == NULL))
  662. continue; // LCOV_EXCL_LINE
  663. strmap_set(have_diff_from, va, diff);
  664. } SMARTLIST_FOREACH_END(diff);
  665. // 3. See which consensuses in 'matches' don't have diffs yet.
  666. smartlist_reverse(matches); // from newest to oldest.
  667. compute_diffs_from = smartlist_new();
  668. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  669. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  670. if (BUG(va == NULL))
  671. continue; // LCOV_EXCL_LINE
  672. if (strmap_get(have_diff_from, va) != NULL)
  673. continue; /* we already have this one. */
  674. smartlist_add(compute_diffs_from, ent);
  675. } SMARTLIST_FOREACH_END(ent);
  676. log_info(LD_DIRSERV,
  677. "The most recent %s consensus is valid-after %s. We have diffs to "
  678. "this consensus for %d/%d older %s consensuses. Generating diffs "
  679. "for the other %d.",
  680. flavname,
  681. most_recent_valid_after,
  682. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  683. smartlist_len(matches),
  684. flavname,
  685. smartlist_len(compute_diffs_from));
  686. // 4. Update the hashtable; remove entries in this flavor to other
  687. // target consensuses.
  688. cdm_diff_ht_purge(flavor, most_recent_sha3);
  689. // 5. Actually launch the requests.
  690. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  691. if (BUG(c == most_recent))
  692. continue; // LCOV_EXCL_LINE
  693. uint8_t this_sha3[DIGEST256_LEN];
  694. if (BUG(cdm_entry_get_sha3_value(this_sha3, c,
  695. LABEL_SHA3_DIGEST_UNCOMPRESSED)<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,
  849. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  850. const char *lv_from_flavor =
  851. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  852. const char *lv_to_flavor =
  853. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  854. const char *lv_to_digest =
  855. consensus_cache_entry_get_value(job->diff_to,
  856. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  857. /* All these values are mandatory on the input */
  858. if (BUG(!lv_to_valid_after) ||
  859. BUG(!lv_from_valid_after) ||
  860. BUG(!lv_from_digest) ||
  861. BUG(!lv_from_flavor) ||
  862. BUG(!lv_to_flavor)) {
  863. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  864. }
  865. /* The flavors need to match */
  866. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  867. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  868. }
  869. char *consensus_diff;
  870. {
  871. // XXXX the input might not be nul-terminated. And also we wanted to
  872. // XXXX support compression later I guess. So, we need to copy here.
  873. char *diff_from_nt, *diff_to_nt;
  874. diff_from_nt = tor_memdup_nulterm(diff_from, len_from);
  875. diff_to_nt = tor_memdup_nulterm(diff_to, len_to);
  876. // XXXX ugh; this is going to calculate the SHA3 of both its
  877. // XXXX inputs again, even though we already have that. Maybe it's time
  878. // XXXX to change the API here?
  879. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  880. tor_free(diff_from_nt);
  881. tor_free(diff_to_nt);
  882. }
  883. if (!consensus_diff) {
  884. /* Couldn't generate consensus; we'll leave the reply blank. */
  885. return WQ_RPL_REPLY;
  886. }
  887. /* Send the reply */
  888. job->body_out = (uint8_t *) consensus_diff;
  889. job->bodylen_out = strlen(consensus_diff);
  890. cdm_labels_prepend_sha3(&job->labels_out, LABEL_SHA3_DIGEST,
  891. job->body_out, job->bodylen_out);
  892. cdm_labels_prepend_sha3(&job->labels_out, LABEL_SHA3_DIGEST_UNCOMPRESSED,
  893. job->body_out, job->bodylen_out);
  894. config_line_prepend(&job->labels_out, LABEL_FROM_VALID_AFTER,
  895. lv_from_valid_after);
  896. config_line_prepend(&job->labels_out, LABEL_VALID_AFTER, lv_to_valid_after);
  897. config_line_prepend(&job->labels_out, LABEL_FLAVOR, lv_from_flavor);
  898. config_line_prepend(&job->labels_out, LABEL_FROM_SHA3_DIGEST,
  899. lv_from_digest);
  900. config_line_prepend(&job->labels_out, LABEL_TARGET_SHA3_DIGEST,
  901. lv_to_digest);
  902. config_line_prepend(&job->labels_out, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  903. return WQ_RPL_REPLY;
  904. }
  905. /**
  906. * Helper: release all storage held in <b>job</b>.
  907. */
  908. static void
  909. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  910. {
  911. if (!job)
  912. return;
  913. tor_free(job->body_out);
  914. config_free_lines(job->labels_out);
  915. consensus_cache_entry_decref(job->diff_from);
  916. consensus_cache_entry_decref(job->diff_to);
  917. tor_free(job);
  918. }
  919. /**
  920. * Worker function: This function runs in the main thread, and receives
  921. * a consensus_diff_worker_job_t that the worker thread has already
  922. * processed.
  923. */
  924. static void
  925. consensus_diff_worker_replyfn(void *work_)
  926. {
  927. tor_assert(in_main_thread());
  928. tor_assert(work_);
  929. consensus_diff_worker_job_t *job = work_;
  930. const char *lv_from_digest =
  931. consensus_cache_entry_get_value(job->diff_from,
  932. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  933. const char *lv_to_digest =
  934. consensus_cache_entry_get_value(job->diff_to,
  935. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  936. const char *lv_flavor =
  937. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  938. if (BUG(lv_from_digest == NULL))
  939. lv_from_digest = "???"; // LCOV_EXCL_LINE
  940. if (BUG(lv_to_digest == NULL))
  941. lv_to_digest = "???"; // LCOV_EXCL_LINE
  942. uint8_t from_sha3[DIGEST256_LEN];
  943. uint8_t to_sha3[DIGEST256_LEN];
  944. int flav = -1;
  945. int cache = 1;
  946. if (BUG(cdm_entry_get_sha3_value(from_sha3, job->diff_from,
  947. LABEL_SHA3_DIGEST_UNCOMPRESSED) < 0))
  948. cache = 0;
  949. if (BUG(cdm_entry_get_sha3_value(to_sha3, job->diff_to,
  950. LABEL_SHA3_DIGEST_UNCOMPRESSED) < 0))
  951. cache = 0;
  952. if (BUG(lv_flavor == NULL)) {
  953. cache = 0;
  954. } else if ((flav = networkstatus_parse_flavor_name(lv_flavor)) < 0) {
  955. cache = 0;
  956. }
  957. int status;
  958. consensus_cache_entry_handle_t *handle = NULL;
  959. if (job->body_out && job->bodylen_out && job->labels_out) {
  960. /* Success! Store the results */
  961. log_info(LD_DIRSERV, "Adding consensus diff from %s to %s",
  962. lv_from_digest, lv_to_digest);
  963. consensus_cache_entry_t *ent =
  964. consensus_cache_add(cdm_cache_get(), job->labels_out,
  965. job->body_out,
  966. job->bodylen_out);
  967. status = CDM_DIFF_PRESENT;
  968. handle = consensus_cache_entry_handle_new(ent);
  969. consensus_cache_entry_decref(ent);
  970. } else {
  971. /* Failure! Nothing to do but complain */
  972. log_warn(LD_DIRSERV,
  973. "Worker was unable to compute consensus diff "
  974. "from %s to %s", lv_from_digest, lv_to_digest);
  975. /* Cache this error so we don't try to compute this one again. */
  976. status = CDM_DIFF_ERROR;
  977. }
  978. if (cache)
  979. cdm_diff_ht_set_status(flav, from_sha3, to_sha3, status, handle);
  980. else
  981. consensus_cache_entry_handle_free(handle);
  982. consensus_diff_worker_job_free(job);
  983. }
  984. /**
  985. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  986. * in a worker thread.
  987. */
  988. static int
  989. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  990. consensus_cache_entry_t *diff_to)
  991. {
  992. tor_assert(in_main_thread());
  993. consensus_cache_entry_incref(diff_from);
  994. consensus_cache_entry_incref(diff_to);
  995. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  996. job->diff_from = diff_from;
  997. job->diff_to = diff_to;
  998. /* Make sure body is mapped. */
  999. const uint8_t *body;
  1000. size_t bodylen;
  1001. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  1002. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  1003. if (r1 < 0 || r2 < 0)
  1004. goto err;
  1005. workqueue_entry_t *work;
  1006. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  1007. consensus_diff_worker_replyfn,
  1008. job);
  1009. if (!work)
  1010. goto err;
  1011. return 0;
  1012. err:
  1013. consensus_diff_worker_job_free(job); // includes decrefs.
  1014. return -1;
  1015. }