]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/data.h
Filter: Add 'weight' route attribute
[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
62 T_SET = 0x80,
63 T_PREFIX_SET = 0x81,
64 } PACKED;
65
66 /* Filter value; size of this affects filter memory consumption */
67 struct 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;
75 const char *s;
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
84 /* Dynamic attribute definition (eattrs) */
85 struct 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
92 enum 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,
102 SA_WEIGHT,
103 } PACKED;
104
105 /* Static attribute definition (members of struct rta) */
106 struct f_static_attr {
107 enum f_type f_type; /* Filter type */
108 enum f_sa_code sa_code; /* Static attribute id */
109 int readonly:1; /* Don't allow writing */
110 };
111
112 /* Filter l-value type */
113 enum f_lval_type {
114 F_LVAL_VARIABLE,
115 F_LVAL_PREFERENCE,
116 F_LVAL_SA,
117 F_LVAL_EA,
118 };
119
120 /* Filter l-value */
121 struct f_lval {
122 enum f_lval_type type;
123 union {
124 struct symbol *sym;
125 struct f_dynamic_attr da;
126 struct f_static_attr sa;
127 };
128 };
129
130 /* IP prefix range structure */
131 struct f_prefix {
132 net_addr net; /* The matching prefix must match this net */
133 u8 lo, hi; /* And its length must fit between lo and hi */
134 };
135
136 struct f_tree {
137 struct f_tree *left, *right;
138 struct f_val from, to;
139 void *data;
140 };
141
142 struct f_trie_node4
143 {
144 ip4_addr addr, mask, accept;
145 uint plen;
146 struct f_trie_node4 *c[2];
147 };
148
149 struct f_trie_node6
150 {
151 ip6_addr addr, mask, accept;
152 uint plen;
153 struct f_trie_node6 *c[2];
154 };
155
156 struct f_trie_node
157 {
158 union {
159 struct f_trie_node4 v4;
160 struct f_trie_node6 v6;
161 };
162 };
163
164 struct f_trie
165 {
166 linpool *lp;
167 u8 zero;
168 s8 ipv4; /* -1 for undefined / empty */
169 u16 data_size; /* Additional data for each trie node */
170 struct f_trie_node root; /* Root trie node */
171 };
172
173 struct f_tree *f_new_tree(void);
174 struct f_tree *build_tree(struct f_tree *);
175 const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
176 int same_tree(const struct f_tree *t0, const struct f_tree *t2);
177 void tree_format(const struct f_tree *t, buffer *buf);
178
179 struct f_trie *f_new_trie(linpool *lp, uint data_size);
180 void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
181 int trie_match_net(const struct f_trie *t, const net_addr *n);
182 int trie_same(const struct f_trie *t1, const struct f_trie *t2);
183 void trie_format(const struct f_trie *t, buffer *buf);
184
185 #define F_CMP_ERROR 999
186
187 const char *f_type_name(enum f_type t);
188
189 int val_same(const struct f_val *v1, const struct f_val *v2);
190 int val_compare(const struct f_val *v1, const struct f_val *v2);
191 void val_format(const struct f_val *v, buffer *buf);
192 char *val_format_str(struct linpool *lp, const struct f_val *v);
193 const char *val_dump(const struct f_val *v);
194
195 static inline int val_is_ip4(const struct f_val *v)
196 { return (v->type == T_IP) && ipa_is_ip4(v->val.ip); }
197 int val_in_range(const struct f_val *v1, const struct f_val *v2);
198
199 int clist_set_type(const struct f_tree *set, struct f_val *v);
200 static inline int eclist_set_type(const struct f_tree *set)
201 { return set->from.type == T_EC; }
202 static inline int lclist_set_type(const struct f_tree *set)
203 { return set->from.type == T_LC; }
204
205 const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
206 const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
207 const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
208
209
210 /* Special undef value for paths and clists */
211 static inline int
212 undef_value(struct f_val v)
213 {
214 return ((v.type == T_PATH) || (v.type == T_CLIST) ||
215 (v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
216 (v.val.ad == &null_adata);
217 }
218
219 extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist;
220
221 enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres);
222
223 #endif