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