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

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

47
48
49
#include "nest/bird.h"
#include "nest/route.h"
#include "nest/protocol.h"
50
#include "nest/iface.h"
51
#include "nest/cli.h"
52
#include "nest/attrs.h"
Ondřej Filip's avatar
Ondřej Filip committed
53
#include "lib/alloca.h"
Ondřej Zajíček's avatar
Ondřej Zajíček committed
54
#include "lib/hash.h"
55
#include "lib/resource.h"
56
#include "lib/string.h"
57

58
59
pool *rta_pool;

60
static slab *rta_slab;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
61
static slab *mpnh_slab;
62
63
64
65
66
static slab *rte_src_slab;

/* rte source ID bitmap */
static u32 *src_ids;
static u32 src_id_size, src_id_used, src_id_pos;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
67
#define SRC_ID_INIT_SIZE 4
68
69

/* rte source hash */
Ondřej Zajíček's avatar
Ondřej Zajíček committed
70
71
72
73
74
75
76
77
78
79
80

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

#define RSH_REHASH		rte_src_rehash
#define RSH_PARAMS		/2, *2, 1, 1, 8, 20
#define RSH_INIT_ORDER		6

static HASH(struct rte_src) src_hash;
81

82
83
struct protocol *attr_class_to_protocol[EAP_MAX];

84
85
86
87
88
89
90

static void
rte_src_init(void)
{
  rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));

  src_id_pos = 0;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
91
  src_id_size = SRC_ID_INIT_SIZE;
92
93
94
95
96
97
  src_ids = mb_allocz(rta_pool, src_id_size * sizeof(u32));

 /* ID 0 is reserved */
  src_ids[0] = 1;
  src_id_used = 1;

Ondřej Zajíček's avatar
Ondřej Zajíček committed
98
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
}

static inline int u32_cto(unsigned int x) { return ffs(~x) - 1; }

static inline u32
rte_src_alloc_id(void)
{
  int i, j;
  for (i = src_id_pos; i < src_id_size; i++)
    if (src_ids[i] != 0xffffffff)
      goto found;

  /* If we are at least 7/8 full, expand */
  if (src_id_used > (src_id_size * 28))
    {
      src_id_size *= 2;
115
      src_ids = mb_realloc(src_ids, src_id_size * sizeof(u32));
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
      bzero(src_ids + i, (src_id_size - i) * sizeof(u32));
      goto found;
    }

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

  ASSERT(0);

 found:
  ASSERT(i < 0x8000000);

  src_id_pos = i;
  j = u32_cto(src_ids[i]);

  src_ids[i] |= (1 << j);
  src_id_used++;
  return 32 * i + j;
}

static inline void
rte_src_free_id(u32 id)
{
  int i = id / 32;
  int j = id % 32;

  ASSERT((i < src_id_size) && (src_ids[i] & (1 << j)));
  src_ids[i] &= ~(1 << j);
  src_id_used--;
}


Ondřej Zajíček's avatar
Ondřej Zajíček committed
149
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
150
151
152
153

struct rte_src *
rt_find_source(struct proto *p, u32 id)
{
Ondřej Zajíček's avatar
Ondřej Zajíček committed
154
  return HASH_FIND(src_hash, RSH, p, id);
155
156
157
158
159
}

struct rte_src *
rt_get_source(struct proto *p, u32 id)
{
Ondřej Zajíček's avatar
Ondřej Zajíček committed
160
  struct rte_src *src = rt_find_source(p, id);
161

Ondřej Zajíček's avatar
Ondřej Zajíček committed
162
163
  if (src)
    return src;
164
165
166
167
168
169

  src = sl_alloc(rte_src_slab);
  src->proto = p;
  src->private_id = id;
  src->global_id = rte_src_alloc_id();
  src->uc = 0;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
170
171
  
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
172
173
174
175
176
177
178

  return src;
}

void
rt_prune_sources(void)
{
Ondřej Zajíček's avatar
Ondřej Zajíček committed
179
180
181
  HASH_WALK_FILTER(src_hash, next, src, sp)
  {
    if (src->uc == 0)
182
    {
Ondřej Zajíček's avatar
Ondřej Zajíček committed
183
184
185
      HASH_DO_REMOVE(src_hash, RSH, sp);
      rte_src_free_id(src->global_id);
      sl_free(rte_src_slab, src);
186
    }
Ondřej Zajíček's avatar
Ondřej Zajíček committed
187
188
  }
  HASH_WALK_FILTER_END;
189

Ondřej Zajíček's avatar
Ondřej Zajíček committed
190
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
191
192
193
194
195
196
197
}


/*
 *	Multipath Next Hop
 */

Ondřej Zajíček's avatar
Ondřej Zajíček committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
static inline unsigned int
mpnh_hash(struct mpnh *x)
{
  unsigned int h = 0;
  for (; x; x = x->next)
    h ^= ipa_hash(x->gw);

  return h;
}

int
mpnh__same(struct mpnh *x, struct mpnh *y)
{
  for (; x && y; x = x->next, y = y->next)
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight))
      return 0;

  return x == y;
}

static struct mpnh *
mpnh_copy(struct mpnh *o)
{
  struct mpnh *first = NULL;
  struct mpnh **last = &first;

  for (; o; o = o->next)
    {
      struct mpnh *n = sl_alloc(mpnh_slab);
      n->gw = o->gw;
      n->iface = o->iface;
      n->next = NULL;
      n->weight = o->weight;

      *last = n;
      last = &(n->next);
    }

  return first;
}

static void
mpnh_free(struct mpnh *o)
{
  struct mpnh *n;

  while (o)
    {
      n = o->next;
      sl_free(mpnh_slab, o);
      o = n;
    }
}


253
254
255
256
/*
 *	Extended Attributes
 */

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

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

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

  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
      !(id & EA_ALLOW_UNDEF))
    return NULL;
  return a;
}

310
311
312
313
314
315
316
317
318
319
/**
 * ea_get_int - fetch an integer attribute
 * @e: attribute list
 * @id: attribute ID
 * @def: default value
 *
 * This function is a shortcut for retrieving a value of an integer attribute
 * by calling ea_find() to find the attribute, extracting its value or returning
 * a provided default if no such attribute is present.
 */
320
321
322
323
324
325
326
327
328
int
ea_get_int(ea_list *e, unsigned id, int def)
{
  eattr *a = ea_find(e, id);
  if (!a)
    return def;
  return a->u.data;
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
static inline void
ea_do_sort(ea_list *e)
{
  unsigned n = e->count;
  eattr *a = e->attrs;
  eattr *b = alloca(n * sizeof(eattr));
  unsigned s, ss;

  /* We need to use a stable sorting algorithm, hence mergesort */
  do
    {
      s = ss = 0;
      while (s < n)
	{
	  eattr *p, *q, *lo, *hi;
	  p = b;
	  ss = s;
	  *p++ = a[s++];
	  while (s < n && p[-1].id <= a[s].id)
	    *p++ = a[s++];
	  if (s < n)
	    {
	      q = p;
	      *p++ = a[s++];
	      while (s < n && p[-1].id <= a[s].id)
		*p++ = a[s++];
	      lo = b;
	      hi = q;
	      s = ss;
	      while (lo < q && hi < p)
		if (lo->id <= hi->id)
		  a[s++] = *lo++;
		else
		  a[s++] = *hi++;
	      while (lo < q)
		a[s++] = *lo++;
	      while (hi < p)
		a[s++] = *hi++;
	    }
	}
    }
  while (ss);
}

static inline void
ea_do_prune(ea_list *e)
{
376
  eattr *s, *d, *l, *s0;
377
378
379
380
381
382
383
  int i = 0;

  /* Discard duplicates and undefs. Do you remember sorting was stable? */
  s = d = e->attrs;
  l = e->attrs + e->count;
  while (s < l)
    {
384
385
386
387
388
      s0 = s++;
      while (s < l && s->id == s[-1].id)
	s++;
      /* s0 is the most recent version, s[-1] the oldest one */
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
389
	{
390
391
392
	  *d = *s0;
	  d->type = (d->type & ~EAF_ORIGINATED) | (s[-1].type & EAF_ORIGINATED);
	  d++;
393
394
395
396
	  i++;
	}
    }
  e->count = i;
397
398
}

399
400
401
402
403
404
405
406
/**
 * ea_sort - sort an attribute list
 * @e: list to be sorted
 *
 * This function takes a &ea_list chain and sorts the attributes
 * within each of its entries.
 *
 * If an attribute occurs multiple times in a single &ea_list,
Martin Mareš's avatar
Martin Mareš committed
407
 * ea_sort() leaves only the first (the only significant) occurrence.
408
 */
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
void
ea_sort(ea_list *e)
{
  while (e)
    {
      if (!(e->flags & EALF_SORTED))
	{
	  ea_do_sort(e);
	  ea_do_prune(e);
	  e->flags |= EALF_SORTED;
	}
      if (e->count > 5)
	e->flags |= EALF_BISECT;
      e = e->next;
    }
}

426
427
428
429
430
431
432
/**
 * ea_scan - estimate attribute list size
 * @e: attribute list
 *
 * This function calculates an upper bound of the size of
 * a given &ea_list after merging with ea_merge().
 */
433
434
435
436
437
438
439
440
441
442
443
444
445
unsigned
ea_scan(ea_list *e)
{
  unsigned cnt = 0;

  while (e)
    {
      cnt += e->count;
      e = e->next;
    }
  return sizeof(ea_list) + sizeof(eattr)*cnt;
}

446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
 * ea_merge - merge segments of an attribute list
 * @e: attribute list
 * @t: buffer to store the result to
 *
 * This function takes a possibly multi-segment attribute list
 * and merges all of its segments to one.
 *
 * The primary use of this function is for &ea_list normalization:
 * first call ea_scan() to determine how much memory will the result
 * take, then allocate a buffer (usually using alloca()), merge the
 * segments with ea_merge() and finally sort and prune the result
 * by calling ea_sort().
 */
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
void
ea_merge(ea_list *e, ea_list *t)
{
  eattr *d = t->attrs;

  t->flags = 0;
  t->count = 0;
  t->next = NULL;
  while (e)
    {
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
      t->count += e->count;
      d += e->count;
      e = e->next;
    }
}

477
478
479
480
481
482
483
484
/**
 * ea_same - compare two &ea_list's
 * @x: attribute list
 * @y: attribute list
 *
 * ea_same() compares two normalized attribute lists @x and @y and returns
 * 1 if they contain the same attributes, 0 otherwise.
 */
485
int
486
487
488
489
ea_same(ea_list *x, ea_list *y)
{
  int c;

490
491
492
493
494
495
  if (!x || !y)
    return x == y;
  ASSERT(!x->next && !y->next);
  if (x->count != y->count)
    return 0;
  for(c=0; c<x->count; c++)
496
    {
497
498
499
500
501
502
      eattr *a = &x->attrs[c];
      eattr *b = &y->attrs[c];

      if (a->id != b->id ||
	  a->flags != b->flags ||
	  a->type != b->type ||
Ondřej Zajíček's avatar
Ondřej Zajíček committed
503
	  ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
504
	return 0;
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    }
  return 1;
}

static inline ea_list *
ea_list_copy(ea_list *o)
{
  ea_list *n;
  unsigned i, len;

  if (!o)
    return NULL;
  ASSERT(!o->next);
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
  n = mb_alloc(rta_pool, len);
  memcpy(n, o, len);
  n->flags |= EALF_CACHED;
  for(i=0; i<o->count; i++)
    {
      eattr *a = &n->attrs[i];
525
      if (!(a->type & EAF_EMBEDDED))
526
527
528
529
530
531
532
533
534
535
	{
	  unsigned size = sizeof(struct adata) + a->u.ptr->length;
	  struct adata *d = mb_alloc(rta_pool, size);
	  memcpy(d, a->u.ptr, size);
	  a->u.ptr = d;
	}
    }
  return n;
}

Martin Mareš's avatar
Martin Mareš committed
536
537
538
static inline void
ea_free(ea_list *o)
{
539
540
  int i;

Martin Mareš's avatar
Martin Mareš committed
541
542
543
  if (o)
    {
      ASSERT(!o->next);
544
545
546
547
548
549
      for(i=0; i<o->count; i++)
	{
	  eattr *a = &o->attrs[i];
	  if (!(a->type & EAF_EMBEDDED))
	    mb_free(a->u.ptr);
	}
Martin Mareš's avatar
Martin Mareš committed
550
551
552
553
      mb_free(o);
    }
}

Ondřej Zajíček's avatar
Ondřej Zajíček committed
554
555
556
557
558
559
560
561
562
563
564
565
static int
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
{
  if (a->id == EA_GEN_IGP_METRIC)
    {
      *buf += bsprintf(*buf, "igp_metric");
      return GA_NAME;
    }
 
  return GA_UNKNOWN;
}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
static inline void
opaque_format(struct adata *ad, byte *buf, unsigned int size)
{
  byte *bound = buf + size - 10;
  int i;

  for(i = 0; i < ad->length; i++)
    {
      if (buf > bound)
	{
	  strcpy(buf, " ...");
	  return;
	}
      if (i)
	*buf++ = ' ';

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

  *buf = 0;
  return;
}

static inline void
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
{
  int i = int_set_format(ad, way, 0, pos, end - pos);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
593
  cli_printf(c, -1012, "\t%s", buf);
594
595
596
  while (i)
    {
      i = int_set_format(ad, way, i, buf, end - buf - 1);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
597
      cli_printf(c, -1012, "\t\t%s", buf);
598
599
600
    }
}

Ondřej Zajíček's avatar
Ondřej Zajíček committed
601
602
603
604
static inline void
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
{
  int i = ec_set_format(ad, 0, pos, end - pos);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
605
  cli_printf(c, -1012, "\t%s", buf);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
606
607
608
  while (i)
    {
      i = ec_set_format(ad, i, buf, end - buf - 1);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
609
      cli_printf(c, -1012, "\t\t%s", buf);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
610
611
612
    }
}

613
/**
614
615
616
 * ea_show - print an &eattr to CLI
 * @c: destination CLI
 * @e: attribute to be printed
617
 *
618
619
 * This function takes an extended attribute represented by its &eattr
 * structure and prints it to the CLI according to the type information.
620
621
622
623
 *
 * If the protocol defining the attribute provides its own
 * get_attr() hook, it's consulted first.
 */
624
void
625
ea_show(struct cli *c, eattr *e)
626
627
628
629
{
  struct protocol *p;
  int status = GA_UNKNOWN;
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
630
631
  byte buf[CLI_MSG_SIZE];
  byte *pos = buf, *end = buf + sizeof(buf);
632
633
634

  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
    {
635
      pos += bsprintf(pos, "%s.", p->name);
636
      if (p->get_attr)
637
638
	status = p->get_attr(e, pos, end - pos);
      pos += strlen(pos);
639
640
    }
  else if (EA_PROTO(e->id))
641
    pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
Ondřej Zajíček's avatar
Ondřej Zajíček committed
642
  else 
643
    status = get_generic_attr(e, &pos, end - pos);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
644

645
  if (status < GA_NAME)
646
    pos += bsprintf(pos, "%02x", EA_ID(e->id));
647
648
  if (status < GA_FULL)
    {
649
650
      *pos++ = ':';
      *pos++ = ' ';
651
652
653
      switch (e->type & EAF_TYPE_MASK)
	{
	case EAF_TYPE_INT:
654
	  bsprintf(pos, "%u", e->u.data);
655
656
	  break;
	case EAF_TYPE_OPAQUE:
657
	  opaque_format(ad, pos, end - pos);
658
659
	  break;
	case EAF_TYPE_IP_ADDRESS:
660
	  bsprintf(pos, "%I", *(ip_addr *) ad->data);
661
662
	  break;
	case EAF_TYPE_ROUTER_ID:
663
	  bsprintf(pos, "%R", e->u.data);
664
	  break;
665
	case EAF_TYPE_AS_PATH:
666
	  as_path_format(ad, pos, end - pos);
667
668
	  break;
	case EAF_TYPE_INT_SET:
669
670
	  ea_show_int_set(c, ad, 1, pos, buf, end);
	  return;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
671
672
673
	case EAF_TYPE_EC_SET:
	  ea_show_ec_set(c, ad, pos, buf, end);
	  return;
674
675
	case EAF_TYPE_UNDEF:
	default:
676
	  bsprintf(pos, "<type %02x>", e->type);
677
678
	}
    }
Ondřej Zajíček's avatar
Ondřej Zajíček committed
679
  cli_printf(c, -1012, "\t%s", buf);
680
681
}

682
683
684
685
686
687
688
/**
 * ea_dump - dump an extended attribute
 * @e: attribute to be dumped
 *
 * ea_dump() dumps contents of the extended attribute given to
 * the debug output.
 */
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
void
ea_dump(ea_list *e)
{
  int i;

  if (!e)
    {
      debug("NONE");
      return;
    }
  while (e)
    {
      debug("[%c%c%c]",
	    (e->flags & EALF_SORTED) ? 'S' : 's',
	    (e->flags & EALF_BISECT) ? 'B' : 'b',
	    (e->flags & EALF_CACHED) ? 'C' : 'c');
      for(i=0; i<e->count; i++)
706
	{
707
708
	  eattr *a = &e->attrs[i];
	  debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
709
710
	  if (a->type & EAF_TEMP)
	    debug("T");
711
	  debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
712
713
	  if (a->type & EAF_ORIGINATED)
	    debug("o");
714
715
716
717
718
719
720
721
722
	  if (a->type & EAF_EMBEDDED)
	    debug(":%08x", a->u.data);
	  else
	    {
	      int j, len = a->u.ptr->length;
	      debug("[%d]:", len);
	      for(j=0; j<len; j++)
		debug("%02x", a->u.ptr->data[j]);
	    }
723
	}
724
725
      if (e = e->next)
	debug(" | ");
726
727
728
    }
}

729
730
731
732
733
734
735
/**
 * ea_hash - calculate an &ea_list hash key
 * @e: attribute list
 *
 * ea_hash() takes an extended attribute list and calculated a hopefully
 * uniformly distributed hash value from its contents.
 */
736
inline unsigned int
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
ea_hash(ea_list *e)
{
  u32 h = 0;
  int i;

  if (e)			/* Assuming chain of length 1 */
    {
      for(i=0; i<e->count; i++)
	{
	  struct eattr *a = &e->attrs[i];
	  h ^= a->id;
	  if (a->type & EAF_EMBEDDED)
	    h ^= a->u.data;
	  else
	    {
	      struct adata *d = a->u.ptr;
	      int size = d->length;
	      byte *z = d->data;
	      while (size >= 4)
		{
		  h ^= *(u32 *)z;
		  z += 4;
		  size -= 4;
		}
	      while (size--)
		h = (h >> 24) ^ (h << 8) ^ *z++;
	    }
	}
      h ^= h >> 16;
      h ^= h >> 6;
      h &= 0xffff;
    }
  return h;
}

772
773
774
775
776
777
778
779
/**
 * ea_append - concatenate &ea_list's
 * @to: destination list (can be %NULL)
 * @what: list to be appended (can be %NULL)
 *
 * This function appends the &ea_list @what at the end of
 * &ea_list @to and returns a pointer to the resulting list.
 */
780
781
782
783
784
785
786
787
788
789
790
791
792
793
ea_list *
ea_append(ea_list *to, ea_list *what)
{
  ea_list *res;

  if (!to)
    return what;
  res = to;
  while (to->next)
    to = to->next;
  to->next = what;
  return res;
}

794
795
796
797
/*
 *	rta's
 */

798
799
800
801
802
803
804
805
806
static unsigned int rta_cache_count;
static unsigned int rta_cache_size = 32;
static unsigned int rta_cache_limit;
static unsigned int rta_cache_mask;
static rta **rta_hash_table;

static void
rta_alloc_hash(void)
{
Martin Mareš's avatar
Martin Mareš committed
807
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
808
809
810
811
812
813
814
815
816
817
  if (rta_cache_size < 32768)
    rta_cache_limit = rta_cache_size * 2;
  else
    rta_cache_limit = ~0;
  rta_cache_mask = rta_cache_size - 1;
}

static inline unsigned int
rta_hash(rta *a)
{
Ondřej Zajíček's avatar
Ondřej Zajíček committed
818
  return (((uint) (uintptr_t) a->src) ^ ipa_hash(a->gw) ^
Ondřej Zajíček's avatar
Ondřej Zajíček committed
819
	  mpnh_hash(a->nexthops) ^ ea_hash(a->eattrs)) & 0xffff;
820
821
}

822
823
824
static inline int
rta_same(rta *x, rta *y)
{
825
  return (x->src == y->src &&
826
827
828
829
830
	  x->source == y->source &&
	  x->scope == y->scope &&
	  x->cast == y->cast &&
	  x->dest == y->dest &&
	  x->flags == y->flags &&
831
	  x->igp_metric == y->igp_metric &&
832
833
834
	  ipa_equal(x->gw, y->gw) &&
	  ipa_equal(x->from, y->from) &&
	  x->iface == y->iface &&
835
	  x->hostentry == y->hostentry &&
Ondřej Zajíček's avatar
Ondřej Zajíček committed
836
	  mpnh_same(x->nexthops, y->nexthops) &&
837
	  ea_same(x->eattrs, y->eattrs));
838
839
840
841
842
843
844
845
846
}

static rta *
rta_copy(rta *o)
{
  rta *r = sl_alloc(rta_slab);

  memcpy(r, o, sizeof(rta));
  r->uc = 1;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
847
  r->nexthops = mpnh_copy(o->nexthops);
848
  r->eattrs = ea_list_copy(o->eattrs);
849
850
851
  return r;
}

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
static inline void
rta_insert(rta *r)
{
  unsigned int h = r->hash_key & rta_cache_mask;
  r->next = rta_hash_table[h];
  if (r->next)
    r->next->pprev = &r->next;
  r->pprev = &rta_hash_table[h];
  rta_hash_table[h] = r;
}

static void
rta_rehash(void)
{
  unsigned int ohs = rta_cache_size;
  unsigned int h;
  rta *r, *n;
  rta **oht = rta_hash_table;

  rta_cache_size = 2*rta_cache_size;
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
  rta_alloc_hash();
  for(h=0; h<ohs; h++)
    for(r=oht[h]; r; r=n)
      {
	n = r->next;
	rta_insert(r);
      }
  mb_free(oht);
}

883
884
/**
 * rta_lookup - look up a &rta in attribute cache
Martin Mareš's avatar
Martin Mareš committed
885
 * @o: a un-cached &rta
886
 *
Martin Mareš's avatar
Martin Mareš committed
887
 * rta_lookup() gets an un-cached &rta structure and returns its cached
888
889
890
891
892
893
894
895
 * counterpart. It starts with examining the attribute cache to see whether
 * there exists a matching entry. If such an entry exists, it's returned and
 * its use count is incremented, else a new entry is created with use count
 * set to 1.
 *
 * The extended attribute lists attached to the &rta are automatically
 * converted to the normalized form.
 */
896
897
898
899
rta *
rta_lookup(rta *o)
{
  rta *r;
900
  unsigned int h;
901

902
  ASSERT(!(o->aflags & RTAF_CACHED));
903
  if (o->eattrs)
904
    {
905
      if (o->eattrs->next)	/* Multiple ea_list's, need to merge them */
906
	{
907
908
909
	  ea_list *ml = alloca(ea_scan(o->eattrs));
	  ea_merge(o->eattrs, ml);
	  o->eattrs = ml;
910
	}
911
      ea_sort(o->eattrs);
912
913
    }

914
915
916
  h = rta_hash(o);
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
    if (r->hash_key == h && rta_same(r, o))
917
      return rta_clone(r);
918

919
  r = rta_copy(o);
920
  r->hash_key = h;
921
  r->aflags = RTAF_CACHED;
922
  rt_lock_source(r->src);
923
  rt_lock_hostentry(r->hostentry);
924
925
926
927
928
  rta_insert(r);

  if (++rta_cache_count > rta_cache_limit)
    rta_rehash();

929
930
931
932
  return r;
}

void
933
rta__free(rta *a)
934
{
935
936
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
  rta_cache_count--;
Martin Mareš's avatar
Martin Mareš committed
937
938
939
940
  *a->pprev = a->next;
  if (a->next)
    a->next->pprev = a->pprev;
  a->aflags = 0;		/* Poison the entry */
941
  rt_unlock_hostentry(a->hostentry);
942
  rt_unlock_source(a->src);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
943
  mpnh_free(a->nexthops);
Martin Mareš's avatar
Martin Mareš committed
944
945
  ea_free(a->eattrs);
  sl_free(rta_slab, a);
946
947
}

948
949
950
951
952
953
/**
 * rta_dump - dump route attributes
 * @a: attribute structure to dump
 *
 * This function takes a &rta and dumps its contents to the debug output.
 */
954
void
955
rta_dump(rta *a)
956
{
957
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
Martin Mareš's avatar
Martin Mareš committed
958
			 "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
Ondřej Filip's avatar
Ondřej Filip committed
959
960
			 "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
                         "RTS_OSPF_EXT2", "RTS_BGP" };
961
962
963
  static char *rtc[] = { "", " BC", " MC", " AC" };
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };

964
  debug("p=%s uc=%d %s %s%s%s h=%04x",
965
	a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
966
	rtd[a->dest], a->hash_key);
967
968
  if (!(a->aflags & RTAF_CACHED))
    debug(" !CACHED");
969
  debug(" <-%I", a->from);
970
  if (a->dest == RTD_ROUTER)
971
    debug(" ->%I", a->gw);
972
  if (a->dest == RTD_DEVICE || a->dest == RTD_ROUTER)
973
    debug(" [%s]", a->iface ? a->iface->name : "???" );
974
  if (a->eattrs)
975
976
    {
      debug(" EA: ");
977
      ea_dump(a->eattrs);
978
    }
979
980
}

981
982
983
984
985
986
/**
 * rta_dump_all - dump attribute cache
 *
 * This function dumps the whole contents of route attribute cache
 * to the debug output.
 */
987
988
989
void
rta_dump_all(void)
{
990
  rta *a;
991
992
993
994
995
996
997
998
999
1000
  unsigned int h;

  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
  for(h=0; h<rta_cache_size; h++)
    for(a=rta_hash_table[h]; a; a=a->next)
      {
	debug("%p ", a);
	rta_dump(a);
	debug("\n");
      }
1001
  debug("\n");
1002
1003
}

1004
void
1005
rta_show(struct cli *c, rta *a, ea_list *eal)
1006
1007
{
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1008
			       "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1009
  static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
1010
  int i;
1011
1012

  cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
1013
1014
1015
  if (!eal)
    eal = a->eattrs;
  for(; eal; eal=eal->next)
1016
    for(i=0; i<eal->count; i++)
1017
      ea_show(c, &eal->attrs[i]);
1018
1019
}

1020
1021
1022
1023
1024
1025
/**
 * rta_init - initialize route attribute cache
 *
 * This function is called during initialization of the routing
 * table module to set up the internals of the attribute cache.
 */
1026
1027
1028
void
rta_init(void)
{
Martin Mareš's avatar
Martin Mareš committed
1029
  rta_pool = rp_new(&root_pool, "Attributes");
1030
  rta_slab = sl_new(rta_pool, sizeof(rta));
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1031
  mpnh_slab = sl_new(rta_pool, sizeof(struct mpnh));
1032
  rta_alloc_hash();
1033
  rte_src_init();
1034
}
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064

/*
 *  Documentation for functions declared inline in route.h
 */
#if 0

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

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

#endif