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