rt-table.c 65.3 KB
Newer Older
1
/*
Martin Mareš's avatar
Martin Mareš committed
2
 *	BIRD -- Routing Tables
3
 *
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
/**
 * DOC: Routing tables
 *
 * Routing tables are probably the most important structures BIRD uses. They
 * hold all the information about known networks, the associated routes and
 * their attributes.
 *
16
 * There are multiple routing tables (a primary one together with any
Martin Mareš's avatar
Martin Mareš committed
17 18
 * number of secondary ones if requested by the configuration). Each table
 * is basically a FIB containing entries describing the individual
Martin Mareš's avatar
Martin Mareš committed
19
 * destination networks. For each network (represented by structure &net),
20 21
 * there is a one-way linked list of route entries (&rte), the first entry
 * on the list being the best one (i.e., the one we currently use
Martin Mareš's avatar
Martin Mareš committed
22 23 24 25 26 27 28 29 30
 * for routing), the order of the other ones is undetermined.
 *
 * The &rte contains information specific to the route (preference, protocol
 * metrics, time of last modification etc.) and a pointer to a &rta structure
 * (see the route attribute module for a precise explanation) holding the
 * remaining route attributes which are expected to be shared by multiple
 * routes in order to conserve memory.
 */

31
#undef LOCAL_DEBUG
32

33 34
#include "nest/bird.h"
#include "nest/route.h"
35
#include "nest/protocol.h"
36 37
#include "nest/cli.h"
#include "nest/iface.h"
38
#include "lib/resource.h"
39
#include "lib/event.h"
40
#include "lib/string.h"
41
#include "conf/conf.h"
42
#include "filter/filter.h"
43
#include "lib/string.h"
44
#include "lib/alloca.h"
Martin Mareš's avatar
Martin Mareš committed
45

46 47
pool *rt_table_pool;

48
static slab *rte_slab;
49
static linpool *rte_update_pool;
50

51
static list routing_tables;
52

53
static byte *rt_format_via(rte *e);
54 55 56 57
static void rt_free_hostcache(rtable *tab);
static void rt_notify_hostcache(rtable *tab, net *net);
static void rt_update_hostcache(rtable *tab);
static void rt_next_hop_update(rtable *tab);
58
static inline int rt_prune_table(rtable *tab);
59
static inline void rt_schedule_gc(rtable *tab);
60 61
static inline void rt_schedule_prune(rtable *tab);

62

63 64 65 66 67 68 69 70 71 72 73
/* Like fib_route(), but skips empty net entries */
static net *
net_route(rtable *tab, ip_addr a, int len)
{
  ip_addr a0;
  net *n;

  while (len >= 0)
    {
      a0 = ipa_and(a, ipa_mkmask(len));
      n = fib_find(&tab->fib, &a0, len);
74
      if (n && rte_is_valid(n->routes))
75 76 77 78 79 80
	return n;
      len--;
    }
  return NULL;
}

81
static void
82 83 84 85
rte_init(struct fib_node *N)
{
  net *n = (net *) N;

86
  N->flags = 0;
87 88 89
  n->routes = NULL;
}

Martin Mareš's avatar
Martin Mareš committed
90 91 92
/**
 * rte_find - find a route
 * @net: network node
93
 * @src: route source
Martin Mareš's avatar
Martin Mareš committed
94 95
 *
 * The rte_find() function returns a route for destination @net
96
 * which is from route source @src.
Martin Mareš's avatar
Martin Mareš committed
97
 */
98
rte *
99
rte_find(net *net, struct rte_src *src)
100 101 102
{
  rte *e = net->routes;

103
  while (e && e->attrs->src != src)
104 105 106 107
    e = e->next;
  return e;
}

Martin Mareš's avatar
Martin Mareš committed
108 109
/**
 * rte_get_temp - get a temporary &rte
110
 * @a: attributes to assign to the new route (a &rta; in case it's
111
 * un-cached, rte_update() will create a cached copy automatically)
Martin Mareš's avatar
Martin Mareš committed
112 113 114 115 116
 *
 * Create a temporary &rte and bind it with the attributes @a.
 * Also set route preference to the default preference set for
 * the protocol.
 */
117 118 119 120 121 122
rte *
rte_get_temp(rta *a)
{
  rte *e = sl_alloc(rte_slab);

  e->attrs = a;
123
  e->flags = 0;
124
  e->pref = a->src->proto->preference;
125 126 127
  return e;
}

128 129 130 131 132 133 134 135 136 137 138
rte *
rte_do_cow(rte *r)
{
  rte *e = sl_alloc(rte_slab);

  memcpy(e, r, sizeof(rte));
  e->attrs = rta_clone(r->attrs);
  e->flags = 0;
  return e;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/**
 * rte_cow_rta - get a private writable copy of &rte with writable &rta
 * @r: a route entry to be copied
 * @lp: a linpool from which to allocate &rta
 *
 * rte_cow_rta() takes a &rte and prepares it and associated &rta for
 * modification. There are three possibilities: First, both &rte and &rta are
 * private copies, in that case they are returned unchanged.  Second, &rte is
 * private copy, but &rta is cached, in that case &rta is duplicated using
 * rta_do_cow(). Third, both &rte is shared and &rta is cached, in that case
 * both structures are duplicated by rte_do_cow() and rta_do_cow().
 *
 * Note that in the second case, cached &rta loses one reference, while private
 * copy created by rta_do_cow() is a shallow copy sharing indirect data (eattrs,
 * nexthops, ...) with it. To work properly, original shared &rta should have
 * another reference during the life of created private copy.
 *
 * Result: a pointer to the new writable &rte with writable &rta.
 */
rte *
rte_cow_rta(rte *r, linpool *lp)
{
  if (!rta_is_cached(r->attrs))
    return r;

  rte *e = rte_cow(r);
  rta *a = rta_do_cow(r->attrs, lp);
  rta_free(e->attrs);
  e->attrs = a;
  return e;
}

171 172 173
static int				/* Actually better or at least as good as */
rte_better(rte *new, rte *old)
{
174 175
  int (*better)(rte *, rte *);

176
  if (!rte_is_valid(old))
177
    return 1;
178 179 180
  if (!rte_is_valid(new))
    return 0;

181 182 183 184
  if (new->pref > old->pref)
    return 1;
  if (new->pref < old->pref)
    return 0;
185
  if (new->attrs->src->proto->proto != old->attrs->src->proto->proto)
186 187 188 189 190 191
    {
      /*
       *  If the user has configured protocol preferences, so that two different protocols
       *  have the same preference, try to break the tie by comparing addresses. Not too
       *  useful, but keeps the ordering of routes unambiguous.
       */
192
      return new->attrs->src->proto->proto > old->attrs->src->proto->proto;
193
    }
194
  if (better = new->attrs->src->proto->rte_better)
195 196
    return better(new, old);
  return 0;
197 198
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static int
rte_mergable(rte *pri, rte *sec)
{
  int (*mergable)(rte *, rte *);

  if (!rte_is_valid(pri) || !rte_is_valid(sec))
    return 0;

  if (pri->pref != sec->pref)
    return 0;

  if (pri->attrs->src->proto->proto != sec->attrs->src->proto->proto)
    return 0;

  if (mergable = pri->attrs->src->proto->rte_mergable)
    return mergable(pri, sec);

  return 0;
}

219 220 221
static void
rte_trace(struct proto *p, rte *e, int dir, char *msg)
{
222
  log(L_TRACE "%s %c %s %I/%d %s", p->name, dir, msg, e->net->n.prefix, e->net->n.pxlen, rt_format_via(e));
223 224 225
}

static inline void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
226
rte_trace_in(uint flag, struct proto *p, rte *e, char *msg)
227 228
{
  if (p->debug & flag)
229
    rte_trace(p, e, '>', msg);
230 231 232
}

static inline void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
233
rte_trace_out(uint flag, struct proto *p, rte *e, char *msg)
234 235
{
  if (p->debug & flag)
236
    rte_trace(p, e, '<', msg);
237 238
}

239
static rte *
240
export_filter_(struct announce_hook *ah, rte *rt0, rte **rt_free, ea_list **tmpa, linpool *pool, int silent)
241
{
242 243 244
  struct proto *p = ah->proto;
  struct filter *filter = ah->out_filter;
  struct proto_stats *stats = ah->stats;
245 246 247
  ea_list *tmpb = NULL;
  rte *rt;
  int v;
248

249 250
  rt = rt0;
  *rt_free = NULL;
251

252
  if (!tmpa)
253 254
    tmpa = &tmpb;

255
  *tmpa = rte_make_tmp_attrs(rt, pool);
256

257
  v = p->import_control ? p->import_control(p, &rt, tmpa, pool) : 0;
258 259 260 261
  if (v < 0)
    {
      if (silent)
	goto reject;
262

263
      stats->exp_updates_rejected++;
264 265
      if (v == RIC_REJECT)
	rte_trace_out(D_FILTERS, p, rt, "rejected by protocol");
266 267 268
      goto reject;
    }
  if (v > 0)
269
    {
270 271 272
      if (!silent)
	rte_trace_out(D_FILTERS, p, rt, "forced accept by protocol");
      goto accept;
273
    }
274

275
  v = filter && ((filter == FILTER_REJECT) ||
276
		 (f_run(filter, &rt, tmpa, pool, FF_FORCE_TMPATTR) > F_ACCEPT));
277 278 279 280 281 282 283 284
  if (v)
    {
      if (silent)
	goto reject;

      stats->exp_updates_filtered++;
      rte_trace_out(D_FILTERS, p, rt, "filtered out");
      goto reject;
285
    }
286

287 288 289 290 291 292 293 294 295 296 297 298
 accept:
  if (rt != rt0)
    *rt_free = rt;
  return rt;

 reject:
  /* Discard temporary rte */
  if (rt != rt0)
    rte_free(rt);
  return NULL;
}

299 300 301 302 303 304
static inline rte *
export_filter(struct announce_hook *ah, rte *rt0, rte **rt_free, ea_list **tmpa, int silent)
{
  return export_filter_(ah, rt0, rt_free, tmpa, rte_update_pool, silent);
}

305 306 307 308 309
static void
do_rt_notify(struct announce_hook *ah, net *net, rte *new, rte *old, ea_list *tmpa, int refeed)
{
  struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;
310

311

312
  /*
313 314
   * First, apply export limit.
   *
315 316
   * Export route limits has several problems. Because exp_routes
   * counter is reset before refeed, we don't really know whether
317
   * limit is breached and whether the update is new or not. Therefore
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
   * the number of really exported routes may exceed the limit
   * temporarily (routes exported before and new routes in refeed).
   *
   * Minor advantage is that if the limit is decreased and refeed is
   * requested, the number of exported routes really decrease.
   *
   * Second problem is that with export limits, we don't know whether
   * old was really exported (it might be blocked by limit). When a
   * withdraw is exported, we announce it even when the previous
   * update was blocked. This is not a big issue, but the same problem
   * is in updating exp_routes counter. Therefore, to be consistent in
   * increases and decreases of exp_routes, we count exported routes
   * regardless of blocking by limits.
   *
   * Similar problem is in handling updates - when a new route is
   * received and blocking is active, the route would be blocked, but
   * when an update for the route will be received later, the update
   * would be propagated (as old != NULL). Therefore, we have to block
   * also non-new updates (contrary to import blocking).
   */
338

339
  struct proto_limit *l = ah->out_limit;
340
  if (l && new)
341
    {
342
      if ((!old || refeed) && (stats->exp_routes >= l->limit))
343
	proto_notify_limit(ah, l, PLD_OUT, stats->exp_routes);
344 345 346

      if (l->state == PLS_BLOCKED)
	{
347
	  stats->exp_routes++;	/* see note above */
348 349
	  stats->exp_updates_rejected++;
	  rte_trace_out(D_FILTERS, p, new, "rejected [limit]");
350
	  new = NULL;
351 352 353

	  if (!old)
	    return;
354 355 356
	}
    }

357

358
  if (new)
359
    stats->exp_updates_accepted++;
360
  else
361
    stats->exp_withdraws_accepted++;
362

363 364
  /* Hack: We do not decrease exp_routes during refeed, we instead
     reset exp_routes at the start of refeed. */
365
  if (new)
366
    stats->exp_routes++;
367
  if (old && !refeed)
368
    stats->exp_routes--;
369

370 371 372 373 374 375
  if (p->debug & D_ROUTES)
    {
      if (new && old)
	rte_trace_out(D_ROUTES, p, new, "replaced");
      else if (new)
	rte_trace_out(D_ROUTES, p, new, "added");
376
      else if (old)
377 378
	rte_trace_out(D_ROUTES, p, old, "removed");
    }
379
  if (!new)
380
    p->rt_notify(p, ah->table, net, NULL, old, NULL);
381 382
  else if (tmpa)
    {
383 384 385 386
      ea_list *t = tmpa;
      while (t->next)
	t = t->next;
      t->next = new->attrs->eattrs;
387
      p->rt_notify(p, ah->table, net, new, old, tmpa);
388
      t->next = NULL;
389 390
    }
  else
391
    p->rt_notify(p, ah->table, net, new, old, new->attrs->eattrs);
392 393 394
}

static void
395
rt_notify_basic(struct announce_hook *ah, net *net, rte *new0, rte *old0, int refeed)
396
{
397
  struct proto *p = ah->proto;
398 399
  struct proto_stats *stats = ah->stats;

400 401
  rte *new = new0;
  rte *old = old0;
402 403
  rte *new_free = NULL;
  rte *old_free = NULL;
404
  ea_list *tmpa = NULL;
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  if (new)
    stats->exp_updates_received++;
  else
    stats->exp_withdraws_received++;

  /*
   * This is a tricky part - we don't know whether route 'old' was
   * exported to protocol 'p' or was filtered by the export filter.
   * We try to run the export filter to know this to have a correct
   * value in 'old' argument of rte_update (and proper filter value)
   *
   * FIXME - this is broken because 'configure soft' may change
   * filters but keep routes. Refeed is expected to be called after
   * change of the filters and with old == new, therefore we do not
420
   * even try to run the filter on an old route, This may lead to
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
   * 'spurious withdraws' but ensure that there are no 'missing
   * withdraws'.
   *
   * This is not completely safe as there is a window between
   * reconfiguration and the end of refeed - if a newly filtered
   * route disappears during this period, proper withdraw is not
   * sent (because old would be also filtered) and the route is
   * not refeeded (because it disappeared before that).
   */

  if (new)
    new = export_filter(ah, new, &new_free, &tmpa, 0);

  if (old && !refeed)
    old = export_filter(ah, old, &old_free, NULL, 1);

  if (!new && !old)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  {
    /*
     * As mentioned above, 'old' value may be incorrect in some race conditions.
     * We generally ignore it with the exception of withdraw to pipe protocol.
     * In that case we rather propagate unfiltered withdraws regardless of
     * export filters to ensure that when a protocol is flushed, its routes are
     * removed from all tables. Possible spurious unfiltered withdraws are not
     * problem here as they are ignored if there is no corresponding route at
     * the other end of the pipe. We directly call rt_notify() hook instead of
     * do_rt_notify() to avoid logging and stat counters.
     */

#ifdef CONFIG_PIPE
    if ((p->proto == &proto_pipe) && !new0 && (p != old0->sender->proto))
      p->rt_notify(p, ah->table, net, NULL, old0, NULL);
#endif

455
    return;
456
  }
457 458 459 460 461 462 463 464 465 466 467

  do_rt_notify(ah, net, new, old, tmpa, refeed);

  /* Discard temporary rte's */
  if (new_free)
    rte_free(new_free);
  if (old_free)
    rte_free(old_free);
}

static void
468
rt_notify_accepted(struct announce_hook *ah, net *net, rte *new_changed, rte *old_changed, rte *before_old, int feed)
469 470 471 472
{
  // struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;

473
  rte *r;
474 475 476 477
  rte *new_best = NULL;
  rte *old_best = NULL;
  rte *new_free = NULL;
  rte *old_free = NULL;
478
  ea_list *tmpa = NULL;
479

480 481 482 483 484 485 486
  /* Used to track whether we met old_changed position. If before_old is NULL
     old_changed was the first and we met it implicitly before current best route. */
  int old_meet = old_changed && !before_old;

  /* Note that before_old is either NULL or valid (not rejected) route.
     If old_changed is valid, before_old have to be too. If old changed route
     was not valid, caller must use NULL for both old_changed and before_old. */
487 488 489 490 491 492 493

  if (new_changed)
    stats->exp_updates_received++;
  else
    stats->exp_withdraws_received++;

  /* First, find the new_best route - first accepted by filters */
494
  for (r=net->routes; rte_is_valid(r); r=r->next)
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    {
      if (new_best = export_filter(ah, r, &new_free, &tmpa, 0))
	break;

      /* Note if we walked around the position of old_changed route */
      if (r == before_old)
	old_meet = 1;
    }

  /* 
   * Second, handle the feed case. That means we do not care for
   * old_best. It is NULL for feed, and the new_best for refeed. 
   * For refeed, there is a hack similar to one in rt_notify_basic()
   * to ensure withdraws in case of changed filters
   */
  if (feed)
    {
      if (feed == 2)	/* refeed */
513 514
	old_best = new_best ? new_best :
	  (rte_is_valid(net->routes) ? net->routes : NULL);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      else
	old_best = NULL;

      if (!new_best && !old_best)
	return;

      goto found;
    }

  /*
   * Now, we find the old_best route. Generally, it is the same as the
   * new_best, unless new_best is the same as new_changed or
   * old_changed is accepted before new_best.
   *
   * There are four cases:
   *
   * - We would find and accept old_changed before new_best, therefore
   *   old_changed is old_best. In remaining cases we suppose this
   *   is not true.
   *
   * - We found no new_best, therefore there is also no old_best and
   *   we ignore this withdraw.
   *
   * - We found new_best different than new_changed, therefore
   *   old_best is the same as new_best and we ignore this update.
   *
   * - We found new_best the same as new_changed, therefore it cannot
   *   be old_best and we have to continue search for old_best.
   */

  /* First case */
  if (old_meet)
    if (old_best = export_filter(ah, old_changed, &old_free, NULL, 1))
      goto found;

  /* Second case */
  if (!new_best)
    return;
553

554
  /* Third case, we use r instead of new_best, because export_filter() could change it */
555 556 557 558 559 560 561 562
  if (r != new_changed)
    {
      if (new_free)
	rte_free(new_free);
      return;
    }

  /* Fourth case */
563
  for (r=r->next; rte_is_valid(r); r=r->next)
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    {
      if (old_best = export_filter(ah, r, &old_free, NULL, 1))
	goto found;

      if (r == before_old)
	if (old_best = export_filter(ah, old_changed, &old_free, NULL, 1))
	  goto found;
    }

  /* Implicitly, old_best is NULL and new_best is non-NULL */

 found:
  do_rt_notify(ah, net, new_best, old_best, tmpa, (feed == 2));

  /* Discard temporary rte's */
  if (new_free)
    rte_free(new_free);
  if (old_free)
    rte_free(old_free);
583 584
}

585 586

static struct mpnh *
587
mpnh_merge_rta(struct mpnh *nhs, rta *a, linpool *pool, int max)
588 589 590
{
  struct mpnh nh = { .gw = a->gw, .iface = a->iface };
  struct mpnh *nh2 = (a->dest == RTD_MULTIPATH) ? a->nexthops : &nh;
591
  return mpnh_merge(nhs, nh2, 1, 0, max, pool);
592 593 594
}

rte *
595
rt_export_merged(struct announce_hook *ah, net *net, rte **rt_free, ea_list **tmpa, linpool *pool, int silent)
596 597 598 599 600 601 602 603 604 605 606
{
  // struct proto *p = ah->proto;
  struct mpnh *nhs = NULL;
  rte *best0, *best, *rt0, *rt, *tmp;

  best0 = net->routes;
  *rt_free = NULL;

  if (!rte_is_valid(best0))
    return NULL;

607
  best = export_filter_(ah, best0, rt_free, tmpa, pool, silent);
608 609 610 611 612 613 614 615 616

  if (!best || !rte_is_reachable(best))
    return best;

  for (rt0 = best0->next; rt0; rt0 = rt0->next)
  {
    if (!rte_mergable(best0, rt0))
      continue;

617
    rt = export_filter_(ah, rt0, &tmp, NULL, pool, 1);
618 619 620 621 622

    if (!rt)
      continue;

    if (rte_is_reachable(rt))
623
      nhs = mpnh_merge_rta(nhs, rt->attrs, pool, ah->proto->merge_limit);
624 625 626 627 628 629 630

    if (tmp)
      rte_free(tmp);
  }

  if (nhs)
  {
631
    nhs = mpnh_merge_rta(nhs, best->attrs, pool, ah->proto->merge_limit);
632 633 634

    if (nhs->next)
    {
635
      best = rte_cow_rta(best, pool);
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
      best->attrs->dest = RTD_MULTIPATH;
      best->attrs->nexthops = nhs;
    }
  }

  if (best != best0)
    *rt_free = best;

  return best;
}


static void
rt_notify_merged(struct announce_hook *ah, net *net, rte *new_changed, rte *old_changed,
		 rte *new_best, rte*old_best, int refeed)
{
  // struct proto *p = ah->proto;

  rte *new_best_free = NULL;
  rte *old_best_free = NULL;
  rte *new_changed_free = NULL;
  rte *old_changed_free = NULL;
  ea_list *tmpa = NULL;

  /* We assume that all rte arguments are either NULL or rte_is_valid() */

  /* This check should be done by the caller */
  if (!new_best && !old_best)
    return;

  /* Check whether the change is relevant to the merged route */
  if ((new_best == old_best) && !refeed)
  {
    new_changed = rte_mergable(new_best, new_changed) ?
      export_filter(ah, new_changed, &new_changed_free, NULL, 1) : NULL;

    old_changed = rte_mergable(old_best, old_changed) ?
      export_filter(ah, old_changed, &old_changed_free, NULL, 1) : NULL;

    if (!new_changed && !old_changed)
      return;
  }

  if (new_best)
    ah->stats->exp_updates_received++;
  else
    ah->stats->exp_withdraws_received++;

  /* Prepare new merged route */
  if (new_best)
686
    new_best = rt_export_merged(ah, net, &new_best_free, &tmpa, rte_update_pool, 0);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

  /* Prepare old merged route (without proper merged next hops) */
  /* There are some issues with running filter on old route - see rt_notify_basic() */
  if (old_best && !refeed)
    old_best = export_filter(ah, old_best, &old_best_free, NULL, 1);

  if (new_best || old_best)
    do_rt_notify(ah, net, new_best, old_best, tmpa, refeed);

  /* Discard temporary rte's */
  if (new_best_free)
    rte_free(new_best_free);
  if (old_best_free)
    rte_free(old_best_free);
  if (new_changed_free)
    rte_free(new_changed_free);
  if (old_changed_free)
    rte_free(old_changed_free);
}


708 709 710
/**
 * rte_announce - announce a routing table change
 * @tab: table the route has been added to
711
 * @type: type of route announcement (RA_OPTIMAL or RA_ANY)
712 713
 * @net: network in question
 * @new: the new route to be announced
714
 * @old: the previous route for the same network
715 716 717 718
 * @new_best: the new best route for the same network
 * @old_best: the previous best route for the same network
 * @before_old: The previous route before @old for the same network.
 * 		If @before_old is NULL @old was the first.
719 720
 *
 * This function gets a routing table update and announces it
Ondřej Zajíček's avatar
Ondřej Zajíček committed
721 722
 * to all protocols that acccepts given type of route announcement
 * and are connected to the same table by their announcement hooks.
723
 *
724
 * Route announcement of type %RA_OPTIMAL si generated when optimal
Ondřej Zajíček's avatar
Ondřej Zajíček committed
725 726
 * route (in routing table @tab) changes. In that case @old stores the
 * old optimal route.
727
 *
728
 * Route announcement of type %RA_ANY si generated when any route (in
Ondřej Zajíček's avatar
Ondřej Zajíček committed
729 730 731 732 733 734 735 736 737 738
 * routing table @tab) changes In that case @old stores the old route
 * from the same protocol.
 *
 * For each appropriate protocol, we first call its import_control()
 * hook which performs basic checks on the route (each protocol has a
 * right to veto or force accept of the route before any filter is
 * asked) and adds default values of attributes specific to the new
 * protocol (metrics, tags etc.).  Then it consults the protocol's
 * export filter and if it accepts the route, the rt_notify() hook of
 * the protocol gets called.
739
 */
740
static void
741 742
rte_announce(rtable *tab, unsigned type, net *net, rte *new, rte *old,
	     rte *new_best, rte *old_best, rte *before_old)
743
{
744 745 746
  if (!rte_is_valid(new))
    new = NULL;

747 748 749
  if (!rte_is_valid(old))
    old = before_old = NULL;

750 751 752 753 754
  if (!rte_is_valid(new_best))
    new_best = NULL;

  if (!rte_is_valid(old_best))
    old_best = NULL;
755 756 757

  if (!old && !new)
    return;
758

759 760 761
  if (type == RA_OPTIMAL)
    {
      if (new)
762
	new->attrs->src->proto->stats.pref_routes++;
763
      if (old)
764
	old->attrs->src->proto->stats.pref_routes--;
765 766 767

      if (tab->hostcache)
	rt_notify_hostcache(tab, net);
768 769
    }

770
  struct announce_hook *a;
771
  WALK_LIST(a, tab->hooks)
772
    {
773
      ASSERT(a->proto->export_state != ES_DOWN);
774
      if (a->proto->accept_ra_types == type)
775
	if (type == RA_ACCEPTED)
776
	  rt_notify_accepted(a, net, new, old, before_old, 0);
777 778
	else if (type == RA_MERGED)
	  rt_notify_merged(a, net, new, old, new_best, old_best, 0);
779
	else
780
	  rt_notify_basic(a, net, new, old, 0);
781
    }
782 783
}

784 785 786 787 788 789
static inline int
rte_validate(rte *e)
{
  int c;
  net *n = e->net;

790
  if ((n->n.pxlen > BITS_PER_IP_ADDRESS) || !ip_is_prefix(n->n.prefix,n->n.pxlen))
791
    {
792
      log(L_WARN "Ignoring bogus prefix %I/%d received via %s",
793
	  n->n.prefix, n->n.pxlen, e->sender->proto->name);
794 795
      return 0;
    }
796 797 798

  c = ipa_classify_net(n->n.prefix);
  if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
799
    {
800
      log(L_WARN "Ignoring bogus route %I/%d received via %s",
801
	  n->n.prefix, n->n.pxlen, e->sender->proto->name);
802
      return 0;
803
    }
804

805 806 807 808 809 810 811
  if ((e->attrs->dest == RTD_MULTIPATH) && !mpnh_is_sorted(e->attrs->nexthops))
    {
      log(L_WARN "Ignoring unsorted multipath route %I/%d received via %s",
	  n->n.prefix, n->n.pxlen, e->sender->proto->name);
      return 0;
    }

812 813 814
  return 1;
}

Martin Mareš's avatar
Martin Mareš committed
815 816 817 818 819 820
/**
 * rte_free - delete a &rte
 * @e: &rte to be deleted
 *
 * rte_free() deletes the given &rte from the routing table it's linked to.
 */
821
void
822
rte_free(rte *e)
823
{
824
  if (rta_is_cached(e->attrs))
825 826 827 828 829 830
    rta_free(e->attrs);
  sl_free(rte_slab, e);
}

static inline void
rte_free_quick(rte *e)
831 832 833 834 835
{
  rta_free(e->attrs);
  sl_free(rte_slab, e);
}

836 837 838 839 840 841 842 843
static int
rte_same(rte *x, rte *y)
{
  return
    x->attrs == y->attrs &&
    x->flags == y->flags &&
    x->pflags == y->pflags &&
    x->pref == y->pref &&
844
    (!x->attrs->src->proto->rte_same || x->attrs->src->proto->rte_same(x, y));
845 846
}

847 848
static inline int rte_is_ok(rte *e) { return e && !rte_is_filtered(e); }

849
static void
850
rte_recalculate(struct announce_hook *ah, net *net, rte *new, struct rte_src *src)
851
{
852 853 854
  struct proto *p = ah->proto;
  struct rtable *table = ah->table;
  struct proto_stats *stats = ah->stats;
855
  static struct tbf rl_pipe = TBF_DEFAULT_LOG_LIMITS;
856
  rte *before_old = NULL;
857 858
  rte *old_best = net->routes;
  rte *old = NULL;
859
  rte **k;
860 861 862 863

  k = &net->routes;			/* Find and remove original route from the same protocol */
  while (old = *k)
    {
864
      if (old->attrs->src == src)
865
	{
866 867 868 869 870 871 872 873 874
	  /* If there is the same route in the routing table but from
	   * a different sender, then there are two paths from the
	   * source protocol to this routing table through transparent
	   * pipes, which is not allowed.
	   *
	   * We log that and ignore the route. If it is withdraw, we
	   * ignore it completely (there might be 'spurious withdraws',
	   * see FIXME in do_rte_announce())
	   */
875
	  if (old->sender->proto != p)
876 877 878
	    {
	      if (new)
		{
879
		  log_rl(&rl_pipe, L_ERR "Pipe collision detected when sending %I/%d to table %s",
880 881 882 883 884 885
		      net->n.prefix, net->n.pxlen, table->name);
		  rte_free_quick(new);
		}
	      return;
	    }

886
	  if (new && rte_same(old, new))
887 888
	    {
	      /* No changes, ignore the new route */
889

890
	      if (!rte_is_filtered(new))
891 892 893 894 895
		{
		  stats->imp_updates_ignored++;
		  rte_trace_in(D_ROUTES, p, new, "ignored");
		}

896 897 898
	      rte_free_quick(new);
	      return;
	    }
899 900 901 902
	  *k = old->next;
	  break;
	}
      k = &old->next;
903
      before_old = old;
904 905
    }

906 907 908
  if (!old)
    before_old = NULL;

909 910
  if (!old && !new)
    {
911
      stats->imp_withdraws_ignored++;
912 913 914
      return;
    }

915 916 917 918
  int new_ok = rte_is_ok(new);
  int old_ok = rte_is_ok(old);

  struct proto_limit *l = ah->rx_limit;
919
  if (l && !old && new)
920
    {
921
      u32 all_routes = stats->imp_routes + stats->filt_routes;
922 923

      if (all_routes >= l->limit)
924
	proto_notify_limit(ah, l, PLD_RX, all_routes);
925 926 927

      if (l->state == PLS_BLOCKED)
	{
928 929 930
	  /* In receive limit the situation is simple, old is NULL so
	     we just free new and exit like nothing happened */

931 932 933 934 935
	  stats->imp_updates_ignored++;
	  rte_trace_in(D_FILTERS, p, new, "ignored [limit]");
	  rte_free_quick(new);
	  return;
	}
936 937
    }

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
  l = ah->in_limit;
  if (l && !old_ok && new_ok)
    {
      if (stats->imp_routes >= l->limit)
	proto_notify_limit(ah, l, PLD_IN, stats->imp_routes);

      if (l->state == PLS_BLOCKED)
	{
	  /* In import limit the situation is more complicated. We
	     shouldn't just drop the route, we should handle it like
	     it was filtered. We also have to continue the route
	     processing if old or new is non-NULL, but we should exit
	     if both are NULL as this case is probably assumed to be
	     already handled. */

	  stats->imp_updates_ignored++;
	  rte_trace_in(D_FILTERS, p, new, "ignored [limit]");

	  if (ah->in_keep_filtered)
	    new->flags |= REF_FILTERED;
	  else
	    { rte_free_quick(new); new = NULL; }

	  /* Note that old && !new could be possible when
	     ah->in_keep_filtered changed in the recent past. */

	  if (!old && !new)
	    return;

	  new_ok = 0;
	  goto skip_stats1;
	}
    }
971 972

  if (new_ok)
973
    stats->imp_updates_accepted++;
974
  else if (old_ok)
975
    stats->imp_withdraws_accepted++;
976 977
  else
    stats->imp_withdraws_ignored++;
978

979
 skip_stats1:
980 981

  if (new)
982
    rte_is_filtered(new) ? stats->filt_routes++ : stats->imp_routes++;
983
  if (old)
984
    rte_is_filtered(old) ? stats->filt_routes-- : stats->imp_routes--;
985

986
  if (table->config->sorted)
987
    {
988 989 990 991 992 993 994
      /* If routes are sorted, just insert new route to appropriate position */
      if (new)
	{
	  if (before_old && !rte_better(new, before_old))
	    k = &before_old->next;
	  else
	    k = &net->routes;
995

996 997 998
	  for (; *k; k=&(*k)->next)
	    if (rte_better(new, *k))
	      break;
999

1000 1001 1002
	  new->next = *k;
	  *k = new;
	}
1003
    }
1004
  else
1005
    {
1006 1007 1008
      /* If routes are not sorted, find the best route and move it on
	 the first position. There are several optimized cases. */

1009
      if (src->proto->rte_recalculate && src->proto->rte_recalculate(table, net, new, old, old_best))
1010 1011 1012
	goto do_recalculate;

      if (new && rte_better(new, old_best))
1013
	{
1014 1015 1016
	  /* The first case - the new route is cleary optimal,
	     we link it at the first position */

1017 1018 1019
	  new->next = net->routes;
	  net->routes = new;
	}
1020
      else if (old == old_best)
1021
	{
1022 1023 1024 1025 1026 1027 1028 1029 1030
	  /* The second case - the old best route disappeared, we add the
	     new route (if we have any) to the list (we don't care about
	     position) and then we elect the new optimal route and relink
	     that route at the first position and announce it. New optimal
	     route might be NULL if there is no more routes */

	do_recalculate:
	  /* Add the new route to the list */
	  if (new)
1031
	    {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	      new->next = net->routes;
	      net->routes = new;
	    }

	  /* Find a new optimal route (if there is any) */
	  if (net->routes)
	    {
	      rte **bp = &net->routes;
	      for (k=&(*bp)->next; *k; k=&(*k)->next)
		if (rte_better(*k, *bp))
		  bp = k;

	      /* And relink it */
	      rte *best = *bp;
	      *bp = best->next;
	      best->next = net->routes;
	      net->routes = best;
1049 1050
	    }
	}
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
      else if (new)
	{
	  /* The third case - the new route is not better than the old
	     best route (therefore old_best != NULL) and the old best
	     route was not removed (therefore old_best == net->routes).
	     We just link the new route after the old best route. */

	  ASSERT(net->routes != NULL);
	  new->next = net->routes->next;
	  net->routes->next = new;
	}
      /* The fourth (empty) case - suboptimal route was removed, nothing to do */
1063
    }
1064

1065 1066 1067 1068
  if (new)
    new->lastmod = now;

  /* Log the route change */
1069
  if (p->debug & D_ROUTES)
1070
    {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
      if (new_ok)
	rte_trace(p, new, '>', new == net->routes ? "added [best]" : "added");
      else if (old_ok)
	{
	  if (old != old_best)
	    rte_trace(p, old, '>', "removed");
	  else if (rte_is_ok(net->routes))
	    rte_trace(p, old, '>', "removed [replaced]");
	  else
	    rte_trace(p, old, '>', "removed [sole]");
	}
1082 1083
    }

1084
  /* Propagate the route change */
1085
  rte_announce(table, RA_ANY, net, new, old, NULL, NULL, NULL);
1086
  if (net->routes != old_best)
1087
    rte_announce(table, RA_OPTIMAL, net, net->routes, old_best, NULL, NULL, NULL);
1088
  if (table->config->sorted)
1089 1090
    rte_announce(table, RA_ACCEPTED, net, new, old, NULL, NULL, before_old);
  rte_announce(table, RA_MERGED, net, new, old, net->routes, old_best, NULL);
1091 1092 1093 1094 1095 1096

  if (!net->routes &&
      (table->gc_counter++ >= table->config->gc_max_ops) &&
      (table->gc_time + table->config->gc_min_time <= now))
    rt_schedule_gc(table);

1097 1098 1099 1100 1101
  if (old_ok && p->rte_remove)
    p->rte_remove(net, old);
  if (new_ok && p->rte_insert)
    p->rte_insert(net, new);

1102
  if (old)
1103
    rte_free_quick(old);
1104 1105
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
static int rte_update_nest_cnt;		/* Nesting counter to allow recursive updates */

static inline void
rte_update_lock(void)
{
  rte_update_nest_cnt++;
}

static inline void
rte_update_unlock(void)
{
  if (!--rte_update_nest_cnt)
    lp_flush(rte_update_pool);
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
static inline void
rte_hide_dummy_routes(net *net, rte **dummy)
{
  if (net->routes && net->routes->attrs->source == RTS_DUMMY)
  {
    *dummy = net->routes;
    net->routes = (*dummy)->next;
  }
}

static inline void
rte_unhide_dummy_routes(net *net, rte **dummy)
{
  if (*dummy)
  {
    (*dummy)->next = net->routes;
    net->routes = *dummy;
  }
}

Martin Mareš's avatar
Martin Mareš committed
1141 1142 1143
/**
 * rte_update - enter a new update to a routing table
 * @table: table to be updated
1144
 * @ah: pointer to table announce hook
Martin Mareš's avatar
Martin Mareš committed
1145 1146
 * @net: network node
 * @p: protocol submitting the update
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1147
 * @src: protocol originating the update
Martin Mareš's avatar
Martin Mareš committed
1148 1149 1150 1151
 * @new: a &rte representing the new route or %NULL for route removal.
 *
 * This function is called by the routing protocols whenever they discover
 * a new route or wish to update/remove an existing route. The right announcement
1152
 * sequence is to build route attributes first (either un-cached with @aflags set
Martin Mareš's avatar
Martin Mareš committed
1153 1154 1155 1156 1157
 * to zero or a cached one using rta_lookup(); in this case please note that
 * you need to increase the use count of the attributes yourself by calling
 * rta_clone()), call rte_get_temp() to obtain a temporary &rte, fill in all
 * the appropriate data and finally submit the new &rte by calling rte_update().
 *
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1158 1159 1160 1161 1162 1163
 * @src specifies the protocol that originally created the route and the meaning
 * of protocol-dependent data of @new. If @new is not %NULL, @src have to be the
 * same value as @new->attrs->proto. @p specifies the protocol that called
 * rte_update(). In most cases it is the same protocol as @src. rte_update()
 * stores @p in @new->sender;
 *
1164 1165 1166 1167 1168 1169 1170 1171 1172
 * When rte_update() gets any route, it automatically validates it (checks,
 * whether the network and next hop address are valid IP addresses and also
 * whether a normal routing protocol doesn't try to smuggle a host or link
 * scope route to the table), converts all protocol dependent attributes stored
 * in the &rte to temporary extended attributes, consults import filters of the
 * protocol to see if the route should be accepted and/or its attributes modified,
 * stores the temporary attributes back to the &rte.
 *
 * Now, having a "public" version of the route, we
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1173
 * automatically find any old route defined by the protocol @src
Martin Mareš's avatar
Martin Mareš committed
1174 1175
 * for network @n, replace it by the new one (or removing it if @new is %NULL),
 * recalculate the optimal route for this destination and finally broadcast
1176
 * the change (if any) to all routing protocols by calling rte_announce().
1177 1178 1179 1180
 *
 * All memory used for attribute lists and other temporary allocations is taken
 * from a special linear pool @rte_update_pool and freed when rte_update()
 * finishes.
Martin Mareš's avatar
Martin Mareš committed
1181
 */
1182 1183

void
1184
rte_update2(struct announce_hook *ah, net *net, rte *new, struct rte_src *src)
1185
{
1186 1187 1188
  struct proto *p = ah->proto;
  struct proto_stats *stats = ah->stats;
  struct filter *filter = ah->in_filter;
1189
  ea_list *tmpa = NULL;
1190
  rte *dummy = NULL;
1191 1192 1193 1194

  rte_update_lock();
  if (new)
    {
1195
      new->sender = ah;
1196

1197
      stats->imp_updates_received++;
1198 1199 1200
      if (!rte_validate(new))
	{
	  rte_trace_in(D_FILTERS, p, new, "invalid");
1201
	  stats->imp_updates_invalid++;
1202 1203
	  goto drop;
	}
1204

1205
      if (filter == FILTER_REJECT)
1206
	{
1207
	  stats->imp_updates_filtered++;
1208
	  rte_trace_in(D_FILTERS, p, new, "filtered out");
1209

1210
	  if (! ah->in_keep_filtered)
1211 1212 1213
	    goto drop;

	  /* new is a private copy, i could modify it */
1214
	  new->flags |= REF_FILTERED;
1215
	}
1216
      else
1217
	{
1218
	  tmpa = rte_make_tmp_attrs(new, rte_update_pool);
1219
	  if (filter && (filter != FILTER_REJECT))
1220
	    {
1221 1222 1223 1224 1225 1226 1227
	      ea_list *old_tmpa = tmpa;
	      int fr = f_run(filter, &new, &tmpa, rte_update_pool, 0);
	      if (fr > F_ACCEPT)
		{
		  stats->imp_updates_filtered++;
		  rte_trace_in(D_FILTERS, p, new, "filtered out");

1228
		  if (! ah->in_keep_filtered)
1229 1230
		    goto drop;

1231
		  new->flags |= REF_FILTERED;
1232
		}
1233 1234
	      if (tmpa != old_tmpa && src->proto->store_tmp_attrs)
		src->proto->store_tmp_attrs(new, tmpa);
1235
	    }
1236
	}
1237
      if (!rta_is_cached(new->attrs)) /* Need to copy attributes */
1238 1239 1240
	new->attrs = rta_lookup(new->attrs);
      new->flags |= REF_COW;
    }
1241
  else
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    {
      stats->imp_withdraws_received++;

      if (!net || !src)
	{
	  stats->imp_withdraws_ignored++;
	  rte_update_unlock();
	  return;
	}
    }
1252

1253 1254
 recalc:
  rte_hide_dummy_routes(net, &dummy);
1255
  rte_recalculate(ah, net, new, src);
1256
  rte_unhide_dummy_routes(net, &dummy);
1257 1258 1259
  rte_update_unlock();
  return;

1260
 drop:
1261
  rte_free(new);
1262 1263
  new = NULL;
  goto recalc;
1264 1265
}

1266 1267 1268
/* Independent call to rte_announce(), used from next hop
   recalculation, outside of rte_update(). new must be non-NULL */
static inline void 
1269 1270
rte_announce_i(rtable *tab, unsigned type, net *net, rte *new, rte *old,
	       rte *new_best, rte *old_best)
1271 1272
{
  rte_update_lock();
1273
  rte_announce(tab, type, net, new, old, new_best, old_best, NULL);
1274 1275 1276
  rte_update_unlock();
}

1277 1278
static inline void
rte_discard(rte *old)	/* Non-filtered route deletion, used during garbage collection */
1279
{
1280
  rte_update_lock();
1281
  rte_recalculate(old->sender, old->net, NULL, old->attrs->src);
1282
  rte_update_unlock();
1283 1284
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
/* Check rtable for best route to given net whether it would be exported do p */
int
rt_examine(rtable *t, ip_addr prefix, int pxlen, struct proto *p, struct filter *filter)
{
  net *n = net_find(t, prefix, pxlen);
  rte *rt = n ? n->routes : NULL;

  if (!rte_is_valid(rt))
    return 0;

  rte_update_lock();

  /* Rest is stripped down export_filter() */
1298
  ea_list *tmpa = rte_make_tmp_attrs(rt, rte_update_pool);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  int v = p->import_control ? p->import_control(p, &rt, &tmpa, rte_update_pool) : 0;
  if (v == RIC_PROCESS)
    v = (f_run(filter, &rt, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);

   /* Discard temporary rte */
  if (rt != n->routes)
    rte_free(rt);

  rte_update_unlock();

  return v > 0;
}

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

/**
 * rt_refresh_begin - start a refresh cycle
 * @t: related routing table
 * @ah: related announce hook 
 *
 * This function starts a refresh cycle for given routing table and announce
 * hook. The refresh cycle is a sequence where the protocol sends all its valid
 * routes to the routing table (by rte_update()). After that, all protocol
 * routes (more precisely routes with @ah as @sender) not sent during the
 * refresh cycle but still in the table from the past are pruned. This is
 * implemented by marking all related routes as stale by REF_STALE flag in
 * rt_refresh_begin(), then marking all related stale routes with REF_DISCARD
 * flag in rt_refresh_end() and then removing such routes in the prune loop.
 */
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
void
rt_refresh_begin(rtable *t, struct announce_hook *ah)
{
  net *n;
  rte *e;

  FIB_WALK(&t->fib, fn)
    {
      n = (net *) fn;
      for (e = n->routes; e; e = e->next)
	if (e->sender == ah)
	  e->flags |= REF_STALE;
    }
  FIB_WALK_END;
}

1343 1344 1345 1346 1347 1348 1349 1350
/**
 * rt_refresh_end - end a refresh cycle
 * @t: related routing table
 * @ah: related announce hook 
 *
 * This function starts a refresh cycle for given routing table and announce
 * hook. See rt_refresh_begin() for description of refresh cycles.
 */
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
void
rt_refresh_end(rtable *t, struct announce_hook *ah)
{
  int prune = 0;
  net *n;
  rte *e;

  FIB_WALK(&t->fib, fn)
    {
      n = (net *) fn;
      for (e = n->routes; e; e = e->next)
	if ((e->sender == ah) && (e->flags & REF_STALE))
	  {
	    e->flags |= REF_DISCARD;
	    prune = 1;
	  }
    }
  FIB_WALK_END;

  if (prune)
    rt_schedule_prune(t);
}


Martin Mareš's avatar
Martin Mareš committed
1375 1376 1377 1378 1379 1380
/**
 * rte_dump - dump a route
 * @e: &rte to be dumped
 *
 * This functions dumps contents of a &rte to debug output.
 */
1381
void
1382
rte_dump(rte *e)
1383
{
1384
  net *n = e->net;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
1385
  debug("%-1I/%2d ", n->n.prefix, n->n.pxlen);
1386
  debug("KF=%02x PF=%02x pref=%d lm=%d ", n->n.flags, e->pflags, e->pref, now-e->lastmod);
1387
  rta_dump(e->attrs);
1388 1389
  if (e->attrs->src->proto->proto->dump_attrs)
    e->attrs->src->proto->proto->dump_attrs(e);
1390
  debug("\n");
1391
}
1392

Martin Mareš's avatar
Martin Mareš committed
1393 1394 1395 1396 1397 1398
/**
 * rt_dump - dump a routing table
 * @t: routing table to be dumped
 *
 * This function dumps contents of a given routing table to debug output.
 */
1399 1400 1401
void
rt_dump(rtable *t)
{
1402 1403
  rte *e;
  net *n;
1404
  struct announce_hook *a;
1405 1406

  debug("Dump of routing table <%s>\n", t->name);
1407
#ifdef DEBUGGING
1408
  fib_check(&t->fib);
1409
#endif
1410 1411 1412 1413 1414
  FIB_WALK(&t->fib, fn)
    {
      n = (net *) fn;
      for(e=n->routes; e; e=e->next)
	rte_dump(e);
1415
    }
1416
  FIB_WALK_END;
1417 1418
  WALK_LIST(a, t->hooks)
    debug("\tAnnounces routes to protocol %s\n", a->proto->name);
1419
  debug("\n");
1420
}
1421

Martin Mareš's avatar
Martin Mareš committed
1422 1423 1424 1425 1426
/**
 * rt_dump_all - dump all routing tables
 *
 * This function dumps contents of all routing tables to debug output.
 */
1427 1428 1429
void
rt_dump_all(void)
{
1430 1431 1432 1433
  rtable *t;

  WALK_LIST(t, routing_tables)
    rt_dump(t);
1434 1435
}

1436 1437 1438 1439 1440 1441 1442
static inline void
rt_schedule_prune(rtable *tab)
{
  rt_mark_for_prune(tab);
  ev_schedule(tab->rt_event);
}

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
static inline void
rt_schedule_gc(rtable *tab)
{
  if (tab->gc_scheduled)
    return;

  tab->gc_scheduled = 1;
  ev_schedule(tab->rt_event);
}

static inline void
rt_schedule_hcu(rtable *tab)
{
  if (tab->hcu_scheduled)
    return;

  tab->hcu_scheduled = 1;
  ev_schedule(tab->rt_event);
}

static inline void
rt_schedule_nhu(rtable *tab)
{
  if (tab->nhu_state == 0)
    ev_schedule(tab->rt_event);

  /* state change 0->1, 2->3 */
  tab->nhu_state |= 1;
}

1473

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
static void
rt_prune_nets(rtable *tab)
{
  struct fib_iterator fit;
  int ncnt = 0, ndel = 0<