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 () */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 32 | #include "pqueue.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include "ospfd/ospfd.h" |
| 35 | #include "ospfd/ospf_interface.h" |
| 36 | #include "ospfd/ospf_ism.h" |
| 37 | #include "ospfd/ospf_asbr.h" |
| 38 | #include "ospfd/ospf_lsa.h" |
| 39 | #include "ospfd/ospf_lsdb.h" |
| 40 | #include "ospfd/ospf_neighbor.h" |
| 41 | #include "ospfd/ospf_nsm.h" |
| 42 | #include "ospfd/ospf_spf.h" |
| 43 | #include "ospfd/ospf_route.h" |
| 44 | #include "ospfd/ospf_ia.h" |
| 45 | #include "ospfd/ospf_ase.h" |
| 46 | #include "ospfd/ospf_abr.h" |
| 47 | #include "ospfd/ospf_dump.h" |
| 48 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 49 | /* Heap related functions, for the managment of the candidates, to |
| 50 | * be used with pqueue. */ |
| 51 | static int |
| 52 | cmp (void * node1 , void * node2) |
| 53 | { |
| 54 | struct vertex * v1 = (struct vertex *) node1; |
| 55 | struct vertex * v2 = (struct vertex *) node2; |
| 56 | if (v1 != NULL && v2 != NULL ) |
| 57 | return (v1->distance - v2->distance); |
| 58 | else |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 63 | update_stat (void *node , int position) |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 64 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 65 | struct vertex *v = node; |
| 66 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 67 | /* Set the status of the vertex, when its position changes. */ |
| 68 | *(v->stat) = position; |
| 69 | } |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 70 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 71 | static struct vertex_nexthop * |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 72 | vertex_nexthop_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 74 | return XCALLOC (MTYPE_OSPF_NEXTHOP, sizeof (struct vertex_nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | } |
| 76 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 77 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | vertex_nexthop_free (struct vertex_nexthop *nh) |
| 79 | { |
| 80 | XFREE (MTYPE_OSPF_NEXTHOP, nh); |
| 81 | } |
| 82 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 83 | /* Free the canonical nexthop objects for an area, ie the nexthop objects |
| 84 | * attached to the first-hop router vertices. |
| 85 | */ |
| 86 | static void |
| 87 | ospf_canonical_nexthops_free (struct vertex *root) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 89 | struct listnode *node, *nnode; |
| 90 | struct vertex *child; |
| 91 | |
| 92 | for (ALL_LIST_ELEMENTS (root->children, node, nnode, child)) |
| 93 | { |
| 94 | struct listnode *n2, *nn2; |
| 95 | struct vertex_parent *vp; |
| 96 | |
paul | 58e1bef | 2005-11-11 12:10:03 +0000 | [diff] [blame] | 97 | /* router vertices through an attached network each |
| 98 | * have a distinct (canonical / not inherited) nexthop |
| 99 | * which must be freed. |
| 100 | * |
| 101 | * A network vertex can only have router vertices as its |
| 102 | * children, so only one level of recursion is possible. |
| 103 | */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 104 | if (child->type == OSPF_VERTEX_NETWORK) |
| 105 | ospf_canonical_nexthops_free (child); |
| 106 | |
paul | 58e1bef | 2005-11-11 12:10:03 +0000 | [diff] [blame] | 107 | /* Free child nexthops pointing back to this root vertex */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 108 | for (ALL_LIST_ELEMENTS (child->parents, n2, nn2, vp)) |
paul | 58e1bef | 2005-11-11 12:10:03 +0000 | [diff] [blame] | 109 | if (vp->parent == root) |
| 110 | vertex_nexthop_free (vp->nexthop); |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | static struct vertex_parent * |
| 115 | vertex_parent_new (struct vertex *v, int backlink, struct vertex_nexthop *hop) |
| 116 | { |
| 117 | struct vertex_parent *new; |
| 118 | |
| 119 | new = XMALLOC (MTYPE_OSPF_VERTEX_PARENT, sizeof (struct vertex_parent)); |
| 120 | |
| 121 | if (new == NULL) |
| 122 | return NULL; |
| 123 | |
| 124 | new->parent = v; |
| 125 | new->backlink = backlink; |
| 126 | new->nexthop = hop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | return new; |
| 128 | } |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 129 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 130 | static void |
| 131 | vertex_parent_free (struct vertex_parent *p) |
| 132 | { |
| 133 | XFREE (MTYPE_OSPF_VERTEX_PARENT, p); |
| 134 | } |
| 135 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 136 | static struct vertex * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | ospf_vertex_new (struct ospf_lsa *lsa) |
| 138 | { |
| 139 | struct vertex *new; |
| 140 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 141 | new = XCALLOC (MTYPE_OSPF_VERTEX, sizeof (struct vertex)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 142 | |
| 143 | new->flags = 0; |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 144 | new->stat = &(lsa->stat); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | new->type = lsa->data->type; |
| 146 | new->id = lsa->data->id; |
| 147 | new->lsa = lsa->data; |
| 148 | new->distance = 0; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 149 | new->children = list_new (); |
| 150 | new->parents = list_new (); |
| 151 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | return new; |
| 153 | } |
| 154 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 155 | /* Recursively free the given vertex and all its children |
| 156 | * and associated parent and nexthop information |
| 157 | * Parent should indicate which parent is trying to free this child, |
| 158 | * NULL parent is only valid for the root vertex. |
| 159 | */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 160 | static void |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 161 | ospf_vertex_free (struct vertex *v, const struct ospf_area *area) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 163 | struct listnode *node, *nnode; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 164 | struct vertex *vc; |
paul | 7461d45 | 2005-06-13 13:57:16 +0000 | [diff] [blame] | 165 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 166 | assert (*(v->stat) == LSA_SPF_IN_SPFTREE); |
| 167 | |
| 168 | /* recurse down the tree, freeing furthest children first */ |
| 169 | if (v->children) |
| 170 | { |
| 171 | for (ALL_LIST_ELEMENTS (v->children, node, nnode, vc)) |
| 172 | ospf_vertex_free (vc, area); |
| 173 | |
| 174 | list_delete (v->children); |
| 175 | v->children = NULL; |
| 176 | } |
| 177 | |
| 178 | /* The vertex itself can only be freed when the last remaining parent |
| 179 | * with a reference to this vertex frees this vertex. Until then, we |
| 180 | * just cleanup /one/ parent association (doesnt matter which) - |
| 181 | * essentially using the parents list as a refcount. |
| 182 | */ |
| 183 | if (listcount (v->parents) > 0) |
| 184 | { |
| 185 | vertex_parent_free (listgetdata (listhead (v->parents))); |
| 186 | list_delete_node (v->parents, listhead (v->parents)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 188 | if (listcount (v->parents) > 0) |
| 189 | return; /* there are still parent references left */ |
| 190 | } |
| 191 | |
| 192 | list_delete (v->parents); |
| 193 | v->parents = NULL; |
| 194 | |
| 195 | v->lsa = NULL; |
paul | 7461d45 | 2005-06-13 13:57:16 +0000 | [diff] [blame] | 196 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | XFREE (MTYPE_OSPF_VERTEX, v); |
| 198 | } |
| 199 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 200 | static void |
hasso | eb1ce60 | 2004-10-08 08:17:22 +0000 | [diff] [blame] | 201 | ospf_vertex_dump(const char *msg, struct vertex *v, |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 202 | int print_parents, int print_children) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 203 | { |
| 204 | if ( ! IS_DEBUG_OSPF_EVENT) |
| 205 | return; |
| 206 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 207 | zlog_debug("%s %s vertex %s distance %u flags %u", |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 208 | msg, |
| 209 | v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network", |
| 210 | inet_ntoa(v->lsa->id), |
| 211 | v->distance, |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 212 | (unsigned int)v->flags); |
| 213 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 214 | if (print_parents) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 215 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 216 | struct listnode *node; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 217 | struct vertex_parent *vp; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 218 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 219 | for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp)) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 220 | { |
| 221 | char buf1[BUFSIZ]; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 222 | |
| 223 | if (vp) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 224 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 225 | zlog_debug ("parent %s backlink %d nexthop %s interface %s", |
| 226 | inet_ntoa(vp->parent->lsa->id), vp->backlink, |
| 227 | inet_ntop(AF_INET, &vp->nexthop->router, buf1, BUFSIZ), |
| 228 | vp->nexthop->oi ? IF_NAME(vp->nexthop->oi) : "NULL"); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (print_children) |
| 234 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 235 | struct listnode *cnode; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 236 | struct vertex *cv; |
| 237 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 238 | for (ALL_LIST_ELEMENTS_RO (v->children, cnode, cv)) |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 239 | ospf_vertex_dump(" child:", cv, 0, 0); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* Add a vertex to the list of children in each of its parents. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 245 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | ospf_vertex_add_parent (struct vertex *v) |
| 247 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 248 | struct vertex_parent *vp; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 249 | struct listnode *node; |
paul | 7461d45 | 2005-06-13 13:57:16 +0000 | [diff] [blame] | 250 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 251 | assert (v && v->children); |
paul | 7461d45 | 2005-06-13 13:57:16 +0000 | [diff] [blame] | 252 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 253 | for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 255 | assert (vp->parent && vp->parent->children); |
paul | 7461d45 | 2005-06-13 13:57:16 +0000 | [diff] [blame] | 256 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 257 | /* No need to add two links from the same parent. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 258 | if (listnode_lookup (vp->parent->children, v) == NULL) |
| 259 | listnode_add (vp->parent->children, v); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 263 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 264 | ospf_spf_init (struct ospf_area *area) |
| 265 | { |
| 266 | struct vertex *v; |
| 267 | |
| 268 | /* Create root node. */ |
| 269 | v = ospf_vertex_new (area->router_lsa_self); |
| 270 | |
| 271 | area->spf = v; |
| 272 | |
| 273 | /* Reset ABR and ASBR router counts. */ |
| 274 | area->abr_count = 0; |
| 275 | area->asbr_count = 0; |
| 276 | } |
| 277 | |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 278 | /* return index of link back to V from W, or -1 if no link found */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 279 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | ospf_lsa_has_link (struct lsa_header *w, struct lsa_header *v) |
| 281 | { |
hasso | eb1ce60 | 2004-10-08 08:17:22 +0000 | [diff] [blame] | 282 | unsigned int i, length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | struct router_lsa *rl; |
| 284 | struct network_lsa *nl; |
| 285 | |
| 286 | /* In case of W is Network LSA. */ |
| 287 | if (w->type == OSPF_NETWORK_LSA) |
| 288 | { |
| 289 | if (v->type == OSPF_NETWORK_LSA) |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 290 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | |
| 292 | nl = (struct network_lsa *) w; |
| 293 | length = (ntohs (w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 294 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | for (i = 0; i < length; i++) |
| 296 | if (IPV4_ADDR_SAME (&nl->routers[i], &v->id)) |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 297 | return i; |
| 298 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* In case of W is Router LSA. */ |
| 302 | if (w->type == OSPF_ROUTER_LSA) |
| 303 | { |
| 304 | rl = (struct router_lsa *) w; |
| 305 | |
| 306 | length = ntohs (w->length); |
| 307 | |
| 308 | for (i = 0; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 309 | i < ntohs (rl->links) && length >= sizeof (struct router_lsa); |
| 310 | i++, length -= 12) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | { |
| 312 | switch (rl->link[i].type) |
| 313 | { |
| 314 | case LSA_LINK_TYPE_POINTOPOINT: |
| 315 | case LSA_LINK_TYPE_VIRTUALLINK: |
| 316 | /* Router LSA ID. */ |
| 317 | if (v->type == OSPF_ROUTER_LSA && |
| 318 | IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id)) |
| 319 | { |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 320 | return i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | } |
| 322 | break; |
| 323 | case LSA_LINK_TYPE_TRANSIT: |
| 324 | /* Network LSA ID. */ |
| 325 | if (v->type == OSPF_NETWORK_LSA && |
| 326 | IPV4_ADDR_SAME (&rl->link[i].link_id, &v->id)) |
| 327 | { |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 328 | return i; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 329 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | break; |
| 331 | case LSA_LINK_TYPE_STUB: |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 332 | /* Stub can't lead anywhere, carry on */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | continue; |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 339 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | #define ROUTER_LSA_MIN_SIZE 12 |
| 343 | #define ROUTER_LSA_TOS_SIZE 4 |
| 344 | |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 345 | /* Find the next link after prev_link from v to w. If prev_link is |
| 346 | * NULL, return the first link from v to w. Ignore stub and virtual links; |
| 347 | * these link types will never be returned. |
| 348 | */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 349 | static struct router_lsa_link * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 350 | ospf_get_next_link (struct vertex *v, struct vertex *w, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 351 | struct router_lsa_link *prev_link) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 352 | { |
| 353 | u_char *p; |
| 354 | u_char *lim; |
| 355 | struct router_lsa_link *l; |
| 356 | |
| 357 | if (prev_link == NULL) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 358 | p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | else |
| 360 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 361 | p = (u_char *) prev_link; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 362 | p += (ROUTER_LSA_MIN_SIZE + |
| 363 | (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 364 | } |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 365 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 366 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 367 | |
| 368 | while (p < lim) |
| 369 | { |
| 370 | l = (struct router_lsa_link *) p; |
| 371 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 372 | 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] | 373 | |
| 374 | if (l->m[0].type == LSA_LINK_TYPE_STUB) |
| 375 | continue; |
| 376 | |
| 377 | /* Defer NH calculation via VLs until summaries from |
| 378 | transit areas area confidered */ |
| 379 | |
| 380 | if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 381 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | |
| 383 | if (IPV4_ADDR_SAME (&l->link_id, &w->id)) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 384 | return l; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
paul | 75ee0b8 | 2004-08-05 09:10:31 +0000 | [diff] [blame] | 390 | /* |
| 391 | * Consider supplied next-hop for inclusion to the supplied list of |
| 392 | * equal-cost next-hops, adjust list as neccessary. |
| 393 | * |
| 394 | * (Discussed on GNU Zebra list 27 May 2003, [zebra 19184]) |
| 395 | * |
| 396 | * Note that below is a bit of a hack, and limits ECMP to paths that go to |
| 397 | * same nexthop. Where as paths via inequal output_cost interfaces could |
| 398 | * still quite easily be ECMP due to remote cost differences. |
| 399 | * |
| 400 | * TODO: It really should be done by way of recording currently valid |
| 401 | * backlinks and determining the appropriate nexthops from the list of |
| 402 | * backlinks, or even simpler, just flushing nexthop list if we find a lower |
| 403 | * cost path to a candidate vertex in SPF, maybe. |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 404 | */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 405 | static void |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 406 | ospf_spf_add_parent (struct vertex *v, struct vertex *w, |
| 407 | struct vertex_nexthop *newhop) |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 408 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 409 | struct vertex_parent *vp; |
| 410 | |
| 411 | /* we must have a newhop.. */ |
| 412 | assert (v && w && newhop); |
| 413 | |
| 414 | /* new parent is <= existing parents, add it */ |
| 415 | vp = vertex_parent_new (v, ospf_lsa_has_link (w->lsa, v->lsa), newhop); |
| 416 | listnode_add (w->parents, vp); |
paul | bf9392c | 2003-06-06 23:23:36 +0000 | [diff] [blame] | 417 | |
| 418 | return; |
| 419 | } |
| 420 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 421 | static void |
| 422 | ospf_spf_flush_parents (struct vertex *w) |
| 423 | { |
| 424 | struct vertex_parent *vp; |
| 425 | struct listnode *ln, *nn; |
| 426 | |
| 427 | /* delete the existing nexthops */ |
| 428 | for (ALL_LIST_ELEMENTS (w->parents, ln, nn, vp)) |
| 429 | { |
| 430 | list_delete_node (w->parents, ln); |
| 431 | vertex_parent_free (vp); |
| 432 | } |
| 433 | } |
| 434 | |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 435 | /* 16.1.1. Calculate nexthop from root through V (parent) to |
| 436 | * vertex W (destination). |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 437 | * |
| 438 | * The link must be supplied if V is the root vertex. In all other cases |
| 439 | * it may be NULL. |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 440 | */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 441 | static void |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 442 | ospf_nexthop_calculation (struct ospf_area *area, struct vertex *v, |
| 443 | struct vertex *w, struct router_lsa_link *l) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 445 | struct listnode *node, *nnode; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 446 | struct vertex_nexthop *nh; |
| 447 | struct vertex_parent *vp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 448 | struct ospf_interface *oi = NULL; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 449 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | if (IS_DEBUG_OSPF_EVENT) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 451 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 452 | zlog_debug ("ospf_nexthop_calculation(): Start"); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 453 | ospf_vertex_dump("V (parent):", v, 1, 1); |
| 454 | ospf_vertex_dump("W (dest) :", w, 1, 1); |
| 455 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 456 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | if (v == area->spf) |
| 458 | { |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 459 | /* 16.1.1 para 4. In the first case, the parent vertex (V) is the |
| 460 | root (the calculating router itself). This means that the |
| 461 | destination is either a directly connected network or directly |
| 462 | connected router. The outgoing interface in this case is simply |
| 463 | the OSPF interface connecting to the destination network/router. |
| 464 | */ |
| 465 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | if (w->type == OSPF_VERTEX_ROUTER) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 467 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 468 | /* l is a link from v to w |
| 469 | * l2 will be link from w to v |
| 470 | */ |
| 471 | struct router_lsa_link *l2 = NULL; |
| 472 | |
| 473 | /* we *must* be supplied with the link data */ |
| 474 | assert (l != NULL); |
| 475 | |
| 476 | if (IS_DEBUG_OSPF_EVENT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 477 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 478 | char buf1[BUFSIZ]; |
| 479 | char buf2[BUFSIZ]; |
| 480 | |
| 481 | zlog_debug("ospf_nexthop_calculation(): considering link " |
| 482 | "type %d link_id %s link_data %s", |
| 483 | l->m[0].type, |
| 484 | inet_ntop (AF_INET, &l->link_id, buf1, BUFSIZ), |
| 485 | inet_ntop (AF_INET, &l->link_data, buf2, BUFSIZ)); |
| 486 | } |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 487 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 488 | if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT) |
| 489 | { |
| 490 | /* If the destination is a router which connects to |
| 491 | the calculating router via a Point-to-MultiPoint |
| 492 | network, the destination's next hop IP address(es) |
| 493 | can be determined by examining the destination's |
| 494 | router-LSA: each link pointing back to the |
| 495 | calculating router and having a Link Data field |
| 496 | belonging to the Point-to-MultiPoint network |
| 497 | provides an IP address of the next hop router. |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 498 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 499 | At this point l is a link from V to W, and V is the |
| 500 | root ("us"). Find the local interface associated |
| 501 | with l (its address is in l->link_data). If it |
| 502 | is a point-to-multipoint interface, then look through |
| 503 | the links in the opposite direction (W to V). If |
| 504 | any of them have an address that lands within the |
| 505 | subnet declared by the PtMP link, then that link |
| 506 | is a constituent of the PtMP link, and its address is |
| 507 | a nexthop address for V. |
| 508 | */ |
| 509 | oi = ospf_if_is_configured (area->ospf, &l->link_data); |
| 510 | if (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 511 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 512 | struct prefix_ipv4 la; |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 513 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 514 | la.family = AF_INET; |
| 515 | la.prefixlen = oi->address->prefixlen; |
| 516 | |
| 517 | /* V links to W on PtMP interface |
| 518 | - find the interface address on W */ |
| 519 | while ((l2 = ospf_get_next_link (w, v, l2))) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 520 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 521 | la.prefix = l2->link_data; |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 522 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 523 | if (prefix_cmp ((struct prefix *) &la, |
| 524 | oi->address) == 0) |
| 525 | /* link_data is on our PtMP network */ |
| 526 | break; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 527 | } |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 528 | } /* end l is on point-to-multipoint link */ |
| 529 | else |
| 530 | { |
| 531 | /* l is a regular point-to-point link. |
| 532 | Look for a link from W to V. |
| 533 | */ |
| 534 | while ((l2 = ospf_get_next_link (w, v, l2))) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 535 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 536 | oi = ospf_if_is_configured (area->ospf, |
| 537 | &(l2->link_data)); |
| 538 | |
| 539 | if (oi == NULL) |
| 540 | continue; |
| 541 | |
| 542 | if (!IPV4_ADDR_SAME (&oi->address->u.prefix4, |
| 543 | &l->link_data)) |
| 544 | continue; |
| 545 | |
| 546 | break; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 547 | } |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (oi && l2) |
| 551 | { |
| 552 | /* found all necessary info to build nexthop */ |
| 553 | nh = vertex_nexthop_new (); |
| 554 | nh->oi = oi; |
| 555 | nh->router = l2->link_data; |
| 556 | ospf_spf_add_parent (v, w, nh); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | zlog_info("ospf_nexthop_calculation(): " |
| 561 | "could not determine nexthop for link"); |
| 562 | } |
| 563 | } /* end point-to-point link from V to W */ |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 564 | } /* end W is a Router vertex */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 565 | else |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 566 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 567 | assert(w->type == OSPF_VERTEX_NETWORK); |
| 568 | oi = ospf_if_is_configured (area->ospf, &(l->link_data)); |
| 569 | if (oi) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 570 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 571 | nh = vertex_nexthop_new (); |
| 572 | nh->oi = oi; |
| 573 | nh->router.s_addr = 0; |
| 574 | ospf_spf_add_parent (v, w, nh); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | return; |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 578 | } /* end V is the root */ |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 579 | /* Check if W's parent is a network connected to root. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | else if (v->type == OSPF_VERTEX_NETWORK) |
| 581 | { |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 582 | /* See if any of V's parents are the root. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 583 | for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 585 | if (vp->parent == area->spf) /* connects to root? */ |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 586 | { |
| 587 | /* 16.1.1 para 5. ...the parent vertex is a network that |
| 588 | * directly connects the calculating router to the destination |
| 589 | * router. The list of next hops is then determined by |
| 590 | * examining the destination's router-LSA... |
| 591 | */ |
| 592 | |
| 593 | assert(w->type == OSPF_VERTEX_ROUTER); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 594 | while ((l = ospf_get_next_link (w, v, l))) |
| 595 | { |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 596 | /* ...For each link in the router-LSA that points back to the |
| 597 | * parent network, the link's Link Data field provides the IP |
| 598 | * address of a next hop router. The outgoing interface to |
| 599 | * use can then be derived from the next hop IP address (or |
| 600 | * it can be inherited from the parent network). |
| 601 | */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 602 | nh = vertex_nexthop_new (); |
| 603 | nh->oi = vp->nexthop->oi; |
| 604 | nh->router = l->link_data; |
| 605 | ospf_spf_add_parent (v, w, nh); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 606 | } |
| 607 | return; |
| 608 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 612 | /* 16.1.1 para 4. If there is at least one intervening router in the |
| 613 | * current shortest path between the destination and the root, the |
| 614 | * destination simply inherits the set of next hops from the |
| 615 | * parent. |
| 616 | */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 617 | for (ALL_LIST_ELEMENTS (v->parents, node, nnode, vp)) |
| 618 | ospf_spf_add_parent (v, w, vp->nexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | } |
| 620 | |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 621 | /* RFC2328 Section 16.1 (2). |
| 622 | * v is on the SPF tree. Examine the links in v's LSA. Update the list |
| 623 | * of candidates with any vertices not already on the list. If a lower-cost |
| 624 | * path is found to a vertex already on the candidate list, store the new cost. |
| 625 | */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 626 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | ospf_spf_next (struct vertex *v, struct ospf_area *area, |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 628 | struct pqueue * candidate) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 629 | { |
| 630 | struct ospf_lsa *w_lsa = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | u_char *p; |
| 632 | u_char *lim; |
| 633 | struct router_lsa_link *l = NULL; |
| 634 | struct in_addr *r; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | int type = 0; |
| 636 | |
| 637 | /* If this is a router-LSA, and bit V of the router-LSA (see Section |
| 638 | A.4.2:RFC2328) is set, set Area A's TransitCapability to TRUE. */ |
| 639 | if (v->type == OSPF_VERTEX_ROUTER) |
| 640 | { |
| 641 | if (IS_ROUTER_LSA_VIRTUAL ((struct router_lsa *) v->lsa)) |
| 642 | area->transit = OSPF_TRANSIT_TRUE; |
| 643 | } |
| 644 | |
| 645 | p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4; |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 646 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 647 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | while (p < lim) |
| 649 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 650 | struct vertex *w; |
| 651 | unsigned int distance; |
paul | d355bfa | 2004-04-08 07:43:45 +0000 | [diff] [blame] | 652 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 653 | /* In case of V is Router-LSA. */ |
| 654 | if (v->lsa->type == OSPF_ROUTER_LSA) |
| 655 | { |
| 656 | l = (struct router_lsa_link *) p; |
| 657 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 658 | p += (ROUTER_LSA_MIN_SIZE + |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 659 | (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 660 | |
| 661 | /* (a) If this is a link to a stub network, examine the next |
| 662 | link in V's LSA. Links to stub networks will be |
| 663 | considered in the second stage of the shortest path |
| 664 | calculation. */ |
| 665 | if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB) |
| 666 | continue; |
| 667 | |
| 668 | /* (b) Otherwise, W is a transit vertex (router or transit |
| 669 | network). Look up the vertex W's LSA (router-LSA or |
| 670 | network-LSA) in Area A's link state database. */ |
| 671 | switch (type) |
| 672 | { |
| 673 | case LSA_LINK_TYPE_POINTOPOINT: |
| 674 | case LSA_LINK_TYPE_VIRTUALLINK: |
| 675 | if (type == LSA_LINK_TYPE_VIRTUALLINK) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 676 | { |
| 677 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 678 | zlog_debug ("looking up LSA through VL: %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 679 | inet_ntoa (l->link_id)); |
| 680 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 681 | |
| 682 | w_lsa = ospf_lsa_lookup (area, OSPF_ROUTER_LSA, l->link_id, |
| 683 | l->link_id); |
| 684 | if (w_lsa) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 685 | { |
| 686 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 687 | zlog_debug ("found Router LSA %s", inet_ntoa (l->link_id)); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 688 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 689 | break; |
| 690 | case LSA_LINK_TYPE_TRANSIT: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 691 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 692 | zlog_debug ("Looking up Network LSA, ID: %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 693 | inet_ntoa (l->link_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 694 | w_lsa = ospf_lsa_lookup_by_id (area, OSPF_NETWORK_LSA, |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 695 | l->link_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 696 | if (w_lsa) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 697 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 698 | zlog_debug ("found the LSA"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | break; |
| 700 | default: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 701 | zlog_warn ("Invalid LSA link type %d", type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 702 | continue; |
| 703 | } |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | /* In case of V is Network-LSA. */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 708 | r = (struct in_addr *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | p += sizeof (struct in_addr); |
| 710 | |
| 711 | /* Lookup the vertex W's LSA. */ |
| 712 | w_lsa = ospf_lsa_lookup_by_id (area, OSPF_ROUTER_LSA, *r); |
| 713 | } |
| 714 | |
| 715 | /* (b cont.) If the LSA does not exist, or its LS age is equal |
| 716 | to MaxAge, or it does not have a link back to vertex V, |
| 717 | examine the next link in V's LSA.[23] */ |
| 718 | if (w_lsa == NULL) |
| 719 | continue; |
| 720 | |
| 721 | if (IS_LSA_MAXAGE (w_lsa)) |
| 722 | continue; |
| 723 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 724 | if (ospf_lsa_has_link (w_lsa->data, v->lsa) < 0 ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 725 | { |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 726 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 727 | zlog_debug ("The LSA doesn't have a link back"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 728 | continue; |
| 729 | } |
| 730 | |
| 731 | /* (c) If vertex W is already on the shortest-path tree, examine |
| 732 | the next link in the LSA. */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 733 | if (w_lsa->stat == LSA_SPF_IN_SPFTREE) |
| 734 | { |
| 735 | if (IS_DEBUG_OSPF_EVENT) |
| 736 | zlog_debug ("The LSA is already in SPF"); |
| 737 | continue; |
| 738 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | |
| 740 | /* (d) Calculate the link state cost D of the resulting path |
| 741 | from the root to vertex W. D is equal to the sum of the link |
| 742 | state cost of the (already calculated) shortest path to |
| 743 | vertex V and the advertised cost of the link between vertices |
| 744 | V and W. If D is: */ |
| 745 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | /* calculate link cost D. */ |
| 747 | if (v->lsa->type == OSPF_ROUTER_LSA) |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 748 | distance = v->distance + ntohs (l->m[0].metric); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 749 | else /* v is not a Router-LSA */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 750 | distance = v->distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 751 | |
| 752 | /* Is there already vertex W in candidate list? */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 753 | if (w_lsa->stat == LSA_SPF_NOT_EXPLORED) |
| 754 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 755 | /* prepare vertex W. */ |
| 756 | w = ospf_vertex_new (w_lsa); |
| 757 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 758 | /* Calculate nexthop to W. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 759 | w->distance = distance; |
| 760 | ospf_nexthop_calculation (area, v, w, l); |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 761 | pqueue_enqueue (w, candidate); |
| 762 | } |
| 763 | else if (w_lsa->stat >= 0) |
| 764 | { |
| 765 | /* Get the vertex from candidates. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 766 | w = candidate->array[w_lsa->stat]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 768 | /* if D is greater than. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 769 | if (w->distance < distance) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 770 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | continue; |
| 772 | } |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 773 | /* equal to. */ |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 774 | else if (w->distance == distance) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 775 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 776 | /* Found an equal-cost path to W. |
| 777 | * Calculate nexthop of to W from V. */ |
| 778 | ospf_nexthop_calculation (area, v, w, l); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 779 | } |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 780 | /* less than. */ |
| 781 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 782 | { |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 783 | /* Found a lower-cost path to W. */ |
| 784 | w->distance = distance; |
| 785 | |
| 786 | /* Flush existing parent list from W */ |
| 787 | ospf_spf_flush_parents (w); |
| 788 | |
| 789 | /* Calculate new nexthop(s) to W. */ |
| 790 | ospf_nexthop_calculation (area, v, w, l); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 792 | /* Decrease the key of the node in the heap, re-sort the heap. */ |
| 793 | trickle_down (w_lsa->stat, candidate); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 794 | } |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 795 | } /* end W is already on the candidate list */ |
| 796 | } /* end loop over the links in V's LSA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 797 | } |
| 798 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 799 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 800 | ospf_spf_dump (struct vertex *v, int i) |
| 801 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 802 | struct listnode *cnode; |
| 803 | struct listnode *nnode; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 804 | struct vertex_parent *parent; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | |
| 806 | if (v->type == OSPF_VERTEX_ROUTER) |
| 807 | { |
| 808 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 809 | zlog_debug ("SPF Result: %d [R] %s", i, inet_ntoa (v->lsa->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 810 | } |
| 811 | else |
| 812 | { |
| 813 | struct network_lsa *lsa = (struct network_lsa *) v->lsa; |
| 814 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 815 | zlog_debug ("SPF Result: %d [N] %s/%d", i, inet_ntoa (v->lsa->id), |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 816 | ip_masklen (lsa->mask)); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 817 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 818 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 819 | if (IS_DEBUG_OSPF_EVENT) |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 820 | for (ALL_LIST_ELEMENTS_RO (v->parents, nnode, parent)) |
| 821 | { |
| 822 | zlog_debug (" nexthop %p %s %s", |
| 823 | parent->nexthop, |
| 824 | inet_ntoa (parent->nexthop->router), |
| 825 | parent->nexthop->oi ? IF_NAME(parent->nexthop->oi) |
| 826 | : "NULL"); |
| 827 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | |
| 829 | i++; |
| 830 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 831 | for (ALL_LIST_ELEMENTS_RO (v->children, cnode, v)) |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 832 | ospf_spf_dump (v, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* Second stage of SPF calculation. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 836 | static void |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 837 | ospf_spf_process_stubs (struct ospf_area *area, struct vertex *v, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 838 | struct route_table *rt) |
| 839 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 840 | struct listnode *cnode, *cnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 841 | struct vertex *child; |
| 842 | |
| 843 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 844 | zlog_debug ("ospf_process_stub():processing stubs for area %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 845 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 846 | if (v->type == OSPF_VERTEX_ROUTER) |
| 847 | { |
| 848 | u_char *p; |
| 849 | u_char *lim; |
| 850 | struct router_lsa_link *l; |
| 851 | struct router_lsa *rlsa; |
| 852 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 853 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 854 | zlog_debug ("ospf_process_stubs():processing router LSA, id: %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 855 | inet_ntoa (v->lsa->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 856 | rlsa = (struct router_lsa *) v->lsa; |
| 857 | |
| 858 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 859 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 860 | zlog_debug ("ospf_process_stubs(): we have %d links to process", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 861 | ntohs (rlsa->links)); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 862 | p = ((u_char *) v->lsa) + OSPF_LSA_HEADER_SIZE + 4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 863 | lim = ((u_char *) v->lsa) + ntohs (v->lsa->length); |
| 864 | |
| 865 | while (p < lim) |
| 866 | { |
| 867 | l = (struct router_lsa_link *) p; |
| 868 | |
| 869 | p += (ROUTER_LSA_MIN_SIZE + |
| 870 | (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE)); |
| 871 | |
| 872 | if (l->m[0].type == LSA_LINK_TYPE_STUB) |
| 873 | ospf_intra_add_stub (rt, l, v, area); |
| 874 | } |
| 875 | } |
| 876 | |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 877 | ospf_vertex_dump("ospf_process_stubs(): after examining links: ", v, 1, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 878 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 879 | for (ALL_LIST_ELEMENTS (v->children, cnode, cnnode, child)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 881 | if (CHECK_FLAG (child->flags, OSPF_VERTEX_PROCESSED)) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 882 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 883 | |
| 884 | ospf_spf_process_stubs (area, child, rt); |
| 885 | |
| 886 | SET_FLAG (child->flags, OSPF_VERTEX_PROCESSED); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void |
| 891 | ospf_rtrs_free (struct route_table *rtrs) |
| 892 | { |
| 893 | struct route_node *rn; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 894 | struct list *or_list; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 895 | struct ospf_route *or; |
| 896 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 897 | |
| 898 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 899 | zlog_debug ("Route: Router Routing Table free"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | |
| 901 | for (rn = route_top (rtrs); rn; rn = route_next (rn)) |
| 902 | if ((or_list = rn->info) != NULL) |
| 903 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 904 | for (ALL_LIST_ELEMENTS (or_list, node, nnode, or)) |
| 905 | ospf_route_free (or); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 906 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 907 | list_delete (or_list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 908 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 909 | /* Unlock the node. */ |
| 910 | rn->info = NULL; |
| 911 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | } |
| 913 | route_table_finish (rtrs); |
| 914 | } |
| 915 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 916 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | ospf_rtrs_print (struct route_table *rtrs) |
| 918 | { |
| 919 | struct route_node *rn; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 920 | struct list *or_list; |
| 921 | struct listnode *ln; |
| 922 | struct listnode *pnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 923 | struct ospf_route *or; |
| 924 | struct ospf_path *path; |
| 925 | char buf1[BUFSIZ]; |
| 926 | char buf2[BUFSIZ]; |
| 927 | |
| 928 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 929 | zlog_debug ("ospf_rtrs_print() start"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 930 | |
| 931 | for (rn = route_top (rtrs); rn; rn = route_next (rn)) |
| 932 | if ((or_list = rn->info) != NULL) |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 933 | for (ALL_LIST_ELEMENTS_RO (or_list, ln, or)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 934 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 935 | switch (or->path_type) |
| 936 | { |
| 937 | case OSPF_PATH_INTRA_AREA: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 938 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 939 | zlog_debug ("%s [%d] area: %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 940 | inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), |
| 941 | or->cost, inet_ntop (AF_INET, &or->u.std.area_id, |
| 942 | buf2, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | break; |
| 944 | case OSPF_PATH_INTER_AREA: |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 945 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 946 | zlog_debug ("%s IA [%d] area: %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 947 | inet_ntop (AF_INET, &or->id, buf1, BUFSIZ), |
| 948 | or->cost, inet_ntop (AF_INET, &or->u.std.area_id, |
| 949 | buf2, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 950 | break; |
| 951 | default: |
| 952 | break; |
| 953 | } |
| 954 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 955 | for (ALL_LIST_ELEMENTS_RO (or->paths, pnode, path)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | if (path->nexthop.s_addr == 0) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 958 | { |
| 959 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 960 | zlog_debug (" directly attached to %s\r\n", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 961 | IF_NAME (path->oi)); |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 966 | zlog_debug (" via %s, %s\r\n", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 967 | inet_ntoa (path->nexthop), IF_NAME (path->oi)); |
| 968 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 972 | zlog_debug ("ospf_rtrs_print() end"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* Calculating the shortest-path tree for an area. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 976 | static void |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 977 | ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 978 | struct route_table *new_rtrs) |
| 979 | { |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 980 | struct pqueue *candidate; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | struct vertex *v; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 982 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | if (IS_DEBUG_OSPF_EVENT) |
| 984 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 985 | zlog_debug ("ospf_spf_calculate: Start"); |
| 986 | zlog_debug ("ospf_spf_calculate: running Dijkstra for area %s", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 987 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* Check router-lsa-self. If self-router-lsa is not yet allocated, |
| 991 | return this area's calculation. */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 992 | if (!area->router_lsa_self) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 993 | { |
| 994 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 995 | zlog_debug ("ospf_spf_calculate: " |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 996 | "Skip area %s's calculation due to empty router_lsa_self", |
| 997 | inet_ntoa (area->area_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | /* RFC2328 16.1. (1). */ |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1002 | /* Initialize the algorithm's data structures. */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1003 | |
| 1004 | /* This function scans all the LSA database and set the stat field to |
| 1005 | * LSA_SPF_NOT_EXPLORED. */ |
| 1006 | ospf_lsdb_clean_stat (area->lsdb); |
| 1007 | /* Create a new heap for the candidates. */ |
| 1008 | candidate = pqueue_create(); |
| 1009 | candidate->cmp = cmp; |
| 1010 | candidate->update = update_stat; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1011 | |
| 1012 | /* Initialize the shortest-path tree to only the root (which is the |
| 1013 | router doing the calculation). */ |
| 1014 | ospf_spf_init (area); |
| 1015 | v = area->spf; |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1016 | /* Set LSA position to LSA_SPF_IN_SPFTREE. This vertex is the root of the |
| 1017 | * spanning tree. */ |
| 1018 | *(v->stat) = LSA_SPF_IN_SPFTREE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* Set Area A's TransitCapability to FALSE. */ |
| 1021 | area->transit = OSPF_TRANSIT_FALSE; |
| 1022 | area->shortcut_capability = 1; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 1023 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1024 | for (;;) |
| 1025 | { |
| 1026 | /* RFC2328 16.1. (2). */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1027 | ospf_spf_next (v, area, candidate); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* RFC2328 16.1. (3). */ |
| 1030 | /* If at this step the candidate list is empty, the shortest- |
| 1031 | path tree (of transit vertices) has been completely built and |
| 1032 | this stage of the procedure terminates. */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1033 | if (candidate->size == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | |
| 1036 | /* Otherwise, choose the vertex belonging to the candidate list |
| 1037 | that is closest to the root, and add it to the shortest-path |
| 1038 | tree (removing it from the candidate list in the |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1039 | process). */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1040 | /* Extract from the candidates the node with the lower key. */ |
| 1041 | v = (struct vertex *) pqueue_dequeue (candidate); |
| 1042 | /* Update stat field in vertex. */ |
| 1043 | *(v->stat) = LSA_SPF_IN_SPFTREE; |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 1044 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1045 | ospf_vertex_add_parent (v); |
| 1046 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1047 | /* Note that when there is a choice of vertices closest to the |
| 1048 | root, network vertices must be chosen before router vertices |
| 1049 | in order to necessarily find all equal-cost paths. */ |
| 1050 | /* We don't do this at this moment, we should add the treatment |
| 1051 | above codes. -- kunihiro. */ |
| 1052 | |
| 1053 | /* RFC2328 16.1. (4). */ |
| 1054 | if (v->type == OSPF_VERTEX_ROUTER) |
| 1055 | ospf_intra_add_router (new_rtrs, v, area); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1056 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1057 | ospf_intra_add_transit (new_table, v, area); |
| 1058 | |
| 1059 | /* RFC2328 16.1. (5). */ |
| 1060 | /* Iterate the algorithm by returning to Step 2. */ |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 1061 | |
| 1062 | } /* end loop until no more candidate vertices */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (IS_DEBUG_OSPF_EVENT) |
| 1065 | { |
| 1066 | ospf_spf_dump (area->spf, 0); |
| 1067 | ospf_route_table_dump (new_table); |
| 1068 | } |
| 1069 | |
| 1070 | /* Second stage of SPF calculation procedure's */ |
| 1071 | ospf_spf_process_stubs (area, area->spf, new_table); |
| 1072 | |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 1073 | /* Free candidate queue. */ |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 1074 | pqueue_delete (candidate); |
paul | eb3da6d | 2005-10-18 04:20:33 +0000 | [diff] [blame] | 1075 | |
| 1076 | ospf_vertex_dump (__func__, area->spf, 0, 1); |
| 1077 | /* Free nexthop information, canonical versions of which are attached |
| 1078 | * the first level of router vertices attached to the root vertex, see |
| 1079 | * ospf_nexthop_calculation. |
| 1080 | */ |
| 1081 | ospf_canonical_nexthops_free (area->spf); |
| 1082 | |
| 1083 | /* Free SPF vertices */ |
| 1084 | ospf_vertex_free (area->spf, area); |
| 1085 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | /* Increment SPF Calculation Counter. */ |
| 1087 | area->spf_calculation++; |
| 1088 | |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1089 | gettimeofday (&area->ospf->ts_spf, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1090 | |
| 1091 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1092 | zlog_debug ("ospf_spf_calculate: Stop"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* Timer for SPF calculation. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1096 | static int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1097 | ospf_spf_calculate_timer (struct thread *thread) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1098 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1099 | struct ospf *ospf = THREAD_ARG (thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1100 | struct route_table *new_table, *new_rtrs; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1101 | struct ospf_area *area; |
| 1102 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1105 | zlog_debug ("SPF: Timer (SPF calculation expire)"); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1106 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1107 | ospf->t_spf_calc = NULL; |
| 1108 | |
| 1109 | /* Allocate new table tree. */ |
| 1110 | new_table = route_table_init (); |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1111 | new_rtrs = route_table_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1112 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1113 | ospf_vl_unapprove (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* Calculate SPF for each area. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1116 | for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area)) |
| 1117 | ospf_spf_calculate (area, new_table, new_rtrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1119 | ospf_vl_shut_unapproved (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1120 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1121 | ospf_ia_routing (ospf, new_table, new_rtrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | |
| 1123 | ospf_prune_unreachable_networks (new_table); |
| 1124 | ospf_prune_unreachable_routers (new_rtrs); |
| 1125 | |
| 1126 | /* AS-external-LSA calculation should not be performed here. */ |
| 1127 | |
| 1128 | /* If new Router Route is installed, |
| 1129 | then schedule re-calculate External routes. */ |
| 1130 | if (1) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1131 | ospf_ase_calculate_schedule (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1132 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1133 | ospf_ase_calculate_timer_add (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Update routing table. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1136 | ospf_route_install (ospf, new_table); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* Update ABR/ASBR routing table */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1139 | if (ospf->old_rtrs) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | { |
| 1141 | /* old_rtrs's node holds linked list of ospf_route. --kunihiro. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1142 | /* ospf_route_delete (ospf->old_rtrs); */ |
| 1143 | ospf_rtrs_free (ospf->old_rtrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1146 | ospf->old_rtrs = ospf->new_rtrs; |
| 1147 | ospf->new_rtrs = new_rtrs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1148 | |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1149 | if (IS_OSPF_ABR (ospf)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1150 | ospf_abr_task (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1151 | |
| 1152 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1153 | zlog_debug ("SPF: calculation complete"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | /* Add schedule for SPF calculation. To avoid frequenst SPF calc, we |
| 1159 | set timer for SPF calc. */ |
| 1160 | void |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1161 | ospf_spf_calculate_schedule (struct ospf *ospf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | { |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1163 | unsigned long delay, elapsed, ht; |
| 1164 | struct timeval result; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1167 | zlog_debug ("SPF: calculation timer scheduled"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* OSPF instance does not exist. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1170 | if (ospf == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1171 | return; |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1173 | /* SPF calculation timer is already scheduled. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1174 | if (ospf->t_spf_calc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1175 | { |
| 1176 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1177 | zlog_debug ("SPF: calculation timer is already scheduled: %p", |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1178 | ospf->t_spf_calc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* XXX Monotic timers: we only care about relative time here. */ |
paul | c8c1521 | 2005-11-04 12:31:39 +0000 | [diff] [blame] | 1183 | result = tv_sub (recent_time, ospf->ts_spf); |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1184 | |
| 1185 | elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000); |
| 1186 | ht = ospf->spf_holdtime * ospf->spf_hold_multiplier; |
| 1187 | |
| 1188 | if (ht > ospf->spf_max_holdtime) |
| 1189 | ht = ospf->spf_max_holdtime; |
| 1190 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1191 | /* Get SPF calculation delay time. */ |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1192 | if (elapsed < ht) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1193 | { |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1194 | /* Got an event within the hold time of last SPF. We need to |
| 1195 | * increase the hold_multiplier, if it's not already at/past |
| 1196 | * maximum value, and wasn't already increased.. |
| 1197 | */ |
| 1198 | if (ht < ospf->spf_max_holdtime) |
| 1199 | ospf->spf_hold_multiplier++; |
| 1200 | |
| 1201 | /* always honour the SPF initial delay */ |
| 1202 | if ( (ht - elapsed) < ospf->spf_delay) |
paul | 0c0f9cd | 2003-06-06 23:27:04 +0000 | [diff] [blame] | 1203 | delay = ospf->spf_delay; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1204 | else |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1205 | delay = ht - elapsed; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1206 | } |
| 1207 | else |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1208 | { |
| 1209 | /* Event is past required hold-time of last SPF */ |
| 1210 | delay = ospf->spf_delay; |
| 1211 | ospf->spf_hold_multiplier = 1; |
| 1212 | } |
| 1213 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1214 | if (IS_DEBUG_OSPF_EVENT) |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1215 | zlog_debug ("SPF: calculation timer delay = %ld", delay); |
| 1216 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1217 | ospf->t_spf_calc = |
paul | d24f6e2 | 2005-10-21 09:23:12 +0000 | [diff] [blame] | 1218 | thread_add_timer_msec (master, ospf_spf_calculate_timer, ospf, delay); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1219 | } |