Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-fib.c @ ce4aca09

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

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

    
75
static inline unsigned
76
fib_hash(struct fib *f, ip_addr *a)
77
{
78
  return ipa_hash(*a) >> f->hash_shift;
79
}
80

    
81
static void
82
fib_dummy_init(struct fib_node *dummy)
83
{
84
}
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, unsigned node_size, unsigned hash_order, fib_init_func init)
100
{
101
  if (!hash_order)
102
    hash_order = HASH_DEF_ORDER;
103
  f->fib_pool = p;
104
  f->fib_slab = sl_new(p, node_size);
105
  f->hash_order = hash_order;
106
  fib_ht_alloc(f);
107
  bzero(f->hash_table, f->hash_size * sizeof(struct fib_node *));
108
  f->entries = 0;
109
  f->entries_min = 0;
110
  f->init = init ? : fib_dummy_init;
111
}
112

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

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

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

    
156
/**
157
 * fib_find - search for FIB node by prefix
158
 * @f: FIB to search in
159
 * @a: pointer to IP address of the prefix
160
 * @len: prefix length
161
 *
162
 * Search for a FIB node corresponding to the given prefix, return
163
 * a pointer to it or %NULL if no such node exists.
164
 */
165
void *
166
fib_find(struct fib *f, ip_addr *a, int len)
167
{
168
  struct fib_node *e = f->hash_table[fib_hash(f, a)];
169

    
170
  while (e && (e->pxlen != len || !ipa_equal(*a, e->prefix)))
171
    e = e->next;
172
  return e;
173
}
174

    
175
/**
176
 * fib_get - find or create a FIB node
177
 * @f: FIB to work with
178
 * @a: pointer to IP address of the prefix
179
 * @len: prefix length
180
 *
181
 * Search for a FIB node corresponding to the given prefix and
182
 * return a pointer to it. If no such node exists, create it.
183
 */
184
void *
185
fib_get(struct fib *f, ip_addr *a, int len)
186
{
187
  unsigned int h = ipa_hash(*a);
188
  struct fib_node **ee = f->hash_table + (h >> f->hash_shift);
189
  struct fib_node *g, *e = *ee;
190

    
191
  while (e && (e->pxlen != len || !ipa_equal(*a, e->prefix)))
192
    e = e->next;
193
  if (e)
194
    return e;
195
#ifdef DEBUGGING
196
  if (len < 0 || len > BITS_PER_IP_ADDRESS || !ip_is_prefix(*a,len))
197
    bug("fib_get() called for invalid address");
198
#endif
199
  e = sl_alloc(f->fib_slab);
200
  e->prefix = *a;
201
  e->pxlen = len;
202
  while ((g = *ee) && ipa_hash(g->prefix) < h)
203
    ee = &g->next;
204
  e->next = *ee;
205
  *ee = e;
206
  e->readers = NULL;
207
  f->init(e);
208
  if (f->entries++ > f->entries_max)
209
    fib_rehash(f, HASH_HI_STEP);
210
  return e;
211
}
212

    
213
/**
214
 * fib_route - CIDR routing lookup
215
 * @f: FIB to search in
216
 * @a: pointer to IP address of the prefix
217
 * @len: prefix length
218
 *
219
 * Search for a FIB node with longest prefix matching the given
220
 * network, that is a node which a CIDR router would use for routing
221
 * that network.
222
 */
223
void *
224
fib_route(struct fib *f, ip_addr a, int len)
225
{
226
  ip_addr a0;
227
  void *t;
228

    
229
  while (len >= 0)
230
    {
231
      a0 = ipa_and(a, ipa_mkmask(len));
232
      t = fib_find(f, &a0, len);
233
      if (t)
234
        return t;
235
      len--;
236
    }
237
  return NULL;
238
}
239

    
240
static inline void
241
fib_merge_readers(struct fib_iterator *i, struct fib_node *to)
242
{
243
  if (to)
244
    {
245
      struct fib_iterator *j = to->readers;
246
      if (!j)
247
        {
248
          /* Fast path */
249
          to->readers = i;
250
          i->prev = (struct fib_iterator *) to;
251
        }
252
      else
253
        {
254
          /* Really merging */
255
          while (j->next)
256
            j = j->next;
257
          j->next = i;
258
          i->prev = j;
259
        }
260
      while (i && i->node)
261
        {
262
          i->node = NULL;
263
          i = i->next;
264
        }
265
    }
266
  else                                        /* No more nodes */
267
    while (i)
268
      {
269
        i->prev = NULL;
270
        i = i->next;
271
      }
272
}
273

    
274
/**
275
 * fib_delete - delete a FIB node
276
 * @f: FIB to delete from
277
 * @E: entry to delete
278
 *
279
 * This function removes the given entry from the FIB,
280
 * taking care of all the asynchronous readers by shifting
281
 * them to the next node in the canonical reading order.
282
 */
283
void
284
fib_delete(struct fib *f, void *E)
285
{
286
  struct fib_node *e = E;
287
  unsigned int h = fib_hash(f, &e->prefix);
288
  struct fib_node **ee = f->hash_table + h;
289
  struct fib_iterator *it;
290

    
291
  while (*ee)
292
    {
293
      if (*ee == e)
294
        {
295
          *ee = e->next;
296
          if (it = e->readers)
297
            {
298
              struct fib_node *l = e->next;
299
              while (!l)
300
                {
301
                  h++;
302
                  if (h >= f->hash_size)
303
                    break;
304
                  else
305
                    l = f->hash_table[h];
306
                }
307
              fib_merge_readers(it, l);
308
            }
309
          sl_free(f->fib_slab, e);
310
          if (f->entries-- < f->entries_min)
311
            fib_rehash(f, -HASH_LO_STEP);
312
          return;
313
        }
314
      ee = &((*ee)->next);
315
    }
316
  bug("fib_delete() called for invalid node");
317
}
318

    
319
/**
320
 * fib_free - delete a FIB
321
 * @f: FIB to be deleted
322
 *
323
 * This function deletes a FIB -- it frees all memory associated
324
 * with it and all its entries.
325
 */
326
void
327
fib_free(struct fib *f)
328
{
329
  fib_ht_free(f->hash_table);
330
  rfree(f->fib_slab);
331
}
332

    
333
void
334
fit_init(struct fib_iterator *i, struct fib *f)
335
{
336
  unsigned h;
337
  struct fib_node *n;
338

    
339
  i->efef = 0xff;
340
  for(h=0; h<f->hash_size; h++)
341
    if (n = f->hash_table[h])
342
      {
343
        i->prev = (struct fib_iterator *) n;
344
        if (i->next = n->readers)
345
          i->next->prev = i;
346
        n->readers = i;
347
        i->node = n;
348
        return;
349
      }
350
  /* The fib is empty, nothing to do */
351
  i->prev = i->next = NULL;
352
  i->node = NULL;
353
}
354

    
355
struct fib_node *
356
fit_get(struct fib *f, struct fib_iterator *i)
357
{
358
  struct fib_node *n;
359
  struct fib_iterator *j, *k;
360

    
361
  if (!i->prev)
362
    {
363
      /* We are at the end */
364
      i->hash = ~0 - 1;
365
      return NULL;
366
    }
367
  if (!(n = i->node))
368
    {
369
      /* No node info available, we are a victim of merging. Try harder. */
370
      j = i;
371
      while (j->efef == 0xff)
372
        j = j->prev;
373
      n = (struct fib_node *) j;
374
    }
375
  j = i->prev;
376
  if (k = i->next)
377
    k->prev = j;
378
  j->next = k;
379
  i->hash = fib_hash(f, &n->prefix);
380
  return n;
381
}
382

    
383
void
384
fit_put(struct fib_iterator *i, struct fib_node *n)
385
{
386
  struct fib_iterator *j;
387

    
388
  i->node = n;
389
  if (j = n->readers)
390
    j->prev = i;
391
  i->next = j;
392
  n->readers = i;
393
  i->prev = (struct fib_iterator *) n;
394
}
395

    
396
#ifdef DEBUGGING
397

    
398
/**
399
 * fib_check - audit a FIB
400
 * @f: FIB to be checked
401
 *
402
 * This debugging function audits a FIB by checking its internal consistency.
403
 * Use when you suspect somebody from corrupting innocent data structures.
404
 */
405
void
406
fib_check(struct fib *f)
407
{
408
  unsigned int i, ec, lo, nulls;
409

    
410
  ec = 0;
411
  for(i=0; i<f->hash_size; i++)
412
    {
413
      struct fib_node *n;
414
      lo = 0;
415
      for(n=f->hash_table[i]; n; n=n->next)
416
        {
417
          struct fib_iterator *j, *j0;
418
          unsigned int h0 = ipa_hash(n->prefix);
419
          if (h0 < lo)
420
            bug("fib_check: discord in hash chains");
421
          lo = h0;
422
          if ((h0 >> f->hash_shift) != i)
423
            bug("fib_check: mishashed %x->%x (order %d)", h0, i, f->hash_order);
424
          j0 = (struct fib_iterator *) n;
425
          nulls = 0;
426
          for(j=n->readers; j; j=j->next)
427
            {
428
              if (j->prev != j0)
429
                bug("fib_check: iterator->prev mismatch");
430
              j0 = j;
431
              if (!j->node)
432
                nulls++;
433
              else if (nulls)
434
                bug("fib_check: iterator nullified");
435
              else if (j->node != n)
436
                bug("fib_check: iterator->node mismatch");
437
            }
438
          ec++;
439
        }
440
    }
441
  if (ec != f->entries)
442
    bug("fib_check: invalid entry count (%d != %d)", ec, f->entries);
443
}
444

    
445
#endif
446

    
447
#ifdef TEST
448

    
449
#include "lib/resource.h"
450

    
451
struct fib f;
452

    
453
void dump(char *m)
454
{
455
  unsigned int i;
456

    
457
  debug("%s ... order=%d, size=%d, entries=%d\n", m, f.hash_order, f.hash_size, f.hash_size);
458
  for(i=0; i<f.hash_size; i++)
459
    {
460
      struct fib_node *n;
461
      struct fib_iterator *j;
462
      for(n=f.hash_table[i]; n; n=n->next)
463
        {
464
          debug("%04x %04x %p %I/%2d", i, ipa_hash(n->prefix), n, n->prefix, n->pxlen);
465
          for(j=n->readers; j; j=j->next)
466
            debug(" %p[%p]", j, j->node);
467
          debug("\n");
468
        }
469
    }
470
  fib_check(&f);
471
  debug("-----\n");
472
}
473

    
474
void init(struct fib_node *n)
475
{
476
}
477

    
478
int main(void)
479
{
480
  struct fib_node *n;
481
  struct fib_iterator i, j;
482
  ip_addr a;
483
  int c;
484

    
485
  log_init_debug(NULL);
486
  resource_init();
487
  fib_init(&f, &root_pool, sizeof(struct fib_node), 4, init);
488
  dump("init");
489

    
490
  a = ipa_from_u32(0x01020304); n = fib_get(&f, &a, 32);
491
  a = ipa_from_u32(0x02030405); n = fib_get(&f, &a, 32);
492
  a = ipa_from_u32(0x03040506); n = fib_get(&f, &a, 32);
493
  a = ipa_from_u32(0x00000000); n = fib_get(&f, &a, 32);
494
  a = ipa_from_u32(0x00000c01); n = fib_get(&f, &a, 32);
495
  a = ipa_from_u32(0xffffffff); n = fib_get(&f, &a, 32);
496
  dump("fill");
497

    
498
  fit_init(&i, &f);
499
  dump("iter init");
500

    
501
  fib_rehash(&f, 1);
502
  dump("rehash up");
503

    
504
  fib_rehash(&f, -1);
505
  dump("rehash down");
506

    
507
next:
508
  c = 0;
509
  FIB_ITERATE_START(&f, &i, z)
510
    {
511
      if (c)
512
        {
513
          FIB_ITERATE_PUT(&i, z);
514
          dump("iter");
515
          goto next;
516
        }
517
      c = 1;
518
      debug("got %p\n", z);
519
    }
520
  FIB_ITERATE_END;
521
  dump("iter end");
522

    
523
  fit_init(&i, &f);
524
  fit_init(&j, &f);
525
  dump("iter init 2");
526

    
527
  n = fit_get(&f, &i);
528
  dump("iter step 2");
529

    
530
  fit_put(&i, n->next);
531
  dump("iter step 3");
532

    
533
  a = ipa_from_u32(0xffffffff); n = fib_get(&f, &a, 32);
534
  fib_delete(&f, n);
535
  dump("iter step 3");
536

    
537
  return 0;
538
}
539

    
540
#endif