From 695b0bd1d5aca52a05df1a697a6b23a20be529d4 Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Sun, 12 Jun 2016 19:07:11 +0000 Subject: Implement DL_SCHED_RANDOM_EXPONENTIAL support for download_status_t --- src/or/directory.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 9 deletions(-) (limited to 'src/or/directory.c') diff --git a/src/or/directory.c b/src/or/directory.c index 6caca11737..5b890cab1e 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -3762,6 +3762,28 @@ find_dl_schedule(download_status_t *dls, const or_options_t *options) return NULL; } +/** Decide which minimum and maximum delay step we want to use based on + * descriptor type in dls and options. + * Helper function for download_status_schedule_get_delay(). */ +STATIC void +find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, + int *min, int *max) +{ + tor_assert(dls); + tor_assert(options); + tor_assert(min); + tor_assert(max); + + /* + * For now, just use the existing schedule config stuff and pick the + * first/last entries off to get min/max delay for backoff purposes + */ + const smartlist_t *schedule = find_dl_schedule(dls, options); + tor_assert(schedule != NULL && smartlist_len(schedule) >= 2); + *min = *((int *)(smartlist_get(schedule, 0))); + *max = *((int *)((smartlist_get(schedule, smartlist_len(schedule) - 1)))); +} + /* Find the current delay for dls based on schedule. * Set dls->next_attempt_at based on now, and return the delay. * Helper for download_status_increment_failure and @@ -3769,23 +3791,85 @@ find_dl_schedule(download_status_t *dls, const or_options_t *options) STATIC int download_status_schedule_get_delay(download_status_t *dls, const smartlist_t *schedule, + int min_delay, int max_delay, time_t now) { tor_assert(dls); - tor_assert(schedule); + /* We don't need a schedule if we're using random exponential backoff */ + tor_assert(dls->backoff == DL_SCHED_RANDOM_EXPONENTIAL || + schedule != NULL); + /* If we're using random exponential backoff, we do need min/max delay */ + tor_assert(dls->backoff != DL_SCHED_RANDOM_EXPONENTIAL || + (min_delay >= 0 && max_delay >= min_delay && + max_delay <= INT_MAX)); int delay = INT_MAX; + int delay_increment, i; uint8_t dls_schedule_position = (dls->increment_on == DL_SCHED_INCREMENT_ATTEMPT ? dls->n_download_attempts : dls->n_download_failures); + uint8_t entropy; - if (dls_schedule_position < smartlist_len(schedule)) - delay = *(int *)smartlist_get(schedule, dls_schedule_position); - else if (dls_schedule_position == IMPOSSIBLE_TO_DOWNLOAD) - delay = INT_MAX; - else - delay = *(int *)smartlist_get(schedule, smartlist_len(schedule) - 1); + if (dls->backoff == DL_SCHED_DETERMINISTIC) { + if (dls_schedule_position < smartlist_len(schedule)) + delay = *(int *)smartlist_get(schedule, dls_schedule_position); + else if (dls_schedule_position == IMPOSSIBLE_TO_DOWNLOAD) + delay = INT_MAX; + else + delay = *(int *)smartlist_get(schedule, smartlist_len(schedule) - 1); + } else if (dls->backoff == DL_SCHED_RANDOM_EXPONENTIAL) { + /* Check if we missed a reset somehow */ + if (dls->last_backoff_position > dls_schedule_position) { + dls->last_backoff_position = 0; + dls->last_delay_used = 0; + } + + if (dls_schedule_position > 0) { + delay = dls->last_delay_used; + + while (dls->last_backoff_position < dls_schedule_position) { + /* + * Backoff step: we want to multiply by something ~1.5, and then add + * 1 with non-zero probability so we can't get stuck at zero even if + * we start out with zero delay. To do this, pick a uint8_t of + * entropy in the range [0,255], and use it to construct an + * increment. + */ + delay_increment = 0; + /* Get a byte of entropy */ + crypto_rand((char *)(&entropy), sizeof(entropy)); + /* Clamp it just to be sure */ + entropy &= 0xff; + /* If we have non-zero delay; otherwise this is a no-op */ + if (delay > 0) { + /* Use the low 7 bits for the increment */ + for (i = 0; i < 7; ++i) { + if (entropy & (0x1 << i)) delay_increment += (delay >> (i + 1)); + } + } + /* + * Using the remaining bit of entropy, add 1 with probability 50% so + * we can't get stuck at 0 + */ + if (entropy & 0x80) delay_increment += 1; + /* Increment delay, make sure to saturate if we would wrap around */ + if (delay_increment < max_delay - delay) delay += delay_increment; + else delay = max_delay; + ++(dls->last_backoff_position); + } + } else { + delay = min_delay; + } + + /* Clamp it within min/max if we have them */ + if (min_delay >= 0 && delay < min_delay) delay = min_delay; + if (max_delay != INT_MAX && delay > max_delay) delay = max_delay; + + /* Store it for next time */ + dls->last_backoff_position = dls_schedule_position; + dls->last_delay_used = delay; + } /* A negative delay makes no sense. Knowing that delay is * non-negative allows us to safely do the wrapping check below. */ @@ -3846,6 +3930,8 @@ download_status_increment_failure(download_status_t *dls, int status_code, const char *item, int server, time_t now) { int increment = -1; + int min_delay = 0, max_delay = INT_MAX; + tor_assert(dls); /* only count the failure if it's permanent, or we're a server */ @@ -3866,7 +3952,9 @@ download_status_increment_failure(download_status_t *dls, int status_code, /* only return a failure retry time if this schedule increments on failures */ const smartlist_t *schedule = find_dl_schedule(dls, get_options()); - increment = download_status_schedule_get_delay(dls, schedule, now); + find_dl_min_and_max_delay(dls, get_options(), &min_delay, &max_delay); + increment = download_status_schedule_get_delay(dls, schedule, + min_delay, max_delay, now); } download_status_log_helper(item, !dls->increment_on, "failed", @@ -3895,6 +3983,8 @@ download_status_increment_attempt(download_status_t *dls, const char *item, time_t now) { int delay = -1; + int min_delay = 0, max_delay = INT_MAX; + tor_assert(dls); if (dls->increment_on == DL_SCHED_INCREMENT_FAILURE) { @@ -3909,7 +3999,9 @@ download_status_increment_attempt(download_status_t *dls, const char *item, ++dls->n_download_attempts; const smartlist_t *schedule = find_dl_schedule(dls, get_options()); - delay = download_status_schedule_get_delay(dls, schedule, now); + find_dl_min_and_max_delay(dls, get_options(), &min_delay, &max_delay); + delay = download_status_schedule_get_delay(dls, schedule, + min_delay, max_delay, now); download_status_log_helper(item, dls->increment_on, "attempted", "on failure", dls->n_download_attempts, @@ -3941,6 +4033,8 @@ download_status_reset(download_status_t *dls) dls->n_download_failures = 0; dls->n_download_attempts = 0; dls->next_attempt_at = time(NULL) + *(int *)smartlist_get(schedule, 0); + dls->last_backoff_position = 0; + dls->last_delay_used = 0; /* Don't reset dls->want_authority or dls->increment_on */ } -- cgit v1.2.3-54-g00ecf From 1dfbfd319e417c06c6e6d97d8c617522873ad43f Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Sat, 18 Jun 2016 17:11:32 +0000 Subject: Better comment for download_status_schedule_get_delay() per code review --- src/or/directory.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/or/directory.c') diff --git a/src/or/directory.c b/src/or/directory.c index 5b890cab1e..1a8fd2cb21 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -3784,8 +3784,11 @@ find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, *max = *((int *)((smartlist_get(schedule, smartlist_len(schedule) - 1)))); } -/* Find the current delay for dls based on schedule. - * Set dls->next_attempt_at based on now, and return the delay. +/** Find the current delay for dls based on schedule or min_delay/ + * max_delay if we're using exponential backoff. If dls->backoff is + * DL_SCHED_RANDOM_EXPONENTIAL, we must have 0 <= min_delay <= max_delay <= + * INT_MAX, but schedule may be set to NULL; otherwise schedule is required. + * This function sets dls->next_attempt_at based on now, and returns the delay. * Helper for download_status_increment_failure and * download_status_increment_attempt. */ STATIC int -- cgit v1.2.3-54-g00ecf From 1f1df4ab740b2d2c2a833a81553bb723512bdd97 Mon Sep 17 00:00:00 2001 From: Andrea Shepard Date: Sat, 18 Jun 2016 18:23:55 +0000 Subject: Move exponential-random backoff computation out of download_status_schedule_get_delay() into separate function, per code review --- src/or/directory.c | 79 ++++++++++++++++++++++++++++++++++-------------------- src/or/directory.h | 2 ++ 2 files changed, 52 insertions(+), 29 deletions(-) (limited to 'src/or/directory.c') diff --git a/src/or/directory.c b/src/or/directory.c index 1a8fd2cb21..02016887ea 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -3784,6 +3784,52 @@ find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, *max = *((int *)((smartlist_get(schedule, smartlist_len(schedule) - 1)))); } +/** Advance one delay step. The algorithm is to use the previous delay to + * compute an increment. Consuming one byte of entropy per step, we use 7 + * bits to construct an increment between 0 and (127/128)*delay by adding + * right-shifted copies of delay, controlled by each bit. Then, to prevent + * getting stuck at zero if we start from zero, we use one last bit to add + * 1 with probability 50%. Finally, we add the increment to the original + * delay, clamp the value <= max_delay, and return it. + */ +STATIC int +next_random_exponential_delay(int delay, int max_delay) +{ + int delay_increment, i; + uint8_t entropy; + + /* + * Backoff step: we want to multiply by something ~1.5, and then add + * 1 with non-zero probability so we can't get stuck at zero even if + * we start out with zero delay. To do this, pick a uint8_t of + * entropy in the range [0,255], and use it to construct an + * increment. + */ + delay_increment = 0; + /* Get a byte of entropy */ + crypto_rand((char *)(&entropy), sizeof(entropy)); + /* Clamp it just to be sure */ + entropy &= 0xff; + /* If we have non-zero delay; otherwise this is a no-op */ + if (delay > 0) { + /* Use the low 7 bits for the increment */ + for (i = 0; i < 7; ++i) { + if (entropy & (0x1 << i)) delay_increment += (delay >> (i + 1)); + } + } + /* + * Using the remaining bit of entropy, add 1 with probability 50% so + * we can't get stuck at 0 + */ + if (entropy & 0x80) delay_increment += 1; + /* Increment delay, make sure to saturate if we would wrap around */ + if (delay_increment < max_delay - delay) delay += delay_increment; + else delay = max_delay; + + /* Return the updated delay */ + return delay; +} + /** Find the current delay for dls based on schedule or min_delay/ * max_delay if we're using exponential backoff. If dls->backoff is * DL_SCHED_RANDOM_EXPONENTIAL, we must have 0 <= min_delay <= max_delay <= @@ -3807,12 +3853,10 @@ download_status_schedule_get_delay(download_status_t *dls, max_delay <= INT_MAX)); int delay = INT_MAX; - int delay_increment, i; uint8_t dls_schedule_position = (dls->increment_on == DL_SCHED_INCREMENT_ATTEMPT ? dls->n_download_attempts : dls->n_download_failures); - uint8_t entropy; if (dls->backoff == DL_SCHED_DETERMINISTIC) { if (dls_schedule_position < smartlist_len(schedule)) @@ -3832,36 +3876,13 @@ download_status_schedule_get_delay(download_status_t *dls, delay = dls->last_delay_used; while (dls->last_backoff_position < dls_schedule_position) { - /* - * Backoff step: we want to multiply by something ~1.5, and then add - * 1 with non-zero probability so we can't get stuck at zero even if - * we start out with zero delay. To do this, pick a uint8_t of - * entropy in the range [0,255], and use it to construct an - * increment. - */ - delay_increment = 0; - /* Get a byte of entropy */ - crypto_rand((char *)(&entropy), sizeof(entropy)); - /* Clamp it just to be sure */ - entropy &= 0xff; - /* If we have non-zero delay; otherwise this is a no-op */ - if (delay > 0) { - /* Use the low 7 bits for the increment */ - for (i = 0; i < 7; ++i) { - if (entropy & (0x1 << i)) delay_increment += (delay >> (i + 1)); - } - } - /* - * Using the remaining bit of entropy, add 1 with probability 50% so - * we can't get stuck at 0 - */ - if (entropy & 0x80) delay_increment += 1; - /* Increment delay, make sure to saturate if we would wrap around */ - if (delay_increment < max_delay - delay) delay += delay_increment; - else delay = max_delay; + /* Do one increment step */ + delay = next_random_exponential_delay(delay, max_delay); + /* Update our position */ ++(dls->last_backoff_position); } } else { + /* If we're just starting out, use the minimum delay */ delay = min_delay; } diff --git a/src/or/directory.h b/src/or/directory.h index d867aa38c4..afa3bcc611 100644 --- a/src/or/directory.h +++ b/src/or/directory.h @@ -158,6 +158,8 @@ STATIC const smartlist_t *find_dl_schedule(download_status_t *dls, STATIC void find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, int *min, int *max); +STATIC int next_random_exponential_delay(int delay, int max_delay); + #endif #endif -- cgit v1.2.3-54-g00ecf From a09ec22a9b1d213716ac1792752c266c3a92a1f6 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 20 Jun 2016 10:10:02 -0400 Subject: Simpler implementation of random exponential backoff. Consumes more entropy, but is easier to read. --- src/or/directory.c | 63 ++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 37 deletions(-) (limited to 'src/or/directory.c') diff --git a/src/or/directory.c b/src/or/directory.c index 02016887ea..c1b5ae7519 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -3785,49 +3785,38 @@ find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, } /** Advance one delay step. The algorithm is to use the previous delay to - * compute an increment. Consuming one byte of entropy per step, we use 7 - * bits to construct an increment between 0 and (127/128)*delay by adding - * right-shifted copies of delay, controlled by each bit. Then, to prevent - * getting stuck at zero if we start from zero, we use one last bit to add - * 1 with probability 50%. Finally, we add the increment to the original - * delay, clamp the value <= max_delay, and return it. + * compute an increment, we construct a value uniformly at random between + * delay and MAX(delay*2,delay+1). We then clamp that value to be no larger + * than max_delay, and return it. + * + * Requires that delay is less than INT_MAX, and delay is in [0,max_delay]. */ STATIC int next_random_exponential_delay(int delay, int max_delay) { - int delay_increment, i; - uint8_t entropy; + /* Check preconditions */ + if (BUG(delay > max_delay)) + delay = max_delay; + if (BUG(delay == INT_MAX)) + delay -= 1; /* prevent overflow */ + if (BUG(delay < 0)) + delay = 0; + + /* How much are we willing to add to the delay? */ + int max_increment; + + if (delay) + max_increment = delay; /* no more than double. */ + else + max_increment = 1; /* we're always willing to slow down a little. */ - /* - * Backoff step: we want to multiply by something ~1.5, and then add - * 1 with non-zero probability so we can't get stuck at zero even if - * we start out with zero delay. To do this, pick a uint8_t of - * entropy in the range [0,255], and use it to construct an - * increment. - */ - delay_increment = 0; - /* Get a byte of entropy */ - crypto_rand((char *)(&entropy), sizeof(entropy)); - /* Clamp it just to be sure */ - entropy &= 0xff; - /* If we have non-zero delay; otherwise this is a no-op */ - if (delay > 0) { - /* Use the low 7 bits for the increment */ - for (i = 0; i < 7; ++i) { - if (entropy & (0x1 << i)) delay_increment += (delay >> (i + 1)); - } - } - /* - * Using the remaining bit of entropy, add 1 with probability 50% so - * we can't get stuck at 0 - */ - if (entropy & 0x80) delay_increment += 1; - /* Increment delay, make sure to saturate if we would wrap around */ - if (delay_increment < max_delay - delay) delay += delay_increment; - else delay = max_delay; + /* the + 1 here is so that we include the end of the interval */ + int increment = crypto_rand_int(max_increment+1); - /* Return the updated delay */ - return delay; + if (increment < max_delay - delay) + return delay + increment; + else + return max_delay; } /** Find the current delay for dls based on schedule or min_delay/ -- cgit v1.2.3-54-g00ecf