]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Backport settle timer from v3
authorIgor Putovny <igor.putovny@nic.cz>
Thu, 16 May 2024 11:50:08 +0000 (13:50 +0200)
committerIgor Putovny <igor.putovny@nic.cz>
Thu, 16 May 2024 11:50:08 +0000 (13:50 +0200)
conf/confbase.Y
lib/birdlib.h
lib/settle.h [new file with mode: 0644]

index 1d5738ff06d1f917cc79c93cf73cec5fe2e4c45a..e81ac2843331cd632d70eab49da36375925bd66a 100644 (file)
@@ -22,6 +22,7 @@ CF_HDR
 #include "nest/bfd.h"
 #include "nest/cli.h"
 #include "filter/filter.h"
+#include "lib/settle.h"
 
 /* FIXME: Turn on YYERROR_VERBOSE and work around lots of bison bugs? */
 
@@ -92,6 +93,7 @@ CF_DECLS
   struct proto_spec ps;
   struct channel_limit cl;
   struct timeformat *tf;
+  struct settle_config settle;
   mpls_label_stack *mls;
   struct bytestring *bs;
 }
@@ -110,6 +112,7 @@ CF_DECLS
 
 %type <i> expr bool pxlen4
 %type <time> expr_us time
+%type <settle> settle
 %type <a> ipa
 %type <net> net_ip4_ net_ip4 net_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
 %type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_ net_ip6_sadr_ net_mpls_
index 81d4908acd9d1bf188622b505298dc33f780597c..c1090417a34dc1df9f42d6b5386a83997d4aa958 100644 (file)
@@ -13,6 +13,9 @@
 
 /* Ugly structure offset handling macros */
 
+#define SAME_TYPE(a, b)        ({ int _ = ((a) != (b)); !_; })
+#define TYPE_CAST(from, to, what) ( SAME_TYPE(((from) NULL), (what)), ((to) (what)))
+
 struct align_probe { char x; long int y; };
 
 #define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
diff --git a/lib/settle.h b/lib/settle.h
new file mode 100644 (file)
index 0000000..4c92c3c
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ *     BIRD -- Settle timer
+ *
+ *     (c) 2022 Maria Matejka <mq@jmq.cz>
+ *     (c) 2022 CZ.NIC z.s.p.o.
+ *
+ *     Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_SETTLE_H_
+#define _BIRD_SETTLE_H_
+
+#include "lib/birdlib.h"
+#include "lib/timer.h"
+
+struct settle_config {
+  btime min, max;
+};
+
+struct settle {
+  union {
+    /* Timer hook polymorphism. */
+    struct {
+      resource _r;
+      void (*hook)(struct settle *);
+    };
+    timer tm;
+  };
+  struct settle_config cf;
+  btime started;
+};
+
+STATIC_ASSERT(OFFSETOF(struct settle, hook) == OFFSETOF(struct settle, tm) + OFFSETOF(timer, hook));
+
+#define SETTLE_INIT(_cfp, _hook, _data) (struct settle) { .tm = { .data = (_data), .hook = TYPE_CAST(void (*)(struct settle *), void (*)(struct timer *), (_hook)), }, .cf = ({ASSERT_DIE((_cfp)->min <= (_cfp)->max); *(_cfp); }), }
+
+
+static inline void settle_init(struct settle *s, struct settle_config *cf, void (*hook)(struct settle *), void *data)
+{
+  *s = SETTLE_INIT(cf, hook, data);
+}
+
+#define settle_active(s) tm_active(&(s)->tm)
+
+static inline void settle_kick(struct settle *s)
+{
+  if (!tm_active(&s->tm))
+  {
+    s->started = current_time();
+    tm_set(&s->tm, s->started + s->cf.min);
+  }
+  else
+  {
+    btime now = current_time();
+    tm_set(&s->tm, MIN_(now + s->cf.min, s->started + s->cf.max));
+  }
+}
+
+static inline void settle_cancel(struct settle *s)
+{
+  tm_stop(&s->tm);
+}
+
+#endif