blob: 6e92bb2051e2fad0c6522af14a1e5ba4df5de8f6 [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 {
paul7afa08d2002-12-13 20:59:45 +0000368 /* check for PtMP, signified by PtP link V->W with link_data our PtMP interface */
369 oi = ospf_if_is_configured(&l->link_data);
370 if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
paul718e3742002-12-13 20:15:29 +0000371 {
paul7afa08d2002-12-13 20:59:45 +0000372
373 struct prefix_ipv4 * la = prefix_ipv4_new();
374 la->prefixlen = oi->address->prefixlen;
paul718e3742002-12-13 20:15:29 +0000375
paul7afa08d2002-12-13 20:59:45 +0000376 /* we link to them on PtMP interface - find the interface on w */
377 while ((l2 = ospf_get_next_link (w, v, l2)))
378 {
379 la->prefix = l2->link_data;
380
381 if (prefix_cmp((struct prefix *)la, oi->address) == 0)
382 /* link_data is on our PtMP network */
383 break;
384
385 }
386 }
387 else
388 {
389 while ((l2 = ospf_get_next_link (w, v, l2)))
390 {
391 oi = ospf_if_is_configured (&(l2->link_data));
392
393 if (oi == NULL)
394 continue;
395
396 if (!IPV4_ADDR_SAME (&oi->address->u.prefix4, &l->link_data))
397 continue;
398
399 break;
400 }
paul718e3742002-12-13 20:15:29 +0000401 }
402
403 if (oi && l2)
404 {
405 nh = vertex_nexthop_new (v);
406 nh->oi = oi;
407 nh->router = l2->link_data;
408 listnode_add (w->nexthop, nh);
409 }
410 }
411 }
412 }
413 else
414 {
415 while ((l = ospf_get_next_link (v, w, l)))
416 {
417 oi = ospf_if_is_configured (&(l->link_data));
418 if (oi)
419 {
420 nh = vertex_nexthop_new (v);
421 nh->oi = oi;
422 nh->router.s_addr = 0;
423 listnode_add (w->nexthop, nh);
424 }
425 }
426 }
427 return;
428 }
429 /* In case of W's parent is network connected to root. */
430 else if (v->type == OSPF_VERTEX_NETWORK)
431 {
432 for (node = listhead (v->nexthop); node; nextnode (node))
433 {
434 x = (struct vertex_nexthop *) getdata (node);
435 if (x->parent == area->spf)
436 {
437 while ((l = ospf_get_next_link (w, v, l)))
438 {
439 nh = vertex_nexthop_new (v);
440 nh->oi = x->oi;
441 nh->router = l->link_data;
442 listnode_add (w->nexthop, nh);
443 }
444 return;
445 }
446 }
447 }
448
449 /* Inherit V's nexthop. */
450 for (node = listhead (v->nexthop); node; nextnode (node))
451 {
452 nh = vertex_nexthop_dup (node->data);
453 nh->parent = v;
454 ospf_nexthop_add_unique (nh, w->nexthop);
455 }
456}
457
458void
459ospf_install_candidate (list candidate, struct vertex *w)
460{
461 listnode node;
462 struct vertex *cw;
463
464 if (list_isempty (candidate))
465 {
466 listnode_add (candidate, w);
467 return;
468 }
469
470 /* Install vertex with sorting by distance. */
471 for (node = listhead (candidate); node; nextnode (node))
472 {
473 cw = (struct vertex *) getdata (node);
474 if (cw->distance > w->distance)
475 {
476 list_add_node_prev (candidate, node, w);
477 break;
478 }
479 else if (node->next == NULL)
480 {
481 list_add_node_next (candidate, node, w);
482 break;
483 }
484 }
485}
486
487/* RFC2328 Section 16.1 (2). */
488void
489ospf_spf_next (struct vertex *v, struct ospf_area *area,
490 list candidate, struct route_table *rv,
491 struct route_table *nv)
492{
493 struct ospf_lsa *w_lsa = NULL;
494 struct vertex *w, *cw;
495 u_char *p;
496 u_char *lim;
497 struct router_lsa_link *l = NULL;
498 struct in_addr *r;
499 listnode node;
500 int type = 0;
501
502 /* If this is a router-LSA, and bit V of the router-LSA (see Section
503 A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */
504 if (v->type == OSPF_VERTEX_ROUTER)
505 {
506 if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa))
507 area->transit = OSPF_TRANSIT_TRUE;
508 }
509
510 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
511 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
512
513 while (p < lim)
514 {
515 /* In case of V is Router-LSA. */
516 if (v->lsa->type == OSPF_ROUTER_LSA)
517 {
518 l = (struct router_lsa_link *) p;
519
520 p += (ROUTER_LSA_MIN_SIZE +
521 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
522
523 /* (a) If this is a link to a stub network, examine the next
524 link in V's LSA. Links to stub networks will be
525 considered in the second stage of the shortest path
526 calculation. */
527 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
528 continue;
529
530 /* (b) Otherwise, W is a transit vertex (router or transit
531 network). Look up the vertex W's LSA (router-LSA or
532 network-LSA) in Area A's link state database. */
533 switch (type)
534 {
535 case LSA_LINK_TYPE_POINTOPOINT:
536 case LSA_LINK_TYPE_VIRTUALLINK:
537 if (type == LSA_LINK_TYPE_VIRTUALLINK)
538 {
539 if (IS_DEBUG_OSPF_EVENT)
540 zlog_info ("looking up LSA through VL: %s",
541 inet_ntoa (l->link_id));
542 }
543
544 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
545 l->link_id);
546 if (w_lsa)
547 {
548 if (IS_DEBUG_OSPF_EVENT)
549 zlog_info("found the LSA");
550 }
551 break;
552 case LSA_LINK_TYPE_TRANSIT:
553 if (IS_DEBUG_OSPF_EVENT)
554
555 zlog_info ("Looking up Network LSA, ID: %s",
556 inet_ntoa(l->link_id));
557 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
558 l->link_id);
559 if (w_lsa)
560 if (IS_DEBUG_OSPF_EVENT)
561 zlog_info("found the LSA");
562 break;
563 default:
564 zlog_warn ("Invalid LSA link type %d", type);
565 continue;
566 }
567 }
568 else
569 {
570 /* In case of V is Network-LSA. */
571 r = (struct in_addr *) p ;
572 p += sizeof (struct in_addr);
573
574 /* Lookup the vertex W's LSA. */
575 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
576 }
577
578 /* (b cont.) If the LSA does not exist, or its LS age is equal
579 to MaxAge, or it does not have a link back to vertex V,
580 examine the next link in V's LSA.[23] */
581 if (w_lsa == NULL)
582 continue;
583
584 if (IS_LSA_MAXAGE (w_lsa))
585 continue;
586
587 if (! ospf_lsa_has_link (w_lsa->data, v->lsa))
588 {
589 if (IS_DEBUG_OSPF_EVENT)
590 zlog_info ("The LSA doesn't have a link back");
591 continue;
592 }
593
594 /* (c) If vertex W is already on the shortest-path tree, examine
595 the next link in the LSA. */
596 if (ospf_spf_has_vertex (rv, nv, w_lsa->data))
597 {
598 if (IS_DEBUG_OSPF_EVENT)
599 zlog_info ("The LSA is already in SPF");
600 continue;
601 }
602
603 /* (d) Calculate the link state cost D of the resulting path
604 from the root to vertex W. D is equal to the sum of the link
605 state cost of the (already calculated) shortest path to
606 vertex V and the advertised cost of the link between vertices
607 V and W. If D is: */
608
609 /* prepare vertex W. */
610 w = ospf_vertex_new (w_lsa);
611
612 /* calculate link cost D. */
613 if (v->lsa->type == OSPF_ROUTER_LSA)
614 w->distance = v->distance + ntohs (l->m[0].metric);
615 else
616 w->distance = v->distance;
617
618 /* Is there already vertex W in candidate list? */
619 node = ospf_vertex_lookup (candidate, w->id, w->type);
620 if (node == NULL)
621 {
622 /* Calculate nexthop to W. */
623 ospf_nexthop_calculation (area, v, w);
624
625 ospf_install_candidate (candidate, w);
626 }
627 else
628 {
629 cw = (struct vertex *) getdata (node);
630
631 /* if D is greater than. */
632 if (cw->distance < w->distance)
633 {
634 ospf_vertex_free (w);
635 continue;
636 }
637 /* equal to. */
638 else if (cw->distance == w->distance)
639 {
640 /* Calculate nexthop to W. */
641 ospf_nexthop_calculation (area, v, w);
642 ospf_nexthop_merge (cw->nexthop, w->nexthop);
643 list_delete_all_node (w->nexthop);
644 ospf_vertex_free (w);
645 }
646 /* less than. */
647 else
648 {
649 /* Calculate nexthop. */
650 ospf_nexthop_calculation (area, v, w);
651
652 /* Remove old vertex from candidate list. */
653 ospf_vertex_free (cw);
654 listnode_delete (candidate, cw);
655
656 /* Install new to candidate. */
657 ospf_install_candidate (candidate, w);
658 }
659 }
660 }
661}
662
663/* Add vertex V to SPF tree. */
664void
665ospf_spf_register (struct vertex *v, struct route_table *rv,
666 struct route_table *nv)
667{
668 struct prefix p;
669 struct route_node *rn;
670
671 p.family = AF_INET;
672 p.prefixlen = IPV4_MAX_BITLEN;
673 p.u.prefix4 = v->id;
674
675 if (v->type == OSPF_VERTEX_ROUTER)
676 rn = route_node_get (rv, &p);
677 else
678 rn = route_node_get (nv, &p);
679
680 rn->info = v;
681}
682
683void
684ospf_spf_route_free (struct route_table *table)
685{
686 struct route_node *rn;
687 struct vertex *v;
688
689 for (rn = route_top (table); rn; rn = route_next (rn))
690 {
691 if ((v = rn->info))
692 {
693 ospf_vertex_free (v);
694 rn->info = NULL;
695 }
696
697 route_unlock_node (rn);
698 }
699
700 route_table_finish (table);
701}
702
703void
704ospf_spf_dump (struct vertex *v, int i)
705{
706 listnode cnode;
707 listnode nnode;
708 struct vertex_nexthop *nexthop;
709
710 if (v->type == OSPF_VERTEX_ROUTER)
711 {
712 if (IS_DEBUG_OSPF_EVENT)
713 zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
714 }
715 else
716 {
717 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
718 if (IS_DEBUG_OSPF_EVENT)
719 zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
720 ip_masklen (lsa->mask));
721
722 for (nnode = listhead (v->nexthop); nnode; nextnode (nnode))
723 {
724 nexthop = getdata (nnode);
725 if (IS_DEBUG_OSPF_EVENT)
726 zlog_info (" nexthop %s", inet_ntoa (nexthop->router));
727 }
728 }
729
730 i++;
731
732 for (cnode = listhead (v->child); cnode; nextnode (cnode))
733 {
734 v = getdata (cnode);
735 ospf_spf_dump (v, i);
736 }
737}
738
739/* Second stage of SPF calculation. */
740void
741ospf_spf_process_stubs (struct ospf_area *area, struct vertex * v,
742 struct route_table *rt)
743{
744 listnode cnode;
745 struct vertex *child;
746
747 if (IS_DEBUG_OSPF_EVENT)
748 zlog_info ("ospf_process_stub():processing stubs for area %s",
749 inet_ntoa (area->area_id));
750 if (v->type == OSPF_VERTEX_ROUTER)
751 {
752 u_char *p;
753 u_char *lim;
754 struct router_lsa_link *l;
755 struct router_lsa *rlsa;
756
757 if (IS_DEBUG_OSPF_EVENT)
758 zlog_info ("ospf_process_stub():processing router LSA, id: %s",
759 inet_ntoa (v->lsa->id));
760 rlsa = (struct router_lsa *) v->lsa;
761
762
763 if (IS_DEBUG_OSPF_EVENT)
764 zlog_info ("ospf_process_stub(): we have %d links to process",
765 ntohs (rlsa->links));
766 p = ((u_char *) v->lsa) + 24;
767 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
768
769 while (p < lim)
770 {
771 l = (struct router_lsa_link *) p;
772
773 p += (ROUTER_LSA_MIN_SIZE +
774 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
775
776 if (l->m[0].type == LSA_LINK_TYPE_STUB)
777 ospf_intra_add_stub (rt, l, v, area);
778 }
779 }
780
781 if (IS_DEBUG_OSPF_EVENT)
782 zlog_info ("children of V:");
783 for (cnode = listhead (v->child); cnode; nextnode (cnode))
784 {
785 child = getdata (cnode);
786 if (IS_DEBUG_OSPF_EVENT)
787 zlog_info (" child : %s", inet_ntoa (child->id));
788 }
789
790 for (cnode = listhead (v->child); cnode; nextnode (cnode))
791 {
792 child = getdata (cnode);
793
794 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
795 continue;
796
797 ospf_spf_process_stubs (area, child, rt);
798
799 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
800 }
801}
802
803void
804ospf_rtrs_free (struct route_table *rtrs)
805{
806 struct route_node *rn;
807 list or_list;
808 listnode node;
809
810 if (IS_DEBUG_OSPF_EVENT)
811 zlog_info ("Route: Router Routing Table free");
812
813 for (rn = route_top (rtrs); rn; rn = route_next (rn))
814 if ((or_list = rn->info) != NULL)
815 {
816 for (node = listhead (or_list); node; nextnode (node))
817 ospf_route_free (node->data);
818
819 list_delete (or_list);
820
821 /* Unlock the node. */
822 rn->info = NULL;
823 route_unlock_node (rn);
824 }
825 route_table_finish (rtrs);
826}
827
828void
829ospf_rtrs_print (struct route_table *rtrs)
830{
831 struct route_node *rn;
832 list or_list;
833 listnode ln;
834 listnode pnode;
835 struct ospf_route *or;
836 struct ospf_path *path;
837 char buf1[BUFSIZ];
838 char buf2[BUFSIZ];
839
840 if (IS_DEBUG_OSPF_EVENT)
841 zlog_info ("ospf_rtrs_print() start");
842
843 for (rn = route_top (rtrs); rn; rn = route_next (rn))
844 if ((or_list = rn->info) != NULL)
845 for (ln = listhead (or_list); ln; nextnode (ln))
846 {
847 or = getdata (ln);
848
849 switch (or->path_type)
850 {
851 case OSPF_PATH_INTRA_AREA:
852 if (IS_DEBUG_OSPF_EVENT)
853 zlog_info ("%s [%d] area: %s",
854 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), or->cost,
855 inet_ntop (AF_INET, &or->u.std.area_id,
856 buf2, BUFSIZ));
857 break;
858 case OSPF_PATH_INTER_AREA:
859 if (IS_DEBUG_OSPF_EVENT)
860 zlog_info ("%s IA [%d] area: %s",
861 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), or->cost,
862 inet_ntop (AF_INET, &or->u.std.area_id,
863 buf2, BUFSIZ));
864 break;
865 default:
866 break;
867 }
868
869 for (pnode = listhead (or->path); pnode; nextnode (pnode))
870 {
871 path = getdata (pnode);
872 if (path->nexthop.s_addr == 0)
873 {
874 if (IS_DEBUG_OSPF_EVENT)
875 zlog_info (" directly attached to %s\r\n",
876 IF_NAME (path->oi));
877 }
878 else
879 {
880 if (IS_DEBUG_OSPF_EVENT)
881 zlog_info (" via %s, %s\r\n",
882 inet_ntoa (path->nexthop), IF_NAME (path->oi));
883 }
884 }
885 }
886
887 zlog_info ("ospf_rtrs_print() end");
888}
889
890/* Calculating the shortest-path tree for an area. */
891void
892ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
893 struct route_table *new_rtrs)
894{
895 list candidate;
896 listnode node;
897 struct vertex *v;
898 struct route_table *rv;
899 struct route_table *nv;
900
901 if (IS_DEBUG_OSPF_EVENT)
902 {
903 zlog_info ("ospf_spf_calculate: Start");
904 zlog_info ("ospf_spf_calculate: running Dijkstra for area %s",
905 inet_ntoa (area->area_id));
906 }
907
908 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
909 return this area's calculation. */
910 if (! area->router_lsa_self)
911 {
912 if (IS_DEBUG_OSPF_EVENT)
913 zlog_info ("ospf_spf_calculate: "
914 "Skip area %s's calculation due to empty router_lsa_self",
915 inet_ntoa (area->area_id));
916 return;
917 }
918
919 /* RFC2328 16.1. (1). */
920 /* Initialize the algorithm's data structures. */
921 rv = route_table_init ();
922 nv = route_table_init ();
923
924 /* Clear the list of candidate vertices. */
925 candidate = list_new ();
926
927 /* Initialize the shortest-path tree to only the root (which is the
928 router doing the calculation). */
929 ospf_spf_init (area);
930 v = area->spf;
931 ospf_spf_register (v, rv, nv);
932
933 /* Set Area A's TransitCapability to FALSE. */
934 area->transit = OSPF_TRANSIT_FALSE;
935 area->shortcut_capability = 1;
936
937 for (;;)
938 {
939 /* RFC2328 16.1. (2). */
940 ospf_spf_next (v, area, candidate, rv, nv);
941
942 /* RFC2328 16.1. (3). */
943 /* If at this step the candidate list is empty, the shortest-
944 path tree (of transit vertices) has been completely built and
945 this stage of the procedure terminates. */
946 if (listcount (candidate) == 0)
947 break;
948
949 /* Otherwise, choose the vertex belonging to the candidate list
950 that is closest to the root, and add it to the shortest-path
951 tree (removing it from the candidate list in the
952 process). */
953 node = listhead (candidate);
954 v = getdata (node);
955 ospf_vertex_add_parent (v);
956
957 /* Reveve from the candidate list. */
958 listnode_delete (candidate, v);
959
960 /* Add to SPF tree. */
961 ospf_spf_register (v, rv, nv);
962
963 /* Note that when there is a choice of vertices closest to the
964 root, network vertices must be chosen before router vertices
965 in order to necessarily find all equal-cost paths. */
966 /* We don't do this at this moment, we should add the treatment
967 above codes. -- kunihiro. */
968
969 /* RFC2328 16.1. (4). */
970 if (v->type == OSPF_VERTEX_ROUTER)
971 ospf_intra_add_router (new_rtrs, v, area);
972 else
973 ospf_intra_add_transit (new_table, v, area);
974
975 /* RFC2328 16.1. (5). */
976 /* Iterate the algorithm by returning to Step 2. */
977 }
978
979 if (IS_DEBUG_OSPF_EVENT)
980 {
981 ospf_spf_dump (area->spf, 0);
982 ospf_route_table_dump (new_table);
983 }
984
985 /* Second stage of SPF calculation procedure's */
986 ospf_spf_process_stubs (area, area->spf, new_table);
987
988 /* Free all vertices which allocated for SPF calculation */
989 ospf_spf_route_free (rv);
990 ospf_spf_route_free (nv);
991
992 /* Free candidate list */
993 list_free (candidate);
994
995 /* Increment SPF Calculation Counter. */
996 area->spf_calculation++;
997
998 ospf_top->ts_spf = time (NULL);
999
1000 if (IS_DEBUG_OSPF_EVENT)
1001 zlog_info ("ospf_spf_calculate: Stop");
1002}
1003
1004/* Timer for SPF calculation. */
1005int
1006ospf_spf_calculate_timer (struct thread *t)
1007{
1008 struct route_table *new_table, *new_rtrs;
1009 struct ospf *ospf;
1010 /* struct ospf_area *area; */
1011 listnode node;
1012
1013 if (IS_DEBUG_OSPF_EVENT)
1014 zlog_info ("SPF: Timer (SPF calculation expire)");
1015
1016 ospf = THREAD_ARG (t);
1017 ospf->t_spf_calc = NULL;
1018
1019 /* Allocate new table tree. */
1020 new_table = route_table_init ();
1021 new_rtrs = route_table_init ();
1022
1023 ospf_vl_unapprove ();
1024
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
1029 ospf_vl_shut_unapproved ();
1030
1031 ospf_ia_routing (new_table, new_rtrs);
1032
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)
1041 ospf_ase_calculate_schedule ();
1042
1043 ospf_ase_calculate_timer_add ();
1044
1045 /* Update routing table. */
1046 ospf_route_install (new_table);
1047
1048 /* Update ABR/ASBR routing table */
1049 if (ospf_top->old_rtrs)
1050 {
1051 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
1052 /* ospf_route_delete (ospf_top->old_rtrs); */
1053 ospf_rtrs_free (ospf_top->old_rtrs);
1054 }
1055
1056 ospf_top->old_rtrs = ospf_top->new_rtrs;
1057 ospf_top->new_rtrs = new_rtrs;
1058
1059 if (OSPF_IS_ABR)
1060 ospf_abr_task (new_table, new_rtrs);
1061
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
1071ospf_spf_calculate_schedule ()
1072{
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. */
1079 if (!ospf_top)
1080 return;
1081
1082 /* SPF calculation timer is already scheduled. */
1083 if (ospf_top->t_spf_calc)
1084 {
1085 if (IS_DEBUG_OSPF_EVENT)
1086 zlog_info ("SPF: calculation timer is already scheduled: %p",
1087 ospf_top->t_spf_calc);
1088 return;
1089 }
1090
1091 ht = time (NULL) - ospf_top->ts_spf;
1092
1093 /* Get SPF calculation delay time. */
1094 if (ht < ospf_top->spf_holdtime)
1095 {
1096 if (ospf_top->spf_holdtime - ht < ospf_top->spf_delay)
1097 delay = ospf_top->spf_delay;
1098 else
1099 delay = ospf_top->spf_holdtime - ht;
1100 }
1101 else
1102 delay = ospf_top->spf_delay;
1103
1104 if (IS_DEBUG_OSPF_EVENT)
1105 zlog_info ("SPF: calculation timer delay = %ld", delay);
1106 ospf_top->t_spf_calc =
1107 thread_add_timer (master, ospf_spf_calculate_timer, ospf_top, delay);
1108}
1109