]>
Commit | Line | Data |
---|---|---|
2326b001 MM |
1 | /* |
2 | * BIRD -- Route Attribute Cache | |
3 | * | |
ee76a92a | 4 | * (c) 1998--2000 Martin Mares <mj@ucw.cz> |
2326b001 MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
3ce8c610 MM |
9 | /** |
10 | * DOC: Route attribute cache | |
11 | * | |
12 | * Each route entry carries a set of route attributes. Several of them | |
13 | * vary from route to route, but most attributes are usually common | |
14 | * for a large number of routes. To conserve memory, we've decided to | |
15 | * store only the varying ones directly in the &rte and hold the rest | |
16 | * in a special structure called &rta which is shared among all the | |
17 | * &rte's with these attributes. | |
18 | * | |
19 | * Each &rta contains all the static attributes of the route (i.e., | |
20 | * those which are always present) as structure members and a list of | |
21 | * dynamic attributes represented by a linked list of &ea_list | |
22 | * structures, each of them consisting of an array of &eattr's containing | |
23 | * the individual attributes. An attribute can be specified more than once | |
2e9b2421 | 24 | * in the &ea_list chain and in such case the first occurrence overrides |
3ce8c610 MM |
25 | * the others. This semantics is used especially when someone (for example |
26 | * a filter) wishes to alter values of several dynamic attributes, but | |
27 | * it wants to preserve the original attribute lists maintained by | |
28 | * another module. | |
29 | * | |
30 | * Each &eattr contains an attribute identifier (split to protocol ID and | |
31 | * per-protocol attribute ID), protocol dependent flags, a type code (consisting | |
32 | * of several bit fields describing attribute characteristics) and either an | |
33 | * embedded 32-bit value or a pointer to a &adata structure holding attribute | |
34 | * contents. | |
35 | * | |
2e9b2421 | 36 | * There exist two variants of &rta's -- cached and un-cached ones. Un-cached |
3ce8c610 MM |
37 | * &rta's can have arbitrarily complex structure of &ea_list's and they |
38 | * can be modified by any module in the route processing chain. Cached | |
39 | * &rta's have their attribute lists normalized (that means at most one | |
40 | * &ea_list is present and its values are sorted in order to speed up | |
41 | * searching), they are stored in a hash table to make fast lookup possible | |
42 | * and they are provided with a use count to allow sharing. | |
43 | * | |
44 | * Routing tables always contain only cached &rta's. | |
45 | */ | |
46 | ||
2326b001 MM |
47 | #include "nest/bird.h" |
48 | #include "nest/route.h" | |
49 | #include "nest/protocol.h" | |
66e53309 | 50 | #include "nest/iface.h" |
730f2e2c | 51 | #include "nest/cli.h" |
c6add07f | 52 | #include "nest/attrs.h" |
10af3676 | 53 | #include "lib/alloca.h" |
e7d2ac44 | 54 | #include "lib/hash.h" |
74c838a8 | 55 | #include "lib/idm.h" |
2326b001 | 56 | #include "lib/resource.h" |
221135d6 | 57 | #include "lib/string.h" |
2326b001 | 58 | |
9a74622c JMM |
59 | #include <stddef.h> |
60 | ||
4c553c5a MM |
61 | const adata null_adata; /* adata of length 0 */ |
62 | ||
69b2f63d | 63 | const char * const rta_src_names[RTS_MAX] = { |
69b2f63d OZ |
64 | [RTS_STATIC] = "static", |
65 | [RTS_INHERIT] = "inherit", | |
66 | [RTS_DEVICE] = "device", | |
67 | [RTS_STATIC_DEVICE] = "static-device", | |
68 | [RTS_REDIRECT] = "redirect", | |
69 | [RTS_RIP] = "RIP", | |
70 | [RTS_OSPF] = "OSPF", | |
71 | [RTS_OSPF_IA] = "OSPF-IA", | |
72 | [RTS_OSPF_EXT1] = "OSPF-E1", | |
73 | [RTS_OSPF_EXT2] = "OSPF-E2", | |
74 | [RTS_BGP] = "BGP", | |
75 | [RTS_PIPE] = "pipe", | |
76 | [RTS_BABEL] = "Babel", | |
77 | [RTS_RPKI] = "RPKI", | |
977b82fb | 78 | [RTS_PERF] = "Perf", |
bcff3ae7 | 79 | [RTS_L3VPN] = "L3VPN", |
977b82fb | 80 | [RTS_AGGREGATED] = "aggregated", |
69b2f63d OZ |
81 | }; |
82 | ||
665be7f6 OZ |
83 | const char * rta_dest_names[RTD_MAX] = { |
84 | [RTD_NONE] = "", | |
85 | [RTD_UNICAST] = "unicast", | |
86 | [RTD_BLACKHOLE] = "blackhole", | |
87 | [RTD_UNREACHABLE] = "unreachable", | |
88 | [RTD_PROHIBIT] = "prohibited", | |
89 | }; | |
90 | ||
acb60628 OZ |
91 | pool *rta_pool; |
92 | ||
ec5e5d23 JMM |
93 | static slab *rta_slab_[4]; |
94 | static slab *nexthop_slab_[4]; | |
094d2bdb OZ |
95 | static slab *rte_src_slab; |
96 | ||
74c838a8 | 97 | static struct idm src_ids; |
e7d2ac44 | 98 | #define SRC_ID_INIT_SIZE 4 |
094d2bdb OZ |
99 | |
100 | /* rte source hash */ | |
e7d2ac44 OZ |
101 | |
102 | #define RSH_KEY(n) n->proto, n->private_id | |
103 | #define RSH_NEXT(n) n->next | |
104 | #define RSH_EQ(p1,n1,p2,n2) p1 == p2 && n1 == n2 | |
21213be5 | 105 | #define RSH_FN(p,n) p->hash_key ^ u64_hash(n) |
e7d2ac44 OZ |
106 | |
107 | #define RSH_REHASH rte_src_rehash | |
108 | #define RSH_PARAMS /2, *2, 1, 1, 8, 20 | |
109 | #define RSH_INIT_ORDER 6 | |
110 | ||
111 | static HASH(struct rte_src) src_hash; | |
2326b001 | 112 | |
094d2bdb OZ |
113 | static void |
114 | rte_src_init(void) | |
115 | { | |
116 | rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src)); | |
117 | ||
74c838a8 | 118 | idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE); |
094d2bdb | 119 | |
e7d2ac44 | 120 | HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER); |
094d2bdb OZ |
121 | } |
122 | ||
094d2bdb | 123 | |
e7d2ac44 | 124 | HASH_DEFINE_REHASH_FN(RSH, struct rte_src) |
094d2bdb OZ |
125 | |
126 | struct rte_src * | |
127 | rt_find_source(struct proto *p, u32 id) | |
128 | { | |
e7d2ac44 | 129 | return HASH_FIND(src_hash, RSH, p, id); |
094d2bdb OZ |
130 | } |
131 | ||
132 | struct rte_src * | |
133 | rt_get_source(struct proto *p, u32 id) | |
134 | { | |
e7d2ac44 | 135 | struct rte_src *src = rt_find_source(p, id); |
094d2bdb | 136 | |
e7d2ac44 OZ |
137 | if (src) |
138 | return src; | |
094d2bdb | 139 | |
c9ae8165 | 140 | src = sl_allocz(rte_src_slab); |
094d2bdb OZ |
141 | src->proto = p; |
142 | src->private_id = id; | |
74c838a8 | 143 | src->global_id = idm_alloc(&src_ids); |
094d2bdb | 144 | src->uc = 0; |
d217ba51 | 145 | |
e7d2ac44 | 146 | HASH_INSERT2(src_hash, RSH, rta_pool, src); |
094d2bdb OZ |
147 | |
148 | return src; | |
149 | } | |
150 | ||
094d2bdb OZ |
151 | void |
152 | rt_prune_sources(void) | |
153 | { | |
e7d2ac44 OZ |
154 | HASH_WALK_FILTER(src_hash, next, src, sp) |
155 | { | |
156 | if (src->uc == 0) | |
094d2bdb | 157 | { |
e7d2ac44 | 158 | HASH_DO_REMOVE(src_hash, RSH, sp); |
74c838a8 | 159 | idm_free(&src_ids, src->global_id); |
ebd807c0 | 160 | sl_free(src); |
094d2bdb | 161 | } |
e7d2ac44 OZ |
162 | } |
163 | HASH_WALK_FILTER_END; | |
094d2bdb | 164 | |
e7d2ac44 | 165 | HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool); |
094d2bdb OZ |
166 | } |
167 | ||
168 | ||
169 | /* | |
170 | * Multipath Next Hop | |
171 | */ | |
172 | ||
04632fd7 | 173 | static inline u32 |
4e276a89 | 174 | nexthop_hash(struct nexthop *x) |
7e95c05d | 175 | { |
04632fd7 | 176 | u32 h = 0; |
7e95c05d | 177 | for (; x; x = x->next) |
ec5e5d23 JMM |
178 | { |
179 | h ^= ipa_hash(x->gw) ^ (h << 5) ^ (h >> 9); | |
62e64905 OZ |
180 | |
181 | for (int i = 0; i < x->labels; i++) | |
ec5e5d23 JMM |
182 | h ^= x->label[i] ^ (h << 6) ^ (h >> 7); |
183 | } | |
7e95c05d OZ |
184 | |
185 | return h; | |
186 | } | |
187 | ||
f40e2bc2 OZ |
188 | static inline int |
189 | nexthop_equal_1(struct nexthop *x, struct nexthop *y) | |
190 | { | |
191 | if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || | |
192 | (x->flags != y->flags) || (x->weight != y->weight) || | |
193 | (x->labels != y->labels)) | |
194 | return 0; | |
195 | ||
196 | for (int i = 0; i < x->labels; i++) | |
197 | if (x->label[i] != y->label[i]) | |
198 | return 0; | |
199 | ||
200 | return 1; | |
201 | } | |
202 | ||
7e95c05d | 203 | int |
f40e2bc2 | 204 | nexthop_equal_(struct nexthop *x, struct nexthop *y) |
7e95c05d | 205 | { |
f40e2bc2 OZ |
206 | /* Like nexthop_same(), but ignores difference between local labels and labels from hostentry */ |
207 | ||
7e95c05d | 208 | for (; x && y; x = x->next, y = y->next) |
f40e2bc2 | 209 | if (!nexthop_equal_1(x, y)) |
7e95c05d | 210 | return 0; |
62e64905 | 211 | |
f40e2bc2 OZ |
212 | return x == y; |
213 | } | |
214 | ||
215 | int | |
216 | nexthop__same(struct nexthop *x, struct nexthop *y) | |
217 | { | |
218 | for (; x && y; x = x->next, y = y->next) | |
219 | if (!nexthop_equal_1(x, y) || | |
220 | (x->labels_orig != y->labels_orig)) | |
221 | return 0; | |
7e95c05d | 222 | |
62e64905 | 223 | return x == y; |
7e95c05d OZ |
224 | } |
225 | ||
d217ba51 | 226 | static int |
0fa8bf91 | 227 | nexthop_compare_node(const struct nexthop *x, const struct nexthop *y) |
d217ba51 OZ |
228 | { |
229 | int r; | |
230 | ||
231 | if (!x) | |
232 | return 1; | |
233 | ||
234 | if (!y) | |
235 | return -1; | |
236 | ||
a1f5e514 OZ |
237 | /* Should we also compare flags ? */ |
238 | ||
d217ba51 OZ |
239 | r = ((int) y->weight) - ((int) x->weight); |
240 | if (r) | |
241 | return r; | |
242 | ||
243 | r = ipa_compare(x->gw, y->gw); | |
244 | if (r) | |
245 | return r; | |
246 | ||
ec5e5d23 JMM |
247 | r = ((int) y->labels) - ((int) x->labels); |
248 | if (r) | |
249 | return r; | |
250 | ||
62e64905 | 251 | for (int i = 0; i < y->labels; i++) |
ec5e5d23 JMM |
252 | { |
253 | r = ((int) y->label[i]) - ((int) x->label[i]); | |
254 | if (r) | |
255 | return r; | |
256 | } | |
257 | ||
d217ba51 OZ |
258 | return ((int) x->iface->index) - ((int) y->iface->index); |
259 | } | |
260 | ||
4e276a89 JMM |
261 | static inline struct nexthop * |
262 | nexthop_copy_node(const struct nexthop *src, linpool *lp) | |
d217ba51 | 263 | { |
ec5e5d23 JMM |
264 | struct nexthop *n = lp_alloc(lp, nexthop_size(src)); |
265 | ||
266 | memcpy(n, src, nexthop_size(src)); | |
d217ba51 | 267 | n->next = NULL; |
ec5e5d23 | 268 | |
d217ba51 OZ |
269 | return n; |
270 | } | |
271 | ||
272 | /** | |
4e276a89 | 273 | * nexthop_merge - merge nexthop lists |
d217ba51 OZ |
274 | * @x: list 1 |
275 | * @y: list 2 | |
276 | * @rx: reusability of list @x | |
277 | * @ry: reusability of list @y | |
278 | * @max: max number of nexthops | |
279 | * @lp: linpool for allocating nexthops | |
280 | * | |
4e276a89 | 281 | * The nexthop_merge() function takes two nexthop lists @x and @y and merges them, |
d217ba51 OZ |
282 | * eliminating possible duplicates. The input lists must be sorted and the |
283 | * result is sorted too. The number of nexthops in result is limited by @max. | |
284 | * New nodes are allocated from linpool @lp. | |
285 | * | |
286 | * The arguments @rx and @ry specify whether corresponding input lists may be | |
287 | * consumed by the function (i.e. their nodes reused in the resulting list), in | |
288 | * that case the caller should not access these lists after that. To eliminate | |
289 | * issues with deallocation of these lists, the caller should use some form of | |
290 | * bulk deallocation (e.g. stack or linpool) to free these nodes when the | |
291 | * resulting list is no longer needed. When reusability is not set, the | |
292 | * corresponding lists are not modified nor linked from the resulting list. | |
293 | */ | |
4e276a89 JMM |
294 | struct nexthop * |
295 | nexthop_merge(struct nexthop *x, struct nexthop *y, int rx, int ry, int max, linpool *lp) | |
d217ba51 | 296 | { |
4e276a89 JMM |
297 | struct nexthop *root = NULL; |
298 | struct nexthop **n = &root; | |
d217ba51 OZ |
299 | |
300 | while ((x || y) && max--) | |
301 | { | |
4e276a89 | 302 | int cmp = nexthop_compare_node(x, y); |
0fa8bf91 | 303 | |
d217ba51 OZ |
304 | if (cmp < 0) |
305 | { | |
0fa8bf91 | 306 | ASSUME(x); |
4e276a89 | 307 | *n = rx ? x : nexthop_copy_node(x, lp); |
d217ba51 OZ |
308 | x = x->next; |
309 | } | |
310 | else if (cmp > 0) | |
311 | { | |
0fa8bf91 | 312 | ASSUME(y); |
4e276a89 | 313 | *n = ry ? y : nexthop_copy_node(y, lp); |
d217ba51 OZ |
314 | y = y->next; |
315 | } | |
316 | else | |
317 | { | |
0fa8bf91 | 318 | ASSUME(x && y); |
4e276a89 | 319 | *n = rx ? x : (ry ? y : nexthop_copy_node(x, lp)); |
d217ba51 OZ |
320 | x = x->next; |
321 | y = y->next; | |
322 | } | |
323 | n = &((*n)->next); | |
324 | } | |
325 | *n = NULL; | |
326 | ||
327 | return root; | |
328 | } | |
329 | ||
84cac51a | 330 | void |
62e64905 | 331 | nexthop_insert(struct nexthop **n, struct nexthop *x) |
84cac51a | 332 | { |
62e64905 | 333 | for (; *n; n = &((*n)->next)) |
4e276a89 | 334 | { |
62e64905 | 335 | int cmp = nexthop_compare_node(*n, x); |
84cac51a OZ |
336 | |
337 | if (cmp < 0) | |
338 | continue; | |
62e64905 OZ |
339 | else if (cmp > 0) |
340 | break; | |
341 | else | |
342 | return; | |
84cac51a OZ |
343 | } |
344 | ||
62e64905 OZ |
345 | x->next = *n; |
346 | *n = x; | |
84cac51a OZ |
347 | } |
348 | ||
59d3a361 OZ |
349 | struct nexthop * |
350 | nexthop_sort(struct nexthop *x) | |
351 | { | |
352 | struct nexthop *s = NULL; | |
353 | ||
354 | /* Simple insert-sort */ | |
355 | while (x) | |
356 | { | |
357 | struct nexthop *n = x; | |
358 | x = n->next; | |
359 | n->next = NULL; | |
360 | ||
361 | nexthop_insert(&s, n); | |
362 | } | |
363 | ||
364 | return s; | |
365 | } | |
366 | ||
84cac51a | 367 | int |
4e276a89 | 368 | nexthop_is_sorted(struct nexthop *x) |
84cac51a OZ |
369 | { |
370 | for (; x && x->next; x = x->next) | |
4e276a89 | 371 | if (nexthop_compare_node(x, x->next) >= 0) |
84cac51a OZ |
372 | return 0; |
373 | ||
374 | return 1; | |
375 | } | |
d217ba51 | 376 | |
ec5e5d23 JMM |
377 | static inline slab * |
378 | nexthop_slab(struct nexthop *nh) | |
379 | { | |
62e64905 | 380 | return nexthop_slab_[MIN(nh->labels, 3)]; |
ec5e5d23 JMM |
381 | } |
382 | ||
4e276a89 JMM |
383 | static struct nexthop * |
384 | nexthop_copy(struct nexthop *o) | |
7e95c05d | 385 | { |
4e276a89 JMM |
386 | struct nexthop *first = NULL; |
387 | struct nexthop **last = &first; | |
7e95c05d OZ |
388 | |
389 | for (; o; o = o->next) | |
390 | { | |
c9ae8165 | 391 | struct nexthop *n = sl_allocz(nexthop_slab(o)); |
7e95c05d OZ |
392 | n->gw = o->gw; |
393 | n->iface = o->iface; | |
394 | n->next = NULL; | |
33716595 | 395 | n->flags = o->flags; |
7e95c05d | 396 | n->weight = o->weight; |
bda58634 | 397 | n->labels_orig = o->labels_orig; |
f2010f9c JMM |
398 | n->labels = o->labels; |
399 | for (int i=0; i<o->labels; i++) | |
400 | n->label[i] = o->label[i]; | |
7e95c05d OZ |
401 | |
402 | *last = n; | |
403 | last = &(n->next); | |
404 | } | |
405 | ||
406 | return first; | |
407 | } | |
408 | ||
409 | static void | |
4e276a89 | 410 | nexthop_free(struct nexthop *o) |
7e95c05d | 411 | { |
4e276a89 | 412 | struct nexthop *n; |
7e95c05d OZ |
413 | |
414 | while (o) | |
415 | { | |
416 | n = o->next; | |
ebd807c0 | 417 | sl_free(o); |
7e95c05d OZ |
418 | o = n; |
419 | } | |
420 | } | |
421 | ||
422 | ||
b77ae37d MM |
423 | /* |
424 | * Extended Attributes | |
425 | */ | |
426 | ||
8d24b689 MM |
427 | static inline eattr * |
428 | ea__find(ea_list *e, unsigned id) | |
b77ae37d MM |
429 | { |
430 | eattr *a; | |
431 | int l, r, m; | |
432 | ||
433 | while (e) | |
434 | { | |
435 | if (e->flags & EALF_BISECT) | |
436 | { | |
437 | l = 0; | |
fee78355 | 438 | r = e->count - 1; |
b77ae37d MM |
439 | while (l <= r) |
440 | { | |
441 | m = (l+r) / 2; | |
442 | a = &e->attrs[m]; | |
443 | if (a->id == id) | |
444 | return a; | |
445 | else if (a->id < id) | |
446 | l = m+1; | |
447 | else | |
448 | r = m-1; | |
449 | } | |
450 | } | |
451 | else | |
452 | for(m=0; m<e->count; m++) | |
453 | if (e->attrs[m].id == id) | |
454 | return &e->attrs[m]; | |
455 | e = e->next; | |
456 | } | |
457 | return NULL; | |
458 | } | |
459 | ||
3ce8c610 MM |
460 | /** |
461 | * ea_find - find an extended attribute | |
462 | * @e: attribute list to search in | |
463 | * @id: attribute ID to search for | |
464 | * | |
465 | * Given an extended attribute list, ea_find() searches for a first | |
2e9b2421 | 466 | * occurrence of an attribute with specified ID, returning either a pointer |
3ce8c610 MM |
467 | * to its &eattr structure or %NULL if no such attribute exists. |
468 | */ | |
8d24b689 MM |
469 | eattr * |
470 | ea_find(ea_list *e, unsigned id) | |
471 | { | |
472 | eattr *a = ea__find(e, id & EA_CODE_MASK); | |
473 | ||
0f685152 | 474 | if (a && a->undef && !(id & EA_ALLOW_UNDEF)) |
8d24b689 MM |
475 | return NULL; |
476 | return a; | |
477 | } | |
478 | ||
9fdf9d29 OZ |
479 | /** |
480 | * ea_walk - walk through extended attributes | |
481 | * @s: walk state structure | |
482 | * @id: start of attribute ID interval | |
483 | * @max: length of attribute ID interval | |
484 | * | |
485 | * Given an extended attribute list, ea_walk() walks through the list looking | |
486 | * for first occurrences of attributes with ID in specified interval from @id to | |
487 | * (@id + @max - 1), returning pointers to found &eattr structures, storing its | |
488 | * walk state in @s for subsequent calls. | |
8e433d6a | 489 | * |
9fdf9d29 OZ |
490 | * The function ea_walk() is supposed to be called in a loop, with initially |
491 | * zeroed walk state structure @s with filled the initial extended attribute | |
492 | * list, returning one found attribute in each call or %NULL when no other | |
493 | * attribute exists. The extended attribute list or the arguments should not be | |
494 | * modified between calls. The maximum value of @max is 128. | |
495 | */ | |
496 | eattr * | |
497 | ea_walk(struct ea_walk_state *s, uint id, uint max) | |
498 | { | |
499 | ea_list *e = s->eattrs; | |
500 | eattr *a = s->ea; | |
501 | eattr *a_max; | |
502 | ||
503 | max = id + max; | |
504 | ||
505 | if (a) | |
506 | goto step; | |
507 | ||
508 | for (; e; e = e->next) | |
509 | { | |
510 | if (e->flags & EALF_BISECT) | |
511 | { | |
512 | int l, r, m; | |
513 | ||
514 | l = 0; | |
515 | r = e->count - 1; | |
516 | while (l < r) | |
517 | { | |
518 | m = (l+r) / 2; | |
519 | if (e->attrs[m].id < id) | |
520 | l = m + 1; | |
521 | else | |
522 | r = m; | |
523 | } | |
524 | a = e->attrs + l; | |
525 | } | |
526 | else | |
527 | a = e->attrs; | |
528 | ||
529 | step: | |
530 | a_max = e->attrs + e->count; | |
531 | for (; a < a_max; a++) | |
532 | if ((a->id >= id) && (a->id < max)) | |
533 | { | |
534 | int n = a->id - id; | |
535 | ||
536 | if (BIT32_TEST(s->visited, n)) | |
537 | continue; | |
538 | ||
539 | BIT32_SET(s->visited, n); | |
540 | ||
0f685152 | 541 | if (a->undef) |
9fdf9d29 OZ |
542 | continue; |
543 | ||
544 | s->eattrs = e; | |
545 | s->ea = a; | |
546 | return a; | |
547 | } | |
548 | else if (e->flags & EALF_BISECT) | |
549 | break; | |
550 | } | |
551 | ||
552 | return NULL; | |
553 | } | |
554 | ||
3ce8c610 MM |
555 | /** |
556 | * ea_get_int - fetch an integer attribute | |
557 | * @e: attribute list | |
558 | * @id: attribute ID | |
559 | * @def: default value | |
560 | * | |
561 | * This function is a shortcut for retrieving a value of an integer attribute | |
562 | * by calling ea_find() to find the attribute, extracting its value or returning | |
563 | * a provided default if no such attribute is present. | |
564 | */ | |
6e13df70 MM |
565 | uintptr_t |
566 | ea_get_int(ea_list *e, unsigned id, uintptr_t def) | |
c0100454 PM |
567 | { |
568 | eattr *a = ea_find(e, id); | |
569 | if (!a) | |
570 | return def; | |
571 | return a->u.data; | |
572 | } | |
573 | ||
b77ae37d MM |
574 | static inline void |
575 | ea_do_sort(ea_list *e) | |
576 | { | |
577 | unsigned n = e->count; | |
578 | eattr *a = e->attrs; | |
579 | eattr *b = alloca(n * sizeof(eattr)); | |
580 | unsigned s, ss; | |
581 | ||
582 | /* We need to use a stable sorting algorithm, hence mergesort */ | |
583 | do | |
584 | { | |
585 | s = ss = 0; | |
586 | while (s < n) | |
587 | { | |
588 | eattr *p, *q, *lo, *hi; | |
589 | p = b; | |
590 | ss = s; | |
591 | *p++ = a[s++]; | |
592 | while (s < n && p[-1].id <= a[s].id) | |
593 | *p++ = a[s++]; | |
594 | if (s < n) | |
595 | { | |
596 | q = p; | |
597 | *p++ = a[s++]; | |
598 | while (s < n && p[-1].id <= a[s].id) | |
599 | *p++ = a[s++]; | |
600 | lo = b; | |
601 | hi = q; | |
602 | s = ss; | |
603 | while (lo < q && hi < p) | |
604 | if (lo->id <= hi->id) | |
605 | a[s++] = *lo++; | |
606 | else | |
607 | a[s++] = *hi++; | |
608 | while (lo < q) | |
609 | a[s++] = *lo++; | |
610 | while (hi < p) | |
611 | a[s++] = *hi++; | |
612 | } | |
613 | } | |
614 | } | |
615 | while (ss); | |
616 | } | |
617 | ||
13c0be19 | 618 | /** |
875cc073 OZ |
619 | * In place discard duplicates and undefs in sorted ea_list. We use stable sort |
620 | * for this reason. | |
13c0be19 | 621 | **/ |
b77ae37d MM |
622 | static inline void |
623 | ea_do_prune(ea_list *e) | |
624 | { | |
51a183af | 625 | eattr *s, *d, *l, *s0; |
8d24b689 MM |
626 | int i = 0; |
627 | ||
13c0be19 JMM |
628 | s = d = e->attrs; /* Beginning of the list. @s is source, @d is destination. */ |
629 | l = e->attrs + e->count; /* End of the list */ | |
630 | ||
631 | /* Walk from begin to end. */ | |
8d24b689 MM |
632 | while (s < l) |
633 | { | |
51a183af | 634 | s0 = s++; |
13c0be19 | 635 | /* Find a consecutive block of the same attribute */ |
51a183af MM |
636 | while (s < l && s->id == s[-1].id) |
637 | s++; | |
13c0be19 JMM |
638 | |
639 | /* Now s0 is the most recent version, s[-1] the oldest one */ | |
640 | /* Drop undefs */ | |
0f685152 | 641 | if (s0->undef) |
13c0be19 JMM |
642 | continue; |
643 | ||
13c0be19 JMM |
644 | /* Copy the newest version to destination */ |
645 | *d = *s0; | |
646 | ||
647 | /* Preserve info whether it originated locally */ | |
63cf5d5d MM |
648 | d->originated = s[-1].originated; |
649 | ||
650 | /* Not fresh any more, we prefer surstroemming */ | |
651 | d->fresh = 0; | |
13c0be19 JMM |
652 | |
653 | /* Next destination */ | |
654 | d++; | |
655 | i++; | |
8d24b689 | 656 | } |
13c0be19 | 657 | |
8d24b689 | 658 | e->count = i; |
b77ae37d MM |
659 | } |
660 | ||
3ce8c610 MM |
661 | /** |
662 | * ea_sort - sort an attribute list | |
663 | * @e: list to be sorted | |
664 | * | |
665 | * This function takes a &ea_list chain and sorts the attributes | |
666 | * within each of its entries. | |
667 | * | |
668 | * If an attribute occurs multiple times in a single &ea_list, | |
2e9b2421 | 669 | * ea_sort() leaves only the first (the only significant) occurrence. |
3ce8c610 | 670 | */ |
b77ae37d MM |
671 | void |
672 | ea_sort(ea_list *e) | |
673 | { | |
674 | while (e) | |
675 | { | |
676 | if (!(e->flags & EALF_SORTED)) | |
677 | { | |
678 | ea_do_sort(e); | |
679 | ea_do_prune(e); | |
680 | e->flags |= EALF_SORTED; | |
681 | } | |
b77ae37d | 682 | if (e->count > 5) |
b77ae37d MM |
683 | e->flags |= EALF_BISECT; |
684 | e = e->next; | |
685 | } | |
686 | } | |
687 | ||
3ce8c610 MM |
688 | /** |
689 | * ea_scan - estimate attribute list size | |
690 | * @e: attribute list | |
691 | * | |
692 | * This function calculates an upper bound of the size of | |
693 | * a given &ea_list after merging with ea_merge(). | |
694 | */ | |
b77ae37d MM |
695 | unsigned |
696 | ea_scan(ea_list *e) | |
697 | { | |
698 | unsigned cnt = 0; | |
699 | ||
700 | while (e) | |
701 | { | |
702 | cnt += e->count; | |
703 | e = e->next; | |
704 | } | |
705 | return sizeof(ea_list) + sizeof(eattr)*cnt; | |
706 | } | |
707 | ||
3ce8c610 MM |
708 | /** |
709 | * ea_merge - merge segments of an attribute list | |
710 | * @e: attribute list | |
711 | * @t: buffer to store the result to | |
712 | * | |
713 | * This function takes a possibly multi-segment attribute list | |
714 | * and merges all of its segments to one. | |
715 | * | |
716 | * The primary use of this function is for &ea_list normalization: | |
717 | * first call ea_scan() to determine how much memory will the result | |
718 | * take, then allocate a buffer (usually using alloca()), merge the | |
719 | * segments with ea_merge() and finally sort and prune the result | |
720 | * by calling ea_sort(). | |
721 | */ | |
b77ae37d MM |
722 | void |
723 | ea_merge(ea_list *e, ea_list *t) | |
724 | { | |
725 | eattr *d = t->attrs; | |
726 | ||
727 | t->flags = 0; | |
728 | t->count = 0; | |
729 | t->next = NULL; | |
730 | while (e) | |
731 | { | |
732 | memcpy(d, e->attrs, sizeof(eattr)*e->count); | |
733 | t->count += e->count; | |
734 | d += e->count; | |
735 | e = e->next; | |
736 | } | |
737 | } | |
738 | ||
3ce8c610 MM |
739 | /** |
740 | * ea_same - compare two &ea_list's | |
741 | * @x: attribute list | |
742 | * @y: attribute list | |
743 | * | |
744 | * ea_same() compares two normalized attribute lists @x and @y and returns | |
745 | * 1 if they contain the same attributes, 0 otherwise. | |
746 | */ | |
6f57dcc0 | 747 | int |
2326b001 MM |
748 | ea_same(ea_list *x, ea_list *y) |
749 | { | |
750 | int c; | |
751 | ||
b77ae37d MM |
752 | if (!x || !y) |
753 | return x == y; | |
754 | ASSERT(!x->next && !y->next); | |
755 | if (x->count != y->count) | |
756 | return 0; | |
757 | for(c=0; c<x->count; c++) | |
2326b001 | 758 | { |
b77ae37d MM |
759 | eattr *a = &x->attrs[c]; |
760 | eattr *b = &y->attrs[c]; | |
761 | ||
762 | if (a->id != b->id || | |
763 | a->flags != b->flags || | |
764 | a->type != b->type || | |
63cf5d5d MM |
765 | a->originated != b->originated || |
766 | a->fresh != b->fresh || | |
0f685152 | 767 | a->undef != b->undef || |
28a10f84 | 768 | ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr))) |
2326b001 | 769 | return 0; |
b77ae37d MM |
770 | } |
771 | return 1; | |
772 | } | |
773 | ||
774 | static inline ea_list * | |
775 | ea_list_copy(ea_list *o) | |
776 | { | |
777 | ea_list *n; | |
7c8b7649 | 778 | unsigned i, adpos, elen; |
b77ae37d MM |
779 | |
780 | if (!o) | |
781 | return NULL; | |
782 | ASSERT(!o->next); | |
7c8b7649 MM |
783 | elen = adpos = sizeof(ea_list) + sizeof(eattr) * o->count; |
784 | ||
785 | for(i=0; i<o->count; i++) | |
786 | { | |
787 | eattr *a = &o->attrs[i]; | |
788 | if (!(a->type & EAF_EMBEDDED)) | |
7d2c7d59 | 789 | elen += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, EA_DATA_ALIGN); |
7c8b7649 MM |
790 | } |
791 | ||
792 | n = mb_alloc(rta_pool, elen); | |
793 | memcpy(n, o, adpos); | |
b77ae37d MM |
794 | n->flags |= EALF_CACHED; |
795 | for(i=0; i<o->count; i++) | |
796 | { | |
797 | eattr *a = &n->attrs[i]; | |
08732b71 | 798 | if (!(a->type & EAF_EMBEDDED)) |
b77ae37d | 799 | { |
7d2c7d59 OZ |
800 | uint size_u = sizeof(struct adata) + a->u.ptr->length; |
801 | uint size = BIRD_ALIGN(size_u, EA_DATA_ALIGN); | |
7c8b7649 MM |
802 | ASSERT_DIE(adpos + size <= elen); |
803 | ||
804 | struct adata *d = ((void *) n) + adpos; | |
7d2c7d59 | 805 | memcpy(d, a->u.ptr, size_u); |
b77ae37d | 806 | a->u.ptr = d; |
7c8b7649 MM |
807 | |
808 | adpos += size; | |
b77ae37d MM |
809 | } |
810 | } | |
7c8b7649 | 811 | ASSERT_DIE(adpos == elen); |
b77ae37d MM |
812 | return n; |
813 | } | |
814 | ||
5d86aefb MM |
815 | static inline void |
816 | ea_free(ea_list *o) | |
817 | { | |
818 | if (o) | |
819 | { | |
820 | ASSERT(!o->next); | |
821 | mb_free(o); | |
822 | } | |
823 | } | |
824 | ||
ba5e5940 | 825 | static int |
258be565 | 826 | get_generic_attr(const eattr *a, byte **buf, int buflen UNUSED) |
ba5e5940 | 827 | { |
333ddd4f OZ |
828 | switch (a->id) |
829 | { | |
830 | case EA_GEN_IGP_METRIC: | |
831 | *buf += bsprintf(*buf, "igp_metric"); | |
832 | return GA_NAME; | |
833 | ||
834 | case EA_MPLS_LABEL: | |
835 | *buf += bsprintf(*buf, "mpls_label"); | |
836 | return GA_NAME; | |
d217ba51 | 837 | |
333ddd4f OZ |
838 | case EA_MPLS_POLICY: |
839 | *buf += bsprintf(*buf, "mpls_policy"); | |
840 | return GA_NAME; | |
841 | ||
842 | case EA_MPLS_CLASS: | |
843 | *buf += bsprintf(*buf, "mpls_class"); | |
844 | return GA_NAME; | |
845 | ||
846 | default: | |
847 | return GA_UNKNOWN; | |
848 | } | |
ba5e5940 OZ |
849 | } |
850 | ||
315f23a0 | 851 | void |
258be565 | 852 | ea_format_bitfield(const struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max) |
315f23a0 OZ |
853 | { |
854 | byte *bound = buf + bufsize - 32; | |
855 | u32 data = a->u.data; | |
856 | int i; | |
857 | ||
858 | for (i = min; i < max; i++) | |
859 | if ((data & (1u << i)) && names[i]) | |
860 | { | |
861 | if (buf > bound) | |
862 | { | |
863 | strcpy(buf, " ..."); | |
864 | return; | |
865 | } | |
866 | ||
867 | buf += bsprintf(buf, " %s", names[i]); | |
868 | data &= ~(1u << i); | |
869 | } | |
870 | ||
871 | if (data) | |
872 | bsprintf(buf, " %08x", data); | |
873 | ||
874 | return; | |
875 | } | |
876 | ||
fdf16eb6 | 877 | static inline void |
4c553c5a | 878 | opaque_format(const struct adata *ad, byte *buf, uint size) |
fdf16eb6 OZ |
879 | { |
880 | byte *bound = buf + size - 10; | |
3e236955 | 881 | uint i; |
fdf16eb6 OZ |
882 | |
883 | for(i = 0; i < ad->length; i++) | |
884 | { | |
885 | if (buf > bound) | |
886 | { | |
887 | strcpy(buf, " ..."); | |
888 | return; | |
889 | } | |
890 | if (i) | |
891 | *buf++ = ' '; | |
892 | ||
893 | buf += bsprintf(buf, "%02x", ad->data[i]); | |
894 | } | |
895 | ||
896 | *buf = 0; | |
897 | return; | |
898 | } | |
899 | ||
900 | static inline void | |
4c553c5a | 901 | ea_show_int_set(struct cli *c, const struct adata *ad, int way, byte *pos, byte *buf, byte *end) |
fdf16eb6 OZ |
902 | { |
903 | int i = int_set_format(ad, way, 0, pos, end - pos); | |
a52d52fa | 904 | cli_printf(c, -1012, "\t%s", buf); |
fdf16eb6 OZ |
905 | while (i) |
906 | { | |
907 | i = int_set_format(ad, way, i, buf, end - buf - 1); | |
a52d52fa | 908 | cli_printf(c, -1012, "\t\t%s", buf); |
fdf16eb6 OZ |
909 | } |
910 | } | |
911 | ||
42a0c054 | 912 | static inline void |
4c553c5a | 913 | ea_show_ec_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end) |
42a0c054 OZ |
914 | { |
915 | int i = ec_set_format(ad, 0, pos, end - pos); | |
a52d52fa | 916 | cli_printf(c, -1012, "\t%s", buf); |
42a0c054 OZ |
917 | while (i) |
918 | { | |
919 | i = ec_set_format(ad, i, buf, end - buf - 1); | |
a52d52fa | 920 | cli_printf(c, -1012, "\t\t%s", buf); |
42a0c054 OZ |
921 | } |
922 | } | |
923 | ||
66dbdbd9 | 924 | static inline void |
4c553c5a | 925 | ea_show_lc_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end) |
66dbdbd9 OZ |
926 | { |
927 | int i = lc_set_format(ad, 0, pos, end - pos); | |
928 | cli_printf(c, -1012, "\t%s", buf); | |
929 | while (i) | |
930 | { | |
931 | i = lc_set_format(ad, i, buf, end - buf - 1); | |
932 | cli_printf(c, -1012, "\t\t%s", buf); | |
933 | } | |
934 | } | |
935 | ||
3ce8c610 | 936 | /** |
fdf16eb6 OZ |
937 | * ea_show - print an &eattr to CLI |
938 | * @c: destination CLI | |
939 | * @e: attribute to be printed | |
3ce8c610 | 940 | * |
fdf16eb6 OZ |
941 | * This function takes an extended attribute represented by its &eattr |
942 | * structure and prints it to the CLI according to the type information. | |
3ce8c610 MM |
943 | * |
944 | * If the protocol defining the attribute provides its own | |
945 | * get_attr() hook, it's consulted first. | |
946 | */ | |
3991d84e | 947 | void |
258be565 | 948 | ea_show(struct cli *c, const eattr *e) |
3991d84e MM |
949 | { |
950 | struct protocol *p; | |
951 | int status = GA_UNKNOWN; | |
4c553c5a | 952 | const struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr; |
fdf16eb6 OZ |
953 | byte buf[CLI_MSG_SIZE]; |
954 | byte *pos = buf, *end = buf + sizeof(buf); | |
3991d84e | 955 | |
265419a3 MM |
956 | if (EA_IS_CUSTOM(e->id)) |
957 | { | |
958 | const char *name = ea_custom_name(e->id); | |
959 | if (name) | |
960 | { | |
961 | pos += bsprintf(pos, "%s", name); | |
962 | status = GA_NAME; | |
963 | } | |
964 | else | |
965 | pos += bsprintf(pos, "%02x.", EA_PROTO(e->id)); | |
966 | } | |
967 | else if (p = class_to_protocol[EA_PROTO(e->id)]) | |
3991d84e | 968 | { |
fdf16eb6 | 969 | pos += bsprintf(pos, "%s.", p->name); |
3991d84e | 970 | if (p->get_attr) |
fdf16eb6 OZ |
971 | status = p->get_attr(e, pos, end - pos); |
972 | pos += strlen(pos); | |
3991d84e MM |
973 | } |
974 | else if (EA_PROTO(e->id)) | |
fdf16eb6 | 975 | pos += bsprintf(pos, "%02x.", EA_PROTO(e->id)); |
d217ba51 | 976 | else |
fdf16eb6 | 977 | status = get_generic_attr(e, &pos, end - pos); |
ba5e5940 | 978 | |
3991d84e | 979 | if (status < GA_NAME) |
fdf16eb6 | 980 | pos += bsprintf(pos, "%02x", EA_ID(e->id)); |
3991d84e MM |
981 | if (status < GA_FULL) |
982 | { | |
fdf16eb6 OZ |
983 | *pos++ = ':'; |
984 | *pos++ = ' '; | |
0f685152 MM |
985 | |
986 | if (e->undef) | |
987 | bsprintf(pos, "undefined"); | |
988 | else | |
3991d84e MM |
989 | switch (e->type & EAF_TYPE_MASK) |
990 | { | |
991 | case EAF_TYPE_INT: | |
fdf16eb6 | 992 | bsprintf(pos, "%u", e->u.data); |
3991d84e MM |
993 | break; |
994 | case EAF_TYPE_OPAQUE: | |
fdf16eb6 | 995 | opaque_format(ad, pos, end - pos); |
3991d84e MM |
996 | break; |
997 | case EAF_TYPE_IP_ADDRESS: | |
fdf16eb6 | 998 | bsprintf(pos, "%I", *(ip_addr *) ad->data); |
3991d84e MM |
999 | break; |
1000 | case EAF_TYPE_ROUTER_ID: | |
fdf16eb6 | 1001 | bsprintf(pos, "%R", e->u.data); |
3991d84e | 1002 | break; |
c6add07f | 1003 | case EAF_TYPE_AS_PATH: |
fdf16eb6 | 1004 | as_path_format(ad, pos, end - pos); |
c6add07f | 1005 | break; |
315f23a0 OZ |
1006 | case EAF_TYPE_BITFIELD: |
1007 | bsprintf(pos, "%08x", e->u.data); | |
1008 | break; | |
c6add07f | 1009 | case EAF_TYPE_INT_SET: |
fdf16eb6 OZ |
1010 | ea_show_int_set(c, ad, 1, pos, buf, end); |
1011 | return; | |
42a0c054 OZ |
1012 | case EAF_TYPE_EC_SET: |
1013 | ea_show_ec_set(c, ad, pos, buf, end); | |
1014 | return; | |
66dbdbd9 OZ |
1015 | case EAF_TYPE_LC_SET: |
1016 | ea_show_lc_set(c, ad, pos, buf, end); | |
1017 | return; | |
224a152c OZ |
1018 | case EAF_TYPE_STRING: |
1019 | bsnprintf(pos, end - pos, "%s", (const char *) ad->data); | |
1020 | break; | |
3991d84e | 1021 | default: |
fdf16eb6 | 1022 | bsprintf(pos, "<type %02x>", e->type); |
3991d84e MM |
1023 | } |
1024 | } | |
b28431e5 OZ |
1025 | |
1026 | if (status != GA_HIDDEN) | |
1027 | cli_printf(c, -1012, "\t%s", buf); | |
3991d84e MM |
1028 | } |
1029 | ||
3ce8c610 MM |
1030 | /** |
1031 | * ea_dump - dump an extended attribute | |
1032 | * @e: attribute to be dumped | |
1033 | * | |
1034 | * ea_dump() dumps contents of the extended attribute given to | |
1035 | * the debug output. | |
1036 | */ | |
b77ae37d MM |
1037 | void |
1038 | ea_dump(ea_list *e) | |
1039 | { | |
1040 | int i; | |
1041 | ||
1042 | if (!e) | |
1043 | { | |
1044 | debug("NONE"); | |
1045 | return; | |
1046 | } | |
1047 | while (e) | |
1048 | { | |
1049 | debug("[%c%c%c]", | |
1050 | (e->flags & EALF_SORTED) ? 'S' : 's', | |
1051 | (e->flags & EALF_BISECT) ? 'B' : 'b', | |
1052 | (e->flags & EALF_CACHED) ? 'C' : 'c'); | |
1053 | for(i=0; i<e->count; i++) | |
2326b001 | 1054 | { |
b77ae37d MM |
1055 | eattr *a = &e->attrs[i]; |
1056 | debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags); | |
b77ae37d | 1057 | debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]); |
63cf5d5d | 1058 | if (a->originated) |
51a183af | 1059 | debug("o"); |
b77ae37d MM |
1060 | if (a->type & EAF_EMBEDDED) |
1061 | debug(":%08x", a->u.data); | |
1062 | else | |
1063 | { | |
1064 | int j, len = a->u.ptr->length; | |
1065 | debug("[%d]:", len); | |
1066 | for(j=0; j<len; j++) | |
1067 | debug("%02x", a->u.ptr->data[j]); | |
1068 | } | |
2326b001 | 1069 | } |
b77ae37d MM |
1070 | if (e = e->next) |
1071 | debug(" | "); | |
2326b001 | 1072 | } |
2326b001 MM |
1073 | } |
1074 | ||
3ce8c610 MM |
1075 | /** |
1076 | * ea_hash - calculate an &ea_list hash key | |
1077 | * @e: attribute list | |
1078 | * | |
1079 | * ea_hash() takes an extended attribute list and calculated a hopefully | |
1080 | * uniformly distributed hash value from its contents. | |
1081 | */ | |
ae80a2de | 1082 | inline uint |
ee76a92a MM |
1083 | ea_hash(ea_list *e) |
1084 | { | |
9a74622c JMM |
1085 | const u64 mul = 0x68576150f3d6847; |
1086 | u64 h = 0xafcef24eda8b29; | |
ee76a92a MM |
1087 | int i; |
1088 | ||
1089 | if (e) /* Assuming chain of length 1 */ | |
1090 | { | |
1091 | for(i=0; i<e->count; i++) | |
1092 | { | |
1093 | struct eattr *a = &e->attrs[i]; | |
9a74622c | 1094 | h ^= a->id; h *= mul; |
ee76a92a MM |
1095 | if (a->type & EAF_EMBEDDED) |
1096 | h ^= a->u.data; | |
1097 | else | |
1098 | { | |
4c553c5a | 1099 | const struct adata *d = a->u.ptr; |
9a74622c | 1100 | h ^= mem_hash(d->data, d->length); |
ee76a92a | 1101 | } |
9a74622c | 1102 | h *= mul; |
ee76a92a | 1103 | } |
ee76a92a | 1104 | } |
9a74622c | 1105 | return (h >> 32) ^ (h & 0xffffffff); |
ee76a92a MM |
1106 | } |
1107 | ||
3ce8c610 MM |
1108 | /** |
1109 | * ea_append - concatenate &ea_list's | |
1110 | * @to: destination list (can be %NULL) | |
1111 | * @what: list to be appended (can be %NULL) | |
1112 | * | |
1113 | * This function appends the &ea_list @what at the end of | |
1114 | * &ea_list @to and returns a pointer to the resulting list. | |
1115 | */ | |
ce1da96e MM |
1116 | ea_list * |
1117 | ea_append(ea_list *to, ea_list *what) | |
1118 | { | |
1119 | ea_list *res; | |
1120 | ||
1121 | if (!to) | |
1122 | return what; | |
1123 | res = to; | |
1124 | while (to->next) | |
1125 | to = to->next; | |
1126 | to->next = what; | |
1127 | return res; | |
1128 | } | |
1129 | ||
b77ae37d MM |
1130 | /* |
1131 | * rta's | |
1132 | */ | |
1133 | ||
ae80a2de PT |
1134 | static uint rta_cache_count; |
1135 | static uint rta_cache_size = 32; | |
1136 | static uint rta_cache_limit; | |
1137 | static uint rta_cache_mask; | |
ee76a92a MM |
1138 | static rta **rta_hash_table; |
1139 | ||
1140 | static void | |
1141 | rta_alloc_hash(void) | |
1142 | { | |
5d86aefb | 1143 | rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size); |
ee76a92a MM |
1144 | if (rta_cache_size < 32768) |
1145 | rta_cache_limit = rta_cache_size * 2; | |
1146 | else | |
1147 | rta_cache_limit = ~0; | |
1148 | rta_cache_mask = rta_cache_size - 1; | |
1149 | } | |
1150 | ||
ae80a2de | 1151 | static inline uint |
ee76a92a MM |
1152 | rta_hash(rta *a) |
1153 | { | |
d39d41fb | 1154 | u64 h; |
54ac0bec | 1155 | mem_hash_init(&h); |
d39d41fb | 1156 | #define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f)); |
eb937358 | 1157 | #define BMIX(f) mem_hash_mix_num(&h, a->f); |
54ac0bec | 1158 | MIX(hostentry); |
54ac0bec JMM |
1159 | MIX(from); |
1160 | MIX(igp_metric); | |
eb937358 MM |
1161 | BMIX(source); |
1162 | BMIX(scope); | |
1163 | BMIX(dest); | |
1164 | MIX(pref); | |
d39d41fb | 1165 | #undef MIX |
54ac0bec | 1166 | |
4e276a89 | 1167 | return mem_hash_value(&h) ^ nexthop_hash(&(a->nh)) ^ ea_hash(a->eattrs); |
ee76a92a MM |
1168 | } |
1169 | ||
2326b001 MM |
1170 | static inline int |
1171 | rta_same(rta *x, rta *y) | |
1172 | { | |
5cff1d5f | 1173 | return (x->source == y->source && |
2326b001 | 1174 | x->scope == y->scope && |
2326b001 | 1175 | x->dest == y->dest && |
d1e146f2 | 1176 | x->igp_metric == y->igp_metric && |
2326b001 | 1177 | ipa_equal(x->from, y->from) && |
d1e146f2 | 1178 | x->hostentry == y->hostentry && |
4e276a89 | 1179 | nexthop_same(&(x->nh), &(y->nh)) && |
2727bb7c | 1180 | ea_same(x->eattrs, y->eattrs)); |
2326b001 MM |
1181 | } |
1182 | ||
ec5e5d23 JMM |
1183 | static inline slab * |
1184 | rta_slab(rta *a) | |
1185 | { | |
1186 | return rta_slab_[a->nh.labels > 2 ? 3 : a->nh.labels]; | |
1187 | } | |
1188 | ||
2326b001 MM |
1189 | static rta * |
1190 | rta_copy(rta *o) | |
1191 | { | |
ec5e5d23 | 1192 | rta *r = sl_alloc(rta_slab(o)); |
2326b001 | 1193 | |
ec5e5d23 | 1194 | memcpy(r, o, rta_size(o)); |
2326b001 | 1195 | r->uc = 1; |
4e276a89 | 1196 | r->nh.next = nexthop_copy(o->nh.next); |
2727bb7c | 1197 | r->eattrs = ea_list_copy(o->eattrs); |
2326b001 MM |
1198 | return r; |
1199 | } | |
1200 | ||
ee76a92a MM |
1201 | static inline void |
1202 | rta_insert(rta *r) | |
1203 | { | |
ae80a2de | 1204 | uint h = r->hash_key & rta_cache_mask; |
ee76a92a MM |
1205 | r->next = rta_hash_table[h]; |
1206 | if (r->next) | |
1207 | r->next->pprev = &r->next; | |
1208 | r->pprev = &rta_hash_table[h]; | |
1209 | rta_hash_table[h] = r; | |
1210 | } | |
1211 | ||
1212 | static void | |
1213 | rta_rehash(void) | |
1214 | { | |
ae80a2de PT |
1215 | uint ohs = rta_cache_size; |
1216 | uint h; | |
ee76a92a MM |
1217 | rta *r, *n; |
1218 | rta **oht = rta_hash_table; | |
1219 | ||
1220 | rta_cache_size = 2*rta_cache_size; | |
1221 | DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size); | |
1222 | rta_alloc_hash(); | |
1223 | for(h=0; h<ohs; h++) | |
1224 | for(r=oht[h]; r; r=n) | |
1225 | { | |
1226 | n = r->next; | |
1227 | rta_insert(r); | |
1228 | } | |
1229 | mb_free(oht); | |
1230 | } | |
1231 | ||
3ce8c610 MM |
1232 | /** |
1233 | * rta_lookup - look up a &rta in attribute cache | |
2e9b2421 | 1234 | * @o: a un-cached &rta |
3ce8c610 | 1235 | * |
2e9b2421 | 1236 | * rta_lookup() gets an un-cached &rta structure and returns its cached |
3ce8c610 MM |
1237 | * counterpart. It starts with examining the attribute cache to see whether |
1238 | * there exists a matching entry. If such an entry exists, it's returned and | |
1239 | * its use count is incremented, else a new entry is created with use count | |
1240 | * set to 1. | |
1241 | * | |
1242 | * The extended attribute lists attached to the &rta are automatically | |
1243 | * converted to the normalized form. | |
1244 | */ | |
2326b001 MM |
1245 | rta * |
1246 | rta_lookup(rta *o) | |
1247 | { | |
1248 | rta *r; | |
ae80a2de | 1249 | uint h; |
2326b001 | 1250 | |
eb937358 | 1251 | ASSERT(!o->cached); |
2727bb7c | 1252 | if (o->eattrs) |
13c0be19 | 1253 | ea_normalize(o->eattrs); |
b77ae37d | 1254 | |
ee76a92a MM |
1255 | h = rta_hash(o); |
1256 | for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next) | |
1257 | if (r->hash_key == h && rta_same(r, o)) | |
2326b001 | 1258 | return rta_clone(r); |
b77ae37d | 1259 | |
2326b001 | 1260 | r = rta_copy(o); |
ee76a92a | 1261 | r->hash_key = h; |
eb937358 | 1262 | r->cached = 1; |
cfe34a31 | 1263 | rt_lock_hostentry(r->hostentry); |
ee76a92a MM |
1264 | rta_insert(r); |
1265 | ||
1266 | if (++rta_cache_count > rta_cache_limit) | |
1267 | rta_rehash(); | |
1268 | ||
2326b001 MM |
1269 | return r; |
1270 | } | |
1271 | ||
1272 | void | |
b77ae37d | 1273 | rta__free(rta *a) |
2326b001 | 1274 | { |
eb937358 | 1275 | ASSERT(rta_cache_count && a->cached); |
ee76a92a | 1276 | rta_cache_count--; |
5d86aefb MM |
1277 | *a->pprev = a->next; |
1278 | if (a->next) | |
1279 | a->next->pprev = a->pprev; | |
cfe34a31 | 1280 | rt_unlock_hostentry(a->hostentry); |
4e276a89 JMM |
1281 | if (a->nh.next) |
1282 | nexthop_free(a->nh.next); | |
5d86aefb | 1283 | ea_free(a->eattrs); |
eb937358 | 1284 | a->cached = 0; |
ebd807c0 | 1285 | sl_free(a); |
2326b001 MM |
1286 | } |
1287 | ||
8d9eef17 OZ |
1288 | rta * |
1289 | rta_do_cow(rta *o, linpool *lp) | |
1290 | { | |
63f1c4d9 | 1291 | rta *r = lp_alloc(lp, RTA_MAX_SIZE); |
ec5e5d23 | 1292 | memcpy(r, o, rta_size(o)); |
f2010f9c JMM |
1293 | for (struct nexthop **nhn = &(r->nh.next), *nho = o->nh.next; nho; nho = nho->next) |
1294 | { | |
1295 | *nhn = lp_alloc(lp, nexthop_size(nho)); | |
1296 | memcpy(*nhn, nho, nexthop_size(nho)); | |
1297 | nhn = &((*nhn)->next); | |
1298 | } | |
eb937358 | 1299 | r->cached = 0; |
8d9eef17 OZ |
1300 | r->uc = 0; |
1301 | return r; | |
1302 | } | |
1303 | ||
3ce8c610 MM |
1304 | /** |
1305 | * rta_dump - dump route attributes | |
1306 | * @a: attribute structure to dump | |
1307 | * | |
1308 | * This function takes a &rta and dumps its contents to the debug output. | |
1309 | */ | |
2326b001 | 1310 | void |
66e53309 | 1311 | rta_dump(rta *a) |
2326b001 | 1312 | { |
3660f19d | 1313 | static char *rts[] = { "", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE", |
beaf86e1 | 1314 | "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP", |
98ac6176 | 1315 | "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1", |
977b82fb IP |
1316 | "RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL", |
1317 | "RTS_RPKI", "RTS_PERF", "RTS_AGGREGATED", }; | |
66e53309 MM |
1318 | static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" }; |
1319 | ||
5cff1d5f MM |
1320 | debug("pref=%d uc=%d %s %s%s h=%04x", |
1321 | a->pref, a->uc, rts[a->source], ip_scope_text(a->scope), | |
ee76a92a | 1322 | rtd[a->dest], a->hash_key); |
eb937358 | 1323 | if (!a->cached) |
04925e90 | 1324 | debug(" !CACHED"); |
962ba482 | 1325 | debug(" <-%I", a->from); |
4e276a89 JMM |
1326 | if (a->dest == RTD_UNICAST) |
1327 | for (struct nexthop *nh = &(a->nh); nh; nh = nh->next) | |
1328 | { | |
1329 | if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw); | |
ec5e5d23 JMM |
1330 | if (nh->labels) debug(" L %d", nh->label[0]); |
1331 | for (int i=1; i<nh->labels; i++) | |
1332 | debug("/%d", nh->label[i]); | |
4e276a89 JMM |
1333 | debug(" [%s]", nh->iface ? nh->iface->name : "???"); |
1334 | } | |
2727bb7c | 1335 | if (a->eattrs) |
b77ae37d MM |
1336 | { |
1337 | debug(" EA: "); | |
2727bb7c | 1338 | ea_dump(a->eattrs); |
b77ae37d | 1339 | } |
2326b001 MM |
1340 | } |
1341 | ||
3ce8c610 MM |
1342 | /** |
1343 | * rta_dump_all - dump attribute cache | |
1344 | * | |
1345 | * This function dumps the whole contents of route attribute cache | |
1346 | * to the debug output. | |
1347 | */ | |
2326b001 MM |
1348 | void |
1349 | rta_dump_all(void) | |
1350 | { | |
66e53309 | 1351 | rta *a; |
ae80a2de | 1352 | uint h; |
ee76a92a MM |
1353 | |
1354 | debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit); | |
1355 | for(h=0; h<rta_cache_size; h++) | |
1356 | for(a=rta_hash_table[h]; a; a=a->next) | |
1357 | { | |
1358 | debug("%p ", a); | |
1359 | rta_dump(a); | |
1360 | debug("\n"); | |
1361 | } | |
66e53309 | 1362 | debug("\n"); |
2326b001 MM |
1363 | } |
1364 | ||
730f2e2c | 1365 | void |
13c0be19 | 1366 | rta_show(struct cli *c, rta *a) |
730f2e2c | 1367 | { |
69b2f63d | 1368 | cli_printf(c, -1008, "\tType: %s %s", rta_src_names[a->source], ip_scope_text(a->scope)); |
13c0be19 JMM |
1369 | |
1370 | for(ea_list *eal = a->eattrs; eal; eal=eal->next) | |
1371 | for(int i=0; i<eal->count; i++) | |
fdf16eb6 | 1372 | ea_show(c, &eal->attrs[i]); |
730f2e2c MM |
1373 | } |
1374 | ||
3ce8c610 MM |
1375 | /** |
1376 | * rta_init - initialize route attribute cache | |
1377 | * | |
1378 | * This function is called during initialization of the routing | |
1379 | * table module to set up the internals of the attribute cache. | |
1380 | */ | |
2326b001 MM |
1381 | void |
1382 | rta_init(void) | |
1383 | { | |
ed68a5c6 | 1384 | rta_pool = rp_new(&root_pool, "Attributes"); |
ec5e5d23 JMM |
1385 | |
1386 | rta_slab_[0] = sl_new(rta_pool, sizeof(rta)); | |
1387 | rta_slab_[1] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)); | |
1388 | rta_slab_[2] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*2); | |
d14f8c3c | 1389 | rta_slab_[3] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*MPLS_MAX_LABEL_STACK); |
ec5e5d23 JMM |
1390 | |
1391 | nexthop_slab_[0] = sl_new(rta_pool, sizeof(struct nexthop)); | |
1392 | nexthop_slab_[1] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)); | |
1393 | nexthop_slab_[2] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*2); | |
d14f8c3c | 1394 | nexthop_slab_[3] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*MPLS_MAX_LABEL_STACK); |
ec5e5d23 | 1395 | |
ee76a92a | 1396 | rta_alloc_hash(); |
094d2bdb | 1397 | rte_src_init(); |
2326b001 | 1398 | } |
3ce8c610 MM |
1399 | |
1400 | /* | |
1401 | * Documentation for functions declared inline in route.h | |
1402 | */ | |
1403 | #if 0 | |
1404 | ||
1405 | /** | |
1406 | * rta_clone - clone route attributes | |
1407 | * @r: a &rta to be cloned | |
1408 | * | |
1409 | * rta_clone() takes a cached &rta and returns its identical cached | |
1410 | * copy. Currently it works by just returning the original &rta with | |
1411 | * its use count incremented. | |
1412 | */ | |
1413 | static inline rta *rta_clone(rta *r) | |
1414 | { DUMMY; } | |
1415 | ||
1416 | /** | |
1417 | * rta_free - free route attributes | |
1418 | * @r: a &rta to be freed | |
1419 | * | |
1420 | * If you stop using a &rta (for example when deleting a route which uses | |
1421 | * it), you need to call rta_free() to notify the attribute cache the | |
1422 | * attribute is no longer in use and can be freed if you were the last | |
1423 | * user (which rta_free() tests by inspecting the use count). | |
1424 | */ | |
1425 | static inline void rta_free(rta *r) | |
1426 | { DUMMY; } | |
1427 | ||
1428 | #endif |