12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556 |
- #define CIRCUITSTATS_PRIVATE
- #include "or.h"
- #include "circuitbuild.h"
- #include "circuitstats.h"
- #include "config.h"
- #include "confparse.h"
- #include "control.h"
- #include "networkstatus.h"
- #include "statefile.h"
- #undef log
- #include <math.h>
- #define CBT_BIN_TO_MS(bin) ((bin)*CBT_BIN_WIDTH + (CBT_BIN_WIDTH/2))
- circuit_build_times_t circ_times;
- static int unit_tests = 0;
- int
- circuit_build_times_disabled(void)
- {
- if (unit_tests) {
- return 0;
- } else {
- int consensus_disabled = networkstatus_get_param(NULL, "cbtdisabled",
- 0, 0, 1);
- int config_disabled = !get_options()->LearnCircuitBuildTimeout;
- int dirauth_disabled = get_options()->AuthoritativeDir;
- int state_disabled = did_last_state_file_write_fail() ? 1 : 0;
- if (consensus_disabled || config_disabled || dirauth_disabled ||
- state_disabled) {
- log_debug(LD_CIRC,
- "CircuitBuildTime learning is disabled. "
- "Consensus=%d, Config=%d, AuthDir=%d, StateFile=%d",
- consensus_disabled, config_disabled, dirauth_disabled,
- state_disabled);
- return 1;
- } else {
- log_debug(LD_CIRC,
- "CircuitBuildTime learning is not disabled. "
- "Consensus=%d, Config=%d, AuthDir=%d, StateFile=%d",
- consensus_disabled, config_disabled, dirauth_disabled,
- state_disabled);
- return 0;
- }
- }
- }
- static int32_t
- circuit_build_times_max_timeouts(void)
- {
- int32_t cbt_maxtimeouts;
- cbt_maxtimeouts = networkstatus_get_param(NULL, "cbtmaxtimeouts",
- CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT,
- CBT_MIN_MAX_RECENT_TIMEOUT_COUNT,
- CBT_MAX_MAX_RECENT_TIMEOUT_COUNT);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_max_timeouts() called, cbtmaxtimeouts is"
- " %d",
- cbt_maxtimeouts);
- }
- return cbt_maxtimeouts;
- }
- static int32_t
- circuit_build_times_default_num_xm_modes(void)
- {
- int32_t num = networkstatus_get_param(NULL, "cbtnummodes",
- CBT_DEFAULT_NUM_XM_MODES,
- CBT_MIN_NUM_XM_MODES,
- CBT_MAX_NUM_XM_MODES);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_default_num_xm_modes() called, cbtnummodes"
- " is %d",
- num);
- }
- return num;
- }
- static int32_t
- circuit_build_times_min_circs_to_observe(void)
- {
- int32_t num = networkstatus_get_param(NULL, "cbtmincircs",
- CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE,
- CBT_MIN_MIN_CIRCUITS_TO_OBSERVE,
- CBT_MAX_MIN_CIRCUITS_TO_OBSERVE);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_min_circs_to_observe() called, cbtmincircs"
- " is %d",
- num);
- }
- return num;
- }
- int
- circuit_build_times_enough_to_compute(circuit_build_times_t *cbt)
- {
- return cbt->total_build_times >= circuit_build_times_min_circs_to_observe();
- }
- double
- circuit_build_times_quantile_cutoff(void)
- {
- int32_t num = networkstatus_get_param(NULL, "cbtquantile",
- CBT_DEFAULT_QUANTILE_CUTOFF,
- CBT_MIN_QUANTILE_CUTOFF,
- CBT_MAX_QUANTILE_CUTOFF);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_quantile_cutoff() called, cbtquantile"
- " is %d",
- num);
- }
- return num/100.0;
- }
- static double
- circuit_build_times_close_quantile(void)
- {
- int32_t param;
-
- int32_t min = (int)tor_lround(100*circuit_build_times_quantile_cutoff());
- param = networkstatus_get_param(NULL, "cbtclosequantile",
- CBT_DEFAULT_CLOSE_QUANTILE,
- CBT_MIN_CLOSE_QUANTILE,
- CBT_MAX_CLOSE_QUANTILE);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_close_quantile() called, cbtclosequantile"
- " is %d", param);
- }
- if (param < min) {
- log_warn(LD_DIR, "Consensus parameter cbtclosequantile is "
- "too small, raising to %d", min);
- param = min;
- }
- return param / 100.0;
- }
- static int32_t
- circuit_build_times_test_frequency(void)
- {
- int32_t num = networkstatus_get_param(NULL, "cbttestfreq",
- CBT_DEFAULT_TEST_FREQUENCY,
- CBT_MIN_TEST_FREQUENCY,
- CBT_MAX_TEST_FREQUENCY);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_test_frequency() called, cbttestfreq is %d",
- num);
- }
- return num;
- }
- static int32_t
- circuit_build_times_min_timeout(void)
- {
- int32_t num = networkstatus_get_param(NULL, "cbtmintimeout",
- CBT_DEFAULT_TIMEOUT_MIN_VALUE,
- CBT_MIN_TIMEOUT_MIN_VALUE,
- CBT_MAX_TIMEOUT_MIN_VALUE);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_min_timeout() called, cbtmintimeout is %d",
- num);
- }
- return num;
- }
- int32_t
- circuit_build_times_initial_timeout(void)
- {
- int32_t min = circuit_build_times_min_timeout();
- int32_t param = networkstatus_get_param(NULL, "cbtinitialtimeout",
- CBT_DEFAULT_TIMEOUT_INITIAL_VALUE,
- CBT_MIN_TIMEOUT_INITIAL_VALUE,
- CBT_MAX_TIMEOUT_INITIAL_VALUE);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_initial_timeout() called, "
- "cbtinitialtimeout is %d",
- param);
- }
- if (param < min) {
- log_warn(LD_DIR, "Consensus parameter cbtinitialtimeout is too small, "
- "raising to %d", min);
- param = min;
- }
- return param;
- }
- static int32_t
- circuit_build_times_recent_circuit_count(networkstatus_t *ns)
- {
- int32_t num;
- num = networkstatus_get_param(ns, "cbtrecentcount",
- CBT_DEFAULT_RECENT_CIRCUITS,
- CBT_MIN_RECENT_CIRCUITS,
- CBT_MAX_RECENT_CIRCUITS);
- if (!(get_options()->LearnCircuitBuildTimeout)) {
- log_debug(LD_BUG,
- "circuit_build_times_recent_circuit_count() called, "
- "cbtrecentcount is %d",
- num);
- }
- return num;
- }
- void
- circuit_build_times_new_consensus_params(circuit_build_times_t *cbt,
- networkstatus_t *ns)
- {
- int32_t num;
-
- if (!circuit_build_times_disabled()) {
- num = circuit_build_times_recent_circuit_count(ns);
- if (num > 0) {
- if (num != cbt->liveness.num_recent_circs) {
- int8_t *recent_circs;
- log_notice(LD_CIRC, "The Tor Directory Consensus has changed how many "
- "circuits we must track to detect network failures from %d "
- "to %d.", cbt->liveness.num_recent_circs, num);
- tor_assert(cbt->liveness.timeouts_after_firsthop ||
- cbt->liveness.num_recent_circs == 0);
-
- recent_circs = tor_malloc_zero(sizeof(int8_t)*num);
- if (cbt->liveness.timeouts_after_firsthop &&
- cbt->liveness.num_recent_circs > 0) {
- memcpy(recent_circs, cbt->liveness.timeouts_after_firsthop,
- sizeof(int8_t)*MIN(num, cbt->liveness.num_recent_circs));
- }
-
- if (num < cbt->liveness.num_recent_circs) {
- cbt->liveness.after_firsthop_idx = MIN(num-1,
- cbt->liveness.after_firsthop_idx);
- }
- tor_free(cbt->liveness.timeouts_after_firsthop);
- cbt->liveness.timeouts_after_firsthop = recent_circs;
- cbt->liveness.num_recent_circs = num;
- }
-
- } else {
-
- log_warn(LD_CIRC,
- "The cbtrecentcircs consensus parameter came back zero! "
- "This disables adaptive timeouts since we can't keep track of "
- "any recent circuits.");
- circuit_build_times_free_timeouts(cbt);
- }
- } else {
-
- circuit_build_times_free_timeouts(cbt);
- }
- }
- static double
- circuit_build_times_get_initial_timeout(void)
- {
- double timeout;
-
- if (!unit_tests && get_options()->CircuitBuildTimeout) {
- timeout = get_options()->CircuitBuildTimeout*1000;
- if (get_options()->LearnCircuitBuildTimeout &&
- timeout < circuit_build_times_min_timeout()) {
- log_warn(LD_CIRC, "Config CircuitBuildTimeout too low. Setting to %ds",
- circuit_build_times_min_timeout()/1000);
- timeout = circuit_build_times_min_timeout();
- }
- } else {
- timeout = circuit_build_times_initial_timeout();
- }
- return timeout;
- }
- void
- circuit_build_times_reset(circuit_build_times_t *cbt)
- {
- memset(cbt->circuit_build_times, 0, sizeof(cbt->circuit_build_times));
- cbt->total_build_times = 0;
- cbt->build_times_idx = 0;
- cbt->have_computed_timeout = 0;
- }
- void
- circuit_build_times_init(circuit_build_times_t *cbt)
- {
- memset(cbt, 0, sizeof(*cbt));
-
- if (!circuit_build_times_disabled()) {
- cbt->liveness.num_recent_circs =
- circuit_build_times_recent_circuit_count(NULL);
- cbt->liveness.timeouts_after_firsthop =
- tor_malloc_zero(sizeof(int8_t)*cbt->liveness.num_recent_circs);
- } else {
- cbt->liveness.num_recent_circs = 0;
- cbt->liveness.timeouts_after_firsthop = NULL;
- }
- cbt->close_ms = cbt->timeout_ms = circuit_build_times_get_initial_timeout();
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
- }
- void
- circuit_build_times_free_timeouts(circuit_build_times_t *cbt)
- {
- if (!cbt) return;
- if (cbt->liveness.timeouts_after_firsthop) {
- tor_free(cbt->liveness.timeouts_after_firsthop);
- }
- cbt->liveness.num_recent_circs = 0;
- }
- #if 0
- static void
- circuit_build_times_rewind_history(circuit_build_times_t *cbt, int n)
- {
- int i = 0;
- cbt->build_times_idx -= n;
- cbt->build_times_idx %= CBT_NCIRCUITS_TO_OBSERVE;
- for (i = 0; i < n; i++) {
- cbt->circuit_build_times[(i+cbt->build_times_idx)
- %CBT_NCIRCUITS_TO_OBSERVE]=0;
- }
- if (cbt->total_build_times > n) {
- cbt->total_build_times -= n;
- } else {
- cbt->total_build_times = 0;
- }
- log_info(LD_CIRC,
- "Rewound history by %d places. Current index: %d. "
- "Total: %d", n, cbt->build_times_idx, cbt->total_build_times);
- }
- #endif
- int
- circuit_build_times_add_time(circuit_build_times_t *cbt, build_time_t time)
- {
- if (time <= 0 || time > CBT_BUILD_TIME_MAX) {
- log_warn(LD_BUG, "Circuit build time is too large (%u)."
- "This is probably a bug.", time);
- tor_fragile_assert();
- return -1;
- }
- log_debug(LD_CIRC, "Adding circuit build time %u", time);
- cbt->circuit_build_times[cbt->build_times_idx] = time;
- cbt->build_times_idx = (cbt->build_times_idx + 1) % CBT_NCIRCUITS_TO_OBSERVE;
- if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
- cbt->total_build_times++;
- if ((cbt->total_build_times % CBT_SAVE_STATE_EVERY) == 0) {
-
- if (!unit_tests && !get_options()->AvoidDiskWrites)
- or_state_mark_dirty(get_or_state(), 0);
- }
- return 0;
- }
- static build_time_t
- circuit_build_times_max(circuit_build_times_t *cbt)
- {
- int i = 0;
- build_time_t max_build_time = 0;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] > max_build_time
- && cbt->circuit_build_times[i] != CBT_BUILD_ABANDONED)
- max_build_time = cbt->circuit_build_times[i];
- }
- return max_build_time;
- }
- #if 0
- build_time_t
- circuit_build_times_min(circuit_build_times_t *cbt)
- {
- int i = 0;
- build_time_t min_build_time = CBT_BUILD_TIME_MAX;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] &&
- cbt->circuit_build_times[i] < min_build_time)
- min_build_time = cbt->circuit_build_times[i];
- }
- if (min_build_time == CBT_BUILD_TIME_MAX) {
- log_warn(LD_CIRC, "No build times less than CBT_BUILD_TIME_MAX!");
- }
- return min_build_time;
- }
- #endif
- static uint32_t *
- circuit_build_times_create_histogram(circuit_build_times_t *cbt,
- build_time_t *nbins)
- {
- uint32_t *histogram;
- build_time_t max_build_time = circuit_build_times_max(cbt);
- int i, c;
- *nbins = 1 + (max_build_time / CBT_BIN_WIDTH);
- histogram = tor_malloc_zero(*nbins * sizeof(build_time_t));
-
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] == 0
- || cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
- continue;
- c = (cbt->circuit_build_times[i] / CBT_BIN_WIDTH);
- histogram[c]++;
- }
- return histogram;
- }
- static build_time_t
- circuit_build_times_get_xm(circuit_build_times_t *cbt)
- {
- build_time_t i, nbins;
- build_time_t *nth_max_bin;
- int32_t bin_counts=0;
- build_time_t ret = 0;
- uint32_t *histogram = circuit_build_times_create_histogram(cbt, &nbins);
- int n=0;
- int num_modes = circuit_build_times_default_num_xm_modes();
- tor_assert(nbins > 0);
- tor_assert(num_modes > 0);
-
-
- if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
- num_modes = 1;
- nth_max_bin = (build_time_t*)tor_malloc_zero(num_modes*sizeof(build_time_t));
-
- for (i = 0; i < nbins; i++) {
- if (histogram[i] >= histogram[nth_max_bin[0]]) {
- nth_max_bin[0] = i;
- }
- for (n = 1; n < num_modes; n++) {
- if (histogram[i] >= histogram[nth_max_bin[n]] &&
- (!histogram[nth_max_bin[n-1]]
- || histogram[i] < histogram[nth_max_bin[n-1]])) {
- nth_max_bin[n] = i;
- }
- }
- }
- for (n = 0; n < num_modes; n++) {
- bin_counts += histogram[nth_max_bin[n]];
- ret += CBT_BIN_TO_MS(nth_max_bin[n])*histogram[nth_max_bin[n]];
- log_info(LD_CIRC, "Xm mode #%d: %u %u", n, CBT_BIN_TO_MS(nth_max_bin[n]),
- histogram[nth_max_bin[n]]);
- }
-
- tor_assert(bin_counts > 0);
- ret /= bin_counts;
- tor_free(histogram);
- tor_free(nth_max_bin);
- return ret;
- }
- void
- circuit_build_times_update_state(circuit_build_times_t *cbt,
- or_state_t *state)
- {
- uint32_t *histogram;
- build_time_t i = 0;
- build_time_t nbins = 0;
- config_line_t **next, *line;
- histogram = circuit_build_times_create_histogram(cbt, &nbins);
-
- config_free_lines(state->BuildtimeHistogram);
- next = &state->BuildtimeHistogram;
- *next = NULL;
- state->TotalBuildTimes = cbt->total_build_times;
- state->CircuitBuildAbandonedCount = 0;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
- state->CircuitBuildAbandonedCount++;
- }
- for (i = 0; i < nbins; i++) {
-
- if (histogram[i] == 0) continue;
- *next = line = tor_malloc_zero(sizeof(config_line_t));
- line->key = tor_strdup("CircuitBuildTimeBin");
- tor_asprintf(&line->value, "%d %d",
- CBT_BIN_TO_MS(i), histogram[i]);
- next = &(line->next);
- }
- if (!unit_tests) {
- if (!get_options()->AvoidDiskWrites)
- or_state_mark_dirty(get_or_state(), 0);
- }
- tor_free(histogram);
- }
- static void
- circuit_build_times_shuffle_and_store_array(circuit_build_times_t *cbt,
- build_time_t *raw_times,
- uint32_t num_times)
- {
- uint32_t n = num_times;
- if (num_times > CBT_NCIRCUITS_TO_OBSERVE) {
- log_notice(LD_CIRC, "The number of circuit times that this Tor version "
- "uses to calculate build times is less than the number stored "
- "in your state file. Decreasing the circuit time history from "
- "%lu to %d.", (unsigned long)num_times,
- CBT_NCIRCUITS_TO_OBSERVE);
- }
- if (n > INT_MAX-1) {
- log_warn(LD_CIRC, "For some insane reasons, you had %lu circuit build "
- "observations in your state file. That's far too many; probably "
- "there's a bug here.", (unsigned long)n);
- n = INT_MAX-1;
- }
-
- while (n-- > 1) {
- int k = crypto_rand_int(n + 1);
- build_time_t tmp = raw_times[k];
- raw_times[k] = raw_times[n];
- raw_times[n] = tmp;
- }
-
- for (n = 0; n < MIN(num_times, CBT_NCIRCUITS_TO_OBSERVE); n++) {
- circuit_build_times_add_time(cbt, raw_times[n]);
- }
- }
- static int
- circuit_build_times_filter_timeouts(circuit_build_times_t *cbt)
- {
- int num_filtered=0, i=0;
- double timeout_rate = 0;
- build_time_t max_timeout = 0;
- timeout_rate = circuit_build_times_timeout_rate(cbt);
- max_timeout = (build_time_t)cbt->close_ms;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] > max_timeout) {
- build_time_t replaced = cbt->circuit_build_times[i];
- num_filtered++;
- cbt->circuit_build_times[i] = CBT_BUILD_ABANDONED;
- log_debug(LD_CIRC, "Replaced timeout %d with %d", replaced,
- cbt->circuit_build_times[i]);
- }
- }
- log_info(LD_CIRC,
- "We had %d timeouts out of %d build times, "
- "and filtered %d above the max of %u",
- (int)(cbt->total_build_times*timeout_rate),
- cbt->total_build_times, num_filtered, max_timeout);
- return num_filtered;
- }
- int
- circuit_build_times_parse_state(circuit_build_times_t *cbt,
- or_state_t *state)
- {
- int tot_values = 0;
- uint32_t loaded_cnt = 0, N = 0;
- config_line_t *line;
- unsigned int i;
- build_time_t *loaded_times;
- int err = 0;
- circuit_build_times_init(cbt);
- if (circuit_build_times_disabled()) {
- return 0;
- }
-
- loaded_times = tor_malloc_zero(sizeof(build_time_t)*state->TotalBuildTimes);
- for (line = state->BuildtimeHistogram; line; line = line->next) {
- smartlist_t *args = smartlist_new();
- smartlist_split_string(args, line->value, " ",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- if (smartlist_len(args) < 2) {
- log_warn(LD_GENERAL, "Unable to parse circuit build times: "
- "Too few arguments to CircuitBuildTime");
- err = 1;
- SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
- smartlist_free(args);
- break;
- } else {
- const char *ms_str = smartlist_get(args,0);
- const char *count_str = smartlist_get(args,1);
- uint32_t count, k;
- build_time_t ms;
- int ok;
- ms = (build_time_t)tor_parse_ulong(ms_str, 0, 0,
- CBT_BUILD_TIME_MAX, &ok, NULL);
- if (!ok) {
- log_warn(LD_GENERAL, "Unable to parse circuit build times: "
- "Unparsable bin number");
- err = 1;
- SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
- smartlist_free(args);
- break;
- }
- count = (uint32_t)tor_parse_ulong(count_str, 0, 0,
- UINT32_MAX, &ok, NULL);
- if (!ok) {
- log_warn(LD_GENERAL, "Unable to parse circuit build times: "
- "Unparsable bin count");
- err = 1;
- SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
- smartlist_free(args);
- break;
- }
- if (loaded_cnt+count+state->CircuitBuildAbandonedCount
- > state->TotalBuildTimes) {
- log_warn(LD_CIRC,
- "Too many build times in state file. "
- "Stopping short before %d",
- loaded_cnt+count);
- SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
- smartlist_free(args);
- break;
- }
- for (k = 0; k < count; k++) {
- loaded_times[loaded_cnt++] = ms;
- }
- N++;
- SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
- smartlist_free(args);
- }
- }
- log_info(LD_CIRC,
- "Adding %d timeouts.", state->CircuitBuildAbandonedCount);
- for (i=0; i < state->CircuitBuildAbandonedCount; i++) {
- loaded_times[loaded_cnt++] = CBT_BUILD_ABANDONED;
- }
- if (loaded_cnt != state->TotalBuildTimes) {
- log_warn(LD_CIRC,
- "Corrupt state file? Build times count mismatch. "
- "Read %d times, but file says %d", loaded_cnt,
- state->TotalBuildTimes);
- err = 1;
- circuit_build_times_reset(cbt);
- goto done;
- }
- circuit_build_times_shuffle_and_store_array(cbt, loaded_times, loaded_cnt);
-
- for (i=0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (!cbt->circuit_build_times[i])
- break;
- tot_values++;
- }
- log_info(LD_CIRC,
- "Loaded %d/%d values from %d lines in circuit time histogram",
- tot_values, cbt->total_build_times, N);
- if (cbt->total_build_times != tot_values
- || cbt->total_build_times > CBT_NCIRCUITS_TO_OBSERVE) {
- log_warn(LD_CIRC,
- "Corrupt state file? Shuffled build times mismatch. "
- "Read %d times, but file says %d", tot_values,
- state->TotalBuildTimes);
- err = 1;
- circuit_build_times_reset(cbt);
- goto done;
- }
- circuit_build_times_set_timeout(cbt);
- if (!state->CircuitBuildAbandonedCount && cbt->total_build_times) {
- circuit_build_times_filter_timeouts(cbt);
- }
- done:
- tor_free(loaded_times);
- return err ? -1 : 0;
- }
- int
- circuit_build_times_update_alpha(circuit_build_times_t *cbt)
- {
- build_time_t *x=cbt->circuit_build_times;
- double a = 0;
- int n=0,i=0,abandoned_count=0;
- build_time_t max_time=0;
-
-
- cbt->Xm = circuit_build_times_get_xm(cbt);
- tor_assert(cbt->Xm > 0);
- for (i=0; i< CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (!x[i]) {
- continue;
- }
- if (x[i] < cbt->Xm) {
- a += tor_mathlog(cbt->Xm);
- } else if (x[i] == CBT_BUILD_ABANDONED) {
- abandoned_count++;
- } else {
- a += tor_mathlog(x[i]);
- if (x[i] > max_time)
- max_time = x[i];
- }
- n++;
- }
-
- if (n!=cbt->total_build_times) {
- log_err(LD_CIRC, "Discrepancy in build times count: %d vs %d", n,
- cbt->total_build_times);
- }
- tor_assert(n==cbt->total_build_times);
- if (max_time <= 0) {
-
- log_warn(LD_BUG,
- "Could not determine largest build time (%d). "
- "Xm is %dms and we've abandoned %d out of %d circuits.", max_time,
- cbt->Xm, abandoned_count, n);
- return 0;
- }
- a += abandoned_count*tor_mathlog(max_time);
- a -= n*tor_mathlog(cbt->Xm);
-
-
-
- a = (n-abandoned_count)/a;
- cbt->alpha = a;
- return 1;
- }
- double
- circuit_build_times_calculate_timeout(circuit_build_times_t *cbt,
- double quantile)
- {
- double ret;
- tor_assert(quantile >= 0);
- tor_assert(1.0-quantile > 0);
- tor_assert(cbt->Xm > 0);
- ret = cbt->Xm/pow(1.0-quantile,1.0/cbt->alpha);
- if (ret > INT32_MAX) {
- ret = INT32_MAX;
- }
- tor_assert(ret > 0);
- return ret;
- }
- double
- circuit_build_times_cdf(circuit_build_times_t *cbt, double x)
- {
- double ret;
- tor_assert(cbt->Xm > 0);
- ret = 1.0-pow(cbt->Xm/x,cbt->alpha);
- tor_assert(0 <= ret && ret <= 1.0);
- return ret;
- }
- build_time_t
- circuit_build_times_generate_sample(circuit_build_times_t *cbt,
- double q_lo, double q_hi)
- {
- double randval = crypto_rand_double();
- build_time_t ret;
- double u;
-
-
- q_hi -= 1.0/(INT32_MAX);
- tor_assert(q_lo >= 0);
- tor_assert(q_hi < 1);
- tor_assert(q_lo < q_hi);
- u = q_lo + (q_hi-q_lo)*randval;
- tor_assert(0 <= u && u < 1.0);
-
- ret = (build_time_t)
- tor_lround(circuit_build_times_calculate_timeout(cbt, u));
- tor_assert(ret > 0);
- return ret;
- }
- void
- circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
- double quantile, double timeout_ms)
- {
-
-
-
-
-
-
- tor_assert(quantile >= 0);
- tor_assert(cbt->Xm > 0);
- cbt->alpha = tor_mathlog(1.0-quantile)/
- (tor_mathlog(cbt->Xm)-tor_mathlog(timeout_ms));
- tor_assert(cbt->alpha > 0);
- }
- int
- circuit_build_times_needs_circuits(circuit_build_times_t *cbt)
- {
-
- return !circuit_build_times_enough_to_compute(cbt);
- }
- int
- circuit_build_times_needs_circuits_now(circuit_build_times_t *cbt)
- {
- return circuit_build_times_needs_circuits(cbt) &&
- approx_time()-cbt->last_circ_at > circuit_build_times_test_frequency();
- }
- void
- circuit_build_times_network_is_live(circuit_build_times_t *cbt)
- {
- time_t now = approx_time();
- if (cbt->liveness.nonlive_timeouts > 0) {
- log_notice(LD_CIRC,
- "Tor now sees network activity. Restoring circuit build "
- "timeout recording. Network was down for %d seconds "
- "during %d circuit attempts.",
- (int)(now - cbt->liveness.network_last_live),
- cbt->liveness.nonlive_timeouts);
- }
- cbt->liveness.network_last_live = now;
- cbt->liveness.nonlive_timeouts = 0;
- }
- void
- circuit_build_times_network_circ_success(circuit_build_times_t *cbt)
- {
-
- if (cbt->liveness.timeouts_after_firsthop &&
- cbt->liveness.num_recent_circs > 0) {
- cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx]
- = 0;
- cbt->liveness.after_firsthop_idx++;
- cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
- }
- }
- static void
- circuit_build_times_network_timeout(circuit_build_times_t *cbt,
- int did_onehop)
- {
-
- if (cbt->liveness.timeouts_after_firsthop &&
- cbt->liveness.num_recent_circs > 0) {
- if (did_onehop) {
- cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx]
- = 1;
- cbt->liveness.after_firsthop_idx++;
- cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
- }
- }
- }
- static void
- circuit_build_times_network_close(circuit_build_times_t *cbt,
- int did_onehop, time_t start_time)
- {
- time_t now = time(NULL);
-
- if (cbt->liveness.network_last_live < start_time) {
- if (did_onehop) {
- char last_live_buf[ISO_TIME_LEN+1];
- char start_time_buf[ISO_TIME_LEN+1];
- char now_buf[ISO_TIME_LEN+1];
- format_local_iso_time(last_live_buf, cbt->liveness.network_last_live);
- format_local_iso_time(start_time_buf, start_time);
- format_local_iso_time(now_buf, now);
- log_warn(LD_BUG,
- "Circuit somehow completed a hop while the network was "
- "not live. Network was last live at %s, but circuit launched "
- "at %s. It's now %s.", last_live_buf, start_time_buf,
- now_buf);
- }
- cbt->liveness.nonlive_timeouts++;
- if (cbt->liveness.nonlive_timeouts == 1) {
- log_notice(LD_CIRC,
- "Tor has not observed any network activity for the past %d "
- "seconds. Disabling circuit build timeout recording.",
- (int)(now - cbt->liveness.network_last_live));
- } else {
- log_info(LD_CIRC,
- "Got non-live timeout. Current count is: %d",
- cbt->liveness.nonlive_timeouts);
- }
- }
- }
- int
- circuit_build_times_network_check_live(circuit_build_times_t *cbt)
- {
- if (cbt->liveness.nonlive_timeouts > 0) {
- return 0;
- }
- return 1;
- }
- int
- circuit_build_times_network_check_changed(circuit_build_times_t *cbt)
- {
- int total_build_times = cbt->total_build_times;
- int timeout_count=0;
- int i;
- if (cbt->liveness.timeouts_after_firsthop &&
- cbt->liveness.num_recent_circs > 0) {
-
- for (i = 0; i < cbt->liveness.num_recent_circs; i++) {
- timeout_count += cbt->liveness.timeouts_after_firsthop[i];
- }
- }
-
- if (timeout_count < circuit_build_times_max_timeouts()) {
- return 0;
- }
- circuit_build_times_reset(cbt);
- if (cbt->liveness.timeouts_after_firsthop &&
- cbt->liveness.num_recent_circs > 0) {
- memset(cbt->liveness.timeouts_after_firsthop, 0,
- sizeof(*cbt->liveness.timeouts_after_firsthop)*
- cbt->liveness.num_recent_circs);
- }
- cbt->liveness.after_firsthop_idx = 0;
-
- if (cbt->timeout_ms >= circuit_build_times_get_initial_timeout()) {
- if (cbt->timeout_ms > INT32_MAX/2 || cbt->close_ms > INT32_MAX/2) {
- log_warn(LD_CIRC, "Insanely large circuit build timeout value. "
- "(timeout = %fmsec, close = %fmsec)",
- cbt->timeout_ms, cbt->close_ms);
- } else {
- cbt->timeout_ms *= 2;
- cbt->close_ms *= 2;
- }
- } else {
- cbt->close_ms = cbt->timeout_ms
- = circuit_build_times_get_initial_timeout();
- }
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
- log_notice(LD_CIRC,
- "Your network connection speed appears to have changed. Resetting "
- "timeout to %lds after %d timeouts and %d buildtimes.",
- tor_lround(cbt->timeout_ms/1000), timeout_count,
- total_build_times);
- return 1;
- }
- double
- circuit_build_times_timeout_rate(const circuit_build_times_t *cbt)
- {
- int i=0,timeouts=0;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] >= cbt->timeout_ms) {
- timeouts++;
- }
- }
- if (!cbt->total_build_times)
- return 0;
- return ((double)timeouts)/cbt->total_build_times;
- }
- double
- circuit_build_times_close_rate(const circuit_build_times_t *cbt)
- {
- int i=0,closed=0;
- for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
- if (cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED) {
- closed++;
- }
- }
- if (!cbt->total_build_times)
- return 0;
- return ((double)closed)/cbt->total_build_times;
- }
- int
- circuit_build_times_count_close(circuit_build_times_t *cbt,
- int did_onehop,
- time_t start_time)
- {
- if (circuit_build_times_disabled()) {
- cbt->close_ms = cbt->timeout_ms
- = circuit_build_times_get_initial_timeout();
- return 0;
- }
-
- circuit_build_times_network_close(cbt, did_onehop, start_time);
-
- if (!circuit_build_times_network_check_live(cbt)) {
- return 0;
- }
- circuit_build_times_add_time(cbt, CBT_BUILD_ABANDONED);
- return 1;
- }
- void
- circuit_build_times_count_timeout(circuit_build_times_t *cbt,
- int did_onehop)
- {
- if (circuit_build_times_disabled()) {
- cbt->close_ms = cbt->timeout_ms
- = circuit_build_times_get_initial_timeout();
- return;
- }
-
- circuit_build_times_network_timeout(cbt, did_onehop);
-
- circuit_build_times_network_check_changed(cbt);
- }
- static int
- circuit_build_times_set_timeout_worker(circuit_build_times_t *cbt)
- {
- build_time_t max_time;
- if (!circuit_build_times_enough_to_compute(cbt))
- return 0;
- if (!circuit_build_times_update_alpha(cbt))
- return 0;
- cbt->timeout_ms = circuit_build_times_calculate_timeout(cbt,
- circuit_build_times_quantile_cutoff());
- cbt->close_ms = circuit_build_times_calculate_timeout(cbt,
- circuit_build_times_close_quantile());
- max_time = circuit_build_times_max(cbt);
- if (cbt->timeout_ms > max_time) {
- log_info(LD_CIRC,
- "Circuit build timeout of %dms is beyond the maximum build "
- "time we have ever observed. Capping it to %dms.",
- (int)cbt->timeout_ms, max_time);
- cbt->timeout_ms = max_time;
- }
- if (max_time < INT32_MAX/2 && cbt->close_ms > 2*max_time) {
- log_info(LD_CIRC,
- "Circuit build measurement period of %dms is more than twice "
- "the maximum build time we have ever observed. Capping it to "
- "%dms.", (int)cbt->close_ms, 2*max_time);
- cbt->close_ms = 2*max_time;
- }
-
- cbt->close_ms = MAX(cbt->close_ms, circuit_build_times_initial_timeout());
- cbt->have_computed_timeout = 1;
- return 1;
- }
- void
- circuit_build_times_set_timeout(circuit_build_times_t *cbt)
- {
- long prev_timeout = tor_lround(cbt->timeout_ms/1000);
- double timeout_rate;
-
- if (circuit_build_times_disabled())
- return;
- if (!circuit_build_times_set_timeout_worker(cbt))
- return;
- if (cbt->timeout_ms < circuit_build_times_min_timeout()) {
- log_info(LD_CIRC, "Set buildtimeout to low value %fms. Setting to %dms",
- cbt->timeout_ms, circuit_build_times_min_timeout());
- cbt->timeout_ms = circuit_build_times_min_timeout();
- if (cbt->close_ms < cbt->timeout_ms) {
-
- cbt->close_ms = circuit_build_times_initial_timeout();
- }
- }
- control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_COMPUTED);
- timeout_rate = circuit_build_times_timeout_rate(cbt);
- if (prev_timeout > tor_lround(cbt->timeout_ms/1000)) {
- log_info(LD_CIRC,
- "Based on %d circuit times, it looks like we don't need to "
- "wait so long for circuits to finish. We will now assume a "
- "circuit is too slow to use after waiting %ld seconds.",
- cbt->total_build_times,
- tor_lround(cbt->timeout_ms/1000));
- log_info(LD_CIRC,
- "Circuit timeout data: %fms, %fms, Xm: %d, a: %f, r: %f",
- cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
- timeout_rate);
- } else if (prev_timeout < tor_lround(cbt->timeout_ms/1000)) {
- log_info(LD_CIRC,
- "Based on %d circuit times, it looks like we need to wait "
- "longer for circuits to finish. We will now assume a "
- "circuit is too slow to use after waiting %ld seconds.",
- cbt->total_build_times,
- tor_lround(cbt->timeout_ms/1000));
- log_info(LD_CIRC,
- "Circuit timeout data: %fms, %fms, Xm: %d, a: %f, r: %f",
- cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
- timeout_rate);
- } else {
- log_info(LD_CIRC,
- "Set circuit build timeout to %lds (%fms, %fms, Xm: %d, a: %f,"
- " r: %f) based on %d circuit times",
- tor_lround(cbt->timeout_ms/1000),
- cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha, timeout_rate,
- cbt->total_build_times);
- }
- }
- void
- circuitbuild_running_unit_tests(void)
- {
- unit_tests = 1;
- }
|