]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Timers: Fix TBF and some last remains
authorOndrej Zajicek (work) <santiago@crfreenet.org>
Tue, 28 Nov 2017 16:06:10 +0000 (17:06 +0100)
committerOndrej Zajicek (work) <santiago@crfreenet.org>
Thu, 7 Dec 2017 12:53:42 +0000 (13:53 +0100)
conf/conf.c
lib/birdlib.h
lib/tbf.c
lib/timer.h
nest/proto.c
proto/ospf/ospf.h
proto/rip/packets.c
sysdep/linux/netlink.c
sysdep/unix/io.c
sysdep/unix/log.c
sysdep/unix/timer.h

index 2b5e9c7169ad02bc6983afd1f331555824a4920c..c68ded7faf1dd90cc4f8130e0ecc435de1d79426 100644 (file)
@@ -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;
 }
 
index 9a77fc7bc691bf0cb5893cd3f859172f3ea14412..f41cceb532bef11ab4fc489ec38cc08ede7515e5 100644 (file)
@@ -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 */
index c1dafee836d6a41283c1b5561577a042161d77fe..e6e84b4f9b057fc8c109ca8290a9ab4cfd161e2f 100644 (file)
--- a/lib/tbf.c
+++ b/lib/tbf.c
 #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;
+  }
 }
index 61a2aa94b7575eea329f09d726482dc5eb48aef4..250bb3cd750013cc194a9c5d5474290e87b0ef9f 100644 (file)
@@ -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);
index 27c8fdedea208800825cd766c04d4195a8671123..72f1f94d36fc1d875d737613b72c083c08b6cbdc 100644 (file)
@@ -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;
 }
 
index dbc231b6a984fb6f0ea7d2af2a21b6cb0d070a8c..d4571bf64f1c5d07dafb88376d370bccdc12783c 100644 (file)
@@ -58,7 +58,7 @@
   log_rl(&p->log_lsa_tbf, L_REMOTE "%s: " msg, p->p.name, args)
 
 #define LOG_LSA2(msg, args...) \
-  do { if (! p->log_lsa_tbf.mark) \
+  do { if (! p->log_lsa_tbf.drop) \
     log(L_REMOTE "%s: " msg, p->p.name, args); } while(0)
 
 
index 1518dd3f564f53562f40b46a5cbc182aacf2ca1e..4925ca36be8702fde252e8ec9b18ce2ae4b8fef6 100644 (file)
@@ -189,7 +189,10 @@ rip_update_csn(struct rip_proto *p UNUSED, struct rip_iface *ifa)
    * have the same CSN. We are using real time, but enforcing monotonicity.
    */
   if (ifa->cf->auth_type == RIP_AUTH_CRYPTO)
-    ifa->csn = (ifa->csn < (u32) now_real) ? (u32) now_real : ifa->csn + 1;
+  {
+    u32 now_real = (u32) (current_real_time() TO_S);
+    ifa->csn = (ifa->csn < now_real) ? now_real : ifa->csn + 1;
+  }
 }
 
 static void
index c9d5cdec5314e4e2155ede631afcd68d3b886750..6477d18c21c495a6abe5ca2dca03172552cdec78 100644 (file)
@@ -151,7 +151,7 @@ nl_open_sock(struct nl_sock *nl)
       nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
       if (nl->fd < 0)
        die("Unable to open rtnetlink socket: %m");
-      nl->seq = now;
+      nl->seq = (u32) (current_time() TO_S); /* Or perhaps random_u32() ? */
       nl->rx_buffer = xmalloc(NL_RX_SIZE);
       nl->last_hdr = NULL;
       nl->last_size = 0;
index 3e56a32d2192f026b36246242c8eeb1c2eb359ae..a196bbe29e23e3e92d43d8f25a3b88d0288e893d 100644 (file)
@@ -2132,7 +2132,7 @@ io_init(void)
   // XXX init_times();
   // XXX update_times();
   boot_time = current_time();
-  srandom((int) now_real);
+  srandom((uint) (current_real_time() TO_S));
 }
 
 static int short_loops = 0;
index e564f8f298eae8faedb0607ec24074b4da2b6be1..f9dccc390279fa7a9d0f494e692f7d40d7d6a9a1 100644 (file)
@@ -180,19 +180,18 @@ log_msg(const char *msg, ...)
 void
 log_rl(struct tbf *f, const char *msg, ...)
 {
-  int last_hit = f->mark;
   int class = 1;
   va_list args;
 
   /* Rate limiting is a bit tricky here as it also logs '...' during the first hit */
-  if (tbf_limit(f) && last_hit)
+  if (tbf_limit(f) && (f->drop > 1))
     return;
 
   if (*msg >= 1 && *msg <= 8)
     class = *msg++;
 
   va_start(args, msg);
-  vlog(class, (f->mark ? "..." : msg), args);
+  vlog(class, (f->drop ? "..." : msg), args);
   va_end(args);
 }
 
@@ -332,7 +331,7 @@ void
 mrt_dump_message(struct proto *p, u16 type, u16 subtype, byte *buf, u32 len)
 {
   /* Prepare header */
-  put_u32(buf+0, now_real);
+  put_u32(buf+0, current_real_time() TO_S);
   put_u16(buf+4, type);
   put_u16(buf+6, subtype);
   put_u32(buf+8, len - MRTDUMP_HDR_LENGTH);
index 495d10c71e68adb88ec748c31fd0d56d13fe09d8..989574bfe37f00c1de844eea05153f8729c0ada2 100644 (file)
@@ -16,7 +16,7 @@
 
 
 typedef struct timer2 timer;
-
+#if 0
 static inline timer *tm_new(pool *p)
 { return (void *) tm2_new(p); }
 
@@ -44,8 +44,8 @@ static inline void tm_start_max(timer *t, bird_clock_t after)
 static inline timer * tm_new_set(pool *p, void (*hook)(timer *), void *data, uint rand, uint rec)
 { return tm2_new_init(p, hook, data, rec S_, rand S_); }
 
+#endif
 
-#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
 
 
 #endif