]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/f-inst.h
Filter: any lvalue can get its methods called
[thirdparty/bird.git] / filter / f-inst.h
1 /*
2 * BIRD Internet Routing Daemon -- Filter instructions
3 *
4 * (c) 1999 Pavel Machek <pavel@ucw.cz>
5 * (c) 2018--2019 Maria Matejka <mq@jmq.cz>
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 *
9 * Filter interpreter data structures and internal API.
10 * See filter/f-inst.c for documentation.
11 */
12
13 #ifndef _BIRD_F_INST_H_
14 #define _BIRD_F_INST_H_
15
16 #include "nest/bird.h"
17 #include "conf/conf.h"
18 #include "filter/filter.h"
19 #include "filter/data.h"
20 #include "lib/buffer.h"
21 #include "lib/flowspec.h"
22 #include "lib/string.h"
23
24 /* Flags for instructions */
25 enum f_instruction_flags {
26 FIF_RECURSIVE = 1, /* FI_CALL: function is directly recursive */
27 } PACKED;
28
29 /* Include generated filter instruction declarations */
30 #include "filter/inst-gen.h"
31
32 #define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
33
34 /* Convert the instruction back to the enum name */
35 const char *f_instruction_name_(enum f_instruction_code fi);
36 static inline const char *f_instruction_name(enum f_instruction_code fi)
37 { return f_instruction_name_(fi) + 3; }
38
39 struct f_arg {
40 struct symbol *arg;
41 struct f_arg *next;
42 };
43
44 /* Filter structures for execution */
45 /* Line of instructions to be unconditionally executed one after another */
46 struct f_line {
47 uint len; /* Line length */
48 u8 args; /* Function: Args required */
49 u8 vars;
50 u8 results; /* Results left on stack: cmd -> 0, term -> 1 */
51 struct f_arg *arg_list;
52 struct f_line_item items[0]; /* The items themselves */
53 };
54
55 /* Convert the f_inst infix tree to the f_line structures */
56 struct f_line *f_linearize_concat(const struct f_inst * const inst[], uint count, uint results);
57 static inline struct f_line *f_linearize(const struct f_inst *root, uint results)
58 { return f_linearize_concat(&root, 1, results); }
59
60 void f_dump_line(const struct f_line *, uint indent);
61
62
63 /* Recursive iteration over filter instructions */
64
65 struct filter_iterator {
66 BUFFER_(const struct f_line *) lines;
67 };
68
69 void f_add_lines(const struct f_line_item *what, struct filter_iterator *fit);
70
71 #define FILTER_ITERATE_INIT(fit, filter, pool) \
72 ({ \
73 BUFFER_INIT((fit)->lines, (pool), 32); \
74 BUFFER_PUSH((fit)->lines) = (filter)->root; \
75 })
76
77 #define FILTER_ITERATE(fit, fi) ({ \
78 const struct f_line *fl_; \
79 while (!BUFFER_EMPTY((fit)->lines)) \
80 { \
81 BUFFER_POP((fit)->lines); \
82 fl_ = (fit)->lines.data[(fit)->lines.used]; \
83 for (uint i_ = 0; i_ < fl_->len; i_++) \
84 { \
85 const struct f_line_item *fi = &fl_->items[i_]; \
86 f_add_lines(fi, (fit));
87
88 #define FILTER_ITERATE_END } } })
89
90 #define FILTER_ITERATE_CLEANUP(fit) \
91 ({ \
92 mb_free((fit)->lines.data); \
93 memset((fit), 0, sizeof(struct filter_iterator)); \
94 })
95
96
97 struct filter *f_new_where(struct f_inst *);
98 static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
99 { return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
100 static inline struct f_dynamic_attr f_new_dynamic_attr_bit(u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
101 { return (struct f_dynamic_attr) { .type = EAF_TYPE_BITFIELD, .bit = bit, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
102 static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
103 { return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
104
105 /* Hook for call bt_assert() function in configuration */
106 extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
107
108 /* Bird Tests */
109 struct f_bt_test_suite {
110 node n; /* Node in config->tests */
111 const struct f_line *fn; /* Root of function */
112 const struct f_line *cmp; /* Compare to this function */
113 const char *fn_name; /* Name of test */
114 const char *dsc; /* Description */
115 int result; /* Desired result */
116 };
117
118 #endif