aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2017-07-07 18:34:46 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2017-07-20 03:37:39 +0200
commit5c9a354c8f770761e26d2df0cb10e02ee21ab0b4 (patch)
treee8b74ab029fac25aa2dc7a5ea4cd4eaa25783df8 /src
parentcompat: work around odd kernels that backport kvfree (diff)
downloadwireguard-monolithic-historical-5c9a354c8f770761e26d2df0cb10e02ee21ab0b4.tar.xz
wireguard-monolithic-historical-5c9a354c8f770761e26d2df0cb10e02ee21ab0b4.zip
selftests: ensure that there isnt CPU lag when testing rate limiter
Diffstat (limited to 'src')
-rw-r--r--src/selftest/ratelimiter.h26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/selftest/ratelimiter.h b/src/selftest/ratelimiter.h
index 5b68014..f0bed8f 100644
--- a/src/selftest/ratelimiter.h
+++ b/src/selftest/ratelimiter.h
@@ -2,6 +2,8 @@
#ifdef DEBUG
+#include <linux/jiffies.h>
+
static const struct { bool result; unsigned int msec_to_sleep_before; } expected_results[] __initconst = {
[0 ... PACKETS_BURSTABLE - 1] = { true, 0 },
[PACKETS_BURSTABLE] = { false, 0 },
@@ -12,6 +14,16 @@ static const struct { bool result; unsigned int msec_to_sleep_before; } expected
[PACKETS_BURSTABLE + 5] = { false, 0 }
};
+static unsigned int maximum_jiffies_before_index(int index)
+{
+ unsigned int total_msecs = 2 * MSEC_PER_SEC / PACKETS_PER_SECOND / 3;
+ int i;
+
+ for (i = 0; i < index; ++i)
+ total_msecs += expected_results[i].msec_to_sleep_before;
+ return msecs_to_jiffies(total_msecs);
+}
+
bool __init ratelimiter_selftest(void)
{
struct sk_buff *skb4;
@@ -20,7 +32,8 @@ bool __init ratelimiter_selftest(void)
struct sk_buff *skb6;
struct ipv6hdr *hdr6;
#endif
- int i = -1, ret = false;
+ int i = -1, tries = 0, ret = false;
+ unsigned long loop_start_time;
BUILD_BUG_ON(MSEC_PER_SEC % PACKETS_PER_SECOND != 0);
@@ -57,7 +70,18 @@ bool __init ratelimiter_selftest(void)
skb_reset_network_header(skb6);
#endif
+restart:
+ loop_start_time = jiffies;
for (i = 0; i < ARRAY_SIZE(expected_results); ++i) {
+ if (time_is_before_jiffies(loop_start_time + maximum_jiffies_before_index(i))) {
+ if (++tries >= 1000)
+ goto err;
+ gc_entries(NULL);
+ rcu_barrier();
+ msleep(300);
+ goto restart;
+ }
+
if (expected_results[i].msec_to_sleep_before)
msleep(expected_results[i].msec_to_sleep_before);