blob: a7155bc64a70ffc501f4bad03c5139515a9d3ea7 [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 () */
hasso462f20d2005-02-23 11:29:02 +000032#include "pqueue.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "ospfd/ospfd.h"
35#include "ospfd/ospf_interface.h"
36#include "ospfd/ospf_ism.h"
37#include "ospfd/ospf_asbr.h"
38#include "ospfd/ospf_lsa.h"
39#include "ospfd/ospf_lsdb.h"
40#include "ospfd/ospf_neighbor.h"
41#include "ospfd/ospf_nsm.h"
42#include "ospfd/ospf_spf.h"
43#include "ospfd/ospf_route.h"
44#include "ospfd/ospf_ia.h"
45#include "ospfd/ospf_ase.h"
46#include "ospfd/ospf_abr.h"
47#include "ospfd/ospf_dump.h"
48
Dinesh G Dutt50f38b32014-09-30 12:53:28 -070049/* Variables to ensure a SPF scheduled log message is printed only once */
50
51static unsigned int spf_reason_flags = 0;
52
53static void ospf_clear_spf_reason_flags ()
54{
55 spf_reason_flags = 0;
56}
57
58void ospf_flag_spf_reason (unsigned int reason)
59{
60 if (reason <= SPF_FLAG_MAX_VALUE)
61 spf_reason_flags |= reason;
62 else
63 spf_reason_flags |= SPF_FLAG_MISC;
64}
65
66static void
67ospf_get_spf_reason_str (char *buf)
68{
69 if (buf)
70 {
71 buf[0] = '\0';
72 if (spf_reason_flags)
73 {
74 if (spf_reason_flags & SPF_FLAG_ROUTER_LSA_INSTALL)
75 strcat (buf, "R, ");
76 if (spf_reason_flags & SPF_FLAG_NETWORK_LSA_INSTALL)
77 strcat (buf, "N, ");
78 if (spf_reason_flags & SPF_FLAG_SUMMARY_LSA_INSTALL)
79 strcat (buf, "S, ");
80 if (spf_reason_flags & SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL)
81 strcat (buf, "AS, ");
82 if (spf_reason_flags & SPF_FLAG_ABR_STATUS_CHANGE)
83 strcat (buf, "ABR, ");
84 if (spf_reason_flags & SPF_FLAG_ASBR_STATUS_CHANGE)
85 strcat (buf, "ASBR, ");
86 if (spf_reason_flags & SPF_FLAG_MAXAGE)
87 strcat (buf, "M, ");
88 }
89 buf[strlen(buf)-2] = '\0'; /* skip the last ", " */
90 }
91}
92
Paul Jakma9c27ef92006-05-04 07:32:57 +000093static 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 };
David Lamparter6b0655a2014-06-04 06:53:35 +020099
hasso462f20d2005-02-23 11:29:02 +0000100/* 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 )
Paul Jakma9c27ef92006-05-04 07:32:57 +0000108 {
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;
hasso462f20d2005-02-23 11:29:02 +0000127}
128
129static void
pauleb3da6d2005-10-18 04:20:33 +0000130update_stat (void *node , int position)
hasso462f20d2005-02-23 11:29:02 +0000131{
pauleb3da6d2005-10-18 04:20:33 +0000132 struct vertex *v = node;
133
hasso462f20d2005-02-23 11:29:02 +0000134 /* Set the status of the vertex, when its position changes. */
135 *(v->stat) = position;
136}
David Lamparter6b0655a2014-06-04 06:53:35 +0200137
paul4dadc292005-05-06 21:37:42 +0000138static struct vertex_nexthop *
pauleb3da6d2005-10-18 04:20:33 +0000139vertex_nexthop_new (void)
paul718e3742002-12-13 20:15:29 +0000140{
pauleb3da6d2005-10-18 04:20:33 +0000141 return XCALLOC (MTYPE_OSPF_NEXTHOP, sizeof (struct vertex_nexthop));
paul718e3742002-12-13 20:15:29 +0000142}
143
paul4dadc292005-05-06 21:37:42 +0000144static void
paul718e3742002-12-13 20:15:29 +0000145vertex_nexthop_free (struct vertex_nexthop *nh)
146{
147 XFREE (MTYPE_OSPF_NEXTHOP, nh);
148}
149
pauleb3da6d2005-10-18 04:20:33 +0000150/* Free the canonical nexthop objects for an area, ie the nexthop objects
Paul Jakma9c27ef92006-05-04 07:32:57 +0000151 * attached to the first-hop router vertices, and any intervening network
152 * vertices.
pauleb3da6d2005-10-18 04:20:33 +0000153 */
154static void
155ospf_canonical_nexthops_free (struct vertex *root)
paul718e3742002-12-13 20:15:29 +0000156{
pauleb3da6d2005-10-18 04:20:33 +0000157 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
paul58e1bef2005-11-11 12:10:03 +0000165 /* 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 */
pauleb3da6d2005-10-18 04:20:33 +0000172 if (child->type == OSPF_VERTEX_NETWORK)
173 ospf_canonical_nexthops_free (child);
174
paul58e1bef2005-11-11 12:10:03 +0000175 /* Free child nexthops pointing back to this root vertex */
pauleb3da6d2005-10-18 04:20:33 +0000176 for (ALL_LIST_ELEMENTS (child->parents, n2, nn2, vp))
Paul Jakma9c27ef92006-05-04 07:32:57 +0000177 if (vp->parent == root && vp->nexthop)
paul58e1bef2005-11-11 12:10:03 +0000178 vertex_nexthop_free (vp->nexthop);
pauleb3da6d2005-10-18 04:20:33 +0000179 }
180}
David Lamparter6b0655a2014-06-04 06:53:35 +0200181
Paul Jakma9c27ef92006-05-04 07:32:57 +0000182/* TODO: Parent list should be excised, in favour of maintaining only
183 * vertex_nexthop, with refcounts.
184 */
pauleb3da6d2005-10-18 04:20:33 +0000185static 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;
paul718e3742002-12-13 20:15:29 +0000198 return new;
199}
paul0c0f9cd2003-06-06 23:27:04 +0000200
pauleb3da6d2005-10-18 04:20:33 +0000201static void
Paul Jakma9c27ef92006-05-04 07:32:57 +0000202vertex_parent_free (void *p)
pauleb3da6d2005-10-18 04:20:33 +0000203{
204 XFREE (MTYPE_OSPF_VERTEX_PARENT, p);
205}
David Lamparter6b0655a2014-06-04 06:53:35 +0200206
paul4dadc292005-05-06 21:37:42 +0000207static struct vertex *
paul718e3742002-12-13 20:15:29 +0000208ospf_vertex_new (struct ospf_lsa *lsa)
209{
210 struct vertex *new;
211
pauleb3da6d2005-10-18 04:20:33 +0000212 new = XCALLOC (MTYPE_OSPF_VERTEX, sizeof (struct vertex));
paul718e3742002-12-13 20:15:29 +0000213
214 new->flags = 0;
hasso462f20d2005-02-23 11:29:02 +0000215 new->stat = &(lsa->stat);
paul718e3742002-12-13 20:15:29 +0000216 new->type = lsa->data->type;
217 new->id = lsa->data->id;
218 new->lsa = lsa->data;
pauleb3da6d2005-10-18 04:20:33 +0000219 new->children = list_new ();
220 new->parents = list_new ();
Paul Jakma9c27ef92006-05-04 07:32:57 +0000221 new->parents->del = vertex_parent_free;
pauleb3da6d2005-10-18 04:20:33 +0000222
Paul Jakma9c27ef92006-05-04 07:32:57 +0000223 listnode_add (&vertex_list, new);
224
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));
paul718e3742002-12-13 20:15:29 +0000229 return new;
230}
231
paul4dadc292005-05-06 21:37:42 +0000232static void
Paul Jakma9c27ef92006-05-04 07:32:57 +0000233ospf_vertex_free (void *data)
paul718e3742002-12-13 20:15:29 +0000234{
Paul Jakma9c27ef92006-05-04 07:32:57 +0000235 struct vertex *v = data;
paul7461d452005-06-13 13:57:16 +0000236
Paul Jakma9c27ef92006-05-04 07:32:57 +0000237 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));
pauleb3da6d2005-10-18 04:20:33 +0000241
Paul Jakma9c27ef92006-05-04 07:32:57 +0000242 /* 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
pauleb3da6d2005-10-18 04:20:33 +0000245 */
Paul Jakma9c27ef92006-05-04 07:32:57 +0000246 //assert (listcount (v->parents) == 0);
pauleb3da6d2005-10-18 04:20:33 +0000247
Paul Jakma9c27ef92006-05-04 07:32:57 +0000248 if (v->children)
249 list_delete (v->children);
250 v->children = NULL;
251
252 if (v->parents)
253 list_delete (v->parents);
pauleb3da6d2005-10-18 04:20:33 +0000254 v->parents = NULL;
255
256 v->lsa = NULL;
paul7461d452005-06-13 13:57:16 +0000257
paul718e3742002-12-13 20:15:29 +0000258 XFREE (MTYPE_OSPF_VERTEX, v);
259}
260
paul4dadc292005-05-06 21:37:42 +0000261static void
hassoeb1ce602004-10-08 08:17:22 +0000262ospf_vertex_dump(const char *msg, struct vertex *v,
pauleb3da6d2005-10-18 04:20:33 +0000263 int print_parents, int print_children)
gdt630e4802004-08-31 17:28:41 +0000264{
265 if ( ! IS_DEBUG_OSPF_EVENT)
266 return;
267
pauleb3da6d2005-10-18 04:20:33 +0000268 zlog_debug("%s %s vertex %s distance %u flags %u",
gdt630e4802004-08-31 17:28:41 +0000269 msg,
270 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
271 inet_ntoa(v->lsa->id),
272 v->distance,
gdt630e4802004-08-31 17:28:41 +0000273 (unsigned int)v->flags);
274
pauleb3da6d2005-10-18 04:20:33 +0000275 if (print_parents)
gdt630e4802004-08-31 17:28:41 +0000276 {
paul1eb8ef22005-04-07 07:30:20 +0000277 struct listnode *node;
pauleb3da6d2005-10-18 04:20:33 +0000278 struct vertex_parent *vp;
paul1eb8ef22005-04-07 07:30:20 +0000279
pauleb3da6d2005-10-18 04:20:33 +0000280 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
gdt630e4802004-08-31 17:28:41 +0000281 {
282 char buf1[BUFSIZ];
pauleb3da6d2005-10-18 04:20:33 +0000283
284 if (vp)
gdt630e4802004-08-31 17:28:41 +0000285 {
pauleb3da6d2005-10-18 04:20:33 +0000286 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");
gdt630e4802004-08-31 17:28:41 +0000290 }
291 }
292 }
293
294 if (print_children)
295 {
hasso52dc7ee2004-09-23 19:18:23 +0000296 struct listnode *cnode;
paul1eb8ef22005-04-07 07:30:20 +0000297 struct vertex *cv;
298
pauleb3da6d2005-10-18 04:20:33 +0000299 for (ALL_LIST_ELEMENTS_RO (v->children, cnode, cv))
paul1eb8ef22005-04-07 07:30:20 +0000300 ospf_vertex_dump(" child:", cv, 0, 0);
gdt630e4802004-08-31 17:28:41 +0000301 }
302}
303
304
305/* Add a vertex to the list of children in each of its parents. */
paul4dadc292005-05-06 21:37:42 +0000306static void
paul718e3742002-12-13 20:15:29 +0000307ospf_vertex_add_parent (struct vertex *v)
308{
pauleb3da6d2005-10-18 04:20:33 +0000309 struct vertex_parent *vp;
hasso52dc7ee2004-09-23 19:18:23 +0000310 struct listnode *node;
paul7461d452005-06-13 13:57:16 +0000311
Paul Jakma9c27ef92006-05-04 07:32:57 +0000312 assert (v && v->parents);
paul7461d452005-06-13 13:57:16 +0000313
pauleb3da6d2005-10-18 04:20:33 +0000314 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
paul718e3742002-12-13 20:15:29 +0000315 {
pauleb3da6d2005-10-18 04:20:33 +0000316 assert (vp->parent && vp->parent->children);
paul7461d452005-06-13 13:57:16 +0000317
paul718e3742002-12-13 20:15:29 +0000318 /* No need to add two links from the same parent. */
pauleb3da6d2005-10-18 04:20:33 +0000319 if (listnode_lookup (vp->parent->children, v) == NULL)
320 listnode_add (vp->parent->children, v);
paul718e3742002-12-13 20:15:29 +0000321 }
322}
David Lamparter6b0655a2014-06-04 06:53:35 +0200323
paul4dadc292005-05-06 21:37:42 +0000324static void
paul718e3742002-12-13 20:15:29 +0000325ospf_spf_init (struct ospf_area *area)
326{
327 struct vertex *v;
Paul Jakma9c27ef92006-05-04 07:32:57 +0000328
paul718e3742002-12-13 20:15:29 +0000329 /* Create root node. */
330 v = ospf_vertex_new (area->router_lsa_self);
Paul Jakmabd34fb32007-02-26 17:14:48 +0000331
paul718e3742002-12-13 20:15:29 +0000332 area->spf = v;
333
334 /* Reset ABR and ASBR router counts. */
335 area->abr_count = 0;
336 area->asbr_count = 0;
337}
338
pauld355bfa2004-04-08 07:43:45 +0000339/* return index of link back to V from W, or -1 if no link found */
paul4dadc292005-05-06 21:37:42 +0000340static int
paul718e3742002-12-13 20:15:29 +0000341ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v)
342{
hassoeb1ce602004-10-08 08:17:22 +0000343 unsigned int i, length;
paul718e3742002-12-13 20:15:29 +0000344 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)
pauld355bfa2004-04-08 07:43:45 +0000351 return -1;
paul718e3742002-12-13 20:15:29 +0000352
353 nl = (struct network_lsa *) w;
354 length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
paul0c0f9cd2003-06-06 23:27:04 +0000355
paul718e3742002-12-13 20:15:29 +0000356 for (i = 0; i < length; i++)
357 if (IPV4_ADDR_SAME (&nl->routers[i], &v->id))
pauld355bfa2004-04-08 07:43:45 +0000358 return i;
359 return -1;
paul718e3742002-12-13 20:15:29 +0000360 }
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;
paul0c0f9cd2003-06-06 23:27:04 +0000370 i < ntohs (rl->links) && length >= sizeof (struct router_lsa);
371 i++, length -= 12)
paul718e3742002-12-13 20:15:29 +0000372 {
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 {
pauld355bfa2004-04-08 07:43:45 +0000381 return i;
paul718e3742002-12-13 20:15:29 +0000382 }
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 {
pauld355bfa2004-04-08 07:43:45 +0000389 return i;
paul0c0f9cd2003-06-06 23:27:04 +0000390 }
paul718e3742002-12-13 20:15:29 +0000391 break;
392 case LSA_LINK_TYPE_STUB:
pauleb3da6d2005-10-18 04:20:33 +0000393 /* Stub can't lead anywhere, carry on */
paul718e3742002-12-13 20:15:29 +0000394 continue;
395 default:
396 break;
397 }
398 }
399 }
pauld355bfa2004-04-08 07:43:45 +0000400 return -1;
paul718e3742002-12-13 20:15:29 +0000401}
402
gdt630e4802004-08-31 17:28:41 +0000403/* 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 */
paul4dadc292005-05-06 21:37:42 +0000407static struct router_lsa_link *
paul718e3742002-12-13 20:15:29 +0000408ospf_get_next_link (struct vertex *v, struct vertex *w,
paul0c0f9cd2003-06-06 23:27:04 +0000409 struct router_lsa_link *prev_link)
paul718e3742002-12-13 20:15:29 +0000410{
411 u_char *p;
412 u_char *lim;
Joakim Tjernlundbd540372009-07-27 12:42:31 +0200413 u_char lsa_type = LSA_LINK_TYPE_TRANSIT;
paul718e3742002-12-13 20:15:29 +0000414 struct router_lsa_link *l;
415
Joakim Tjernlundbd540372009-07-27 12:42:31 +0200416 if (w->type == OSPF_VERTEX_ROUTER)
417 lsa_type = LSA_LINK_TYPE_POINTOPOINT;
418
paul718e3742002-12-13 20:15:29 +0000419 if (prev_link == NULL)
gdt630e4802004-08-31 17:28:41 +0000420 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
paul718e3742002-12-13 20:15:29 +0000421 else
422 {
paul0c0f9cd2003-06-06 23:27:04 +0000423 p = (u_char *) prev_link;
Denis Ovsienko05b77092011-08-23 11:36:27 +0400424 p += (OSPF_ROUTER_LSA_LINK_SIZE +
425 (prev_link->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +0000426 }
paul0c0f9cd2003-06-06 23:27:04 +0000427
paul718e3742002-12-13 20:15:29 +0000428 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
429
430 while (p < lim)
431 {
432 l = (struct router_lsa_link *) p;
433
Denis Ovsienko05b77092011-08-23 11:36:27 +0400434 p += (OSPF_ROUTER_LSA_LINK_SIZE + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +0000435
Joakim Tjernlundbd540372009-07-27 12:42:31 +0200436 if (l->m[0].type != lsa_type)
paul0c0f9cd2003-06-06 23:27:04 +0000437 continue;
paul718e3742002-12-13 20:15:29 +0000438
439 if (IPV4_ADDR_SAME (&l->link_id, &w->id))
paul0c0f9cd2003-06-06 23:27:04 +0000440 return l;
paul718e3742002-12-13 20:15:29 +0000441 }
442
443 return NULL;
444}
445
pauleb3da6d2005-10-18 04:20:33 +0000446static 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
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000460/*
461 * Consider supplied next-hop for inclusion to the supplied list of
462 * equal-cost next-hops, adjust list as neccessary.
463 */
464static void
465ospf_spf_add_parent (struct vertex *v, struct vertex *w,
466 struct vertex_nexthop *newhop,
Paul Jakmabd34fb32007-02-26 17:14:48 +0000467 unsigned int distance)
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000468{
David Lamparter7b925892012-07-23 18:17:57 +0200469 struct vertex_parent *vp, *wp;
470 struct listnode *node;
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000471
Paul Jakma08d3d5b2007-05-07 16:38:35 +0000472 /* we must have a newhop, and a distance */
Paul Jakmabd34fb32007-02-26 17:14:48 +0000473 assert (v && w && newhop);
Paul Jakma08d3d5b2007-05-07 16:38:35 +0000474 assert (distance);
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000475
Paul Jakma08d3d5b2007-05-07 16:38:35 +0000476 /* 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)).
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000479 */
Paul Jakma08d3d5b2007-05-07 16:38:35 +0000480 if (w->distance)
481 assert (distance <= w->distance);
482 else
483 w->distance = distance;
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000484
Paul Jakmab75ae992007-03-23 11:17:28 +0000485 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
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000494 /* Adding parent for a new, better path: flush existing parents from W. */
Paul Jakmabd34fb32007-02-26 17:14:48 +0000495 if (distance < w->distance)
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000496 {
Paul Jakmab75ae992007-03-23 11:17:28 +0000497 if (IS_DEBUG_OSPF_EVENT)
498 zlog_debug ("%s: distance %d better than %d, flushing existing parents",
499 __func__, distance, w->distance);
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000500 ospf_spf_flush_parents (w);
Paul Jakmabd34fb32007-02-26 17:14:48 +0000501 w->distance = distance;
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000502 }
503
David Lamparter7b925892012-07-23 18:17:57 +0200504 /* 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
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000517 vp = vertex_parent_new (v, ospf_lsa_has_link (w->lsa, v->lsa), newhop);
518 listnode_add (w->parents, vp);
519
520 return;
521}
522
gdt630e4802004-08-31 17:28:41 +0000523/* 16.1.1. Calculate nexthop from root through V (parent) to
Paul Jakmabd34fb32007-02-26 17:14:48 +0000524 * vertex W (destination), with given distance from root->W.
pauleb3da6d2005-10-18 04:20:33 +0000525 *
526 * The link must be supplied if V is the root vertex. In all other cases
527 * it may be NULL.
Paul Jakmabd34fb32007-02-26 17:14:48 +0000528 *
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.
gdt630e4802004-08-31 17:28:41 +0000533 */
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000534static unsigned int
pauleb3da6d2005-10-18 04:20:33 +0000535ospf_nexthop_calculation (struct ospf_area *area, struct vertex *v,
Paul Jakmabd34fb32007-02-26 17:14:48 +0000536 struct vertex *w, struct router_lsa_link *l,
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200537 unsigned int distance, int lsa_pos)
paul718e3742002-12-13 20:15:29 +0000538{
paul1eb8ef22005-04-07 07:30:20 +0000539 struct listnode *node, *nnode;
pauleb3da6d2005-10-18 04:20:33 +0000540 struct vertex_nexthop *nh;
541 struct vertex_parent *vp;
paul718e3742002-12-13 20:15:29 +0000542 struct ospf_interface *oi = NULL;
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000543 unsigned int added = 0;
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200544 char buf1[BUFSIZ];
545 char buf2[BUFSIZ];
paul0c0f9cd2003-06-06 23:27:04 +0000546
paul718e3742002-12-13 20:15:29 +0000547 if (IS_DEBUG_OSPF_EVENT)
gdt630e4802004-08-31 17:28:41 +0000548 {
ajs2a42e282004-12-08 18:43:03 +0000549 zlog_debug ("ospf_nexthop_calculation(): Start");
gdt630e4802004-08-31 17:28:41 +0000550 ospf_vertex_dump("V (parent):", v, 1, 1);
551 ospf_vertex_dump("W (dest) :", w, 1, 1);
Paul Jakmabd34fb32007-02-26 17:14:48 +0000552 zlog_debug ("V->W distance: %d", distance);
gdt630e4802004-08-31 17:28:41 +0000553 }
paul718e3742002-12-13 20:15:29 +0000554
paul718e3742002-12-13 20:15:29 +0000555 if (v == area->spf)
Paul Jakma9c27ef92006-05-04 07:32:57 +0000556 {
gdt630e4802004-08-31 17:28:41 +0000557 /* 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
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200564 /* 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
paul718e3742002-12-13 20:15:29 +0000585 if (w->type == OSPF_VERTEX_ROUTER)
paul0c0f9cd2003-06-06 23:27:04 +0000586 {
pauleb3da6d2005-10-18 04:20:33 +0000587 /* 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;
paul0c0f9cd2003-06-06 23:27:04 +0000591
pauleb3da6d2005-10-18 04:20:33 +0000592 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT)
593 {
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200594 struct in_addr nexthop;
595
pauleb3da6d2005-10-18 04:20:33 +0000596 /* 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.
gdt630e4802004-08-31 17:28:41 +0000604
pauleb3da6d2005-10-18 04:20:33 +0000605 At this point l is a link from V to W, and V is the
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200606 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
pauleb3da6d2005-10-18 04:20:33 +0000609 subnet declared by the PtMP link, then that link
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200610 is a constituent of the PtMP link, and its address is
pauleb3da6d2005-10-18 04:20:33 +0000611 a nexthop address for V.
612 */
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200613 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
614 {
Christian Frankef2b53da2013-03-20 15:28:46 +0000615 /* 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 }
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200631 }
632 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
633 {
634 struct prefix_ipv4 la;
gdt630e4802004-08-31 17:28:41 +0000635
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200636 la.family = AF_INET;
637 la.prefixlen = oi->address->prefixlen;
pauleb3da6d2005-10-18 04:20:33 +0000638
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200639 /* 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;
gdt630e4802004-08-31 17:28:41 +0000644
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200645 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 }
pauleb3da6d2005-10-18 04:20:33 +0000654
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200655 if (added)
pauleb3da6d2005-10-18 04:20:33 +0000656 {
657 /* found all necessary info to build nexthop */
658 nh = vertex_nexthop_new ();
659 nh->oi = oi;
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200660 nh->router = nexthop;
Paul Jakmabd34fb32007-02-26 17:14:48 +0000661 ospf_spf_add_parent (v, w, nh, distance);
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000662 return 1;
pauleb3da6d2005-10-18 04:20:33 +0000663 }
664 else
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200665 zlog_info("%s: could not determine nexthop for link %s",
666 __func__, oi->ifp->name);
pauleb3da6d2005-10-18 04:20:33 +0000667 } /* end point-to-point link from V to W */
Paul Jakma9c27ef92006-05-04 07:32:57 +0000668 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))
683 {
684 nh = vertex_nexthop_new ();
685 nh->oi = vl_data->nexthop.oi;
686 nh->router = vl_data->nexthop.router;
Paul Jakmabd34fb32007-02-26 17:14:48 +0000687 ospf_spf_add_parent (v, w, nh, distance);
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000688 return 1;
Paul Jakma9c27ef92006-05-04 07:32:57 +0000689 }
690 else
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000691 zlog_info("ospf_nexthop_calculation(): "
692 "vl_data for VL link not found");
Paul Jakma9c27ef92006-05-04 07:32:57 +0000693 } /* end virtual-link from V to W */
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000694 return 0;
gdt630e4802004-08-31 17:28:41 +0000695 } /* end W is a Router vertex */
paul718e3742002-12-13 20:15:29 +0000696 else
paul0c0f9cd2003-06-06 23:27:04 +0000697 {
pauleb3da6d2005-10-18 04:20:33 +0000698 assert(w->type == OSPF_VERTEX_NETWORK);
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200699
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;
paul0c0f9cd2003-06-06 23:27:04 +0000705 }
gdt630e4802004-08-31 17:28:41 +0000706 } /* end V is the root */
gdt630e4802004-08-31 17:28:41 +0000707 /* Check if W's parent is a network connected to root. */
paul718e3742002-12-13 20:15:29 +0000708 else if (v->type == OSPF_VERTEX_NETWORK)
709 {
gdt630e4802004-08-31 17:28:41 +0000710 /* See if any of V's parents are the root. */
pauleb3da6d2005-10-18 04:20:33 +0000711 for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp))
paul718e3742002-12-13 20:15:29 +0000712 {
pauleb3da6d2005-10-18 04:20:33 +0000713 if (vp->parent == area->spf) /* connects to root? */
gdt630e4802004-08-31 17:28:41 +0000714 {
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);
paul0c0f9cd2003-06-06 23:27:04 +0000722 while ((l = ospf_get_next_link (w, v, l)))
723 {
gdt630e4802004-08-31 17:28:41 +0000724 /* ...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 */
pauleb3da6d2005-10-18 04:20:33 +0000730 nh = vertex_nexthop_new ();
731 nh->oi = vp->nexthop->oi;
732 nh->router = l->link_data;
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000733 added = 1;
Paul Jakmabd34fb32007-02-26 17:14:48 +0000734 ospf_spf_add_parent (v, w, nh, distance);
paul0c0f9cd2003-06-06 23:27:04 +0000735 }
Paul Jakma945ea292012-08-06 12:17:12 +0100736 /* Note lack of return is deliberate. See next comment. */
737 }
paul718e3742002-12-13 20:15:29 +0000738 }
Paul Jakma945ea292012-08-06 12:17:12 +0100739 /* 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;
paul718e3742002-12-13 20:15:29 +0000767 }
768
gdt630e4802004-08-31 17:28:41 +0000769 /* 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 */
Paul Jakmab75ae992007-03-23 11:17:28 +0000774 if (IS_DEBUG_OSPF_EVENT)
775 zlog_debug ("%s: Intervening routers, adding parent(s)", __func__);
776
pauleb3da6d2005-10-18 04:20:33 +0000777 for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp))
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000778 {
779 added = 1;
Paul Jakmabd34fb32007-02-26 17:14:48 +0000780 ospf_spf_add_parent (v, w, vp->nexthop, distance);
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000781 }
Paul Jakma9c27ef92006-05-04 07:32:57 +0000782
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000783 return added;
paul718e3742002-12-13 20:15:29 +0000784}
785
gdt630e4802004-08-31 17:28:41 +0000786/* 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 */
paul4dadc292005-05-06 21:37:42 +0000791static void
paul718e3742002-12-13 20:15:29 +0000792ospf_spf_next (struct vertex *v, struct ospf_area *area,
hasso462f20d2005-02-23 11:29:02 +0000793 struct pqueue * candidate)
paul718e3742002-12-13 20:15:29 +0000794{
795 struct ospf_lsa *w_lsa = NULL;
paul718e3742002-12-13 20:15:29 +0000796 u_char *p;
797 u_char *lim;
798 struct router_lsa_link *l = NULL;
799 struct in_addr *r;
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200800 int type = 0, lsa_pos=-1, lsa_pos_next=0;
paul718e3742002-12-13 20:15:29 +0000801
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 }
Paul Jakmab75ae992007-03-23 11:17:28 +0000809
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
paul718e3742002-12-13 20:15:29 +0000816 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
paul0c0f9cd2003-06-06 23:27:04 +0000817 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
818
paul718e3742002-12-13 20:15:29 +0000819 while (p < lim)
820 {
pauleb3da6d2005-10-18 04:20:33 +0000821 struct vertex *w;
822 unsigned int distance;
pauld355bfa2004-04-08 07:43:45 +0000823
paul718e3742002-12-13 20:15:29 +0000824 /* In case of V is Router-LSA. */
825 if (v->lsa->type == OSPF_ROUTER_LSA)
826 {
827 l = (struct router_lsa_link *) p;
828
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200829 lsa_pos = lsa_pos_next; /* LSA link position */
830 lsa_pos_next++;
Denis Ovsienko05b77092011-08-23 11:36:27 +0400831 p += (OSPF_ROUTER_LSA_LINK_SIZE +
832 (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +0000833
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;
Paul Jakma08d3d5b2007-05-07 16:38:35 +0000840
841 /* Infinite distance links shouldn't be followed, except
842 * for local links (a stub-routed router still wants to
843 * calculate tree, so must follow its own links).
844 */
845 if ((v != area->spf) && l->m[0].metric >= OSPF_OUTPUT_COST_INFINITE)
846 continue;
paul718e3742002-12-13 20:15:29 +0000847
848 /* (b) Otherwise, W is a transit vertex (router or transit
849 network). Look up the vertex W's LSA (router-LSA or
850 network-LSA) in Area A's link state database. */
851 switch (type)
852 {
853 case LSA_LINK_TYPE_POINTOPOINT:
854 case LSA_LINK_TYPE_VIRTUALLINK:
855 if (type == LSA_LINK_TYPE_VIRTUALLINK)
paul0c0f9cd2003-06-06 23:27:04 +0000856 {
857 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000858 zlog_debug ("looking up LSA through VL: %s",
paul0c0f9cd2003-06-06 23:27:04 +0000859 inet_ntoa (l->link_id));
860 }
paul718e3742002-12-13 20:15:29 +0000861
862 w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id,
863 l->link_id);
864 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000865 {
866 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000867 zlog_debug ("found Router LSA %s", inet_ntoa (l->link_id));
paul0c0f9cd2003-06-06 23:27:04 +0000868 }
paul718e3742002-12-13 20:15:29 +0000869 break;
870 case LSA_LINK_TYPE_TRANSIT:
paul0c0f9cd2003-06-06 23:27:04 +0000871 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000872 zlog_debug ("Looking up Network LSA, ID: %s",
paul0c0f9cd2003-06-06 23:27:04 +0000873 inet_ntoa (l->link_id));
paul718e3742002-12-13 20:15:29 +0000874 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA,
paul0c0f9cd2003-06-06 23:27:04 +0000875 l->link_id);
paul718e3742002-12-13 20:15:29 +0000876 if (w_lsa)
paul0c0f9cd2003-06-06 23:27:04 +0000877 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000878 zlog_debug ("found the LSA");
paul718e3742002-12-13 20:15:29 +0000879 break;
880 default:
paul0c0f9cd2003-06-06 23:27:04 +0000881 zlog_warn ("Invalid LSA link type %d", type);
paul718e3742002-12-13 20:15:29 +0000882 continue;
883 }
884 }
885 else
886 {
887 /* In case of V is Network-LSA. */
paul0c0f9cd2003-06-06 23:27:04 +0000888 r = (struct in_addr *) p;
paul718e3742002-12-13 20:15:29 +0000889 p += sizeof (struct in_addr);
890
891 /* Lookup the vertex W's LSA. */
892 w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r);
Paul Jakmab75ae992007-03-23 11:17:28 +0000893 if (w_lsa)
894 {
895 if (IS_DEBUG_OSPF_EVENT)
896 zlog_debug ("found Router LSA %s", inet_ntoa (w_lsa->data->id));
897 }
paul718e3742002-12-13 20:15:29 +0000898 }
899
900 /* (b cont.) If the LSA does not exist, or its LS age is equal
901 to MaxAge, or it does not have a link back to vertex V,
902 examine the next link in V's LSA.[23] */
903 if (w_lsa == NULL)
Paul Jakmab75ae992007-03-23 11:17:28 +0000904 {
905 if (IS_DEBUG_OSPF_EVENT)
906 zlog_debug ("No LSA found");
907 continue;
908 }
paul718e3742002-12-13 20:15:29 +0000909
910 if (IS_LSA_MAXAGE (w_lsa))
Paul Jakmab75ae992007-03-23 11:17:28 +0000911 {
912 if (IS_DEBUG_OSPF_EVENT)
913 zlog_debug ("LSA is MaxAge");
914 continue;
915 }
paul718e3742002-12-13 20:15:29 +0000916
pauleb3da6d2005-10-18 04:20:33 +0000917 if (ospf_lsa_has_link (w_lsa->data, v->lsa) < 0 )
paul718e3742002-12-13 20:15:29 +0000918 {
paul0c0f9cd2003-06-06 23:27:04 +0000919 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000920 zlog_debug ("The LSA doesn't have a link back");
paul718e3742002-12-13 20:15:29 +0000921 continue;
922 }
923
924 /* (c) If vertex W is already on the shortest-path tree, examine
925 the next link in the LSA. */
hasso462f20d2005-02-23 11:29:02 +0000926 if (w_lsa->stat == LSA_SPF_IN_SPFTREE)
927 {
928 if (IS_DEBUG_OSPF_EVENT)
929 zlog_debug ("The LSA is already in SPF");
930 continue;
931 }
paul718e3742002-12-13 20:15:29 +0000932
933 /* (d) Calculate the link state cost D of the resulting path
934 from the root to vertex W. D is equal to the sum of the link
935 state cost of the (already calculated) shortest path to
936 vertex V and the advertised cost of the link between vertices
937 V and W. If D is: */
938
paul718e3742002-12-13 20:15:29 +0000939 /* calculate link cost D. */
940 if (v->lsa->type == OSPF_ROUTER_LSA)
pauleb3da6d2005-10-18 04:20:33 +0000941 distance = v->distance + ntohs (l->m[0].metric);
gdt630e4802004-08-31 17:28:41 +0000942 else /* v is not a Router-LSA */
pauleb3da6d2005-10-18 04:20:33 +0000943 distance = v->distance;
paul718e3742002-12-13 20:15:29 +0000944
945 /* Is there already vertex W in candidate list? */
hasso462f20d2005-02-23 11:29:02 +0000946 if (w_lsa->stat == LSA_SPF_NOT_EXPLORED)
947 {
pauleb3da6d2005-10-18 04:20:33 +0000948 /* prepare vertex W. */
949 w = ospf_vertex_new (w_lsa);
950
hasso462f20d2005-02-23 11:29:02 +0000951 /* Calculate nexthop to W. */
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200952 if (ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos))
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000953 pqueue_enqueue (w, candidate);
Paul Jakmab75ae992007-03-23 11:17:28 +0000954 else if (IS_DEBUG_OSPF_EVENT)
955 zlog_debug ("Nexthop Calc failed");
hasso462f20d2005-02-23 11:29:02 +0000956 }
957 else if (w_lsa->stat >= 0)
958 {
959 /* Get the vertex from candidates. */
pauleb3da6d2005-10-18 04:20:33 +0000960 w = candidate->array[w_lsa->stat];
paul718e3742002-12-13 20:15:29 +0000961
hasso462f20d2005-02-23 11:29:02 +0000962 /* if D is greater than. */
pauleb3da6d2005-10-18 04:20:33 +0000963 if (w->distance < distance)
paul718e3742002-12-13 20:15:29 +0000964 {
paul718e3742002-12-13 20:15:29 +0000965 continue;
966 }
hasso462f20d2005-02-23 11:29:02 +0000967 /* equal to. */
pauleb3da6d2005-10-18 04:20:33 +0000968 else if (w->distance == distance)
paul718e3742002-12-13 20:15:29 +0000969 {
pauleb3da6d2005-10-18 04:20:33 +0000970 /* Found an equal-cost path to W.
971 * Calculate nexthop of to W from V. */
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200972 ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos);
paul718e3742002-12-13 20:15:29 +0000973 }
hasso462f20d2005-02-23 11:29:02 +0000974 /* less than. */
975 else
paul718e3742002-12-13 20:15:29 +0000976 {
Paul Jakmabc20c1a2007-01-24 14:51:51 +0000977 /* Found a lower-cost path to W.
978 * nexthop_calculation is conditional, if it finds
979 * valid nexthop it will call spf_add_parents, which
980 * will flush the old parents
981 */
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200982 if (ospf_nexthop_calculation (area, v, w, l, distance, lsa_pos))
Paul Jakma7591d8b2007-08-06 18:52:45 +0000983 /* Decrease the key of the node in the heap.
984 * trickle-sort it up towards root, just in case this
985 * node should now be the new root due the cost change.
Paul Jakmae95537f2007-08-07 16:22:05 +0000986 * (next pqueu_{de,en}queue will fully re-heap the queue).
Paul Jakma7591d8b2007-08-06 18:52:45 +0000987 */
988 trickle_up (w_lsa->stat, candidate);
paul718e3742002-12-13 20:15:29 +0000989 }
gdt630e4802004-08-31 17:28:41 +0000990 } /* end W is already on the candidate list */
991 } /* end loop over the links in V's LSA */
paul718e3742002-12-13 20:15:29 +0000992}
993
paul4dadc292005-05-06 21:37:42 +0000994static void
paul718e3742002-12-13 20:15:29 +0000995ospf_spf_dump (struct vertex *v, int i)
996{
hasso52dc7ee2004-09-23 19:18:23 +0000997 struct listnode *cnode;
998 struct listnode *nnode;
pauleb3da6d2005-10-18 04:20:33 +0000999 struct vertex_parent *parent;
paul718e3742002-12-13 20:15:29 +00001000
1001 if (v->type == OSPF_VERTEX_ROUTER)
1002 {
1003 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001004 zlog_debug ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +00001005 }
1006 else
1007 {
1008 struct network_lsa *lsa = (struct network_lsa *) v->lsa;
1009 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001010 zlog_debug ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id),
paul0c0f9cd2003-06-06 23:27:04 +00001011 ip_masklen (lsa->mask));
gdt630e4802004-08-31 17:28:41 +00001012 }
paul718e3742002-12-13 20:15:29 +00001013
paul1eb8ef22005-04-07 07:30:20 +00001014 if (IS_DEBUG_OSPF_EVENT)
pauleb3da6d2005-10-18 04:20:33 +00001015 for (ALL_LIST_ELEMENTS_RO (v->parents, nnode, parent))
1016 {
1017 zlog_debug (" nexthop %p %s %s",
1018 parent->nexthop,
1019 inet_ntoa (parent->nexthop->router),
1020 parent->nexthop->oi ? IF_NAME(parent->nexthop->oi)
1021 : "NULL");
1022 }
paul718e3742002-12-13 20:15:29 +00001023
1024 i++;
1025
pauleb3da6d2005-10-18 04:20:33 +00001026 for (ALL_LIST_ELEMENTS_RO (v->children, cnode, v))
paul1eb8ef22005-04-07 07:30:20 +00001027 ospf_spf_dump (v, i);
paul718e3742002-12-13 20:15:29 +00001028}
1029
1030/* Second stage of SPF calculation. */
paul4dadc292005-05-06 21:37:42 +00001031static void
paul0c0f9cd2003-06-06 23:27:04 +00001032ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v,
Paul Jakmab3bc68e2008-09-04 13:52:07 +01001033 struct route_table *rt,
1034 int parent_is_root)
paul718e3742002-12-13 20:15:29 +00001035{
paul1eb8ef22005-04-07 07:30:20 +00001036 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +00001037 struct vertex *child;
1038
1039 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001040 zlog_debug ("ospf_process_stub():processing stubs for area %s",
paul0c0f9cd2003-06-06 23:27:04 +00001041 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +00001042 if (v->type == OSPF_VERTEX_ROUTER)
1043 {
1044 u_char *p;
1045 u_char *lim;
1046 struct router_lsa_link *l;
1047 struct router_lsa *rlsa;
Joakim Tjernlund57c639f2012-07-07 17:06:12 +02001048 int lsa_pos = 0;
paul718e3742002-12-13 20:15:29 +00001049
paul0c0f9cd2003-06-06 23:27:04 +00001050 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001051 zlog_debug ("ospf_process_stubs():processing router LSA, id: %s",
paul0c0f9cd2003-06-06 23:27:04 +00001052 inet_ntoa (v->lsa->id));
paul718e3742002-12-13 20:15:29 +00001053 rlsa = (struct router_lsa *) v->lsa;
1054
1055
paul0c0f9cd2003-06-06 23:27:04 +00001056 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001057 zlog_debug ("ospf_process_stubs(): we have %d links to process",
paul0c0f9cd2003-06-06 23:27:04 +00001058 ntohs (rlsa->links));
gdt630e4802004-08-31 17:28:41 +00001059 p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
paul718e3742002-12-13 20:15:29 +00001060 lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
1061
1062 while (p < lim)
1063 {
1064 l = (struct router_lsa_link *) p;
1065
Denis Ovsienko05b77092011-08-23 11:36:27 +04001066 p += (OSPF_ROUTER_LSA_LINK_SIZE +
1067 (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
paul718e3742002-12-13 20:15:29 +00001068
1069 if (l->m[0].type == LSA_LINK_TYPE_STUB)
Joakim Tjernlund57c639f2012-07-07 17:06:12 +02001070 ospf_intra_add_stub (rt, l, v, area, parent_is_root, lsa_pos);
1071 lsa_pos++;
paul718e3742002-12-13 20:15:29 +00001072 }
1073 }
1074
gdt630e4802004-08-31 17:28:41 +00001075 ospf_vertex_dump("ospf_process_stubs(): after examining links: ", v, 1, 1);
paul718e3742002-12-13 20:15:29 +00001076
pauleb3da6d2005-10-18 04:20:33 +00001077 for (ALL_LIST_ELEMENTS (v->children, cnode, cnnode, child))
paul718e3742002-12-13 20:15:29 +00001078 {
paul718e3742002-12-13 20:15:29 +00001079 if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED))
paul0c0f9cd2003-06-06 23:27:04 +00001080 continue;
Paul Jakmab3bc68e2008-09-04 13:52:07 +01001081
1082 /* the first level of routers connected to the root
1083 * should have 'parent_is_root' set, including those
1084 * connected via a network vertex.
1085 */
1086 if (area->spf == v)
1087 parent_is_root = 1;
1088 else if (v->type == OSPF_VERTEX_ROUTER)
1089 parent_is_root = 0;
1090
1091 ospf_spf_process_stubs (area, child, rt, parent_is_root);
paul718e3742002-12-13 20:15:29 +00001092
1093 SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED);
1094 }
1095}
1096
1097void
1098ospf_rtrs_free (struct route_table *rtrs)
1099{
1100 struct route_node *rn;
hasso52dc7ee2004-09-23 19:18:23 +00001101 struct list *or_list;
paul1eb8ef22005-04-07 07:30:20 +00001102 struct ospf_route *or;
1103 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001104
1105 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001106 zlog_debug ("Route: Router Routing Table free");
paul718e3742002-12-13 20:15:29 +00001107
1108 for (rn = route_top (rtrs); rn; rn = route_next (rn))
1109 if ((or_list = rn->info) != NULL)
1110 {
paul1eb8ef22005-04-07 07:30:20 +00001111 for (ALL_LIST_ELEMENTS (or_list, node, nnode, or))
1112 ospf_route_free (or);
paul718e3742002-12-13 20:15:29 +00001113
paul0c0f9cd2003-06-06 23:27:04 +00001114 list_delete (or_list);
paul718e3742002-12-13 20:15:29 +00001115
paul0c0f9cd2003-06-06 23:27:04 +00001116 /* Unlock the node. */
1117 rn->info = NULL;
1118 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001119 }
1120 route_table_finish (rtrs);
1121}
1122
Stephen Hemminger075e12f2011-12-06 23:54:17 +04001123#if 0
paul4dadc292005-05-06 21:37:42 +00001124static void
paul718e3742002-12-13 20:15:29 +00001125ospf_rtrs_print (struct route_table *rtrs)
1126{
1127 struct route_node *rn;
hasso52dc7ee2004-09-23 19:18:23 +00001128 struct list *or_list;
1129 struct listnode *ln;
1130 struct listnode *pnode;
paul718e3742002-12-13 20:15:29 +00001131 struct ospf_route *or;
1132 struct ospf_path *path;
1133 char buf1[BUFSIZ];
1134 char buf2[BUFSIZ];
1135
1136 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001137 zlog_debug ("ospf_rtrs_print() start");
paul718e3742002-12-13 20:15:29 +00001138
1139 for (rn = route_top (rtrs); rn; rn = route_next (rn))
1140 if ((or_list = rn->info) != NULL)
paul1eb8ef22005-04-07 07:30:20 +00001141 for (ALL_LIST_ELEMENTS_RO (or_list, ln, or))
paul718e3742002-12-13 20:15:29 +00001142 {
paul718e3742002-12-13 20:15:29 +00001143 switch (or->path_type)
1144 {
1145 case OSPF_PATH_INTRA_AREA:
paul0c0f9cd2003-06-06 23:27:04 +00001146 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001147 zlog_debug ("%s [%d] area: %s",
paul0c0f9cd2003-06-06 23:27:04 +00001148 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
1149 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
1150 buf2, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001151 break;
1152 case OSPF_PATH_INTER_AREA:
paul0c0f9cd2003-06-06 23:27:04 +00001153 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001154 zlog_debug ("%s IA [%d] area: %s",
paul0c0f9cd2003-06-06 23:27:04 +00001155 inet_ntop (AF_INET, &or->id, buf1, BUFSIZ),
1156 or->cost, inet_ntop (AF_INET, &or->u.std.area_id,
1157 buf2, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001158 break;
1159 default:
1160 break;
1161 }
1162
paul1eb8ef22005-04-07 07:30:20 +00001163 for (ALL_LIST_ELEMENTS_RO (or->paths, pnode, path))
paul718e3742002-12-13 20:15:29 +00001164 {
paul718e3742002-12-13 20:15:29 +00001165 if (path->nexthop.s_addr == 0)
paul0c0f9cd2003-06-06 23:27:04 +00001166 {
1167 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001168 zlog_debug (" directly attached to %s\r\n",
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +02001169 ifindex2ifname (path->ifindex));
paul0c0f9cd2003-06-06 23:27:04 +00001170 }
1171 else
1172 {
1173 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001174 zlog_debug (" via %s, %s\r\n",
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +02001175 inet_ntoa (path->nexthop),
1176 ifindex2ifname (path->ifindex));
paul0c0f9cd2003-06-06 23:27:04 +00001177 }
paul718e3742002-12-13 20:15:29 +00001178 }
1179 }
1180
ajs2a42e282004-12-08 18:43:03 +00001181 zlog_debug ("ospf_rtrs_print() end");
paul718e3742002-12-13 20:15:29 +00001182}
Stephen Hemminger075e12f2011-12-06 23:54:17 +04001183#endif
paul718e3742002-12-13 20:15:29 +00001184
1185/* Calculating the shortest-path tree for an area. */
paul4dadc292005-05-06 21:37:42 +00001186static void
paul0c0f9cd2003-06-06 23:27:04 +00001187ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
paul718e3742002-12-13 20:15:29 +00001188 struct route_table *new_rtrs)
1189{
hasso462f20d2005-02-23 11:29:02 +00001190 struct pqueue *candidate;
paul718e3742002-12-13 20:15:29 +00001191 struct vertex *v;
pauleb3da6d2005-10-18 04:20:33 +00001192
paul718e3742002-12-13 20:15:29 +00001193 if (IS_DEBUG_OSPF_EVENT)
1194 {
ajs2a42e282004-12-08 18:43:03 +00001195 zlog_debug ("ospf_spf_calculate: Start");
1196 zlog_debug ("ospf_spf_calculate: running Dijkstra for area %s",
paul0c0f9cd2003-06-06 23:27:04 +00001197 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +00001198 }
1199
1200 /* Check router-lsa-self. If self-router-lsa is not yet allocated,
1201 return this area's calculation. */
paul0c0f9cd2003-06-06 23:27:04 +00001202 if (!area->router_lsa_self)
paul718e3742002-12-13 20:15:29 +00001203 {
1204 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001205 zlog_debug ("ospf_spf_calculate: "
paul0c0f9cd2003-06-06 23:27:04 +00001206 "Skip area %s's calculation due to empty router_lsa_self",
1207 inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +00001208 return;
1209 }
1210
1211 /* RFC2328 16.1. (1). */
paul0c0f9cd2003-06-06 23:27:04 +00001212 /* Initialize the algorithm's data structures. */
hasso462f20d2005-02-23 11:29:02 +00001213
1214 /* This function scans all the LSA database and set the stat field to
1215 * LSA_SPF_NOT_EXPLORED. */
1216 ospf_lsdb_clean_stat (area->lsdb);
1217 /* Create a new heap for the candidates. */
1218 candidate = pqueue_create();
1219 candidate->cmp = cmp;
1220 candidate->update = update_stat;
paul718e3742002-12-13 20:15:29 +00001221
1222 /* Initialize the shortest-path tree to only the root (which is the
1223 router doing the calculation). */
1224 ospf_spf_init (area);
1225 v = area->spf;
hasso462f20d2005-02-23 11:29:02 +00001226 /* Set LSA position to LSA_SPF_IN_SPFTREE. This vertex is the root of the
1227 * spanning tree. */
1228 *(v->stat) = LSA_SPF_IN_SPFTREE;
paul718e3742002-12-13 20:15:29 +00001229
1230 /* Set Area A's TransitCapability to FALSE. */
1231 area->transit = OSPF_TRANSIT_FALSE;
1232 area->shortcut_capability = 1;
pauleb3da6d2005-10-18 04:20:33 +00001233
paul718e3742002-12-13 20:15:29 +00001234 for (;;)
1235 {
1236 /* RFC2328 16.1. (2). */
hasso462f20d2005-02-23 11:29:02 +00001237 ospf_spf_next (v, area, candidate);
paul718e3742002-12-13 20:15:29 +00001238
1239 /* RFC2328 16.1. (3). */
1240 /* If at this step the candidate list is empty, the shortest-
1241 path tree (of transit vertices) has been completely built and
1242 this stage of the procedure terminates. */
hasso462f20d2005-02-23 11:29:02 +00001243 if (candidate->size == 0)
paul718e3742002-12-13 20:15:29 +00001244 break;
1245
1246 /* Otherwise, choose the vertex belonging to the candidate list
1247 that is closest to the root, and add it to the shortest-path
1248 tree (removing it from the candidate list in the
paul0c0f9cd2003-06-06 23:27:04 +00001249 process). */
hasso462f20d2005-02-23 11:29:02 +00001250 /* Extract from the candidates the node with the lower key. */
1251 v = (struct vertex *) pqueue_dequeue (candidate);
1252 /* Update stat field in vertex. */
1253 *(v->stat) = LSA_SPF_IN_SPFTREE;
pauleb3da6d2005-10-18 04:20:33 +00001254
paul718e3742002-12-13 20:15:29 +00001255 ospf_vertex_add_parent (v);
1256
paul718e3742002-12-13 20:15:29 +00001257 /* RFC2328 16.1. (4). */
1258 if (v->type == OSPF_VERTEX_ROUTER)
1259 ospf_intra_add_router (new_rtrs, v, area);
paul0c0f9cd2003-06-06 23:27:04 +00001260 else
paul718e3742002-12-13 20:15:29 +00001261 ospf_intra_add_transit (new_table, v, area);
1262
1263 /* RFC2328 16.1. (5). */
1264 /* Iterate the algorithm by returning to Step 2. */
gdt630e4802004-08-31 17:28:41 +00001265
1266 } /* end loop until no more candidate vertices */
paul718e3742002-12-13 20:15:29 +00001267
1268 if (IS_DEBUG_OSPF_EVENT)
1269 {
1270 ospf_spf_dump (area->spf, 0);
1271 ospf_route_table_dump (new_table);
1272 }
1273
1274 /* Second stage of SPF calculation procedure's */
Paul Jakmab3bc68e2008-09-04 13:52:07 +01001275 ospf_spf_process_stubs (area, area->spf, new_table, 0);
paul718e3742002-12-13 20:15:29 +00001276
pauleb3da6d2005-10-18 04:20:33 +00001277 /* Free candidate queue. */
hasso462f20d2005-02-23 11:29:02 +00001278 pqueue_delete (candidate);
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001279
pauleb3da6d2005-10-18 04:20:33 +00001280 ospf_vertex_dump (__func__, area->spf, 0, 1);
1281 /* Free nexthop information, canonical versions of which are attached
1282 * the first level of router vertices attached to the root vertex, see
1283 * ospf_nexthop_calculation.
1284 */
1285 ospf_canonical_nexthops_free (area->spf);
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001286
paul718e3742002-12-13 20:15:29 +00001287 /* Increment SPF Calculation Counter. */
1288 area->spf_calculation++;
1289
Paul Jakma2518efd2006-08-27 06:49:29 +00001290 quagga_gettime (QUAGGA_CLK_MONOTONIC, &area->ospf->ts_spf);
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001291 area->ts_spf = area->ospf->ts_spf;
paul718e3742002-12-13 20:15:29 +00001292
1293 if (IS_DEBUG_OSPF_EVENT)
Paul Jakma9c27ef92006-05-04 07:32:57 +00001294 zlog_debug ("ospf_spf_calculate: Stop. %ld vertices",
1295 mtype_stats_alloc(MTYPE_OSPF_VERTEX));
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001296
1297 /* Free SPF vertices, but not the list. List has ospf_vertex_free
1298 * as deconstructor.
1299 */
1300 list_delete_all_node (&vertex_list);
paul718e3742002-12-13 20:15:29 +00001301}
David Lamparter6b0655a2014-06-04 06:53:35 +02001302
paul718e3742002-12-13 20:15:29 +00001303/* Timer for SPF calculation. */
paul4dadc292005-05-06 21:37:42 +00001304static int
paul68980082003-03-25 05:07:42 +00001305ospf_spf_calculate_timer (struct thread *thread)
paul718e3742002-12-13 20:15:29 +00001306{
paul68980082003-03-25 05:07:42 +00001307 struct ospf *ospf = THREAD_ARG (thread);
paul718e3742002-12-13 20:15:29 +00001308 struct route_table *new_table, *new_rtrs;
paul1eb8ef22005-04-07 07:30:20 +00001309 struct ospf_area *area;
1310 struct listnode *node, *nnode;
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001311 struct timeval start_time, stop_time, spf_start_time;
1312 int areas_processed = 0;
1313 unsigned long ia_time, prune_time, rt_time;
1314 unsigned long abr_time, total_spf_time, spf_time;
1315 char rbuf[32]; /* reason_buf */
paul718e3742002-12-13 20:15:29 +00001316
1317 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001318 zlog_debug ("SPF: Timer (SPF calculation expire)");
paul0c0f9cd2003-06-06 23:27:04 +00001319
paul718e3742002-12-13 20:15:29 +00001320 ospf->t_spf_calc = NULL;
1321
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001322 quagga_gettime (QUAGGA_CLK_MONOTONIC, &spf_start_time);
paul718e3742002-12-13 20:15:29 +00001323 /* Allocate new table tree. */
1324 new_table = route_table_init ();
paul0c0f9cd2003-06-06 23:27:04 +00001325 new_rtrs = route_table_init ();
paul718e3742002-12-13 20:15:29 +00001326
paul68980082003-03-25 05:07:42 +00001327 ospf_vl_unapprove (ospf);
paul718e3742002-12-13 20:15:29 +00001328
1329 /* Calculate SPF for each area. */
paul1eb8ef22005-04-07 07:30:20 +00001330 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
Paul Jakma9c27ef92006-05-04 07:32:57 +00001331 {
1332 /* Do backbone last, so as to first discover intra-area paths
1333 * for any back-bone virtual-links
1334 */
1335 if (ospf->backbone && ospf->backbone == area)
1336 continue;
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001337
Paul Jakma9c27ef92006-05-04 07:32:57 +00001338 ospf_spf_calculate (area, new_table, new_rtrs);
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001339 areas_processed++;
Paul Jakma9c27ef92006-05-04 07:32:57 +00001340 }
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001341
Paul Jakma9c27ef92006-05-04 07:32:57 +00001342 /* SPF for backbone, if required */
1343 if (ospf->backbone)
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001344 {
1345 ospf_spf_calculate (ospf->backbone, new_table, new_rtrs);
1346 areas_processed++;
1347 }
1348
1349 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1350 spf_time = timeval_elapsed (stop_time, spf_start_time);
1351
paul68980082003-03-25 05:07:42 +00001352 ospf_vl_shut_unapproved (ospf);
paul718e3742002-12-13 20:15:29 +00001353
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001354 start_time = stop_time; /* saving a call */
1355
paul68980082003-03-25 05:07:42 +00001356 ospf_ia_routing (ospf, new_table, new_rtrs);
paul718e3742002-12-13 20:15:29 +00001357
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001358 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1359 ia_time = timeval_elapsed (stop_time, start_time);
1360
1361 quagga_gettime (QUAGGA_CLK_MONOTONIC, &start_time);
paul718e3742002-12-13 20:15:29 +00001362 ospf_prune_unreachable_networks (new_table);
1363 ospf_prune_unreachable_routers (new_rtrs);
1364
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001365 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1366 prune_time = timeval_elapsed (stop_time, start_time);
paul718e3742002-12-13 20:15:29 +00001367 /* AS-external-LSA calculation should not be performed here. */
1368
1369 /* If new Router Route is installed,
1370 then schedule re-calculate External routes. */
1371 if (1)
paul68980082003-03-25 05:07:42 +00001372 ospf_ase_calculate_schedule (ospf);
paul718e3742002-12-13 20:15:29 +00001373
paul68980082003-03-25 05:07:42 +00001374 ospf_ase_calculate_timer_add (ospf);
paul718e3742002-12-13 20:15:29 +00001375
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001376 quagga_gettime (QUAGGA_CLK_MONOTONIC, &start_time);
1377
paul718e3742002-12-13 20:15:29 +00001378 /* Update routing table. */
paul68980082003-03-25 05:07:42 +00001379 ospf_route_install (ospf, new_table);
paul718e3742002-12-13 20:15:29 +00001380
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001381 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1382 rt_time = timeval_elapsed (stop_time, start_time);
paul718e3742002-12-13 20:15:29 +00001383 /* Update ABR/ASBR routing table */
paul68980082003-03-25 05:07:42 +00001384 if (ospf->old_rtrs)
paul718e3742002-12-13 20:15:29 +00001385 {
1386 /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */
paul68980082003-03-25 05:07:42 +00001387 /* ospf_route_delete (ospf->old_rtrs); */
1388 ospf_rtrs_free (ospf->old_rtrs);
paul718e3742002-12-13 20:15:29 +00001389 }
1390
paul68980082003-03-25 05:07:42 +00001391 ospf->old_rtrs = ospf->new_rtrs;
1392 ospf->new_rtrs = new_rtrs;
paul718e3742002-12-13 20:15:29 +00001393
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001394 quagga_gettime (QUAGGA_CLK_MONOTONIC, &start_time);
paul0c0f9cd2003-06-06 23:27:04 +00001395 if (IS_OSPF_ABR (ospf))
paul68980082003-03-25 05:07:42 +00001396 ospf_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001397
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001398 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1399 abr_time = timeval_elapsed (stop_time, start_time);
1400
1401 quagga_gettime (QUAGGA_CLK_MONOTONIC, &stop_time);
1402 total_spf_time = timeval_elapsed (stop_time, spf_start_time);
1403 ospf->ts_spf_duration.tv_sec = total_spf_time/1000000;
1404 ospf->ts_spf_duration.tv_usec = total_spf_time % 1000000;
1405
1406 ospf_get_spf_reason_str (rbuf);
1407
1408 if (IS_OSPF_ABR (ospf))
1409 zlog_info ("SPF Processing Time(usecs): # Areas: %d, SPF Time: %ld, InterArea: %ld, Prune: %ld, RouteInstall: %ld, ABR: %ld, Total: %ld, Reason: %s\n",
1410 areas_processed, spf_time, ia_time, prune_time, rt_time, abr_time, total_spf_time, rbuf);
1411 else
1412 zlog_info ("SPF Processing Time(usecs): SPF Time: %ld, InterArea: %ld, Prune: %ld, RouteInstall: %ld, Total: %ld, Reason: %s\n",
1413 spf_time, ia_time, prune_time, rt_time, total_spf_time, rbuf);
1414
1415 ospf_clear_spf_reason_flags ();
paul718e3742002-12-13 20:15:29 +00001416
1417 return 0;
1418}
1419
1420/* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
1421 set timer for SPF calc. */
1422void
paul68980082003-03-25 05:07:42 +00001423ospf_spf_calculate_schedule (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001424{
pauld24f6e22005-10-21 09:23:12 +00001425 unsigned long delay, elapsed, ht;
1426 struct timeval result;
paul718e3742002-12-13 20:15:29 +00001427
1428 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001429 zlog_debug ("SPF: calculation timer scheduled");
paul718e3742002-12-13 20:15:29 +00001430
1431 /* OSPF instance does not exist. */
paul68980082003-03-25 05:07:42 +00001432 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001433 return;
pauld24f6e22005-10-21 09:23:12 +00001434
paul718e3742002-12-13 20:15:29 +00001435 /* SPF calculation timer is already scheduled. */
paul68980082003-03-25 05:07:42 +00001436 if (ospf->t_spf_calc)
paul718e3742002-12-13 20:15:29 +00001437 {
1438 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001439 zlog_debug ("SPF: calculation timer is already scheduled: %p",
paul0c0f9cd2003-06-06 23:27:04 +00001440 ospf->t_spf_calc);
paul718e3742002-12-13 20:15:29 +00001441 return;
1442 }
pauld24f6e22005-10-21 09:23:12 +00001443
1444 /* XXX Monotic timers: we only care about relative time here. */
Paul Jakma2518efd2006-08-27 06:49:29 +00001445 result = tv_sub (recent_relative_time (), ospf->ts_spf);
pauld24f6e22005-10-21 09:23:12 +00001446
1447 elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
1448 ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
1449
1450 if (ht > ospf->spf_max_holdtime)
1451 ht = ospf->spf_max_holdtime;
1452
paul718e3742002-12-13 20:15:29 +00001453 /* Get SPF calculation delay time. */
pauld24f6e22005-10-21 09:23:12 +00001454 if (elapsed < ht)
paul718e3742002-12-13 20:15:29 +00001455 {
pauld24f6e22005-10-21 09:23:12 +00001456 /* Got an event within the hold time of last SPF. We need to
1457 * increase the hold_multiplier, if it's not already at/past
1458 * maximum value, and wasn't already increased..
1459 */
1460 if (ht < ospf->spf_max_holdtime)
1461 ospf->spf_hold_multiplier++;
1462
1463 /* always honour the SPF initial delay */
1464 if ( (ht - elapsed) < ospf->spf_delay)
paul0c0f9cd2003-06-06 23:27:04 +00001465 delay = ospf->spf_delay;
paul718e3742002-12-13 20:15:29 +00001466 else
pauld24f6e22005-10-21 09:23:12 +00001467 delay = ht - elapsed;
paul718e3742002-12-13 20:15:29 +00001468 }
1469 else
pauld24f6e22005-10-21 09:23:12 +00001470 {
1471 /* Event is past required hold-time of last SPF */
1472 delay = ospf->spf_delay;
1473 ospf->spf_hold_multiplier = 1;
1474 }
1475
paul718e3742002-12-13 20:15:29 +00001476 if (IS_DEBUG_OSPF_EVENT)
pauld24f6e22005-10-21 09:23:12 +00001477 zlog_debug ("SPF: calculation timer delay = %ld", delay);
1478
Dinesh G Dutt50f38b32014-09-30 12:53:28 -07001479 zlog_info ("SPF: Scheduled in %ld msec", delay);
1480
paul68980082003-03-25 05:07:42 +00001481 ospf->t_spf_calc =
pauld24f6e22005-10-21 09:23:12 +00001482 thread_add_timer_msec (master, ospf_spf_calculate_timer, ospf, delay);
paul718e3742002-12-13 20:15:29 +00001483}