]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/rt-attr.c
Nest: Fix bug in recursive routes with MPLS-labeled nexthops
[thirdparty/bird.git] / nest / rt-attr.c
CommitLineData
2326b001
MM
1/*
2 * BIRD -- Route Attribute Cache
3 *
ee76a92a 4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
2326b001
MM
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
3ce8c610
MM
9/**
10 * DOC: Route attribute cache
11 *
12 * Each route entry carries a set of route attributes. Several of them
13 * vary from route to route, but most attributes are usually common
14 * for a large number of routes. To conserve memory, we've decided to
15 * store only the varying ones directly in the &rte and hold the rest
16 * in a special structure called &rta which is shared among all the
17 * &rte's with these attributes.
18 *
19 * Each &rta contains all the static attributes of the route (i.e.,
20 * those which are always present) as structure members and a list of
21 * dynamic attributes represented by a linked list of &ea_list
22 * structures, each of them consisting of an array of &eattr's containing
23 * the individual attributes. An attribute can be specified more than once
2e9b2421 24 * in the &ea_list chain and in such case the first occurrence overrides
3ce8c610
MM
25 * the others. This semantics is used especially when someone (for example
26 * a filter) wishes to alter values of several dynamic attributes, but
27 * it wants to preserve the original attribute lists maintained by
28 * another module.
29 *
30 * Each &eattr contains an attribute identifier (split to protocol ID and
31 * per-protocol attribute ID), protocol dependent flags, a type code (consisting
32 * of several bit fields describing attribute characteristics) and either an
33 * embedded 32-bit value or a pointer to a &adata structure holding attribute
34 * contents.
35 *
2e9b2421 36 * There exist two variants of &rta's -- cached and un-cached ones. Un-cached
3ce8c610
MM
37 * &rta's can have arbitrarily complex structure of &ea_list's and they
38 * can be modified by any module in the route processing chain. Cached
39 * &rta's have their attribute lists normalized (that means at most one
40 * &ea_list is present and its values are sorted in order to speed up
41 * searching), they are stored in a hash table to make fast lookup possible
42 * and they are provided with a use count to allow sharing.
43 *
44 * Routing tables always contain only cached &rta's.
45 */
46
2326b001
MM
47#include "nest/bird.h"
48#include "nest/route.h"
49#include "nest/protocol.h"
66e53309 50#include "nest/iface.h"
730f2e2c 51#include "nest/cli.h"
c6add07f 52#include "nest/attrs.h"
10af3676 53#include "lib/alloca.h"
e7d2ac44 54#include "lib/hash.h"
74c838a8 55#include "lib/idm.h"
2326b001 56#include "lib/resource.h"
221135d6 57#include "lib/string.h"
2326b001 58
9a74622c
JMM
59#include <stddef.h>
60
4c553c5a
MM
61const adata null_adata; /* adata of length 0 */
62
69b2f63d 63const char * const rta_src_names[RTS_MAX] = {
69b2f63d
OZ
64 [RTS_STATIC] = "static",
65 [RTS_INHERIT] = "inherit",
66 [RTS_DEVICE] = "device",
67 [RTS_STATIC_DEVICE] = "static-device",
68 [RTS_REDIRECT] = "redirect",
69 [RTS_RIP] = "RIP",
70 [RTS_OSPF] = "OSPF",
71 [RTS_OSPF_IA] = "OSPF-IA",
72 [RTS_OSPF_EXT1] = "OSPF-E1",
73 [RTS_OSPF_EXT2] = "OSPF-E2",
74 [RTS_BGP] = "BGP",
75 [RTS_PIPE] = "pipe",
76 [RTS_BABEL] = "Babel",
77 [RTS_RPKI] = "RPKI",
977b82fb 78 [RTS_PERF] = "Perf",
bcff3ae7 79 [RTS_L3VPN] = "L3VPN",
977b82fb 80 [RTS_AGGREGATED] = "aggregated",
69b2f63d
OZ
81};
82
665be7f6
OZ
83const char * rta_dest_names[RTD_MAX] = {
84 [RTD_NONE] = "",
85 [RTD_UNICAST] = "unicast",
86 [RTD_BLACKHOLE] = "blackhole",
87 [RTD_UNREACHABLE] = "unreachable",
88 [RTD_PROHIBIT] = "prohibited",
89};
90
acb60628
OZ
91pool *rta_pool;
92
ec5e5d23
JMM
93static slab *rta_slab_[4];
94static slab *nexthop_slab_[4];
094d2bdb
OZ
95static slab *rte_src_slab;
96
74c838a8 97static struct idm src_ids;
e7d2ac44 98#define SRC_ID_INIT_SIZE 4
094d2bdb
OZ
99
100/* rte source hash */
e7d2ac44
OZ
101
102#define RSH_KEY(n) n->proto, n->private_id
103#define RSH_NEXT(n) n->next
104#define RSH_EQ(p1,n1,p2,n2) p1 == p2 && n1 == n2
21213be5 105#define RSH_FN(p,n) p->hash_key ^ u64_hash(n)
e7d2ac44
OZ
106
107#define RSH_REHASH rte_src_rehash
108#define RSH_PARAMS /2, *2, 1, 1, 8, 20
109#define RSH_INIT_ORDER 6
110
111static HASH(struct rte_src) src_hash;
2326b001 112
094d2bdb
OZ
113static void
114rte_src_init(void)
115{
116 rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
117
74c838a8 118 idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
094d2bdb 119
e7d2ac44 120 HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
094d2bdb
OZ
121}
122
094d2bdb 123
e7d2ac44 124HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
094d2bdb
OZ
125
126struct rte_src *
127rt_find_source(struct proto *p, u32 id)
128{
e7d2ac44 129 return HASH_FIND(src_hash, RSH, p, id);
094d2bdb
OZ
130}
131
132struct rte_src *
133rt_get_source(struct proto *p, u32 id)
134{
e7d2ac44 135 struct rte_src *src = rt_find_source(p, id);
094d2bdb 136
e7d2ac44
OZ
137 if (src)
138 return src;
094d2bdb 139
c9ae8165 140 src = sl_allocz(rte_src_slab);
094d2bdb
OZ
141 src->proto = p;
142 src->private_id = id;
74c838a8 143 src->global_id = idm_alloc(&src_ids);
094d2bdb 144 src->uc = 0;
d217ba51 145
e7d2ac44 146 HASH_INSERT2(src_hash, RSH, rta_pool, src);
094d2bdb
OZ
147
148 return src;
149}
150
094d2bdb
OZ
151void
152rt_prune_sources(void)
153{
e7d2ac44
OZ
154 HASH_WALK_FILTER(src_hash, next, src, sp)
155 {
156 if (src->uc == 0)
094d2bdb 157 {
e7d2ac44 158 HASH_DO_REMOVE(src_hash, RSH, sp);
74c838a8 159 idm_free(&src_ids, src->global_id);
ebd807c0 160 sl_free(src);
094d2bdb 161 }
e7d2ac44
OZ
162 }
163 HASH_WALK_FILTER_END;
094d2bdb 164
e7d2ac44 165 HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
094d2bdb
OZ
166}
167
168
169/*
170 * Multipath Next Hop
171 */
172
04632fd7 173static inline u32
4e276a89 174nexthop_hash(struct nexthop *x)
7e95c05d 175{
04632fd7 176 u32 h = 0;
7e95c05d 177 for (; x; x = x->next)
ec5e5d23
JMM
178 {
179 h ^= ipa_hash(x->gw) ^ (h << 5) ^ (h >> 9);
62e64905
OZ
180
181 for (int i = 0; i < x->labels; i++)
ec5e5d23
JMM
182 h ^= x->label[i] ^ (h << 6) ^ (h >> 7);
183 }
7e95c05d
OZ
184
185 return h;
186}
187
f40e2bc2
OZ
188static inline int
189nexthop_equal_1(struct nexthop *x, struct nexthop *y)
190{
191 if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) ||
192 (x->flags != y->flags) || (x->weight != y->weight) ||
193 (x->labels != y->labels))
194 return 0;
195
196 for (int i = 0; i < x->labels; i++)
197 if (x->label[i] != y->label[i])
198 return 0;
199
200 return 1;
201}
202
7e95c05d 203int
f40e2bc2 204nexthop_equal_(struct nexthop *x, struct nexthop *y)
7e95c05d 205{
f40e2bc2
OZ
206 /* Like nexthop_same(), but ignores difference between local labels and labels from hostentry */
207
7e95c05d 208 for (; x && y; x = x->next, y = y->next)
f40e2bc2 209 if (!nexthop_equal_1(x, y))
7e95c05d 210 return 0;
62e64905 211
f40e2bc2
OZ
212 return x == y;
213}
214
215int
216nexthop__same(struct nexthop *x, struct nexthop *y)
217{
218 for (; x && y; x = x->next, y = y->next)
219 if (!nexthop_equal_1(x, y) ||
220 (x->labels_orig != y->labels_orig))
221 return 0;
7e95c05d 222
62e64905 223 return x == y;
7e95c05d
OZ
224}
225
d217ba51 226static int
0fa8bf91 227nexthop_compare_node(const struct nexthop *x, const struct nexthop *y)
d217ba51
OZ
228{
229 int r;
230
231 if (!x)
232 return 1;
233
234 if (!y)
235 return -1;
236
a1f5e514
OZ
237 /* Should we also compare flags ? */
238
d217ba51
OZ
239 r = ((int) y->weight) - ((int) x->weight);
240 if (r)
241 return r;
242
243 r = ipa_compare(x->gw, y->gw);
244 if (r)
245 return r;
246
ec5e5d23
JMM
247 r = ((int) y->labels) - ((int) x->labels);
248 if (r)
249 return r;
250
62e64905 251 for (int i = 0; i < y->labels; i++)
ec5e5d23
JMM
252 {
253 r = ((int) y->label[i]) - ((int) x->label[i]);
254 if (r)
255 return r;
256 }
257
d217ba51
OZ
258 return ((int) x->iface->index) - ((int) y->iface->index);
259}
260
4e276a89
JMM
261static inline struct nexthop *
262nexthop_copy_node(const struct nexthop *src, linpool *lp)
d217ba51 263{
ec5e5d23
JMM
264 struct nexthop *n = lp_alloc(lp, nexthop_size(src));
265
266 memcpy(n, src, nexthop_size(src));
d217ba51 267 n->next = NULL;
ec5e5d23 268
d217ba51
OZ
269 return n;
270}
271
272/**
4e276a89 273 * nexthop_merge - merge nexthop lists
d217ba51
OZ
274 * @x: list 1
275 * @y: list 2
276 * @rx: reusability of list @x
277 * @ry: reusability of list @y
278 * @max: max number of nexthops
279 * @lp: linpool for allocating nexthops
280 *
4e276a89 281 * The nexthop_merge() function takes two nexthop lists @x and @y and merges them,
d217ba51
OZ
282 * eliminating possible duplicates. The input lists must be sorted and the
283 * result is sorted too. The number of nexthops in result is limited by @max.
284 * New nodes are allocated from linpool @lp.
285 *
286 * The arguments @rx and @ry specify whether corresponding input lists may be
287 * consumed by the function (i.e. their nodes reused in the resulting list), in
288 * that case the caller should not access these lists after that. To eliminate
289 * issues with deallocation of these lists, the caller should use some form of
290 * bulk deallocation (e.g. stack or linpool) to free these nodes when the
291 * resulting list is no longer needed. When reusability is not set, the
292 * corresponding lists are not modified nor linked from the resulting list.
293 */
4e276a89
JMM
294struct nexthop *
295nexthop_merge(struct nexthop *x, struct nexthop *y, int rx, int ry, int max, linpool *lp)
d217ba51 296{
4e276a89
JMM
297 struct nexthop *root = NULL;
298 struct nexthop **n = &root;
d217ba51
OZ
299
300 while ((x || y) && max--)
301 {
4e276a89 302 int cmp = nexthop_compare_node(x, y);
0fa8bf91 303
d217ba51
OZ
304 if (cmp < 0)
305 {
0fa8bf91 306 ASSUME(x);
4e276a89 307 *n = rx ? x : nexthop_copy_node(x, lp);
d217ba51
OZ
308 x = x->next;
309 }
310 else if (cmp > 0)
311 {
0fa8bf91 312 ASSUME(y);
4e276a89 313 *n = ry ? y : nexthop_copy_node(y, lp);
d217ba51
OZ
314 y = y->next;
315 }
316 else
317 {
0fa8bf91 318 ASSUME(x && y);
4e276a89 319 *n = rx ? x : (ry ? y : nexthop_copy_node(x, lp));
d217ba51
OZ
320 x = x->next;
321 y = y->next;
322 }
323 n = &((*n)->next);
324 }
325 *n = NULL;
326
327 return root;
328}
329
84cac51a 330void
62e64905 331nexthop_insert(struct nexthop **n, struct nexthop *x)
84cac51a 332{
62e64905 333 for (; *n; n = &((*n)->next))
4e276a89 334 {
62e64905 335 int cmp = nexthop_compare_node(*n, x);
84cac51a
OZ
336
337 if (cmp < 0)
338 continue;
62e64905
OZ
339 else if (cmp > 0)
340 break;
341 else
342 return;
84cac51a
OZ
343 }
344
62e64905
OZ
345 x->next = *n;
346 *n = x;
84cac51a
OZ
347}
348
59d3a361
OZ
349struct nexthop *
350nexthop_sort(struct nexthop *x)
351{
352 struct nexthop *s = NULL;
353
354 /* Simple insert-sort */
355 while (x)
356 {
357 struct nexthop *n = x;
358 x = n->next;
359 n->next = NULL;
360
361 nexthop_insert(&s, n);
362 }
363
364 return s;
365}
366
84cac51a 367int
4e276a89 368nexthop_is_sorted(struct nexthop *x)
84cac51a
OZ
369{
370 for (; x && x->next; x = x->next)
4e276a89 371 if (nexthop_compare_node(x, x->next) >= 0)
84cac51a
OZ
372 return 0;
373
374 return 1;
375}
d217ba51 376
ec5e5d23
JMM
377static inline slab *
378nexthop_slab(struct nexthop *nh)
379{
62e64905 380 return nexthop_slab_[MIN(nh->labels, 3)];
ec5e5d23
JMM
381}
382
4e276a89
JMM
383static struct nexthop *
384nexthop_copy(struct nexthop *o)
7e95c05d 385{
4e276a89
JMM
386 struct nexthop *first = NULL;
387 struct nexthop **last = &first;
7e95c05d
OZ
388
389 for (; o; o = o->next)
390 {
c9ae8165 391 struct nexthop *n = sl_allocz(nexthop_slab(o));
7e95c05d
OZ
392 n->gw = o->gw;
393 n->iface = o->iface;
394 n->next = NULL;
33716595 395 n->flags = o->flags;
7e95c05d 396 n->weight = o->weight;
bda58634 397 n->labels_orig = o->labels_orig;
f2010f9c
JMM
398 n->labels = o->labels;
399 for (int i=0; i<o->labels; i++)
400 n->label[i] = o->label[i];
7e95c05d
OZ
401
402 *last = n;
403 last = &(n->next);
404 }
405
406 return first;
407}
408
409static void
4e276a89 410nexthop_free(struct nexthop *o)
7e95c05d 411{
4e276a89 412 struct nexthop *n;
7e95c05d
OZ
413
414 while (o)
415 {
416 n = o->next;
ebd807c0 417 sl_free(o);
7e95c05d
OZ
418 o = n;
419 }
420}
421
422
b77ae37d
MM
423/*
424 * Extended Attributes
425 */
426
8d24b689
MM
427static inline eattr *
428ea__find(ea_list *e, unsigned id)
b77ae37d
MM
429{
430 eattr *a;
431 int l, r, m;
432
433 while (e)
434 {
435 if (e->flags & EALF_BISECT)
436 {
437 l = 0;
fee78355 438 r = e->count - 1;
b77ae37d
MM
439 while (l <= r)
440 {
441 m = (l+r) / 2;
442 a = &e->attrs[m];
443 if (a->id == id)
444 return a;
445 else if (a->id < id)
446 l = m+1;
447 else
448 r = m-1;
449 }
450 }
451 else
452 for(m=0; m<e->count; m++)
453 if (e->attrs[m].id == id)
454 return &e->attrs[m];
455 e = e->next;
456 }
457 return NULL;
458}
459
3ce8c610
MM
460/**
461 * ea_find - find an extended attribute
462 * @e: attribute list to search in
463 * @id: attribute ID to search for
464 *
465 * Given an extended attribute list, ea_find() searches for a first
2e9b2421 466 * occurrence of an attribute with specified ID, returning either a pointer
3ce8c610
MM
467 * to its &eattr structure or %NULL if no such attribute exists.
468 */
8d24b689
MM
469eattr *
470ea_find(ea_list *e, unsigned id)
471{
472 eattr *a = ea__find(e, id & EA_CODE_MASK);
473
0f685152 474 if (a && a->undef && !(id & EA_ALLOW_UNDEF))
8d24b689
MM
475 return NULL;
476 return a;
477}
478
9fdf9d29
OZ
479/**
480 * ea_walk - walk through extended attributes
481 * @s: walk state structure
482 * @id: start of attribute ID interval
483 * @max: length of attribute ID interval
484 *
485 * Given an extended attribute list, ea_walk() walks through the list looking
486 * for first occurrences of attributes with ID in specified interval from @id to
487 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
488 * walk state in @s for subsequent calls.
8e433d6a 489 *
9fdf9d29
OZ
490 * The function ea_walk() is supposed to be called in a loop, with initially
491 * zeroed walk state structure @s with filled the initial extended attribute
492 * list, returning one found attribute in each call or %NULL when no other
493 * attribute exists. The extended attribute list or the arguments should not be
494 * modified between calls. The maximum value of @max is 128.
495 */
496eattr *
497ea_walk(struct ea_walk_state *s, uint id, uint max)
498{
499 ea_list *e = s->eattrs;
500 eattr *a = s->ea;
501 eattr *a_max;
502
503 max = id + max;
504
505 if (a)
506 goto step;
507
508 for (; e; e = e->next)
509 {
510 if (e->flags & EALF_BISECT)
511 {
512 int l, r, m;
513
514 l = 0;
515 r = e->count - 1;
516 while (l < r)
517 {
518 m = (l+r) / 2;
519 if (e->attrs[m].id < id)
520 l = m + 1;
521 else
522 r = m;
523 }
524 a = e->attrs + l;
525 }
526 else
527 a = e->attrs;
528
529 step:
530 a_max = e->attrs + e->count;
531 for (; a < a_max; a++)
532 if ((a->id >= id) && (a->id < max))
533 {
534 int n = a->id - id;
535
536 if (BIT32_TEST(s->visited, n))
537 continue;
538
539 BIT32_SET(s->visited, n);
540
0f685152 541 if (a->undef)
9fdf9d29
OZ
542 continue;
543
544 s->eattrs = e;
545 s->ea = a;
546 return a;
547 }
548 else if (e->flags & EALF_BISECT)
549 break;
550 }
551
552 return NULL;
553}
554
3ce8c610
MM
555/**
556 * ea_get_int - fetch an integer attribute
557 * @e: attribute list
558 * @id: attribute ID
559 * @def: default value
560 *
561 * This function is a shortcut for retrieving a value of an integer attribute
562 * by calling ea_find() to find the attribute, extracting its value or returning
563 * a provided default if no such attribute is present.
564 */
6e13df70
MM
565uintptr_t
566ea_get_int(ea_list *e, unsigned id, uintptr_t def)
c0100454
PM
567{
568 eattr *a = ea_find(e, id);
569 if (!a)
570 return def;
571 return a->u.data;
572}
573
b77ae37d
MM
574static inline void
575ea_do_sort(ea_list *e)
576{
577 unsigned n = e->count;
578 eattr *a = e->attrs;
579 eattr *b = alloca(n * sizeof(eattr));
580 unsigned s, ss;
581
582 /* We need to use a stable sorting algorithm, hence mergesort */
583 do
584 {
585 s = ss = 0;
586 while (s < n)
587 {
588 eattr *p, *q, *lo, *hi;
589 p = b;
590 ss = s;
591 *p++ = a[s++];
592 while (s < n && p[-1].id <= a[s].id)
593 *p++ = a[s++];
594 if (s < n)
595 {
596 q = p;
597 *p++ = a[s++];
598 while (s < n && p[-1].id <= a[s].id)
599 *p++ = a[s++];
600 lo = b;
601 hi = q;
602 s = ss;
603 while (lo < q && hi < p)
604 if (lo->id <= hi->id)
605 a[s++] = *lo++;
606 else
607 a[s++] = *hi++;
608 while (lo < q)
609 a[s++] = *lo++;
610 while (hi < p)
611 a[s++] = *hi++;
612 }
613 }
614 }
615 while (ss);
616}
617
13c0be19 618/**
875cc073
OZ
619 * In place discard duplicates and undefs in sorted ea_list. We use stable sort
620 * for this reason.
13c0be19 621 **/
b77ae37d
MM
622static inline void
623ea_do_prune(ea_list *e)
624{
51a183af 625 eattr *s, *d, *l, *s0;
8d24b689
MM
626 int i = 0;
627
13c0be19
JMM
628 s = d = e->attrs; /* Beginning of the list. @s is source, @d is destination. */
629 l = e->attrs + e->count; /* End of the list */
630
631 /* Walk from begin to end. */
8d24b689
MM
632 while (s < l)
633 {
51a183af 634 s0 = s++;
13c0be19 635 /* Find a consecutive block of the same attribute */
51a183af
MM
636 while (s < l && s->id == s[-1].id)
637 s++;
13c0be19
JMM
638
639 /* Now s0 is the most recent version, s[-1] the oldest one */
640 /* Drop undefs */
0f685152 641 if (s0->undef)
13c0be19
JMM
642 continue;
643
13c0be19
JMM
644 /* Copy the newest version to destination */
645 *d = *s0;
646
647 /* Preserve info whether it originated locally */
63cf5d5d
MM
648 d->originated = s[-1].originated;
649
650 /* Not fresh any more, we prefer surstroemming */
651 d->fresh = 0;
13c0be19
JMM
652
653 /* Next destination */
654 d++;
655 i++;
8d24b689 656 }
13c0be19 657
8d24b689 658 e->count = i;
b77ae37d
MM
659}
660
3ce8c610
MM
661/**
662 * ea_sort - sort an attribute list
663 * @e: list to be sorted
664 *
665 * This function takes a &ea_list chain and sorts the attributes
666 * within each of its entries.
667 *
668 * If an attribute occurs multiple times in a single &ea_list,
2e9b2421 669 * ea_sort() leaves only the first (the only significant) occurrence.
3ce8c610 670 */
b77ae37d
MM
671void
672ea_sort(ea_list *e)
673{
674 while (e)
675 {
676 if (!(e->flags & EALF_SORTED))
677 {
678 ea_do_sort(e);
679 ea_do_prune(e);
680 e->flags |= EALF_SORTED;
681 }
b77ae37d 682 if (e->count > 5)
b77ae37d
MM
683 e->flags |= EALF_BISECT;
684 e = e->next;
685 }
686}
687
3ce8c610
MM
688/**
689 * ea_scan - estimate attribute list size
690 * @e: attribute list
691 *
692 * This function calculates an upper bound of the size of
693 * a given &ea_list after merging with ea_merge().
694 */
b77ae37d
MM
695unsigned
696ea_scan(ea_list *e)
697{
698 unsigned cnt = 0;
699
700 while (e)
701 {
702 cnt += e->count;
703 e = e->next;
704 }
705 return sizeof(ea_list) + sizeof(eattr)*cnt;
706}
707
3ce8c610
MM
708/**
709 * ea_merge - merge segments of an attribute list
710 * @e: attribute list
711 * @t: buffer to store the result to
712 *
713 * This function takes a possibly multi-segment attribute list
714 * and merges all of its segments to one.
715 *
716 * The primary use of this function is for &ea_list normalization:
717 * first call ea_scan() to determine how much memory will the result
718 * take, then allocate a buffer (usually using alloca()), merge the
719 * segments with ea_merge() and finally sort and prune the result
720 * by calling ea_sort().
721 */
b77ae37d
MM
722void
723ea_merge(ea_list *e, ea_list *t)
724{
725 eattr *d = t->attrs;
726
727 t->flags = 0;
728 t->count = 0;
729 t->next = NULL;
730 while (e)
731 {
732 memcpy(d, e->attrs, sizeof(eattr)*e->count);
733 t->count += e->count;
734 d += e->count;
735 e = e->next;
736 }
737}
738
3ce8c610
MM
739/**
740 * ea_same - compare two &ea_list's
741 * @x: attribute list
742 * @y: attribute list
743 *
744 * ea_same() compares two normalized attribute lists @x and @y and returns
745 * 1 if they contain the same attributes, 0 otherwise.
746 */
6f57dcc0 747int
2326b001
MM
748ea_same(ea_list *x, ea_list *y)
749{
750 int c;
751
b77ae37d
MM
752 if (!x || !y)
753 return x == y;
754 ASSERT(!x->next && !y->next);
755 if (x->count != y->count)
756 return 0;
757 for(c=0; c<x->count; c++)
2326b001 758 {
b77ae37d
MM
759 eattr *a = &x->attrs[c];
760 eattr *b = &y->attrs[c];
761
762 if (a->id != b->id ||
763 a->flags != b->flags ||
764 a->type != b->type ||
63cf5d5d
MM
765 a->originated != b->originated ||
766 a->fresh != b->fresh ||
0f685152 767 a->undef != b->undef ||
28a10f84 768 ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
2326b001 769 return 0;
b77ae37d
MM
770 }
771 return 1;
772}
773
774static inline ea_list *
775ea_list_copy(ea_list *o)
776{
777 ea_list *n;
7c8b7649 778 unsigned i, adpos, elen;
b77ae37d
MM
779
780 if (!o)
781 return NULL;
782 ASSERT(!o->next);
7c8b7649
MM
783 elen = adpos = sizeof(ea_list) + sizeof(eattr) * o->count;
784
785 for(i=0; i<o->count; i++)
786 {
787 eattr *a = &o->attrs[i];
788 if (!(a->type & EAF_EMBEDDED))
7d2c7d59 789 elen += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, EA_DATA_ALIGN);
7c8b7649
MM
790 }
791
792 n = mb_alloc(rta_pool, elen);
793 memcpy(n, o, adpos);
b77ae37d
MM
794 n->flags |= EALF_CACHED;
795 for(i=0; i<o->count; i++)
796 {
797 eattr *a = &n->attrs[i];
08732b71 798 if (!(a->type & EAF_EMBEDDED))
b77ae37d 799 {
7d2c7d59
OZ
800 uint size_u = sizeof(struct adata) + a->u.ptr->length;
801 uint size = BIRD_ALIGN(size_u, EA_DATA_ALIGN);
7c8b7649
MM
802 ASSERT_DIE(adpos + size <= elen);
803
804 struct adata *d = ((void *) n) + adpos;
7d2c7d59 805 memcpy(d, a->u.ptr, size_u);
b77ae37d 806 a->u.ptr = d;
7c8b7649
MM
807
808 adpos += size;
b77ae37d
MM
809 }
810 }
7c8b7649 811 ASSERT_DIE(adpos == elen);
b77ae37d
MM
812 return n;
813}
814
5d86aefb
MM
815static inline void
816ea_free(ea_list *o)
817{
818 if (o)
819 {
820 ASSERT(!o->next);
821 mb_free(o);
822 }
823}
824
ba5e5940 825static int
258be565 826get_generic_attr(const eattr *a, byte **buf, int buflen UNUSED)
ba5e5940 827{
333ddd4f
OZ
828 switch (a->id)
829 {
830 case EA_GEN_IGP_METRIC:
831 *buf += bsprintf(*buf, "igp_metric");
832 return GA_NAME;
833
834 case EA_MPLS_LABEL:
835 *buf += bsprintf(*buf, "mpls_label");
836 return GA_NAME;
d217ba51 837
333ddd4f
OZ
838 case EA_MPLS_POLICY:
839 *buf += bsprintf(*buf, "mpls_policy");
840 return GA_NAME;
841
842 case EA_MPLS_CLASS:
843 *buf += bsprintf(*buf, "mpls_class");
844 return GA_NAME;
845
846 default:
847 return GA_UNKNOWN;
848 }
ba5e5940
OZ
849}
850
315f23a0 851void
258be565 852ea_format_bitfield(const struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
315f23a0
OZ
853{
854 byte *bound = buf + bufsize - 32;
855 u32 data = a->u.data;
856 int i;
857
858 for (i = min; i < max; i++)
859 if ((data & (1u << i)) && names[i])
860 {
861 if (buf > bound)
862 {
863 strcpy(buf, " ...");
864 return;
865 }
866
867 buf += bsprintf(buf, " %s", names[i]);
868 data &= ~(1u << i);
869 }
870
871 if (data)
872 bsprintf(buf, " %08x", data);
873
874 return;
875}
876
fdf16eb6 877static inline void
4c553c5a 878opaque_format(const struct adata *ad, byte *buf, uint size)
fdf16eb6
OZ
879{
880 byte *bound = buf + size - 10;
3e236955 881 uint i;
fdf16eb6
OZ
882
883 for(i = 0; i < ad->length; i++)
884 {
885 if (buf > bound)
886 {
887 strcpy(buf, " ...");
888 return;
889 }
890 if (i)
891 *buf++ = ' ';
892
893 buf += bsprintf(buf, "%02x", ad->data[i]);
894 }
895
896 *buf = 0;
897 return;
898}
899
900static inline void
4c553c5a 901ea_show_int_set(struct cli *c, const struct adata *ad, int way, byte *pos, byte *buf, byte *end)
fdf16eb6
OZ
902{
903 int i = int_set_format(ad, way, 0, pos, end - pos);
a52d52fa 904 cli_printf(c, -1012, "\t%s", buf);
fdf16eb6
OZ
905 while (i)
906 {
907 i = int_set_format(ad, way, i, buf, end - buf - 1);
a52d52fa 908 cli_printf(c, -1012, "\t\t%s", buf);
fdf16eb6
OZ
909 }
910}
911
42a0c054 912static inline void
4c553c5a 913ea_show_ec_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end)
42a0c054
OZ
914{
915 int i = ec_set_format(ad, 0, pos, end - pos);
a52d52fa 916 cli_printf(c, -1012, "\t%s", buf);
42a0c054
OZ
917 while (i)
918 {
919 i = ec_set_format(ad, i, buf, end - buf - 1);
a52d52fa 920 cli_printf(c, -1012, "\t\t%s", buf);
42a0c054
OZ
921 }
922}
923
66dbdbd9 924static inline void
4c553c5a 925ea_show_lc_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end)
66dbdbd9
OZ
926{
927 int i = lc_set_format(ad, 0, pos, end - pos);
928 cli_printf(c, -1012, "\t%s", buf);
929 while (i)
930 {
931 i = lc_set_format(ad, i, buf, end - buf - 1);
932 cli_printf(c, -1012, "\t\t%s", buf);
933 }
934}
935
3ce8c610 936/**
fdf16eb6
OZ
937 * ea_show - print an &eattr to CLI
938 * @c: destination CLI
939 * @e: attribute to be printed
3ce8c610 940 *
fdf16eb6
OZ
941 * This function takes an extended attribute represented by its &eattr
942 * structure and prints it to the CLI according to the type information.
3ce8c610
MM
943 *
944 * If the protocol defining the attribute provides its own
945 * get_attr() hook, it's consulted first.
946 */
3991d84e 947void
258be565 948ea_show(struct cli *c, const eattr *e)
3991d84e
MM
949{
950 struct protocol *p;
951 int status = GA_UNKNOWN;
4c553c5a 952 const struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
fdf16eb6
OZ
953 byte buf[CLI_MSG_SIZE];
954 byte *pos = buf, *end = buf + sizeof(buf);
3991d84e 955
265419a3
MM
956 if (EA_IS_CUSTOM(e->id))
957 {
958 const char *name = ea_custom_name(e->id);
959 if (name)
960 {
961 pos += bsprintf(pos, "%s", name);
962 status = GA_NAME;
963 }
964 else
965 pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
966 }
967 else if (p = class_to_protocol[EA_PROTO(e->id)])
3991d84e 968 {
fdf16eb6 969 pos += bsprintf(pos, "%s.", p->name);
3991d84e 970 if (p->get_attr)
fdf16eb6
OZ
971 status = p->get_attr(e, pos, end - pos);
972 pos += strlen(pos);
3991d84e
MM
973 }
974 else if (EA_PROTO(e->id))
fdf16eb6 975 pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
d217ba51 976 else
fdf16eb6 977 status = get_generic_attr(e, &pos, end - pos);
ba5e5940 978
3991d84e 979 if (status < GA_NAME)
fdf16eb6 980 pos += bsprintf(pos, "%02x", EA_ID(e->id));
3991d84e
MM
981 if (status < GA_FULL)
982 {
fdf16eb6
OZ
983 *pos++ = ':';
984 *pos++ = ' ';
0f685152
MM
985
986 if (e->undef)
987 bsprintf(pos, "undefined");
988 else
3991d84e
MM
989 switch (e->type & EAF_TYPE_MASK)
990 {
991 case EAF_TYPE_INT:
fdf16eb6 992 bsprintf(pos, "%u", e->u.data);
3991d84e
MM
993 break;
994 case EAF_TYPE_OPAQUE:
fdf16eb6 995 opaque_format(ad, pos, end - pos);
3991d84e
MM
996 break;
997 case EAF_TYPE_IP_ADDRESS:
fdf16eb6 998 bsprintf(pos, "%I", *(ip_addr *) ad->data);
3991d84e
MM
999 break;
1000 case EAF_TYPE_ROUTER_ID:
fdf16eb6 1001 bsprintf(pos, "%R", e->u.data);
3991d84e 1002 break;
c6add07f 1003 case EAF_TYPE_AS_PATH:
fdf16eb6 1004 as_path_format(ad, pos, end - pos);
c6add07f 1005 break;
315f23a0
OZ
1006 case EAF_TYPE_BITFIELD:
1007 bsprintf(pos, "%08x", e->u.data);
1008 break;
c6add07f 1009 case EAF_TYPE_INT_SET:
fdf16eb6
OZ
1010 ea_show_int_set(c, ad, 1, pos, buf, end);
1011 return;
42a0c054
OZ
1012 case EAF_TYPE_EC_SET:
1013 ea_show_ec_set(c, ad, pos, buf, end);
1014 return;
66dbdbd9
OZ
1015 case EAF_TYPE_LC_SET:
1016 ea_show_lc_set(c, ad, pos, buf, end);
1017 return;
3991d84e 1018 default:
fdf16eb6 1019 bsprintf(pos, "<type %02x>", e->type);
3991d84e
MM
1020 }
1021 }
b28431e5
OZ
1022
1023 if (status != GA_HIDDEN)
1024 cli_printf(c, -1012, "\t%s", buf);
3991d84e
MM
1025}
1026
3ce8c610
MM
1027/**
1028 * ea_dump - dump an extended attribute
1029 * @e: attribute to be dumped
1030 *
1031 * ea_dump() dumps contents of the extended attribute given to
1032 * the debug output.
1033 */
b77ae37d
MM
1034void
1035ea_dump(ea_list *e)
1036{
1037 int i;
1038
1039 if (!e)
1040 {
1041 debug("NONE");
1042 return;
1043 }
1044 while (e)
1045 {
1046 debug("[%c%c%c]",
1047 (e->flags & EALF_SORTED) ? 'S' : 's',
1048 (e->flags & EALF_BISECT) ? 'B' : 'b',
1049 (e->flags & EALF_CACHED) ? 'C' : 'c');
1050 for(i=0; i<e->count; i++)
2326b001 1051 {
b77ae37d
MM
1052 eattr *a = &e->attrs[i];
1053 debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
b77ae37d 1054 debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
63cf5d5d 1055 if (a->originated)
51a183af 1056 debug("o");
b77ae37d
MM
1057 if (a->type & EAF_EMBEDDED)
1058 debug(":%08x", a->u.data);
1059 else
1060 {
1061 int j, len = a->u.ptr->length;
1062 debug("[%d]:", len);
1063 for(j=0; j<len; j++)
1064 debug("%02x", a->u.ptr->data[j]);
1065 }
2326b001 1066 }
b77ae37d
MM
1067 if (e = e->next)
1068 debug(" | ");
2326b001 1069 }
2326b001
MM
1070}
1071
3ce8c610
MM
1072/**
1073 * ea_hash - calculate an &ea_list hash key
1074 * @e: attribute list
1075 *
1076 * ea_hash() takes an extended attribute list and calculated a hopefully
1077 * uniformly distributed hash value from its contents.
1078 */
ae80a2de 1079inline uint
ee76a92a
MM
1080ea_hash(ea_list *e)
1081{
9a74622c
JMM
1082 const u64 mul = 0x68576150f3d6847;
1083 u64 h = 0xafcef24eda8b29;
ee76a92a
MM
1084 int i;
1085
1086 if (e) /* Assuming chain of length 1 */
1087 {
1088 for(i=0; i<e->count; i++)
1089 {
1090 struct eattr *a = &e->attrs[i];
9a74622c 1091 h ^= a->id; h *= mul;
ee76a92a
MM
1092 if (a->type & EAF_EMBEDDED)
1093 h ^= a->u.data;
1094 else
1095 {
4c553c5a 1096 const struct adata *d = a->u.ptr;
9a74622c 1097 h ^= mem_hash(d->data, d->length);
ee76a92a 1098 }
9a74622c 1099 h *= mul;
ee76a92a 1100 }
ee76a92a 1101 }
9a74622c 1102 return (h >> 32) ^ (h & 0xffffffff);
ee76a92a
MM
1103}
1104
3ce8c610
MM
1105/**
1106 * ea_append - concatenate &ea_list's
1107 * @to: destination list (can be %NULL)
1108 * @what: list to be appended (can be %NULL)
1109 *
1110 * This function appends the &ea_list @what at the end of
1111 * &ea_list @to and returns a pointer to the resulting list.
1112 */
ce1da96e
MM
1113ea_list *
1114ea_append(ea_list *to, ea_list *what)
1115{
1116 ea_list *res;
1117
1118 if (!to)
1119 return what;
1120 res = to;
1121 while (to->next)
1122 to = to->next;
1123 to->next = what;
1124 return res;
1125}
1126
b77ae37d
MM
1127/*
1128 * rta's
1129 */
1130
ae80a2de
PT
1131static uint rta_cache_count;
1132static uint rta_cache_size = 32;
1133static uint rta_cache_limit;
1134static uint rta_cache_mask;
ee76a92a
MM
1135static rta **rta_hash_table;
1136
1137static void
1138rta_alloc_hash(void)
1139{
5d86aefb 1140 rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
ee76a92a
MM
1141 if (rta_cache_size < 32768)
1142 rta_cache_limit = rta_cache_size * 2;
1143 else
1144 rta_cache_limit = ~0;
1145 rta_cache_mask = rta_cache_size - 1;
1146}
1147
ae80a2de 1148static inline uint
ee76a92a
MM
1149rta_hash(rta *a)
1150{
d39d41fb 1151 u64 h;
54ac0bec 1152 mem_hash_init(&h);
d39d41fb 1153#define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f));
eb937358 1154#define BMIX(f) mem_hash_mix_num(&h, a->f);
54ac0bec 1155 MIX(hostentry);
54ac0bec
JMM
1156 MIX(from);
1157 MIX(igp_metric);
eb937358
MM
1158 BMIX(source);
1159 BMIX(scope);
1160 BMIX(dest);
1161 MIX(pref);
d39d41fb 1162#undef MIX
54ac0bec 1163
4e276a89 1164 return mem_hash_value(&h) ^ nexthop_hash(&(a->nh)) ^ ea_hash(a->eattrs);
ee76a92a
MM
1165}
1166
2326b001
MM
1167static inline int
1168rta_same(rta *x, rta *y)
1169{
5cff1d5f 1170 return (x->source == y->source &&
2326b001 1171 x->scope == y->scope &&
2326b001 1172 x->dest == y->dest &&
d1e146f2 1173 x->igp_metric == y->igp_metric &&
2326b001 1174 ipa_equal(x->from, y->from) &&
d1e146f2 1175 x->hostentry == y->hostentry &&
4e276a89 1176 nexthop_same(&(x->nh), &(y->nh)) &&
2727bb7c 1177 ea_same(x->eattrs, y->eattrs));
2326b001
MM
1178}
1179
ec5e5d23
JMM
1180static inline slab *
1181rta_slab(rta *a)
1182{
1183 return rta_slab_[a->nh.labels > 2 ? 3 : a->nh.labels];
1184}
1185
2326b001
MM
1186static rta *
1187rta_copy(rta *o)
1188{
ec5e5d23 1189 rta *r = sl_alloc(rta_slab(o));
2326b001 1190
ec5e5d23 1191 memcpy(r, o, rta_size(o));
2326b001 1192 r->uc = 1;
4e276a89 1193 r->nh.next = nexthop_copy(o->nh.next);
2727bb7c 1194 r->eattrs = ea_list_copy(o->eattrs);
2326b001
MM
1195 return r;
1196}
1197
ee76a92a
MM
1198static inline void
1199rta_insert(rta *r)
1200{
ae80a2de 1201 uint h = r->hash_key & rta_cache_mask;
ee76a92a
MM
1202 r->next = rta_hash_table[h];
1203 if (r->next)
1204 r->next->pprev = &r->next;
1205 r->pprev = &rta_hash_table[h];
1206 rta_hash_table[h] = r;
1207}
1208
1209static void
1210rta_rehash(void)
1211{
ae80a2de
PT
1212 uint ohs = rta_cache_size;
1213 uint h;
ee76a92a
MM
1214 rta *r, *n;
1215 rta **oht = rta_hash_table;
1216
1217 rta_cache_size = 2*rta_cache_size;
1218 DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
1219 rta_alloc_hash();
1220 for(h=0; h<ohs; h++)
1221 for(r=oht[h]; r; r=n)
1222 {
1223 n = r->next;
1224 rta_insert(r);
1225 }
1226 mb_free(oht);
1227}
1228
3ce8c610
MM
1229/**
1230 * rta_lookup - look up a &rta in attribute cache
2e9b2421 1231 * @o: a un-cached &rta
3ce8c610 1232 *
2e9b2421 1233 * rta_lookup() gets an un-cached &rta structure and returns its cached
3ce8c610
MM
1234 * counterpart. It starts with examining the attribute cache to see whether
1235 * there exists a matching entry. If such an entry exists, it's returned and
1236 * its use count is incremented, else a new entry is created with use count
1237 * set to 1.
1238 *
1239 * The extended attribute lists attached to the &rta are automatically
1240 * converted to the normalized form.
1241 */
2326b001
MM
1242rta *
1243rta_lookup(rta *o)
1244{
1245 rta *r;
ae80a2de 1246 uint h;
2326b001 1247
eb937358 1248 ASSERT(!o->cached);
2727bb7c 1249 if (o->eattrs)
13c0be19 1250 ea_normalize(o->eattrs);
b77ae37d 1251
ee76a92a
MM
1252 h = rta_hash(o);
1253 for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1254 if (r->hash_key == h && rta_same(r, o))
2326b001 1255 return rta_clone(r);
b77ae37d 1256
2326b001 1257 r = rta_copy(o);
ee76a92a 1258 r->hash_key = h;
eb937358 1259 r->cached = 1;
cfe34a31 1260 rt_lock_hostentry(r->hostentry);
ee76a92a
MM
1261 rta_insert(r);
1262
1263 if (++rta_cache_count > rta_cache_limit)
1264 rta_rehash();
1265
2326b001
MM
1266 return r;
1267}
1268
1269void
b77ae37d 1270rta__free(rta *a)
2326b001 1271{
eb937358 1272 ASSERT(rta_cache_count && a->cached);
ee76a92a 1273 rta_cache_count--;
5d86aefb
MM
1274 *a->pprev = a->next;
1275 if (a->next)
1276 a->next->pprev = a->pprev;
cfe34a31 1277 rt_unlock_hostentry(a->hostentry);
4e276a89
JMM
1278 if (a->nh.next)
1279 nexthop_free(a->nh.next);
5d86aefb 1280 ea_free(a->eattrs);
eb937358 1281 a->cached = 0;
ebd807c0 1282 sl_free(a);
2326b001
MM
1283}
1284
8d9eef17
OZ
1285rta *
1286rta_do_cow(rta *o, linpool *lp)
1287{
63f1c4d9 1288 rta *r = lp_alloc(lp, RTA_MAX_SIZE);
ec5e5d23 1289 memcpy(r, o, rta_size(o));
f2010f9c
JMM
1290 for (struct nexthop **nhn = &(r->nh.next), *nho = o->nh.next; nho; nho = nho->next)
1291 {
1292 *nhn = lp_alloc(lp, nexthop_size(nho));
1293 memcpy(*nhn, nho, nexthop_size(nho));
1294 nhn = &((*nhn)->next);
1295 }
eb937358 1296 r->cached = 0;
8d9eef17
OZ
1297 r->uc = 0;
1298 return r;
1299}
1300
3ce8c610
MM
1301/**
1302 * rta_dump - dump route attributes
1303 * @a: attribute structure to dump
1304 *
1305 * This function takes a &rta and dumps its contents to the debug output.
1306 */
2326b001 1307void
66e53309 1308rta_dump(rta *a)
2326b001 1309{
3660f19d 1310 static char *rts[] = { "", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
beaf86e1 1311 "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
98ac6176 1312 "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
977b82fb
IP
1313 "RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL",
1314 "RTS_RPKI", "RTS_PERF", "RTS_AGGREGATED", };
66e53309
MM
1315 static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1316
5cff1d5f
MM
1317 debug("pref=%d uc=%d %s %s%s h=%04x",
1318 a->pref, a->uc, rts[a->source], ip_scope_text(a->scope),
ee76a92a 1319 rtd[a->dest], a->hash_key);
eb937358 1320 if (!a->cached)
04925e90 1321 debug(" !CACHED");
962ba482 1322 debug(" <-%I", a->from);
4e276a89
JMM
1323 if (a->dest == RTD_UNICAST)
1324 for (struct nexthop *nh = &(a->nh); nh; nh = nh->next)
1325 {
1326 if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw);
ec5e5d23
JMM
1327 if (nh->labels) debug(" L %d", nh->label[0]);
1328 for (int i=1; i<nh->labels; i++)
1329 debug("/%d", nh->label[i]);
4e276a89
JMM
1330 debug(" [%s]", nh->iface ? nh->iface->name : "???");
1331 }
2727bb7c 1332 if (a->eattrs)
b77ae37d
MM
1333 {
1334 debug(" EA: ");
2727bb7c 1335 ea_dump(a->eattrs);
b77ae37d 1336 }
2326b001
MM
1337}
1338
3ce8c610
MM
1339/**
1340 * rta_dump_all - dump attribute cache
1341 *
1342 * This function dumps the whole contents of route attribute cache
1343 * to the debug output.
1344 */
2326b001
MM
1345void
1346rta_dump_all(void)
1347{
66e53309 1348 rta *a;
ae80a2de 1349 uint h;
ee76a92a
MM
1350
1351 debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1352 for(h=0; h<rta_cache_size; h++)
1353 for(a=rta_hash_table[h]; a; a=a->next)
1354 {
1355 debug("%p ", a);
1356 rta_dump(a);
1357 debug("\n");
1358 }
66e53309 1359 debug("\n");
2326b001
MM
1360}
1361
730f2e2c 1362void
13c0be19 1363rta_show(struct cli *c, rta *a)
730f2e2c 1364{
69b2f63d 1365 cli_printf(c, -1008, "\tType: %s %s", rta_src_names[a->source], ip_scope_text(a->scope));
13c0be19
JMM
1366
1367 for(ea_list *eal = a->eattrs; eal; eal=eal->next)
1368 for(int i=0; i<eal->count; i++)
fdf16eb6 1369 ea_show(c, &eal->attrs[i]);
730f2e2c
MM
1370}
1371
3ce8c610
MM
1372/**
1373 * rta_init - initialize route attribute cache
1374 *
1375 * This function is called during initialization of the routing
1376 * table module to set up the internals of the attribute cache.
1377 */
2326b001
MM
1378void
1379rta_init(void)
1380{
ed68a5c6 1381 rta_pool = rp_new(&root_pool, "Attributes");
ec5e5d23
JMM
1382
1383 rta_slab_[0] = sl_new(rta_pool, sizeof(rta));
1384 rta_slab_[1] = sl_new(rta_pool, sizeof(rta) + sizeof(u32));
1385 rta_slab_[2] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*2);
d14f8c3c 1386 rta_slab_[3] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
ec5e5d23
JMM
1387
1388 nexthop_slab_[0] = sl_new(rta_pool, sizeof(struct nexthop));
1389 nexthop_slab_[1] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32));
1390 nexthop_slab_[2] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*2);
d14f8c3c 1391 nexthop_slab_[3] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
ec5e5d23 1392
ee76a92a 1393 rta_alloc_hash();
094d2bdb 1394 rte_src_init();
2326b001 1395}
3ce8c610
MM
1396
1397/*
1398 * Documentation for functions declared inline in route.h
1399 */
1400#if 0
1401
1402/**
1403 * rta_clone - clone route attributes
1404 * @r: a &rta to be cloned
1405 *
1406 * rta_clone() takes a cached &rta and returns its identical cached
1407 * copy. Currently it works by just returning the original &rta with
1408 * its use count incremented.
1409 */
1410static inline rta *rta_clone(rta *r)
1411{ DUMMY; }
1412
1413/**
1414 * rta_free - free route attributes
1415 * @r: a &rta to be freed
1416 *
1417 * If you stop using a &rta (for example when deleting a route which uses
1418 * it), you need to call rta_free() to notify the attribute cache the
1419 * attribute is no longer in use and can be freed if you were the last
1420 * user (which rta_free() tests by inspecting the use count).
1421 */
1422static inline void rta_free(rta *r)
1423{ DUMMY; }
1424
1425#endif