diff --git a/filter/config.Y b/filter/config.Y index 8916ea97..61ee19ca 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -658,6 +658,7 @@ bgp_path: bgp_path_tail: NUM bgp_path_tail { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .asn = $1, .kind = PM_ASN, }, }); $$->next = $2; } | NUM DDOT NUM bgp_path_tail { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .from = $1, .to = $3, .kind = PM_ASN_RANGE }, }); $$->next = $4; } + | '[' ']' bgp_path_tail { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .set = NULL, .kind = PM_ASN_SET }, }); $$->next = $3; } | '[' set_items ']' bgp_path_tail { if ($2->from.type != T_INT) cf_error("Only integer sets allowed in path mask"); $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .set = build_tree($2), .kind = PM_ASN_SET }, }); $$->next = $4; @@ -677,6 +678,7 @@ constant: | fipa { $$ = f_new_inst(FI_CONSTANT, $1); } | VPN_RD { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_RD, .val.ec = $1, }); } | net_ { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_NET, .val.net = $1, }); } + | '[' ']' { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_SET, .val.t = NULL, }); } | '[' set_items ']' { DBG( "We've got a set here..." ); $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_SET, .val.t = build_tree($2), }); diff --git a/filter/data.c b/filter/data.c index 56c1fb17..7295887d 100644 --- a/filter/data.c +++ b/filter/data.c @@ -301,6 +301,12 @@ val_same(const struct f_val *v1, const struct f_val *v2) int clist_set_type(const struct f_tree *set, struct f_val *v) { + if (!set) + { + v->type = T_VOID; + return 1; + } + switch (set->from.type) { case T_PAIR: @@ -537,6 +543,9 @@ val_in_range(const struct f_val *v1, const struct f_val *v2) if (v2->type != T_SET) return F_CMP_ERROR; + if (!v2->val.t) + return 0; + /* With integrated Quad<->IP implicit conversion */ if ((v1->type == v2->val.t->from.type) || ((v1->type == T_QUAD) && val_is_ip4(&(v2->val.t->from)) && val_is_ip4(&(v2->val.t->to)))) diff --git a/filter/data.h b/filter/data.h index 4cb6b7a8..84d48a7c 100644 --- a/filter/data.h +++ b/filter/data.h @@ -279,9 +279,11 @@ int val_in_range(const struct f_val *v1, const struct f_val *v2); int clist_set_type(const struct f_tree *set, struct f_val *v); static inline int eclist_set_type(const struct f_tree *set) -{ return set->from.type == T_EC; } +{ return !set || set->from.type == T_EC; } static inline int lclist_set_type(const struct f_tree *set) -{ return set->from.type == T_LC; } +{ return !set || set->from.type == T_LC; } +static inline int path_set_type(const struct f_tree *set) +{ return !set || set->from.type == T_INT; } const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos); const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos); diff --git a/filter/decl.m4 b/filter/decl.m4 index 5242c04c..75e65212 100644 --- a/filter/decl.m4 +++ b/filter/decl.m4 @@ -494,6 +494,15 @@ f_const_promotion(struct f_inst *arg, enum f_type want) }; return 1; } + else if ((c->type == T_SET) && (want == T_PREFIX_SET)) { + if (c->val.t) + return 0; + *c = (struct f_val) { + .type = T_PREFIX_SET, + .val.ti = f_new_trie(cfg_mem, 0), + }; + return 1; + } return 0; } diff --git a/filter/f-inst.c b/filter/f-inst.c index 901d2939..999b7fc7 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -349,7 +349,7 @@ break; case T_SET: - if (vv(i).val.t->from.type != T_INT) + if (!path_set_type(vv(i).val.t)) runtime("Only integer sets allowed in path mask"); pm->item[i] = (struct f_path_mask_item) { @@ -983,7 +983,7 @@ RESULT(T_INT, i, v1.val.lc.ldp2); } - INST(FI_MIN, 1, 1) { /* Get minimum element from set */ + INST(FI_MIN, 1, 1) { /* Get minimum element from list */ ARG_ANY(1); RESULT_TYPE(f_type_element_type(v1.type)); switch(v1.type) @@ -1017,7 +1017,7 @@ } } - INST(FI_MAX, 1, 1) { /* Get maximum element from set */ + INST(FI_MAX, 1, 1) { /* Get maximum element from list */ ARG_ANY(1); RESULT_TYPE(f_type_element_type(v1.type)); switch(v1.type) @@ -1215,15 +1215,18 @@ { const struct f_tree *set = NULL; u32 key = 0; + int use_set = 0; if (v2.type == T_INT) key = v2.val.i; - else if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT)) + else if ((v2.type == T_SET) && path_set_type(v2.val.t)) { + use_set = 1; set = v2.val.t; + } else runtime("Can't delete non-integer (set)"); - RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, set, key, 0) ]]); + RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, set, key, use_set, 0) ]]); } else if (v1.type == T_CLIST) @@ -1275,12 +1278,20 @@ if (v1.type == T_PATH) { + const struct f_tree *set = NULL; u32 key = 0; + int use_set = 0; - if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT)) - RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, v2.val.t, key, 1) ]]); + if (v2.type == T_INT) + key = v2.val.i; + else if ((v2.type == T_SET) && path_set_type(v2.val.t)) { + use_set = 1; + set = v2.val.t; + } else - runtime("Can't filter integer"); + runtime("Can't filter non-integer (set)"); + + RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, set, key, use_set, 1) ]]); } else if (v1.type == T_CLIST) diff --git a/filter/test.conf b/filter/test.conf index 484628e5..65c680cd 100644 --- a/filter/test.conf +++ b/filter/test.conf @@ -18,6 +18,7 @@ protocol device { } define one = 1; define ten = 10; +define empty_set = []; function onef(int a) { @@ -130,12 +131,22 @@ define is3 = [5, 17, 2, 11, 8, 15, 3, 19]; function t_int_set() int set is; { + bt_assert([] = empty_set); + is = []; + is = empty_set; + bt_assert(is = empty_set); + bt_assert(is = []); + bt_assert(0 !~ is); + bt_assert(1 ~ [1,2,3]); bt_assert(5 ~ [1..20]); bt_assert(2 ~ [ 1, 2, 3 ]); bt_assert(5 ~ [ 4 .. 7 ]); bt_assert(1 !~ [ 2, 3, 4 ]); bt_assert(999 !~ [ 666, 333 ]); + bt_assert(1 !~ []); + bt_assert(1 !~ empty_set); + bt_assert(1 !~ is); is = [ 2, 3, 4, 7..11 ]; bt_assert(10 ~ is); @@ -170,6 +181,7 @@ int set is; bt_assert([1,4..10,20] = [1,4..10,20]); bt_assert(format([ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ]) = "[1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5]"); + bt_assert(format([]) = "[]"); } bt_test_suite(t_int_set, "Testing sets of integers"); @@ -236,6 +248,9 @@ function t_pair_set() pair pp; pair set ps; { + ps = []; + ps = empty_set; + pp = (1, 2); ps = [(1,(one+one)), (3,4)..(4,8), (5,*), (6,3..6)]; bt_assert(format(ps) = "[(1,2), (3,4)..(4,8), (5,0)..(5,65535), (6,3)..(6,6)]"); @@ -253,6 +268,8 @@ pair set ps; bt_assert((6,6+one) !~ ps); bt_assert(((one+6),2) !~ ps); bt_assert((1,1) !~ ps); + bt_assert(pp !~ []); + bt_assert(pp !~ empty_set); ps = [(20..150, 200..300), (50100..50200, 1000..50000), (*, 5+5)]; bt_assert((100,200) ~ ps); @@ -304,6 +321,8 @@ quad qq; qq = 1.2.3.4; bt_assert(qq ~ [1.2.3.4, 5.6.7.8]); bt_assert(qq !~ [1.2.1.1, 1.2.3.5]); + bt_assert(qq !~ []); + bt_assert(qq !~ empty_set); } bt_test_suite(t_quad_set, "Testing sets of quads"); @@ -372,6 +391,9 @@ define ip1222 = 1.2.2.2; function t_ip_set() ip set ips; { + ips = []; + ips = empty_set; + ips = [ 1.1.1.0 .. 1.1.1.255, ip1222]; bt_assert(format(ips) = "[1.1.1.0..1.1.1.255, 1.2.2.2]"); bt_assert(1.1.1.0 ~ ips); @@ -384,6 +406,8 @@ ip set ips; bt_assert(1.2.3.4 !~ [ 1.2.3.3, 1.2.3.5 ]); bt_assert(1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ]); + bt_assert(1.2.3.4 !~ []); + bt_assert(1.2.3.4 !~ empty_set); } bt_test_suite(t_ip_set, "Testing sets of ip address"); @@ -473,6 +497,8 @@ function test_pxset(prefix set pxs) bt_assert(1.0.0.0/8 ~ [ 1.0.0.0/8+ ]); bt_assert(1.0.0.0/9 !~ [ 1.0.0.0/8- ]); bt_assert(1.2.0.0/17 !~ [ 1.0.0.0/8{ 15 , 16 } ]); + bt_assert(net10 !~ []); + bt_assert(net10 !~ empty_set); bt_assert([ 10.0.0.0/8{ 15 , 17 } ] = [ 10.0.0.0/8{ 15 , 17 } ]); } @@ -480,6 +506,15 @@ function test_pxset(prefix set pxs) function t_prefix_set() prefix set pxs; { + pxs = []; + pxs = empty_set; + bt_assert(format(pxs) = "[]"); + #bt_assert(pxs = []); + #bt_assert(pxs = empty_set); + bt_assert(1.2.0.0/16 !~ []); + bt_assert(1.2.0.0/16 !~ empty_set); + bt_assert(1.2.0.0/16 !~ pxs); + pxs = [ 1.2.0.0/16, 1.4.0.0/16+, 44.66.88.64/30{24,28}, 12.34.56.0/24{8,16} ]; bt_assert(format(pxs) = "[1.2.0.0/16{0.1.0.0}, 1.4.0.0/16{0.1.255.255}, 12.34.0.0/16{1.255.0.0}, 44.66.88.64/28{0.0.1.240}]"); @@ -564,6 +599,15 @@ bt_test_suite(t_prefix6, "Testing prefix IPv6"); function t_prefix6_set() prefix set pxs; { + pxs = []; + pxs = empty_set; + bt_assert(format(pxs) = "[]"); + #bt_assert(pxs = []); + #bt_assert(pxs = empty_set); + bt_assert(12::34/128 !~ []); + bt_assert(12::34/128 !~ empty_set); + bt_assert(12::34/128 !~ pxs); + bt_assert(1180::/16 ~ [ 1100::/8{15, 17} ]); bt_assert(12::34 = 12::34); bt_assert(12::34 ~ [ 12::33..12::35 ]); @@ -681,6 +725,8 @@ int set set12; bt_assert(3 ~ p2); bt_assert(p2 ~ [2, 10..20]); bt_assert(p2 ~ [4, 10..20]); + bt_assert(p2 !~ []); + bt_assert(p2 !~ empty_set); p2 = prepend(p2, 5); bt_assert(p2 !~ pm1); @@ -691,6 +737,9 @@ int set set12; bt_assert(p2 ~ [= 5 [2, 4, 6] 3 [1..2] 1 =]); bt_assert(p2 ~ [= 5 set35 3 set12 set12 =]); bt_assert(p2 ~ mkpath(5, 4)); + bt_assert(p2 ~ [= * [3] * =]); + bt_assert(p2 !~ [= * [] * =]); + bt_assert(p2 !~ [= * empty_set * =]); bt_assert(p2.len = 5); bt_assert(p2.first = 5); @@ -699,6 +748,10 @@ int set set12; bt_assert(p2.len = 5); bt_assert(delete(p2, 3) = prepend(prepend(prepend(prepend(+empty+, 1), 2), 4), 5)); bt_assert(filter(p2, [1..3]) = prepend(prepend(prepend(+empty+, 1), 2), 3)); + bt_assert(delete(p2, []) = p2); + bt_assert(filter(p2, []) = +empty+); + bt_assert(delete(prepend(prepend(+empty+, 0), 1), []) = prepend(prepend(+empty+, 0), 1)); + bt_assert(filter(prepend(prepend(+empty+, 0), 1), []) = +empty+); p2 = prepend( + empty +, 5 ); p2 = prepend( p2, 4 ); @@ -759,6 +812,8 @@ clist r; bt_assert(l ~ [(2,2..3)]); bt_assert(l ~ [(1,1..2)]); bt_assert(l ~ [(1,1)..(1,2)]); + bt_assert(l !~ []); + bt_assert(l !~ empty_set); l = add(l, (2,5)); l = add(l, (5,one)); @@ -796,6 +851,9 @@ clist r; bt_assert(l !~ [(*,(one+6))]); bt_assert(l !~ [(*, (one+one+one))]); + bt_assert(delete(l, []) = l); + bt_assert(filter(l, []) = -empty-); + l = delete(l, [(*,(one+onef(3)))]); l = delete(l, [(*,(4+one))]); bt_assert(l = add(-empty-, (3,1))); @@ -913,11 +971,16 @@ eclist r; bt_assert((ro, 10.20.30.40, 100) !~ el); bt_assert(el !~ [(rt, 10, 35..40)]); bt_assert(el !~ [(ro, 10, *)]); + bt_assert(el !~ []); + bt_assert(el !~ empty_set); el = add(el, (rt, 10, 40)); el2 = filter(el, [(rt, 10, 20..40)] ); el2 = add(el2, (rt, 10, 50)); + bt_assert(delete(el, []) = el); + bt_assert(filter(el, []) = --empty--); + # eclist A (1,30,40) bt_assert(el = add(add(add(--empty--, (rt, 10, 1)), (rt, 10, 30)), (rt, 10, 40))); bt_assert(format(el) = "(eclist (rt, 10, 1) (rt, 10, 30) (rt, 10, 40))"); @@ -1035,6 +1098,9 @@ lclist r; ll2 = add(ll2, (30, 30, 30)); ll2 = add(ll2, (40, 40, 40)); + bt_assert(delete(ll, []) = ll); + bt_assert(filter(ll, []) = ---empty---); + # lclist A (10, 20, 30) bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))"); @@ -1094,6 +1160,8 @@ lc set lls; bt_assert(ll !~ [(5,10,15), (10,21,30)]); bt_assert(ll !~ [(10,21..25,*)]); bt_assert(ll !~ [(11, *, *)]); + bt_assert(ll !~ []); + bt_assert(ll !~ empty_set); lls = [(10, 10, 10), (20, 20, 15..25), (30, 30, *), (40, 35..45, *), (50, *, *), (55..65, *, *)]; bt_assert(format(lls) = "[(10, 10, 10), (20, 20, 15)..(20, 20, 25), (30, 30, 0)..(30, 30, 4294967295), (40, 35, 0)..(40, 45, 4294967295), (50, 0, 0)..(50, 4294967295, 4294967295), (55, 0, 0)..(65, 4294967295, 4294967295)]"); @@ -1150,6 +1218,12 @@ bt_test_suite(t_rd, "Testing route distinguishers"); function t_rd_set() rd set rds; { + rds = []; + rds = empty_set; + bt_assert(rds = []); + bt_assert(rds = empty_set); + bt_assert(10:20 !~ rds); + rds = [10:20, 100000:100..100000:200]; bt_assert(format(rds) = "[10:20, 100000:100..100000:200]"); @@ -1160,6 +1234,7 @@ rd set rds; bt_assert(100000:128 ~ rds); bt_assert(100000:200 ~ rds); bt_assert(100010:150 !~ rds); + bt_assert(100010:150 !~ []); } bt_test_suite(t_rd_set, "Testing sets of route distinguishers"); diff --git a/nest/a-path.c b/nest/a-path.c index 2e34a3d1..ed4457b0 100644 --- a/nest/a-path.c +++ b/nest/a-path.c @@ -602,7 +602,7 @@ as_path_match_set(const struct adata *path, const struct f_tree *set) } const struct adata * -as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int pos) +as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int use_set, int pos) { if (!path) return NULL; @@ -629,7 +629,7 @@ as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tr u32 as = get_as(p); int match; - if (set) + if (use_set) { struct f_val v = {T_INT, .val.i = as}; match = !!find_tree(set, &v); diff --git a/nest/a-path_test.c b/nest/a-path_test.c index 9ed0a786..e02c6fd0 100644 --- a/nest/a-path_test.c +++ b/nest/a-path_test.c @@ -136,8 +136,8 @@ t_path_include(void) int counts_of_contains = count_asn_in_array(as_nums, as_nums[i]); bt_assert_msg(as_path_contains(as_path, as_nums[i], counts_of_contains), "AS Path should contains %d-times number %d", counts_of_contains, as_nums[i]); - bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0) != NULL); - bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 1) != NULL); + bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0, 0) != NULL); + bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0, 1) != NULL); } for (i = 0; i < 10000; i++) diff --git a/nest/attrs.h b/nest/attrs.h index ef2b95e6..c92a8826 100644 --- a/nest/attrs.h +++ b/nest/attrs.h @@ -49,7 +49,7 @@ int as_path_get_last(const struct adata *path, u32 *last_as); u32 as_path_get_last_nonaggregated(const struct adata *path); int as_path_contains(const struct adata *path, u32 as, int min); int as_path_match_set(const struct adata *path, const struct f_tree *set); -const struct adata *as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int pos); +const struct adata *as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int use_set, int pos); static inline struct adata *as_path_prepend(struct linpool *pool, const struct adata *path, u32 as) { return as_path_prepend2(pool, path, AS_PATH_SEQUENCE, as); }