consdiffmgr.c 39 KB

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