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