blob: c0ec4b3df470aceabec1fa3b5971e4ed4718055b [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
34struct ospf_lsdb *
35ospf_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
45void
46ospf_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
54void
55ospf_lsdb_free (struct ospf_lsdb *lsdb)
56{
57 ospf_lsdb_cleanup (lsdb);
58 XFREE (MTYPE_OSPF_LSDB, lsdb);
59}
60
61void
62ospf_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
paul4dadc292005-05-06 21:37:42 +000074static void
paul718e3742002-12-13 20:15:29 +000075lsdb_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
84/* Add new LSA to lsdb. */
85void
86ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
87{
88 struct route_table *table;
89 struct prefix_ls lp;
90 struct route_node *rn;
hasso082253f2005-02-11 08:31:54 +000091 struct ospf_lsa *old;
paul718e3742002-12-13 20:15:29 +000092
93 table = lsdb->type[lsa->data->type].db;
94 lsdb_prefix_set (&lp, lsa);
95 rn = route_node_get (table, (struct prefix *)&lp);
96 if (!rn->info)
97 {
98 if (IS_LSA_SELF (lsa))
99 lsdb->type[lsa->data->type].count_self++;
100 lsdb->type[lsa->data->type].count++;
101 lsdb->total++;
102 }
103 else
104 {
105 if (rn->info == lsa)
106 return;
107
hasso082253f2005-02-11 08:31:54 +0000108 old = rn->info;
109 lsdb->type[old->data->type].checksum -= ntohs(old->data->checksum);
110
paul718e3742002-12-13 20:15:29 +0000111 ospf_lsa_unlock (rn->info);
112 route_unlock_node (rn);
113 }
114
115#ifdef MONITOR_LSDB_CHANGE
116 if (lsdb->new_lsa_hook != NULL)
117 (* lsdb->new_lsa_hook)(lsa);
118#endif /* MONITOR_LSDB_CHANGE */
hasso082253f2005-02-11 08:31:54 +0000119 lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
paul718e3742002-12-13 20:15:29 +0000120 rn->info = ospf_lsa_lock (lsa);
121}
122
123void
124ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
125{
126 struct route_table *table;
127 struct prefix_ls lp;
128 struct route_node *rn;
129
130 table = lsdb->type[lsa->data->type].db;
131 lsdb_prefix_set (&lp, lsa);
132 rn = route_node_lookup (table, (struct prefix *) &lp);
133 if (rn)
134 if (rn->info == lsa)
135 {
136 if (IS_LSA_SELF (lsa))
137 lsdb->type[lsa->data->type].count_self--;
138 lsdb->type[lsa->data->type].count--;
hassofe71a972004-12-22 16:16:02 +0000139 lsdb->type[lsa->data->type].checksum -= ntohs(lsa->data->checksum);
paul718e3742002-12-13 20:15:29 +0000140 lsdb->total--;
141 rn->info = NULL;
142 route_unlock_node (rn);
143 route_unlock_node (rn);
144#ifdef MONITOR_LSDB_CHANGE
145 if (lsdb->del_lsa_hook != NULL)
146 (* lsdb->del_lsa_hook)(lsa);
147#endif /* MONITOR_LSDB_CHANGE */
148 ospf_lsa_unlock (lsa);
149 return;
150 }
151}
152
153void
154ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
155{
156 struct route_table *table;
157 struct route_node *rn;
158 struct ospf_lsa *lsa;
159 int i;
160
161 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
162 {
163 table = lsdb->type[i].db;
164 for (rn = route_top (table); rn; rn = route_next (rn))
165 if ((lsa = (rn->info)) != NULL)
166 {
167 if (IS_LSA_SELF (lsa))
168 lsdb->type[i].count_self--;
169 lsdb->type[i].count--;
hassofe71a972004-12-22 16:16:02 +0000170 lsdb->type[i].checksum -= ntohs(lsa->data->checksum);
paul718e3742002-12-13 20:15:29 +0000171 lsdb->total--;
172 rn->info = NULL;
173 route_unlock_node (rn);
174#ifdef MONITOR_LSDB_CHANGE
175 if (lsdb->del_lsa_hook != NULL)
176 (* lsdb->del_lsa_hook)(lsa);
177#endif /* MONITOR_LSDB_CHANGE */
178 ospf_lsa_unlock (lsa);
179 }
180 }
181}
182
hasso462f20d2005-02-23 11:29:02 +0000183void
184ospf_lsdb_clean_stat (struct ospf_lsdb *lsdb)
185{
186 struct route_table *table;
187 struct route_node *rn;
188 struct ospf_lsa *lsa;
189 int i;
190
191 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
192 {
193 table = lsdb->type[i].db;
194 for (rn = route_top (table); rn; rn = route_next (rn))
195 if ((lsa = (rn->info)) != NULL)
196 lsa->stat = LSA_SPF_NOT_EXPLORED;
197 }
198}
199
paul718e3742002-12-13 20:15:29 +0000200struct ospf_lsa *
201ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
202{
203 struct route_table *table;
204 struct prefix_ls lp;
205 struct route_node *rn;
206 struct ospf_lsa *find;
207
208 table = lsdb->type[lsa->data->type].db;
209 lsdb_prefix_set (&lp, lsa);
210 rn = route_node_lookup (table, (struct prefix *) &lp);
211 if (rn)
212 {
213 find = rn->info;
214 route_unlock_node (rn);
215 return find;
216 }
217 return NULL;
218}
219
220struct ospf_lsa *
221ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type,
222 struct in_addr id, struct in_addr adv_router)
223{
224 struct route_table *table;
225 struct prefix_ls lp;
226 struct route_node *rn;
227 struct ospf_lsa *find;
228
229 table = lsdb->type[type].db;
230
231 memset (&lp, 0, sizeof (struct prefix_ls));
232 lp.family = 0;
233 lp.prefixlen = 64;
234 lp.id = id;
235 lp.adv_router = adv_router;
236
237 rn = route_node_lookup (table, (struct prefix *) &lp);
238 if (rn)
239 {
240 find = rn->info;
241 route_unlock_node (rn);
242 return find;
243 }
244 return NULL;
245}
246
247struct ospf_lsa *
248ospf_lsdb_lookup_by_id_next (struct ospf_lsdb *lsdb, u_char type,
249 struct in_addr id, struct in_addr adv_router,
250 int first)
251{
252 struct route_table *table;
253 struct prefix_ls lp;
254 struct route_node *rn;
255 struct ospf_lsa *find;
256
257 table = lsdb->type[type].db;
258
259 memset (&lp, 0, sizeof (struct prefix_ls));
260 lp.family = 0;
261 lp.prefixlen = 64;
262 lp.id = id;
263 lp.adv_router = adv_router;
264
265 if (first)
266 rn = route_top (table);
267 else
268 {
269 rn = route_node_get (table, (struct prefix *) &lp);
270 rn = route_next (rn);
271 }
272
273 for (; rn; rn = route_next (rn))
274 if (rn->info)
275 break;
276
277 if (rn && rn->info)
278 {
279 find = rn->info;
280 route_unlock_node (rn);
281 return find;
282 }
283 return NULL;
284}
285
286unsigned long
287ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
288{
289 return lsdb->total;
290}
291
292unsigned long
293ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
294{
295 return lsdb->type[type].count;
296}
297
298unsigned long
299ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
300{
301 return lsdb->type[type].count_self;
302}
303
hassofe71a972004-12-22 16:16:02 +0000304unsigned int
305ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
306{
307 return lsdb->type[type].checksum;
308}
309
paul718e3742002-12-13 20:15:29 +0000310unsigned long
311ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
312{
313 return (lsdb->total == 0);
314}