blob: 94d839f2d7a2dc0342acc8c6c13d44a0f6b213a9 [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
74void
75lsdb_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
183struct ospf_lsa *
184ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
185{
186 struct route_table *table;
187 struct prefix_ls lp;
188 struct route_node *rn;
189 struct ospf_lsa *find;
190
191 table = lsdb->type[lsa->data->type].db;
192 lsdb_prefix_set (&lp, lsa);
193 rn = route_node_lookup (table, (struct prefix *) &lp);
194 if (rn)
195 {
196 find = rn->info;
197 route_unlock_node (rn);
198 return find;
199 }
200 return NULL;
201}
202
203struct ospf_lsa *
204ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type,
205 struct in_addr id, struct in_addr adv_router)
206{
207 struct route_table *table;
208 struct prefix_ls lp;
209 struct route_node *rn;
210 struct ospf_lsa *find;
211
212 table = lsdb->type[type].db;
213
214 memset (&lp, 0, sizeof (struct prefix_ls));
215 lp.family = 0;
216 lp.prefixlen = 64;
217 lp.id = id;
218 lp.adv_router = adv_router;
219
220 rn = route_node_lookup (table, (struct prefix *) &lp);
221 if (rn)
222 {
223 find = rn->info;
224 route_unlock_node (rn);
225 return find;
226 }
227 return NULL;
228}
229
230struct ospf_lsa *
231ospf_lsdb_lookup_by_id_next (struct ospf_lsdb *lsdb, u_char type,
232 struct in_addr id, struct in_addr adv_router,
233 int first)
234{
235 struct route_table *table;
236 struct prefix_ls lp;
237 struct route_node *rn;
238 struct ospf_lsa *find;
239
240 table = lsdb->type[type].db;
241
242 memset (&lp, 0, sizeof (struct prefix_ls));
243 lp.family = 0;
244 lp.prefixlen = 64;
245 lp.id = id;
246 lp.adv_router = adv_router;
247
248 if (first)
249 rn = route_top (table);
250 else
251 {
252 rn = route_node_get (table, (struct prefix *) &lp);
253 rn = route_next (rn);
254 }
255
256 for (; rn; rn = route_next (rn))
257 if (rn->info)
258 break;
259
260 if (rn && rn->info)
261 {
262 find = rn->info;
263 route_unlock_node (rn);
264 return find;
265 }
266 return NULL;
267}
268
269unsigned long
270ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
271{
272 return lsdb->total;
273}
274
275unsigned long
276ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
277{
278 return lsdb->type[type].count;
279}
280
281unsigned long
282ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
283{
284 return lsdb->type[type].count_self;
285}
286
hassofe71a972004-12-22 16:16:02 +0000287unsigned int
288ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
289{
290 return lsdb->type[type].checksum;
291}
292
paul718e3742002-12-13 20:15:29 +0000293unsigned long
294ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
295{
296 return (lsdb->total == 0);
297}