summaryrefslogtreecommitdiff
path: root/lib/tbf.c
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2014-10-02 11:41:34 +0200
committerOndrej Zajicek <santiago@crfreenet.org>2014-10-02 12:52:50 +0200
commit1123e707400984108f48ac7c1be559f7ed8d9306 (patch)
treef303a7df3d685d3c7886fbf30cb43a4288341fde /lib/tbf.c
parentdcde7ae597ccb7d81648b9ecab7c0f61c88e60f2 (diff)
Implements token bucket filter for rate limiting.
Diffstat (limited to 'lib/tbf.c')
-rw-r--r--lib/tbf.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/tbf.c b/lib/tbf.c
new file mode 100644
index 00000000..39e18e57
--- /dev/null
+++ b/lib/tbf.c
@@ -0,0 +1,29 @@
+/*
+ * BIRD Library -- Token Bucket Filter
+ *
+ * (c) 2014 Ondrej Zajicek <santiago@crfreenet.org>
+ * (c) 2014 CZ.NIC z.s.p.o.
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#include "nest/bird.h"
+
+void
+tbf_update(struct tbf *f)
+{
+ bird_clock_t delta = now - f->timestamp;
+
+ if (delta == 0)
+ return;
+
+ f->timestamp = now;
+
+ if ((0 < delta) && (delta < f->burst))
+ {
+ u32 next = f->count + delta * f->rate;
+ f->count = MIN(next, f->burst);
+ }
+ else
+ f->count = f->burst;
+}