summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorOndrej Zajicek (work) <santiago@crfreenet.org>2017-11-28 17:06:10 +0100
committerOndrej Zajicek (work) <santiago@crfreenet.org>2017-12-07 13:53:42 +0100
commit574b2324275d3292e98a8e329f791eb5c799f7f2 (patch)
tree37f5bcc1edf1bcdcd61380c76fe214afce523bcd /lib
parent3b3b0910ffb1b212b1c9ea420db6c575a3ecb71a (diff)
Timers: Fix TBF and some last remains
Diffstat (limited to 'lib')
-rw-r--r--lib/birdlib.h30
-rw-r--r--lib/tbf.c29
-rw-r--r--lib/timer.h4
3 files changed, 28 insertions, 35 deletions
diff --git a/lib/birdlib.h b/lib/birdlib.h
index 9a77fc7b..f41cceb5 100644
--- a/lib/birdlib.h
+++ b/lib/birdlib.h
@@ -69,7 +69,7 @@ static inline int u64_cmp(u64 i1, u64 i2)
/* Microsecond time */
typedef s64 btime;
-typedef s64 bird_clock_t;
+//typedef s64 bird_clock_t;
#define S_ * (btime) 1000000
#define MS_ * (btime) 1000
@@ -85,37 +85,23 @@ typedef s64 bird_clock_t;
#define NS /1000
#endif
+#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
+
/* Rate limiting */
struct tbf {
- bird_clock_t timestamp; /* Last update */
- u16 count; /* Available tokens */
+ btime timestamp; /* Last update */
+ u64 count; /* Available micro-tokens */
u16 burst; /* Max number of tokens */
- u16 rate; /* Rate of replenishment */
- u16 mark; /* Whether last op was limited */
+ u16 rate; /* Rate of replenishment (tokens / sec) */
+ u32 drop; /* Number of failed request since last successful */
};
/* Default TBF values for rate limiting log messages */
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
-void tbf_update(struct tbf *f);
-
-static inline int
-tbf_limit(struct tbf *f)
-{
- tbf_update(f);
-
- if (!f->count)
- {
- f->mark = 1;
- return 1;
- }
-
- f->count--;
- f->mark = 0;
- return 0;
-}
+int tbf_limit(struct tbf *f);
/* Logging and dying */
diff --git a/lib/tbf.c b/lib/tbf.c
index c1dafee8..e6e84b4f 100644
--- a/lib/tbf.c
+++ b/lib/tbf.c
@@ -10,21 +10,28 @@
#include "nest/bird.h"
#include "lib/timer.h"
-void
-tbf_update(struct tbf *f)
+int
+tbf_limit(struct tbf *f)
{
- bird_clock_t delta = now - f->timestamp;
+ btime delta = current_time() - f->timestamp;
- if (delta == 0)
- return;
-
- f->timestamp = now;
+ if (delta > 0)
+ {
+ u64 next = f->count + delta * f->rate;
+ u64 burst = (u64) f->burst << 20;
+ f->count = MIN(next, burst);
+ f->timestamp += delta;
+ }
- if ((0 < delta) && (delta < f->burst))
+ if (f->count < 1000000)
{
- u32 next = f->count + delta * f->rate;
- f->count = MIN(next, f->burst);
+ f->drop++;
+ return 1;
}
else
- f->count = f->burst;
+ {
+ f->count -= 1000000;
+ f->drop = 0;
+ return 0;
+ }
}
diff --git a/lib/timer.h b/lib/timer.h
index 61a2aa94..250bb3cd 100644
--- a/lib/timer.h
+++ b/lib/timer.h
@@ -46,8 +46,8 @@ extern struct timeloop main_timeloop;
btime current_time(void);
btime current_real_time(void);
-#define now (current_time() TO_S)
-#define now_real (current_real_time() TO_S)
+//#define now (current_time() TO_S)
+//#define now_real (current_real_time() TO_S)
extern btime boot_time;
timer2 *tm2_new(pool *p);