From: Vojtech Vilimek Date: Wed, 20 Jul 2022 15:04:49 +0000 (+0200) Subject: Add new global counter for stats channel X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=727a8f32c44ca81e189674adb215f8778e1ec43c;p=thirdparty%2Fbird.git Add new global counter for stats channel New symbol is added for each stats protocol channel, the symbol is accessed by same name as the underlying channel. Symbols evaluate to the sum of all routes exported from connected table with generation less than max generation of particular channel. Default max generation is 16. Beware you shouldn't make cyclic references as the behavior of such configuration is not defined! --- diff --git a/conf/conf.h b/conf/conf.h index 4ccaa54e9..d193155bd 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -124,6 +124,7 @@ struct symbol { struct ea_class *attribute; /* For SYM_ATTRIBUTE */ struct f_val *val; /* For SYM_CONSTANT */ uint offset; /* For SYM_VARIABLE */ + struct channel_config *ch_config; /* For SYM_COUNTER */ }; char name[0]; @@ -156,6 +157,7 @@ struct bytestring { #define SYM_FILTER 4 #define SYM_TABLE 5 #define SYM_ATTRIBUTE 6 +#define SYM_COUNTER 7 #define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */ #define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff) diff --git a/filter/config.Y b/filter/config.Y index 8cecf9361..23b258cd9 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -758,6 +758,9 @@ symbol_value: symbol_known case SYM_ATTRIBUTE: $$ = f_new_inst(FI_EA_GET, $1->attribute); break; + case SYM_COUNTER: + $$ = f_new_inst(FI_COUNTER, $1); + break; default: cf_error("Can't get value of symbol %s", $1->name); } diff --git a/filter/f-inst.c b/filter/f-inst.c index e7b642abf..4dfd276b0 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -473,6 +473,12 @@ RESULT_VAL(fstk->vstk[curline.vbase + sym->offset]); } + INST(FI_COUNTER, 0, 1) { + SYMBOL; + NEVER_CONSTANT; + RESULT(T_INT, i, get_stats_sum(sym)); + } + INST(FI_CONSTANT, 0, 1) { FID_MEMBER( struct f_val, diff --git a/filter/filter.c b/filter/filter.c index ad2fafe21..bd65777f3 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -43,6 +43,7 @@ #include "filter/filter.h" #include "filter/f-inst.h" #include "filter/data.h" +#include "proto/stats/stats.h" /* provides function get_stats_sum used in f-inst.c */ /* Exception bits */ diff --git a/proto/stats/config.Y b/proto/stats/config.Y index b32ea773a..087ac75ae 100644 --- a/proto/stats/config.Y +++ b/proto/stats/config.Y @@ -61,6 +61,9 @@ stats_channel_start: net_type symbol { this_channel = channel_config_get(&channel_stats, $2->name, $1, this_proto); STATS_CC->max_generation = 16; + /* from filter/config.Y: + cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++) */ + $2 = cf_define_symbol($2, SYM_COUNTER, ch_config, this_channel); } stats_proto: diff --git a/proto/stats/stats.h b/proto/stats/stats.h index 51802341c..d061e4eaf 100644 --- a/proto/stats/stats.h +++ b/proto/stats/stats.h @@ -35,6 +35,13 @@ struct stats_channel_config { u8 max_generation; }; - +static inline int +get_stats_sum(struct symbol *sym) +{ + if (sym->ch_config->channel) + return (int) ((struct stats_channel *) sym->ch_config->channel)->sum; + else + return 0; +} #endif