Commit 1123e707 authored by Ondrej Zajicek's avatar Ondrej Zajicek
Browse files

Implements token bucket filter for rate limiting.

parent dcde7ae5
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -493,7 +493,7 @@ f_rta_cow(void)
  }
}

static struct rate_limit rl_runtime_err;
static struct tbf rl_runtime_err = TBF_DEFAULT_LOG_LIMITS;

#define runtime(x) do { \
    log_rl(&rl_runtime_err, L_ERR "filters, line %d: %s", what->lineno, x); \
@@ -1492,7 +1492,7 @@ f_run(struct filter *filter, struct rte **rte, struct ea_list **tmp_attrs, struc


  if (res.type != T_RETURN) {
    log( L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name); 
    log_rl(&rl_runtime_err, L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name);
    return F_ERROR;
  }
  DBG( "done (%u)\n", res.val.i );
+1 −1
Original line number Diff line number Diff line
H Library functions
S ip.c ipv4.c ipv6.c
S lists.c
S checksum.c bitops.c patmatch.c printf.c xmalloc.c
S checksum.c bitops.c patmatch.c printf.c xmalloc.c tbf.c
D resource.sgml
S resource.c
S mempool.c
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ resource.c
resource.h
slab.c
socket.h
tbf.c
unaligned.h
xmalloc.c
printf.c
+33 −7
Original line number Diff line number Diff line
@@ -68,6 +68,38 @@ typedef s64 btime;
#endif


/* Rate limiting */

struct tbf {
  bird_clock_t timestamp;		/* Last update */
  u16 count;				/* Available tokens */
  u16 burst;				/* Max number of tokens */
  u16 rate;				/* Rate of replenishment */
  u16 mark;				/* Whether last op was limited */
};

/* Default TBF values for rate limiting log messages */
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }

void tbf_update(struct tbf *f);

static inline int
tbf_limit(struct tbf *f)
{
  tbf_update(f);

  if (!f->count)
  {
    f->mark = 1;
    return 1;
  }

  f->count--;
  f->mark = 0;
  return 0;
}


/* Logging and dying */

typedef struct buffer {
@@ -88,16 +120,10 @@ typedef struct buffer {

#define LOG_BUFFER_SIZE 1024


struct rate_limit {
  bird_clock_t timestamp;
  int count;
};

#define log log_msg
void log_commit(int class, buffer *buf);
void log_msg(char *msg, ...);
void log_rl(struct rate_limit *rl, char *msg, ...);
void log_rl(struct tbf *rl, char *msg, ...);
void die(char *msg, ...) NORET;
void bug(char *msg, ...) NORET;

lib/tbf.c

0 → 100644
+29 −0
Original line number Diff line number Diff line
/*
 *	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;
}
Loading