paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2003 Yasuhiro Ohara |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "memory.h" |
| 25 | #include "log.h" |
| 26 | #include "command.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 27 | #include "prefix.h" |
| 28 | #include "table.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 30 | #include "ospf6d.h" |
| 31 | #include "ospf6_proto.h" |
| 32 | #include "ospf6_lsa.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | #include "ospf6_lsdb.h" |
| 34 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | struct ospf6_lsdb * |
| 36 | ospf6_lsdb_create () |
| 37 | { |
| 38 | struct ospf6_lsdb *lsdb; |
| 39 | |
| 40 | lsdb = XCALLOC (MTYPE_OSPF6_LSDB, sizeof (struct ospf6_lsdb)); |
| 41 | if (lsdb == NULL) |
| 42 | { |
| 43 | zlog_warn ("Can't malloc lsdb"); |
| 44 | return NULL; |
| 45 | } |
| 46 | memset (lsdb, 0, sizeof (struct ospf6_lsdb)); |
| 47 | |
| 48 | lsdb->table = route_table_init (); |
| 49 | return lsdb; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | ospf6_lsdb_delete (struct ospf6_lsdb *lsdb) |
| 54 | { |
| 55 | ospf6_lsdb_remove_all (lsdb); |
| 56 | route_table_finish (lsdb->table); |
| 57 | XFREE (MTYPE_OSPF6_LSDB, lsdb); |
| 58 | } |
| 59 | |
| 60 | static void |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 61 | ospf6_lsdb_set_key (struct prefix_ipv6 *key, void *value, int len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 63 | assert (key->prefixlen % 8 == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 65 | memcpy ((caddr_t) &key->prefix + key->prefixlen / 8, |
| 66 | (caddr_t) value, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | key->family = AF_INET6; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 68 | key->prefixlen += len * 8; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 71 | #ifndef NDEBUG |
| 72 | static void |
| 73 | _lsdb_count_assert (struct ospf6_lsdb *lsdb) |
| 74 | { |
| 75 | struct ospf6_lsa *debug; |
| 76 | int num = 0; |
| 77 | for (debug = ospf6_lsdb_head (lsdb); debug; |
| 78 | debug = ospf6_lsdb_next (debug)) |
| 79 | num++; |
| 80 | |
| 81 | if (num == lsdb->count) |
| 82 | return; |
| 83 | |
| 84 | if (IS_OSPF6_DEBUG_LSA (DATABASE)) |
| 85 | { |
| 86 | zlog_info ("PANIC !! lsdb[%p]->count = %d, real = %d", |
| 87 | lsdb, lsdb->count, num); |
| 88 | for (debug = ospf6_lsdb_head (lsdb); debug; |
| 89 | debug = ospf6_lsdb_next (debug)) |
| 90 | zlog_info ("%p %p %s", debug->prev, debug->next, debug->name); |
| 91 | zlog_info ("DUMP END"); |
| 92 | } |
| 93 | assert (num == lsdb->count); |
| 94 | } |
| 95 | #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t)) |
| 96 | #else /*NDEBUG*/ |
| 97 | #define ospf6_lsdb_count_assert(t) ((void) 0) |
| 98 | #endif /*NDEBUG*/ |
| 99 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | void |
| 101 | ospf6_lsdb_add (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb) |
| 102 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | struct prefix_ipv6 key; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 104 | struct route_node *current, *nextnode, *prevnode; |
| 105 | struct ospf6_lsa *next, *prev, *old = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 107 | memset (&key, 0, sizeof (key)); |
| 108 | ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type)); |
| 109 | ospf6_lsdb_set_key (&key, &lsa->header->adv_router, |
| 110 | sizeof (lsa->header->adv_router)); |
| 111 | ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 113 | current = route_node_get (lsdb->table, (struct prefix *) &key); |
| 114 | old = current->info; |
| 115 | current->info = lsa; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | ospf6_lsa_lock (lsa); |
| 117 | |
| 118 | if (old) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 119 | { |
| 120 | if (old->prev) |
| 121 | old->prev->next = lsa; |
| 122 | if (old->next) |
| 123 | old->next->prev = lsa; |
| 124 | lsa->next = old->next; |
| 125 | lsa->prev = old->prev; |
| 126 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | else |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 128 | { |
| 129 | /* next link */ |
| 130 | nextnode = current; |
| 131 | route_lock_node (nextnode); |
| 132 | do { |
| 133 | nextnode = route_next (nextnode); |
| 134 | } while (nextnode && nextnode->info == NULL); |
| 135 | if (nextnode == NULL) |
| 136 | lsa->next = NULL; |
| 137 | else |
| 138 | { |
| 139 | next = nextnode->info; |
| 140 | lsa->next = next; |
| 141 | next->prev = lsa; |
| 142 | route_unlock_node (nextnode); |
| 143 | } |
| 144 | |
| 145 | /* prev link */ |
| 146 | prevnode = current; |
| 147 | route_lock_node (prevnode); |
| 148 | do { |
| 149 | prevnode = route_prev (prevnode); |
| 150 | } while (prevnode && prevnode->info == NULL); |
| 151 | if (prevnode == NULL) |
| 152 | lsa->prev = NULL; |
| 153 | else |
| 154 | { |
| 155 | prev = prevnode->info; |
| 156 | lsa->prev = prev; |
| 157 | prev->next = lsa; |
| 158 | route_unlock_node (prevnode); |
| 159 | } |
| 160 | |
| 161 | lsdb->count++; |
| 162 | } |
| 163 | |
| 164 | if (old) |
| 165 | { |
| 166 | if (OSPF6_LSA_IS_CHANGED (old, lsa)) |
| 167 | { |
| 168 | if (OSPF6_LSA_IS_MAXAGE (lsa)) |
| 169 | { |
| 170 | if (lsdb->hook_remove) |
| 171 | { |
| 172 | (*lsdb->hook_remove) (old); |
| 173 | (*lsdb->hook_remove) (lsa); |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | if (lsdb->hook_remove) |
| 179 | (*lsdb->hook_remove) (old); |
| 180 | if (lsdb->hook_add) |
| 181 | (*lsdb->hook_add) (lsa); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | else if (OSPF6_LSA_IS_MAXAGE (lsa)) |
| 186 | { |
| 187 | if (lsdb->hook_remove) |
| 188 | (*lsdb->hook_remove) (lsa); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | if (lsdb->hook_add) |
| 193 | (*lsdb->hook_add) (lsa); |
| 194 | } |
| 195 | |
| 196 | if (old) |
| 197 | ospf6_lsa_unlock (old); |
| 198 | |
| 199 | ospf6_lsdb_count_assert (lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
| 203 | ospf6_lsdb_remove (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb) |
| 204 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 205 | struct route_node *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | struct prefix_ipv6 key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 208 | memset (&key, 0, sizeof (key)); |
| 209 | ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type)); |
| 210 | ospf6_lsdb_set_key (&key, &lsa->header->adv_router, |
| 211 | sizeof (lsa->header->adv_router)); |
| 212 | ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 214 | node = route_node_lookup (lsdb->table, (struct prefix *) &key); |
| 215 | assert (node && node->info == lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 217 | if (lsa->prev) |
| 218 | lsa->prev->next = lsa->next; |
| 219 | if (lsa->next) |
| 220 | lsa->next->prev = lsa->prev; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 222 | node->info = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | lsdb->count--; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 225 | if (lsdb->hook_remove) |
| 226 | (*lsdb->hook_remove) (lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 227 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 228 | ospf6_lsa_unlock (lsa); |
| 229 | route_unlock_node (node); |
| 230 | ospf6_lsdb_count_assert (lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | struct ospf6_lsa * |
| 234 | ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router, |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 235 | struct ospf6_lsdb *lsdb) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 237 | struct route_node *node; |
| 238 | struct prefix_ipv6 key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 240 | if (lsdb == NULL) |
| 241 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 243 | memset (&key, 0, sizeof (key)); |
| 244 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
| 245 | ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router)); |
| 246 | ospf6_lsdb_set_key (&key, &id, sizeof (id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 248 | node = route_node_lookup (lsdb->table, (struct prefix *) &key); |
| 249 | if (node == NULL || node->info == NULL) |
| 250 | return NULL; |
| 251 | return (struct ospf6_lsa *) node->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | } |
| 253 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 254 | /* Iteration function */ |
| 255 | struct ospf6_lsa * |
| 256 | ospf6_lsdb_head (struct ospf6_lsdb *lsdb) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 257 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 258 | struct route_node *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 260 | node = route_top (lsdb->table); |
| 261 | if (node == NULL) |
| 262 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 264 | /* skip to the existing lsdb entry */ |
| 265 | while (node && node->info == NULL) |
| 266 | node = route_next (node); |
| 267 | if (node == NULL) |
| 268 | return NULL; |
| 269 | |
| 270 | route_unlock_node (node); |
| 271 | if (node->info) |
| 272 | ospf6_lsa_lock ((struct ospf6_lsa *) node->info); |
| 273 | return (struct ospf6_lsa *) node->info; |
| 274 | } |
| 275 | |
| 276 | struct ospf6_lsa * |
| 277 | ospf6_lsdb_next (struct ospf6_lsa *lsa) |
| 278 | { |
| 279 | struct ospf6_lsa *next = lsa->next; |
| 280 | |
| 281 | ospf6_lsa_unlock (lsa); |
| 282 | if (next) |
| 283 | ospf6_lsa_lock (next); |
| 284 | |
| 285 | return next; |
| 286 | } |
| 287 | |
| 288 | /* Macro version of check_bit (). */ |
| 289 | #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1) |
| 290 | |
| 291 | struct ospf6_lsa * |
| 292 | ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router, |
| 293 | struct ospf6_lsdb *lsdb) |
| 294 | { |
| 295 | struct route_node *node; |
| 296 | struct prefix_ipv6 key; |
| 297 | struct ospf6_lsa *lsa; |
| 298 | |
| 299 | memset (&key, 0, sizeof (key)); |
| 300 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
| 301 | ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router)); |
| 302 | |
| 303 | node = lsdb->table->top; |
| 304 | |
| 305 | /* Walk down tree. */ |
| 306 | while (node && node->p.prefixlen <= key.prefixlen && |
| 307 | prefix_match (&node->p, (struct prefix *) &key)) |
| 308 | node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)]; |
| 309 | |
| 310 | if (node) |
| 311 | route_lock_node (node); |
| 312 | while (node && node->info == NULL) |
| 313 | node = route_next (node); |
| 314 | |
| 315 | if (node == NULL) |
| 316 | return NULL; |
| 317 | else |
| 318 | route_unlock_node (node); |
| 319 | |
| 320 | if (! prefix_match ((struct prefix *) &key, &node->p)) |
| 321 | return NULL; |
| 322 | |
| 323 | lsa = node->info; |
| 324 | ospf6_lsa_lock (lsa); |
| 325 | |
| 326 | return lsa; |
| 327 | } |
| 328 | |
| 329 | struct ospf6_lsa * |
| 330 | ospf6_lsdb_type_router_next (u_int16_t type, u_int32_t adv_router, |
| 331 | struct ospf6_lsa *lsa) |
| 332 | { |
| 333 | struct ospf6_lsa *next = lsa->next; |
| 334 | |
| 335 | if (next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 337 | if (next->header->type != type || |
| 338 | next->header->adv_router != adv_router) |
| 339 | next = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 342 | if (next) |
| 343 | ospf6_lsa_lock (next); |
| 344 | ospf6_lsa_unlock (lsa); |
| 345 | return next; |
| 346 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 348 | struct ospf6_lsa * |
| 349 | ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb) |
| 350 | { |
| 351 | struct route_node *node; |
| 352 | struct prefix_ipv6 key; |
| 353 | struct ospf6_lsa *lsa; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 354 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 355 | memset (&key, 0, sizeof (key)); |
| 356 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 357 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 358 | /* Walk down tree. */ |
| 359 | node = lsdb->table->top; |
| 360 | while (node && node->p.prefixlen <= key.prefixlen && |
| 361 | prefix_match (&node->p, (struct prefix *) &key)) |
| 362 | node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 364 | if (node) |
| 365 | route_lock_node (node); |
| 366 | while (node && node->info == NULL) |
| 367 | node = route_next (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 368 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 369 | if (node == NULL) |
| 370 | return NULL; |
| 371 | else |
| 372 | route_unlock_node (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 374 | if (! prefix_match ((struct prefix *) &key, &node->p)) |
| 375 | return NULL; |
| 376 | |
| 377 | lsa = node->info; |
| 378 | ospf6_lsa_lock (lsa); |
| 379 | |
| 380 | return lsa; |
| 381 | } |
| 382 | |
| 383 | struct ospf6_lsa * |
| 384 | ospf6_lsdb_type_next (u_int16_t type, struct ospf6_lsa *lsa) |
| 385 | { |
| 386 | struct ospf6_lsa *next = lsa->next; |
| 387 | |
| 388 | if (next) |
| 389 | { |
| 390 | if (next->header->type != type) |
| 391 | next = NULL; |
| 392 | } |
| 393 | |
| 394 | if (next) |
| 395 | ospf6_lsa_lock (next); |
| 396 | ospf6_lsa_unlock (lsa); |
| 397 | return next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void |
| 401 | ospf6_lsdb_remove_all (struct ospf6_lsdb *lsdb) |
| 402 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | struct ospf6_lsa *lsa; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame^] | 404 | for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa)) |
| 405 | ospf6_lsdb_remove (lsa, lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |