Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (14.5 KB)

1
/*
2
 *        BIRD -- Forwarding Information Base -- Data Structures
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: Forwarding Information Base
11
 *
12
 * FIB is a data structure designed for storage of routes indexed by their
13
 * network prefixes. It supports insertion, deletion, searching by prefix,
14
 * `routing' (in CIDR sense, that is searching for a longest prefix matching
15
 * a given IP address) and (which makes the structure very tricky to implement)
16
 * asynchronous reading, that is enumerating the contents of a FIB while other
17
 * modules add, modify or remove entries.
18
 *
19
 * Internally, each FIB is represented as a collection of nodes of type &fib_node
20
 * indexed using a sophisticated hashing mechanism.
21
 * We use two-stage hashing where we calculate a 16-bit primary hash key independent
22
 * on hash table size and then we just divide the primary keys modulo table size
23
 * to get a real hash key used for determining the bucket containing the node.
24
 * The lists of nodes in each bucket are sorted according to the primary hash
25
 * key, hence if we keep the total number of buckets to be a power of two,
26
 * re-hashing of the structure keeps the relative order of the nodes.
27
 *
28
 * To get the asynchronous reading consistent over node deletions, we need to
29
 * keep a list of readers for each node. When a node gets deleted, its readers
30
 * are automatically moved to the next node in the table.
31
 *
32
 * Basic FIB operations are performed by functions defined by this module,
33
 * enumerating of FIB contents is accomplished by using the FIB_WALK() macro
34
 * or FIB_ITERATE_START() if you want to do it asynchronously.
35
 */
36

    
37
#undef LOCAL_DEBUG
38

    
39
#include "nest/bird.h"
40
#include "nest/route.h"
41
#include "lib/string.h"
42

    
43
#define HASH_DEF_ORDER 10
44
#define HASH_HI_MARK *4
45
#define HASH_HI_STEP 2
46
#define HASH_HI_MAX 16
47
#define HASH_LO_MARK /5
48
#define HASH_LO_STEP 2
49
#define HASH_LO_MIN 10
50

    
51

    
52
static void
53
fib_ht_alloc(struct fib *f)
54
{
55
  f->hash_size = 1 << f->hash_order;
56
  f->hash_shift = 32 - f->hash_order;
57
  if (f->hash_order > HASH_HI_MAX - HASH_HI_STEP)
58
    f->entries_max = ~0;
59
  else
60
    f->entries_max = f->hash_size HASH_HI_MARK;
61
  if (f->hash_order < HASH_LO_MIN + HASH_LO_STEP)
62
    f->entries_min = 0;
63
  else
64
    f->entries_min = f->hash_size HASH_LO_MARK;
65
  DBG("Allocating FIB hash of order %d: %d entries, %d low, %d high\n",
66
      f->hash_order, f->hash_size, f->entries_min, f->entries_max);
67
  f->hash_table = mb_alloc(f->fib_pool, f->hash_size * sizeof(struct fib_node *));
68
}
69

    
70
static inline void
71
fib_ht_free(struct fib_node **h)
72
{
73
  mb_free(h);
74
}
75

    
76

    
77
static u32
78
fib_hash(struct fib *f, const net_addr *a);
79

    
80
/**
81
 * fib_init - initialize a new FIB
82
 * @f: the FIB to be initialized (the structure itself being allocated by the caller)
83
 * @p: pool to allocate the nodes in
84
 * @node_size: node size to be used (each node consists of a standard header &fib_node
85
 * followed by user data)
86
 * @hash_order: initial hash order (a binary logarithm of hash table size), 0 to use default order
87
 * (recommended)
88
 * @init: pointer a function to be called to initialize a newly created node
89
 *
90
 * This function initializes a newly allocated FIB and prepares it for use.
91
 */
92
void
93
fib_init(struct fib *f, pool *p, uint addr_type, uint node_size, uint node_offset, uint hash_order, fib_init_fn init)
94
{
95
  uint addr_length = net_addr_length[addr_type];
96

    
97
  if (!hash_order)
98
    hash_order = HASH_DEF_ORDER;
99
  f->fib_pool = p;
100
  f->fib_slab = addr_length ? sl_new(p, node_size + addr_length) : NULL;
101
  f->addr_type = addr_type;
102
  f->node_size = node_size;
103
  f->node_offset = node_offset;
104
  f->hash_order = hash_order;
105
  fib_ht_alloc(f);
106
  bzero(f->hash_table, f->hash_size * sizeof(struct fib_node *));
107
  f->entries = 0;
108
  f->entries_min = 0;
109
  f->init = init;
110
}
111

    
112
static void
113
fib_rehash(struct fib *f, int step)
114
{
115
  unsigned old, new, oldn, newn, ni, nh;
116
  struct fib_node **n, *e, *x, **t, **m, **h;
117

    
118
  old = f->hash_order;
119
  oldn = f->hash_size;
120
  new = old + step;
121
  m = h = f->hash_table;
122
  DBG("Re-hashing FIB from order %d to %d\n", old, new);
123
  f->hash_order = new;
124
  fib_ht_alloc(f);
125
  t = n = f->hash_table;
126
  newn = f->hash_size;
127
  ni = 0;
128

    
129
  while (oldn--)
130
    {
131
      x = *h++;
132
      while (e = x)
133
        {
134
          x = e->next;
135
          nh = fib_hash(f, e->addr);
136
          while (nh > ni)
137
            {
138
              *t = NULL;
139
              ni++;
140
              t = ++n;
141
            }
142
          *t = e;
143
          t = &e->next;
144
        }
145
    }
146
  while (ni < newn)
147
    {
148
      *t = NULL;
149
      ni++;
150
      t = ++n;
151
    }
152
  fib_ht_free(m);
153
}
154

    
155
#define CAST(t) (const net_addr_##t *)
156
#define CAST2(t) (net_addr_##t *)
157

    
158
#define FIB_HASH(f,a,t) (net_hash_##t(CAST(t) a) >> f->hash_shift)
159

    
160
#define FIB_FIND(f,a,t)                                                        \
161
  ({                                                                        \
162
    struct fib_node *e = f->hash_table[FIB_HASH(f, a, t)];                \
163
    while (e && !net_equal_##t(CAST(t) e->addr, CAST(t) a))                \
164
      e = e->next;                                                        \
165
    fib_node_to_user(f, e);                                                \
166
  })
167

    
168
#define FIB_INSERT(f,a,e,t)                                                \
169
  ({                                                                        \
170
  u32 h = net_hash_##t(CAST(t) a);                                        \
171
  struct fib_node **ee = f->hash_table + (h >> f->hash_shift);                \
172
  struct fib_node *g;                                                        \
173
                                                                        \
174
  while ((g = *ee) && (net_hash_##t(CAST(t) g->addr) < h))                \
175
    ee = &g->next;                                                        \
176
                                                                        \
177
  net_copy_##t(CAST2(t) e->addr, CAST(t) a);                                \
178
  e->next = *ee;                                                        \
179
  *ee = e;                                                                \
180
  })
181

    
182

    
183
static u32
184
fib_hash(struct fib *f, const net_addr *a)
185
{
186
  switch (f->addr_type)
187
  {
188
  case NET_IP4: return FIB_HASH(f, a, ip4);
189
  case NET_IP6: return FIB_HASH(f, a, ip6);
190
  case NET_VPN4: return FIB_HASH(f, a, vpn4);
191
  case NET_VPN6: return FIB_HASH(f, a, vpn6);
192
  default: bug("invalid type");
193
  }
194
}
195

    
196
/**
197
 * fib_find - search for FIB node by prefix
198
 * @f: FIB to search in
199
 * @n: network address
200
 *
201
 * Search for a FIB node corresponding to the given prefix, return
202
 * a pointer to it or %NULL if no such node exists.
203
 */
204
void *
205
fib_find(struct fib *f, const net_addr *a)
206
{
207
  ASSERT(f->addr_type == a->type);
208

    
209
  switch (f->addr_type)
210
  {
211
  case NET_IP4: return FIB_FIND(f, a, ip4);
212
  case NET_IP6: return FIB_FIND(f, a, ip6);
213
  case NET_VPN4: return FIB_FIND(f, a, vpn4);
214
  case NET_VPN6: return FIB_FIND(f, a, vpn6);
215
  default: bug("invalid type");
216
  }
217
}
218

    
219
static void
220
fib_insert(struct fib *f, const net_addr *a, struct fib_node *e)
221
{
222
  switch (f->addr_type)
223
  {
224
  case NET_IP4: FIB_INSERT(f, a, e, ip4); return;
225
  case NET_IP6: FIB_INSERT(f, a, e, ip6); return;
226
  case NET_VPN4: FIB_INSERT(f, a, e, vpn4); return;
227
  case NET_VPN6: FIB_INSERT(f, a, e, vpn6); return;
228
  default: bug("invalid type");
229
  }
230
}
231

    
232

    
233
/**
234
 * fib_get - find or create a FIB node
235
 * @f: FIB to work with
236
 * @n: network address
237
 *
238
 * Search for a FIB node corresponding to the given prefix and
239
 * return a pointer to it. If no such node exists, create it.
240
 */
241
void *
242
fib_get(struct fib *f, const net_addr *a)
243
{
244
  void *b = fib_find(f, a);
245
  if (b)
246
    return b;
247

    
248
  if (f->fib_slab)
249
    b = sl_alloc(f->fib_slab);
250
  else
251
    b = mb_alloc(f->fib_pool, f->node_size + a->length);
252

    
253
  struct fib_node *e = fib_user_to_node(f, b);
254
  e->readers = NULL;
255
  e->flags = 0;
256
  fib_insert(f, a, e);
257

    
258
  memset(b, 0, f->node_offset);
259
  if (f->init)
260
    f->init(b);
261

    
262
  if (f->entries++ > f->entries_max)
263
    fib_rehash(f, HASH_HI_STEP);
264

    
265
  return b;
266
}
267

    
268
static inline void *
269
fib_route_ip4(struct fib *f, net_addr_ip4 *n)
270
{
271
  void *r;
272

    
273
  while (!(r = fib_find(f, (net_addr *) n)) && (n->pxlen > 0))
274
  {
275
    n->pxlen--;
276
    ip4_clrbit(&n->prefix, n->pxlen);
277
  }
278

    
279
  return r;
280
}
281

    
282
static inline void *
283
fib_route_ip6(struct fib *f, net_addr_ip6 *n)
284
{
285
  void *r;
286

    
287
  while (!(r = fib_find(f, (net_addr *) n)) && (n->pxlen > 0))
288
  {
289
    n->pxlen--;
290
    ip6_clrbit(&n->prefix, n->pxlen);
291
  }
292

    
293
  return r;
294
}
295

    
296
/**
297
 * fib_route - CIDR routing lookup
298
 * @f: FIB to search in
299
 * @n: network address
300
 *
301
 * Search for a FIB node with longest prefix matching the given
302
 * network, that is a node which a CIDR router would use for routing
303
 * that network.
304
 */
305
void *
306
fib_route(struct fib *f, const net_addr *n)
307
{
308
  ASSERT(f->addr_type == n->type);
309

    
310
  net_addr *n0 = alloca(n->length);
311
  net_copy(n0, n);
312

    
313
  switch (n->type)
314
  {
315
  case NET_IP4:
316
  case NET_VPN4:
317
    return fib_route_ip4(f, (net_addr_ip4 *) n0);
318

    
319
  case NET_IP6:
320
  case NET_VPN6:
321
    return fib_route_ip6(f, (net_addr_ip6 *) n0);
322

    
323
  default:
324
    return NULL;
325
  }
326
}
327

    
328

    
329
static inline void
330
fib_merge_readers(struct fib_iterator *i, struct fib_node *to)
331
{
332
  if (to)
333
    {
334
      struct fib_iterator *j = to->readers;
335
      if (!j)
336
        {
337
          /* Fast path */
338
          to->readers = i;
339
          i->prev = (struct fib_iterator *) to;
340
        }
341
      else
342
        {
343
          /* Really merging */
344
          while (j->next)
345
            j = j->next;
346
          j->next = i;
347
          i->prev = j;
348
        }
349
      while (i && i->node)
350
        {
351
          i->node = NULL;
352
          i = i->next;
353
        }
354
    }
355
  else                                        /* No more nodes */
356
    while (i)
357
      {
358
        i->prev = NULL;
359
        i = i->next;
360
      }
361
}
362

    
363
/**
364
 * fib_delete - delete a FIB node
365
 * @f: FIB to delete from
366
 * @E: entry to delete
367
 *
368
 * This function removes the given entry from the FIB,
369
 * taking care of all the asynchronous readers by shifting
370
 * them to the next node in the canonical reading order.
371
 */
372
void
373
fib_delete(struct fib *f, void *E)
374
{
375
  struct fib_node *e = fib_user_to_node(f, E);
376
  uint h = fib_hash(f, e->addr);
377
  struct fib_node **ee = f->hash_table + h;
378
  struct fib_iterator *it;
379

    
380
  while (*ee)
381
    {
382
      if (*ee == e)
383
        {
384
          *ee = e->next;
385
          if (it = e->readers)
386
            {
387
              struct fib_node *l = e->next;
388
              while (!l)
389
                {
390
                  h++;
391
                  if (h >= f->hash_size)
392
                    break;
393
                  else
394
                    l = f->hash_table[h];
395
                }
396
              fib_merge_readers(it, l);
397
            }
398

    
399
          if (f->fib_slab)
400
            sl_free(f->fib_slab, E);
401
          else
402
            mb_free(E);
403

    
404
          if (f->entries-- < f->entries_min)
405
            fib_rehash(f, -HASH_LO_STEP);
406
          return;
407
        }
408
      ee = &((*ee)->next);
409
    }
410
  bug("fib_delete() called for invalid node");
411
}
412

    
413
/**
414
 * fib_free - delete a FIB
415
 * @f: FIB to be deleted
416
 *
417
 * This function deletes a FIB -- it frees all memory associated
418
 * with it and all its entries.
419
 */
420
void
421
fib_free(struct fib *f)
422
{
423
  fib_ht_free(f->hash_table);
424
  rfree(f->fib_slab);
425
}
426

    
427
void
428
fit_init(struct fib_iterator *i, struct fib *f)
429
{
430
  unsigned h;
431
  struct fib_node *n;
432

    
433
  i->efef = 0xff;
434
  for(h=0; h<f->hash_size; h++)
435
    if (n = f->hash_table[h])
436
      {
437
        i->prev = (struct fib_iterator *) n;
438
        if (i->next = n->readers)
439
          i->next->prev = i;
440
        n->readers = i;
441
        i->node = n;
442
        return;
443
      }
444
  /* The fib is empty, nothing to do */
445
  i->prev = i->next = NULL;
446
  i->node = NULL;
447
}
448

    
449
struct fib_node *
450
fit_get(struct fib *f, struct fib_iterator *i)
451
{
452
  struct fib_node *n;
453
  struct fib_iterator *j, *k;
454

    
455
  if (!i->prev)
456
    {
457
      /* We are at the end */
458
      i->hash = ~0 - 1;
459
      return NULL;
460
    }
461
  if (!(n = i->node))
462
    {
463
      /* No node info available, we are a victim of merging. Try harder. */
464
      j = i;
465
      while (j->efef == 0xff)
466
        j = j->prev;
467
      n = (struct fib_node *) j;
468
    }
469
  j = i->prev;
470
  if (k = i->next)
471
    k->prev = j;
472
  j->next = k;
473
  i->hash = fib_hash(f, n->addr);
474
  return n;
475
}
476

    
477
void
478
fit_put(struct fib_iterator *i, struct fib_node *n)
479
{
480
  struct fib_iterator *j;
481

    
482
  i->node = n;
483
  if (j = n->readers)
484
    j->prev = i;
485
  i->next = j;
486
  n->readers = i;
487
  i->prev = (struct fib_iterator *) n;
488
}
489

    
490
void
491
fit_put_next(struct fib *f, struct fib_iterator *i, struct fib_node *n, uint hpos)
492
{
493
  if (n = n->next)
494
    goto found;
495

    
496
  while (++hpos < f->hash_size)
497
    if (n = f->hash_table[hpos])
498
      goto found;
499

    
500
  /* We are at the end */
501
  i->prev = i->next = NULL;
502
  i->node = NULL;
503
  return;
504

    
505
found:
506
  fit_put(i, n);
507
}
508

    
509
#ifdef DEBUGGING
510

    
511
/**
512
 * fib_check - audit a FIB
513
 * @f: FIB to be checked
514
 *
515
 * This debugging function audits a FIB by checking its internal consistency.
516
 * Use when you suspect somebody of corrupting innocent data structures.
517
 */
518
void
519
fib_check(struct fib *f)
520
{
521
#if 0
522
  uint i, ec, lo, nulls;
523

524
  ec = 0;
525
  for(i=0; i<f->hash_size; i++)
526
    {
527
      struct fib_node *n;
528
      lo = 0;
529
      for(n=f->hash_table[i]; n; n=n->next)
530
        {
531
          struct fib_iterator *j, *j0;
532
          uint h0 = ipa_hash(n->prefix);
533
          if (h0 < lo)
534
            bug("fib_check: discord in hash chains");
535
          lo = h0;
536
          if ((h0 >> f->hash_shift) != i)
537
            bug("fib_check: mishashed %x->%x (order %d)", h0, i, f->hash_order);
538
          j0 = (struct fib_iterator *) n;
539
          nulls = 0;
540
          for(j=n->readers; j; j=j->next)
541
            {
542
              if (j->prev != j0)
543
                bug("fib_check: iterator->prev mismatch");
544
              j0 = j;
545
              if (!j->node)
546
                nulls++;
547
              else if (nulls)
548
                bug("fib_check: iterator nullified");
549
              else if (j->node != n)
550
                bug("fib_check: iterator->node mismatch");
551
            }
552
          ec++;
553
        }
554
    }
555
  if (ec != f->entries)
556
    bug("fib_check: invalid entry count (%d != %d)", ec, f->entries);
557
#endif
558
  return;
559
}
560

    
561
/*
562
int
563
fib_histogram(struct fib *f)
564
{
565
  log(L_WARN "Histogram dump start %d %d", f->hash_size, f->entries);
566

567
  int i, j;
568
  struct fib_node *e;
569

570
  for (i = 0; i < f->hash_size; i++)
571
    {
572
      j = 0;
573
      for (e = f->hash_table[i]; e != NULL; e = e->next)
574
        j++;
575
      if (j > 0)
576
        log(L_WARN "Histogram line %d: %d", i, j);
577
    }
578

579
  log(L_WARN "Histogram dump end");
580
}
581
*/
582

    
583
#endif
584

    
585
#ifdef TEST
586

    
587
#include "lib/resource.h"
588

    
589
struct fib f;
590

    
591
void dump(char *m)
592
{
593
  uint i;
594

    
595
  debug("%s ... order=%d, size=%d, entries=%d\n", m, f.hash_order, f.hash_size, f.hash_size);
596
  for(i=0; i<f.hash_size; i++)
597
    {
598
      struct fib_node *n;
599
      struct fib_iterator *j;
600
      for(n=f.hash_table[i]; n; n=n->next)
601
        {
602
          debug("%04x %08x %p %N", i, ipa_hash(n->prefix), n, n->addr);
603
          for(j=n->readers; j; j=j->next)
604
            debug(" %p[%p]", j, j->node);
605
          debug("\n");
606
        }
607
    }
608
  fib_check(&f);
609
  debug("-----\n");
610
}
611

    
612
void init(struct fib_node *n)
613
{
614
}
615

    
616
int main(void)
617
{
618
  struct fib_node *n;
619
  struct fib_iterator i, j;
620
  ip_addr a;
621
  int c;
622

    
623
  log_init_debug(NULL);
624
  resource_init();
625
  fib_init(&f, &root_pool, sizeof(struct fib_node), 4, init);
626
  dump("init");
627

    
628
  a = ipa_from_u32(0x01020304); n = fib_get(&f, &a, 32);
629
  a = ipa_from_u32(0x02030405); n = fib_get(&f, &a, 32);
630
  a = ipa_from_u32(0x03040506); n = fib_get(&f, &a, 32);
631
  a = ipa_from_u32(0x00000000); n = fib_get(&f, &a, 32);
632
  a = ipa_from_u32(0x00000c01); n = fib_get(&f, &a, 32);
633
  a = ipa_from_u32(0xffffffff); n = fib_get(&f, &a, 32);
634
  dump("fill");
635

    
636
  fit_init(&i, &f);
637
  dump("iter init");
638

    
639
  fib_rehash(&f, 1);
640
  dump("rehash up");
641

    
642
  fib_rehash(&f, -1);
643
  dump("rehash down");
644

    
645
next:
646
  c = 0;
647
  FIB_ITERATE_START(&f, &i, z)
648
    {
649
      if (c)
650
        {
651
          FIB_ITERATE_PUT(&i, z);
652
          dump("iter");
653
          goto next;
654
        }
655
      c = 1;
656
      debug("got %p\n", z);
657
    }
658
  FIB_ITERATE_END(z);
659
  dump("iter end");
660

    
661
  fit_init(&i, &f);
662
  fit_init(&j, &f);
663
  dump("iter init 2");
664

    
665
  n = fit_get(&f, &i);
666
  dump("iter step 2");
667

    
668
  fit_put(&i, n->next);
669
  dump("iter step 3");
670

    
671
  a = ipa_from_u32(0xffffffff); n = fib_get(&f, &a, 32);
672
  fib_delete(&f, n);
673
  dump("iter step 3");
674

    
675
  return 0;
676
}
677

    
678
#endif