File: tbf.c

package info (click to toggle)
bird2 2.0.12-5~bpo11%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye-backports
  • size: 5,652 kB
  • sloc: ansic: 67,582; perl: 3,431; sh: 3,265; lex: 887; xml: 494; makefile: 404; python: 123; sed: 13
file content (37 lines) | stat: -rw-r--r-- 632 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 *	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"
#include "lib/timer.h"

int
tbf_limit(struct tbf *f)
{
  btime delta = current_time() - f->timestamp;

  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 (f->count < 1000000)
  {
    f->drop++;
    return 1;
  }
  else
  {
    f->count -= 1000000;
    f->drop = 0;
    return 0;
  }
}