return f_new_inst(FI_EA_SET, f_new_inst(FI_CONSTANT, empty), dyn);
}
-#if 0
-
-static inline struct f_inst *
-f_generate_dpair(struct f_inst *t1, struct f_inst *t2)
-{
- struct f_inst *rv;
-
- if ((t1->fi_code == FI_CONSTANT) && (t2->fi_code == FI_CONSTANT)) {
- if ((t1->val.type != T_INT) || (t2->val.type != T_INT))
- cf_error( "Can't operate with value of non-integer type in pair constructor");
-
- check_u16(t1->a[1].i);
- check_u16(t2->a[1].i);
-
- rv = f_new_inst(FI_CONSTANT);
- rv->val = (struct f_val) {
- .type = T_PAIR,
- .val.i = pair(t1->a[1].i, t2->a[1].i),
- };
- }
- else {
- rv = f_new_inst(FI_PAIR_CONSTRUCT);
- rv->a[0].p = t1;
- rv->a[1].p = t2;
- }
-
- return rv;
-}
-
-static inline struct f_inst *
-f_generate_ec(u16 kind, struct f_inst *tk, struct f_inst *tv)
-{
- struct f_inst *rv;
- int c1 = 0, c2 = 0, ipv4_used = 0;
- u32 key = 0, val2 = 0;
-
- if (tk->fi_code == FI_CONSTANT) {
- c1 = 1;
- struct f_val *val = &(tk->val);
-
- if (val->type == T_INT) {
- ipv4_used = 0; key = val->val.i;
- }
- else if (tk->val.type == T_QUAD) {
- ipv4_used = 1; key = val->val.i;
- }
- else if ((val->type == T_IP) && ipa_is_ip4(val->val.ip)) {
- ipv4_used = 1; key = ipa_to_u32(val->val.ip);
- }
- else
- cf_error("Can't operate with key of non-integer/IPv4 type in EC constructor");
- }
-
- if (tv->fi_code == FI_CONSTANT) {
- if (tv->val.type != T_INT)
- cf_error("Can't operate with value of non-integer type in EC constructor");
- c2 = 1;
- val2 = tv->val.val.i;
- }
-
- if (c1 && c2) {
- u64 ec;
-
- if (kind == EC_GENERIC) {
- ec = ec_generic(key, val2);
- }
- else if (ipv4_used) {
- check_u16(val2);
- ec = ec_ip4(kind, key, val2);
- }
- else if (key < 0x10000) {
- ec = ec_as2(kind, key, val2);
- }
- else {
- check_u16(val2);
- ec = ec_as4(kind, key, val2);
- }
-
- rv = f_new_inst(FI_CONSTANT);
- rv->val = (struct f_val) {
- .type = T_EC,
- .val.ec = ec,
- };
- }
- else {
- rv = f_new_inst(FI_EC_CONSTRUCT);
- rv->aux = kind;
- rv->a[0].p = tk;
- rv->a[1].p = tv;
- }
-
- return rv;
-}
-
-static inline struct f_inst *
-f_generate_lc(struct f_inst *t1, struct f_inst *t2, struct f_inst *t3)
-{
- struct f_inst *rv;
-
- if ((t1->fi_code == FI_CONSTANT) && (t2->fi_code == FI_CONSTANT) && (t3->fi_code == FI_CONSTANT)) {
- if ((t1->val.type != T_INT) || (t2->val.type != T_INT) || (t3->val.type != T_INT))
- cf_error( "LC - Can't operate with value of non-integer type in tuple constructor");
-
- rv = f_new_inst(FI_CONSTANT);
- rv->val = (struct f_val) {
- .type = T_LC,
- .val.lc = (lcomm) { t1->a[1].i, t2->a[1].i, t3->a[1].i },
- };
- }
- else
- {
- rv = f_new_inst(FI_LC_CONSTRUCT);
- rv->a[0].p = t1;
- rv->a[1].p = t2;
- rv->a[2].p = t3;
- }
-
- return rv;
-}
-
-static inline struct f_inst *
-f_generate_path_mask(struct f_inst *t)
-{
- uint len = 0;
- uint dyn = 0;
- for (const struct f_inst *tt = t; tt; tt = tt->next) {
- if (tt->fi_code != FI_CONSTANT)
- dyn++;
- len++;
- }
-
- if (dyn) {
- struct f_inst *pmc = f_new_inst(FI_PATHMASK_CONSTRUCT);
- pmc->a[0].p = t;
- pmc->a[1].i = len;
- return pmc;
- }
-
- struct f_path_mask *pm = cfg_allocz(sizeof(struct f_path_mask) + len * sizeof(struct f_path_mask_item));
-
- uint i = 0;
- for (const struct f_inst *tt = t; tt; tt = tt->next)
- pm->item[i++] = tt->val.val.pmi;
-
- pm->len = i;
- struct f_inst *pmc = f_new_inst(FI_CONSTANT);
- pmc->val = (struct f_val) { .type = T_PATH_MASK, .val.path_mask = pm, };
-
- return pmc;
-}
-
-#endif
-
/*
* Remove all new lines and doubled whitespaces
* and convert all tabulators to spaces
struct f_inst *setter, *getter, *checker;
switch (lval->type) {
case F_LVAL_VARIABLE:
- setter = f_new_inst(FI_SET, expr, lval->sym);
- getter = f_new_inst(FI_VARIABLE, lval->sym);
+ setter = f_new_inst(FI_VAR_SET, expr, lval->sym);
+ getter = f_new_inst(FI_VAR_GET, lval->sym);
break;
case F_LVAL_PREFERENCE:
setter = f_new_inst(FI_PREF_SET, expr);
CF_DECLS
CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
- ACCEPT, REJECT, ERROR, QUITBIRD,
+ ACCEPT, REJECT, ERROR,
INT, BOOL, IP, TYPE, PREFIX, RD, PAIR, QUAD, EC, LC,
SET, STRING, BGPMASK, BGPPATH, CLIST, ECLIST, LCLIST,
IF, THEN, ELSE, CASE,
TRUE, FALSE, RT, RO, UNKNOWN, GENERIC,
FROM, GW, NET, MASK, PROTO, SOURCE, SCOPE, DEST, IFNAME, IFINDEX,
PREFERENCE,
- ROA_CHECK, ASN, SRC,
+ ROA_CHECK, ASN, SRC, DST,
IS_V4, IS_V6,
LEN, MAXLEN,
DEFINED,
%nonassoc THEN
%nonassoc ELSE
-%type <xp> cmds_int function_body declsn function_params
-%type <x> term block cmd cmds constant constructor print_one print_list var_list var_listn function_call symbol_value bgp_path_expr bgp_path bgp_path_tail one_decl decls
+%type <xp> cmds_int cmd_prep
+%type <x> term block cmd cmds constant constructor print_list var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail
%type <fda> dynamic_attr
%type <fsa> static_attr
%type <f> filter where_filter
-%type <fl> filter_body
+%type <fl> filter_body function_body
%type <flv> lvalue
-%type <i> type
+%type <i> type function_args function_vars
%type <ecs> ec_kind
%type <fret> break_command
%type <i32> cnum
conf: filter_def ;
filter_def:
- FILTER CF_SYM_VOID { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); }
+ FILTER symbol { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); }
filter_body {
struct filter *f = cfg_alloc(sizeof(struct filter));
*f = (struct filter) { .sym = $2, .root = $4 };
conf: filter_eval ;
filter_eval:
- EVAL term { f_eval_int(f_postfixify($2)); }
+ EVAL term { f_eval_int(f_linearize($2)); }
;
conf: custom_attr ;
-custom_attr: ATTRIBUTE type CF_SYM_VOID ';' {
+custom_attr: ATTRIBUTE type symbol ';' {
cf_define_symbol($3, SYM_ATTRIBUTE, attribute, ca_lookup(new_config->pool, $3->name, $2)->fda);
};
}
;
-one_decl:
- type CF_SYM_VOID {
- struct f_val * val = cfg_alloc(sizeof(struct f_val));
- val->type = T_VOID;
- $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val, val);
- DBG( "New variable %s type %x\n", $2->name, $1 );
- $$ = f_new_inst(FI_SET, NULL, $2);
+function_argsn:
+ /* EMPTY */
+ | function_argsn type symbol ';' {
+ if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
+ cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
}
;
-/* Decls with ';' at the end. Beware; these are reversed. */
-decls: /* EMPTY */ { $$ = NULL; }
- | one_decl ';' decls {
- $$ = $1;
- $$->next = $3;
+function_args:
+ '(' ')' { $$ = 0; }
+ | '(' function_argsn type symbol ')' {
+ cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++);
+ $$ = $4->scope->slots;
}
;
-/* Declarations that have no ';' at the end. */
-declsn: one_decl { $$[0] = $$[1] = $1; }
- | one_decl ';' declsn {
- $3[1]->next = $1;
- $$[1] = $3[1] = $1;
- $$[0] = $3[0];
+function_vars:
+ /* EMPTY */ { $$ = 0; }
+ | function_vars type symbol ';' {
+ cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
+ $$ = $1 + 1;
}
;
-filter_body:
- function_body {
- if ($1[0]) {
- const struct f_inst *inst[2] = { $1[0], $1[1] };
- $$ = f_postfixify_concat(inst, 2);
- }
- else
- $$ = f_postfixify($1[1]);
- }
- ;
+filter_body: function_body ;
filter:
CF_SYM_KNOWN {
}
;
-function_params:
- '(' declsn ')' { $$[0] = $2[0]; $$[1] = $2[1]; }
- | '(' ')' { $$[0] = $$[1] = NULL; }
- ;
-
function_body:
- decls '{' cmds '}' {
- $$[0] = $1 ? f_clear_local_vars($1) : NULL;
- $$[1] = $3;
+ function_vars '{' cmds '}' {
+ $$ = f_linearize($3);
+ $$->vars = $1;
}
;
conf: function_def ;
function_def:
- FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name );
+ FUNCTION symbol { DBG( "Beginning of function %s\n", $2->name );
$2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
cf_push_scope($2);
- } function_params function_body {
- const struct f_inst *catlist[4];
- uint count = 0;
-
- /* Argument setters */
- if ($4[0])
- catlist[count++] = $4[0];
-
- /* Local var clearers */
- if ($5[0])
- catlist[count++] = $5[0];
-
- /* Return void if no return is needed */
- catlist[count++] = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
-
- /* Function body itself */
- if ($5[1])
- catlist[count++] = $5[1];
-
- struct f_line *fl = f_postfixify_concat(catlist, count);
-
- fl->args = 0;
- for (const struct f_inst *arg = $4[0]; arg; arg = arg->next)
- fl->args++;
-
- $2->function = fl;
-
+ } function_args function_body {
+ DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars);
+ $5->args = $4;
+ $2->function = $5;
cf_pop_scope();
}
;
/* Programs */
cmds: /* EMPTY */ { $$ = NULL; }
- | cmds_int { $$ = $1[0]; }
+ | cmds_int { $$ = $1.begin; }
;
-cmds_int: cmd { $$[0] = $$[1] = $1; }
- | cmds_int cmd { $$[1] = $2; $1[1]->next = $2; $$[0] = $1[0]; }
+cmd_prep: cmd {
+ $$.begin = $$.end = $1;
+ if ($1)
+ while ($$.end->next)
+ $$.end = $$.end->next;
+}
+ ;
+
+cmds_int: cmd_prep
+ | cmds_int cmd_prep {
+ if (!$1.begin)
+ $$ = $2;
+ else if (!$2.begin)
+ $$ = $1;
+ else {
+ $$.begin = $1.begin;
+ $$.end = $2.end;
+ $1.end->next = $2.begin;
+ }
+ }
;
block:
| VPN_RD { $$.type = T_RD; $$.val.ec = $1; }
| ENUM { $$.type = pair_a($1); $$.val.i = pair_b($1); }
| '(' term ')' {
- if (f_eval(f_postfixify($2), cfg_mem, &($$)) > F_RETURN) cf_error("Runtime error");
+ if (f_eval(f_linearize($2), cfg_mem, &($$)) > F_RETURN) cf_error("Runtime error");
if (!f_valid_set_type($$.type)) cf_error("Set-incompatible type");
}
| CF_SYM_KNOWN {
switch_atom:
NUM { $$.type = T_INT; $$.val.i = $1; }
- | '(' term ')' { $$.type = T_INT; $$.val.i = f_eval_int(f_postfixify($2)); }
+ | '(' term ')' { $$.type = T_INT; $$.val.i = f_eval_int(f_linearize($2)); }
| fipa { $$ = $1; }
| ENUM { $$.type = pair_a($1); $$.val.i = pair_b($1); }
;
cnum:
- term { $$ = f_eval_int(f_postfixify($1)); }
+ term { $$ = f_eval_int(f_linearize($1)); }
pair_item:
'(' cnum ',' cnum ')' { $$ = f_new_pair_item($2, $2, $4, $4); }
;
fprefix_set:
- fprefix { $$ = f_new_trie(cfg_mem, sizeof(struct f_trie_node)); trie_add_prefix($$, &($1.net), $1.lo, $1.hi); }
- | fprefix_set ',' fprefix { $$ = $1; trie_add_prefix($$, &($3.net), $3.lo, $3.hi); }
+ fprefix { $$ = f_new_trie(cfg_mem, 0); trie_add_prefix($$, &($1.net), $1.lo, $1.hi); }
+ | fprefix_set ',' fprefix { $$ = $1; if (!trie_add_prefix($$, &($3.net), $3.lo, $3.hi)) cf_error("Mixed IPv4/IPv6 prefixes in prefix set"); }
;
switch_body: /* EMPTY */ { $$ = NULL; }
| switch_body switch_items ':' cmds {
/* Fill data fields */
struct f_tree *t;
- struct f_line *line = f_postfixify($4);
+ struct f_line *line = f_linearize($4);
for (t = $2; t; t = t->left)
t->data = line;
$$ = f_merge_items($1, $2);
struct f_tree *t = f_new_tree();
t->from.type = t->to.type = T_VOID;
t->right = t;
- t->data = f_postfixify($3);
+ t->data = f_linearize($3);
$$ = f_merge_items($1, t);
}
;
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; }
+ | '[' 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;
+ }
| '*' bgp_path_tail { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .kind = PM_ASTERISK }, }); $$->next = $2; }
| '?' bgp_path_tail { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PATH_MASK_ITEM, .val.pmi = { .kind = PM_QUESTION }, }); $$->next = $2; }
| bgp_path_expr bgp_path_tail { $$ = $1; $$->next = $2; }
'(' term ',' term ')' { $$ = f_new_inst(FI_PAIR_CONSTRUCT, $2, $4); }
| '(' ec_kind ',' term ',' term ')' { $$ = f_new_inst(FI_EC_CONSTRUCT, $4, $6, $2); }
| '(' term ',' term ',' term ')' { $$ = f_new_inst(FI_LC_CONSTRUCT, $2, $4, $6); }
- | bgp_path { $$ = f_new_inst(FI_PATHMASK_CONSTRUCT, $1, 0); }
+ | bgp_path { $$ = f_new_inst(FI_PATHMASK_CONSTRUCT, $1); }
;
+/* This generates the function_call variable list backwards. */
+var_list: /* EMPTY */ { $$ = NULL; }
+ | term { $$ = $1; }
+ | var_list ',' term { $$ = $3; $$->next = $1; }
+
function_call:
CF_SYM_KNOWN '(' var_list ')' {
- $$ = f_new_inst(FI_CALL, $1, $3);
+ if ($1->class != SYM_FUNCTION)
+ cf_error("You can't call something which is not a function. Really.");
+
+ struct f_inst *fc = f_new_inst(FI_CALL, $1);
+ uint args = 0;
+ while ($3) {
+ args++;
+ struct f_inst *tmp = $3->next;
+ $3->next = fc;
+
+ fc = $3;
+ $3 = tmp;
+ }
+
+ if (args != $1->function->args)
+ cf_error("Function call '%s' got %u arguments, need %u arguments.",
+ $1->name, args, $1->function->args);
+
+ $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
+ $$->next = fc;
}
;
{
switch ($1->class) {
case SYM_CONSTANT_RANGE:
+ $$ = f_new_inst(FI_CONSTANT, *($1->val));
+ break;
case SYM_VARIABLE_RANGE:
- $$ = f_new_inst(FI_VARIABLE, $1);
+ $$ = f_new_inst(FI_VAR_GET, $1);
break;
case SYM_ATTRIBUTE:
$$ = f_new_inst(FI_EA_GET, *$1->attribute);
| term '.' LEN { $$ = f_new_inst(FI_LENGTH, $1); }
| term '.' MAXLEN { $$ = f_new_inst(FI_ROA_MAXLEN, $1); }
| term '.' ASN { $$ = f_new_inst(FI_ROA_ASN, $1); }
- | term '.' SRC { $$ = f_new_inst(FI_SADR_SRC, $1); }
+ | term '.' SRC { $$ = f_new_inst(FI_NET_SRC, $1); }
+ | term '.' DST { $$ = f_new_inst(FI_NET_DST, $1); }
| term '.' MASK '(' term ')' { $$ = f_new_inst(FI_IP_MASK, $1, $5); }
| term '.' FIRST { $$ = f_new_inst(FI_AS_PATH_FIRST, $1); }
| term '.' LAST { $$ = f_new_inst(FI_AS_PATH_LAST, $1); }
;
break_command:
- QUITBIRD { $$ = F_QUITBIRD; }
- | ACCEPT { $$ = F_ACCEPT; }
+ ACCEPT { $$ = F_ACCEPT; }
| REJECT { $$ = F_REJECT; }
| ERROR { $$ = F_ERROR; }
- | PRINT { $$ = F_NOP; }
- | PRINTN { $$ = F_NONL; }
- ;
-
-print_one:
- term { $$ = f_new_inst(FI_PRINT, $1); }
;
print_list: /* EMPTY */ { $$ = NULL; }
- | print_one { $$ = $1; }
- | print_one ',' print_list {
- if ($1) {
- $1->next = $3;
- $$ = $1;
- } else $$ = $3;
- }
- ;
-
-var_listn: term {
- $$ = $1;
- }
- | term ',' var_listn {
+ | term { $$ = $1; }
+ | term ',' print_list {
+ ASSERT($1);
+ ASSERT($1->next == NULL);
+ $1->next = $3;
$$ = $1;
- $$->next = $3;
}
;
-var_list: /* EMPTY */ { $$ = NULL; }
- | var_listn { $$ = $1; }
- ;
-
cmd:
IF term THEN block {
$$ = f_new_inst(FI_CONDITION, $2, $4, NULL);
| CF_SYM_KNOWN '=' term ';' {
switch ($1->class) {
case SYM_VARIABLE_RANGE:
- $$ = f_new_inst(FI_SET, $3, $1);
+ $$ = f_new_inst(FI_VAR_SET, $3, $1);
break;
case SYM_ATTRIBUTE:
$$ = f_new_inst(FI_EA_SET, $3, *$1->attribute);
| UNSET '(' dynamic_attr ')' ';' {
$$ = f_new_inst(FI_EA_UNSET, $3);
}
- | break_command print_list ';' { $$ = f_new_inst(FI_PRINT_AND_DIE, $2, $1); }
+ | break_command print_list ';' {
+ struct f_inst *breaker = f_new_inst(FI_DIE, $1);
+ if ($2) {
+ struct f_inst *printer = f_new_inst(FI_PRINT, $2);
+ struct f_inst *flusher = f_new_inst(FI_FLUSH);
+ printer->next = flusher;
+ flusher->next = breaker;
+ $$ = printer;
+ } else
+ $$ = breaker;
+ }
+ | PRINT print_list ';' {
+ $$ = f_new_inst(FI_PRINT, $2);
+ $$->next = f_new_inst(FI_FLUSH);
+ }
+ | PRINTN print_list ';' {
+ $$ = f_new_inst(FI_PRINT, $2);
+ }
| function_call ';' { $$ = f_new_inst(FI_DROP_RESULT, $1); }
| CASE term '{' switch_body '}' {
$$ = f_new_inst(FI_SWITCH, $2, build_tree($4));