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