tbf.c 632 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 *	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"
11
#include "lib/timer.h"
12

13 14
int
tbf_limit(struct tbf *f)
15
{
16
  btime delta = current_time() - f->timestamp;
17

18 19 20 21 22 23 24
  if (delta > 0)
  {
    u64 next = f->count + delta * f->rate;
    u64 burst = (u64) f->burst << 20;
    f->count = MIN(next, burst);
    f->timestamp += delta;
  }
25

26
  if (f->count < 1000000)
27
  {
28 29
    f->drop++;
    return 1;
30 31
  }
  else
32 33 34 35 36
  {
    f->count -= 1000000;
    f->drop = 0;
    return 0;
  }
37
}