0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

tbf logging rate option merged from v2

This commit is contained in:
Katerina Kubecova 2024-05-09 16:46:26 +02:00
commit 5afb33fd75
17 changed files with 241 additions and 13 deletions

View File

@ -99,12 +99,16 @@ CF_DECLS
btime time;
struct f_prefix px;
struct proto_spec ps;
struct table_spec ts;
struct channel_limit cl;
struct timeformat *tf;
struct settle_config settle;
struct adata *ad;
const struct adata *bs;
struct aggr_item_node *ai;
struct logging_rate_targets *lrt;
struct tbf_config *tc;
enum tbf_targets tt;
}
%token END CLI_MARKER INVALID_TOKEN ELSECOL DDOT

View File

@ -139,16 +139,45 @@ typedef s64 btime;
/* Rate limiting */
struct tbf_config {
u16 burst; /* Max number of tokens */
u16 rate; /* Rate of replenishment (tokens / sec) */
};
struct tbf {
btime timestamp; /* Last update */
u64 count; /* Available micro-tokens */
u16 burst; /* Max number of tokens */
u16 rate; /* Rate of replenishment (tokens / sec) */
u32 drop; /* Number of failed request since last successful */
struct tbf_config cf; /* Configuration */
};
enum tbf_targets {
TBF_INVALID = 0,
TBF_PKT,
TBF_LSA,
TBF_RTE,
TBF_ALL
};
struct logging_rate_targets {
enum tbf_targets target;
struct logging_rate_targets *next;
};
struct cmd_logging_rate_info {
int all_protos;
struct tbf_config *tbfc;
struct logging_rate_targets *targets;
};
struct table_spec {
const void *ptr;
int patt;
};
/* Default TBF values for rate limiting log messages */
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
#define TBF_DEFAULT_LOG_LIMITS { .cf.rate = 1, .cf.burst = 5 }
int tbf_limit(struct tbf *f);

View File

@ -17,8 +17,8 @@ tbf_limit(struct tbf *f)
if (delta > 0)
{
u64 next = f->count + delta * f->rate;
u64 burst = (u64) f->burst << 20;
u64 next = f->count + delta * f->cf.rate;
u64 burst = (u64) f->cf.burst << 20;
f->count = MIN(next, burst);
f->timestamp += delta;
}

View File

@ -15,6 +15,7 @@ CF_HDR
#include "nest/mpls.h"
#include "lib/lists.h"
#include "lib/mac.h"
#include "lib/birdlib.h"
CF_DEFINES
@ -166,6 +167,8 @@ CF_KEYWORDS(MIN, IDLE, RX, TX, INTERVAL, MULTIPLIER, PASSIVE)
CF_KEYWORDS(CHECK, LINK)
CF_KEYWORDS(CORK, SORTED, TRIE, MIN, MAX, ROA, ROUTE, REFRESH, SETTLE, TIME, GC, THRESHOLD, PERIOD)
CF_KEYWORDS(MPLS_LABEL, MPLS_POLICY, MPLS_CLASS)
CF_KEYWORDS(LOGGING, RATE)
CF_KEYWORDS(PKT, LSA, RTE)
/* For r_args_channel */
CF_KEYWORDS(IPV4, IPV4_MC, IPV4_MPLS, IPV6, IPV6_MC, IPV6_MPLS, IPV6_SADR, VPN4, VPN4_MC, VPN4_MPLS, VPN6, VPN6_MC, VPN6_MPLS, ROA4, ROA6, FLOW4, FLOW6, MPLS, PRI, SEC)
@ -187,6 +190,7 @@ CF_ENUM(T_ENUM_MPLS_POLICY, MPLS_POLICY_, NONE, STATIC, PREFIX, AGGREGATE, VRF)
%type <sd> sym_args
%type <i> proto_start debug_mask debug_list debug_flag mrtdump_mask mrtdump_list mrtdump_flag export_mode limit_action net_type net_type_base tos password_algorithm
%type <ps> proto_patt proto_patt2
%type <ts> table_patt
%type <cc> channel_start proto_channel
%type <cl> limit_spec
%type <net> r_args_for_val
@ -194,6 +198,9 @@ CF_ENUM(T_ENUM_MPLS_POLICY, MPLS_POLICY_, NONE, STATIC, PREFIX, AGGREGATE, VRF)
%type <t> channel_sym
%type <c> channel_arg
%type <const_trie> partial_opt
%type <lrt> logging_rate_targets
%type <tc> logging_rate
%type <tt> tbf_target
CF_GRAMMAR
@ -259,6 +266,8 @@ table: table_start table_sorted table_opt_list ;
table_start: net_type TABLE symbol {
this_table = rt_new_table($3, $1);
this_table->log_tbf_cf.rate = 1;
this_table->log_tbf_cf.burst = 5;
}
;
@ -283,6 +292,7 @@ table_opt:
this_table->cork_threshold.high = $4; }
| EXPORT SETTLE TIME settle { this_table->export_settle = $4; }
| ROUTE REFRESH EXPORT SETTLE TIME settle { this_table->export_rr_settle = $6; }
| LOGGING RATE expr expr { this_table->log_tbf_cf.rate = $3; this_table->log_tbf_cf.burst = $4; }
;
table_opts:
@ -994,6 +1004,46 @@ CF_CLI_HELP(DEBUG, ..., [[Control protocol debugging via BIRD logs]])
CF_CLI(DEBUG, debug_args, (<protocol> | <channel> | \"<pattern>\" | all) (all | off | { states|routes|filters|interfaces|events|packets [, ...] }), [[Control protocol debugging via BIRD logs]])
{ /* Done in debug_args */ };
tbf_target:
PKT { $$ = TBF_PKT; }
|LSA { $$ = TBF_LSA; }
|RTE { $$ = TBF_RTE; }
|ALL { $$ = TBF_ALL; }
logging_rate_targets:
logging_rate_targets tbf_target {
$$ = (struct logging_rate_targets *) cfg_allocz(sizeof(struct logging_rate_targets));
$$->next = $1;
$$->target = $2;
} | tbf_target {
$$ = cfg_allocz(sizeof(struct logging_rate_targets));
$$->next = NULL;
$$->target = $1;
};
logging_rate: expr expr
{
$$ = cfg_allocz(sizeof(struct tbf_config));
$$->rate = $1;
$$->burst = $2;
};
CF_CLI(LOGGING RATE PROTOCOL, proto_patt logging_rate_targets logging_rate, (<protocol> | \"<pattern>\" | all) targets <rate> <burst>, [[Set logging rate for given protocols and targets]])
{
struct cmd_logging_rate_info info = {
.tbfc = $6,
.targets = $5,
.all_protos = $4.ptr == NULL
};
proto_apply_cmd($4, proto_cmd_logging_rate, 1, (uintptr_t) &info);
};
CF_CLI(LOGGING RATE TABLE, table_patt logging_rate, (<table> | \"<pattern>\" | all) <rate> <burst>, [[Set logging rate for given tables and targets]])
{
table_logging_cmd($4, $5);
};
CF_CLI_OPT(DEBUG ALL)
CF_CLI_OPT(DEBUG OFF)
CF_CLI_OPT(DEBUG STATES)
@ -1027,6 +1077,13 @@ proto_patt2:
| TEXT { $$.ptr = $1; $$.patt = 1; }
;
table_patt:
CF_SYM_KNOWN { cf_assert_symbol($1, SYM_TABLE); $$.ptr = $1; $$.patt = 0; }
| ALL { $$.ptr = NULL; $$.patt = 1; }
| TEXT { $$.ptr = $1; $$.patt = 1; }
;
CF_CODE
CF_END

View File

@ -3003,6 +3003,43 @@ proto_cmd_mrtdump(struct proto *p, uintptr_t mask, int cnt UNUSED)
p->mrtdump = mask;
}
void
proto_cmd_logging_rate(struct proto *p, uintptr_t arg, int cnt UNUSED)
{
struct cmd_logging_rate_info *args = (struct cmd_logging_rate_info *) arg;
struct logging_rate_targets *targets = args->targets;
while (targets)
{
int rate_changed = 0;
if ((targets->target == TBF_PKT || targets->target == TBF_ALL) && p->tbfs.pkt)
{
p->tbfs.pkt->cf.rate = args->tbfc->rate;
p->tbfs.pkt->cf.burst = args->tbfc->burst;
rate_changed++;
}
if ((targets->target == TBF_LSA || targets->target == TBF_ALL) && p->tbfs.lsa)
{
p->tbfs.lsa->cf.rate = args->tbfc->rate;
p->tbfs.lsa->cf.burst = args->tbfc->burst;
rate_changed++;
}
if ((targets->target == TBF_RTE || targets->target == TBF_ALL) && p->tbfs.rte)
{
p->tbfs.rte->cf.rate = args->tbfc->rate;
p->tbfs.rte->cf.burst = args->tbfc->burst;
rate_changed++;
}
if (rate_changed == 0 && args->all_protos == 0)
{
if (p->tbfs.pkt || p->tbfs.lsa || p->tbfs.rte)
cli_msg(9002, "%s supports rated logging only for%s%s%s", p->name, p->tbfs.pkt? " PKT":"", p->tbfs.lsa? " LSA":"", p->tbfs.rte? " RTE":"");
else
cli_msg(9002, "%s does not use rated logging", p->name);
}
targets = targets->next;
}
}
static void
proto_apply_cmd_symbol(const struct symbol *s, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
{

View File

@ -128,6 +128,13 @@ struct channel_import_request {
const struct f_trie *trie; /* Reload only matching nets */
};
struct proto_tbfs {
/* Pointers to rate limiting logging structures */
struct tbf *pkt;
struct tbf *lsa;
struct tbf *rte;
};
#define TLIST_PREFIX proto
#define TLIST_TYPE struct proto
#define TLIST_ITEM n
@ -178,6 +185,7 @@ struct proto {
btime last_state_change; /* Time of last state transition */
char *last_state_name_announced; /* Last state name we've announced to the user */
char *message; /* State-change message, allocated from proto_pool */
struct proto_tbfs tbfs; /* Pointers to rate limiting logging structures */
/*
* General protocol hooks:
@ -275,6 +283,7 @@ void proto_cmd_restart(struct proto *, uintptr_t, int);
void proto_cmd_reload(struct proto *, uintptr_t, int);
void proto_cmd_debug(struct proto *, uintptr_t, int);
void proto_cmd_mrtdump(struct proto *, uintptr_t, int);
void proto_cmd_logging_rate(struct proto *, uintptr_t, int);
void proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uintptr_t, int), int restricted, uintptr_t arg);
struct proto *proto_get_named(struct symbol *, struct protocol *);

View File

@ -70,6 +70,9 @@ struct rtable_config {
struct settle_config export_settle; /* Export announcement settler */
struct settle_config export_rr_settle;/* Export announcement settler config valid when any
route refresh is running */
btime min_settle_time; /* Minimum settle time for notifications */
btime max_settle_time; /* Maximum settle time for notifications */
struct tbf_config log_tbf_cf; /* Config logging rate for rtable */
};
struct rt_export_hook;
@ -166,6 +169,7 @@ struct rtable_private {
struct f_trie *flowspec_trie; /* Trie for evaluation of flowspec notifications */
// struct mpls_domain *mpls_domain; /* Label allocator for MPLS */
struct tbf log_tbf; /* Actual logging rate for rtable (might be changed in cmd) */
};
/* The final union private-public rtable structure */
@ -722,6 +726,8 @@ ea_set_hostentry(ea_list **to, rtable *dep, rtable *tab, ip_addr gw, ip_addr ll,
void ea_show_hostentry(const struct adata *ad, byte *buf, uint size);
void ea_show_nexthop_list(struct cli *c, struct nexthop_adata *nhad);
void table_logging_cmd(struct table_spec ts, struct tbf_config *rate);
/*
* Default protocol preferences
*/

View File

@ -110,6 +110,7 @@
#include "lib/flowspec.h"
#include "lib/idm.h"
#include "lib/netindex_private.h"
#include "nest/cli.h"
#ifdef CONFIG_BGP
#include "proto/bgp/bgp.h"
@ -2818,6 +2819,8 @@ rt_setup(pool *pp, struct rtable_config *cf)
t->id = idm_alloc(&rtable_idm);
if (t->id >= rtable_max_id)
rtable_max_id = t->id + 1;
t->log_tbf.cf.rate = cf->log_tbf_cf.rate;
t->log_tbf.cf.burst = cf->log_tbf_cf.burst;
t->netindex = rt_global_netindex_hash;
t->routes = mb_allocz(p, (t->routes_block_size = 128) * sizeof(net));
@ -4082,6 +4085,8 @@ rt_reconfigure(struct rtable_private *tab, struct rtable_config *new, struct rta
tab->name = new->name;
tab->config = new;
tab->debug = new->debug;
tab->log_tbf.cf.rate = new->log_tbf_cf.rate;
tab->log_tbf.cf.burst = new->log_tbf_cf.burst;
if (tab->hostcache)
tab->hostcache->req.trace_routes = new->debug;
@ -4852,6 +4857,50 @@ rt_get_hostentry(struct rtable_private *tab, ip_addr a, ip_addr ll, rtable *dep)
return he;
}
void
cmd_logging_rate(rtable *tp, struct tbf_config *rate)
{
RT_LOCKED(tp, table)
{
table->log_tbf.cf.rate = rate->rate;
table->log_tbf.cf.burst = rate->burst;
}
}
void
table_logging_cmd(struct table_spec ts, struct tbf_config *rate)
{
if (ts.patt)
{
const char *patt = (void *) ts.ptr;
int cnt = 0;
rtable *t;
node *n;
WALK_LIST2(t, n, routing_tables, n)
if (!ts.ptr || patmatch(patt, t->name))
{
cmd_logging_rate(t, rate);
cnt++;
}
if (!cnt)
cli_msg(8003, "No tables match");
else
cli_msg(0, "");
}
else
{
const struct symbol *s = (struct symbol*) ts.ptr;
if (s->table->table)
{
cmd_logging_rate(s->table->table, rate);
cli_msg(0, "");
}
else
cli_msg(9002, "%s does not exist", s->name);
}
}
/*
* Documentation for functions declared inline in route.h

View File

@ -2642,7 +2642,10 @@ babel_start(struct proto *P)
p->msg_slab = sl_new(P->pool, sizeof(struct babel_msg_node));
p->seqno_slab = sl_new(P->pool, sizeof(struct babel_seqno_request));
p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
p->log_pkt_tbf = (struct tbf){ .cf.rate = cf->log_pkt_tbf.rate, .cf.burst = cf->log_pkt_tbf.burst };
P->tbfs.pkt = &p->log_pkt_tbf;
P->tbfs.rte = NULL;
P->tbfs.lsa = NULL;
return PS_UP;
}
@ -2697,6 +2700,8 @@ babel_reconfigure(struct proto *P, struct proto_config *CF)
p->p.cf = CF;
babel_reconfigure_ifaces(p, new);
p->log_pkt_tbf.cf.rate = new->log_pkt_tbf.rate;
p->log_pkt_tbf.cf.burst = new->log_pkt_tbf.burst;
babel_trigger_update(p);
babel_kick_timer(p);

View File

@ -132,6 +132,8 @@ struct babel_config {
struct channel_config *ip4_channel;
struct channel_config *ip6_channel;
struct tbf_config log_pkt_tbf;
};
struct babel_iface_config {

View File

@ -26,7 +26,8 @@ CF_KEYWORDS(BABEL, INTERFACE, METRIC, RXCOST, HELLO, UPDATE, INTERVAL, PORT,
TYPE, WIRED, WIRELESS, RX, TX, BUFFER, PRIORITY, LENGTH, CHECK, LINK,
NEXT, HOP, IPV4, IPV6, SHOW, INTERFACES, NEIGHBORS,
ENTRIES, RANDOMIZE, ROUTER, ID, AUTHENTICATION, NONE, MAC, PERMISSIVE,
EXTENDED, TUNNEL, RTT, MIN, MAX, DECAY, SEND, TIMESTAMPS, COST, DELAY)
EXTENDED, TUNNEL, RTT, MIN, MAX, DECAY, SEND, TIMESTAMPS, COST, DELAY,
PKT, LOGGING, RATE, BURST)
CF_GRAMMAR
@ -37,6 +38,8 @@ babel_proto_start: proto_start BABEL
this_proto = proto_config_new(&proto_babel, $1);
init_list(&BABEL_CFG->iface_list);
BABEL_CFG->hold_time = 1 S_;
BABEL_CFG->log_pkt_tbf.rate = 1;
BABEL_CFG->log_pkt_tbf.burst = 5;
};
babel_proto_item:
@ -44,6 +47,7 @@ babel_proto_item:
| proto_channel
| INTERFACE babel_iface
| RANDOMIZE ROUTER ID bool { BABEL_CFG->randomize_router_id = $4; }
| LOGGING RATE PKT expr expr { BABEL_CFG->log_pkt_tbf.rate = $4; BABEL_CFG->log_pkt_tbf.burst = $5; }
;
babel_proto_opts:

View File

@ -201,6 +201,7 @@ CF_KEYWORDS(WAIT, DELAY, LSADB, ECMP, LIMIT, WEIGHT, NSSA, TRANSLATOR, STABILITY
CF_KEYWORDS(GLOBAL, LSID, ROUTER, SELF, INSTANCE, REAL, NETMASK, TX, PRIORITY, LENGTH)
CF_KEYWORDS(MERGE, LSA, SUPPRESSION, MULTICAST, RFC5838, VPN, PE, ADDRESS)
CF_KEYWORDS(GRACEFUL, RESTART, AWARE, TIME)
CF_KEYWORDS(PKT, LSA, LOGGING, RATE, BURST)
%type <ld> lsadb_args
%type <i> ospf_variant ospf_af_mc nbma_eligible
@ -229,6 +230,10 @@ ospf_proto_start: proto_start ospf_variant
OSPF_CFG->af_ext = !$2;
OSPF_CFG->gr_mode = OSPF_GR_AWARE;
OSPF_CFG->gr_time = OSPF_DEFAULT_GR_TIME;
OSPF_CFG->log_pkt_tbf.rate = 1;
OSPF_CFG->log_lsa_tbf.rate = 4;
OSPF_CFG->log_pkt_tbf.burst = 5;
OSPF_CFG->log_lsa_tbf.burst = 20;
};
ospf_proto:
@ -269,6 +274,8 @@ ospf_proto_item:
| TICK expr { OSPF_CFG->tick = $2; if($2 <= 0) cf_error("Tick must be greater than zero"); }
| INSTANCE ID expr { OSPF_CFG->instance_id = $3; OSPF_CFG->instance_id_set = 1; if ($3 > 255) cf_error("Instance ID must be in range 0-255"); }
| ospf_area
| LOGGING RATE PKT expr expr { OSPF_CFG->log_pkt_tbf.rate = $4; OSPF_CFG->log_pkt_tbf.burst = $5; }
| LOGGING RATE LSA expr expr { OSPF_CFG->log_lsa_tbf.rate = $4; OSPF_CFG->log_lsa_tbf.burst = $4; }
;
ospf_area_start: AREA idval {

View File

@ -312,8 +312,11 @@ ospf_start(struct proto *P)
p->flood_event = ev_new_init(P->pool, ospf_flood_event, p);
p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
p->log_pkt_tbf = (struct tbf){ .cf.rate = c->log_pkt_tbf.rate, .cf.burst = c->log_pkt_tbf.burst };
p->log_lsa_tbf = (struct tbf){ .cf.rate = c->log_lsa_tbf.rate, .cf.burst = c->log_lsa_tbf.burst };
P->tbfs.pkt = &p->log_pkt_tbf;
P->tbfs.rte = NULL;
P->tbfs.lsa = &p->log_lsa_tbf;
/* Lock the channel when in GR recovery mode */
if (p->p.gr_recovery && (p->gr_mode == OSPF_GR_ABLE))
@ -762,7 +765,6 @@ ospf_reconfigure(struct proto *P, struct proto_config *CF)
return 1;
}
void
ospf_sh_neigh(struct proto *P, const char *iff)
{

View File

@ -106,6 +106,8 @@ struct ospf_config
uint ecmp;
list area_list; /* list of area configs (struct ospf_area_config) */
list vlink_list; /* list of configured vlinks (struct ospf_iface_patt) */
struct tbf_config log_pkt_tbf;
struct tbf_config log_lsa_tbf;
};
struct ospf_area_config

View File

@ -37,7 +37,7 @@ CF_KEYWORDS(RIP, NG, ECMP, LIMIT, WEIGHT, INFINITY, METRIC, UPDATE, TIMEOUT,
PASSIVE, VERSION, SPLIT, HORIZON, POISON, REVERSE, CHECK, ZERO,
TIME, BFD, AUTHENTICATION, NONE, PLAINTEXT, CRYPTOGRAPHIC, MD5,
TTL, SECURITY, RX, TX, BUFFER, LENGTH, PRIORITY, ONLY, LINK,
DEMAND, CIRCUIT)
DEMAND, CIRCUIT, PKT, RTE, LOGGING, RATE, BURST)
%type <i> rip_variant rip_auth
@ -61,6 +61,10 @@ rip_proto_start: proto_start rip_variant
RIP_CFG->infinity = RIP_DEFAULT_INFINITY;
RIP_CFG->min_timeout_time = 60 S_;
RIP_CFG->max_garbage_time = 60 S_;
RIP_CFG->log_pkt_tbf.rate = 1;
RIP_CFG->log_rte_tbf.rate = 4;
RIP_CFG->log_pkt_tbf.burst = 5;
RIP_CFG->log_rte_tbf.burst = 20;
};
rip_proto_item:
@ -70,6 +74,8 @@ rip_proto_item:
| ECMP bool LIMIT expr { RIP_CFG->ecmp = $2 ? $4 : 0; }
| INFINITY expr { RIP_CFG->infinity = $2; }
| INTERFACE rip_iface
| LOGGING RATE PKT expr expr { RIP_CFG->log_pkt_tbf.rate = $4; RIP_CFG->log_pkt_tbf.burst = $5; }
| LOGGING RATE RTE expr expr { RIP_CFG->log_rte_tbf.rate = $4; RIP_CFG->log_rte_tbf.burst = $4; }
;
rip_proto_opts:

View File

@ -1246,8 +1246,11 @@ rip_start(struct proto *P)
p->infinity = cf->infinity;
p->triggered = 0;
p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
p->log_rte_tbf = (struct tbf){ .rate = 4, .burst = 20 };
p->log_pkt_tbf = (struct tbf){ .cf.rate = cf->log_pkt_tbf.rate, .cf.burst = cf->log_pkt_tbf.burst };
p->log_rte_tbf = (struct tbf){ .cf.rate = cf->log_rte_tbf.rate, .cf.burst = cf->log_pkt_tbf.burst };
P->tbfs.pkt = &p->log_pkt_tbf;
P->tbfs.rte = &p->log_rte_tbf;
P->tbfs.lsa = NULL;
tm_start(p->timer, MIN(cf->min_timeout_time, cf->max_garbage_time));
@ -1289,6 +1292,10 @@ rip_reconfigure(struct proto *P, struct proto_config *CF)
p->p.cf = CF;
p->ecmp = new->ecmp;
rip_reconfigure_ifaces(p, new);
p->log_pkt_tbf.cf.rate = new->log_pkt_tbf.rate;
p->log_pkt_tbf.cf.burst = new->log_pkt_tbf.burst;
p->log_rte_tbf.cf.rate = new->log_rte_tbf.rate;
p->log_rte_tbf.cf.burst = new->log_rte_tbf.burst;
p->rt_reload = 1;
rip_kick_timer(p);

View File

@ -55,6 +55,8 @@ struct rip_config
btime min_timeout_time; /* Minimum of interface timeout_time */
btime max_garbage_time; /* Maximum of interface garbage_time */
struct tbf_config log_pkt_tbf;
struct tbf_config log_rte_tbf;
};
struct rip_iface_config