Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-attr.c @ ae80a2de

History | View | Annotate | Download (25.1 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/hash.h"
55
#include "lib/resource.h"
56
#include "lib/string.h"
57

    
58
pool *rta_pool;
59

    
60
static slab *rta_slab;
61
static slab *mpnh_slab;
62
static slab *rte_src_slab;
63

    
64
/* rte source ID bitmap */
65
static u32 *src_ids;
66
static u32 src_id_size, src_id_used, src_id_pos;
67
#define SRC_ID_INIT_SIZE 4
68

    
69
/* rte source hash */
70

    
71
#define RSH_KEY(n)                n->proto, n->private_id
72
#define RSH_NEXT(n)                n->next
73
#define RSH_EQ(p1,n1,p2,n2)        p1 == p2 && n1 == n2
74
#define RSH_FN(p,n)                p->hash_key ^ u32_hash(n)
75

    
76
#define RSH_REHASH                rte_src_rehash
77
#define RSH_PARAMS                /2, *2, 1, 1, 8, 20
78
#define RSH_INIT_ORDER                6
79

    
80
static HASH(struct rte_src) src_hash;
81

    
82
struct protocol *attr_class_to_protocol[EAP_MAX];
83

    
84

    
85
static void
86
rte_src_init(void)
87
{
88
  rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
89

    
90
  src_id_pos = 0;
91
  src_id_size = SRC_ID_INIT_SIZE;
92
  src_ids = mb_allocz(rta_pool, src_id_size * sizeof(u32));
93

    
94
 /* ID 0 is reserved */
95
  src_ids[0] = 1;
96
  src_id_used = 1;
97

    
98
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
99
}
100

    
101
static inline int u32_cto(uint x) { return ffs(~x) - 1; }
102

    
103
static inline u32
104
rte_src_alloc_id(void)
105
{
106
  int i, j;
107
  for (i = src_id_pos; i < src_id_size; i++)
108
    if (src_ids[i] != 0xffffffff)
109
      goto found;
110

    
111
  /* If we are at least 7/8 full, expand */
112
  if (src_id_used > (src_id_size * 28))
113
    {
114
      src_id_size *= 2;
115
      src_ids = mb_realloc(src_ids, src_id_size * sizeof(u32));
116
      bzero(src_ids + i, (src_id_size - i) * sizeof(u32));
117
      goto found;
118
    }
119

    
120
  for (i = 0; i < src_id_pos; i++)
121
    if (src_ids[i] != 0xffffffff)
122
      goto found;
123

    
124
  ASSERT(0);
125

    
126
 found:
127
  ASSERT(i < 0x8000000);
128

    
129
  src_id_pos = i;
130
  j = u32_cto(src_ids[i]);
131

    
132
  src_ids[i] |= (1 << j);
133
  src_id_used++;
134
  return 32 * i + j;
135
}
136

    
137
static inline void
138
rte_src_free_id(u32 id)
139
{
140
  int i = id / 32;
141
  int j = id % 32;
142

    
143
  ASSERT((i < src_id_size) && (src_ids[i] & (1 << j)));
144
  src_ids[i] &= ~(1 << j);
145
  src_id_used--;
146
}
147

    
148

    
149
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
150

    
151
struct rte_src *
152
rt_find_source(struct proto *p, u32 id)
153
{
154
  return HASH_FIND(src_hash, RSH, p, id);
155
}
156

    
157
struct rte_src *
158
rt_get_source(struct proto *p, u32 id)
159
{
160
  struct rte_src *src = rt_find_source(p, id);
161

    
162
  if (src)
163
    return src;
164

    
165
  src = sl_alloc(rte_src_slab);
166
  src->proto = p;
167
  src->private_id = id;
168
  src->global_id = rte_src_alloc_id();
169
  src->uc = 0;
170
  
171
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
172

    
173
  return src;
174
}
175

    
176
void
177
rt_prune_sources(void)
178
{
179
  HASH_WALK_FILTER(src_hash, next, src, sp)
180
  {
181
    if (src->uc == 0)
182
    {
183
      HASH_DO_REMOVE(src_hash, RSH, sp);
184
      rte_src_free_id(src->global_id);
185
      sl_free(rte_src_slab, src);
186
    }
187
  }
188
  HASH_WALK_FILTER_END;
189

    
190
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
191
}
192

    
193

    
194
/*
195
 *        Multipath Next Hop
196
 */
197

    
198
static inline uint
199
mpnh_hash(struct mpnh *x)
200
{
201
  uint h = 0;
202
  for (; x; x = x->next)
203
    h ^= ipa_hash(x->gw);
204

    
205
  return h;
206
}
207

    
208
int
209
mpnh__same(struct mpnh *x, struct mpnh *y)
210
{
211
  for (; x && y; x = x->next, y = y->next)
212
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight))
213
      return 0;
214

    
215
  return x == y;
216
}
217

    
218
static struct mpnh *
219
mpnh_copy(struct mpnh *o)
220
{
221
  struct mpnh *first = NULL;
222
  struct mpnh **last = &first;
223

    
224
  for (; o; o = o->next)
225
    {
226
      struct mpnh *n = sl_alloc(mpnh_slab);
227
      n->gw = o->gw;
228
      n->iface = o->iface;
229
      n->next = NULL;
230
      n->weight = o->weight;
231

    
232
      *last = n;
233
      last = &(n->next);
234
    }
235

    
236
  return first;
237
}
238

    
239
static void
240
mpnh_free(struct mpnh *o)
241
{
242
  struct mpnh *n;
243

    
244
  while (o)
245
    {
246
      n = o->next;
247
      sl_free(mpnh_slab, o);
248
      o = n;
249
    }
250
}
251

    
252

    
253
/*
254
 *        Extended Attributes
255
 */
256

    
257
static inline eattr *
258
ea__find(ea_list *e, unsigned id)
259
{
260
  eattr *a;
261
  int l, r, m;
262

    
263
  while (e)
264
    {
265
      if (e->flags & EALF_BISECT)
266
        {
267
          l = 0;
268
          r = e->count - 1;
269
          while (l <= r)
270
            {
271
              m = (l+r) / 2;
272
              a = &e->attrs[m];
273
              if (a->id == id)
274
                return a;
275
              else if (a->id < id)
276
                l = m+1;
277
              else
278
                r = m-1;
279
            }
280
        }
281
      else
282
        for(m=0; m<e->count; m++)
283
          if (e->attrs[m].id == id)
284
            return &e->attrs[m];
285
      e = e->next;
286
    }
287
  return NULL;
288
}
289

    
290
/**
291
 * ea_find - find an extended attribute
292
 * @e: attribute list to search in
293
 * @id: attribute ID to search for
294
 *
295
 * Given an extended attribute list, ea_find() searches for a first
296
 * occurrence of an attribute with specified ID, returning either a pointer
297
 * to its &eattr structure or %NULL if no such attribute exists.
298
 */
299
eattr *
300
ea_find(ea_list *e, unsigned id)
301
{
302
  eattr *a = ea__find(e, id & EA_CODE_MASK);
303

    
304
  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
305
      !(id & EA_ALLOW_UNDEF))
306
    return NULL;
307
  return a;
308
}
309

    
310
/**
311
 * ea_walk - walk through extended attributes
312
 * @s: walk state structure
313
 * @id: start of attribute ID interval
314
 * @max: length of attribute ID interval
315
 *
316
 * Given an extended attribute list, ea_walk() walks through the list looking
317
 * for first occurrences of attributes with ID in specified interval from @id to
318
 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
319
 * walk state in @s for subsequent calls.
320

321
 * The function ea_walk() is supposed to be called in a loop, with initially
322
 * zeroed walk state structure @s with filled the initial extended attribute
323
 * list, returning one found attribute in each call or %NULL when no other
324
 * attribute exists. The extended attribute list or the arguments should not be
325
 * modified between calls. The maximum value of @max is 128.
326
 */
327
eattr *
328
ea_walk(struct ea_walk_state *s, uint id, uint max)
329
{
330
  ea_list *e = s->eattrs;
331
  eattr *a = s->ea;
332
  eattr *a_max;
333

    
334
  max = id + max;
335

    
336
  if (a)
337
    goto step;
338

    
339
  for (; e; e = e->next)
340
  {
341
    if (e->flags & EALF_BISECT)
342
    {
343
      int l, r, m;
344

    
345
      l = 0;
346
      r = e->count - 1;
347
      while (l < r)
348
      {
349
        m = (l+r) / 2;
350
        if (e->attrs[m].id < id)
351
          l = m + 1;
352
        else
353
          r = m;
354
      }
355
      a = e->attrs + l;
356
    }
357
    else
358
      a = e->attrs;
359

    
360
  step:
361
    a_max = e->attrs + e->count;
362
    for (; a < a_max; a++)
363
      if ((a->id >= id) && (a->id < max))
364
      {
365
        int n = a->id - id;
366

    
367
        if (BIT32_TEST(s->visited, n))
368
          continue;
369

    
370
        BIT32_SET(s->visited, n);
371

    
372
        if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
373
          continue;
374

    
375
        s->eattrs = e;
376
        s->ea = a;
377
        return a;
378
      }
379
      else if (e->flags & EALF_BISECT)
380
        break;
381
  }
382

    
383
  return NULL;
384
}
385

    
386
/**
387
 * ea_get_int - fetch an integer attribute
388
 * @e: attribute list
389
 * @id: attribute ID
390
 * @def: default value
391
 *
392
 * This function is a shortcut for retrieving a value of an integer attribute
393
 * by calling ea_find() to find the attribute, extracting its value or returning
394
 * a provided default if no such attribute is present.
395
 */
396
int
397
ea_get_int(ea_list *e, unsigned id, int def)
398
{
399
  eattr *a = ea_find(e, id);
400
  if (!a)
401
    return def;
402
  return a->u.data;
403
}
404

    
405
static inline void
406
ea_do_sort(ea_list *e)
407
{
408
  unsigned n = e->count;
409
  eattr *a = e->attrs;
410
  eattr *b = alloca(n * sizeof(eattr));
411
  unsigned s, ss;
412

    
413
  /* We need to use a stable sorting algorithm, hence mergesort */
414
  do
415
    {
416
      s = ss = 0;
417
      while (s < n)
418
        {
419
          eattr *p, *q, *lo, *hi;
420
          p = b;
421
          ss = s;
422
          *p++ = a[s++];
423
          while (s < n && p[-1].id <= a[s].id)
424
            *p++ = a[s++];
425
          if (s < n)
426
            {
427
              q = p;
428
              *p++ = a[s++];
429
              while (s < n && p[-1].id <= a[s].id)
430
                *p++ = a[s++];
431
              lo = b;
432
              hi = q;
433
              s = ss;
434
              while (lo < q && hi < p)
435
                if (lo->id <= hi->id)
436
                  a[s++] = *lo++;
437
                else
438
                  a[s++] = *hi++;
439
              while (lo < q)
440
                a[s++] = *lo++;
441
              while (hi < p)
442
                a[s++] = *hi++;
443
            }
444
        }
445
    }
446
  while (ss);
447
}
448

    
449
static inline void
450
ea_do_prune(ea_list *e)
451
{
452
  eattr *s, *d, *l, *s0;
453
  int i = 0;
454

    
455
  /* Discard duplicates and undefs. Do you remember sorting was stable? */
456
  s = d = e->attrs;
457
  l = e->attrs + e->count;
458
  while (s < l)
459
    {
460
      s0 = s++;
461
      while (s < l && s->id == s[-1].id)
462
        s++;
463
      /* s0 is the most recent version, s[-1] the oldest one */
464
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
465
        {
466
          *d = *s0;
467
          d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED);
468
          d++;
469
          i++;
470
        }
471
    }
472
  e->count = i;
473
}
474

    
475
/**
476
 * ea_sort - sort an attribute list
477
 * @e: list to be sorted
478
 *
479
 * This function takes a &ea_list chain and sorts the attributes
480
 * within each of its entries.
481
 *
482
 * If an attribute occurs multiple times in a single &ea_list,
483
 * ea_sort() leaves only the first (the only significant) occurrence.
484
 */
485
void
486
ea_sort(ea_list *e)
487
{
488
  while (e)
489
    {
490
      if (!(e->flags & EALF_SORTED))
491
        {
492
          ea_do_sort(e);
493
          ea_do_prune(e);
494
          e->flags |= EALF_SORTED;
495
        }
496
      if (e->count > 5)
497
        e->flags |= EALF_BISECT;
498
      e = e->next;
499
    }
500
}
501

    
502
/**
503
 * ea_scan - estimate attribute list size
504
 * @e: attribute list
505
 *
506
 * This function calculates an upper bound of the size of
507
 * a given &ea_list after merging with ea_merge().
508
 */
509
unsigned
510
ea_scan(ea_list *e)
511
{
512
  unsigned cnt = 0;
513

    
514
  while (e)
515
    {
516
      cnt += e->count;
517
      e = e->next;
518
    }
519
  return sizeof(ea_list) + sizeof(eattr)*cnt;
520
}
521

    
522
/**
523
 * ea_merge - merge segments of an attribute list
524
 * @e: attribute list
525
 * @t: buffer to store the result to
526
 *
527
 * This function takes a possibly multi-segment attribute list
528
 * and merges all of its segments to one.
529
 *
530
 * The primary use of this function is for &ea_list normalization:
531
 * first call ea_scan() to determine how much memory will the result
532
 * take, then allocate a buffer (usually using alloca()), merge the
533
 * segments with ea_merge() and finally sort and prune the result
534
 * by calling ea_sort().
535
 */
536
void
537
ea_merge(ea_list *e, ea_list *t)
538
{
539
  eattr *d = t->attrs;
540

    
541
  t->flags = 0;
542
  t->count = 0;
543
  t->next = NULL;
544
  while (e)
545
    {
546
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
547
      t->count += e->count;
548
      d += e->count;
549
      e = e->next;
550
    }
551
}
552

    
553
/**
554
 * ea_same - compare two &ea_list's
555
 * @x: attribute list
556
 * @y: attribute list
557
 *
558
 * ea_same() compares two normalized attribute lists @x and @y and returns
559
 * 1 if they contain the same attributes, 0 otherwise.
560
 */
561
int
562
ea_same(ea_list *x, ea_list *y)
563
{
564
  int c;
565

    
566
  if (!x || !y)
567
    return x == y;
568
  ASSERT(!x->next && !y->next);
569
  if (x->count != y->count)
570
    return 0;
571
  for(c=0; c<x->count; c++)
572
    {
573
      eattr *a = &x->attrs[c];
574
      eattr *b = &y->attrs[c];
575

    
576
      if (a->id != b->id ||
577
          a->flags != b->flags ||
578
          a->type != b->type ||
579
          ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
580
        return 0;
581
    }
582
  return 1;
583
}
584

    
585
static inline ea_list *
586
ea_list_copy(ea_list *o)
587
{
588
  ea_list *n;
589
  unsigned i, len;
590

    
591
  if (!o)
592
    return NULL;
593
  ASSERT(!o->next);
594
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
595
  n = mb_alloc(rta_pool, len);
596
  memcpy(n, o, len);
597
  n->flags |= EALF_CACHED;
598
  for(i=0; i<o->count; i++)
599
    {
600
      eattr *a = &n->attrs[i];
601
      if (!(a->type & EAF_EMBEDDED))
602
        {
603
          unsigned size = sizeof(struct adata) + a->u.ptr->length;
604
          struct adata *d = mb_alloc(rta_pool, size);
605
          memcpy(d, a->u.ptr, size);
606
          a->u.ptr = d;
607
        }
608
    }
609
  return n;
610
}
611

    
612
static inline void
613
ea_free(ea_list *o)
614
{
615
  int i;
616

    
617
  if (o)
618
    {
619
      ASSERT(!o->next);
620
      for(i=0; i<o->count; i++)
621
        {
622
          eattr *a = &o->attrs[i];
623
          if (!(a->type & EAF_EMBEDDED))
624
            mb_free(a->u.ptr);
625
        }
626
      mb_free(o);
627
    }
628
}
629

    
630
static int
631
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
632
{
633
  if (a->id == EA_GEN_IGP_METRIC)
634
    {
635
      *buf += bsprintf(*buf, "igp_metric");
636
      return GA_NAME;
637
    }
638
 
639
  return GA_UNKNOWN;
640
}
641

    
642
void
643
ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
644
{
645
  byte *bound = buf + bufsize - 32;
646
  u32 data = a->u.data;
647
  int i;
648

    
649
  for (i = min; i < max; i++)
650
    if ((data & (1u << i)) && names[i])
651
    {
652
      if (buf > bound)
653
      {
654
        strcpy(buf, " ...");
655
        return;
656
      }
657

    
658
      buf += bsprintf(buf, " %s", names[i]);
659
      data &= ~(1u << i);
660
    }
661

    
662
  if (data)
663
    bsprintf(buf, " %08x", data);
664

    
665
  return;
666
}
667

    
668
static inline void
669
opaque_format(struct adata *ad, byte *buf, uint size)
670
{
671
  byte *bound = buf + size - 10;
672
  int i;
673

    
674
  for(i = 0; i < ad->length; i++)
675
    {
676
      if (buf > bound)
677
        {
678
          strcpy(buf, " ...");
679
          return;
680
        }
681
      if (i)
682
        *buf++ = ' ';
683

    
684
      buf += bsprintf(buf, "%02x", ad->data[i]);
685
    }
686

    
687
  *buf = 0;
688
  return;
689
}
690

    
691
static inline void
692
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
693
{
694
  int i = int_set_format(ad, way, 0, pos, end - pos);
695
  cli_printf(c, -1012, "\t%s", buf);
696
  while (i)
697
    {
698
      i = int_set_format(ad, way, i, buf, end - buf - 1);
699
      cli_printf(c, -1012, "\t\t%s", buf);
700
    }
701
}
702

    
703
static inline void
704
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
705
{
706
  int i = ec_set_format(ad, 0, pos, end - pos);
707
  cli_printf(c, -1012, "\t%s", buf);
708
  while (i)
709
    {
710
      i = ec_set_format(ad, i, buf, end - buf - 1);
711
      cli_printf(c, -1012, "\t\t%s", buf);
712
    }
713
}
714

    
715
/**
716
 * ea_show - print an &eattr to CLI
717
 * @c: destination CLI
718
 * @e: attribute to be printed
719
 *
720
 * This function takes an extended attribute represented by its &eattr
721
 * structure and prints it to the CLI according to the type information.
722
 *
723
 * If the protocol defining the attribute provides its own
724
 * get_attr() hook, it's consulted first.
725
 */
726
void
727
ea_show(struct cli *c, eattr *e)
728
{
729
  struct protocol *p;
730
  int status = GA_UNKNOWN;
731
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
732
  byte buf[CLI_MSG_SIZE];
733
  byte *pos = buf, *end = buf + sizeof(buf);
734

    
735
  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
736
    {
737
      pos += bsprintf(pos, "%s.", p->name);
738
      if (p->get_attr)
739
        status = p->get_attr(e, pos, end - pos);
740
      pos += strlen(pos);
741
    }
742
  else if (EA_PROTO(e->id))
743
    pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
744
  else 
745
    status = get_generic_attr(e, &pos, end - pos);
746

    
747
  if (status < GA_NAME)
748
    pos += bsprintf(pos, "%02x", EA_ID(e->id));
749
  if (status < GA_FULL)
750
    {
751
      *pos++ = ':';
752
      *pos++ = ' ';
753
      switch (e->type & EAF_TYPE_MASK)
754
        {
755
        case EAF_TYPE_INT:
756
          bsprintf(pos, "%u", e->u.data);
757
          break;
758
        case EAF_TYPE_OPAQUE:
759
          opaque_format(ad, pos, end - pos);
760
          break;
761
        case EAF_TYPE_IP_ADDRESS:
762
          bsprintf(pos, "%I", *(ip_addr *) ad->data);
763
          break;
764
        case EAF_TYPE_ROUTER_ID:
765
          bsprintf(pos, "%R", e->u.data);
766
          break;
767
        case EAF_TYPE_AS_PATH:
768
          as_path_format(ad, pos, end - pos);
769
          break;
770
        case EAF_TYPE_BITFIELD:
771
          bsprintf(pos, "%08x", e->u.data);
772
          break;
773
        case EAF_TYPE_INT_SET:
774
          ea_show_int_set(c, ad, 1, pos, buf, end);
775
          return;
776
        case EAF_TYPE_EC_SET:
777
          ea_show_ec_set(c, ad, pos, buf, end);
778
          return;
779
        case EAF_TYPE_UNDEF:
780
        default:
781
          bsprintf(pos, "<type %02x>", e->type);
782
        }
783
    }
784
  cli_printf(c, -1012, "\t%s", buf);
785
}
786

    
787
/**
788
 * ea_dump - dump an extended attribute
789
 * @e: attribute to be dumped
790
 *
791
 * ea_dump() dumps contents of the extended attribute given to
792
 * the debug output.
793
 */
794
void
795
ea_dump(ea_list *e)
796
{
797
  int i;
798

    
799
  if (!e)
800
    {
801
      debug("NONE");
802
      return;
803
    }
804
  while (e)
805
    {
806
      debug("[%c%c%c]",
807
            (e->flags & EALF_SORTED) ? 'S' : 's',
808
            (e->flags & EALF_BISECT) ? 'B' : 'b',
809
            (e->flags & EALF_CACHED) ? 'C' : 'c');
810
      for(i=0; i<e->count; i++)
811
        {
812
          eattr *a = &e->attrs[i];
813
          debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
814
          if (a->type & EAF_TEMP)
815
            debug("T");
816
          debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
817
          if (a->type & EAF_ORIGINATED)
818
            debug("o");
819
          if (a->type & EAF_EMBEDDED)
820
            debug(":%08x", a->u.data);
821
          else
822
            {
823
              int j, len = a->u.ptr->length;
824
              debug("[%d]:", len);
825
              for(j=0; j<len; j++)
826
                debug("%02x", a->u.ptr->data[j]);
827
            }
828
        }
829
      if (e = e->next)
830
        debug(" | ");
831
    }
832
}
833

    
834
/**
835
 * ea_hash - calculate an &ea_list hash key
836
 * @e: attribute list
837
 *
838
 * ea_hash() takes an extended attribute list and calculated a hopefully
839
 * uniformly distributed hash value from its contents.
840
 */
841
inline uint
842
ea_hash(ea_list *e)
843
{
844
  u32 h = 0;
845
  int i;
846

    
847
  if (e)                        /* Assuming chain of length 1 */
848
    {
849
      for(i=0; i<e->count; i++)
850
        {
851
          struct eattr *a = &e->attrs[i];
852
          h ^= a->id;
853
          if (a->type & EAF_EMBEDDED)
854
            h ^= a->u.data;
855
          else
856
            {
857
              struct adata *d = a->u.ptr;
858
              int size = d->length;
859
              byte *z = d->data;
860
              while (size >= 4)
861
                {
862
                  h ^= *(u32 *)z;
863
                  z += 4;
864
                  size -= 4;
865
                }
866
              while (size--)
867
                h = (h >> 24) ^ (h << 8) ^ *z++;
868
            }
869
        }
870
      h ^= h >> 16;
871
      h ^= h >> 6;
872
      h &= 0xffff;
873
    }
874
  return h;
875
}
876

    
877
/**
878
 * ea_append - concatenate &ea_list's
879
 * @to: destination list (can be %NULL)
880
 * @what: list to be appended (can be %NULL)
881
 *
882
 * This function appends the &ea_list @what at the end of
883
 * &ea_list @to and returns a pointer to the resulting list.
884
 */
885
ea_list *
886
ea_append(ea_list *to, ea_list *what)
887
{
888
  ea_list *res;
889

    
890
  if (!to)
891
    return what;
892
  res = to;
893
  while (to->next)
894
    to = to->next;
895
  to->next = what;
896
  return res;
897
}
898

    
899
/*
900
 *        rta's
901
 */
902

    
903
static uint rta_cache_count;
904
static uint rta_cache_size = 32;
905
static uint rta_cache_limit;
906
static uint rta_cache_mask;
907
static rta **rta_hash_table;
908

    
909
static void
910
rta_alloc_hash(void)
911
{
912
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
913
  if (rta_cache_size < 32768)
914
    rta_cache_limit = rta_cache_size * 2;
915
  else
916
    rta_cache_limit = ~0;
917
  rta_cache_mask = rta_cache_size - 1;
918
}
919

    
920
static inline uint
921
rta_hash(rta *a)
922
{
923
  return (((uint) (uintptr_t) a->src) ^ ipa_hash(a->gw) ^
924
          mpnh_hash(a->nexthops) ^ ea_hash(a->eattrs)) & 0xffff;
925
}
926

    
927
static inline int
928
rta_same(rta *x, rta *y)
929
{
930
  return (x->src == y->src &&
931
          x->source == y->source &&
932
          x->scope == y->scope &&
933
          x->cast == y->cast &&
934
          x->dest == y->dest &&
935
          x->flags == y->flags &&
936
          x->igp_metric == y->igp_metric &&
937
          ipa_equal(x->gw, y->gw) &&
938
          ipa_equal(x->from, y->from) &&
939
          x->iface == y->iface &&
940
          x->hostentry == y->hostentry &&
941
          mpnh_same(x->nexthops, y->nexthops) &&
942
          ea_same(x->eattrs, y->eattrs));
943
}
944

    
945
static rta *
946
rta_copy(rta *o)
947
{
948
  rta *r = sl_alloc(rta_slab);
949

    
950
  memcpy(r, o, sizeof(rta));
951
  r->uc = 1;
952
  r->nexthops = mpnh_copy(o->nexthops);
953
  r->eattrs = ea_list_copy(o->eattrs);
954
  return r;
955
}
956

    
957
static inline void
958
rta_insert(rta *r)
959
{
960
  uint h = r->hash_key & rta_cache_mask;
961
  r->next = rta_hash_table[h];
962
  if (r->next)
963
    r->next->pprev = &r->next;
964
  r->pprev = &rta_hash_table[h];
965
  rta_hash_table[h] = r;
966
}
967

    
968
static void
969
rta_rehash(void)
970
{
971
  uint ohs = rta_cache_size;
972
  uint h;
973
  rta *r, *n;
974
  rta **oht = rta_hash_table;
975

    
976
  rta_cache_size = 2*rta_cache_size;
977
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
978
  rta_alloc_hash();
979
  for(h=0; h<ohs; h++)
980
    for(r=oht[h]; r; r=n)
981
      {
982
        n = r->next;
983
        rta_insert(r);
984
      }
985
  mb_free(oht);
986
}
987

    
988
/**
989
 * rta_lookup - look up a &rta in attribute cache
990
 * @o: a un-cached &rta
991
 *
992
 * rta_lookup() gets an un-cached &rta structure and returns its cached
993
 * counterpart. It starts with examining the attribute cache to see whether
994
 * there exists a matching entry. If such an entry exists, it's returned and
995
 * its use count is incremented, else a new entry is created with use count
996
 * set to 1.
997
 *
998
 * The extended attribute lists attached to the &rta are automatically
999
 * converted to the normalized form.
1000
 */
1001
rta *
1002
rta_lookup(rta *o)
1003
{
1004
  rta *r;
1005
  uint h;
1006

    
1007
  ASSERT(!(o->aflags & RTAF_CACHED));
1008
  if (o->eattrs)
1009
    {
1010
      if (o->eattrs->next)        /* Multiple ea_list's, need to merge them */
1011
        {
1012
          ea_list *ml = alloca(ea_scan(o->eattrs));
1013
          ea_merge(o->eattrs, ml);
1014
          o->eattrs = ml;
1015
        }
1016
      ea_sort(o->eattrs);
1017
    }
1018

    
1019
  h = rta_hash(o);
1020
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1021
    if (r->hash_key == h && rta_same(r, o))
1022
      return rta_clone(r);
1023

    
1024
  r = rta_copy(o);
1025
  r->hash_key = h;
1026
  r->aflags = RTAF_CACHED;
1027
  rt_lock_source(r->src);
1028
  rt_lock_hostentry(r->hostentry);
1029
  rta_insert(r);
1030

    
1031
  if (++rta_cache_count > rta_cache_limit)
1032
    rta_rehash();
1033

    
1034
  return r;
1035
}
1036

    
1037
void
1038
rta__free(rta *a)
1039
{
1040
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
1041
  rta_cache_count--;
1042
  *a->pprev = a->next;
1043
  if (a->next)
1044
    a->next->pprev = a->pprev;
1045
  a->aflags = 0;                /* Poison the entry */
1046
  rt_unlock_hostentry(a->hostentry);
1047
  rt_unlock_source(a->src);
1048
  mpnh_free(a->nexthops);
1049
  ea_free(a->eattrs);
1050
  sl_free(rta_slab, a);
1051
}
1052

    
1053
/**
1054
 * rta_dump - dump route attributes
1055
 * @a: attribute structure to dump
1056
 *
1057
 * This function takes a &rta and dumps its contents to the debug output.
1058
 */
1059
void
1060
rta_dump(rta *a)
1061
{
1062
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
1063
                         "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
1064
                         "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
1065
                         "RTS_OSPF_EXT2", "RTS_BGP" };
1066
  static char *rtc[] = { "", " BC", " MC", " AC" };
1067
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1068

    
1069
  debug("p=%s uc=%d %s %s%s%s h=%04x",
1070
        a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
1071
        rtd[a->dest], a->hash_key);
1072
  if (!(a->aflags & RTAF_CACHED))
1073
    debug(" !CACHED");
1074
  debug(" <-%I", a->from);
1075
  if (a->dest == RTD_ROUTER)
1076
    debug(" ->%I", a->gw);
1077
  if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER)
1078
    debug(" [%s]", a->iface ? a->iface->name : "???" );
1079
  if (a->eattrs)
1080
    {
1081
      debug(" EA: ");
1082
      ea_dump(a->eattrs);
1083
    }
1084
}
1085

    
1086
/**
1087
 * rta_dump_all - dump attribute cache
1088
 *
1089
 * This function dumps the whole contents of route attribute cache
1090
 * to the debug output.
1091
 */
1092
void
1093
rta_dump_all(void)
1094
{
1095
  rta *a;
1096
  uint h;
1097

    
1098
  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1099
  for(h=0; h<rta_cache_size; h++)
1100
    for(a=rta_hash_table[h]; a; a=a->next)
1101
      {
1102
        debug("%p ", a);
1103
        rta_dump(a);
1104
        debug("\n");
1105
      }
1106
  debug("\n");
1107
}
1108

    
1109
void
1110
rta_show(struct cli *c, rta *a, ea_list *eal)
1111
{
1112
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1113
                               "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1114
  static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
1115
  int i;
1116

    
1117
  cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
1118
  if (!eal)
1119
    eal = a->eattrs;
1120
  for(; eal; eal=eal->next)
1121
    for(i=0; i<eal->count; i++)
1122
      ea_show(c, &eal->attrs[i]);
1123
}
1124

    
1125
/**
1126
 * rta_init - initialize route attribute cache
1127
 *
1128
 * This function is called during initialization of the routing
1129
 * table module to set up the internals of the attribute cache.
1130
 */
1131
void
1132
rta_init(void)
1133
{
1134
  rta_pool = rp_new(&root_pool, "Attributes");
1135
  rta_slab = sl_new(rta_pool, sizeof(rta));
1136
  mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh));
1137
  rta_alloc_hash();
1138
  rte_src_init();
1139
}
1140

    
1141
/*
1142
 *  Documentation for functions declared inline in route.h
1143
 */
1144
#if 0
1145

1146
/**
1147
 * rta_clone - clone route attributes
1148
 * @r: a &rta to be cloned
1149
 *
1150
 * rta_clone() takes a cached &rta and returns its identical cached
1151
 * copy. Currently it works by just returning the original &rta with
1152
 * its use count incremented.
1153
 */
1154
static inline rta *rta_clone(rta *r)
1155
{ DUMMY; }
1156

1157
/**
1158
 * rta_free - free route attributes
1159
 * @r: a &rta to be freed
1160
 *
1161
 * If you stop using a &rta (for example when deleting a route which uses
1162
 * it), you need to call rta_free() to notify the attribute cache the
1163
 * attribute is no longer in use and can be freed if you were the last
1164
 * user (which rta_free() tests by inspecting the use count).
1165
 */
1166
static inline void rta_free(rta *r)
1167
{ DUMMY; }
1168

1169
#endif