]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/data.h
Filter: Implement multiple dispatch for methods
[thirdparty/bird.git] / filter / data.h
1 /*
2 * BIRD Internet Routing Daemon -- Dynamic data structures
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
10 #ifndef _BIRD_FILTER_DATA_H_
11 #define _BIRD_FILTER_DATA_H_
12
13 #include "nest/bird.h"
14
15 /* Type numbers must be in 0..0xff range */
16 #define T_MASK 0xff
17
18 /* Internal types */
19 enum f_type {
20 /* Nothing. Simply nothing. */
21 T_VOID = 0,
22
23 /* User visible types, which fit in int */
24 T_INT = 0x10,
25 T_BOOL = 0x11,
26 T_PAIR = 0x12, /* Notice that pair is stored as integer: first << 16 | second */
27 T_QUAD = 0x13,
28
29 /* Put enumerational types in 0x30..0x3f range */
30 T_ENUM_LO = 0x30,
31 T_ENUM_HI = 0x3f,
32
33 T_ENUM_RTS = 0x30,
34 T_ENUM_BGP_ORIGIN = 0x31,
35 T_ENUM_SCOPE = 0x32,
36 T_ENUM_RTC = 0x33,
37 T_ENUM_RTD = 0x34,
38 T_ENUM_ROA = 0x35,
39 T_ENUM_NETTYPE = 0x36,
40 T_ENUM_RA_PREFERENCE = 0x37,
41 T_ENUM_AF = 0x38,
42
43 /* new enums go here */
44 T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */
45
46 #define T_ENUM T_ENUM_LO ... T_ENUM_HI
47
48 /* Bigger ones */
49 T_IP = 0x20,
50 T_NET = 0x21,
51 T_STRING = 0x22,
52 T_PATH_MASK = 0x23, /* mask for BGP path */
53 T_PATH = 0x24, /* BGP path */
54 T_CLIST = 0x25, /* Community list */
55 T_EC = 0x26, /* Extended community value, u64 */
56 T_ECLIST = 0x27, /* Extended community list */
57 T_LC = 0x28, /* Large community value, lcomm */
58 T_LCLIST = 0x29, /* Large community list */
59 T_RD = 0x2a, /* Route distinguisher for VPN addresses */
60 T_PATH_MASK_ITEM = 0x2b, /* Path mask item for path mask constructors */
61 T_BYTESTRING = 0x2c,
62
63 T_SET = 0x80,
64 T_PREFIX_SET = 0x81,
65 } PACKED;
66
67 struct f_method {
68 struct symbol *sym;
69 struct f_inst *(*new_inst)(struct f_inst *obj, struct f_inst *args);
70 const struct f_method *next;
71 uint arg_num;
72 enum f_type args_type[];
73 };
74
75 /* Filter value; size of this affects filter memory consumption */
76 struct f_val {
77 enum f_type type; /* T_* */
78 union {
79 uint i;
80 u64 ec;
81 lcomm lc;
82 ip_addr ip;
83 const net_addr *net;
84 const char *s;
85 const struct bytestring *bs;
86 const struct f_tree *t;
87 const struct f_trie *ti;
88 const struct adata *ad;
89 const struct f_path_mask *path_mask;
90 struct f_path_mask_item pmi;
91 } val;
92 };
93
94 /* Dynamic attribute definition (eattrs) */
95 struct f_dynamic_attr {
96 u8 type; /* EA type (EAF_*) */
97 u8 bit; /* For bitfield accessors */
98 enum f_type f_type; /* Filter type */
99 uint ea_code; /* EA code */
100 };
101
102 enum f_sa_code {
103 SA_FROM = 1,
104 SA_GW,
105 SA_NET,
106 SA_PROTO,
107 SA_SOURCE,
108 SA_SCOPE,
109 SA_DEST,
110 SA_IFNAME,
111 SA_IFINDEX,
112 SA_WEIGHT,
113 SA_PREF,
114 SA_GW_MPLS,
115 SA_ONLINK,
116 } PACKED;
117
118 /* Static attribute definition (members of struct rta) */
119 struct f_static_attr {
120 enum f_type f_type; /* Filter type */
121 enum f_sa_code sa_code; /* Static attribute id */
122 int readonly:1; /* Don't allow writing */
123 };
124
125 /* Filter l-value type */
126 enum f_lval_type {
127 F_LVAL_VARIABLE,
128 F_LVAL_PREFERENCE,
129 F_LVAL_SA,
130 F_LVAL_EA,
131 };
132
133 /* Filter l-value */
134 struct f_lval {
135 enum f_lval_type type;
136 union {
137 struct symbol *sym;
138 struct f_dynamic_attr da;
139 struct f_static_attr sa;
140 };
141 };
142
143 /* IP prefix range structure */
144 struct f_prefix {
145 net_addr net; /* The matching prefix must match this net */
146 u8 lo, hi; /* And its length must fit between lo and hi */
147 };
148
149 struct f_tree {
150 struct f_tree *left, *right;
151 struct f_val from, to;
152 void *data;
153 };
154
155 #ifdef ENABLE_COMPACT_TRIES
156 /* Compact 4-way tries */
157 #define TRIE_STEP 2
158 #define TRIE_STACK_LENGTH 65
159 #else
160 /* Faster 16-way tries */
161 #define TRIE_STEP 4
162 #define TRIE_STACK_LENGTH 33
163 #endif
164
165 struct f_trie_node4
166 {
167 ip4_addr addr, mask, accept;
168 u16 plen;
169 u16 local;
170 struct f_trie_node4 *c[1 << TRIE_STEP];
171 };
172
173 struct f_trie_node6
174 {
175 ip6_addr addr, mask, accept;
176 u16 plen;
177 u16 local;
178 struct f_trie_node6 *c[1 << TRIE_STEP];
179 };
180
181 struct f_trie_node
182 {
183 union {
184 struct f_trie_node4 v4;
185 struct f_trie_node6 v6;
186 };
187 };
188
189 struct f_trie
190 {
191 linpool *lp;
192 u8 zero;
193 s8 ipv4; /* -1 for undefined / empty */
194 u16 data_size; /* Additional data for each trie node */
195 u32 prefix_count; /* Works only for restricted tries (pxlen == l == h) */
196 struct f_trie_node root; /* Root trie node */
197 };
198
199 struct f_trie_walk_state
200 {
201 u8 ipv4;
202 u8 accept_length; /* Current inter-node prefix position */
203 u8 start_pos; /* Initial prefix position in stack[0] */
204 u8 local_pos; /* Current intra-node prefix position */
205 u8 stack_pos; /* Current node in stack below */
206 const struct f_trie_node *stack[TRIE_STACK_LENGTH];
207 };
208
209 struct f_tree *f_new_tree(void);
210 struct f_tree *build_tree(struct f_tree *);
211 const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
212 const struct f_tree *find_tree_linear(const struct f_tree *t, const struct f_val *val);
213 int same_tree(const struct f_tree *t0, const struct f_tree *t2);
214 int tree_node_count(const struct f_tree *t);
215 void tree_format(const struct f_tree *t, buffer *buf);
216 void tree_walk(const struct f_tree *t, void (*hook)(const struct f_tree *, void *), void *data);
217
218 struct f_trie *f_new_trie(linpool *lp, uint data_size);
219 void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
220 int trie_match_net(const struct f_trie *t, const net_addr *n);
221 int trie_match_longest_ip4(const struct f_trie *t, const net_addr_ip4 *net, net_addr_ip4 *dst, ip4_addr *found0);
222 int trie_match_longest_ip6(const struct f_trie *t, const net_addr_ip6 *net, net_addr_ip6 *dst, ip6_addr *found0);
223 void trie_walk_init(struct f_trie_walk_state *s, const struct f_trie *t, const net_addr *from);
224 int trie_walk_next(struct f_trie_walk_state *s, net_addr *net);
225 int trie_same(const struct f_trie *t1, const struct f_trie *t2);
226 void trie_format(const struct f_trie *t, buffer *buf);
227
228 static inline int
229 trie_match_next_longest_ip4(net_addr_ip4 *n, ip4_addr *found)
230 {
231 while (n->pxlen)
232 {
233 n->pxlen--;
234 ip4_clrbit(&n->prefix, n->pxlen);
235
236 if (ip4_getbit(*found, n->pxlen))
237 return 1;
238 }
239
240 return 0;
241 }
242
243 static inline int
244 trie_match_next_longest_ip6(net_addr_ip6 *n, ip6_addr *found)
245 {
246 while (n->pxlen)
247 {
248 n->pxlen--;
249 ip6_clrbit(&n->prefix, n->pxlen);
250
251 if (ip6_getbit(*found, n->pxlen))
252 return 1;
253 }
254
255 return 0;
256 }
257
258
259 #define TRIE_WALK_TO_ROOT_IP4(trie, net, dst) ({ \
260 net_addr_ip4 dst; \
261 ip4_addr _found; \
262 for (int _n = trie_match_longest_ip4(trie, net, &dst, &_found); \
263 _n; \
264 _n = trie_match_next_longest_ip4(&dst, &_found))
265
266 #define TRIE_WALK_TO_ROOT_IP6(trie, net, dst) ({ \
267 net_addr_ip6 dst; \
268 ip6_addr _found; \
269 for (int _n = trie_match_longest_ip6(trie, net, &dst, &_found); \
270 _n; \
271 _n = trie_match_next_longest_ip6(&dst, &_found))
272
273 #define TRIE_WALK_TO_ROOT_END })
274
275
276 #define TRIE_WALK(trie, net, from) ({ \
277 net_addr net; \
278 struct f_trie_walk_state tws_; \
279 trie_walk_init(&tws_, trie, from); \
280 while (trie_walk_next(&tws_, &net))
281
282 #define TRIE_WALK_END })
283
284
285 #define F_CMP_ERROR 999
286
287 const char *f_type_name(enum f_type t);
288 enum f_type f_type_element_type(enum f_type t);
289 struct sym_scope *f_type_method_scope(enum f_type t);
290
291 int val_same(const struct f_val *v1, const struct f_val *v2);
292 int val_compare(const struct f_val *v1, const struct f_val *v2);
293 void val_format(const struct f_val *v, buffer *buf);
294 char *val_format_str(struct linpool *lp, const struct f_val *v);
295 const char *val_dump(const struct f_val *v);
296
297 static inline int val_is_ip4(const struct f_val *v)
298 { return (v->type == T_IP) && ipa_is_ip4(v->val.ip); }
299 int val_in_range(const struct f_val *v1, const struct f_val *v2);
300
301 int clist_set_type(const struct f_tree *set, struct f_val *v);
302 static inline int eclist_set_type(const struct f_tree *set)
303 { return !set || set->from.type == T_EC; }
304 static inline int lclist_set_type(const struct f_tree *set)
305 { return !set || set->from.type == T_LC; }
306 static inline int path_set_type(const struct f_tree *set)
307 { return !set || set->from.type == T_INT; }
308
309 int clist_match_set(const struct adata *clist, const struct f_tree *set);
310 int eclist_match_set(const struct adata *list, const struct f_tree *set);
311 int lclist_match_set(const struct adata *list, const struct f_tree *set);
312
313 const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
314 const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
315 const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
316
317
318 /* Special undef value for paths and clists */
319 static inline int
320 undef_value(struct f_val v)
321 {
322 return ((v.type == T_PATH) || (v.type == T_CLIST) ||
323 (v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
324 (v.val.ad == &null_adata);
325 }
326
327 extern const struct f_val f_const_empty_prefix_set;
328
329 enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres);
330
331 #endif