consdiffmgr.c 39 KB

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