Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-attr.c @ 74c838a8

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

    
59
pool *rta_pool;
60

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

    
65
static struct idm src_ids;
66
#define SRC_ID_INIT_SIZE 4
67

    
68
/* rte source hash */
69

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

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

    
79
static HASH(struct rte_src) src_hash;
80

    
81
struct protocol *attr_class_to_protocol[EAP_MAX];
82

    
83

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

    
89
  idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
90

    
91
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
92
}
93

    
94

    
95
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
96

    
97
struct rte_src *
98
rt_find_source(struct proto *p, u32 id)
99
{
100
  return HASH_FIND(src_hash, RSH, p, id);
101
}
102

    
103
struct rte_src *
104
rt_get_source(struct proto *p, u32 id)
105
{
106
  struct rte_src *src = rt_find_source(p, id);
107

    
108
  if (src)
109
    return src;
110

    
111
  src = sl_alloc(rte_src_slab);
112
  src->proto = p;
113
  src->private_id = id;
114
  src->global_id = idm_alloc(&src_ids);
115
  src->uc = 0;
116

    
117
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
118

    
119
  return src;
120
}
121

    
122
void
123
rt_prune_sources(void)
124
{
125
  HASH_WALK_FILTER(src_hash, next, src, sp)
126
  {
127
    if (src->uc == 0)
128
    {
129
      HASH_DO_REMOVE(src_hash, RSH, sp);
130
      idm_free(&src_ids, src->global_id);
131
      sl_free(rte_src_slab, src);
132
    }
133
  }
134
  HASH_WALK_FILTER_END;
135

    
136
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
137
}
138

    
139

    
140
/*
141
 *        Multipath Next Hop
142
 */
143

    
144
static inline u32
145
mpnh_hash(struct mpnh *x)
146
{
147
  u32 h = 0;
148
  for (; x; x = x->next)
149
    h ^= ipa_hash(x->gw);
150

    
151
  return h;
152
}
153

    
154
int
155
mpnh__same(struct mpnh *x, struct mpnh *y)
156
{
157
  for (; x && y; x = x->next, y = y->next)
158
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight))
159
      return 0;
160

    
161
  return x == y;
162
}
163

    
164
static int
165
mpnh_compare_node(struct mpnh *x, struct mpnh *y)
166
{
167
  int r;
168

    
169
  if (!x)
170
    return 1;
171

    
172
  if (!y)
173
    return -1;
174

    
175
  r = ((int) y->weight) - ((int) x->weight);
176
  if (r)
177
    return r;
178

    
179
  r = ipa_compare(x->gw, y->gw);
180
  if (r)
181
    return r;
182

    
183
  return ((int) x->iface->index) - ((int) y->iface->index);
184
}
185

    
186
static inline struct mpnh *
187
mpnh_copy_node(const struct mpnh *src, linpool *lp)
188
{
189
  struct mpnh *n = lp_alloc(lp, sizeof(struct mpnh));
190
  n->gw = src->gw;
191
  n->iface = src->iface;
192
  n->next = NULL;
193
  n->weight = src->weight;
194
  return n;
195
}
196

    
197
/**
198
 * mpnh_merge - merge nexthop lists
199
 * @x: list 1
200
 * @y: list 2
201
 * @rx: reusability of list @x
202
 * @ry: reusability of list @y
203
 * @max: max number of nexthops
204
 * @lp: linpool for allocating nexthops
205
 *
206
 * The mpnh_merge() function takes two nexthop lists @x and @y and merges them,
207
 * eliminating possible duplicates. The input lists must be sorted and the
208
 * result is sorted too. The number of nexthops in result is limited by @max.
209
 * New nodes are allocated from linpool @lp.
210
 *
211
 * The arguments @rx and @ry specify whether corresponding input lists may be
212
 * consumed by the function (i.e. their nodes reused in the resulting list), in
213
 * that case the caller should not access these lists after that. To eliminate
214
 * issues with deallocation of these lists, the caller should use some form of
215
 * bulk deallocation (e.g. stack or linpool) to free these nodes when the
216
 * resulting list is no longer needed. When reusability is not set, the
217
 * corresponding lists are not modified nor linked from the resulting list.
218
 */
219
struct mpnh *
220
mpnh_merge(struct mpnh *x, struct mpnh *y, int rx, int ry, int max, linpool *lp)
221
{
222
  struct mpnh *root = NULL;
223
  struct mpnh **n = &root;
224

    
225
  while ((x || y) && max--)
226
  {
227
    int cmp = mpnh_compare_node(x, y);
228
    if (cmp < 0)
229
    {
230
      *n = rx ? x : mpnh_copy_node(x, lp);
231
      x = x->next;
232
    }
233
    else if (cmp > 0)
234
    {
235
      *n = ry ? y : mpnh_copy_node(y, lp);
236
      y = y->next;
237
    }
238
    else
239
    {
240
      *n = rx ? x : (ry ? y : mpnh_copy_node(x, lp));
241
      x = x->next;
242
      y = y->next;
243
    }
244
    n = &((*n)->next);
245
  }
246
  *n = NULL;
247

    
248
  return root;
249
}
250

    
251

    
252
static struct mpnh *
253
mpnh_copy(struct mpnh *o)
254
{
255
  struct mpnh *first = NULL;
256
  struct mpnh **last = &first;
257

    
258
  for (; o; o = o->next)
259
    {
260
      struct mpnh *n = sl_alloc(mpnh_slab);
261
      n->gw = o->gw;
262
      n->iface = o->iface;
263
      n->next = NULL;
264
      n->weight = o->weight;
265

    
266
      *last = n;
267
      last = &(n->next);
268
    }
269

    
270
  return first;
271
}
272

    
273
static void
274
mpnh_free(struct mpnh *o)
275
{
276
  struct mpnh *n;
277

    
278
  while (o)
279
    {
280
      n = o->next;
281
      sl_free(mpnh_slab, o);
282
      o = n;
283
    }
284
}
285

    
286

    
287
/*
288
 *        Extended Attributes
289
 */
290

    
291
static inline eattr *
292
ea__find(ea_list *e, unsigned id)
293
{
294
  eattr *a;
295
  int l, r, m;
296

    
297
  while (e)
298
    {
299
      if (e->flags & EALF_BISECT)
300
        {
301
          l = 0;
302
          r = e->count - 1;
303
          while (l <= r)
304
            {
305
              m = (l+r) / 2;
306
              a = &e->attrs[m];
307
              if (a->id == id)
308
                return a;
309
              else if (a->id < id)
310
                l = m+1;
311
              else
312
                r = m-1;
313
            }
314
        }
315
      else
316
        for(m=0; m<e->count; m++)
317
          if (e->attrs[m].id == id)
318
            return &e->attrs[m];
319
      e = e->next;
320
    }
321
  return NULL;
322
}
323

    
324
/**
325
 * ea_find - find an extended attribute
326
 * @e: attribute list to search in
327
 * @id: attribute ID to search for
328
 *
329
 * Given an extended attribute list, ea_find() searches for a first
330
 * occurrence of an attribute with specified ID, returning either a pointer
331
 * to its &eattr structure or %NULL if no such attribute exists.
332
 */
333
eattr *
334
ea_find(ea_list *e, unsigned id)
335
{
336
  eattr *a = ea__find(e, id & EA_CODE_MASK);
337

    
338
  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
339
      !(id & EA_ALLOW_UNDEF))
340
    return NULL;
341
  return a;
342
}
343

    
344
/**
345
 * ea_walk - walk through extended attributes
346
 * @s: walk state structure
347
 * @id: start of attribute ID interval
348
 * @max: length of attribute ID interval
349
 *
350
 * Given an extended attribute list, ea_walk() walks through the list looking
351
 * for first occurrences of attributes with ID in specified interval from @id to
352
 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
353
 * walk state in @s for subsequent calls.
354

355
 * The function ea_walk() is supposed to be called in a loop, with initially
356
 * zeroed walk state structure @s with filled the initial extended attribute
357
 * list, returning one found attribute in each call or %NULL when no other
358
 * attribute exists. The extended attribute list or the arguments should not be
359
 * modified between calls. The maximum value of @max is 128.
360
 */
361
eattr *
362
ea_walk(struct ea_walk_state *s, uint id, uint max)
363
{
364
  ea_list *e = s->eattrs;
365
  eattr *a = s->ea;
366
  eattr *a_max;
367

    
368
  max = id + max;
369

    
370
  if (a)
371
    goto step;
372

    
373
  for (; e; e = e->next)
374
  {
375
    if (e->flags & EALF_BISECT)
376
    {
377
      int l, r, m;
378

    
379
      l = 0;
380
      r = e->count - 1;
381
      while (l < r)
382
      {
383
        m = (l+r) / 2;
384
        if (e->attrs[m].id < id)
385
          l = m + 1;
386
        else
387
          r = m;
388
      }
389
      a = e->attrs + l;
390
    }
391
    else
392
      a = e->attrs;
393

    
394
  step:
395
    a_max = e->attrs + e->count;
396
    for (; a < a_max; a++)
397
      if ((a->id >= id) && (a->id < max))
398
      {
399
        int n = a->id - id;
400

    
401
        if (BIT32_TEST(s->visited, n))
402
          continue;
403

    
404
        BIT32_SET(s->visited, n);
405

    
406
        if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
407
          continue;
408

    
409
        s->eattrs = e;
410
        s->ea = a;
411
        return a;
412
      }
413
      else if (e->flags & EALF_BISECT)
414
        break;
415
  }
416

    
417
  return NULL;
418
}
419

    
420
/**
421
 * ea_get_int - fetch an integer attribute
422
 * @e: attribute list
423
 * @id: attribute ID
424
 * @def: default value
425
 *
426
 * This function is a shortcut for retrieving a value of an integer attribute
427
 * by calling ea_find() to find the attribute, extracting its value or returning
428
 * a provided default if no such attribute is present.
429
 */
430
int
431
ea_get_int(ea_list *e, unsigned id, int def)
432
{
433
  eattr *a = ea_find(e, id);
434
  if (!a)
435
    return def;
436
  return a->u.data;
437
}
438

    
439
static inline void
440
ea_do_sort(ea_list *e)
441
{
442
  unsigned n = e->count;
443
  eattr *a = e->attrs;
444
  eattr *b = alloca(n * sizeof(eattr));
445
  unsigned s, ss;
446

    
447
  /* We need to use a stable sorting algorithm, hence mergesort */
448
  do
449
    {
450
      s = ss = 0;
451
      while (s < n)
452
        {
453
          eattr *p, *q, *lo, *hi;
454
          p = b;
455
          ss = s;
456
          *p++ = a[s++];
457
          while (s < n && p[-1].id <= a[s].id)
458
            *p++ = a[s++];
459
          if (s < n)
460
            {
461
              q = p;
462
              *p++ = a[s++];
463
              while (s < n && p[-1].id <= a[s].id)
464
                *p++ = a[s++];
465
              lo = b;
466
              hi = q;
467
              s = ss;
468
              while (lo < q && hi < p)
469
                if (lo->id <= hi->id)
470
                  a[s++] = *lo++;
471
                else
472
                  a[s++] = *hi++;
473
              while (lo < q)
474
                a[s++] = *lo++;
475
              while (hi < p)
476
                a[s++] = *hi++;
477
            }
478
        }
479
    }
480
  while (ss);
481
}
482

    
483
static inline void
484
ea_do_prune(ea_list *e)
485
{
486
  eattr *s, *d, *l, *s0;
487
  int i = 0;
488

    
489
  /* Discard duplicates and undefs. Do you remember sorting was stable? */
490
  s = d = e->attrs;
491
  l = e->attrs + e->count;
492
  while (s < l)
493
    {
494
      s0 = s++;
495
      while (s < l && s->id == s[-1].id)
496
        s++;
497
      /* s0 is the most recent version, s[-1] the oldest one */
498
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
499
        {
500
          *d = *s0;
501
          d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED);
502
          d++;
503
          i++;
504
        }
505
    }
506
  e->count = i;
507
}
508

    
509
/**
510
 * ea_sort - sort an attribute list
511
 * @e: list to be sorted
512
 *
513
 * This function takes a &ea_list chain and sorts the attributes
514
 * within each of its entries.
515
 *
516
 * If an attribute occurs multiple times in a single &ea_list,
517
 * ea_sort() leaves only the first (the only significant) occurrence.
518
 */
519
void
520
ea_sort(ea_list *e)
521
{
522
  while (e)
523
    {
524
      if (!(e->flags & EALF_SORTED))
525
        {
526
          ea_do_sort(e);
527
          ea_do_prune(e);
528
          e->flags |= EALF_SORTED;
529
        }
530
      if (e->count > 5)
531
        e->flags |= EALF_BISECT;
532
      e = e->next;
533
    }
534
}
535

    
536
/**
537
 * ea_scan - estimate attribute list size
538
 * @e: attribute list
539
 *
540
 * This function calculates an upper bound of the size of
541
 * a given &ea_list after merging with ea_merge().
542
 */
543
unsigned
544
ea_scan(ea_list *e)
545
{
546
  unsigned cnt = 0;
547

    
548
  while (e)
549
    {
550
      cnt += e->count;
551
      e = e->next;
552
    }
553
  return sizeof(ea_list) + sizeof(eattr)*cnt;
554
}
555

    
556
/**
557
 * ea_merge - merge segments of an attribute list
558
 * @e: attribute list
559
 * @t: buffer to store the result to
560
 *
561
 * This function takes a possibly multi-segment attribute list
562
 * and merges all of its segments to one.
563
 *
564
 * The primary use of this function is for &ea_list normalization:
565
 * first call ea_scan() to determine how much memory will the result
566
 * take, then allocate a buffer (usually using alloca()), merge the
567
 * segments with ea_merge() and finally sort and prune the result
568
 * by calling ea_sort().
569
 */
570
void
571
ea_merge(ea_list *e, ea_list *t)
572
{
573
  eattr *d = t->attrs;
574

    
575
  t->flags = 0;
576
  t->count = 0;
577
  t->next = NULL;
578
  while (e)
579
    {
580
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
581
      t->count += e->count;
582
      d += e->count;
583
      e = e->next;
584
    }
585
}
586

    
587
/**
588
 * ea_same - compare two &ea_list's
589
 * @x: attribute list
590
 * @y: attribute list
591
 *
592
 * ea_same() compares two normalized attribute lists @x and @y and returns
593
 * 1 if they contain the same attributes, 0 otherwise.
594
 */
595
int
596
ea_same(ea_list *x, ea_list *y)
597
{
598
  int c;
599

    
600
  if (!x || !y)
601
    return x == y;
602
  ASSERT(!x->next && !y->next);
603
  if (x->count != y->count)
604
    return 0;
605
  for(c=0; c<x->count; c++)
606
    {
607
      eattr *a = &x->attrs[c];
608
      eattr *b = &y->attrs[c];
609

    
610
      if (a->id != b->id ||
611
          a->flags != b->flags ||
612
          a->type != b->type ||
613
          ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
614
        return 0;
615
    }
616
  return 1;
617
}
618

    
619
static inline ea_list *
620
ea_list_copy(ea_list *o)
621
{
622
  ea_list *n;
623
  unsigned i, len;
624

    
625
  if (!o)
626
    return NULL;
627
  ASSERT(!o->next);
628
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
629
  n = mb_alloc(rta_pool, len);
630
  memcpy(n, o, len);
631
  n->flags |= EALF_CACHED;
632
  for(i=0; i<o->count; i++)
633
    {
634
      eattr *a = &n->attrs[i];
635
      if (!(a->type & EAF_EMBEDDED))
636
        {
637
          unsigned size = sizeof(struct adata) + a->u.ptr->length;
638
          struct adata *d = mb_alloc(rta_pool, size);
639
          memcpy(d, a->u.ptr, size);
640
          a->u.ptr = d;
641
        }
642
    }
643
  return n;
644
}
645

    
646
static inline void
647
ea_free(ea_list *o)
648
{
649
  int i;
650

    
651
  if (o)
652
    {
653
      ASSERT(!o->next);
654
      for(i=0; i<o->count; i++)
655
        {
656
          eattr *a = &o->attrs[i];
657
          if (!(a->type & EAF_EMBEDDED))
658
            mb_free(a->u.ptr);
659
        }
660
      mb_free(o);
661
    }
662
}
663

    
664
static int
665
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
666
{
667
  if (a->id == EA_GEN_IGP_METRIC)
668
    {
669
      *buf += bsprintf(*buf, "igp_metric");
670
      return GA_NAME;
671
    }
672

    
673
  return GA_UNKNOWN;
674
}
675

    
676
void
677
ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
678
{
679
  byte *bound = buf + bufsize - 32;
680
  u32 data = a->u.data;
681
  int i;
682

    
683
  for (i = min; i < max; i++)
684
    if ((data & (1u << i)) && names[i])
685
    {
686
      if (buf > bound)
687
      {
688
        strcpy(buf, " ...");
689
        return;
690
      }
691

    
692
      buf += bsprintf(buf, " %s", names[i]);
693
      data &= ~(1u << i);
694
    }
695

    
696
  if (data)
697
    bsprintf(buf, " %08x", data);
698

    
699
  return;
700
}
701

    
702
static inline void
703
opaque_format(struct adata *ad, byte *buf, uint size)
704
{
705
  byte *bound = buf + size - 10;
706
  int i;
707

    
708
  for(i = 0; i < ad->length; i++)
709
    {
710
      if (buf > bound)
711
        {
712
          strcpy(buf, " ...");
713
          return;
714
        }
715
      if (i)
716
        *buf++ = ' ';
717

    
718
      buf += bsprintf(buf, "%02x", ad->data[i]);
719
    }
720

    
721
  *buf = 0;
722
  return;
723
}
724

    
725
static inline void
726
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
727
{
728
  int i = int_set_format(ad, way, 0, pos, end - pos);
729
  cli_printf(c, -1012, "\t%s", buf);
730
  while (i)
731
    {
732
      i = int_set_format(ad, way, i, buf, end - buf - 1);
733
      cli_printf(c, -1012, "\t\t%s", buf);
734
    }
735
}
736

    
737
static inline void
738
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
739
{
740
  int i = ec_set_format(ad, 0, pos, end - pos);
741
  cli_printf(c, -1012, "\t%s", buf);
742
  while (i)
743
    {
744
      i = ec_set_format(ad, i, buf, end - buf - 1);
745
      cli_printf(c, -1012, "\t\t%s", buf);
746
    }
747
}
748

    
749
/**
750
 * ea_show - print an &eattr to CLI
751
 * @c: destination CLI
752
 * @e: attribute to be printed
753
 *
754
 * This function takes an extended attribute represented by its &eattr
755
 * structure and prints it to the CLI according to the type information.
756
 *
757
 * If the protocol defining the attribute provides its own
758
 * get_attr() hook, it's consulted first.
759
 */
760
void
761
ea_show(struct cli *c, eattr *e)
762
{
763
  struct protocol *p;
764
  int status = GA_UNKNOWN;
765
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
766
  byte buf[CLI_MSG_SIZE];
767
  byte *pos = buf, *end = buf + sizeof(buf);
768

    
769
  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
770
    {
771
      pos += bsprintf(pos, "%s.", p->name);
772
      if (p->get_attr)
773
        status = p->get_attr(e, pos, end - pos);
774
      pos += strlen(pos);
775
    }
776
  else if (EA_PROTO(e->id))
777
    pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
778
  else
779
    status = get_generic_attr(e, &pos, end - pos);
780

    
781
  if (status < GA_NAME)
782
    pos += bsprintf(pos, "%02x", EA_ID(e->id));
783
  if (status < GA_FULL)
784
    {
785
      *pos++ = ':';
786
      *pos++ = ' ';
787
      switch (e->type & EAF_TYPE_MASK)
788
        {
789
        case EAF_TYPE_INT:
790
          bsprintf(pos, "%u", e->u.data);
791
          break;
792
        case EAF_TYPE_OPAQUE:
793
          opaque_format(ad, pos, end - pos);
794
          break;
795
        case EAF_TYPE_IP_ADDRESS:
796
          bsprintf(pos, "%I", *(ip_addr *) ad->data);
797
          break;
798
        case EAF_TYPE_ROUTER_ID:
799
          bsprintf(pos, "%R", e->u.data);
800
          break;
801
        case EAF_TYPE_AS_PATH:
802
          as_path_format(ad, pos, end - pos);
803
          break;
804
        case EAF_TYPE_BITFIELD:
805
          bsprintf(pos, "%08x", e->u.data);
806
          break;
807
        case EAF_TYPE_INT_SET:
808
          ea_show_int_set(c, ad, 1, pos, buf, end);
809
          return;
810
        case EAF_TYPE_EC_SET:
811
          ea_show_ec_set(c, ad, pos, buf, end);
812
          return;
813
        case EAF_TYPE_UNDEF:
814
        default:
815
          bsprintf(pos, "<type %02x>", e->type);
816
        }
817
    }
818
  cli_printf(c, -1012, "\t%s", buf);
819
}
820

    
821
/**
822
 * ea_dump - dump an extended attribute
823
 * @e: attribute to be dumped
824
 *
825
 * ea_dump() dumps contents of the extended attribute given to
826
 * the debug output.
827
 */
828
void
829
ea_dump(ea_list *e)
830
{
831
  int i;
832

    
833
  if (!e)
834
    {
835
      debug("NONE");
836
      return;
837
    }
838
  while (e)
839
    {
840
      debug("[%c%c%c]",
841
            (e->flags & EALF_SORTED) ? 'S' : 's',
842
            (e->flags & EALF_BISECT) ? 'B' : 'b',
843
            (e->flags & EALF_CACHED) ? 'C' : 'c');
844
      for(i=0; i<e->count; i++)
845
        {
846
          eattr *a = &e->attrs[i];
847
          debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
848
          if (a->type & EAF_TEMP)
849
            debug("T");
850
          debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
851
          if (a->type & EAF_ORIGINATED)
852
            debug("o");
853
          if (a->type & EAF_EMBEDDED)
854
            debug(":%08x", a->u.data);
855
          else
856
            {
857
              int j, len = a->u.ptr->length;
858
              debug("[%d]:", len);
859
              for(j=0; j<len; j++)
860
                debug("%02x", a->u.ptr->data[j]);
861
            }
862
        }
863
      if (e = e->next)
864
        debug(" | ");
865
    }
866
}
867

    
868
/**
869
 * ea_hash - calculate an &ea_list hash key
870
 * @e: attribute list
871
 *
872
 * ea_hash() takes an extended attribute list and calculated a hopefully
873
 * uniformly distributed hash value from its contents.
874
 */
875
inline uint
876
ea_hash(ea_list *e)
877
{
878
  u32 h = 0;
879
  int i;
880

    
881
  if (e)                        /* Assuming chain of length 1 */
882
    {
883
      for(i=0; i<e->count; i++)
884
        {
885
          struct eattr *a = &e->attrs[i];
886
          h ^= a->id;
887
          if (a->type & EAF_EMBEDDED)
888
            h ^= a->u.data;
889
          else
890
            {
891
              struct adata *d = a->u.ptr;
892
              int size = d->length;
893
              byte *z = d->data;
894
              while (size >= 4)
895
                {
896
                  h ^= *(u32 *)z;
897
                  z += 4;
898
                  size -= 4;
899
                }
900
              while (size--)
901
                h = (h >> 24) ^ (h << 8) ^ *z++;
902
            }
903
        }
904
      h ^= h >> 16;
905
      h ^= h >> 6;
906
      h &= 0xffff;
907
    }
908
  return h;
909
}
910

    
911
/**
912
 * ea_append - concatenate &ea_list's
913
 * @to: destination list (can be %NULL)
914
 * @what: list to be appended (can be %NULL)
915
 *
916
 * This function appends the &ea_list @what at the end of
917
 * &ea_list @to and returns a pointer to the resulting list.
918
 */
919
ea_list *
920
ea_append(ea_list *to, ea_list *what)
921
{
922
  ea_list *res;
923

    
924
  if (!to)
925
    return what;
926
  res = to;
927
  while (to->next)
928
    to = to->next;
929
  to->next = what;
930
  return res;
931
}
932

    
933
/*
934
 *        rta's
935
 */
936

    
937
static uint rta_cache_count;
938
static uint rta_cache_size = 32;
939
static uint rta_cache_limit;
940
static uint rta_cache_mask;
941
static rta **rta_hash_table;
942

    
943
static void
944
rta_alloc_hash(void)
945
{
946
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
947
  if (rta_cache_size < 32768)
948
    rta_cache_limit = rta_cache_size * 2;
949
  else
950
    rta_cache_limit = ~0;
951
  rta_cache_mask = rta_cache_size - 1;
952
}
953

    
954
static inline uint
955
rta_hash(rta *a)
956
{
957
  /* XXXX fully convert to u32 hashing */
958
  return (((uint) (uintptr_t) a->src) ^ (ipa_hash(a->gw) >> 16) ^
959
          (mpnh_hash(a->nexthops) >> 16) ^ ea_hash(a->eattrs)) & 0xffff;
960
}
961

    
962
static inline int
963
rta_same(rta *x, rta *y)
964
{
965
  return (x->src == y->src &&
966
          x->source == y->source &&
967
          x->scope == y->scope &&
968
          x->cast == y->cast &&
969
          x->dest == y->dest &&
970
          x->flags == y->flags &&
971
          x->igp_metric == y->igp_metric &&
972
          ipa_equal(x->gw, y->gw) &&
973
          ipa_equal(x->from, y->from) &&
974
          x->iface == y->iface &&
975
          x->hostentry == y->hostentry &&
976
          mpnh_same(x->nexthops, y->nexthops) &&
977
          ea_same(x->eattrs, y->eattrs));
978
}
979

    
980
static rta *
981
rta_copy(rta *o)
982
{
983
  rta *r = sl_alloc(rta_slab);
984

    
985
  memcpy(r, o, sizeof(rta));
986
  r->uc = 1;
987
  r->nexthops = mpnh_copy(o->nexthops);
988
  r->eattrs = ea_list_copy(o->eattrs);
989
  return r;
990
}
991

    
992
static inline void
993
rta_insert(rta *r)
994
{
995
  uint h = r->hash_key & rta_cache_mask;
996
  r->next = rta_hash_table[h];
997
  if (r->next)
998
    r->next->pprev = &r->next;
999
  r->pprev = &rta_hash_table[h];
1000
  rta_hash_table[h] = r;
1001
}
1002

    
1003
static void
1004
rta_rehash(void)
1005
{
1006
  uint ohs = rta_cache_size;
1007
  uint h;
1008
  rta *r, *n;
1009
  rta **oht = rta_hash_table;
1010

    
1011
  rta_cache_size = 2*rta_cache_size;
1012
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
1013
  rta_alloc_hash();
1014
  for(h=0; h<ohs; h++)
1015
    for(r=oht[h]; r; r=n)
1016
      {
1017
        n = r->next;
1018
        rta_insert(r);
1019
      }
1020
  mb_free(oht);
1021
}
1022

    
1023
/**
1024
 * rta_lookup - look up a &rta in attribute cache
1025
 * @o: a un-cached &rta
1026
 *
1027
 * rta_lookup() gets an un-cached &rta structure and returns its cached
1028
 * counterpart. It starts with examining the attribute cache to see whether
1029
 * there exists a matching entry. If such an entry exists, it's returned and
1030
 * its use count is incremented, else a new entry is created with use count
1031
 * set to 1.
1032
 *
1033
 * The extended attribute lists attached to the &rta are automatically
1034
 * converted to the normalized form.
1035
 */
1036
rta *
1037
rta_lookup(rta *o)
1038
{
1039
  rta *r;
1040
  uint h;
1041

    
1042
  ASSERT(!(o->aflags & RTAF_CACHED));
1043
  if (o->eattrs)
1044
    {
1045
      if (o->eattrs->next)        /* Multiple ea_list's, need to merge them */
1046
        {
1047
          ea_list *ml = alloca(ea_scan(o->eattrs));
1048
          ea_merge(o->eattrs, ml);
1049
          o->eattrs = ml;
1050
        }
1051
      ea_sort(o->eattrs);
1052
    }
1053

    
1054
  h = rta_hash(o);
1055
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1056
    if (r->hash_key == h && rta_same(r, o))
1057
      return rta_clone(r);
1058

    
1059
  r = rta_copy(o);
1060
  r->hash_key = h;
1061
  r->aflags = RTAF_CACHED;
1062
  rt_lock_source(r->src);
1063
  rt_lock_hostentry(r->hostentry);
1064
  rta_insert(r);
1065

    
1066
  if (++rta_cache_count > rta_cache_limit)
1067
    rta_rehash();
1068

    
1069
  return r;
1070
}
1071

    
1072
void
1073
rta__free(rta *a)
1074
{
1075
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
1076
  rta_cache_count--;
1077
  *a->pprev = a->next;
1078
  if (a->next)
1079
    a->next->pprev = a->pprev;
1080
  a->aflags = 0;                /* Poison the entry */
1081
  rt_unlock_hostentry(a->hostentry);
1082
  rt_unlock_source(a->src);
1083
  mpnh_free(a->nexthops);
1084
  ea_free(a->eattrs);
1085
  sl_free(rta_slab, a);
1086
}
1087

    
1088
rta *
1089
rta_do_cow(rta *o, linpool *lp)
1090
{
1091
  rta *r = lp_alloc(lp, sizeof(rta));
1092
  memcpy(r, o, sizeof(rta));
1093
  r->aflags = 0;
1094
  r->uc = 0;
1095
  return r;
1096
}
1097

    
1098
/**
1099
 * rta_dump - dump route attributes
1100
 * @a: attribute structure to dump
1101
 *
1102
 * This function takes a &rta and dumps its contents to the debug output.
1103
 */
1104
void
1105
rta_dump(rta *a)
1106
{
1107
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
1108
                         "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
1109
                         "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
1110
                         "RTS_OSPF_EXT2", "RTS_BGP" };
1111
  static char *rtc[] = { "", " BC", " MC", " AC" };
1112
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1113

    
1114
  debug("p=%s uc=%d %s %s%s%s h=%04x",
1115
        a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
1116
        rtd[a->dest], a->hash_key);
1117
  if (!(a->aflags & RTAF_CACHED))
1118
    debug(" !CACHED");
1119
  debug(" <-%I", a->from);
1120
  if (a->dest == RTD_ROUTER)
1121
    debug(" ->%I", a->gw);
1122
  if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER)
1123
    debug(" [%s]", a->iface ? a->iface->name : "???" );
1124
  if (a->eattrs)
1125
    {
1126
      debug(" EA: ");
1127
      ea_dump(a->eattrs);
1128
    }
1129
}
1130

    
1131
/**
1132
 * rta_dump_all - dump attribute cache
1133
 *
1134
 * This function dumps the whole contents of route attribute cache
1135
 * to the debug output.
1136
 */
1137
void
1138
rta_dump_all(void)
1139
{
1140
  rta *a;
1141
  uint h;
1142

    
1143
  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1144
  for(h=0; h<rta_cache_size; h++)
1145
    for(a=rta_hash_table[h]; a; a=a->next)
1146
      {
1147
        debug("%p ", a);
1148
        rta_dump(a);
1149
        debug("\n");
1150
      }
1151
  debug("\n");
1152
}
1153

    
1154
void
1155
rta_show(struct cli *c, rta *a, ea_list *eal)
1156
{
1157
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1158
                               "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1159
  static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
1160
  int i;
1161

    
1162
  cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
1163
  if (!eal)
1164
    eal = a->eattrs;
1165
  for(; eal; eal=eal->next)
1166
    for(i=0; i<eal->count; i++)
1167
      ea_show(c, &eal->attrs[i]);
1168
}
1169

    
1170
/**
1171
 * rta_init - initialize route attribute cache
1172
 *
1173
 * This function is called during initialization of the routing
1174
 * table module to set up the internals of the attribute cache.
1175
 */
1176
void
1177
rta_init(void)
1178
{
1179
  rta_pool = rp_new(&root_pool, "Attributes");
1180
  rta_slab = sl_new(rta_pool, sizeof(rta));
1181
  mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh));
1182
  rta_alloc_hash();
1183
  rte_src_init();
1184
}
1185

    
1186
/*
1187
 *  Documentation for functions declared inline in route.h
1188
 */
1189
#if 0
1190

1191
/**
1192
 * rta_clone - clone route attributes
1193
 * @r: a &rta to be cloned
1194
 *
1195
 * rta_clone() takes a cached &rta and returns its identical cached
1196
 * copy. Currently it works by just returning the original &rta with
1197
 * its use count incremented.
1198
 */
1199
static inline rta *rta_clone(rta *r)
1200
{ DUMMY; }
1201

1202
/**
1203
 * rta_free - free route attributes
1204
 * @r: a &rta to be freed
1205
 *
1206
 * If you stop using a &rta (for example when deleting a route which uses
1207
 * it), you need to call rta_free() to notify the attribute cache the
1208
 * attribute is no longer in use and can be freed if you were the last
1209
 * user (which rta_free() tests by inspecting the use count).
1210
 */
1211
static inline void rta_free(rta *r)
1212
{ DUMMY; }
1213

1214
#endif