From 9b46748d5b50d1e8c242a571e80fe1f9f33aeb73 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 21 Jan 2019 09:17:54 +0100 Subject: Filter: refactoring of instruction constructors --- conf/conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'conf/conf.h') diff --git a/conf/conf.h b/conf/conf.h index 427569fd..6138ccec 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -107,7 +107,7 @@ struct symbol { struct sym_scope *scope; int class; int aux; - void *aux2; + uint aux2; void *def; char name[1]; }; -- cgit v1.2.3 From c0e958e022aac79f69e6aca2652fdb6a529e68e2 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Wed, 30 Jan 2019 14:03:47 +0100 Subject: Filter + Config: Fix bugs, tests and split symbols by type --- conf/cf-lex.l | 22 ++++++-- conf/conf.h | 4 -- conf/confbase.Y | 35 ++++++++---- filter/config.Y | 144 ++++++++++++++++++++++++++++---------------------- filter/f-inst.c | 13 +++-- filter/filter.h | 22 +++++++- filter/filter_test.c | 35 +++++++----- filter/test.conf | 11 +++- nest/config.Y | 52 +++++++++--------- proto/babel/config.Y | 8 +-- proto/bfd/config.Y | 4 +- proto/ospf/config.Y | 16 +++--- proto/ospf/ospf.c | 2 +- proto/ospf/ospf.h | 2 +- proto/rip/config.Y | 4 +- proto/static/config.Y | 2 +- 16 files changed, 224 insertions(+), 152 deletions(-) (limited to 'conf/conf.h') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 9bbb3660..5e7c8418 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -285,7 +285,18 @@ else: { } cf_lval.s = cf_get_symbol(yytext); - return SYM; + switch (cf_lval.s->class) { + case SYM_VOID: return CF_SYM_VOID; + case SYM_PROTO: return CF_SYM_PROTO; + case SYM_TEMPLATE: return CF_SYM_TEMPLATE; + case SYM_FUNCTION: return CF_SYM_FUNCTION; + case SYM_FILTER: return CF_SYM_FILTER; + case SYM_TABLE: return CF_SYM_TABLE; + case SYM_ATTRIBUTE: return CF_SYM_ATTRIBUTE; + case SYM_VARIABLE_RANGE: return CF_SYM_VARIABLE; + case SYM_CONSTANT_RANGE: return CF_SYM_CONSTANT; + default: bug("Unknown symbol class %d", cf_lval.s->class); + } } (.|\n) { @@ -723,9 +734,6 @@ cf_pop_scope(void) char * cf_symbol_class_name(struct symbol *sym) { - if (cf_symbol_is_constant(sym)) - return "constant"; - switch (sym->class) { case SYM_VOID: @@ -740,6 +748,12 @@ cf_symbol_class_name(struct symbol *sym) return "filter"; case SYM_TABLE: return "routing table"; + case SYM_ATTRIBUTE: + return "custom attribute"; + case SYM_CONSTANT_RANGE: + return "constant"; + case SYM_VARIABLE_RANGE: + return "variable"; default: return "unknown type"; } diff --git a/conf/conf.h b/conf/conf.h index 6138ccec..4e3addb3 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -165,10 +165,6 @@ void cf_push_scope(struct symbol *); void cf_pop_scope(void); char *cf_symbol_class_name(struct symbol *sym); -static inline int cf_symbol_is_constant(struct symbol *sym) -{ return (sym->class & 0xff00) == SYM_CONSTANT; } - - /* Parser */ extern char *cf_text; diff --git a/conf/confbase.Y b/conf/confbase.Y index 13f6aade..2195e8fc 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -55,6 +55,7 @@ CF_DECLS enum ec_subtype ecs; struct f_dynamic_attr fda; struct f_static_attr fsa; + struct f_lval flv; struct filter *f; struct f_tree *e; struct f_trie *trie; @@ -81,7 +82,7 @@ CF_DECLS %token IP4 %token IP6 %token VPN_RD -%token SYM +%token CF_SYM_VOID CF_SYM_PROTO CF_SYM_TEMPLATE CF_SYM_FUNCTION CF_SYM_FILTER CF_SYM_TABLE CF_SYM_ATTRIBUTE CF_SYM_VARIABLE CF_SYM_CONSTANT %token TEXT %type ipa_scope @@ -93,6 +94,7 @@ CF_DECLS %type label_stack_start label_stack %type text opttext +%type symbol %nonassoc PREFIX_DUMMY %left AND OR @@ -125,7 +127,7 @@ conf: ';' ; conf: definition ; definition: - DEFINE SYM '=' term ';' { + DEFINE CF_SYM_VOID '=' term ';' { struct f_val *val = cfg_alloc(sizeof(struct f_val)); if (f_eval(f_postfixify($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error"); cf_define_symbol($2, SYM_CONSTANT | val->type, val); @@ -135,18 +137,29 @@ definition: expr: NUM | '(' term ')' { $$ = f_eval_int(f_postfixify($2)); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number expected"); $$ = SYM_VAL($1).i; } ; - expr_us: expr S { $$ = $1 S_; } | expr MS { $$ = $1 MS_; } | expr US { $$ = $1 US_; } ; +symbol: + CF_SYM_VOID + | CF_SYM_PROTO + | CF_SYM_TEMPLATE + | CF_SYM_FUNCTION + | CF_SYM_FILTER + | CF_SYM_TABLE + | CF_SYM_ATTRIBUTE + | CF_SYM_VARIABLE + | CF_SYM_CONSTANT + ; + /* Switches */ bool: @@ -164,7 +177,7 @@ bool: ipa: IP4 { $$ = ipa_from_ip4($1); } | IP6 { $$ = ipa_from_ip6($1); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address expected"); $$ = SYM_VAL($1).ip; } @@ -172,7 +185,7 @@ ipa: ipa_scope: /* empty */ { $$ = NULL; } - | '%' SYM { $$ = if_get_by_name($2->name); } + | '%' symbol { $$ = if_get_by_name($2->name); } ; @@ -279,7 +292,7 @@ net_: net_ip6: net_ip6_ - | SYM { + | CF_SYM_CONSTANT { if (($1->class != (SYM_CONSTANT | T_NET)) || (SYM_VAL($1).net->type != NET_IP6)) cf_error("IPv6 network expected"); $$ = * SYM_VAL($1).net; @@ -288,7 +301,7 @@ net_ip6: net_ip: net_ip_ - | SYM { + | CF_SYM_CONSTANT { if (($1->class != (SYM_CONSTANT | T_NET)) || !net_is_ip(SYM_VAL($1).net)) cf_error("IP network expected"); $$ = * SYM_VAL($1).net; @@ -297,7 +310,7 @@ net_ip: net_any: net_ - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_NET)) cf_error("Network expected"); $$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */ @@ -309,7 +322,7 @@ net_or_ipa: | net_ip6_ | IP4 { net_fill_ip4(&($$), $1, IP4_MAX_PREFIX_LENGTH); } | IP6 { net_fill_ip6(&($$), $1, IP6_MAX_PREFIX_LENGTH); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class == (SYM_CONSTANT | T_IP)) net_fill_ip_host(&($$), SYM_VAL($1).ip); else if (($1->class == (SYM_CONSTANT | T_NET)) && net_is_ip(SYM_VAL($1).net)) @@ -346,7 +359,7 @@ time: text: TEXT - | SYM { + | CF_SYM_CONSTANT { if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String expected"); $$ = SYM_VAL($1).s; } diff --git a/filter/config.Y b/filter/config.Y index 607f534e..1306849f 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -17,7 +17,7 @@ static inline u32 pair_a(u32 p) { return p >> 16; } static inline u32 pair_b(u32 p) { return p & 0xFFFF; } #define f_generate_complex(fi_code, da, arg) \ - f_new_inst(FI_EA_SET, da, f_new_inst(fi_code, f_new_inst(FI_EA_GET, da), arg)) + f_new_inst(FI_EA_SET, f_new_inst(fi_code, f_new_inst(FI_EA_GET, da), arg), da) /* * Sets and their items are during parsing handled as lists, linked @@ -179,7 +179,7 @@ f_generate_empty(struct f_dynamic_attr dyn) cf_error("Can't empty that attribute"); } - return f_new_inst(FI_EA_SET, dyn, f_new_inst(FI_CONSTANT, empty)); + return f_new_inst(FI_EA_SET, f_new_inst(FI_CONSTANT, empty), dyn); } #if 0 @@ -389,6 +389,37 @@ assert_done(struct f_inst *expr, const char *start, const char *end) : "???"); } +static struct f_inst * +assert_assign(struct f_lval *lval, struct f_inst *expr, const char *start, const char *end) +{ + struct f_inst *setter, *getter, *checker; + switch (lval->type) { + case F_LVAL_VARIABLE: + setter = f_new_inst(FI_SET, expr, lval->sym); + getter = f_new_inst(FI_VARIABLE, lval->sym); + break; + case F_LVAL_PREFERENCE: + setter = f_new_inst(FI_PREF_SET, expr); + getter = f_new_inst(FI_PREF_GET); + break; + case F_LVAL_SA: + setter = f_new_inst(FI_RTA_SET, expr, lval->sa); + getter = f_new_inst(FI_RTA_GET, lval->sa); + break; + case F_LVAL_EA: + setter = f_new_inst(FI_EA_SET, expr, lval->da); + getter = f_new_inst(FI_EA_GET, lval->da); + break; + default: + bug("Unknown lval type"); + } + + checker = f_new_inst(FI_EQ, expr, getter); + f_inst_next(setter, checker); + + return assert_done(setter, start, end); +} + CF_DECLS CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, @@ -407,17 +438,18 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, PREPEND, FIRST, LAST, LAST_NONAGGREGATED, MATCH, EMPTY, FILTER, WHERE, EVAL, ATTRIBUTE, - BT_ASSERT, BT_TEST_SUITE, FORMAT) + BT_ASSERT, BT_TEST_SUITE, BT_CHECK_ASSIGN, FORMAT) %nonassoc THEN %nonassoc ELSE %type function_params declsn %type cmds_int function_body -%type term block cmd cmds constant constructor print_one print_list var_list var_listn function_call symbol bgp_path_expr bgp_path bgp_path_tail one_decl decls +%type term block cmd cmds constant constructor print_one print_list var_list var_listn function_call symbol_value bgp_path_expr bgp_path bgp_path_tail one_decl decls %type dynamic_attr %type static_attr %type filter filter_body where_filter +%type lvalue %type type %type ec_kind %type break_command @@ -432,7 +464,7 @@ CF_GRAMMAR conf: filter_def ; filter_def: - FILTER SYM { $2 = cf_define_symbol($2, SYM_FILTER, NULL); cf_push_scope( $2 ); } + FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, NULL); cf_push_scope( $2 ); } filter_body { $2->def = $4; $4->name = $2->name; @@ -447,16 +479,13 @@ filter_eval: ; conf: custom_attr ; -custom_attr: ATTRIBUTE type SYM ';' { +custom_attr: ATTRIBUTE type CF_SYM_VOID ';' { cf_define_symbol($3, SYM_ATTRIBUTE, ca_lookup(new_config->pool, $3->name, $2)->fda); }; conf: bt_test_suite ; bt_test_suite: - BT_TEST_SUITE '(' SYM ',' text ')' { - if (!($3->class & SYM_FUNCTION)) - cf_error("Function expected"); - + BT_TEST_SUITE '(' CF_SYM_FUNCTION ',' text ')' { struct f_bt_test_suite *t = cfg_alloc(sizeof(struct f_bt_test_suite)); t->fn = $3->def; t->fn_name = $3->name; @@ -505,7 +534,7 @@ type: ; one_decl: - type SYM { + type CF_SYM_VOID { struct f_val * val = cfg_alloc(sizeof(struct f_val)); val->type = T_VOID; $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val); @@ -545,8 +574,7 @@ filter_body: ; filter: - SYM { - if ($1->class != SYM_FILTER) cf_error("No such filter."); + CF_SYM_FILTER { $$ = $1->def; } | filter_body @@ -573,7 +601,7 @@ function_body: conf: function_def ; function_def: - FUNCTION SYM { DBG( "Beginning of function %s\n", $2->name ); + FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name ); $2 = cf_define_symbol($2, SYM_FUNCTION, NULL); cf_push_scope($2); } function_params function_body { @@ -647,8 +675,7 @@ set_atom: if (f_eval(f_postfixify($2), cfg_mem, &($$)) > F_RETURN) cf_error("Runtime error"); if (!f_valid_set_type($$.type)) cf_error("Set-incompatible type"); } - | SYM { - if (!cf_symbol_is_constant($1)) cf_error("%s: constant expected", $1->name); + | CF_SYM_CONSTANT { if (!f_valid_set_type(SYM_TYPE($1))) cf_error("%s: set-incompatible type", $1->name); $$ = *(struct f_val *)($1->def); } @@ -764,7 +791,7 @@ switch_body: /* EMPTY */ { $$ = NULL; } ; bgp_path_expr: - symbol { $$ = $1; } + symbol_value { $$ = $1; } | '(' term ')' { $$ = $2; } ; @@ -806,31 +833,17 @@ constructor: ; -rtadot: /* EMPTY, we are not permitted RTA. prefix */ - ; - function_call: - SYM '(' var_list ')' { + CF_SYM_FUNCTION '(' var_list ')' { $$ = f_new_inst(FI_CALL, $1, $3); } ; -symbol: - SYM { - switch ($1->class & 0xffff) { - case SYM_CONSTANT_RANGE: - $$ = f_new_inst(FI_CONSTANT_INDIRECT, $1->def); - break; - case SYM_VARIABLE_RANGE: - $$ = f_new_inst(FI_VARIABLE, $1); - break; - case SYM_ATTRIBUTE: - $$ = f_new_inst(FI_EA_GET, *((struct f_dynamic_attr *) $1->def)); - break; - default: - cf_error("%s: variable expected.", $1->name); - } - } +symbol_value: + CF_SYM_CONSTANT { $$ = f_new_inst(FI_CONSTANT_INDIRECT, $1->def); } + | CF_SYM_VARIABLE { $$ = f_new_inst(FI_VARIABLE, $1); } + | CF_SYM_ATTRIBUTE { $$ = f_new_inst(FI_EA_GET, *((struct f_dynamic_attr *) $1->def)); } + ; static_attr: FROM { $$ = f_new_static_attr(T_IP, SA_FROM, 0); } @@ -863,15 +876,15 @@ term: | '!' term { $$ = f_new_inst(FI_NOT, $2); } | DEFINED '(' term ')' { $$ = f_new_inst(FI_DEFINED, $3); } - | symbol { $$ = $1; } + | symbol_value { $$ = $1; } | constant { $$ = $1; } | constructor { $$ = $1; } | PREFERENCE { $$ = f_new_inst(FI_PREF_GET); } - | rtadot static_attr { $$ = f_new_inst(FI_RTA_GET, $2); } + | static_attr { $$ = f_new_inst(FI_RTA_GET, $1); } - | rtadot dynamic_attr { $$ = f_new_inst(FI_EA_GET, $2); } + | dynamic_attr { $$ = f_new_inst(FI_EA_GET, $1); } | term '.' IS_V4 { $$ = f_new_inst(FI_IS_V4, $1); } | term '.' TYPE { $$ = f_new_inst(FI_TYPE, $1); } @@ -888,10 +901,10 @@ term: /* Communities */ /* This causes one shift/reduce conflict - | rtadot dynamic_attr '.' ADD '(' term ')' { } - | rtadot dynamic_attr '.' DELETE '(' term ')' { } - | rtadot dynamic_attr '.' CONTAINS '(' term ')' { } - | rtadot dynamic_attr '.' RESET{ } + | dynamic_attr '.' ADD '(' term ')' { } + | dynamic_attr '.' DELETE '(' term ')' { } + | dynamic_attr '.' CONTAINS '(' term ')' { } + | dynamic_attr '.' RESET{ } */ | '+' EMPTY '+' { $$ = f_new_inst(FI_CONSTANT, f_const_empty_path); } @@ -956,32 +969,29 @@ cmd: | IF term THEN block ELSE block { $$ = f_new_inst(FI_CONDITION, $2, $4, $6); } - | SYM '=' term ';' { - DBG( "Ook, we'll set value\n" ); - if ($1->class == SYM_ATTRIBUTE) { - $$ = f_new_inst(FI_EA_SET, *((struct f_dynamic_attr *) $1->def), $3); - } else if (($1->class & ~T_MASK) == SYM_VARIABLE) { - $$ = f_new_inst(FI_SET, $3, $1); - } else - cf_error( "Symbol `%s' is read-only.", $1->name ); + | CF_SYM_ATTRIBUTE '=' term ';' { + $$ = f_new_inst(FI_EA_SET, $3, *((struct f_dynamic_attr *) $1->def)); + } + | CF_SYM_VARIABLE '=' term ';' { + $$ = f_new_inst(FI_SET, $3, $1); } | RETURN term ';' { DBG( "Ook, we'll return the value\n" ); $$ = f_new_inst(FI_RETURN, $2); } - | rtadot dynamic_attr '=' term ';' { - $$ = f_new_inst(FI_EA_SET, $2, $4); + | dynamic_attr '=' term ';' { + $$ = f_new_inst(FI_EA_SET, $3, $1); } - | rtadot static_attr '=' term ';' { - if ($2.readonly) + | static_attr '=' term ';' { + if ($1.readonly) cf_error( "This static attribute is read-only."); - $$ = f_new_inst(FI_RTA_SET, $2, $4); + $$ = f_new_inst(FI_RTA_SET, $3, $1); } | PREFERENCE '=' term ';' { $$ = f_new_inst(FI_PREF_SET, $3); } - | UNSET '(' rtadot dynamic_attr ')' ';' { - $$ = f_new_inst(FI_EA_UNSET, $4); + | UNSET '(' dynamic_attr ')' ';' { + $$ = f_new_inst(FI_EA_UNSET, $3); } | break_command print_list ';' { $$ = f_new_inst(FI_PRINT_AND_DIE, $2, $1); } | function_call ';' { $$ = f_new_inst(FI_DROP_RESULT, $1); } @@ -989,12 +999,13 @@ cmd: $$ = f_new_inst(FI_SWITCH, $2, build_tree($4)); } - | rtadot dynamic_attr '.' EMPTY ';' { $$ = f_generate_empty($2); } - | rtadot dynamic_attr '.' PREPEND '(' term ')' ';' { $$ = f_generate_complex( FI_PATH_PREPEND, $2, $6 ); } - | rtadot dynamic_attr '.' ADD '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_ADD, $2, $6 ); } - | rtadot dynamic_attr '.' DELETE '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_DEL, $2, $6 ); } - | rtadot dynamic_attr '.' FILTER '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_FILTER, $2, $6 ); } + | dynamic_attr '.' EMPTY ';' { $$ = f_generate_empty($1); } + | dynamic_attr '.' PREPEND '(' term ')' ';' { $$ = f_generate_complex( FI_PATH_PREPEND, $1, $5 ); } + | dynamic_attr '.' ADD '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_ADD, $1, $5 ); } + | dynamic_attr '.' DELETE '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_DEL, $1, $5 ); } + | dynamic_attr '.' FILTER '(' term ')' ';' { $$ = f_generate_complex( FI_CLIST_FILTER, $1, $5 ); } | BT_ASSERT '(' get_cf_position term get_cf_position ')' ';' { $$ = assert_done($4, $3 + 1, $5 - 1); } + | BT_CHECK_ASSIGN '(' get_cf_position lvalue get_cf_position ',' term ')' ';' { $$ = assert_assign(&$4, $7, $3 + 1, $5 - 1); } ; get_cf_position: @@ -1002,5 +1013,10 @@ get_cf_position: $$ = cf_text; }; +lvalue: + CF_SYM_VARIABLE { $$ = (struct f_lval) { .type = F_LVAL_VARIABLE, .sym = $1 }; } + | PREFERENCE { $$ = (struct f_lval) { .type = F_LVAL_PREFERENCE }; } + | static_attr { $$ = (struct f_lval) { .type = F_LVAL_SA, .sa = $1 }; } + | dynamic_attr { $$ = (struct f_lval) { .type = F_LVAL_EA, .da = $1 }; }; CF_END diff --git a/filter/f-inst.c b/filter/f-inst.c index ae2b5289..0dd9f9f6 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -109,7 +109,7 @@ ARG_ANY(1); COUNT(2); - NEW([[]], [[ + NEW(, [[ uint len = 0; uint dyn = 0; for (const struct f_inst *tt = f1; tt; tt = tt->next, len++) @@ -329,9 +329,9 @@ } INST(FI_RTA_SET, 1, 0) { - STATIC_ATTR; ACCESS_RTE; ARG_ANY(1); + STATIC_ATTR; if (sa.f_type != v1.type) runtime( "Attempt to set static attribute to incompatible type" ); @@ -475,10 +475,10 @@ } INST(FI_EA_SET, 1, 0) { - DYNAMIC_ATTR; ACCESS_RTE; ACCESS_EATTRS; ARG_ANY(1); + DYNAMIC_ATTR; { struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr)); @@ -719,7 +719,7 @@ /* Then push the arguments */ LINE(1,1); - NEW([[]], [[ + NEW(, [[ if (sym->class != SYM_FUNCTION) cf_error("You can't call something which is not a function. Really."); @@ -984,5 +984,8 @@ INST(FI_ASSERT, 1, 0) { /* Birdtest Assert */ ARG(1, T_BOOL); STRING; - CALL(bt_assert_hook, res.val.i, what); + if (!bt_assert_hook) + runtime("No bt_assert hook registered, can't assert"); + + bt_assert_hook(res.val.i, what); } diff --git a/filter/filter.h b/filter/filter.h index 39f16e93..15a24fd4 100644 --- a/filter/filter.h +++ b/filter/filter.h @@ -128,6 +128,24 @@ enum filter_return { F_QUITBIRD, }; +/* Filter l-value type */ +enum f_lval_type { + F_LVAL_VARIABLE, + F_LVAL_PREFERENCE, + F_LVAL_SA, + F_LVAL_EA, +}; + +/* Filter l-value */ +struct f_lval { + enum f_lval_type type; + union { + const struct symbol *sym; + struct f_dynamic_attr da; + struct f_static_attr sa; + }; +}; + /* Filter instruction declarations */ #define FI__LIST \ F(FI_NOP) \ @@ -159,9 +177,9 @@ enum filter_return { F(FI_CONDITION, ARG, LINE, LINE) \ F(FI_PRINT_AND_DIE, ARG, FRET) \ F(FI_RTA_GET, SA) \ - F(FI_RTA_SET, SA, ARG) \ + F(FI_RTA_SET, ARG, SA) \ F(FI_EA_GET, EA) \ - F(FI_EA_SET, EA, ARG) \ + F(FI_EA_SET, ARG, EA) \ F(FI_EA_UNSET, EA) \ F(FI_PREF_GET) \ F(FI_PREF_SET, ARG) \ diff --git a/filter/filter_test.c b/filter/filter_test.c index e19b0a75..af6b590f 100644 --- a/filter/filter_test.c +++ b/filter/filter_test.c @@ -22,19 +22,21 @@ #define BT_CONFIG_FILE "filter/test.conf" -static struct config * -parse_config_file(const void *filename_void) -{ - bt_bird_init(); +struct parse_config_file_arg { + struct config **cp; + const char *filename; +}; - size_t fn_size = strlen((const char *) filename_void) + 1; +static int +parse_config_file(const void *argv) +{ + const struct parse_config_file_arg *arg = argv; + size_t fn_size = strlen(arg->filename) + 1; char *filename = alloca(fn_size); - strncpy(filename, filename_void, fn_size); - - struct config *c = bt_config_file_parse(filename); - bt_bird_cleanup(); - - return c; + strncpy(filename, arg->filename, fn_size); + + *(arg->cp) = bt_config_file_parse(filename); + return !!*(arg->cp); } static int @@ -68,13 +70,18 @@ int main(int argc, char *argv[]) { bt_init(argc, argv); + bt_bird_init(); + + bt_assert_hook = bt_assert_filter; - struct config *c = parse_config_file(BT_CONFIG_FILE); + struct config *c = NULL; + struct parse_config_file_arg pcfa = { .cp = &c, .filename = BT_CONFIG_FILE }; + bt_test_suite_base(parse_config_file, "conf", (const void *) &pcfa, 0, 0, "parse config file"); + + bt_bird_cleanup(); if (c) { - bt_assert_hook = bt_assert_filter; - struct f_bt_test_suite *t; WALK_LIST(t, c->tests) bt_test_suite_base(run_function, t->fn_name, t->fn, BT_FORKING, BT_TIMEOUT, "%s", t->dsc); diff --git a/filter/test.conf b/filter/test.conf index 39b349cc..e6b6ca4e 100644 --- a/filter/test.conf +++ b/filter/test.conf @@ -1336,7 +1336,7 @@ bt_test_suite(t_mixed_prefix, "Testing mixed net types"); filter vpn_filter { - bt_assert(format(net) = "0:1:2 10.1.10.0/24"); + bt_assert(format(net) = "1:2 10.1.10.0/24"); bt_assert(net.type = NET_VPN4); bt_assert(net.type != NET_IP4); bt_assert(net.type != NET_IP6); @@ -1347,6 +1347,15 @@ filter vpn_filter NET_IP6: print "IPV6"; } +# aoiufahkejtrhaweifdsbhydkfj,ysdnm + + bt_check_assign(from, 10.20.30.40); + bt_check_assign(gw, 55.55.55.44); + + bgp_community.add((3,5)); + bgp_ext_community.add((ro, 135, 999)); + bgp_large_community.add((6464156, 89646354, 8675643)); + accept; } diff --git a/nest/config.Y b/nest/config.Y index 51fb0bd7..fb75c593 100644 --- a/nest/config.Y +++ b/nest/config.Y @@ -88,7 +88,7 @@ CF_ENUM(T_ENUM_ROA, ROA_, UNKNOWN, VALID, INVALID) %type idval %type imexport %type rtable -%type optsym +%type optproto sym_proto_or_template %type r_args %type sym_args %type proto_start echo_mask echo_size debug_mask debug_list debug_flag mrtdump_mask mrtdump_list mrtdump_flag export_mode limit_action net_type table_sorted tos password_algorithm @@ -114,7 +114,7 @@ idval: NUM { $$ = $1; } | '(' term ')' { $$ = f_eval_int(f_postfixify($2)); } | IP4 { $$ = ip4_to_u32($1); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class == (SYM_CONSTANT | T_INT) || $1->class == (SYM_CONSTANT | T_QUAD)) $$ = SYM_VAL($1).i; else if (($1->class == (SYM_CONSTANT | T_IP)) && ipa_is_ip4(SYM_VAL($1).ip)) @@ -156,7 +156,7 @@ table_sorted: | SORTED { $$ = 1; } ; -table: net_type TABLE SYM table_sorted { +table: net_type TABLE CF_SYM_VOID table_sorted { struct rtable_config *cf; cf = rt_new_table($3, $1); cf->sorted = $4; @@ -173,6 +173,8 @@ proto_start: | TEMPLATE { $$ = SYM_TEMPLATE; } ; +sym_proto_or_template: CF_SYM_PROTO | CF_SYM_TEMPLATE ; + proto_name: /* EMPTY */ { struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter); @@ -180,11 +182,11 @@ proto_name: s->def = this_proto; this_proto->name = s->name; } - | SYM { + | CF_SYM_VOID { cf_define_symbol($1, this_proto->class, this_proto); this_proto->name = $1->name; } - | FROM SYM { + | FROM sym_proto_or_template { struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter); s->class = this_proto->class; s->def = this_proto; @@ -193,7 +195,7 @@ proto_name: if (($2->class != SYM_TEMPLATE) && ($2->class != SYM_PROTO)) cf_error("Template or protocol name expected"); proto_copy_config(this_proto, $2->def); } - | SYM FROM SYM { + | CF_SYM_VOID FROM sym_proto_or_template { cf_define_symbol($1, this_proto->class, this_proto); this_proto->name = $1->name; @@ -254,12 +256,7 @@ channel_end: proto_channel: channel_start channel_opt_list channel_end; -rtable: - SYM { - if ($1->class != SYM_TABLE) cf_error("Table expected"); - $$ = $1->def; - } - ; +rtable: CF_SYM_TABLE { $$ = $1->def; } ; imexport: FILTER filter { $$ = $2; } @@ -512,8 +509,8 @@ CF_CLI(SHOW PROTOCOLS, proto_patt2, [ | \"\"], [[Show routing CF_CLI(SHOW PROTOCOLS ALL, proto_patt2, [ | \"\"], [[Show routing protocol details]]) { proto_apply_cmd($4, proto_cmd_show, 0, 1); } ; -optsym: - SYM +optproto: + CF_SYM_PROTO | /* empty */ { $$ = NULL; } ; @@ -545,9 +542,8 @@ r_args: $$->show_for = 1; $$->addr = $3; } - | r_args TABLE SYM { + | r_args TABLE CF_SYM_TABLE { $$ = $1; - if ($3->class != SYM_TABLE) cf_error("%s is not a table", $3->name); rt_show_add_table($$, ((struct rtable_config *)$3->def)->table); $$->tables_defined_by = RSD_TDB_DIRECT; } @@ -558,10 +554,10 @@ r_args: rt_show_add_table($$, t->table); $$->tables_defined_by = RSD_TDB_ALL; } - | r_args IMPORT TABLE SYM '.' r_args_channel { + | r_args IMPORT TABLE CF_SYM_PROTO '.' r_args_channel { $$ = $1; struct proto_config *cf = (void *) $4->def; - if ($4->class != SYM_PROTO || !cf->proto) cf_error("%s is not a protocol", $4->name); + if (!cf->proto) cf_error("%s is not a protocol", $4->name); struct channel *c = proto_find_channel_by_name(cf->proto, $6); if (!c) cf_error("Channel %s.%s not found", $4->name, $6); if (!c->in_table) cf_error("No import table in channel %s.%s", $4->name, $6); @@ -590,30 +586,30 @@ r_args: $$ = $1; $$->filtered = 1; } - | r_args export_mode SYM { + | r_args export_mode CF_SYM_PROTO { struct proto_config *c = (struct proto_config *) $3->def; $$ = $1; if ($$->export_mode) cf_error("Export specified twice"); - if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name); + if (!c->proto) cf_error("%s is not a protocol", $3->name); $$->export_mode = $2; $$->export_protocol = c->proto; $$->tables_defined_by = RSD_TDB_INDIRECT; } - | r_args export_mode SYM '.' r_args_channel { + | r_args export_mode CF_SYM_PROTO '.' r_args_channel { struct proto_config *c = (struct proto_config *) $3->def; $$ = $1; if ($$->export_mode) cf_error("Export specified twice"); - if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name); + if (!c->proto) cf_error("%s is not a protocol", $3->name); $$->export_mode = $2; $$->export_channel = proto_find_channel_by_name(c->proto, $5); if (!$$->export_channel) cf_error("Export channel not found"); $$->tables_defined_by = RSD_TDB_INDIRECT; } - | r_args PROTOCOL SYM { + | r_args PROTOCOL CF_SYM_PROTO { struct proto_config *c = (struct proto_config *) $3->def; $$ = $1; if ($$->show_protocol) cf_error("Protocol specified twice"); - if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name); + if (!c->proto) cf_error("%s is not a protocol", $3->name); $$->show_protocol = c->proto; $$->tables_defined_by = RSD_TDB_INDIRECT; } @@ -647,7 +643,7 @@ r_args_for: $$ = cfg_alloc(sizeof(net_addr_ip6_sadr)); net_fill_ip6_sadr($$, $1, IP6_MAX_PREFIX_LENGTH, $3, IP6_MAX_PREFIX_LENGTH); } - | SYM { + | CF_SYM_CONSTANT { if ($1->class == (SYM_CONSTANT | T_IP)) { $$ = cfg_alloc(ipa_is_ip4(SYM_VAL($1).ip) ? sizeof(net_addr_ip4) : sizeof(net_addr_ip6)); @@ -709,7 +705,7 @@ sym_args: | sym_args FILTER { $$ = $1; $$->type = SYM_FILTER; } | sym_args PROTOCOL { $$ = $1; $$->type = SYM_PROTO; } | sym_args TEMPLATE { $$ = $1; $$->type = SYM_TEMPLATE; } - | sym_args SYM { $$ = $1; $$->sym = $2; } + | sym_args symbol { $$ = $1; $$->sym = $2; } ; @@ -779,13 +775,13 @@ CF_CLI(RESTRICT,,,[[Restrict current CLI session to safe commands]]) { this_cli->restricted = 1; cli_msg(16, "Access restricted"); } ; proto_patt: - SYM { $$.ptr = $1; $$.patt = 0; } + CF_SYM_PROTO { $$.ptr = $1; $$.patt = 0; } | ALL { $$.ptr = NULL; $$.patt = 1; } | TEXT { $$.ptr = $1; $$.patt = 1; } ; proto_patt2: - SYM { $$.ptr = $1; $$.patt = 0; } + CF_SYM_PROTO { $$.ptr = $1; $$.patt = 0; } | { $$.ptr = NULL; $$.patt = 1; } | TEXT { $$.ptr = $1; $$.patt = 1; } ; diff --git a/proto/babel/config.Y b/proto/babel/config.Y index b93a423b..78175323 100644 --- a/proto/babel/config.Y +++ b/proto/babel/config.Y @@ -129,16 +129,16 @@ dynamic_attr: BABEL_METRIC { $$ = f_new_dynamic_attr(EAF_TYPE_INT, 0, T_INT, EA_ CF_CLI_HELP(SHOW BABEL, ..., [[Show information about Babel protocol]]); -CF_CLI(SHOW BABEL INTERFACES, optsym opttext, [] [\"\"], [[Show information about Babel interfaces]]) +CF_CLI(SHOW BABEL INTERFACES, optproto opttext, [] [\"\"], [[Show information about Babel interfaces]]) { babel_show_interfaces(proto_get_named($4, &proto_babel), $5); }; -CF_CLI(SHOW BABEL NEIGHBORS, optsym opttext, [] [\"\"], [[Show information about Babel neighbors]]) +CF_CLI(SHOW BABEL NEIGHBORS, optproto opttext, [] [\"\"], [[Show information about Babel neighbors]]) { babel_show_neighbors(proto_get_named($4, &proto_babel), $5); }; -CF_CLI(SHOW BABEL ENTRIES, optsym opttext, [], [[Show information about Babel prefix entries]]) +CF_CLI(SHOW BABEL ENTRIES, optproto opttext, [], [[Show information about Babel prefix entries]]) { babel_show_entries(proto_get_named($4, &proto_babel)); }; -CF_CLI(SHOW BABEL ROUTES, optsym opttext, [], [[Show information about Babel route entries]]) +CF_CLI(SHOW BABEL ROUTES, optproto opttext, [], [[Show information about Babel route entries]]) { babel_show_routes(proto_get_named($4, &proto_babel)); }; CF_CODE diff --git a/proto/bfd/config.Y b/proto/bfd/config.Y index 3f5714fd..41228e51 100644 --- a/proto/bfd/config.Y +++ b/proto/bfd/config.Y @@ -134,7 +134,7 @@ bfd_multihop: bfd_neigh_iface: /* empty */ { $$ = NULL; } - | '%' SYM { $$ = if_get_by_name($2->name); } + | '%' symbol { $$ = if_get_by_name($2->name); } | DEV text { $$ = if_get_by_name($2); } ; @@ -167,7 +167,7 @@ bfd_neighbor: ipa bfd_neigh_iface bfd_neigh_local bfd_neigh_multihop CF_CLI_HELP(SHOW BFD, ..., [[Show information about BFD protocol]]); -CF_CLI(SHOW BFD SESSIONS, optsym, [], [[Show information about BFD sessions]]) +CF_CLI(SHOW BFD SESSIONS, optproto, [], [[Show information about BFD sessions]]) { bfd_show_sessions(proto_get_named($4, &proto_bfd)); }; CF_CODE diff --git a/proto/ospf/config.Y b/proto/ospf/config.Y index 9669b708..38b09deb 100644 --- a/proto/ospf/config.Y +++ b/proto/ospf/config.Y @@ -504,29 +504,29 @@ dynamic_attr: OSPF_TAG { $$ = f_new_dynamic_attr(EAF_TYPE_INT, 0, T_INT, EA_OSPF dynamic_attr: OSPF_ROUTER_ID { $$ = f_new_dynamic_attr(EAF_TYPE_ROUTER_ID, 0, T_QUAD, EA_OSPF_ROUTER_ID); } ; CF_CLI_HELP(SHOW OSPF, ..., [[Show information about OSPF protocol]]); -CF_CLI(SHOW OSPF, optsym, [], [[Show information about OSPF protocol]]) +CF_CLI(SHOW OSPF, optproto, [], [[Show information about OSPF protocol]]) { ospf_sh(proto_get_named($3, &proto_ospf)); }; -CF_CLI(SHOW OSPF NEIGHBORS, optsym opttext, [] [\"\"], [[Show information about OSPF neighbors]]) +CF_CLI(SHOW OSPF NEIGHBORS, optproto opttext, [] [\"\"], [[Show information about OSPF neighbors]]) { ospf_sh_neigh(proto_get_named($4, &proto_ospf), $5); }; -CF_CLI(SHOW OSPF INTERFACE, optsym opttext, [] [\"\"], [[Show information about interface]]) +CF_CLI(SHOW OSPF INTERFACE, optproto opttext, [] [\"\"], [[Show information about interface]]) { ospf_sh_iface(proto_get_named($4, &proto_ospf), $5); }; CF_CLI_HELP(SHOW OSPF TOPOLOGY, [all] [], [[Show information about OSPF network topology]]) -CF_CLI(SHOW OSPF TOPOLOGY, optsym opttext, [], [[Show information about reachable OSPF network topology]]) +CF_CLI(SHOW OSPF TOPOLOGY, optproto opttext, [], [[Show information about reachable OSPF network topology]]) { ospf_sh_state(proto_get_named($4, &proto_ospf), 0, 1); }; -CF_CLI(SHOW OSPF TOPOLOGY ALL, optsym opttext, [], [[Show information about all OSPF network topology]]) +CF_CLI(SHOW OSPF TOPOLOGY ALL, optproto opttext, [], [[Show information about all OSPF network topology]]) { ospf_sh_state(proto_get_named($5, &proto_ospf), 0, 0); }; CF_CLI_HELP(SHOW OSPF STATE, [all] [], [[Show information about OSPF network state]]) -CF_CLI(SHOW OSPF STATE, optsym opttext, [], [[Show information about reachable OSPF network state]]) +CF_CLI(SHOW OSPF STATE, optproto opttext, [], [[Show information about reachable OSPF network state]]) { ospf_sh_state(proto_get_named($4, &proto_ospf), 1, 1); }; -CF_CLI(SHOW OSPF STATE ALL, optsym opttext, [], [[Show information about all OSPF network state]]) +CF_CLI(SHOW OSPF STATE ALL, optproto opttext, [], [[Show information about all OSPF network state]]) { ospf_sh_state(proto_get_named($5, &proto_ospf), 1, 0); }; CF_CLI_HELP(SHOW OSPF LSADB, ..., [[Show content of OSPF LSA database]]); @@ -544,7 +544,7 @@ lsadb_args: | lsadb_args LSID idval { $$ = $1; $$->lsid = $3; } | lsadb_args SELF { $$ = $1; $$->router = SH_ROUTER_SELF; } | lsadb_args ROUTER idval { $$ = $1; $$->router = $3; } - | lsadb_args SYM { $$ = $1; $$->name = $2; } + | lsadb_args CF_SYM_PROTO { $$ = $1; $$->proto = (struct ospf_proto *) proto_get_named($2, &proto_ospf); } ; CF_CODE diff --git a/proto/ospf/ospf.c b/proto/ospf/ospf.c index ef2a0df4..816f33aa 100644 --- a/proto/ospf/ospf.c +++ b/proto/ospf/ospf.c @@ -1402,7 +1402,7 @@ lsa_compare_for_lsadb(const void *p1, const void *p2) void ospf_sh_lsadb(struct lsadb_show_data *ld) { - struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf); + struct ospf_proto *p = ld->proto; uint num = p->gr->hash_entries; uint i, j; int last_dscope = -1; diff --git a/proto/ospf/ospf.h b/proto/ospf/ospf.h index 7fac47c8..82ae4df4 100644 --- a/proto/ospf/ospf.h +++ b/proto/ospf/ospf.h @@ -900,7 +900,7 @@ struct ospf_lsreq_header #define SH_ROUTER_SELF 0xffffffff struct lsadb_show_data { - struct symbol *name; /* Protocol to request data from */ + struct ospf_proto *proto; /* Protocol to request data from */ u16 type; /* LSA Type, 0 -> all */ u16 scope; /* Scope, 0 -> all, hack to handle link scope as 1 */ u32 area; /* Specified for area scope */ diff --git a/proto/rip/config.Y b/proto/rip/config.Y index 265912b2..4ab793d1 100644 --- a/proto/rip/config.Y +++ b/proto/rip/config.Y @@ -191,10 +191,10 @@ dynamic_attr: RIP_TAG { $$ = f_new_dynamic_attr(EAF_TYPE_INT, 0, T_INT, EA_RIP_T CF_CLI_HELP(SHOW RIP, ..., [[Show information about RIP protocol]]); -CF_CLI(SHOW RIP INTERFACES, optsym opttext, [] [\"\"], [[Show information about RIP interfaces]]) +CF_CLI(SHOW RIP INTERFACES, optproto opttext, [] [\"\"], [[Show information about RIP interfaces]]) { rip_show_interfaces(proto_get_named($4, &proto_rip), $5); }; -CF_CLI(SHOW RIP NEIGHBORS, optsym opttext, [] [\"\"], [[Show information about RIP neighbors]]) +CF_CLI(SHOW RIP NEIGHBORS, optproto opttext, [] [\"\"], [[Show information about RIP neighbors]]) { rip_show_neighbors(proto_get_named($4, &proto_rip), $5); }; diff --git a/proto/static/config.Y b/proto/static/config.Y index 527046ee..0e53c978 100644 --- a/proto/static/config.Y +++ b/proto/static/config.Y @@ -157,7 +157,7 @@ stat_route_opt_list: ; -CF_CLI(SHOW STATIC, optsym, [], [[Show details of static protocol]]) +CF_CLI(SHOW STATIC, optproto, [], [[Show details of static protocol]]) { static_show(proto_get_named($3, &proto_static)); } ; CF_CODE -- cgit v1.2.3 From 0b39b1cbb70c6f37a30a3130e1c308ddd0ba36de Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Fri, 15 Feb 2019 13:53:17 +0100 Subject: Conf: Symbol implementation converted from void pointers to union ... and consted some declarations. --- conf/cf-lex.l | 59 +++++++++++++++++++++++------------------------------- conf/conf.h | 49 +++++++++++++++++++++++++++++++++++++-------- conf/confbase.Y | 5 +++-- filter/config.Y | 53 ++++++++++++++++++++++++++---------------------- filter/decl.m4 | 4 ++-- filter/f-inst.c | 10 ++++----- filter/f-inst.h | 5 +++-- filter/f-util.c | 4 ++-- filter/filter.c | 5 ++--- filter/filter.h | 6 +++--- nest/config.Y | 24 +++++++++++----------- nest/proto.c | 6 +++--- nest/protocol.h | 6 +++--- nest/route.h | 4 ++-- nest/rt-table.c | 14 ++++++------- proto/mrt/mrt.h | 6 +++--- sysdep/unix/krt.c | 2 +- sysdep/unix/main.c | 8 +++----- 18 files changed, 149 insertions(+), 121 deletions(-) (limited to 'conf/conf.h') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 6cc09425..f1b402a0 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -539,11 +539,8 @@ cf_new_symbol(byte *c) if (l > SYM_MAX_LEN) cf_error("Symbol too long"); - s = cfg_alloc(sizeof(struct symbol) + l); - s->scope = conf_this_scope; - s->class = SYM_VOID; - s->def = NULL; - s->aux = 0; + s = cfg_allocz(sizeof(struct symbol) + l + 1); + *s = (struct symbol) { .scope = conf_this_scope, .class = SYM_VOID, }; strcpy(s->name, c); if (!new_config->sym_hash.data) @@ -574,6 +571,7 @@ cf_find_symbol(struct config *cfg, byte *c) (s = HASH_FIND(cfg->sym_hash, SYM, c, 1))) return s; + /* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */ if (cfg->fallback && cfg->fallback->sym_hash.data && (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1))) @@ -597,6 +595,28 @@ cf_get_symbol(byte *c) return cf_find_symbol(new_config, c) ?: cf_new_symbol(c); } +/** + * cf_localize_symbol - get the local instance of given symbol + * @sym: the symbol to localize + * + * This functions finds the symbol that is local to current scope + * for purposes of cf_define_symbol(). + */ +struct symbol * +cf_localize_symbol(struct symbol *sym) +{ + /* If the symbol type is void, it has been recently allocated just in this scope. */ + if (!sym->class) + return sym; + + /* If the scope is the current, it is already defined in this scope. */ + if (sym->scope == conf_this_scope) + cf_error("Symbol already defined"); + + /* Not allocated here yet, doing it now. */ + return cf_new_symbol(sym->name); +} + struct symbol * cf_default_name(char *template, int *counter) { @@ -616,35 +636,6 @@ cf_default_name(char *template, int *counter) cf_error("Unable to generate default name"); } -/** - * cf_define_symbol - define meaning of a symbol - * @sym: symbol to be defined - * @type: symbol class to assign - * @def: class dependent data - * - * Defines new meaning of a symbol. If the symbol is an undefined - * one (%SYM_VOID), it's just re-defined to the new type. If it's defined - * in different scope, a new symbol in current scope is created and the - * meaning is assigned to it. If it's already defined in the current scope, - * an error is reported via cf_error(). - * - * Result: Pointer to the newly defined symbol. If we are in the top-level - * scope, it's the same @sym as passed to the function. - */ -struct symbol * -cf_define_symbol(struct symbol *sym, int type, void *def) -{ - if (sym->class) - { - if (sym->scope == conf_this_scope) - cf_error("Symbol already defined"); - sym = cf_new_symbol(sym->name); - } - sym->class = type; - sym->def = def; - return sym; -} - static void cf_lex_init_kh(void) { diff --git a/conf/conf.h b/conf/conf.h index 4e3addb3..21d4f1e2 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -105,11 +105,19 @@ extern int (*cf_read_hook)(byte *buf, uint max, int fd); struct symbol { struct symbol *next; struct sym_scope *scope; - int class; - int aux; - uint aux2; - void *def; - char name[1]; + int class; /* SYM_* */ + uint flags; /* SYM_FLAG_* */ + + union { + struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */ + const struct f_line *function; /* For SYM_FUNCTION */ + const struct filter *filter; /* For SYM_FILTER */ + struct rtable_config *table; /* For SYM_TABLE */ + struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */ + struct f_val *val; /* For SYM_CONSTANT or SYM_VARIABLE */ + }; + + char name[0]; }; struct sym_scope { @@ -134,8 +142,11 @@ struct sym_scope { #define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */ #define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff) -#define SYM_TYPE(s) (((struct f_val *) (s)->def)->type) -#define SYM_VAL(s) (((struct f_val *) (s)->def)->val) +#define SYM_TYPE(s) ((s)->val->type) +#define SYM_VAL(s) ((s)->val->val) + +/* Symbol flags */ +#define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */ struct include_file_stack { void *buffer; /* Internal lexer state */ @@ -160,7 +171,29 @@ struct symbol *cf_find_symbol(struct config *cfg, byte *c); struct symbol *cf_get_symbol(byte *c); struct symbol *cf_default_name(char *template, int *counter); -struct symbol *cf_define_symbol(struct symbol *symbol, int type, void *def); +struct symbol *cf_localize_symbol(struct symbol *sym); + +/** + * cf_define_symbol - define meaning of a symbol + * @sym: symbol to be defined + * @type: symbol class to assign + * @def: class dependent data + * + * Defines new meaning of a symbol. If the symbol is an undefined + * one (%SYM_VOID), it's just re-defined to the new type. If it's defined + * in different scope, a new symbol in current scope is created and the + * meaning is assigned to it. If it's already defined in the current scope, + * an error is reported via cf_error(). + * + * Result: Pointer to the newly defined symbol. If we are in the top-level + * scope, it's the same @sym as passed to the function. + */ +#define cf_define_symbol(sym_, type_, var_, def_) ({ \ + struct symbol *sym = cf_localize_symbol(sym_); \ + sym->class = type_; \ + sym->var_ = def_; \ + sym; }) + void cf_push_scope(struct symbol *); void cf_pop_scope(void); char *cf_symbol_class_name(struct symbol *sym); diff --git a/conf/confbase.Y b/conf/confbase.Y index 2195e8fc..62415b4c 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -56,7 +56,8 @@ CF_DECLS struct f_dynamic_attr fda; struct f_static_attr fsa; struct f_lval flv; - struct filter *f; + const struct f_line *fl; + const struct filter *f; struct f_tree *e; struct f_trie *trie; struct f_val v; @@ -130,7 +131,7 @@ definition: DEFINE CF_SYM_VOID '=' term ';' { struct f_val *val = cfg_alloc(sizeof(struct f_val)); if (f_eval(f_postfixify($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error"); - cf_define_symbol($2, SYM_CONSTANT | val->type, val); + cf_define_symbol($2, SYM_CONSTANT | val->type, val, val); } ; diff --git a/filter/config.Y b/filter/config.Y index 488b9ced..b3a04958 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -451,7 +451,8 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, %type term block cmd cmds constant constructor print_one print_list var_list var_listn function_call symbol_value bgp_path_expr bgp_path bgp_path_tail one_decl decls %type dynamic_attr %type static_attr -%type filter filter_body where_filter +%type filter where_filter +%type filter_body %type lvalue %type type %type ec_kind @@ -467,11 +468,12 @@ CF_GRAMMAR conf: filter_def ; filter_def: - FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, NULL); cf_push_scope( $2 ); } + FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); } filter_body { - $2->def = $4; - $4->name = $2->name; - DBG( "We have new filter defined (%s)\n", $2->name ); + struct filter *f = cfg_alloc(sizeof(struct filter)); + *f = (struct filter) { .name = $2->name, .root = $4 }; + $2->filter = f; + cf_pop_scope(); } ; @@ -483,14 +485,14 @@ filter_eval: conf: custom_attr ; custom_attr: ATTRIBUTE type CF_SYM_VOID ';' { - cf_define_symbol($3, SYM_ATTRIBUTE, ca_lookup(new_config->pool, $3->name, $2)->fda); + cf_define_symbol($3, SYM_ATTRIBUTE, attribute, ca_lookup(new_config->pool, $3->name, $2)->fda); }; conf: bt_test_suite ; bt_test_suite: BT_TEST_SUITE '(' CF_SYM_FUNCTION ',' text ')' { struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite)); - t->fn = $3->def; + t->fn = $3->function; t->fn_name = $3->name; t->dsc = $5; @@ -502,8 +504,8 @@ conf: bt_test_same ; bt_test_same: BT_TEST_SAME '(' CF_SYM_FUNCTION ',' CF_SYM_FUNCTION ',' NUM ')' { struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite)); - t->fn = $3->def; - t->cmp = $5->def; + t->fn = $3->function; + t->cmp = $5->function; t->result = $7; t->fn_name = $3->name; t->dsc = $5->name; @@ -553,7 +555,7 @@ one_decl: type CF_SYM_VOID { struct f_val * val = cfg_alloc(sizeof(struct f_val)); val->type = T_VOID; - $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val); + $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val, val); DBG( "New variable %s type %x\n", $2->name, $1 ); $$ = f_new_inst(FI_SET, NULL, $2); } @@ -578,22 +580,24 @@ declsn: one_decl { $$.inst = $1; $$.count = 1; } filter_body: function_body { - $$ = cfg_alloc(sizeof(struct filter)); - $$->name = NULL; if ($1[0]) { const struct f_inst *inst[2] = { $1[0], $1[1] }; - $$->root = f_postfixify_concat(inst, 2); + $$ = f_postfixify_concat(inst, 2); } else - $$->root = f_postfixify($1[1]); + $$ = f_postfixify($1[1]); } ; filter: CF_SYM_FILTER { - $$ = $1->def; + $$ = $1->filter; + } + | filter_body { + struct filter *f = cfg_alloc(sizeof(struct filter)); + *f = (struct filter) { .root = $1 }; + $$ = f; } - | filter_body ; where_filter: @@ -618,7 +622,7 @@ function_body: conf: function_def ; function_def: FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name ); - $2 = cf_define_symbol($2, SYM_FUNCTION, NULL); + $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL); cf_push_scope($2); } function_params function_body { const struct f_inst *catlist[4]; @@ -639,9 +643,10 @@ function_def: if ($5[1]) catlist[count++] = $5[1]; - $2->def = f_postfixify_concat(catlist, count); - $2->aux2 = $4.count; - DBG("Hmm, we've got one function here - %s\n", $2->name); + struct f_line *fl = f_postfixify_concat(catlist, count); + fl->args = $4.count; + $2->function = fl; + cf_pop_scope(); } ; @@ -693,7 +698,7 @@ set_atom: } | CF_SYM_CONSTANT { if (!f_valid_set_type(SYM_TYPE($1))) cf_error("%s: set-incompatible type", $1->name); - $$ = *(struct f_val *)($1->def); + $$ = *$1->val; } ; @@ -856,9 +861,9 @@ function_call: ; symbol_value: - CF_SYM_CONSTANT { $$ = f_new_inst(FI_CONSTANT_INDIRECT, $1->def); } + CF_SYM_CONSTANT { $$ = f_new_inst(FI_CONSTANT_INDIRECT, $1->val); } | CF_SYM_VARIABLE { $$ = f_new_inst(FI_VARIABLE, $1); } - | CF_SYM_ATTRIBUTE { $$ = f_new_inst(FI_EA_GET, *((struct f_dynamic_attr *) $1->def)); } + | CF_SYM_ATTRIBUTE { $$ = f_new_inst(FI_EA_GET, *$1->attribute); } ; static_attr: @@ -986,7 +991,7 @@ cmd: $$ = f_new_inst(FI_CONDITION, $2, $4, $6); } | CF_SYM_ATTRIBUTE '=' term ';' { - $$ = f_new_inst(FI_EA_SET, $3, *((struct f_dynamic_attr *) $1->def)); + $$ = f_new_inst(FI_EA_SET, $3, *$1->attribute); } | CF_SYM_VARIABLE '=' term ';' { $$ = f_new_inst(FI_SET, $3, $1); diff --git a/filter/decl.m4 b/filter/decl.m4 index 5ac62cbd..3043f4fc 100644 --- a/filter/decl.m4 +++ b/filter/decl.m4 @@ -188,9 +188,9 @@ const struct symbol *sym; FID_NEW_ARGS , const struct symbol *sym FID_NEW_BODY -what->valp = (what->sym = sym)->def; +what->valp = (what->sym = sym)->val; FID_POSTFIXIFY_BODY -dest->items[pos].vp = (dest->items[pos].sym = what->sym)->def; +dest->items[pos].vp = (dest->items[pos].sym = what->sym)->val; FID_SAME_BODY if (strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)) return 0; FID_DUMP_BODY diff --git a/filter/f-inst.c b/filter/f-inst.c index 4ab46529..6f563873 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -233,7 +233,7 @@ /* IP->Quad implicit conversion */ if ((sym->class == (SYM_VARIABLE | T_QUAD)) && val_is_ip4(&v1)) { - *((struct f_val *) sym->def) = (struct f_val) { + *(sym->val) = (struct f_val) { .type = T_QUAD, .val.i = ipa_to_u32(v1.val.ip), }; @@ -241,7 +241,7 @@ } runtime( "Assigning to variable of incompatible type" ); } - *((struct f_val *) sym->def) = v1; + *(sym->val) = v1; } /* some constants have value in a[1], some in *a[0].p, strange. */ @@ -709,7 +709,7 @@ /* Postfixify extracts the function body from the symbol */ FID_POSTFIXIFY_BODY - dest->items[pos].lines[0] = what->sym->def; + dest->items[pos].lines[0] = what->sym->function; FID_END /* First push the body on stack */ @@ -727,8 +727,8 @@ for (const struct f_inst *inst = f1; inst; inst = inst->next) count++; - if (count != sym->aux2) - cf_error("Function %s takes %u arguments, got %u.", sym->name, sym->aux2, count); + if (count != sym->function->args) + cf_error("Function %s takes %u arguments, got %u.", sym->name, sym->function->args, count); FID_END /* FIXME: Optimization of function comparison. */ diff --git a/filter/f-inst.h b/filter/f-inst.h index e1d0b675..201be5f8 100644 --- a/filter/f-inst.h +++ b/filter/f-inst.h @@ -59,6 +59,7 @@ struct f_line_item { /* Line of instructions to be unconditionally executed one after another */ struct f_line { uint len; /* Line length */ + u16 args; /* Function: Args required */ struct f_line_item items[0]; /* The items themselves */ }; @@ -81,8 +82,8 @@ extern void (*bt_assert_hook)(int result, const struct f_line_item *assert); /* Bird Tests */ struct f_bt_test_suite { node n; /* Node in config->tests */ - struct f_line *fn; /* Root of function */ - struct f_line *cmp; /* Compare to this function */ + const struct f_line *fn; /* Root of function */ + const struct f_line *cmp; /* Compare to this function */ const char *fn_name; /* Name of test */ const char *dsc; /* Description */ int result; /* Desired result */ diff --git a/filter/f-util.c b/filter/f-util.c index 82aaa385..85f5d1c4 100644 --- a/filter/f-util.c +++ b/filter/f-util.c @@ -17,8 +17,8 @@ #define P(a,b) ((a<<8) | b) -char * -filter_name(struct filter *filter) +const char * +filter_name(const struct filter *filter) { if (!filter) return "ACCEPT"; diff --git a/filter/filter.c b/filter/filter.c index 29e78204..4249d4ee 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -359,15 +359,14 @@ f_eval_buf(const struct f_line *expr, struct linpool *tmp_pool, buffer *buf) /** * filter_same - compare two filters * @new: first filter to be compared - * @old: second filter to be compared, notice that this filter is - * damaged while comparing. + * @old: second filter to be compared * * Returns 1 in case filters are same, otherwise 0. If there are * underlying bugs, it will rather say 0 on same filters than say * 1 on different. */ int -filter_same(struct filter *new, struct filter *old) +filter_same(const struct filter *new, const struct filter *old) { if (old == new) /* Handle FILTER_ACCEPT and FILTER_REJECT */ return 1; diff --git a/filter/filter.h b/filter/filter.h index d03c6438..26faeaa3 100644 --- a/filter/filter.h +++ b/filter/filter.h @@ -48,7 +48,7 @@ struct f_val; struct f_line; struct filter { char *name; - struct f_line *root; + const struct f_line *root; }; struct rte; @@ -58,8 +58,8 @@ enum filter_return f_eval_rte(const struct f_line *expr, struct rte **rte, struc uint f_eval_int(const struct f_line *expr); enum filter_return f_eval_buf(const struct f_line *expr, struct linpool *tmp_pool, buffer *buf); -char *filter_name(struct filter *filter); -int filter_same(struct filter *new, struct filter *old); +const char *filter_name(const struct filter *filter); +int filter_same(const struct filter *new, const struct filter *old); int f_same(const struct f_line *f1, const struct f_line *f2); #define FILTER_ACCEPT NULL diff --git a/nest/config.Y b/nest/config.Y index fb75c593..c2622ed2 100644 --- a/nest/config.Y +++ b/nest/config.Y @@ -179,28 +179,28 @@ proto_name: /* EMPTY */ { struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter); s->class = this_proto->class; - s->def = this_proto; + s->proto = this_proto; this_proto->name = s->name; } | CF_SYM_VOID { - cf_define_symbol($1, this_proto->class, this_proto); + cf_define_symbol($1, this_proto->class, proto, this_proto); this_proto->name = $1->name; } | FROM sym_proto_or_template { struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter); s->class = this_proto->class; - s->def = this_proto; + s->proto = this_proto; this_proto->name = s->name; if (($2->class != SYM_TEMPLATE) && ($2->class != SYM_PROTO)) cf_error("Template or protocol name expected"); - proto_copy_config(this_proto, $2->def); + proto_copy_config(this_proto, $2->proto); } | CF_SYM_VOID FROM sym_proto_or_template { - cf_define_symbol($1, this_proto->class, this_proto); + cf_define_symbol($1, this_proto->class, proto, this_proto); this_proto->name = $1->name; if (($3->class != SYM_TEMPLATE) && ($3->class != SYM_PROTO)) cf_error("Template or protocol name expected"); - proto_copy_config(this_proto, $3->def); + proto_copy_config(this_proto, $3->proto); } ; @@ -256,7 +256,7 @@ channel_end: proto_channel: channel_start channel_opt_list channel_end; -rtable: CF_SYM_TABLE { $$ = $1->def; } ; +rtable: CF_SYM_TABLE { $$ = $1->table; } ; imexport: FILTER filter { $$ = $2; } @@ -544,7 +544,7 @@ r_args: } | r_args TABLE CF_SYM_TABLE { $$ = $1; - rt_show_add_table($$, ((struct rtable_config *)$3->def)->table); + rt_show_add_table($$, $3->table->table); $$->tables_defined_by = RSD_TDB_DIRECT; } | r_args TABLE ALL { @@ -556,7 +556,7 @@ r_args: } | r_args IMPORT TABLE CF_SYM_PROTO '.' r_args_channel { $$ = $1; - struct proto_config *cf = (void *) $4->def; + struct proto_config *cf = $4->proto; if (!cf->proto) cf_error("%s is not a protocol", $4->name); struct channel *c = proto_find_channel_by_name(cf->proto, $6); if (!c) cf_error("Channel %s.%s not found", $4->name, $6); @@ -587,7 +587,7 @@ r_args: $$->filtered = 1; } | r_args export_mode CF_SYM_PROTO { - struct proto_config *c = (struct proto_config *) $3->def; + struct proto_config *c = (struct proto_config *) $3->proto; $$ = $1; if ($$->export_mode) cf_error("Export specified twice"); if (!c->proto) cf_error("%s is not a protocol", $3->name); @@ -596,7 +596,7 @@ r_args: $$->tables_defined_by = RSD_TDB_INDIRECT; } | r_args export_mode CF_SYM_PROTO '.' r_args_channel { - struct proto_config *c = (struct proto_config *) $3->def; + struct proto_config *c = (struct proto_config *) $3->proto; $$ = $1; if ($$->export_mode) cf_error("Export specified twice"); if (!c->proto) cf_error("%s is not a protocol", $3->name); @@ -606,7 +606,7 @@ r_args: $$->tables_defined_by = RSD_TDB_INDIRECT; } | r_args PROTOCOL CF_SYM_PROTO { - struct proto_config *c = (struct proto_config *) $3->def; + struct proto_config *c = (struct proto_config *) $3->proto; $$ = $1; if ($$->show_protocol) cf_error("Protocol specified twice"); if (!c->proto) cf_error("%s is not a protocol", $3->name); diff --git a/nest/proto.c b/nest/proto.c index d4a333d0..77bf082a 100644 --- a/nest/proto.c +++ b/nest/proto.c @@ -977,7 +977,7 @@ protos_commit(struct config *new, struct config *old, int force_reconfig, int ty { /* Found match, let's check if we can smoothly switch to new configuration */ /* No need to check description */ - nc = sym->def; + nc = sym->proto; nc->proto = p; /* We will try to reconfigure protocol p */ @@ -1905,7 +1905,7 @@ proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uintptr_t, return; } - cmd(((struct proto_config *)s->def)->proto, arg, 0); + cmd(s->proto->proto, arg, 0); cli_msg(0, ""); } @@ -1948,7 +1948,7 @@ proto_get_named(struct symbol *sym, struct protocol *pr) if (sym->class != SYM_PROTO) cf_error("%s: Not a protocol", sym->name); - p = ((struct proto_config *) sym->def)->proto; + p = sym->proto->proto; if (!p || p->proto != pr) cf_error("%s: Not a %s protocol", sym->name, pr->name); } diff --git a/nest/protocol.h b/nest/protocol.h index 6c04071b..82b46261 100644 --- a/nest/protocol.h +++ b/nest/protocol.h @@ -489,7 +489,7 @@ struct channel_config { struct proto_config *parent; /* Where channel is defined (proto or template) */ struct rtable_config *table; /* Table we're attached to */ - struct filter *in_filter, *out_filter; /* Attached filters */ + const struct filter *in_filter, *out_filter; /* Attached filters */ struct channel_limit rx_limit; /* Limit for receiving routes from protocol (relevant when in_keep_filtered is active) */ struct channel_limit in_limit; /* Limit for importing routes from protocol */ @@ -511,8 +511,8 @@ struct channel { struct proto *proto; struct rtable *table; - struct filter *in_filter; /* Input filter */ - struct filter *out_filter; /* Output filter */ + const struct filter *in_filter; /* Input filter */ + const struct filter *out_filter; /* Output filter */ struct channel_limit rx_limit; /* Receive limit (for in_keep_filtered) */ struct channel_limit in_limit; /* Input limit */ struct channel_limit out_limit; /* Output limit */ diff --git a/nest/route.h b/nest/route.h index c7ed80ff..7c9f3005 100644 --- a/nest/route.h +++ b/nest/route.h @@ -297,7 +297,7 @@ rte *rte_find(net *net, struct rte_src *src); rte *rte_get_temp(struct rta *); void rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src); /* rte_update() moved to protocol.h to avoid dependency conflicts */ -int rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter); +int rt_examine(rtable *t, net_addr *a, struct proto *p, const struct filter *filter); rte *rt_export_merged(struct channel *c, net *net, rte **rt_free, linpool *pool, int silent); void rt_refresh_begin(rtable *t, struct channel *c); void rt_refresh_end(rtable *t, struct channel *c); @@ -335,7 +335,7 @@ struct rt_show_data { struct rt_show_data_rtable *last_table; /* Last table in output */ struct fib_iterator fit; /* Iterator over networks in table */ int verbose, tables_defined_by; - struct filter *filter; + const struct filter *filter; struct proto *show_protocol; struct proto *export_protocol; struct channel *export_channel; diff --git a/nest/rt-table.c b/nest/rt-table.c index 6c8e662e..f05bd718 100644 --- a/nest/rt-table.c +++ b/nest/rt-table.c @@ -398,7 +398,7 @@ static rte * export_filter_(struct channel *c, rte *rt0, rte **rt_free, linpool *pool, int silent) { struct proto *p = c->proto; - struct filter *filter = c->out_filter; + const struct filter *filter = c->out_filter; struct proto_stats *stats = &c->stats; rte *rt; int v; @@ -1362,7 +1362,7 @@ rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src) { struct proto *p = c->proto; struct proto_stats *stats = &c->stats; - struct filter *filter = c->in_filter; + const struct filter *filter = c->in_filter; rte *dummy = NULL; net *nn; @@ -1503,7 +1503,7 @@ rte_modify(rte *old) /* Check rtable for best route to given net whether it would be exported do p */ int -rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter) +rt_examine(rtable *t, net_addr *a, struct proto *p, const struct filter *filter) { net *n = net_find(t, a); rte *rt = n ? n->routes : NULL; @@ -2106,13 +2106,13 @@ rt_new_table(struct symbol *s, uint addr_type) { /* Hack that allows to 'redefine' the master table */ if ((s->class == SYM_TABLE) && - (s->def == new_config->def_tables[addr_type]) && + (s->table == new_config->def_tables[addr_type]) && ((addr_type == NET_IP4) || (addr_type == NET_IP6))) - return s->def; + return s->table; struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config)); - cf_define_symbol(s, SYM_TABLE, c); + cf_define_symbol(s, SYM_TABLE, table, c); c->name = s->name; c->addr_type = addr_type; c->gc_max_ops = 1000; @@ -2171,7 +2171,7 @@ static struct rtable_config * rt_find_table_config(struct config *cf, char *name) { struct symbol *sym = cf_find_symbol(cf, name); - return (sym && (sym->class == SYM_TABLE)) ? sym->def : NULL; + return (sym && (sym->class == SYM_TABLE)) ? sym->table : NULL; } /** diff --git a/proto/mrt/mrt.h b/proto/mrt/mrt.h index b2cec09d..4dfb1b19 100644 --- a/proto/mrt/mrt.h +++ b/proto/mrt/mrt.h @@ -23,7 +23,7 @@ struct mrt_config { struct rtable_config *table_cf; const char *table_expr; - struct filter *filter; + const struct filter *filter; const char *filename; uint period; int always_add_path; @@ -41,7 +41,7 @@ struct mrt_proto { struct mrt_dump_data { const char *table_expr; struct rtable *table_ptr; - struct filter *filter; + const struct filter *filter; char *filename; }; @@ -61,7 +61,7 @@ struct mrt_table_dump_state { /* Configuration information */ const char *table_expr; /* Wildcard for table name (or NULL) */ struct rtable *table_ptr; /* Explicit table (or NULL) */ - struct filter *filter; /* Optional filter */ + const struct filter *filter; /* Optional filter */ const char *filename; /* Filename pattern */ int always_add_path; /* Always use *_ADDPATH message subtypes */ diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index ded5dfe4..24a4168d 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -562,7 +562,7 @@ static struct rte * krt_export_net(struct krt_proto *p, net *net, rte **rt_free) { struct channel *c = p->p.main_channel; - struct filter *filter = c->out_filter; + const struct filter *filter = c->out_filter; rte *rt; if (c->ra_mode == RA_MERGED) diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 0fdd5b34..b0d764fa 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -94,11 +94,9 @@ drop_gid(gid_t gid) static inline void add_num_const(char *name, int val) { - struct symbol *s = cf_get_symbol(name); - s->class = SYM_CONSTANT | T_INT; - s->def = cfg_allocz(sizeof(struct f_val)); - SYM_TYPE(s) = T_INT; - SYM_VAL(s).i = val; + struct f_val *v = cfg_alloc(sizeof(struct f_val)); + *v = (struct f_val) { .type = T_INT, .val.i = val }; + cf_define_symbol(cf_get_symbol(name), SYM_CONSTANT | T_INT, val, v); } /* the code of read_iproute_table() is based on -- cgit v1.2.3 From 99911873a196975f5221aad89ae5eac42e1330e0 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 25 Feb 2019 17:19:47 +0100 Subject: Conf: Lexer parses quoted strings in a more descriptive way --- conf/cf-lex.l | 137 ++++++++++++++++++++++++++++++++++++++-------------------- conf/conf.h | 4 +- 2 files changed, 92 insertions(+), 49 deletions(-) (limited to 'conf/conf.h') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index f1b402a0..45327cd9 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -65,7 +65,7 @@ struct keyword { #endif -static uint cf_hash(byte *c); +static uint cf_hash(const byte *c); #define KW_KEY(n) n->name #define KW_NEXT(n) n->next @@ -96,6 +96,15 @@ int (*cf_read_hook)(byte *buf, unsigned int max, int fd); struct include_file_stack *ifs; static struct include_file_stack *ifs_head; +#define QUOTED_BUFFER_SIZE 4096 +static BUFFER_(char) quoted_buffer; +static char quoted_buffer_data[QUOTED_BUFFER_SIZE]; +static inline void quoted_buffer_init(void) { + quoted_buffer.used = 0; + quoted_buffer.size = QUOTED_BUFFER_SIZE; + quoted_buffer.data = quoted_buffer_data; +} + #define MAX_INCLUDE_DEPTH 8 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd); @@ -106,6 +115,8 @@ static struct include_file_stack *ifs_head; static void cf_include(char *arg, int alen); static int check_eof(void); +static enum yytokentype cf_lex_symbol(const char *data); + %} %option noyywrap @@ -113,24 +124,26 @@ static int check_eof(void); %option nounput %option noreject -%x COMMENT CCOMM CLI +%x COMMENT CCOMM CLI QUOTED APOSTROPHED INCLUDE ALPHA [a-zA-Z_] DIGIT [0-9] XIGIT [0-9a-fA-F] ALNUM [a-zA-Z_0-9] WHITE [ \t] -include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; %% -{include} { - char *start, *end; - +^{WHITE}*include{WHITE}*\" { if (!ifs->depth) cf_error("Include not allowed in CLI"); - start = strchr(yytext, '"'); - start++; + BEGIN(INCLUDE); +} + +[^"\n]+["]{WHITE}*; { + char *start, *end; + + start = yytext; end = strchr(start, '"'); *end = 0; @@ -139,8 +152,16 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*; cf_error("Include with empty argument"); cf_include(start, end-start); + + BEGIN(INITIAL); } +["] cf_error("Include with empty argument"); +. cf_error("Unterminated include"); +\n cf_error("Unterminated include"); +<> cf_error("Unterminated include"); + + {DIGIT}+:{DIGIT}+ { uint len1 UNUSED, len2; u64 l; @@ -267,37 +288,23 @@ else: { return ELSECOL; } -({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) { - if(*yytext == '\'') { - yytext[yyleng-1] = 0; - yytext++; - } +['] { + BEGIN(APOSTROPHED); + quoted_buffer_init(); +} - struct keyword *k = HASH_FIND(kw_hash, KW, yytext); - if (k) - { - if (k->value > 0) - return k->value; - else - { - cf_lval.i = -k->value; - return ENUM; - } - } +{ALNUM}|[-]|[.:] BUFFER_PUSH(quoted_buffer) = yytext[0]; +\n cf_error("Unterminated symbol"); +<> cf_error("Unterminated symbol"); +['] { + BEGIN(INITIAL); + BUFFER_PUSH(quoted_buffer) = 0; + return cf_lex_symbol(quoted_buffer_data); +} +. cf_error("Invalid character in apostrophed symbol"); - cf_lval.s = cf_get_symbol(yytext); - switch (cf_lval.s->class) { - case SYM_VOID: return CF_SYM_VOID; - case SYM_PROTO: return CF_SYM_PROTO; - case SYM_TEMPLATE: return CF_SYM_TEMPLATE; - case SYM_FUNCTION: return CF_SYM_FUNCTION; - case SYM_FILTER: return CF_SYM_FILTER; - case SYM_TABLE: return CF_SYM_TABLE; - case SYM_ATTRIBUTE: return CF_SYM_ATTRIBUTE; - case SYM_VARIABLE_RANGE: return CF_SYM_VARIABLE; - case SYM_CONSTANT_RANGE: return CF_SYM_CONSTANT; - default: bug("Unknown symbol class %d", cf_lval.s->class); - } +({ALPHA}{ALNUM}*) { + return cf_lex_symbol(yytext); } (.|\n) { @@ -313,14 +320,21 @@ else: { return yytext[0]; } -["][^"\n]*["] { - yytext[yyleng-1] = 0; - cf_lval.t = cfg_strdup(yytext+1); - yytext[yyleng-1] = '"'; +["] { + BEGIN(QUOTED); + quoted_buffer_init(); +} + +\n cf_error("Unterminated string"); +<> cf_error("Unterminated string"); +["] { + BEGIN(INITIAL); + BUFFER_PUSH(quoted_buffer) = 0; + cf_lval.t = cfg_strdup(quoted_buffer_data); return TEXT; } -["][^"\n]*\n cf_error("Unterminated string"); +. BUFFER_PUSH(quoted_buffer) = yytext[0]; <> { if (check_eof()) return END; } @@ -361,7 +375,7 @@ else: { %% static uint -cf_hash(byte *c) +cf_hash(const byte *c) { uint h = 13 << 24; @@ -370,7 +384,6 @@ cf_hash(byte *c) return h; } - /* * IFS stack - it contains structures needed for recursive processing * of include in config files. On the top of the stack is a structure @@ -531,7 +544,7 @@ check_eof(void) } static struct symbol * -cf_new_symbol(byte *c) +cf_new_symbol(const byte *c) { struct symbol *s; @@ -563,7 +576,7 @@ cf_new_symbol(byte *c) * signify no match. */ struct symbol * -cf_find_symbol(struct config *cfg, byte *c) +cf_find_symbol(struct config *cfg, const byte *c) { struct symbol *s; @@ -590,7 +603,7 @@ cf_find_symbol(struct config *cfg, byte *c) * existing symbol is found. */ struct symbol * -cf_get_symbol(byte *c) +cf_get_symbol(const byte *c) { return cf_find_symbol(new_config, c) ?: cf_new_symbol(c); } @@ -636,6 +649,36 @@ cf_default_name(char *template, int *counter) cf_error("Unable to generate default name"); } +static enum yytokentype +cf_lex_symbol(const char *data) +{ + struct keyword *k = HASH_FIND(kw_hash, KW, data); + if (k) + { + if (k->value > 0) + return k->value; + else + { + cf_lval.i = -k->value; + return ENUM; + } + } + + cf_lval.s = cf_get_symbol(data); + switch (cf_lval.s->class) { + case SYM_VOID: return CF_SYM_VOID; + case SYM_PROTO: return CF_SYM_PROTO; + case SYM_TEMPLATE: return CF_SYM_TEMPLATE; + case SYM_FUNCTION: return CF_SYM_FUNCTION; + case SYM_FILTER: return CF_SYM_FILTER; + case SYM_TABLE: return CF_SYM_TABLE; + case SYM_ATTRIBUTE: return CF_SYM_ATTRIBUTE; + case SYM_VARIABLE_RANGE: return CF_SYM_VARIABLE; + case SYM_CONSTANT_RANGE: return CF_SYM_CONSTANT; + default: bug("Unknown symbol class %d", cf_lval.s->class); + } +} + static void cf_lex_init_kh(void) { diff --git a/conf/conf.h b/conf/conf.h index 21d4f1e2..acb3413b 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -167,9 +167,9 @@ int cf_lex(void); void cf_lex_init(int is_cli, struct config *c); void cf_lex_unwind(void); -struct symbol *cf_find_symbol(struct config *cfg, byte *c); +struct symbol *cf_find_symbol(struct config *cfg, const byte *c); -struct symbol *cf_get_symbol(byte *c); +struct symbol *cf_get_symbol(const byte *c); struct symbol *cf_default_name(char *template, int *counter); struct symbol *cf_localize_symbol(struct symbol *sym); -- cgit v1.2.3 From f249d0b84c840242a084966999a1d228c603b431 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 26 Feb 2019 16:44:24 +0100 Subject: Filters: comparison of functions and filters caching --- conf/cf-lex.l | 4 +++- conf/conf.c | 3 +++ conf/conf.h | 4 +++- filter/config.Y | 2 +- filter/f-inst.h | 4 +--- filter/f-util.c | 9 ++++----- filter/filter.c | 46 +++++++++++++++++++++++++++++++++++++++++-- filter/filter.h | 4 +++- filter/test-reconf-begin.conf | 23 ++++++++++++++++++++++ filter/test-reconf-end.conf | 23 ++++++++++++++++++++++ 10 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 filter/test-reconf-begin.conf create mode 100644 filter/test-reconf-end.conf (limited to 'conf/conf.h') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 593df9d6..4f69993e 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -561,6 +561,8 @@ cf_new_symbol(const byte *c) HASH_INSERT2(new_config->sym_hash, SYM, new_config->pool, s); + add_tail(&(new_config->symbols), &(s->n)); + return s; } @@ -576,7 +578,7 @@ cf_new_symbol(const byte *c) * signify no match. */ struct symbol * -cf_find_symbol(struct config *cfg, const byte *c) +cf_find_symbol(const struct config *cfg, const byte *c) { struct symbol *s; diff --git a/conf/conf.c b/conf/conf.c index 439aa41d..b0980d7e 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -98,6 +98,7 @@ config_alloc(const char *name) memcpy(ndup, name, nlen); init_list(&c->tests); + init_list(&c->symbols); c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */ c->pool = p; c->mem = l; @@ -258,6 +259,8 @@ config_do_commit(struct config *c, int type) if (old_config) old_config->obstacle_count++; + DBG("filter_commit\n"); + filter_commit(c, old_config); DBG("sysdep_commit\n"); int force_restart = sysdep_commit(c, old_config); DBG("global_commit\n"); diff --git a/conf/conf.h b/conf/conf.h index acb3413b..f14c0e21 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -24,6 +24,7 @@ struct config { list tables; /* Configured routing tables (struct rtable_config) */ list logfiles; /* Configured log files (sysdep) */ list tests; /* Configured unit tests (f_bt_test_suite) */ + list symbols; /* Configured symbols in config order */ int mrtdump_file; /* Configured MRTDump file (sysdep, fd in unix) */ char *syslog_name; /* Name used for syslog (NULL -> no syslog) */ @@ -103,6 +104,7 @@ void cfg_copy_list(list *dest, list *src, unsigned node_size); extern int (*cf_read_hook)(byte *buf, uint max, int fd); struct symbol { + node n; /* In list of symbols in config */ struct symbol *next; struct sym_scope *scope; int class; /* SYM_* */ @@ -167,7 +169,7 @@ int cf_lex(void); void cf_lex_init(int is_cli, struct config *c); void cf_lex_unwind(void); -struct symbol *cf_find_symbol(struct config *cfg, const byte *c); +struct symbol *cf_find_symbol(const struct config *cfg, const byte *c); struct symbol *cf_get_symbol(const byte *c); struct symbol *cf_default_name(char *template, int *counter); diff --git a/filter/config.Y b/filter/config.Y index 5ec226f0..a7618987 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -470,7 +470,7 @@ filter_def: FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); } filter_body { struct filter *f = cfg_alloc(sizeof(struct filter)); - *f = (struct filter) { .name = $2->name, .root = $4 }; + *f = (struct filter) { .sym = $2, .root = $4 }; $2->filter = f; cf_pop_scope(); diff --git a/filter/f-inst.h b/filter/f-inst.h index 5b397a5d..4b42c57c 100644 --- a/filter/f-inst.h +++ b/filter/f-inst.h @@ -31,12 +31,10 @@ const char *f_instruction_name(enum f_instruction_code fi); struct f_inst *f_clear_local_vars(struct f_inst *decls); /* Filter structures for execution */ -struct f_line; - /* Line of instructions to be unconditionally executed one after another */ struct f_line { uint len; /* Line length */ - u16 args; /* Function: Args required */ + u8 args; /* Function: Args required */ struct f_line_item items[0]; /* The items themselves */ }; diff --git a/filter/f-util.c b/filter/f-util.c index 85f5d1c4..35944b2c 100644 --- a/filter/f-util.c +++ b/filter/f-util.c @@ -24,10 +24,10 @@ filter_name(const struct filter *filter) return "ACCEPT"; else if (filter == FILTER_REJECT) return "REJECT"; - else if (!filter->name) + else if (!filter->sym) return "(unnamed)"; else - return filter->name; + return filter->sym->name; } void f_inst_next(struct f_inst *first, const struct f_inst *append) @@ -54,7 +54,7 @@ struct filter *f_new_where(const struct f_inst *where) struct f_inst i = { .fi_code = FI_CONDITION, .lineno = ifs->lino, - .size = 3, + .size = 3 + where->size, .i_FI_CONDITION = { .f1 = where, .f2 = &acc, @@ -62,8 +62,7 @@ struct filter *f_new_where(const struct f_inst *where) }, }; - struct filter *f = cfg_alloc(sizeof(struct filter)); - f->name = NULL; + struct filter *f = cfg_allocz(sizeof(struct filter)); f->root = f_postfixify(&i); return f; } diff --git a/filter/filter.c b/filter/filter.c index ee5d5426..3be7343c 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -293,7 +293,7 @@ f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, i if (fret < F_ACCEPT) { if (!(fs.flags & FF_SILENT)) - log_rl(&rl_runtime_err, L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name); + log_rl(&rl_runtime_err, L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter_name(filter)); return F_ERROR; } DBG( "done (%u)\n", res.val.i ); @@ -378,5 +378,47 @@ filter_same(const struct filter *new, const struct filter *old) if (old == FILTER_ACCEPT || old == FILTER_REJECT || new == FILTER_ACCEPT || new == FILTER_REJECT) return 0; - return f_same(new->root, old->root); + + if ((!old->sym) && (!new->sym)) + return f_same(new->root, old->root); + + if ((!old->sym) || (!new->sym)) + return 0; + + if (strcmp(old->sym->name, new->sym->name)) + return 0; + + return new->sym->flags & SYM_FLAG_SAME; +} + +/** + * filter_commit - do filter comparisons on all the named functions and filters + */ +void +filter_commit(const struct config *new, const struct config *old) +{ + if (!old) + return; + + struct symbol *sym, *osym; + WALK_LIST(sym, new->symbols) + switch (sym->class) { + case SYM_FUNCTION: + if ((osym = cf_find_symbol(old, sym->name)) && + (osym->class == SYM_FUNCTION) && + f_same(sym->function, osym->function)) + sym->flags |= SYM_FLAG_SAME; + else + sym->flags &= ~SYM_FLAG_SAME; + break; + + case SYM_FILTER: + if ((osym = cf_find_symbol(old, sym->name)) && + (osym->class == SYM_FILTER) && + f_same(sym->filter->root, osym->filter->root)) + sym->flags |= SYM_FLAG_SAME; + else + sym->flags &= ~SYM_FLAG_SAME; + break; + } } diff --git a/filter/filter.h b/filter/filter.h index 26faeaa3..d919cc74 100644 --- a/filter/filter.h +++ b/filter/filter.h @@ -47,7 +47,7 @@ struct f_val; /* The filter encapsulating structure to be pointed-to from outside */ struct f_line; struct filter { - char *name; + struct symbol *sym; const struct f_line *root; }; @@ -62,6 +62,8 @@ const char *filter_name(const struct filter *filter); int filter_same(const struct filter *new, const struct filter *old); int f_same(const struct f_line *f1, const struct f_line *f2); +void filter_commit(const struct config *new, const struct config *old); + #define FILTER_ACCEPT NULL #define FILTER_REJECT ((void *) 1) #define FILTER_UNDEF ((void *) 2) /* Used in BGP */ diff --git a/filter/test-reconf-begin.conf b/filter/test-reconf-begin.conf new file mode 100644 index 00000000..aeed007f --- /dev/null +++ b/filter/test-reconf-begin.conf @@ -0,0 +1,23 @@ +router id 1.1.1.1; +protocol device {} + +function a() { + return true; +} + +function b() { + return a(); +} + +function c() { + return b(); +} + +filter d { + if c() then accept; else reject; +} + +protocol static { + ipv4 { import filter d; }; + route 10.0.0.0/24 unreachable; +} diff --git a/filter/test-reconf-end.conf b/filter/test-reconf-end.conf new file mode 100644 index 00000000..19164825 --- /dev/null +++ b/filter/test-reconf-end.conf @@ -0,0 +1,23 @@ +router id 1.1.1.1; +protocol device {} + +function a() { + return false; +} + +function b() { + return a(); +} + +function c() { + return b(); +} + +filter d { + if c() then accept; else reject; +} + +protocol static { + ipv4 { import filter d; }; + route 10.0.0.0/24 unreachable; +} -- cgit v1.2.3 From 96d757c13fe4b2e21b265275af9ac7d1e2c3f075 Mon Sep 17 00:00:00 2001 From: Jan Maria Matejka Date: Tue, 21 May 2019 16:33:37 +0000 Subject: Filter: Store variables and function arguments on stack --- conf/conf.h | 3 +- conf/confbase.Y | 2 +- filter/config.Y | 140 +++++++++++++++++++++------------------------------ filter/data.h | 4 +- filter/decl.m4 | 3 +- filter/f-inst.c | 95 ++++++++++++---------------------- filter/f-inst.h | 3 +- filter/f-util.c | 22 -------- filter/filter.c | 44 +++++++++------- filter/filter_test.c | 3 +- filter/test.conf | 3 +- 11 files changed, 126 insertions(+), 196 deletions(-) (limited to 'conf/conf.h') diff --git a/conf/conf.h b/conf/conf.h index f14c0e21..d88d9a44 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -116,7 +116,8 @@ struct symbol { const struct filter *filter; /* For SYM_FILTER */ struct rtable_config *table; /* For SYM_TABLE */ struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */ - struct f_val *val; /* For SYM_CONSTANT or SYM_VARIABLE */ + struct f_val *val; /* For SYM_CONSTANT */ + uint offset; /* For SYM_VARIABLE */ }; char name[0]; diff --git a/conf/confbase.Y b/conf/confbase.Y index e104e54f..9978aec8 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -70,7 +70,7 @@ CF_DECLS struct f_dynamic_attr fda; struct f_static_attr fsa; struct f_lval flv; - const struct f_line *fl; + struct f_line *fl; const struct filter *f; struct f_tree *e; struct f_trie *trie; diff --git a/filter/config.Y b/filter/config.Y index 3eccc3ed..5f9b8356 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -15,6 +15,8 @@ CF_HDR CF_DEFINES +static uint decls_count; + static inline u32 pair(u32 a, u32 b) { return (a << 16) | b; } static inline u32 pair_a(u32 p) { return p >> 16; } static inline u32 pair_b(u32 p) { return p & 0xFFFF; } @@ -398,8 +400,8 @@ assert_assign(struct f_lval *lval, struct f_inst *expr, const char *start, const struct f_inst *setter, *getter, *checker; switch (lval->type) { case F_LVAL_VARIABLE: - setter = f_new_inst(FI_SET, expr, lval->sym); - getter = f_new_inst(FI_VARIABLE, lval->sym); + setter = f_new_inst(FI_VAR_SET, expr, lval->sym); + getter = f_new_inst(FI_VAR_GET, lval->sym); break; case F_LVAL_PREFERENCE: setter = f_new_inst(FI_PREF_SET, expr); @@ -446,14 +448,14 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, %nonassoc THEN %nonassoc ELSE -%type cmds_int function_body declsn function_params -%type term block cmd cmds constant constructor print_one print_list var_list var_listn function_call symbol_value bgp_path_expr bgp_path bgp_path_tail one_decl decls +%type cmds_int +%type term block cmd cmds constant constructor print_one print_list var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail %type dynamic_attr %type static_attr %type filter where_filter -%type filter_body +%type filter_body function_body %type lvalue -%type type +%type type function_params %type ec_kind %type break_command %type cnum @@ -553,44 +555,25 @@ type: } ; -one_decl: - type CF_SYM_VOID { - struct f_val * val = cfg_alloc(sizeof(struct f_val)); - val->type = T_VOID; - $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val, val); - DBG( "New variable %s type %x\n", $2->name, $1 ); - $$ = f_new_inst(FI_SET, NULL, $2); - } - ; - -/* Decls with ';' at the end. Beware; these are reversed. */ -decls: /* EMPTY */ { $$ = NULL; } - | one_decl ';' decls { - $$ = $1; - $$->next = $3; - } +/* Declarations with ';' at the end */ +decls: + /* EMPTY */ + | declsn ';' ; /* Declarations that have no ';' at the end. */ -declsn: one_decl { $$[0] = $$[1] = $1; } - | one_decl ';' declsn { - $3[1]->next = $1; - $$[1] = $3[1] = $1; - $$[0] = $3[0]; +declsn: + type CF_SYM_VOID { + cf_define_symbol($2, SYM_VARIABLE | $1, offset, decls_count++); } - ; - -filter_body: - function_body { - if ($1[0]) { - const struct f_inst *inst[2] = { $1[0], $1[1] }; - $$ = f_postfixify_concat(inst, 2); - } - else - $$ = f_postfixify($1[1]); + | declsn ';' type CF_SYM_VOID { + if (decls_count >= 0xff) cf_error("Too many declarations, at most 255 allowed"); + cf_define_symbol($4, SYM_VARIABLE | $3, offset, decls_count++); } ; +filter_body: { decls_count = 0; } function_body { $$ = $2; } ; + filter: CF_SYM_KNOWN { cf_assert_symbol($1, SYM_FILTER); @@ -611,14 +594,14 @@ where_filter: ; function_params: - '(' declsn ')' { $$[0] = $2[0]; $$[1] = $2[1]; } - | '(' ')' { $$[0] = $$[1] = NULL; } + '(' declsn ')' { $$ = decls_count; } + | '(' ')' { $$ = 0; } ; function_body: decls '{' cmds '}' { - $$[0] = $1 ? f_clear_local_vars($1) : NULL; - $$[1] = $3; + $$ = f_postfixify($3); + $$->vars = decls_count; } ; @@ -627,33 +610,11 @@ function_def: FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name ); $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL); cf_push_scope($2); + decls_count = 0; } function_params function_body { - const struct f_inst *catlist[4]; - uint count = 0; - - /* Argument setters */ - if ($4[0]) - catlist[count++] = $4[0]; - - /* Local var clearers */ - if ($5[0]) - catlist[count++] = $5[0]; - - /* Return void if no return is needed */ - catlist[count++] = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID }); - - /* Function body itself */ - if ($5[1]) - catlist[count++] = $5[1]; - - struct f_line *fl = f_postfixify_concat(catlist, count); - - fl->args = 0; - for (const struct f_inst *arg = $4[0]; arg; arg = arg->next) - fl->args++; - - $2->function = fl; - + $5->vars -= $4; + $5->args = $4; + $2->function = $5; cf_pop_scope(); } ; @@ -862,9 +823,33 @@ constructor: ; +/* This generates the function_call variable list backwards. */ +var_list: /* EMPTY */ { $$ = NULL; } + | term { $$ = $1; } + | var_list ',' term { $$ = $3; $$->next = $1; } + function_call: CF_SYM_KNOWN '(' var_list ')' { - $$ = f_new_inst(FI_CALL, $1, $3); + if ($1->class != SYM_FUNCTION) + cf_error("You can't call something which is not a function. Really."); + + struct f_inst *fc = f_new_inst(FI_CALL, $1); + uint args = 0; + while ($3) { + args++; + struct f_inst *tmp = $3->next; + $3->next = fc; + + fc = $3; + $3 = tmp; + } + + if (args != $1->function->args) + cf_error("Function call '%s' got %u arguments, need %u arguments.", + $1->name, args, $1->function->args); + + $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID }); + $$->next = fc; } ; @@ -872,8 +857,10 @@ symbol_value: CF_SYM_KNOWN { switch ($1->class) { case SYM_CONSTANT_RANGE: + $$ = f_new_inst(FI_CONSTANT_DEFINED, $1); + break; case SYM_VARIABLE_RANGE: - $$ = f_new_inst(FI_VARIABLE, $1); + $$ = f_new_inst(FI_VAR_GET, $1); break; case SYM_ATTRIBUTE: $$ = f_new_inst(FI_EA_GET, *$1->attribute); @@ -988,19 +975,6 @@ print_list: /* EMPTY */ { $$ = NULL; } } ; -var_listn: term { - $$ = $1; - } - | term ',' var_listn { - $$ = $1; - $$->next = $3; - } - ; - -var_list: /* EMPTY */ { $$ = NULL; } - | var_listn { $$ = $1; } - ; - cmd: IF term THEN block { $$ = f_new_inst(FI_CONDITION, $2, $4, NULL); @@ -1011,7 +985,7 @@ cmd: | CF_SYM_KNOWN '=' term ';' { switch ($1->class) { case SYM_VARIABLE_RANGE: - $$ = f_new_inst(FI_SET, $3, $1); + $$ = f_new_inst(FI_VAR_SET, $3, $1); break; case SYM_ATTRIBUTE: $$ = f_new_inst(FI_EA_SET, $3, *$1->attribute); diff --git a/filter/data.h b/filter/data.h index 6ef2024f..6973008f 100644 --- a/filter/data.h +++ b/filter/data.h @@ -17,8 +17,8 @@ /* Internal types */ enum f_type { -/* Do not use type of zero, that way we'll see errors easier. */ - T_VOID = 1, +/* Nothing. Simply nothing. */ + T_VOID = 0, /* User visible types, which fit in int */ T_INT = 0x10, diff --git a/filter/decl.m4 b/filter/decl.m4 index bdd59f20..c9b5c8c5 100644 --- a/filter/decl.m4 +++ b/filter/decl.m4 @@ -209,6 +209,7 @@ do { estk.item[estk.cnt].pos = 0; estk.item[estk.cnt].line = $1; estk.item[estk.cnt].ventry = vstk.cnt; + estk.item[estk.cnt].vbase = estk.item[estk.cnt-1].vbase; estk.item[estk.cnt].emask = 0; estk.cnt++; } while (0)m4_dnl @@ -377,7 +378,7 @@ FID_WR_PUT(4) /* Filter instruction structure for config */ struct f_inst { - const struct f_inst *next; /* Next instruction */ + struct f_inst *next; /* Next instruction */ enum f_instruction_code fi_code; /* Instruction code */ int size; /* How many instructions are underneath */ int lineno; /* Line number */ diff --git a/filter/f-inst.c b/filter/f-inst.c index d6c292b6..4a4f6016 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -261,23 +261,28 @@ } /* Set to indirect value prepared in v1 */ - INST(FI_SET, 1, 0) { + INST(FI_VAR_SET, 1, 0) { ARG_ANY(2); SYMBOL(1); if ((sym->class != (SYM_VARIABLE | v1.type)) && (v1.type != T_VOID)) { /* IP->Quad implicit conversion */ if ((sym->class == (SYM_VARIABLE | T_QUAD)) && val_is_ip4(&v1)) - { - *(sym->val) = (struct f_val) { + v1 = (struct f_val) { .type = T_QUAD, .val.i = ipa_to_u32(v1.val.ip), - }; - break; - } - runtime( "Assigning to variable of incompatible type" ); + }; + else + runtime( "Assigning to variable of incompatible type" ); } - *(sym->val) = v1; + + vstk.val[curline.vbase + sym->offset] = v1; + } + + INST(FI_VAR_GET, 0, 1) { + SYMBOL(1); + res = vstk.val[curline.vbase + sym->offset]; + RESULT_OK; } /* some constants have value in a[1], some in *a[0].p, strange. */ @@ -301,7 +306,7 @@ res = whati->val; RESULT_OK; } - INST(FI_VARIABLE, 0, 1) { + INST(FI_CONSTANT_DEFINED, 0, 1) { FID_STRUCT_IN const struct symbol *sym; FID_LINE_IN @@ -314,18 +319,9 @@ FID_POSTFIXIFY_BODY item->valp = (item->sym = what->sym)->val; FID_SAME_BODY - if (strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)) return 0; + if (strcmp(f1->sym->name, f2->sym->name) || !val_same(f1->sym->val, f2->sym->val)) return 0; FID_DUMP_BODY - switch (item->sym->class) { - case SYM_CONSTANT_RANGE: - debug("%sconstant %s with value %s\n", INDENT, item->sym->name, val_dump(item->valp)); - break; - case SYM_VARIABLE_RANGE: - debug("%svariable %s with current value %s\n", INDENT, item->sym->name, val_dump(item->valp)); - break; - default: - bug("Symbol %s of type %d doesn't reference a value", item->sym->name, item->sym->class); - } + debug("%sconstant %s with value %s\n", INDENT, item->sym->name, val_dump(item->valp)); FID_ALL res = *whati->valp; @@ -768,56 +764,31 @@ else runtime("Can't return non-bool from non-function"); - /* Set the value stack position */ - vstk.cnt = estk.item[estk.cnt].ventry; + /* Set the value stack position, overwriting the former implicit void */ + vstk.cnt = estk.item[estk.cnt].ventry - 1; /* Copy the return value */ RESULT_VAL(vstk.val[retpos]); } INST(FI_CALL, 0, 1) { - FID_LINE_IN - const struct f_line *args; - const struct f_line *body; - struct symbol *sym; - FID_STRUCT_IN - struct symbol *sym; - const struct f_inst *args; - FID_NEW_ARGS - , struct symbol * sym - , const struct f_inst *args - FID_NEW_BODY - if (sym->class != SYM_FUNCTION) - cf_error("You can't call something which is not a function. Really."); - - uint count = 0; - for (const struct f_inst *inst = args; inst; inst = inst->next) - count++; + SYMBOL; - if (count != sym->function->args) - cf_error("Function %s takes %u arguments, got %u.", sym->name, sym->function->args, count); - - what->sym = sym; - what->args = args; - FID_DUMP_BODY - debug("%scalling %s with following args\n", INDENT, item->sym->name); - f_dump_line(item->args, indent + 1); - FID_POSTFIXIFY_BODY - item->args = f_postfixify(what->args); - item->body = (item->sym = what->sym)->function; - FID_SAME_BODY - /* To be done better */ - if (strcmp(f1->sym->name, f2->sym->name)) return 0; - if (!f_same(f1->args, f2->args)) return 0; - if (!f_same(f1->body, f2->body)) return 0; - FID_ALL - - /* First push the body on stack */ - LINEX(whati->body); + /* Push the body on stack */ + LINEX(sym->function); curline.emask |= FE_RETURN; - - /* Then push the arguments */ - LINEX(whati->args); + + /* Before this instruction was called, there was the T_VOID + * automatic return value pushed on value stack and also + * sym->function->args function arguments. Setting the + * vbase to point to first argument. */ + ASSERT(curline.ventry >= sym->function->args); + curline.ventry -= sym->function->args; + curline.vbase = curline.ventry; + + /* Storage for local variables */ + memset(&(vstk.val[vstk.cnt]), 0, sizeof(struct f_val) * sym->function->vars); + vstk.cnt += sym->function->vars; } INST(FI_DROP_RESULT, 1, 0) { diff --git a/filter/f-inst.h b/filter/f-inst.h index 1e2d63a2..21cec454 100644 --- a/filter/f-inst.h +++ b/filter/f-inst.h @@ -63,13 +63,12 @@ enum f_instruction_flags { /* Convert the instruction back to the enum name */ const char *f_instruction_name(enum f_instruction_code fi); -struct f_inst *f_clear_local_vars(struct f_inst *decls); - /* Filter structures for execution */ /* Line of instructions to be unconditionally executed one after another */ struct f_line { uint len; /* Line length */ u8 args; /* Function: Args required */ + u8 vars; struct f_line_item items[0]; /* The items themselves */ }; diff --git a/filter/f-util.c b/filter/f-util.c index 35944b2c..79201fba 100644 --- a/filter/f-util.c +++ b/filter/f-util.c @@ -30,11 +30,6 @@ filter_name(const struct filter *filter) return filter->sym->name; } -void f_inst_next(struct f_inst *first, const struct f_inst *append) -{ - first->next = append; -} - struct filter *f_new_where(const struct f_inst *where) { struct f_inst acc = { @@ -67,23 +62,6 @@ struct filter *f_new_where(const struct f_inst *where) return f; } -struct f_inst *f_clear_local_vars(struct f_inst *decls) -{ - /* Prepend instructions to clear local variables */ - struct f_inst *head = NULL; - - for (const struct f_inst *si = decls; si; si = si->next) { - struct f_inst *cur = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID }); - if (head) - f_inst_next(cur, head); - else - f_inst_next(cur, si); - head = cur; /* The first FI_CONSTANT put there */ - } - - return head; -} - #define CA_KEY(n) n->name, n->fda.type #define CA_NEXT(n) n->next #define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb)) diff --git a/filter/filter.c b/filter/filter.c index 50d9414b..dbc2376b 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -135,6 +135,8 @@ static struct tbf rl_runtime_err = TBF_DEFAULT_LOG_LIMITS; static enum filter_return interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) { + /* No arguments allowed */ + ASSERT(line->args == 0); #define F_VAL_STACK_MAX 4096 /* Value stack for execution */ @@ -145,8 +147,12 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) /* The stack itself is intentionally kept as-is for performance reasons. * Do NOT rewrite this to initialization by struct literal. It's slow. - */ - vstk.cnt = 0; + * + * Reserving space for local variables. */ + + vstk.cnt = line->vars; + memset(vstk.val, 0, sizeof(struct f_val) * line->vars); + #define F_EXEC_STACK_MAX 4096 /* Exception bits */ @@ -160,6 +166,7 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) const struct f_line *line; /* The line that is being executed */ uint pos; /* Instruction index in the line */ uint ventry; /* Value stack depth on entry */ + uint vbase; /* Where to index variable positions from */ enum f_exception emask; /* Exception mask */ } item[F_EXEC_STACK_MAX]; uint cnt; /* Current stack size; 0 for empty */ @@ -181,7 +188,6 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) while (curline.pos < curline.line->len) { const struct f_line_item *what = &(curline.line->items[curline.pos++]); - switch (what->fi_code) { #define res vstk.val[vstk.cnt] #define v1 vstk.val[vstk.cnt] @@ -207,26 +213,28 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) #undef ACCESS_EATTRS } } + + /* End of current line. Drop local variables before exiting. */ + vstk.cnt -= curline.line->vars; + vstk.cnt -= curline.line->args; estk.cnt--; } - switch (vstk.cnt) { - case 0: - if (val) { - log_rl(&rl_runtime_err, L_ERR "filters: No value left on stack"); - return F_ERROR; - } - return F_NOP; - case 1: - if (val) { - *val = vstk.val[0]; - return F_NOP; - } - /* fallthrough */ - default: - log_rl(&rl_runtime_err, L_ERR "Too many items left on stack: %u", vstk.cnt); + if (vstk.cnt == 0) { + if (val) { + log_rl(&rl_runtime_err, L_ERR "filters: No value left on stack"); return F_ERROR; + } + return F_NOP; } + + if (val && (vstk.cnt == 1)) { + *val = vstk.val[0]; + return F_NOP; + } + + log_rl(&rl_runtime_err, L_ERR "Too many items left on stack: %u", vstk.cnt); + return F_ERROR; } diff --git a/filter/filter_test.c b/filter/filter_test.c index d0dd281a..3abe095b 100644 --- a/filter/filter_test.c +++ b/filter/filter_test.c @@ -56,8 +56,7 @@ run_function(const void *arg) } linpool *tmp = lp_new_default(&root_pool); - struct f_val res; - enum filter_return fret = f_eval(t->fn, tmp, &res); + enum filter_return fret = f_eval(t->fn, tmp, NULL); rfree(tmp); return (fret < F_REJECT); diff --git a/filter/test.conf b/filter/test.conf index ba25a34b..9abd76f3 100644 --- a/filter/test.conf +++ b/filter/test.conf @@ -7,8 +7,7 @@ router id 62.168.0.1; /* We have to setup any protocol */ -protocol static { ipv4; } - +protocol device { } -- cgit v1.2.3 From 63e7620462b80c9c6bbbd4f128b6816e0748d6c6 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 25 Jun 2019 16:18:06 +0200 Subject: Conf/Filters: Moved argument count to conf scope --- conf/cf-lex.l | 1 + conf/conf.h | 1 + filter/config.Y | 23 +++++++++++------------ 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'conf/conf.h') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 09f3db8d..0aa9273f 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -742,6 +742,7 @@ cf_push_scope(struct symbol *sym) conf_this_scope = s; s->active = 1; s->name = sym; + s->slots = 0; } /** diff --git a/conf/conf.h b/conf/conf.h index d88d9a44..708a1034 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -126,6 +126,7 @@ struct symbol { struct sym_scope { struct sym_scope *next; /* Next on scope stack */ struct symbol *name; /* Name of this scope */ + uint slots; /* Variable slots */ int active; /* Currently entered */ }; diff --git a/filter/config.Y b/filter/config.Y index 3898748c..72866bb0 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -15,8 +15,6 @@ CF_HDR CF_DEFINES -static uint decls_count; - static inline u32 pair(u32 a, u32 b) { return (a << 16) | b; } static inline u32 pair_a(u32 p) { return p >> 16; } static inline u32 pair_b(u32 p) { return p & 0xFFFF; } @@ -455,7 +453,7 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, %type filter where_filter %type filter_body function_body %type lvalue -%type type function_params +%type type function_params declsn decls %type ec_kind %type break_command %type cnum @@ -557,22 +555,24 @@ type: /* Declarations with ';' at the end */ decls: - /* EMPTY */ - | declsn ';' + /* EMPTY */ { $$ = 0; } + | declsn ';' { $$ = $1; } ; /* Declarations that have no ';' at the end. */ declsn: type CF_SYM_VOID { - cf_define_symbol($2, SYM_VARIABLE | $1, offset, decls_count++); + cf_define_symbol($2, SYM_VARIABLE | $1, offset, $2->scope->slots++); + $$ = $2->scope->slots; } | declsn ';' type CF_SYM_VOID { - if (decls_count >= 0xff) cf_error("Too many declarations, at most 255 allowed"); - cf_define_symbol($4, SYM_VARIABLE | $3, offset, decls_count++); + if ($4->scope->slots >= 0xff) cf_error("Too many declarations, at most 255 allowed"); + cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++); + $$ = $4->scope->slots; } ; -filter_body: { decls_count = 0; } function_body { $$ = $2; } ; +filter_body: function_body ; filter: CF_SYM_KNOWN { @@ -594,14 +594,14 @@ where_filter: ; function_params: - '(' declsn ')' { $$ = decls_count; } + '(' declsn ')' { $$ = $2; } | '(' ')' { $$ = 0; } ; function_body: decls '{' cmds '}' { $$ = f_linearize($3); - $$->vars = decls_count; + $$->vars = $1; } ; @@ -610,7 +610,6 @@ function_def: FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name ); $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL); cf_push_scope($2); - decls_count = 0; } function_params function_body { $5->vars -= $4; $5->args = $4; -- cgit v1.2.3