blob: c906f05283b6ddcac750dcee31b8c26170762ff4 [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{
paul718e3742002-12-13 20:15:29 +000078 lp->family = 0;
79 lp->prefixlen = 64;
80 lp->id = lsa->data->id;
81 lp->adv_router = lsa->data->adv_router;
82}
83
Paul Jakmaba122e72006-08-27 06:24:34 +000084static void
85ospf_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
paul718e3742002-12-13 20:15:29 +0000109/* Add new LSA to lsdb. */
110void
111ospf_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 Jakmaba122e72006-08-27 06:24:34 +0000120
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);
hasso082253f2005-02-11 08:31:54 +0000128
Paul Jakmaba122e72006-08-27 06:24:34 +0000129 if (IS_LSA_SELF (lsa))
130 lsdb->type[lsa->data->type].count_self++;
131 lsdb->type[lsa->data->type].count++;
132 lsdb->total++;
paul718e3742002-12-13 20:15:29 +0000133
134#ifdef MONITOR_LSDB_CHANGE
135 if (lsdb->new_lsa_hook != NULL)
136 (* lsdb->new_lsa_hook)(lsa);
137#endif /* MONITOR_LSDB_CHANGE */
hasso082253f2005-02-11 08:31:54 +0000138 lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
Paul Jakmaba122e72006-08-27 06:24:34 +0000139 rn->info = ospf_lsa_lock (lsa); /* lsdb */
paul718e3742002-12-13 20:15:29 +0000140}
141
142void
143ospf_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 Jakmaac904de2006-06-15 12:04:57 +0000149 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
paul718e3742002-12-13 20:15:29 +0000165 table = lsdb->type[lsa->data->type].db;
166 lsdb_prefix_set (&lp, lsa);
167 rn = route_node_lookup (table, (struct prefix *) &lp);
Paul Jakmaba122e72006-08-27 06:24:34 +0000168 if (rn && (rn->info == lsa))
169 {
170 ospf_lsdb_delete_entry (lsdb, rn);
171 route_unlock_node (rn); /* route_node_lookup */
172 }
paul718e3742002-12-13 20:15:29 +0000173}
174
175void
176ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
177{
178 struct route_table *table;
179 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000180 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 Jakmaba122e72006-08-27 06:24:34 +0000186 if (rn->info != NULL)
187 ospf_lsdb_delete_entry (lsdb, rn);
paul718e3742002-12-13 20:15:29 +0000188 }
189}
190
hasso462f20d2005-02-23 11:29:02 +0000191void
192ospf_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
paul718e3742002-12-13 20:15:29 +0000208struct ospf_lsa *
209ospf_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
228struct ospf_lsa *
229ospf_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
255struct ospf_lsa *
256ospf_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
294unsigned long
295ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
296{
297 return lsdb->total;
298}
299
300unsigned long
301ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
302{
303 return lsdb->type[type].count;
304}
305
306unsigned long
307ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
308{
309 return lsdb->type[type].count_self;
310}
311
hassofe71a972004-12-22 16:16:02 +0000312unsigned int
313ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
314{
315 return lsdb->type[type].checksum;
316}
317
paul718e3742002-12-13 20:15:29 +0000318unsigned long
319ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
320{
321 return (lsdb->total == 0);
322}