blob: 2e1bdedffe973896d8930d9fb0d4f5ba34b8404b [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"
Tomasz Pala3fc1eca2009-06-24 21:48:22 +010028#include "log.h"
paul718e3742002-12-13 20:15:29 +000029
30#include "ospfd/ospfd.h"
31#include "ospfd/ospf_asbr.h"
32#include "ospfd/ospf_lsa.h"
33#include "ospfd/ospf_lsdb.h"
34
35struct ospf_lsdb *
36ospf_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
46void
47ospf_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
55void
56ospf_lsdb_free (struct ospf_lsdb *lsdb)
57{
58 ospf_lsdb_cleanup (lsdb);
59 XFREE (MTYPE_OSPF_LSDB, lsdb);
60}
61
62void
63ospf_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
paul4dadc292005-05-06 21:37:42 +000075static void
paul718e3742002-12-13 20:15:29 +000076lsdb_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 Jakmaba122e72006-08-27 06:24:34 +000085static void
86ospf_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
paul718e3742002-12-13 20:15:29 +0000110/* Add new LSA to lsdb. */
111void
112ospf_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 Jakmaba122e72006-08-27 06:24:34 +0000121
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);
hasso082253f2005-02-11 08:31:54 +0000129
Paul Jakmaba122e72006-08-27 06:24:34 +0000130 if (IS_LSA_SELF (lsa))
131 lsdb->type[lsa->data->type].count_self++;
132 lsdb->type[lsa->data->type].count++;
133 lsdb->total++;
paul718e3742002-12-13 20:15:29 +0000134
135#ifdef MONITOR_LSDB_CHANGE
136 if (lsdb->new_lsa_hook != NULL)
137 (* lsdb->new_lsa_hook)(lsa);
138#endif /* MONITOR_LSDB_CHANGE */
hasso082253f2005-02-11 08:31:54 +0000139 lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
Paul Jakmaba122e72006-08-27 06:24:34 +0000140 rn->info = ospf_lsa_lock (lsa); /* lsdb */
paul718e3742002-12-13 20:15:29 +0000141}
142
143void
144ospf_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 Jakmaac904de2006-06-15 12:04:57 +0000150 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
paul718e3742002-12-13 20:15:29 +0000166 table = lsdb->type[lsa->data->type].db;
167 lsdb_prefix_set (&lp, lsa);
168 rn = route_node_lookup (table, (struct prefix *) &lp);
Paul Jakmaba122e72006-08-27 06:24:34 +0000169 if (rn && (rn->info == lsa))
170 {
171 ospf_lsdb_delete_entry (lsdb, rn);
172 route_unlock_node (rn); /* route_node_lookup */
173 }
paul718e3742002-12-13 20:15:29 +0000174}
175
176void
177ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
178{
179 struct route_table *table;
180 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000181 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 Jakmaba122e72006-08-27 06:24:34 +0000187 if (rn->info != NULL)
188 ospf_lsdb_delete_entry (lsdb, rn);
paul718e3742002-12-13 20:15:29 +0000189 }
190}
191
hasso462f20d2005-02-23 11:29:02 +0000192void
193ospf_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
paul718e3742002-12-13 20:15:29 +0000209struct ospf_lsa *
210ospf_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
229struct ospf_lsa *
230ospf_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
256struct ospf_lsa *
257ospf_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
295unsigned long
296ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
297{
298 return lsdb->total;
299}
300
301unsigned long
302ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
303{
304 return lsdb->type[type].count;
305}
306
307unsigned long
308ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
309{
310 return lsdb->type[type].count_self;
311}
312
hassofe71a972004-12-22 16:16:02 +0000313unsigned int
314ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
315{
316 return lsdb->type[type].checksum;
317}
318
paul718e3742002-12-13 20:15:29 +0000319unsigned long
320ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
321{
322 return (lsdb->total == 0);
323}