]>
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 | ||
101 | static inline int u32_cto(unsigned int x) { return ffs(~x) - 1; } | |
102 | ||
103 | static inline u32 | |
104 | rte_src_alloc_id(void) | |
105 | { | |
106 | int i, j; | |
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; | |
e7d2ac44 OZ |
170 | |
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 | ||
7e95c05d OZ |
198 | static inline unsigned int |
199 | mpnh_hash(struct mpnh *x) | |
200 | { | |
201 | unsigned int h = 0; | |
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 | ||
218 | static struct mpnh * | |
219 | mpnh_copy(struct mpnh *o) | |
220 | { | |
221 | struct mpnh *first = NULL; | |
222 | struct mpnh **last = &first; | |
223 | ||
224 | for (; o; o = o->next) | |
225 | { | |
226 | struct mpnh *n = sl_alloc(mpnh_slab); | |
227 | n->gw = o->gw; | |
228 | n->iface = o->iface; | |
229 | n->next = NULL; | |
230 | n->weight = o->weight; | |
231 | ||
232 | *last = n; | |
233 | last = &(n->next); | |
234 | } | |
235 | ||
236 | return first; | |
237 | } | |
238 | ||
239 | static void | |
240 | mpnh_free(struct mpnh *o) | |
241 | { | |
242 | struct mpnh *n; | |
243 | ||
244 | while (o) | |
245 | { | |
246 | n = o->next; | |
247 | sl_free(mpnh_slab, o); | |
248 | o = n; | |
249 | } | |
250 | } | |
251 | ||
252 | ||
b77ae37d MM |
253 | /* |
254 | * Extended Attributes | |
255 | */ | |
256 | ||
8d24b689 MM |
257 | static inline eattr * |
258 | ea__find(ea_list *e, unsigned id) | |
b77ae37d MM |
259 | { |
260 | eattr *a; | |
261 | int l, r, m; | |
262 | ||
263 | while (e) | |
264 | { | |
265 | if (e->flags & EALF_BISECT) | |
266 | { | |
267 | l = 0; | |
fee78355 | 268 | r = e->count - 1; |
b77ae37d MM |
269 | while (l <= r) |
270 | { | |
271 | m = (l+r) / 2; | |
272 | a = &e->attrs[m]; | |
273 | if (a->id == id) | |
274 | return a; | |
275 | else if (a->id < id) | |
276 | l = m+1; | |
277 | else | |
278 | r = m-1; | |
279 | } | |
280 | } | |
281 | else | |
282 | for(m=0; m<e->count; m++) | |
283 | if (e->attrs[m].id == id) | |
284 | return &e->attrs[m]; | |
285 | e = e->next; | |
286 | } | |
287 | return NULL; | |
288 | } | |
289 | ||
3ce8c610 MM |
290 | /** |
291 | * ea_find - find an extended attribute | |
292 | * @e: attribute list to search in | |
293 | * @id: attribute ID to search for | |
294 | * | |
295 | * Given an extended attribute list, ea_find() searches for a first | |
2e9b2421 | 296 | * occurrence of an attribute with specified ID, returning either a pointer |
3ce8c610 MM |
297 | * to its &eattr structure or %NULL if no such attribute exists. |
298 | */ | |
8d24b689 MM |
299 | eattr * |
300 | ea_find(ea_list *e, unsigned id) | |
301 | { | |
302 | eattr *a = ea__find(e, id & EA_CODE_MASK); | |
303 | ||
304 | if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF && | |
305 | !(id & EA_ALLOW_UNDEF)) | |
306 | return NULL; | |
307 | return a; | |
308 | } | |
309 | ||
3ce8c610 MM |
310 | /** |
311 | * ea_get_int - fetch an integer attribute | |
312 | * @e: attribute list | |
313 | * @id: attribute ID | |
314 | * @def: default value | |
315 | * | |
316 | * This function is a shortcut for retrieving a value of an integer attribute | |
317 | * by calling ea_find() to find the attribute, extracting its value or returning | |
318 | * a provided default if no such attribute is present. | |
319 | */ | |
c0100454 PM |
320 | int |
321 | ea_get_int(ea_list *e, unsigned id, int def) | |
322 | { | |
323 | eattr *a = ea_find(e, id); | |
324 | if (!a) | |
325 | return def; | |
326 | return a->u.data; | |
327 | } | |
328 | ||
b77ae37d MM |
329 | static inline void |
330 | ea_do_sort(ea_list *e) | |
331 | { | |
332 | unsigned n = e->count; | |
333 | eattr *a = e->attrs; | |
334 | eattr *b = alloca(n * sizeof(eattr)); | |
335 | unsigned s, ss; | |
336 | ||
337 | /* We need to use a stable sorting algorithm, hence mergesort */ | |
338 | do | |
339 | { | |
340 | s = ss = 0; | |
341 | while (s < n) | |
342 | { | |
343 | eattr *p, *q, *lo, *hi; | |
344 | p = b; | |
345 | ss = s; | |
346 | *p++ = a[s++]; | |
347 | while (s < n && p[-1].id <= a[s].id) | |
348 | *p++ = a[s++]; | |
349 | if (s < n) | |
350 | { | |
351 | q = p; | |
352 | *p++ = a[s++]; | |
353 | while (s < n && p[-1].id <= a[s].id) | |
354 | *p++ = a[s++]; | |
355 | lo = b; | |
356 | hi = q; | |
357 | s = ss; | |
358 | while (lo < q && hi < p) | |
359 | if (lo->id <= hi->id) | |
360 | a[s++] = *lo++; | |
361 | else | |
362 | a[s++] = *hi++; | |
363 | while (lo < q) | |
364 | a[s++] = *lo++; | |
365 | while (hi < p) | |
366 | a[s++] = *hi++; | |
367 | } | |
368 | } | |
369 | } | |
370 | while (ss); | |
371 | } | |
372 | ||
373 | static inline void | |
374 | ea_do_prune(ea_list *e) | |
375 | { | |
51a183af | 376 | eattr *s, *d, *l, *s0; |
8d24b689 MM |
377 | int i = 0; |
378 | ||
379 | /* Discard duplicates and undefs. Do you remember sorting was stable? */ | |
380 | s = d = e->attrs; | |
381 | l = e->attrs + e->count; | |
382 | while (s < l) | |
383 | { | |
51a183af MM |
384 | s0 = s++; |
385 | while (s < l && s->id == s[-1].id) | |
386 | s++; | |
387 | /* s0 is the most recent version, s[-1] the oldest one */ | |
388 | if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF) | |
8d24b689 | 389 | { |
51a183af MM |
390 | *d = *s0; |
391 | d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED); | |
392 | d++; | |
8d24b689 MM |
393 | i++; |
394 | } | |
8d24b689 MM |
395 | } |
396 | e->count = i; | |
b77ae37d MM |
397 | } |
398 | ||
3ce8c610 MM |
399 | /** |
400 | * ea_sort - sort an attribute list | |
401 | * @e: list to be sorted | |
402 | * | |
403 | * This function takes a &ea_list chain and sorts the attributes | |
404 | * within each of its entries. | |
405 | * | |
406 | * If an attribute occurs multiple times in a single &ea_list, | |
2e9b2421 | 407 | * ea_sort() leaves only the first (the only significant) occurrence. |
3ce8c610 | 408 | */ |
b77ae37d MM |
409 | void |
410 | ea_sort(ea_list *e) | |
411 | { | |
412 | while (e) | |
413 | { | |
414 | if (!(e->flags & EALF_SORTED)) | |
415 | { | |
416 | ea_do_sort(e); | |
417 | ea_do_prune(e); | |
418 | e->flags |= EALF_SORTED; | |
419 | } | |
b77ae37d | 420 | if (e->count > 5) |
b77ae37d MM |
421 | e->flags |= EALF_BISECT; |
422 | e = e->next; | |
423 | } | |
424 | } | |
425 | ||
3ce8c610 MM |
426 | /** |
427 | * ea_scan - estimate attribute list size | |
428 | * @e: attribute list | |
429 | * | |
430 | * This function calculates an upper bound of the size of | |
431 | * a given &ea_list after merging with ea_merge(). | |
432 | */ | |
b77ae37d MM |
433 | unsigned |
434 | ea_scan(ea_list *e) | |
435 | { | |
436 | unsigned cnt = 0; | |
437 | ||
438 | while (e) | |
439 | { | |
440 | cnt += e->count; | |
441 | e = e->next; | |
442 | } | |
443 | return sizeof(ea_list) + sizeof(eattr)*cnt; | |
444 | } | |
445 | ||
3ce8c610 MM |
446 | /** |
447 | * ea_merge - merge segments of an attribute list | |
448 | * @e: attribute list | |
449 | * @t: buffer to store the result to | |
450 | * | |
451 | * This function takes a possibly multi-segment attribute list | |
452 | * and merges all of its segments to one. | |
453 | * | |
454 | * The primary use of this function is for &ea_list normalization: | |
455 | * first call ea_scan() to determine how much memory will the result | |
456 | * take, then allocate a buffer (usually using alloca()), merge the | |
457 | * segments with ea_merge() and finally sort and prune the result | |
458 | * by calling ea_sort(). | |
459 | */ | |
b77ae37d MM |
460 | void |
461 | ea_merge(ea_list *e, ea_list *t) | |
462 | { | |
463 | eattr *d = t->attrs; | |
464 | ||
465 | t->flags = 0; | |
466 | t->count = 0; | |
467 | t->next = NULL; | |
468 | while (e) | |
469 | { | |
470 | memcpy(d, e->attrs, sizeof(eattr)*e->count); | |
471 | t->count += e->count; | |
472 | d += e->count; | |
473 | e = e->next; | |
474 | } | |
475 | } | |
476 | ||
3ce8c610 MM |
477 | /** |
478 | * ea_same - compare two &ea_list's | |
479 | * @x: attribute list | |
480 | * @y: attribute list | |
481 | * | |
482 | * ea_same() compares two normalized attribute lists @x and @y and returns | |
483 | * 1 if they contain the same attributes, 0 otherwise. | |
484 | */ | |
6f57dcc0 | 485 | int |
2326b001 MM |
486 | ea_same(ea_list *x, ea_list *y) |
487 | { | |
488 | int c; | |
489 | ||
b77ae37d MM |
490 | if (!x || !y) |
491 | return x == y; | |
492 | ASSERT(!x->next && !y->next); | |
493 | if (x->count != y->count) | |
494 | return 0; | |
495 | for(c=0; c<x->count; c++) | |
2326b001 | 496 | { |
b77ae37d MM |
497 | eattr *a = &x->attrs[c]; |
498 | eattr *b = &y->attrs[c]; | |
499 | ||
500 | if (a->id != b->id || | |
501 | a->flags != b->flags || | |
502 | a->type != b->type || | |
28a10f84 | 503 | ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr))) |
2326b001 | 504 | return 0; |
b77ae37d MM |
505 | } |
506 | return 1; | |
507 | } | |
508 | ||
509 | static inline ea_list * | |
510 | ea_list_copy(ea_list *o) | |
511 | { | |
512 | ea_list *n; | |
513 | unsigned i, len; | |
514 | ||
515 | if (!o) | |
516 | return NULL; | |
517 | ASSERT(!o->next); | |
518 | len = sizeof(ea_list) + sizeof(eattr) * o->count; | |
519 | n = mb_alloc(rta_pool, len); | |
520 | memcpy(n, o, len); | |
521 | n->flags |= EALF_CACHED; | |
522 | for(i=0; i<o->count; i++) | |
523 | { | |
524 | eattr *a = &n->attrs[i]; | |
08732b71 | 525 | if (!(a->type & EAF_EMBEDDED)) |
b77ae37d MM |
526 | { |
527 | unsigned size = sizeof(struct adata) + a->u.ptr->length; | |
528 | struct adata *d = mb_alloc(rta_pool, size); | |
529 | memcpy(d, a->u.ptr, size); | |
530 | a->u.ptr = d; | |
531 | } | |
532 | } | |
533 | return n; | |
534 | } | |
535 | ||
5d86aefb MM |
536 | static inline void |
537 | ea_free(ea_list *o) | |
538 | { | |
3ced9b34 MM |
539 | int i; |
540 | ||
5d86aefb MM |
541 | if (o) |
542 | { | |
543 | ASSERT(!o->next); | |
3ced9b34 MM |
544 | for(i=0; i<o->count; i++) |
545 | { | |
546 | eattr *a = &o->attrs[i]; | |
547 | if (!(a->type & EAF_EMBEDDED)) | |
548 | mb_free(a->u.ptr); | |
549 | } | |
5d86aefb MM |
550 | mb_free(o); |
551 | } | |
552 | } | |
553 | ||
ba5e5940 OZ |
554 | static int |
555 | get_generic_attr(eattr *a, byte **buf, int buflen UNUSED) | |
556 | { | |
557 | if (a->id == EA_GEN_IGP_METRIC) | |
558 | { | |
559 | *buf += bsprintf(*buf, "igp_metric"); | |
560 | return GA_NAME; | |
561 | } | |
562 | ||
563 | return GA_UNKNOWN; | |
564 | } | |
565 | ||
fdf16eb6 OZ |
566 | static inline void |
567 | opaque_format(struct adata *ad, byte *buf, unsigned int size) | |
568 | { | |
569 | byte *bound = buf + size - 10; | |
570 | int i; | |
571 | ||
572 | for(i = 0; i < ad->length; i++) | |
573 | { | |
574 | if (buf > bound) | |
575 | { | |
576 | strcpy(buf, " ..."); | |
577 | return; | |
578 | } | |
579 | if (i) | |
580 | *buf++ = ' '; | |
581 | ||
582 | buf += bsprintf(buf, "%02x", ad->data[i]); | |
583 | } | |
584 | ||
585 | *buf = 0; | |
586 | return; | |
587 | } | |
588 | ||
589 | static inline void | |
590 | ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end) | |
591 | { | |
592 | int i = int_set_format(ad, way, 0, pos, end - pos); | |
a52d52fa | 593 | cli_printf(c, -1012, "\t%s", buf); |
fdf16eb6 OZ |
594 | while (i) |
595 | { | |
596 | i = int_set_format(ad, way, i, buf, end - buf - 1); | |
a52d52fa | 597 | cli_printf(c, -1012, "\t\t%s", buf); |
fdf16eb6 OZ |
598 | } |
599 | } | |
600 | ||
42a0c054 OZ |
601 | static inline void |
602 | ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end) | |
603 | { | |
604 | int i = ec_set_format(ad, 0, pos, end - pos); | |
a52d52fa | 605 | cli_printf(c, -1012, "\t%s", buf); |
42a0c054 OZ |
606 | while (i) |
607 | { | |
608 | i = ec_set_format(ad, i, buf, end - buf - 1); | |
a52d52fa | 609 | cli_printf(c, -1012, "\t\t%s", buf); |
42a0c054 OZ |
610 | } |
611 | } | |
612 | ||
3ce8c610 | 613 | /** |
fdf16eb6 OZ |
614 | * ea_show - print an &eattr to CLI |
615 | * @c: destination CLI | |
616 | * @e: attribute to be printed | |
3ce8c610 | 617 | * |
fdf16eb6 OZ |
618 | * This function takes an extended attribute represented by its &eattr |
619 | * structure and prints it to the CLI according to the type information. | |
3ce8c610 MM |
620 | * |
621 | * If the protocol defining the attribute provides its own | |
622 | * get_attr() hook, it's consulted first. | |
623 | */ | |
3991d84e | 624 | void |
fdf16eb6 | 625 | ea_show(struct cli *c, eattr *e) |
3991d84e MM |
626 | { |
627 | struct protocol *p; | |
628 | int status = GA_UNKNOWN; | |
3991d84e | 629 | struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr; |
fdf16eb6 OZ |
630 | byte buf[CLI_MSG_SIZE]; |
631 | byte *pos = buf, *end = buf + sizeof(buf); | |
3991d84e MM |
632 | |
633 | if (p = attr_class_to_protocol[EA_PROTO(e->id)]) | |
634 | { | |
fdf16eb6 | 635 | pos += bsprintf(pos, "%s.", p->name); |
3991d84e | 636 | if (p->get_attr) |
fdf16eb6 OZ |
637 | status = p->get_attr(e, pos, end - pos); |
638 | pos += strlen(pos); | |
3991d84e MM |
639 | } |
640 | else if (EA_PROTO(e->id)) | |
fdf16eb6 | 641 | pos += bsprintf(pos, "%02x.", EA_PROTO(e->id)); |
ba5e5940 | 642 | else |
fdf16eb6 | 643 | status = get_generic_attr(e, &pos, end - pos); |
ba5e5940 | 644 | |
3991d84e | 645 | if (status < GA_NAME) |
fdf16eb6 | 646 | pos += bsprintf(pos, "%02x", EA_ID(e->id)); |
3991d84e MM |
647 | if (status < GA_FULL) |
648 | { | |
fdf16eb6 OZ |
649 | *pos++ = ':'; |
650 | *pos++ = ' '; | |
3991d84e MM |
651 | switch (e->type & EAF_TYPE_MASK) |
652 | { | |
653 | case EAF_TYPE_INT: | |
fdf16eb6 | 654 | bsprintf(pos, "%u", e->u.data); |
3991d84e MM |
655 | break; |
656 | case EAF_TYPE_OPAQUE: | |
fdf16eb6 | 657 | opaque_format(ad, pos, end - pos); |
3991d84e MM |
658 | break; |
659 | case EAF_TYPE_IP_ADDRESS: | |
fdf16eb6 | 660 | bsprintf(pos, "%I", *(ip_addr *) ad->data); |
3991d84e MM |
661 | break; |
662 | case EAF_TYPE_ROUTER_ID: | |
fdf16eb6 | 663 | bsprintf(pos, "%R", e->u.data); |
3991d84e | 664 | break; |
c6add07f | 665 | case EAF_TYPE_AS_PATH: |
fdf16eb6 | 666 | as_path_format(ad, pos, end - pos); |
c6add07f MM |
667 | break; |
668 | case EAF_TYPE_INT_SET: | |
fdf16eb6 OZ |
669 | ea_show_int_set(c, ad, 1, pos, buf, end); |
670 | return; | |
42a0c054 OZ |
671 | case EAF_TYPE_EC_SET: |
672 | ea_show_ec_set(c, ad, pos, buf, end); | |
673 | return; | |
3991d84e MM |
674 | case EAF_TYPE_UNDEF: |
675 | default: | |
fdf16eb6 | 676 | bsprintf(pos, "<type %02x>", e->type); |
3991d84e MM |
677 | } |
678 | } | |
a52d52fa | 679 | cli_printf(c, -1012, "\t%s", buf); |
3991d84e MM |
680 | } |
681 | ||
3ce8c610 MM |
682 | /** |
683 | * ea_dump - dump an extended attribute | |
684 | * @e: attribute to be dumped | |
685 | * | |
686 | * ea_dump() dumps contents of the extended attribute given to | |
687 | * the debug output. | |
688 | */ | |
b77ae37d MM |
689 | void |
690 | ea_dump(ea_list *e) | |
691 | { | |
692 | int i; | |
693 | ||
694 | if (!e) | |
695 | { | |
696 | debug("NONE"); | |
697 | return; | |
698 | } | |
699 | while (e) | |
700 | { | |
701 | debug("[%c%c%c]", | |
702 | (e->flags & EALF_SORTED) ? 'S' : 's', | |
703 | (e->flags & EALF_BISECT) ? 'B' : 'b', | |
704 | (e->flags & EALF_CACHED) ? 'C' : 'c'); | |
705 | for(i=0; i<e->count; i++) | |
2326b001 | 706 | { |
b77ae37d MM |
707 | eattr *a = &e->attrs[i]; |
708 | debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags); | |
9f4929e7 MM |
709 | if (a->type & EAF_TEMP) |
710 | debug("T"); | |
b77ae37d | 711 | debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]); |
51a183af MM |
712 | if (a->type & EAF_ORIGINATED) |
713 | debug("o"); | |
b77ae37d MM |
714 | if (a->type & EAF_EMBEDDED) |
715 | debug(":%08x", a->u.data); | |
716 | else | |
717 | { | |
718 | int j, len = a->u.ptr->length; | |
719 | debug("[%d]:", len); | |
720 | for(j=0; j<len; j++) | |
721 | debug("%02x", a->u.ptr->data[j]); | |
722 | } | |
2326b001 | 723 | } |
b77ae37d MM |
724 | if (e = e->next) |
725 | debug(" | "); | |
2326b001 | 726 | } |
2326b001 MM |
727 | } |
728 | ||
3ce8c610 MM |
729 | /** |
730 | * ea_hash - calculate an &ea_list hash key | |
731 | * @e: attribute list | |
732 | * | |
733 | * ea_hash() takes an extended attribute list and calculated a hopefully | |
734 | * uniformly distributed hash value from its contents. | |
735 | */ | |
6f57dcc0 | 736 | inline unsigned int |
ee76a92a MM |
737 | ea_hash(ea_list *e) |
738 | { | |
739 | u32 h = 0; | |
740 | int i; | |
741 | ||
742 | if (e) /* Assuming chain of length 1 */ | |
743 | { | |
744 | for(i=0; i<e->count; i++) | |
745 | { | |
746 | struct eattr *a = &e->attrs[i]; | |
747 | h ^= a->id; | |
748 | if (a->type & EAF_EMBEDDED) | |
749 | h ^= a->u.data; | |
750 | else | |
751 | { | |
752 | struct adata *d = a->u.ptr; | |
753 | int size = d->length; | |
754 | byte *z = d->data; | |
755 | while (size >= 4) | |
756 | { | |
757 | h ^= *(u32 *)z; | |
758 | z += 4; | |
759 | size -= 4; | |
760 | } | |
761 | while (size--) | |
762 | h = (h >> 24) ^ (h << 8) ^ *z++; | |
763 | } | |
764 | } | |
765 | h ^= h >> 16; | |
766 | h ^= h >> 6; | |
767 | h &= 0xffff; | |
768 | } | |
769 | return h; | |
770 | } | |
771 | ||
3ce8c610 MM |
772 | /** |
773 | * ea_append - concatenate &ea_list's | |
774 | * @to: destination list (can be %NULL) | |
775 | * @what: list to be appended (can be %NULL) | |
776 | * | |
777 | * This function appends the &ea_list @what at the end of | |
778 | * &ea_list @to and returns a pointer to the resulting list. | |
779 | */ | |
ce1da96e MM |
780 | ea_list * |
781 | ea_append(ea_list *to, ea_list *what) | |
782 | { | |
783 | ea_list *res; | |
784 | ||
785 | if (!to) | |
786 | return what; | |
787 | res = to; | |
788 | while (to->next) | |
789 | to = to->next; | |
790 | to->next = what; | |
791 | return res; | |
792 | } | |
793 | ||
b77ae37d MM |
794 | /* |
795 | * rta's | |
796 | */ | |
797 | ||
ee76a92a MM |
798 | static unsigned int rta_cache_count; |
799 | static unsigned int rta_cache_size = 32; | |
800 | static unsigned int rta_cache_limit; | |
801 | static unsigned int rta_cache_mask; | |
802 | static rta **rta_hash_table; | |
803 | ||
804 | static void | |
805 | rta_alloc_hash(void) | |
806 | { | |
5d86aefb | 807 | rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size); |
ee76a92a MM |
808 | if (rta_cache_size < 32768) |
809 | rta_cache_limit = rta_cache_size * 2; | |
810 | else | |
811 | rta_cache_limit = ~0; | |
812 | rta_cache_mask = rta_cache_size - 1; | |
813 | } | |
814 | ||
815 | static inline unsigned int | |
816 | rta_hash(rta *a) | |
817 | { | |
094d2bdb | 818 | return (((unsigned) a->src) ^ ipa_hash(a->gw) ^ |
7e95c05d | 819 | mpnh_hash(a->nexthops) ^ ea_hash(a->eattrs)) & 0xffff; |
ee76a92a MM |
820 | } |
821 | ||
2326b001 MM |
822 | static inline int |
823 | rta_same(rta *x, rta *y) | |
824 | { | |
094d2bdb | 825 | return (x->src == y->src && |
2326b001 MM |
826 | x->source == y->source && |
827 | x->scope == y->scope && | |
828 | x->cast == y->cast && | |
829 | x->dest == y->dest && | |
2326b001 | 830 | x->flags == y->flags && |
d1e146f2 | 831 | x->igp_metric == y->igp_metric && |
2326b001 MM |
832 | ipa_equal(x->gw, y->gw) && |
833 | ipa_equal(x->from, y->from) && | |
834 | x->iface == y->iface && | |
d1e146f2 | 835 | x->hostentry == y->hostentry && |
7e95c05d | 836 | mpnh_same(x->nexthops, y->nexthops) && |
2727bb7c | 837 | ea_same(x->eattrs, y->eattrs)); |
2326b001 MM |
838 | } |
839 | ||
840 | static rta * | |
841 | rta_copy(rta *o) | |
842 | { | |
843 | rta *r = sl_alloc(rta_slab); | |
844 | ||
845 | memcpy(r, o, sizeof(rta)); | |
846 | r->uc = 1; | |
7e95c05d | 847 | r->nexthops = mpnh_copy(o->nexthops); |
2727bb7c | 848 | r->eattrs = ea_list_copy(o->eattrs); |
2326b001 MM |
849 | return r; |
850 | } | |
851 | ||
ee76a92a MM |
852 | static inline void |
853 | rta_insert(rta *r) | |
854 | { | |
855 | unsigned int h = r->hash_key & rta_cache_mask; | |
856 | r->next = rta_hash_table[h]; | |
857 | if (r->next) | |
858 | r->next->pprev = &r->next; | |
859 | r->pprev = &rta_hash_table[h]; | |
860 | rta_hash_table[h] = r; | |
861 | } | |
862 | ||
863 | static void | |
864 | rta_rehash(void) | |
865 | { | |
866 | unsigned int ohs = rta_cache_size; | |
867 | unsigned int h; | |
868 | rta *r, *n; | |
869 | rta **oht = rta_hash_table; | |
870 | ||
871 | rta_cache_size = 2*rta_cache_size; | |
872 | DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size); | |
873 | rta_alloc_hash(); | |
874 | for(h=0; h<ohs; h++) | |
875 | for(r=oht[h]; r; r=n) | |
876 | { | |
877 | n = r->next; | |
878 | rta_insert(r); | |
879 | } | |
880 | mb_free(oht); | |
881 | } | |
882 | ||
3ce8c610 MM |
883 | /** |
884 | * rta_lookup - look up a &rta in attribute cache | |
2e9b2421 | 885 | * @o: a un-cached &rta |
3ce8c610 | 886 | * |
2e9b2421 | 887 | * rta_lookup() gets an un-cached &rta structure and returns its cached |
3ce8c610 MM |
888 | * counterpart. It starts with examining the attribute cache to see whether |
889 | * there exists a matching entry. If such an entry exists, it's returned and | |
890 | * its use count is incremented, else a new entry is created with use count | |
891 | * set to 1. | |
892 | * | |
893 | * The extended attribute lists attached to the &rta are automatically | |
894 | * converted to the normalized form. | |
895 | */ | |
2326b001 MM |
896 | rta * |
897 | rta_lookup(rta *o) | |
898 | { | |
899 | rta *r; | |
ee76a92a | 900 | unsigned int h; |
2326b001 | 901 | |
b77ae37d | 902 | ASSERT(!(o->aflags & RTAF_CACHED)); |
2727bb7c | 903 | if (o->eattrs) |
b77ae37d | 904 | { |
2727bb7c | 905 | if (o->eattrs->next) /* Multiple ea_list's, need to merge them */ |
b77ae37d | 906 | { |
2727bb7c MM |
907 | ea_list *ml = alloca(ea_scan(o->eattrs)); |
908 | ea_merge(o->eattrs, ml); | |
909 | o->eattrs = ml; | |
b77ae37d | 910 | } |
2727bb7c | 911 | ea_sort(o->eattrs); |
b77ae37d MM |
912 | } |
913 | ||
ee76a92a MM |
914 | h = rta_hash(o); |
915 | for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next) | |
916 | if (r->hash_key == h && rta_same(r, o)) | |
2326b001 | 917 | return rta_clone(r); |
b77ae37d | 918 | |
2326b001 | 919 | r = rta_copy(o); |
ee76a92a | 920 | r->hash_key = h; |
04925e90 | 921 | r->aflags = RTAF_CACHED; |
094d2bdb | 922 | rt_lock_source(r->src); |
cfe34a31 | 923 | rt_lock_hostentry(r->hostentry); |
ee76a92a MM |
924 | rta_insert(r); |
925 | ||
926 | if (++rta_cache_count > rta_cache_limit) | |
927 | rta_rehash(); | |
928 | ||
2326b001 MM |
929 | return r; |
930 | } | |
931 | ||
932 | void | |
b77ae37d | 933 | rta__free(rta *a) |
2326b001 | 934 | { |
ee76a92a MM |
935 | ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED)); |
936 | rta_cache_count--; | |
5d86aefb MM |
937 | *a->pprev = a->next; |
938 | if (a->next) | |
939 | a->next->pprev = a->pprev; | |
940 | a->aflags = 0; /* Poison the entry */ | |
cfe34a31 | 941 | rt_unlock_hostentry(a->hostentry); |
094d2bdb | 942 | rt_unlock_source(a->src); |
7e95c05d | 943 | mpnh_free(a->nexthops); |
5d86aefb MM |
944 | ea_free(a->eattrs); |
945 | sl_free(rta_slab, a); | |
2326b001 MM |
946 | } |
947 | ||
3ce8c610 MM |
948 | /** |
949 | * rta_dump - dump route attributes | |
950 | * @a: attribute structure to dump | |
951 | * | |
952 | * This function takes a &rta and dumps its contents to the debug output. | |
953 | */ | |
2326b001 | 954 | void |
66e53309 | 955 | rta_dump(rta *a) |
2326b001 | 956 | { |
618533af | 957 | static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE", |
beaf86e1 | 958 | "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP", |
98ac6176 OF |
959 | "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1", |
960 | "RTS_OSPF_EXT2", "RTS_BGP" }; | |
66e53309 MM |
961 | static char *rtc[] = { "", " BC", " MC", " AC" }; |
962 | static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" }; | |
963 | ||
ee76a92a | 964 | debug("p=%s uc=%d %s %s%s%s h=%04x", |
094d2bdb | 965 | a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast], |
ee76a92a | 966 | rtd[a->dest], a->hash_key); |
04925e90 MM |
967 | if (!(a->aflags & RTAF_CACHED)) |
968 | debug(" !CACHED"); | |
962ba482 | 969 | debug(" <-%I", a->from); |
66e53309 | 970 | if (a->dest == RTD_ROUTER) |
962ba482 | 971 | debug(" ->%I", a->gw); |
66e53309 | 972 | if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER) |
48b41d58 | 973 | debug(" [%s]", a->iface ? a->iface->name : "???" ); |
2727bb7c | 974 | if (a->eattrs) |
b77ae37d MM |
975 | { |
976 | debug(" EA: "); | |
2727bb7c | 977 | ea_dump(a->eattrs); |
b77ae37d | 978 | } |
2326b001 MM |
979 | } |
980 | ||
3ce8c610 MM |
981 | /** |
982 | * rta_dump_all - dump attribute cache | |
983 | * | |
984 | * This function dumps the whole contents of route attribute cache | |
985 | * to the debug output. | |
986 | */ | |
2326b001 MM |
987 | void |
988 | rta_dump_all(void) | |
989 | { | |
66e53309 | 990 | rta *a; |
ee76a92a MM |
991 | unsigned int h; |
992 | ||
993 | debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit); | |
994 | for(h=0; h<rta_cache_size; h++) | |
995 | for(a=rta_hash_table[h]; a; a=a->next) | |
996 | { | |
997 | debug("%p ", a); | |
998 | rta_dump(a); | |
999 | debug("\n"); | |
1000 | } | |
66e53309 | 1001 | debug("\n"); |
2326b001 MM |
1002 | } |
1003 | ||
730f2e2c | 1004 | void |
ce1da96e | 1005 | rta_show(struct cli *c, rta *a, ea_list *eal) |
730f2e2c MM |
1006 | { |
1007 | static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect", | |
6370d6f6 | 1008 | "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" }; |
730f2e2c | 1009 | static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" }; |
3991d84e | 1010 | int i; |
730f2e2c MM |
1011 | |
1012 | cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope)); | |
ce1da96e MM |
1013 | if (!eal) |
1014 | eal = a->eattrs; | |
1015 | for(; eal; eal=eal->next) | |
3991d84e | 1016 | for(i=0; i<eal->count; i++) |
fdf16eb6 | 1017 | ea_show(c, &eal->attrs[i]); |
730f2e2c MM |
1018 | } |
1019 | ||
3ce8c610 MM |
1020 | /** |
1021 | * rta_init - initialize route attribute cache | |
1022 | * | |
1023 | * This function is called during initialization of the routing | |
1024 | * table module to set up the internals of the attribute cache. | |
1025 | */ | |
2326b001 MM |
1026 | void |
1027 | rta_init(void) | |
1028 | { | |
ed68a5c6 | 1029 | rta_pool = rp_new(&root_pool, "Attributes"); |
2326b001 | 1030 | rta_slab = sl_new(rta_pool, sizeof(rta)); |
7e95c05d | 1031 | mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh)); |
ee76a92a | 1032 | rta_alloc_hash(); |
094d2bdb | 1033 | rte_src_init(); |
2326b001 | 1034 | } |
3ce8c610 MM |
1035 | |
1036 | /* | |
1037 | * Documentation for functions declared inline in route.h | |
1038 | */ | |
1039 | #if 0 | |
1040 | ||
1041 | /** | |
1042 | * rta_clone - clone route attributes | |
1043 | * @r: a &rta to be cloned | |
1044 | * | |
1045 | * rta_clone() takes a cached &rta and returns its identical cached | |
1046 | * copy. Currently it works by just returning the original &rta with | |
1047 | * its use count incremented. | |
1048 | */ | |
1049 | static inline rta *rta_clone(rta *r) | |
1050 | { DUMMY; } | |
1051 | ||
1052 | /** | |
1053 | * rta_free - free route attributes | |
1054 | * @r: a &rta to be freed | |
1055 | * | |
1056 | * If you stop using a &rta (for example when deleting a route which uses | |
1057 | * it), you need to call rta_free() to notify the attribute cache the | |
1058 | * attribute is no longer in use and can be freed if you were the last | |
1059 | * user (which rta_free() tests by inspecting the use count). | |
1060 | */ | |
1061 | static inline void rta_free(rta *r) | |
1062 | { DUMMY; } | |
1063 | ||
1064 | #endif |