]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_spf.c
remove zebra/irdp.c because it is no longer in the source tree.
[mirror_frr.git] / ospfd / ospf_spf.c
CommitLineData
718e3744 1/* OSPF SPF calculation.
2 Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "thread.h"
24#include "memory.h"
25#include "hash.h"
26#include "linklist.h"
27#include "prefix.h"
28#include "if.h"
29#include "table.h"
30#include "log.h"
31#include "sockunion.h" /* for inet_ntop () */
32
33#include "ospfd/ospfd.h"
34#include "ospfd/ospf_interface.h"
35#include "ospfd/ospf_ism.h"
36#include "ospfd/ospf_asbr.h"
37#include "ospfd/ospf_lsa.h"
38#include "ospfd/ospf_lsdb.h"
39#include "ospfd/ospf_neighbor.h"
40#include "ospfd/ospf_nsm.h"
41#include "ospfd/ospf_spf.h"
42#include "ospfd/ospf_route.h"
43#include "ospfd/ospf_ia.h"
44#include "ospfd/ospf_ase.h"
45#include "ospfd/ospf_abr.h"
46#include "ospfd/ospf_dump.h"
47
48#define DEBUG
49
50struct vertex_nexthop *
51vertex_nexthop_new (struct vertex *parent)
52{
53 struct vertex_nexthop *new;
54
55 new = XCALLOC (MTYPE_OSPF_NEXTHOP, sizeof (struct vertex_nexthop));
56 new->parent = parent;
57
58 return new;
59}
60
61void
62vertex_nexthop_free (struct vertex_nexthop *nh)
63{
64 XFREE (MTYPE_OSPF_NEXTHOP, nh);
65}
66
67struct vertex_nexthop *
68vertex_nexthop_dup (struct vertex_nexthop *nh)
69{
70 struct vertex_nexthop *new;
71
72 new = vertex_nexthop_new (nh->parent);
73
74 new->oi = nh->oi;
75 new->router = nh->router;
76
77 return new;
78}
718e3744 79\f
0c0f9cd5 80
718e3744 81struct vertex *
82ospf_vertex_new (struct ospf_lsa *lsa)
83{
84 struct vertex *new;
85
86 new = XMALLOC (MTYPE_OSPF_VERTEX, sizeof (struct vertex));
87 memset (new, 0, sizeof (struct vertex));
88
89 new->flags = 0;
90 new->type = lsa->data->type;
91 new->id = lsa->data->id;
92 new->lsa = lsa->data;
93 new->distance = 0;
94 new->child = list_new ();
95 new->nexthop = list_new ();
d355bfa7 96 new->backlink = -1;
718e3744 97
98 return new;
99}
100
101void
102ospf_vertex_free (struct vertex *v)
103{
104 listnode node;
105
106 list_delete (v->child);
107
108 if (listcount (v->nexthop) > 0)
109 for (node = listhead (v->nexthop); node; nextnode (node))
110 vertex_nexthop_free (node->data);
111
112 list_delete (v->nexthop);
113
114 XFREE (MTYPE_OSPF_VERTEX, v);
115}
116
117void
118ospf_vertex_add_parent (struct vertex *v)
119{
120 struct vertex_nexthop *nh;
121 listnode node;
122
123 for (node = listhead (v->nexthop); node; nextnode (node))
124 {
125 nh = (struct vertex_nexthop *) getdata (node);
126
127 /* No need to add two links from the same parent. */
128 if (listnode_lookup (nh->parent->child, v) == NULL)
0c0f9cd5 129 listnode_add (nh->parent->child, v);
718e3744 130 }
131}
132\f
133void
134ospf_spf_init (struct ospf_area *area)
135{
136 struct vertex *v;
137
138 /* Create root node. */
139 v = ospf_vertex_new (area->router_lsa_self);
140
141 area->spf = v;
142
143 /* Reset ABR and ASBR router counts. */
144 area->abr_count = 0;
145 area->asbr_count = 0;
146}
147
148int
149ospf_spf_has_vertex (struct route_table *rv, struct route_table *nv,
150 struct lsa_header *lsa)
151{
152 struct prefix p;
153 struct route_node *rn;
154
155 p.family = AF_INET;
156 p.prefixlen = IPV4_MAX_BITLEN;
157 p.u.prefix4 = lsa->id;
158
159 if (lsa->type == OSPF_ROUTER_LSA)
160 rn = route_node_get (rv, &p);
161 else
162 rn = route_node_get (nv, &p);
163
164 if (rn->info != NULL)
165 {
166 route_unlock_node (rn);
167 return 1;
168 }
169 return 0;
170}
171
172listnode
173ospf_vertex_lookup (list vlist, struct in_addr id, int type)
174{
175 listnode node;
176 struct vertex *v;
177
178 for (node = listhead (vlist); node; nextnode (node))
179 {
180 v = (struct vertex *) getdata (node);
181 if (IPV4_ADDR_SAME (&id, &v->id) && type == v->type)
182 return node;
183 }
184
185 return NULL;
186}
187
d355bfa7 188/* return index of link back to V from W, or -1 if no link found */
718e3744 189int
190ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v)
191{
192 int i;
193 int length;
194 struct router_lsa *rl;
195 struct network_lsa *nl;
196
197 /* In case of W is Network LSA. */
198 if (w->type == OSPF_NETWORK_LSA)
199 {
200 if (v->type == OSPF_NETWORK_LSA)
d355bfa7 201 return -1;
718e3744 202
203 nl = (struct network_lsa *) w;
204 length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
0c0f9cd5 205
718e3744 206 for (i = 0; i < length; i++)
207 if (IPV4_ADDR_SAME (&nl->routers[i], &v->id))
d355bfa7 208 return i;
209 return -1;
718e3744 210 }
211
212 /* In case of W is Router LSA. */
213 if (w->type == OSPF_ROUTER_LSA)
214 {
215 rl = (struct router_lsa *) w;
216
217 length = ntohs (w->length);
218
219 for (i = 0;
0c0f9cd5 220 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
221 i++, length -= 12)
718e3744 222 {
223 switch (rl->link[i].type)
224 {
225 case LSA_LINK_TYPE_POINTOPOINT:
226 case LSA_LINK_TYPE_VIRTUALLINK:
227 /* Router LSA ID. */
228 if (v->type == OSPF_ROUTER_LSA &&
229 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
230 {
d355bfa7 231 return i;
718e3744 232 }
233 break;
234 case LSA_LINK_TYPE_TRANSIT:
235 /* Network LSA ID. */
236 if (v->type == OSPF_NETWORK_LSA &&
237 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
238 {
d355bfa7 239 return i;
0c0f9cd5 240 }
718e3744 241 break;
242 case LSA_LINK_TYPE_STUB:
243 /* Not take into count? */
244 continue;
245 default:
246 break;
247 }
248 }
249 }
d355bfa7 250 return -1;
718e3744 251}
252
253/* Add the nexthop to the list, only if it is unique.
254 * If it's not unique, free the nexthop entry.
255 */
256void
257ospf_nexthop_add_unique (struct vertex_nexthop *new, list nexthop)
258{
259 struct vertex_nexthop *nh;
260 listnode node;
261 int match;
262
263 match = 0;
264 for (node = listhead (nexthop); node; nextnode (node))
265 {
266 nh = node->data;
267
268 /* Compare the two entries. */
269 /* XXX
270 * Comparing the parent preserves the shortest path tree
271 * structure even when the nexthops are identical.
272 */
273 if (nh->oi == new->oi &&
0c0f9cd5 274 IPV4_ADDR_SAME (&nh->router, &new->router) &&
275 nh->parent == new->parent)
276 {
277 match = 1;
278 break;
279 }
718e3744 280 }
281
282 if (!match)
283 listnode_add (nexthop, new);
284 else
285 vertex_nexthop_free (new);
286}
287
288/* Merge entries in list b into list a. */
289void
290ospf_nexthop_merge (list a, list b)
291{
292 struct listnode *n;
293
294 for (n = listhead (b); n; nextnode (n))
295 {
296 ospf_nexthop_add_unique (n->data, a);
297 }
298}
299
300#define ROUTER_LSA_MIN_SIZE 12
301#define ROUTER_LSA_TOS_SIZE 4
302
303struct router_lsa_link *
304ospf_get_next_link (struct vertex *v, struct vertex *w,
0c0f9cd5 305 struct router_lsa_link *prev_link)
718e3744 306{
307 u_char *p;
308 u_char *lim;
309 struct router_lsa_link *l;
310
311 if (prev_link == NULL)
312 p = ((u_char *) v->lsa) + 24;
313 else
314 {
0c0f9cd5 315 p = (u_char *) prev_link;
718e3744 316 p += (ROUTER_LSA_MIN_SIZE +
317 (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
318 }
0c0f9cd5 319
718e3744 320 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
321
322 while (p < lim)
323 {
324 l = (struct router_lsa_link *) p;
325
0c0f9cd5 326 p += (ROUTER_LSA_MIN_SIZE + (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
718e3744 327
328 if (l->m[0].type == LSA_LINK_TYPE_STUB)
329 continue;
330
331 /* Defer NH calculation via VLs until summaries from
332 transit areas area confidered */
333
334 if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK)
0c0f9cd5 335 continue;
718e3744 336
337 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
0c0f9cd5 338 return l;
718e3744 339 }
340
341 return NULL;
342}
343
75ee0b8e 344/*
345 * Consider supplied next-hop for inclusion to the supplied list of
346 * equal-cost next-hops, adjust list as neccessary.
347 *
348 * (Discussed on GNU Zebra list 27 May 2003, [zebra 19184])
349 *
350 * Note that below is a bit of a hack, and limits ECMP to paths that go to
351 * same nexthop. Where as paths via inequal output_cost interfaces could
352 * still quite easily be ECMP due to remote cost differences.
353 *
354 * TODO: It really should be done by way of recording currently valid
355 * backlinks and determining the appropriate nexthops from the list of
356 * backlinks, or even simpler, just flushing nexthop list if we find a lower
357 * cost path to a candidate vertex in SPF, maybe.
bf9392c6 358 */
0c0f9cd5 359void
360ospf_spf_consider_nexthop (struct list *nexthops,
361 struct vertex_nexthop *newhop)
bf9392c6 362{
bf9392c6 363 struct vertex_nexthop *hop;
75ee0b8e 364 struct listnode *ln, *nn;
0c0f9cd5 365
75ee0b8e 366 /* nexthop list should contain only the set of nexthops that have the lowest
367 * equal cost
368 */
369 if (nexthops->head != NULL)
370 {
371 hop = getdata (nexthops->head);
372
373 /* weed out hops with higher cost than the newhop */
374 if (hop->oi->output_cost > newhop->oi->output_cost)
375 {
376 /* delete the existing nexthops */
377 for (ln = nexthops->head; ln; ln = nn)
378 {
379 nn = ln->next;
380 hop = getdata (ln);
381
382 listnode_delete (nexthops, hop);
383 vertex_nexthop_free (hop);
384 }
385 }
386 else if (hop->oi->output_cost < newhop->oi->output_cost)
0c0f9cd5 387 return;
75ee0b8e 388 }
0c0f9cd5 389
bf9392c6 390 /* new hop is <= existing hops, add it */
0c0f9cd5 391 listnode_add (nexthops, newhop);
bf9392c6 392
393 return;
394}
395
718e3744 396/* Calculate nexthop from root to vertex W. */
397void
398ospf_nexthop_calculation (struct ospf_area *area,
399 struct vertex *v, struct vertex *w)
400{
401 listnode node;
402 struct vertex_nexthop *nh, *x;
403 struct ospf_interface *oi = NULL;
404 struct router_lsa_link *l = NULL;
0c0f9cd5 405
406
718e3744 407 if (IS_DEBUG_OSPF_EVENT)
408 zlog_info ("ospf_nexthop_calculation(): Start");
409
410 /* W's parent is root. */
411 if (v == area->spf)
412 {
413 if (w->type == OSPF_VERTEX_ROUTER)
0c0f9cd5 414 {
415 while ((l = ospf_get_next_link (v, w, l)))
416 {
417 struct router_lsa_link *l2 = NULL;
418
419 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT)
420 {
421 /* Check for PtMP, signified by PtP link V->W
422 with link_data our PtMP interface. */
68980084 423 oi = ospf_if_is_configured (area->ospf, &l->link_data);
7afa08da 424 if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
0c0f9cd5 425 {
426 struct prefix_ipv4 la;
d7d93997 427 la.family = AF_INET;
0c0f9cd5 428 la.prefixlen = oi->address->prefixlen;
429 /* We link to them on PtMP interface
430 - find the interface on w */
431 while ((l2 = ospf_get_next_link (w, v, l2)))
432 {
433 la.prefix = l2->link_data;
434
435 if (prefix_cmp ((struct prefix *) &la,
436 oi->address) == 0)
437 /* link_data is on our PtMP network */
438 break;
439 }
440 }
441 else
442 {
443 while ((l2 = ospf_get_next_link (w, v, l2)))
444 {
445 oi = ospf_if_is_configured (area->ospf,
446 &(l2->link_data));
447
448 if (oi == NULL)
449 continue;
450
451 if (!IPV4_ADDR_SAME (&oi->address->u.prefix4,
452 &l->link_data))
453 continue;
454
455 break;
456 }
457 }
458
459 if (oi && l2)
460 {
461 nh = vertex_nexthop_new (v);
462 nh->oi = oi;
463 nh->router = l2->link_data;
464 ospf_spf_consider_nexthop (w->nexthop, nh);
465 }
466 }
467 }
468 }
718e3744 469 else
0c0f9cd5 470 {
471 while ((l = ospf_get_next_link (v, w, l)))
472 {
473 oi = ospf_if_is_configured (area->ospf, &(l->link_data));
474 if (oi)
475 {
476 nh = vertex_nexthop_new (v);
477 nh->oi = oi;
478 nh->router.s_addr = 0;
75ee0b8e 479 ospf_spf_consider_nexthop (w->nexthop, nh);
0c0f9cd5 480 }
481 }
482 }
718e3744 483 return;
484 }
485 /* In case of W's parent is network connected to root. */
486 else if (v->type == OSPF_VERTEX_NETWORK)
487 {
488 for (node = listhead (v->nexthop); node; nextnode (node))
489 {
490 x = (struct vertex_nexthop *) getdata (node);
491 if (x->parent == area->spf)
492 {
0c0f9cd5 493 while ((l = ospf_get_next_link (w, v, l)))
494 {
495 nh = vertex_nexthop_new (v);
496 nh->oi = x->oi;
497 nh->router = l->link_data;
75ee0b8e 498 ospf_spf_consider_nexthop (w->nexthop, nh);
0c0f9cd5 499 }
500 return;
501 }
718e3744 502 }
503 }
504
505 /* Inherit V's nexthop. */
506 for (node = listhead (v->nexthop); node; nextnode (node))
507 {
508 nh = vertex_nexthop_dup (node->data);
509 nh->parent = v;
510 ospf_nexthop_add_unique (nh, w->nexthop);
511 }
512}
513
514void
515ospf_install_candidate (list candidate, struct vertex *w)
516{
517 listnode node;
518 struct vertex *cw;
519
520 if (list_isempty (candidate))
521 {
522 listnode_add (candidate, w);
523 return;
524 }
525
526 /* Install vertex with sorting by distance. */
527 for (node = listhead (candidate); node; nextnode (node))
528 {
529 cw = (struct vertex *) getdata (node);
530 if (cw->distance > w->distance)
531 {
532 list_add_node_prev (candidate, node, w);
533 break;
534 }
535 else if (node->next == NULL)
536 {
537 list_add_node_next (candidate, node, w);
538 break;
539 }
540 }
541}
542
543/* RFC2328 Section 16.1 (2). */
544void
545ospf_spf_next (struct vertex *v, struct ospf_area *area,
0c0f9cd5 546 list candidate, struct route_table *rv, struct route_table *nv)
718e3744 547{
548 struct ospf_lsa *w_lsa = NULL;
549 struct vertex *w, *cw;
550 u_char *p;
551 u_char *lim;
552 struct router_lsa_link *l = NULL;
553 struct in_addr *r;
554 listnode node;
555 int type = 0;
556
557 /* If this is a router-LSA, and bit V of the router-LSA (see Section
558 A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */
559 if (v->type == OSPF_VERTEX_ROUTER)
560 {
561 if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa))
562 area->transit = OSPF_TRANSIT_TRUE;
563 }
564
565 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
0c0f9cd5 566 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
567
718e3744 568 while (p < lim)
569 {
d355bfa7 570 int link = -1; /* link index for w's back link */
571
718e3744 572 /* In case of V is Router-LSA. */
573 if (v->lsa->type == OSPF_ROUTER_LSA)
574 {
575 l = (struct router_lsa_link *) p;
576
0c0f9cd5 577 p += (ROUTER_LSA_MIN_SIZE +
718e3744 578 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
579
580 /* (a) If this is a link to a stub network, examine the next
581 link in V's LSA. Links to stub networks will be
582 considered in the second stage of the shortest path
583 calculation. */
584 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
585 continue;
586
587 /* (b) Otherwise, W is a transit vertex (router or transit
588 network). Look up the vertex W's LSA (router-LSA or
589 network-LSA) in Area A's link state database. */
590 switch (type)
591 {
592 case LSA_LINK_TYPE_POINTOPOINT:
593 case LSA_LINK_TYPE_VIRTUALLINK:
594 if (type == LSA_LINK_TYPE_VIRTUALLINK)
0c0f9cd5 595 {
596 if (IS_DEBUG_OSPF_EVENT)
597 zlog_info ("looking up LSA through VL: %s",
598 inet_ntoa (l->link_id));
599 }
718e3744 600
601 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
602 l->link_id);
603 if (w_lsa)
0c0f9cd5 604 {
605 if (IS_DEBUG_OSPF_EVENT)
606 zlog_info ("found the LSA");
607 }
718e3744 608 break;
609 case LSA_LINK_TYPE_TRANSIT:
0c0f9cd5 610 if (IS_DEBUG_OSPF_EVENT)
718e3744 611
0c0f9cd5 612 zlog_info ("Looking up Network LSA, ID: %s",
613 inet_ntoa (l->link_id));
718e3744 614 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
0c0f9cd5 615 l->link_id);
718e3744 616 if (w_lsa)
0c0f9cd5 617 if (IS_DEBUG_OSPF_EVENT)
618 zlog_info ("found the LSA");
718e3744 619 break;
620 default:
0c0f9cd5 621 zlog_warn ("Invalid LSA link type %d", type);
718e3744 622 continue;
623 }
624 }
625 else
626 {
627 /* In case of V is Network-LSA. */
0c0f9cd5 628 r = (struct in_addr *) p;
718e3744 629 p += sizeof (struct in_addr);
630
631 /* Lookup the vertex W's LSA. */
632 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
633 }
634
635 /* (b cont.) If the LSA does not exist, or its LS age is equal
636 to MaxAge, or it does not have a link back to vertex V,
637 examine the next link in V's LSA.[23] */
638 if (w_lsa == NULL)
639 continue;
640
641 if (IS_LSA_MAXAGE (w_lsa))
642 continue;
643
d355bfa7 644 if ( (link = ospf_lsa_has_link (w_lsa->data, v->lsa)) < 0 )
718e3744 645 {
0c0f9cd5 646 if (IS_DEBUG_OSPF_EVENT)
647 zlog_info ("The LSA doesn't have a link back");
718e3744 648 continue;
649 }
650
651 /* (c) If vertex W is already on the shortest-path tree, examine
652 the next link in the LSA. */
653 if (ospf_spf_has_vertex (rv, nv, w_lsa->data))
654 {
0c0f9cd5 655 if (IS_DEBUG_OSPF_EVENT)
656 zlog_info ("The LSA is already in SPF");
718e3744 657 continue;
658 }
659
660 /* (d) Calculate the link state cost D of the resulting path
661 from the root to vertex W. D is equal to the sum of the link
662 state cost of the (already calculated) shortest path to
663 vertex V and the advertised cost of the link between vertices
664 V and W. If D is: */
665
666 /* prepare vertex W. */
667 w = ospf_vertex_new (w_lsa);
668
d355bfa7 669 /* Save W's back link index number, for use by virtual links */
670 w->backlink = link;
671
718e3744 672 /* calculate link cost D. */
673 if (v->lsa->type == OSPF_ROUTER_LSA)
674 w->distance = v->distance + ntohs (l->m[0].metric);
675 else
676 w->distance = v->distance;
677
678 /* Is there already vertex W in candidate list? */
679 node = ospf_vertex_lookup (candidate, w->id, w->type);
680 if (node == NULL)
681 {
682 /* Calculate nexthop to W. */
683 ospf_nexthop_calculation (area, v, w);
684
685 ospf_install_candidate (candidate, w);
686 }
687 else
688 {
689 cw = (struct vertex *) getdata (node);
690
691 /* if D is greater than. */
692 if (cw->distance < w->distance)
693 {
694 ospf_vertex_free (w);
695 continue;
696 }
697 /* equal to. */
698 else if (cw->distance == w->distance)
699 {
700 /* Calculate nexthop to W. */
701 ospf_nexthop_calculation (area, v, w);
702 ospf_nexthop_merge (cw->nexthop, w->nexthop);
703 list_delete_all_node (w->nexthop);
704 ospf_vertex_free (w);
705 }
706 /* less than. */
707 else
708 {
709 /* Calculate nexthop. */
710 ospf_nexthop_calculation (area, v, w);
711
712 /* Remove old vertex from candidate list. */
713 ospf_vertex_free (cw);
714 listnode_delete (candidate, cw);
715
716 /* Install new to candidate. */
717 ospf_install_candidate (candidate, w);
718 }
719 }
720 }
721}
722
723/* Add vertex V to SPF tree. */
724void
725ospf_spf_register (struct vertex *v, struct route_table *rv,
0c0f9cd5 726 struct route_table *nv)
718e3744 727{
728 struct prefix p;
729 struct route_node *rn;
730
731 p.family = AF_INET;
732 p.prefixlen = IPV4_MAX_BITLEN;
733 p.u.prefix4 = v->id;
734
735 if (v->type == OSPF_VERTEX_ROUTER)
736 rn = route_node_get (rv, &p);
737 else
738 rn = route_node_get (nv, &p);
739
740 rn->info = v;
741}
742
743void
744ospf_spf_route_free (struct route_table *table)
745{
746 struct route_node *rn;
747 struct vertex *v;
748
749 for (rn = route_top (table); rn; rn = route_next (rn))
750 {
751 if ((v = rn->info))
0c0f9cd5 752 {
753 ospf_vertex_free (v);
754 rn->info = NULL;
755 }
718e3744 756
757 route_unlock_node (rn);
758 }
759
760 route_table_finish (table);
761}
762
763void
764ospf_spf_dump (struct vertex *v, int i)
765{
766 listnode cnode;
767 listnode nnode;
768 struct vertex_nexthop *nexthop;
769
770 if (v->type == OSPF_VERTEX_ROUTER)
771 {
772 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 773 zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
718e3744 774 }
775 else
776 {
777 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
778 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 779 zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
780 ip_masklen (lsa->mask));
718e3744 781
782 for (nnode = listhead (v->nexthop); nnode; nextnode (nnode))
783 {
784 nexthop = getdata (nnode);
0c0f9cd5 785 if (IS_DEBUG_OSPF_EVENT)
786 zlog_info (" nexthop %s", inet_ntoa (nexthop->router));
718e3744 787 }
788 }
789
790 i++;
791
792 for (cnode = listhead (v->child); cnode; nextnode (cnode))
793 {
794 v = getdata (cnode);
795 ospf_spf_dump (v, i);
796 }
797}
798
799/* Second stage of SPF calculation. */
800void
0c0f9cd5 801ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v,
718e3744 802 struct route_table *rt)
803{
804 listnode cnode;
805 struct vertex *child;
806
807 if (IS_DEBUG_OSPF_EVENT)
808 zlog_info ("ospf_process_stub():processing stubs for area %s",
0c0f9cd5 809 inet_ntoa (area->area_id));
718e3744 810 if (v->type == OSPF_VERTEX_ROUTER)
811 {
812 u_char *p;
813 u_char *lim;
814 struct router_lsa_link *l;
815 struct router_lsa *rlsa;
816
0c0f9cd5 817 if (IS_DEBUG_OSPF_EVENT)
818 zlog_info ("ospf_process_stub():processing router LSA, id: %s",
819 inet_ntoa (v->lsa->id));
718e3744 820 rlsa = (struct router_lsa *) v->lsa;
821
822
0c0f9cd5 823 if (IS_DEBUG_OSPF_EVENT)
824 zlog_info ("ospf_process_stub(): we have %d links to process",
825 ntohs (rlsa->links));
718e3744 826 p = ((u_char *) v->lsa) + 24;
827 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
828
829 while (p < lim)
830 {
831 l = (struct router_lsa_link *) p;
832
833 p += (ROUTER_LSA_MIN_SIZE +
834 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
835
836 if (l->m[0].type == LSA_LINK_TYPE_STUB)
837 ospf_intra_add_stub (rt, l, v, area);
838 }
839 }
840
841 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 842 zlog_info ("children of V:");
718e3744 843 for (cnode = listhead (v->child); cnode; nextnode (cnode))
844 {
845 child = getdata (cnode);
0c0f9cd5 846 if (IS_DEBUG_OSPF_EVENT)
847 zlog_info (" child : %s", inet_ntoa (child->id));
718e3744 848 }
849
850 for (cnode = listhead (v->child); cnode; nextnode (cnode))
851 {
852 child = getdata (cnode);
853
854 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
0c0f9cd5 855 continue;
718e3744 856
857 ospf_spf_process_stubs (area, child, rt);
858
859 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
860 }
861}
862
863void
864ospf_rtrs_free (struct route_table *rtrs)
865{
866 struct route_node *rn;
867 list or_list;
868 listnode node;
869
870 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 871 zlog_info ("Route: Router Routing Table free");
718e3744 872
873 for (rn = route_top (rtrs); rn; rn = route_next (rn))
874 if ((or_list = rn->info) != NULL)
875 {
0c0f9cd5 876 for (node = listhead (or_list); node; nextnode (node))
877 ospf_route_free (node->data);
718e3744 878
0c0f9cd5 879 list_delete (or_list);
718e3744 880
0c0f9cd5 881 /* Unlock the node. */
882 rn->info = NULL;
883 route_unlock_node (rn);
718e3744 884 }
885 route_table_finish (rtrs);
886}
887
888void
889ospf_rtrs_print (struct route_table *rtrs)
890{
891 struct route_node *rn;
892 list or_list;
893 listnode ln;
894 listnode pnode;
895 struct ospf_route *or;
896 struct ospf_path *path;
897 char buf1[BUFSIZ];
898 char buf2[BUFSIZ];
899
900 if (IS_DEBUG_OSPF_EVENT)
901 zlog_info ("ospf_rtrs_print() start");
902
903 for (rn = route_top (rtrs); rn; rn = route_next (rn))
904 if ((or_list = rn->info) != NULL)
905 for (ln = listhead (or_list); ln; nextnode (ln))
906 {
907 or = getdata (ln);
908
909 switch (or->path_type)
910 {
911 case OSPF_PATH_INTRA_AREA:
0c0f9cd5 912 if (IS_DEBUG_OSPF_EVENT)
913 zlog_info ("%s [%d] area: %s",
914 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
915 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
916 buf2, BUFSIZ));
718e3744 917 break;
918 case OSPF_PATH_INTER_AREA:
0c0f9cd5 919 if (IS_DEBUG_OSPF_EVENT)
920 zlog_info ("%s IA [%d] area: %s",
921 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
922 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
923 buf2, BUFSIZ));
718e3744 924 break;
925 default:
926 break;
927 }
928
96735eea 929 for (pnode = listhead (or->paths); pnode; nextnode (pnode))
718e3744 930 {
931 path = getdata (pnode);
932 if (path->nexthop.s_addr == 0)
0c0f9cd5 933 {
934 if (IS_DEBUG_OSPF_EVENT)
935 zlog_info (" directly attached to %s\r\n",
936 IF_NAME (path->oi));
937 }
938 else
939 {
940 if (IS_DEBUG_OSPF_EVENT)
941 zlog_info (" via %s, %s\r\n",
942 inet_ntoa (path->nexthop), IF_NAME (path->oi));
943 }
718e3744 944 }
945 }
946
947 zlog_info ("ospf_rtrs_print() end");
948}
949
950/* Calculating the shortest-path tree for an area. */
951void
0c0f9cd5 952ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
718e3744 953 struct route_table *new_rtrs)
954{
955 list candidate;
956 listnode node;
957 struct vertex *v;
958 struct route_table *rv;
959 struct route_table *nv;
960
961 if (IS_DEBUG_OSPF_EVENT)
962 {
963 zlog_info ("ospf_spf_calculate: Start");
0c0f9cd5 964 zlog_info ("ospf_spf_calculate: running Dijkstra for area %s",
965 inet_ntoa (area->area_id));
718e3744 966 }
967
968 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
969 return this area's calculation. */
0c0f9cd5 970 if (!area->router_lsa_self)
718e3744 971 {
972 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 973 zlog_info ("ospf_spf_calculate: "
974 "Skip area %s's calculation due to empty router_lsa_self",
975 inet_ntoa (area->area_id));
718e3744 976 return;
977 }
978
979 /* RFC2328 16.1. (1). */
0c0f9cd5 980 /* Initialize the algorithm's data structures. */
718e3744 981 rv = route_table_init ();
982 nv = route_table_init ();
983
0c0f9cd5 984 /* Clear the list of candidate vertices. */
718e3744 985 candidate = list_new ();
986
987 /* Initialize the shortest-path tree to only the root (which is the
988 router doing the calculation). */
989 ospf_spf_init (area);
990 v = area->spf;
991 ospf_spf_register (v, rv, nv);
992
993 /* Set Area A's TransitCapability to FALSE. */
994 area->transit = OSPF_TRANSIT_FALSE;
995 area->shortcut_capability = 1;
996
997 for (;;)
998 {
999 /* RFC2328 16.1. (2). */
1000 ospf_spf_next (v, area, candidate, rv, nv);
1001
1002 /* RFC2328 16.1. (3). */
1003 /* If at this step the candidate list is empty, the shortest-
1004 path tree (of transit vertices) has been completely built and
1005 this stage of the procedure terminates. */
1006 if (listcount (candidate) == 0)
1007 break;
1008
1009 /* Otherwise, choose the vertex belonging to the candidate list
1010 that is closest to the root, and add it to the shortest-path
1011 tree (removing it from the candidate list in the
0c0f9cd5 1012 process). */
718e3744 1013 node = listhead (candidate);
1014 v = getdata (node);
1015 ospf_vertex_add_parent (v);
1016
1017 /* Reveve from the candidate list. */
1018 listnode_delete (candidate, v);
1019
1020 /* Add to SPF tree. */
1021 ospf_spf_register (v, rv, nv);
1022
1023 /* Note that when there is a choice of vertices closest to the
1024 root, network vertices must be chosen before router vertices
1025 in order to necessarily find all equal-cost paths. */
1026 /* We don't do this at this moment, we should add the treatment
1027 above codes. -- kunihiro. */
1028
1029 /* RFC2328 16.1. (4). */
1030 if (v->type == OSPF_VERTEX_ROUTER)
1031 ospf_intra_add_router (new_rtrs, v, area);
0c0f9cd5 1032 else
718e3744 1033 ospf_intra_add_transit (new_table, v, area);
1034
1035 /* RFC2328 16.1. (5). */
1036 /* Iterate the algorithm by returning to Step 2. */
1037 }
1038
1039 if (IS_DEBUG_OSPF_EVENT)
1040 {
1041 ospf_spf_dump (area->spf, 0);
1042 ospf_route_table_dump (new_table);
1043 }
1044
1045 /* Second stage of SPF calculation procedure's */
1046 ospf_spf_process_stubs (area, area->spf, new_table);
1047
1048 /* Free all vertices which allocated for SPF calculation */
1049 ospf_spf_route_free (rv);
1050 ospf_spf_route_free (nv);
1051
1052 /* Free candidate list */
1053 list_free (candidate);
1054
1055 /* Increment SPF Calculation Counter. */
1056 area->spf_calculation++;
1057
68980084 1058 area->ospf->ts_spf = time (NULL);
718e3744 1059
1060 if (IS_DEBUG_OSPF_EVENT)
1061 zlog_info ("ospf_spf_calculate: Stop");
1062}
1063\f
1064/* Timer for SPF calculation. */
1065int
68980084 1066ospf_spf_calculate_timer (struct thread *thread)
718e3744 1067{
68980084 1068 struct ospf *ospf = THREAD_ARG (thread);
718e3744 1069 struct route_table *new_table, *new_rtrs;
718e3744 1070 listnode node;
1071
1072 if (IS_DEBUG_OSPF_EVENT)
1073 zlog_info ("SPF: Timer (SPF calculation expire)");
0c0f9cd5 1074
718e3744 1075 ospf->t_spf_calc = NULL;
1076
1077 /* Allocate new table tree. */
1078 new_table = route_table_init ();
0c0f9cd5 1079 new_rtrs = route_table_init ();
718e3744 1080
68980084 1081 ospf_vl_unapprove (ospf);
718e3744 1082
1083 /* Calculate SPF for each area. */
1084 for (node = listhead (ospf->areas); node; node = nextnode (node))
1085 ospf_spf_calculate (node->data, new_table, new_rtrs);
1086
68980084 1087 ospf_vl_shut_unapproved (ospf);
718e3744 1088
68980084 1089 ospf_ia_routing (ospf, new_table, new_rtrs);
718e3744 1090
1091 ospf_prune_unreachable_networks (new_table);
1092 ospf_prune_unreachable_routers (new_rtrs);
1093
1094 /* AS-external-LSA calculation should not be performed here. */
1095
1096 /* If new Router Route is installed,
1097 then schedule re-calculate External routes. */
1098 if (1)
68980084 1099 ospf_ase_calculate_schedule (ospf);
718e3744 1100
68980084 1101 ospf_ase_calculate_timer_add (ospf);
718e3744 1102
1103 /* Update routing table. */
68980084 1104 ospf_route_install (ospf, new_table);
718e3744 1105
1106 /* Update ABR/ASBR routing table */
68980084 1107 if (ospf->old_rtrs)
718e3744 1108 {
1109 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
68980084 1110 /* ospf_route_delete (ospf->old_rtrs); */
1111 ospf_rtrs_free (ospf->old_rtrs);
718e3744 1112 }
1113
68980084 1114 ospf->old_rtrs = ospf->new_rtrs;
1115 ospf->new_rtrs = new_rtrs;
718e3744 1116
0c0f9cd5 1117 if (IS_OSPF_ABR (ospf))
68980084 1118 ospf_abr_task (ospf);
718e3744 1119
1120 if (IS_DEBUG_OSPF_EVENT)
1121 zlog_info ("SPF: calculation complete");
1122
1123 return 0;
1124}
1125
1126/* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
1127 set timer for SPF calc. */
1128void
68980084 1129ospf_spf_calculate_schedule (struct ospf *ospf)
718e3744 1130{
1131 time_t ht, delay;
1132
1133 if (IS_DEBUG_OSPF_EVENT)
1134 zlog_info ("SPF: calculation timer scheduled");
1135
1136 /* OSPF instance does not exist. */
68980084 1137 if (ospf == NULL)
718e3744 1138 return;
1139
1140 /* SPF calculation timer is already scheduled. */
68980084 1141 if (ospf->t_spf_calc)
718e3744 1142 {
1143 if (IS_DEBUG_OSPF_EVENT)
0c0f9cd5 1144 zlog_info ("SPF: calculation timer is already scheduled: %p",
1145 ospf->t_spf_calc);
718e3744 1146 return;
1147 }
1148
68980084 1149 ht = time (NULL) - ospf->ts_spf;
718e3744 1150
1151 /* Get SPF calculation delay time. */
68980084 1152 if (ht < ospf->spf_holdtime)
718e3744 1153 {
68980084 1154 if (ospf->spf_holdtime - ht < ospf->spf_delay)
0c0f9cd5 1155 delay = ospf->spf_delay;
718e3744 1156 else
0c0f9cd5 1157 delay = ospf->spf_holdtime - ht;
718e3744 1158 }
1159 else
68980084 1160 delay = ospf->spf_delay;
718e3744 1161
1162 if (IS_DEBUG_OSPF_EVENT)
fa2b17e3 1163 zlog_info ("SPF: calculation timer delay = %ld", (long)delay);
68980084 1164 ospf->t_spf_calc =
1165 thread_add_timer (master, ospf_spf_calculate_timer, ospf, delay);
718e3744 1166}