paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF LSDB support. |
| 3 | * Copyright (C) 1999, 2000 Alex Zinin, Kunihiro Ishiguro, Toshiaki Takada |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "prefix.h" |
| 26 | #include "table.h" |
| 27 | #include "memory.h" |
Tomasz Pala | 3fc1eca | 2009-06-24 21:48:22 +0100 | [diff] [blame^] | 28 | #include "log.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | |
| 30 | #include "ospfd/ospfd.h" |
| 31 | #include "ospfd/ospf_asbr.h" |
| 32 | #include "ospfd/ospf_lsa.h" |
| 33 | #include "ospfd/ospf_lsdb.h" |
| 34 | |
| 35 | struct ospf_lsdb * |
| 36 | ospf_lsdb_new () |
| 37 | { |
| 38 | struct ospf_lsdb *new; |
| 39 | |
| 40 | new = XCALLOC (MTYPE_OSPF_LSDB, sizeof (struct ospf_lsdb)); |
| 41 | ospf_lsdb_init (new); |
| 42 | |
| 43 | return new; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | ospf_lsdb_init (struct ospf_lsdb *lsdb) |
| 48 | { |
| 49 | int i; |
| 50 | |
| 51 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 52 | lsdb->type[i].db = route_table_init (); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | ospf_lsdb_free (struct ospf_lsdb *lsdb) |
| 57 | { |
| 58 | ospf_lsdb_cleanup (lsdb); |
| 59 | XFREE (MTYPE_OSPF_LSDB, lsdb); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | ospf_lsdb_cleanup (struct ospf_lsdb *lsdb) |
| 64 | { |
| 65 | int i; |
| 66 | assert (lsdb); |
| 67 | assert (lsdb->total == 0); |
| 68 | |
| 69 | ospf_lsdb_delete_all (lsdb); |
| 70 | |
| 71 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 72 | route_table_finish (lsdb->type[i].db); |
| 73 | } |
| 74 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 75 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | lsdb_prefix_set (struct prefix_ls *lp, struct ospf_lsa *lsa) |
| 77 | { |
| 78 | memset (lp, 0, sizeof (struct prefix_ls)); |
| 79 | lp->family = 0; |
| 80 | lp->prefixlen = 64; |
| 81 | lp->id = lsa->data->id; |
| 82 | lp->adv_router = lsa->data->adv_router; |
| 83 | } |
| 84 | |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 85 | static void |
| 86 | ospf_lsdb_delete_entry (struct ospf_lsdb *lsdb, struct route_node *rn) |
| 87 | { |
| 88 | struct ospf_lsa *lsa = rn->info; |
| 89 | |
| 90 | if (!lsa) |
| 91 | return; |
| 92 | |
| 93 | assert (rn->table == lsdb->type[lsa->data->type].db); |
| 94 | |
| 95 | if (IS_LSA_SELF (lsa)) |
| 96 | lsdb->type[lsa->data->type].count_self--; |
| 97 | lsdb->type[lsa->data->type].count--; |
| 98 | lsdb->type[lsa->data->type].checksum -= ntohs(lsa->data->checksum); |
| 99 | lsdb->total--; |
| 100 | rn->info = NULL; |
| 101 | route_unlock_node (rn); |
| 102 | #ifdef MONITOR_LSDB_CHANGE |
| 103 | if (lsdb->del_lsa_hook != NULL) |
| 104 | (* lsdb->del_lsa_hook)(lsa); |
| 105 | #endif /* MONITOR_LSDB_CHANGE */ |
| 106 | ospf_lsa_unlock (&lsa); /* lsdb */ |
| 107 | return; |
| 108 | } |
| 109 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | /* Add new LSA to lsdb. */ |
| 111 | void |
| 112 | ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 113 | { |
| 114 | struct route_table *table; |
| 115 | struct prefix_ls lp; |
| 116 | struct route_node *rn; |
| 117 | |
| 118 | table = lsdb->type[lsa->data->type].db; |
| 119 | lsdb_prefix_set (&lp, lsa); |
| 120 | rn = route_node_get (table, (struct prefix *)&lp); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 121 | |
| 122 | /* nothing to do? */ |
| 123 | if (rn->info && rn->info == lsa) |
| 124 | return; |
| 125 | |
| 126 | /* purge old entry? */ |
| 127 | if (rn->info) |
| 128 | ospf_lsdb_delete_entry (lsdb, rn); |
hasso | 082253f | 2005-02-11 08:31:54 +0000 | [diff] [blame] | 129 | |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 130 | if (IS_LSA_SELF (lsa)) |
| 131 | lsdb->type[lsa->data->type].count_self++; |
| 132 | lsdb->type[lsa->data->type].count++; |
| 133 | lsdb->total++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | |
| 135 | #ifdef MONITOR_LSDB_CHANGE |
| 136 | if (lsdb->new_lsa_hook != NULL) |
| 137 | (* lsdb->new_lsa_hook)(lsa); |
| 138 | #endif /* MONITOR_LSDB_CHANGE */ |
hasso | 082253f | 2005-02-11 08:31:54 +0000 | [diff] [blame] | 139 | lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 140 | rn->info = ospf_lsa_lock (lsa); /* lsdb */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 145 | { |
| 146 | struct route_table *table; |
| 147 | struct prefix_ls lp; |
| 148 | struct route_node *rn; |
| 149 | |
Paul Jakma | ac904de | 2006-06-15 12:04:57 +0000 | [diff] [blame] | 150 | if (!lsdb) |
| 151 | { |
| 152 | zlog_warn ("%s: Called with NULL LSDB", __func__); |
| 153 | if (lsa) |
| 154 | zlog_warn ("LSA[Type%d:%s]: LSA %p, lsa->lsdb %p", |
| 155 | lsa->data->type, inet_ntoa (lsa->data->id), |
| 156 | lsa, lsa->lsdb); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (!lsa) |
| 161 | { |
| 162 | zlog_warn ("%s: Called with NULL LSA", __func__); |
| 163 | return; |
| 164 | } |
| 165 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | table = lsdb->type[lsa->data->type].db; |
| 167 | lsdb_prefix_set (&lp, lsa); |
| 168 | rn = route_node_lookup (table, (struct prefix *) &lp); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 169 | if (rn && (rn->info == lsa)) |
| 170 | { |
| 171 | ospf_lsdb_delete_entry (lsdb, rn); |
| 172 | route_unlock_node (rn); /* route_node_lookup */ |
| 173 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
| 177 | ospf_lsdb_delete_all (struct ospf_lsdb *lsdb) |
| 178 | { |
| 179 | struct route_table *table; |
| 180 | struct route_node *rn; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | int i; |
| 182 | |
| 183 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 184 | { |
| 185 | table = lsdb->type[i].db; |
| 186 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 187 | if (rn->info != NULL) |
| 188 | ospf_lsdb_delete_entry (lsdb, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 192 | void |
| 193 | ospf_lsdb_clean_stat (struct ospf_lsdb *lsdb) |
| 194 | { |
| 195 | struct route_table *table; |
| 196 | struct route_node *rn; |
| 197 | struct ospf_lsa *lsa; |
| 198 | int i; |
| 199 | |
| 200 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 201 | { |
| 202 | table = lsdb->type[i].db; |
| 203 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 204 | if ((lsa = (rn->info)) != NULL) |
| 205 | lsa->stat = LSA_SPF_NOT_EXPLORED; |
| 206 | } |
| 207 | } |
| 208 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | struct ospf_lsa * |
| 210 | ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 211 | { |
| 212 | struct route_table *table; |
| 213 | struct prefix_ls lp; |
| 214 | struct route_node *rn; |
| 215 | struct ospf_lsa *find; |
| 216 | |
| 217 | table = lsdb->type[lsa->data->type].db; |
| 218 | lsdb_prefix_set (&lp, lsa); |
| 219 | rn = route_node_lookup (table, (struct prefix *) &lp); |
| 220 | if (rn) |
| 221 | { |
| 222 | find = rn->info; |
| 223 | route_unlock_node (rn); |
| 224 | return find; |
| 225 | } |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | struct ospf_lsa * |
| 230 | ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type, |
| 231 | struct in_addr id, struct in_addr adv_router) |
| 232 | { |
| 233 | struct route_table *table; |
| 234 | struct prefix_ls lp; |
| 235 | struct route_node *rn; |
| 236 | struct ospf_lsa *find; |
| 237 | |
| 238 | table = lsdb->type[type].db; |
| 239 | |
| 240 | memset (&lp, 0, sizeof (struct prefix_ls)); |
| 241 | lp.family = 0; |
| 242 | lp.prefixlen = 64; |
| 243 | lp.id = id; |
| 244 | lp.adv_router = adv_router; |
| 245 | |
| 246 | rn = route_node_lookup (table, (struct prefix *) &lp); |
| 247 | if (rn) |
| 248 | { |
| 249 | find = rn->info; |
| 250 | route_unlock_node (rn); |
| 251 | return find; |
| 252 | } |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | struct ospf_lsa * |
| 257 | ospf_lsdb_lookup_by_id_next (struct ospf_lsdb *lsdb, u_char type, |
| 258 | struct in_addr id, struct in_addr adv_router, |
| 259 | int first) |
| 260 | { |
| 261 | struct route_table *table; |
| 262 | struct prefix_ls lp; |
| 263 | struct route_node *rn; |
| 264 | struct ospf_lsa *find; |
| 265 | |
| 266 | table = lsdb->type[type].db; |
| 267 | |
| 268 | memset (&lp, 0, sizeof (struct prefix_ls)); |
| 269 | lp.family = 0; |
| 270 | lp.prefixlen = 64; |
| 271 | lp.id = id; |
| 272 | lp.adv_router = adv_router; |
| 273 | |
| 274 | if (first) |
| 275 | rn = route_top (table); |
| 276 | else |
| 277 | { |
| 278 | rn = route_node_get (table, (struct prefix *) &lp); |
| 279 | rn = route_next (rn); |
| 280 | } |
| 281 | |
| 282 | for (; rn; rn = route_next (rn)) |
| 283 | if (rn->info) |
| 284 | break; |
| 285 | |
| 286 | if (rn && rn->info) |
| 287 | { |
| 288 | find = rn->info; |
| 289 | route_unlock_node (rn); |
| 290 | return find; |
| 291 | } |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | unsigned long |
| 296 | ospf_lsdb_count_all (struct ospf_lsdb *lsdb) |
| 297 | { |
| 298 | return lsdb->total; |
| 299 | } |
| 300 | |
| 301 | unsigned long |
| 302 | ospf_lsdb_count (struct ospf_lsdb *lsdb, int type) |
| 303 | { |
| 304 | return lsdb->type[type].count; |
| 305 | } |
| 306 | |
| 307 | unsigned long |
| 308 | ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type) |
| 309 | { |
| 310 | return lsdb->type[type].count_self; |
| 311 | } |
| 312 | |
hasso | fe71a97 | 2004-12-22 16:16:02 +0000 | [diff] [blame] | 313 | unsigned int |
| 314 | ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type) |
| 315 | { |
| 316 | return lsdb->type[type].checksum; |
| 317 | } |
| 318 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 319 | unsigned long |
| 320 | ospf_lsdb_isempty (struct ospf_lsdb *lsdb) |
| 321 | { |
| 322 | return (lsdb->total == 0); |
| 323 | } |