rt-fib.c 14.5 KB
Newer Older
1 2 3
/*
 *	BIRD -- Forwarding Information Base -- Data Structures
 *
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.
 */

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

37
#undef LOCAL_DEBUG
38 39 40

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

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

51

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

static inline void
fib_ht_free(struct fib_node **h)
{
  mb_free(h);
}


77
static u32
78
fib_hash(struct fib *f, const net_addr *a);
Pavel Machek's avatar
Pavel Machek committed
79

Martin Mareš's avatar
Martin Mareš committed
80 81 82 83 84 85 86 87 88 89 90 91
/**
 * fib_init - initialize a new FIB
 * @f: the FIB to be initialized (the structure itself being allocated by the caller)
 * @p: pool to allocate the nodes in
 * @node_size: node size to be used (each node consists of a standard header &fib_node
 * followed by user data)
 * @hash_order: initial hash order (a binary logarithm of hash table size), 0 to use default order
 * (recommended)
 * @init: pointer a function to be called to initialize a newly created node
 *
 * This function initializes a newly allocated FIB and prepares it for use.
 */
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 96
  uint addr_length = net_addr_length[addr_type];

97 98
  if (!hash_order)
    hash_order = HASH_DEF_ORDER;
99
  f->fib_pool = p;
100 101 102 103
  f->fib_slab = addr_length ? sl_new(p, node_size + addr_length) : NULL;
  f->addr_type = addr_type;
  f->node_size = node_size;
  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 108
  f->entries = 0;
  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 117
  struct fib_node **n, *e, *x, **t, **m, **h;

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

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

155 156
#define CAST(t) (const net_addr_##t *)
#define CAST2(t) (net_addr_##t *)
157 158 159 160 161 162 163 164

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

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

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


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

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

  switch (f->addr_type)
  {
  case NET_IP4: return FIB_FIND(f, a, ip4);
  case NET_IP6: return FIB_FIND(f, a, ip6);
  case NET_VPN4: return FIB_FIND(f, a, vpn4);
  case NET_VPN6: return FIB_FIND(f, a, vpn6);
  default: bug("invalid type");
  }
}

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


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

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

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

258 259 260
  memset(b, 0, f->node_offset);
  if (f->init)
    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 269
static inline void *
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 276 277 278
  {
    n->pxlen--;
    ip4_clrbit(&n->prefix, n->pxlen);
  }

279
  return r;
280 281
}

282 283
static inline void *
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 290 291 292
  {
    n->pxlen--;
    ip6_clrbit(&n->prefix, n->pxlen);
  }

293
  return r;
294 295
}

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

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

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

  case NET_IP6:
  case NET_VPN6:
321
    return fib_route_ip6(f, (net_addr_ip6 *) n0);
322 323 324 325

  default:
    return NULL;
  }
326 327
}

328

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

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

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

	  if (f->fib_slab)
	    sl_free(f->fib_slab, E);
	  else
	    mb_free(E);

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

Martin Mareš's avatar
Martin Mareš committed
413 414 415 416 417 418 419
/**
 * fib_free - delete a FIB
 * @f: FIB to be deleted
 *
 * This function deletes a FIB -- it frees all memory associated
 * with it and all its entries.
 */
420 421 422 423 424 425
void
fib_free(struct fib *f)
{
  fib_ht_free(f->hash_table);
  rfree(f->fib_slab);
}
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

void
fit_init(struct fib_iterator *i, struct fib *f)
{
  unsigned h;
  struct fib_node *n;

  i->efef = 0xff;
  for(h=0; h<f->hash_size; h++)
    if (n = f->hash_table[h])
      {
	i->prev = (struct fib_iterator *) n;
	if (i->next = n->readers)
	  i->next->prev = i;
	n->readers = i;
	i->node = n;
	return;
      }
  /* The fib is empty, nothing to do */
  i->prev = i->next = NULL;
  i->node = NULL;
}

struct fib_node *
fit_get(struct fib *f, struct fib_iterator *i)
{
  struct fib_node *n;
  struct fib_iterator *j, *k;

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

void
fit_put(struct fib_iterator *i, struct fib_node *n)
{
  struct fib_iterator *j;

  i->node = n;
  if (j = n->readers)
    j->prev = i;
  i->next = j;
  n->readers = i;
  i->prev = (struct fib_iterator *) n;
}

Ondřej Zajíček's avatar
Ondřej Zajíček committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
void
fit_put_next(struct fib *f, struct fib_iterator *i, struct fib_node *n, uint hpos)
{
  if (n = n->next)
    goto found;

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

  /* We are at the end */
  i->prev = i->next = NULL;
  i->node = NULL;
  return;

found:
  fit_put(i, n);
}

509 510
#ifdef DEBUGGING

Martin Mareš's avatar
Martin Mareš committed
511 512 513 514 515
/**
 * fib_check - audit a FIB
 * @f: FIB to be checked
 *
 * This debugging function audits a FIB by checking its internal consistency.
Martin Mareš's avatar
Martin Mareš committed
516
 * Use when you suspect somebody of corrupting innocent data structures.
Martin Mareš's avatar
Martin Mareš committed
517
 */
518 519 520
void
fib_check(struct fib *f)
{
521
#if 0
Pavel Tvrdík's avatar
Pavel Tvrdík committed
522
  uint i, ec, lo, nulls;
523 524 525 526 527 528 529 530 531

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

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/*
int
fib_histogram(struct fib *f)
{
  log(L_WARN "Histogram dump start %d %d", f->hash_size, f->entries);

  int i, j;
  struct fib_node *e;

  for (i = 0; i < f->hash_size; i++)
    {
      j = 0;
      for (e = f->hash_table[i]; e != NULL; e = e->next)
	j++;
      if (j > 0)
        log(L_WARN "Histogram line %d: %d", i, j);
    }

  log(L_WARN "Histogram dump end");
}
*/

583 584 585 586 587 588 589 590 591 592
#endif

#ifdef TEST

#include "lib/resource.h"

struct fib f;

void dump(char *m)
{
Pavel Tvrdík's avatar
Pavel Tvrdík committed
593
  uint i;
594 595 596 597 598 599 600 601

  debug("%s ... order=%d, size=%d, entries=%d\n", m, f.hash_order, f.hash_size, f.hash_size);
  for(i=0; i<f.hash_size; i++)
    {
      struct fib_node *n;
      struct fib_iterator *j;
      for(n=f.hash_table[i]; n; n=n->next)
	{
602
	  debug("%04x %08x %p %N", i, ipa_hash(n->prefix), n, n->addr);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	  for(j=n->readers; j; j=j->next)
	    debug(" %p[%p]", j, j->node);
	  debug("\n");
	}
    }
  fib_check(&f);
  debug("-----\n");
}

void init(struct fib_node *n)
{
}

int main(void)
{
  struct fib_node *n;
  struct fib_iterator i, j;
  ip_addr a;
  int c;

  log_init_debug(NULL);
  resource_init();
  fib_init(&f, &root_pool, sizeof(struct fib_node), 4, init);
  dump("init");

  a = ipa_from_u32(0x01020304); n = fib_get(&f, &a, 32);
  a = ipa_from_u32(0x02030405); n = fib_get(&f, &a, 32);
  a = ipa_from_u32(0x03040506); n = fib_get(&f, &a, 32);
  a = ipa_from_u32(0x00000000); n = fib_get(&f, &a, 32);
  a = ipa_from_u32(0x00000c01); n = fib_get(&f, &a, 32);
  a = ipa_from_u32(0xffffffff); n = fib_get(&f, &a, 32);
  dump("fill");

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

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

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

next:
  c = 0;
  FIB_ITERATE_START(&f, &i, z)
    {
      if (c)
	{
	  FIB_ITERATE_PUT(&i, z);
	  dump("iter");
	  goto next;
	}
      c = 1;
      debug("got %p\n", z);
    }
Pavel Tvrdík's avatar
Pavel Tvrdík committed
658
  FIB_ITERATE_END(z);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  dump("iter end");

  fit_init(&i, &f);
  fit_init(&j, &f);
  dump("iter init 2");

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

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

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

  return 0;
}

#endif