blob: 04367f083773e0b1ea7530cc38ce4c6a78c11918 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
hasso508e53e2004-05-18 18:57:06 +00002 * Copyright (C) 2003 Yasuhiro Ohara
paul718e3742002-12-13 20:15:29 +00003 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
hasso508e53e2004-05-18 18:57:06 +000021
paul718e3742002-12-13 20:15:29 +000022/* Shortest Path First calculation for OSPFv3 */
23
hasso508e53e2004-05-18 18:57:06 +000024#include <zebra.h>
paul718e3742002-12-13 20:15:29 +000025
hasso508e53e2004-05-18 18:57:06 +000026#include "log.h"
27#include "memory.h"
28#include "command.h"
29#include "vty.h"
paul718e3742002-12-13 20:15:29 +000030#include "prefix.h"
hasso508e53e2004-05-18 18:57:06 +000031#include "pqueue.h"
32#include "linklist.h"
33#include "thread.h"
paul718e3742002-12-13 20:15:29 +000034
paul718e3742002-12-13 20:15:29 +000035#include "ospf6_lsa.h"
36#include "ospf6_lsdb.h"
37#include "ospf6_route.h"
paul718e3742002-12-13 20:15:29 +000038#include "ospf6_area.h"
hasso508e53e2004-05-18 18:57:06 +000039#include "ospf6_spf.h"
40#include "ospf6_intra.h"
41#include "ospf6_interface.h"
hasso049207c2004-08-04 20:02:13 +000042#include "ospf6d.h"
paul718e3742002-12-13 20:15:29 +000043
hasso508e53e2004-05-18 18:57:06 +000044unsigned char conf_debug_ospf6_spf = 0;
paul718e3742002-12-13 20:15:29 +000045
Paul Jakma6ac29a52008-08-15 13:45:30 +010046static int
hasso508e53e2004-05-18 18:57:06 +000047ospf6_vertex_cmp (void *a, void *b)
paul718e3742002-12-13 20:15:29 +000048{
hasso508e53e2004-05-18 18:57:06 +000049 struct ospf6_vertex *va = (struct ospf6_vertex *) a;
50 struct ospf6_vertex *vb = (struct ospf6_vertex *) b;
51
52 /* ascending order */
Dmitrij Tejbluma1239bc2011-01-13 18:25:40 +030053 if (va->cost != vb->cost)
54 return (va->cost - vb->cost);
55 return (va->hops - vb->hops);
paul718e3742002-12-13 20:15:29 +000056}
57
Paul Jakma6ac29a52008-08-15 13:45:30 +010058static int
hasso508e53e2004-05-18 18:57:06 +000059ospf6_vertex_id_cmp (void *a, void *b)
paul718e3742002-12-13 20:15:29 +000060{
hasso508e53e2004-05-18 18:57:06 +000061 struct ospf6_vertex *va = (struct ospf6_vertex *) a;
62 struct ospf6_vertex *vb = (struct ospf6_vertex *) b;
63 int ret = 0;
paul718e3742002-12-13 20:15:29 +000064
hasso508e53e2004-05-18 18:57:06 +000065 ret = ntohl (ospf6_linkstate_prefix_adv_router (&va->vertex_id)) -
66 ntohl (ospf6_linkstate_prefix_adv_router (&vb->vertex_id));
67 if (ret)
68 return ret;
paul718e3742002-12-13 20:15:29 +000069
hasso508e53e2004-05-18 18:57:06 +000070 ret = ntohl (ospf6_linkstate_prefix_id (&va->vertex_id)) -
71 ntohl (ospf6_linkstate_prefix_id (&vb->vertex_id));
paul718e3742002-12-13 20:15:29 +000072 return ret;
73}
74
Paul Jakma6ac29a52008-08-15 13:45:30 +010075static struct ospf6_vertex *
hasso508e53e2004-05-18 18:57:06 +000076ospf6_vertex_create (struct ospf6_lsa *lsa)
77{
78 struct ospf6_vertex *v;
79 int i;
80
81 v = (struct ospf6_vertex *)
82 XMALLOC (MTYPE_OSPF6_VERTEX, sizeof (struct ospf6_vertex));
83
84 /* type */
85 if (ntohs (lsa->header->type) == OSPF6_LSTYPE_ROUTER)
86 v->type = OSPF6_VERTEX_TYPE_ROUTER;
87 else if (ntohs (lsa->header->type) == OSPF6_LSTYPE_NETWORK)
88 v->type = OSPF6_VERTEX_TYPE_NETWORK;
89 else
90 assert (0);
91
92 /* vertex_id */
93 ospf6_linkstate_prefix (lsa->header->adv_router, lsa->header->id,
94 &v->vertex_id);
95
96 /* name */
97 ospf6_linkstate_prefix2str (&v->vertex_id, v->name, sizeof (v->name));
98
99 /* Associated LSA */
100 v->lsa = lsa;
101
102 /* capability bits + options */
103 v->capability = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header));
104 v->options[0] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 1);
105 v->options[1] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 2);
106 v->options[2] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 3);
107
108 for (i = 0; i < OSPF6_MULTI_PATH_LIMIT; i++)
109 ospf6_nexthop_clear (&v->nexthop[i]);
110
111 v->parent = NULL;
112 v->child_list = list_new ();
113 v->child_list->cmp = ospf6_vertex_id_cmp;
114
115 return v;
116}
117
Paul Jakma6ac29a52008-08-15 13:45:30 +0100118static void
hasso508e53e2004-05-18 18:57:06 +0000119ospf6_vertex_delete (struct ospf6_vertex *v)
paul718e3742002-12-13 20:15:29 +0000120{
hasso508e53e2004-05-18 18:57:06 +0000121 list_delete (v->child_list);
paul718e3742002-12-13 20:15:29 +0000122 XFREE (MTYPE_OSPF6_VERTEX, v);
123}
124
Paul Jakma6ac29a52008-08-15 13:45:30 +0100125static struct ospf6_lsa *
hasso508e53e2004-05-18 18:57:06 +0000126ospf6_lsdesc_lsa (caddr_t lsdesc, struct ospf6_vertex *v)
paul718e3742002-12-13 20:15:29 +0000127{
paul718e3742002-12-13 20:15:29 +0000128 struct ospf6_lsa *lsa;
hasso508e53e2004-05-18 18:57:06 +0000129 u_int16_t type = 0;
130 u_int32_t id = 0, adv_router = 0;
paul718e3742002-12-13 20:15:29 +0000131
hasso508e53e2004-05-18 18:57:06 +0000132 if (VERTEX_IS_TYPE (NETWORK, v))
paul718e3742002-12-13 20:15:29 +0000133 {
hasso508e53e2004-05-18 18:57:06 +0000134 type = htons (OSPF6_LSTYPE_ROUTER);
135 id = htonl (0);
136 adv_router = NETWORK_LSDESC_GET_NBR_ROUTERID (lsdesc);
paul718e3742002-12-13 20:15:29 +0000137 }
paul718e3742002-12-13 20:15:29 +0000138 else
paul718e3742002-12-13 20:15:29 +0000139 {
hasso508e53e2004-05-18 18:57:06 +0000140 if (ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, lsdesc))
paul718e3742002-12-13 20:15:29 +0000141 {
hasso508e53e2004-05-18 18:57:06 +0000142 type = htons (OSPF6_LSTYPE_ROUTER);
143 id = htonl (0);
144 adv_router = ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc);
145 }
146 else if (ROUTER_LSDESC_IS_TYPE (TRANSIT_NETWORK, lsdesc))
147 {
148 type = htons (OSPF6_LSTYPE_NETWORK);
149 id = htonl (ROUTER_LSDESC_GET_NBR_IFID (lsdesc));
150 adv_router = ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc);
paul718e3742002-12-13 20:15:29 +0000151 }
152 }
153
hasso508e53e2004-05-18 18:57:06 +0000154 lsa = ospf6_lsdb_lookup (type, id, adv_router, v->area->lsdb);
paul718e3742002-12-13 20:15:29 +0000155
hasso3b687352004-08-19 06:56:53 +0000156 if (IS_OSPF6_DEBUG_SPF (PROCESS))
paul718e3742002-12-13 20:15:29 +0000157 {
hasso508e53e2004-05-18 18:57:06 +0000158 char ibuf[16], abuf[16];
159 inet_ntop (AF_INET, &id, ibuf, sizeof (ibuf));
160 inet_ntop (AF_INET, &adv_router, abuf, sizeof (abuf));
161 if (lsa)
hassoc6487d62004-12-24 06:00:11 +0000162 zlog_debug (" Link to: %s", lsa->name);
hasso508e53e2004-05-18 18:57:06 +0000163 else
hassoc6487d62004-12-24 06:00:11 +0000164 zlog_debug (" Link to: [%s Id:%s Adv:%s] No LSA",
165 ospf6_lstype_name (type), ibuf, abuf);
paul718e3742002-12-13 20:15:29 +0000166 }
167
hasso508e53e2004-05-18 18:57:06 +0000168 return lsa;
169}
170
Paul Jakma6ac29a52008-08-15 13:45:30 +0100171static char *
hasso508e53e2004-05-18 18:57:06 +0000172ospf6_lsdesc_backlink (struct ospf6_lsa *lsa,
173 caddr_t lsdesc, struct ospf6_vertex *v)
174{
175 caddr_t backlink, found = NULL;
176 int size;
177
178 size = (OSPF6_LSA_IS_TYPE (ROUTER, lsa) ?
179 sizeof (struct ospf6_router_lsdesc) :
180 sizeof (struct ospf6_network_lsdesc));
181 for (backlink = OSPF6_LSA_HEADER_END (lsa->header) + 4;
182 backlink + size <= OSPF6_LSA_END (lsa->header); backlink += size)
183 {
184 assert (! (OSPF6_LSA_IS_TYPE (NETWORK, lsa) &&
185 VERTEX_IS_TYPE (NETWORK, v)));
186
187 if (OSPF6_LSA_IS_TYPE (NETWORK, lsa) &&
188 NETWORK_LSDESC_GET_NBR_ROUTERID (backlink)
189 == v->lsa->header->adv_router)
190 found = backlink;
191 else if (VERTEX_IS_TYPE (NETWORK, v) &&
192 ROUTER_LSDESC_IS_TYPE (TRANSIT_NETWORK, backlink) &&
193 ROUTER_LSDESC_GET_NBR_ROUTERID (backlink)
194 == v->lsa->header->adv_router &&
195 ROUTER_LSDESC_GET_NBR_IFID (backlink)
196 == ntohl (v->lsa->header->id))
197 found = backlink;
198 else
199 {
200 if (! ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, backlink) ||
201 ! ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, lsdesc))
202 continue;
203 if (ROUTER_LSDESC_GET_NBR_IFID (backlink) !=
204 ROUTER_LSDESC_GET_IFID (lsdesc) ||
205 ROUTER_LSDESC_GET_NBR_IFID (lsdesc) !=
206 ROUTER_LSDESC_GET_IFID (backlink))
207 continue;
208 if (ROUTER_LSDESC_GET_NBR_ROUTERID (backlink) !=
209 v->lsa->header->adv_router ||
210 ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc) !=
211 lsa->header->adv_router)
212 continue;
213 found = backlink;
214 }
215 }
216
hasso3b687352004-08-19 06:56:53 +0000217 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000218 zlog_debug (" Backlink %s", (found ? "OK" : "FAIL"));
hasso508e53e2004-05-18 18:57:06 +0000219
220 return found;
221}
222
Paul Jakma6ac29a52008-08-15 13:45:30 +0100223static void
hasso508e53e2004-05-18 18:57:06 +0000224ospf6_nexthop_calc (struct ospf6_vertex *w, struct ospf6_vertex *v,
225 caddr_t lsdesc)
226{
227 int i, ifindex;
228 struct ospf6_interface *oi;
229 u_int16_t type;
230 u_int32_t adv_router;
231 struct ospf6_lsa *lsa;
232 struct ospf6_link_lsa *link_lsa;
233 char buf[64];
234
235 assert (VERTEX_IS_TYPE (ROUTER, w));
236 ifindex = (VERTEX_IS_TYPE (NETWORK, v) ? v->nexthop[0].ifindex :
237 ROUTER_LSDESC_GET_IFID (lsdesc));
238 oi = ospf6_interface_lookup_by_ifindex (ifindex);
239 if (oi == NULL)
240 {
hasso3b687352004-08-19 06:56:53 +0000241 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000242 zlog_debug ("Can't find interface in SPF: ifindex %d", ifindex);
hasso508e53e2004-05-18 18:57:06 +0000243 return;
244 }
245
246 type = htons (OSPF6_LSTYPE_LINK);
247 adv_router = (VERTEX_IS_TYPE (NETWORK, v) ?
248 NETWORK_LSDESC_GET_NBR_ROUTERID (lsdesc) :
249 ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc));
250
251 i = 0;
252 for (lsa = ospf6_lsdb_type_router_head (type, adv_router, oi->lsdb); lsa;
253 lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa))
254 {
255 if (VERTEX_IS_TYPE (ROUTER, v) &&
256 htonl (ROUTER_LSDESC_GET_NBR_IFID (lsdesc)) != lsa->header->id)
257 continue;
258
259 link_lsa = (struct ospf6_link_lsa *) OSPF6_LSA_HEADER_END (lsa->header);
hasso3b687352004-08-19 06:56:53 +0000260 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hasso508e53e2004-05-18 18:57:06 +0000261 {
262 inet_ntop (AF_INET6, &link_lsa->linklocal_addr, buf, sizeof (buf));
hassoc6487d62004-12-24 06:00:11 +0000263 zlog_debug (" nexthop %s from %s", buf, lsa->name);
hasso508e53e2004-05-18 18:57:06 +0000264 }
265
266 if (i < OSPF6_MULTI_PATH_LIMIT)
267 {
268 memcpy (&w->nexthop[i].address, &link_lsa->linklocal_addr,
269 sizeof (struct in6_addr));
270 w->nexthop[i].ifindex = ifindex;
271 i++;
272 }
273 }
274
hasso3b687352004-08-19 06:56:53 +0000275 if (i == 0 && IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000276 zlog_debug ("No nexthop for %s found", w->name);
hasso508e53e2004-05-18 18:57:06 +0000277}
278
Paul Jakma6ac29a52008-08-15 13:45:30 +0100279static int
hasso508e53e2004-05-18 18:57:06 +0000280ospf6_spf_install (struct ospf6_vertex *v,
281 struct ospf6_route_table *result_table)
282{
283 struct ospf6_route *route;
284 int i, j;
285 struct ospf6_vertex *prev, *w;
paul1eb8ef22005-04-07 07:30:20 +0000286 struct listnode *node, *nnode;
hasso508e53e2004-05-18 18:57:06 +0000287
hasso3b687352004-08-19 06:56:53 +0000288 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000289 zlog_debug ("SPF install %s hops %d cost %d",
290 v->name, v->hops, v->cost);
hasso508e53e2004-05-18 18:57:06 +0000291
292 route = ospf6_route_lookup (&v->vertex_id, result_table);
293 if (route && route->path.cost < v->cost)
294 {
hasso3b687352004-08-19 06:56:53 +0000295 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000296 zlog_debug (" already installed with lower cost (%d), ignore",
297 route->path.cost);
hasso508e53e2004-05-18 18:57:06 +0000298 ospf6_vertex_delete (v);
299 return -1;
300 }
301 else if (route && route->path.cost == v->cost)
302 {
hasso3b687352004-08-19 06:56:53 +0000303 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000304 zlog_debug (" another path found, merge");
hasso508e53e2004-05-18 18:57:06 +0000305
306 for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
307 i < OSPF6_MULTI_PATH_LIMIT; i++)
308 {
309 for (j = 0; j < OSPF6_MULTI_PATH_LIMIT; j++)
310 {
311 if (ospf6_nexthop_is_set (&route->nexthop[j]))
312 {
313 if (ospf6_nexthop_is_same (&route->nexthop[j],
314 &v->nexthop[i]))
315 break;
316 else
317 continue;
318 }
319 ospf6_nexthop_copy (&route->nexthop[j], &v->nexthop[i]);
320 break;
321 }
322 }
323
324 prev = (struct ospf6_vertex *) route->route_option;
Dmitrij Tejbluma1239bc2011-01-13 18:25:40 +0300325 assert (prev->hops <= v->hops);
326 ospf6_vertex_delete (v);
hasso508e53e2004-05-18 18:57:06 +0000327
328 return -1;
329 }
330
331 /* There should be no case where candidate being installed (variable
332 "v") is closer than the one in the SPF tree (variable "route").
hasso6452df02004-08-15 05:52:07 +0000333 In the case something has gone wrong with the behavior of
hasso508e53e2004-05-18 18:57:06 +0000334 Priority-Queue. */
hasso6452df02004-08-15 05:52:07 +0000335
336 /* the case where the route exists already is handled and returned
337 up to here. */
hasso508e53e2004-05-18 18:57:06 +0000338 assert (route == NULL);
339
340 route = ospf6_route_create ();
341 memcpy (&route->prefix, &v->vertex_id, sizeof (struct prefix));
342 route->type = OSPF6_DEST_TYPE_LINKSTATE;
343 route->path.type = OSPF6_PATH_TYPE_INTRA;
344 route->path.origin.type = v->lsa->header->type;
345 route->path.origin.id = v->lsa->header->id;
346 route->path.origin.adv_router = v->lsa->header->adv_router;
347 route->path.metric_type = 1;
348 route->path.cost = v->cost;
349 route->path.cost_e2 = v->hops;
350 route->path.router_bits = v->capability;
351 route->path.options[0] = v->options[0];
352 route->path.options[1] = v->options[1];
353 route->path.options[2] = v->options[2];
354
355 for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
356 i < OSPF6_MULTI_PATH_LIMIT; i++)
357 ospf6_nexthop_copy (&route->nexthop[i], &v->nexthop[i]);
358
359 if (v->parent)
360 listnode_add_sort (v->parent->child_list, v);
361 route->route_option = v;
362
363 ospf6_route_add (route, result_table);
paul718e3742002-12-13 20:15:29 +0000364 return 0;
365}
366
hasso508e53e2004-05-18 18:57:06 +0000367void
368ospf6_spf_table_finish (struct ospf6_route_table *result_table)
369{
370 struct ospf6_route *route;
371 struct ospf6_vertex *v;
372 for (route = ospf6_route_head (result_table); route;
373 route = ospf6_route_next (route))
374 {
375 v = (struct ospf6_vertex *) route->route_option;
376 ospf6_vertex_delete (v);
377 ospf6_route_remove (route, result_table);
378 }
379}
380
hasso6452df02004-08-15 05:52:07 +0000381/* RFC2328 16.1. Calculating the shortest-path tree for an area */
382/* RFC2740 3.8.1. Calculating the shortest path tree for an area */
hasso508e53e2004-05-18 18:57:06 +0000383void
384ospf6_spf_calculation (u_int32_t router_id,
385 struct ospf6_route_table *result_table,
386 struct ospf6_area *oa)
387{
388 struct pqueue *candidate_list;
389 struct ospf6_vertex *root, *v, *w;
390 int i;
391 int size;
392 caddr_t lsdesc;
393 struct ospf6_lsa *lsa;
394
395 /* initialize */
396 candidate_list = pqueue_create ();
397 candidate_list->cmp = ospf6_vertex_cmp;
398
399 ospf6_spf_table_finish (result_table);
400
401 /* Install the calculating router itself as the root of the SPF tree */
402 /* construct root vertex */
403 lsa = ospf6_lsdb_lookup (htons (OSPF6_LSTYPE_ROUTER), htonl (0),
404 router_id, oa->lsdb);
405 if (lsa == NULL)
406 return;
407 root = ospf6_vertex_create (lsa);
408 root->area = oa;
409 root->cost = 0;
410 root->hops = 0;
hasso6452df02004-08-15 05:52:07 +0000411 root->nexthop[0].ifindex = 0; /* loopbak I/F is better ... */
hasso508e53e2004-05-18 18:57:06 +0000412 inet_pton (AF_INET6, "::1", &root->nexthop[0].address);
413
414 /* Actually insert root to the candidate-list as the only candidate */
415 pqueue_enqueue (root, candidate_list);
416
417 /* Iterate until candidate-list becomes empty */
418 while (candidate_list->size)
419 {
420 /* get closest candidate from priority queue */
421 v = pqueue_dequeue (candidate_list);
422
hasso6452df02004-08-15 05:52:07 +0000423 /* installing may result in merging or rejecting of the vertex */
hasso508e53e2004-05-18 18:57:06 +0000424 if (ospf6_spf_install (v, result_table) < 0)
425 continue;
426
427 /* For each LS description in the just-added vertex V's LSA */
428 size = (VERTEX_IS_TYPE (ROUTER, v) ?
429 sizeof (struct ospf6_router_lsdesc) :
430 sizeof (struct ospf6_network_lsdesc));
431 for (lsdesc = OSPF6_LSA_HEADER_END (v->lsa->header) + 4;
432 lsdesc + size <= OSPF6_LSA_END (v->lsa->header); lsdesc += size)
433 {
434 lsa = ospf6_lsdesc_lsa (lsdesc, v);
435 if (lsa == NULL)
436 continue;
437
438 if (! ospf6_lsdesc_backlink (lsa, lsdesc, v))
439 continue;
440
441 w = ospf6_vertex_create (lsa);
442 w->area = oa;
443 w->parent = v;
444 if (VERTEX_IS_TYPE (ROUTER, v))
445 {
446 w->cost = v->cost + ROUTER_LSDESC_GET_METRIC (lsdesc);
447 w->hops = v->hops + (VERTEX_IS_TYPE (NETWORK, w) ? 0 : 1);
448 }
449 else /* NETWORK */
450 {
451 w->cost = v->cost;
452 w->hops = v->hops + 1;
453 }
454
455 /* nexthop calculation */
456 if (w->hops == 0)
457 w->nexthop[0].ifindex = ROUTER_LSDESC_GET_IFID (lsdesc);
458 else if (w->hops == 1 && v->hops == 0)
459 ospf6_nexthop_calc (w, v, lsdesc);
460 else
461 {
462 for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
463 i < OSPF6_MULTI_PATH_LIMIT; i++)
464 ospf6_nexthop_copy (&w->nexthop[i], &v->nexthop[i]);
465 }
466
467 /* add new candidate to the candidate_list */
hasso3b687352004-08-19 06:56:53 +0000468 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000469 zlog_debug (" New candidate: %s hops %d cost %d",
470 w->name, w->hops, w->cost);
hasso508e53e2004-05-18 18:57:06 +0000471 pqueue_enqueue (w, candidate_list);
472 }
473 }
474
475 pqueue_delete (candidate_list);
476}
477
Paul Jakma6ac29a52008-08-15 13:45:30 +0100478static void
hasso2680aa22004-11-25 20:54:46 +0000479ospf6_spf_log_database (struct ospf6_area *oa)
480{
481 char *p, *end, buffer[256];
482 struct listnode *node;
483 struct ospf6_interface *oi;
484
485 p = buffer;
486 end = buffer + sizeof (buffer);
487
488 snprintf (p, end - p, "SPF on DB (#LSAs):");
489 p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
490 snprintf (p, end - p, " Area %s: %d", oa->name, oa->lsdb->count);
491 p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
492
paul1eb8ef22005-04-07 07:30:20 +0000493 for (ALL_LIST_ELEMENTS_RO (oa->if_list, node, oi))
hasso2680aa22004-11-25 20:54:46 +0000494 {
hasso2680aa22004-11-25 20:54:46 +0000495 snprintf (p, end - p, " I/F %s: %d",
496 oi->interface->name, oi->lsdb->count);
497 p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
498 }
499
hassoc6487d62004-12-24 06:00:11 +0000500 zlog_debug ("%s", buffer);
hasso2680aa22004-11-25 20:54:46 +0000501}
502
Paul Jakma6ac29a52008-08-15 13:45:30 +0100503static int
paul718e3742002-12-13 20:15:29 +0000504ospf6_spf_calculation_thread (struct thread *t)
505{
hasso508e53e2004-05-18 18:57:06 +0000506 struct ospf6_area *oa;
507 struct timeval start, end, runtime;
paul718e3742002-12-13 20:15:29 +0000508
hasso508e53e2004-05-18 18:57:06 +0000509 oa = (struct ospf6_area *) THREAD_ARG (t);
510 oa->thread_spf_calculation = NULL;
paul718e3742002-12-13 20:15:29 +0000511
hasso2680aa22004-11-25 20:54:46 +0000512 if (IS_OSPF6_DEBUG_SPF (PROCESS))
hassoc6487d62004-12-24 06:00:11 +0000513 zlog_debug ("SPF calculation for Area %s", oa->name);
hasso2680aa22004-11-25 20:54:46 +0000514 if (IS_OSPF6_DEBUG_SPF (DATABASE))
515 ospf6_spf_log_database (oa);
paul718e3742002-12-13 20:15:29 +0000516
517 /* execute SPF calculation */
Takashi Sogabe86f72dc2009-06-22 13:07:02 +0900518 quagga_gettime (QUAGGA_CLK_MONOTONIC, &start);
hasso508e53e2004-05-18 18:57:06 +0000519 ospf6_spf_calculation (oa->ospf6->router_id, oa->spf_table, oa);
Takashi Sogabe86f72dc2009-06-22 13:07:02 +0900520 quagga_gettime (QUAGGA_CLK_MONOTONIC, &end);
hasso2680aa22004-11-25 20:54:46 +0000521 timersub (&end, &start, &runtime);
paul718e3742002-12-13 20:15:29 +0000522
hasso3b687352004-08-19 06:56:53 +0000523 if (IS_OSPF6_DEBUG_SPF (PROCESS) || IS_OSPF6_DEBUG_SPF (TIME))
hassoc6487d62004-12-24 06:00:11 +0000524 zlog_debug ("SPF runtime: %ld sec %ld usec",
525 runtime.tv_sec, runtime.tv_usec);
paul718e3742002-12-13 20:15:29 +0000526
hasso508e53e2004-05-18 18:57:06 +0000527 ospf6_intra_route_calculation (oa);
hasso6452df02004-08-15 05:52:07 +0000528 ospf6_intra_brouter_calculation (oa);
paul718e3742002-12-13 20:15:29 +0000529
530 return 0;
531}
532
533void
hasso508e53e2004-05-18 18:57:06 +0000534ospf6_spf_schedule (struct ospf6_area *oa)
paul718e3742002-12-13 20:15:29 +0000535{
hasso508e53e2004-05-18 18:57:06 +0000536 if (oa->thread_spf_calculation)
paul718e3742002-12-13 20:15:29 +0000537 return;
hasso508e53e2004-05-18 18:57:06 +0000538 oa->thread_spf_calculation =
539 thread_add_event (master, ospf6_spf_calculation_thread, oa, 0);
paul718e3742002-12-13 20:15:29 +0000540}
541
542void
paul0c083ee2004-10-10 12:54:58 +0000543ospf6_spf_display_subtree (struct vty *vty, const char *prefix, int rest,
hasso508e53e2004-05-18 18:57:06 +0000544 struct ospf6_vertex *v)
paul718e3742002-12-13 20:15:29 +0000545{
paul1eb8ef22005-04-07 07:30:20 +0000546 struct listnode *node, *nnode;
hasso508e53e2004-05-18 18:57:06 +0000547 struct ospf6_vertex *c;
548 char *next_prefix;
549 int len;
paul718e3742002-12-13 20:15:29 +0000550 int restnum;
paul718e3742002-12-13 20:15:29 +0000551
hasso508e53e2004-05-18 18:57:06 +0000552 /* "prefix" is the space prefix of the display line */
hasso049207c2004-08-04 20:02:13 +0000553 vty_out (vty, "%s+-%s [%d]%s", prefix, v->name, v->cost, VNL);
paul718e3742002-12-13 20:15:29 +0000554
hasso508e53e2004-05-18 18:57:06 +0000555 len = strlen (prefix) + 4;
556 next_prefix = (char *) malloc (len);
557 if (next_prefix == NULL)
paul718e3742002-12-13 20:15:29 +0000558 {
hasso049207c2004-08-04 20:02:13 +0000559 vty_out (vty, "malloc failed%s", VNL);
paul718e3742002-12-13 20:15:29 +0000560 return;
561 }
hasso508e53e2004-05-18 18:57:06 +0000562 snprintf (next_prefix, len, "%s%s", prefix, (rest ? "| " : " "));
paul718e3742002-12-13 20:15:29 +0000563
hasso508e53e2004-05-18 18:57:06 +0000564 restnum = listcount (v->child_list);
paul1eb8ef22005-04-07 07:30:20 +0000565 for (ALL_LIST_ELEMENTS (v->child_list, node, nnode, c))
paul718e3742002-12-13 20:15:29 +0000566 {
hasso508e53e2004-05-18 18:57:06 +0000567 restnum--;
568 ospf6_spf_display_subtree (vty, next_prefix, restnum, c);
paul718e3742002-12-13 20:15:29 +0000569 }
570
hasso508e53e2004-05-18 18:57:06 +0000571 free (next_prefix);
paul718e3742002-12-13 20:15:29 +0000572}
573
hasso3b687352004-08-19 06:56:53 +0000574DEFUN (debug_ospf6_spf_process,
575 debug_ospf6_spf_process_cmd,
576 "debug ospf6 spf process",
hasso508e53e2004-05-18 18:57:06 +0000577 DEBUG_STR
paul718e3742002-12-13 20:15:29 +0000578 OSPF6_STR
hasso508e53e2004-05-18 18:57:06 +0000579 "Debug SPF Calculation\n"
hasso3b687352004-08-19 06:56:53 +0000580 "Debug Detailed SPF Process\n"
hasso508e53e2004-05-18 18:57:06 +0000581 )
paul718e3742002-12-13 20:15:29 +0000582{
hasso508e53e2004-05-18 18:57:06 +0000583 unsigned char level = 0;
hasso3b687352004-08-19 06:56:53 +0000584 level = OSPF6_DEBUG_SPF_PROCESS;
hasso508e53e2004-05-18 18:57:06 +0000585 OSPF6_DEBUG_SPF_ON (level);
paul718e3742002-12-13 20:15:29 +0000586 return CMD_SUCCESS;
587}
588
hasso3b687352004-08-19 06:56:53 +0000589DEFUN (debug_ospf6_spf_time,
590 debug_ospf6_spf_time_cmd,
591 "debug ospf6 spf time",
hasso508e53e2004-05-18 18:57:06 +0000592 DEBUG_STR
paul718e3742002-12-13 20:15:29 +0000593 OSPF6_STR
hasso508e53e2004-05-18 18:57:06 +0000594 "Debug SPF Calculation\n"
hasso3b687352004-08-19 06:56:53 +0000595 "Measure time taken by SPF Calculation\n"
hasso508e53e2004-05-18 18:57:06 +0000596 )
paul718e3742002-12-13 20:15:29 +0000597{
hasso508e53e2004-05-18 18:57:06 +0000598 unsigned char level = 0;
hasso3b687352004-08-19 06:56:53 +0000599 level = OSPF6_DEBUG_SPF_TIME;
hasso508e53e2004-05-18 18:57:06 +0000600 OSPF6_DEBUG_SPF_ON (level);
601 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +0000602}
603
hasso2680aa22004-11-25 20:54:46 +0000604DEFUN (debug_ospf6_spf_database,
605 debug_ospf6_spf_database_cmd,
606 "debug ospf6 spf database",
607 DEBUG_STR
608 OSPF6_STR
609 "Debug SPF Calculation\n"
610 "Log number of LSAs at SPF Calculation time\n"
611 )
612{
613 unsigned char level = 0;
614 level = OSPF6_DEBUG_SPF_DATABASE;
615 OSPF6_DEBUG_SPF_ON (level);
616 return CMD_SUCCESS;
617}
618
hasso3b687352004-08-19 06:56:53 +0000619DEFUN (no_debug_ospf6_spf_process,
620 no_debug_ospf6_spf_process_cmd,
621 "no debug ospf6 spf process",
hasso508e53e2004-05-18 18:57:06 +0000622 NO_STR
623 DEBUG_STR
paul718e3742002-12-13 20:15:29 +0000624 OSPF6_STR
hasso508e53e2004-05-18 18:57:06 +0000625 "Quit Debugging SPF Calculation\n"
hasso3b687352004-08-19 06:56:53 +0000626 "Quit Debugging Detailed SPF Process\n"
hasso508e53e2004-05-18 18:57:06 +0000627 )
628{
629 unsigned char level = 0;
hasso3b687352004-08-19 06:56:53 +0000630 level = OSPF6_DEBUG_SPF_PROCESS;
hasso508e53e2004-05-18 18:57:06 +0000631 OSPF6_DEBUG_SPF_OFF (level);
632 return CMD_SUCCESS;
633}
paul718e3742002-12-13 20:15:29 +0000634
hasso3b687352004-08-19 06:56:53 +0000635DEFUN (no_debug_ospf6_spf_time,
636 no_debug_ospf6_spf_time_cmd,
637 "no debug ospf6 spf time",
hasso508e53e2004-05-18 18:57:06 +0000638 NO_STR
639 DEBUG_STR
paul718e3742002-12-13 20:15:29 +0000640 OSPF6_STR
hasso508e53e2004-05-18 18:57:06 +0000641 "Quit Debugging SPF Calculation\n"
hasso3b687352004-08-19 06:56:53 +0000642 "Quit Measuring time taken by SPF Calculation\n"
hasso508e53e2004-05-18 18:57:06 +0000643 )
644{
645 unsigned char level = 0;
hasso3b687352004-08-19 06:56:53 +0000646 level = OSPF6_DEBUG_SPF_TIME;
hasso508e53e2004-05-18 18:57:06 +0000647 OSPF6_DEBUG_SPF_OFF (level);
648 return CMD_SUCCESS;
649}
650
hasso2680aa22004-11-25 20:54:46 +0000651DEFUN (no_debug_ospf6_spf_database,
652 no_debug_ospf6_spf_database_cmd,
653 "no debug ospf6 spf database",
654 NO_STR
655 DEBUG_STR
656 OSPF6_STR
657 "Debug SPF Calculation\n"
658 "Quit Logging number of LSAs at SPF Calculation time\n"
659 )
660{
661 unsigned char level = 0;
662 level = OSPF6_DEBUG_SPF_DATABASE;
663 OSPF6_DEBUG_SPF_OFF (level);
664 return CMD_SUCCESS;
665}
666
hasso508e53e2004-05-18 18:57:06 +0000667int
668config_write_ospf6_debug_spf (struct vty *vty)
669{
hasso3b687352004-08-19 06:56:53 +0000670 if (IS_OSPF6_DEBUG_SPF (PROCESS))
671 vty_out (vty, "debug ospf6 spf process%s", VNL);
672 if (IS_OSPF6_DEBUG_SPF (TIME))
673 vty_out (vty, "debug ospf6 spf time%s", VNL);
hasso2680aa22004-11-25 20:54:46 +0000674 if (IS_OSPF6_DEBUG_SPF (DATABASE))
675 vty_out (vty, "debug ospf6 spf database%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000676 return 0;
677}
678
679void
Paul Jakma6ac29a52008-08-15 13:45:30 +0100680install_element_ospf6_debug_spf (void)
hasso508e53e2004-05-18 18:57:06 +0000681{
hasso3b687352004-08-19 06:56:53 +0000682 install_element (ENABLE_NODE, &debug_ospf6_spf_process_cmd);
683 install_element (ENABLE_NODE, &debug_ospf6_spf_time_cmd);
hasso2680aa22004-11-25 20:54:46 +0000684 install_element (ENABLE_NODE, &debug_ospf6_spf_database_cmd);
hasso3b687352004-08-19 06:56:53 +0000685 install_element (ENABLE_NODE, &no_debug_ospf6_spf_process_cmd);
686 install_element (ENABLE_NODE, &no_debug_ospf6_spf_time_cmd);
hasso2680aa22004-11-25 20:54:46 +0000687 install_element (ENABLE_NODE, &no_debug_ospf6_spf_database_cmd);
hasso3b687352004-08-19 06:56:53 +0000688 install_element (CONFIG_NODE, &debug_ospf6_spf_process_cmd);
689 install_element (CONFIG_NODE, &debug_ospf6_spf_time_cmd);
hasso2680aa22004-11-25 20:54:46 +0000690 install_element (CONFIG_NODE, &debug_ospf6_spf_database_cmd);
hasso3b687352004-08-19 06:56:53 +0000691 install_element (CONFIG_NODE, &no_debug_ospf6_spf_process_cmd);
692 install_element (CONFIG_NODE, &no_debug_ospf6_spf_time_cmd);
hasso2680aa22004-11-25 20:54:46 +0000693 install_element (CONFIG_NODE, &no_debug_ospf6_spf_database_cmd);
hasso508e53e2004-05-18 18:57:06 +0000694}
paul718e3742002-12-13 20:15:29 +0000695
696void
Paul Jakma6ac29a52008-08-15 13:45:30 +0100697ospf6_spf_init (void)
paul718e3742002-12-13 20:15:29 +0000698{
paul718e3742002-12-13 20:15:29 +0000699}
700
hasso508e53e2004-05-18 18:57:06 +0000701