]>
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" |
2326b001 | 54 | #include "lib/resource.h" |
221135d6 | 55 | #include "lib/string.h" |
2326b001 | 56 | |
acb60628 OZ |
57 | pool *rta_pool; |
58 | ||
2326b001 | 59 | static slab *rta_slab; |
2326b001 | 60 | |
3991d84e MM |
61 | struct protocol *attr_class_to_protocol[EAP_MAX]; |
62 | ||
b77ae37d MM |
63 | /* |
64 | * Extended Attributes | |
65 | */ | |
66 | ||
8d24b689 MM |
67 | static inline eattr * |
68 | ea__find(ea_list *e, unsigned id) | |
b77ae37d MM |
69 | { |
70 | eattr *a; | |
71 | int l, r, m; | |
72 | ||
73 | while (e) | |
74 | { | |
75 | if (e->flags & EALF_BISECT) | |
76 | { | |
77 | l = 0; | |
fee78355 | 78 | r = e->count - 1; |
b77ae37d MM |
79 | while (l <= r) |
80 | { | |
81 | m = (l+r) / 2; | |
82 | a = &e->attrs[m]; | |
83 | if (a->id == id) | |
84 | return a; | |
85 | else if (a->id < id) | |
86 | l = m+1; | |
87 | else | |
88 | r = m-1; | |
89 | } | |
90 | } | |
91 | else | |
92 | for(m=0; m<e->count; m++) | |
93 | if (e->attrs[m].id == id) | |
94 | return &e->attrs[m]; | |
95 | e = e->next; | |
96 | } | |
97 | return NULL; | |
98 | } | |
99 | ||
3ce8c610 MM |
100 | /** |
101 | * ea_find - find an extended attribute | |
102 | * @e: attribute list to search in | |
103 | * @id: attribute ID to search for | |
104 | * | |
105 | * Given an extended attribute list, ea_find() searches for a first | |
2e9b2421 | 106 | * occurrence of an attribute with specified ID, returning either a pointer |
3ce8c610 MM |
107 | * to its &eattr structure or %NULL if no such attribute exists. |
108 | */ | |
8d24b689 MM |
109 | eattr * |
110 | ea_find(ea_list *e, unsigned id) | |
111 | { | |
112 | eattr *a = ea__find(e, id & EA_CODE_MASK); | |
113 | ||
114 | if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF && | |
115 | !(id & EA_ALLOW_UNDEF)) | |
116 | return NULL; | |
117 | return a; | |
118 | } | |
119 | ||
3ce8c610 MM |
120 | /** |
121 | * ea_get_int - fetch an integer attribute | |
122 | * @e: attribute list | |
123 | * @id: attribute ID | |
124 | * @def: default value | |
125 | * | |
126 | * This function is a shortcut for retrieving a value of an integer attribute | |
127 | * by calling ea_find() to find the attribute, extracting its value or returning | |
128 | * a provided default if no such attribute is present. | |
129 | */ | |
c0100454 PM |
130 | int |
131 | ea_get_int(ea_list *e, unsigned id, int def) | |
132 | { | |
133 | eattr *a = ea_find(e, id); | |
134 | if (!a) | |
135 | return def; | |
136 | return a->u.data; | |
137 | } | |
138 | ||
b77ae37d MM |
139 | static inline void |
140 | ea_do_sort(ea_list *e) | |
141 | { | |
142 | unsigned n = e->count; | |
143 | eattr *a = e->attrs; | |
144 | eattr *b = alloca(n * sizeof(eattr)); | |
145 | unsigned s, ss; | |
146 | ||
147 | /* We need to use a stable sorting algorithm, hence mergesort */ | |
148 | do | |
149 | { | |
150 | s = ss = 0; | |
151 | while (s < n) | |
152 | { | |
153 | eattr *p, *q, *lo, *hi; | |
154 | p = b; | |
155 | ss = s; | |
156 | *p++ = a[s++]; | |
157 | while (s < n && p[-1].id <= a[s].id) | |
158 | *p++ = a[s++]; | |
159 | if (s < n) | |
160 | { | |
161 | q = p; | |
162 | *p++ = a[s++]; | |
163 | while (s < n && p[-1].id <= a[s].id) | |
164 | *p++ = a[s++]; | |
165 | lo = b; | |
166 | hi = q; | |
167 | s = ss; | |
168 | while (lo < q && hi < p) | |
169 | if (lo->id <= hi->id) | |
170 | a[s++] = *lo++; | |
171 | else | |
172 | a[s++] = *hi++; | |
173 | while (lo < q) | |
174 | a[s++] = *lo++; | |
175 | while (hi < p) | |
176 | a[s++] = *hi++; | |
177 | } | |
178 | } | |
179 | } | |
180 | while (ss); | |
181 | } | |
182 | ||
183 | static inline void | |
184 | ea_do_prune(ea_list *e) | |
185 | { | |
51a183af | 186 | eattr *s, *d, *l, *s0; |
8d24b689 MM |
187 | int i = 0; |
188 | ||
189 | /* Discard duplicates and undefs. Do you remember sorting was stable? */ | |
190 | s = d = e->attrs; | |
191 | l = e->attrs + e->count; | |
192 | while (s < l) | |
193 | { | |
51a183af MM |
194 | s0 = s++; |
195 | while (s < l && s->id == s[-1].id) | |
196 | s++; | |
197 | /* s0 is the most recent version, s[-1] the oldest one */ | |
198 | if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF) | |
8d24b689 | 199 | { |
51a183af MM |
200 | *d = *s0; |
201 | d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED); | |
202 | d++; | |
8d24b689 MM |
203 | i++; |
204 | } | |
8d24b689 MM |
205 | } |
206 | e->count = i; | |
b77ae37d MM |
207 | } |
208 | ||
3ce8c610 MM |
209 | /** |
210 | * ea_sort - sort an attribute list | |
211 | * @e: list to be sorted | |
212 | * | |
213 | * This function takes a &ea_list chain and sorts the attributes | |
214 | * within each of its entries. | |
215 | * | |
216 | * If an attribute occurs multiple times in a single &ea_list, | |
2e9b2421 | 217 | * ea_sort() leaves only the first (the only significant) occurrence. |
3ce8c610 | 218 | */ |
b77ae37d MM |
219 | void |
220 | ea_sort(ea_list *e) | |
221 | { | |
222 | while (e) | |
223 | { | |
224 | if (!(e->flags & EALF_SORTED)) | |
225 | { | |
226 | ea_do_sort(e); | |
227 | ea_do_prune(e); | |
228 | e->flags |= EALF_SORTED; | |
229 | } | |
b77ae37d | 230 | if (e->count > 5) |
b77ae37d MM |
231 | e->flags |= EALF_BISECT; |
232 | e = e->next; | |
233 | } | |
234 | } | |
235 | ||
3ce8c610 MM |
236 | /** |
237 | * ea_scan - estimate attribute list size | |
238 | * @e: attribute list | |
239 | * | |
240 | * This function calculates an upper bound of the size of | |
241 | * a given &ea_list after merging with ea_merge(). | |
242 | */ | |
b77ae37d MM |
243 | unsigned |
244 | ea_scan(ea_list *e) | |
245 | { | |
246 | unsigned cnt = 0; | |
247 | ||
248 | while (e) | |
249 | { | |
250 | cnt += e->count; | |
251 | e = e->next; | |
252 | } | |
253 | return sizeof(ea_list) + sizeof(eattr)*cnt; | |
254 | } | |
255 | ||
3ce8c610 MM |
256 | /** |
257 | * ea_merge - merge segments of an attribute list | |
258 | * @e: attribute list | |
259 | * @t: buffer to store the result to | |
260 | * | |
261 | * This function takes a possibly multi-segment attribute list | |
262 | * and merges all of its segments to one. | |
263 | * | |
264 | * The primary use of this function is for &ea_list normalization: | |
265 | * first call ea_scan() to determine how much memory will the result | |
266 | * take, then allocate a buffer (usually using alloca()), merge the | |
267 | * segments with ea_merge() and finally sort and prune the result | |
268 | * by calling ea_sort(). | |
269 | */ | |
b77ae37d MM |
270 | void |
271 | ea_merge(ea_list *e, ea_list *t) | |
272 | { | |
273 | eattr *d = t->attrs; | |
274 | ||
275 | t->flags = 0; | |
276 | t->count = 0; | |
277 | t->next = NULL; | |
278 | while (e) | |
279 | { | |
280 | memcpy(d, e->attrs, sizeof(eattr)*e->count); | |
281 | t->count += e->count; | |
282 | d += e->count; | |
283 | e = e->next; | |
284 | } | |
285 | } | |
286 | ||
3ce8c610 MM |
287 | /** |
288 | * ea_same - compare two &ea_list's | |
289 | * @x: attribute list | |
290 | * @y: attribute list | |
291 | * | |
292 | * ea_same() compares two normalized attribute lists @x and @y and returns | |
293 | * 1 if they contain the same attributes, 0 otherwise. | |
294 | */ | |
6f57dcc0 | 295 | int |
2326b001 MM |
296 | ea_same(ea_list *x, ea_list *y) |
297 | { | |
298 | int c; | |
299 | ||
b77ae37d MM |
300 | if (!x || !y) |
301 | return x == y; | |
302 | ASSERT(!x->next && !y->next); | |
303 | if (x->count != y->count) | |
304 | return 0; | |
305 | for(c=0; c<x->count; c++) | |
2326b001 | 306 | { |
b77ae37d MM |
307 | eattr *a = &x->attrs[c]; |
308 | eattr *b = &y->attrs[c]; | |
309 | ||
310 | if (a->id != b->id || | |
311 | a->flags != b->flags || | |
312 | a->type != b->type || | |
313 | ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : | |
d72a0ac2 | 314 | (a->u.ptr->length != b->u.ptr->length || memcmp(a->u.ptr->data, b->u.ptr->data, a->u.ptr->length)))) |
2326b001 | 315 | return 0; |
b77ae37d MM |
316 | } |
317 | return 1; | |
318 | } | |
319 | ||
320 | static inline ea_list * | |
321 | ea_list_copy(ea_list *o) | |
322 | { | |
323 | ea_list *n; | |
324 | unsigned i, len; | |
325 | ||
326 | if (!o) | |
327 | return NULL; | |
328 | ASSERT(!o->next); | |
329 | len = sizeof(ea_list) + sizeof(eattr) * o->count; | |
330 | n = mb_alloc(rta_pool, len); | |
331 | memcpy(n, o, len); | |
332 | n->flags |= EALF_CACHED; | |
333 | for(i=0; i<o->count; i++) | |
334 | { | |
335 | eattr *a = &n->attrs[i]; | |
08732b71 | 336 | if (!(a->type & EAF_EMBEDDED)) |
b77ae37d MM |
337 | { |
338 | unsigned size = sizeof(struct adata) + a->u.ptr->length; | |
339 | struct adata *d = mb_alloc(rta_pool, size); | |
340 | memcpy(d, a->u.ptr, size); | |
341 | a->u.ptr = d; | |
342 | } | |
343 | } | |
344 | return n; | |
345 | } | |
346 | ||
5d86aefb MM |
347 | static inline void |
348 | ea_free(ea_list *o) | |
349 | { | |
3ced9b34 MM |
350 | int i; |
351 | ||
5d86aefb MM |
352 | if (o) |
353 | { | |
354 | ASSERT(!o->next); | |
3ced9b34 MM |
355 | for(i=0; i<o->count; i++) |
356 | { | |
357 | eattr *a = &o->attrs[i]; | |
358 | if (!(a->type & EAF_EMBEDDED)) | |
359 | mb_free(a->u.ptr); | |
360 | } | |
5d86aefb MM |
361 | mb_free(o); |
362 | } | |
363 | } | |
364 | ||
3ce8c610 MM |
365 | /** |
366 | * ea_format - format an &eattr for printing | |
367 | * @e: attribute to be formatted | |
368 | * @buf: destination buffer of size %EA_FORMAT_BUF_SIZE | |
369 | * | |
370 | * This function takes an extended attribute represented by its | |
371 | * &eattr structure and formats it nicely for printing according | |
372 | * to the type information. | |
373 | * | |
374 | * If the protocol defining the attribute provides its own | |
375 | * get_attr() hook, it's consulted first. | |
376 | */ | |
3991d84e MM |
377 | void |
378 | ea_format(eattr *e, byte *buf) | |
379 | { | |
380 | struct protocol *p; | |
381 | int status = GA_UNKNOWN; | |
c6add07f | 382 | unsigned int i; |
3991d84e | 383 | struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr; |
c6add07f | 384 | byte *end = buf + EA_FORMAT_BUF_SIZE - 1; |
3991d84e MM |
385 | |
386 | if (p = attr_class_to_protocol[EA_PROTO(e->id)]) | |
387 | { | |
388 | buf += bsprintf(buf, "%s.", p->name); | |
389 | if (p->get_attr) | |
aebe06b4 | 390 | status = p->get_attr(e, buf, end - buf); |
3991d84e MM |
391 | buf += strlen(buf); |
392 | } | |
393 | else if (EA_PROTO(e->id)) | |
394 | buf += bsprintf(buf, "%02x.", EA_PROTO(e->id)); | |
395 | if (status < GA_NAME) | |
396 | buf += bsprintf(buf, "%02x", EA_ID(e->id)); | |
397 | if (status < GA_FULL) | |
398 | { | |
399 | *buf++ = ':'; | |
400 | *buf++ = ' '; | |
401 | switch (e->type & EAF_TYPE_MASK) | |
402 | { | |
403 | case EAF_TYPE_INT: | |
212ff335 | 404 | bsprintf(buf, "%u", e->u.data); |
3991d84e MM |
405 | break; |
406 | case EAF_TYPE_OPAQUE: | |
1528d30a | 407 | *buf = 0; |
c6add07f | 408 | for(i=0; i<ad->length; i++) |
3991d84e | 409 | { |
c6add07f MM |
410 | if (buf > end - 8) |
411 | { | |
412 | strcpy(buf, " ..."); | |
413 | break; | |
414 | } | |
415 | if (i) | |
3991d84e | 416 | *buf++ = ' '; |
c6add07f | 417 | buf += bsprintf(buf, "%02x", ad->data[i]); |
3991d84e | 418 | } |
3991d84e MM |
419 | break; |
420 | case EAF_TYPE_IP_ADDRESS: | |
421 | bsprintf(buf, "%I", *(ip_addr *) ad->data); | |
422 | break; | |
423 | case EAF_TYPE_ROUTER_ID: | |
2f6483cd | 424 | bsprintf(buf, "%R", e->u.data); |
3991d84e | 425 | break; |
c6add07f MM |
426 | case EAF_TYPE_AS_PATH: |
427 | as_path_format(ad, buf, end - buf); | |
428 | break; | |
429 | case EAF_TYPE_INT_SET: | |
aebe06b4 | 430 | int_set_format(ad, 1, buf, end - buf); |
c6add07f | 431 | break; |
3991d84e MM |
432 | case EAF_TYPE_UNDEF: |
433 | default: | |
434 | bsprintf(buf, "<type %02x>", e->type); | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
3ce8c610 MM |
439 | /** |
440 | * ea_dump - dump an extended attribute | |
441 | * @e: attribute to be dumped | |
442 | * | |
443 | * ea_dump() dumps contents of the extended attribute given to | |
444 | * the debug output. | |
445 | */ | |
b77ae37d MM |
446 | void |
447 | ea_dump(ea_list *e) | |
448 | { | |
449 | int i; | |
450 | ||
451 | if (!e) | |
452 | { | |
453 | debug("NONE"); | |
454 | return; | |
455 | } | |
456 | while (e) | |
457 | { | |
458 | debug("[%c%c%c]", | |
459 | (e->flags & EALF_SORTED) ? 'S' : 's', | |
460 | (e->flags & EALF_BISECT) ? 'B' : 'b', | |
461 | (e->flags & EALF_CACHED) ? 'C' : 'c'); | |
462 | for(i=0; i<e->count; i++) | |
2326b001 | 463 | { |
b77ae37d MM |
464 | eattr *a = &e->attrs[i]; |
465 | debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags); | |
9f4929e7 MM |
466 | if (a->type & EAF_TEMP) |
467 | debug("T"); | |
b77ae37d | 468 | debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]); |
51a183af MM |
469 | if (a->type & EAF_ORIGINATED) |
470 | debug("o"); | |
b77ae37d MM |
471 | if (a->type & EAF_EMBEDDED) |
472 | debug(":%08x", a->u.data); | |
473 | else | |
474 | { | |
475 | int j, len = a->u.ptr->length; | |
476 | debug("[%d]:", len); | |
477 | for(j=0; j<len; j++) | |
478 | debug("%02x", a->u.ptr->data[j]); | |
479 | } | |
2326b001 | 480 | } |
b77ae37d MM |
481 | if (e = e->next) |
482 | debug(" | "); | |
2326b001 | 483 | } |
2326b001 MM |
484 | } |
485 | ||
3ce8c610 MM |
486 | /** |
487 | * ea_hash - calculate an &ea_list hash key | |
488 | * @e: attribute list | |
489 | * | |
490 | * ea_hash() takes an extended attribute list and calculated a hopefully | |
491 | * uniformly distributed hash value from its contents. | |
492 | */ | |
6f57dcc0 | 493 | inline unsigned int |
ee76a92a MM |
494 | ea_hash(ea_list *e) |
495 | { | |
496 | u32 h = 0; | |
497 | int i; | |
498 | ||
499 | if (e) /* Assuming chain of length 1 */ | |
500 | { | |
501 | for(i=0; i<e->count; i++) | |
502 | { | |
503 | struct eattr *a = &e->attrs[i]; | |
504 | h ^= a->id; | |
505 | if (a->type & EAF_EMBEDDED) | |
506 | h ^= a->u.data; | |
507 | else | |
508 | { | |
509 | struct adata *d = a->u.ptr; | |
510 | int size = d->length; | |
511 | byte *z = d->data; | |
512 | while (size >= 4) | |
513 | { | |
514 | h ^= *(u32 *)z; | |
515 | z += 4; | |
516 | size -= 4; | |
517 | } | |
518 | while (size--) | |
519 | h = (h >> 24) ^ (h << 8) ^ *z++; | |
520 | } | |
521 | } | |
522 | h ^= h >> 16; | |
523 | h ^= h >> 6; | |
524 | h &= 0xffff; | |
525 | } | |
526 | return h; | |
527 | } | |
528 | ||
3ce8c610 MM |
529 | /** |
530 | * ea_append - concatenate &ea_list's | |
531 | * @to: destination list (can be %NULL) | |
532 | * @what: list to be appended (can be %NULL) | |
533 | * | |
534 | * This function appends the &ea_list @what at the end of | |
535 | * &ea_list @to and returns a pointer to the resulting list. | |
536 | */ | |
ce1da96e MM |
537 | ea_list * |
538 | ea_append(ea_list *to, ea_list *what) | |
539 | { | |
540 | ea_list *res; | |
541 | ||
542 | if (!to) | |
543 | return what; | |
544 | res = to; | |
545 | while (to->next) | |
546 | to = to->next; | |
547 | to->next = what; | |
548 | return res; | |
549 | } | |
550 | ||
b77ae37d MM |
551 | /* |
552 | * rta's | |
553 | */ | |
554 | ||
ee76a92a MM |
555 | static unsigned int rta_cache_count; |
556 | static unsigned int rta_cache_size = 32; | |
557 | static unsigned int rta_cache_limit; | |
558 | static unsigned int rta_cache_mask; | |
559 | static rta **rta_hash_table; | |
560 | ||
561 | static void | |
562 | rta_alloc_hash(void) | |
563 | { | |
5d86aefb | 564 | rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size); |
ee76a92a MM |
565 | if (rta_cache_size < 32768) |
566 | rta_cache_limit = rta_cache_size * 2; | |
567 | else | |
568 | rta_cache_limit = ~0; | |
569 | rta_cache_mask = rta_cache_size - 1; | |
570 | } | |
571 | ||
572 | static inline unsigned int | |
573 | rta_hash(rta *a) | |
574 | { | |
d0126f0b | 575 | return (a->proto->hash_key ^ ipa_hash(a->gw) ^ ea_hash(a->eattrs)) & 0xffff; |
ee76a92a MM |
576 | } |
577 | ||
2326b001 MM |
578 | static inline int |
579 | rta_same(rta *x, rta *y) | |
580 | { | |
581 | return (x->proto == y->proto && | |
582 | x->source == y->source && | |
583 | x->scope == y->scope && | |
584 | x->cast == y->cast && | |
585 | x->dest == y->dest && | |
2326b001 MM |
586 | x->flags == y->flags && |
587 | ipa_equal(x->gw, y->gw) && | |
588 | ipa_equal(x->from, y->from) && | |
589 | x->iface == y->iface && | |
2727bb7c | 590 | ea_same(x->eattrs, y->eattrs)); |
2326b001 MM |
591 | } |
592 | ||
593 | static rta * | |
594 | rta_copy(rta *o) | |
595 | { | |
596 | rta *r = sl_alloc(rta_slab); | |
597 | ||
598 | memcpy(r, o, sizeof(rta)); | |
599 | r->uc = 1; | |
2727bb7c | 600 | r->eattrs = ea_list_copy(o->eattrs); |
2326b001 MM |
601 | return r; |
602 | } | |
603 | ||
ee76a92a MM |
604 | static inline void |
605 | rta_insert(rta *r) | |
606 | { | |
607 | unsigned int h = r->hash_key & rta_cache_mask; | |
608 | r->next = rta_hash_table[h]; | |
609 | if (r->next) | |
610 | r->next->pprev = &r->next; | |
611 | r->pprev = &rta_hash_table[h]; | |
612 | rta_hash_table[h] = r; | |
613 | } | |
614 | ||
615 | static void | |
616 | rta_rehash(void) | |
617 | { | |
618 | unsigned int ohs = rta_cache_size; | |
619 | unsigned int h; | |
620 | rta *r, *n; | |
621 | rta **oht = rta_hash_table; | |
622 | ||
623 | rta_cache_size = 2*rta_cache_size; | |
624 | DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size); | |
625 | rta_alloc_hash(); | |
626 | for(h=0; h<ohs; h++) | |
627 | for(r=oht[h]; r; r=n) | |
628 | { | |
629 | n = r->next; | |
630 | rta_insert(r); | |
631 | } | |
632 | mb_free(oht); | |
633 | } | |
634 | ||
3ce8c610 MM |
635 | /** |
636 | * rta_lookup - look up a &rta in attribute cache | |
2e9b2421 | 637 | * @o: a un-cached &rta |
3ce8c610 | 638 | * |
2e9b2421 | 639 | * rta_lookup() gets an un-cached &rta structure and returns its cached |
3ce8c610 MM |
640 | * counterpart. It starts with examining the attribute cache to see whether |
641 | * there exists a matching entry. If such an entry exists, it's returned and | |
642 | * its use count is incremented, else a new entry is created with use count | |
643 | * set to 1. | |
644 | * | |
645 | * The extended attribute lists attached to the &rta are automatically | |
646 | * converted to the normalized form. | |
647 | */ | |
2326b001 MM |
648 | rta * |
649 | rta_lookup(rta *o) | |
650 | { | |
651 | rta *r; | |
ee76a92a | 652 | unsigned int h; |
2326b001 | 653 | |
b77ae37d | 654 | ASSERT(!(o->aflags & RTAF_CACHED)); |
2727bb7c | 655 | if (o->eattrs) |
b77ae37d | 656 | { |
2727bb7c | 657 | if (o->eattrs->next) /* Multiple ea_list's, need to merge them */ |
b77ae37d | 658 | { |
2727bb7c MM |
659 | ea_list *ml = alloca(ea_scan(o->eattrs)); |
660 | ea_merge(o->eattrs, ml); | |
661 | o->eattrs = ml; | |
b77ae37d | 662 | } |
2727bb7c | 663 | ea_sort(o->eattrs); |
b77ae37d MM |
664 | } |
665 | ||
ee76a92a MM |
666 | h = rta_hash(o); |
667 | for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next) | |
668 | if (r->hash_key == h && rta_same(r, o)) | |
2326b001 | 669 | return rta_clone(r); |
b77ae37d | 670 | |
2326b001 | 671 | r = rta_copy(o); |
ee76a92a | 672 | r->hash_key = h; |
04925e90 | 673 | r->aflags = RTAF_CACHED; |
ee76a92a MM |
674 | rta_insert(r); |
675 | ||
676 | if (++rta_cache_count > rta_cache_limit) | |
677 | rta_rehash(); | |
678 | ||
2326b001 MM |
679 | return r; |
680 | } | |
681 | ||
682 | void | |
b77ae37d | 683 | rta__free(rta *a) |
2326b001 | 684 | { |
ee76a92a MM |
685 | ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED)); |
686 | rta_cache_count--; | |
5d86aefb MM |
687 | *a->pprev = a->next; |
688 | if (a->next) | |
689 | a->next->pprev = a->pprev; | |
690 | a->aflags = 0; /* Poison the entry */ | |
691 | ea_free(a->eattrs); | |
692 | sl_free(rta_slab, a); | |
2326b001 MM |
693 | } |
694 | ||
3ce8c610 MM |
695 | /** |
696 | * rta_dump - dump route attributes | |
697 | * @a: attribute structure to dump | |
698 | * | |
699 | * This function takes a &rta and dumps its contents to the debug output. | |
700 | */ | |
2326b001 | 701 | void |
66e53309 | 702 | rta_dump(rta *a) |
2326b001 | 703 | { |
618533af | 704 | static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE", |
beaf86e1 | 705 | "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP", |
98ac6176 OF |
706 | "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1", |
707 | "RTS_OSPF_EXT2", "RTS_BGP" }; | |
66e53309 MM |
708 | static char *rtc[] = { "", " BC", " MC", " AC" }; |
709 | static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" }; | |
710 | ||
ee76a92a | 711 | debug("p=%s uc=%d %s %s%s%s h=%04x", |
730f2e2c | 712 | a->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast], |
ee76a92a | 713 | rtd[a->dest], a->hash_key); |
04925e90 MM |
714 | if (!(a->aflags & RTAF_CACHED)) |
715 | debug(" !CACHED"); | |
962ba482 | 716 | debug(" <-%I", a->from); |
66e53309 | 717 | if (a->dest == RTD_ROUTER) |
962ba482 | 718 | debug(" ->%I", a->gw); |
66e53309 | 719 | if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER) |
48b41d58 | 720 | debug(" [%s]", a->iface ? a->iface->name : "???" ); |
2727bb7c | 721 | if (a->eattrs) |
b77ae37d MM |
722 | { |
723 | debug(" EA: "); | |
2727bb7c | 724 | ea_dump(a->eattrs); |
b77ae37d | 725 | } |
2326b001 MM |
726 | } |
727 | ||
3ce8c610 MM |
728 | /** |
729 | * rta_dump_all - dump attribute cache | |
730 | * | |
731 | * This function dumps the whole contents of route attribute cache | |
732 | * to the debug output. | |
733 | */ | |
2326b001 MM |
734 | void |
735 | rta_dump_all(void) | |
736 | { | |
66e53309 | 737 | rta *a; |
ee76a92a MM |
738 | unsigned int h; |
739 | ||
740 | debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit); | |
741 | for(h=0; h<rta_cache_size; h++) | |
742 | for(a=rta_hash_table[h]; a; a=a->next) | |
743 | { | |
744 | debug("%p ", a); | |
745 | rta_dump(a); | |
746 | debug("\n"); | |
747 | } | |
66e53309 | 748 | debug("\n"); |
2326b001 MM |
749 | } |
750 | ||
730f2e2c | 751 | void |
ce1da96e | 752 | rta_show(struct cli *c, rta *a, ea_list *eal) |
730f2e2c MM |
753 | { |
754 | static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect", | |
891cec85 | 755 | "RIP", "OSPF", "OSPF-ext", "OSPF-IA", "OSPF-boundary", "BGP" }; |
730f2e2c | 756 | static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" }; |
3991d84e | 757 | int i; |
c6add07f | 758 | byte buf[EA_FORMAT_BUF_SIZE]; |
730f2e2c MM |
759 | |
760 | cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope)); | |
ce1da96e MM |
761 | if (!eal) |
762 | eal = a->eattrs; | |
763 | for(; eal; eal=eal->next) | |
3991d84e MM |
764 | for(i=0; i<eal->count; i++) |
765 | { | |
766 | ea_format(&eal->attrs[i], buf); | |
767 | cli_printf(c, -1012, "\t%s", buf); | |
768 | } | |
730f2e2c MM |
769 | } |
770 | ||
3ce8c610 MM |
771 | /** |
772 | * rta_init - initialize route attribute cache | |
773 | * | |
774 | * This function is called during initialization of the routing | |
775 | * table module to set up the internals of the attribute cache. | |
776 | */ | |
2326b001 MM |
777 | void |
778 | rta_init(void) | |
779 | { | |
ed68a5c6 | 780 | rta_pool = rp_new(&root_pool, "Attributes"); |
2326b001 | 781 | rta_slab = sl_new(rta_pool, sizeof(rta)); |
ee76a92a | 782 | rta_alloc_hash(); |
2326b001 | 783 | } |
3ce8c610 MM |
784 | |
785 | /* | |
786 | * Documentation for functions declared inline in route.h | |
787 | */ | |
788 | #if 0 | |
789 | ||
790 | /** | |
791 | * rta_clone - clone route attributes | |
792 | * @r: a &rta to be cloned | |
793 | * | |
794 | * rta_clone() takes a cached &rta and returns its identical cached | |
795 | * copy. Currently it works by just returning the original &rta with | |
796 | * its use count incremented. | |
797 | */ | |
798 | static inline rta *rta_clone(rta *r) | |
799 | { DUMMY; } | |
800 | ||
801 | /** | |
802 | * rta_free - free route attributes | |
803 | * @r: a &rta to be freed | |
804 | * | |
805 | * If you stop using a &rta (for example when deleting a route which uses | |
806 | * it), you need to call rta_free() to notify the attribute cache the | |
807 | * attribute is no longer in use and can be freed if you were the last | |
808 | * user (which rta_free() tests by inspecting the use count). | |
809 | */ | |
810 | static inline void rta_free(rta *r) | |
811 | { DUMMY; } | |
812 | ||
813 | #endif |