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