2014-10-02 09:41:34 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2017-06-01 10:33:20 +00:00
|
|
|
#include "lib/timer.h"
|
2014-10-02 09:41:34 +00:00
|
|
|
|
2017-11-28 16:06:10 +00:00
|
|
|
int
|
|
|
|
tbf_limit(struct tbf *f)
|
2014-10-02 09:41:34 +00:00
|
|
|
{
|
2017-11-28 16:06:10 +00:00
|
|
|
btime delta = current_time() - f->timestamp;
|
2014-10-02 09:41:34 +00:00
|
|
|
|
2017-11-28 16:06:10 +00:00
|
|
|
if (delta > 0)
|
|
|
|
{
|
2024-05-07 10:09:52 +00:00
|
|
|
u64 next = f->count + delta * f->cf.rate;
|
|
|
|
u64 burst = (u64) f->cf.burst << 20;
|
2017-11-28 16:06:10 +00:00
|
|
|
f->count = MIN(next, burst);
|
|
|
|
f->timestamp += delta;
|
|
|
|
}
|
2014-10-02 09:41:34 +00:00
|
|
|
|
2017-11-28 16:06:10 +00:00
|
|
|
if (f->count < 1000000)
|
2014-10-02 09:41:34 +00:00
|
|
|
{
|
2017-11-28 16:06:10 +00:00
|
|
|
f->drop++;
|
|
|
|
return 1;
|
2014-10-02 09:41:34 +00:00
|
|
|
}
|
|
|
|
else
|
2017-11-28 16:06:10 +00:00
|
|
|
{
|
|
|
|
f->count -= 1000000;
|
|
|
|
f->drop = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-02 09:41:34 +00:00
|
|
|
}
|