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