]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_lsdb.c
version RE-0.99.17.6
[mirror_frr.git] / ospfd / ospf_lsdb.c
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 #include "log.h"
29
30 #include "ospfd/ospfd.h"
31 #include "ospfd/ospf_asbr.h"
32 #include "ospfd/ospf_lsa.h"
33 #include "ospfd/ospf_lsdb.h"
34 \f
35 struct ospf_lsdb *
36 ospf_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
46 void
47 ospf_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
55 void
56 ospf_lsdb_free (struct ospf_lsdb *lsdb)
57 {
58 ospf_lsdb_cleanup (lsdb);
59 XFREE (MTYPE_OSPF_LSDB, lsdb);
60 }
61
62 void
63 ospf_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
75 static void
76 lsdb_prefix_set (struct prefix_ls *lp, struct ospf_lsa *lsa)
77 {
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 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
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);
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);
128
129 if (IS_LSA_SELF (lsa))
130 lsdb->type[lsa->data->type].count_self++;
131 lsdb->type[lsa->data->type].count++;
132 lsdb->total++;
133
134 #ifdef MONITOR_LSDB_CHANGE
135 if (lsdb->new_lsa_hook != NULL)
136 (* lsdb->new_lsa_hook)(lsa);
137 #endif /* MONITOR_LSDB_CHANGE */
138 lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
139 rn->info = ospf_lsa_lock (lsa); /* lsdb */
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
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
165 table = lsdb->type[lsa->data->type].db;
166 lsdb_prefix_set (&lp, lsa);
167 rn = route_node_lookup (table, (struct prefix *) &lp);
168 if (rn && (rn->info == lsa))
169 {
170 ospf_lsdb_delete_entry (lsdb, rn);
171 route_unlock_node (rn); /* route_node_lookup */
172 }
173 }
174
175 void
176 ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
177 {
178 struct route_table *table;
179 struct route_node *rn;
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))
186 if (rn->info != NULL)
187 ospf_lsdb_delete_entry (lsdb, rn);
188 }
189 }
190
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
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
312 unsigned int
313 ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
314 {
315 return lsdb->type[type].checksum;
316 }
317
318 unsigned long
319 ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
320 {
321 return (lsdb->total == 0);
322 }