]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_spf.c
Merge pull request #531 from qlyoung/fix-stack-ref
[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
cbf3e3eb 23#include "monotime.h"
718e3744 24#include "thread.h"
25#include "memory.h"
26#include "hash.h"
27#include "linklist.h"
28#include "prefix.h"
29#include "if.h"
30#include "table.h"
31#include "log.h"
32#include "sockunion.h" /* for inet_ntop () */
462f20d5 33#include "pqueue.h"
718e3744 34
35#include "ospfd/ospfd.h"
36#include "ospfd/ospf_interface.h"
37#include "ospfd/ospf_ism.h"
38#include "ospfd/ospf_asbr.h"
39#include "ospfd/ospf_lsa.h"
40#include "ospfd/ospf_lsdb.h"
41#include "ospfd/ospf_neighbor.h"
42#include "ospfd/ospf_nsm.h"
43#include "ospfd/ospf_spf.h"
44#include "ospfd/ospf_route.h"
45#include "ospfd/ospf_ia.h"
46#include "ospfd/ospf_ase.h"
47#include "ospfd/ospf_abr.h"
48#include "ospfd/ospf_dump.h"
49
cf744958
DS
50/* Variables to ensure a SPF scheduled log message is printed only once */
51
52static unsigned int spf_reason_flags = 0;
53
d3a9c768
PJ
54static void
55ospf_clear_spf_reason_flags (void)
cf744958
DS
56{
57 spf_reason_flags = 0;
58}
59
d3a9c768
PJ
60static void
61ospf_spf_set_reason (ospf_spf_reason_t reason)
cf744958 62{
d3a9c768 63 spf_reason_flags |= 1 << reason;
cf744958
DS
64}
65
66static void
67ospf_get_spf_reason_str (char *buf)
68{
d3a9c768
PJ
69 if (!buf)
70 return;
71
72 buf[0] = '\0';
73 if (spf_reason_flags)
cf744958 74 {
d3a9c768
PJ
75 if (spf_reason_flags & SPF_FLAG_ROUTER_LSA_INSTALL)
76 strcat (buf, "R, ");
77 if (spf_reason_flags & SPF_FLAG_NETWORK_LSA_INSTALL)
78 strcat (buf, "N, ");
79 if (spf_reason_flags & SPF_FLAG_SUMMARY_LSA_INSTALL)
80 strcat (buf, "S, ");
81 if (spf_reason_flags & SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL)
82 strcat (buf, "AS, ");
83 if (spf_reason_flags & SPF_FLAG_ABR_STATUS_CHANGE)
84 strcat (buf, "ABR, ");
85 if (spf_reason_flags & SPF_FLAG_ASBR_STATUS_CHANGE)
86 strcat (buf, "ASBR, ");
87 if (spf_reason_flags & SPF_FLAG_MAXAGE)
88 strcat (buf, "M, ");
cf744958
DS
89 buf[strlen(buf)-2] = '\0'; /* skip the last ", " */
90 }
91}
92
9c27ef9b
PJ
93static void ospf_vertex_free (void *);
94/* List of allocated vertices, to simplify cleanup of SPF.
95 * Not thread-safe obviously. If it ever needs to be, it'd have to be
96 * dynamically allocated at begin of ospf_spf_calculate
97 */
98static struct list vertex_list = { .del = ospf_vertex_free };
6b0655a2 99
462f20d5 100/* Heap related functions, for the managment of the candidates, to
101 * be used with pqueue. */
102static int
103cmp (void * node1 , void * node2)
104{
105 struct vertex * v1 = (struct vertex *) node1;
106 struct vertex * v2 = (struct vertex *) node2;
107 if (v1 != NULL && v2 != NULL )
9c27ef9b
PJ
108 {
109 /* network vertices must be chosen before router vertices of same
110 * cost in order to find all shortest paths
111 */
112 if ( ((v1->distance - v2->distance) == 0)
113 && (v1->type != v2->type))
114 {
115 switch (v1->type)
116 {
117 case OSPF_VERTEX_NETWORK:
118 return -1;
119 case OSPF_VERTEX_ROUTER:
120 return 1;
121 }
122 }
123 else
124 return (v1->distance - v2->distance);
125 }
126 return 0;
462f20d5 127}
128
129static void
eb3da6df 130update_stat (void *node , int position)
462f20d5 131{
eb3da6df 132 struct vertex *v = node;
133
462f20d5 134 /* Set the status of the vertex, when its position changes. */
135 *(v->stat) = position;
136}
6b0655a2 137
4dadc291 138static struct vertex_nexthop *
eb3da6df 139vertex_nexthop_new (void)
718e3744 140{
eb3da6df 141 return XCALLOC (MTYPE_OSPF_NEXTHOP, sizeof (struct vertex_nexthop));
718e3744 142}
143
4dadc291 144static void
718e3744 145vertex_nexthop_free (struct vertex_nexthop *nh)
146{
147 XFREE (MTYPE_OSPF_NEXTHOP, nh);
148}
149
eb3da6df 150/* Free the canonical nexthop objects for an area, ie the nexthop objects
9c27ef9b
PJ
151 * attached to the first-hop router vertices, and any intervening network
152 * vertices.
eb3da6df 153 */
154static void
155ospf_canonical_nexthops_free (struct vertex *root)
718e3744 156{
eb3da6df 157 struct listnode *node, *nnode;
158 struct vertex *child;
159
160 for (ALL_LIST_ELEMENTS (root->children, node, nnode, child))
161 {
162 struct listnode *n2, *nn2;
163 struct vertex_parent *vp;
164
58e1befe 165 /* router vertices through an attached network each
166 * have a distinct (canonical / not inherited) nexthop
167 * which must be freed.
168 *
169 * A network vertex can only have router vertices as its
170 * children, so only one level of recursion is possible.
171 */
eb3da6df 172 if (child->type == OSPF_VERTEX_NETWORK)
173 ospf_canonical_nexthops_free (child);
174
58e1befe 175 /* Free child nexthops pointing back to this root vertex */
eb3da6df 176 for (ALL_LIST_ELEMENTS (child->parents, n2, nn2, vp))
9c27ef9b 177 if (vp->parent == root && vp->nexthop)
58e1befe 178 vertex_nexthop_free (vp->nexthop);
eb3da6df 179 }
180}
6b0655a2 181
9c27ef9b
PJ
182/* TODO: Parent list should be excised, in favour of maintaining only
183 * vertex_nexthop, with refcounts.
184 */
eb3da6df 185static struct vertex_parent *
186vertex_parent_new (struct vertex *v, int backlink, struct vertex_nexthop *hop)
187{
188 struct vertex_parent *new;
189
190 new = XMALLOC (MTYPE_OSPF_VERTEX_PARENT, sizeof (struct vertex_parent));
191
192 if (new == NULL)
193 return NULL;
194
195 new->parent = v;
196 new->backlink = backlink;
197 new->nexthop = hop;
718e3744 198 return new;
199}
0c0f9cd5 200
eb3da6df 201static void
9c27ef9b 202vertex_parent_free (void *p)
eb3da6df 203{
204 XFREE (MTYPE_OSPF_VERTEX_PARENT, p);
205}
6b0655a2 206
4dadc291 207static struct vertex *
718e3744 208ospf_vertex_new (struct ospf_lsa *lsa)
209{
210 struct vertex *new;
211
eb3da6df 212 new = XCALLOC (MTYPE_OSPF_VERTEX, sizeof (struct vertex));
718e3744 213
214 new->flags = 0;
462f20d5 215 new->stat = &(lsa->stat);
718e3744 216 new->type = lsa->data->type;
217 new->id = lsa->data->id;
218 new->lsa = lsa->data;
eb3da6df 219 new->children = list_new ();
220 new->parents = list_new ();
9c27ef9b
PJ
221 new->parents->del = vertex_parent_free;
222
223 listnode_add (&vertex_list, new);
eb3da6df 224
9c27ef9b
PJ
225 if (IS_DEBUG_OSPF_EVENT)
226 zlog_debug ("%s: Created %s vertex %s", __func__,
227 new->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
228 inet_ntoa (new->lsa->id));
718e3744 229 return new;
230}
231
4dadc291 232static void
9c27ef9b 233ospf_vertex_free (void *data)
718e3744 234{
9c27ef9b 235 struct vertex *v = data;
eb3da6df 236
9c27ef9b
PJ
237 if (IS_DEBUG_OSPF_EVENT)
238 zlog_debug ("%s: Free %s vertex %s", __func__,
239 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
240 inet_ntoa (v->lsa->id));
7461d459 241
9c27ef9b
PJ
242 /* There should be no parents potentially holding references to this vertex
243 * Children however may still be there, but presumably referenced by other
244 * vertices
eb3da6df 245 */
9c27ef9b 246 //assert (listcount (v->parents) == 0);
eb3da6df 247
9c27ef9b
PJ
248 if (v->children)
249 list_delete (v->children);
250 v->children = NULL;
251
252 if (v->parents)
253 list_delete (v->parents);
eb3da6df 254 v->parents = NULL;
255
256 v->lsa = NULL;
7461d459 257
718e3744 258 XFREE (MTYPE_OSPF_VERTEX, v);
259}
260
4dadc291 261static void
eb1ce605 262ospf_vertex_dump(const char *msg, struct vertex *v,
eb3da6df 263 int print_parents, int print_children)
630e4807 264{
265 if ( ! IS_DEBUG_OSPF_EVENT)
266 return;
267
eb3da6df 268 zlog_debug("%s %s vertex %s distance %u flags %u",
630e4807 269 msg,
270 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
271 inet_ntoa(v->lsa->id),
272 v->distance,
630e4807 273 (unsigned int)v->flags);
274
eb3da6df 275 if (print_parents)
630e4807 276 {
1eb8ef25 277 struct listnode *node;
eb3da6df 278 struct vertex_parent *vp;
1eb8ef25 279
eb3da6df 280 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
630e4807 281 {
282 char buf1[BUFSIZ];
eb3da6df 283
284 if (vp)
630e4807 285 {
eb3da6df 286 zlog_debug ("parent %s backlink %d nexthop %s interface %s",
287 inet_ntoa(vp->parent->lsa->id), vp->backlink,
288 inet_ntop(AF_INET, &vp->nexthop->router, buf1, BUFSIZ),
289 vp->nexthop->oi ? IF_NAME(vp->nexthop->oi) : "NULL");
630e4807 290 }
291 }
292 }
293
294 if (print_children)
295 {
52dc7ee6 296 struct listnode *cnode;
1eb8ef25 297 struct vertex *cv;
298
eb3da6df 299 for (ALL_LIST_ELEMENTS_RO (v->children, cnode, cv))
1eb8ef25 300 ospf_vertex_dump(" child:", cv, 0, 0);
630e4807 301 }
302}
303
304
305/* Add a vertex to the list of children in each of its parents. */
4dadc291 306static void
718e3744 307ospf_vertex_add_parent (struct vertex *v)
308{
eb3da6df 309 struct vertex_parent *vp;
52dc7ee6 310 struct listnode *node;
7461d459 311
9c27ef9b 312 assert (v && v->parents);
7461d459 313
eb3da6df 314 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
718e3744 315 {
eb3da6df 316 assert (vp->parent && vp->parent->children);
7461d459 317
718e3744 318 /* No need to add two links from the same parent. */
eb3da6df 319 if (listnode_lookup (vp->parent->children, v) == NULL)
320 listnode_add (vp->parent->children, v);
718e3744 321 }
322}
6b0655a2 323
4dadc291 324static void
718e3744 325ospf_spf_init (struct ospf_area *area)
326{
327 struct vertex *v;
9c27ef9b 328
718e3744 329 /* Create root node. */
330 v = ospf_vertex_new (area->router_lsa_self);
bd34fb34 331
718e3744 332 area->spf = v;
333
334 /* Reset ABR and ASBR router counts. */
335 area->abr_count = 0;
336 area->asbr_count = 0;
337}
338
d355bfa7 339/* return index of link back to V from W, or -1 if no link found */
4dadc291 340static int
718e3744 341ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v)
342{
eb1ce605 343 unsigned int i, length;
718e3744 344 struct router_lsa *rl;
345 struct network_lsa *nl;
346
347 /* In case of W is Network LSA. */
348 if (w->type == OSPF_NETWORK_LSA)
349 {
350 if (v->type == OSPF_NETWORK_LSA)
d355bfa7 351 return -1;
718e3744 352
353 nl = (struct network_lsa *) w;
354 length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
0c0f9cd5 355
718e3744 356 for (i = 0; i < length; i++)
357 if (IPV4_ADDR_SAME (&nl->routers[i], &v->id))
d355bfa7 358 return i;
359 return -1;
718e3744 360 }
361
362 /* In case of W is Router LSA. */
363 if (w->type == OSPF_ROUTER_LSA)
364 {
365 rl = (struct router_lsa *) w;
366
367 length = ntohs (w->length);
368
369 for (i = 0;
0c0f9cd5 370 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
371 i++, length -= 12)
718e3744 372 {
373 switch (rl->link[i].type)
374 {
375 case LSA_LINK_TYPE_POINTOPOINT:
376 case LSA_LINK_TYPE_VIRTUALLINK:
377 /* Router LSA ID. */
378 if (v->type == OSPF_ROUTER_LSA &&
379 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
380 {
d355bfa7 381 return i;
718e3744 382 }
383 break;
384 case LSA_LINK_TYPE_TRANSIT:
385 /* Network LSA ID. */
386 if (v->type == OSPF_NETWORK_LSA &&
387 IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id))
388 {
d355bfa7 389 return i;
0c0f9cd5 390 }
718e3744 391 break;
392 case LSA_LINK_TYPE_STUB:
eb3da6df 393 /* Stub can't lead anywhere, carry on */
718e3744 394 continue;
395 default:
396 break;
397 }
398 }
399 }
d355bfa7 400 return -1;
718e3744 401}
402
630e4807 403/* Find the next link after prev_link from v to w. If prev_link is
404 * NULL, return the first link from v to w. Ignore stub and virtual links;
405 * these link types will never be returned.
406 */
4dadc291 407static struct router_lsa_link *
718e3744 408ospf_get_next_link (struct vertex *v, struct vertex *w,
0c0f9cd5 409 struct router_lsa_link *prev_link)
718e3744 410{
411 u_char *p;
412 u_char *lim;
bd540377 413 u_char lsa_type = LSA_LINK_TYPE_TRANSIT;
718e3744 414 struct router_lsa_link *l;
415
bd540377
JT
416 if (w->type == OSPF_VERTEX_ROUTER)
417 lsa_type = LSA_LINK_TYPE_POINTOPOINT;
418
718e3744 419 if (prev_link == NULL)
630e4807 420 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
718e3744 421 else
422 {
0c0f9cd5 423 p = (u_char *) prev_link;
05b7709d
DO
424 p += (OSPF_ROUTER_LSA_LINK_SIZE +
425 (prev_link->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
718e3744 426 }
0c0f9cd5 427
718e3744 428 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
429
430 while (p < lim)
431 {
432 l = (struct router_lsa_link *) p;
433
05b7709d 434 p += (OSPF_ROUTER_LSA_LINK_SIZE + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
718e3744 435
bd540377 436 if (l->m[0].type != lsa_type)
0c0f9cd5 437 continue;
718e3744 438
439 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
0c0f9cd5 440 return l;
718e3744 441 }
442
443 return NULL;
444}
445
bc20c1a4
PJ
446static void
447ospf_spf_flush_parents (struct vertex *w)
448{
449 struct vertex_parent *vp;
450 struct listnode *ln, *nn;
451
452 /* delete the existing nexthops */
453 for (ALL_LIST_ELEMENTS (w->parents, ln, nn, vp))
454 {
455 list_delete_node (w->parents, ln);
456 vertex_parent_free (vp);
457 }
458}
459
75ee0b8e 460/*
461 * Consider supplied next-hop for inclusion to the supplied list of
462 * equal-cost next-hops, adjust list as neccessary.
bf9392c6 463 */
4dadc291 464static void
eb3da6df 465ospf_spf_add_parent (struct vertex *v, struct vertex *w,
bc20c1a4 466 struct vertex_nexthop *newhop,
bd34fb34 467 unsigned int distance)
bf9392c6 468{
7b92589c
DL
469 struct vertex_parent *vp, *wp;
470 struct listnode *node;
eb3da6df 471
08d3d5b3 472 /* we must have a newhop, and a distance */
bd34fb34 473 assert (v && w && newhop);
08d3d5b3 474 assert (distance);
eb3da6df 475
08d3d5b3
PJ
476 /* IFF w has already been assigned a distance, then we shouldn't get here
477 * unless callers have determined V(l)->W is shortest / equal-shortest
478 * path (0 is a special case distance (no distance yet assigned)).
bc20c1a4 479 */
08d3d5b3
PJ
480 if (w->distance)
481 assert (distance <= w->distance);
482 else
483 w->distance = distance;
bc20c1a4 484
b75ae99e
PJ
485 if (IS_DEBUG_OSPF_EVENT)
486 {
487 char buf[2][INET_ADDRSTRLEN];
488 zlog_debug ("%s: Adding %s as parent of %s",
489 __func__,
490 inet_ntop(AF_INET, &v->lsa->id, buf[0], sizeof(buf[0])),
491 inet_ntop(AF_INET, &w->lsa->id, buf[1], sizeof(buf[1])));
492 }
493
bc20c1a4 494 /* Adding parent for a new, better path: flush existing parents from W. */
bd34fb34 495 if (distance < w->distance)
bc20c1a4 496 {
b75ae99e
PJ
497 if (IS_DEBUG_OSPF_EVENT)
498 zlog_debug ("%s: distance %d better than %d, flushing existing parents",
499 __func__, distance, w->distance);
bc20c1a4 500 ospf_spf_flush_parents (w);
bd34fb34 501 w->distance = distance;
bc20c1a4
PJ
502 }
503
7b92589c
DL
504 /* new parent is <= existing parents, add it to parent list (if nexthop
505 * not on parent list)
506 */
507 for (ALL_LIST_ELEMENTS_RO(w->parents, node, wp))
508 {
509 if (memcmp(newhop, wp->nexthop, sizeof(*newhop)) == 0)
510 {
511 if (IS_DEBUG_OSPF_EVENT)
512 zlog_debug ("%s: ... nexthop already on parent list, skipping add", __func__);
513 return;
514 }
515 }
516
eb3da6df 517 vp = vertex_parent_new (v, ospf_lsa_has_link (w->lsa, v->lsa), newhop);
518 listnode_add (w->parents, vp);
0c0f9cd5 519
eb3da6df 520 return;
521}
522
630e4807 523/* 16.1.1. Calculate nexthop from root through V (parent) to
bd34fb34 524 * vertex W (destination), with given distance from root->W.
eb3da6df 525 *
526 * The link must be supplied if V is the root vertex. In all other cases
527 * it may be NULL.
bd34fb34
PJ
528 *
529 * Note that this function may fail, hence the state of the destination
530 * vertex, W, should /not/ be modified in a dependent manner until
531 * this function returns. This function will update the W vertex with the
532 * provided distance as appropriate.
630e4807 533 */
bc20c1a4 534static unsigned int
eb3da6df 535ospf_nexthop_calculation (struct ospf_area *area, struct vertex *v,
bd34fb34 536 struct vertex *w, struct router_lsa_link *l,
c81ee5c9 537 unsigned int distance, int lsa_pos)
718e3744 538{
1eb8ef25 539 struct listnode *node, *nnode;
eb3da6df 540 struct vertex_nexthop *nh;
541 struct vertex_parent *vp;
718e3744 542 struct ospf_interface *oi = NULL;
bc20c1a4 543 unsigned int added = 0;
c81ee5c9
JT
544 char buf1[BUFSIZ];
545 char buf2[BUFSIZ];
0c0f9cd5 546
718e3744 547 if (IS_DEBUG_OSPF_EVENT)
630e4807 548 {
2a42e285 549 zlog_debug ("ospf_nexthop_calculation(): Start");
630e4807 550 ospf_vertex_dump("V (parent):", v, 1, 1);
551 ospf_vertex_dump("W (dest) :", w, 1, 1);
bd34fb34 552 zlog_debug ("V->W distance: %d", distance);
630e4807 553 }
718e3744 554
718e3744 555 if (v == area->spf)
9c27ef9b 556 {
630e4807 557 /* 16.1.1 para 4. In the first case, the parent vertex (V) is the
558 root (the calculating router itself). This means that the
559 destination is either a directly connected network or directly
560 connected router. The outgoing interface in this case is simply
561 the OSPF interface connecting to the destination network/router.
562 */
563
c81ee5c9
JT
564 /* we *must* be supplied with the link data */
565 assert (l != NULL);
566 oi = ospf_if_lookup_by_lsa_pos (area, lsa_pos);
567 if (!oi)
568 {
569 zlog_debug("%s: OI not found in LSA: lsa_pos:%d link_id:%s link_data:%s",
570 __func__, lsa_pos,
571 inet_ntop (AF_INET, &l->link_id, buf1, BUFSIZ),
572 inet_ntop (AF_INET, &l->link_data, buf2, BUFSIZ));
573 return 0;
574 }
575
576 if (IS_DEBUG_OSPF_EVENT)
577 {
578 zlog_debug("%s: considering link:%s "
579 "type:%d link_id:%s link_data:%s",
580 __func__, oi->ifp->name, l->m[0].type,
581 inet_ntop (AF_INET, &l->link_id, buf1, BUFSIZ),
582 inet_ntop (AF_INET, &l->link_data, buf2, BUFSIZ));
583 }
584
718e3744 585 if (w->type == OSPF_VERTEX_ROUTER)
0c0f9cd5 586 {
eb3da6df 587 /* l is a link from v to w
588 * l2 will be link from w to v
589 */
590 struct router_lsa_link *l2 = NULL;
eb3da6df 591
592 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT)
593 {
c5e0075f 594 struct in_addr nexthop = { .s_addr = 0 };
c81ee5c9 595
eb3da6df 596 /* If the destination is a router which connects to
597 the calculating router via a Point-to-MultiPoint
598 network, the destination's next hop IP address(es)
599 can be determined by examining the destination's
600 router-LSA: each link pointing back to the
601 calculating router and having a Link Data field
602 belonging to the Point-to-MultiPoint network
603 provides an IP address of the next hop router.
604
605 At this point l is a link from V to W, and V is the
c81ee5c9
JT
606 root ("us"). If it is a point-to-multipoint interface,
607 then look through the links in the opposite direction (W to V).
608 If any of them have an address that lands within the
eb3da6df 609 subnet declared by the PtMP link, then that link
c81ee5c9 610 is a constituent of the PtMP link, and its address is
eb3da6df 611 a nexthop address for V.
612 */
c81ee5c9
JT
613 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
614 {
f2b53dac
CF
615 /* Having nexthop = 0 is tempting, but NOT acceptable.
616 It breaks AS-External routes with a forwarding address,
617 since ospf_ase_complete_direct_routes() will mistakenly
618 assume we've reached the last hop and should place the
619 forwarding address as nexthop.
620 Also, users may configure multi-access links in p2p mode,
621 so we need the IP to ARP the nexthop.
622 */
623 struct ospf_neighbor *nbr_w;
624
625 nbr_w = ospf_nbr_lookup_by_routerid (oi->nbrs, &l->link_id);
626 if (nbr_w != NULL)
627 {
628 added = 1;
629 nexthop = nbr_w->src;
630 }
c81ee5c9
JT
631 }
632 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
633 {
634 struct prefix_ipv4 la;
635
636 la.family = AF_INET;
637 la.prefixlen = oi->address->prefixlen;
638
639 /* V links to W on PtMP interface
640 - find the interface address on W */
641 while ((l2 = ospf_get_next_link (w, v, l2)))
642 {
643 la.prefix = l2->link_data;
644
645 if (prefix_cmp ((struct prefix *) &la,
646 oi->address) != 0)
647 continue;
648 /* link_data is on our PtMP network */
649 added = 1;
650 nexthop = l2->link_data;
651 break;
652 }
653 }
654
655 if (added)
eb3da6df 656 {
657 /* found all necessary info to build nexthop */
658 nh = vertex_nexthop_new ();
659 nh->oi = oi;
c81ee5c9 660 nh->router = nexthop;
bd34fb34 661 ospf_spf_add_parent (v, w, nh, distance);
bc20c1a4 662 return 1;
eb3da6df 663 }
664 else
c81ee5c9
JT
665 zlog_info("%s: could not determine nexthop for link %s",
666 __func__, oi->ifp->name);
9c27ef9b
PJ
667 } /* end point-to-point link from V to W */
668 else if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK)
669 {
670 struct ospf_vl_data *vl_data;
671
672 /* VLink implementation limitations:
673 * a) vl_data can only reference one nexthop, so no ECMP
674 * to backbone through VLinks. Though transit-area
675 * summaries may be considered, and those can be ECMP.
676 * b) We can only use /one/ VLink, even if multiple ones
677 * exist this router through multiple transit-areas.
678 */
679 vl_data = ospf_vl_lookup (area->ospf, NULL, l->link_id);
680
681 if (vl_data
682 && CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
eb3da6df 683 {
9c27ef9b
PJ
684 nh = vertex_nexthop_new ();
685 nh->oi = vl_data->nexthop.oi;
686 nh->router = vl_data->nexthop.router;
bd34fb34 687 ospf_spf_add_parent (v, w, nh, distance);
bc20c1a4 688 return 1;
eb3da6df 689 }
9c27ef9b 690 else
bc20c1a4
PJ
691 zlog_info("ospf_nexthop_calculation(): "
692 "vl_data for VL link not found");
9c27ef9b 693 } /* end virtual-link from V to W */
bc20c1a4 694 return 0;
630e4807 695 } /* end W is a Router vertex */
718e3744 696 else
0c0f9cd5 697 {
eb3da6df 698 assert(w->type == OSPF_VERTEX_NETWORK);
c81ee5c9
JT
699
700 nh = vertex_nexthop_new ();
701 nh->oi = oi;
702 nh->router.s_addr = 0; /* Nexthop not required */
703 ospf_spf_add_parent (v, w, nh, distance);
704 return 1;
0c0f9cd5 705 }
630e4807 706 } /* end V is the root */
630e4807 707 /* Check if W's parent is a network connected to root. */
718e3744 708 else if (v->type == OSPF_VERTEX_NETWORK)
709 {
630e4807 710 /* See if any of V's parents are the root. */
eb3da6df 711 for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp))
718e3744 712 {
eb3da6df 713 if (vp->parent == area->spf) /* connects to root? */
630e4807 714 {
715 /* 16.1.1 para 5. ...the parent vertex is a network that
716 * directly connects the calculating router to the destination
717 * router. The list of next hops is then determined by
718 * examining the destination's router-LSA...
719 */
720
721 assert(w->type == OSPF_VERTEX_ROUTER);
0c0f9cd5 722 while ((l = ospf_get_next_link (w, v, l)))
723 {
630e4807 724 /* ...For each link in the router-LSA that points back to the
725 * parent network, the link's Link Data field provides the IP
726 * address of a next hop router. The outgoing interface to
727 * use can then be derived from the next hop IP address (or
728 * it can be inherited from the parent network).
729 */
eb3da6df 730 nh = vertex_nexthop_new ();
731 nh->oi = vp->nexthop->oi;
732 nh->router = l->link_data;
bc20c1a4 733 added = 1;
bd34fb34 734 ospf_spf_add_parent (v, w, nh, distance);
0c0f9cd5 735 }
945ea293
PJ
736 /* Note lack of return is deliberate. See next comment. */
737 }
718e3744 738 }
945ea293
PJ
739 /* NB: This code is non-trivial.
740 *
741 * E.g. it is not enough to know that V connects to the root. It is
742 * also important that the while above, looping through all links from
743 * W->V found at least one link, so that we know there is
744 * bi-directional connectivity between V and W (which need not be the
745 * case, e.g. when OSPF has not yet converged fully). Otherwise, if
746 * we /always/ return here, without having checked that root->V->-W
747 * actually resulted in a valid nexthop being created, then we we will
748 * prevent SPF from finding/using higher cost paths.
749 *
750 * It is important, if root->V->W has not been added, that we continue
751 * through to the intervening-router nexthop code below. So as to
752 * ensure other paths to V may be used. This avoids unnecessary
753 * blackholes while OSPF is convergening.
754 *
755 * I.e. we may have arrived at this function, examining V -> W, via
756 * workable paths other than root -> V, and it's important to avoid
757 * getting "confused" by non-working root->V->W path - it's important
758 * to *not* lose the working non-root paths, just because of a
759 * non-viable root->V->W.
760 *
761 * See also bug #330 (required reading!), and:
762 *
763 * http://blogs.oracle.com/paulj/entry/the_difference_a_line_makes
764 */
765 if (added)
766 return added;
718e3744 767 }
768
630e4807 769 /* 16.1.1 para 4. If there is at least one intervening router in the
770 * current shortest path between the destination and the root, the
771 * destination simply inherits the set of next hops from the
772 * parent.
773 */
b75ae99e
PJ
774 if (IS_DEBUG_OSPF_EVENT)
775 zlog_debug ("%s: Intervening routers, adding parent(s)", __func__);
776
eb3da6df 777 for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp))
bc20c1a4
PJ
778 {
779 added = 1;
bd34fb34 780 ospf_spf_add_parent (v, w, vp->nexthop, distance);
bc20c1a4 781 }
9c27ef9b 782
bc20c1a4 783 return added;
718e3744 784}
785
630e4807 786/* RFC2328 Section 16.1 (2).
787 * v is on the SPF tree. Examine the links in v's LSA. Update the list
788 * of candidates with any vertices not already on the list. If a lower-cost
789 * path is found to a vertex already on the candidate list, store the new cost.
790 */
4dadc291 791static void
718e3744 792ospf_spf_next (struct vertex *v, struct ospf_area *area,
462f20d5 793 struct pqueue * candidate)
718e3744 794{
795 struct ospf_lsa *w_lsa = NULL;
718e3744 796 u_char *p;
797 u_char *lim;
798 struct router_lsa_link *l = NULL;
799 struct in_addr *r;
c81ee5c9 800 int type = 0, lsa_pos=-1, lsa_pos_next=0;
718e3744 801
802 /* If this is a router-LSA, and bit V of the router-LSA (see Section
803 A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */
804 if (v->type == OSPF_VERTEX_ROUTER)
805 {
806 if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa))
807 area->transit = OSPF_TRANSIT_TRUE;
808 }
b75ae99e
PJ
809
810 if (IS_DEBUG_OSPF_EVENT)
811 zlog_debug ("%s: Next vertex of %s vertex %s",
812 __func__,
813 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
814 inet_ntoa(v->lsa->id));
815
718e3744 816 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
0c0f9cd5 817 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
818
718e3744 819 while (p < lim)
820 {
eb3da6df 821 struct vertex *w;
822 unsigned int distance;
d355bfa7 823
718e3744 824 /* In case of V is Router-LSA. */
825 if (v->lsa->type == OSPF_ROUTER_LSA)
826 {
827 l = (struct router_lsa_link *) p;
828
c81ee5c9
JT
829 lsa_pos = lsa_pos_next; /* LSA link position */
830 lsa_pos_next++;
05b7709d
DO
831 p += (OSPF_ROUTER_LSA_LINK_SIZE +
832 (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
718e3744 833
834 /* (a) If this is a link to a stub network, examine the next
835 link in V's LSA. Links to stub networks will be
836 considered in the second stage of the shortest path
837 calculation. */
838 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
839 continue;
08d3d5b3 840
718e3744 841 /* (b) Otherwise, W is a transit vertex (router or transit
842 network). Look up the vertex W's LSA (router-LSA or
843 network-LSA) in Area A's link state database. */
844 switch (type)
845 {
846 case LSA_LINK_TYPE_POINTOPOINT:
847 case LSA_LINK_TYPE_VIRTUALLINK:
848 if (type == LSA_LINK_TYPE_VIRTUALLINK)
0c0f9cd5 849 {
850 if (IS_DEBUG_OSPF_EVENT)
2a42e285 851 zlog_debug ("looking up LSA through VL: %s",
0c0f9cd5 852 inet_ntoa (l->link_id));
853 }
718e3744 854
855 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
856 l->link_id);
857 if (w_lsa)
0c0f9cd5 858 {
859 if (IS_DEBUG_OSPF_EVENT)
2a42e285 860 zlog_debug ("found Router LSA %s", inet_ntoa (l->link_id));
0c0f9cd5 861 }
718e3744 862 break;
863 case LSA_LINK_TYPE_TRANSIT:
0c0f9cd5 864 if (IS_DEBUG_OSPF_EVENT)
2a42e285 865 zlog_debug ("Looking up Network LSA, ID: %s",
0c0f9cd5 866 inet_ntoa (l->link_id));
718e3744 867 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
0c0f9cd5 868 l->link_id);
718e3744 869 if (w_lsa)
0c0f9cd5 870 if (IS_DEBUG_OSPF_EVENT)
2a42e285 871 zlog_debug ("found the LSA");
718e3744 872 break;
873 default:
0c0f9cd5 874 zlog_warn ("Invalid LSA link type %d", type);
718e3744 875 continue;
876 }
877 }
878 else
879 {
880 /* In case of V is Network-LSA. */
0c0f9cd5 881 r = (struct in_addr *) p;
718e3744 882 p += sizeof (struct in_addr);
883
884 /* Lookup the vertex W's LSA. */
885 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
b75ae99e
PJ
886 if (w_lsa)
887 {
888 if (IS_DEBUG_OSPF_EVENT)
889 zlog_debug ("found Router LSA %s", inet_ntoa (w_lsa->data->id));
890 }
718e3744 891 }
892
893 /* (b cont.) If the LSA does not exist, or its LS age is equal
894 to MaxAge, or it does not have a link back to vertex V,
895 examine the next link in V's LSA.[23] */
896 if (w_lsa == NULL)
b75ae99e
PJ
897 {
898 if (IS_DEBUG_OSPF_EVENT)
899 zlog_debug ("No LSA found");
900 continue;
901 }
718e3744 902
903 if (IS_LSA_MAXAGE (w_lsa))
b75ae99e
PJ
904 {
905 if (IS_DEBUG_OSPF_EVENT)
906 zlog_debug ("LSA is MaxAge");
907 continue;
908 }
718e3744 909
eb3da6df 910 if (ospf_lsa_has_link (w_lsa->data, v->lsa) < 0 )
718e3744 911 {
0c0f9cd5 912 if (IS_DEBUG_OSPF_EVENT)
2a42e285 913 zlog_debug ("The LSA doesn't have a link back");
718e3744 914 continue;
915 }
916
917 /* (c) If vertex W is already on the shortest-path tree, examine
918 the next link in the LSA. */
462f20d5 919 if (w_lsa->stat == LSA_SPF_IN_SPFTREE)
920 {
921 if (IS_DEBUG_OSPF_EVENT)
922 zlog_debug ("The LSA is already in SPF");
923 continue;
924 }
718e3744 925
926 /* (d) Calculate the link state cost D of the resulting path
927 from the root to vertex W. D is equal to the sum of the link
928 state cost of the (already calculated) shortest path to
929 vertex V and the advertised cost of the link between vertices
930 V and W. If D is: */
931
718e3744 932 /* calculate link cost D. */
933 if (v->lsa->type == OSPF_ROUTER_LSA)
eb3da6df 934 distance = v->distance + ntohs (l->m[0].metric);
630e4807 935 else /* v is not a Router-LSA */
eb3da6df 936 distance = v->distance;
718e3744 937
938 /* Is there already vertex W in candidate list? */
462f20d5 939 if (w_lsa->stat == LSA_SPF_NOT_EXPLORED)
940 {
eb3da6df 941 /* prepare vertex W. */
942 w = ospf_vertex_new (w_lsa);
943
462f20d5 944 /* Calculate nexthop to W. */
c81ee5c9 945 if (ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos))
bc20c1a4 946 pqueue_enqueue (w, candidate);
b75ae99e
PJ
947 else if (IS_DEBUG_OSPF_EVENT)
948 zlog_debug ("Nexthop Calc failed");
462f20d5 949 }
950 else if (w_lsa->stat >= 0)
951 {
952 /* Get the vertex from candidates. */
eb3da6df 953 w = candidate->array[w_lsa->stat];
718e3744 954
462f20d5 955 /* if D is greater than. */
eb3da6df 956 if (w->distance < distance)
718e3744 957 {
718e3744 958 continue;
959 }
462f20d5 960 /* equal to. */
eb3da6df 961 else if (w->distance == distance)
718e3744 962 {
eb3da6df 963 /* Found an equal-cost path to W.
964 * Calculate nexthop of to W from V. */
c81ee5c9 965 ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos);
718e3744 966 }
462f20d5 967 /* less than. */
968 else
718e3744 969 {
bc20c1a4
PJ
970 /* Found a lower-cost path to W.
971 * nexthop_calculation is conditional, if it finds
972 * valid nexthop it will call spf_add_parents, which
973 * will flush the old parents
974 */
c81ee5c9 975 if (ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos))
7591d8b8
PJ
976 /* Decrease the key of the node in the heap.
977 * trickle-sort it up towards root, just in case this
978 * node should now be the new root due the cost change.
e95537f0 979 * (next pqueu_{de,en}queue will fully re-heap the queue).
7591d8b8
PJ
980 */
981 trickle_up (w_lsa->stat, candidate);
718e3744 982 }
630e4807 983 } /* end W is already on the candidate list */
984 } /* end loop over the links in V's LSA */
718e3744 985}
986
4dadc291 987static void
718e3744 988ospf_spf_dump (struct vertex *v, int i)
989{
52dc7ee6 990 struct listnode *cnode;
991 struct listnode *nnode;
eb3da6df 992 struct vertex_parent *parent;
718e3744 993
994 if (v->type == OSPF_VERTEX_ROUTER)
995 {
996 if (IS_DEBUG_OSPF_EVENT)
2a42e285 997 zlog_debug ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
718e3744 998 }
999 else
1000 {
1001 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
1002 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1003 zlog_debug ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
0c0f9cd5 1004 ip_masklen (lsa->mask));
630e4807 1005 }
718e3744 1006
1eb8ef25 1007 if (IS_DEBUG_OSPF_EVENT)
eb3da6df 1008 for (ALL_LIST_ELEMENTS_RO (v->parents, nnode, parent))
1009 {
1010 zlog_debug (" nexthop %p %s %s",
6c4f4e6e 1011 (void *)parent->nexthop,
eb3da6df 1012 inet_ntoa (parent->nexthop->router),
1013 parent->nexthop->oi ? IF_NAME(parent->nexthop->oi)
1014 : "NULL");
1015 }
718e3744 1016
1017 i++;
1018
eb3da6df 1019 for (ALL_LIST_ELEMENTS_RO (v->children, cnode, v))
1eb8ef25 1020 ospf_spf_dump (v, i);
718e3744 1021}
1022
1023/* Second stage of SPF calculation. */
4dadc291 1024static void
0c0f9cd5 1025ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v,
b3bc68e5
PJ
1026 struct route_table *rt,
1027 int parent_is_root)
718e3744 1028{
1eb8ef25 1029 struct listnode *cnode, *cnnode;
718e3744 1030 struct vertex *child;
1031
1032 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1033 zlog_debug ("ospf_process_stub():processing stubs for area %s",
0c0f9cd5 1034 inet_ntoa (area->area_id));
718e3744 1035 if (v->type == OSPF_VERTEX_ROUTER)
1036 {
1037 u_char *p;
1038 u_char *lim;
1039 struct router_lsa_link *l;
1040 struct router_lsa *rlsa;
57c639f0 1041 int lsa_pos = 0;
718e3744 1042
0c0f9cd5 1043 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1044 zlog_debug ("ospf_process_stubs():processing router LSA, id: %s",
0c0f9cd5 1045 inet_ntoa (v->lsa->id));
718e3744 1046 rlsa = (struct router_lsa *) v->lsa;
1047
1048
0c0f9cd5 1049 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1050 zlog_debug ("ospf_process_stubs(): we have %d links to process",
0c0f9cd5 1051 ntohs (rlsa->links));
630e4807 1052 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
718e3744 1053 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
1054
1055 while (p < lim)
1056 {
1057 l = (struct router_lsa_link *) p;
1058
05b7709d
DO
1059 p += (OSPF_ROUTER_LSA_LINK_SIZE +
1060 (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
718e3744 1061
1062 if (l->m[0].type == LSA_LINK_TYPE_STUB)
57c639f0
JT
1063 ospf_intra_add_stub (rt, l, v, area, parent_is_root, lsa_pos);
1064 lsa_pos++;
718e3744 1065 }
1066 }
1067
630e4807 1068 ospf_vertex_dump("ospf_process_stubs(): after examining links: ", v, 1, 1);
718e3744 1069
eb3da6df 1070 for (ALL_LIST_ELEMENTS (v->children, cnode, cnnode, child))
718e3744 1071 {
718e3744 1072 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
0c0f9cd5 1073 continue;
b3bc68e5
PJ
1074
1075 /* the first level of routers connected to the root
1076 * should have 'parent_is_root' set, including those
1077 * connected via a network vertex.
1078 */
1079 if (area->spf == v)
1080 parent_is_root = 1;
1081 else if (v->type == OSPF_VERTEX_ROUTER)
1082 parent_is_root = 0;
1083
1084 ospf_spf_process_stubs (area, child, rt, parent_is_root);
718e3744 1085
1086 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
1087 }
1088}
1089
1090void
1091ospf_rtrs_free (struct route_table *rtrs)
1092{
1093 struct route_node *rn;
52dc7ee6 1094 struct list *or_list;
1eb8ef25 1095 struct ospf_route *or;
1096 struct listnode *node, *nnode;
718e3744 1097
1098 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1099 zlog_debug ("Route: Router Routing Table free");
718e3744 1100
1101 for (rn = route_top (rtrs); rn; rn = route_next (rn))
1102 if ((or_list = rn->info) != NULL)
1103 {
1eb8ef25 1104 for (ALL_LIST_ELEMENTS (or_list, node, nnode, or))
1105 ospf_route_free (or);
718e3744 1106
0c0f9cd5 1107 list_delete (or_list);
718e3744 1108
0c0f9cd5 1109 /* Unlock the node. */
1110 rn->info = NULL;
1111 route_unlock_node (rn);
718e3744 1112 }
1113 route_table_finish (rtrs);
1114}
1115
075e12f5 1116#if 0
4dadc291 1117static void
718e3744 1118ospf_rtrs_print (struct route_table *rtrs)
1119{
1120 struct route_node *rn;
52dc7ee6 1121 struct list *or_list;
1122 struct listnode *ln;
1123 struct listnode *pnode;
718e3744 1124 struct ospf_route *or;
1125 struct ospf_path *path;
1126 char buf1[BUFSIZ];
1127 char buf2[BUFSIZ];
1128
1129 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1130 zlog_debug ("ospf_rtrs_print() start");
718e3744 1131
1132 for (rn = route_top (rtrs); rn; rn = route_next (rn))
1133 if ((or_list = rn->info) != NULL)
1eb8ef25 1134 for (ALL_LIST_ELEMENTS_RO (or_list, ln, or))
718e3744 1135 {
718e3744 1136 switch (or->path_type)
1137 {
1138 case OSPF_PATH_INTRA_AREA:
0c0f9cd5 1139 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1140 zlog_debug ("%s [%d] area: %s",
0c0f9cd5 1141 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
1142 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
1143 buf2, BUFSIZ));
718e3744 1144 break;
1145 case OSPF_PATH_INTER_AREA:
0c0f9cd5 1146 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1147 zlog_debug ("%s IA [%d] area: %s",
0c0f9cd5 1148 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
1149 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
1150 buf2, BUFSIZ));
718e3744 1151 break;
1152 default:
1153 break;
1154 }
1155
1eb8ef25 1156 for (ALL_LIST_ELEMENTS_RO (or->paths, pnode, path))
718e3744 1157 {
718e3744 1158 if (path->nexthop.s_addr == 0)
0c0f9cd5 1159 {
1160 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1161 zlog_debug (" directly attached to %s\r\n",
baaea325 1162 ifindex2ifname (path->ifindex), VRF_DEFAULT);
0c0f9cd5 1163 }
1164 else
1165 {
1166 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1167 zlog_debug (" via %s, %s\r\n",
a8ba847f 1168 inet_ntoa (path->nexthop),
baaea325 1169 ifindex2ifname (path->ifindex), VRF_DEFAULT);
0c0f9cd5 1170 }
718e3744 1171 }
1172 }
1173
2a42e285 1174 zlog_debug ("ospf_rtrs_print() end");
718e3744 1175}
075e12f5 1176#endif
718e3744 1177
1178/* Calculating the shortest-path tree for an area. */
4dadc291 1179static void
0c0f9cd5 1180ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
718e3744 1181 struct route_table *new_rtrs)
1182{
462f20d5 1183 struct pqueue *candidate;
718e3744 1184 struct vertex *v;
eb3da6df 1185
718e3744 1186 if (IS_DEBUG_OSPF_EVENT)
1187 {
2a42e285 1188 zlog_debug ("ospf_spf_calculate: Start");
1189 zlog_debug ("ospf_spf_calculate: running Dijkstra for area %s",
0c0f9cd5 1190 inet_ntoa (area->area_id));
718e3744 1191 }
1192
1193 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
1194 return this area's calculation. */
0c0f9cd5 1195 if (!area->router_lsa_self)
718e3744 1196 {
1197 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1198 zlog_debug ("ospf_spf_calculate: "
0c0f9cd5 1199 "Skip area %s's calculation due to empty router_lsa_self",
1200 inet_ntoa (area->area_id));
718e3744 1201 return;
1202 }
1203
1204 /* RFC2328 16.1. (1). */
0c0f9cd5 1205 /* Initialize the algorithm's data structures. */
462f20d5 1206
1207 /* This function scans all the LSA database and set the stat field to
1208 * LSA_SPF_NOT_EXPLORED. */
1209 ospf_lsdb_clean_stat (area->lsdb);
1210 /* Create a new heap for the candidates. */
1211 candidate = pqueue_create();
1212 candidate->cmp = cmp;
1213 candidate->update = update_stat;
718e3744 1214
1215 /* Initialize the shortest-path tree to only the root (which is the
1216 router doing the calculation). */
1217 ospf_spf_init (area);
1218 v = area->spf;
462f20d5 1219 /* Set LSA position to LSA_SPF_IN_SPFTREE. This vertex is the root of the
1220 * spanning tree. */
1221 *(v->stat) = LSA_SPF_IN_SPFTREE;
718e3744 1222
1223 /* Set Area A's TransitCapability to FALSE. */
1224 area->transit = OSPF_TRANSIT_FALSE;
1225 area->shortcut_capability = 1;
eb3da6df 1226
718e3744 1227 for (;;)
1228 {
1229 /* RFC2328 16.1. (2). */
462f20d5 1230 ospf_spf_next (v, area, candidate);
718e3744 1231
1232 /* RFC2328 16.1. (3). */
1233 /* If at this step the candidate list is empty, the shortest-
1234 path tree (of transit vertices) has been completely built and
1235 this stage of the procedure terminates. */
462f20d5 1236 if (candidate->size == 0)
718e3744 1237 break;
1238
1239 /* Otherwise, choose the vertex belonging to the candidate list
1240 that is closest to the root, and add it to the shortest-path
1241 tree (removing it from the candidate list in the
0c0f9cd5 1242 process). */
462f20d5 1243 /* Extract from the candidates the node with the lower key. */
1244 v = (struct vertex *) pqueue_dequeue (candidate);
1245 /* Update stat field in vertex. */
1246 *(v->stat) = LSA_SPF_IN_SPFTREE;
eb3da6df 1247
718e3744 1248 ospf_vertex_add_parent (v);
1249
718e3744 1250 /* RFC2328 16.1. (4). */
1251 if (v->type == OSPF_VERTEX_ROUTER)
1252 ospf_intra_add_router (new_rtrs, v, area);
0c0f9cd5 1253 else
718e3744 1254 ospf_intra_add_transit (new_table, v, area);
1255
1256 /* RFC2328 16.1. (5). */
1257 /* Iterate the algorithm by returning to Step 2. */
630e4807 1258
1259 } /* end loop until no more candidate vertices */
718e3744 1260
1261 if (IS_DEBUG_OSPF_EVENT)
1262 {
1263 ospf_spf_dump (area->spf, 0);
1264 ospf_route_table_dump (new_table);
1265 }
1266
1267 /* Second stage of SPF calculation procedure's */
b3bc68e5 1268 ospf_spf_process_stubs (area, area->spf, new_table, 0);
718e3744 1269
eb3da6df 1270 /* Free candidate queue. */
462f20d5 1271 pqueue_delete (candidate);
cf744958 1272
eb3da6df 1273 ospf_vertex_dump (__func__, area->spf, 0, 1);
1274 /* Free nexthop information, canonical versions of which are attached
1275 * the first level of router vertices attached to the root vertex, see
1276 * ospf_nexthop_calculation.
1277 */
1278 ospf_canonical_nexthops_free (area->spf);
cf744958 1279
718e3744 1280 /* Increment SPF Calculation Counter. */
1281 area->spf_calculation++;
1282
cf672a86 1283 monotime(&area->ospf->ts_spf);
cf744958 1284 area->ts_spf = area->ospf->ts_spf;
718e3744 1285
1286 if (IS_DEBUG_OSPF_EVENT)
4fede82a 1287 zlog_debug ("ospf_spf_calculate: Stop. %zd vertices",
9c27ef9b 1288 mtype_stats_alloc(MTYPE_OSPF_VERTEX));
cf744958
DS
1289
1290 /* Free SPF vertices, but not the list. List has ospf_vertex_free
1291 * as deconstructor.
1292 */
1293 list_delete_all_node (&vertex_list);
718e3744 1294}
6b0655a2 1295
718e3744 1296/* Timer for SPF calculation. */
4dadc291 1297static int
68980084 1298ospf_spf_calculate_timer (struct thread *thread)
718e3744 1299{
68980084 1300 struct ospf *ospf = THREAD_ARG (thread);
718e3744 1301 struct route_table *new_table, *new_rtrs;
1eb8ef25 1302 struct ospf_area *area;
1303 struct listnode *node, *nnode;
816c2194 1304 struct timeval start_time, spf_start_time;
cf744958
DS
1305 int areas_processed = 0;
1306 unsigned long ia_time, prune_time, rt_time;
1307 unsigned long abr_time, total_spf_time, spf_time;
1308 char rbuf[32]; /* reason_buf */
d3a9c768 1309
718e3744 1310 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1311 zlog_debug ("SPF: Timer (SPF calculation expire)");
0c0f9cd5 1312
718e3744 1313 ospf->t_spf_calc = NULL;
1314
cf672a86 1315 monotime(&spf_start_time);
718e3744 1316 /* Allocate new table tree. */
1317 new_table = route_table_init ();
0c0f9cd5 1318 new_rtrs = route_table_init ();
718e3744 1319
68980084 1320 ospf_vl_unapprove (ospf);
718e3744 1321
1322 /* Calculate SPF for each area. */
1eb8ef25 1323 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
9c27ef9b
PJ
1324 {
1325 /* Do backbone last, so as to first discover intra-area paths
1326 * for any back-bone virtual-links
1327 */
1328 if (ospf->backbone && ospf->backbone == area)
1329 continue;
cf744958 1330
9c27ef9b 1331 ospf_spf_calculate (area, new_table, new_rtrs);
cf744958 1332 areas_processed++;
9c27ef9b 1333 }
cf744958 1334
9c27ef9b
PJ
1335 /* SPF for backbone, if required */
1336 if (ospf->backbone)
cf744958
DS
1337 {
1338 ospf_spf_calculate (ospf->backbone, new_table, new_rtrs);
1339 areas_processed++;
1340 }
1341
816c2194 1342 spf_time = monotime_since(&spf_start_time, NULL);
cf744958 1343
68980084 1344 ospf_vl_shut_unapproved (ospf);
718e3744 1345
816c2194 1346 monotime(&start_time);
68980084 1347 ospf_ia_routing (ospf, new_table, new_rtrs);
816c2194 1348 ia_time = monotime_since(&start_time, NULL);
cf744958 1349
cf672a86 1350 monotime(&start_time);
718e3744 1351 ospf_prune_unreachable_networks (new_table);
1352 ospf_prune_unreachable_routers (new_rtrs);
816c2194 1353 prune_time = monotime_since(&start_time, NULL);
718e3744 1354
1355 /* AS-external-LSA calculation should not be performed here. */
1356
1357 /* If new Router Route is installed,
1358 then schedule re-calculate External routes. */
1359 if (1)
68980084 1360 ospf_ase_calculate_schedule (ospf);
718e3744 1361
68980084 1362 ospf_ase_calculate_timer_add (ospf);
718e3744 1363
1364 /* Update routing table. */
816c2194 1365 monotime(&start_time);
68980084 1366 ospf_route_install (ospf, new_table);
816c2194 1367 rt_time = monotime_since(&start_time, NULL);
718e3744 1368
1369 /* Update ABR/ASBR routing table */
68980084 1370 if (ospf->old_rtrs)
718e3744 1371 {
1372 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
68980084 1373 /* ospf_route_delete (ospf->old_rtrs); */
1374 ospf_rtrs_free (ospf->old_rtrs);
718e3744 1375 }
1376
68980084 1377 ospf->old_rtrs = ospf->new_rtrs;
1378 ospf->new_rtrs = new_rtrs;
718e3744 1379
cf672a86 1380 monotime(&start_time);
0c0f9cd5 1381 if (IS_OSPF_ABR (ospf))
68980084 1382 ospf_abr_task (ospf);
816c2194 1383 abr_time = monotime_since(&start_time, NULL);
718e3744 1384
816c2194 1385 total_spf_time = monotime_since(&spf_start_time, &ospf->ts_spf_duration);
cf744958
DS
1386
1387 ospf_get_spf_reason_str (rbuf);
1388
d3a9c768
PJ
1389 if (IS_DEBUG_OSPF_EVENT)
1390 {
1391 zlog_info ("SPF Processing Time(usecs): %ld", total_spf_time);
1392 zlog_info ("\t SPF Time: %ld", spf_time);
1393 zlog_info ("\t InterArea: %ld", ia_time);
1394 zlog_info ("\t Prune: %ld", prune_time);
1395 zlog_info ("\tRouteInstall: %ld", rt_time);
1396 if (IS_OSPF_ABR (ospf))
1397 zlog_info ("\t ABR: %ld (%d areas)",
1398 abr_time, areas_processed);
1399 zlog_info ("Reason(s) for SPF: %s", rbuf);
1400 }
cf744958
DS
1401
1402 ospf_clear_spf_reason_flags ();
718e3744 1403
1404 return 0;
1405}
1406
1407/* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
1408 set timer for SPF calc. */
1409void
d3a9c768 1410ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason)
718e3744 1411{
d24f6e2a 1412 unsigned long delay, elapsed, ht;
718e3744 1413
1414 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1415 zlog_debug ("SPF: calculation timer scheduled");
718e3744 1416
1417 /* OSPF instance does not exist. */
68980084 1418 if (ospf == NULL)
718e3744 1419 return;
d24f6e2a 1420
d3a9c768
PJ
1421 ospf_spf_set_reason (reason);
1422
718e3744 1423 /* SPF calculation timer is already scheduled. */
68980084 1424 if (ospf->t_spf_calc)
718e3744 1425 {
1426 if (IS_DEBUG_OSPF_EVENT)
2a42e285 1427 zlog_debug ("SPF: calculation timer is already scheduled: %p",
6c4f4e6e 1428 (void *)ospf->t_spf_calc);
718e3744 1429 return;
1430 }
cbf3e3eb
DL
1431
1432 elapsed = monotime_since (&ospf->ts_spf, NULL) / 1000;
1433
d24f6e2a 1434 ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
1435
1436 if (ht > ospf->spf_max_holdtime)
1437 ht = ospf->spf_max_holdtime;
1438
718e3744 1439 /* Get SPF calculation delay time. */
d24f6e2a 1440 if (elapsed < ht)
718e3744 1441 {
d24f6e2a 1442 /* Got an event within the hold time of last SPF. We need to
1443 * increase the hold_multiplier, if it's not already at/past
1444 * maximum value, and wasn't already increased..
1445 */
1446 if (ht < ospf->spf_max_holdtime)
1447 ospf->spf_hold_multiplier++;
1448
1449 /* always honour the SPF initial delay */
1450 if ( (ht - elapsed) < ospf->spf_delay)
0c0f9cd5 1451 delay = ospf->spf_delay;
718e3744 1452 else
d24f6e2a 1453 delay = ht - elapsed;
718e3744 1454 }
1455 else
d24f6e2a 1456 {
1457 /* Event is past required hold-time of last SPF */
1458 delay = ospf->spf_delay;
1459 ospf->spf_hold_multiplier = 1;
1460 }
1461
718e3744 1462 if (IS_DEBUG_OSPF_EVENT)
d24f6e2a 1463 zlog_debug ("SPF: calculation timer delay = %ld", delay);
1464
cf744958
DS
1465 zlog_info ("SPF: Scheduled in %ld msec", delay);
1466
66e78ae6
QY
1467 ospf->t_spf_calc = NULL;
1468 thread_add_timer_msec(master, ospf_spf_calculate_timer, ospf, delay,
1469 &ospf->t_spf_calc);
718e3744 1470}