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