consdiffmgr.c 26 KB

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