0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 01:31:55 +00:00

Filter: merged filter compare functions into common M4 file

This commit is contained in:
Maria Matejka 2019-02-13 12:25:30 +01:00
parent dd4d409551
commit 132529ce89
10 changed files with 134 additions and 138 deletions

View File

@ -10,16 +10,13 @@ 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)f-inst-same.c: $(s)same.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)f-inst-same.c $(o)inst-gen.h
$(o)filter.o: $(o)f-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)

View File

@ -441,7 +441,7 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
PREPEND, FIRST, LAST, LAST_NONAGGREGATED, MATCH,
EMPTY,
FILTER, WHERE, EVAL, ATTRIBUTE,
BT_ASSERT, BT_TEST_SUITE, BT_CHECK_ASSIGN, FORMAT)
BT_ASSERT, BT_TEST_SUITE, BT_CHECK_ASSIGN, BT_TEST_SAME, FORMAT)
%nonassoc THEN
%nonassoc ELSE
@ -489,7 +489,7 @@ custom_attr: ATTRIBUTE type CF_SYM_VOID ';' {
conf: bt_test_suite ;
bt_test_suite:
BT_TEST_SUITE '(' CF_SYM_FUNCTION ',' text ')' {
struct f_bt_test_suite *t = cfg_alloc(sizeof(struct f_bt_test_suite));
struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite));
t->fn = $3->def;
t->fn_name = $3->name;
t->dsc = $5;
@ -498,6 +498,19 @@ bt_test_suite:
}
;
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->result = $7;
t->fn_name = $3->name;
t->dsc = $5->name;
add_tail(&new_config->tests, &t->n);
}
;
type:
INT { $$ = T_INT; }
| BOOL { $$ = T_BOOL; }

View File

@ -146,34 +146,41 @@ val_compare(const struct f_val *v1, const struct f_val *v2)
}
}
static inline int
pmi_same(const struct f_path_mask_item *mi1, const struct f_path_mask_item *mi2)
{
if (mi1->kind != mi2->kind)
return 0;
switch (mi1->kind) {
case PM_ASN:
if (mi1->asn != mi2->asn)
return 0;
break;
case PM_ASN_EXPR:
if (!f_same(mi1->expr, mi2->expr))
return 0;
break;
case PM_ASN_RANGE:
if (mi1->from != mi2->from)
return 0;
if (mi1->to != mi2->to)
return 0;
break;
}
return 1;
}
static int
pm_same(const struct f_path_mask *m1, const struct f_path_mask *m2)
{
if (m1->len != m2->len)
for (uint i=0; i<m1->len; i++)
{
if (m1->item[i].kind != m2->item[i].kind)
if (!pmi_same(&(m1->item[i]), &(m2->item[i])))
return 0;
switch (m1->item[i].kind) {
case PM_ASN:
if (m1->item[i].asn != m2->item[i].asn)
return 0;
break;
case PM_ASN_EXPR:
if (!f_same(m1->item[i].expr, m2->item[i].expr))
return 0;
break;
case PM_ASN_RANGE:
if (m1->item[i].from != m2->item[i].from)
return 0;
if (m1->item[i].to != m2->item[i].to)
return 0;
break;
}
}
return 1;
}
@ -200,6 +207,8 @@ val_same(const struct f_val *v1, const struct f_val *v2)
switch (v1->type) {
case T_PATH_MASK:
return pm_same(v1->val.path_mask, v2->val.path_mask);
case T_PATH_MASK_ITEM:
return pmi_same(&(v1->val.pmi), &(v2->val.pmi));
case T_PATH:
case T_CLIST:
case T_ECLIST:

View File

@ -13,6 +13,7 @@ m4_divert(-1)m4_dnl
# 6 dump line item
# 7 dump line item callers
# 8 postfixify
# 9 same (filter comparator)
# 1 struct f_inst_FI_...
# 2 union in struct f_inst
# 3 constructors
@ -23,6 +24,7 @@ m4_divert(-1)m4_dnl
# 103 constructor body
# 104 dump line item content
# 105 postfixify body
# 106 comparator body
#
# Diversions for TARGET=C: 8xx
# Diversions for TARGET=H: 9xx
@ -41,12 +43,14 @@ m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
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_STRUCT_IN, `m4_divert(101)')
m4_define(FID_NEW_ARGS, `m4_divert(102)')
m4_define(FID_NEW_BODY, `m4_divert(103)')
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_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)]])')
@ -105,6 +109,11 @@ m4_ifdef([[FID_POSTFIXIFY_BODY_EXISTS]], [[const struct f_inst_]]INST_NAME()[[ *
}
m4_undefine([[FID_POSTFIXIFY_BODY_EXISTS]])
FID_SAME
case INST_NAME():
m4_undivert(106)
break;
FID_END
]])')
@ -114,6 +123,7 @@ m4_dnl FID_MEMBER call:
m4_dnl type
m4_dnl name in f_inst
m4_dnl name in f_line_item
m4_dnl comparator for same
m4_dnl dump format string
m4_dnl dump format args
m4_define(FID_MEMBER, `m4_dnl
@ -128,24 +138,28 @@ FID_POSTFIXIFY_BODY
dest->items[pos].$3 = what->$2;
]])
m4_ifelse($4,,,[[
FID_SAME_BODY
if ($4) return 0;
]])
m4_ifelse($5,,,[[
FID_DUMP_BODY
debug("%s$4\n", INDENT, $5);
debug("%s$5\n", INDENT, $6);
]])
FID_END')
m4_define(ARG, `FID_MEMBER(const struct f_inst *, f$1,,,)
m4_define(ARG, `FID_MEMBER(const struct f_inst *, f$1,,,,)
FID_NEW_BODY
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, `FID_MEMBER(const struct f_inst *, f$1,,,)
m4_define(ARG_ANY, `FID_MEMBER(const struct f_inst *, f$1,,,,)
FID_NEW_BODY
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(LINE, `FID_MEMBER(const struct f_inst *, f$1,,,)
m4_define(LINE, `FID_MEMBER(const struct f_inst *, f$1,,[[!f_same(f1->lines[$2], f2->lines[$2])]],,)
FID_DUMP_BODY
f_dump_line(item->lines[$2], indent + 1);
FID_POSTFIXIFY_BODY
@ -157,10 +171,13 @@ FID_DUMP_BODY
f_dump_line(item->lines[$2], indent + 1)
FID_POSTFIXIFY_BODY
dest->items[pos].lines[$2] = what->fl$1;
FID_SAME_BODY
if (!f_same(f1->lines[$2], f2->lines[$2])) return 0;
FID_END')
m4_define(SYMBOL, `FID_MEMBER(const struct symbol *, sym, sym, symbol %s, item->sym->name)')
m4_define(VALI, `FID_MEMBER(struct f_val, vali, val, value %s, val_dump(&item->val))')
m4_define(VALP, `FID_MEMBER(const struct f_val *, valp,, value %s, val_dump(&item->val))
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(VALI, `FID_MEMBER(struct f_val, vali, val, [[!val_same(&f1->val, &f2->val)]], value %s, val_dump(&item->val))')
m4_define(VALP, `FID_MEMBER(const struct f_val *, valp,, [[!val_same(&f1->val, &f2->val)]], value %s, val_dump(&item->val))
FID_POSTFIXIFY_BODY
dest->items[pos].val = *(what->valp);
FID_END')
@ -174,17 +191,19 @@ FID_NEW_BODY
what->valp = (what->sym = sym)->def;
FID_POSTFIXIFY_BODY
dest->items[pos].vp = (dest->items[pos].sym = what->sym)->def;
FID_SAME_BODY
if (strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)) return 0;
FID_DUMP_BODY
debug("%svariable %s with value %s\n", INDENT, item->sym->name, val_dump(item->vp));
FID_END')
m4_define(FRET, `FID_MEMBER(enum filter_return, fret, fret, %s, filter_return_str(item->fret))')
m4_define(ECS, `FID_MEMBER(enum ec_subtype, ecs, ecs, ec subtype %s, ec_subtype_str(item->ecs))')
m4_define(RTC, `FID_MEMBER(const struct rtable_config *, rtc, rtc, route table %s, item->rtc->name)')
m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, sa)')
m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, da)')
m4_define(COUNT, `FID_MEMBER(uint, count, count, number %u, item->count)')
m4_define(TREE, `FID_MEMBER(const struct f_tree *, tree, tree, tree %p, item->tree)')
m4_define(STRING, `FID_MEMBER(const char *, s, s, string \"%s\", item->s)')
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)')
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(STRING, `FID_MEMBER(const char *, s, s, [[strcmp(f1->s, f2->s)]], string \"%s\", item->s)')
m4_define(FID_WR_PUT_LIST)
m4_define(FID_WR_DROP_LIST)
@ -275,6 +294,32 @@ f_postfixify_concat(const struct f_inst * const inst[], uint count)
return out;
}
/* Filter line comparison */
int
f_same(const struct f_line *fl1, const struct f_line *fl2)
{
if ((!fl1) && (!fl2))
return 1;
if ((!fl1) || (!fl2))
return 0;
if (fl1->len != fl2->len)
return 0;
for (uint i=0; i<fl1->len; i++) {
#define f1 (&(fl1->items[i]))
#define f2 (&(fl2->items[i]))
if (f1->fi_code != f2->fi_code)
return 0;
if (f1->flags != f2->flags)
return 0;
switch(f1->fi_code) {
FID_WR_PUT(9)
}
}
return 1;
}
FID_WR_DIRECT(900,H)
/* Filter instruction codes */
enum f_instruction_code {

View File

@ -730,6 +730,8 @@
if (count != sym->aux2)
cf_error("Function %s takes %u arguments, got %u.", sym->name, sym->aux2, count);
FID_END
/* FIXME: Optimization of function comparison. */
}
INST(FI_DROP_RESULT, 1, 0) {

View File

@ -82,8 +82,10 @@ extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
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 char *fn_name; /* Name of test */
const char *dsc; /* Description */
int result; /* Desired result */
};
#endif

View File

@ -220,33 +220,6 @@ interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val)
}
/*
* f_same - function that does real comparing of instruction trees, you should call filter_same from outside
*/
int
f_same(const struct f_line *fl1, const struct f_line *fl2)
{
if ((!fl1) && (!fl2))
return 1;
if ((!fl1) || (!fl2))
return 0;
if (fl1->len != fl2->len)
return 0;
for (uint i=0; i<fl1->len; i++) {
#define f1 (&(fl1->items[i]))
#define f2 (&(fl2->items[i]))
if (f1->fi_code != f2->fi_code)
return 0;
if (f1->flags != f2->flags)
return 0;
switch(f1->fi_code) {
#include "filter/f-inst-same.c"
}
}
return 1;
}
/**
* f_run - run a filter for a route
* @filter: filter to run

View File

@ -42,13 +42,22 @@ parse_config_file(const void *argv)
}
static int
run_function(const void *parsed_fn_def)
run_function(const void *arg)
{
const struct f_line *f = (const struct f_line *) parsed_fn_def;
const struct f_bt_test_suite *t = arg;
if (t->cmp)
return t->result == f_same(t->fn, t->cmp);
if (!f_same(t->fn, t->fn)) {
bt_result = bt_suite_result = 0;
bt_log_suite_case_result(0, "The function doesn't compare to itself as the same");
return 0;
}
linpool *tmp = lp_new_default(&root_pool);
struct f_val res;
enum filter_return fret = f_eval(f, tmp, &res);
enum filter_return fret = f_eval(t->fn, tmp, &res);
rfree(tmp);
return (fret < F_REJECT);
@ -79,6 +88,7 @@ main(int argc, char *argv[])
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_test_suite_base(parse_config_file, "reconf", (const void *) &pcfa, 0, 0, "reconfigure with the same file");
bt_bird_cleanup();
@ -86,7 +96,7 @@ main(int argc, char *argv[])
{
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);
bt_test_suite_base(run_function, t->fn_name, t, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
}
return bt_exit_value();

View File

@ -1,63 +0,0 @@
m4_divert(-1)m4_dnl
#
# BIRD -- Filter Comparator Generator
#
# (c) 2018 Maria Matejka <mq@jmq.cz>
#
# 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():
m4_undivert(2)
break;
m4_divert(-1)
]])')
m4_define(INST, `INST_FLUSH()m4_define([[INST_NAME]], [[$1]])')
m4_define(ARG, `')
m4_define(ARG_ANY, `')
m4_define(LINE, `m4_divert(2)if (!f_same(f1->lines[$2], f2->lines[$2])) return 0;
m4_divert(-1)')
m4_define(LINEP, LINE)
m4_define(SYMBOL, `m4_divert(2){
const struct symbol *s1 = f1->sym, *s2 = f2->sym;
if (strcmp(s1->name, s2->name)) return 0;
if (s1->class != s2->class) return 0;
}
m4_divert(-1)')
m4_define(VALI, `m4_divert(2)if (!val_same(f1->vp, f2->vp)) return 0;
m4_divert(-1)')
m4_define(VALP, `m4_divert(2)if (!val_same(f1->vp, f2->vp)) return 0;
m4_divert(-1)')
m4_define(VAR, `SYMBOL()VALP()')
m4_define(FRET, `m4_divert(2)if (f1->fret != f2->fret) return 0;
m4_divert(-1)')
m4_define(ECS, `m4_divert(2)if (f1->ecs != f2->ecs) return 0;
m4_divert(-1)')
m4_define(RTC, `m4_divert(2)if (strcmp(f1->rtc->name, f2->rtc->name)) return 0;
m4_divert(-1)')
m4_define(STATIC_ATTR, `m4_divert(2)if (f1->sa.sa_code != f2->sa.sa_code) return 0;
m4_divert(-1)')
m4_define(DYNAMIC_ATTR, `m4_divert(2)if (f1->da.ea_code != f2->da.ea_code) return 0;
m4_divert(-1)')
m4_define(SAME, `m4_divert(2)$1m4_divert(-1)')
m4_m4wrap(`
INST_FLUSH()
m4_divert(0)DNL
m4_undivert(1)
default: bug( "Unknown instruction %d (%c)", f1->fi_code, f1->fi_code & 0xff);
')
m4_changequote([[,]])

View File

@ -25,8 +25,19 @@ function onef(int a)
return 1;
}
function twof(int a)
{
return 2;
}
function oneg(int a)
{
return 1;
}
bt_test_same(onef, onef, 1);
bt_test_same(onef, oneg, 1);
bt_test_same(onef, twof, 0);
/*
* Testing boolean expressions
@ -62,7 +73,6 @@ bt_test_suite(t_bool, "Testing boolean expressions");
/*
* Testing integers
* ----------------
@ -1347,8 +1357,6 @@ 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);