Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-attr.c @ 7e95c05d

History | View | Annotate | Download (19.8 KB)

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/resource.h"
55
#include "lib/string.h"
56

    
57
pool *rta_pool;
58

    
59
static slab *rta_slab;
60
static slab *mpnh_slab;
61

    
62
struct protocol *attr_class_to_protocol[EAP_MAX];
63

    
64
static inline unsigned int
65
mpnh_hash(struct mpnh *x)
66
{
67
  unsigned int h = 0;
68
  for (; x; x = x->next)
69
    h ^= ipa_hash(x->gw);
70

    
71
  return h;
72
}
73

    
74
int
75
mpnh__same(struct mpnh *x, struct mpnh *y)
76
{
77
  for (; x && y; x = x->next, y = y->next)
78
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight))
79
      return 0;
80

    
81
  return x == y;
82
}
83

    
84
static struct mpnh *
85
mpnh_copy(struct mpnh *o)
86
{
87
  struct mpnh *first = NULL;
88
  struct mpnh **last = &first;
89

    
90
  for (; o; o = o->next)
91
    {
92
      struct mpnh *n = sl_alloc(mpnh_slab);
93
      n->gw = o->gw;
94
      n->iface = o->iface;
95
      n->next = NULL;
96
      n->weight = o->weight;
97

    
98
      *last = n;
99
      last = &(n->next);
100
    }
101

    
102
  return first;
103
}
104

    
105
static void
106
mpnh_free(struct mpnh *o)
107
{
108
  struct mpnh *n;
109

    
110
  while (o)
111
    {
112
      n = o->next;
113
      sl_free(mpnh_slab, o);
114
      o = n;
115
    }
116
}
117

    
118

    
119
/*
120
 *        Extended Attributes
121
 */
122

    
123
static inline eattr *
124
ea__find(ea_list *e, unsigned id)
125
{
126
  eattr *a;
127
  int l, r, m;
128

    
129
  while (e)
130
    {
131
      if (e->flags & EALF_BISECT)
132
        {
133
          l = 0;
134
          r = e->count - 1;
135
          while (l <= r)
136
            {
137
              m = (l+r) / 2;
138
              a = &e->attrs[m];
139
              if (a->id == id)
140
                return a;
141
              else if (a->id < id)
142
                l = m+1;
143
              else
144
                r = m-1;
145
            }
146
        }
147
      else
148
        for(m=0; m<e->count; m++)
149
          if (e->attrs[m].id == id)
150
            return &e->attrs[m];
151
      e = e->next;
152
    }
153
  return NULL;
154
}
155

    
156
/**
157
 * ea_find - find an extended attribute
158
 * @e: attribute list to search in
159
 * @id: attribute ID to search for
160
 *
161
 * Given an extended attribute list, ea_find() searches for a first
162
 * occurrence of an attribute with specified ID, returning either a pointer
163
 * to its &eattr structure or %NULL if no such attribute exists.
164
 */
165
eattr *
166
ea_find(ea_list *e, unsigned id)
167
{
168
  eattr *a = ea__find(e, id & EA_CODE_MASK);
169

    
170
  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
171
      !(id & EA_ALLOW_UNDEF))
172
    return NULL;
173
  return a;
174
}
175

    
176
/**
177
 * ea_get_int - fetch an integer attribute
178
 * @e: attribute list
179
 * @id: attribute ID
180
 * @def: default value
181
 *
182
 * This function is a shortcut for retrieving a value of an integer attribute
183
 * by calling ea_find() to find the attribute, extracting its value or returning
184
 * a provided default if no such attribute is present.
185
 */
186
int
187
ea_get_int(ea_list *e, unsigned id, int def)
188
{
189
  eattr *a = ea_find(e, id);
190
  if (!a)
191
    return def;
192
  return a->u.data;
193
}
194

    
195
static inline void
196
ea_do_sort(ea_list *e)
197
{
198
  unsigned n = e->count;
199
  eattr *a = e->attrs;
200
  eattr *b = alloca(n * sizeof(eattr));
201
  unsigned s, ss;
202

    
203
  /* We need to use a stable sorting algorithm, hence mergesort */
204
  do
205
    {
206
      s = ss = 0;
207
      while (s < n)
208
        {
209
          eattr *p, *q, *lo, *hi;
210
          p = b;
211
          ss = s;
212
          *p++ = a[s++];
213
          while (s < n && p[-1].id <= a[s].id)
214
            *p++ = a[s++];
215
          if (s < n)
216
            {
217
              q = p;
218
              *p++ = a[s++];
219
              while (s < n && p[-1].id <= a[s].id)
220
                *p++ = a[s++];
221
              lo = b;
222
              hi = q;
223
              s = ss;
224
              while (lo < q && hi < p)
225
                if (lo->id <= hi->id)
226
                  a[s++] = *lo++;
227
                else
228
                  a[s++] = *hi++;
229
              while (lo < q)
230
                a[s++] = *lo++;
231
              while (hi < p)
232
                a[s++] = *hi++;
233
            }
234
        }
235
    }
236
  while (ss);
237
}
238

    
239
static inline void
240
ea_do_prune(ea_list *e)
241
{
242
  eattr *s, *d, *l, *s0;
243
  int i = 0;
244

    
245
  /* Discard duplicates and undefs. Do you remember sorting was stable? */
246
  s = d = e->attrs;
247
  l = e->attrs + e->count;
248
  while (s < l)
249
    {
250
      s0 = s++;
251
      while (s < l && s->id == s[-1].id)
252
        s++;
253
      /* s0 is the most recent version, s[-1] the oldest one */
254
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
255
        {
256
          *d = *s0;
257
          d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED);
258
          d++;
259
          i++;
260
        }
261
    }
262
  e->count = i;
263
}
264

    
265
/**
266
 * ea_sort - sort an attribute list
267
 * @e: list to be sorted
268
 *
269
 * This function takes a &ea_list chain and sorts the attributes
270
 * within each of its entries.
271
 *
272
 * If an attribute occurs multiple times in a single &ea_list,
273
 * ea_sort() leaves only the first (the only significant) occurrence.
274
 */
275
void
276
ea_sort(ea_list *e)
277
{
278
  while (e)
279
    {
280
      if (!(e->flags & EALF_SORTED))
281
        {
282
          ea_do_sort(e);
283
          ea_do_prune(e);
284
          e->flags |= EALF_SORTED;
285
        }
286
      if (e->count > 5)
287
        e->flags |= EALF_BISECT;
288
      e = e->next;
289
    }
290
}
291

    
292
/**
293
 * ea_scan - estimate attribute list size
294
 * @e: attribute list
295
 *
296
 * This function calculates an upper bound of the size of
297
 * a given &ea_list after merging with ea_merge().
298
 */
299
unsigned
300
ea_scan(ea_list *e)
301
{
302
  unsigned cnt = 0;
303

    
304
  while (e)
305
    {
306
      cnt += e->count;
307
      e = e->next;
308
    }
309
  return sizeof(ea_list) + sizeof(eattr)*cnt;
310
}
311

    
312
/**
313
 * ea_merge - merge segments of an attribute list
314
 * @e: attribute list
315
 * @t: buffer to store the result to
316
 *
317
 * This function takes a possibly multi-segment attribute list
318
 * and merges all of its segments to one.
319
 *
320
 * The primary use of this function is for &ea_list normalization:
321
 * first call ea_scan() to determine how much memory will the result
322
 * take, then allocate a buffer (usually using alloca()), merge the
323
 * segments with ea_merge() and finally sort and prune the result
324
 * by calling ea_sort().
325
 */
326
void
327
ea_merge(ea_list *e, ea_list *t)
328
{
329
  eattr *d = t->attrs;
330

    
331
  t->flags = 0;
332
  t->count = 0;
333
  t->next = NULL;
334
  while (e)
335
    {
336
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
337
      t->count += e->count;
338
      d += e->count;
339
      e = e->next;
340
    }
341
}
342

    
343
/**
344
 * ea_same - compare two &ea_list's
345
 * @x: attribute list
346
 * @y: attribute list
347
 *
348
 * ea_same() compares two normalized attribute lists @x and @y and returns
349
 * 1 if they contain the same attributes, 0 otherwise.
350
 */
351
int
352
ea_same(ea_list *x, ea_list *y)
353
{
354
  int c;
355

    
356
  if (!x || !y)
357
    return x == y;
358
  ASSERT(!x->next && !y->next);
359
  if (x->count != y->count)
360
    return 0;
361
  for(c=0; c<x->count; c++)
362
    {
363
      eattr *a = &x->attrs[c];
364
      eattr *b = &y->attrs[c];
365

    
366
      if (a->id != b->id ||
367
          a->flags != b->flags ||
368
          a->type != b->type ||
369
          ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data :
370
           (a->u.ptr->length != b->u.ptr->length || memcmp(a->u.ptr->data, b->u.ptr->data, a->u.ptr->length))))
371
        return 0;
372
    }
373
  return 1;
374
}
375

    
376
static inline ea_list *
377
ea_list_copy(ea_list *o)
378
{
379
  ea_list *n;
380
  unsigned i, len;
381

    
382
  if (!o)
383
    return NULL;
384
  ASSERT(!o->next);
385
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
386
  n = mb_alloc(rta_pool, len);
387
  memcpy(n, o, len);
388
  n->flags |= EALF_CACHED;
389
  for(i=0; i<o->count; i++)
390
    {
391
      eattr *a = &n->attrs[i];
392
      if (!(a->type & EAF_EMBEDDED))
393
        {
394
          unsigned size = sizeof(struct adata) + a->u.ptr->length;
395
          struct adata *d = mb_alloc(rta_pool, size);
396
          memcpy(d, a->u.ptr, size);
397
          a->u.ptr = d;
398
        }
399
    }
400
  return n;
401
}
402

    
403
static inline void
404
ea_free(ea_list *o)
405
{
406
  int i;
407

    
408
  if (o)
409
    {
410
      ASSERT(!o->next);
411
      for(i=0; i<o->count; i++)
412
        {
413
          eattr *a = &o->attrs[i];
414
          if (!(a->type & EAF_EMBEDDED))
415
            mb_free(a->u.ptr);
416
        }
417
      mb_free(o);
418
    }
419
}
420

    
421
static int
422
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
423
{
424
  if (a->id == EA_GEN_IGP_METRIC)
425
    {
426
      *buf += bsprintf(*buf, "igp_metric");
427
      return GA_NAME;
428
    }
429
 
430
  return GA_UNKNOWN;
431
}
432

    
433
/**
434
 * ea_format - format an &eattr for printing
435
 * @e: attribute to be formatted
436
 * @buf: destination buffer of size %EA_FORMAT_BUF_SIZE
437
 *
438
 * This function takes an extended attribute represented by its
439
 * &eattr structure and formats it nicely for printing according
440
 * to the type information.
441
 *
442
 * If the protocol defining the attribute provides its own
443
 * get_attr() hook, it's consulted first.
444
 */
445
void
446
ea_format(eattr *e, byte *buf)
447
{
448
  struct protocol *p;
449
  int status = GA_UNKNOWN;
450
  unsigned int i;
451
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
452
  byte *end = buf + EA_FORMAT_BUF_SIZE - 1;
453

    
454
  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
455
    {
456
      buf += bsprintf(buf, "%s.", p->name);
457
      if (p->get_attr)
458
        status = p->get_attr(e, buf, end - buf);
459
      buf += strlen(buf);
460
    }
461
  else if (EA_PROTO(e->id))
462
    buf += bsprintf(buf, "%02x.", EA_PROTO(e->id));
463
  else 
464
    status = get_generic_attr(e, &buf, end - buf);
465

    
466
  if (status < GA_NAME)
467
    buf += bsprintf(buf, "%02x", EA_ID(e->id));
468
  if (status < GA_FULL)
469
    {
470
      *buf++ = ':';
471
      *buf++ = ' ';
472
      switch (e->type & EAF_TYPE_MASK)
473
        {
474
        case EAF_TYPE_INT:
475
          bsprintf(buf, "%u", e->u.data);
476
          break;
477
        case EAF_TYPE_OPAQUE:
478
          *buf = 0;
479
          for(i=0; i<ad->length; i++)
480
            {
481
              if (buf > end - 8)
482
                {
483
                  strcpy(buf, " ...");
484
                  break;
485
                }
486
              if (i)
487
                *buf++ = ' ';
488
              buf += bsprintf(buf, "%02x", ad->data[i]);
489
            }
490
          break;
491
        case EAF_TYPE_IP_ADDRESS:
492
          bsprintf(buf, "%I", *(ip_addr *) ad->data);
493
          break;
494
        case EAF_TYPE_ROUTER_ID:
495
          bsprintf(buf, "%R", e->u.data);
496
          break;
497
        case EAF_TYPE_AS_PATH:
498
          as_path_format(ad, buf, end - buf);
499
          break;
500
        case EAF_TYPE_INT_SET:
501
          int_set_format(ad, 1, buf, end - buf);
502
          break;
503
        case EAF_TYPE_UNDEF:
504
        default:
505
          bsprintf(buf, "<type %02x>", e->type);
506
        }
507
    }
508
}
509

    
510
/**
511
 * ea_dump - dump an extended attribute
512
 * @e: attribute to be dumped
513
 *
514
 * ea_dump() dumps contents of the extended attribute given to
515
 * the debug output.
516
 */
517
void
518
ea_dump(ea_list *e)
519
{
520
  int i;
521

    
522
  if (!e)
523
    {
524
      debug("NONE");
525
      return;
526
    }
527
  while (e)
528
    {
529
      debug("[%c%c%c]",
530
            (e->flags & EALF_SORTED) ? 'S' : 's',
531
            (e->flags & EALF_BISECT) ? 'B' : 'b',
532
            (e->flags & EALF_CACHED) ? 'C' : 'c');
533
      for(i=0; i<e->count; i++)
534
        {
535
          eattr *a = &e->attrs[i];
536
          debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
537
          if (a->type & EAF_TEMP)
538
            debug("T");
539
          debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
540
          if (a->type & EAF_ORIGINATED)
541
            debug("o");
542
          if (a->type & EAF_EMBEDDED)
543
            debug(":%08x", a->u.data);
544
          else
545
            {
546
              int j, len = a->u.ptr->length;
547
              debug("[%d]:", len);
548
              for(j=0; j<len; j++)
549
                debug("%02x", a->u.ptr->data[j]);
550
            }
551
        }
552
      if (e = e->next)
553
        debug(" | ");
554
    }
555
}
556

    
557
/**
558
 * ea_hash - calculate an &ea_list hash key
559
 * @e: attribute list
560
 *
561
 * ea_hash() takes an extended attribute list and calculated a hopefully
562
 * uniformly distributed hash value from its contents.
563
 */
564
inline unsigned int
565
ea_hash(ea_list *e)
566
{
567
  u32 h = 0;
568
  int i;
569

    
570
  if (e)                        /* Assuming chain of length 1 */
571
    {
572
      for(i=0; i<e->count; i++)
573
        {
574
          struct eattr *a = &e->attrs[i];
575
          h ^= a->id;
576
          if (a->type & EAF_EMBEDDED)
577
            h ^= a->u.data;
578
          else
579
            {
580
              struct adata *d = a->u.ptr;
581
              int size = d->length;
582
              byte *z = d->data;
583
              while (size >= 4)
584
                {
585
                  h ^= *(u32 *)z;
586
                  z += 4;
587
                  size -= 4;
588
                }
589
              while (size--)
590
                h = (h >> 24) ^ (h << 8) ^ *z++;
591
            }
592
        }
593
      h ^= h >> 16;
594
      h ^= h >> 6;
595
      h &= 0xffff;
596
    }
597
  return h;
598
}
599

    
600
/**
601
 * ea_append - concatenate &ea_list's
602
 * @to: destination list (can be %NULL)
603
 * @what: list to be appended (can be %NULL)
604
 *
605
 * This function appends the &ea_list @what at the end of
606
 * &ea_list @to and returns a pointer to the resulting list.
607
 */
608
ea_list *
609
ea_append(ea_list *to, ea_list *what)
610
{
611
  ea_list *res;
612

    
613
  if (!to)
614
    return what;
615
  res = to;
616
  while (to->next)
617
    to = to->next;
618
  to->next = what;
619
  return res;
620
}
621

    
622
/*
623
 *        rta's
624
 */
625

    
626
static unsigned int rta_cache_count;
627
static unsigned int rta_cache_size = 32;
628
static unsigned int rta_cache_limit;
629
static unsigned int rta_cache_mask;
630
static rta **rta_hash_table;
631

    
632
static void
633
rta_alloc_hash(void)
634
{
635
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
636
  if (rta_cache_size < 32768)
637
    rta_cache_limit = rta_cache_size * 2;
638
  else
639
    rta_cache_limit = ~0;
640
  rta_cache_mask = rta_cache_size - 1;
641
}
642

    
643
static inline unsigned int
644
rta_hash(rta *a)
645
{
646
  return (a->proto->hash_key ^ ipa_hash(a->gw) ^
647
          mpnh_hash(a->nexthops) ^ ea_hash(a->eattrs)) & 0xffff;
648
}
649

    
650
static inline int
651
rta_same(rta *x, rta *y)
652
{
653
  return (x->proto == y->proto &&
654
          x->source == y->source &&
655
          x->scope == y->scope &&
656
          x->cast == y->cast &&
657
          x->dest == y->dest &&
658
          x->flags == y->flags &&
659
          x->igp_metric == y->igp_metric &&
660
          ipa_equal(x->gw, y->gw) &&
661
          ipa_equal(x->from, y->from) &&
662
          x->iface == y->iface &&
663
          x->hostentry == y->hostentry &&
664
          mpnh_same(x->nexthops, y->nexthops) &&
665
          ea_same(x->eattrs, y->eattrs));
666
}
667

    
668
static rta *
669
rta_copy(rta *o)
670
{
671
  rta *r = sl_alloc(rta_slab);
672

    
673
  memcpy(r, o, sizeof(rta));
674
  r->uc = 1;
675
  r->nexthops = mpnh_copy(o->nexthops);
676
  r->eattrs = ea_list_copy(o->eattrs);
677
  return r;
678
}
679

    
680
static inline void
681
rta_insert(rta *r)
682
{
683
  unsigned int h = r->hash_key & rta_cache_mask;
684
  r->next = rta_hash_table[h];
685
  if (r->next)
686
    r->next->pprev = &r->next;
687
  r->pprev = &rta_hash_table[h];
688
  rta_hash_table[h] = r;
689
}
690

    
691
static void
692
rta_rehash(void)
693
{
694
  unsigned int ohs = rta_cache_size;
695
  unsigned int h;
696
  rta *r, *n;
697
  rta **oht = rta_hash_table;
698

    
699
  rta_cache_size = 2*rta_cache_size;
700
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
701
  rta_alloc_hash();
702
  for(h=0; h<ohs; h++)
703
    for(r=oht[h]; r; r=n)
704
      {
705
        n = r->next;
706
        rta_insert(r);
707
      }
708
  mb_free(oht);
709
}
710

    
711
/**
712
 * rta_lookup - look up a &rta in attribute cache
713
 * @o: a un-cached &rta
714
 *
715
 * rta_lookup() gets an un-cached &rta structure and returns its cached
716
 * counterpart. It starts with examining the attribute cache to see whether
717
 * there exists a matching entry. If such an entry exists, it's returned and
718
 * its use count is incremented, else a new entry is created with use count
719
 * set to 1.
720
 *
721
 * The extended attribute lists attached to the &rta are automatically
722
 * converted to the normalized form.
723
 */
724
rta *
725
rta_lookup(rta *o)
726
{
727
  rta *r;
728
  unsigned int h;
729

    
730
  ASSERT(!(o->aflags & RTAF_CACHED));
731
  if (o->eattrs)
732
    {
733
      if (o->eattrs->next)        /* Multiple ea_list's, need to merge them */
734
        {
735
          ea_list *ml = alloca(ea_scan(o->eattrs));
736
          ea_merge(o->eattrs, ml);
737
          o->eattrs = ml;
738
        }
739
      ea_sort(o->eattrs);
740
    }
741

    
742
  h = rta_hash(o);
743
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
744
    if (r->hash_key == h && rta_same(r, o))
745
      return rta_clone(r);
746

    
747
  r = rta_copy(o);
748
  r->hash_key = h;
749
  r->aflags = RTAF_CACHED;
750
  rt_lock_hostentry(r->hostentry);
751
  rta_insert(r);
752

    
753
  if (++rta_cache_count > rta_cache_limit)
754
    rta_rehash();
755

    
756
  return r;
757
}
758

    
759
void
760
rta__free(rta *a)
761
{
762
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
763
  rta_cache_count--;
764
  *a->pprev = a->next;
765
  if (a->next)
766
    a->next->pprev = a->pprev;
767
  a->aflags = 0;                /* Poison the entry */
768
  rt_unlock_hostentry(a->hostentry);
769
  mpnh_free(a->nexthops);
770
  ea_free(a->eattrs);
771
  sl_free(rta_slab, a);
772
}
773

    
774
/**
775
 * rta_dump - dump route attributes
776
 * @a: attribute structure to dump
777
 *
778
 * This function takes a &rta and dumps its contents to the debug output.
779
 */
780
void
781
rta_dump(rta *a)
782
{
783
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
784
                         "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
785
                         "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
786
                         "RTS_OSPF_EXT2", "RTS_BGP" };
787
  static char *rtc[] = { "", " BC", " MC", " AC" };
788
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
789

    
790
  debug("p=%s uc=%d %s %s%s%s h=%04x",
791
        a->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
792
        rtd[a->dest], a->hash_key);
793
  if (!(a->aflags & RTAF_CACHED))
794
    debug(" !CACHED");
795
  debug(" <-%I", a->from);
796
  if (a->dest == RTD_ROUTER)
797
    debug(" ->%I", a->gw);
798
  if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER)
799
    debug(" [%s]", a->iface ? a->iface->name : "???" );
800
  if (a->eattrs)
801
    {
802
      debug(" EA: ");
803
      ea_dump(a->eattrs);
804
    }
805
}
806

    
807
/**
808
 * rta_dump_all - dump attribute cache
809
 *
810
 * This function dumps the whole contents of route attribute cache
811
 * to the debug output.
812
 */
813
void
814
rta_dump_all(void)
815
{
816
  rta *a;
817
  unsigned int h;
818

    
819
  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
820
  for(h=0; h<rta_cache_size; h++)
821
    for(a=rta_hash_table[h]; a; a=a->next)
822
      {
823
        debug("%p ", a);
824
        rta_dump(a);
825
        debug("\n");
826
      }
827
  debug("\n");
828
}
829

    
830
void
831
rta_show(struct cli *c, rta *a, ea_list *eal)
832
{
833
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
834
                               "RIP", "OSPF", "OSPF-ext", "OSPF-IA", "OSPF-boundary", "BGP" };
835
  static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
836
  int i;
837
  byte buf[EA_FORMAT_BUF_SIZE];
838

    
839
  cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
840
  if (!eal)
841
    eal = a->eattrs;
842
  for(; eal; eal=eal->next)
843
    for(i=0; i<eal->count; i++)
844
      {
845
        ea_format(&eal->attrs[i], buf);
846
        cli_printf(c, -1012, "\t%s", buf);
847
      }
848
}
849

    
850
/**
851
 * rta_init - initialize route attribute cache
852
 *
853
 * This function is called during initialization of the routing
854
 * table module to set up the internals of the attribute cache.
855
 */
856
void
857
rta_init(void)
858
{
859
  rta_pool = rp_new(&root_pool, "Attributes");
860
  rta_slab = sl_new(rta_pool, sizeof(rta));
861
  mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh));
862
  rta_alloc_hash();
863
}
864

    
865
/*
866
 *  Documentation for functions declared inline in route.h
867
 */
868
#if 0
869

870
/**
871
 * rta_clone - clone route attributes
872
 * @r: a &rta to be cloned
873
 *
874
 * rta_clone() takes a cached &rta and returns its identical cached
875
 * copy. Currently it works by just returning the original &rta with
876
 * its use count incremented.
877
 */
878
static inline rta *rta_clone(rta *r)
879
{ DUMMY; }
880

881
/**
882
 * rta_free - free route attributes
883
 * @r: a &rta to be freed
884
 *
885
 * If you stop using a &rta (for example when deleting a route which uses
886
 * it), you need to call rta_free() to notify the attribute cache the
887
 * attribute is no longer in use and can be freed if you were the last
888
 * user (which rta_free() tests by inspecting the use count).
889
 */
890
static inline void rta_free(rta *r)
891
{ DUMMY; }
892

893
#endif