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