]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/rt-attr.c
Moving of mulipath merging code from OSPF to nest
[thirdparty/bird.git] / nest / rt-attr.c
1 /*
2 * BIRD -- Route Attribute Cache
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
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
24 * in the &ea_list chain and in such case the first occurrence overrides
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 *
36 * There exist two variants of &rta's -- cached and un-cached ones. Un-cached
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
47 #include "nest/bird.h"
48 #include "nest/route.h"
49 #include "nest/protocol.h"
50 #include "nest/iface.h"
51 #include "nest/cli.h"
52 #include "nest/attrs.h"
53 #include "lib/alloca.h"
54 #include "lib/hash.h"
55 #include "lib/resource.h"
56 #include "lib/string.h"
57
58 pool *rta_pool;
59
60 static slab *rta_slab;
61 static slab *mpnh_slab;
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;
67 #define SRC_ID_INIT_SIZE 4
68
69 /* rte source hash */
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;
81
82 struct protocol *attr_class_to_protocol[EAP_MAX];
83
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;
91 src_id_size = SRC_ID_INIT_SIZE;
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
98 HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
99 }
100
101 static inline int u32_cto(uint 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;
115 src_ids = mb_realloc(src_ids, src_id_size * sizeof(u32));
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
148
149 HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
150
151 struct rte_src *
152 rt_find_source(struct proto *p, u32 id)
153 {
154 return HASH_FIND(src_hash, RSH, p, id);
155 }
156
157 struct rte_src *
158 rt_get_source(struct proto *p, u32 id)
159 {
160 struct rte_src *src = rt_find_source(p, id);
161
162 if (src)
163 return src;
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;
170
171 HASH_INSERT2(src_hash, RSH, rta_pool, src);
172
173 return src;
174 }
175
176 void
177 rt_prune_sources(void)
178 {
179 HASH_WALK_FILTER(src_hash, next, src, sp)
180 {
181 if (src->uc == 0)
182 {
183 HASH_DO_REMOVE(src_hash, RSH, sp);
184 rte_src_free_id(src->global_id);
185 sl_free(rte_src_slab, src);
186 }
187 }
188 HASH_WALK_FILTER_END;
189
190 HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
191 }
192
193
194 /*
195 * Multipath Next Hop
196 */
197
198 static inline uint
199 mpnh_hash(struct mpnh *x)
200 {
201 uint 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 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
305
306 static struct mpnh *
307 mpnh_copy(struct mpnh *o)
308 {
309 struct mpnh *first = NULL;
310 struct mpnh **last = &first;
311
312 for (; o; o = o->next)
313 {
314 struct mpnh *n = sl_alloc(mpnh_slab);
315 n->gw = o->gw;
316 n->iface = o->iface;
317 n->next = NULL;
318 n->weight = o->weight;
319
320 *last = n;
321 last = &(n->next);
322 }
323
324 return first;
325 }
326
327 static void
328 mpnh_free(struct mpnh *o)
329 {
330 struct mpnh *n;
331
332 while (o)
333 {
334 n = o->next;
335 sl_free(mpnh_slab, o);
336 o = n;
337 }
338 }
339
340
341 /*
342 * Extended Attributes
343 */
344
345 static inline eattr *
346 ea__find(ea_list *e, unsigned id)
347 {
348 eattr *a;
349 int l, r, m;
350
351 while (e)
352 {
353 if (e->flags & EALF_BISECT)
354 {
355 l = 0;
356 r = e->count - 1;
357 while (l <= r)
358 {
359 m = (l+r) / 2;
360 a = &e->attrs[m];
361 if (a->id == id)
362 return a;
363 else if (a->id < id)
364 l = m+1;
365 else
366 r = m-1;
367 }
368 }
369 else
370 for(m=0; m<e->count; m++)
371 if (e->attrs[m].id == id)
372 return &e->attrs[m];
373 e = e->next;
374 }
375 return NULL;
376 }
377
378 /**
379 * ea_find - find an extended attribute
380 * @e: attribute list to search in
381 * @id: attribute ID to search for
382 *
383 * Given an extended attribute list, ea_find() searches for a first
384 * occurrence of an attribute with specified ID, returning either a pointer
385 * to its &eattr structure or %NULL if no such attribute exists.
386 */
387 eattr *
388 ea_find(ea_list *e, unsigned id)
389 {
390 eattr *a = ea__find(e, id & EA_CODE_MASK);
391
392 if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
393 !(id & EA_ALLOW_UNDEF))
394 return NULL;
395 return a;
396 }
397
398 /**
399 * ea_walk - walk through extended attributes
400 * @s: walk state structure
401 * @id: start of attribute ID interval
402 * @max: length of attribute ID interval
403 *
404 * Given an extended attribute list, ea_walk() walks through the list looking
405 * for first occurrences of attributes with ID in specified interval from @id to
406 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
407 * walk state in @s for subsequent calls.
408
409 * The function ea_walk() is supposed to be called in a loop, with initially
410 * zeroed walk state structure @s with filled the initial extended attribute
411 * list, returning one found attribute in each call or %NULL when no other
412 * attribute exists. The extended attribute list or the arguments should not be
413 * modified between calls. The maximum value of @max is 128.
414 */
415 eattr *
416 ea_walk(struct ea_walk_state *s, uint id, uint max)
417 {
418 ea_list *e = s->eattrs;
419 eattr *a = s->ea;
420 eattr *a_max;
421
422 max = id + max;
423
424 if (a)
425 goto step;
426
427 for (; e; e = e->next)
428 {
429 if (e->flags & EALF_BISECT)
430 {
431 int l, r, m;
432
433 l = 0;
434 r = e->count - 1;
435 while (l < r)
436 {
437 m = (l+r) / 2;
438 if (e->attrs[m].id < id)
439 l = m + 1;
440 else
441 r = m;
442 }
443 a = e->attrs + l;
444 }
445 else
446 a = e->attrs;
447
448 step:
449 a_max = e->attrs + e->count;
450 for (; a < a_max; a++)
451 if ((a->id >= id) && (a->id < max))
452 {
453 int n = a->id - id;
454
455 if (BIT32_TEST(s->visited, n))
456 continue;
457
458 BIT32_SET(s->visited, n);
459
460 if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
461 continue;
462
463 s->eattrs = e;
464 s->ea = a;
465 return a;
466 }
467 else if (e->flags & EALF_BISECT)
468 break;
469 }
470
471 return NULL;
472 }
473
474 /**
475 * ea_get_int - fetch an integer attribute
476 * @e: attribute list
477 * @id: attribute ID
478 * @def: default value
479 *
480 * This function is a shortcut for retrieving a value of an integer attribute
481 * by calling ea_find() to find the attribute, extracting its value or returning
482 * a provided default if no such attribute is present.
483 */
484 int
485 ea_get_int(ea_list *e, unsigned id, int def)
486 {
487 eattr *a = ea_find(e, id);
488 if (!a)
489 return def;
490 return a->u.data;
491 }
492
493 static inline void
494 ea_do_sort(ea_list *e)
495 {
496 unsigned n = e->count;
497 eattr *a = e->attrs;
498 eattr *b = alloca(n * sizeof(eattr));
499 unsigned s, ss;
500
501 /* We need to use a stable sorting algorithm, hence mergesort */
502 do
503 {
504 s = ss = 0;
505 while (s < n)
506 {
507 eattr *p, *q, *lo, *hi;
508 p = b;
509 ss = s;
510 *p++ = a[s++];
511 while (s < n && p[-1].id <= a[s].id)
512 *p++ = a[s++];
513 if (s < n)
514 {
515 q = p;
516 *p++ = a[s++];
517 while (s < n && p[-1].id <= a[s].id)
518 *p++ = a[s++];
519 lo = b;
520 hi = q;
521 s = ss;
522 while (lo < q && hi < p)
523 if (lo->id <= hi->id)
524 a[s++] = *lo++;
525 else
526 a[s++] = *hi++;
527 while (lo < q)
528 a[s++] = *lo++;
529 while (hi < p)
530 a[s++] = *hi++;
531 }
532 }
533 }
534 while (ss);
535 }
536
537 static inline void
538 ea_do_prune(ea_list *e)
539 {
540 eattr *s, *d, *l, *s0;
541 int i = 0;
542
543 /* Discard duplicates and undefs. Do you remember sorting was stable? */
544 s = d = e->attrs;
545 l = e->attrs + e->count;
546 while (s < l)
547 {
548 s0 = s++;
549 while (s < l && s->id == s[-1].id)
550 s++;
551 /* s0 is the most recent version, s[-1] the oldest one */
552 if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
553 {
554 *d = *s0;
555 d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED);
556 d++;
557 i++;
558 }
559 }
560 e->count = i;
561 }
562
563 /**
564 * ea_sort - sort an attribute list
565 * @e: list to be sorted
566 *
567 * This function takes a &ea_list chain and sorts the attributes
568 * within each of its entries.
569 *
570 * If an attribute occurs multiple times in a single &ea_list,
571 * ea_sort() leaves only the first (the only significant) occurrence.
572 */
573 void
574 ea_sort(ea_list *e)
575 {
576 while (e)
577 {
578 if (!(e->flags & EALF_SORTED))
579 {
580 ea_do_sort(e);
581 ea_do_prune(e);
582 e->flags |= EALF_SORTED;
583 }
584 if (e->count > 5)
585 e->flags |= EALF_BISECT;
586 e = e->next;
587 }
588 }
589
590 /**
591 * ea_scan - estimate attribute list size
592 * @e: attribute list
593 *
594 * This function calculates an upper bound of the size of
595 * a given &ea_list after merging with ea_merge().
596 */
597 unsigned
598 ea_scan(ea_list *e)
599 {
600 unsigned cnt = 0;
601
602 while (e)
603 {
604 cnt += e->count;
605 e = e->next;
606 }
607 return sizeof(ea_list) + sizeof(eattr)*cnt;
608 }
609
610 /**
611 * ea_merge - merge segments of an attribute list
612 * @e: attribute list
613 * @t: buffer to store the result to
614 *
615 * This function takes a possibly multi-segment attribute list
616 * and merges all of its segments to one.
617 *
618 * The primary use of this function is for &ea_list normalization:
619 * first call ea_scan() to determine how much memory will the result
620 * take, then allocate a buffer (usually using alloca()), merge the
621 * segments with ea_merge() and finally sort and prune the result
622 * by calling ea_sort().
623 */
624 void
625 ea_merge(ea_list *e, ea_list *t)
626 {
627 eattr *d = t->attrs;
628
629 t->flags = 0;
630 t->count = 0;
631 t->next = NULL;
632 while (e)
633 {
634 memcpy(d, e->attrs, sizeof(eattr)*e->count);
635 t->count += e->count;
636 d += e->count;
637 e = e->next;
638 }
639 }
640
641 /**
642 * ea_same - compare two &ea_list's
643 * @x: attribute list
644 * @y: attribute list
645 *
646 * ea_same() compares two normalized attribute lists @x and @y and returns
647 * 1 if they contain the same attributes, 0 otherwise.
648 */
649 int
650 ea_same(ea_list *x, ea_list *y)
651 {
652 int c;
653
654 if (!x || !y)
655 return x == y;
656 ASSERT(!x->next && !y->next);
657 if (x->count != y->count)
658 return 0;
659 for(c=0; c<x->count; c++)
660 {
661 eattr *a = &x->attrs[c];
662 eattr *b = &y->attrs[c];
663
664 if (a->id != b->id ||
665 a->flags != b->flags ||
666 a->type != b->type ||
667 ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
668 return 0;
669 }
670 return 1;
671 }
672
673 static inline ea_list *
674 ea_list_copy(ea_list *o)
675 {
676 ea_list *n;
677 unsigned i, len;
678
679 if (!o)
680 return NULL;
681 ASSERT(!o->next);
682 len = sizeof(ea_list) + sizeof(eattr) * o->count;
683 n = mb_alloc(rta_pool, len);
684 memcpy(n, o, len);
685 n->flags |= EALF_CACHED;
686 for(i=0; i<o->count; i++)
687 {
688 eattr *a = &n->attrs[i];
689 if (!(a->type & EAF_EMBEDDED))
690 {
691 unsigned size = sizeof(struct adata) + a->u.ptr->length;
692 struct adata *d = mb_alloc(rta_pool, size);
693 memcpy(d, a->u.ptr, size);
694 a->u.ptr = d;
695 }
696 }
697 return n;
698 }
699
700 static inline void
701 ea_free(ea_list *o)
702 {
703 int i;
704
705 if (o)
706 {
707 ASSERT(!o->next);
708 for(i=0; i<o->count; i++)
709 {
710 eattr *a = &o->attrs[i];
711 if (!(a->type & EAF_EMBEDDED))
712 mb_free(a->u.ptr);
713 }
714 mb_free(o);
715 }
716 }
717
718 static int
719 get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
720 {
721 if (a->id == EA_GEN_IGP_METRIC)
722 {
723 *buf += bsprintf(*buf, "igp_metric");
724 return GA_NAME;
725 }
726
727 return GA_UNKNOWN;
728 }
729
730 void
731 ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
732 {
733 byte *bound = buf + bufsize - 32;
734 u32 data = a->u.data;
735 int i;
736
737 for (i = min; i < max; i++)
738 if ((data & (1u << i)) && names[i])
739 {
740 if (buf > bound)
741 {
742 strcpy(buf, " ...");
743 return;
744 }
745
746 buf += bsprintf(buf, " %s", names[i]);
747 data &= ~(1u << i);
748 }
749
750 if (data)
751 bsprintf(buf, " %08x", data);
752
753 return;
754 }
755
756 static inline void
757 opaque_format(struct adata *ad, byte *buf, uint size)
758 {
759 byte *bound = buf + size - 10;
760 int i;
761
762 for(i = 0; i < ad->length; i++)
763 {
764 if (buf > bound)
765 {
766 strcpy(buf, " ...");
767 return;
768 }
769 if (i)
770 *buf++ = ' ';
771
772 buf += bsprintf(buf, "%02x", ad->data[i]);
773 }
774
775 *buf = 0;
776 return;
777 }
778
779 static inline void
780 ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
781 {
782 int i = int_set_format(ad, way, 0, pos, end - pos);
783 cli_printf(c, -1012, "\t%s", buf);
784 while (i)
785 {
786 i = int_set_format(ad, way, i, buf, end - buf - 1);
787 cli_printf(c, -1012, "\t\t%s", buf);
788 }
789 }
790
791 static inline void
792 ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
793 {
794 int i = ec_set_format(ad, 0, pos, end - pos);
795 cli_printf(c, -1012, "\t%s", buf);
796 while (i)
797 {
798 i = ec_set_format(ad, i, buf, end - buf - 1);
799 cli_printf(c, -1012, "\t\t%s", buf);
800 }
801 }
802
803 /**
804 * ea_show - print an &eattr to CLI
805 * @c: destination CLI
806 * @e: attribute to be printed
807 *
808 * This function takes an extended attribute represented by its &eattr
809 * structure and prints it to the CLI according to the type information.
810 *
811 * If the protocol defining the attribute provides its own
812 * get_attr() hook, it's consulted first.
813 */
814 void
815 ea_show(struct cli *c, eattr *e)
816 {
817 struct protocol *p;
818 int status = GA_UNKNOWN;
819 struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
820 byte buf[CLI_MSG_SIZE];
821 byte *pos = buf, *end = buf + sizeof(buf);
822
823 if (p = attr_class_to_protocol[EA_PROTO(e->id)])
824 {
825 pos += bsprintf(pos, "%s.", p->name);
826 if (p->get_attr)
827 status = p->get_attr(e, pos, end - pos);
828 pos += strlen(pos);
829 }
830 else if (EA_PROTO(e->id))
831 pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
832 else
833 status = get_generic_attr(e, &pos, end - pos);
834
835 if (status < GA_NAME)
836 pos += bsprintf(pos, "%02x", EA_ID(e->id));
837 if (status < GA_FULL)
838 {
839 *pos++ = ':';
840 *pos++ = ' ';
841 switch (e->type & EAF_TYPE_MASK)
842 {
843 case EAF_TYPE_INT:
844 bsprintf(pos, "%u", e->u.data);
845 break;
846 case EAF_TYPE_OPAQUE:
847 opaque_format(ad, pos, end - pos);
848 break;
849 case EAF_TYPE_IP_ADDRESS:
850 bsprintf(pos, "%I", *(ip_addr *) ad->data);
851 break;
852 case EAF_TYPE_ROUTER_ID:
853 bsprintf(pos, "%R", e->u.data);
854 break;
855 case EAF_TYPE_AS_PATH:
856 as_path_format(ad, pos, end - pos);
857 break;
858 case EAF_TYPE_BITFIELD:
859 bsprintf(pos, "%08x", e->u.data);
860 break;
861 case EAF_TYPE_INT_SET:
862 ea_show_int_set(c, ad, 1, pos, buf, end);
863 return;
864 case EAF_TYPE_EC_SET:
865 ea_show_ec_set(c, ad, pos, buf, end);
866 return;
867 case EAF_TYPE_UNDEF:
868 default:
869 bsprintf(pos, "<type %02x>", e->type);
870 }
871 }
872 cli_printf(c, -1012, "\t%s", buf);
873 }
874
875 /**
876 * ea_dump - dump an extended attribute
877 * @e: attribute to be dumped
878 *
879 * ea_dump() dumps contents of the extended attribute given to
880 * the debug output.
881 */
882 void
883 ea_dump(ea_list *e)
884 {
885 int i;
886
887 if (!e)
888 {
889 debug("NONE");
890 return;
891 }
892 while (e)
893 {
894 debug("[%c%c%c]",
895 (e->flags & EALF_SORTED) ? 'S' : 's',
896 (e->flags & EALF_BISECT) ? 'B' : 'b',
897 (e->flags & EALF_CACHED) ? 'C' : 'c');
898 for(i=0; i<e->count; i++)
899 {
900 eattr *a = &e->attrs[i];
901 debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
902 if (a->type & EAF_TEMP)
903 debug("T");
904 debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
905 if (a->type & EAF_ORIGINATED)
906 debug("o");
907 if (a->type & EAF_EMBEDDED)
908 debug(":%08x", a->u.data);
909 else
910 {
911 int j, len = a->u.ptr->length;
912 debug("[%d]:", len);
913 for(j=0; j<len; j++)
914 debug("%02x", a->u.ptr->data[j]);
915 }
916 }
917 if (e = e->next)
918 debug(" | ");
919 }
920 }
921
922 /**
923 * ea_hash - calculate an &ea_list hash key
924 * @e: attribute list
925 *
926 * ea_hash() takes an extended attribute list and calculated a hopefully
927 * uniformly distributed hash value from its contents.
928 */
929 inline uint
930 ea_hash(ea_list *e)
931 {
932 u32 h = 0;
933 int i;
934
935 if (e) /* Assuming chain of length 1 */
936 {
937 for(i=0; i<e->count; i++)
938 {
939 struct eattr *a = &e->attrs[i];
940 h ^= a->id;
941 if (a->type & EAF_EMBEDDED)
942 h ^= a->u.data;
943 else
944 {
945 struct adata *d = a->u.ptr;
946 int size = d->length;
947 byte *z = d->data;
948 while (size >= 4)
949 {
950 h ^= *(u32 *)z;
951 z += 4;
952 size -= 4;
953 }
954 while (size--)
955 h = (h >> 24) ^ (h << 8) ^ *z++;
956 }
957 }
958 h ^= h >> 16;
959 h ^= h >> 6;
960 h &= 0xffff;
961 }
962 return h;
963 }
964
965 /**
966 * ea_append - concatenate &ea_list's
967 * @to: destination list (can be %NULL)
968 * @what: list to be appended (can be %NULL)
969 *
970 * This function appends the &ea_list @what at the end of
971 * &ea_list @to and returns a pointer to the resulting list.
972 */
973 ea_list *
974 ea_append(ea_list *to, ea_list *what)
975 {
976 ea_list *res;
977
978 if (!to)
979 return what;
980 res = to;
981 while (to->next)
982 to = to->next;
983 to->next = what;
984 return res;
985 }
986
987 /*
988 * rta's
989 */
990
991 static uint rta_cache_count;
992 static uint rta_cache_size = 32;
993 static uint rta_cache_limit;
994 static uint rta_cache_mask;
995 static rta **rta_hash_table;
996
997 static void
998 rta_alloc_hash(void)
999 {
1000 rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
1001 if (rta_cache_size < 32768)
1002 rta_cache_limit = rta_cache_size * 2;
1003 else
1004 rta_cache_limit = ~0;
1005 rta_cache_mask = rta_cache_size - 1;
1006 }
1007
1008 static inline uint
1009 rta_hash(rta *a)
1010 {
1011 return (((uint) (uintptr_t) a->src) ^ ipa_hash(a->gw) ^
1012 mpnh_hash(a->nexthops) ^ ea_hash(a->eattrs)) & 0xffff;
1013 }
1014
1015 static inline int
1016 rta_same(rta *x, rta *y)
1017 {
1018 return (x->src == y->src &&
1019 x->source == y->source &&
1020 x->scope == y->scope &&
1021 x->cast == y->cast &&
1022 x->dest == y->dest &&
1023 x->flags == y->flags &&
1024 x->igp_metric == y->igp_metric &&
1025 ipa_equal(x->gw, y->gw) &&
1026 ipa_equal(x->from, y->from) &&
1027 x->iface == y->iface &&
1028 x->hostentry == y->hostentry &&
1029 mpnh_same(x->nexthops, y->nexthops) &&
1030 ea_same(x->eattrs, y->eattrs));
1031 }
1032
1033 static rta *
1034 rta_copy(rta *o)
1035 {
1036 rta *r = sl_alloc(rta_slab);
1037
1038 memcpy(r, o, sizeof(rta));
1039 r->uc = 1;
1040 r->nexthops = mpnh_copy(o->nexthops);
1041 r->eattrs = ea_list_copy(o->eattrs);
1042 return r;
1043 }
1044
1045 static inline void
1046 rta_insert(rta *r)
1047 {
1048 uint h = r->hash_key & rta_cache_mask;
1049 r->next = rta_hash_table[h];
1050 if (r->next)
1051 r->next->pprev = &r->next;
1052 r->pprev = &rta_hash_table[h];
1053 rta_hash_table[h] = r;
1054 }
1055
1056 static void
1057 rta_rehash(void)
1058 {
1059 uint ohs = rta_cache_size;
1060 uint h;
1061 rta *r, *n;
1062 rta **oht = rta_hash_table;
1063
1064 rta_cache_size = 2*rta_cache_size;
1065 DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
1066 rta_alloc_hash();
1067 for(h=0; h<ohs; h++)
1068 for(r=oht[h]; r; r=n)
1069 {
1070 n = r->next;
1071 rta_insert(r);
1072 }
1073 mb_free(oht);
1074 }
1075
1076 /**
1077 * rta_lookup - look up a &rta in attribute cache
1078 * @o: a un-cached &rta
1079 *
1080 * rta_lookup() gets an un-cached &rta structure and returns its cached
1081 * counterpart. It starts with examining the attribute cache to see whether
1082 * there exists a matching entry. If such an entry exists, it's returned and
1083 * its use count is incremented, else a new entry is created with use count
1084 * set to 1.
1085 *
1086 * The extended attribute lists attached to the &rta are automatically
1087 * converted to the normalized form.
1088 */
1089 rta *
1090 rta_lookup(rta *o)
1091 {
1092 rta *r;
1093 uint h;
1094
1095 ASSERT(!(o->aflags & RTAF_CACHED));
1096 if (o->eattrs)
1097 {
1098 if (o->eattrs->next) /* Multiple ea_list's, need to merge them */
1099 {
1100 ea_list *ml = alloca(ea_scan(o->eattrs));
1101 ea_merge(o->eattrs, ml);
1102 o->eattrs = ml;
1103 }
1104 ea_sort(o->eattrs);
1105 }
1106
1107 h = rta_hash(o);
1108 for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1109 if (r->hash_key == h && rta_same(r, o))
1110 return rta_clone(r);
1111
1112 r = rta_copy(o);
1113 r->hash_key = h;
1114 r->aflags = RTAF_CACHED;
1115 rt_lock_source(r->src);
1116 rt_lock_hostentry(r->hostentry);
1117 rta_insert(r);
1118
1119 if (++rta_cache_count > rta_cache_limit)
1120 rta_rehash();
1121
1122 return r;
1123 }
1124
1125 void
1126 rta__free(rta *a)
1127 {
1128 ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
1129 rta_cache_count--;
1130 *a->pprev = a->next;
1131 if (a->next)
1132 a->next->pprev = a->pprev;
1133 a->aflags = 0; /* Poison the entry */
1134 rt_unlock_hostentry(a->hostentry);
1135 rt_unlock_source(a->src);
1136 mpnh_free(a->nexthops);
1137 ea_free(a->eattrs);
1138 sl_free(rta_slab, a);
1139 }
1140
1141 /**
1142 * rta_dump - dump route attributes
1143 * @a: attribute structure to dump
1144 *
1145 * This function takes a &rta and dumps its contents to the debug output.
1146 */
1147 void
1148 rta_dump(rta *a)
1149 {
1150 static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
1151 "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
1152 "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
1153 "RTS_OSPF_EXT2", "RTS_BGP" };
1154 static char *rtc[] = { "", " BC", " MC", " AC" };
1155 static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1156
1157 debug("p=%s uc=%d %s %s%s%s h=%04x",
1158 a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
1159 rtd[a->dest], a->hash_key);
1160 if (!(a->aflags & RTAF_CACHED))
1161 debug(" !CACHED");
1162 debug(" <-%I", a->from);
1163 if (a->dest == RTD_ROUTER)
1164 debug(" ->%I", a->gw);
1165 if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER)
1166 debug(" [%s]", a->iface ? a->iface->name : "???" );
1167 if (a->eattrs)
1168 {
1169 debug(" EA: ");
1170 ea_dump(a->eattrs);
1171 }
1172 }
1173
1174 /**
1175 * rta_dump_all - dump attribute cache
1176 *
1177 * This function dumps the whole contents of route attribute cache
1178 * to the debug output.
1179 */
1180 void
1181 rta_dump_all(void)
1182 {
1183 rta *a;
1184 uint h;
1185
1186 debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1187 for(h=0; h<rta_cache_size; h++)
1188 for(a=rta_hash_table[h]; a; a=a->next)
1189 {
1190 debug("%p ", a);
1191 rta_dump(a);
1192 debug("\n");
1193 }
1194 debug("\n");
1195 }
1196
1197 void
1198 rta_show(struct cli *c, rta *a, ea_list *eal)
1199 {
1200 static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1201 "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1202 static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
1203 int i;
1204
1205 cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
1206 if (!eal)
1207 eal = a->eattrs;
1208 for(; eal; eal=eal->next)
1209 for(i=0; i<eal->count; i++)
1210 ea_show(c, &eal->attrs[i]);
1211 }
1212
1213 /**
1214 * rta_init - initialize route attribute cache
1215 *
1216 * This function is called during initialization of the routing
1217 * table module to set up the internals of the attribute cache.
1218 */
1219 void
1220 rta_init(void)
1221 {
1222 rta_pool = rp_new(&root_pool, "Attributes");
1223 rta_slab = sl_new(rta_pool, sizeof(rta));
1224 mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh));
1225 rta_alloc_hash();
1226 rte_src_init();
1227 }
1228
1229 /*
1230 * Documentation for functions declared inline in route.h
1231 */
1232 #if 0
1233
1234 /**
1235 * rta_clone - clone route attributes
1236 * @r: a &rta to be cloned
1237 *
1238 * rta_clone() takes a cached &rta and returns its identical cached
1239 * copy. Currently it works by just returning the original &rta with
1240 * its use count incremented.
1241 */
1242 static inline rta *rta_clone(rta *r)
1243 { DUMMY; }
1244
1245 /**
1246 * rta_free - free route attributes
1247 * @r: a &rta to be freed
1248 *
1249 * If you stop using a &rta (for example when deleting a route which uses
1250 * it), you need to call rta_free() to notify the attribute cache the
1251 * attribute is no longer in use and can be freed if you were the last
1252 * user (which rta_free() tests by inspecting the use count).
1253 */
1254 static inline void rta_free(rta *r)
1255 { DUMMY; }
1256
1257 #endif