Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / tbf.c @ 6b3f1a54

History | View | Annotate | Download (632 Bytes)

1
/*
2
 *        BIRD Library -- Token Bucket Filter
3
 *
4
 *        (c) 2014 Ondrej Zajicek <santiago@crfreenet.org>
5
 *        (c) 2014 CZ.NIC z.s.p.o.
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include "nest/bird.h"
11
#include "lib/timer.h"
12

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

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

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