paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* OSPF SPF calculation. |
| 2 | Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada |
| 3 | |
| 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 Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "thread.h" |
| 24 | #include "memory.h" |
| 25 | #include "hash.h" |
| 26 | #include "linklist.h" |
| 27 | #include "prefix.h" |
| 28 | #include "if.h" |
| 29 | #include "table.h" |
| 30 | #include "log.h" |
| 31 | #include "sockunion.h" /* for inet_ntop () */ |
| 32 | |
| 33 | #include "ospfd/ospfd.h" |
| 34 | #include "ospfd/ospf_interface.h" |
| 35 | #include "ospfd/ospf_ism.h" |
| 36 | #include "ospfd/ospf_asbr.h" |
| 37 | #include "ospfd/ospf_lsa.h" |
| 38 | #include "ospfd/ospf_lsdb.h" |
| 39 | #include "ospfd/ospf_neighbor.h" |
| 40 | #include "ospfd/ospf_nsm.h" |
| 41 | #include "ospfd/ospf_spf.h" |
| 42 | #include "ospfd/ospf_route.h" |
| 43 | #include "ospfd/ospf_ia.h" |
| 44 | #include "ospfd/ospf_ase.h" |
| 45 | #include "ospfd/ospf_abr.h" |
| 46 | #include "ospfd/ospf_dump.h" |
| 47 | |
| 48 | #define DEBUG |
| 49 | |
| 50 | struct vertex_nexthop * |
| 51 | vertex_nexthop_new (struct vertex *parent) |
| 52 | { |
| 53 | struct vertex_nexthop *new; |
| 54 | |
| 55 | new = XCALLOC (MTYPE_OSPF_NEXTHOP, sizeof (struct vertex_nexthop)); |
| 56 | new->parent = parent; |
| 57 | |
| 58 | return new; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | vertex_nexthop_free (struct vertex_nexthop *nh) |
| 63 | { |
| 64 | XFREE (MTYPE_OSPF_NEXTHOP, nh); |
| 65 | } |
| 66 | |
| 67 | struct vertex_nexthop * |
| 68 | vertex_nexthop_dup (struct vertex_nexthop *nh) |
| 69 | { |
| 70 | struct vertex_nexthop *new; |
| 71 | |
| 72 | new = vertex_nexthop_new (nh->parent); |
| 73 | |
| 74 | new->oi = nh->oi; |
| 75 | new->router = nh->router; |
| 76 | |
| 77 | return new; |
| 78 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 80 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | struct vertex * |
| 82 | ospf_vertex_new (struct ospf_lsa *lsa) |
| 83 | { |
| 84 | struct vertex *new; |
| 85 | |
| 86 | new = XMALLOC (MTYPE_OSPF_VERTEX, sizeof (struct vertex)); |
| 87 | memset (new, 0, sizeof (struct vertex)); |
| 88 | |
| 89 | new->flags = 0; |
| 90 | new->type = lsa->data->type; |
| 91 | new->id = lsa->data->id; |
| 92 | new->lsa = lsa->data; |
| 93 | new->distance = 0; |
| 94 | new->child = list_new (); |
| 95 | new->nexthop = list_new (); |
| 96 | |
| 97 | return new; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | ospf_vertex_free (struct vertex *v) |
| 102 | { |
| 103 | listnode node; |
| 104 | |
| 105 | list_delete (v->child); |
| 106 | |
| 107 | if (listcount (v->nexthop) > 0) |
| 108 | for (node = listhead (v->nexthop); node; nextnode (node)) |
| 109 | vertex_nexthop_free (node->data); |
| 110 | |
| 111 | list_delete (v->nexthop); |
| 112 | |
| 113 | XFREE (MTYPE_OSPF_VERTEX, v); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | ospf_vertex_add_parent (struct vertex *v) |
| 118 | { |
| 119 | struct vertex_nexthop *nh; |
| 120 | listnode node; |
| 121 | |
| 122 | for (node = listhead (v->nexthop); node; nextnode (node)) |
| 123 | { |
| 124 | nh = (struct vertex_nexthop *) getdata (node); |
| 125 | |
| 126 | /* No need to add two links from the same parent. */ |
| 127 | if (listnode_lookup (nh->parent->child, v) == NULL) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 128 | listnode_add (nh->parent->child, v); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | ospf_spf_init (struct ospf_area *area) |
| 134 | { |
| 135 | struct vertex *v; |
| 136 | |
| 137 | /* Create root node. */ |
| 138 | v = ospf_vertex_new (area->router_lsa_self); |
| 139 | |
| 140 | area->spf = v; |
| 141 | |
| 142 | /* Reset ABR and ASBR router counts. */ |
| 143 | area->abr_count = 0; |
| 144 | area->asbr_count = 0; |
| 145 | } |
| 146 | |
| 147 | int |
| 148 | ospf_spf_has_vertex (struct route_table *rv, struct route_table *nv, |
| 149 | struct lsa_header *lsa) |
| 150 | { |
| 151 | struct prefix p; |
| 152 | struct route_node *rn; |
| 153 | |
| 154 | p.family = AF_INET; |
| 155 | p.prefixlen = IPV4_MAX_BITLEN; |
| 156 | p.u.prefix4 = lsa->id; |
| 157 | |
| 158 | if (lsa->type == OSPF_ROUTER_LSA) |
| 159 | rn = route_node_get (rv, &p); |
| 160 | else |
| 161 | rn = route_node_get (nv, &p); |
| 162 | |
| 163 | if (rn->info != NULL) |
| 164 | { |
| 165 | route_unlock_node (rn); |
| 166 | return 1; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | listnode |
| 172 | ospf_vertex_lookup (list vlist, struct in_addr id, int type) |
| 173 | { |
| 174 | listnode node; |
| 175 | struct vertex *v; |
| 176 | |
| 177 | for (node = listhead (vlist); node; nextnode (node)) |
| 178 | { |
| 179 | v = (struct vertex *) getdata (node); |
| 180 | if (IPV4_ADDR_SAME (&id, &v->id) && type == v->type) |
| 181 | return node; |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | int |
| 188 | ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v) |
| 189 | { |
| 190 | int i; |
| 191 | int length; |
| 192 | struct router_lsa *rl; |
| 193 | struct network_lsa *nl; |
| 194 | |
| 195 | /* In case of W is Network LSA. */ |
| 196 | if (w->type == OSPF_NETWORK_LSA) |
| 197 | { |
| 198 | if (v->type == OSPF_NETWORK_LSA) |
| 199 | return 0; |
| 200 | |
| 201 | nl = (struct network_lsa *) w; |
| 202 | length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 203 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | for (i = 0; i < length; i++) |
| 205 | if (IPV4_ADDR_SAME (&nl->routers[i], &v->id)) |
| 206 | return 1; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* In case of W is Router LSA. */ |
| 211 | if (w->type == OSPF_ROUTER_LSA) |
| 212 | { |
| 213 | rl = (struct router_lsa *) w; |
| 214 | |
| 215 | length = ntohs (w->length); |
| 216 | |
| 217 | for (i = 0; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 218 | i < ntohs (rl->links) && length >= sizeof (struct router_lsa); |
| 219 | i++, length -= 12) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | { |
| 221 | switch (rl->link[i].type) |
| 222 | { |
| 223 | case LSA_LINK_TYPE_POINTOPOINT: |
| 224 | case LSA_LINK_TYPE_VIRTUALLINK: |
| 225 | /* Router LSA ID. */ |
| 226 | if (v->type == OSPF_ROUTER_LSA && |
| 227 | IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id)) |
| 228 | { |
| 229 | return 1; |
| 230 | } |
| 231 | break; |
| 232 | case LSA_LINK_TYPE_TRANSIT: |
| 233 | /* Network LSA ID. */ |
| 234 | if (v->type == OSPF_NETWORK_LSA && |
| 235 | IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id)) |
| 236 | { |
| 237 | return 1; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 238 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | break; |
| 240 | case LSA_LINK_TYPE_STUB: |
| 241 | /* Not take into count? */ |
| 242 | continue; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* Add the nexthop to the list, only if it is unique. |
| 252 | * If it's not unique, free the nexthop entry. |
| 253 | */ |
| 254 | void |
| 255 | ospf_nexthop_add_unique (struct vertex_nexthop *new, list nexthop) |
| 256 | { |
| 257 | struct vertex_nexthop *nh; |
| 258 | listnode node; |
| 259 | int match; |
| 260 | |
| 261 | match = 0; |
| 262 | for (node = listhead (nexthop); node; nextnode (node)) |
| 263 | { |
| 264 | nh = node->data; |
| 265 | |
| 266 | /* Compare the two entries. */ |
| 267 | /* XXX |
| 268 | * Comparing the parent preserves the shortest path tree |
| 269 | * structure even when the nexthops are identical. |
| 270 | */ |
| 271 | if (nh->oi == new->oi && |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 272 | IPV4_ADDR_SAME (&nh->router, &new->router) && |
| 273 | nh->parent == new->parent) |
| 274 | { |
| 275 | match = 1; |
| 276 | break; |
| 277 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (!match) |
| 281 | listnode_add (nexthop, new); |
| 282 | else |
| 283 | vertex_nexthop_free (new); |
| 284 | } |
| 285 | |
| 286 | /* Merge entries in list b into list a. */ |
| 287 | void |
| 288 | ospf_nexthop_merge (list a, list b) |
| 289 | { |
| 290 | struct listnode *n; |
| 291 | |
| 292 | for (n = listhead (b); n; nextnode (n)) |
| 293 | { |
| 294 | ospf_nexthop_add_unique (n->data, a); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | #define ROUTER_LSA_MIN_SIZE 12 |
| 299 | #define ROUTER_LSA_TOS_SIZE 4 |
| 300 | |
| 301 | struct router_lsa_link * |
| 302 | ospf_get_next_link (struct vertex *v, struct vertex *w, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 303 | struct router_lsa_link *prev_link) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | { |
| 305 | u_char *p; |
| 306 | u_char *lim; |
| 307 | struct router_lsa_link *l; |
| 308 | |
| 309 | if (prev_link == NULL) |
| 310 | p = ((u_char *) v->lsa) + 24; |
| 311 | else |
| 312 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 313 | p = (u_char *) prev_link; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | p += (ROUTER_LSA_MIN_SIZE + |
| 315 | (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 316 | } |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 317 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 319 | |
| 320 | while (p < lim) |
| 321 | { |
| 322 | l = (struct router_lsa_link *) p; |
| 323 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 324 | p += (ROUTER_LSA_MIN_SIZE + (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | |
| 326 | if (l->m[0].type == LSA_LINK_TYPE_STUB) |
| 327 | continue; |
| 328 | |
| 329 | /* Defer NH calculation via VLs until summaries from |
| 330 | transit areas area confidered */ |
| 331 | |
| 332 | if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 333 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | |
| 335 | if (IPV4_ADDR_SAME (&l->link_id, &w->id)) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 336 | return l; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return NULL; |
| 340 | } |
| 341 | |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 342 | /* Consider supplied next-hop for inclusion to the supplied list |
| 343 | * of next-hops, adjust list as neccessary |
| 344 | */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 345 | void |
| 346 | ospf_spf_consider_nexthop (struct list *nexthops, |
| 347 | struct vertex_nexthop *newhop) |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 348 | { |
| 349 | struct listnode *nnode; |
| 350 | struct vertex_nexthop *hop; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 351 | |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 352 | LIST_LOOP (nexthops, hop, nnode) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 353 | { |
| 354 | assert (hop->oi); |
| 355 | /* weed out hops with higher cost than the newhop */ |
| 356 | if (hop->oi->output_cost > newhop->oi->output_cost) |
| 357 | { |
| 358 | /* delete the existing nexthop */ |
| 359 | listnode_delete (nexthops, hop); |
| 360 | vertex_nexthop_free (hop); |
| 361 | } |
| 362 | else if (hop->oi->output_cost < newhop->oi->output_cost) |
| 363 | { |
| 364 | return; |
| 365 | } |
| 366 | } |
| 367 | |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 368 | /* new hop is <= existing hops, add it */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 369 | listnode_add (nexthops, newhop); |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 370 | |
| 371 | return; |
| 372 | } |
| 373 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 374 | /* Calculate nexthop from root to vertex W. */ |
| 375 | void |
| 376 | ospf_nexthop_calculation (struct ospf_area *area, |
| 377 | struct vertex *v, struct vertex *w) |
| 378 | { |
| 379 | listnode node; |
| 380 | struct vertex_nexthop *nh, *x; |
| 381 | struct ospf_interface *oi = NULL; |
| 382 | struct router_lsa_link *l = NULL; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 383 | |
| 384 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 385 | if (IS_DEBUG_OSPF_EVENT) |
| 386 | zlog_info ("ospf_nexthop_calculation(): Start"); |
| 387 | |
| 388 | /* W's parent is root. */ |
| 389 | if (v == area->spf) |
| 390 | { |
| 391 | if (w->type == OSPF_VERTEX_ROUTER) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 392 | { |
| 393 | while ((l = ospf_get_next_link (v, w, l))) |
| 394 | { |
| 395 | struct router_lsa_link *l2 = NULL; |
| 396 | |
| 397 | if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT) |
| 398 | { |
| 399 | /* Check for PtMP, signified by PtP link V->W |
| 400 | with link_data our PtMP interface. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 401 | oi = ospf_if_is_configured (area->ospf, &l->link_data); |
paul | 7afa08d | 2002-12-13 20:59:45 +0000 | [diff] [blame] | 402 | if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 403 | { |
| 404 | struct prefix_ipv4 la; |
| 405 | la.prefixlen = oi->address->prefixlen; |
| 406 | /* We link to them on PtMP interface |
| 407 | - find the interface on w */ |
| 408 | while ((l2 = ospf_get_next_link (w, v, l2))) |
| 409 | { |
| 410 | la.prefix = l2->link_data; |
| 411 | |
| 412 | if (prefix_cmp ((struct prefix *) &la, |
| 413 | oi->address) == 0) |
| 414 | /* link_data is on our PtMP network */ |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | while ((l2 = ospf_get_next_link (w, v, l2))) |
| 421 | { |
| 422 | oi = ospf_if_is_configured (area->ospf, |
| 423 | &(l2->link_data)); |
| 424 | |
| 425 | if (oi == NULL) |
| 426 | continue; |
| 427 | |
| 428 | if (!IPV4_ADDR_SAME (&oi->address->u.prefix4, |
| 429 | &l->link_data)) |
| 430 | continue; |
| 431 | |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (oi && l2) |
| 437 | { |
| 438 | nh = vertex_nexthop_new (v); |
| 439 | nh->oi = oi; |
| 440 | nh->router = l2->link_data; |
| 441 | ospf_spf_consider_nexthop (w->nexthop, nh); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | else |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 447 | { |
| 448 | while ((l = ospf_get_next_link (v, w, l))) |
| 449 | { |
| 450 | oi = ospf_if_is_configured (area->ospf, &(l->link_data)); |
| 451 | if (oi) |
| 452 | { |
| 453 | nh = vertex_nexthop_new (v); |
| 454 | nh->oi = oi; |
| 455 | nh->router.s_addr = 0; |
| 456 | listnode_add (w->nexthop, nh); |
| 457 | } |
| 458 | } |
| 459 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | /* In case of W's parent is network connected to root. */ |
| 463 | else if (v->type == OSPF_VERTEX_NETWORK) |
| 464 | { |
| 465 | for (node = listhead (v->nexthop); node; nextnode (node)) |
| 466 | { |
| 467 | x = (struct vertex_nexthop *) getdata (node); |
| 468 | if (x->parent == area->spf) |
| 469 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 470 | while ((l = ospf_get_next_link (w, v, l))) |
| 471 | { |
| 472 | nh = vertex_nexthop_new (v); |
| 473 | nh->oi = x->oi; |
| 474 | nh->router = l->link_data; |
| 475 | listnode_add (w->nexthop, nh); |
| 476 | } |
| 477 | return; |
| 478 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | /* Inherit V's nexthop. */ |
| 483 | for (node = listhead (v->nexthop); node; nextnode (node)) |
| 484 | { |
| 485 | nh = vertex_nexthop_dup (node->data); |
| 486 | nh->parent = v; |
| 487 | ospf_nexthop_add_unique (nh, w->nexthop); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void |
| 492 | ospf_install_candidate (list candidate, struct vertex *w) |
| 493 | { |
| 494 | listnode node; |
| 495 | struct vertex *cw; |
| 496 | |
| 497 | if (list_isempty (candidate)) |
| 498 | { |
| 499 | listnode_add (candidate, w); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | /* Install vertex with sorting by distance. */ |
| 504 | for (node = listhead (candidate); node; nextnode (node)) |
| 505 | { |
| 506 | cw = (struct vertex *) getdata (node); |
| 507 | if (cw->distance > w->distance) |
| 508 | { |
| 509 | list_add_node_prev (candidate, node, w); |
| 510 | break; |
| 511 | } |
| 512 | else if (node->next == NULL) |
| 513 | { |
| 514 | list_add_node_next (candidate, node, w); |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* RFC2328 Section 16.1 (2). */ |
| 521 | void |
| 522 | ospf_spf_next (struct vertex *v, struct ospf_area *area, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 523 | list candidate, struct route_table *rv, struct route_table *nv) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | { |
| 525 | struct ospf_lsa *w_lsa = NULL; |
| 526 | struct vertex *w, *cw; |
| 527 | u_char *p; |
| 528 | u_char *lim; |
| 529 | struct router_lsa_link *l = NULL; |
| 530 | struct in_addr *r; |
| 531 | listnode node; |
| 532 | int type = 0; |
| 533 | |
| 534 | /* If this is a router-LSA, and bit V of the router-LSA (see Section |
| 535 | A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */ |
| 536 | if (v->type == OSPF_VERTEX_ROUTER) |
| 537 | { |
| 538 | if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa)) |
| 539 | area->transit = OSPF_TRANSIT_TRUE; |
| 540 | } |
| 541 | |
| 542 | p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 543 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 544 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | while (p < lim) |
| 546 | { |
| 547 | /* In case of V is Router-LSA. */ |
| 548 | if (v->lsa->type == OSPF_ROUTER_LSA) |
| 549 | { |
| 550 | l = (struct router_lsa_link *) p; |
| 551 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 552 | p += (ROUTER_LSA_MIN_SIZE + |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 554 | |
| 555 | /* (a) If this is a link to a stub network, examine the next |
| 556 | link in V's LSA. Links to stub networks will be |
| 557 | considered in the second stage of the shortest path |
| 558 | calculation. */ |
| 559 | if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB) |
| 560 | continue; |
| 561 | |
| 562 | /* (b) Otherwise, W is a transit vertex (router or transit |
| 563 | network). Look up the vertex W's LSA (router-LSA or |
| 564 | network-LSA) in Area A's link state database. */ |
| 565 | switch (type) |
| 566 | { |
| 567 | case LSA_LINK_TYPE_POINTOPOINT: |
| 568 | case LSA_LINK_TYPE_VIRTUALLINK: |
| 569 | if (type == LSA_LINK_TYPE_VIRTUALLINK) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 570 | { |
| 571 | if (IS_DEBUG_OSPF_EVENT) |
| 572 | zlog_info ("looking up LSA through VL: %s", |
| 573 | inet_ntoa (l->link_id)); |
| 574 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | |
| 576 | w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id, |
| 577 | l->link_id); |
| 578 | if (w_lsa) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 579 | { |
| 580 | if (IS_DEBUG_OSPF_EVENT) |
| 581 | zlog_info ("found the LSA"); |
| 582 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | break; |
| 584 | case LSA_LINK_TYPE_TRANSIT: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 585 | if (IS_DEBUG_OSPF_EVENT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 587 | zlog_info ("Looking up Network LSA, ID: %s", |
| 588 | inet_ntoa (l->link_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 589 | w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 590 | l->link_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 591 | if (w_lsa) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 592 | if (IS_DEBUG_OSPF_EVENT) |
| 593 | zlog_info ("found the LSA"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 594 | break; |
| 595 | default: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 596 | zlog_warn ("Invalid LSA link type %d", type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 597 | continue; |
| 598 | } |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | /* In case of V is Network-LSA. */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 603 | r = (struct in_addr *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | p += sizeof (struct in_addr); |
| 605 | |
| 606 | /* Lookup the vertex W's LSA. */ |
| 607 | w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r); |
| 608 | } |
| 609 | |
| 610 | /* (b cont.) If the LSA does not exist, or its LS age is equal |
| 611 | to MaxAge, or it does not have a link back to vertex V, |
| 612 | examine the next link in V's LSA.[23] */ |
| 613 | if (w_lsa == NULL) |
| 614 | continue; |
| 615 | |
| 616 | if (IS_LSA_MAXAGE (w_lsa)) |
| 617 | continue; |
| 618 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 619 | if (!ospf_lsa_has_link (w_lsa->data, v->lsa)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 620 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 621 | if (IS_DEBUG_OSPF_EVENT) |
| 622 | zlog_info ("The LSA doesn't have a link back"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 623 | continue; |
| 624 | } |
| 625 | |
| 626 | /* (c) If vertex W is already on the shortest-path tree, examine |
| 627 | the next link in the LSA. */ |
| 628 | if (ospf_spf_has_vertex (rv, nv, w_lsa->data)) |
| 629 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 630 | if (IS_DEBUG_OSPF_EVENT) |
| 631 | zlog_info ("The LSA is already in SPF"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | continue; |
| 633 | } |
| 634 | |
| 635 | /* (d) Calculate the link state cost D of the resulting path |
| 636 | from the root to vertex W. D is equal to the sum of the link |
| 637 | state cost of the (already calculated) shortest path to |
| 638 | vertex V and the advertised cost of the link between vertices |
| 639 | V and W. If D is: */ |
| 640 | |
| 641 | /* prepare vertex W. */ |
| 642 | w = ospf_vertex_new (w_lsa); |
| 643 | |
| 644 | /* calculate link cost D. */ |
| 645 | if (v->lsa->type == OSPF_ROUTER_LSA) |
| 646 | w->distance = v->distance + ntohs (l->m[0].metric); |
| 647 | else |
| 648 | w->distance = v->distance; |
| 649 | |
| 650 | /* Is there already vertex W in candidate list? */ |
| 651 | node = ospf_vertex_lookup (candidate, w->id, w->type); |
| 652 | if (node == NULL) |
| 653 | { |
| 654 | /* Calculate nexthop to W. */ |
| 655 | ospf_nexthop_calculation (area, v, w); |
| 656 | |
| 657 | ospf_install_candidate (candidate, w); |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | cw = (struct vertex *) getdata (node); |
| 662 | |
| 663 | /* if D is greater than. */ |
| 664 | if (cw->distance < w->distance) |
| 665 | { |
| 666 | ospf_vertex_free (w); |
| 667 | continue; |
| 668 | } |
| 669 | /* equal to. */ |
| 670 | else if (cw->distance == w->distance) |
| 671 | { |
| 672 | /* Calculate nexthop to W. */ |
| 673 | ospf_nexthop_calculation (area, v, w); |
| 674 | ospf_nexthop_merge (cw->nexthop, w->nexthop); |
| 675 | list_delete_all_node (w->nexthop); |
| 676 | ospf_vertex_free (w); |
| 677 | } |
| 678 | /* less than. */ |
| 679 | else |
| 680 | { |
| 681 | /* Calculate nexthop. */ |
| 682 | ospf_nexthop_calculation (area, v, w); |
| 683 | |
| 684 | /* Remove old vertex from candidate list. */ |
| 685 | ospf_vertex_free (cw); |
| 686 | listnode_delete (candidate, cw); |
| 687 | |
| 688 | /* Install new to candidate. */ |
| 689 | ospf_install_candidate (candidate, w); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* Add vertex V to SPF tree. */ |
| 696 | void |
| 697 | ospf_spf_register (struct vertex *v, struct route_table *rv, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 698 | struct route_table *nv) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | { |
| 700 | struct prefix p; |
| 701 | struct route_node *rn; |
| 702 | |
| 703 | p.family = AF_INET; |
| 704 | p.prefixlen = IPV4_MAX_BITLEN; |
| 705 | p.u.prefix4 = v->id; |
| 706 | |
| 707 | if (v->type == OSPF_VERTEX_ROUTER) |
| 708 | rn = route_node_get (rv, &p); |
| 709 | else |
| 710 | rn = route_node_get (nv, &p); |
| 711 | |
| 712 | rn->info = v; |
| 713 | } |
| 714 | |
| 715 | void |
| 716 | ospf_spf_route_free (struct route_table *table) |
| 717 | { |
| 718 | struct route_node *rn; |
| 719 | struct vertex *v; |
| 720 | |
| 721 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 722 | { |
| 723 | if ((v = rn->info)) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 724 | { |
| 725 | ospf_vertex_free (v); |
| 726 | rn->info = NULL; |
| 727 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 728 | |
| 729 | route_unlock_node (rn); |
| 730 | } |
| 731 | |
| 732 | route_table_finish (table); |
| 733 | } |
| 734 | |
| 735 | void |
| 736 | ospf_spf_dump (struct vertex *v, int i) |
| 737 | { |
| 738 | listnode cnode; |
| 739 | listnode nnode; |
| 740 | struct vertex_nexthop *nexthop; |
| 741 | |
| 742 | if (v->type == OSPF_VERTEX_ROUTER) |
| 743 | { |
| 744 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 745 | zlog_info ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | } |
| 747 | else |
| 748 | { |
| 749 | struct network_lsa *lsa = (struct network_lsa *) v->lsa; |
| 750 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 751 | zlog_info ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id), |
| 752 | ip_masklen (lsa->mask)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 753 | |
| 754 | for (nnode = listhead (v->nexthop); nnode; nextnode (nnode)) |
| 755 | { |
| 756 | nexthop = getdata (nnode); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 757 | if (IS_DEBUG_OSPF_EVENT) |
| 758 | zlog_info (" nexthop %s", inet_ntoa (nexthop->router)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | i++; |
| 763 | |
| 764 | for (cnode = listhead (v->child); cnode; nextnode (cnode)) |
| 765 | { |
| 766 | v = getdata (cnode); |
| 767 | ospf_spf_dump (v, i); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* Second stage of SPF calculation. */ |
| 772 | void |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 773 | ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 774 | struct route_table *rt) |
| 775 | { |
| 776 | listnode cnode; |
| 777 | struct vertex *child; |
| 778 | |
| 779 | if (IS_DEBUG_OSPF_EVENT) |
| 780 | zlog_info ("ospf_process_stub():processing stubs for area %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 781 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 782 | if (v->type == OSPF_VERTEX_ROUTER) |
| 783 | { |
| 784 | u_char *p; |
| 785 | u_char *lim; |
| 786 | struct router_lsa_link *l; |
| 787 | struct router_lsa *rlsa; |
| 788 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 789 | if (IS_DEBUG_OSPF_EVENT) |
| 790 | zlog_info ("ospf_process_stub():processing router LSA, id: %s", |
| 791 | inet_ntoa (v->lsa->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | rlsa = (struct router_lsa *) v->lsa; |
| 793 | |
| 794 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 795 | if (IS_DEBUG_OSPF_EVENT) |
| 796 | zlog_info ("ospf_process_stub(): we have %d links to process", |
| 797 | ntohs (rlsa->links)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | p = ((u_char *) v->lsa) + 24; |
| 799 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 800 | |
| 801 | while (p < lim) |
| 802 | { |
| 803 | l = (struct router_lsa_link *) p; |
| 804 | |
| 805 | p += (ROUTER_LSA_MIN_SIZE + |
| 806 | (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 807 | |
| 808 | if (l->m[0].type == LSA_LINK_TYPE_STUB) |
| 809 | ospf_intra_add_stub (rt, l, v, area); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 814 | zlog_info ("children of V:"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | for (cnode = listhead (v->child); cnode; nextnode (cnode)) |
| 816 | { |
| 817 | child = getdata (cnode); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 818 | if (IS_DEBUG_OSPF_EVENT) |
| 819 | zlog_info (" child : %s", inet_ntoa (child->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | for (cnode = listhead (v->child); cnode; nextnode (cnode)) |
| 823 | { |
| 824 | child = getdata (cnode); |
| 825 | |
| 826 | if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED)) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 827 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | |
| 829 | ospf_spf_process_stubs (area, child, rt); |
| 830 | |
| 831 | SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | void |
| 836 | ospf_rtrs_free (struct route_table *rtrs) |
| 837 | { |
| 838 | struct route_node *rn; |
| 839 | list or_list; |
| 840 | listnode node; |
| 841 | |
| 842 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 843 | zlog_info ("Route: Router Routing Table free"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | |
| 845 | for (rn = route_top (rtrs); rn; rn = route_next (rn)) |
| 846 | if ((or_list = rn->info) != NULL) |
| 847 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 848 | for (node = listhead (or_list); node; nextnode (node)) |
| 849 | ospf_route_free (node->data); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 850 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 851 | list_delete (or_list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 852 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 853 | /* Unlock the node. */ |
| 854 | rn->info = NULL; |
| 855 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 856 | } |
| 857 | route_table_finish (rtrs); |
| 858 | } |
| 859 | |
| 860 | void |
| 861 | ospf_rtrs_print (struct route_table *rtrs) |
| 862 | { |
| 863 | struct route_node *rn; |
| 864 | list or_list; |
| 865 | listnode ln; |
| 866 | listnode pnode; |
| 867 | struct ospf_route *or; |
| 868 | struct ospf_path *path; |
| 869 | char buf1[BUFSIZ]; |
| 870 | char buf2[BUFSIZ]; |
| 871 | |
| 872 | if (IS_DEBUG_OSPF_EVENT) |
| 873 | zlog_info ("ospf_rtrs_print() start"); |
| 874 | |
| 875 | for (rn = route_top (rtrs); rn; rn = route_next (rn)) |
| 876 | if ((or_list = rn->info) != NULL) |
| 877 | for (ln = listhead (or_list); ln; nextnode (ln)) |
| 878 | { |
| 879 | or = getdata (ln); |
| 880 | |
| 881 | switch (or->path_type) |
| 882 | { |
| 883 | case OSPF_PATH_INTRA_AREA: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 884 | if (IS_DEBUG_OSPF_EVENT) |
| 885 | zlog_info ("%s [%d] area: %s", |
| 886 | inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), |
| 887 | or->cost, inet_ntop (AF_INET, &or->u.std.area_id, |
| 888 | buf2, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 889 | break; |
| 890 | case OSPF_PATH_INTER_AREA: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 891 | if (IS_DEBUG_OSPF_EVENT) |
| 892 | zlog_info ("%s IA [%d] area: %s", |
| 893 | inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), |
| 894 | or->cost, inet_ntop (AF_INET, &or->u.std.area_id, |
| 895 | buf2, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 896 | break; |
| 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | |
| 901 | for (pnode = listhead (or->path); pnode; nextnode (pnode)) |
| 902 | { |
| 903 | path = getdata (pnode); |
| 904 | if (path->nexthop.s_addr == 0) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 905 | { |
| 906 | if (IS_DEBUG_OSPF_EVENT) |
| 907 | zlog_info (" directly attached to %s\r\n", |
| 908 | IF_NAME (path->oi)); |
| 909 | } |
| 910 | else |
| 911 | { |
| 912 | if (IS_DEBUG_OSPF_EVENT) |
| 913 | zlog_info (" via %s, %s\r\n", |
| 914 | inet_ntoa (path->nexthop), IF_NAME (path->oi)); |
| 915 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | zlog_info ("ospf_rtrs_print() end"); |
| 920 | } |
| 921 | |
| 922 | /* Calculating the shortest-path tree for an area. */ |
| 923 | void |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 924 | ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 925 | struct route_table *new_rtrs) |
| 926 | { |
| 927 | list candidate; |
| 928 | listnode node; |
| 929 | struct vertex *v; |
| 930 | struct route_table *rv; |
| 931 | struct route_table *nv; |
| 932 | |
| 933 | if (IS_DEBUG_OSPF_EVENT) |
| 934 | { |
| 935 | zlog_info ("ospf_spf_calculate: Start"); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 936 | zlog_info ("ospf_spf_calculate: running Dijkstra for area %s", |
| 937 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* Check router-lsa-self. If self-router-lsa is not yet allocated, |
| 941 | return this area's calculation. */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 942 | if (!area->router_lsa_self) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | { |
| 944 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 945 | zlog_info ("ospf_spf_calculate: " |
| 946 | "Skip area %s's calculation due to empty router_lsa_self", |
| 947 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | |
| 951 | /* RFC2328 16.1. (1). */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 952 | /* Initialize the algorithm's data structures. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 953 | rv = route_table_init (); |
| 954 | nv = route_table_init (); |
| 955 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 956 | /* Clear the list of candidate vertices. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | candidate = list_new (); |
| 958 | |
| 959 | /* Initialize the shortest-path tree to only the root (which is the |
| 960 | router doing the calculation). */ |
| 961 | ospf_spf_init (area); |
| 962 | v = area->spf; |
| 963 | ospf_spf_register (v, rv, nv); |
| 964 | |
| 965 | /* Set Area A's TransitCapability to FALSE. */ |
| 966 | area->transit = OSPF_TRANSIT_FALSE; |
| 967 | area->shortcut_capability = 1; |
| 968 | |
| 969 | for (;;) |
| 970 | { |
| 971 | /* RFC2328 16.1. (2). */ |
| 972 | ospf_spf_next (v, area, candidate, rv, nv); |
| 973 | |
| 974 | /* RFC2328 16.1. (3). */ |
| 975 | /* If at this step the candidate list is empty, the shortest- |
| 976 | path tree (of transit vertices) has been completely built and |
| 977 | this stage of the procedure terminates. */ |
| 978 | if (listcount (candidate) == 0) |
| 979 | break; |
| 980 | |
| 981 | /* Otherwise, choose the vertex belonging to the candidate list |
| 982 | that is closest to the root, and add it to the shortest-path |
| 983 | tree (removing it from the candidate list in the |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 984 | process). */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 985 | node = listhead (candidate); |
| 986 | v = getdata (node); |
| 987 | ospf_vertex_add_parent (v); |
| 988 | |
| 989 | /* Reveve from the candidate list. */ |
| 990 | listnode_delete (candidate, v); |
| 991 | |
| 992 | /* Add to SPF tree. */ |
| 993 | ospf_spf_register (v, rv, nv); |
| 994 | |
| 995 | /* Note that when there is a choice of vertices closest to the |
| 996 | root, network vertices must be chosen before router vertices |
| 997 | in order to necessarily find all equal-cost paths. */ |
| 998 | /* We don't do this at this moment, we should add the treatment |
| 999 | above codes. -- kunihiro. */ |
| 1000 | |
| 1001 | /* RFC2328 16.1. (4). */ |
| 1002 | if (v->type == OSPF_VERTEX_ROUTER) |
| 1003 | ospf_intra_add_router (new_rtrs, v, area); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1004 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1005 | ospf_intra_add_transit (new_table, v, area); |
| 1006 | |
| 1007 | /* RFC2328 16.1. (5). */ |
| 1008 | /* Iterate the algorithm by returning to Step 2. */ |
| 1009 | } |
| 1010 | |
| 1011 | if (IS_DEBUG_OSPF_EVENT) |
| 1012 | { |
| 1013 | ospf_spf_dump (area->spf, 0); |
| 1014 | ospf_route_table_dump (new_table); |
| 1015 | } |
| 1016 | |
| 1017 | /* Second stage of SPF calculation procedure's */ |
| 1018 | ospf_spf_process_stubs (area, area->spf, new_table); |
| 1019 | |
| 1020 | /* Free all vertices which allocated for SPF calculation */ |
| 1021 | ospf_spf_route_free (rv); |
| 1022 | ospf_spf_route_free (nv); |
| 1023 | |
| 1024 | /* Free candidate list */ |
| 1025 | list_free (candidate); |
| 1026 | |
| 1027 | /* Increment SPF Calculation Counter. */ |
| 1028 | area->spf_calculation++; |
| 1029 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1030 | area->ospf->ts_spf = time (NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (IS_DEBUG_OSPF_EVENT) |
| 1033 | zlog_info ("ospf_spf_calculate: Stop"); |
| 1034 | } |
| 1035 | |
| 1036 | /* Timer for SPF calculation. */ |
| 1037 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1038 | ospf_spf_calculate_timer (struct thread *thread) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1039 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1040 | struct ospf *ospf = THREAD_ARG (thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | struct route_table *new_table, *new_rtrs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1042 | listnode node; |
| 1043 | |
| 1044 | if (IS_DEBUG_OSPF_EVENT) |
| 1045 | zlog_info ("SPF: Timer (SPF calculation expire)"); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1046 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1047 | ospf->t_spf_calc = NULL; |
| 1048 | |
| 1049 | /* Allocate new table tree. */ |
| 1050 | new_table = route_table_init (); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1051 | new_rtrs = route_table_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1052 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1053 | ospf_vl_unapprove (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1054 | |
| 1055 | /* Calculate SPF for each area. */ |
| 1056 | for (node = listhead (ospf->areas); node; node = nextnode (node)) |
| 1057 | ospf_spf_calculate (node->data, new_table, new_rtrs); |
| 1058 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1059 | ospf_vl_shut_unapproved (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1060 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1061 | ospf_ia_routing (ospf, new_table, new_rtrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | |
| 1063 | ospf_prune_unreachable_networks (new_table); |
| 1064 | ospf_prune_unreachable_routers (new_rtrs); |
| 1065 | |
| 1066 | /* AS-external-LSA calculation should not be performed here. */ |
| 1067 | |
| 1068 | /* If new Router Route is installed, |
| 1069 | then schedule re-calculate External routes. */ |
| 1070 | if (1) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1071 | ospf_ase_calculate_schedule (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1072 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1073 | ospf_ase_calculate_timer_add (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* Update routing table. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1076 | ospf_route_install (ospf, new_table); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* Update ABR/ASBR routing table */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1079 | if (ospf->old_rtrs) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1080 | { |
| 1081 | /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1082 | /* ospf_route_delete (ospf->old_rtrs); */ |
| 1083 | ospf_rtrs_free (ospf->old_rtrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1086 | ospf->old_rtrs = ospf->new_rtrs; |
| 1087 | ospf->new_rtrs = new_rtrs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1088 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1089 | if (IS_OSPF_ABR (ospf)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1090 | ospf_abr_task (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (IS_DEBUG_OSPF_EVENT) |
| 1093 | zlog_info ("SPF: calculation complete"); |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | /* Add schedule for SPF calculation. To avoid frequenst SPF calc, we |
| 1099 | set timer for SPF calc. */ |
| 1100 | void |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1101 | ospf_spf_calculate_schedule (struct ospf *ospf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1102 | { |
| 1103 | time_t ht, delay; |
| 1104 | |
| 1105 | if (IS_DEBUG_OSPF_EVENT) |
| 1106 | zlog_info ("SPF: calculation timer scheduled"); |
| 1107 | |
| 1108 | /* OSPF instance does not exist. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1109 | if (ospf == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1110 | return; |
| 1111 | |
| 1112 | /* SPF calculation timer is already scheduled. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1113 | if (ospf->t_spf_calc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | { |
| 1115 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1116 | zlog_info ("SPF: calculation timer is already scheduled: %p", |
| 1117 | ospf->t_spf_calc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | return; |
| 1119 | } |
| 1120 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1121 | ht = time (NULL) - ospf->ts_spf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Get SPF calculation delay time. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1124 | if (ht < ospf->spf_holdtime) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1125 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1126 | if (ospf->spf_holdtime - ht < ospf->spf_delay) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1127 | delay = ospf->spf_delay; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1128 | else |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame^] | 1129 | delay = ospf->spf_holdtime - ht; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | } |
| 1131 | else |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1132 | delay = ospf->spf_delay; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (IS_DEBUG_OSPF_EVENT) |
| 1135 | zlog_info ("SPF: calculation timer delay = %ld", delay); |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1136 | ospf->t_spf_calc = |
| 1137 | thread_add_timer (master, ospf_spf_calculate_timer, ospf, delay); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1138 | } |