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