From d1039926f5ee5a4e0442919474f16f3c93385cc9 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 19 Feb 2019 12:34:16 +0100 Subject: [PATCH] Filter: Interpreter merged into the common m4 generator. The config-time partial evaluation of constant expressions in filters is nearby. --- filter/Makefile | 10 ++--- filter/decl.m4 | 91 +++++++++++++++++++++++++++++++++++---------- filter/f-inst.c | 12 +++--- filter/filter.c | 2 +- filter/interpret.m4 | 80 --------------------------------------- filter/test.conf | 6 +++ 6 files changed, 90 insertions(+), 111 deletions(-) delete mode 100644 filter/interpret.m4 diff --git a/filter/Makefile b/filter/Makefile index 65602ea8..9562a2ee 100644 --- a/filter/Makefile +++ b/filter/Makefile @@ -7,19 +7,19 @@ $(conf-y-targets) $(conf-lex-targets) $(src-o-files): $(o)inst-gen.h M4FLAGS_FILTERS=$(filter-out -s,$(M4FLAGS)) -$(o)f-inst-interpret.c: $(s)interpret.m4 $(s)f-inst.c $(objdir)/.dir-stamp - $(M4) $(M4FLAGS_FILTERS) -P $^ >$@ - $(o)inst-gen.h: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp $(M4) $(M4FLAGS_FILTERS) -DTARGET=H -P $^ >$@ $(o)inst-gen.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp $(M4) $(M4FLAGS_FILTERS) -DTARGET=C -P $^ >$@ -$(o)filter.o: $(o)f-inst-interpret.c $(o)inst-gen.h +$(o)inst-interpret.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp + $(M4) $(M4FLAGS_FILTERS) -DTARGET=I -P $^ >$@ + +$(o)filter.o: $(o)inst-interpret.c $(o)inst-gen.h tests_src := tree_test.c filter_test.c trie_test.c tests_targets := $(tests_targets) $(tests-target-files) tests_objs := $(tests_objs) $(src-o-files) -$(call clean,inst-gen.h inst-gen.c) +$(call clean,inst-gen.h inst-gen.c inst-interpret.c) diff --git a/filter/decl.m4 b/filter/decl.m4 index 3033438d..bdd59f20 100644 --- a/filter/decl.m4 +++ b/filter/decl.m4 @@ -16,6 +16,7 @@ m4_divert(-1)m4_dnl # 9 same (filter comparator) # 1 union in struct f_inst # 3 constructors +# 10 interpreter # # Per-inst Diversions: # 101 content of per-inst struct @@ -24,7 +25,10 @@ m4_divert(-1)m4_dnl # 104 dump line item content # 105 postfixify body # 106 comparator body +# 107 struct f_line_item content +# 108 interpreter body # +# Diversions for TARGET=I: 7xx # Diversions for TARGET=C: 8xx # Diversions for TARGET=H: 9xx @@ -43,6 +47,7 @@ m4_define(FID_DUMP, `FID_ZONE(6, Dump line)') m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)') m4_define(FID_POSTFIXIFY, `FID_ZONE(8, Postfixify)') m4_define(FID_SAME, `FID_ZONE(9, Comparison)') +m4_define(FID_INTERPRET, `FID_ZONE(10, Interpret)') m4_define(FID_STRUCT_IN, `m4_divert(101)') m4_define(FID_NEW_ARGS, `m4_divert(102)') @@ -51,10 +56,14 @@ m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])') m4_define(FID_POSTFIXIFY_BODY, `m4_divert(105)m4_define([[FID_POSTFIXIFY_BODY_EXISTS]])') m4_define(FID_SAME_BODY, `m4_divert(106)') m4_define(FID_LINE_IN, `m4_divert(107)') +m4_define(FID_INTERPRET_BODY, `m4_divert(108)') -m4_define(FID_ALL, `m4_ifdef([[FID_CURDIV]], [[m4_divert(FID_CURDIV)m4_undefine([[FID_CURDIV]])]])') -m4_define(FID_C, `m4_ifelse(TARGET, [[C]], FID_ALL, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])') -m4_define(FID_H, `m4_ifelse(TARGET, [[H]], FID_ALL, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])') +m4_define(FID_ALL, `FID_INTERPRET_BODY'); + +m4_define(FID_ALL_TARGETS, `m4_ifdef([[FID_CURDIV]], [[m4_divert(FID_CURDIV)m4_undefine([[FID_CURDIV]])]])') +m4_define(FID_C, `m4_ifelse(TARGET, [[C]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])') +m4_define(FID_I, `m4_ifelse(TARGET, [[I]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])') +m4_define(FID_H, `m4_ifelse(TARGET, [[H]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])') m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[ @@ -101,7 +110,7 @@ m4_undefine([[FID_DUMP_BODY_EXISTS]]) m4_undivert(104) #undef item } -FID_ALL +FID_ALL_TARGETS FID_POSTFIXIFY case INST_NAME(): { @@ -125,10 +134,23 @@ m4_undivert(106) #undef f2 break; +FID_INTERPRET +case INST_NAME(): +#define whati (&(what->i_]]INST_NAME()[[)) +m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (vstk.cnt < INST_INVAL()) runtime("Stack underflow"); vstk.cnt -= INST_INVAL(); ]]) +m4_undivert(108) +#undef whati +break; + FID_END ]])') -m4_define(INST, `INST_FLUSH()m4_define([[INST_NAME]], [[$1]])') +m4_define(INST, `m4_dnl +INST_FLUSH()m4_dnl +m4_define([[INST_NAME]], [[$1]])m4_dnl +m4_define([[INST_INVAL]], [[$2]])m4_dnl +FID_ALL() m4_dnl +') m4_dnl FID_MEMBER call: m4_dnl type @@ -137,6 +159,7 @@ m4_dnl name in f_line_item m4_dnl comparator for same m4_dnl dump format string m4_dnl dump format args +m4_dnl interpreter body m4_define(FID_MEMBER, `m4_dnl FID_LINE_IN $1 $2; @@ -158,9 +181,13 @@ m4_ifelse($5,,,[[ FID_DUMP_BODY debug("%s$5\n", INDENT, $6); ]]) -FID_END') +m4_ifelse($7,,,[[ +FID_INTERPRET_BODY +$7 +]]) +FID_ALL') -m4_define(ARG, ` +m4_define(ARG_ANY, ` FID_STRUCT_IN const struct f_inst * f$1; FID_NEW_ARGS @@ -169,9 +196,24 @@ FID_NEW_BODY what->f$1 = f$1; for (const struct f_inst *child = f$1; child; child = child->next) what_->size += child->size; FID_POSTFIXIFY_BODY -pos = postfixify(dest, what->f$1, pos); -FID_END') -m4_define(ARG_ANY, `ARG($@)') +pos = postfixify(dest, what->f$1, pos);m4_dnl +FID_ALL()') + +m4_define(ARG, `ARG_ANY($1) +FID_INTERPRET_BODY +if (v$1.type != $2) runtime("Argument $1 of instruction %s must be of type $2, got 0x%02x", f_instruction_name(what->fi_code), v$1.type)m4_dnl +FID_ALL()') + +m4_define(LINEX, `FID_INTERPRET_BODY +do { + estk.item[estk.cnt].pos = 0; + estk.item[estk.cnt].line = $1; + estk.item[estk.cnt].ventry = vstk.cnt; + estk.item[estk.cnt].emask = 0; + estk.cnt++; +} while (0)m4_dnl +FID_ALL()') + m4_define(LINE, ` FID_LINE_IN const struct f_line * fl$1; @@ -187,17 +229,26 @@ FID_POSTFIXIFY_BODY item->fl$1 = f_postfixify(what->f$1); FID_SAME_BODY if (!f_same(f1->fl$1, f2->fl$1)) return 0; -FID_END') +FID_INTERPRET_BODY +do { if (whati->fl$1) { + LINEX(whati->fl$1); +} } while(0)m4_dnl +FID_ALL()') + +m4_define(RESULT_OK, `FID_INTERPRET_BODY()vstk.cnt++FID_ALL()') +m4_define(RESULT, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])') +m4_define(RESULT_VAL, `FID_INTERPRET_BODY()do { res = $1; RESULT_OK; } while (0)FID_ALL()') + m4_define(SYMBOL, `FID_MEMBER(const struct symbol *, sym, sym, -[[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], symbol %s, item->sym->name)') -m4_define(VAL, `FID_MEMBER(struct f_val $1, val, val m4_ifelse($1,,,[0]), [[!val_same(&f1->val, &f2->val)]], value %s, val_dump(&item->val))') -m4_define(FRET, `FID_MEMBER(enum filter_return, fret, fret, f1->fret != f2->fret, %s, filter_return_str(item->fret))') -m4_define(ECS, `FID_MEMBER(enum ec_subtype, ecs, ecs, f1->ecs != f2->ecs, ec subtype %s, ec_subtype_str(item->ecs))') -m4_define(RTC, `FID_MEMBER(const struct rtable_config *, rtc, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], route table %s, item->rtc->name)') -m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, sa, f1->sa.sa_code != f2->sa.sa_code)') -m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, da, f1->da.ea_code != f2->da.ea_code)') +[[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], symbol %s, item->sym->name, const struct symbol *sym = whati->sym)') +m4_define(VAL, `FID_MEMBER(struct f_val $1, val, val m4_ifelse($1,,,[0]), [[!val_same(&f1->val, &f2->val)]], value %s, val_dump(&item->val),)') +m4_define(FRET, `FID_MEMBER(enum filter_return, fret, fret, f1->fret != f2->fret, %s, filter_return_str(item->fret), enum filter_return fret = whati->fret)') +m4_define(ECS, `FID_MEMBER(enum ec_subtype, ecs, ecs, f1->ecs != f2->ecs, ec subtype %s, ec_subtype_str(item->ecs), enum ec_subtype ecs = whati->ecs)') +m4_define(RTC, `FID_MEMBER(const struct rtable_config *, rtc, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], route table %s, item->rtc->name, struct rtable *table = whati->rtc->table)') +m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, sa, f1->sa.sa_code != f2->sa.sa_code,,, struct f_static_attr sa = whati->sa)') +m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, da, f1->da.ea_code != f2->da.ea_code,,, struct f_dynamic_attr da = whati->da)') m4_define(COUNT, `FID_MEMBER(uint, count, count, f1->count != f2->count, number %u, item->count)') -m4_define(TREE, `FID_MEMBER(const struct f_tree *, tree, tree, [[!same_tree(f1->tree, f2->tree)]], tree %p, item->tree)') +m4_define(TREE, `FID_MEMBER(const struct f_tree *, tree, tree, [[!same_tree(f1->tree, f2->tree)]], tree %p, item->tree, const struct f_tree *tree = whati->tree)') m4_define(STRING, `FID_MEMBER(const char *, s, s, [[strcmp(f1->s, f2->s)]], string \"%s\", item->s)') m4_define(FID_WR_PUT_LIST) @@ -210,6 +261,8 @@ m4_define(FID_WR_DIRECT, `m4_define([[FID_WR_CUR_DIRECT]],$1)m4_ifelse(TARGET,[[ m4_dnl m4_define(FID_WR_CUR_DIRECT,m4_ifelse(TARGET,`C',800,TARGET,`H',900,m4_errprint(`Bad TARGET: 'TARGET)m4_m4exit(1))) m4_changequote([[,]]) +FID_WR_DIRECT(700,I) +FID_WR_PUT(10) FID_WR_DIRECT(800,C) #include "nest/bird.h" #include "filter/filter.h" diff --git a/filter/f-inst.c b/filter/f-inst.c index 27a4ab88..8505534a 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -117,7 +117,7 @@ dyn++; what->count = len; - FID_END + FID_ALL if (vstk.cnt < whati->count) /* TODO: make this check systematic */ runtime("Construction of BGP path mask from %u elements must have at least that number of elements", whati->count); @@ -259,7 +259,7 @@ if (!val_same(&(f1->val), &(f2->val))) return 0; FID_DUMP_BODY debug("%svalue %s\n", INDENT, val_dump(&item->val)); - FID_END + FID_ALL res = whati->val; RESULT_OK; @@ -289,7 +289,7 @@ default: bug("Symbol %s of type %d doesn't reference a value", item->sym->name, item->sym->class); } - FID_END + FID_ALL res = *whati->valp; RESULT_OK; @@ -309,7 +309,7 @@ FID_POSTFIXIFY_BODY { uint opos = pos; - FID_END + FID_ALL ARG_ANY(1); @@ -317,7 +317,7 @@ if (opos < pos) dest->items[pos].flags |= FIF_PRINTED; } - FID_END + FID_ALL FRET(2); @@ -773,7 +773,7 @@ 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_END + FID_ALL /* First push the body on stack */ LINEX(whati->body); diff --git a/filter/filter.c b/filter/filter.c index 8db5119b..ee5d5426 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -192,7 +192,7 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val) #define ACCESS_RTE do { if (!fs->rte) runtime("No route to access"); } while (0) #define ACCESS_EATTRS do { if (!fs->eattrs) f_cache_eattrs(fs); } while (0) -#include "filter/f-inst-interpret.c" +#include "filter/inst-interpret.c" #undef res #undef v1 #undef v2 diff --git a/filter/interpret.m4 b/filter/interpret.m4 deleted file mode 100644 index ea91e74e..00000000 --- a/filter/interpret.m4 +++ /dev/null @@ -1,80 +0,0 @@ -m4_divert(-1)m4_dnl -# -# BIRD -- Generator of Filter Instructions -# -# (c) 2018 Maria Matejka -# -# Can be freely distributed and used under the terms of the GNU GPL. -# - -# Common aliases -m4_define(DNL, `m4_dnl') - -m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[ -m4_divert(1) -case INST_NAME(): -#define whati (&(what->i_]]INST_NAME()[[)) -m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (vstk.cnt < INST_INVAL()) runtime("Stack underflow"); vstk.cnt -= INST_INVAL(); ]]) -m4_undivert(2) -#undef whati -break; -m4_divert(-1) -]])') - -m4_define(INST, `INST_FLUSH()m4_define([[INST_NAME]], [[$1]])m4_define([[INST_INVAL]], [[$2]])m4_divert(2)') - -m4_define(ARG, `if (v$1.type != $2) runtime("Argument $1 of instruction %s must be of type $2, got 0x%02x", f_instruction_name(what->fi_code), v$1.type)') - -m4_define(RESULT_OK, `vstk.cnt++') -m4_define(RESULT, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])') -m4_define(RESULT_VAL, `do { res = $1; RESULT_OK; } while (0)') - -m4_define(LINEX, `do { - estk.item[estk.cnt].pos = 0; - estk.item[estk.cnt].line = $1; - estk.item[estk.cnt].ventry = vstk.cnt; - estk.item[estk.cnt].emask = 0; - estk.cnt++; -} while (0)') - -m4_define(LINE, `do { - if (whati->fl$1) { - estk.item[estk.cnt].pos = 0; - estk.item[estk.cnt].line = whati->fl$1; - estk.item[estk.cnt].ventry = vstk.cnt; - estk.item[estk.cnt].emask = 0; - estk.cnt++; - } -} while (0)') - -m4_define(ARG_ANY, `') - -m4_define(SYMBOL, `const struct symbol *sym = whati->sym') - -m4_define(FRET, `enum filter_return fret = whati->fret') -m4_define(ECS, `enum ec_subtype ecs = whati->ecs') -m4_define(RTC, `struct rtable *table = whati->rtc->table') -m4_define(STATIC_ATTR, `struct f_static_attr sa = whati->sa') -m4_define(DYNAMIC_ATTR, `struct f_dynamic_attr da = whati->da') -m4_define(TREE, `const struct f_tree *tree = whati->tree') -m4_define(STRING, `') -m4_define(COUNT, `') -m4_define(SAME, `') -m4_define(FID_STRUCT_IN, `m4_divert(-1)') -m4_define(FID_LINE_IN, `m4_divert(-1)') -m4_define(FID_NEW_ARGS, `m4_divert(-1)') -m4_define(FID_NEW_BODY, `m4_divert(-1)') -m4_define(FID_POSTFIXIFY_BODY, `m4_divert(-1)') -m4_define(FID_SAME_BODY, `m4_divert(-1)') -m4_define(FID_DUMP_BODY, `m4_divert(-1)') -m4_define(FID_END, `m4_divert(2)') - -m4_m4wrap(` -INST_FLUSH() -m4_divert(0)DNL -m4_undivert(1) -default: bug( "Unknown instruction %d (%c)", what->fi_code, what->fi_code & 0xff); -') - -m4_changequote([[,]]) - diff --git a/filter/test.conf b/filter/test.conf index dc47eb7d..b9f1b3aa 100644 --- a/filter/test.conf +++ b/filter/test.conf @@ -1381,3 +1381,9 @@ protocol static vpn4 { table v4; import filter vpn_filter; }; route 0:1:2 10.1.10.0/24 unreachable; } + +protocol static +{ + ipv6 { import where false; }; + route fd01::/48 unreachable; +}