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" |
| 28 | |
| 29 | #include "ospfd/ospfd.h" |
| 30 | #include "ospfd/ospf_asbr.h" |
| 31 | #include "ospfd/ospf_lsa.h" |
| 32 | #include "ospfd/ospf_lsdb.h" |
| 33 | |
| 34 | struct ospf_lsdb * |
| 35 | ospf_lsdb_new () |
| 36 | { |
| 37 | struct ospf_lsdb *new; |
| 38 | |
| 39 | new = XCALLOC (MTYPE_OSPF_LSDB, sizeof (struct ospf_lsdb)); |
| 40 | ospf_lsdb_init (new); |
| 41 | |
| 42 | return new; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | ospf_lsdb_init (struct ospf_lsdb *lsdb) |
| 47 | { |
| 48 | int i; |
| 49 | |
| 50 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 51 | lsdb->type[i].db = route_table_init (); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ospf_lsdb_free (struct ospf_lsdb *lsdb) |
| 56 | { |
| 57 | ospf_lsdb_cleanup (lsdb); |
| 58 | XFREE (MTYPE_OSPF_LSDB, lsdb); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | ospf_lsdb_cleanup (struct ospf_lsdb *lsdb) |
| 63 | { |
| 64 | int i; |
| 65 | assert (lsdb); |
| 66 | assert (lsdb->total == 0); |
| 67 | |
| 68 | ospf_lsdb_delete_all (lsdb); |
| 69 | |
| 70 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 71 | route_table_finish (lsdb->type[i].db); |
| 72 | } |
| 73 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 74 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | lsdb_prefix_set (struct prefix_ls *lp, struct ospf_lsa *lsa) |
| 76 | { |
| 77 | memset (lp, 0, sizeof (struct prefix_ls)); |
| 78 | lp->family = 0; |
| 79 | lp->prefixlen = 64; |
| 80 | lp->id = lsa->data->id; |
| 81 | lp->adv_router = lsa->data->adv_router; |
| 82 | } |
| 83 | |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 84 | static void |
| 85 | ospf_lsdb_delete_entry (struct ospf_lsdb *lsdb, struct route_node *rn) |
| 86 | { |
| 87 | struct ospf_lsa *lsa = rn->info; |
| 88 | |
| 89 | if (!lsa) |
| 90 | return; |
| 91 | |
| 92 | assert (rn->table == lsdb->type[lsa->data->type].db); |
| 93 | |
| 94 | if (IS_LSA_SELF (lsa)) |
| 95 | lsdb->type[lsa->data->type].count_self--; |
| 96 | lsdb->type[lsa->data->type].count--; |
| 97 | lsdb->type[lsa->data->type].checksum -= ntohs(lsa->data->checksum); |
| 98 | lsdb->total--; |
| 99 | rn->info = NULL; |
| 100 | route_unlock_node (rn); |
| 101 | #ifdef MONITOR_LSDB_CHANGE |
| 102 | if (lsdb->del_lsa_hook != NULL) |
| 103 | (* lsdb->del_lsa_hook)(lsa); |
| 104 | #endif /* MONITOR_LSDB_CHANGE */ |
| 105 | ospf_lsa_unlock (&lsa); /* lsdb */ |
| 106 | return; |
| 107 | } |
| 108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | /* Add new LSA to lsdb. */ |
| 110 | void |
| 111 | ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 112 | { |
| 113 | struct route_table *table; |
| 114 | struct prefix_ls lp; |
| 115 | struct route_node *rn; |
| 116 | |
| 117 | table = lsdb->type[lsa->data->type].db; |
| 118 | lsdb_prefix_set (&lp, lsa); |
| 119 | rn = route_node_get (table, (struct prefix *)&lp); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 120 | |
| 121 | /* nothing to do? */ |
| 122 | if (rn->info && rn->info == lsa) |
| 123 | return; |
| 124 | |
| 125 | /* purge old entry? */ |
| 126 | if (rn->info) |
| 127 | ospf_lsdb_delete_entry (lsdb, rn); |
hasso | 082253f | 2005-02-11 08:31:54 +0000 | [diff] [blame] | 128 | |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 129 | if (IS_LSA_SELF (lsa)) |
| 130 | lsdb->type[lsa->data->type].count_self++; |
| 131 | lsdb->type[lsa->data->type].count++; |
| 132 | lsdb->total++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 133 | |
| 134 | #ifdef MONITOR_LSDB_CHANGE |
| 135 | if (lsdb->new_lsa_hook != NULL) |
| 136 | (* lsdb->new_lsa_hook)(lsa); |
| 137 | #endif /* MONITOR_LSDB_CHANGE */ |
hasso | 082253f | 2005-02-11 08:31:54 +0000 | [diff] [blame] | 138 | lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 139 | rn->info = ospf_lsa_lock (lsa); /* lsdb */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
| 143 | ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 144 | { |
| 145 | struct route_table *table; |
| 146 | struct prefix_ls lp; |
| 147 | struct route_node *rn; |
| 148 | |
Paul Jakma | ac904de | 2006-06-15 12:04:57 +0000 | [diff] [blame] | 149 | if (!lsdb) |
| 150 | { |
| 151 | zlog_warn ("%s: Called with NULL LSDB", __func__); |
| 152 | if (lsa) |
| 153 | zlog_warn ("LSA[Type%d:%s]: LSA %p, lsa->lsdb %p", |
| 154 | lsa->data->type, inet_ntoa (lsa->data->id), |
| 155 | lsa, lsa->lsdb); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (!lsa) |
| 160 | { |
| 161 | zlog_warn ("%s: Called with NULL LSA", __func__); |
| 162 | return; |
| 163 | } |
| 164 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | table = lsdb->type[lsa->data->type].db; |
| 166 | lsdb_prefix_set (&lp, lsa); |
| 167 | rn = route_node_lookup (table, (struct prefix *) &lp); |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 168 | if (rn && (rn->info == lsa)) |
| 169 | { |
| 170 | ospf_lsdb_delete_entry (lsdb, rn); |
| 171 | route_unlock_node (rn); /* route_node_lookup */ |
| 172 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
| 176 | ospf_lsdb_delete_all (struct ospf_lsdb *lsdb) |
| 177 | { |
| 178 | struct route_table *table; |
| 179 | struct route_node *rn; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | int i; |
| 181 | |
| 182 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 183 | { |
| 184 | table = lsdb->type[i].db; |
| 185 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Paul Jakma | ba122e7 | 2006-08-27 06:24:34 +0000 | [diff] [blame] | 186 | if (rn->info != NULL) |
| 187 | ospf_lsdb_delete_entry (lsdb, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
hasso | 462f20d | 2005-02-23 11:29:02 +0000 | [diff] [blame] | 191 | void |
| 192 | ospf_lsdb_clean_stat (struct ospf_lsdb *lsdb) |
| 193 | { |
| 194 | struct route_table *table; |
| 195 | struct route_node *rn; |
| 196 | struct ospf_lsa *lsa; |
| 197 | int i; |
| 198 | |
| 199 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 200 | { |
| 201 | table = lsdb->type[i].db; |
| 202 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 203 | if ((lsa = (rn->info)) != NULL) |
| 204 | lsa->stat = LSA_SPF_NOT_EXPLORED; |
| 205 | } |
| 206 | } |
| 207 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 208 | struct ospf_lsa * |
| 209 | ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa) |
| 210 | { |
| 211 | struct route_table *table; |
| 212 | struct prefix_ls lp; |
| 213 | struct route_node *rn; |
| 214 | struct ospf_lsa *find; |
| 215 | |
| 216 | table = lsdb->type[lsa->data->type].db; |
| 217 | lsdb_prefix_set (&lp, lsa); |
| 218 | rn = route_node_lookup (table, (struct prefix *) &lp); |
| 219 | if (rn) |
| 220 | { |
| 221 | find = rn->info; |
| 222 | route_unlock_node (rn); |
| 223 | return find; |
| 224 | } |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | struct ospf_lsa * |
| 229 | ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type, |
| 230 | struct in_addr id, struct in_addr adv_router) |
| 231 | { |
| 232 | struct route_table *table; |
| 233 | struct prefix_ls lp; |
| 234 | struct route_node *rn; |
| 235 | struct ospf_lsa *find; |
| 236 | |
| 237 | table = lsdb->type[type].db; |
| 238 | |
| 239 | memset (&lp, 0, sizeof (struct prefix_ls)); |
| 240 | lp.family = 0; |
| 241 | lp.prefixlen = 64; |
| 242 | lp.id = id; |
| 243 | lp.adv_router = adv_router; |
| 244 | |
| 245 | rn = route_node_lookup (table, (struct prefix *) &lp); |
| 246 | if (rn) |
| 247 | { |
| 248 | find = rn->info; |
| 249 | route_unlock_node (rn); |
| 250 | return find; |
| 251 | } |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | struct ospf_lsa * |
| 256 | ospf_lsdb_lookup_by_id_next (struct ospf_lsdb *lsdb, u_char type, |
| 257 | struct in_addr id, struct in_addr adv_router, |
| 258 | int first) |
| 259 | { |
| 260 | struct route_table *table; |
| 261 | struct prefix_ls lp; |
| 262 | struct route_node *rn; |
| 263 | struct ospf_lsa *find; |
| 264 | |
| 265 | table = lsdb->type[type].db; |
| 266 | |
| 267 | memset (&lp, 0, sizeof (struct prefix_ls)); |
| 268 | lp.family = 0; |
| 269 | lp.prefixlen = 64; |
| 270 | lp.id = id; |
| 271 | lp.adv_router = adv_router; |
| 272 | |
| 273 | if (first) |
| 274 | rn = route_top (table); |
| 275 | else |
| 276 | { |
| 277 | rn = route_node_get (table, (struct prefix *) &lp); |
| 278 | rn = route_next (rn); |
| 279 | } |
| 280 | |
| 281 | for (; rn; rn = route_next (rn)) |
| 282 | if (rn->info) |
| 283 | break; |
| 284 | |
| 285 | if (rn && rn->info) |
| 286 | { |
| 287 | find = rn->info; |
| 288 | route_unlock_node (rn); |
| 289 | return find; |
| 290 | } |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | unsigned long |
| 295 | ospf_lsdb_count_all (struct ospf_lsdb *lsdb) |
| 296 | { |
| 297 | return lsdb->total; |
| 298 | } |
| 299 | |
| 300 | unsigned long |
| 301 | ospf_lsdb_count (struct ospf_lsdb *lsdb, int type) |
| 302 | { |
| 303 | return lsdb->type[type].count; |
| 304 | } |
| 305 | |
| 306 | unsigned long |
| 307 | ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type) |
| 308 | { |
| 309 | return lsdb->type[type].count_self; |
| 310 | } |
| 311 | |
hasso | fe71a97 | 2004-12-22 16:16:02 +0000 | [diff] [blame] | 312 | unsigned int |
| 313 | ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type) |
| 314 | { |
| 315 | return lsdb->type[type].checksum; |
| 316 | } |
| 317 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | unsigned long |
| 319 | ospf_lsdb_isempty (struct ospf_lsdb *lsdb) |
| 320 | { |
| 321 | return (lsdb->total == 0); |
| 322 | } |