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