blob: 33616d8375e36035778bfbf97df0996da3d4664a [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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}
79
80
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 ();
96
97 return new;
98}
99
100void
101ospf_vertex_free (struct vertex *v)
102{
103 listnode node;
104
105 list_delete (v->child);
106
107 if (listcount (v->nexthop) > 0)
108 for (node = listhead (v->nexthop); node; nextnode (node))
109 vertex_nexthop_free (node->data);
110
111 list_delete (v->nexthop);
112
113 XFREE (MTYPE_OSPF_VERTEX, v);
114}
115
116void
117ospf_vertex_add_parent (struct vertex *v)
118{
119 struct vertex_nexthop *nh;
120 listnode node;
121
122 for (node = listhead (v->nexthop); node; nextnode (node))
123 {
124 nh = (struct vertex_nexthop *) getdata (node);
125
126 /* No need to add two links from the same parent. */
127 if (listnode_lookup (nh->parent->child, v) == NULL)
128 listnode_add (nh->parent->child, v);
129 }
130}
131
132void
133ospf_spf_init (struct ospf_area *area)
134{
135 struct vertex *v;
136
137 /* Create root node. */
138 v = ospf_vertex_new (area->router_lsa_self);
139
140 area->spf = v;
141
142 /* Reset ABR and ASBR router counts. */
143 area->abr_count = 0;
144 area->asbr_count = 0;
145}
146
147int
148ospf_spf_has_vertex (struct route_table *rv, struct route_table *nv,
149 struct lsa_header *lsa)
150{
151 struct prefix p;
152 struct route_node *rn;
153
154 p.family = AF_INET;
155 p.prefixlen = IPV4_MAX_BITLEN;
156 p.u.prefix4 = lsa->id;
157
158 if (lsa->type == OSPF_ROUTER_LSA)
159 rn = route_node_get (rv, &p);
160 else
161 rn = route_node_get (nv, &p);
162
163 if (rn->info != NULL)
164 {
165 route_unlock_node (rn);
166 return 1;
167 }
168 return 0;
169}
170
171listnode
172ospf_vertex_lookup (list vlist, struct in_addr id, int type)
173{
174 listnode node;
175 struct vertex *v;
176
177 for (node = listhead (vlist); node; nextnode (node))
178 {
179 v = (struct vertex *) getdata (node);
180 if (IPV4_ADDR_SAME (&id, &v->id) && type == v->type)
181 return node;
182 }
183
184 return NULL;
185}
186
187int
188ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v)
189{
190 int i;
191 int length;
192 struct router_lsa *rl;
193 struct network_lsa *nl;
194
195 /* In case of W is Network LSA. */
196 if (w->type == OSPF_NETWORK_LSA)
197 {
198 if (v->type == OSPF_NETWORK_LSA)
199 return 0;
200
201 nl = (struct network_lsa *) w;
202 length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
203
204 for (i = 0; i < length; i++)
205 if (IPV4_ADDR_SAME (&nl->routers[i], &v->id))
206 return 1;
207 return 0;
208 }
209
210 /* In case of W is Router LSA. */
211 if (w->type == OSPF_ROUTER_LSA)
212 {
213 rl = (struct router_lsa *) w;
214
215 length = ntohs (w->length);
216
217 for (i = 0;
218 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
219 i++, length -= 12)
220 {
221 switch (rl->link[i].type)
222 {
223 case LSA_LINK_TYPE_POINTOPOINT:
224 case LSA_LINK_TYPE_VIRTUALLINK:
225 /* Router LSA ID. */
226 if (v->type == OSPF_ROUTER_LSA &&
227 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
228 {
229 return 1;
230 }
231 break;
232 case LSA_LINK_TYPE_TRANSIT:
233 /* Network LSA ID. */
234 if (v->type == OSPF_NETWORK_LSA &&
235 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
236 {
237 return 1;
238 }
239 break;
240 case LSA_LINK_TYPE_STUB:
241 /* Not take into count? */
242 continue;
243 default:
244 break;
245 }
246 }
247 }
248 return 0;
249}
250
251/* Add the nexthop to the list, only if it is unique.
252 * If it's not unique, free the nexthop entry.
253 */
254void
255ospf_nexthop_add_unique (struct vertex_nexthop *new, list nexthop)
256{
257 struct vertex_nexthop *nh;
258 listnode node;
259 int match;
260
261 match = 0;
262 for (node = listhead (nexthop); node; nextnode (node))
263 {
264 nh = node->data;
265
266 /* Compare the two entries. */
267 /* XXX
268 * Comparing the parent preserves the shortest path tree
269 * structure even when the nexthops are identical.
270 */
271 if (nh->oi == new->oi &&
272 IPV4_ADDR_SAME (&nh->router, &new->router) &&
273 nh->parent == new->parent)
274 {
275 match = 1;
276 break;
277 }
278 }
279
280 if (!match)
281 listnode_add (nexthop, new);
282 else
283 vertex_nexthop_free (new);
284}
285
286/* Merge entries in list b into list a. */
287void
288ospf_nexthop_merge (list a, list b)
289{
290 struct listnode *n;
291
292 for (n = listhead (b); n; nextnode (n))
293 {
294 ospf_nexthop_add_unique (n->data, a);
295 }
296}
297
298#define ROUTER_LSA_MIN_SIZE 12
299#define ROUTER_LSA_TOS_SIZE 4
300
301struct router_lsa_link *
302ospf_get_next_link (struct vertex *v, struct vertex *w,
303 struct router_lsa_link *prev_link)
304{
305 u_char *p;
306 u_char *lim;
307 struct router_lsa_link *l;
308
309 if (prev_link == NULL)
310 p = ((u_char *) v->lsa) + 24;
311 else
312 {
313 p = (u_char *)prev_link;
314 p += (ROUTER_LSA_MIN_SIZE +
315 (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
316 }
317
318 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
319
320 while (p < lim)
321 {
322 l = (struct router_lsa_link *) p;
323
324 p += (ROUTER_LSA_MIN_SIZE +
325 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
326
327 if (l->m[0].type == LSA_LINK_TYPE_STUB)
328 continue;
329
330 /* Defer NH calculation via VLs until summaries from
331 transit areas area confidered */
332
333 if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK)
334 continue;
335
336 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
337 return l;
338 }
339
340 return NULL;
341}
342
343/* Calculate nexthop from root to vertex W. */
344void
345ospf_nexthop_calculation (struct ospf_area *area,
346 struct vertex *v, struct vertex *w)
347{
348 listnode node;
349 struct vertex_nexthop *nh, *x;
350 struct ospf_interface *oi = NULL;
351 struct router_lsa_link *l = NULL;
352
353
354 if (IS_DEBUG_OSPF_EVENT)
355 zlog_info ("ospf_nexthop_calculation(): Start");
356
357 /* W's parent is root. */
358 if (v == area->spf)
359 {
360 if (w->type == OSPF_VERTEX_ROUTER)
361 {
362 while ((l = ospf_get_next_link (v, w, l)))
363 {
364 struct router_lsa_link *l2 = NULL;
365
366 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT)
367 {
paul68980082003-03-25 05:07:42 +0000368 /* Check for PtMP, signified by PtP link V->W
369 with link_data our PtMP interface. */
370 oi = ospf_if_is_configured (area->ospf, &l->link_data);
paul7afa08d2002-12-13 20:59:45 +0000371 if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
paul718e3742002-12-13 20:15:29 +0000372 {
paul68980082003-03-25 05:07:42 +0000373 struct prefix_ipv4 la;
374 la.prefixlen = oi->address->prefixlen;
375 /* We link to them on PtMP interface
376 - find the interface on w */
paul7afa08d2002-12-13 20:59:45 +0000377 while ((l2 = ospf_get_next_link (w, v, l2)))
378 {
paul68980082003-03-25 05:07:42 +0000379 la.prefix = l2->link_data;
paul7afa08d2002-12-13 20:59:45 +0000380
paul68980082003-03-25 05:07:42 +0000381 if (prefix_cmp ((struct prefix *)&la,
382 oi->address) == 0)
paul7afa08d2002-12-13 20:59:45 +0000383 /* link_data is on our PtMP network */
384 break;
paul7afa08d2002-12-13 20:59:45 +0000385 }
386 }
387 else
388 {
389 while ((l2 = ospf_get_next_link (w, v, l2)))
390 {
paul68980082003-03-25 05:07:42 +0000391 oi = ospf_if_is_configured (area->ospf,
392 &(l2->link_data));
paul7afa08d2002-12-13 20:59:45 +0000393
394 if (oi == NULL)
395 continue;
396
paul68980082003-03-25 05:07:42 +0000397 if (!IPV4_ADDR_SAME (&oi->address->u.prefix4,
398 &l->link_data))
paul7afa08d2002-12-13 20:59:45 +0000399 continue;
400
401 break;
paul68980082003-03-25 05:07:42 +0000402 }
paul718e3742002-12-13 20:15:29 +0000403 }
404
405 if (oi && l2)
406 {
407 nh = vertex_nexthop_new (v);
408 nh->oi = oi;
409 nh->router = l2->link_data;
410 listnode_add (w->nexthop, nh);
411 }
412 }
413 }
414 }
415 else
416 {
417 while ((l = ospf_get_next_link (v, w, l)))
418 {
paul68980082003-03-25 05:07:42 +0000419 oi = ospf_if_is_configured (area->ospf, &(l->link_data));
paul718e3742002-12-13 20:15:29 +0000420 if (oi)
421 {
422 nh = vertex_nexthop_new (v);
423 nh->oi = oi;
424 nh->router.s_addr = 0;
425 listnode_add (w->nexthop, nh);
426 }
427 }
428 }
429 return;
430 }
431 /* In case of W's parent is network connected to root. */
432 else if (v->type == OSPF_VERTEX_NETWORK)
433 {
434 for (node = listhead (v->nexthop); node; nextnode (node))
435 {
436 x = (struct vertex_nexthop *) getdata (node);
437 if (x->parent == area->spf)
438 {
439 while ((l = ospf_get_next_link (w, v, l)))
440 {
441 nh = vertex_nexthop_new (v);
442 nh->oi = x->oi;
443 nh->router = l->link_data;
444 listnode_add (w->nexthop, nh);
445 }
446 return;
447 }
448 }
449 }
450
451 /* Inherit V's nexthop. */
452 for (node = listhead (v->nexthop); node; nextnode (node))
453 {
454 nh = vertex_nexthop_dup (node->data);
455 nh->parent = v;
456 ospf_nexthop_add_unique (nh, w->nexthop);
457 }
458}
459
460void
461ospf_install_candidate (list candidate, struct vertex *w)
462{
463 listnode node;
464 struct vertex *cw;
465
466 if (list_isempty (candidate))
467 {
468 listnode_add (candidate, w);
469 return;
470 }
471
472 /* Install vertex with sorting by distance. */
473 for (node = listhead (candidate); node; nextnode (node))
474 {
475 cw = (struct vertex *) getdata (node);
476 if (cw->distance > w->distance)
477 {
478 list_add_node_prev (candidate, node, w);
479 break;
480 }
481 else if (node->next == NULL)
482 {
483 list_add_node_next (candidate, node, w);
484 break;
485 }
486 }
487}
488
489/* RFC2328 Section 16.1 (2). */
490void
491ospf_spf_next (struct vertex *v, struct ospf_area *area,
492 list candidate, struct route_table *rv,
493 struct route_table *nv)
494{
495 struct ospf_lsa *w_lsa = NULL;
496 struct vertex *w, *cw;
497 u_char *p;
498 u_char *lim;
499 struct router_lsa_link *l = NULL;
500 struct in_addr *r;
501 listnode node;
502 int type = 0;
503
504 /* If this is a router-LSA, and bit V of the router-LSA (see Section
505 A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */
506 if (v->type == OSPF_VERTEX_ROUTER)
507 {
508 if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa))
509 area->transit = OSPF_TRANSIT_TRUE;
510 }
511
512 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
513 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
514
515 while (p < lim)
516 {
517 /* In case of V is Router-LSA. */
518 if (v->lsa->type == OSPF_ROUTER_LSA)
519 {
520 l = (struct router_lsa_link *) p;
521
522 p += (ROUTER_LSA_MIN_SIZE +
523 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
524
525 /* (a) If this is a link to a stub network, examine the next
526 link in V's LSA. Links to stub networks will be
527 considered in the second stage of the shortest path
528 calculation. */
529 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
530 continue;
531
532 /* (b) Otherwise, W is a transit vertex (router or transit
533 network). Look up the vertex W's LSA (router-LSA or
534 network-LSA) in Area A's link state database. */
535 switch (type)
536 {
537 case LSA_LINK_TYPE_POINTOPOINT:
538 case LSA_LINK_TYPE_VIRTUALLINK:
539 if (type == LSA_LINK_TYPE_VIRTUALLINK)
540 {
541 if (IS_DEBUG_OSPF_EVENT)
542 zlog_info ("looking up LSA through VL: %s",
543 inet_ntoa (l->link_id));
544 }
545
546 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
547 l->link_id);
548 if (w_lsa)
549 {
550 if (IS_DEBUG_OSPF_EVENT)
551 zlog_info("found the LSA");
552 }
553 break;
554 case LSA_LINK_TYPE_TRANSIT:
555 if (IS_DEBUG_OSPF_EVENT)
556
557 zlog_info ("Looking up Network LSA, ID: %s",
558 inet_ntoa(l->link_id));
559 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
560 l->link_id);
561 if (w_lsa)
562 if (IS_DEBUG_OSPF_EVENT)
563 zlog_info("found the LSA");
564 break;
565 default:
566 zlog_warn ("Invalid LSA link type %d", type);
567 continue;
568 }
569 }
570 else
571 {
572 /* In case of V is Network-LSA. */
573 r = (struct in_addr *) p ;
574 p += sizeof (struct in_addr);
575
576 /* Lookup the vertex W's LSA. */
577 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
578 }
579
580 /* (b cont.) If the LSA does not exist, or its LS age is equal
581 to MaxAge, or it does not have a link back to vertex V,
582 examine the next link in V's LSA.[23] */
583 if (w_lsa == NULL)
584 continue;
585
586 if (IS_LSA_MAXAGE (w_lsa))
587 continue;
588
589 if (! ospf_lsa_has_link (w_lsa->data, v->lsa))
590 {
591 if (IS_DEBUG_OSPF_EVENT)
592 zlog_info ("The LSA doesn't have a link back");
593 continue;
594 }
595
596 /* (c) If vertex W is already on the shortest-path tree, examine
597 the next link in the LSA. */
598 if (ospf_spf_has_vertex (rv, nv, w_lsa->data))
599 {
600 if (IS_DEBUG_OSPF_EVENT)
601 zlog_info ("The LSA is already in SPF");
602 continue;
603 }
604
605 /* (d) Calculate the link state cost D of the resulting path
606 from the root to vertex W. D is equal to the sum of the link
607 state cost of the (already calculated) shortest path to
608 vertex V and the advertised cost of the link between vertices
609 V and W. If D is: */
610
611 /* prepare vertex W. */
612 w = ospf_vertex_new (w_lsa);
613
614 /* calculate link cost D. */
615 if (v->lsa->type == OSPF_ROUTER_LSA)
616 w->distance = v->distance + ntohs (l->m[0].metric);
617 else
618 w->distance = v->distance;
619
620 /* Is there already vertex W in candidate list? */
621 node = ospf_vertex_lookup (candidate, w->id, w->type);
622 if (node == NULL)
623 {
624 /* Calculate nexthop to W. */
625 ospf_nexthop_calculation (area, v, w);
626
627 ospf_install_candidate (candidate, w);
628 }
629 else
630 {
631 cw = (struct vertex *) getdata (node);
632
633 /* if D is greater than. */
634 if (cw->distance < w->distance)
635 {
636 ospf_vertex_free (w);
637 continue;
638 }
639 /* equal to. */
640 else if (cw->distance == w->distance)
641 {
642 /* Calculate nexthop to W. */
643 ospf_nexthop_calculation (area, v, w);
644 ospf_nexthop_merge (cw->nexthop, w->nexthop);
645 list_delete_all_node (w->nexthop);
646 ospf_vertex_free (w);
647 }
648 /* less than. */
649 else
650 {
651 /* Calculate nexthop. */
652 ospf_nexthop_calculation (area, v, w);
653
654 /* Remove old vertex from candidate list. */
655 ospf_vertex_free (cw);
656 listnode_delete (candidate, cw);
657
658 /* Install new to candidate. */
659 ospf_install_candidate (candidate, w);
660 }
661 }
662 }
663}
664
665/* Add vertex V to SPF tree. */
666void
667ospf_spf_register (struct vertex *v, struct route_table *rv,
668 struct route_table *nv)
669{
670 struct prefix p;
671 struct route_node *rn;
672
673 p.family = AF_INET;
674 p.prefixlen = IPV4_MAX_BITLEN;
675 p.u.prefix4 = v->id;
676
677 if (v->type == OSPF_VERTEX_ROUTER)
678 rn = route_node_get (rv, &p);
679 else
680 rn = route_node_get (nv, &p);
681
682 rn->info = v;
683}
684
685void
686ospf_spf_route_free (struct route_table *table)
687{
688 struct route_node *rn;
689 struct vertex *v;
690
691 for (rn = route_top (table); rn; rn = route_next (rn))
692 {
693 if ((v = rn->info))
694 {
695 ospf_vertex_free (v);
696 rn->info = NULL;
697 }
698
699 route_unlock_node (rn);
700 }
701
702 route_table_finish (table);
703}
704
705void
706ospf_spf_dump (struct vertex *v, int i)
707{
708 listnode cnode;
709 listnode nnode;
710 struct vertex_nexthop *nexthop;
711
712 if (v->type == OSPF_VERTEX_ROUTER)
713 {
714 if (IS_DEBUG_OSPF_EVENT)
715 zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
716 }
717 else
718 {
719 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
720 if (IS_DEBUG_OSPF_EVENT)
721 zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
722 ip_masklen (lsa->mask));
723
724 for (nnode = listhead (v->nexthop); nnode; nextnode (nnode))
725 {
726 nexthop = getdata (nnode);
727 if (IS_DEBUG_OSPF_EVENT)
728 zlog_info (" nexthop %s", inet_ntoa (nexthop->router));
729 }
730 }
731
732 i++;
733
734 for (cnode = listhead (v->child); cnode; nextnode (cnode))
735 {
736 v = getdata (cnode);
737 ospf_spf_dump (v, i);
738 }
739}
740
741/* Second stage of SPF calculation. */
742void
743ospf_spf_process_stubs (struct ospf_area *area, struct vertex * v,
744 struct route_table *rt)
745{
746 listnode cnode;
747 struct vertex *child;
748
749 if (IS_DEBUG_OSPF_EVENT)
750 zlog_info ("ospf_process_stub():processing stubs for area %s",
751 inet_ntoa (area->area_id));
752 if (v->type == OSPF_VERTEX_ROUTER)
753 {
754 u_char *p;
755 u_char *lim;
756 struct router_lsa_link *l;
757 struct router_lsa *rlsa;
758
759 if (IS_DEBUG_OSPF_EVENT)
760 zlog_info ("ospf_process_stub():processing router LSA, id: %s",
761 inet_ntoa (v->lsa->id));
762 rlsa = (struct router_lsa *) v->lsa;
763
764
765 if (IS_DEBUG_OSPF_EVENT)
766 zlog_info ("ospf_process_stub(): we have %d links to process",
767 ntohs (rlsa->links));
768 p = ((u_char *) v->lsa) + 24;
769 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
770
771 while (p < lim)
772 {
773 l = (struct router_lsa_link *) p;
774
775 p += (ROUTER_LSA_MIN_SIZE +
776 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
777
778 if (l->m[0].type == LSA_LINK_TYPE_STUB)
779 ospf_intra_add_stub (rt, l, v, area);
780 }
781 }
782
783 if (IS_DEBUG_OSPF_EVENT)
784 zlog_info ("children of V:");
785 for (cnode = listhead (v->child); cnode; nextnode (cnode))
786 {
787 child = getdata (cnode);
788 if (IS_DEBUG_OSPF_EVENT)
789 zlog_info (" child : %s", inet_ntoa (child->id));
790 }
791
792 for (cnode = listhead (v->child); cnode; nextnode (cnode))
793 {
794 child = getdata (cnode);
795
796 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
797 continue;
798
799 ospf_spf_process_stubs (area, child, rt);
800
801 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
802 }
803}
804
805void
806ospf_rtrs_free (struct route_table *rtrs)
807{
808 struct route_node *rn;
809 list or_list;
810 listnode node;
811
812 if (IS_DEBUG_OSPF_EVENT)
813 zlog_info ("Route: Router Routing Table free");
814
815 for (rn = route_top (rtrs); rn; rn = route_next (rn))
816 if ((or_list = rn->info) != NULL)
817 {
818 for (node = listhead (or_list); node; nextnode (node))
819 ospf_route_free (node->data);
820
821 list_delete (or_list);
822
823 /* Unlock the node. */
824 rn->info = NULL;
825 route_unlock_node (rn);
826 }
827 route_table_finish (rtrs);
828}
829
830void
831ospf_rtrs_print (struct route_table *rtrs)
832{
833 struct route_node *rn;
834 list or_list;
835 listnode ln;
836 listnode pnode;
837 struct ospf_route *or;
838 struct ospf_path *path;
839 char buf1[BUFSIZ];
840 char buf2[BUFSIZ];
841
842 if (IS_DEBUG_OSPF_EVENT)
843 zlog_info ("ospf_rtrs_print() start");
844
845 for (rn = route_top (rtrs); rn; rn = route_next (rn))
846 if ((or_list = rn->info) != NULL)
847 for (ln = listhead (or_list); ln; nextnode (ln))
848 {
849 or = getdata (ln);
850
851 switch (or->path_type)
852 {
853 case OSPF_PATH_INTRA_AREA:
854 if (IS_DEBUG_OSPF_EVENT)
855 zlog_info ("%s [%d] area: %s",
856 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), or->cost,
857 inet_ntop (AF_INET, &or->u.std.area_id,
858 buf2, BUFSIZ));
859 break;
860 case OSPF_PATH_INTER_AREA:
861 if (IS_DEBUG_OSPF_EVENT)
862 zlog_info ("%s IA [%d] area: %s",
863 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), or->cost,
864 inet_ntop (AF_INET, &or->u.std.area_id,
865 buf2, BUFSIZ));
866 break;
867 default:
868 break;
869 }
870
871 for (pnode = listhead (or->path); pnode; nextnode (pnode))
872 {
873 path = getdata (pnode);
874 if (path->nexthop.s_addr == 0)
875 {
876 if (IS_DEBUG_OSPF_EVENT)
877 zlog_info (" directly attached to %s\r\n",
878 IF_NAME (path->oi));
879 }
880 else
881 {
882 if (IS_DEBUG_OSPF_EVENT)
883 zlog_info (" via %s, %s\r\n",
884 inet_ntoa (path->nexthop), IF_NAME (path->oi));
885 }
886 }
887 }
888
889 zlog_info ("ospf_rtrs_print() end");
890}
891
892/* Calculating the shortest-path tree for an area. */
893void
894ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
895 struct route_table *new_rtrs)
896{
897 list candidate;
898 listnode node;
899 struct vertex *v;
900 struct route_table *rv;
901 struct route_table *nv;
902
903 if (IS_DEBUG_OSPF_EVENT)
904 {
905 zlog_info ("ospf_spf_calculate: Start");
906 zlog_info ("ospf_spf_calculate: running Dijkstra for area %s",
907 inet_ntoa (area->area_id));
908 }
909
910 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
911 return this area's calculation. */
912 if (! area->router_lsa_self)
913 {
914 if (IS_DEBUG_OSPF_EVENT)
915 zlog_info ("ospf_spf_calculate: "
916 "Skip area %s's calculation due to empty router_lsa_self",
917 inet_ntoa (area->area_id));
918 return;
919 }
920
921 /* RFC2328 16.1. (1). */
922 /* Initialize the algorithm's data structures. */
923 rv = route_table_init ();
924 nv = route_table_init ();
925
926 /* Clear the list of candidate vertices. */
927 candidate = list_new ();
928
929 /* Initialize the shortest-path tree to only the root (which is the
930 router doing the calculation). */
931 ospf_spf_init (area);
932 v = area->spf;
933 ospf_spf_register (v, rv, nv);
934
935 /* Set Area A's TransitCapability to FALSE. */
936 area->transit = OSPF_TRANSIT_FALSE;
937 area->shortcut_capability = 1;
938
939 for (;;)
940 {
941 /* RFC2328 16.1. (2). */
942 ospf_spf_next (v, area, candidate, rv, nv);
943
944 /* RFC2328 16.1. (3). */
945 /* If at this step the candidate list is empty, the shortest-
946 path tree (of transit vertices) has been completely built and
947 this stage of the procedure terminates. */
948 if (listcount (candidate) == 0)
949 break;
950
951 /* Otherwise, choose the vertex belonging to the candidate list
952 that is closest to the root, and add it to the shortest-path
953 tree (removing it from the candidate list in the
954 process). */
955 node = listhead (candidate);
956 v = getdata (node);
957 ospf_vertex_add_parent (v);
958
959 /* Reveve from the candidate list. */
960 listnode_delete (candidate, v);
961
962 /* Add to SPF tree. */
963 ospf_spf_register (v, rv, nv);
964
965 /* Note that when there is a choice of vertices closest to the
966 root, network vertices must be chosen before router vertices
967 in order to necessarily find all equal-cost paths. */
968 /* We don't do this at this moment, we should add the treatment
969 above codes. -- kunihiro. */
970
971 /* RFC2328 16.1. (4). */
972 if (v->type == OSPF_VERTEX_ROUTER)
973 ospf_intra_add_router (new_rtrs, v, area);
974 else
975 ospf_intra_add_transit (new_table, v, area);
976
977 /* RFC2328 16.1. (5). */
978 /* Iterate the algorithm by returning to Step 2. */
979 }
980
981 if (IS_DEBUG_OSPF_EVENT)
982 {
983 ospf_spf_dump (area->spf, 0);
984 ospf_route_table_dump (new_table);
985 }
986
987 /* Second stage of SPF calculation procedure's */
988 ospf_spf_process_stubs (area, area->spf, new_table);
989
990 /* Free all vertices which allocated for SPF calculation */
991 ospf_spf_route_free (rv);
992 ospf_spf_route_free (nv);
993
994 /* Free candidate list */
995 list_free (candidate);
996
997 /* Increment SPF Calculation Counter. */
998 area->spf_calculation++;
999
paul68980082003-03-25 05:07:42 +00001000 area->ospf->ts_spf = time (NULL);
paul718e3742002-12-13 20:15:29 +00001001
1002 if (IS_DEBUG_OSPF_EVENT)
1003 zlog_info ("ospf_spf_calculate: Stop");
1004}
1005
1006/* Timer for SPF calculation. */
1007int
paul68980082003-03-25 05:07:42 +00001008ospf_spf_calculate_timer (struct thread *thread)
paul718e3742002-12-13 20:15:29 +00001009{
paul68980082003-03-25 05:07:42 +00001010 struct ospf *ospf = THREAD_ARG (thread);
paul718e3742002-12-13 20:15:29 +00001011 struct route_table *new_table, *new_rtrs;
paul718e3742002-12-13 20:15:29 +00001012 listnode node;
1013
1014 if (IS_DEBUG_OSPF_EVENT)
1015 zlog_info ("SPF: Timer (SPF calculation expire)");
1016
paul718e3742002-12-13 20:15:29 +00001017 ospf->t_spf_calc = NULL;
1018
1019 /* Allocate new table tree. */
1020 new_table = route_table_init ();
1021 new_rtrs = route_table_init ();
1022
paul68980082003-03-25 05:07:42 +00001023 ospf_vl_unapprove (ospf);
paul718e3742002-12-13 20:15:29 +00001024
1025 /* Calculate SPF for each area. */
1026 for (node = listhead (ospf->areas); node; node = nextnode (node))
1027 ospf_spf_calculate (node->data, new_table, new_rtrs);
1028
paul68980082003-03-25 05:07:42 +00001029 ospf_vl_shut_unapproved (ospf);
paul718e3742002-12-13 20:15:29 +00001030
paul68980082003-03-25 05:07:42 +00001031 ospf_ia_routing (ospf, new_table, new_rtrs);
paul718e3742002-12-13 20:15:29 +00001032
1033 ospf_prune_unreachable_networks (new_table);
1034 ospf_prune_unreachable_routers (new_rtrs);
1035
1036 /* AS-external-LSA calculation should not be performed here. */
1037
1038 /* If new Router Route is installed,
1039 then schedule re-calculate External routes. */
1040 if (1)
paul68980082003-03-25 05:07:42 +00001041 ospf_ase_calculate_schedule (ospf);
paul718e3742002-12-13 20:15:29 +00001042
paul68980082003-03-25 05:07:42 +00001043 ospf_ase_calculate_timer_add (ospf);
paul718e3742002-12-13 20:15:29 +00001044
1045 /* Update routing table. */
paul68980082003-03-25 05:07:42 +00001046 ospf_route_install (ospf, new_table);
paul718e3742002-12-13 20:15:29 +00001047
1048 /* Update ABR/ASBR routing table */
paul68980082003-03-25 05:07:42 +00001049 if (ospf->old_rtrs)
paul718e3742002-12-13 20:15:29 +00001050 {
1051 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
paul68980082003-03-25 05:07:42 +00001052 /* ospf_route_delete (ospf->old_rtrs); */
1053 ospf_rtrs_free (ospf->old_rtrs);
paul718e3742002-12-13 20:15:29 +00001054 }
1055
paul68980082003-03-25 05:07:42 +00001056 ospf->old_rtrs = ospf->new_rtrs;
1057 ospf->new_rtrs = new_rtrs;
paul718e3742002-12-13 20:15:29 +00001058
paul020709f2003-04-04 02:44:16 +00001059 if (IS_OSPF_ABR (ospf))
paul68980082003-03-25 05:07:42 +00001060 ospf_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001061
1062 if (IS_DEBUG_OSPF_EVENT)
1063 zlog_info ("SPF: calculation complete");
1064
1065 return 0;
1066}
1067
1068/* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
1069 set timer for SPF calc. */
1070void
paul68980082003-03-25 05:07:42 +00001071ospf_spf_calculate_schedule (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001072{
1073 time_t ht, delay;
1074
1075 if (IS_DEBUG_OSPF_EVENT)
1076 zlog_info ("SPF: calculation timer scheduled");
1077
1078 /* OSPF instance does not exist. */
paul68980082003-03-25 05:07:42 +00001079 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001080 return;
1081
1082 /* SPF calculation timer is already scheduled. */
paul68980082003-03-25 05:07:42 +00001083 if (ospf->t_spf_calc)
paul718e3742002-12-13 20:15:29 +00001084 {
1085 if (IS_DEBUG_OSPF_EVENT)
1086 zlog_info ("SPF: calculation timer is already scheduled: %p",
paul68980082003-03-25 05:07:42 +00001087 ospf->t_spf_calc);
paul718e3742002-12-13 20:15:29 +00001088 return;
1089 }
1090
paul68980082003-03-25 05:07:42 +00001091 ht = time (NULL) - ospf->ts_spf;
paul718e3742002-12-13 20:15:29 +00001092
1093 /* Get SPF calculation delay time. */
paul68980082003-03-25 05:07:42 +00001094 if (ht < ospf->spf_holdtime)
paul718e3742002-12-13 20:15:29 +00001095 {
paul68980082003-03-25 05:07:42 +00001096 if (ospf->spf_holdtime - ht < ospf->spf_delay)
1097 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001098 else
paul68980082003-03-25 05:07:42 +00001099 delay = ospf->spf_holdtime - ht;
paul718e3742002-12-13 20:15:29 +00001100 }
1101 else
paul68980082003-03-25 05:07:42 +00001102 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001103
1104 if (IS_DEBUG_OSPF_EVENT)
1105 zlog_info ("SPF: calculation timer delay = %ld", delay);
paul68980082003-03-25 05:07:42 +00001106 ospf->t_spf_calc =
1107 thread_add_timer (master, ospf_spf_calculate_timer, ospf, delay);
paul718e3742002-12-13 20:15:29 +00001108}
1109