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