-
Notifications
You must be signed in to change notification settings - Fork 26
icmp rate limiter #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
icmp rate limiter #607
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2026 Farid Mihoub | ||
|
|
||
| #include "icmp_rl.h" | ||
|
|
||
| #include <gr_clock.h> | ||
|
|
||
| #include <stdint.h> | ||
| #include <time.h> | ||
|
|
||
| static struct time_space icmp_rl = {0}; | ||
|
|
||
| void icmp_rl_init(uint32_t interval_ms) { | ||
| icmp_rl.interval_ms = interval_ms; | ||
| icmp_rl.last_ts = gr_clock_us(); | ||
| } | ||
|
|
||
| bool icmp_rl_allow() { | ||
| uint64_t now = gr_clock_us(); | ||
|
|
||
| if ((now - icmp_rl.last_ts) < (icmp_rl.interval_ms * 1000)) | ||
| return false; | ||
|
|
||
| icmp_rl.last_ts = now; | ||
| return true; | ||
|
Comment on lines
+13
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't arm the limiter with the current timestamp.
Possible fix void icmp_rl_init(uint32_t interval_ms) {
+ uint64_t interval_us = (uint64_t)interval_ms * 1000ULL;
icmp_rl.interval_ms = interval_ms;
- icmp_rl.last_ts = gr_clock_us();
+ icmp_rl.last_ts = gr_clock_us() - interval_us;
}
bool icmp_rl_allow() {
uint64_t now = gr_clock_us();
+ uint64_t interval_us = (uint64_t)icmp_rl.interval_ms * 1000ULL;
- if ((now - icmp_rl.last_ts) < (icmp_rl.interval_ms * 1000))
+ if ((now - icmp_rl.last_ts) < interval_us)
return false;🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // Copyright (c) 2026 Farid Mihoub | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <time.h> | ||
|
|
||
| struct time_space { | ||
| uint64_t last_ts; | ||
| uint32_t interval_ms; | ||
| }; | ||
|
|
||
| void icmp_rl_init(uint32_t interval_ms); | ||
| bool icmp_rl_allow(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,6 @@ src += files( | |
| 'icmp.c', | ||
| 'nexthop.c', | ||
| 'route.c', | ||
| 'icmp_rl.c', | ||
| ) | ||
| inc += include_directories('.') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Serialize the shared limiter state.
icmp_rl_allow()andicmp_rl_init()both read/write the same globalicmp_rlobject, but this code is exercised from the ICMP input node, which can run on multiple workers. As written, concurrent packets can race through the check/update and both be accepted, so the rate limit is not enforced reliably.🤖 Prompt for AI Agents