Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-fib.c @ 0f7d5b1a

History | View | Annotate | Download (14.7 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                        /* Must be at most 16 */
47
#define HASH_LO_MARK /5
48
#define HASH_LO_STEP 2
49
#define HASH_LO_MIN 10
50

    
51

    
52
static inline void * fib_node_to_user(struct fib *f, struct fib_node *e)
53
{ return (void *) ((char *) e - f->node_offset); }
54

    
55
static inline struct fib_node * fib_user_to_node(struct fib *f, void *e)
56
{ return (void *) ((char *) e + f->node_offset); }
57

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

    
76
static inline void
77
fib_ht_free(struct fib_node **h)
78
{
79
  mb_free(h);
80
}
81

    
82

    
83
static u32
84
fib_hash(struct fib *f, const net_addr *a);
85

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

    
103
  if (!hash_order)
104
    hash_order = HASH_DEF_ORDER;
105
  f->fib_pool = p;
106
  f->fib_slab = addr_length ? sl_new(p, node_size + addr_length) : NULL;
107
  f->addr_type = addr_type;
108
  f->node_size = node_size;
109
  f->node_offset = node_offset;
110
  f->hash_order = hash_order;
111
  fib_ht_alloc(f);
112
  bzero(f->hash_table, f->hash_size * sizeof(struct fib_node *));
113
  f->entries = 0;
114
  f->entries_min = 0;
115
  f->init = init;
116
}
117

    
118
static void
119
fib_rehash(struct fib *f, int step)
120
{
121
  unsigned old, new, oldn, newn, ni, nh;
122
  struct fib_node **n, *e, *x, **t, **m, **h;
123

    
124
  old = f->hash_order;
125
  oldn = f->hash_size;
126
  new = old + step;
127
  m = h = f->hash_table;
128
  DBG("Re-hashing FIB from order %d to %d\n", old, new);
129
  f->hash_order = new;
130
  fib_ht_alloc(f);
131
  t = n = f->hash_table;
132
  newn = f->hash_size;
133
  ni = 0;
134

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

    
161
#define CAST(t) (const net_addr_##t *)
162
#define CAST2(t) (net_addr_##t *)
163

    
164
#define FIB_HASH(f,a,t) (net_hash_##t(CAST(t) a) >> f->hash_shift)
165

    
166
#define FIB_FIND(f,a,t)                                                        \
167
  ({                                                                        \
168
    struct fib_node *e = f->hash_table[FIB_HASH(f, a, t)];                \
169
    while (e && !net_equal_##t(CAST(t) e->addr, CAST(t) a))                \
170
      e = e->next;                                                        \
171
    fib_node_to_user(f, e);                                                \
172
  })
173

    
174
#define FIB_INSERT(f,a,e,t)                                                \
175
  ({                                                                        \
176
  u32 h = net_hash_##t(CAST(t) a);                                        \
177
  struct fib_node **ee = f->hash_table + (h >> f->hash_shift);                \
178
  struct fib_node *g;                                                        \
179
                                                                        \
180
  while ((g = *ee) && (net_hash_##t(CAST(t) g->addr) < h))                \
181
    ee = &g->next;                                                        \
182
                                                                        \
183
  net_copy_##t(CAST2(t) e->addr, CAST(t) a);                                \
184
  e->next = *ee;                                                        \
185
  *ee = e;                                                                \
186
  })
187

    
188

    
189
static u32
190
fib_hash(struct fib *f, const net_addr *a)
191
{
192
  switch (f->addr_type)
193
  {
194
  case NET_IP4: return FIB_HASH(f, a, ip4);
195
  case NET_IP6: return FIB_HASH(f, a, ip6);
196
  case NET_VPN4: return FIB_HASH(f, a, vpn4);
197
  case NET_VPN6: return FIB_HASH(f, a, vpn6);
198
  default: bug("invalid type");
199
  }
200
}
201

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

    
215
  switch (f->addr_type)
216
  {
217
  case NET_IP4: return FIB_FIND(f, a, ip4);
218
  case NET_IP6: return FIB_FIND(f, a, ip6);
219
  case NET_VPN4: return FIB_FIND(f, a, vpn4);
220
  case NET_VPN6: return FIB_FIND(f, a, vpn6);
221
  default: bug("invalid type");
222
  }
223
}
224

    
225
static void
226
fib_insert(struct fib *f, const net_addr *a, struct fib_node *e)
227
{
228
  switch (f->addr_type)
229
  {
230
  case NET_IP4: FIB_INSERT(f, a, e, ip4); return;
231
  case NET_IP6: FIB_INSERT(f, a, e, ip6); return;
232
  case NET_VPN4: FIB_INSERT(f, a, e, vpn4); return;
233
  case NET_VPN6: FIB_INSERT(f, a, e, vpn6); return;
234
  default: bug("invalid type");
235
  }
236
}
237

    
238

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

    
254
  if (f->fib_slab)
255
    b = sl_alloc(f->fib_slab);
256
  else
257
    b = mb_alloc(f->fib_pool, f->node_size + a->length);
258

    
259
  struct fib_node *e = (void *) (b + f->node_offset);
260
  e->readers = NULL;
261
  e->flags = 0;
262
  e->uid = 0;
263
  fib_insert(f, a, e);
264

    
265
  memset(b, 0, f->node_offset);
266
  if (f->init)
267
    f->init(b);
268

    
269
  if (f->entries++ > f->entries_max)
270
    fib_rehash(f, HASH_HI_STEP);
271

    
272
  return b;
273
}
274

    
275
static void *
276
fib_route_ip4(struct fib *f, const net_addr *n0)
277
{
278
  net_addr net;
279
  net_addr_ip4 *n = (net_addr_ip4 *) &net;
280
  void *b;
281

    
282
  net_copy(&net, n0);
283
  while (!(b = fib_find(f, &net)) && (n->pxlen > 0))
284
  {
285
    n->pxlen--;
286
    ip4_clrbit(&n->prefix, n->pxlen);
287
  }
288

    
289
  return b;
290
}
291

    
292
static void *
293
fib_route_ip6(struct fib *f, const net_addr *n0)
294
{
295
  net_addr net;
296
  net_addr_ip6 *n = (net_addr_ip6 *) &net;
297
  void *b;
298

    
299
  net_copy(&net, n0);
300
  while (!(b = fib_find(f, &net)) && (n->pxlen > 0))
301
  {
302
    n->pxlen--;
303
    ip6_clrbit(&n->prefix, n->pxlen);
304
  }
305

    
306
  return b;
307
}
308

    
309
/**
310
 * fib_route - CIDR routing lookup
311
 * @f: FIB to search in
312
 * @n: network address
313
 *
314
 * Search for a FIB node with longest prefix matching the given
315
 * network, that is a node which a CIDR router would use for routing
316
 * that network.
317
 */
318
void *
319
fib_route(struct fib *f, const net_addr *n)
320
{
321
  ASSERT(f->addr_type == n->type);
322

    
323
  switch (n->type)
324
  {
325
  case NET_IP4:
326
  case NET_VPN4:
327
    return fib_route_ip4(f, n);
328

    
329
  case NET_IP6:
330
  case NET_VPN6:
331
    return fib_route_ip6(f, n);
332

    
333
  default:
334
    return NULL;
335
  }
336
}
337

    
338
static inline void
339
fib_merge_readers(struct fib_iterator *i, struct fib_node *to)
340
{
341
  if (to)
342
    {
343
      struct fib_iterator *j = to->readers;
344
      if (!j)
345
        {
346
          /* Fast path */
347
          to->readers = i;
348
          i->prev = (struct fib_iterator *) to;
349
        }
350
      else
351
        {
352
          /* Really merging */
353
          while (j->next)
354
            j = j->next;
355
          j->next = i;
356
          i->prev = j;
357
        }
358
      while (i && i->node)
359
        {
360
          i->node = NULL;
361
          i = i->next;
362
        }
363
    }
364
  else                                        /* No more nodes */
365
    while (i)
366
      {
367
        i->prev = NULL;
368
        i = i->next;
369
      }
370
}
371

    
372
/**
373
 * fib_delete - delete a FIB node
374
 * @f: FIB to delete from
375
 * @E: entry to delete
376
 *
377
 * This function removes the given entry from the FIB,
378
 * taking care of all the asynchronous readers by shifting
379
 * them to the next node in the canonical reading order.
380
 */
381
void
382
fib_delete(struct fib *f, void *E)
383
{
384
  struct fib_node *e = fib_user_to_node(f, E);
385
  uint h = fib_hash(f, e->addr);
386
  struct fib_node **ee = f->hash_table + h;
387
  struct fib_iterator *it;
388

    
389
  while (*ee)
390
    {
391
      if (*ee == e)
392
        {
393
          *ee = e->next;
394
          if (it = e->readers)
395
            {
396
              struct fib_node *l = e->next;
397
              while (!l)
398
                {
399
                  h++;
400
                  if (h >= f->hash_size)
401
                    break;
402
                  else
403
                    l = f->hash_table[h];
404
                }
405
              fib_merge_readers(it, l);
406
            }
407
          sl_free(f->fib_slab, e);
408
          if (f->entries-- < f->entries_min)
409
            fib_rehash(f, -HASH_LO_STEP);
410
          return;
411
        }
412
      ee = &((*ee)->next);
413
    }
414
  bug("fib_delete() called for invalid node");
415
}
416

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

    
431
void
432
fit_init(struct fib_iterator *i, struct fib *f)
433
{
434
  unsigned h;
435
  struct fib_node *n;
436

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

    
453
struct fib_node *
454
fit_get(struct fib *f, struct fib_iterator *i)
455
{
456
  struct fib_node *n;
457
  struct fib_iterator *j, *k;
458

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

    
481
void
482
fit_put(struct fib_iterator *i, struct fib_node *n)
483
{
484
  struct fib_iterator *j;
485

    
486
  i->node = n;
487
  if (j = n->readers)
488
    j->prev = i;
489
  i->next = j;
490
  n->readers = i;
491
  i->prev = (struct fib_iterator *) n;
492
}
493

    
494
void
495
fit_put_next(struct fib *f, struct fib_iterator *i, struct fib_node *n, uint hpos)
496
{
497
  if (n = n->next)
498
    goto found;
499

    
500
  while (++hpos < f->hash_size)
501
    if (n = f->hash_table[hpos])
502
      goto found;
503

    
504
  /* We are at the end */
505
  i->prev = i->next = NULL;
506
  i->node = NULL;
507
  return;
508

    
509
found:
510
  fit_put(i, n);
511
}
512

    
513
#ifdef DEBUGGING
514

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

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

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

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

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

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

    
584
#endif
585

    
586
#ifdef TEST
587

    
588
#include "lib/resource.h"
589

    
590
struct fib f;
591

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
676
  return 0;
677
}
678

    
679
#endif