blob: 5afdf161b8cf11f15f8472ce52356659dec9929e [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 ();
pauld355bfa2004-04-08 07:43:45 +000096 new->backlink = -1;
paul718e3742002-12-13 20:15:29 +000097
98 return new;
99}
100
101void
102ospf_vertex_free (struct vertex *v)
103{
104 listnode node;
105
106 list_delete (v->child);
107
108 if (listcount (v->nexthop) > 0)
109 for (node = listhead (v->nexthop); node; nextnode (node))
110 vertex_nexthop_free (node->data);
111
112 list_delete (v->nexthop);
113
114 XFREE (MTYPE_OSPF_VERTEX, v);
115}
116
117void
118ospf_vertex_add_parent (struct vertex *v)
119{
120 struct vertex_nexthop *nh;
121 listnode node;
122
123 for (node = listhead (v->nexthop); node; nextnode (node))
124 {
125 nh = (struct vertex_nexthop *) getdata (node);
126
127 /* No need to add two links from the same parent. */
128 if (listnode_lookup (nh->parent->child, v) == NULL)
paul0c0f9cd2003-06-06 23:27:04 +0000129 listnode_add (nh->parent->child, v);
paul718e3742002-12-13 20:15:29 +0000130 }
131}
132
133void
134ospf_spf_init (struct ospf_area *area)
135{
136 struct vertex *v;
137
138 /* Create root node. */
139 v = ospf_vertex_new (area->router_lsa_self);
140
141 area->spf = v;
142
143 /* Reset ABR and ASBR router counts. */
144 area->abr_count = 0;
145 area->asbr_count = 0;
146}
147
148int
149ospf_spf_has_vertex (struct route_table *rv, struct route_table *nv,
150 struct lsa_header *lsa)
151{
152 struct prefix p;
153 struct route_node *rn;
154
155 p.family = AF_INET;
156 p.prefixlen = IPV4_MAX_BITLEN;
157 p.u.prefix4 = lsa->id;
158
159 if (lsa->type == OSPF_ROUTER_LSA)
160 rn = route_node_get (rv, &p);
161 else
162 rn = route_node_get (nv, &p);
163
164 if (rn->info != NULL)
165 {
166 route_unlock_node (rn);
167 return 1;
168 }
169 return 0;
170}
171
172listnode
173ospf_vertex_lookup (list vlist, struct in_addr id, int type)
174{
175 listnode node;
176 struct vertex *v;
177
178 for (node = listhead (vlist); node; nextnode (node))
179 {
180 v = (struct vertex *) getdata (node);
181 if (IPV4_ADDR_SAME (&id, &v->id) && type == v->type)
182 return node;
183 }
184
185 return NULL;
186}
187
pauld355bfa2004-04-08 07:43:45 +0000188/* return index of link back to V from W, or -1 if no link found */
paul718e3742002-12-13 20:15:29 +0000189int
190ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v)
191{
192 int i;
193 int length;
194 struct router_lsa *rl;
195 struct network_lsa *nl;
196
197 /* In case of W is Network LSA. */
198 if (w->type == OSPF_NETWORK_LSA)
199 {
200 if (v->type == OSPF_NETWORK_LSA)
pauld355bfa2004-04-08 07:43:45 +0000201 return -1;
paul718e3742002-12-13 20:15:29 +0000202
203 nl = (struct network_lsa *) w;
204 length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
paul0c0f9cd2003-06-06 23:27:04 +0000205
paul718e3742002-12-13 20:15:29 +0000206 for (i = 0; i < length; i++)
207 if (IPV4_ADDR_SAME (&nl->routers[i], &v->id))
pauld355bfa2004-04-08 07:43:45 +0000208 return i;
209 return -1;
paul718e3742002-12-13 20:15:29 +0000210 }
211
212 /* In case of W is Router LSA. */
213 if (w->type == OSPF_ROUTER_LSA)
214 {
215 rl = (struct router_lsa *) w;
216
217 length = ntohs (w->length);
218
219 for (i = 0;
paul0c0f9cd2003-06-06 23:27:04 +0000220 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
221 i++, length -= 12)
paul718e3742002-12-13 20:15:29 +0000222 {
223 switch (rl->link[i].type)
224 {
225 case LSA_LINK_TYPE_POINTOPOINT:
226 case LSA_LINK_TYPE_VIRTUALLINK:
227 /* Router LSA ID. */
228 if (v->type == OSPF_ROUTER_LSA &&
229 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
230 {
pauld355bfa2004-04-08 07:43:45 +0000231 return i;
paul718e3742002-12-13 20:15:29 +0000232 }
233 break;
234 case LSA_LINK_TYPE_TRANSIT:
235 /* Network LSA ID. */
236 if (v->type == OSPF_NETWORK_LSA &&
237 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
238 {
pauld355bfa2004-04-08 07:43:45 +0000239 return i;
paul0c0f9cd2003-06-06 23:27:04 +0000240 }
paul718e3742002-12-13 20:15:29 +0000241 break;
242 case LSA_LINK_TYPE_STUB:
243 /* Not take into count? */
244 continue;
245 default:
246 break;
247 }
248 }
249 }
pauld355bfa2004-04-08 07:43:45 +0000250 return -1;
paul718e3742002-12-13 20:15:29 +0000251}
252
253/* Add the nexthop to the list, only if it is unique.
254 * If it's not unique, free the nexthop entry.
255 */
256void
257ospf_nexthop_add_unique (struct vertex_nexthop *new, list nexthop)
258{
259 struct vertex_nexthop *nh;
260 listnode node;
261 int match;
262
263 match = 0;
264 for (node = listhead (nexthop); node; nextnode (node))
265 {
266 nh = node->data;
267
268 /* Compare the two entries. */
269 /* XXX
270 * Comparing the parent preserves the shortest path tree
271 * structure even when the nexthops are identical.
272 */
273 if (nh->oi == new->oi &&
paul0c0f9cd2003-06-06 23:27:04 +0000274 IPV4_ADDR_SAME (&nh->router, &new->router) &&
275 nh->parent == new->parent)
276 {
277 match = 1;
278 break;
279 }
paul718e3742002-12-13 20:15:29 +0000280 }
281
282 if (!match)
283 listnode_add (nexthop, new);
284 else
285 vertex_nexthop_free (new);
286}
287
288/* Merge entries in list b into list a. */
289void
290ospf_nexthop_merge (list a, list b)
291{
292 struct listnode *n;
293
294 for (n = listhead (b); n; nextnode (n))
295 {
296 ospf_nexthop_add_unique (n->data, a);
297 }
298}
299
300#define ROUTER_LSA_MIN_SIZE 12
301#define ROUTER_LSA_TOS_SIZE 4
302
303struct router_lsa_link *
304ospf_get_next_link (struct vertex *v, struct vertex *w,
paul0c0f9cd2003-06-06 23:27:04 +0000305 struct router_lsa_link *prev_link)
paul718e3742002-12-13 20:15:29 +0000306{
307 u_char *p;
308 u_char *lim;
309 struct router_lsa_link *l;
310
311 if (prev_link == NULL)
312 p = ((u_char *) v->lsa) + 24;
313 else
314 {
paul0c0f9cd2003-06-06 23:27:04 +0000315 p = (u_char *) prev_link;
paul718e3742002-12-13 20:15:29 +0000316 p += (ROUTER_LSA_MIN_SIZE +
317 (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
318 }
paul0c0f9cd2003-06-06 23:27:04 +0000319
paul718e3742002-12-13 20:15:29 +0000320 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
321
322 while (p < lim)
323 {
324 l = (struct router_lsa_link *) p;
325
paul0c0f9cd2003-06-06 23:27:04 +0000326 p += (ROUTER_LSA_MIN_SIZE + (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +0000327
328 if (l->m[0].type == LSA_LINK_TYPE_STUB)
329 continue;
330
331 /* Defer NH calculation via VLs until summaries from
332 transit areas area confidered */
333
334 if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK)
paul0c0f9cd2003-06-06 23:27:04 +0000335 continue;
paul718e3742002-12-13 20:15:29 +0000336
337 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
paul0c0f9cd2003-06-06 23:27:04 +0000338 return l;
paul718e3742002-12-13 20:15:29 +0000339 }
340
341 return NULL;
342}
343
paul75ee0b82004-08-05 09:10:31 +0000344/*
345 * Consider supplied next-hop for inclusion to the supplied list of
346 * equal-cost next-hops, adjust list as neccessary.
347 *
348 * (Discussed on GNU Zebra list 27 May 2003, [zebra 19184])
349 *
350 * Note that below is a bit of a hack, and limits ECMP to paths that go to
351 * same nexthop. Where as paths via inequal output_cost interfaces could
352 * still quite easily be ECMP due to remote cost differences.
353 *
354 * TODO: It really should be done by way of recording currently valid
355 * backlinks and determining the appropriate nexthops from the list of
356 * backlinks, or even simpler, just flushing nexthop list if we find a lower
357 * cost path to a candidate vertex in SPF, maybe.
paulbf9392c2003-06-06 23:23:36 +0000358 */
paul0c0f9cd2003-06-06 23:27:04 +0000359void
360ospf_spf_consider_nexthop (struct list *nexthops,
361 struct vertex_nexthop *newhop)
paulbf9392c2003-06-06 23:23:36 +0000362{
paulbf9392c2003-06-06 23:23:36 +0000363 struct vertex_nexthop *hop;
paul75ee0b82004-08-05 09:10:31 +0000364 struct listnode *ln, *nn;
paul0c0f9cd2003-06-06 23:27:04 +0000365
paul75ee0b82004-08-05 09:10:31 +0000366 /* nexthop list should contain only the set of nexthops that have the lowest
367 * equal cost
368 */
369 if (nexthops->head != NULL)
370 {
371 hop = getdata (nexthops->head);
372
373 /* weed out hops with higher cost than the newhop */
374 if (hop->oi->output_cost > newhop->oi->output_cost)
375 {
376 /* delete the existing nexthops */
377 for (ln = nexthops->head; ln; ln = nn)
378 {
379 nn = ln->next;
380 hop = getdata (ln);
381
382 listnode_delete (nexthops, hop);
383 vertex_nexthop_free (hop);
384 }
385 }
386 else if (hop->oi->output_cost < newhop->oi->output_cost)
paul0c0f9cd2003-06-06 23:27:04 +0000387 return;
paul75ee0b82004-08-05 09:10:31 +0000388 }
paul0c0f9cd2003-06-06 23:27:04 +0000389
paulbf9392c2003-06-06 23:23:36 +0000390 /* new hop is <= existing hops, add it */
paul0c0f9cd2003-06-06 23:27:04 +0000391 listnode_add (nexthops, newhop);
paulbf9392c2003-06-06 23:23:36 +0000392
393 return;
394}
395
paul718e3742002-12-13 20:15:29 +0000396/* Calculate nexthop from root to vertex W. */
397void
398ospf_nexthop_calculation (struct ospf_area *area,
399 struct vertex *v, struct vertex *w)
400{
401 listnode node;
402 struct vertex_nexthop *nh, *x;
403 struct ospf_interface *oi = NULL;
404 struct router_lsa_link *l = NULL;
paul0c0f9cd2003-06-06 23:27:04 +0000405
406
paul718e3742002-12-13 20:15:29 +0000407 if (IS_DEBUG_OSPF_EVENT)
408 zlog_info ("ospf_nexthop_calculation(): Start");
409
410 /* W's parent is root. */
411 if (v == area->spf)
412 {
413 if (w->type == OSPF_VERTEX_ROUTER)
paul0c0f9cd2003-06-06 23:27:04 +0000414 {
415 while ((l = ospf_get_next_link (v, w, l)))
416 {
417 struct router_lsa_link *l2 = NULL;
418
419 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT)
420 {
421 /* Check for PtMP, signified by PtP link V->W
422 with link_data our PtMP interface. */
paul68980082003-03-25 05:07:42 +0000423 oi = ospf_if_is_configured (area->ospf, &l->link_data);
paul7afa08d2002-12-13 20:59:45 +0000424 if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
paul0c0f9cd2003-06-06 23:27:04 +0000425 {
426 struct prefix_ipv4 la;
427 la.prefixlen = oi->address->prefixlen;
428 /* We link to them on PtMP interface
429 - find the interface on w */
430 while ((l2 = ospf_get_next_link (w, v, l2)))
431 {
432 la.prefix = l2->link_data;
433
434 if (prefix_cmp ((struct prefix *) &la,
435 oi->address) == 0)
436 /* link_data is on our PtMP network */
437 break;
438 }
439 }
440 else
441 {
442 while ((l2 = ospf_get_next_link (w, v, l2)))
443 {
444 oi = ospf_if_is_configured (area->ospf,
445 &(l2->link_data));
446
447 if (oi == NULL)
448 continue;
449
450 if (!IPV4_ADDR_SAME (&oi->address->u.prefix4,
451 &l->link_data))
452 continue;
453
454 break;
455 }
456 }
457
458 if (oi && l2)
459 {
460 nh = vertex_nexthop_new (v);
461 nh->oi = oi;
462 nh->router = l2->link_data;
463 ospf_spf_consider_nexthop (w->nexthop, nh);
464 }
465 }
466 }
467 }
paul718e3742002-12-13 20:15:29 +0000468 else
paul0c0f9cd2003-06-06 23:27:04 +0000469 {
470 while ((l = ospf_get_next_link (v, w, l)))
471 {
472 oi = ospf_if_is_configured (area->ospf, &(l->link_data));
473 if (oi)
474 {
475 nh = vertex_nexthop_new (v);
476 nh->oi = oi;
477 nh->router.s_addr = 0;
paul75ee0b82004-08-05 09:10:31 +0000478 ospf_spf_consider_nexthop (w->nexthop, nh);
paul0c0f9cd2003-06-06 23:27:04 +0000479 }
480 }
481 }
paul718e3742002-12-13 20:15:29 +0000482 return;
483 }
484 /* In case of W's parent is network connected to root. */
485 else if (v->type == OSPF_VERTEX_NETWORK)
486 {
487 for (node = listhead (v->nexthop); node; nextnode (node))
488 {
489 x = (struct vertex_nexthop *) getdata (node);
490 if (x->parent == area->spf)
491 {
paul0c0f9cd2003-06-06 23:27:04 +0000492 while ((l = ospf_get_next_link (w, v, l)))
493 {
494 nh = vertex_nexthop_new (v);
495 nh->oi = x->oi;
496 nh->router = l->link_data;
paul75ee0b82004-08-05 09:10:31 +0000497 ospf_spf_consider_nexthop (w->nexthop, nh);
paul0c0f9cd2003-06-06 23:27:04 +0000498 }
499 return;
500 }
paul718e3742002-12-13 20:15:29 +0000501 }
502 }
503
504 /* Inherit V's nexthop. */
505 for (node = listhead (v->nexthop); node; nextnode (node))
506 {
507 nh = vertex_nexthop_dup (node->data);
508 nh->parent = v;
509 ospf_nexthop_add_unique (nh, w->nexthop);
510 }
511}
512
513void
514ospf_install_candidate (list candidate, struct vertex *w)
515{
516 listnode node;
517 struct vertex *cw;
518
519 if (list_isempty (candidate))
520 {
521 listnode_add (candidate, w);
522 return;
523 }
524
525 /* Install vertex with sorting by distance. */
526 for (node = listhead (candidate); node; nextnode (node))
527 {
528 cw = (struct vertex *) getdata (node);
529 if (cw->distance > w->distance)
530 {
531 list_add_node_prev (candidate, node, w);
532 break;
533 }
534 else if (node->next == NULL)
535 {
536 list_add_node_next (candidate, node, w);
537 break;
538 }
539 }
540}
541
542/* RFC2328 Section 16.1 (2). */
543void
544ospf_spf_next (struct vertex *v, struct ospf_area *area,
paul0c0f9cd2003-06-06 23:27:04 +0000545 list candidate, struct route_table *rv, struct route_table *nv)
paul718e3742002-12-13 20:15:29 +0000546{
547 struct ospf_lsa *w_lsa = NULL;
548 struct vertex *w, *cw;
549 u_char *p;
550 u_char *lim;
551 struct router_lsa_link *l = NULL;
552 struct in_addr *r;
553 listnode node;
554 int type = 0;
555
556 /* If this is a router-LSA, and bit V of the router-LSA (see Section
557 A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */
558 if (v->type == OSPF_VERTEX_ROUTER)
559 {
560 if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa))
561 area->transit = OSPF_TRANSIT_TRUE;
562 }
563
564 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
paul0c0f9cd2003-06-06 23:27:04 +0000565 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
566
paul718e3742002-12-13 20:15:29 +0000567 while (p < lim)
568 {
pauld355bfa2004-04-08 07:43:45 +0000569 int link = -1; /* link index for w's back link */
570
paul718e3742002-12-13 20:15:29 +0000571 /* In case of V is Router-LSA. */
572 if (v->lsa->type == OSPF_ROUTER_LSA)
573 {
574 l = (struct router_lsa_link *) p;
575
paul0c0f9cd2003-06-06 23:27:04 +0000576 p += (ROUTER_LSA_MIN_SIZE +
paul718e3742002-12-13 20:15:29 +0000577 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
578
579 /* (a) If this is a link to a stub network, examine the next
580 link in V's LSA. Links to stub networks will be
581 considered in the second stage of the shortest path
582 calculation. */
583 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
584 continue;
585
586 /* (b) Otherwise, W is a transit vertex (router or transit
587 network). Look up the vertex W's LSA (router-LSA or
588 network-LSA) in Area A's link state database. */
589 switch (type)
590 {
591 case LSA_LINK_TYPE_POINTOPOINT:
592 case LSA_LINK_TYPE_VIRTUALLINK:
593 if (type == LSA_LINK_TYPE_VIRTUALLINK)
paul0c0f9cd2003-06-06 23:27:04 +0000594 {
595 if (IS_DEBUG_OSPF_EVENT)
596 zlog_info ("looking up LSA through VL: %s",
597 inet_ntoa (l->link_id));
598 }
paul718e3742002-12-13 20:15:29 +0000599
600 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
601 l->link_id);
602 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000603 {
604 if (IS_DEBUG_OSPF_EVENT)
605 zlog_info ("found the LSA");
606 }
paul718e3742002-12-13 20:15:29 +0000607 break;
608 case LSA_LINK_TYPE_TRANSIT:
paul0c0f9cd2003-06-06 23:27:04 +0000609 if (IS_DEBUG_OSPF_EVENT)
paul718e3742002-12-13 20:15:29 +0000610
paul0c0f9cd2003-06-06 23:27:04 +0000611 zlog_info ("Looking up Network LSA, ID: %s",
612 inet_ntoa (l->link_id));
paul718e3742002-12-13 20:15:29 +0000613 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
paul0c0f9cd2003-06-06 23:27:04 +0000614 l->link_id);
paul718e3742002-12-13 20:15:29 +0000615 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000616 if (IS_DEBUG_OSPF_EVENT)
617 zlog_info ("found the LSA");
paul718e3742002-12-13 20:15:29 +0000618 break;
619 default:
paul0c0f9cd2003-06-06 23:27:04 +0000620 zlog_warn ("Invalid LSA link type %d", type);
paul718e3742002-12-13 20:15:29 +0000621 continue;
622 }
623 }
624 else
625 {
626 /* In case of V is Network-LSA. */
paul0c0f9cd2003-06-06 23:27:04 +0000627 r = (struct in_addr *) p;
paul718e3742002-12-13 20:15:29 +0000628 p += sizeof (struct in_addr);
629
630 /* Lookup the vertex W's LSA. */
631 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
632 }
633
634 /* (b cont.) If the LSA does not exist, or its LS age is equal
635 to MaxAge, or it does not have a link back to vertex V,
636 examine the next link in V's LSA.[23] */
637 if (w_lsa == NULL)
638 continue;
639
640 if (IS_LSA_MAXAGE (w_lsa))
641 continue;
642
pauld355bfa2004-04-08 07:43:45 +0000643 if ( (link = ospf_lsa_has_link (w_lsa->data, v->lsa)) < 0 )
paul718e3742002-12-13 20:15:29 +0000644 {
paul0c0f9cd2003-06-06 23:27:04 +0000645 if (IS_DEBUG_OSPF_EVENT)
646 zlog_info ("The LSA doesn't have a link back");
paul718e3742002-12-13 20:15:29 +0000647 continue;
648 }
649
650 /* (c) If vertex W is already on the shortest-path tree, examine
651 the next link in the LSA. */
652 if (ospf_spf_has_vertex (rv, nv, w_lsa->data))
653 {
paul0c0f9cd2003-06-06 23:27:04 +0000654 if (IS_DEBUG_OSPF_EVENT)
655 zlog_info ("The LSA is already in SPF");
paul718e3742002-12-13 20:15:29 +0000656 continue;
657 }
658
659 /* (d) Calculate the link state cost D of the resulting path
660 from the root to vertex W. D is equal to the sum of the link
661 state cost of the (already calculated) shortest path to
662 vertex V and the advertised cost of the link between vertices
663 V and W. If D is: */
664
665 /* prepare vertex W. */
666 w = ospf_vertex_new (w_lsa);
667
pauld355bfa2004-04-08 07:43:45 +0000668 /* Save W's back link index number, for use by virtual links */
669 w->backlink = link;
670
paul718e3742002-12-13 20:15:29 +0000671 /* calculate link cost D. */
672 if (v->lsa->type == OSPF_ROUTER_LSA)
673 w->distance = v->distance + ntohs (l->m[0].metric);
674 else
675 w->distance = v->distance;
676
677 /* Is there already vertex W in candidate list? */
678 node = ospf_vertex_lookup (candidate, w->id, w->type);
679 if (node == NULL)
680 {
681 /* Calculate nexthop to W. */
682 ospf_nexthop_calculation (area, v, w);
683
684 ospf_install_candidate (candidate, w);
685 }
686 else
687 {
688 cw = (struct vertex *) getdata (node);
689
690 /* if D is greater than. */
691 if (cw->distance < w->distance)
692 {
693 ospf_vertex_free (w);
694 continue;
695 }
696 /* equal to. */
697 else if (cw->distance == w->distance)
698 {
699 /* Calculate nexthop to W. */
700 ospf_nexthop_calculation (area, v, w);
701 ospf_nexthop_merge (cw->nexthop, w->nexthop);
702 list_delete_all_node (w->nexthop);
703 ospf_vertex_free (w);
704 }
705 /* less than. */
706 else
707 {
708 /* Calculate nexthop. */
709 ospf_nexthop_calculation (area, v, w);
710
711 /* Remove old vertex from candidate list. */
712 ospf_vertex_free (cw);
713 listnode_delete (candidate, cw);
714
715 /* Install new to candidate. */
716 ospf_install_candidate (candidate, w);
717 }
718 }
719 }
720}
721
722/* Add vertex V to SPF tree. */
723void
724ospf_spf_register (struct vertex *v, struct route_table *rv,
paul0c0f9cd2003-06-06 23:27:04 +0000725 struct route_table *nv)
paul718e3742002-12-13 20:15:29 +0000726{
727 struct prefix p;
728 struct route_node *rn;
729
730 p.family = AF_INET;
731 p.prefixlen = IPV4_MAX_BITLEN;
732 p.u.prefix4 = v->id;
733
734 if (v->type == OSPF_VERTEX_ROUTER)
735 rn = route_node_get (rv, &p);
736 else
737 rn = route_node_get (nv, &p);
738
739 rn->info = v;
740}
741
742void
743ospf_spf_route_free (struct route_table *table)
744{
745 struct route_node *rn;
746 struct vertex *v;
747
748 for (rn = route_top (table); rn; rn = route_next (rn))
749 {
750 if ((v = rn->info))
paul0c0f9cd2003-06-06 23:27:04 +0000751 {
752 ospf_vertex_free (v);
753 rn->info = NULL;
754 }
paul718e3742002-12-13 20:15:29 +0000755
756 route_unlock_node (rn);
757 }
758
759 route_table_finish (table);
760}
761
762void
763ospf_spf_dump (struct vertex *v, int i)
764{
765 listnode cnode;
766 listnode nnode;
767 struct vertex_nexthop *nexthop;
768
769 if (v->type == OSPF_VERTEX_ROUTER)
770 {
771 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000772 zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +0000773 }
774 else
775 {
776 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
777 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000778 zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
779 ip_masklen (lsa->mask));
paul718e3742002-12-13 20:15:29 +0000780
781 for (nnode = listhead (v->nexthop); nnode; nextnode (nnode))
782 {
783 nexthop = getdata (nnode);
paul0c0f9cd2003-06-06 23:27:04 +0000784 if (IS_DEBUG_OSPF_EVENT)
785 zlog_info (" nexthop %s", inet_ntoa (nexthop->router));
paul718e3742002-12-13 20:15:29 +0000786 }
787 }
788
789 i++;
790
791 for (cnode = listhead (v->child); cnode; nextnode (cnode))
792 {
793 v = getdata (cnode);
794 ospf_spf_dump (v, i);
795 }
796}
797
798/* Second stage of SPF calculation. */
799void
paul0c0f9cd2003-06-06 23:27:04 +0000800ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v,
paul718e3742002-12-13 20:15:29 +0000801 struct route_table *rt)
802{
803 listnode cnode;
804 struct vertex *child;
805
806 if (IS_DEBUG_OSPF_EVENT)
807 zlog_info ("ospf_process_stub():processing stubs for area %s",
paul0c0f9cd2003-06-06 23:27:04 +0000808 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000809 if (v->type == OSPF_VERTEX_ROUTER)
810 {
811 u_char *p;
812 u_char *lim;
813 struct router_lsa_link *l;
814 struct router_lsa *rlsa;
815
paul0c0f9cd2003-06-06 23:27:04 +0000816 if (IS_DEBUG_OSPF_EVENT)
817 zlog_info ("ospf_process_stub():processing router LSA, id: %s",
818 inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +0000819 rlsa = (struct router_lsa *) v->lsa;
820
821
paul0c0f9cd2003-06-06 23:27:04 +0000822 if (IS_DEBUG_OSPF_EVENT)
823 zlog_info ("ospf_process_stub(): we have %d links to process",
824 ntohs (rlsa->links));
paul718e3742002-12-13 20:15:29 +0000825 p = ((u_char *) v->lsa) + 24;
826 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
827
828 while (p < lim)
829 {
830 l = (struct router_lsa_link *) p;
831
832 p += (ROUTER_LSA_MIN_SIZE +
833 (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
834
835 if (l->m[0].type == LSA_LINK_TYPE_STUB)
836 ospf_intra_add_stub (rt, l, v, area);
837 }
838 }
839
840 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000841 zlog_info ("children of V:");
paul718e3742002-12-13 20:15:29 +0000842 for (cnode = listhead (v->child); cnode; nextnode (cnode))
843 {
844 child = getdata (cnode);
paul0c0f9cd2003-06-06 23:27:04 +0000845 if (IS_DEBUG_OSPF_EVENT)
846 zlog_info (" child : %s", inet_ntoa (child->id));
paul718e3742002-12-13 20:15:29 +0000847 }
848
849 for (cnode = listhead (v->child); cnode; nextnode (cnode))
850 {
851 child = getdata (cnode);
852
853 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
paul0c0f9cd2003-06-06 23:27:04 +0000854 continue;
paul718e3742002-12-13 20:15:29 +0000855
856 ospf_spf_process_stubs (area, child, rt);
857
858 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
859 }
860}
861
862void
863ospf_rtrs_free (struct route_table *rtrs)
864{
865 struct route_node *rn;
866 list or_list;
867 listnode node;
868
869 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000870 zlog_info ("Route: Router Routing Table free");
paul718e3742002-12-13 20:15:29 +0000871
872 for (rn = route_top (rtrs); rn; rn = route_next (rn))
873 if ((or_list = rn->info) != NULL)
874 {
paul0c0f9cd2003-06-06 23:27:04 +0000875 for (node = listhead (or_list); node; nextnode (node))
876 ospf_route_free (node->data);
paul718e3742002-12-13 20:15:29 +0000877
paul0c0f9cd2003-06-06 23:27:04 +0000878 list_delete (or_list);
paul718e3742002-12-13 20:15:29 +0000879
paul0c0f9cd2003-06-06 23:27:04 +0000880 /* Unlock the node. */
881 rn->info = NULL;
882 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +0000883 }
884 route_table_finish (rtrs);
885}
886
887void
888ospf_rtrs_print (struct route_table *rtrs)
889{
890 struct route_node *rn;
891 list or_list;
892 listnode ln;
893 listnode pnode;
894 struct ospf_route *or;
895 struct ospf_path *path;
896 char buf1[BUFSIZ];
897 char buf2[BUFSIZ];
898
899 if (IS_DEBUG_OSPF_EVENT)
900 zlog_info ("ospf_rtrs_print() start");
901
902 for (rn = route_top (rtrs); rn; rn = route_next (rn))
903 if ((or_list = rn->info) != NULL)
904 for (ln = listhead (or_list); ln; nextnode (ln))
905 {
906 or = getdata (ln);
907
908 switch (or->path_type)
909 {
910 case OSPF_PATH_INTRA_AREA:
paul0c0f9cd2003-06-06 23:27:04 +0000911 if (IS_DEBUG_OSPF_EVENT)
912 zlog_info ("%s [%d] area: %s",
913 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
914 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
915 buf2, BUFSIZ));
paul718e3742002-12-13 20:15:29 +0000916 break;
917 case OSPF_PATH_INTER_AREA:
paul0c0f9cd2003-06-06 23:27:04 +0000918 if (IS_DEBUG_OSPF_EVENT)
919 zlog_info ("%s IA [%d] area: %s",
920 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
921 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
922 buf2, BUFSIZ));
paul718e3742002-12-13 20:15:29 +0000923 break;
924 default:
925 break;
926 }
927
paul96735ee2003-08-10 02:51:22 +0000928 for (pnode = listhead (or->paths); pnode; nextnode (pnode))
paul718e3742002-12-13 20:15:29 +0000929 {
930 path = getdata (pnode);
931 if (path->nexthop.s_addr == 0)
paul0c0f9cd2003-06-06 23:27:04 +0000932 {
933 if (IS_DEBUG_OSPF_EVENT)
934 zlog_info (" directly attached to %s\r\n",
935 IF_NAME (path->oi));
936 }
937 else
938 {
939 if (IS_DEBUG_OSPF_EVENT)
940 zlog_info (" via %s, %s\r\n",
941 inet_ntoa (path->nexthop), IF_NAME (path->oi));
942 }
paul718e3742002-12-13 20:15:29 +0000943 }
944 }
945
946 zlog_info ("ospf_rtrs_print() end");
947}
948
949/* Calculating the shortest-path tree for an area. */
950void
paul0c0f9cd2003-06-06 23:27:04 +0000951ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
paul718e3742002-12-13 20:15:29 +0000952 struct route_table *new_rtrs)
953{
954 list candidate;
955 listnode node;
956 struct vertex *v;
957 struct route_table *rv;
958 struct route_table *nv;
959
960 if (IS_DEBUG_OSPF_EVENT)
961 {
962 zlog_info ("ospf_spf_calculate: Start");
paul0c0f9cd2003-06-06 23:27:04 +0000963 zlog_info ("ospf_spf_calculate: running Dijkstra for area %s",
964 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000965 }
966
967 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
968 return this area's calculation. */
paul0c0f9cd2003-06-06 23:27:04 +0000969 if (!area->router_lsa_self)
paul718e3742002-12-13 20:15:29 +0000970 {
971 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +0000972 zlog_info ("ospf_spf_calculate: "
973 "Skip area %s's calculation due to empty router_lsa_self",
974 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +0000975 return;
976 }
977
978 /* RFC2328 16.1. (1). */
paul0c0f9cd2003-06-06 23:27:04 +0000979 /* Initialize the algorithm's data structures. */
paul718e3742002-12-13 20:15:29 +0000980 rv = route_table_init ();
981 nv = route_table_init ();
982
paul0c0f9cd2003-06-06 23:27:04 +0000983 /* Clear the list of candidate vertices. */
paul718e3742002-12-13 20:15:29 +0000984 candidate = list_new ();
985
986 /* Initialize the shortest-path tree to only the root (which is the
987 router doing the calculation). */
988 ospf_spf_init (area);
989 v = area->spf;
990 ospf_spf_register (v, rv, nv);
991
992 /* Set Area A's TransitCapability to FALSE. */
993 area->transit = OSPF_TRANSIT_FALSE;
994 area->shortcut_capability = 1;
995
996 for (;;)
997 {
998 /* RFC2328 16.1. (2). */
999 ospf_spf_next (v, area, candidate, rv, nv);
1000
1001 /* RFC2328 16.1. (3). */
1002 /* If at this step the candidate list is empty, the shortest-
1003 path tree (of transit vertices) has been completely built and
1004 this stage of the procedure terminates. */
1005 if (listcount (candidate) == 0)
1006 break;
1007
1008 /* Otherwise, choose the vertex belonging to the candidate list
1009 that is closest to the root, and add it to the shortest-path
1010 tree (removing it from the candidate list in the
paul0c0f9cd2003-06-06 23:27:04 +00001011 process). */
paul718e3742002-12-13 20:15:29 +00001012 node = listhead (candidate);
1013 v = getdata (node);
1014 ospf_vertex_add_parent (v);
1015
1016 /* Reveve from the candidate list. */
1017 listnode_delete (candidate, v);
1018
1019 /* Add to SPF tree. */
1020 ospf_spf_register (v, rv, nv);
1021
1022 /* Note that when there is a choice of vertices closest to the
1023 root, network vertices must be chosen before router vertices
1024 in order to necessarily find all equal-cost paths. */
1025 /* We don't do this at this moment, we should add the treatment
1026 above codes. -- kunihiro. */
1027
1028 /* RFC2328 16.1. (4). */
1029 if (v->type == OSPF_VERTEX_ROUTER)
1030 ospf_intra_add_router (new_rtrs, v, area);
paul0c0f9cd2003-06-06 23:27:04 +00001031 else
paul718e3742002-12-13 20:15:29 +00001032 ospf_intra_add_transit (new_table, v, area);
1033
1034 /* RFC2328 16.1. (5). */
1035 /* Iterate the algorithm by returning to Step 2. */
1036 }
1037
1038 if (IS_DEBUG_OSPF_EVENT)
1039 {
1040 ospf_spf_dump (area->spf, 0);
1041 ospf_route_table_dump (new_table);
1042 }
1043
1044 /* Second stage of SPF calculation procedure's */
1045 ospf_spf_process_stubs (area, area->spf, new_table);
1046
1047 /* Free all vertices which allocated for SPF calculation */
1048 ospf_spf_route_free (rv);
1049 ospf_spf_route_free (nv);
1050
1051 /* Free candidate list */
1052 list_free (candidate);
1053
1054 /* Increment SPF Calculation Counter. */
1055 area->spf_calculation++;
1056
paul68980082003-03-25 05:07:42 +00001057 area->ospf->ts_spf = time (NULL);
paul718e3742002-12-13 20:15:29 +00001058
1059 if (IS_DEBUG_OSPF_EVENT)
1060 zlog_info ("ospf_spf_calculate: Stop");
1061}
1062
1063/* Timer for SPF calculation. */
1064int
paul68980082003-03-25 05:07:42 +00001065ospf_spf_calculate_timer (struct thread *thread)
paul718e3742002-12-13 20:15:29 +00001066{
paul68980082003-03-25 05:07:42 +00001067 struct ospf *ospf = THREAD_ARG (thread);
paul718e3742002-12-13 20:15:29 +00001068 struct route_table *new_table, *new_rtrs;
paul718e3742002-12-13 20:15:29 +00001069 listnode node;
1070
1071 if (IS_DEBUG_OSPF_EVENT)
1072 zlog_info ("SPF: Timer (SPF calculation expire)");
paul0c0f9cd2003-06-06 23:27:04 +00001073
paul718e3742002-12-13 20:15:29 +00001074 ospf->t_spf_calc = NULL;
1075
1076 /* Allocate new table tree. */
1077 new_table = route_table_init ();
paul0c0f9cd2003-06-06 23:27:04 +00001078 new_rtrs = route_table_init ();
paul718e3742002-12-13 20:15:29 +00001079
paul68980082003-03-25 05:07:42 +00001080 ospf_vl_unapprove (ospf);
paul718e3742002-12-13 20:15:29 +00001081
1082 /* Calculate SPF for each area. */
1083 for (node = listhead (ospf->areas); node; node = nextnode (node))
1084 ospf_spf_calculate (node->data, new_table, new_rtrs);
1085
paul68980082003-03-25 05:07:42 +00001086 ospf_vl_shut_unapproved (ospf);
paul718e3742002-12-13 20:15:29 +00001087
paul68980082003-03-25 05:07:42 +00001088 ospf_ia_routing (ospf, new_table, new_rtrs);
paul718e3742002-12-13 20:15:29 +00001089
1090 ospf_prune_unreachable_networks (new_table);
1091 ospf_prune_unreachable_routers (new_rtrs);
1092
1093 /* AS-external-LSA calculation should not be performed here. */
1094
1095 /* If new Router Route is installed,
1096 then schedule re-calculate External routes. */
1097 if (1)
paul68980082003-03-25 05:07:42 +00001098 ospf_ase_calculate_schedule (ospf);
paul718e3742002-12-13 20:15:29 +00001099
paul68980082003-03-25 05:07:42 +00001100 ospf_ase_calculate_timer_add (ospf);
paul718e3742002-12-13 20:15:29 +00001101
1102 /* Update routing table. */
paul68980082003-03-25 05:07:42 +00001103 ospf_route_install (ospf, new_table);
paul718e3742002-12-13 20:15:29 +00001104
1105 /* Update ABR/ASBR routing table */
paul68980082003-03-25 05:07:42 +00001106 if (ospf->old_rtrs)
paul718e3742002-12-13 20:15:29 +00001107 {
1108 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
paul68980082003-03-25 05:07:42 +00001109 /* ospf_route_delete (ospf->old_rtrs); */
1110 ospf_rtrs_free (ospf->old_rtrs);
paul718e3742002-12-13 20:15:29 +00001111 }
1112
paul68980082003-03-25 05:07:42 +00001113 ospf->old_rtrs = ospf->new_rtrs;
1114 ospf->new_rtrs = new_rtrs;
paul718e3742002-12-13 20:15:29 +00001115
paul0c0f9cd2003-06-06 23:27:04 +00001116 if (IS_OSPF_ABR (ospf))
paul68980082003-03-25 05:07:42 +00001117 ospf_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001118
1119 if (IS_DEBUG_OSPF_EVENT)
1120 zlog_info ("SPF: calculation complete");
1121
1122 return 0;
1123}
1124
1125/* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
1126 set timer for SPF calc. */
1127void
paul68980082003-03-25 05:07:42 +00001128ospf_spf_calculate_schedule (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001129{
1130 time_t ht, delay;
1131
1132 if (IS_DEBUG_OSPF_EVENT)
1133 zlog_info ("SPF: calculation timer scheduled");
1134
1135 /* OSPF instance does not exist. */
paul68980082003-03-25 05:07:42 +00001136 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001137 return;
1138
1139 /* SPF calculation timer is already scheduled. */
paul68980082003-03-25 05:07:42 +00001140 if (ospf->t_spf_calc)
paul718e3742002-12-13 20:15:29 +00001141 {
1142 if (IS_DEBUG_OSPF_EVENT)
paul0c0f9cd2003-06-06 23:27:04 +00001143 zlog_info ("SPF: calculation timer is already scheduled: %p",
1144 ospf->t_spf_calc);
paul718e3742002-12-13 20:15:29 +00001145 return;
1146 }
1147
paul68980082003-03-25 05:07:42 +00001148 ht = time (NULL) - ospf->ts_spf;
paul718e3742002-12-13 20:15:29 +00001149
1150 /* Get SPF calculation delay time. */
paul68980082003-03-25 05:07:42 +00001151 if (ht < ospf->spf_holdtime)
paul718e3742002-12-13 20:15:29 +00001152 {
paul68980082003-03-25 05:07:42 +00001153 if (ospf->spf_holdtime - ht < ospf->spf_delay)
paul0c0f9cd2003-06-06 23:27:04 +00001154 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001155 else
paul0c0f9cd2003-06-06 23:27:04 +00001156 delay = ospf->spf_holdtime - ht;
paul718e3742002-12-13 20:15:29 +00001157 }
1158 else
paul68980082003-03-25 05:07:42 +00001159 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001160
1161 if (IS_DEBUG_OSPF_EVENT)
hassofa2b17e2004-03-04 17:45:00 +00001162 zlog_info ("SPF: calculation timer delay = %ld", (long)delay);
paul68980082003-03-25 05:07:42 +00001163 ospf->t_spf_calc =
1164 thread_add_timer (master, ospf_spf_calculate_timer, ospf, delay);
paul718e3742002-12-13 20:15:29 +00001165}