consdiffmgr.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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 "routerparse.h"
  20. #include "workqueue.h"
  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, as compressed (if any) */
  32. #define LABEL_SHA3_DIGEST "sha3-digest"
  33. /* A hex encoded SHA3 digest of the object before compression. */
  34. #define LABEL_SHA3_DIGEST_UNCOMPRESSED "sha3-digest-uncompressed"
  35. /* A hex encoded SHA3 digest-as-signed of a consensus */
  36. #define LABEL_SHA3_DIGEST_AS_SIGNED "sha3-digest-as-signed"
  37. /* The flavor of the consensus or consensuses diff */
  38. #define LABEL_FLAVOR "consensus-flavor"
  39. /* Diff only: the SHA3 digest-as-signed of the source consensus. */
  40. #define LABEL_FROM_SHA3_DIGEST "from-sha3-digest"
  41. /* Diff only: the SHA3 digest-in-full of the target consensus. */
  42. #define LABEL_TARGET_SHA3_DIGEST "target-sha3-digest"
  43. /* Diff only: the valid-after date of the source consensus. */
  44. #define LABEL_FROM_VALID_AFTER "from-valid-after"
  45. /* What kind of compression was used? */
  46. #define LABEL_COMPRESSION_TYPE "compression"
  47. /** @} */
  48. #define DOCTYPE_CONSENSUS "consensus"
  49. #define DOCTYPE_CONSENSUS_DIFF "consensus-diff"
  50. /**
  51. * Underlying directory that stores consensuses and consensus diffs. Don't
  52. * use this directly: use cdm_cache_get() instead.
  53. */
  54. static consensus_cache_t *cons_diff_cache = NULL;
  55. /**
  56. * If true, we have learned at least one new consensus since the
  57. * consensus cache was last up-to-date.
  58. */
  59. static int cdm_cache_dirty = 0;
  60. /**
  61. * If true, we have scanned the cache to update our hashtable of diffs.
  62. */
  63. static int cdm_cache_loaded = 0;
  64. /**
  65. * Possible status values for cdm_diff_t.cdm_diff_status
  66. **/
  67. typedef enum cdm_diff_status_t {
  68. CDM_DIFF_PRESENT=1,
  69. CDM_DIFF_IN_PROGRESS=2,
  70. CDM_DIFF_ERROR=3,
  71. } cdm_diff_status_t;
  72. /** Which methods do we use for precompressing diffs? */
  73. static const compress_method_t compress_diffs_with[] = {
  74. NO_METHOD,
  75. GZIP_METHOD,
  76. #ifdef HAVE_LZMA
  77. LZMA_METHOD,
  78. #endif
  79. #ifdef HAVE_ZSTD
  80. ZSTD_METHOD,
  81. #endif
  82. };
  83. /** How many different methods will we try to use for diff compression? */
  84. STATIC unsigned
  85. n_diff_compression_methods(void)
  86. {
  87. return ARRAY_LENGTH(compress_diffs_with);
  88. }
  89. /** Which methods do we use for precompressing consensuses? */
  90. static const compress_method_t compress_consensus_with[] = {
  91. ZLIB_METHOD,
  92. #ifdef HAVE_LZMA
  93. LZMA_METHOD,
  94. #endif
  95. #ifdef HAVE_ZSTD
  96. ZSTD_METHOD,
  97. #endif
  98. };
  99. /** How many different methods will we try to use for diff compression? */
  100. static unsigned
  101. n_consensus_compression_methods(void)
  102. {
  103. return ARRAY_LENGTH(compress_consensus_with);
  104. }
  105. /** For which compression method do we retain old consensuses? There's no
  106. * need to keep all of them, since we won't be serving them. We'll
  107. * go with ZLIB_METHOD because it's pretty fast and everyone has it.
  108. */
  109. #define RETAIN_CONSENSUS_COMPRESSED_WITH_METHOD ZLIB_METHOD
  110. /** Handles pointing to the latest consensus entries as compressed and
  111. * stored. */
  112. static consensus_cache_entry_handle_t *
  113. latest_consensus[N_CONSENSUS_FLAVORS]
  114. [ARRAY_LENGTH(compress_consensus_with)];
  115. /** Hashtable node used to remember the current status of the diff
  116. * from a given sha3 digest to the current consensus. */
  117. typedef struct cdm_diff_t {
  118. HT_ENTRY(cdm_diff_t) node;
  119. /** Consensus flavor for this diff (part of ht key) */
  120. consensus_flavor_t flavor;
  121. /** SHA3-256 digest of the consensus that this diff is _from_. (part of the
  122. * ht key) */
  123. uint8_t from_sha3[DIGEST256_LEN];
  124. /** Method by which the diff is compressed. (part of the ht key */
  125. compress_method_t compress_method;
  126. /** One of the CDM_DIFF_* values, depending on whether this diff
  127. * is available, in progress, or impossible to compute. */
  128. cdm_diff_status_t cdm_diff_status;
  129. /** SHA3-256 digest of the consensus that this diff is _to. */
  130. uint8_t target_sha3[DIGEST256_LEN];
  131. /** Handle to the cache entry for this diff, if any. We use a handle here
  132. * to avoid thinking too hard about cache entry lifetime issues. */
  133. consensus_cache_entry_handle_t *entry;
  134. } cdm_diff_t;
  135. /** Hashtable mapping flavor and source consensus digest to status. */
  136. static HT_HEAD(cdm_diff_ht, cdm_diff_t) cdm_diff_ht = HT_INITIALIZER();
  137. /**
  138. * Configuration for this module
  139. */
  140. static consdiff_cfg_t consdiff_cfg = {
  141. /* .cache_max_age_hours = */ 24 * 90,
  142. // XXXX I'd like to make this number bigger, but it interferes with the
  143. // XXXX seccomp2 syscall filter, which tops out at BPF_MAXINS (4096)
  144. // XXXX rules.
  145. /* .cache_max_num = */ 128
  146. };
  147. static int consdiffmgr_ensure_space_for_files(int n);
  148. static int consensus_queue_compression_work(const char *consensus,
  149. consensus_flavor_t flavor,
  150. time_t valid_after);
  151. static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  152. consensus_cache_entry_t *diff_to);
  153. static void consdiffmgr_set_cache_flags(void);
  154. /* =====
  155. * Hashtable setup
  156. * ===== */
  157. /** Helper: hash the key of a cdm_diff_t. */
  158. static unsigned
  159. cdm_diff_hash(const cdm_diff_t *diff)
  160. {
  161. uint8_t tmp[DIGEST256_LEN + 2];
  162. memcpy(tmp, diff->from_sha3, DIGEST256_LEN);
  163. tmp[DIGEST256_LEN] = (uint8_t) diff->flavor;
  164. tmp[DIGEST256_LEN+1] = (uint8_t) diff->compress_method;
  165. return (unsigned) siphash24g(tmp, sizeof(tmp));
  166. }
  167. /** Helper: compare two cdm_diff_t objects for key equality */
  168. static int
  169. cdm_diff_eq(const cdm_diff_t *diff1, const cdm_diff_t *diff2)
  170. {
  171. return fast_memeq(diff1->from_sha3, diff2->from_sha3, DIGEST256_LEN) &&
  172. diff1->flavor == diff2->flavor &&
  173. diff1->compress_method == diff2->compress_method;
  174. }
  175. HT_PROTOTYPE(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq)
  176. HT_GENERATE2(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq,
  177. 0.6, tor_reallocarray, tor_free_)
  178. /** Release all storage held in <b>diff</b>. */
  179. static void
  180. cdm_diff_free(cdm_diff_t *diff)
  181. {
  182. if (!diff)
  183. return;
  184. consensus_cache_entry_handle_free(diff->entry);
  185. tor_free(diff);
  186. }
  187. /** Create and return a new cdm_diff_t with the given values. Does not
  188. * add it to the hashtable. */
  189. static cdm_diff_t *
  190. cdm_diff_new(consensus_flavor_t flav,
  191. const uint8_t *from_sha3,
  192. const uint8_t *target_sha3,
  193. compress_method_t method)
  194. {
  195. cdm_diff_t *ent;
  196. ent = tor_malloc_zero(sizeof(cdm_diff_t));
  197. ent->flavor = flav;
  198. memcpy(ent->from_sha3, from_sha3, DIGEST256_LEN);
  199. memcpy(ent->target_sha3, target_sha3, DIGEST256_LEN);
  200. ent->compress_method = method;
  201. return ent;
  202. }
  203. /**
  204. * Examine the diff hashtable to see whether we know anything about computing
  205. * a diff of type <b>flav</b> between consensuses with the two provided
  206. * SHA3-256 digests. If a computation is in progress, or if the computation
  207. * has already been tried and failed, return 1. Otherwise, note the
  208. * computation as "in progress" so that we don't reattempt it later, and
  209. * return 0.
  210. */
  211. static int
  212. cdm_diff_ht_check_and_note_pending(consensus_flavor_t flav,
  213. const uint8_t *from_sha3,
  214. const uint8_t *target_sha3)
  215. {
  216. struct cdm_diff_t search, *ent;
  217. unsigned u;
  218. int result = 0;
  219. for (u = 0; u < n_diff_compression_methods(); ++u) {
  220. compress_method_t method = compress_diffs_with[u];
  221. memset(&search, 0, sizeof(cdm_diff_t));
  222. search.flavor = flav;
  223. search.compress_method = method;
  224. memcpy(search.from_sha3, from_sha3, DIGEST256_LEN);
  225. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  226. if (ent) {
  227. tor_assert_nonfatal(ent->cdm_diff_status != CDM_DIFF_PRESENT);
  228. result = 1;
  229. continue;
  230. }
  231. ent = cdm_diff_new(flav, from_sha3, target_sha3, method);
  232. ent->cdm_diff_status = CDM_DIFF_IN_PROGRESS;
  233. HT_INSERT(cdm_diff_ht, &cdm_diff_ht, ent);
  234. }
  235. return result;
  236. }
  237. /**
  238. * Update the status of the diff of type <b>flav</b> between consensuses with
  239. * the two provided SHA3-256 digests, so that its status becomes
  240. * <b>status</b>, and its value becomes the <b>handle</b>. If <b>handle</b>
  241. * is NULL, then the old handle (if any) is freed, and replaced with NULL.
  242. */
  243. static void
  244. cdm_diff_ht_set_status(consensus_flavor_t flav,
  245. const uint8_t *from_sha3,
  246. const uint8_t *to_sha3,
  247. compress_method_t method,
  248. int status,
  249. consensus_cache_entry_handle_t *handle)
  250. {
  251. struct cdm_diff_t search, *ent;
  252. memset(&search, 0, sizeof(cdm_diff_t));
  253. search.flavor = flav;
  254. search.compress_method = method,
  255. memcpy(search.from_sha3, from_sha3, DIGEST256_LEN);
  256. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  257. if (!ent) {
  258. ent = cdm_diff_new(flav, from_sha3, to_sha3, method);
  259. ent->cdm_diff_status = CDM_DIFF_IN_PROGRESS;
  260. HT_INSERT(cdm_diff_ht, &cdm_diff_ht, ent);
  261. } else if (fast_memneq(ent->target_sha3, to_sha3, DIGEST256_LEN)) {
  262. // This can happen under certain really pathological conditions
  263. // if we decide we don't care about a diff before it is actually
  264. // done computing.
  265. return;
  266. }
  267. tor_assert_nonfatal(ent->cdm_diff_status == CDM_DIFF_IN_PROGRESS);
  268. ent->cdm_diff_status = status;
  269. consensus_cache_entry_handle_free(ent->entry);
  270. ent->entry = handle;
  271. }
  272. /**
  273. * Helper: Remove from the hash table every present (actually computed) diff
  274. * of type <b>flav</b> whose target digest does not match
  275. * <b>unless_target_sha3_matches</b>.
  276. *
  277. * This function is used for the hash table to throw away references to diffs
  278. * that do not lead to the most given consensus of a given flavor.
  279. */
  280. static void
  281. cdm_diff_ht_purge(consensus_flavor_t flav,
  282. const uint8_t *unless_target_sha3_matches)
  283. {
  284. cdm_diff_t **diff, **next;
  285. for (diff = HT_START(cdm_diff_ht, &cdm_diff_ht); diff; diff = next) {
  286. cdm_diff_t *this = *diff;
  287. if ((*diff)->cdm_diff_status == CDM_DIFF_PRESENT &&
  288. flav == (*diff)->flavor) {
  289. if (consensus_cache_entry_handle_get((*diff)->entry) == NULL) {
  290. /* the underlying entry has gone away; drop this. */
  291. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  292. cdm_diff_free(this);
  293. continue;
  294. }
  295. if (unless_target_sha3_matches &&
  296. fast_memneq(unless_target_sha3_matches, (*diff)->target_sha3,
  297. DIGEST256_LEN)) {
  298. /* target hash doesn't match; drop this. */
  299. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  300. cdm_diff_free(this);
  301. continue;
  302. }
  303. }
  304. next = HT_NEXT(cdm_diff_ht, &cdm_diff_ht, diff);
  305. }
  306. }
  307. /**
  308. * Helper: initialize <b>cons_diff_cache</b>.
  309. */
  310. static void
  311. cdm_cache_init(void)
  312. {
  313. unsigned n_entries = consdiff_cfg.cache_max_num * 2;
  314. tor_assert(cons_diff_cache == NULL);
  315. cons_diff_cache = consensus_cache_open("diff-cache", n_entries);
  316. if (cons_diff_cache == NULL) {
  317. // LCOV_EXCL_START
  318. log_err(LD_FS, "Error: Couldn't open storage for consensus diffs.");
  319. tor_assert_unreached();
  320. // LCOV_EXCL_STOP
  321. } else {
  322. consdiffmgr_set_cache_flags();
  323. }
  324. cdm_cache_dirty = 1;
  325. cdm_cache_loaded = 0;
  326. }
  327. /**
  328. * Helper: return the consensus_cache_t * that backs this manager,
  329. * initializing it if needed.
  330. */
  331. STATIC consensus_cache_t *
  332. cdm_cache_get(void)
  333. {
  334. if (PREDICT_UNLIKELY(cons_diff_cache == NULL)) {
  335. cdm_cache_init();
  336. }
  337. return cons_diff_cache;
  338. }
  339. /**
  340. * Helper: given a list of labels, prepend the hex-encoded SHA3 digest
  341. * of the <b>bodylen</b>-byte object at <b>body</b> to those labels,
  342. * with <b>label</b> as its label.
  343. */
  344. static void
  345. cdm_labels_prepend_sha3(config_line_t **labels,
  346. const char *label,
  347. const uint8_t *body,
  348. size_t bodylen)
  349. {
  350. uint8_t sha3_digest[DIGEST256_LEN];
  351. char hexdigest[HEX_DIGEST256_LEN+1];
  352. crypto_digest256((char *)sha3_digest,
  353. (const char *)body, bodylen, DIGEST_SHA3_256);
  354. base16_encode(hexdigest, sizeof(hexdigest),
  355. (const char *)sha3_digest, sizeof(sha3_digest));
  356. config_line_prepend(labels, label, hexdigest);
  357. }
  358. /** Helper: if there is a sha3-256 hex-encoded digest in <b>ent</b> with the
  359. * given label, set <b>digest_out</b> to that value (decoded), and return 0.
  360. *
  361. * Return -1 if there is no such label, and -2 if it is badly formatted. */
  362. STATIC int
  363. cdm_entry_get_sha3_value(uint8_t *digest_out,
  364. consensus_cache_entry_t *ent,
  365. const char *label)
  366. {
  367. if (ent == NULL)
  368. return -1;
  369. const char *hex = consensus_cache_entry_get_value(ent, label);
  370. if (hex == NULL)
  371. return -1;
  372. int n = base16_decode((char*)digest_out, DIGEST256_LEN, hex, strlen(hex));
  373. if (n != DIGEST256_LEN)
  374. return -2;
  375. else
  376. return 0;
  377. }
  378. /**
  379. * Helper: look for a consensus with the given <b>flavor</b> and
  380. * <b>valid_after</b> time in the cache. Return that consensus if it's
  381. * present, or NULL if it's missing.
  382. */
  383. STATIC consensus_cache_entry_t *
  384. cdm_cache_lookup_consensus(consensus_flavor_t flavor, time_t valid_after)
  385. {
  386. char formatted_time[ISO_TIME_LEN+1];
  387. format_iso_time_nospace(formatted_time, valid_after);
  388. const char *flavname = networkstatus_get_flavor_name(flavor);
  389. /* We'll filter by valid-after time first, since that should
  390. * match the fewest documents. */
  391. /* We could add an extra hashtable here, but since we only do this scan
  392. * when adding a new consensus, it probably doesn't matter much. */
  393. smartlist_t *matches = smartlist_new();
  394. consensus_cache_find_all(matches, cdm_cache_get(),
  395. LABEL_VALID_AFTER, formatted_time);
  396. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  397. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  398. consensus_cache_entry_t *result = NULL;
  399. if (smartlist_len(matches)) {
  400. result = smartlist_get(matches, 0);
  401. }
  402. smartlist_free(matches);
  403. return result;
  404. }
  405. /**
  406. * Given a string containing a networkstatus consensus, and the results of
  407. * having parsed that consensus, add that consensus to the cache if it is not
  408. * already present and not too old. Create new consensus diffs from or to
  409. * that consensus as appropriate.
  410. *
  411. * Return 0 on success and -1 on failure.
  412. */
  413. int
  414. consdiffmgr_add_consensus(const char *consensus,
  415. const networkstatus_t *as_parsed)
  416. {
  417. if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
  418. return -1; // LCOV_EXCL_LINE
  419. if (BUG(as_parsed->type != NS_TYPE_CONSENSUS))
  420. return -1; // LCOV_EXCL_LINE
  421. const consensus_flavor_t flavor = as_parsed->flavor;
  422. const time_t valid_after = as_parsed->valid_after;
  423. if (valid_after < approx_time() - 3600 * consdiff_cfg.cache_max_age_hours) {
  424. log_info(LD_DIRSERV, "We don't care about this consensus document; it's "
  425. "too old.");
  426. return -1;
  427. }
  428. /* Do we already have this one? */
  429. consensus_cache_entry_t *entry =
  430. cdm_cache_lookup_consensus(flavor, valid_after);
  431. if (entry) {
  432. log_info(LD_DIRSERV, "We already have a copy of that consensus");
  433. return -1;
  434. }
  435. /* We don't have it. Add it to the cache. */
  436. return consensus_queue_compression_work(consensus, flavor, valid_after);
  437. }
  438. /**
  439. * Helper: used to sort two smartlists of consensus_cache_entry_t by their
  440. * LABEL_VALID_AFTER labels.
  441. */
  442. static int
  443. compare_by_valid_after_(const void **a, const void **b)
  444. {
  445. const consensus_cache_entry_t *e1 = *a;
  446. const consensus_cache_entry_t *e2 = *b;
  447. /* We're in luck here: sorting UTC iso-encoded values lexically will work
  448. * fine (until 9999). */
  449. return strcmp_opt(consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER),
  450. consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER));
  451. }
  452. /**
  453. * Helper: Sort <b>lst</b> by LABEL_VALID_AFTER and return the most recent
  454. * entry.
  455. */
  456. static consensus_cache_entry_t *
  457. sort_and_find_most_recent(smartlist_t *lst)
  458. {
  459. smartlist_sort(lst, compare_by_valid_after_);
  460. if (smartlist_len(lst)) {
  461. return smartlist_get(lst, smartlist_len(lst) - 1);
  462. } else {
  463. return NULL;
  464. }
  465. }
  466. /**
  467. * If we know a consensus with the flavor <b>flavor</b> compressed with
  468. * <b>method</b>, set *<b>entry_out</b> to that value. Return values are as
  469. * for consdiffmgr_find_diff_from().
  470. */
  471. consdiff_status_t
  472. consdiffmgr_find_consensus(struct consensus_cache_entry_t **entry_out,
  473. consensus_flavor_t flavor,
  474. compress_method_t method)
  475. {
  476. int pos=-1;
  477. unsigned i;
  478. tor_assert(flavor < N_CONSENSUS_FLAVORS);
  479. // Find the index of method withing compress_consensus_with
  480. for (i = 0; i < n_consensus_compression_methods(); ++i) {
  481. if (compress_consensus_with[i] == method) {
  482. pos = i;
  483. break;
  484. }
  485. }
  486. if (pos < 0) {
  487. // We don't compress consensuses with this method.
  488. return CONSDIFF_NOT_FOUND;
  489. }
  490. consensus_cache_entry_handle_t *handle = latest_consensus[flavor][pos];
  491. if (!handle)
  492. return CONSDIFF_NOT_FOUND;
  493. *entry_out = consensus_cache_entry_handle_get(handle);
  494. if (entry_out)
  495. return CONSDIFF_AVAILABLE;
  496. else
  497. return CONSDIFF_NOT_FOUND;
  498. }
  499. /**
  500. * Look up consensus_cache_entry_t for the consensus of type <b>flavor</b>,
  501. * from the source consensus with the specified digest (which must be SHA3).
  502. *
  503. * If the diff is present, store it into *<b>entry_out</b> and return
  504. * CONSDIFF_AVAILABLE. Otherwise return CONSDIFF_NOT_FOUND or
  505. * CONSDIFF_IN_PROGRESS.
  506. */
  507. consdiff_status_t
  508. consdiffmgr_find_diff_from(consensus_cache_entry_t **entry_out,
  509. consensus_flavor_t flavor,
  510. int digest_type,
  511. const uint8_t *digest,
  512. size_t digestlen,
  513. compress_method_t method)
  514. {
  515. if (BUG(digest_type != DIGEST_SHA3_256) ||
  516. BUG(digestlen != DIGEST256_LEN)) {
  517. return CONSDIFF_NOT_FOUND; // LCOV_EXCL_LINE
  518. }
  519. // Try to look up the entry in the hashtable.
  520. cdm_diff_t search, *ent;
  521. memset(&search, 0, sizeof(search));
  522. search.flavor = flavor;
  523. search.compress_method = method;
  524. memcpy(search.from_sha3, digest, DIGEST256_LEN);
  525. ent = HT_FIND(cdm_diff_ht, &cdm_diff_ht, &search);
  526. if (ent == NULL ||
  527. ent->cdm_diff_status == CDM_DIFF_ERROR) {
  528. return CONSDIFF_NOT_FOUND;
  529. } else if (ent->cdm_diff_status == CDM_DIFF_IN_PROGRESS) {
  530. return CONSDIFF_IN_PROGRESS;
  531. } else if (BUG(ent->cdm_diff_status != CDM_DIFF_PRESENT)) {
  532. return CONSDIFF_IN_PROGRESS;
  533. }
  534. *entry_out = consensus_cache_entry_handle_get(ent->entry);
  535. return (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  536. #if 0
  537. // XXXX Remove this. I'm keeping it around for now in case we need to
  538. // XXXX debug issues in the hashtable.
  539. char hex[HEX_DIGEST256_LEN+1];
  540. base16_encode(hex, sizeof(hex), (const char *)digest, digestlen);
  541. const char *flavname = networkstatus_get_flavor_name(flavor);
  542. smartlist_t *matches = smartlist_new();
  543. consensus_cache_find_all(matches, cdm_cache_get(),
  544. LABEL_FROM_SHA3_DIGEST, hex);
  545. consensus_cache_filter_list(matches, LABEL_FLAVOR, flavname);
  546. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  547. *entry_out = sort_and_find_most_recent(matches);
  548. consdiff_status_t result =
  549. (*entry_out) ? CONSDIFF_AVAILABLE : CONSDIFF_NOT_FOUND;
  550. smartlist_free(matches);
  551. return result;
  552. #endif
  553. }
  554. /**
  555. * Perform periodic cleanup tasks on the consensus diff cache. Return
  556. * the number of objects marked for deletion.
  557. */
  558. int
  559. consdiffmgr_cleanup(void)
  560. {
  561. smartlist_t *objects = smartlist_new();
  562. smartlist_t *consensuses = smartlist_new();
  563. smartlist_t *diffs = smartlist_new();
  564. int n_to_delete = 0;
  565. log_debug(LD_DIRSERV, "Looking for consdiffmgr entries to remove");
  566. // 1. Delete any consensus or diff or anything whose valid_after is too old.
  567. const time_t valid_after_cutoff =
  568. approx_time() - 3600 * consdiff_cfg.cache_max_age_hours;
  569. consensus_cache_find_all(objects, cdm_cache_get(),
  570. NULL, NULL);
  571. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  572. const char *lv_valid_after =
  573. consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  574. if (! lv_valid_after) {
  575. log_debug(LD_DIRSERV, "Ignoring entry because it had no %s label",
  576. LABEL_VALID_AFTER);
  577. continue;
  578. }
  579. time_t valid_after = 0;
  580. if (parse_iso_time_nospace(lv_valid_after, &valid_after) < 0) {
  581. log_debug(LD_DIRSERV, "Ignoring entry because its %s value (%s) was "
  582. "unparseable", LABEL_VALID_AFTER, escaped(lv_valid_after));
  583. continue;
  584. }
  585. if (valid_after < valid_after_cutoff) {
  586. log_debug(LD_DIRSERV, "Deleting entry because its %s value (%s) was "
  587. "too old", LABEL_VALID_AFTER, lv_valid_after);
  588. consensus_cache_entry_mark_for_removal(ent);
  589. ++n_to_delete;
  590. }
  591. } SMARTLIST_FOREACH_END(ent);
  592. // 2. Delete all diffs that lead to a consensus whose valid-after is not the
  593. // latest.
  594. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  595. const char *flavname = networkstatus_get_flavor_name(flav);
  596. /* Determine the most recent consensus of this flavor */
  597. consensus_cache_find_all(consensuses, cdm_cache_get(),
  598. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  599. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  600. consensus_cache_entry_t *most_recent =
  601. sort_and_find_most_recent(consensuses);
  602. if (most_recent == NULL)
  603. continue;
  604. const char *most_recent_sha3 =
  605. consensus_cache_entry_get_value(most_recent,
  606. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  607. if (BUG(most_recent_sha3 == NULL))
  608. continue; // LCOV_EXCL_LINE
  609. /* consider all such-flavored diffs, and look to see if they match. */
  610. consensus_cache_find_all(diffs, cdm_cache_get(),
  611. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  612. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  613. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  614. const char *this_diff_target_sha3 =
  615. consensus_cache_entry_get_value(diff, LABEL_TARGET_SHA3_DIGEST);
  616. if (!this_diff_target_sha3)
  617. continue;
  618. if (strcmp(this_diff_target_sha3, most_recent_sha3)) {
  619. consensus_cache_entry_mark_for_removal(diff);
  620. ++n_to_delete;
  621. }
  622. } SMARTLIST_FOREACH_END(diff);
  623. smartlist_clear(consensuses);
  624. smartlist_clear(diffs);
  625. }
  626. // 3. Delete all consensuses except the most recent that are compressed with
  627. // an un-preferred method.
  628. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  629. const char *flavname = networkstatus_get_flavor_name(flav);
  630. /* Determine the most recent consensus of this flavor */
  631. consensus_cache_find_all(consensuses, cdm_cache_get(),
  632. LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  633. consensus_cache_filter_list(consensuses, LABEL_FLAVOR, flavname);
  634. consensus_cache_entry_t *most_recent =
  635. sort_and_find_most_recent(consensuses);
  636. if (most_recent == NULL)
  637. continue;
  638. const char *most_recent_sha3_uncompressed =
  639. consensus_cache_entry_get_value(most_recent,
  640. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  641. const char *retain_methodname = compression_method_get_name(
  642. RETAIN_CONSENSUS_COMPRESSED_WITH_METHOD);
  643. if (BUG(most_recent_sha3_uncompressed == NULL))
  644. continue;
  645. SMARTLIST_FOREACH_BEGIN(consensuses, consensus_cache_entry_t *, ent) {
  646. const char *lv_sha3_uncompressed =
  647. consensus_cache_entry_get_value(ent, LABEL_SHA3_DIGEST_UNCOMPRESSED);
  648. if (BUG(! lv_sha3_uncompressed))
  649. continue;
  650. if (!strcmp(lv_sha3_uncompressed, most_recent_sha3_uncompressed))
  651. continue; // This _is_ the most recent.
  652. const char *lv_methodname =
  653. consensus_cache_entry_get_value(ent, LABEL_COMPRESSION_TYPE);
  654. if (! lv_methodname || strcmp(lv_methodname, retain_methodname)) {
  655. consensus_cache_entry_mark_for_removal(ent);
  656. ++n_to_delete;
  657. }
  658. } SMARTLIST_FOREACH_END(ent);
  659. }
  660. smartlist_free(objects);
  661. smartlist_free(consensuses);
  662. smartlist_free(diffs);
  663. // Actually remove files, if they're not used.
  664. consensus_cache_delete_pending(cdm_cache_get(), 0);
  665. return n_to_delete;
  666. }
  667. /**
  668. * Initialize the consensus diff manager and its cache, and configure
  669. * its parameters based on the latest torrc and networkstatus parameters.
  670. */
  671. void
  672. consdiffmgr_configure(const consdiff_cfg_t *cfg)
  673. {
  674. if (cfg)
  675. memcpy(&consdiff_cfg, cfg, sizeof(consdiff_cfg));
  676. (void) cdm_cache_get();
  677. }
  678. /**
  679. * Tell the sandbox (if any) configured by <b>cfg</b> to allow the
  680. * operations that the consensus diff manager will need.
  681. */
  682. int
  683. consdiffmgr_register_with_sandbox(struct sandbox_cfg_elem **cfg)
  684. {
  685. return consensus_cache_register_with_sandbox(cdm_cache_get(), cfg);
  686. }
  687. /**
  688. * Scan the consensus diff manager's cache for any grossly malformed entries,
  689. * and mark them as deletable. Return 0 if no problems were found; 1
  690. * if problems were found and fixed.
  691. */
  692. int
  693. consdiffmgr_validate(void)
  694. {
  695. /* Right now, we only check for entries that have bad sha3 values */
  696. int problems = 0;
  697. smartlist_t *objects = smartlist_new();
  698. consensus_cache_find_all(objects, cdm_cache_get(),
  699. NULL, NULL);
  700. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, obj) {
  701. uint8_t sha3_expected[DIGEST256_LEN];
  702. uint8_t sha3_received[DIGEST256_LEN];
  703. int r = cdm_entry_get_sha3_value(sha3_expected, obj, LABEL_SHA3_DIGEST);
  704. if (r == -1) {
  705. /* digest isn't there; that's allowed */
  706. continue;
  707. } else if (r == -2) {
  708. /* digest is malformed; that's not allowed */
  709. problems = 1;
  710. consensus_cache_entry_mark_for_removal(obj);
  711. continue;
  712. }
  713. const uint8_t *body;
  714. size_t bodylen;
  715. consensus_cache_entry_incref(obj);
  716. r = consensus_cache_entry_get_body(obj, &body, &bodylen);
  717. if (r == 0) {
  718. crypto_digest256((char *)sha3_received, (const char *)body, bodylen,
  719. DIGEST_SHA3_256);
  720. }
  721. consensus_cache_entry_decref(obj);
  722. if (r < 0)
  723. continue;
  724. // Deconfuse coverity about the possibility of sha3_received being
  725. // uninitialized
  726. tor_assert(r <= 0);
  727. if (fast_memneq(sha3_received, sha3_expected, DIGEST256_LEN)) {
  728. problems = 1;
  729. consensus_cache_entry_mark_for_removal(obj);
  730. continue;
  731. }
  732. } SMARTLIST_FOREACH_END(obj);
  733. smartlist_free(objects);
  734. return problems;
  735. }
  736. /**
  737. * Helper: build new diffs of <b>flavor</b> as needed
  738. */
  739. static void
  740. consdiffmgr_rescan_flavor_(consensus_flavor_t flavor)
  741. {
  742. smartlist_t *matches = NULL;
  743. smartlist_t *diffs = NULL;
  744. smartlist_t *compute_diffs_from = NULL;
  745. strmap_t *have_diff_from = NULL;
  746. // look for the most recent consensus, and for all previous in-range
  747. // consensuses. Do they all have diffs to it?
  748. const char *flavname = networkstatus_get_flavor_name(flavor);
  749. // 1. find the most recent consensus, and the ones that we might want
  750. // to diff to it.
  751. const char *methodname = compression_method_get_name(
  752. RETAIN_CONSENSUS_COMPRESSED_WITH_METHOD);
  753. matches = smartlist_new();
  754. consensus_cache_find_all(matches, cdm_cache_get(),
  755. LABEL_FLAVOR, flavname);
  756. consensus_cache_filter_list(matches, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  757. consensus_cache_filter_list(matches, LABEL_COMPRESSION_TYPE, methodname);
  758. consensus_cache_entry_t *most_recent = sort_and_find_most_recent(matches);
  759. if (!most_recent) {
  760. log_info(LD_DIRSERV, "No 'most recent' %s consensus found; "
  761. "not making diffs", flavname);
  762. goto done;
  763. }
  764. tor_assert(smartlist_len(matches));
  765. smartlist_del(matches, smartlist_len(matches) - 1);
  766. const char *most_recent_valid_after =
  767. consensus_cache_entry_get_value(most_recent, LABEL_VALID_AFTER);
  768. if (BUG(most_recent_valid_after == NULL))
  769. goto done; //LCOV_EXCL_LINE
  770. uint8_t most_recent_sha3[DIGEST256_LEN];
  771. if (BUG(cdm_entry_get_sha3_value(most_recent_sha3, most_recent,
  772. LABEL_SHA3_DIGEST_UNCOMPRESSED) < 0))
  773. goto done; //LCOV_EXCL_LINE
  774. // 2. Find all the relevant diffs _to_ this consensus. These are ones
  775. // that we don't need to compute.
  776. diffs = smartlist_new();
  777. consensus_cache_find_all(diffs, cdm_cache_get(),
  778. LABEL_VALID_AFTER, most_recent_valid_after);
  779. consensus_cache_filter_list(diffs, LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  780. consensus_cache_filter_list(diffs, LABEL_FLAVOR, flavname);
  781. have_diff_from = strmap_new();
  782. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  783. const char *va = consensus_cache_entry_get_value(diff,
  784. LABEL_FROM_VALID_AFTER);
  785. if (BUG(va == NULL))
  786. continue; // LCOV_EXCL_LINE
  787. strmap_set(have_diff_from, va, diff);
  788. } SMARTLIST_FOREACH_END(diff);
  789. // 3. See which consensuses in 'matches' don't have diffs yet.
  790. smartlist_reverse(matches); // from newest to oldest.
  791. compute_diffs_from = smartlist_new();
  792. SMARTLIST_FOREACH_BEGIN(matches, consensus_cache_entry_t *, ent) {
  793. const char *va = consensus_cache_entry_get_value(ent, LABEL_VALID_AFTER);
  794. if (BUG(va == NULL))
  795. continue; // LCOV_EXCL_LINE
  796. if (strmap_get(have_diff_from, va) != NULL)
  797. continue; /* we already have this one. */
  798. smartlist_add(compute_diffs_from, ent);
  799. /* Since we are not going to serve this as the most recent consensus
  800. * any more, we should stop keeping it mmap'd when it's not in use.
  801. */
  802. consensus_cache_entry_mark_for_aggressive_release(ent);
  803. } SMARTLIST_FOREACH_END(ent);
  804. log_info(LD_DIRSERV,
  805. "The most recent %s consensus is valid-after %s. We have diffs to "
  806. "this consensus for %d/%d older %s consensuses. Generating diffs "
  807. "for the other %d.",
  808. flavname,
  809. most_recent_valid_after,
  810. smartlist_len(matches) - smartlist_len(compute_diffs_from),
  811. smartlist_len(matches),
  812. flavname,
  813. smartlist_len(compute_diffs_from));
  814. // 4. Update the hashtable; remove entries in this flavor to other
  815. // target consensuses.
  816. cdm_diff_ht_purge(flavor, most_recent_sha3);
  817. // 5. Actually launch the requests.
  818. SMARTLIST_FOREACH_BEGIN(compute_diffs_from, consensus_cache_entry_t *, c) {
  819. if (BUG(c == most_recent))
  820. continue; // LCOV_EXCL_LINE
  821. uint8_t this_sha3[DIGEST256_LEN];
  822. if (cdm_entry_get_sha3_value(this_sha3, c,
  823. LABEL_SHA3_DIGEST_AS_SIGNED)<0) {
  824. // Not actually a bug, since we might be running with a directory
  825. // with stale files from before the #22143 fixes.
  826. continue;
  827. }
  828. if (cdm_diff_ht_check_and_note_pending(flavor,
  829. this_sha3, most_recent_sha3)) {
  830. // This is already pending, or we encountered an error.
  831. continue;
  832. }
  833. consensus_diff_queue_diff_work(c, most_recent);
  834. } SMARTLIST_FOREACH_END(c);
  835. done:
  836. smartlist_free(matches);
  837. smartlist_free(diffs);
  838. smartlist_free(compute_diffs_from);
  839. strmap_free(have_diff_from, NULL);
  840. }
  841. /**
  842. * Scan the cache for diffs, and add them to the hashtable.
  843. */
  844. static void
  845. consdiffmgr_diffs_load(void)
  846. {
  847. smartlist_t *diffs = smartlist_new();
  848. consensus_cache_find_all(diffs, cdm_cache_get(),
  849. LABEL_DOCTYPE, DOCTYPE_CONSENSUS_DIFF);
  850. SMARTLIST_FOREACH_BEGIN(diffs, consensus_cache_entry_t *, diff) {
  851. const char *lv_flavor =
  852. consensus_cache_entry_get_value(diff, LABEL_FLAVOR);
  853. if (!lv_flavor)
  854. continue;
  855. int flavor = networkstatus_parse_flavor_name(lv_flavor);
  856. if (flavor < 0)
  857. continue;
  858. const char *lv_compression =
  859. consensus_cache_entry_get_value(diff, LABEL_COMPRESSION_TYPE);
  860. compress_method_t method = NO_METHOD;
  861. if (lv_compression) {
  862. method = compression_method_get_by_name(lv_compression);
  863. if (method == UNKNOWN_METHOD) {
  864. continue;
  865. }
  866. }
  867. uint8_t from_sha3[DIGEST256_LEN];
  868. uint8_t to_sha3[DIGEST256_LEN];
  869. if (cdm_entry_get_sha3_value(from_sha3, diff, LABEL_FROM_SHA3_DIGEST)<0)
  870. continue;
  871. if (cdm_entry_get_sha3_value(to_sha3, diff, LABEL_TARGET_SHA3_DIGEST)<0)
  872. continue;
  873. cdm_diff_ht_set_status(flavor, from_sha3, to_sha3,
  874. method,
  875. CDM_DIFF_PRESENT,
  876. consensus_cache_entry_handle_new(diff));
  877. } SMARTLIST_FOREACH_END(diff);
  878. smartlist_free(diffs);
  879. }
  880. /**
  881. * Build new diffs as needed.
  882. */
  883. void
  884. consdiffmgr_rescan(void)
  885. {
  886. if (cdm_cache_dirty == 0)
  887. return;
  888. // Clean up here to make room for new diffs, and to ensure that older
  889. // consensuses do not have any entries.
  890. consdiffmgr_cleanup();
  891. if (cdm_cache_loaded == 0) {
  892. consdiffmgr_diffs_load();
  893. cdm_cache_loaded = 1;
  894. }
  895. for (int flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  896. consdiffmgr_rescan_flavor_((consensus_flavor_t) flav);
  897. }
  898. cdm_cache_dirty = 0;
  899. }
  900. /**
  901. * Helper: compare two files by their from-valid-after and valid-after labels,
  902. * trying to sort in ascending order by from-valid-after (when present) and
  903. * valid-after (when not). Place everything that has neither label first in
  904. * the list.
  905. */
  906. static int
  907. compare_by_staleness_(const void **a, const void **b)
  908. {
  909. const consensus_cache_entry_t *e1 = *a;
  910. const consensus_cache_entry_t *e2 = *b;
  911. const char *va1, *fva1, *va2, *fva2;
  912. va1 = consensus_cache_entry_get_value(e1, LABEL_VALID_AFTER);
  913. va2 = consensus_cache_entry_get_value(e2, LABEL_VALID_AFTER);
  914. fva1 = consensus_cache_entry_get_value(e1, LABEL_FROM_VALID_AFTER);
  915. fva2 = consensus_cache_entry_get_value(e2, LABEL_FROM_VALID_AFTER);
  916. if (fva1)
  917. va1 = fva1;
  918. if (fva2)
  919. va2 = fva2;
  920. /* See note about iso-encoded values in compare_by_valid_after_. Also note
  921. * that missing dates will get placed first. */
  922. return strcmp_opt(va1, va2);
  923. }
  924. /** If there are not enough unused filenames to store <b>n</b> files, then
  925. * delete old consensuses until there are. (We have to keep track of the
  926. * number of filenames because of the way that the seccomp2 cache works.)
  927. *
  928. * Return 0 on success, -1 on failure.
  929. **/
  930. static int
  931. consdiffmgr_ensure_space_for_files(int n)
  932. {
  933. consensus_cache_t *cache = cdm_cache_get();
  934. if (consensus_cache_get_n_filenames_available(cache) >= n) {
  935. // there are already enough unused filenames.
  936. return 0;
  937. }
  938. // Try a cheap deletion of stuff that's waiting to get deleted.
  939. consensus_cache_delete_pending(cache, 0);
  940. if (consensus_cache_get_n_filenames_available(cache) >= n) {
  941. // okay, _that_ made enough filenames available.
  942. return 0;
  943. }
  944. // Let's get more assertive: clean out unused stuff, and force-remove
  945. // the files.
  946. consdiffmgr_cleanup();
  947. consensus_cache_delete_pending(cache, 1);
  948. const int n_to_remove = n - consensus_cache_get_n_filenames_available(cache);
  949. if (n_to_remove <= 0) {
  950. // okay, finally!
  951. return 0;
  952. }
  953. // At this point, we're going to have to throw out objects that will be
  954. // missed. Too bad!
  955. smartlist_t *objects = smartlist_new();
  956. consensus_cache_find_all(objects, cache, NULL, NULL);
  957. smartlist_sort(objects, compare_by_staleness_);
  958. int n_marked = 0;
  959. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  960. consensus_cache_entry_mark_for_removal(ent);
  961. if (++n_marked >= n_to_remove)
  962. break;
  963. } SMARTLIST_FOREACH_END(ent);
  964. consensus_cache_delete_pending(cache, 1);
  965. if (BUG(n_marked < n_to_remove))
  966. return -1;
  967. else
  968. return 0;
  969. }
  970. /**
  971. * Set consensus cache flags on the objects in this consdiffmgr.
  972. */
  973. static void
  974. consdiffmgr_set_cache_flags(void)
  975. {
  976. /* Right now, we just mark the consensus objects for aggressive release,
  977. * so that they get mmapped for as little time as possible. */
  978. smartlist_t *objects = smartlist_new();
  979. consensus_cache_find_all(objects, cdm_cache_get(), LABEL_DOCTYPE,
  980. DOCTYPE_CONSENSUS);
  981. SMARTLIST_FOREACH_BEGIN(objects, consensus_cache_entry_t *, ent) {
  982. consensus_cache_entry_mark_for_aggressive_release(ent);
  983. } SMARTLIST_FOREACH_END(ent);
  984. smartlist_free(objects);
  985. }
  986. /**
  987. * Called before shutdown: drop all storage held by the consdiffmgr.c module.
  988. */
  989. void
  990. consdiffmgr_free_all(void)
  991. {
  992. cdm_diff_t **diff, **next;
  993. for (diff = HT_START(cdm_diff_ht, &cdm_diff_ht); diff; diff = next) {
  994. cdm_diff_t *this = *diff;
  995. next = HT_NEXT_RMV(cdm_diff_ht, &cdm_diff_ht, diff);
  996. cdm_diff_free(this);
  997. }
  998. int i;
  999. unsigned j;
  1000. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  1001. for (j = 0; j < n_consensus_compression_methods(); ++j) {
  1002. consensus_cache_entry_handle_free(latest_consensus[i][j]);
  1003. }
  1004. }
  1005. memset(latest_consensus, 0, sizeof(latest_consensus));
  1006. consensus_cache_free(cons_diff_cache);
  1007. cons_diff_cache = NULL;
  1008. }
  1009. /* =====
  1010. Thread workers
  1011. =====*/
  1012. typedef struct compressed_result_t {
  1013. config_line_t *labels;
  1014. /**
  1015. * Output: Body of the diff, as compressed.
  1016. */
  1017. uint8_t *body;
  1018. /**
  1019. * Output: length of body_out
  1020. */
  1021. size_t bodylen;
  1022. } compressed_result_t;
  1023. /**
  1024. * Compress the bytestring <b>input</b> of length <b>len</b> using the
  1025. * <n>n_methods</b> compression methods listed in the array <b>methods</b>.
  1026. *
  1027. * For each successful compression, set the fields in the <b>results_out</b>
  1028. * array in the position corresponding to the compression method. Use
  1029. * <b>labels_in</b> as a basis for the labels of the result.
  1030. *
  1031. * Return 0 if all compression succeeded; -1 if any failed.
  1032. */
  1033. static int
  1034. compress_multiple(compressed_result_t *results_out, int n_methods,
  1035. const compress_method_t *methods,
  1036. const uint8_t *input, size_t len,
  1037. const config_line_t *labels_in)
  1038. {
  1039. int rv = 0;
  1040. int i;
  1041. for (i = 0; i < n_methods; ++i) {
  1042. compress_method_t method = methods[i];
  1043. const char *methodname = compression_method_get_name(method);
  1044. char *result;
  1045. size_t sz;
  1046. if (0 == tor_compress(&result, &sz, (const char*)input, len, method)) {
  1047. results_out[i].body = (uint8_t*)result;
  1048. results_out[i].bodylen = sz;
  1049. results_out[i].labels = config_lines_dup(labels_in);
  1050. cdm_labels_prepend_sha3(&results_out[i].labels, LABEL_SHA3_DIGEST,
  1051. results_out[i].body,
  1052. results_out[i].bodylen);
  1053. config_line_prepend(&results_out[i].labels,
  1054. LABEL_COMPRESSION_TYPE,
  1055. methodname);
  1056. } else {
  1057. rv = -1;
  1058. }
  1059. }
  1060. return rv;
  1061. }
  1062. /**
  1063. * Given an array of <b>n</b> compressed_result_t in <b>results</b>,
  1064. * as produced by compress_multiple, store them all into the
  1065. * consdiffmgr, and store handles to them in the <b>handles_out</b>
  1066. * array.
  1067. *
  1068. * Return CDM_DIFF_PRESENT if any was stored, and CDM_DIFF_ERROR if none
  1069. * was stored.
  1070. */
  1071. static cdm_diff_status_t
  1072. store_multiple(consensus_cache_entry_handle_t **handles_out,
  1073. int n,
  1074. const compress_method_t *methods,
  1075. const compressed_result_t *results,
  1076. const char *description)
  1077. {
  1078. cdm_diff_status_t status = CDM_DIFF_ERROR;
  1079. consdiffmgr_ensure_space_for_files(n);
  1080. int i;
  1081. for (i = 0; i < n; ++i) {
  1082. compress_method_t method = methods[i];
  1083. uint8_t *body_out = results[i].body;
  1084. size_t bodylen_out = results[i].bodylen;
  1085. config_line_t *labels = results[i].labels;
  1086. const char *methodname = compression_method_get_name(method);
  1087. if (body_out && bodylen_out && labels) {
  1088. /* Success! Store the results */
  1089. log_info(LD_DIRSERV, "Adding %s, compressed with %s",
  1090. description, methodname);
  1091. consensus_cache_entry_t *ent =
  1092. consensus_cache_add(cdm_cache_get(),
  1093. labels,
  1094. body_out,
  1095. bodylen_out);
  1096. if (BUG(ent == NULL))
  1097. continue;
  1098. status = CDM_DIFF_PRESENT;
  1099. handles_out[i] = consensus_cache_entry_handle_new(ent);
  1100. consensus_cache_entry_decref(ent);
  1101. }
  1102. }
  1103. return status;
  1104. }
  1105. /**
  1106. * An object passed to a worker thread that will try to produce a consensus
  1107. * diff.
  1108. */
  1109. typedef struct consensus_diff_worker_job_t {
  1110. /**
  1111. * Input: The consensus to compute the diff from. Holds a reference to the
  1112. * cache entry, which must not be released until the job is passed back to
  1113. * the main thread. The body must be mapped into memory in the main thread.
  1114. */
  1115. consensus_cache_entry_t *diff_from;
  1116. /**
  1117. * Input: The consensus to compute the diff to. Holds a reference to the
  1118. * cache entry, which must not be released until the job is passed back to
  1119. * the main thread. The body must be mapped into memory in the main thread.
  1120. */
  1121. consensus_cache_entry_t *diff_to;
  1122. /** Output: labels and bodies */
  1123. compressed_result_t out[ARRAY_LENGTH(compress_diffs_with)];
  1124. } consensus_diff_worker_job_t;
  1125. /** Given a consensus_cache_entry_t, check whether it has a label claiming
  1126. * that it was compressed. If so, uncompress its contents into <b>out</b> and
  1127. * set <b>outlen</b> to hold their size. If not, just copy the body into
  1128. * <b>out</b> and set <b>outlen</b> to its length. Return 0 on success,
  1129. * -1 on failure.
  1130. *
  1131. * In all cases, the output is nul-terminated. */
  1132. STATIC int
  1133. uncompress_or_copy(char **out, size_t *outlen,
  1134. consensus_cache_entry_t *ent)
  1135. {
  1136. const uint8_t *body;
  1137. size_t bodylen;
  1138. if (consensus_cache_entry_get_body(ent, &body, &bodylen) < 0)
  1139. return -1;
  1140. const char *lv_compression =
  1141. consensus_cache_entry_get_value(ent, LABEL_COMPRESSION_TYPE);
  1142. compress_method_t method = NO_METHOD;
  1143. if (lv_compression)
  1144. method = compression_method_get_by_name(lv_compression);
  1145. return tor_uncompress(out, outlen, (const char *)body, bodylen,
  1146. method, 1, LOG_WARN);
  1147. }
  1148. /**
  1149. * Worker function. This function runs inside a worker thread and receives
  1150. * a consensus_diff_worker_job_t as its input.
  1151. */
  1152. static workqueue_reply_t
  1153. consensus_diff_worker_threadfn(void *state_, void *work_)
  1154. {
  1155. (void)state_;
  1156. consensus_diff_worker_job_t *job = work_;
  1157. const uint8_t *diff_from, *diff_to;
  1158. size_t len_from, len_to;
  1159. int r;
  1160. /* We need to have the body already mapped into RAM here.
  1161. */
  1162. r = consensus_cache_entry_get_body(job->diff_from, &diff_from, &len_from);
  1163. if (BUG(r < 0))
  1164. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  1165. r = consensus_cache_entry_get_body(job->diff_to, &diff_to, &len_to);
  1166. if (BUG(r < 0))
  1167. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  1168. const char *lv_to_valid_after =
  1169. consensus_cache_entry_get_value(job->diff_to, LABEL_VALID_AFTER);
  1170. const char *lv_from_valid_after =
  1171. consensus_cache_entry_get_value(job->diff_from, LABEL_VALID_AFTER);
  1172. const char *lv_from_digest =
  1173. consensus_cache_entry_get_value(job->diff_from,
  1174. LABEL_SHA3_DIGEST_AS_SIGNED);
  1175. const char *lv_from_flavor =
  1176. consensus_cache_entry_get_value(job->diff_from, LABEL_FLAVOR);
  1177. const char *lv_to_flavor =
  1178. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  1179. const char *lv_to_digest =
  1180. consensus_cache_entry_get_value(job->diff_to,
  1181. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  1182. if (! lv_from_digest) {
  1183. /* This isn't a bug right now, since it can happen if you're migrating
  1184. * from an older version of master to a newer one. The older ones didn't
  1185. * annotate their stored consensus objects with sha3-digest-as-signed.
  1186. */
  1187. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  1188. }
  1189. /* All these values are mandatory on the input */
  1190. if (BUG(!lv_to_valid_after) ||
  1191. BUG(!lv_from_valid_after) ||
  1192. BUG(!lv_from_flavor) ||
  1193. BUG(!lv_to_flavor)) {
  1194. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  1195. }
  1196. /* The flavors need to match */
  1197. if (BUG(strcmp(lv_from_flavor, lv_to_flavor))) {
  1198. return WQ_RPL_REPLY; // LCOV_EXCL_LINE
  1199. }
  1200. char *consensus_diff;
  1201. {
  1202. char *diff_from_nt = NULL, *diff_to_nt = NULL;
  1203. size_t diff_from_nt_len, diff_to_nt_len;
  1204. if (uncompress_or_copy(&diff_from_nt, &diff_from_nt_len,
  1205. job->diff_from) < 0) {
  1206. return WQ_RPL_REPLY;
  1207. }
  1208. if (uncompress_or_copy(&diff_to_nt, &diff_to_nt_len,
  1209. job->diff_to) < 0) {
  1210. tor_free(diff_from_nt);
  1211. return WQ_RPL_REPLY;
  1212. }
  1213. tor_assert(diff_from_nt);
  1214. tor_assert(diff_to_nt);
  1215. // XXXX ugh; this is going to calculate the SHA3 of both its
  1216. // XXXX inputs again, even though we already have that. Maybe it's time
  1217. // XXXX to change the API here?
  1218. consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt);
  1219. tor_free(diff_from_nt);
  1220. tor_free(diff_to_nt);
  1221. }
  1222. if (!consensus_diff) {
  1223. /* Couldn't generate consensus; we'll leave the reply blank. */
  1224. return WQ_RPL_REPLY;
  1225. }
  1226. /* Compress the results and send the reply */
  1227. tor_assert(compress_diffs_with[0] == NO_METHOD);
  1228. size_t difflen = strlen(consensus_diff);
  1229. job->out[0].body = (uint8_t *) consensus_diff;
  1230. job->out[0].bodylen = difflen;
  1231. config_line_t *common_labels = NULL;
  1232. cdm_labels_prepend_sha3(&common_labels,
  1233. LABEL_SHA3_DIGEST_UNCOMPRESSED,
  1234. job->out[0].body,
  1235. job->out[0].bodylen);
  1236. config_line_prepend(&common_labels, LABEL_FROM_VALID_AFTER,
  1237. lv_from_valid_after);
  1238. config_line_prepend(&common_labels, LABEL_VALID_AFTER,
  1239. lv_to_valid_after);
  1240. config_line_prepend(&common_labels, LABEL_FLAVOR, lv_from_flavor);
  1241. config_line_prepend(&common_labels, LABEL_FROM_SHA3_DIGEST,
  1242. lv_from_digest);
  1243. config_line_prepend(&common_labels, LABEL_TARGET_SHA3_DIGEST,
  1244. lv_to_digest);
  1245. config_line_prepend(&common_labels, LABEL_DOCTYPE,
  1246. DOCTYPE_CONSENSUS_DIFF);
  1247. job->out[0].labels = config_lines_dup(common_labels);
  1248. cdm_labels_prepend_sha3(&job->out[0].labels,
  1249. LABEL_SHA3_DIGEST,
  1250. job->out[0].body,
  1251. job->out[0].bodylen);
  1252. compress_multiple(job->out+1,
  1253. n_diff_compression_methods()-1,
  1254. compress_diffs_with+1,
  1255. (const uint8_t*)consensus_diff, difflen, common_labels);
  1256. config_free_lines(common_labels);
  1257. return WQ_RPL_REPLY;
  1258. }
  1259. /**
  1260. * Helper: release all storage held in <b>job</b>.
  1261. */
  1262. static void
  1263. consensus_diff_worker_job_free(consensus_diff_worker_job_t *job)
  1264. {
  1265. if (!job)
  1266. return;
  1267. unsigned u;
  1268. for (u = 0; u < n_diff_compression_methods(); ++u) {
  1269. config_free_lines(job->out[u].labels);
  1270. tor_free(job->out[u].body);
  1271. }
  1272. consensus_cache_entry_decref(job->diff_from);
  1273. consensus_cache_entry_decref(job->diff_to);
  1274. tor_free(job);
  1275. }
  1276. /**
  1277. * Worker function: This function runs in the main thread, and receives
  1278. * a consensus_diff_worker_job_t that the worker thread has already
  1279. * processed.
  1280. */
  1281. static void
  1282. consensus_diff_worker_replyfn(void *work_)
  1283. {
  1284. tor_assert(in_main_thread());
  1285. tor_assert(work_);
  1286. consensus_diff_worker_job_t *job = work_;
  1287. const char *lv_from_digest =
  1288. consensus_cache_entry_get_value(job->diff_from,
  1289. LABEL_SHA3_DIGEST_AS_SIGNED);
  1290. const char *lv_to_digest =
  1291. consensus_cache_entry_get_value(job->diff_to,
  1292. LABEL_SHA3_DIGEST_UNCOMPRESSED);
  1293. const char *lv_flavor =
  1294. consensus_cache_entry_get_value(job->diff_to, LABEL_FLAVOR);
  1295. if (BUG(lv_from_digest == NULL))
  1296. lv_from_digest = "???"; // LCOV_EXCL_LINE
  1297. if (BUG(lv_to_digest == NULL))
  1298. lv_to_digest = "???"; // LCOV_EXCL_LINE
  1299. uint8_t from_sha3[DIGEST256_LEN];
  1300. uint8_t to_sha3[DIGEST256_LEN];
  1301. int flav = -1;
  1302. int cache = 1;
  1303. if (BUG(cdm_entry_get_sha3_value(from_sha3, job->diff_from,
  1304. LABEL_SHA3_DIGEST_AS_SIGNED) < 0))
  1305. cache = 0;
  1306. if (BUG(cdm_entry_get_sha3_value(to_sha3, job->diff_to,
  1307. LABEL_SHA3_DIGEST_UNCOMPRESSED) < 0))
  1308. cache = 0;
  1309. if (BUG(lv_flavor == NULL)) {
  1310. cache = 0;
  1311. } else if ((flav = networkstatus_parse_flavor_name(lv_flavor)) < 0) {
  1312. cache = 0;
  1313. }
  1314. consensus_cache_entry_handle_t *handles[ARRAY_LENGTH(compress_diffs_with)];
  1315. memset(handles, 0, sizeof(handles));
  1316. char description[128];
  1317. tor_snprintf(description, sizeof(description),
  1318. "consensus diff from %s to %s",
  1319. lv_from_digest, lv_to_digest);
  1320. int status = store_multiple(handles,
  1321. n_diff_compression_methods(),
  1322. compress_diffs_with,
  1323. job->out,
  1324. description);
  1325. if (status != CDM_DIFF_PRESENT) {
  1326. /* Failure! Nothing to do but complain */
  1327. log_warn(LD_DIRSERV,
  1328. "Worker was unable to compute consensus diff "
  1329. "from %s to %s", lv_from_digest, lv_to_digest);
  1330. /* Cache this error so we don't try to compute this one again. */
  1331. status = CDM_DIFF_ERROR;
  1332. }
  1333. unsigned u;
  1334. for (u = 0; u < ARRAY_LENGTH(handles); ++u) {
  1335. compress_method_t method = compress_diffs_with[u];
  1336. if (cache) {
  1337. cdm_diff_ht_set_status(flav, from_sha3, to_sha3, method, status,
  1338. handles[u]);
  1339. } else {
  1340. consensus_cache_entry_handle_free(handles[u]);
  1341. }
  1342. }
  1343. consensus_diff_worker_job_free(job);
  1344. }
  1345. /**
  1346. * Queue the job of computing the diff from <b>diff_from</b> to <b>diff_to</b>
  1347. * in a worker thread.
  1348. */
  1349. static int
  1350. consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
  1351. consensus_cache_entry_t *diff_to)
  1352. {
  1353. tor_assert(in_main_thread());
  1354. consensus_cache_entry_incref(diff_from);
  1355. consensus_cache_entry_incref(diff_to);
  1356. consensus_diff_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  1357. job->diff_from = diff_from;
  1358. job->diff_to = diff_to;
  1359. /* Make sure body is mapped. */
  1360. const uint8_t *body;
  1361. size_t bodylen;
  1362. int r1 = consensus_cache_entry_get_body(diff_from, &body, &bodylen);
  1363. int r2 = consensus_cache_entry_get_body(diff_to, &body, &bodylen);
  1364. if (r1 < 0 || r2 < 0)
  1365. goto err;
  1366. workqueue_entry_t *work;
  1367. work = cpuworker_queue_work(consensus_diff_worker_threadfn,
  1368. consensus_diff_worker_replyfn,
  1369. job);
  1370. if (!work)
  1371. goto err;
  1372. return 0;
  1373. err:
  1374. consensus_diff_worker_job_free(job); // includes decrefs.
  1375. return -1;
  1376. }
  1377. /**
  1378. * Holds requests and replies for consensus_compress_workers.
  1379. */
  1380. typedef struct consensus_compress_worker_job_t {
  1381. char *consensus;
  1382. size_t consensus_len;
  1383. consensus_flavor_t flavor;
  1384. time_t valid_after;
  1385. compressed_result_t out[ARRAY_LENGTH(compress_consensus_with)];
  1386. } consensus_compress_worker_job_t;
  1387. /**
  1388. * Free all resources held in <b>job</b>
  1389. */
  1390. static void
  1391. consensus_compress_worker_job_free(consensus_compress_worker_job_t *job)
  1392. {
  1393. if (!job)
  1394. return;
  1395. tor_free(job->consensus);
  1396. unsigned u;
  1397. for (u = 0; u < n_consensus_compression_methods(); ++u) {
  1398. config_free_lines(job->out[u].labels);
  1399. tor_free(job->out[u].body);
  1400. }
  1401. tor_free(job);
  1402. }
  1403. /**
  1404. * Worker function. This function runs inside a worker thread and receives
  1405. * a consensus_compress_worker_job_t as its input.
  1406. */
  1407. static workqueue_reply_t
  1408. consensus_compress_worker_threadfn(void *state_, void *work_)
  1409. {
  1410. (void)state_;
  1411. consensus_compress_worker_job_t *job = work_;
  1412. consensus_flavor_t flavor = job->flavor;
  1413. const char *consensus = job->consensus;
  1414. size_t bodylen = job->consensus_len;
  1415. time_t valid_after = job->valid_after;
  1416. config_line_t *labels = NULL;
  1417. char formatted_time[ISO_TIME_LEN+1];
  1418. format_iso_time_nospace(formatted_time, valid_after);
  1419. const char *flavname = networkstatus_get_flavor_name(flavor);
  1420. cdm_labels_prepend_sha3(&labels, LABEL_SHA3_DIGEST_UNCOMPRESSED,
  1421. (const uint8_t *)consensus, bodylen);
  1422. {
  1423. const char *start, *end;
  1424. if (router_get_networkstatus_v3_signed_boundaries(consensus,
  1425. &start, &end) < 0) {
  1426. start = consensus;
  1427. end = consensus+bodylen;
  1428. }
  1429. cdm_labels_prepend_sha3(&labels, LABEL_SHA3_DIGEST_AS_SIGNED,
  1430. (const uint8_t *)start,
  1431. end - start);
  1432. }
  1433. config_line_prepend(&labels, LABEL_FLAVOR, flavname);
  1434. config_line_prepend(&labels, LABEL_VALID_AFTER, formatted_time);
  1435. config_line_prepend(&labels, LABEL_DOCTYPE, DOCTYPE_CONSENSUS);
  1436. compress_multiple(job->out,
  1437. n_consensus_compression_methods(),
  1438. compress_consensus_with,
  1439. (const uint8_t*)consensus, bodylen, labels);
  1440. config_free_lines(labels);
  1441. return WQ_RPL_REPLY;
  1442. }
  1443. /**
  1444. * Worker function: This function runs in the main thread, and receives
  1445. * a consensus_diff_compress_job_t that the worker thread has already
  1446. * processed.
  1447. */
  1448. static void
  1449. consensus_compress_worker_replyfn(void *work_)
  1450. {
  1451. consensus_compress_worker_job_t *job = work_;
  1452. consensus_cache_entry_handle_t *handles[
  1453. ARRAY_LENGTH(compress_consensus_with)];
  1454. memset(handles, 0, sizeof(handles));
  1455. store_multiple(handles,
  1456. n_consensus_compression_methods(),
  1457. compress_consensus_with,
  1458. job->out,
  1459. "consensus");
  1460. cdm_cache_dirty = 1;
  1461. unsigned u;
  1462. consensus_flavor_t f = job->flavor;
  1463. tor_assert(f < N_CONSENSUS_FLAVORS);
  1464. for (u = 0; u < ARRAY_LENGTH(handles); ++u) {
  1465. if (handles[u] == NULL)
  1466. continue;
  1467. consensus_cache_entry_handle_free(latest_consensus[f][u]);
  1468. latest_consensus[f][u] = handles[u];
  1469. }
  1470. consensus_compress_worker_job_free(job);
  1471. }
  1472. /**
  1473. * If true, we compress in worker threads.
  1474. */
  1475. static int background_compression = 0;
  1476. /**
  1477. * Queue a job to compress <b>consensus</b> and store its compressed
  1478. * text in the cache.
  1479. */
  1480. static int
  1481. consensus_queue_compression_work(const char *consensus,
  1482. consensus_flavor_t flavor,
  1483. time_t valid_after)
  1484. {
  1485. consensus_compress_worker_job_t *job = tor_malloc_zero(sizeof(*job));
  1486. job->consensus = tor_strdup(consensus);
  1487. job->consensus_len = strlen(consensus);
  1488. job->flavor = flavor;
  1489. job->valid_after = valid_after;
  1490. if (background_compression) {
  1491. workqueue_entry_t *work;
  1492. work = cpuworker_queue_work(consensus_compress_worker_threadfn,
  1493. consensus_compress_worker_replyfn,
  1494. job);
  1495. if (!work) {
  1496. consensus_compress_worker_job_free(job);
  1497. return -1;
  1498. }
  1499. return 0;
  1500. } else {
  1501. consensus_compress_worker_threadfn(NULL, job);
  1502. consensus_compress_worker_replyfn(job);
  1503. return 0;
  1504. }
  1505. }
  1506. /**
  1507. * Tell the consdiffmgr backend to compress consensuses in worker threads.
  1508. */
  1509. void
  1510. consdiffmgr_enable_background_compression(void)
  1511. {
  1512. // This isn't the default behavior because it would break unit tests.
  1513. background_compression = 1;
  1514. }