0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-08 12:18:42 +00:00

Backport settle timer from v3

This commit is contained in:
Igor Putovny 2024-05-16 13:50:08 +02:00
parent bbac9ca958
commit 17fab002a1
3 changed files with 70 additions and 0 deletions

View 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_

View 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)

64
lib/settle.h Normal file
View File

@ -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