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