consdiffmgr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. * Configuration for this module
  56. */
  57. static consdiff_cfg_t consdiff_cfg = {
  58. /* .cache_max_age_hours = */ 24 * 90,
  59. /* .cache_max_num = */ 1440
  60. };
  61. static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  62. consensus_cache_entry_t *diff_to);
  63. static void consdiffmgr_set_cache_flags(void);
  64. /**
  65. * Helper: initialize <b>cons_diff_cache</b>.
  66. */
  67. static void
  68. cdm_cache_init(void)
  69. {
  70. unsigned n_entries = consdiff_cfg.cache_max_num * 2;
  71. tor_assert(cons_diff_cache == NULL);
  72. cons_diff_cache = consensus_cache_open("diff-cache", n_entries);
  73. if (cons_diff_cache == NULL) {
  74. // LCOV_EXCL_START
  75. log_err(LD_FS, "Error: Couldn't open storage for consensus diffs.");
  76. tor_assert_unreached();
  77. // LCOV_EXCL_STOP
  78. } else {
  79. consdiffmgr_set_cache_flags();
  80. }
  81. cdm_cache_dirty = 1;
  82. }
  83. /**
  84. * Helper: return the consensus_cache_t * that backs this manager,
  85. * initializing it if needed.
  86. */
  87. STATIC consensus_cache_t *
  88. cdm_cache_get(void)
  89. {
  90. if (PREDICT_UNLIKELY(cons_diff_cache == NULL)) {
  91. cdm_cache_init();
  92. }
  93. return cons_diff_cache;
  94. }
  95. /**
  96. * Helper: given a list of labels, prepend the hex-encoded SHA3 digest
  97. * of the <b>bodylen</b>-byte object at <b>body</b> to those labels,
  98. * with LABEL_SHA3_DIGEST as its label.
  99. */
  100. static void
  101. cdm_labels_prepend_sha3(config_line_t **labels,
  102. const uint8_t *body,
  103. size_t bodylen)
  104. {
  105. uint8_t sha3_digest[DIGEST256_LEN];
  106. char hexdigest[HEX_DIGEST256_LEN+1];
  107. crypto_digest256((char *)sha3_digest,
  108. (const char *)body, bodylen, DIGEST_SHA3_256);
  109. base16_encode(hexdigest, sizeof(hexdigest),
  110. (const char *)sha3_digest, sizeof(sha3_digest));
  111. config_line_prepend(labels, LABEL_SHA3_DIGEST, hexdigest);
  112. }
  113. /** Helper: if there is a sha3-256 hex-encoded digest in <b>ent</b> with the
  114. * given label, set <b>digest_out</b> to that value (decoded), and return 0.
  115. *
  116. * Return -1 if there is no such label, and -2 if it is badly formatted. */
  117. static int
  118. cdm_entry_get_sha3_value(uint8_t *digest_out,
  119. consensus_cache_entry_t *ent,
  120. const char *label)
  121. {
  122. if (ent == NULL)
  123. return -1;
  124. const char *hex = consensus_cache_entry_get_value(ent, label);
  125. if (hex == NULL)
  126. return -1;
  127. int n = base16_decode((char*)digest_out, DIGEST256_LEN, hex, strlen(hex));
  128. if (n != DIGEST256_LEN)
  129. return -2;
  130. else
  131. return 0;
  132. }
  133. /**
  134. * Helper: look for a consensus with the given <b>flavor</b> and
  135. * <b>valid_after</b> time in the cache. Return that consensus if it's
  136. * present, or NULL if it's missing.
  137. */
  138. STATIC consensus_cache_entry_t *
  139. cdm_cache_lookup_consensus(consensus_flavor_t flavor, time_t valid_after)
  140. {
  141. char formatted_time[ISO_TIME_LEN+1];
  142. format_iso_time_nospace(formatted_time, valid_after);
  143. const char *flavname = networkstatus_get_flavor_name(flavor);
  144. /* We'll filter by valid-after time first, since that should
  145. * match the fewest documents. */
  146. // XXXX This is stupid and it should be a hash table.
  147. smartlist_t *matches = smartlist_new();
  148. consensus_cache_find_all(matches, cdm_cache_get(),
  149. LABEL_VALID_AFTER, formatted_time);
  150. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  151. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  152. consensus_cache_entry_t *result = NULL;
  153. if (smartlist_len(matches) > 1) {
  154. log_warn(LD_BUG, "How odd; there appear to be two matching consensuses "
  155. "with flavor %s published at %s.",
  156. flavname, formatted_time);
  157. }
  158. if (smartlist_len(matches)) {
  159. result = smartlist_get(matches, 0);
  160. }
  161. smartlist_free(matches);
  162. return result;
  163. }
  164. /**
  165. * Given a string containing a networkstatus consensus, and the results of
  166. * having parsed that consensus, add that consensus to the cache if it is not
  167. * already present and not too old. Create new consensus diffs from or to
  168. * that consensus as appropriate.
  169. *
  170. * Return 0 on success and -1 on failure.
  171. */
  172. int
  173. consdiffmgr_add_consensus(const char *consensus,
  174. const networkstatus_t *as_parsed)
  175. {
  176. if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
  177. return -1; // LCOV_EXCL_LINE
  178. if (BUG(as_parsed->type != NS_TYPE_CONSENSUS))
  179. return -1; // LCOV_EXCL_LINE
  180. const consensus_flavor_t flavor = as_parsed->flavor;
  181. const time_t valid_after = as_parsed->valid_after;
  182. if (valid_after < approx_time() - 3600 * consdiff_cfg.cache_max_age_hours) {
  183. log_info(LD_DIRSERV, "We don't care about this consensus document; it's "
  184. "too old.");
  185. return -1;
  186. }
  187. /* Do we already have this one? */
  188. consensus_cache_entry_t *entry =
  189. cdm_cache_lookup_consensus(flavor, valid_after);
  190. if (entry) {
  191. log_info(LD_DIRSERV, "We already have a copy of that consensus");
  192. return -1;
  193. }
  194. /* We don't have it. Add it to the cache. */
  195. {
  196. size_t bodylen = strlen(consensus);
  197. config_line_t *labels = NULL;
  198. char formatted_time[ISO_TIME_LEN+1];
  199. format_iso_time_nospace(formatted_time, valid_after);
  200. const char *flavname = networkstatus_get_flavor_name(flavor);
  201. cdm_labels_prepend_sha3(&labels, (const uint8_t *)consensus, bodylen);
  202. config_line_prepend(&labels, LABEL_FLAVOR, flavname);
  203. config_line_prepend(&labels, LABEL_VALID_AFTER, formatted_time);
  204. config_line_prepend(&labels, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  205. entry = consensus_cache_add(cdm_cache_get(),
  206. labels,
  207. (const uint8_t *)consensus,
  208. bodylen);
  209. config_free_lines(labels);
  210. }
  211. if (entry) {
  212. consensus_cache_entry_mark_for_aggressive_release(entry);
  213. consensus_cache_entry_decref(entry);
  214. }
  215. cdm_cache_dirty = 1;
  216. return entry ? 0 : -1;
  217. }
  218. /**
  219. * Helper: used to sort two smartlists of consensus_cache_entry_t by their
  220. * LABEL_VALID_AFTER labels.
  221. */
  222. static int
  223. compare_by_valid_after_(const void **a, const void **b)
  224. {
  225. const consensus_cache_entry_t *e1 = *a;
  226. const consensus_cache_entry_t *e2 = *b;
  227. /* We're in luck here: sorting UTC iso-encoded values lexically will work
  228. * fine (until 9999). */
  229. return strcmp_opt(consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER),
  230. consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER));
  231. }
  232. /**
  233. * Helper: Sort <b>lst</b> by LABEL_VALID_AFTER and return the most recent
  234. * entry.
  235. */
  236. static consensus_cache_entry_t *
  237. sort_and_find_most_recent(smartlist_t *lst)
  238. {
  239. smartlist_sort(lst, compare_by_valid_after_);
  240. if (smartlist_len(lst)) {
  241. return smartlist_get(lst, smartlist_len(lst) - 1);
  242. } else {
  243. return NULL;
  244. }
  245. }
  246. /**
  247. * Look up consensus_cache_entry_t for the consensus of type <b>flavor</b>,
  248. * from the source consensus with the specified digest (which must be SHA3).
  249. *
  250. * If the diff is present, store it into *<b>entry_out</b> and return
  251. * CONSDIFF_AVAILABLE. Otherwise return CONSDIFF_NOT_FOUND or
  252. * CONSDIFF_IN_PROGRESS.
  253. */
  254. consdiff_status_t
  255. consdiffmgr_find_diff_from(consensus_cache_entry_t **entry_out,
  256. consensus_flavor_t flavor,
  257. int digest_type,
  258. const uint8_t *digest,
  259. size_t digestlen)
  260. {
  261. // XXXX actually return IN_PROGRESS some times?
  262. if (BUG(digest_type != DIGEST_SHA3_256) ||
  263. BUG(digestlen != DIGEST256_LEN)) {
  264. return CONSDIFF_NOT_FOUND; // LCOV_EXCL_LINE
  265. }
  266. char hex[HEX_DIGEST256_LEN+1];
  267. base16_encode(hex, sizeof(hex), (const char *)digest, digestlen);
  268. const char *flavname = networkstatus_get_flavor_name(flavor);
  269. smartlist_t *matches = smartlist_new();
  270. consensus_cache_find_all(matches, cdm_cache_get(),
  271. LABEL_FROM_SHA3_DIGEST, hex);
  272. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  273. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  274. *entry_out = sort_and_find_most_recent(matches);
  275. consdiff_status_t result =
  276. (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  277. smartlist_free(matches);
  278. return result;
  279. }
  280. /**
  281. * Perform periodic cleanup tasks on the consensus diff cache. Return
  282. * the number of objects marked for deletion.
  283. */
  284. int
  285. consdiffmgr_cleanup(void)
  286. {
  287. smartlist_t *objects = smartlist_new();
  288. smartlist_t *consensuses = smartlist_new();
  289. smartlist_t *diffs = smartlist_new();
  290. int n_to_delete = 0;
  291. log_debug(LD_DIRSERV, "Looking for consdiffmgr entries to remove");
  292. // 1. Delete any consensus or diff or anything whose valid_after is too old.
  293. const time_t valid_after_cutoff =
  294. approx_time() - 3600 * consdiff_cfg.cache_max_age_hours;
  295. consensus_cache_find_all(objects, cdm_cache_get(),
  296. NULL, NULL);
  297. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  298. const char *lv_valid_after =
  299. consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  300. if (! lv_valid_after) {
  301. log_debug(LD_DIRSERV, "Ignoring entry because it had no %s label",
  302. LABEL_VALID_AFTER);
  303. continue;
  304. }
  305. time_t valid_after = 0;
  306. if (parse_iso_time_nospace(lv_valid_after, &valid_after) < 0) {
  307. log_debug(LD_DIRSERV, "Ignoring entry because its %s value (%s) was "
  308. "unparseable", LABEL_VALID_AFTER, escaped(lv_valid_after));
  309. continue;
  310. }
  311. if (valid_after < valid_after_cutoff) {
  312. log_debug(LD_DIRSERV, "Deleting entry because its %s value (%s) was "
  313. "too old", LABEL_VALID_AFTER, lv_valid_after);
  314. consensus_cache_entry_mark_for_removal(ent);
  315. ++n_to_delete;
  316. }
  317. } SMARTLIST_FOREACH_END(ent);
  318. // 2. Delete all diffs that lead to a consensus whose valid-after is not the
  319. // latest.
  320. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  321. const char *flavname = networkstatus_get_flavor_name(flav);
  322. /* Determine the most recent consensus of this flavor */
  323. consensus_cache_find_all(consensuses, cdm_cache_get(),
  324. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  325. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  326. consensus_cache_entry_t *most_recent =
  327. sort_and_find_most_recent(consensuses);
  328. if (most_recent == NULL)
  329. continue;
  330. const char *most_recent_sha3 =
  331. consensus_cache_entry_get_value(most_recent, LABEL_SHA3_DIGEST);
  332. if (BUG(most_recent_sha3 == NULL))
  333. continue; // LCOV_EXCL_LINE
  334. /* consider all such-flavored diffs, and look to see if they match. */
  335. consensus_cache_find_all(diffs, cdm_cache_get(),
  336. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  337. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  338. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  339. const char *this_diff_target_sha3 =
  340. consensus_cache_entry_get_value(diff, LABEL_TARGET_SHA3_DIGEST);
  341. if (!this_diff_target_sha3)
  342. continue;
  343. if (strcmp(this_diff_target_sha3, most_recent_sha3)) {
  344. consensus_cache_entry_mark_for_removal(diff);
  345. ++n_to_delete;
  346. }
  347. } SMARTLIST_FOREACH_END(diff);
  348. smartlist_clear(consensuses);
  349. smartlist_clear(diffs);
  350. }
  351. smartlist_free(objects);
  352. smartlist_free(consensuses);
  353. smartlist_free(diffs);
  354. // Actually remove files, if they're not used.
  355. consensus_cache_delete_pending(cdm_cache_get());
  356. return n_to_delete;
  357. }
  358. /**
  359. * Initialize the consensus diff manager and its cache, and configure
  360. * its parameters based on the latest torrc and networkstatus parameters.
  361. */
  362. void
  363. consdiffmgr_configure(const consdiff_cfg_t *cfg)
  364. {
  365. memcpy(&consdiff_cfg, cfg, sizeof(consdiff_cfg));
  366. (void) cdm_cache_get();
  367. }
  368. /**
  369. * Scan the consensus diff manager's cache for any grossly malformed entries,
  370. * and mark them as deletable. Return 0 if no problems were found; 1
  371. * if problems were found and fixed.
  372. */
  373. int
  374. consdiffmgr_validate(void)
  375. {
  376. /* Right now, we only check for entries that have bad sha3 values */
  377. int problems = 0;
  378. smartlist_t *objects = smartlist_new();
  379. consensus_cache_find_all(objects, cdm_cache_get(),
  380. NULL, NULL);
  381. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, obj) {
  382. uint8_t sha3_expected[DIGEST256_LEN];
  383. uint8_t sha3_received[DIGEST256_LEN];
  384. int r = cdm_entry_get_sha3_value(sha3_expected, obj, LABEL_SHA3_DIGEST);
  385. if (r == -1) {
  386. /* digest isn't there; that's allowed */
  387. continue;
  388. } else if (r == -2) {
  389. /* digest is malformed; that's not allowed */
  390. problems = 1;
  391. consensus_cache_entry_mark_for_removal(obj);
  392. continue;
  393. }
  394. const uint8_t *body;
  395. size_t bodylen;
  396. consensus_cache_entry_incref(obj);
  397. r = consensus_cache_entry_get_body(obj, &body, &bodylen);
  398. if (r == 0) {
  399. crypto_digest256((char *)sha3_received, (const char *)body, bodylen,
  400. DIGEST_SHA3_256);
  401. }
  402. consensus_cache_entry_decref(obj);
  403. if (r < 0)
  404. continue;
  405. if (fast_memneq(sha3_received, sha3_expected, DIGEST256_LEN)) {
  406. problems = 1;
  407. consensus_cache_entry_mark_for_removal(obj);
  408. continue;
  409. }
  410. } SMARTLIST_FOREACH_END(obj);
  411. smartlist_free(objects);
  412. return problems;
  413. }
  414. /**
  415. * Helper: build new diffs of <b>flavor</b> as needed
  416. */
  417. static void
  418. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  419. {
  420. smartlist_t *matches = NULL;
  421. smartlist_t *diffs = NULL;
  422. smartlist_t *compute_diffs_from = NULL;
  423. strmap_t *have_diff_from = NULL;
  424. // look for the most recent consensus, and for all previous in-range
  425. // consensuses. Do they all have diffs to it?
  426. const char *flavname = networkstatus_get_flavor_name(flavor);
  427. // 1. find the most recent consensus, and the ones that we might want
  428. // to diff to it.
  429. matches = smartlist_new();
  430. consensus_cache_find_all(matches, cdm_cache_get(),
  431. LABEL_FLAVOR, flavname);
  432. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  433. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  434. if (!most_recent) {
  435. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  436. "not making diffs", flavname);
  437. goto done;
  438. }
  439. tor_assert(smartlist_len(matches));
  440. smartlist_del(matches, smartlist_len(matches) - 1);
  441. const char *most_recent_valid_after =
  442. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  443. if (BUG(most_recent_valid_after == NULL))
  444. goto done; //LCOV_EXCL_LINE
  445. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  446. // that we don't need to compute.
  447. diffs = smartlist_new();
  448. consensus_cache_find_all(diffs, cdm_cache_get(),
  449. LABEL_VALID_AFTER, most_recent_valid_after);
  450. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  451. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  452. have_diff_from = strmap_new();
  453. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  454. const char *va = consensus_cache_entry_get_value(diff,
  455. LABEL_FROM_VALID_AFTER);
  456. if (BUG(va == NULL))
  457. continue; // LCOV_EXCL_LINE
  458. strmap_set(have_diff_from, va, diff);
  459. } SMARTLIST_FOREACH_END(diff);
  460. // 3. See which consensuses in 'matches' don't have diffs yet.
  461. smartlist_reverse(matches); // from newest to oldest.
  462. compute_diffs_from = smartlist_new();
  463. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  464. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  465. if (BUG(va == NULL))
  466. continue; // LCOV_EXCL_LINE
  467. if (strmap_get(have_diff_from, va) != NULL)
  468. continue; /* we already have this one. */
  469. smartlist_add(compute_diffs_from, ent);
  470. } SMARTLIST_FOREACH_END(ent);
  471. log_info(LD_DIRSERV,
  472. "The most recent %s consensus is valid-after %s. We have diffs to "
  473. "this consensus for %d/%d older %s consensuses. Generating diffs "
  474. "for the other %d.",
  475. flavname,
  476. most_recent_valid_after,
  477. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  478. smartlist_len(matches),
  479. flavname,
  480. smartlist_len(compute_diffs_from));
  481. // 4. Actually launch the requests.
  482. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  483. if (BUG(c == most_recent))
  484. continue; // LCOV_EXCL_LINE
  485. // XXXX how do we know that we are not already computing this?????
  486. // XXXX DO NOT MERGE UNTIL THAT ISSUE IS SOLVED.
  487. consensus_diff_queue_diff_work(c, most_recent);
  488. } SMARTLIST_FOREACH_END(c);
  489. done:
  490. smartlist_free(matches);
  491. smartlist_free(diffs);
  492. smartlist_free(compute_diffs_from);
  493. strmap_free(have_diff_from, NULL);
  494. }
  495. /**
  496. * Build new diffs as needed.
  497. */
  498. void
  499. consdiffmgr_rescan(void)
  500. {
  501. if (cdm_cache_dirty == 0)
  502. return;
  503. // Clean up here to make room for new diffs, and to ensure that older
  504. // consensuses do not have any entries.
  505. consdiffmgr_cleanup();
  506. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  507. consdiffmgr_rescan_flavor_((consensus_flavor_t) flav);
  508. }
  509. cdm_cache_dirty = 0;
  510. }
  511. /**
  512. * Set consensus cache flags on the objects in this consdiffmgr.
  513. */
  514. static void
  515. consdiffmgr_set_cache_flags(void)
  516. {
  517. /* Right now, we just mark the consensus objects for aggressive release,
  518. * so that they get mmapped for as little time as possible. */
  519. smartlist_t *objects = smartlist_new();
  520. consensus_cache_find_all(objects, cdm_cache_get(), LABEL_DOCTYPE,
  521. DOCTYPE_CONSENSUS);
  522. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  523. consensus_cache_entry_mark_for_aggressive_release(ent);
  524. } SMARTLIST_FOREACH_END(ent);
  525. smartlist_free(objects);
  526. }
  527. /**
  528. * Called before shutdown: drop all storage held by the consdiffmgr.c module.
  529. */
  530. void
  531. consdiffmgr_free_all(void)
  532. {
  533. consensus_cache_free(cons_diff_cache);
  534. cons_diff_cache = NULL;
  535. }
  536. /* =====
  537. Thread workers
  538. =====*/
  539. /**
  540. * An object passed to a worker thread that will try to produce a consensus
  541. * diff.
  542. */
  543. typedef struct consensus_diff_worker_job_t {
  544. /**
  545. * Input: The consensus to compute the diff from. Holds a reference to the
  546. * cache entry, which must not be released until the job is passed back to
  547. * the main thread. The body must be mapped into memory in the main thread.
  548. */
  549. consensus_cache_entry_t *diff_from;
  550. /**
  551. * Input: The consensus to compute the diff to. Holds a reference to the
  552. * cache entry, which must not be released until the job is passed back to
  553. * the main thread. The body must be mapped into memory in the main thread.
  554. */
  555. consensus_cache_entry_t *diff_to;
  556. /**
  557. * Output: Labels to store in the cache associated with this diff.
  558. */
  559. config_line_t *labels_out;
  560. /**
  561. * Output: Body of the diff
  562. */
  563. uint8_t *body_out;
  564. /**
  565. * Output: length of body_out
  566. */
  567. size_t bodylen_out;
  568. } consensus_diff_worker_job_t;
  569. /**
  570. * Worker function. This function runs inside a worker thread and receives
  571. * a consensus_diff_worker_job_t as its input.
  572. */
  573. static workqueue_reply_t
  574. consensus_diff_worker_threadfn(void *state_, void *work_)
  575. {
  576. (void)state_;
  577. consensus_diff_worker_job_t *job = work_;
  578. const uint8_t *diff_from, *diff_to;
  579. size_t len_from, len_to;
  580. int r;
  581. /* We need to have the body already mapped into RAM here.
  582. */
  583. r = consensus_cache_entry_get_body(job->diff_from, &diff_from, &len_from);
  584. if (BUG(r < 0))
  585. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  586. r = consensus_cache_entry_get_body(job->diff_to, &diff_to, &len_to);
  587. if (BUG(r < 0))
  588. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  589. const char *lv_to_valid_after =
  590. consensus_cache_entry_get_value(job->diff_to, LABEL_VALID_AFTER);
  591. const char *lv_from_valid_after =
  592. consensus_cache_entry_get_value(job->diff_from, LABEL_VALID_AFTER);
  593. const char *lv_from_digest =
  594. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  595. const char *lv_from_flavor =
  596. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  597. const char *lv_to_flavor =
  598. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  599. const char *lv_to_digest =
  600. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  601. /* All these values are mandatory on the input */
  602. if (BUG(!lv_to_valid_after) ||
  603. BUG(!lv_from_valid_after) ||
  604. BUG(!lv_from_digest) ||
  605. BUG(!lv_from_flavor) ||
  606. BUG(!lv_to_flavor)) {
  607. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  608. }
  609. /* The flavors need to match */
  610. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  611. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  612. }
  613. char *consensus_diff;
  614. {
  615. // XXXX the input might not be nul-terminated. And also we wanted to
  616. // XXXX support compression later I guess. So, we need to copy here.
  617. char *diff_from_nt, *diff_to_nt;
  618. diff_from_nt = tor_memdup_nulterm(diff_from, len_from);
  619. diff_to_nt = tor_memdup_nulterm(diff_to, len_to);
  620. // XXXX ugh; this is going to calculate the SHA3 of both its
  621. // XXXX inputs again, even though we already have that. Maybe it's time
  622. // XXXX to change the API here?
  623. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  624. tor_free(diff_from_nt);
  625. tor_free(diff_to_nt);
  626. }
  627. if (!consensus_diff) {
  628. /* Couldn't generate consensus; we'll leave the reply blank. */
  629. return WQ_RPL_REPLY;
  630. }
  631. /* Send the reply */
  632. job->body_out = (uint8_t *) consensus_diff;
  633. job->bodylen_out = strlen(consensus_diff);
  634. cdm_labels_prepend_sha3(&job->labels_out, job->body_out, job->bodylen_out);
  635. config_line_prepend(&job->labels_out, LABEL_FROM_VALID_AFTER,
  636. lv_from_valid_after);
  637. config_line_prepend(&job->labels_out, LABEL_VALID_AFTER, lv_to_valid_after);
  638. config_line_prepend(&job->labels_out, LABEL_FLAVOR, lv_from_flavor);
  639. config_line_prepend(&job->labels_out, LABEL_FROM_SHA3_DIGEST,
  640. lv_from_digest);
  641. config_line_prepend(&job->labels_out, LABEL_TARGET_SHA3_DIGEST,
  642. lv_to_digest);
  643. config_line_prepend(&job->labels_out, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  644. return WQ_RPL_REPLY;
  645. }
  646. /**
  647. * Helper: release all storage held in <b>job</b>.
  648. */
  649. static void
  650. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  651. {
  652. if (!job)
  653. return;
  654. tor_free(job->body_out);
  655. config_free_lines(job->labels_out);
  656. consensus_cache_entry_decref(job->diff_from);
  657. consensus_cache_entry_decref(job->diff_to);
  658. tor_free(job);
  659. }
  660. /**
  661. * Worker function: This function runs in the main thread, and receives
  662. * a consensus_diff_worker_job_t that the worker thread has already
  663. * processed.
  664. */
  665. static void
  666. consensus_diff_worker_replyfn(void *work_)
  667. {
  668. tor_assert(in_main_thread());
  669. tor_assert(work_);
  670. consensus_diff_worker_job_t *job = work_;
  671. const char *lv_from_digest =
  672. consensus_cache_entry_get_value(job->diff_from, LABEL_SHA3_DIGEST);
  673. const char *lv_to_digest =
  674. consensus_cache_entry_get_value(job->diff_to, LABEL_SHA3_DIGEST);
  675. if (BUG(lv_from_digest == NULL))
  676. lv_from_digest = "???"; // LCOV_EXCL_LINE
  677. if (BUG(lv_to_digest == NULL))
  678. lv_to_digest = "???"; // LCOV_EXCL_LINE
  679. if (job->body_out && job->bodylen_out && job->labels_out) {
  680. /* Success! Store the results */
  681. log_info(LD_DIRSERV, "Adding consensus diff from %s to %s",
  682. lv_from_digest, lv_to_digest);
  683. consensus_cache_add(cdm_cache_get(), job->labels_out,
  684. job->body_out,
  685. job->bodylen_out);
  686. } else {
  687. /* Failure! Nothing to do but complain */
  688. log_warn(LD_DIRSERV,
  689. "Worker was unable to compute consensus diff "
  690. "from %s to %s", lv_from_digest, lv_to_digest);
  691. /* XXXX Actually, we should cache this failure and not repeat the
  692. * attempt over and over */
  693. }
  694. consensus_diff_worker_job_free(job);
  695. }
  696. /**
  697. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  698. * in a worker thread.
  699. */
  700. static int
  701. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  702. consensus_cache_entry_t *diff_to)
  703. {
  704. tor_assert(in_main_thread());
  705. consensus_cache_entry_incref(diff_from);
  706. consensus_cache_entry_incref(diff_to);
  707. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  708. job->diff_from = diff_from;
  709. job->diff_to = diff_to;
  710. /* Make sure body is mapped. */
  711. const uint8_t *body;
  712. size_t bodylen;
  713. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  714. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  715. if (r1 < 0 || r2 < 0)
  716. goto err;
  717. workqueue_entry_t *work;
  718. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  719. consensus_diff_worker_replyfn,
  720. job);
  721. if (!work)
  722. goto err;
  723. return 0;
  724. err:
  725. consensus_diff_worker_job_free(job); // includes decrefs.
  726. return -1;
  727. }