blob: 8399e18e8b1ac7f2ce635162fc7f27b268d7fe87 [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}
paul718e3742002-12-13 20:15:29 +000079
paul0c0f9cd2003-06-06 23:27:04 +000080
paul718e3742002-12-13 20:15:29 +000081struct 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)
paul0c0f9cd2003-06-06 23:27:04 +0000128 listnode_add (nh->parent->child, v);
paul718e3742002-12-13 20:15:29 +0000129 }
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;
paul0c0f9cd2003-06-06 23:27:04 +0000203
paul718e3742002-12-13 20:15:29 +0000204 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;
paul0c0f9cd2003-06-06 23:27:04 +0000218 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
219 i++, length -= 12)
paul718e3742002-12-13 20:15:29 +0000220 {
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;
paul0c0f9cd2003-06-06 23:27:04 +0000238 }
paul718e3742002-12-13 20:15:29 +0000239 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 &&
paul0c0f9cd2003-06-06 23:27:04 +0000272 IPV4_ADDR_SAME (&nh->router, &new->router) &&
273 nh->parent == new->parent)
274 {
275 match = 1;
276 break;
277 }
paul718e3742002-12-13 20:15:29 +0000278 }
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,
paul0c0f9cd2003-06-06 23:27:04 +0000303 struct router_lsa_link *prev_link)
paul718e3742002-12-13 20:15:29 +0000304{
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 {
paul0c0f9cd2003-06-06 23:27:04 +0000313 p = (u_char *) prev_link;
paul718e3742002-12-13 20:15:29 +0000314 p += (ROUTER_LSA_MIN_SIZE +
315 (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
316 }
paul0c0f9cd2003-06-06 23:27:04 +0000317
paul718e3742002-12-13 20:15:29 +0000318 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
319
320 while (p < lim)
321 {
322 l = (struct router_lsa_link *) p;
323
paul0c0f9cd2003-06-06 23:27:04 +0000324 p += (ROUTER_LSA_MIN_SIZE + (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +0000325
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)
paul0c0f9cd2003-06-06 23:27:04 +0000333 continue;
paul718e3742002-12-13 20:15:29 +0000334
335 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
paul0c0f9cd2003-06-06 23:27:04 +0000336 return l;
paul718e3742002-12-13 20:15:29 +0000337 }
338
339 return NULL;
340}
341
paulbf9392c2003-06-06 23:23:36 +0000342/* Consider supplied next-hop for inclusion to the supplied list
343 * of next-hops, adjust list as neccessary
344 */
paul0c0f9cd2003-06-06 23:27:04 +0000345void
346ospf_spf_consider_nexthop (struct list *nexthops,
347 struct vertex_nexthop *newhop)
paulbf9392c2003-06-06 23:23:36 +0000348{
349 struct listnode *nnode;
350 struct vertex_nexthop *hop;
paul0c0f9cd2003-06-06 23:27:04 +0000351
paulbf9392c2003-06-06 23:23:36 +0000352 LIST_LOOP (nexthops, hop, nnode)
paul0c0f9cd2003-06-06 23:27:04 +0000353 {
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
paulbf9392c2003-06-06 23:23:36 +0000368 /* new hop is <= existing hops, add it */
paul0c0f9cd2003-06-06 23:27:04 +0000369 listnode_add (nexthops, newhop);
paulbf9392c2003-06-06 23:23:36 +0000370
371 return;
372}
373
paul718e3742002-12-13 20:15:29 +0000374/* 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;
paul0c0f9cd2003-06-06 23:27:04 +0000383
384
paul718e3742002-12-13 20:15:29 +0000385 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)
paul0c0f9cd2003-06-06 23:27:04 +0000392 {
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. */
paul68980082003-03-25 05:07:42 +0000401 oi = ospf_if_is_configured (area->ospf, &l->link_data);
paul7afa08d2002-12-13 20:59:45 +0000402 if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
paul0c0f9cd2003-06-06 23:27:04 +0000403 {
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 }
paul718e3742002-12-13 20:15:29 +0000446 else
paul0c0f9cd2003-06-06 23:27:04 +0000447 {
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 }
paul718e3742002-12-13 20:15:29 +0000460 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 {
paul0c0f9cd2003-06-06 23:27:04 +0000470 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 }
paul718e3742002-12-13 20:15:29 +0000479 }
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,
paul0c0f9cd2003-06-06 23:27:04 +0000523 list candidate, struct route_table *rv, struct route_table *nv)
paul718e3742002-12-13 20:15:29 +0000524{
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;
paul0c0f9cd2003-06-06 23:27:04 +0000543 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
544
paul718e3742002-12-13 20:15:29 +0000545 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
paul0c0f9cd2003-06-06 23:27:04 +0000552 p += (ROUTER_LSA_MIN_SIZE +
paul718e3742002-12-13 20:15:29 +0000553 (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)
paul0c0f9cd2003-06-06 23:27:04 +0000570 {
571 if (IS_DEBUG_OSPF_EVENT)
572 zlog_info ("looking up LSA through VL: %s",
573 inet_ntoa (l->link_id));
574 }
paul718e3742002-12-13 20:15:29 +0000575
576 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
577 l->link_id);
578 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000579 {
580 if (IS_DEBUG_OSPF_EVENT)
581 zlog_info ("found the LSA");
582 }
paul718e3742002-12-13 20:15:29 +0000583 break;
584 case LSA_LINK_TYPE_TRANSIT:
paul0c0f9cd2003-06-06 23:27:04 +0000585 if (IS_DEBUG_OSPF_EVENT)
paul718e3742002-12-13 20:15:29 +0000586
paul0c0f9cd2003-06-06 23:27:04 +0000587 zlog_info ("Looking up Network LSA, ID: %s",
588 inet_ntoa (l->link_id));
paul718e3742002-12-13 20:15:29 +0000589 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
paul0c0f9cd2003-06-06 23:27:04 +0000590 l->link_id);
paul718e3742002-12-13 20:15:29 +0000591 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000592 if (IS_DEBUG_OSPF_EVENT)
593 zlog_info ("found the LSA");
paul718e3742002-12-13 20:15:29 +0000594 break;
595 default:
paul0c0f9cd2003-06-06 23:27:04 +0000596 zlog_warn ("Invalid LSA link type %d", type);
paul718e3742002-12-13 20:15:29 +0000597 continue;
598 }
599 }
600 else
601 {
602 /* In case of V is Network-LSA. */
paul0c0f9cd2003-06-06 23:27:04 +0000603 r = (struct in_addr *) p;
paul718e3742002-12-13 20:15:29 +0000604 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
paul0c0f9cd2003-06-06 23:27:04 +0000619 if (!ospf_lsa_has_link (w_lsa->data, v->lsa))
paul718e3742002-12-13 20:15:29 +0000620 {
paul0c0f9cd2003-06-06 23:27:04 +0000621 if (IS_DEBUG_OSPF_EVENT)
622 zlog_info ("The LSA doesn't have a link back");
paul718e3742002-12-13 20:15:29 +0000623 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 {
paul0c0f9cd2003-06-06 23:27:04 +0000630 if (IS_DEBUG_OSPF_EVENT)
631 zlog_info ("The LSA is already in SPF");
paul718e3742002-12-13 20:15:29 +0000632 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,
paul0c0f9cd2003-06-06 23:27:04 +0000698 struct route_table *nv)
paul718e3742002-12-13 20:15:29 +0000699{
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))
paul0c0f9cd2003-06-06 23:27:04 +0000724 {
725 ospf_vertex_free (v);
726 rn->info = NULL;
727 }
paul718e3742002-12-13 20:15:29 +0000728
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)
paul0c0f9cd2003-06-06 23:27:04 +0000745 zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +0000746 }
747 else
748 {
749 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
750 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000751 zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
752 ip_masklen (lsa->mask));
paul718e3742002-12-13 20:15:29 +0000753
754 for (nnode = listhead (v->nexthop); nnode; nextnode (nnode))
755 {
756 nexthop = getdata (nnode);
paul0c0f9cd2003-06-06 23:27:04 +0000757 if (IS_DEBUG_OSPF_EVENT)
758 zlog_info (" nexthop %s", inet_ntoa (nexthop->router));
paul718e3742002-12-13 20:15:29 +0000759 }
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
paul0c0f9cd2003-06-06 23:27:04 +0000773ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v,
paul718e3742002-12-13 20:15:29 +0000774 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",
paul0c0f9cd2003-06-06 23:27:04 +0000781 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000782 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
paul0c0f9cd2003-06-06 23:27:04 +0000789 if (IS_DEBUG_OSPF_EVENT)
790 zlog_info ("ospf_process_stub():processing router LSA, id: %s",
791 inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +0000792 rlsa = (struct router_lsa *) v->lsa;
793
794
paul0c0f9cd2003-06-06 23:27:04 +0000795 if (IS_DEBUG_OSPF_EVENT)
796 zlog_info ("ospf_process_stub(): we have %d links to process",
797 ntohs (rlsa->links));
paul718e3742002-12-13 20:15:29 +0000798 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)
paul0c0f9cd2003-06-06 23:27:04 +0000814 zlog_info ("children of V:");
paul718e3742002-12-13 20:15:29 +0000815 for (cnode = listhead (v->child); cnode; nextnode (cnode))
816 {
817 child = getdata (cnode);
paul0c0f9cd2003-06-06 23:27:04 +0000818 if (IS_DEBUG_OSPF_EVENT)
819 zlog_info (" child : %s", inet_ntoa (child->id));
paul718e3742002-12-13 20:15:29 +0000820 }
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))
paul0c0f9cd2003-06-06 23:27:04 +0000827 continue;
paul718e3742002-12-13 20:15:29 +0000828
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)
paul0c0f9cd2003-06-06 23:27:04 +0000843 zlog_info ("Route: Router Routing Table free");
paul718e3742002-12-13 20:15:29 +0000844
845 for (rn = route_top (rtrs); rn; rn = route_next (rn))
846 if ((or_list = rn->info) != NULL)
847 {
paul0c0f9cd2003-06-06 23:27:04 +0000848 for (node = listhead (or_list); node; nextnode (node))
849 ospf_route_free (node->data);
paul718e3742002-12-13 20:15:29 +0000850
paul0c0f9cd2003-06-06 23:27:04 +0000851 list_delete (or_list);
paul718e3742002-12-13 20:15:29 +0000852
paul0c0f9cd2003-06-06 23:27:04 +0000853 /* Unlock the node. */
854 rn->info = NULL;
855 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +0000856 }
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:
paul0c0f9cd2003-06-06 23:27:04 +0000884 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));
paul718e3742002-12-13 20:15:29 +0000889 break;
890 case OSPF_PATH_INTER_AREA:
paul0c0f9cd2003-06-06 23:27:04 +0000891 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));
paul718e3742002-12-13 20:15:29 +0000896 break;
897 default:
898 break;
899 }
900
paul96735ee2003-08-10 02:51:22 +0000901 for (pnode = listhead (or->paths); pnode; nextnode (pnode))
paul718e3742002-12-13 20:15:29 +0000902 {
903 path = getdata (pnode);
904 if (path->nexthop.s_addr == 0)
paul0c0f9cd2003-06-06 23:27:04 +0000905 {
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 }
paul718e3742002-12-13 20:15:29 +0000916 }
917 }
918
919 zlog_info ("ospf_rtrs_print() end");
920}
921
922/* Calculating the shortest-path tree for an area. */
923void
paul0c0f9cd2003-06-06 23:27:04 +0000924ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
paul718e3742002-12-13 20:15:29 +0000925 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");
paul0c0f9cd2003-06-06 23:27:04 +0000936 zlog_info ("ospf_spf_calculate: running Dijkstra for area %s",
937 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000938 }
939
940 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
941 return this area's calculation. */
paul0c0f9cd2003-06-06 23:27:04 +0000942 if (!area->router_lsa_self)
paul718e3742002-12-13 20:15:29 +0000943 {
944 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000945 zlog_info ("ospf_spf_calculate: "
946 "Skip area %s's calculation due to empty router_lsa_self",
947 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000948 return;
949 }
950
951 /* RFC2328 16.1. (1). */
paul0c0f9cd2003-06-06 23:27:04 +0000952 /* Initialize the algorithm's data structures. */
paul718e3742002-12-13 20:15:29 +0000953 rv = route_table_init ();
954 nv = route_table_init ();
955
paul0c0f9cd2003-06-06 23:27:04 +0000956 /* Clear the list of candidate vertices. */
paul718e3742002-12-13 20:15:29 +0000957 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
paul0c0f9cd2003-06-06 23:27:04 +0000984 process). */
paul718e3742002-12-13 20:15:29 +0000985 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);
paul0c0f9cd2003-06-06 23:27:04 +00001004 else
paul718e3742002-12-13 20:15:29 +00001005 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
paul68980082003-03-25 05:07:42 +00001030 area->ospf->ts_spf = time (NULL);
paul718e3742002-12-13 20:15:29 +00001031
1032 if (IS_DEBUG_OSPF_EVENT)
1033 zlog_info ("ospf_spf_calculate: Stop");
1034}
1035
1036/* Timer for SPF calculation. */
1037int
paul68980082003-03-25 05:07:42 +00001038ospf_spf_calculate_timer (struct thread *thread)
paul718e3742002-12-13 20:15:29 +00001039{
paul68980082003-03-25 05:07:42 +00001040 struct ospf *ospf = THREAD_ARG (thread);
paul718e3742002-12-13 20:15:29 +00001041 struct route_table *new_table, *new_rtrs;
paul718e3742002-12-13 20:15:29 +00001042 listnode node;
1043
1044 if (IS_DEBUG_OSPF_EVENT)
1045 zlog_info ("SPF: Timer (SPF calculation expire)");
paul0c0f9cd2003-06-06 23:27:04 +00001046
paul718e3742002-12-13 20:15:29 +00001047 ospf->t_spf_calc = NULL;
1048
1049 /* Allocate new table tree. */
1050 new_table = route_table_init ();
paul0c0f9cd2003-06-06 23:27:04 +00001051 new_rtrs = route_table_init ();
paul718e3742002-12-13 20:15:29 +00001052
paul68980082003-03-25 05:07:42 +00001053 ospf_vl_unapprove (ospf);
paul718e3742002-12-13 20:15:29 +00001054
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
paul68980082003-03-25 05:07:42 +00001059 ospf_vl_shut_unapproved (ospf);
paul718e3742002-12-13 20:15:29 +00001060
paul68980082003-03-25 05:07:42 +00001061 ospf_ia_routing (ospf, new_table, new_rtrs);
paul718e3742002-12-13 20:15:29 +00001062
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)
paul68980082003-03-25 05:07:42 +00001071 ospf_ase_calculate_schedule (ospf);
paul718e3742002-12-13 20:15:29 +00001072
paul68980082003-03-25 05:07:42 +00001073 ospf_ase_calculate_timer_add (ospf);
paul718e3742002-12-13 20:15:29 +00001074
1075 /* Update routing table. */
paul68980082003-03-25 05:07:42 +00001076 ospf_route_install (ospf, new_table);
paul718e3742002-12-13 20:15:29 +00001077
1078 /* Update ABR/ASBR routing table */
paul68980082003-03-25 05:07:42 +00001079 if (ospf->old_rtrs)
paul718e3742002-12-13 20:15:29 +00001080 {
1081 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
paul68980082003-03-25 05:07:42 +00001082 /* ospf_route_delete (ospf->old_rtrs); */
1083 ospf_rtrs_free (ospf->old_rtrs);
paul718e3742002-12-13 20:15:29 +00001084 }
1085
paul68980082003-03-25 05:07:42 +00001086 ospf->old_rtrs = ospf->new_rtrs;
1087 ospf->new_rtrs = new_rtrs;
paul718e3742002-12-13 20:15:29 +00001088
paul0c0f9cd2003-06-06 23:27:04 +00001089 if (IS_OSPF_ABR (ospf))
paul68980082003-03-25 05:07:42 +00001090 ospf_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001091
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
paul68980082003-03-25 05:07:42 +00001101ospf_spf_calculate_schedule (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001102{
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. */
paul68980082003-03-25 05:07:42 +00001109 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001110 return;
1111
1112 /* SPF calculation timer is already scheduled. */
paul68980082003-03-25 05:07:42 +00001113 if (ospf->t_spf_calc)
paul718e3742002-12-13 20:15:29 +00001114 {
1115 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +00001116 zlog_info ("SPF: calculation timer is already scheduled: %p",
1117 ospf->t_spf_calc);
paul718e3742002-12-13 20:15:29 +00001118 return;
1119 }
1120
paul68980082003-03-25 05:07:42 +00001121 ht = time (NULL) - ospf->ts_spf;
paul718e3742002-12-13 20:15:29 +00001122
1123 /* Get SPF calculation delay time. */
paul68980082003-03-25 05:07:42 +00001124 if (ht < ospf->spf_holdtime)
paul718e3742002-12-13 20:15:29 +00001125 {
paul68980082003-03-25 05:07:42 +00001126 if (ospf->spf_holdtime - ht < ospf->spf_delay)
paul0c0f9cd2003-06-06 23:27:04 +00001127 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001128 else
paul0c0f9cd2003-06-06 23:27:04 +00001129 delay = ospf->spf_holdtime - ht;
paul718e3742002-12-13 20:15:29 +00001130 }
1131 else
paul68980082003-03-25 05:07:42 +00001132 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001133
1134 if (IS_DEBUG_OSPF_EVENT)
1135 zlog_info ("SPF: calculation timer delay = %ld", delay);
paul68980082003-03-25 05:07:42 +00001136 ospf->t_spf_calc =
1137 thread_add_timer (master, ospf_spf_calculate_timer, ospf, delay);
paul718e3742002-12-13 20:15:29 +00001138}