]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/data.h
Alloc: Disable transparent huge pages
[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"
8bdb05ed
MM
14
15/* Type numbers must be in 0..0xff range */
16#define T_MASK 0xff
17
18/* Internal types */
19enum f_type {
96d757c1
JMM
20/* Nothing. Simply nothing. */
21 T_VOID = 0,
8bdb05ed
MM
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,
0edf0c8c 41 T_ENUM_AF = 0x38,
8bdb05ed
MM
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
62 T_SET = 0x80,
63 T_PREFIX_SET = 0x81,
64} PACKED;
65
66/* Filter value; size of this affects filter memory consumption */
67struct f_val {
68 enum f_type type; /* T_* */
69 union {
70 uint i;
71 u64 ec;
72 lcomm lc;
73 ip_addr ip;
74 const net_addr *net;
fd9f0c06 75 const char *s;
8bdb05ed
MM
76 const struct f_tree *t;
77 const struct f_trie *ti;
78 const struct adata *ad;
79 const struct f_path_mask *path_mask;
80 struct f_path_mask_item pmi;
81 } val;
82};
83
8bdb05ed
MM
84/* Dynamic attribute definition (eattrs) */
85struct f_dynamic_attr {
86 u8 type; /* EA type (EAF_*) */
87 u8 bit; /* For bitfield accessors */
88 enum f_type f_type; /* Filter type */
89 uint ea_code; /* EA code */
90};
91
92enum f_sa_code {
93 SA_FROM = 1,
94 SA_GW,
95 SA_NET,
96 SA_PROTO,
97 SA_SOURCE,
98 SA_SCOPE,
99 SA_DEST,
100 SA_IFNAME,
101 SA_IFINDEX,
8cc5bb09 102 SA_WEIGHT,
eb937358 103 SA_PREF,
e5468d16 104 SA_GW_MPLS,
8bdb05ed
MM
105} PACKED;
106
107/* Static attribute definition (members of struct rta) */
108struct f_static_attr {
109 enum f_type f_type; /* Filter type */
110 enum f_sa_code sa_code; /* Static attribute id */
111 int readonly:1; /* Don't allow writing */
112};
113
4f082dfa
MM
114/* Filter l-value type */
115enum f_lval_type {
116 F_LVAL_VARIABLE,
117 F_LVAL_PREFERENCE,
118 F_LVAL_SA,
119 F_LVAL_EA,
120};
121
122/* Filter l-value */
123struct f_lval {
124 enum f_lval_type type;
125 union {
b40c0f02 126 struct symbol *sym;
4f082dfa
MM
127 struct f_dynamic_attr da;
128 struct f_static_attr sa;
129 };
130};
131
132/* IP prefix range structure */
133struct f_prefix {
134 net_addr net; /* The matching prefix must match this net */
135 u8 lo, hi; /* And its length must fit between lo and hi */
136};
137
8bdb05ed
MM
138struct f_tree {
139 struct f_tree *left, *right;
140 struct f_val from, to;
141 void *data;
142};
143
d3f50ede
OZ
144#ifdef ENABLE_COMPACT_TRIES
145/* Compact 4-way tries */
146#define TRIE_STEP 2
147#define TRIE_STACK_LENGTH 65
148#else
149/* Faster 16-way tries */
062e69bf
OZ
150#define TRIE_STEP 4
151#define TRIE_STACK_LENGTH 33
d3f50ede 152#endif
13225f1d 153
27550028
OZ
154struct f_trie_node4
155{
156 ip4_addr addr, mask, accept;
13225f1d
OZ
157 u16 plen;
158 u16 local;
159 struct f_trie_node4 *c[1 << TRIE_STEP];
27550028
OZ
160};
161
162struct f_trie_node6
8bdb05ed 163{
27550028 164 ip6_addr addr, mask, accept;
13225f1d
OZ
165 u16 plen;
166 u16 local;
167 struct f_trie_node6 *c[1 << TRIE_STEP];
27550028
OZ
168};
169
170struct f_trie_node
171{
172 union {
173 struct f_trie_node4 v4;
174 struct f_trie_node6 v6;
175 };
8bdb05ed
MM
176};
177
178struct f_trie
179{
180 linpool *lp;
27550028
OZ
181 u8 zero;
182 s8 ipv4; /* -1 for undefined / empty */
183 u16 data_size; /* Additional data for each trie node */
ba5aec94 184 u32 prefix_count; /* Works only for restricted tries (pxlen == l == h) */
27550028 185 struct f_trie_node root; /* Root trie node */
8bdb05ed
MM
186};
187
062e69bf
OZ
188struct f_trie_walk_state
189{
190 u8 ipv4;
191 u8 accept_length; /* Current inter-node prefix position */
192 u8 start_pos; /* Initial prefix position in stack[0] */
193 u8 local_pos; /* Current intra-node prefix position */
194 u8 stack_pos; /* Current node in stack below */
195 const struct f_trie_node *stack[TRIE_STACK_LENGTH];
196};
197
8bdb05ed
MM
198struct f_tree *f_new_tree(void);
199struct f_tree *build_tree(struct f_tree *);
200const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
e20bef69 201const struct f_tree *find_tree_linear(const struct f_tree *t, const struct f_val *val);
8bdb05ed 202int same_tree(const struct f_tree *t0, const struct f_tree *t2);
92a85655 203int tree_node_count(const struct f_tree *t);
8bdb05ed 204void tree_format(const struct f_tree *t, buffer *buf);
d06a875b 205void tree_walk(const struct f_tree *t, void (*hook)(const struct f_tree *, void *), void *data);
8bdb05ed 206
27550028 207struct f_trie *f_new_trie(linpool *lp, uint data_size);
8bdb05ed
MM
208void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
209int trie_match_net(const struct f_trie *t, const net_addr *n);
14fc24f3
OZ
210int trie_match_longest_ip4(const struct f_trie *t, const net_addr_ip4 *net, net_addr_ip4 *dst, ip4_addr *found0);
211int trie_match_longest_ip6(const struct f_trie *t, const net_addr_ip6 *net, net_addr_ip6 *dst, ip6_addr *found0);
062e69bf
OZ
212void trie_walk_init(struct f_trie_walk_state *s, const struct f_trie *t, const net_addr *from);
213int trie_walk_next(struct f_trie_walk_state *s, net_addr *net);
8bdb05ed
MM
214int trie_same(const struct f_trie *t1, const struct f_trie *t2);
215void trie_format(const struct f_trie *t, buffer *buf);
216
14fc24f3
OZ
217static inline int
218trie_match_next_longest_ip4(net_addr_ip4 *n, ip4_addr *found)
219{
220 while (n->pxlen)
221 {
222 n->pxlen--;
223 ip4_clrbit(&n->prefix, n->pxlen);
224
225 if (ip4_getbit(*found, n->pxlen))
226 return 1;
227 }
228
229 return 0;
230}
231
232static inline int
233trie_match_next_longest_ip6(net_addr_ip6 *n, ip6_addr *found)
234{
235 while (n->pxlen)
236 {
237 n->pxlen--;
238 ip6_clrbit(&n->prefix, n->pxlen);
239
240 if (ip6_getbit(*found, n->pxlen))
241 return 1;
242 }
243
244 return 0;
245}
246
247
248#define TRIE_WALK_TO_ROOT_IP4(trie, net, dst) ({ \
249 net_addr_ip4 dst; \
250 ip4_addr _found; \
251 for (int _n = trie_match_longest_ip4(trie, net, &dst, &_found); \
252 _n; \
253 _n = trie_match_next_longest_ip4(&dst, &_found))
254
255#define TRIE_WALK_TO_ROOT_IP6(trie, net, dst) ({ \
256 net_addr_ip6 dst; \
257 ip6_addr _found; \
258 for (int _n = trie_match_longest_ip6(trie, net, &dst, &_found); \
259 _n; \
260 _n = trie_match_next_longest_ip6(&dst, &_found))
261
262#define TRIE_WALK_TO_ROOT_END })
263
264
062e69bf
OZ
265#define TRIE_WALK(trie, net, from) ({ \
266 net_addr net; \
267 struct f_trie_walk_state tws_; \
268 trie_walk_init(&tws_, trie, from); \
269 while (trie_walk_next(&tws_, &net))
270
271#define TRIE_WALK_END })
272
14fc24f3 273
52893045
MM
274#define F_CMP_ERROR 999
275
87512e97
OZ
276const char *f_type_name(enum f_type t);
277
0e1fd7ea
AZ
278enum f_type f_type_element_type(enum f_type t);
279
52893045
MM
280int val_same(const struct f_val *v1, const struct f_val *v2);
281int val_compare(const struct f_val *v1, const struct f_val *v2);
282void val_format(const struct f_val *v, buffer *buf);
b40c0f02 283char *val_format_str(struct linpool *lp, const struct f_val *v);
52893045
MM
284const char *val_dump(const struct f_val *v);
285
286static inline int val_is_ip4(const struct f_val *v)
287{ return (v->type == T_IP) && ipa_is_ip4(v->val.ip); }
288int val_in_range(const struct f_val *v1, const struct f_val *v2);
289
290int clist_set_type(const struct f_tree *set, struct f_val *v);
291static inline int eclist_set_type(const struct f_tree *set)
b2d6d294 292{ return !set || set->from.type == T_EC; }
52893045 293static inline int lclist_set_type(const struct f_tree *set)
b2d6d294 294{ return !set || set->from.type == T_LC; }
8f3c6151 295static inline int path_set_type(const struct f_tree *set)
b2d6d294 296{ return !set || set->from.type == T_INT; }
52893045 297
92a85655
OZ
298int clist_match_set(const struct adata *clist, const struct f_tree *set);
299int eclist_match_set(const struct adata *list, const struct f_tree *set);
300int lclist_match_set(const struct adata *list, const struct f_tree *set);
301
52893045
MM
302const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
303const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
304const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
305
306
307/* Special undef value for paths and clists */
308static inline int
309undef_value(struct f_val v)
310{
311 return ((v.type == T_PATH) || (v.type == T_CLIST) ||
312 (v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
313 (v.val.ad == &null_adata);
314}
315
b2d6d294 316extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist, f_const_empty_prefix_set;
8bdb05ed 317
52893045
MM
318enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres);
319
8bdb05ed 320#endif