Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / nest / rt-attr.c @ 6b3f1a54

History | View | Annotate | Download (28.8 KB)

1
/*
2
 *        BIRD -- Route Attribute Cache
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Route attribute cache
11
 *
12
 * Each route entry carries a set of route attributes. Several of them
13
 * vary from route to route, but most attributes are usually common
14
 * for a large number of routes. To conserve memory, we've decided to
15
 * store only the varying ones directly in the &rte and hold the rest
16
 * in a special structure called &rta which is shared among all the
17
 * &rte's with these attributes.
18
 *
19
 * Each &rta contains all the static attributes of the route (i.e.,
20
 * those which are always present) as structure members and a list of
21
 * dynamic attributes represented by a linked list of &ea_list
22
 * structures, each of them consisting of an array of &eattr's containing
23
 * the individual attributes. An attribute can be specified more than once
24
 * in the &ea_list chain and in such case the first occurrence overrides
25
 * the others. This semantics is used especially when someone (for example
26
 * a filter) wishes to alter values of several dynamic attributes, but
27
 * it wants to preserve the original attribute lists maintained by
28
 * another module.
29
 *
30
 * Each &eattr contains an attribute identifier (split to protocol ID and
31
 * per-protocol attribute ID), protocol dependent flags, a type code (consisting
32
 * of several bit fields describing attribute characteristics) and either an
33
 * embedded 32-bit value or a pointer to a &adata structure holding attribute
34
 * contents.
35
 *
36
 * There exist two variants of &rta's -- cached and un-cached ones. Un-cached
37
 * &rta's can have arbitrarily complex structure of &ea_list's and they
38
 * can be modified by any module in the route processing chain. Cached
39
 * &rta's have their attribute lists normalized (that means at most one
40
 * &ea_list is present and its values are sorted in order to speed up
41
 * searching), they are stored in a hash table to make fast lookup possible
42
 * and they are provided with a use count to allow sharing.
43
 *
44
 * Routing tables always contain only cached &rta's.
45
 */
46

    
47
#include "nest/bird.h"
48
#include "nest/route.h"
49
#include "nest/protocol.h"
50
#include "nest/iface.h"
51
#include "nest/cli.h"
52
#include "nest/attrs.h"
53
#include "lib/alloca.h"
54
#include "lib/hash.h"
55
#include "lib/idm.h"
56
#include "lib/resource.h"
57
#include "lib/string.h"
58

    
59
#include <stddef.h>
60

    
61
const char * rta_dest_names[RTD_MAX] = {
62
  [RTD_NONE]                = "",
63
  [RTD_UNICAST]                = "unicast",
64
  [RTD_BLACKHOLE]        = "blackhole",
65
  [RTD_UNREACHABLE]        = "unreachable",
66
  [RTD_PROHIBIT]        = "prohibited",
67
};
68

    
69
pool *rta_pool;
70

    
71
static slab *rta_slab_[4];
72
static slab *nexthop_slab_[4];
73
static slab *rte_src_slab;
74

    
75
static struct idm src_ids;
76
#define SRC_ID_INIT_SIZE 4
77

    
78
/* rte source hash */
79

    
80
#define RSH_KEY(n)                n->proto, n->private_id
81
#define RSH_NEXT(n)                n->next
82
#define RSH_EQ(p1,n1,p2,n2)        p1 == p2 && n1 == n2
83
#define RSH_FN(p,n)                p->hash_key ^ u32_hash(n)
84

    
85
#define RSH_REHASH                rte_src_rehash
86
#define RSH_PARAMS                /2, *2, 1, 1, 8, 20
87
#define RSH_INIT_ORDER                6
88

    
89
static HASH(struct rte_src) src_hash;
90

    
91
struct protocol *attr_class_to_protocol[EAP_MAX];
92

    
93

    
94
static void
95
rte_src_init(void)
96
{
97
  rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
98

    
99
  idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
100

    
101
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
102
}
103

    
104

    
105
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
106

    
107
struct rte_src *
108
rt_find_source(struct proto *p, u32 id)
109
{
110
  return HASH_FIND(src_hash, RSH, p, id);
111
}
112

    
113
struct rte_src *
114
rt_get_source(struct proto *p, u32 id)
115
{
116
  struct rte_src *src = rt_find_source(p, id);
117

    
118
  if (src)
119
    return src;
120

    
121
  src = sl_alloc(rte_src_slab);
122
  src->proto = p;
123
  src->private_id = id;
124
  src->global_id = idm_alloc(&src_ids);
125
  src->uc = 0;
126

    
127
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
128

    
129
  return src;
130
}
131

    
132
void
133
rt_prune_sources(void)
134
{
135
  HASH_WALK_FILTER(src_hash, next, src, sp)
136
  {
137
    if (src->uc == 0)
138
    {
139
      HASH_DO_REMOVE(src_hash, RSH, sp);
140
      idm_free(&src_ids, src->global_id);
141
      sl_free(rte_src_slab, src);
142
    }
143
  }
144
  HASH_WALK_FILTER_END;
145

    
146
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
147
}
148

    
149

    
150
/*
151
 *        Multipath Next Hop
152
 */
153

    
154
static inline u32
155
nexthop_hash(struct nexthop *x)
156
{
157
  u32 h = 0;
158
  for (; x; x = x->next)
159
  {
160
    h ^= ipa_hash(x->gw) ^ (h << 5) ^ (h >> 9);
161

    
162
    for (int i = 0; i < x->labels; i++)
163
      h ^= x->label[i] ^ (h << 6) ^ (h >> 7);
164
  }
165

    
166
  return h;
167
}
168

    
169
int
170
nexthop__same(struct nexthop *x, struct nexthop *y)
171
{
172
  for (; x && y; x = x->next, y = y->next)
173
  {
174
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) ||
175
        (x->flags != y->flags) || (x->weight != y->weight) ||
176
        (x->labels != y->labels))
177
      return 0;
178

    
179
    for (int i = 0; i < x->labels; i++)
180
      if (x->label[i] != y->label[i])
181
        return 0;
182
  }
183

    
184
  return x == y;
185
}
186

    
187
static int
188
nexthop_compare_node(struct nexthop *x, struct nexthop *y)
189
{
190
  int r;
191

    
192
  if (!x)
193
    return 1;
194

    
195
  if (!y)
196
    return -1;
197

    
198
  /* Should we also compare flags ? */
199

    
200
  r = ((int) y->weight) - ((int) x->weight);
201
  if (r)
202
    return r;
203

    
204
  r = ipa_compare(x->gw, y->gw);
205
  if (r)
206
    return r;
207

    
208
  r = ((int) y->labels) - ((int) x->labels);
209
  if (r)
210
    return r;
211

    
212
  for (int i = 0; i < y->labels; i++)
213
  {
214
    r = ((int) y->label[i]) - ((int) x->label[i]);
215
    if (r)
216
      return r;
217
  }
218

    
219
  return ((int) x->iface->index) - ((int) y->iface->index);
220
}
221

    
222
static inline struct nexthop *
223
nexthop_copy_node(const struct nexthop *src, linpool *lp)
224
{
225
  struct nexthop *n = lp_alloc(lp, nexthop_size(src));
226

    
227
  memcpy(n, src, nexthop_size(src));
228
  n->next = NULL;
229

    
230
  return n;
231
}
232

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

    
261
  while ((x || y) && max--)
262
  {
263
    int cmp = nexthop_compare_node(x, y);
264
    if (cmp < 0)
265
    {
266
      *n = rx ? x : nexthop_copy_node(x, lp);
267
      x = x->next;
268
    }
269
    else if (cmp > 0)
270
    {
271
      *n = ry ? y : nexthop_copy_node(y, lp);
272
      y = y->next;
273
    }
274
    else
275
    {
276
      *n = rx ? x : (ry ? y : nexthop_copy_node(x, lp));
277
      x = x->next;
278
      y = y->next;
279
    }
280
    n = &((*n)->next);
281
  }
282
  *n = NULL;
283

    
284
  return root;
285
}
286

    
287
void
288
nexthop_insert(struct nexthop **n, struct nexthop *x)
289
{
290
  for (; *n; n = &((*n)->next))
291
  {
292
    int cmp = nexthop_compare_node(*n, x);
293

    
294
    if (cmp < 0)
295
      continue;
296
    else if (cmp > 0)
297
      break;
298
    else
299
      return;
300
  }
301

    
302
  x->next = *n;
303
  *n = x;
304
}
305

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

    
313
  return 1;
314
}
315

    
316
static inline slab *
317
nexthop_slab(struct nexthop *nh)
318
{
319
  return nexthop_slab_[MIN(nh->labels, 3)];
320
}
321

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

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

    
339
      *last = n;
340
      last = &(n->next);
341
    }
342

    
343
  return first;
344
}
345

    
346
static void
347
nexthop_free(struct nexthop *o)
348
{
349
  struct nexthop *n;
350

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

    
359

    
360
/*
361
 *        Extended Attributes
362
 */
363

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

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

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

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

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

    
441
  max = id + max;
442

    
443
  if (a)
444
    goto step;
445

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

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

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

    
474
        if (BIT32_TEST(s->visited, n))
475
          continue;
476

    
477
        BIT32_SET(s->visited, n);
478

    
479
        if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
480
          continue;
481

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

    
490
  return NULL;
491
}
492

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
746
  return GA_UNKNOWN;
747
}
748

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

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

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

    
769
  if (data)
770
    bsprintf(buf, " %08x", data);
771

    
772
  return;
773
}
774

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

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

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

    
794
  *buf = 0;
795
  return;
796
}
797

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

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

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

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

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

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

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

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

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

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

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

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

    
1011
/*
1012
 *        rta's
1013
 */
1014

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1156
  if (++rta_cache_count > rta_cache_limit)
1157
    rta_rehash();
1158

    
1159
  return r;
1160
}
1161

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

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

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

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

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

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

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

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

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

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

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

    
1291
  rta_alloc_hash();
1292
  rte_src_init();
1293
}
1294

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

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

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

1323
#endif