Commit 574b2324 authored by Ondrej Zajicek (work)'s avatar Ondrej Zajicek (work)
Browse files

Timers: Fix TBF and some last remains

parent 3b3b0910
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -384,7 +384,7 @@ config_confirm(void)
  if (config_timer->expires == 0)
    return CONF_NOTHING;

  tm_stop(config_timer);
  tm2_stop(config_timer);

  return CONF_CONFIRM;
}
@@ -420,7 +420,7 @@ config_undo(void)
    return CONF_NOTHING;

  undo_available = 0;
  tm_stop(config_timer);
  tm2_stop(config_timer);

  if (configuring)
    {
@@ -468,7 +468,7 @@ config_init(void)
  config_event = ev_new(&root_pool);
  config_event->hook = config_done;

  config_timer = tm_new(&root_pool);
  config_timer = tm2_new(&root_pool);
  config_timer->hook = config_timeout;
}

+8 −22
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ static inline int u64_cmp(u64 i1, u64 i2)
/* Microsecond time */

typedef s64 btime;
typedef s64 bird_clock_t;
//typedef s64 bird_clock_t;

#define S_	* (btime) 1000000
#define MS_	* (btime) 1000
@@ -85,37 +85,23 @@ typedef s64 bird_clock_t;
#define NS	/1000
#endif

#define TIME_INFINITY ((s64) 0x7fffffffffffffff)


/* Rate limiting */

struct tbf {
  bird_clock_t timestamp;		/* Last update */
  u16 count;				/* Available tokens */
  btime timestamp;			/* Last update */
  u64 count;				/* Available micro-tokens */
  u16 burst;				/* Max number of tokens */
  u16 rate;				/* Rate of replenishment */
  u16 mark;				/* Whether last op was limited */
  u16 rate;				/* Rate of replenishment (tokens / sec) */
  u32 drop;				/* Number of failed request since last successful */
};

/* 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;
}
int tbf_limit(struct tbf *f);


/* Logging and dying */
+18 −11
Original line number Diff line number Diff line
@@ -10,21 +10,28 @@
#include "nest/bird.h"
#include "lib/timer.h"

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

  if (delta == 0)
    return;

  f->timestamp = now;
  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 ((0 < delta) && (delta < f->burst))
  if (f->count < 1000000)
  {
    u32 next = f->count + delta * f->rate;
    f->count = MIN(next, f->burst);
    f->drop++;
    return 1;
  }
  else
    f->count = f->burst;
  {
    f->count -= 1000000;
    f->drop = 0;
    return 0;
  }
}
+2 −2
Original line number Diff line number Diff line
@@ -46,8 +46,8 @@ extern struct timeloop main_timeloop;
btime current_time(void);
btime current_real_time(void);

#define now (current_time() TO_S)
#define now_real (current_real_time() TO_S)
//#define now (current_time() TO_S)
//#define now_real (current_real_time() TO_S)
extern btime boot_time;

timer2 *tm2_new(pool *p);
+1 −1
Original line number Diff line number Diff line
@@ -1288,7 +1288,7 @@ protos_build(void)
#endif

  proto_pool = rp_new(&root_pool, "Protocols");
  proto_shutdown_timer = tm_new(proto_pool);
  proto_shutdown_timer = tm2_new(proto_pool);
  proto_shutdown_timer->hook = proto_shutdown_loop;
}

Loading