diff options
author | Lukáš Ondráček <lukas.ondracek@nic.cz> | 2024-10-24 18:37:00 +0200 |
---|---|---|
committer | Vladimír Čunát <vladimir.cunat@nic.cz> | 2024-11-04 14:39:07 +0100 |
commit | 960739fc892591043911cc38a55ddd35156ca730 (patch) | |
tree | 5a84b61d22294de14e4e162fc10c1234ec2e11c2 /daemon | |
parent | Revert "pytests: change log level from debug to notice" (diff) | |
download | knot-resolver-960739fc892591043911cc38a55ddd35156ca730.tar.xz knot-resolver-960739fc892591043911cc38a55ddd35156ca730.zip |
daemon/ratelimiting: use slip instead of tc_limit
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/lua/kres-gen-33.lua | 2 | ||||
-rw-r--r-- | daemon/ratelimiting.c | 20 | ||||
-rw-r--r-- | daemon/ratelimiting.h | 2 | ||||
-rw-r--r-- | daemon/ratelimiting.test/tests.inc.c | 2 |
4 files changed, 15 insertions, 11 deletions
diff --git a/daemon/lua/kres-gen-33.lua b/daemon/lua/kres-gen-33.lua index 26547de5..fcd3eb35 100644 --- a/daemon/lua/kres-gen-33.lua +++ b/daemon/lua/kres-gen-33.lua @@ -616,7 +616,7 @@ knot_pkt_t *worker_resolve_mk_pkt(const char *, uint16_t, uint16_t, const struct struct qr_task *worker_resolve_start(knot_pkt_t *, struct kr_qflags); int zi_zone_import(const zi_config_t); _Bool ratelimiting_request_begin(struct kr_request *); -int ratelimiting_init(const char *, size_t, uint32_t, uint32_t, int); +int ratelimiting_init(const char *, size_t, uint32_t, uint32_t, uint16_t); int defer_init(const char *, int); struct engine { char _stub[]; diff --git a/daemon/ratelimiting.c b/daemon/ratelimiting.c index dbb10668..8d34efbc 100644 --- a/daemon/ratelimiting.c +++ b/daemon/ratelimiting.c @@ -18,7 +18,7 @@ struct ratelimiting { size_t capacity; uint32_t instant_limit; uint32_t rate_limit; - uint16_t tc_limit; + uint16_t slip; bool using_avx2; kru_price_t v4_prices[V4_PREFIXES_CNT]; kru_price_t v6_prices[V6_PREFIXES_CNT]; @@ -35,7 +35,7 @@ static bool using_avx2(void) return result; } -int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_limit, uint32_t rate_limit, int tc_limit_perc) +int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_limit, uint32_t rate_limit, uint16_t slip) { size_t capacity_log = 0; @@ -47,7 +47,7 @@ int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_l .capacity = capacity, .instant_limit = instant_limit, .rate_limit = rate_limit, - .tc_limit = (tc_limit_perc == 100 ? -1 : ((uint32_t)tc_limit_perc << 16) / 100), + .slip = slip, .using_avx2 = using_avx2() }; @@ -56,7 +56,7 @@ int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_l sizeof(header.capacity) + sizeof(header.instant_limit) + sizeof(header.rate_limit) + - sizeof(header.tc_limit) + + sizeof(header.slip) + sizeof(header.using_avx2)); // no undefined padding inside int ret = mmapped_init(&ratelimiting_mmapped, mmap_file, size, &header, header_size); @@ -121,21 +121,25 @@ bool ratelimiting_request_begin(struct kr_request *req) if (ratelimiting) { _Alignas(16) uint8_t key[16] = {0, }; uint8_t limited_prefix; - uint16_t max_final_load = 0; if (req->qsource.addr->sa_family == AF_INET6) { struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)req->qsource.addr; memcpy(key, &ipv6->sin6_addr, 16); limited_prefix = KRU.limited_multi_prefix_or((struct kru *)ratelimiting->kru, kr_now(), - 1, key, V6_PREFIXES, ratelimiting->v6_prices, V6_PREFIXES_CNT, &max_final_load); + 1, key, V6_PREFIXES, ratelimiting->v6_prices, V6_PREFIXES_CNT, NULL); } else { struct sockaddr_in *ipv4 = (struct sockaddr_in *)req->qsource.addr; memcpy(key, &ipv4->sin_addr, 4); // TODO append port? limited_prefix = KRU.limited_multi_prefix_or((struct kru *)ratelimiting->kru, kr_now(), - 0, key, V4_PREFIXES, ratelimiting->v4_prices, V4_PREFIXES_CNT, &max_final_load); + 0, key, V4_PREFIXES, ratelimiting->v4_prices, V4_PREFIXES_CNT, NULL); + } + if (limited_prefix) { + limited = + (ratelimiting->slip > 1) ? + ((kr_rand_bytes(1) % ratelimiting->slip == 0) ? 1 : 2) : + ((ratelimiting->slip == 1) ? 1 : 2); } - limited = (limited_prefix ? 2 : (max_final_load > ratelimiting->tc_limit ? 1 : 0)); } if (!limited) return false; diff --git a/daemon/ratelimiting.h b/daemon/ratelimiting.h index 8322772d..def9d2f3 100644 --- a/daemon/ratelimiting.h +++ b/daemon/ratelimiting.h @@ -7,7 +7,7 @@ struct kr_request; * The existing data are used if another instance is already using the file * and it was initialized with the same parameters; it fails on mismatch. */ KR_EXPORT -int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_limit, uint32_t rate_limit, int tc_limit_perc); +int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_limit, uint32_t rate_limit, uint16_t slip); /** Do rate-limiting, during knot_layer_api::begin. */ KR_EXPORT diff --git a/daemon/ratelimiting.test/tests.inc.c b/daemon/ratelimiting.test/tests.inc.c index 467473c0..e2427b5f 100644 --- a/daemon/ratelimiting.test/tests.inc.c +++ b/daemon/ratelimiting.test/tests.inc.c @@ -95,7 +95,7 @@ static void test_rrl(void **state) { const char *tmpdir = test_tmpdir_create(); char mmap_file[64]; stpcpy(stpcpy(mmap_file, tmpdir), "/rrl"); - ratelimiting_init(mmap_file, RRL_TABLE_SIZE, RRL_INSTANT_LIMIT, RRL_RATE_LIMIT, 100); + ratelimiting_init(mmap_file, RRL_TABLE_SIZE, RRL_INSTANT_LIMIT, RRL_RATE_LIMIT, 0); if (KRU.initialize == KRU_GENERIC.initialize) { struct kru_generic *kru = (struct kru_generic *) ratelimiting->kru; |