]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Filters: split the large filter.h file to smaller files.
authorMaria Matejka <mq@ucw.cz>
Thu, 7 Feb 2019 20:25:38 +0000 (21:25 +0100)
committerMaria Matejka <mq@ucw.cz>
Wed, 20 Feb 2019 21:30:54 +0000 (22:30 +0100)
This should be revised, there are still ugly things in the filter API.

17 files changed:
conf/cf-lex.l
filter/config.Y
filter/f-inst.h
filter/f-util.c
filter/f-util.h [new file with mode: 0644]
filter/filter.c
filter/filter.h
filter/filter_test.c
filter/new.m4
filter/tree.c
filter/tree_test.c
filter/trie.c
filter/trie_test.c
nest/a-path.c
nest/cmds.c
nest/rt-table.c
sysdep/unix/main.c

index 5e7c84180e3d03152ad4ff9060634241d91a4887..6cc09425b44f285632b4cdfec53e732c8f6471f6 100644 (file)
@@ -45,6 +45,7 @@
 #include "nest/route.h"
 #include "nest/protocol.h"
 #include "filter/filter.h"
+#include "filter/f-inst.h"
 #include "conf/conf.h"
 #include "conf/cf-parse.tab.h"
 #include "lib/string.h"
index 1306849fd829967d2783292f6511a3f1d79f00ef..31ceb3f5460e1c37a70adf0025356ca3fde82ab5 100644 (file)
@@ -10,6 +10,9 @@
 
 CF_HDR
 
+#include "filter/f-inst.h"
+#include "filter/f-util.h"
+
 CF_DEFINES
 
 static inline u32 pair(u32 a, u32 b) { return (a << 16) | b; }
index 5c3d1d58e2cbad02c88c51ada924ccd5113f039c..63124aa130b6154aa5ca293ed450d083e728c35c 100644 (file)
@@ -1,76 +1,99 @@
 /*
  *     BIRD Internet Routing Daemon -- Filter instructions
  *
+ *     (c) 1999 Pavel Machek <pavel@ucw.cz>
  *     (c) 2018--2019 Maria Matejka <mq@jmq.cz>
  *
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-/* Filter instruction words */
-#define FI__TWOCHAR(a,b)       ((a<<8) | b)
+#ifndef _BIRD_F_INST_H_
+#define _BIRD_F_INST_H_
+
+#include "filter/filter.h"
+#include "filter/f-util.h"
+
+/* Filter l-value type */
+enum f_lval_type {
+  F_LVAL_VARIABLE,
+  F_LVAL_PREFERENCE,
+  F_LVAL_SA,
+  F_LVAL_EA,
+};
+
+/* Filter l-value */
+struct f_lval {
+  enum f_lval_type type;
+  union {
+    const struct symbol *sym;
+    struct f_dynamic_attr da;
+    struct f_static_attr sa;
+  };
+};
+
+/* Filter instruction declarations */
 #define FI__LIST \
-  F(FI_NOP,                      0, '0') \
-  F(FI_ADD,                      0, '+') \
-  F(FI_SUBTRACT,                 0, '-') \
-  F(FI_MULTIPLY,                 0, '*') \
-  F(FI_DIVIDE,                   0, '/') \
-  F(FI_AND,                      0, '&') \
-  F(FI_OR,                       0, '|') \
-  F(FI_PAIR_CONSTRUCT,         'm', 'p') \
-  F(FI_EC_CONSTRUCT,           'm', 'c') \
-  F(FI_LC_CONSTRUCT,           'm', 'l') \
-  F(FI_PATHMASK_CONSTRUCT,     'm', 'P') \
-  F(FI_NEQ,                    '!', '=') \
-  F(FI_EQ,                     '=', '=') \
-  F(FI_LT,                       0, '<') \
-  F(FI_LTE,                    '<', '=') \
-  F(FI_NOT,                      0, '!') \
-  F(FI_MATCH,                    0, '~') \
-  F(FI_NOT_MATCH,              '!', '~') \
-  F(FI_DEFINED,                        'd', 'e') \
-  F(FI_TYPE,                     0, 'T') \
-  F(FI_IS_V4,                  'I', 'i') \
-  F(FI_SET,                      0, 's') \
-  F(FI_CONSTANT,                 0, 'c') \
-  F(FI_VARIABLE,                 0, 'V') \
-  F(FI_CONSTANT_INDIRECT,        0, 'C') \
-  F(FI_PRINT,                    0, 'p') \
-  F(FI_CONDITION,                0, '?') \
-  F(FI_PRINT_AND_DIE,          'p', ',') \
-  F(FI_RTA_GET,                          0, 'a') \
-  F(FI_RTA_SET,                        'a', 'S') \
-  F(FI_EA_GET,                 'e', 'a') \
-  F(FI_EA_SET,                 'e', 'S') \
-  F(FI_PREF_GET,                 0, 'P') \
-  F(FI_PREF_SET,               'P', 'S') \
-  F(FI_LENGTH,                   0, 'L') \
-  F(FI_ROA_MAXLEN,             'R', 'M') \
-  F(FI_ROA_ASN,                        'R', 'A') \
-  F(FI_SADR_SRC,               'n', 's') \
-  F(FI_IP,                     'c', 'p') \
-  F(FI_ROUTE_DISTINGUISHER,    'R', 'D') \
-  F(FI_AS_PATH_FIRST,          'a', 'f') \
-  F(FI_AS_PATH_LAST,           'a', 'l') \
-  F(FI_AS_PATH_LAST_NAG,       'a', 'L') \
-  F(FI_RETURN,                   0, 'r') \
-  F(FI_CALL,                   'c', 'a') \
-  F(FI_DROP_RESULT,            'd', 'r') \
-  F(FI_CLEAR_LOCAL_VARS,       'c', 'V') \
-  F(FI_SWITCH,                 'S', 'W') \
-  F(FI_IP_MASK,                        'i', 'M') \
-  F(FI_PATH_PREPEND,           'A', 'p') \
-  F(FI_CLIST_ADD,              'C', 'a') \
-  F(FI_CLIST_DEL,              'C', 'd') \
-  F(FI_CLIST_FILTER,           'C', 'f') \
-  F(FI_ROA_CHECK_IMPLICIT,     'R', 'i') \
-  F(FI_ROA_CHECK_EXPLICIT,     'R', 'e') \
-  F(FI_FORMAT,                   0, 'F') \
-  F(FI_ASSERT,                 'a', 's')
+  F(FI_NOP) \
+  F(FI_ADD, ARG, ARG) \
+  F(FI_SUBTRACT, ARG, ARG) \
+  F(FI_MULTIPLY, ARG, ARG) \
+  F(FI_DIVIDE, ARG, ARG) \
+  F(FI_AND, ARG, LINE) \
+  F(FI_OR, ARG, LINE) \
+  F(FI_PAIR_CONSTRUCT, ARG, ARG) \
+  F(FI_EC_CONSTRUCT, ARG, ARG, ECS) \
+  F(FI_LC_CONSTRUCT, ARG, ARG, ARG) \
+  F(FI_PATHMASK_CONSTRUCT, ARG, COUNT) \
+  F(FI_NEQ, ARG, ARG) \
+  F(FI_EQ, ARG, ARG) \
+  F(FI_LT, ARG, ARG) \
+  F(FI_LTE, ARG, ARG) \
+  F(FI_NOT, ARG) \
+  F(FI_MATCH, ARG, ARG) \
+  F(FI_NOT_MATCH, ARG, ARG) \
+  F(FI_DEFINED, ARG) \
+  F(FI_TYPE, ARG) \
+  F(FI_IS_V4, ARG) \
+  F(FI_SET, ARG, SYMBOL) \
+  F(FI_CONSTANT, VALI) \
+  F(FI_VARIABLE, SYMBOL) \
+  F(FI_CONSTANT_INDIRECT, VALP) \
+  F(FI_PRINT, ARG) \
+  F(FI_CONDITION, ARG, LINE, LINE) \
+  F(FI_PRINT_AND_DIE, ARG, FRET) \
+  F(FI_RTA_GET, SA) \
+  F(FI_RTA_SET, ARG, SA) \
+  F(FI_EA_GET, EA) \
+  F(FI_EA_SET, ARG, EA) \
+  F(FI_EA_UNSET, EA) \
+  F(FI_PREF_GET) \
+  F(FI_PREF_SET, ARG) \
+  F(FI_LENGTH, ARG) \
+  F(FI_ROA_MAXLEN, ARG) \
+  F(FI_ROA_ASN, ARG) \
+  F(FI_SADR_SRC, ARG) \
+  F(FI_IP, ARG) \
+  F(FI_ROUTE_DISTINGUISHER, ARG) \
+  F(FI_AS_PATH_FIRST, ARG) \
+  F(FI_AS_PATH_LAST, ARG) \
+  F(FI_AS_PATH_LAST_NAG, ARG) \
+  F(FI_RETURN, ARG) \
+  F(FI_CALL, SYMBOL, LINE) \
+  F(FI_DROP_RESULT, ARG) \
+  F(FI_SWITCH, ARG, TREE) \
+  F(FI_IP_MASK, ARG, ARG) \
+  F(FI_PATH_PREPEND, ARG, ARG) \
+  F(FI_CLIST_ADD, ARG, ARG) \
+  F(FI_CLIST_DEL, ARG, ARG) \
+  F(FI_CLIST_FILTER, ARG, ARG) \
+  F(FI_ROA_CHECK_IMPLICIT, RTC) \
+  F(FI_ROA_CHECK_EXPLICIT, ARG, ARG, RTC) \
+  F(FI_FORMAT, ARG) \
+  F(FI_ASSERT, ARG, STRING)
 
 /* The enum itself */
 enum f_instruction_code {
-#define F(c,a,b) \
-  c,
+#define F(c, ...) c,
 FI__LIST
 #undef F
   FI__MAX,
@@ -79,46 +102,107 @@ FI__LIST
 /* Convert the instruction back to the enum name */
 const char *f_instruction_name(enum f_instruction_code fi);
 
+struct f_inst;
+void f_inst_next(struct f_inst *first, const struct f_inst *append);
+struct f_inst *f_clear_local_vars(struct f_inst *decls);
+
+#define FIA(x) , FIA_##x
+#define FIA_ARG        const struct f_inst *
+#define FIA_LINE const struct f_inst *
+#define FIA_COUNT uint
+#define FIA_SYMBOL const struct symbol *
+#define FIA_VALI struct f_val
+#define FIA_VALP const struct f_val *
+#define FIA_FRET enum filter_return
+#define FIA_ECS enum ec_subtype
+#define FIA_SA struct f_static_attr
+#define FIA_EA struct f_dynamic_attr
+#define FIA_RTC const struct rtable_config *
+#define FIA_TREE const struct f_tree *
+#define FIA_STRING const char *
+#define F(c, ...) \
+  struct f_inst *f_new_inst_##c(enum f_instruction_code MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))()(MACRO_FOREACH(FIA, __VA_ARGS__)));
+FI__LIST
+#undef F
+#undef FIA_ARG
+#undef FIA_LINE
+#undef FIA_LINEP
+#undef FIA_COUNT
+#undef FIA_SYMBOL
+#undef FIA_VALI
+#undef FIA_VALP
+#undef FIA_FRET
+#undef FIA_ECS
+#undef FIA_SA
+#undef FIA_EA
+#undef FIA_RTC
+#undef FIA_STRING
+#undef FIA
+
+#define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
+
+/* Flags for instructions */
+enum f_instruction_flags {
+  FIF_PRINTED = 1,             /* FI_PRINT_AND_DIE: message put in buffer */
+};
 
+/* Filter structures for execution */
+struct f_line;
 
-/* Instruction structure for config */
-struct f_inst {
-  const struct f_inst *next;           /* Next instruction to be executed */
-  union {                              /* Instruction content */
-    struct {                           /* Instruction code for dispatching purposes */
-      enum f_instruction_code fi_code;
-    };
-
+/* The single instruction item */
+struct f_line_item {
+  enum f_instruction_code fi_code;     /* What to do */
+  enum f_instruction_flags flags;      /* Flags, instruction-specific */
+  uint lineno;                         /* Where */
+  union {
     struct {
-      enum f_instruction_code fi_code_a;
-      const struct f_inst *p[3];       /* Three arguments at most */
+      const struct f_val *vp;
+      const struct symbol *sym;
     };
+    struct f_val val;
+    const struct f_line *lines[2];
+    enum filter_return fret;
+    struct f_static_attr sa;
+    struct f_dynamic_attr da;
+    enum ec_subtype ecs;
+    const char *s;
+    const struct f_tree *tree;
+    const struct rtable_config *rtc;
+    uint count;
+  };                                   /* Additional instruction data */
+};
 
-    struct {
-
-
-
-    struct {
-      enum f_instruction_code 
-
-
-
-
-    enum f_iknst
-  u16 aux;             /* Extension to instruction code, T_*, EA_*, EAF_*  */
-  union {
+/* Line of instructions to be unconditionally executed one after another */
+struct f_line {
+  uint len;                            /* Line length */
+  struct f_line_item items[0];         /* The items themselves */
+};
 
-    union f_inst_attr a[3];            /* The three arguments */
-    struct f_val val;  /* The value if FI_CONSTANT */
-    struct {
-      union f_inst_attr sa_a[1];
-      struct f_static_attr sa; /* Static attribute def for FI_RTA_* */
-    };
-    struct {
-      union f_inst_attr da_a[1];
-      struct f_dynamic_attr da; /* Dynamic attribute def for FI_EA_* */
-    };
-  };
-  int lineno;
+/* Convert the f_inst infix tree to the f_line structures */
+struct f_line *f_postfixify_concat(const struct f_inst * const inst[], uint count);
+static inline struct f_line *f_postfixify(const struct f_inst *root)
+{ return f_postfixify_concat(&root, 1); }
+
+struct filter *f_new_where(const struct f_inst *);
+static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
+{ return (struct f_dynamic_attr) { .type = type, .bit = bit, .f_type = f_type, .ea_code = code }; }   /* f_type currently unused; will be handy for static type checking */
+static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
+{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
+struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
+struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
+
+/* Hook for call bt_assert() function in configuration */
+extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
+
+/* Bird Tests */
+struct f_bt_test_suite {
+  node n;                      /* Node in config->tests */
+  struct f_line *fn;           /* Root of function */
+  const char *fn_name;         /* Name of test */
+  const char *dsc;             /* Description */
 };
 
+/* Include the auto-generated structures */
+#include "filter/f-inst-struct.h"
+
+#endif
index e94331b66f76eed01961dedb73a6a1bd9de62e8c..eb948fa0baa7e63a1f05c786eb6be6cab65ddcb9 100644 (file)
@@ -10,7 +10,7 @@
 #include "nest/bird.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
-#include "filter/f-inst-struct.h"
+#include "filter/f-inst.h"
 #include "lib/idm.h"
 #include "nest/protocol.h"
 #include "nest/route.h"
diff --git a/filter/f-util.h b/filter/f-util.h
new file mode 100644 (file)
index 0000000..a7d77bb
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ *     BIRD Internet Routing Daemon -- Filter utils
+ *
+ *     (c) 1999 Pavel Machek <pavel@ucw.cz>
+ *     (c) 2018--2019 Maria Matejka <mq@jmq.cz>
+ *
+ *     Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_F_UTIL_H_
+#define _BIRD_F_UTIL_H_
+
+/* IP prefix range structure */
+struct f_prefix {
+  net_addr net;                /* The matching prefix must match this net */
+  u8 lo, hi;           /* And its length must fit between lo and hi */
+};
+
+/* Type numbers must be in 0..0xff range */
+#define T_MASK 0xff
+
+/* Internal types */
+enum f_type {
+/* Do not use type of zero, that way we'll see errors easier. */
+  T_VOID = 1,
+
+/* User visible types, which fit in int */
+  T_INT = 0x10,
+  T_BOOL = 0x11,
+  T_PAIR = 0x12,  /*   Notice that pair is stored as integer: first << 16 | second */
+  T_QUAD = 0x13,
+
+/* Put enumerational types in 0x30..0x3f range */
+  T_ENUM_LO = 0x30,
+  T_ENUM_HI = 0x3f,
+
+  T_ENUM_RTS = 0x30,
+  T_ENUM_BGP_ORIGIN = 0x31,
+  T_ENUM_SCOPE = 0x32,
+  T_ENUM_RTC = 0x33,
+  T_ENUM_RTD = 0x34,
+  T_ENUM_ROA = 0x35,
+  T_ENUM_NETTYPE = 0x36,
+  T_ENUM_RA_PREFERENCE = 0x37,
+
+/* new enums go here */
+  T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */
+
+#define T_ENUM T_ENUM_LO ... T_ENUM_HI
+
+/* Bigger ones */
+  T_IP = 0x20,
+  T_NET = 0x21,
+  T_STRING = 0x22,
+  T_PATH_MASK = 0x23,  /* mask for BGP path */
+  T_PATH = 0x24,               /* BGP path */
+  T_CLIST = 0x25,              /* Community list */
+  T_EC = 0x26,         /* Extended community value, u64 */
+  T_ECLIST = 0x27,             /* Extended community list */
+  T_LC = 0x28,         /* Large community value, lcomm */
+  T_LCLIST = 0x29,             /* Large community list */
+  T_RD = 0x2a,         /* Route distinguisher for VPN addresses */
+  T_PATH_MASK_ITEM = 0x2b,     /* Path mask item for path mask constructors */
+
+  T_SET = 0x80,
+  T_PREFIX_SET = 0x81,
+} PACKED;
+
+/* Filter value; size of this affects filter memory consumption */
+struct f_val {
+  enum f_type type;    /* T_*  */
+  union {
+    uint i;
+    u64 ec;
+    lcomm lc;
+    ip_addr ip;
+    const net_addr *net;
+    char *s;
+    const struct f_tree *t;
+    const struct f_trie *ti;
+    const struct adata *ad;
+    const struct f_path_mask *path_mask;
+    struct f_path_mask_item pmi;
+  } val;
+};
+
+#define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
+
+/* Dynamic attribute definition (eattrs) */
+struct f_dynamic_attr {
+  u8 type;             /* EA type (EAF_*) */
+  u8 bit;              /* For bitfield accessors */
+  enum f_type f_type;  /* Filter type */
+  uint ea_code;                /* EA code */
+};
+
+enum f_sa_code {
+  SA_FROM = 1,
+  SA_GW,
+  SA_NET,
+  SA_PROTO,
+  SA_SOURCE,
+  SA_SCOPE,
+  SA_DEST,
+  SA_IFNAME,
+  SA_IFINDEX,
+} PACKED;
+
+/* Static attribute definition (members of struct rta) */
+struct f_static_attr {
+  enum f_type f_type;          /* Filter type */
+  enum f_sa_code sa_code;      /* Static attribute id */
+  int readonly:1;                      /* Don't allow writing */
+};
+
+struct f_tree {
+  struct f_tree *left, *right;
+  struct f_val from, to;
+  void *data;
+};
+
+struct f_trie_node
+{
+  ip_addr addr, mask, accept;
+  uint plen;
+  struct f_trie_node *c[2];
+};
+
+struct f_trie
+{
+  linpool *lp;
+  int zero;
+  uint node_size;
+  struct f_trie_node root[0];          /* Root trie node follows */
+};
+
+struct f_tree *f_new_tree(void);
+struct f_tree *build_tree(struct f_tree *);
+const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
+int same_tree(const struct f_tree *t0, const struct f_tree *t2);
+void tree_format(const struct f_tree *t, buffer *buf);
+
+struct f_trie *f_new_trie(linpool *lp, uint node_size);
+void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
+int trie_match_net(const struct f_trie *t, const net_addr *n);
+int trie_same(const struct f_trie *t1, const struct f_trie *t2);
+void trie_format(const struct f_trie *t, buffer *buf);
+
+extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist;
+
+#endif
index bb5e4235120d3e59258cde73f6f751aeacc469a6..ff702f2bc1a03e61d6807e2e6d804cc0ccd100f5 100644 (file)
@@ -47,7 +47,8 @@
 #include "nest/attrs.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
-#include "filter/f-inst-struct.h"
+#include "filter/f-inst.h"
+#include "filter/f-util.h"
 
 #define CMP_ERROR 999
 
@@ -715,12 +716,39 @@ f_postfixify_concat(const struct f_inst * const inst[], uint count)
 static enum filter_return
 interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val)
 {
-  struct f_val_stack vstk;
+
+#define F_VAL_STACK_MAX        4096
+  /* Value stack for execution */
+  struct f_val_stack {
+    uint cnt;                          /* Current stack size; 0 for empty */
+    struct f_val val[F_VAL_STACK_MAX]; /* The stack itself */
+  } vstk;
+
+  /* The stack itself is intentionally kept as-is for performance reasons.
+   * Do NOT rewrite this to initialization by struct literal. It's slow.
+   */
   vstk.cnt = 0;
+#define F_EXEC_STACK_MAX 4096
+
+  /* Exception bits */
+  enum f_exception {
+    FE_RETURN = 0x1,
+  };
 
-  struct f_exec_stack estk;
+  /* Instruction stack for execution */
+  struct f_exec_stack {
+    struct {
+      const struct f_line *line;               /* The line that is being executed */
+      uint pos;                                /* Instruction index in the line */
+      uint ventry;                     /* Value stack depth on entry */
+      enum f_exception emask;          /* Exception mask */
+    } item[F_EXEC_STACK_MAX];
+    uint cnt;                          /* Current stack size; 0 for empty */
+  } estk;
+
+  /* The same as with the value stack. Not resetting the stack for performance reasons. */
   estk.cnt = 1;
-  estk.item[0].line = line;
+  estk.item[0].line = line;            
   estk.item[0].pos = 0;
 
 #define curline estk.item[estk.cnt-1]
index 15a24fd4a4f1fbec89d18c592d32a82a38160250..9b3886fb49740ec8bf3df35345e23b68932289e1 100644 (file)
 #include "nest/route.h"
 #include "nest/attrs.h"
 
-/* IP prefix range structure */
-struct f_prefix {
-  net_addr net;                /* The matching prefix must match this net */
-  u8 lo, hi;           /* And its length must fit between lo and hi */
-};
-
-/* Type numbers must be in 0..0xff range */
-#define T_MASK 0xff
-
-/* Internal types */
-enum f_type {
-/* Do not use type of zero, that way we'll see errors easier. */
-  T_VOID = 1,
-
-/* User visible types, which fit in int */
-  T_INT = 0x10,
-  T_BOOL = 0x11,
-  T_PAIR = 0x12,  /*   Notice that pair is stored as integer: first << 16 | second */
-  T_QUAD = 0x13,
-
-/* Put enumerational types in 0x30..0x3f range */
-  T_ENUM_LO = 0x30,
-  T_ENUM_HI = 0x3f,
-
-  T_ENUM_RTS = 0x30,
-  T_ENUM_BGP_ORIGIN = 0x31,
-  T_ENUM_SCOPE = 0x32,
-  T_ENUM_RTC = 0x33,
-  T_ENUM_RTD = 0x34,
-  T_ENUM_ROA = 0x35,
-  T_ENUM_NETTYPE = 0x36,
-  T_ENUM_RA_PREFERENCE = 0x37,
-
-/* new enums go here */
-  T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */
-
-#define T_ENUM T_ENUM_LO ... T_ENUM_HI
-
-/* Bigger ones */
-  T_IP = 0x20,
-  T_NET = 0x21,
-  T_STRING = 0x22,
-  T_PATH_MASK = 0x23,  /* mask for BGP path */
-  T_PATH = 0x24,               /* BGP path */
-  T_CLIST = 0x25,              /* Community list */
-  T_EC = 0x26,         /* Extended community value, u64 */
-  T_ECLIST = 0x27,             /* Extended community list */
-  T_LC = 0x28,         /* Large community value, lcomm */
-  T_LCLIST = 0x29,             /* Large community list */
-  T_RD = 0x2a,         /* Route distinguisher for VPN addresses */
-  T_PATH_MASK_ITEM = 0x2b,     /* Path mask item for path mask constructors */
-
-  T_SET = 0x80,
-  T_PREFIX_SET = 0x81,
-} PACKED;
-
-/* Filter value; size of this affects filter memory consumption */
-struct f_val {
-  enum f_type type;    /* T_*  */
-  union {
-    uint i;
-    u64 ec;
-    lcomm lc;
-    ip_addr ip;
-    const net_addr *net;
-    char *s;
-    const struct f_tree *t;
-    const struct f_trie *ti;
-    const struct adata *ad;
-    const struct f_path_mask *path_mask;
-    struct f_path_mask_item pmi;
-  } val;
-};
-
-/* Dynamic attribute definition (eattrs) */
-struct f_dynamic_attr {
-  u8 type;             /* EA type (EAF_*) */
-  u8 bit;              /* For bitfield accessors */
-  enum f_type f_type;  /* Filter type */
-  uint ea_code;                /* EA code */
-};
-
-enum f_sa_code {
-  SA_FROM = 1,
-  SA_GW,
-  SA_NET,
-  SA_PROTO,
-  SA_SOURCE,
-  SA_SCOPE,
-  SA_DEST,
-  SA_IFNAME,
-  SA_IFINDEX,
-} PACKED;
-
-/* Static attribute definition (members of struct rta) */
-struct f_static_attr {
-  enum f_type f_type;          /* Filter type */
-  enum f_sa_code sa_code;      /* Static attribute id */
-  int readonly:1;                      /* Don't allow writing */
-};
-
 /* Possible return values of filter execution */
 enum filter_return {
   F_NOP = 0,
@@ -128,230 +27,15 @@ enum filter_return {
   F_QUITBIRD,
 };
 
-/* Filter l-value type */
-enum f_lval_type {
-  F_LVAL_VARIABLE,
-  F_LVAL_PREFERENCE,
-  F_LVAL_SA,
-  F_LVAL_EA,
-};
-
-/* Filter l-value */
-struct f_lval {
-  enum f_lval_type type;
-  union {
-    const struct symbol *sym;
-    struct f_dynamic_attr da;
-    struct f_static_attr sa;
-  };
-};
-
-/* Filter instruction declarations */
-#define FI__LIST \
-  F(FI_NOP) \
-  F(FI_ADD, ARG, ARG) \
-  F(FI_SUBTRACT, ARG, ARG) \
-  F(FI_MULTIPLY, ARG, ARG) \
-  F(FI_DIVIDE, ARG, ARG) \
-  F(FI_AND, ARG, LINE) \
-  F(FI_OR, ARG, LINE) \
-  F(FI_PAIR_CONSTRUCT, ARG, ARG) \
-  F(FI_EC_CONSTRUCT, ARG, ARG, ECS) \
-  F(FI_LC_CONSTRUCT, ARG, ARG, ARG) \
-  F(FI_PATHMASK_CONSTRUCT, ARG, COUNT) \
-  F(FI_NEQ, ARG, ARG) \
-  F(FI_EQ, ARG, ARG) \
-  F(FI_LT, ARG, ARG) \
-  F(FI_LTE, ARG, ARG) \
-  F(FI_NOT, ARG) \
-  F(FI_MATCH, ARG, ARG) \
-  F(FI_NOT_MATCH, ARG, ARG) \
-  F(FI_DEFINED, ARG) \
-  F(FI_TYPE, ARG) \
-  F(FI_IS_V4, ARG) \
-  F(FI_SET, ARG, SYMBOL) \
-  F(FI_CONSTANT, VALI) \
-  F(FI_VARIABLE, SYMBOL) \
-  F(FI_CONSTANT_INDIRECT, VALP) \
-  F(FI_PRINT, ARG) \
-  F(FI_CONDITION, ARG, LINE, LINE) \
-  F(FI_PRINT_AND_DIE, ARG, FRET) \
-  F(FI_RTA_GET, SA) \
-  F(FI_RTA_SET, ARG, SA) \
-  F(FI_EA_GET, EA) \
-  F(FI_EA_SET, ARG, EA) \
-  F(FI_EA_UNSET, EA) \
-  F(FI_PREF_GET) \
-  F(FI_PREF_SET, ARG) \
-  F(FI_LENGTH, ARG) \
-  F(FI_ROA_MAXLEN, ARG) \
-  F(FI_ROA_ASN, ARG) \
-  F(FI_SADR_SRC, ARG) \
-  F(FI_IP, ARG) \
-  F(FI_ROUTE_DISTINGUISHER, ARG) \
-  F(FI_AS_PATH_FIRST, ARG) \
-  F(FI_AS_PATH_LAST, ARG) \
-  F(FI_AS_PATH_LAST_NAG, ARG) \
-  F(FI_RETURN, ARG) \
-  F(FI_CALL, SYMBOL, LINE) \
-  F(FI_DROP_RESULT, ARG) \
-  F(FI_SWITCH, ARG, TREE) \
-  F(FI_IP_MASK, ARG, ARG) \
-  F(FI_PATH_PREPEND, ARG, ARG) \
-  F(FI_CLIST_ADD, ARG, ARG) \
-  F(FI_CLIST_DEL, ARG, ARG) \
-  F(FI_CLIST_FILTER, ARG, ARG) \
-  F(FI_ROA_CHECK_IMPLICIT, RTC) \
-  F(FI_ROA_CHECK_EXPLICIT, ARG, ARG, RTC) \
-  F(FI_FORMAT, ARG) \
-  F(FI_ASSERT, ARG, STRING)
-
-/* The enum itself */
-enum f_instruction_code {
-#define F(c, ...) c,
-FI__LIST
-#undef F
-  FI__MAX,
-} PACKED;
-
-/* Convert the instruction back to the enum name */
-const char *f_instruction_name(enum f_instruction_code fi);
-
-struct f_inst;
-void f_inst_next(struct f_inst *first, const struct f_inst *append);
-struct f_inst *f_clear_local_vars(struct f_inst *decls);
-
-#define FIA(x) , FIA_##x
-#define FIA_ARG        const struct f_inst *
-#define FIA_LINE const struct f_inst *
-#define FIA_COUNT uint
-#define FIA_SYMBOL const struct symbol *
-#define FIA_VALI struct f_val
-#define FIA_VALP const struct f_val *
-#define FIA_FRET enum filter_return
-#define FIA_ECS enum ec_subtype
-#define FIA_SA struct f_static_attr
-#define FIA_EA struct f_dynamic_attr
-#define FIA_RTC const struct rtable_config *
-#define FIA_TREE const struct f_tree *
-#define FIA_STRING const char *
-#define F(c, ...) \
-  struct f_inst *f_new_inst_##c(enum f_instruction_code MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))()(MACRO_FOREACH(FIA, __VA_ARGS__)));
-FI__LIST
-#undef F
-#undef FIA_ARG
-#undef FIA_LINE
-#undef FIA_LINEP
-#undef FIA_COUNT
-#undef FIA_SYMBOL
-#undef FIA_VALI
-#undef FIA_VALP
-#undef FIA_FRET
-#undef FIA_ECS
-#undef FIA_SA
-#undef FIA_EA
-#undef FIA_RTC
-#undef FIA_STRING
-#undef FIA
-
-#define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
-
-/* Flags for instructions */
-enum f_instruction_flags {
-  FIF_PRINTED = 1,             /* FI_PRINT_AND_DIE: message put in buffer */
-};
-
-/* Filter structures for execution */
-struct f_line;
-
-/* The single instruction item */
-struct f_line_item {
-  enum f_instruction_code fi_code;     /* What to do */
-  enum f_instruction_flags flags;      /* Flags, instruction-specific */
-  uint lineno;                         /* Where */
-  union {
-    struct {
-      const struct f_val *vp;
-      const struct symbol *sym;
-    };
-    struct f_val val;
-    const struct f_line *lines[2];
-    enum filter_return fret;
-    struct f_static_attr sa;
-    struct f_dynamic_attr da;
-    enum ec_subtype ecs;
-    const char *s;
-    const struct f_tree *tree;
-    const struct rtable_config *rtc;
-    uint count;
-  };                                   /* Additional instruction data */
-};
-
-/* Line of instructions to be unconditionally executed one after another */
-struct f_line {
-  uint len;                            /* Line length */
-  struct f_line_item items[0];         /* The items themselves */
-};
+struct f_val;
 
 /* The filter encapsulating structure to be pointed-to from outside */
+struct f_line;
 struct filter {
   char *name;
   struct f_line *root;
 };
 
-/* Convert the f_inst infix tree to the f_line structures */
-struct f_line *f_postfixify_concat(const struct f_inst * const inst[], uint count);
-static inline struct f_line *f_postfixify(const struct f_inst *root)
-{ return f_postfixify_concat(&root, 1); }
-
-#define F_VAL_STACK_MAX        4096
-
-/* Value stack for execution */
-struct f_val_stack {
-  uint cnt;                            /* Current stack size; 0 for empty */
-  struct f_val val[F_VAL_STACK_MAX];   /* The stack itself */
-};
-
-#define F_EXEC_STACK_MAX 4096
-
-/* Exception bits */
-enum f_exception {
-  FE_RETURN = 0x1,
-};
-
-/* Instruction stack for execution */
-struct f_exec_stack {
-  struct {
-    const struct f_line *line;         /* The line that is being executed */
-    uint pos;                          /* Instruction index in the line */
-    uint ventry;                       /* Value stack depth on entry */
-    enum f_exception emask;            /* Exception mask */
-  } item[F_EXEC_STACK_MAX];
-  uint cnt;                            /* Current stack size; 0 for empty */
-};
-
-struct filter *f_new_where(const struct f_inst *);
-static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
-{ return (struct f_dynamic_attr) { .type = type, .bit = bit, .f_type = f_type, .ea_code = code }; }   /* f_type currently unused; will be handy for static type checking */
-static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
-{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
-struct f_tree *f_new_tree(void);
-struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
-struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
-
-
-struct f_tree *build_tree(struct f_tree *);
-const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
-int same_tree(const struct f_tree *t1, const struct f_tree *t2);
-void tree_format(const struct f_tree *t, buffer *buf);
-
-struct f_trie *f_new_trie(linpool *lp, uint node_size);
-void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
-int trie_match_net(const struct f_trie *t, const net_addr *n);
-int trie_same(const struct f_trie *t1, const struct f_trie *t2);
-void trie_format(const struct f_trie *t, buffer *buf);
-
-struct ea_list;
 struct rte;
 
 enum filter_return f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);
@@ -361,43 +45,16 @@ uint f_eval_int(const struct f_line *expr);
 
 char *filter_name(struct filter *filter);
 int filter_same(struct filter *new, struct filter *old);
-
 int f_same(const struct f_line *f1, const struct f_line *f2);
 
 int val_compare(const struct f_val *v1, const struct f_val *v2);
 
 void val_format(const struct f_val *v, buffer *buf);
 
-extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist;
-
 #define FILTER_ACCEPT NULL
 #define FILTER_REJECT ((void *) 1)
 #define FILTER_UNDEF  ((void *) 2)     /* Used in BGP */
 
-
-struct f_tree {
-  struct f_tree *left, *right;
-  struct f_val from, to;
-  void *data;
-};
-
-struct f_trie_node
-{
-  ip_addr addr, mask, accept;
-  uint plen;
-  struct f_trie_node *c[2];
-};
-
-struct f_trie
-{
-  linpool *lp;
-  int zero;
-  uint node_size;
-  struct f_trie_node root[0];          /* Root trie node follows */
-};
-
-#define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
-
 #define FF_SILENT 2                    /* Silent filter execution */
 
 /* Custom route attributes */
@@ -409,15 +66,4 @@ struct custom_attribute {
 
 struct custom_attribute *ca_lookup(pool *p, const char *name, int ea_type);
 
-/* Bird Tests */
-struct f_bt_test_suite {
-  node n;                      /* Node in config->tests */
-  struct f_line *fn;           /* Root of function */
-  const char *fn_name;         /* Name of test */
-  const char *dsc;             /* Description */
-};
-
-/* Hook for call bt_assert() function in configuration */
-extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
-
 #endif
index af6b590fdc624294c88063f9d4e43e31849041ca..54ea3e9be0aee40cb1c32805d765a721fde106cc 100644 (file)
@@ -17,6 +17,8 @@
 #include "test/bt-utils.h"
 
 #include "filter/filter.h"
+#include "filter/f-util.h"
+#include "filter/f-inst.h"
 #include "conf/conf.h"
 
 #define BT_CONFIG_FILE "filter/test.conf"
index d499c94b033f12090ab40ed5c0891673676e5f34..38295e5cd0e2b0d03c7f06fb634e160a697c47c0 100644 (file)
@@ -69,7 +69,7 @@ m4_divert(0)
 #include "nest/bird.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
-#include "filter/f-inst-struct.h"
+#include "filter/f-inst.h"
 
 m4_undivert(1)
 
index 80e1d3958b2a69f8e8d2a6fe8b762c36554334b9..879b08599627bfb5d605bb488b9057145071f3b1 100644 (file)
@@ -10,6 +10,7 @@
 #include "nest/bird.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
+#include "filter/f-util.h"
 
 /**
  * find_tree
index f3e8ce4981908936fa2fa6ccd9a054f211ad0cc1..9e0de50f49909040523e384b27efde89f6ab32e3 100644 (file)
@@ -10,6 +10,7 @@
 #include "test/bt-utils.h"
 
 #include "filter/filter.h"
+#include "filter/f-util.h"
 #include "conf/conf.h"
 
 #define MAX_TREE_HEIGHT 13
index a279e38c324b5fdf4b27a7b08292c61535d8c403..dccf9130ea60e11cdcd02944c56cd627473da0f0 100644 (file)
@@ -73,6 +73,7 @@
 #include "lib/string.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
+#include "filter/f-util.h"
 
 
 /*
index 7529a5c5aa01d18df9e37c71a9a62b21e9374c83..b6959c4a9e1482cee90f6ab4685c8adfda56b3ac 100644 (file)
@@ -10,6 +10,7 @@
 #include "test/bt-utils.h"
 
 #include "filter/filter.h"
+#include "filter/f-util.h"
 #include "conf/conf.h"
 
 #define TESTS_NUM              10
index d3a1d6364aa3f5cbdb77cac896255cee49a3e3da..ac13d0fad8838e720cd16d11cd0d6f9ee101260f 100644 (file)
@@ -13,7 +13,7 @@
 #include "lib/resource.h"
 #include "lib/unaligned.h"
 #include "lib/string.h"
-#include "filter/filter.h"
+#include "filter/f-util.h"
 
 // static inline void put_as(byte *data, u32 as) { put_u32(data, as); }
 // static inline u32 get_as(byte *data) { return get_u32(data); }
index 6daafcb3d51c3c8c826d837222ffcbd1d3adeb97..40d397cc4358b08eef081156fc1d140b8f885809 100644 (file)
@@ -15,6 +15,7 @@
 #include "lib/string.h"
 #include "lib/resource.h"
 #include "filter/filter.h"
+#include "filter/f-util.h"
 
 extern int shutting_down;
 extern int configuring;
index 85afe83866d072976eebc98abe5e6bb0844b6542..d72a8695d4d57890244797d7706667e87a261143 100644 (file)
@@ -39,6 +39,7 @@
 #include "lib/string.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
+#include "filter/f-util.h"
 #include "lib/hash.h"
 #include "lib/string.h"
 #include "lib/alloca.h"
index 921115b1a9cff12568e6ebe7a3bd0f7548fb27ff..f9002d58ee9a8dd1f82d91b7568432b92d1b95d3 100644 (file)
@@ -36,6 +36,7 @@
 #include "nest/locks.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
+#include "filter/f-util.h"
 
 #include "unix.h"
 #include "krt.h"