Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (28.9 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
#include <stddef.h>
60

    
61
pool *rta_pool;
62

    
63
static slab *rta_slab_[4];
64
static slab *nexthop_slab_[4];
65
static slab *rte_src_slab;
66

    
67
static struct idm src_ids;
68
#define SRC_ID_INIT_SIZE 4
69

    
70
/* rte source hash */
71

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

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

    
81
static HASH(struct rte_src) src_hash;
82

    
83
struct protocol *attr_class_to_protocol[EAP_MAX];
84

    
85

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

    
91
  idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
92

    
93
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
94
}
95

    
96

    
97
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
98

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

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

    
110
  if (src)
111
    return src;
112

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

    
119
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
120

    
121
  return src;
122
}
123

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

    
138
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
139
}
140

    
141

    
142
/*
143
 *        Multipath Next Hop
144
 */
145

    
146
static inline u32
147
nexthop_hash(struct nexthop *x)
148
{
149
  u32 h = 0;
150
  for (; x; x = x->next)
151
  {
152
    h ^= ipa_hash(x->gw) ^ (h << 5) ^ (h >> 9);
153
    for (int i=0; i<x->labels; i++)
154
      h ^= x->label[i] ^ (h << 6) ^ (h >> 7);
155
  }
156

    
157
  return h;
158
}
159

    
160
int
161
nexthop__same(struct nexthop *x, struct nexthop *y)
162
{
163
  for (; x && y; x = x->next, y = y->next)
164
  {
165
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight) || (x->labels != y->labels))
166
      return 0;
167
    for (int i=0; i<x->labels; i++)
168
      if (x->label[i] != y->label[i])
169
        return 0;
170
  }
171

    
172
  return 1;
173
}
174

    
175
static int
176
nexthop_compare_node(struct nexthop *x, struct nexthop *y)
177
{
178
  int r;
179

    
180
  if (!x)
181
    return 1;
182

    
183
  if (!y)
184
    return -1;
185

    
186
  r = ((int) y->weight) - ((int) x->weight);
187
  if (r)
188
    return r;
189

    
190
  r = ipa_compare(x->gw, y->gw);
191
  if (r)
192
    return r;
193

    
194
  r = ((int) y->labels) - ((int) x->labels);
195
  if (r)
196
    return r;
197

    
198
  for (int i=0; i<y->labels; i++)
199
  {
200
    r = ((int) y->label[i]) - ((int) x->label[i]);
201
    if (r)
202
      return r;
203
  }
204

    
205
  return ((int) x->iface->index) - ((int) y->iface->index);
206
}
207

    
208
static inline struct nexthop *
209
nexthop_copy_node(const struct nexthop *src, linpool *lp)
210
{
211
  struct nexthop *n = lp_alloc(lp, nexthop_size(src));
212

    
213
  memcpy(n, src, nexthop_size(src));
214
  n->next = NULL;
215

    
216
  return n;
217
}
218

    
219
/**
220
 * nexthop_merge - merge nexthop lists
221
 * @x: list 1
222
 * @y: list 2
223
 * @rx: reusability of list @x
224
 * @ry: reusability of list @y
225
 * @max: max number of nexthops
226
 * @lp: linpool for allocating nexthops
227
 *
228
 * The nexthop_merge() function takes two nexthop lists @x and @y and merges them,
229
 * eliminating possible duplicates. The input lists must be sorted and the
230
 * result is sorted too. The number of nexthops in result is limited by @max.
231
 * New nodes are allocated from linpool @lp.
232
 *
233
 * The arguments @rx and @ry specify whether corresponding input lists may be
234
 * consumed by the function (i.e. their nodes reused in the resulting list), in
235
 * that case the caller should not access these lists after that. To eliminate
236
 * issues with deallocation of these lists, the caller should use some form of
237
 * bulk deallocation (e.g. stack or linpool) to free these nodes when the
238
 * resulting list is no longer needed. When reusability is not set, the
239
 * corresponding lists are not modified nor linked from the resulting list.
240
 */
241
struct nexthop *
242
nexthop_merge(struct nexthop *x, struct nexthop *y, int rx, int ry, int max, linpool *lp)
243
{
244
  struct nexthop *root = NULL;
245
  struct nexthop **n = &root;
246

    
247
  while ((x || y) && max--)
248
  {
249
    int cmp = nexthop_compare_node(x, y);
250
    if (cmp < 0)
251
    {
252
      *n = rx ? x : nexthop_copy_node(x, lp);
253
      x = x->next;
254
    }
255
    else if (cmp > 0)
256
    {
257
      *n = ry ? y : nexthop_copy_node(y, lp);
258
      y = y->next;
259
    }
260
    else
261
    {
262
      *n = rx ? x : (ry ? y : nexthop_copy_node(x, lp));
263
      x = x->next;
264
      y = y->next;
265
    }
266
    n = &((*n)->next);
267
  }
268
  *n = NULL;
269

    
270
  return root;
271
}
272

    
273
void
274
nexthop_insert(struct nexthop *n, struct nexthop *x)
275
{
276
  struct nexthop tmp;
277
  memcpy(&tmp, n, sizeof(struct nexthop));
278
  if (nexthop_compare_node(n, x) > 0) /* Insert to the included nexthop */
279
  {
280
    memcpy(n, x, sizeof(struct nexthop));
281
    memcpy(x, &tmp, sizeof(struct nexthop));
282
    n->next = x;
283
    return;
284
  }
285

    
286
  for (struct nexthop **nn = &(n->next); *nn; nn = &((*nn)->next))
287
  {
288
    int cmp = nexthop_compare_node(*nn, x);
289

    
290
    if (cmp < 0)
291
      continue;
292
    
293
    if (cmp > 0)
294
    {
295
      x->next = *nn;
296
      *nn = x;
297
    }
298
    
299
    return;
300
  }
301

    
302
}
303

    
304
int
305
nexthop_is_sorted(struct nexthop *x)
306
{
307
  for (; x && x->next; x = x->next)
308
    if (nexthop_compare_node(x, x->next) >= 0)
309
      return 0;
310

    
311
  return 1;
312
}
313

    
314
static inline slab *
315
nexthop_slab(struct nexthop *nh)
316
{
317
  return nexthop_slab_[nh->labels > 2 ? 3 : nh->labels];
318
}
319

    
320
static struct nexthop *
321
nexthop_copy(struct nexthop *o)
322
{
323
  struct nexthop *first = NULL;
324
  struct nexthop **last = &first;
325

    
326
  for (; o; o = o->next)
327
    {
328
      struct nexthop *n = sl_alloc(nexthop_slab(o));
329
      n->gw = o->gw;
330
      n->iface = o->iface;
331
      n->next = NULL;
332
      n->weight = o->weight;
333
      n->labels = o->labels;
334
      for (int i=0; i<o->labels; i++)
335
        n->label[i] = o->label[i];
336

    
337
      *last = n;
338
      last = &(n->next);
339
    }
340

    
341
  return first;
342
}
343

    
344
static void
345
nexthop_free(struct nexthop *o)
346
{
347
  struct nexthop *n;
348

    
349
  while (o)
350
    {
351
      n = o->next;
352
      sl_free(nexthop_slab(o), o);
353
      o = n;
354
    }
355
}
356

    
357

    
358
/*
359
 *        Extended Attributes
360
 */
361

    
362
static inline eattr *
363
ea__find(ea_list *e, unsigned id)
364
{
365
  eattr *a;
366
  int l, r, m;
367

    
368
  while (e)
369
    {
370
      if (e->flags & EALF_BISECT)
371
        {
372
          l = 0;
373
          r = e->count - 1;
374
          while (l <= r)
375
            {
376
              m = (l+r) / 2;
377
              a = &e->attrs[m];
378
              if (a->id == id)
379
                return a;
380
              else if (a->id < id)
381
                l = m+1;
382
              else
383
                r = m-1;
384
            }
385
        }
386
      else
387
        for(m=0; m<e->count; m++)
388
          if (e->attrs[m].id == id)
389
            return &e->attrs[m];
390
      e = e->next;
391
    }
392
  return NULL;
393
}
394

    
395
/**
396
 * ea_find - find an extended attribute
397
 * @e: attribute list to search in
398
 * @id: attribute ID to search for
399
 *
400
 * Given an extended attribute list, ea_find() searches for a first
401
 * occurrence of an attribute with specified ID, returning either a pointer
402
 * to its &eattr structure or %NULL if no such attribute exists.
403
 */
404
eattr *
405
ea_find(ea_list *e, unsigned id)
406
{
407
  eattr *a = ea__find(e, id & EA_CODE_MASK);
408

    
409
  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
410
      !(id & EA_ALLOW_UNDEF))
411
    return NULL;
412
  return a;
413
}
414

    
415
/**
416
 * ea_walk - walk through extended attributes
417
 * @s: walk state structure
418
 * @id: start of attribute ID interval
419
 * @max: length of attribute ID interval
420
 *
421
 * Given an extended attribute list, ea_walk() walks through the list looking
422
 * for first occurrences of attributes with ID in specified interval from @id to
423
 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
424
 * walk state in @s for subsequent calls.
425
 *
426
 * The function ea_walk() is supposed to be called in a loop, with initially
427
 * zeroed walk state structure @s with filled the initial extended attribute
428
 * list, returning one found attribute in each call or %NULL when no other
429
 * attribute exists. The extended attribute list or the arguments should not be
430
 * modified between calls. The maximum value of @max is 128.
431
 */
432
eattr *
433
ea_walk(struct ea_walk_state *s, uint id, uint max)
434
{
435
  ea_list *e = s->eattrs;
436
  eattr *a = s->ea;
437
  eattr *a_max;
438

    
439
  max = id + max;
440

    
441
  if (a)
442
    goto step;
443

    
444
  for (; e; e = e->next)
445
  {
446
    if (e->flags & EALF_BISECT)
447
    {
448
      int l, r, m;
449

    
450
      l = 0;
451
      r = e->count - 1;
452
      while (l < r)
453
      {
454
        m = (l+r) / 2;
455
        if (e->attrs[m].id < id)
456
          l = m + 1;
457
        else
458
          r = m;
459
      }
460
      a = e->attrs + l;
461
    }
462
    else
463
      a = e->attrs;
464

    
465
  step:
466
    a_max = e->attrs + e->count;
467
    for (; a < a_max; a++)
468
      if ((a->id >= id) && (a->id < max))
469
      {
470
        int n = a->id - id;
471

    
472
        if (BIT32_TEST(s->visited, n))
473
          continue;
474

    
475
        BIT32_SET(s->visited, n);
476

    
477
        if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
478
          continue;
479

    
480
        s->eattrs = e;
481
        s->ea = a;
482
        return a;
483
      }
484
      else if (e->flags & EALF_BISECT)
485
        break;
486
  }
487

    
488
  return NULL;
489
}
490

    
491
/**
492
 * ea_get_int - fetch an integer attribute
493
 * @e: attribute list
494
 * @id: attribute ID
495
 * @def: default value
496
 *
497
 * This function is a shortcut for retrieving a value of an integer attribute
498
 * by calling ea_find() to find the attribute, extracting its value or returning
499
 * a provided default if no such attribute is present.
500
 */
501
int
502
ea_get_int(ea_list *e, unsigned id, int def)
503
{
504
  eattr *a = ea_find(e, id);
505
  if (!a)
506
    return def;
507
  return a->u.data;
508
}
509

    
510
static inline void
511
ea_do_sort(ea_list *e)
512
{
513
  unsigned n = e->count;
514
  eattr *a = e->attrs;
515
  eattr *b = alloca(n * sizeof(eattr));
516
  unsigned s, ss;
517

    
518
  /* We need to use a stable sorting algorithm, hence mergesort */
519
  do
520
    {
521
      s = ss = 0;
522
      while (s < n)
523
        {
524
          eattr *p, *q, *lo, *hi;
525
          p = b;
526
          ss = s;
527
          *p++ = a[s++];
528
          while (s < n && p[-1].id <= a[s].id)
529
            *p++ = a[s++];
530
          if (s < n)
531
            {
532
              q = p;
533
              *p++ = a[s++];
534
              while (s < n && p[-1].id <= a[s].id)
535
                *p++ = a[s++];
536
              lo = b;
537
              hi = q;
538
              s = ss;
539
              while (lo < q && hi < p)
540
                if (lo->id <= hi->id)
541
                  a[s++] = *lo++;
542
                else
543
                  a[s++] = *hi++;
544
              while (lo < q)
545
                a[s++] = *lo++;
546
              while (hi < p)
547
                a[s++] = *hi++;
548
            }
549
        }
550
    }
551
  while (ss);
552
}
553

    
554
static inline void
555
ea_do_prune(ea_list *e)
556
{
557
  eattr *s, *d, *l, *s0;
558
  int i = 0;
559

    
560
  /* Discard duplicates and undefs. Do you remember sorting was stable? */
561
  s = d = e->attrs;
562
  l = e->attrs + e->count;
563
  while (s < l)
564
    {
565
      s0 = s++;
566
      while (s < l && s->id == s[-1].id)
567
        s++;
568
      /* s0 is the most recent version, s[-1] the oldest one */
569
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
570
        {
571
          *d = *s0;
572
          d->type = (d->type & ~(EAF_ORIGINATED|EAF_FRESH)) | (s[-1].type & EAF_ORIGINATED);
573
          d++;
574
          i++;
575
        }
576
    }
577
  e->count = i;
578
}
579

    
580
/**
581
 * ea_sort - sort an attribute list
582
 * @e: list to be sorted
583
 *
584
 * This function takes a &ea_list chain and sorts the attributes
585
 * within each of its entries.
586
 *
587
 * If an attribute occurs multiple times in a single &ea_list,
588
 * ea_sort() leaves only the first (the only significant) occurrence.
589
 */
590
void
591
ea_sort(ea_list *e)
592
{
593
  while (e)
594
    {
595
      if (!(e->flags & EALF_SORTED))
596
        {
597
          ea_do_sort(e);
598
          ea_do_prune(e);
599
          e->flags |= EALF_SORTED;
600
        }
601
      if (e->count > 5)
602
        e->flags |= EALF_BISECT;
603
      e = e->next;
604
    }
605
}
606

    
607
/**
608
 * ea_scan - estimate attribute list size
609
 * @e: attribute list
610
 *
611
 * This function calculates an upper bound of the size of
612
 * a given &ea_list after merging with ea_merge().
613
 */
614
unsigned
615
ea_scan(ea_list *e)
616
{
617
  unsigned cnt = 0;
618

    
619
  while (e)
620
    {
621
      cnt += e->count;
622
      e = e->next;
623
    }
624
  return sizeof(ea_list) + sizeof(eattr)*cnt;
625
}
626

    
627
/**
628
 * ea_merge - merge segments of an attribute list
629
 * @e: attribute list
630
 * @t: buffer to store the result to
631
 *
632
 * This function takes a possibly multi-segment attribute list
633
 * and merges all of its segments to one.
634
 *
635
 * The primary use of this function is for &ea_list normalization:
636
 * first call ea_scan() to determine how much memory will the result
637
 * take, then allocate a buffer (usually using alloca()), merge the
638
 * segments with ea_merge() and finally sort and prune the result
639
 * by calling ea_sort().
640
 */
641
void
642
ea_merge(ea_list *e, ea_list *t)
643
{
644
  eattr *d = t->attrs;
645

    
646
  t->flags = 0;
647
  t->count = 0;
648
  t->next = NULL;
649
  while (e)
650
    {
651
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
652
      t->count += e->count;
653
      d += e->count;
654
      e = e->next;
655
    }
656
}
657

    
658
/**
659
 * ea_same - compare two &ea_list's
660
 * @x: attribute list
661
 * @y: attribute list
662
 *
663
 * ea_same() compares two normalized attribute lists @x and @y and returns
664
 * 1 if they contain the same attributes, 0 otherwise.
665
 */
666
int
667
ea_same(ea_list *x, ea_list *y)
668
{
669
  int c;
670

    
671
  if (!x || !y)
672
    return x == y;
673
  ASSERT(!x->next && !y->next);
674
  if (x->count != y->count)
675
    return 0;
676
  for(c=0; c<x->count; c++)
677
    {
678
      eattr *a = &x->attrs[c];
679
      eattr *b = &y->attrs[c];
680

    
681
      if (a->id != b->id ||
682
          a->flags != b->flags ||
683
          a->type != b->type ||
684
          ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
685
        return 0;
686
    }
687
  return 1;
688
}
689

    
690
static inline ea_list *
691
ea_list_copy(ea_list *o)
692
{
693
  ea_list *n;
694
  unsigned i, len;
695

    
696
  if (!o)
697
    return NULL;
698
  ASSERT(!o->next);
699
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
700
  n = mb_alloc(rta_pool, len);
701
  memcpy(n, o, len);
702
  n->flags |= EALF_CACHED;
703
  for(i=0; i<o->count; i++)
704
    {
705
      eattr *a = &n->attrs[i];
706
      if (!(a->type & EAF_EMBEDDED))
707
        {
708
          unsigned size = sizeof(struct adata) + a->u.ptr->length;
709
          struct adata *d = mb_alloc(rta_pool, size);
710
          memcpy(d, a->u.ptr, size);
711
          a->u.ptr = d;
712
        }
713
    }
714
  return n;
715
}
716

    
717
static inline void
718
ea_free(ea_list *o)
719
{
720
  int i;
721

    
722
  if (o)
723
    {
724
      ASSERT(!o->next);
725
      for(i=0; i<o->count; i++)
726
        {
727
          eattr *a = &o->attrs[i];
728
          if (!(a->type & EAF_EMBEDDED))
729
            mb_free(a->u.ptr);
730
        }
731
      mb_free(o);
732
    }
733
}
734

    
735
static int
736
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
737
{
738
  if (a->id == EA_GEN_IGP_METRIC)
739
    {
740
      *buf += bsprintf(*buf, "igp_metric");
741
      return GA_NAME;
742
    }
743

    
744
  return GA_UNKNOWN;
745
}
746

    
747
void
748
ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
749
{
750
  byte *bound = buf + bufsize - 32;
751
  u32 data = a->u.data;
752
  int i;
753

    
754
  for (i = min; i < max; i++)
755
    if ((data & (1u << i)) && names[i])
756
    {
757
      if (buf > bound)
758
      {
759
        strcpy(buf, " ...");
760
        return;
761
      }
762

    
763
      buf += bsprintf(buf, " %s", names[i]);
764
      data &= ~(1u << i);
765
    }
766

    
767
  if (data)
768
    bsprintf(buf, " %08x", data);
769

    
770
  return;
771
}
772

    
773
static inline void
774
opaque_format(struct adata *ad, byte *buf, uint size)
775
{
776
  byte *bound = buf + size - 10;
777
  uint i;
778

    
779
  for(i = 0; i < ad->length; i++)
780
    {
781
      if (buf > bound)
782
        {
783
          strcpy(buf, " ...");
784
          return;
785
        }
786
      if (i)
787
        *buf++ = ' ';
788

    
789
      buf += bsprintf(buf, "%02x", ad->data[i]);
790
    }
791

    
792
  *buf = 0;
793
  return;
794
}
795

    
796
static inline void
797
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
798
{
799
  int i = int_set_format(ad, way, 0, pos, end - pos);
800
  cli_printf(c, -1012, "\t%s", buf);
801
  while (i)
802
    {
803
      i = int_set_format(ad, way, i, buf, end - buf - 1);
804
      cli_printf(c, -1012, "\t\t%s", buf);
805
    }
806
}
807

    
808
static inline void
809
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
810
{
811
  int i = ec_set_format(ad, 0, pos, end - pos);
812
  cli_printf(c, -1012, "\t%s", buf);
813
  while (i)
814
    {
815
      i = ec_set_format(ad, i, buf, end - buf - 1);
816
      cli_printf(c, -1012, "\t\t%s", buf);
817
    }
818
}
819

    
820
static inline void
821
ea_show_lc_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
822
{
823
  int i = lc_set_format(ad, 0, pos, end - pos);
824
  cli_printf(c, -1012, "\t%s", buf);
825
  while (i)
826
    {
827
      i = lc_set_format(ad, i, buf, end - buf - 1);
828
      cli_printf(c, -1012, "\t\t%s", buf);
829
    }
830
}
831

    
832
/**
833
 * ea_show - print an &eattr to CLI
834
 * @c: destination CLI
835
 * @e: attribute to be printed
836
 *
837
 * This function takes an extended attribute represented by its &eattr
838
 * structure and prints it to the CLI according to the type information.
839
 *
840
 * If the protocol defining the attribute provides its own
841
 * get_attr() hook, it's consulted first.
842
 */
843
void
844
ea_show(struct cli *c, eattr *e)
845
{
846
  struct protocol *p;
847
  int status = GA_UNKNOWN;
848
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
849
  byte buf[CLI_MSG_SIZE];
850
  byte *pos = buf, *end = buf + sizeof(buf);
851

    
852
  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
853
    {
854
      pos += bsprintf(pos, "%s.", p->name);
855
      if (p->get_attr)
856
        status = p->get_attr(e, pos, end - pos);
857
      pos += strlen(pos);
858
    }
859
  else if (EA_PROTO(e->id))
860
    pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
861
  else
862
    status = get_generic_attr(e, &pos, end - pos);
863

    
864
  if (status < GA_NAME)
865
    pos += bsprintf(pos, "%02x", EA_ID(e->id));
866
  if (status < GA_FULL)
867
    {
868
      *pos++ = ':';
869
      *pos++ = ' ';
870
      switch (e->type & EAF_TYPE_MASK)
871
        {
872
        case EAF_TYPE_INT:
873
          bsprintf(pos, "%u", e->u.data);
874
          break;
875
        case EAF_TYPE_OPAQUE:
876
          opaque_format(ad, pos, end - pos);
877
          break;
878
        case EAF_TYPE_IP_ADDRESS:
879
          bsprintf(pos, "%I", *(ip_addr *) ad->data);
880
          break;
881
        case EAF_TYPE_ROUTER_ID:
882
          bsprintf(pos, "%R", e->u.data);
883
          break;
884
        case EAF_TYPE_AS_PATH:
885
          as_path_format(ad, pos, end - pos);
886
          break;
887
        case EAF_TYPE_BITFIELD:
888
          bsprintf(pos, "%08x", e->u.data);
889
          break;
890
        case EAF_TYPE_INT_SET:
891
          ea_show_int_set(c, ad, 1, pos, buf, end);
892
          return;
893
        case EAF_TYPE_EC_SET:
894
          ea_show_ec_set(c, ad, pos, buf, end);
895
          return;
896
        case EAF_TYPE_LC_SET:
897
          ea_show_lc_set(c, ad, pos, buf, end);
898
          return;
899
        case EAF_TYPE_UNDEF:
900
        default:
901
          bsprintf(pos, "<type %02x>", e->type);
902
        }
903
    }
904
  cli_printf(c, -1012, "\t%s", buf);
905
}
906

    
907
/**
908
 * ea_dump - dump an extended attribute
909
 * @e: attribute to be dumped
910
 *
911
 * ea_dump() dumps contents of the extended attribute given to
912
 * the debug output.
913
 */
914
void
915
ea_dump(ea_list *e)
916
{
917
  int i;
918

    
919
  if (!e)
920
    {
921
      debug("NONE");
922
      return;
923
    }
924
  while (e)
925
    {
926
      debug("[%c%c%c]",
927
            (e->flags & EALF_SORTED) ? 'S' : 's',
928
            (e->flags & EALF_BISECT) ? 'B' : 'b',
929
            (e->flags & EALF_CACHED) ? 'C' : 'c');
930
      for(i=0; i<e->count; i++)
931
        {
932
          eattr *a = &e->attrs[i];
933
          debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
934
          if (a->type & EAF_TEMP)
935
            debug("T");
936
          debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
937
          if (a->type & EAF_ORIGINATED)
938
            debug("o");
939
          if (a->type & EAF_EMBEDDED)
940
            debug(":%08x", a->u.data);
941
          else
942
            {
943
              int j, len = a->u.ptr->length;
944
              debug("[%d]:", len);
945
              for(j=0; j<len; j++)
946
                debug("%02x", a->u.ptr->data[j]);
947
            }
948
        }
949
      if (e = e->next)
950
        debug(" | ");
951
    }
952
}
953

    
954
/**
955
 * ea_hash - calculate an &ea_list hash key
956
 * @e: attribute list
957
 *
958
 * ea_hash() takes an extended attribute list and calculated a hopefully
959
 * uniformly distributed hash value from its contents.
960
 */
961
inline uint
962
ea_hash(ea_list *e)
963
{
964
  const u64 mul = 0x68576150f3d6847;
965
  u64 h = 0xafcef24eda8b29;
966
  int i;
967

    
968
  if (e)                        /* Assuming chain of length 1 */
969
    {
970
      for(i=0; i<e->count; i++)
971
        {
972
          struct eattr *a = &e->attrs[i];
973
          h ^= a->id; h *= mul;
974
          if (a->type & EAF_EMBEDDED)
975
            h ^= a->u.data;
976
          else
977
            {
978
              struct adata *d = a->u.ptr;
979
              h ^= mem_hash(d->data, d->length);
980
            }
981
          h *= mul;
982
        }
983
    }
984
  return (h >> 32) ^ (h & 0xffffffff);
985
}
986

    
987
/**
988
 * ea_append - concatenate &ea_list's
989
 * @to: destination list (can be %NULL)
990
 * @what: list to be appended (can be %NULL)
991
 *
992
 * This function appends the &ea_list @what at the end of
993
 * &ea_list @to and returns a pointer to the resulting list.
994
 */
995
ea_list *
996
ea_append(ea_list *to, ea_list *what)
997
{
998
  ea_list *res;
999

    
1000
  if (!to)
1001
    return what;
1002
  res = to;
1003
  while (to->next)
1004
    to = to->next;
1005
  to->next = what;
1006
  return res;
1007
}
1008

    
1009
/*
1010
 *        rta's
1011
 */
1012

    
1013
static uint rta_cache_count;
1014
static uint rta_cache_size = 32;
1015
static uint rta_cache_limit;
1016
static uint rta_cache_mask;
1017
static rta **rta_hash_table;
1018

    
1019
static void
1020
rta_alloc_hash(void)
1021
{
1022
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
1023
  if (rta_cache_size < 32768)
1024
    rta_cache_limit = rta_cache_size * 2;
1025
  else
1026
    rta_cache_limit = ~0;
1027
  rta_cache_mask = rta_cache_size - 1;
1028
}
1029

    
1030
static inline uint
1031
rta_hash(rta *a)
1032
{
1033
  u64 h;
1034
  mem_hash_init(&h);
1035
#define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f));
1036
  MIX(src);
1037
  MIX(hostentry);
1038
  MIX(from);
1039
  MIX(igp_metric);
1040
  MIX(source);
1041
  MIX(scope);
1042
  MIX(dest);
1043
#undef MIX
1044

    
1045
  return mem_hash_value(&h) ^ nexthop_hash(&(a->nh)) ^ ea_hash(a->eattrs);
1046
}
1047

    
1048
static inline int
1049
rta_same(rta *x, rta *y)
1050
{
1051
  return (x->src == y->src &&
1052
          x->source == y->source &&
1053
          x->scope == y->scope &&
1054
          x->dest == y->dest &&
1055
          x->igp_metric == y->igp_metric &&
1056
          ipa_equal(x->from, y->from) &&
1057
          x->hostentry == y->hostentry &&
1058
          nexthop_same(&(x->nh), &(y->nh)) &&
1059
          ea_same(x->eattrs, y->eattrs));
1060
}
1061

    
1062
static inline slab *
1063
rta_slab(rta *a)
1064
{
1065
  return rta_slab_[a->nh.labels > 2 ? 3 : a->nh.labels];
1066
}
1067

    
1068
static rta *
1069
rta_copy(rta *o)
1070
{
1071
  rta *r = sl_alloc(rta_slab(o));
1072

    
1073
  memcpy(r, o, rta_size(o));
1074
  r->uc = 1;
1075
  r->nh.next = nexthop_copy(o->nh.next);
1076
  r->eattrs = ea_list_copy(o->eattrs);
1077
  return r;
1078
}
1079

    
1080
static inline void
1081
rta_insert(rta *r)
1082
{
1083
  uint h = r->hash_key & rta_cache_mask;
1084
  r->next = rta_hash_table[h];
1085
  if (r->next)
1086
    r->next->pprev = &r->next;
1087
  r->pprev = &rta_hash_table[h];
1088
  rta_hash_table[h] = r;
1089
}
1090

    
1091
static void
1092
rta_rehash(void)
1093
{
1094
  uint ohs = rta_cache_size;
1095
  uint h;
1096
  rta *r, *n;
1097
  rta **oht = rta_hash_table;
1098

    
1099
  rta_cache_size = 2*rta_cache_size;
1100
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
1101
  rta_alloc_hash();
1102
  for(h=0; h<ohs; h++)
1103
    for(r=oht[h]; r; r=n)
1104
      {
1105
        n = r->next;
1106
        rta_insert(r);
1107
      }
1108
  mb_free(oht);
1109
}
1110

    
1111
/**
1112
 * rta_lookup - look up a &rta in attribute cache
1113
 * @o: a un-cached &rta
1114
 *
1115
 * rta_lookup() gets an un-cached &rta structure and returns its cached
1116
 * counterpart. It starts with examining the attribute cache to see whether
1117
 * there exists a matching entry. If such an entry exists, it's returned and
1118
 * its use count is incremented, else a new entry is created with use count
1119
 * set to 1.
1120
 *
1121
 * The extended attribute lists attached to the &rta are automatically
1122
 * converted to the normalized form.
1123
 */
1124
rta *
1125
rta_lookup(rta *o)
1126
{
1127
  rta *r;
1128
  uint h;
1129

    
1130
  ASSERT(!(o->aflags & RTAF_CACHED));
1131
  if (o->eattrs)
1132
    {
1133
      if (o->eattrs->next)        /* Multiple ea_list's, need to merge them */
1134
        {
1135
          ea_list *ml = alloca(ea_scan(o->eattrs));
1136
          ea_merge(o->eattrs, ml);
1137
          o->eattrs = ml;
1138
        }
1139
      ea_sort(o->eattrs);
1140
    }
1141

    
1142
  h = rta_hash(o);
1143
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1144
    if (r->hash_key == h && rta_same(r, o))
1145
      return rta_clone(r);
1146

    
1147
  r = rta_copy(o);
1148
  r->hash_key = h;
1149
  r->aflags = RTAF_CACHED;
1150
  rt_lock_source(r->src);
1151
  rt_lock_hostentry(r->hostentry);
1152
  rta_insert(r);
1153

    
1154
  if (++rta_cache_count > rta_cache_limit)
1155
    rta_rehash();
1156

    
1157
  return r;
1158
}
1159

    
1160
void
1161
rta__free(rta *a)
1162
{
1163
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
1164
  rta_cache_count--;
1165
  *a->pprev = a->next;
1166
  if (a->next)
1167
    a->next->pprev = a->pprev;
1168
  a->aflags = 0;                /* Poison the entry */
1169
  rt_unlock_hostentry(a->hostentry);
1170
  rt_unlock_source(a->src);
1171
  if (a->nh.next)
1172
    nexthop_free(a->nh.next);
1173
  ea_free(a->eattrs);
1174
  sl_free(rta_slab(a), a);
1175
}
1176

    
1177
rta *
1178
rta_do_cow(rta *o, linpool *lp)
1179
{
1180
  rta *r = lp_alloc(lp, rta_size(o));
1181
  memcpy(r, o, rta_size(o));
1182
  for (struct nexthop **nhn = &(r->nh.next), *nho = o->nh.next; nho; nho = nho->next)
1183
    {
1184
      *nhn = lp_alloc(lp, nexthop_size(nho));
1185
      memcpy(*nhn, nho, nexthop_size(nho));
1186
      nhn = &((*nhn)->next);
1187
    }
1188
  r->aflags = 0;
1189
  r->uc = 0;
1190
  return r;
1191
}
1192

    
1193
/**
1194
 * rta_dump - dump route attributes
1195
 * @a: attribute structure to dump
1196
 *
1197
 * This function takes a &rta and dumps its contents to the debug output.
1198
 */
1199
void
1200
rta_dump(rta *a)
1201
{
1202
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
1203
                         "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
1204
                         "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
1205
                         "RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL" };
1206
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1207

    
1208
  debug("p=%s uc=%d %s %s%s h=%04x",
1209
        a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope),
1210
        rtd[a->dest], a->hash_key);
1211
  if (!(a->aflags & RTAF_CACHED))
1212
    debug(" !CACHED");
1213
  debug(" <-%I", a->from);
1214
  if (a->dest == RTD_UNICAST)
1215
    for (struct nexthop *nh = &(a->nh); nh; nh = nh->next)
1216
      {
1217
        if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw);
1218
        if (nh->labels) debug(" L %d", nh->label[0]);
1219
        for (int i=1; i<nh->labels; i++)
1220
          debug("/%d", nh->label[i]);
1221
        debug(" [%s]", nh->iface ? nh->iface->name : "???");
1222
      }
1223
  if (a->eattrs)
1224
    {
1225
      debug(" EA: ");
1226
      ea_dump(a->eattrs);
1227
    }
1228
}
1229

    
1230
/**
1231
 * rta_dump_all - dump attribute cache
1232
 *
1233
 * This function dumps the whole contents of route attribute cache
1234
 * to the debug output.
1235
 */
1236
void
1237
rta_dump_all(void)
1238
{
1239
  rta *a;
1240
  uint h;
1241

    
1242
  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1243
  for(h=0; h<rta_cache_size; h++)
1244
    for(a=rta_hash_table[h]; a; a=a->next)
1245
      {
1246
        debug("%p ", a);
1247
        rta_dump(a);
1248
        debug("\n");
1249
      }
1250
  debug("\n");
1251
}
1252

    
1253
void
1254
rta_show(struct cli *c, rta *a, ea_list *eal)
1255
{
1256
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1257
                               "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1258
  int i;
1259

    
1260
  cli_printf(c, -1008, "\tType: %s %s", src_names[a->source], ip_scope_text(a->scope));
1261
  if (!eal)
1262
    eal = a->eattrs;
1263
  for(; eal; eal=eal->next)
1264
    for(i=0; i<eal->count; i++)
1265
      ea_show(c, &eal->attrs[i]);
1266
}
1267

    
1268
/**
1269
 * rta_init - initialize route attribute cache
1270
 *
1271
 * This function is called during initialization of the routing
1272
 * table module to set up the internals of the attribute cache.
1273
 */
1274
void
1275
rta_init(void)
1276
{
1277
  rta_pool = rp_new(&root_pool, "Attributes");
1278

    
1279
  rta_slab_[0] = sl_new(rta_pool, sizeof(rta));
1280
  rta_slab_[1] = sl_new(rta_pool, sizeof(rta) + sizeof(u32));
1281
  rta_slab_[2] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*2);
1282
  rta_slab_[3] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
1283

    
1284
  nexthop_slab_[0] = sl_new(rta_pool, sizeof(struct nexthop));
1285
  nexthop_slab_[1] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32));
1286
  nexthop_slab_[2] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*2);
1287
  nexthop_slab_[3] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
1288

    
1289
  rta_alloc_hash();
1290
  rte_src_init();
1291
}
1292

    
1293
/*
1294
 *  Documentation for functions declared inline in route.h
1295
 */
1296
#if 0
1297

1298
/**
1299
 * rta_clone - clone route attributes
1300
 * @r: a &rta to be cloned
1301
 *
1302
 * rta_clone() takes a cached &rta and returns its identical cached
1303
 * copy. Currently it works by just returning the original &rta with
1304
 * its use count incremented.
1305
 */
1306
static inline rta *rta_clone(rta *r)
1307
{ DUMMY; }
1308

1309
/**
1310
 * rta_free - free route attributes
1311
 * @r: a &rta to be freed
1312
 *
1313
 * If you stop using a &rta (for example when deleting a route which uses
1314
 * it), you need to call rta_free() to notify the attribute cache the
1315
 * attribute is no longer in use and can be freed if you were the last
1316
 * user (which rta_free() tests by inspecting the use count).
1317
 */
1318
static inline void rta_free(rta *r)
1319
{ DUMMY; }
1320

1321
#endif