Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-fib.c @ 04632fd7

History | View | Annotate | Download (14.5 KB)

1 62aa008a Martin Mares
/*
2
 *        BIRD -- Forwarding Information Base -- Data Structures
3
 *
4 6998bb9e Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 62aa008a Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 ce4aca09 Martin Mares
/**
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 58f7d004 Martin Mares
 * The lists of nodes in each bucket are sorted according to the primary hash
25 ce4aca09 Martin Mares
 * 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 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
38 62aa008a Martin Mares
39
#include "nest/bird.h"
40
#include "nest/route.h"
41 221135d6 Martin Mares
#include "lib/string.h"
42 62aa008a Martin Mares
43 3ab001b9 Martin Mares
#define HASH_DEF_ORDER 10
44
#define HASH_HI_MARK *4
45
#define HASH_HI_STEP 2
46 04632fd7 Ondrej Zajicek (work)
#define HASH_HI_MAX 16
47 3ab001b9 Martin Mares
#define HASH_LO_MARK /5
48
#define HASH_LO_STEP 2
49
#define HASH_LO_MIN 10
50 62aa008a Martin Mares
51 fe9f1a6d Ondrej Zajicek (work)
52 62aa008a Martin Mares
static void
53
fib_ht_alloc(struct fib *f)
54
{
55 3ab001b9 Martin Mares
  f->hash_size = 1 << f->hash_order;
56 23c212e7 Ondrej Zajicek (work)
  f->hash_shift = 32 - f->hash_order;
57 3ab001b9 Martin Mares
  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 62aa008a Martin Mares
    f->entries_min = 0;
63 3ab001b9 Martin Mares
  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 62aa008a Martin Mares
  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 fe9f1a6d Ondrej Zajicek (work)
static u32
78 0f7d5b1a Ondrej Zajicek (work)
fib_hash(struct fib *f, const net_addr *a);
79 ce45fc12 Pavel Machek
80 ce4aca09 Martin Mares
/**
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 62aa008a Martin Mares
void
93 fe9f1a6d Ondrej Zajicek (work)
fib_init(struct fib *f, pool *p, uint addr_type, uint node_size, uint node_offset, uint hash_order, fib_init_fn init)
94 62aa008a Martin Mares
{
95 fe9f1a6d Ondrej Zajicek (work)
  uint addr_length = net_addr_length[addr_type];
96
97 3ab001b9 Martin Mares
  if (!hash_order)
98
    hash_order = HASH_DEF_ORDER;
99 62aa008a Martin Mares
  f->fib_pool = p;
100 fe9f1a6d Ondrej Zajicek (work)
  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 3ab001b9 Martin Mares
  f->hash_order = hash_order;
105 62aa008a Martin Mares
  fib_ht_alloc(f);
106 3ab001b9 Martin Mares
  bzero(f->hash_table, f->hash_size * sizeof(struct fib_node *));
107 62aa008a Martin Mares
  f->entries = 0;
108
  f->entries_min = 0;
109 fe9f1a6d Ondrej Zajicek (work)
  f->init = init;
110 62aa008a Martin Mares
}
111
112
static void
113 3ab001b9 Martin Mares
fib_rehash(struct fib *f, int step)
114 62aa008a Martin Mares
{
115 3ab001b9 Martin Mares
  unsigned old, new, oldn, newn, ni, nh;
116 62aa008a Martin Mares
  struct fib_node **n, *e, *x, **t, **m, **h;
117
118 3ab001b9 Martin Mares
  old = f->hash_order;
119
  oldn = f->hash_size;
120
  new = old + step;
121 62aa008a Martin Mares
  m = h = f->hash_table;
122 3ab001b9 Martin Mares
  DBG("Re-hashing FIB from order %d to %d\n", old, new);
123
  f->hash_order = new;
124 62aa008a Martin Mares
  fib_ht_alloc(f);
125 3ab001b9 Martin Mares
  t = n = f->hash_table;
126
  newn = f->hash_size;
127
  ni = 0;
128
129 6998bb9e Martin Mares
  while (oldn--)
130 62aa008a Martin Mares
    {
131
      x = *h++;
132
      while (e = x)
133
        {
134
          x = e->next;
135 fe9f1a6d Ondrej Zajicek (work)
          nh = fib_hash(f, e->addr);
136 3ab001b9 Martin Mares
          while (nh > ni)
137
            {
138
              *t = NULL;
139
              ni++;
140
              t = ++n;
141
            }
142 62aa008a Martin Mares
          *t = e;
143 3ab001b9 Martin Mares
          t = &e->next;
144 62aa008a Martin Mares
        }
145
    }
146 3ab001b9 Martin Mares
  while (ni < newn)
147
    {
148
      *t = NULL;
149
      ni++;
150
      t = ++n;
151
    }
152 62aa008a Martin Mares
  fib_ht_free(m);
153
}
154
155 0f7d5b1a Ondrej Zajicek (work)
#define CAST(t) (const net_addr_##t *)
156
#define CAST2(t) (net_addr_##t *)
157 fe9f1a6d Ondrej Zajicek (work)
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 600998fc Ondrej Zajicek (work)
    fib_node_to_user(f, e);                                                \
166 fe9f1a6d Ondrej Zajicek (work)
  })
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 0f7d5b1a Ondrej Zajicek (work)
  net_copy_##t(CAST2(t) e->addr, CAST(t) a);                                \
178 fe9f1a6d Ondrej Zajicek (work)
  e->next = *ee;                                                        \
179
  *ee = e;                                                                \
180
  })
181
182
183
static u32
184 0f7d5b1a Ondrej Zajicek (work)
fib_hash(struct fib *f, const net_addr *a)
185 fe9f1a6d Ondrej Zajicek (work)
{
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 0f7d5b1a Ondrej Zajicek (work)
/**
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 fe9f1a6d Ondrej Zajicek (work)
void *
205 0f7d5b1a Ondrej Zajicek (work)
fib_find(struct fib *f, const net_addr *a)
206 fe9f1a6d Ondrej Zajicek (work)
{
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 0f7d5b1a Ondrej Zajicek (work)
fib_insert(struct fib *f, const net_addr *a, struct fib_node *e)
221 fe9f1a6d Ondrej Zajicek (work)
{
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 ce4aca09 Martin Mares
/**
234
 * fib_get - find or create a FIB node
235
 * @f: FIB to work with
236 0f7d5b1a Ondrej Zajicek (work)
 * @n: network address
237 ce4aca09 Martin Mares
 *
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 62aa008a Martin Mares
void *
242 0f7d5b1a Ondrej Zajicek (work)
fib_get(struct fib *f, const net_addr *a)
243 62aa008a Martin Mares
{
244 23c212e7 Ondrej Zajicek (work)
  void *b = fib_find(f, a);
245 fe9f1a6d Ondrej Zajicek (work)
  if (b)
246
    return b;
247 d82fc18d Ondrej Zajicek
248 fe9f1a6d Ondrej Zajicek (work)
  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 d82fc18d Ondrej Zajicek
253 23c212e7 Ondrej Zajicek (work)
  struct fib_node *e = fib_user_to_node(f, b);
254 fe9f1a6d Ondrej Zajicek (work)
  e->readers = NULL;
255
  e->flags = 0;
256
  e->uid = 0;
257
  fib_insert(f, a, e);
258 d82fc18d Ondrej Zajicek
259 fe9f1a6d Ondrej Zajicek (work)
  memset(b, 0, f->node_offset);
260
  if (f->init)
261
    f->init(b);
262 d82fc18d Ondrej Zajicek
263 62aa008a Martin Mares
  if (f->entries++ > f->entries_max)
264 3ab001b9 Martin Mares
    fib_rehash(f, HASH_HI_STEP);
265 d82fc18d Ondrej Zajicek
266 fe9f1a6d Ondrej Zajicek (work)
  return b;
267 62aa008a Martin Mares
}
268
269 04632fd7 Ondrej Zajicek (work)
static inline void *
270
fib_route_ip4(struct fib *f, net_addr_ip4 *n)
271 0f7d5b1a Ondrej Zajicek (work)
{
272 04632fd7 Ondrej Zajicek (work)
  void *r;
273 0f7d5b1a Ondrej Zajicek (work)
274 04632fd7 Ondrej Zajicek (work)
  while (!(r = fib_find(f, (net_addr *) n)) && (n->pxlen > 0))
275 0f7d5b1a Ondrej Zajicek (work)
  {
276
    n->pxlen--;
277
    ip4_clrbit(&n->prefix, n->pxlen);
278
  }
279
280 04632fd7 Ondrej Zajicek (work)
  return r;
281 0f7d5b1a Ondrej Zajicek (work)
}
282
283 04632fd7 Ondrej Zajicek (work)
static inline void *
284
fib_route_ip6(struct fib *f, net_addr_ip6 *n)
285 0f7d5b1a Ondrej Zajicek (work)
{
286 04632fd7 Ondrej Zajicek (work)
  void *r;
287 0f7d5b1a Ondrej Zajicek (work)
288 04632fd7 Ondrej Zajicek (work)
  while (!(r = fib_find(f, (net_addr *) n)) && (n->pxlen > 0))
289 0f7d5b1a Ondrej Zajicek (work)
  {
290
    n->pxlen--;
291
    ip6_clrbit(&n->prefix, n->pxlen);
292
  }
293
294 04632fd7 Ondrej Zajicek (work)
  return r;
295 0f7d5b1a Ondrej Zajicek (work)
}
296
297 ce4aca09 Martin Mares
/**
298
 * fib_route - CIDR routing lookup
299
 * @f: FIB to search in
300 0f7d5b1a Ondrej Zajicek (work)
 * @n: network address
301 ce4aca09 Martin Mares
 *
302
 * Search for a FIB node with longest prefix matching the given
303
 * network, that is a node which a CIDR router would use for routing
304
 * that network.
305
 */
306 56d6c530 Martin Mares
void *
307 0f7d5b1a Ondrej Zajicek (work)
fib_route(struct fib *f, const net_addr *n)
308 56d6c530 Martin Mares
{
309 0f7d5b1a Ondrej Zajicek (work)
  ASSERT(f->addr_type == n->type);
310 56d6c530 Martin Mares
311 04632fd7 Ondrej Zajicek (work)
  net_addr *n0 = alloca(n->length);
312
  net_copy(n0, n);
313
314 0f7d5b1a Ondrej Zajicek (work)
  switch (n->type)
315
  {
316
  case NET_IP4:
317
  case NET_VPN4:
318 04632fd7 Ondrej Zajicek (work)
    return fib_route_ip4(f, (net_addr_ip4 *) n0);
319 0f7d5b1a Ondrej Zajicek (work)
320
  case NET_IP6:
321
  case NET_VPN6:
322 04632fd7 Ondrej Zajicek (work)
    return fib_route_ip6(f, (net_addr_ip6 *) n0);
323 0f7d5b1a Ondrej Zajicek (work)
324
  default:
325
    return NULL;
326
  }
327 56d6c530 Martin Mares
}
328
329 04632fd7 Ondrej Zajicek (work)
330 3ab001b9 Martin Mares
static inline void
331
fib_merge_readers(struct fib_iterator *i, struct fib_node *to)
332
{
333
  if (to)
334
    {
335
      struct fib_iterator *j = to->readers;
336
      if (!j)
337
        {
338
          /* Fast path */
339
          to->readers = i;
340
          i->prev = (struct fib_iterator *) to;
341
        }
342 8abbde02 Martin Mares
      else
343
        {
344
          /* Really merging */
345
          while (j->next)
346
            j = j->next;
347
          j->next = i;
348
          i->prev = j;
349
        }
350
      while (i && i->node)
351
        {
352
          i->node = NULL;
353
          i = i->next;
354
        }
355 3ab001b9 Martin Mares
    }
356
  else                                        /* No more nodes */
357
    while (i)
358
      {
359
        i->prev = NULL;
360
        i = i->next;
361
      }
362
}
363
364 ce4aca09 Martin Mares
/**
365
 * fib_delete - delete a FIB node
366
 * @f: FIB to delete from
367
 * @E: entry to delete
368
 *
369
 * This function removes the given entry from the FIB,
370
 * taking care of all the asynchronous readers by shifting
371
 * them to the next node in the canonical reading order.
372
 */
373 62aa008a Martin Mares
void
374
fib_delete(struct fib *f, void *E)
375
{
376 fe9f1a6d Ondrej Zajicek (work)
  struct fib_node *e = fib_user_to_node(f, E);
377
  uint h = fib_hash(f, e->addr);
378 3ab001b9 Martin Mares
  struct fib_node **ee = f->hash_table + h;
379
  struct fib_iterator *it;
380 62aa008a Martin Mares
381
  while (*ee)
382
    {
383
      if (*ee == e)
384
        {
385
          *ee = e->next;
386 3ab001b9 Martin Mares
          if (it = e->readers)
387
            {
388
              struct fib_node *l = e->next;
389
              while (!l)
390
                {
391
                  h++;
392
                  if (h >= f->hash_size)
393
                    break;
394
                  else
395
                    l = f->hash_table[h];
396
                }
397
              fib_merge_readers(it, l);
398
            }
399 04632fd7 Ondrej Zajicek (work)
400
          if (f->fib_slab)
401
            sl_free(f->fib_slab, E);
402
          else
403
            mb_free(E);
404
405 62aa008a Martin Mares
          if (f->entries-- < f->entries_min)
406 3ab001b9 Martin Mares
            fib_rehash(f, -HASH_LO_STEP);
407 62aa008a Martin Mares
          return;
408
        }
409
      ee = &((*ee)->next);
410
    }
411 08c69a77 Martin Mares
  bug("fib_delete() called for invalid node");
412 62aa008a Martin Mares
}
413
414 ce4aca09 Martin Mares
/**
415
 * fib_free - delete a FIB
416
 * @f: FIB to be deleted
417
 *
418
 * This function deletes a FIB -- it frees all memory associated
419
 * with it and all its entries.
420
 */
421 62aa008a Martin Mares
void
422
fib_free(struct fib *f)
423
{
424
  fib_ht_free(f->hash_table);
425
  rfree(f->fib_slab);
426
}
427 3ab001b9 Martin Mares
428
void
429
fit_init(struct fib_iterator *i, struct fib *f)
430
{
431
  unsigned h;
432
  struct fib_node *n;
433
434
  i->efef = 0xff;
435
  for(h=0; h<f->hash_size; h++)
436
    if (n = f->hash_table[h])
437
      {
438
        i->prev = (struct fib_iterator *) n;
439
        if (i->next = n->readers)
440
          i->next->prev = i;
441
        n->readers = i;
442
        i->node = n;
443
        return;
444
      }
445
  /* The fib is empty, nothing to do */
446
  i->prev = i->next = NULL;
447
  i->node = NULL;
448
}
449
450
struct fib_node *
451
fit_get(struct fib *f, struct fib_iterator *i)
452
{
453
  struct fib_node *n;
454
  struct fib_iterator *j, *k;
455
456
  if (!i->prev)
457
    {
458
      /* We are at the end */
459 8abbde02 Martin Mares
      i->hash = ~0 - 1;
460 3ab001b9 Martin Mares
      return NULL;
461
    }
462
  if (!(n = i->node))
463
    {
464
      /* No node info available, we are a victim of merging. Try harder. */
465
      j = i;
466
      while (j->efef == 0xff)
467
        j = j->prev;
468
      n = (struct fib_node *) j;
469
    }
470
  j = i->prev;
471
  if (k = i->next)
472
    k->prev = j;
473
  j->next = k;
474 fe9f1a6d Ondrej Zajicek (work)
  i->hash = fib_hash(f, n->addr);
475 3ab001b9 Martin Mares
  return n;
476
}
477
478
void
479
fit_put(struct fib_iterator *i, struct fib_node *n)
480
{
481
  struct fib_iterator *j;
482
483
  i->node = n;
484
  if (j = n->readers)
485
    j->prev = i;
486
  i->next = j;
487
  n->readers = i;
488
  i->prev = (struct fib_iterator *) n;
489
}
490
491 8465dccb Ondrej Zajicek (work)
void
492
fit_put_next(struct fib *f, struct fib_iterator *i, struct fib_node *n, uint hpos)
493
{
494
  if (n = n->next)
495
    goto found;
496
497
  while (++hpos < f->hash_size)
498
    if (n = f->hash_table[hpos])
499
      goto found;
500
501
  /* We are at the end */
502
  i->prev = i->next = NULL;
503
  i->node = NULL;
504
  return;
505
506
found:
507
  fit_put(i, n);
508
}
509
510 3ab001b9 Martin Mares
#ifdef DEBUGGING
511
512 ce4aca09 Martin Mares
/**
513
 * fib_check - audit a FIB
514
 * @f: FIB to be checked
515
 *
516
 * This debugging function audits a FIB by checking its internal consistency.
517 58f7d004 Martin Mares
 * Use when you suspect somebody of corrupting innocent data structures.
518 ce4aca09 Martin Mares
 */
519 3ab001b9 Martin Mares
void
520
fib_check(struct fib *f)
521
{
522 23c212e7 Ondrej Zajicek (work)
#if 0
523 ae80a2de Pavel Tvrdík
  uint i, ec, lo, nulls;
524 3ab001b9 Martin Mares

525
  ec = 0;
526
  for(i=0; i<f->hash_size; i++)
527
    {
528
      struct fib_node *n;
529
      lo = 0;
530
      for(n=f->hash_table[i]; n; n=n->next)
531
        {
532
          struct fib_iterator *j, *j0;
533 ae80a2de Pavel Tvrdík
          uint h0 = ipa_hash(n->prefix);
534 3ab001b9 Martin Mares
          if (h0 < lo)
535 08c69a77 Martin Mares
            bug("fib_check: discord in hash chains");
536 3ab001b9 Martin Mares
          lo = h0;
537
          if ((h0 >> f->hash_shift) != i)
538 08c69a77 Martin Mares
            bug("fib_check: mishashed %x->%x (order %d)", h0, i, f->hash_order);
539 3ab001b9 Martin Mares
          j0 = (struct fib_iterator *) n;
540
          nulls = 0;
541
          for(j=n->readers; j; j=j->next)
542
            {
543
              if (j->prev != j0)
544 08c69a77 Martin Mares
                bug("fib_check: iterator->prev mismatch");
545 3ab001b9 Martin Mares
              j0 = j;
546
              if (!j->node)
547
                nulls++;
548
              else if (nulls)
549 08c69a77 Martin Mares
                bug("fib_check: iterator nullified");
550 3ab001b9 Martin Mares
              else if (j->node != n)
551 08c69a77 Martin Mares
                bug("fib_check: iterator->node mismatch");
552 3ab001b9 Martin Mares
            }
553
          ec++;
554
        }
555
    }
556
  if (ec != f->entries)
557 08c69a77 Martin Mares
    bug("fib_check: invalid entry count (%d != %d)", ec, f->entries);
558 23c212e7 Ondrej Zajicek (work)
#endif
559
  return;
560 3ab001b9 Martin Mares
}
561
562 fe9f1a6d Ondrej Zajicek (work)
/*
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 3ab001b9 Martin Mares
#endif
585
586
#ifdef TEST
587
588
#include "lib/resource.h"
589
590
struct fib f;
591
592
void dump(char *m)
593
{
594 ae80a2de Pavel Tvrdík
  uint i;
595 3ab001b9 Martin Mares
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 fe9f1a6d Ondrej Zajicek (work)
          debug("%04x %08x %p %N", i, ipa_hash(n->prefix), n, n->addr);
604 3ab001b9 Martin Mares
          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 6264aad1 Pavel Tvrdík
  FIB_ITERATE_END(z);
660 3ab001b9 Martin Mares
  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