0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

Filter: Methods rework

Methods can now be called as x.m(y), as long as x can have its type
inferred in config time. If used as a command, it modifies the object,
if used as a value, it keeps the original object intact.

Also functions add(x,y), delete(x,y), filter(x,y) and prepend(x,y) now
spit a warning and are considered deprecated.

It's also possible to call a method on a constant, see filter/test.conf
for examples like bgp_path = +empty+.prepend(1).

Inside instruction definitions (filter/f-inst.c), a METHOD_CONSTRUCTOR()
call is added, which registers the instruction as a method for the type
of its first argument. Each type has its own method symbol table and
filter parser switches between them based on the inferred type of the
object calling the method.

Also FI_CLIST_(ADD|DELETE|FILTER) instructions have been split to allow
for this method dispatch. With type inference, it's now possible.
This commit is contained in:
Maria Matejka 2023-06-16 17:35:37 +02:00 committed by Igor Putovny
parent 44bac39361
commit 8ac15bf7a4
11 changed files with 805 additions and 282 deletions

View File

@ -54,7 +54,6 @@
struct keyword { struct keyword {
byte *name; byte *name;
int value; int value;
enum keyword_scope scope;
}; };
#include "conf/keywords.h" #include "conf/keywords.h"
@ -80,7 +79,8 @@ static uint cf_hash(const byte *c);
HASH_DEFINE_REHASH_FN(SYM, struct symbol) HASH_DEFINE_REHASH_FN(SYM, struct symbol)
struct sym_scope *global_root_scope; struct sym_scope *global_root_scope;
static pool *global_root_scope_pool; pool *global_root_scope_pool;
linpool *global_root_scope_linpool;
linpool *cfg_mem; linpool *cfg_mem;
@ -568,7 +568,7 @@ check_eof(void)
static inline void cf_swap_soft_scope(struct config *conf); static inline void cf_swap_soft_scope(struct config *conf);
static struct symbol * struct symbol *
cf_new_symbol(struct sym_scope *scope, pool *p, struct linpool *lp, const byte *c) cf_new_symbol(struct sym_scope *scope, pool *p, struct linpool *lp, const byte *c)
{ {
if (scope->readonly) if (scope->readonly)
@ -697,6 +697,8 @@ cf_lex_symbol(const char *data)
cf_lval.i = -val; cf_lval.i = -val;
return ENUM; return ENUM;
} }
case SYM_METHOD:
return sym->method->arg_num ? CF_SYM_METHOD_ARGS : CF_SYM_METHOD_BARE;
case SYM_VOID: case SYM_VOID:
return CF_SYM_UNDEFINED; return CF_SYM_UNDEFINED;
default: default:
@ -704,6 +706,8 @@ cf_lex_symbol(const char *data)
} }
} }
void f_type_methods_register(void);
/** /**
* cf_lex_init - initialize the lexer * cf_lex_init - initialize the lexer
* @is_cli: true if we're going to parse CLI command, false for configuration * @is_cli: true if we're going to parse CLI command, false for configuration
@ -718,18 +722,19 @@ cf_lex_init(int is_cli, struct config *c)
if (!global_root_scope_pool) if (!global_root_scope_pool)
{ {
global_root_scope_pool = rp_new(&root_pool, "Keywords pool"); global_root_scope_pool = rp_new(&root_pool, "Keywords pool");
linpool *kwlp = lp_new(global_root_scope_pool); global_root_scope_linpool = lp_new(global_root_scope_pool);
global_root_scope = lp_allocz(kwlp, sizeof(*global_root_scope) * CFK__MAX); global_root_scope = lp_allocz(global_root_scope_linpool, sizeof(*global_root_scope));
for (const struct keyword *k = keyword_list; k->name; k++) for (const struct keyword *k = keyword_list; k->name; k++)
{ {
struct symbol *sym = cf_new_symbol(&global_root_scope[k->scope], global_root_scope_pool, kwlp, k->name); struct symbol *sym = cf_new_symbol(global_root_scope, global_root_scope_pool, global_root_scope_linpool, k->name);
sym->class = SYM_KEYWORD; sym->class = SYM_KEYWORD;
sym->keyword = k; sym->keyword = k;
} }
for (int s = 0; s < CFK__MAX; s++) global_root_scope->readonly = 1;
global_root_scope[s].readonly = 1;
f_type_methods_register();
} }
ifs_head = ifs = push_ifs(NULL); ifs_head = ifs = push_ifs(NULL);
@ -753,7 +758,7 @@ cf_lex_init(int is_cli, struct config *c)
if (is_cli) if (is_cli)
c->current_scope->next = config->root_scope; c->current_scope->next = config->root_scope;
else else
c->current_scope->next = &global_root_scope[CFK_KEYWORDS]; c->current_scope->next = global_root_scope;
} }
/** /**

View File

@ -126,6 +126,7 @@ struct symbol {
struct f_val *val; /* For SYM_CONSTANT */ struct f_val *val; /* For SYM_CONSTANT */
uint offset; /* For SYM_VARIABLE */ uint offset; /* For SYM_VARIABLE */
const struct keyword *keyword; /* For SYM_KEYWORD */ const struct keyword *keyword; /* For SYM_KEYWORD */
const struct f_method *method; /* For SYM_METHOD */
}; };
char name[0]; char name[0];
@ -144,13 +145,9 @@ struct sym_scope {
byte readonly:1; /* Do not add new symbols */ byte readonly:1; /* Do not add new symbols */
}; };
enum keyword_scope {
CFK_KEYWORDS,
CFK_METHODS,
CFK__MAX
};
extern struct sym_scope *global_root_scope; extern struct sym_scope *global_root_scope;
extern pool *global_root_scope_pool;
extern linpool *global_root_scope_linpool;
struct bytestring { struct bytestring {
size_t length; size_t length;
@ -168,6 +165,7 @@ struct bytestring {
#define SYM_TABLE 5 #define SYM_TABLE 5
#define SYM_ATTRIBUTE 6 #define SYM_ATTRIBUTE 6
#define SYM_KEYWORD 7 #define SYM_KEYWORD 7
#define SYM_METHOD 8
#define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */ #define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */
#define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff) #define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff)
@ -215,6 +213,9 @@ struct symbol *cf_localize_symbol(struct config *conf, struct symbol *sym);
static inline int cf_symbol_is_local(struct config *conf, struct symbol *sym) static inline int cf_symbol_is_local(struct config *conf, struct symbol *sym)
{ return (sym->scope == conf->current_scope) && !conf->current_scope->soft_scopes; } { return (sym->scope == conf->current_scope) && !conf->current_scope->soft_scopes; }
/* internal */
struct symbol *cf_new_symbol(struct sym_scope *scope, pool *p, struct linpool *lp, const byte *c);
/** /**
* cf_define_symbol - define meaning of a symbol * cf_define_symbol - define meaning of a symbol
* @sym: symbol to be defined * @sym: symbol to be defined

View File

@ -106,7 +106,7 @@ CF_DECLS
%token <ip4> IP4 %token <ip4> IP4
%token <ip6> IP6 %token <ip6> IP6
%token <i64> VPN_RD %token <i64> VPN_RD
%token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED %token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED CF_SYM_METHOD_BARE CF_SYM_METHOD_ARGS
%token <t> TEXT %token <t> TEXT
%token <bs> BYTESTRING %token <bs> BYTESTRING
%type <iface> ipa_scope %type <iface> ipa_scope

View File

@ -23,11 +23,10 @@ m4_define(CF_DECLS, `m4_divert(-1)')
m4_define(CF_DEFINES, `m4_divert(-1)') m4_define(CF_DEFINES, `m4_divert(-1)')
# Keywords are translated to C initializers # Keywords are translated to C initializers
m4_define(CF_handle_kw, `m4_divert(1){ "m4_translit($1,[[A-Z]],[[a-z]])", $1, CF_keywd_target }, m4_define(CF_handle_kw, `m4_divert(1){ "m4_translit($1,[[A-Z]],[[a-z]])", $1 },
m4_divert(-1)') m4_divert(-1)')
m4_define(CF_keywd, `m4_ifdef([[CF_tok_]]CF_keywd_target[[_$1]],,[[m4_define([[CF_tok_]]CF_keywd_target[[_$1]],1)CF_handle_kw($1)]])') m4_define(CF_keywd, `m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)CF_handle_kw($1)]])')
m4_define(CF_KEYWORDS, `m4_define([[CF_keywd_target]],CFK_KEYWORDS)CF_iterate([[CF_keywd]], [[$@]])DNL') m4_define(CF_KEYWORDS, `CF_iterate([[CF_keywd]], [[$@]])DNL')
m4_define(CF_METHODS, `m4_define([[CF_keywd_target]],CFK_METHODS)CF_iterate([[CF_keywd]], [[$@]])DNL')
# CLI commands generate keywords as well # CLI commands generate keywords as well

View File

@ -1573,24 +1573,24 @@ in the foot).
<cf><m/P/.len</cf> returns the length of path <m/P/. <cf><m/P/.len</cf> returns the length of path <m/P/.
<cf><m/P/.empty</cf> makes the path <m/P/ empty. <cf><m/P/.empty</cf> makes the path <m/P/ empty. Can't be used as a value, always modifies the object.
<cf>prepend(<m/P/,<m/A/)</cf> prepends ASN <m/A/ to path <m/P/ and <cf><m/P/.prepend(<m/A/)</cf> prepends ASN <m/A/ to path <m/P/ and
returns the result. returns the result.
<cf>delete(<m/P/,<m/A/)</cf> deletes all instances of ASN <m/A/ from <cf><m/P/.delete(<m/A/)</cf> deletes all instances of ASN <m/A/ from
from path <m/P/ and returns the result. <m/A/ may also be an integer from path <m/P/ and returns the result. <m/A/ may also be an integer
set, in that case the operator deletes all ASNs from path <m/P/ that are set, in that case the operator deletes all ASNs from path <m/P/ that are
also members of set <m/A/. also members of set <m/A/.
<cf>filter(<m/P/,<m/A/)</cf> deletes all ASNs from path <m/P/ that are <cf><m/P/.filter(<m/A/)</cf> deletes all ASNs from path <m/P/ that are
not members of integer set <m/A/. I.e., <cf/filter/ do the same as not members of integer set <m/A/, and returns the result.
<cf/delete/ with inverted set <m/A/. I.e., <cf/filter/ do the same as <cf/delete/ with inverted set <m/A/.
Statement <cf><m/P/ = prepend(<m/P/, <m/A/);</cf> can be shortened to Methods <cf>prepend</cf>, <cf>delete</cf> and <cf>filter</cf> keep the
<cf><m/P/.prepend(<m/A/);</cf> if <m/P/ is appropriate route attribute original object intact as long as you use the result in any way. You can
(for example <cf/bgp_path/) or a local variable. also write e.g. <cf><m/P/.prepend(<m/A/);</cf> as a standalone statement.
Similarly for <cf/delete/ and <cf/filter/. This variant does modify the original object with the result of the operation.
<tag><label id="type-bgpmask">bgpmask</tag> <tag><label id="type-bgpmask">bgpmask</tag>
BGP masks are patterns used for BGP path matching (using <cf>path BGP masks are patterns used for BGP path matching (using <cf>path
@ -1618,29 +1618,29 @@ in the foot).
<cf><m/C/.len</cf> returns the length of clist <m/C/. <cf><m/C/.len</cf> returns the length of clist <m/C/.
<cf><m/C/.empty</cf> makes the list <m/C/ empty. <cf><m/C/.empty</cf> makes the list <m/C/ empty. Can't be used as a value, always modifies the object.
<cf>add(<m/C/,<m/P/)</cf> adds pair (or quad) <m/P/ to clist <m/C/ and <cf><m/C/.add(<m/P/)</cf> adds pair (or quad) <m/P/ to clist <m/C/ and
returns the result. If item <m/P/ is already in clist <m/C/, it does returns the result. If item <m/P/ is already in clist <m/C/, it does
nothing. <m/P/ may also be a clist, in that case all its members are nothing. <m/P/ may also be a clist, in that case all its members are
added; i.e., it works as clist union. added; i.e., it works as clist union.
<cf>delete(<m/C/,<m/P/)</cf> deletes pair (or quad) <m/P/ from clist <cf><m/C/.delete(<m/P/)</cf> deletes pair (or quad) <m/P/ from clist
<m/C/ and returns the result. If clist <m/C/ does not contain item <m/C/ and returns the result. If clist <m/C/ does not contain item
<m/P/, it does nothing. <m/P/ may also be a pair (or quad) set, in that <m/P/, it does nothing. <m/P/ may also be a pair (or quad) set, in that
case the operator deletes all items from clist <m/C/ that are also case the operator deletes all items from clist <m/C/ that are also
members of set <m/P/. Moreover, <m/P/ may also be a clist, which works members of set <m/P/. Moreover, <m/P/ may also be a clist, which works
analogously; i.e., it works as clist difference. analogously; i.e., it works as clist difference.
<cf>filter(<m/C/,<m/P/)</cf> deletes all items from clist <m/C/ that are <cf><m/C/.filter(<m/P/)</cf> deletes all items from clist <m/C/ that are
not members of pair (or quad) set <m/P/. I.e., <cf/filter/ do the same not members of pair (or quad) set <m/P/, and returns the result. I.e., <cf/filter/ do the same
as <cf/delete/ with inverted set <m/P/. <m/P/ may also be a clist, which as <cf/delete/ with inverted set <m/P/. <m/P/ may also be a clist, which
works analogously; i.e., it works as clist intersection. works analogously; i.e., it works as clist intersection.
Statement <cf><m/C/ = add(<m/C/, <m/P/);</cf> can be shortened to Methods <cf>add</cf>, <cf>delete</cf> and <cf>filter</cf> keep the
<cf><m/C/.add(<m/P/);</cf> if <m/C/ is appropriate route attribute (for original object intact as long as you use the result in any way. You can
example <cf/bgp_community/) or a local variable. also write e.g. <cf><m/P/.add(<m/A/);</cf> as a standalone statement.
Similarly for <cf/delete/ and <cf/filter/. This variant does modify the original object with the result of the operation.
<cf><m/C/.min</cf> returns the minimum element of clist <m/C/. <cf><m/C/.min</cf> returns the minimum element of clist <m/C/.

View File

@ -24,21 +24,30 @@ static enum f_type this_var_type;
static struct f_method_scope { static struct f_method_scope {
struct f_inst *object; struct f_inst *object;
struct sym_scope *main;
struct sym_scope scope; struct sym_scope scope;
} f_method_scope_stack[32]; } f_method_scope_stack[32];
static int f_method_scope_pos = -1; static int f_method_scope_pos = -1;
#define FM (f_method_scope_stack[f_method_scope_pos]) #define FM (f_method_scope_stack[f_method_scope_pos])
static inline void f_push_method_scope(struct f_inst *object) static inline void f_method_call_start(struct f_inst *object)
{ {
if (object->type == T_VOID)
cf_error("Can't infer type to properly call a method, please assign the value to a variable");
if (++f_method_scope_pos >= (int) ARRAY_SIZE(f_method_scope_stack)) if (++f_method_scope_pos >= (int) ARRAY_SIZE(f_method_scope_stack))
cf_error("Too many nested method calls"); cf_error("Too many nested method calls");
struct sym_scope *scope = f_type_method_scope(object->type);
if (!scope)
cf_error("No methods defined for type %s", f_type_name(object->type));
FM = (struct f_method_scope) { FM = (struct f_method_scope) {
.object = object, .object = object,
.main = new_config->current_scope,
.scope = { .scope = {
.next = new_config->current_scope, .next = NULL,
.hash = global_root_scope[CFK_METHODS].hash, .hash = scope->hash,
.active = 1, .active = 1,
.block = 1, .block = 1,
.readonly = 1, .readonly = 1,
@ -47,12 +56,23 @@ static inline void f_push_method_scope(struct f_inst *object)
new_config->current_scope = &FM.scope; new_config->current_scope = &FM.scope;
} }
static inline void f_pop_method_scope(void) static inline void f_method_call_args(void)
{
ASSERT_DIE(FM.scope.active);
FM.scope.active = 0;
new_config->current_scope = FM.main;
}
static inline void f_method_call_end(void)
{ {
ASSERT_DIE(f_method_scope_pos >= 0); ASSERT_DIE(f_method_scope_pos >= 0);
if (FM.scope.active) {
ASSERT_DIE(&FM.scope == new_config->current_scope);
new_config->current_scope = FM.main;
ASSERT_DIE(&FM.scope == new_config->current_scope); FM.scope.active = 0;
new_config->current_scope = FM.scope.next; }
f_method_scope_pos--; f_method_scope_pos--;
} }
@ -357,7 +377,7 @@ CF_METHODS(IS_V4, TYPE, IP, RD, LEN, MAXLEN, ASN, SRC, DST, MASK,
%nonassoc ELSE %nonassoc ELSE
%type <xp> cmds_int cmd_prep %type <xp> cmds_int cmd_prep
%type <x> term cmd cmd_var cmds cmds_scoped constant constructor print_list var var_init var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail method_cmd method_term %type <x> term cmd cmd_var cmds cmds_scoped constant constructor print_list var var_init var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail term_dot_method method_name_cont
%type <fda> dynamic_attr %type <fda> dynamic_attr
%type <fsa> static_attr %type <fsa> static_attr
%type <f> filter where_filter %type <f> filter where_filter
@ -538,7 +558,7 @@ function_def:
FUNCTION maybe_type symbol { FUNCTION maybe_type symbol {
DBG( "Beginning of function %s\n", $3->name ); DBG( "Beginning of function %s\n", $3->name );
this_function = cf_define_symbol(new_config, $3, SYM_FUNCTION, function, NULL); this_function = cf_define_symbol(new_config, $3, SYM_FUNCTION, function, NULL);
/* if ($2 == T_VOID) log(L_WARN "Support for functions without explicit return type will be removed soon" ); */ /* if ($2 == T_VOID) cf_warn("Support for functions without explicit return type will be removed soon" ); */
cf_push_scope(new_config, this_function); cf_push_scope(new_config, this_function);
} function_args { } function_args {
/* Make dummy f_line for storing function prototype */ /* Make dummy f_line for storing function prototype */
@ -847,33 +867,18 @@ static_attr:
| ONLINK { $$ = f_new_static_attr(T_BOOL, SA_ONLINK, 0); } | ONLINK { $$ = f_new_static_attr(T_BOOL, SA_ONLINK, 0); }
; ;
method_term: term_dot_method: term '.' { f_method_call_start($1); } method_name_cont { $$ = $4; };
IS_V4 { $$ = f_new_inst(FI_IS_V4, FM.object); } method_name_cont:
| TYPE { $$ = f_new_inst(FI_TYPE, FM.object); } CF_SYM_METHOD_BARE {
| IP { $$ = f_new_inst(FI_IP, FM.object); } $$ = $1->method->new_inst(FM.object, NULL);
| RD { $$ = f_new_inst(FI_ROUTE_DISTINGUISHER, FM.object); } f_method_call_end();
| LEN { $$ = f_new_inst(FI_LENGTH, FM.object); } }
| MAXLEN { $$ = f_new_inst(FI_ROA_MAXLEN, FM.object); } | CF_SYM_METHOD_ARGS {
| ASN { $$ = f_new_inst(FI_ASN, FM.object); } f_method_call_args();
| SRC { $$ = f_new_inst(FI_NET_SRC, FM.object); } } '(' var_list ')' {
| DST { $$ = f_new_inst(FI_NET_DST, FM.object); } $$ = $1->method->new_inst(FM.object, $4);
| MASK '(' term ')' { $$ = f_new_inst(FI_IP_MASK, FM.object, $3); } f_method_call_end();
| FIRST { $$ = f_new_inst(FI_AS_PATH_FIRST, FM.object); } }
| LAST { $$ = f_new_inst(FI_AS_PATH_LAST, FM.object); }
| LAST_NONAGGREGATED { $$ = f_new_inst(FI_AS_PATH_LAST_NAG, FM.object); }
| DATA { $$ = f_new_inst(FI_PAIR_DATA, FM.object); }
| DATA1 { $$ = f_new_inst(FI_LC_DATA1, FM.object); }
| DATA2 { $$ = f_new_inst(FI_LC_DATA2, FM.object); }
| MIN { $$ = f_new_inst(FI_MIN, FM.object); }
| MAX { $$ = f_new_inst(FI_MAX, FM.object); }
;
method_cmd:
EMPTY { $$ = f_const_empty(FM.object->i_FI_EA_GET.da.f_type); }
| PREPEND '(' term ')' { $$ = f_new_inst(FI_PATH_PREPEND, FM.object, $3 ); }
| ADD '(' term ')' { $$ = f_new_inst(FI_CLIST_ADD, FM.object, $3 ); }
| DELETE '(' term ')' { $$ = f_new_inst(FI_CLIST_DEL, FM.object, $3 ); }
| FILTER '(' term ')' { $$ = f_new_inst(FI_CLIST_FILTER, FM.object, $3 ); }
; ;
term: term:
@ -903,21 +908,42 @@ term:
| dynamic_attr { $$ = f_new_inst(FI_EA_GET, $1); } | dynamic_attr { $$ = f_new_inst(FI_EA_GET, $1); }
| term '.' { | term_dot_method
f_push_method_scope($1);
} method_term {
f_pop_method_scope();
$$ = $4;
}
| '+' EMPTY '+' { $$ = f_const_empty(T_PATH); } | '+' EMPTY '+' { $$ = f_const_empty(T_PATH); }
| '-' EMPTY '-' { $$ = f_const_empty(T_CLIST); } | '-' EMPTY '-' { $$ = f_const_empty(T_CLIST); }
| '-' '-' EMPTY '-' '-' { $$ = f_const_empty(T_ECLIST); } | '-' '-' EMPTY '-' '-' { $$ = f_const_empty(T_ECLIST); }
| '-' '-' '-' EMPTY '-' '-' '-' { $$ = f_const_empty(T_LCLIST); } | '-' '-' '-' EMPTY '-' '-' '-' { $$ = f_const_empty(T_LCLIST); }
| PREPEND '(' term ',' term ')' { $$ = f_new_inst(FI_PATH_PREPEND, $3, $5); } | PREPEND '(' term ',' term ')' { $$ = f_new_inst(FI_PATH_PREPEND, $3, $5); }
| ADD '(' term ',' term ')' { $$ = f_new_inst(FI_CLIST_ADD, $3, $5); } | ADD '(' term ',' term ')' {
| DELETE '(' term ',' term ')' { $$ = f_new_inst(FI_CLIST_DEL, $3, $5); } switch ($3->type) {
| FILTER '(' term ',' term ')' { $$ = f_new_inst(FI_CLIST_FILTER, $3, $5); } case T_CLIST: $$ = f_new_inst(FI_CLIST_ADD, $3, $5); break;
case T_ECLIST: $$ = f_new_inst(FI_ECLIST_ADD, $3, $5); break;
case T_LCLIST: $$ = f_new_inst(FI_LCLIST_ADD, $3, $5); break;
default: cf_error("Can't add to type %s", f_type_name($3->type));
}
cf_warn("add(x,y) function is deprecated, please use x.add(y)");
}
| DELETE '(' term ',' term ')' {
switch ($3->type) {
case T_PATH: $$ = f_new_inst(FI_PATH_DEL, $3, $5); break;
case T_CLIST: $$ = f_new_inst(FI_CLIST_DEL, $3, $5); break;
case T_ECLIST: $$ = f_new_inst(FI_ECLIST_DEL, $3, $5); break;
case T_LCLIST: $$ = f_new_inst(FI_LCLIST_DEL, $3, $5); break;
default: cf_error("Can't delete from type %s", f_type_name($3->type));
}
cf_warn("delete(x,y) function is deprecated, please use x.delete(y)");
}
| FILTER '(' term ',' term ')' {
switch ($3->type) {
case T_PATH: $$ = f_new_inst(FI_PATH_FILTER, $3, $5); break;
case T_CLIST: $$ = f_new_inst(FI_CLIST_FILTER, $3, $5); break;
case T_ECLIST: $$ = f_new_inst(FI_ECLIST_FILTER, $3, $5); break;
case T_LCLIST: $$ = f_new_inst(FI_LCLIST_FILTER, $3, $5); break;
default: cf_error("Can't filter type %s", f_type_name($3->type));
}
cf_warn("filter(x,y) function is deprecated, please use x.filter(y)");
}
| ROA_CHECK '(' rtable ')' { $$ = f_new_inst(FI_ROA_CHECK_IMPLICIT, $3); } | ROA_CHECK '(' rtable ')' { $$ = f_new_inst(FI_ROA_CHECK_IMPLICIT, $3); }
| ROA_CHECK '(' rtable ',' term ',' term ')' { $$ = f_new_inst(FI_ROA_CHECK_EXPLICIT, $5, $7, $3); } | ROA_CHECK '(' rtable ',' term ',' term ')' { $$ = f_new_inst(FI_ROA_CHECK_EXPLICIT, $5, $7, $3); }
@ -1000,7 +1026,7 @@ cmd:
if (this_function->function->return_type == T_VOID) if (this_function->function->return_type == T_VOID)
{ {
if ($2->type != T_VOID) if ($2->type != T_VOID)
log(L_WARN "Inferring function %s return type from its return value: %s", this_function->name, f_type_name($2->type)); cf_warn("Inferring function %s return type from its return value: %s", this_function->name, f_type_name($2->type));
((struct f_line *) this_function->function)->return_type = $2->type; ((struct f_line *) this_function->function)->return_type = $2->type;
} }
else if (this_function->function->return_type != $2->type) else if (this_function->function->return_type != $2->type)
@ -1044,9 +1070,8 @@ cmd:
} }
| lvalue '.' { | lvalue '.' {
f_push_method_scope(f_lval_getter(&$1)); f_method_call_start(f_lval_getter(&$1));
} method_cmd ';' { } method_name_cont ';' {
f_pop_method_scope();
$$ = f_lval_setter(&$1, $4); $$ = f_lval_setter(&$1, $4);
} }
| BT_ASSERT '(' get_cf_position term get_cf_position ')' ';' { $$ = assert_done($4, $3 + 1, $5 - 1); } | BT_ASSERT '(' get_cf_position term get_cf_position ')' ';' { $$ = assert_done($4, $3 + 1, $5 - 1); }

View File

@ -611,4 +611,3 @@ val_dump(const struct f_val *v) {
val_format(v, &b); val_format(v, &b);
return val_dump_buffer; return val_dump_buffer;
} }

View File

@ -63,6 +63,12 @@ enum f_type {
T_PREFIX_SET = 0x81, T_PREFIX_SET = 0x81,
} PACKED; } PACKED;
struct f_method {
struct symbol *sym;
struct f_inst *(*new_inst)(struct f_inst *obj, struct f_inst *args);
uint arg_num;
};
/* Filter value; size of this affects filter memory consumption */ /* Filter value; size of this affects filter memory consumption */
struct f_val { struct f_val {
enum f_type type; /* T_* */ enum f_type type; /* T_* */
@ -275,8 +281,8 @@ trie_match_next_longest_ip6(net_addr_ip6 *n, ip6_addr *found)
#define F_CMP_ERROR 999 #define F_CMP_ERROR 999
const char *f_type_name(enum f_type t); const char *f_type_name(enum f_type t);
enum f_type f_type_element_type(enum f_type t); enum f_type f_type_element_type(enum f_type t);
struct sym_scope *f_type_method_scope(enum f_type t);
int val_same(const struct f_val *v1, const struct f_val *v2); int val_same(const struct f_val *v1, const struct f_val *v2);
int val_compare(const struct f_val *v1, const struct f_val *v2); int val_compare(const struct f_val *v1, const struct f_val *v2);

View File

@ -34,6 +34,9 @@ m4_divert(-1)m4_dnl
# 102 constructor arguments # 102 constructor arguments
# 110 constructor attributes # 110 constructor attributes
# 103 constructor body # 103 constructor body
# 111 method constructor body
# 112 instruction constructor call from method constructor
# 113 method constructor symbol registrator
# 104 dump line item content # 104 dump line item content
# (there may be nothing in dump-line content and # (there may be nothing in dump-line content and
# it must be handled specially in phase 2) # it must be handled specially in phase 2)
@ -48,6 +51,8 @@ m4_define(FID_STRUCT_IN, `m4_divert(101)')
m4_define(FID_NEW_ARGS, `m4_divert(102)') m4_define(FID_NEW_ARGS, `m4_divert(102)')
m4_define(FID_NEW_ATTRIBUTES, `m4_divert(110)') m4_define(FID_NEW_ATTRIBUTES, `m4_divert(110)')
m4_define(FID_NEW_BODY, `m4_divert(103)') m4_define(FID_NEW_BODY, `m4_divert(103)')
m4_define(FID_NEW_METHOD, `m4_divert(111)')
m4_define(FID_METHOD_CALL, `m4_divert(112)')
m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])') m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
m4_define(FID_LINEARIZE_BODY, `m4_divert(105)') m4_define(FID_LINEARIZE_BODY, `m4_divert(105)')
m4_define(FID_SAME_BODY, `m4_divert(106)') m4_define(FID_SAME_BODY, `m4_divert(106)')
@ -120,7 +125,13 @@ FID_IFCONST([[
constargs = 0; constargs = 0;
]]) ]])
} while (child$1 = child$1->next); } while (child$1 = child$1->next);
FID_LINEARIZE_BODY m4_define([[INST_METHOD_NUM_ARGS]],m4_eval($1-1))m4_dnl
m4_ifelse($1,1,,[[FID_NEW_METHOD()m4_dnl
struct f_inst *arg$1 = args;
if (args == NULL) cf_error("Not enough arguments"); /* INST_NAME */
args = args->next;
FID_METHOD_CALL() , arg$1]])
FID_LINEARIZE_BODY()m4_dnl
pos = linearize(dest, whati->f$1, pos); pos = linearize(dest, whati->f$1, pos);
FID_INTERPRET_BODY()') FID_INTERPRET_BODY()')
@ -170,28 +181,29 @@ FID_HIC(,[[
m4_define(ARG, `ARG_ANY($1) ARG_TYPE($1,$2)') m4_define(ARG, `ARG_ANY($1) ARG_TYPE($1,$2)')
m4_define(ARG_TYPE, `ARG_TYPE_STATIC($1,$2) ARG_TYPE_DYNAMIC($1,$2)') m4_define(ARG_TYPE, `ARG_TYPE_STATIC($1,$2) ARG_TYPE_DYNAMIC($1,$2)')
m4_define(ARG_TYPE_STATIC, ` m4_define(ARG_TYPE_STATIC, `m4_dnl
m4_ifelse($1,1,[[m4_define([[INST_METHOD_OBJECT_TYPE]],$2)]],)m4_dnl
FID_NEW_BODY()m4_dnl FID_NEW_BODY()m4_dnl
if (f$1->type && (f$1->type != ($2)) && !f_const_promotion(f$1, ($2))) if (f$1->type && (f$1->type != ($2)) && !f_const_promotion(f$1, ($2)))
cf_error("Argument $1 of %s must be of type %s, got type %s", cf_error("Argument $1 of %s must be of type %s, got type %s",
f_instruction_name(what->fi_code), f_type_name($2), f_type_name(f$1->type)); f_instruction_name(what->fi_code), f_type_name($2), f_type_name(f$1->type));
FID_INTERPRET_BODY()') FID_INTERPRET_BODY()')
m4_define(ARG_TYPE_DYNAMIC, ` m4_define(ARG_TYPE_DYNAMIC, `m4_dnl
FID_INTERPRET_EXEC()m4_dnl FID_INTERPRET_EXEC()m4_dnl
if (v$1.type != ($2)) if (v$1.type != ($2))
runtime("Argument $1 of %s must be of type %s, got type %s", runtime("Argument $1 of %s must be of type %s, got type %s",
f_instruction_name(what->fi_code), f_type_name($2), f_type_name(v$1.type)); f_instruction_name(what->fi_code), f_type_name($2), f_type_name(v$1.type));
FID_INTERPRET_BODY()') FID_INTERPRET_BODY()')
m4_define(ARG_SAME_TYPE, ` m4_define(ARG_SAME_TYPE, `m4_dnl
FID_NEW_BODY()m4_dnl FID_NEW_BODY()m4_dnl
if (f$1->type && f$2->type && (f$1->type != f$2->type) && if (f$1->type && f$2->type && (f$1->type != f$2->type) &&
!f_const_promotion(f$2, f$1->type) && !f_const_promotion(f$1, f$2->type)) !f_const_promotion(f$2, f$1->type) && !f_const_promotion(f$1, f$2->type))
cf_error("Arguments $1 and $2 of %s must be of the same type", f_instruction_name(what->fi_code)); cf_error("Arguments $1 and $2 of %s must be of the same type", f_instruction_name(what->fi_code));
FID_INTERPRET_BODY()') FID_INTERPRET_BODY()')
m4_define(ARG_PREFER_SAME_TYPE, ` m4_define(ARG_PREFER_SAME_TYPE, `m4_dnl
FID_NEW_BODY()m4_dnl FID_NEW_BODY()m4_dnl
if (f$1->type && f$2->type && (f$1->type != f$2->type)) if (f$1->type && f$2->type && (f$1->type != f$2->type))
(void) (f_const_promotion(f$2, f$1->type) || f_const_promotion(f$1, f$2->type)); (void) (f_const_promotion(f$2, f$1->type) || f_const_promotion(f$1, f$2->type));
@ -268,6 +280,14 @@ m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, f1->sa.sa_code != f
m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)') m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)')
m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())') m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())')
# Method constructor block
m4_define(METHOD_CONSTRUCTOR, `m4_dnl
FID_NEW_METHOD()m4_dnl
if (args) cf_error("Too many arguments");
m4_define([[INST_IS_METHOD]])
m4_define([[INST_METHOD_NAME]],$1)
FID_INTERPRET_BODY()')
# 2) Code wrapping # 2) Code wrapping
# The code produced in 1xx temporary diversions is a raw code without # The code produced in 1xx temporary diversions is a raw code without
# any auxiliary commands and syntactical structures around. When the # any auxiliary commands and syntactical structures around. When the
@ -287,6 +307,7 @@ m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access
# 10 iterate # 10 iterate
# 1 union in struct f_inst # 1 union in struct f_inst
# 3 constructors + interpreter # 3 constructors + interpreter
# 11 method constructors
# #
# These global diversions contain blocks of code that can be directly # These global diversions contain blocks of code that can be directly
# put into the final file, yet it still can't be written out now as # put into the final file, yet it still can't be written out now as
@ -306,6 +327,9 @@ m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)') m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
m4_define(FID_SAME, `FID_ZONE(9, Comparison)') m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
m4_define(FID_ITERATE, `FID_ZONE(10, Iteration)') m4_define(FID_ITERATE, `FID_ZONE(10, Iteration)')
m4_define(FID_METHOD, `FID_ZONE(11, Method constructor)')
m4_define(FID_METHOD_SCOPE_INIT, `FID_ZONE(12, Method scope initializator)')
m4_define(FID_METHOD_REGISTER, `FID_ZONE(13, Method registrator)')
# This macro does all the code wrapping. See inline comments. # This macro does all the code wrapping. See inline comments.
m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[ m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
@ -363,6 +387,35 @@ m4_undivert(102)m4_dnl
} }
]]) ]])
m4_ifdef([[INST_IS_METHOD]],m4_dnl
FID_METHOD()m4_dnl
[[struct f_inst * NONNULL(1)
f_new_method_]]INST_NAME()[[(struct f_inst *obj, struct f_inst *args)
{
/* Unwind the arguments (INST_METHOD_NUM_ARGS) */
m4_undivert(111)m4_dnl
return f_new_inst(INST_NAME, obj
m4_undivert(112)
);
}
FID_METHOD_SCOPE_INIT()m4_dnl
[INST_METHOD_OBJECT_TYPE] = {},
FID_METHOD_REGISTER()m4_dnl
sym = cf_new_symbol(&f_type_method_scopes[INST_METHOD_OBJECT_TYPE],
global_root_scope_pool, global_root_scope_linpool,
INST_METHOD_NAME);
sym->class = SYM_METHOD;
sym->method = method = lp_allocz(global_root_scope_linpool, sizeof(struct f_method));
*method = (struct f_method) {
.sym = sym,
.new_inst = f_new_method_]]INST_NAME()[[,
.arg_num = INST_METHOD_NUM_ARGS,
};
]])m4_dnl
FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug) FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug)
case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break; case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
@ -416,6 +469,8 @@ m4_define([[INST_INVAL]], [[$2]])m4_dnl instruction input value count,
m4_define([[INST_OUTVAL]], [[$3]])m4_dnl instruction output value count, m4_define([[INST_OUTVAL]], [[$3]])m4_dnl instruction output value count,
m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl reset NEVER_CONSTANT trigger, m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl reset NEVER_CONSTANT trigger,
m4_undefine([[INST_RESULT_TYPE]])m4_dnl and reset RESULT_TYPE value. m4_undefine([[INST_RESULT_TYPE]])m4_dnl and reset RESULT_TYPE value.
m4_undefine([[INST_IS_METHOD]])m4_dnl and reset method constructor request.
m4_undefine([[INST_METHOD_OBJECT_TYPE]],)m4_dnl reset method object type,
FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code. FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code.
') ')
@ -539,6 +594,37 @@ FID_WR_PUT(3)
#undef v3 #undef v3
#undef vv #undef vv
/* Method constructor wrappers */
FID_WR_PUT(11)
#if defined(__GNUC__) && __GNUC__ >= 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverride-init"
#endif
static struct sym_scope f_type_method_scopes[] = {
FID_WR_PUT(12)
};
#if defined(__GNUC__) && __GNUC__ >= 6
#pragma GCC diagnostic pop
#endif
struct sym_scope *f_type_method_scope(enum f_type t)
{
return (t < ARRAY_SIZE(f_type_method_scopes)) ? &f_type_method_scopes[t] : NULL;
}
void f_type_methods_register(void)
{
struct symbol *sym;
struct f_method *method;
FID_WR_PUT(13)
for (uint i = 0; i < ARRAY_SIZE(f_type_method_scopes); i++)
f_type_method_scopes[i].readonly = 1;
}
/* Line dumpers */ /* Line dumpers */
#define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1) #define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
static const char f_dump_line_indent_str[] = " "; static const char f_dump_line_indent_str[] = " ";

View File

@ -72,6 +72,8 @@
* m4_dnl ACCESS_RTE; this instruction needs route * m4_dnl ACCESS_RTE; this instruction needs route
* m4_dnl ACCESS_EATTRS; this instruction needs extended attributes * m4_dnl ACCESS_EATTRS; this instruction needs extended attributes
* *
* m4_dnl METHOD_CONSTRUCTOR(name); this instruction is in fact a method of the first argument's type; register it with the given name for that type
*
* m4_dnl FID_MEMBER( custom instruction member * m4_dnl FID_MEMBER( custom instruction member
* m4_dnl C type, for storage in structs * m4_dnl C type, for storage in structs
* m4_dnl name, how the member is named * m4_dnl name, how the member is named
@ -489,20 +491,15 @@
RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1)); RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1));
} }
INST(FI_TYPE, 1, 1) { INST(FI_NET_TYPE, 1, 1) {
ARG_ANY(1); /* There may be more types supporting this operation */ ARG(1, T_NET);
switch (v1.type) METHOD_CONSTRUCTOR("type");
{ RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
case T_NET:
RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
break;
default:
runtime( "Can't determine type of this item" );
}
} }
INST(FI_IS_V4, 1, 1) { INST(FI_IS_V4, 1, 1) {
ARG(1, T_IP); ARG(1, T_IP);
METHOD_CONSTRUCTOR("is_v4");
RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip)); RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip));
} }
@ -548,6 +545,30 @@
RESULT_VAL(val); RESULT_VAL(val);
} }
INST(FI_PATH_EMPTY, 1, 1) {
ARG(1, T_PATH);
METHOD_CONSTRUCTOR("empty");
RESULT(T_PATH, ad, &null_adata);
}
INST(FI_CLIST_EMPTY, 1, 1) {
ARG(1, T_CLIST);
METHOD_CONSTRUCTOR("empty");
RESULT(T_CLIST, ad, &null_adata);
}
INST(FI_ECLIST_EMPTY, 1, 1) {
ARG(1, T_ECLIST);
METHOD_CONSTRUCTOR("empty");
RESULT(T_ECLIST, ad, &null_adata);
}
INST(FI_LCLIST_EMPTY, 1, 1) {
ARG(1, T_LCLIST);
METHOD_CONSTRUCTOR("empty");
RESULT(T_LCLIST, ad, &null_adata);
}
INST(FI_FOR_INIT, 1, 0) { INST(FI_FOR_INIT, 1, 0) {
NEVER_CONSTANT; NEVER_CONSTANT;
ARG_ANY(1); ARG_ANY(1);
@ -965,20 +986,39 @@
ea_unset_attr(fs->eattrs, fs->pool, 1, da.ea_code); ea_unset_attr(fs->eattrs, fs->pool, 1, da.ea_code);
} }
INST(FI_LENGTH, 1, 1) { /* Get length of */ INST(FI_NET_LENGTH, 1, 1) { /* Get length of */
ARG_ANY(1); ARG(1, T_NET);
switch(v1.type) { METHOD_CONSTRUCTOR("len");
case T_NET: RESULT(T_INT, i, net_pxlen(v1.val.net)); break; RESULT(T_INT, i, net_pxlen(v1.val.net));
case T_PATH: RESULT(T_INT, i, as_path_getlen(v1.val.ad)); break; }
case T_CLIST: RESULT(T_INT, i, int_set_get_size(v1.val.ad)); break;
case T_ECLIST: RESULT(T_INT, i, ec_set_get_size(v1.val.ad)); break; INST(FI_PATH_LENGTH, 1, 1) { /* Get length of */
case T_LCLIST: RESULT(T_INT, i, lc_set_get_size(v1.val.ad)); break; ARG(1, T_PATH);
default: runtime( "Prefix, path, clist or eclist expected" ); METHOD_CONSTRUCTOR("len");
} RESULT(T_INT, i, as_path_getlen(v1.val.ad));
}
INST(FI_CLIST_LENGTH, 1, 1) { /* Get length of */
ARG(1, T_CLIST);
METHOD_CONSTRUCTOR("len");
RESULT(T_INT, i, int_set_get_size(v1.val.ad));
}
INST(FI_ECLIST_LENGTH, 1, 1) { /* Get length of */
ARG(1, T_ECLIST);
METHOD_CONSTRUCTOR("len");
RESULT(T_INT, i, ec_set_get_size(v1.val.ad));
}
INST(FI_LCLIST_LENGTH, 1, 1) { /* Get length of */
ARG(1, T_LCLIST);
METHOD_CONSTRUCTOR("len");
RESULT(T_INT, i, lc_set_get_size(v1.val.ad));
} }
INST(FI_NET_SRC, 1, 1) { /* Get src prefix */ INST(FI_NET_SRC, 1, 1) { /* Get src prefix */
ARG(1, T_NET); ARG(1, T_NET);
METHOD_CONSTRUCTOR("src");
net_addr_union *net = (void *) v1.val.net; net_addr_union *net = (void *) v1.val.net;
net_addr *src = falloc(sizeof(net_addr_ip6)); net_addr *src = falloc(sizeof(net_addr_ip6));
@ -1014,6 +1054,7 @@
INST(FI_NET_DST, 1, 1) { /* Get dst prefix */ INST(FI_NET_DST, 1, 1) { /* Get dst prefix */
ARG(1, T_NET); ARG(1, T_NET);
METHOD_CONSTRUCTOR("dst");
net_addr_union *net = (void *) v1.val.net; net_addr_union *net = (void *) v1.val.net;
net_addr *dst = falloc(sizeof(net_addr_ip6)); net_addr *dst = falloc(sizeof(net_addr_ip6));
@ -1049,6 +1090,7 @@
INST(FI_ROA_MAXLEN, 1, 1) { /* Get ROA max prefix length */ INST(FI_ROA_MAXLEN, 1, 1) { /* Get ROA max prefix length */
ARG(1, T_NET); ARG(1, T_NET);
METHOD_CONSTRUCTOR("maxlen")
if (!net_is_roa(v1.val.net)) if (!net_is_roa(v1.val.net))
runtime( "ROA expected" ); runtime( "ROA expected" );
@ -1057,40 +1099,38 @@
((net_addr_roa6 *) v1.val.net)->max_pxlen); ((net_addr_roa6 *) v1.val.net)->max_pxlen);
} }
INST(FI_ASN, 1, 1) { /* Get ROA ASN or community ASN part */ INST(FI_NET_ASN, 1, 1) { /* Get ROA ASN or community ASN part */
ARG_ANY(1); ARG(1, T_NET);
RESULT_TYPE(T_INT); METHOD_CONSTRUCTOR("asn");
switch(v1.type)
{
case T_NET:
if (!net_is_roa(v1.val.net)) if (!net_is_roa(v1.val.net))
runtime( "ROA expected" ); runtime( "ROA expected" );
RESULT_(T_INT, i, (v1.val.net->type == NET_ROA4) ? RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
((net_addr_roa4 *) v1.val.net)->asn : ((net_addr_roa4 *) v1.val.net)->asn :
((net_addr_roa6 *) v1.val.net)->asn); ((net_addr_roa6 *) v1.val.net)->asn);
break;
case T_PAIR:
RESULT_(T_INT, i, v1.val.i >> 16);
break;
case T_LC:
RESULT_(T_INT, i, v1.val.lc.asn);
break;
default:
runtime( "Net, pair or lc expected" );
}
} }
INST(FI_IP, 1, 1) { /* Convert prefix to ... */ INST(FI_PAIR_ASN, 1, 1) { /* Get ROA ASN or community ASN part */
ARG(1, T_PAIR);
METHOD_CONSTRUCTOR("asn");
RESULT(T_INT, i, v1.val.i >> 16);
}
INST(FI_LC_ASN, 1, 1) { /* Get ROA ASN or community ASN part */
ARG(1, T_LC);
METHOD_CONSTRUCTOR("asn");
RESULT(T_INT, i, v1.val.lc.asn);
}
INST(FI_NET_IP, 1, 1) { /* Convert prefix to ... */
ARG(1, T_NET); ARG(1, T_NET);
METHOD_CONSTRUCTOR("ip");
RESULT(T_IP, ip, net_prefix(v1.val.net)); RESULT(T_IP, ip, net_prefix(v1.val.net));
} }
INST(FI_ROUTE_DISTINGUISHER, 1, 1) { INST(FI_ROUTE_DISTINGUISHER, 1, 1) {
ARG(1, T_NET); ARG(1, T_NET);
METHOD_CONSTRUCTOR("rd");
if (!net_is_vpn(v1.val.net)) if (!net_is_vpn(v1.val.net))
runtime( "VPN address expected" ); runtime( "VPN address expected" );
RESULT(T_RD, ec, net_rd(v1.val.net)); RESULT(T_RD, ec, net_rd(v1.val.net));
@ -1098,6 +1138,7 @@
INST(FI_AS_PATH_FIRST, 1, 1) { /* Get first ASN from AS PATH */ INST(FI_AS_PATH_FIRST, 1, 1) { /* Get first ASN from AS PATH */
ARG(1, T_PATH); ARG(1, T_PATH);
METHOD_CONSTRUCTOR("first");
u32 as = 0; u32 as = 0;
as_path_get_first(v1.val.ad, &as); as_path_get_first(v1.val.ad, &as);
RESULT(T_INT, i, as); RESULT(T_INT, i, as);
@ -1105,6 +1146,7 @@
INST(FI_AS_PATH_LAST, 1, 1) { /* Get last ASN from AS PATH */ INST(FI_AS_PATH_LAST, 1, 1) { /* Get last ASN from AS PATH */
ARG(1, T_PATH); ARG(1, T_PATH);
METHOD_CONSTRUCTOR("last");
u32 as = 0; u32 as = 0;
as_path_get_last(v1.val.ad, &as); as_path_get_last(v1.val.ad, &as);
RESULT(T_INT, i, as); RESULT(T_INT, i, as);
@ -1112,90 +1154,74 @@
INST(FI_AS_PATH_LAST_NAG, 1, 1) { /* Get last ASN from non-aggregated part of AS PATH */ INST(FI_AS_PATH_LAST_NAG, 1, 1) { /* Get last ASN from non-aggregated part of AS PATH */
ARG(1, T_PATH); ARG(1, T_PATH);
METHOD_CONSTRUCTOR("last_nonaggregated");
RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad)); RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad));
} }
INST(FI_PAIR_DATA, 1, 1) { /* Get data part from the standard community */ INST(FI_PAIR_DATA, 1, 1) { /* Get data part from the standard community */
ARG(1, T_PAIR); ARG(1, T_PAIR);
METHOD_CONSTRUCTOR("data");
RESULT(T_INT, i, v1.val.i & 0xFFFF); RESULT(T_INT, i, v1.val.i & 0xFFFF);
} }
INST(FI_LC_DATA1, 1, 1) { /* Get data1 part from the large community */ INST(FI_LC_DATA1, 1, 1) { /* Get data1 part from the large community */
ARG(1, T_LC); ARG(1, T_LC);
METHOD_CONSTRUCTOR("data1");
RESULT(T_INT, i, v1.val.lc.ldp1); RESULT(T_INT, i, v1.val.lc.ldp1);
} }
INST(FI_LC_DATA2, 1, 1) { /* Get data2 part from the large community */ INST(FI_LC_DATA2, 1, 1) { /* Get data2 part from the large community */
ARG(1, T_LC); ARG(1, T_LC);
METHOD_CONSTRUCTOR("data2");
RESULT(T_INT, i, v1.val.lc.ldp2); RESULT(T_INT, i, v1.val.lc.ldp2);
} }
INST(FI_MIN, 1, 1) { /* Get minimum element from list */ INST(FI_CLIST_MIN, 1, 1) { /* Get minimum element from list */
ARG_ANY(1); ARG(1, T_CLIST);
RESULT_TYPE(f_type_element_type(v1.type)); METHOD_CONSTRUCTOR("min");
switch(v1.type) u32 val = 0;
{ int_set_min(v1.val.ad, &val);
case T_CLIST: RESULT(T_PAIR, i, val);
{
u32 val = 0;
int_set_min(v1.val.ad, &val);
RESULT_(T_PAIR, i, val);
}
break;
case T_ECLIST:
{
u64 val = 0;
ec_set_min(v1.val.ad, &val);
RESULT_(T_EC, ec, val);
}
break;
case T_LCLIST:
{
lcomm val = { 0, 0, 0 };
lc_set_min(v1.val.ad, &val);
RESULT_(T_LC, lc, val);
}
break;
default:
runtime( "Clist or lclist expected" );
}
} }
INST(FI_MAX, 1, 1) { /* Get maximum element from list */ INST(FI_CLIST_MAX, 1, 1) { /* Get minimum element from list */
ARG_ANY(1); ARG(1, T_CLIST);
RESULT_TYPE(f_type_element_type(v1.type)); METHOD_CONSTRUCTOR("max");
switch(v1.type) u32 val = 0;
{ int_set_max(v1.val.ad, &val);
case T_CLIST: RESULT(T_PAIR, i, val);
{ }
u32 val = 0;
int_set_max(v1.val.ad, &val);
RESULT_(T_PAIR, i, val);
}
break;
case T_ECLIST: INST(FI_ECLIST_MIN, 1, 1) { /* Get minimum element from list */
{ ARG(1, T_ECLIST);
u64 val = 0; METHOD_CONSTRUCTOR("min");
ec_set_max(v1.val.ad, &val); u64 val = 0;
RESULT_(T_EC, ec, val); ec_set_min(v1.val.ad, &val);
} RESULT(T_EC, ec, val);
break; }
case T_LCLIST: INST(FI_ECLIST_MAX, 1, 1) { /* Get minimum element from list */
{ ARG(1, T_ECLIST);
lcomm val = { 0, 0, 0 }; METHOD_CONSTRUCTOR("max");
lc_set_max(v1.val.ad, &val); u64 val = 0;
RESULT_(T_LC, lc, val); ec_set_max(v1.val.ad, &val);
} RESULT(T_EC, ec, val);
break; }
default: INST(FI_LCLIST_MIN, 1, 1) { /* Get minimum element from list */
runtime( "Clist or lclist expected" ); ARG(1, T_LCLIST);
} METHOD_CONSTRUCTOR("min");
lcomm val = {};
lc_set_min(v1.val.ad, &val);
RESULT(T_LC, lc, val);
}
INST(FI_LCLIST_MAX, 1, 1) { /* Get minimum element from list */
ARG(1, T_LCLIST);
METHOD_CONSTRUCTOR("max");
lcomm val = {};
lc_set_max(v1.val.ad, &val);
RESULT(T_LC, lc, val);
} }
INST(FI_RETURN, 1, 0) { INST(FI_RETURN, 1, 0) {
@ -1335,6 +1361,7 @@
INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */ INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */
ARG(1, T_IP); ARG(1, T_IP);
ARG(2, T_INT); ARG(2, T_INT);
METHOD_CONSTRUCTOR("mask");
RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ? RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ?
ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) : ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]); ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]);
@ -1343,165 +1370,156 @@
INST(FI_PATH_PREPEND, 2, 1) { /* Path prepend */ INST(FI_PATH_PREPEND, 2, 1) { /* Path prepend */
ARG(1, T_PATH); ARG(1, T_PATH);
ARG(2, T_INT); ARG(2, T_INT);
METHOD_CONSTRUCTOR("prepend");
RESULT(T_PATH, ad, [[ as_path_prepend(fpool, v1.val.ad, v2.val.i) ]]); RESULT(T_PATH, ad, [[ as_path_prepend(fpool, v1.val.ad, v2.val.i) ]]);
} }
INST(FI_CLIST_ADD, 2, 1) { /* (Extended) Community list add */ INST(FI_CLIST_ADD, 2, 1) { /* (Extended) Community list add */
ARG_ANY(1); ARG(1, T_CLIST);
ARG_ANY(2); ARG_ANY(2);
RESULT_TYPE(f1->type); METHOD_CONSTRUCTOR("add");
if (v1.type == T_PATH)
runtime("Can't add to path");
else if (v1.type == T_CLIST)
{
/* Community (or cluster) list */
struct f_val dummy; struct f_val dummy;
if ((v2.type == T_PAIR) || (v2.type == T_QUAD)) if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, v2.val.i) ]]); RESULT(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, v2.val.i) ]]);
/* IP->Quad implicit conversion */ /* IP->Quad implicit conversion */
else if (val_is_ip4(&v2)) else if (val_is_ip4(&v2))
RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]); RESULT(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy)) else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
runtime("Can't add set"); runtime("Can't add set");
else if (v2.type == T_CLIST) else if (v2.type == T_CLIST)
RESULT_(T_CLIST, ad, [[ int_set_union(fpool, v1.val.ad, v2.val.ad) ]]); RESULT(T_CLIST, ad, [[ int_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
else else
runtime("Can't add non-pair"); runtime("Can't add non-pair");
} }
else if (v1.type == T_ECLIST) INST(FI_ECLIST_ADD, 2, 1) {
{ ARG(1, T_ECLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("add");
/* v2.val is either EC or EC-set */ /* v2.val is either EC or EC-set */
if ((v2.type == T_SET) && eclist_set_type(v2.val.t)) if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
runtime("Can't add set"); runtime("Can't add set");
else if (v2.type == T_ECLIST) else if (v2.type == T_ECLIST)
RESULT_(T_ECLIST, ad, [[ ec_set_union(fpool, v1.val.ad, v2.val.ad) ]]); RESULT(T_ECLIST, ad, [[ ec_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
else if (v2.type != T_EC) else if (v2.type != T_EC)
runtime("Can't add non-ec"); runtime("Can't add non-ec");
else else
RESULT_(T_ECLIST, ad, [[ ec_set_add(fpool, v1.val.ad, v2.val.ec) ]]); RESULT(T_ECLIST, ad, [[ ec_set_add(fpool, v1.val.ad, v2.val.ec) ]]);
} }
else if (v1.type == T_LCLIST) INST(FI_LCLIST_ADD, 2, 1) {
{ ARG(1, T_LCLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("add");
/* v2.val is either LC or LC-set */ /* v2.val is either LC or LC-set */
if ((v2.type == T_SET) && lclist_set_type(v2.val.t)) if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
runtime("Can't add set"); runtime("Can't add set");
else if (v2.type == T_LCLIST) else if (v2.type == T_LCLIST)
RESULT_(T_LCLIST, ad, [[ lc_set_union(fpool, v1.val.ad, v2.val.ad) ]]); RESULT(T_LCLIST, ad, [[ lc_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
else if (v2.type != T_LC) else if (v2.type != T_LC)
runtime("Can't add non-lc"); runtime("Can't add non-lc");
else else
RESULT_(T_LCLIST, ad, [[ lc_set_add(fpool, v1.val.ad, v2.val.lc) ]]); RESULT(T_LCLIST, ad, [[ lc_set_add(fpool, v1.val.ad, v2.val.lc) ]]);
}
} INST(FI_PATH_DEL, 2, 1) { /* Path delete */
ARG(1, T_PATH);
else ARG_ANY(2);
runtime("Can't add to non-[e|l]clist"); METHOD_CONSTRUCTOR("delete");
if ((v2.type == T_SET) && path_set_type(v2.val.t) || (v2.type == T_INT))
RESULT(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 0) ]]);
else
runtime("Can't delete non-integer (set)");
} }
INST(FI_CLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */ INST(FI_CLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
ARG_ANY(1); ARG(1, T_CLIST);
ARG_ANY(2); ARG_ANY(2);
RESULT_TYPE(f1->type); METHOD_CONSTRUCTOR("delete");
if (v1.type == T_PATH)
{
if ((v2.type == T_SET) && path_set_type(v2.val.t) || (v2.type == T_INT))
RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 0) ]]);
else
runtime("Can't delete non-integer (set)");
}
else if (v1.type == T_CLIST)
{
/* Community (or cluster) list */ /* Community (or cluster) list */
struct f_val dummy; struct f_val dummy;
if ((v2.type == T_PAIR) || (v2.type == T_QUAD)) if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, v2.val.i) ]]); RESULT(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, v2.val.i) ]]);
/* IP->Quad implicit conversion */ /* IP->Quad implicit conversion */
else if (val_is_ip4(&v2)) else if (val_is_ip4(&v2))
RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]); RESULT(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST)) else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 0) ]]); RESULT(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 0) ]]);
else else
runtime("Can't delete non-pair"); runtime("Can't delete non-pair");
} }
else if (v1.type == T_ECLIST) INST(FI_ECLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
{ ARG(1, T_ECLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("delete");
/* v2.val is either EC or EC-set */ /* v2.val is either EC or EC-set */
if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST)) if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 0) ]]); RESULT(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
else if (v2.type != T_EC) else if (v2.type != T_EC)
runtime("Can't delete non-ec"); runtime("Can't delete non-ec");
else else
RESULT_(T_ECLIST, ad, [[ ec_set_del(fpool, v1.val.ad, v2.val.ec) ]]); RESULT(T_ECLIST, ad, [[ ec_set_del(fpool, v1.val.ad, v2.val.ec) ]]);
} }
else if (v1.type == T_LCLIST) INST(FI_LCLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
{ ARG(1, T_LCLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("delete");
/* v2.val is either LC or LC-set */ /* v2.val is either LC or LC-set */
if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST)) if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 0) ]]); RESULT(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
else if (v2.type != T_LC) else if (v2.type != T_LC)
runtime("Can't delete non-lc"); runtime("Can't delete non-lc");
else else
RESULT_(T_LCLIST, ad, [[ lc_set_del(fpool, v1.val.ad, v2.val.lc) ]]); RESULT(T_LCLIST, ad, [[ lc_set_del(fpool, v1.val.ad, v2.val.lc) ]]);
}
else
runtime("Can't delete in non-[e|l]clist");
} }
INST(FI_CLIST_FILTER, 2, 1) { /* (Extended) Community list add or delete */ INST(FI_PATH_FILTER, 2, 1) { /* (Extended) Community list add or delete */
ARG_ANY(1); ARG(1, T_PATH);
ARG_ANY(2); ARG_ANY(2);
RESULT_TYPE(f1->type); METHOD_CONSTRUCTOR("filter");
if (v1.type == T_PATH)
{
if ((v2.type == T_SET) && path_set_type(v2.val.t)) if ((v2.type == T_SET) && path_set_type(v2.val.t))
RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 1) ]]); RESULT(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 1) ]]);
else else
runtime("Can't filter integer"); runtime("Can't filter integer");
} }
else if (v1.type == T_CLIST) INST(FI_CLIST_FILTER, 2, 1) {
{ ARG(1, T_CLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("filter");
/* Community (or cluster) list */ /* Community (or cluster) list */
struct f_val dummy; struct f_val dummy;
if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST)) if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 1) ]]); RESULT(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 1) ]]);
else else
runtime("Can't filter pair"); runtime("Can't filter pair");
} }
else if (v1.type == T_ECLIST) INST(FI_ECLIST_FILTER, 2, 1) {
{ ARG(1, T_ECLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("filter");
/* v2.val is either EC or EC-set */ /* v2.val is either EC or EC-set */
if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST)) if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 1) ]]); RESULT(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
else else
runtime("Can't filter ec"); runtime("Can't filter ec");
} }
else if (v1.type == T_LCLIST) INST(FI_LCLIST_FILTER, 2, 1) {
{ ARG(1, T_LCLIST);
ARG_ANY(2);
METHOD_CONSTRUCTOR("filter");
/* v2.val is either LC or LC-set */ /* v2.val is either LC or LC-set */
if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST)) if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 1) ]]); RESULT(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
else else
runtime("Can't filter lc"); runtime("Can't filter lc");
}
else
runtime("Can't filter non-[e|l]clist");
} }
INST(FI_ROA_CHECK_IMPLICIT, 0, 1) { /* ROA Check */ INST(FI_ROA_CHECK_IMPLICIT, 0, 1) { /* ROA Check */

View File

@ -703,7 +703,7 @@ function bgpmask mkpath(int a; int b)
define set35 = [3 .. 5]; define set35 = [3 .. 5];
function t_path() function t_path_old()
bgpmask pm1; bgpmask pm1;
bgppath p2; bgppath p2;
int set set12; int set set12;
@ -782,7 +782,91 @@ int set set12;
bt_assert(x = 18 && y = 50); bt_assert(x = 18 && y = 50);
} }
bt_test_suite(t_path, "Testing paths"); bt_test_suite(t_path_old, "Testing paths (old syntax)");
function t_path_new()
{
bgpmask pm1 = [= 4 3 2 1 =];
int set set12 = [1, 2];
bt_assert(format(pm1) = "[= 4 3 2 1 =]");
bt_assert(+empty+ = +empty+);
bt_assert(10 !~ +empty+);
bgppath p2;
bt_assert(p2 = +empty+);
p2.prepend(1);
p2.prepend(2);
p2.prepend(3);
p2.prepend(4);
bt_assert(p2.empty = +empty+);
bt_assert(format(p2) = "(path 4 3 2 1)");
bt_assert(p2.len = 4);
bt_assert(p2 ~ pm1);
bt_assert(3 ~ p2);
bt_assert(p2 ~ [2, 10..20]);
bt_assert(p2 ~ [4, 10..20]);
bt_assert(p2 !~ []);
p2.prepend(5);
bt_assert(p2 !~ pm1);
bt_assert(10 !~ p2);
bt_assert(p2 !~ [8, ten..(2*ten)]);
bt_assert(p2 ~ [= * 4 3 * 1 =]);
bt_assert(p2 ~ [= (3+2) (2*2) 3 2 1 =]);
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.len = 5);
bt_assert(p2.first = 5);
bt_assert(p2.last = 1);
bt_assert(p2.len = 5);
bt_assert(p2.delete(3) = +empty+.prepend(1).prepend(2).prepend(4).prepend(5));
bt_assert(p2.filter([1..3]) = +empty+.prepend(1).prepend(2).prepend(3));
bt_assert(p2.delete([]) = p2);
bt_assert(p2.filter([]) = +empty+);
bt_assert(+empty+.prepend(0).prepend(1).delete([]) = +empty+.prepend(0).prepend(1));
bt_assert(+empty+.prepend(0).prepend(1).filter([]) = +empty+);
p2 = +empty+;
p2.prepend(5);
p2.prepend(4);
p2.prepend(3);
p2.prepend(3);
p2.prepend(2);
p2.prepend(1);
bt_assert(p2 !~ [= 1 2 3 4 5 =]);
bt_assert(p2 ~ [= 1 2 * 4 5 =]);
bt_assert(p2 ~ [= 1 2 * 3 4 5 =]);
bt_assert(p2 ~ [= 1 2 3+ 4 5 =]);
bt_assert(p2 ~ [= 1 2 3+ 4+ 5 =]);
bt_assert(p2 !~ [= 1 2 3+ 5+ 4 5 =]);
bt_assert(p2 !~ [= 1 2 3 3 5+ 4 5 =]);
bt_assert(p2.delete(3) = +empty+.prepend(5).prepend(4).prepend(2).prepend(1));
bt_assert(p2.delete([4..5]) = +empty+.prepend(3).prepend(3).prepend(2).prepend(1));
bt_assert(format([= 1 2+ 3 =]) = "[= 1 2 + 3 =]");
# iteration over path
int x = 0;
int y = 0;
for int i in p2 do {
x = x + i;
y = y + x;
}
bt_assert(x = 18 && y = 50);
}
bt_test_suite(t_path_new, "Testing paths (new syntax)");
@ -794,7 +878,7 @@ bt_test_suite(t_path, "Testing paths");
define p23 = (2, 3); define p23 = (2, 3);
function t_clist() function t_clist_old()
clist l; clist l;
clist l2; clist l2;
clist r; clist r;
@ -914,7 +998,129 @@ clist r;
bt_assert(x = 36); bt_assert(x = 36);
} }
bt_test_suite(t_clist, "Testing lists of communities"); bt_test_suite(t_clist_old, "Testing lists of communities (old syntax)");
function t_clist_new()
{
bt_assert((10, 20).asn = 10);
bt_assert((10, 20).data = 20);
bt_assert(p23.asn = 2);
bt_assert(p23.data = 3);
clist l;
bt_assert(l = -empty-);
bt_assert(l !~ [(*,*)]);
bt_assert((l ~ [(*,*)]) != (l !~ [(*,*)]));
bt_assert(-empty- = -empty-);
l.add( (one,2) );
bt_assert(l ~ [(*,*)]);
l.add( (2,one+2) );
bt_assert(format(l) = "(clist (1,2) (2,3))");
bt_assert(l.empty = -empty-);
bt_assert((2,3) ~ l);
bt_assert(l ~ [(1,*)]);
bt_assert(l ~ [p23]);
bt_assert(l ~ [(2,2..3)]);
bt_assert(l ~ [(1,1..2)]);
bt_assert(l ~ [(1,1)..(1,2)]);
bt_assert(l !~ []);
l.add((2,5));
l.add((5,one));
l.add((6,one));
l.add((one,one));
l.delete([(5,1),(6,one),(one,1)]);
l.delete([(5,one),(6,one)]);
l.filter([(1,*)]);
bt_assert(l = -empty-.add((1,2)));
bt_assert((2,3) !~ l);
bt_assert(l !~ [(2,*)]);
bt_assert(l !~ [(one,3..6)]);
bt_assert(l ~ [(*,*)]);
l.add((3,one));
l.add((one+one+one,one+one));
l.add((3,3));
l.add((3,4));
l.add((3,5));
clist l2 = l.filter([(3,*)]);
l.delete([(3,2..4)]);
bt_assert(l = -empty-.add((1,2)).add((3,1)).add((3,5)));
bt_assert(l.len = 3);
l.add((3,2));
l.add((4,5));
bt_assert(l = -empty-.add((1,2)).add((3,1)).add((3,5)).add((3,2)).add((4,5)));
bt_assert(l.len = 5);
bt_assert(l ~ [(*,2)]);
bt_assert(l ~ [(*,5)]);
bt_assert(l ~ [(*, one)]);
bt_assert(l !~ [(*,3)]);
bt_assert(l !~ [(*,(one+6))]);
bt_assert(l !~ [(*, (one+one+one))]);
bt_assert(l.delete([]) = l);
bt_assert(l.filter([]) = -empty-);
l.delete([(*,(one+onef(3)))]);
l.delete([(*,(4+one))]);
bt_assert(l = -empty-.add((3,1)));
l.delete([(*,(onef(5)))]);
bt_assert(l = -empty-);
l2.add((3,6));
l = l2.filter([(3,1..4)]);
l2.filter([(3,3..6)]);
# clist A (10,20,30)
bt_assert(l = -empty-.add((3,1)).add((3,2)).add((3,3)).add((3,4)));
bt_assert(format(l) = "(clist (3,1) (3,2) (3,3) (3,4))");
# clist B (30,40,50)
bt_assert(l2 = -empty-.add((3,3)).add((3,4)).add((3,5)).add((3,6)));
bt_assert(format(l2) = "(clist (3,3) (3,4) (3,5) (3,6))");
# clist A union B
clist r = l.add(l2);
bt_assert(r = -empty-.add((3,1)).add((3,2)).add((3,3)).add((3,4)).add((3,5)).add((3,6)));
bt_assert(format(r) = "(clist (3,1) (3,2) (3,3) (3,4) (3,5) (3,6))");
# clist A isect B
r = l.filter(l2);
bt_assert(r = -empty-.add((3,3)).add((3,4)));
bt_assert(format(r) = "(clist (3,3) (3,4))");
# clist A \ B
r = l.delete(l2);
bt_assert(r = -empty-.add((3,1)).add((3,2)));
bt_assert(format(r) = "(clist (3,1) (3,2))");
# clist in c set
r = l.filter([(3,1), (*,2)]);
bt_assert(r = -empty-.add((3,1)).add((3,2)));
bt_assert(format(r) = "(clist (3,1) (3,2))");
# minimim & maximum element
r = -empty-.add((2,1)).add((1,3)).add((2,2)).add((3,1)).add((2,3));
bt_assert(format(r) = "(clist (2,1) (1,3) (2,2) (3,1) (2,3))");
bt_assert(r.min = (1,3));
bt_assert(r.max = (3,1));
# iteration over clist
int x = 0;
for pair c in r do
x = x + c.asn * c.asn * c.data;
bt_assert(x = 36);
}
bt_test_suite(t_clist_new, "Testing lists of communities (new syntax)");
@ -949,11 +1155,12 @@ bt_test_suite(t_ec, "Testing extended communities");
* ------------------------------- * -------------------------------
*/ */
function t_eclist() function t_eclist_old()
eclist el; eclist el;
eclist el2; eclist el2;
eclist r; eclist r;
{ {
# Deprecated syntax
el = -- empty --; el = -- empty --;
el = add(el, (rt, 10, 20)); el = add(el, (rt, 10, 20));
el = add(el, (ro, 10.20.30.40, 100)); el = add(el, (ro, 10.20.30.40, 100));
@ -1036,7 +1243,99 @@ eclist r;
bt_assert(x = 3); bt_assert(x = 3);
} }
bt_test_suite(t_eclist, "Testing lists of extended communities"); bt_test_suite(t_eclist_old, "Testing lists of extended communities");
function t_eclist_new()
{
# New syntax
eclist el;
bt_assert(el = --empty--);
el.add((rt, 10, 20));
el.add((ro, 10.20.30.40, 100));
el.add((ro, 11.21.31.41.mask(16), 200));
bt_assert(--empty-- = --empty--);
bt_assert(((rt, 10, 20)) !~ --empty--);
bt_assert(format(el) = "(eclist (rt, 10, 20) (ro, 10.20.30.40, 100) (ro, 11.21.0.0, 200))");
bt_assert(el.len = 3);
el.delete((rt, 10, 20));
el.delete((rt, 10, 30));
bt_assert(el = (--empty--).add((ro, 10.20.30.40, 100)).add((ro, 11.21.0.0, 200)));
bt_assert(el.empty = --empty--);
el.add((unknown 2, ten, 1));
el.add((unknown 5, ten, 1));
el.add((rt, ten, one+one));
el.add((rt, 10, 3));
el.add((rt, 10, 4));
el.add((rt, 10, 5));
el.add((generic, 0x2000a, 3*ten));
el.delete([(rt, 10, 2..ten)]);
bt_assert(el = (--empty--).add((ro, 10.20.30.40, 100)).add((ro, 11.21.0.0, 200)).add((rt, 10, 1)).add((unknown 5, 10, 1)).add((rt, 10, 30)));
el.filter([(rt, 10, *)]);
bt_assert(el = (--empty--).add((rt, 10, 1)).add((rt, 10, 30)));
bt_assert((rt, 10, 1) ~ el);
bt_assert(el ~ [(rt, 10, ten..40)]);
bt_assert((rt, 10, 20) !~ el);
bt_assert((ro, 10.20.30.40, 100) !~ el);
bt_assert(el !~ [(rt, 10, 35..40)]);
bt_assert(el !~ [(ro, 10, *)]);
bt_assert(el !~ []);
el.add((rt, 10, 40));
eclist el2 = el.filter([(rt, 10, 20..40)] );
el2.add((rt, 10, 50));
bt_assert(el.delete([]) = el);
bt_assert(el.filter([]) = --empty--);
# eclist A (1,30,40)
bt_assert(el = --empty--.add((rt, 10, 1)).add((rt, 10, 30)).add((rt, 10, 40)));
bt_assert(format(el) = "(eclist (rt, 10, 1) (rt, 10, 30) (rt, 10, 40))");
# eclist B (30,40,50)
bt_assert(el2 = --empty--.add((rt, 10, 30)).add((rt, 10, 40)).add((rt, 10, 50)));
bt_assert(format(el2) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50))");
# eclist A union B
eclist r = el2.add(el);
bt_assert(r = --empty--.add((rt, 10, 30)).add((rt, 10, 40)).add((rt, 10, 50)).add((rt, 10, 1)));
bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40) (rt, 10, 50) (rt, 10, 1))");
# eclist A isect B
r = el.filter(el2);
bt_assert(r = --empty--.add((rt, 10, 30)).add((rt, 10, 40)));
bt_assert(format(r) = "(eclist (rt, 10, 30) (rt, 10, 40))");
# eclist A \ B
r = el.delete(el2);
bt_assert(r = --empty--.add((rt, 10, 1)));
bt_assert(format(r) = "(eclist (rt, 10, 1))");
# eclist in ec set
r = el.filter([(rt, 10, 1), (rt, 10, 25..30), (ro, 10, 40)]);
bt_assert(r = --empty--.add((rt, 10, 1)).add((rt, 10, 30)));
bt_assert(format(r) = "(eclist (rt, 10, 1) (rt, 10, 30))");
# minimim & maximum element
r = --empty--.add((rt, 2, 1)).add((rt, 1, 3)).add((rt, 2, 2)).add((rt, 3, 1)).add((rt, 2, 3));
bt_assert(format(r) = "(eclist (rt, 2, 1) (rt, 1, 3) (rt, 2, 2) (rt, 3, 1) (rt, 2, 3))");
bt_assert(r.min = (rt, 1, 3));
bt_assert(r.max = (rt, 3, 1));
# iteration over eclist
int x = 0;
for ec c in r do
if c > (rt, 2, 0) && c < (rt, 3, 0) then
x = x + 1;
bt_assert(x = 3);
}
bt_test_suite(t_eclist_new, "Testing lists of extended communities");
@ -1085,7 +1384,7 @@ function lc mktrip(int a)
return (a, 2*a, 3*a); return (a, 2*a, 3*a);
} }
function t_lclist() function t_lclist_old()
lclist ll; lclist ll;
lclist ll2; lclist ll2;
lclist r; lclist r;
@ -1167,7 +1466,92 @@ lclist r;
bt_assert(mx = r.max); bt_assert(mx = r.max);
} }
bt_test_suite(t_lclist, "Testing lists of large communities"); bt_test_suite(t_lclist_old, "Testing lists of large communities");
function t_lclist_new()
{
bt_assert(---empty--- = ---empty---);
bt_assert((10, 20, 30) !~ ---empty---);
bt_assert((10, 20, 30).asn = 10);
bt_assert((10, 20, 30).data1 = 20);
bt_assert((10, 20, 30).data2 = 30);
lclist ll;
bt_assert(ll = ---empty---);
ll.add((ten, 20, 30));
ll.add((1000, 2000, 3000));
ll.add(mktrip(100000));
bt_assert(ll.empty = ---empty---);
bt_assert(format(ll) = "(lclist (10, 20, 30) (1000, 2000, 3000) (100000, 200000, 300000))");
bt_assert(ll.len = 3);
bt_assert(ll = ---empty---.add((10, 20, 30)).add((1000, 2000, 3000)).add((100000, 200000, 300000)));
bt_assert(mktrip(1000) ~ ll);
bt_assert(mktrip(100) !~ ll);
ll.empty;
ll.add((10, 10, 10));
ll.add((20, 20, 20));
ll.add((30, 30, 30));
lclist ll2;
ll2.add((20, 20, 20));
ll2.add((30, 30, 30));
ll2.add((40, 40, 40));
bt_assert(ll.delete([]) = ll);
bt_assert(ll.filter([]) = ---empty---);
# lclist A (10, 20, 30)
bt_assert(format(ll) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30))");
# lclist B (20, 30, 40)
bt_assert(format(ll2) = "(lclist (20, 20, 20) (30, 30, 30) (40, 40, 40))");
# lclist A union B
lclist r = ll.add(ll2);
bt_assert(r = ---empty---.add((10,10,10)).add((20,20,20)).add((30,30,30)).add((40,40,40)));
bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20) (30, 30, 30) (40, 40, 40))");
# lclist A isect B
r = ll.filter(ll2);
bt_assert(r = ---empty---.add((20, 20, 20)).add((30, 30, 30)));
bt_assert(format(r) = "(lclist (20, 20, 20) (30, 30, 30))");
# lclist A \ B
r = ll.delete(ll2);
bt_assert(r = ---empty---.add((10, 10, 10)));
bt_assert(format(r) = "(lclist (10, 10, 10))");
# lclist in lc set
r = ll.filter([(5..15, *, *), (20, 15..25, *)]);
bt_assert(r = ---empty---.add((10, 10, 10)).add((20, 20, 20)));
bt_assert(format(r) = "(lclist (10, 10, 10) (20, 20, 20))");
# minimim & maximum element
r = ---empty---.add((2, 3, 3)).add((1, 2, 3)).add((2, 3, 1)).add((3, 1, 2)).add((2, 1, 3));
bt_assert(format(r) = "(lclist (2, 3, 3) (1, 2, 3) (2, 3, 1) (3, 1, 2) (2, 1, 3))");
bt_assert(r.min = (1, 2, 3));
bt_assert(r.max = (3, 1, 2));
# iteration over lclist
int x = 0;
int y = 0;
lc mx = (0, 0, 0);
for lc c in r do {
int asn2 = c.asn * c.asn;
x = x + asn2 * c.data1;
y = y + asn2 * c.data2;
if c > mx then mx = c;
}
bt_assert(x = 39 && y = 49);
bt_assert(mx = r.max);
}
bt_test_suite(t_lclist_new, "Testing lists of large communities");