build: Add QuaggaId to README.NetBSD
[jleu-quagga.git] / ospfd / ospf_lsdb.c
blobc906f05283b6ddcac750dcee31b8c26170762ff4
1 /*
2 * OSPF LSDB support.
3 * Copyright (C) 1999, 2000 Alex Zinin, Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
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.
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.
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.
23 #include <zebra.h>
25 #include "prefix.h"
26 #include "table.h"
27 #include "memory.h"
28 #include "log.h"
30 #include "ospfd/ospfd.h"
31 #include "ospfd/ospf_asbr.h"
32 #include "ospfd/ospf_lsa.h"
33 #include "ospfd/ospf_lsdb.h"
35 struct ospf_lsdb *
36 ospf_lsdb_new ()
38 struct ospf_lsdb *new;
40 new = XCALLOC (MTYPE_OSPF_LSDB, sizeof (struct ospf_lsdb));
41 ospf_lsdb_init (new);
43 return new;
46 void
47 ospf_lsdb_init (struct ospf_lsdb *lsdb)
49 int i;
51 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
52 lsdb->type[i].db = route_table_init ();
55 void
56 ospf_lsdb_free (struct ospf_lsdb *lsdb)
58 ospf_lsdb_cleanup (lsdb);
59 XFREE (MTYPE_OSPF_LSDB, lsdb);
62 void
63 ospf_lsdb_cleanup (struct ospf_lsdb *lsdb)
65 int i;
66 assert (lsdb);
67 assert (lsdb->total == 0);
69 ospf_lsdb_delete_all (lsdb);
71 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
72 route_table_finish (lsdb->type[i].db);
75 static void
76 lsdb_prefix_set (struct prefix_ls *lp, struct ospf_lsa *lsa)
78 lp->family = 0;
79 lp->prefixlen = 64;
80 lp->id = lsa->data->id;
81 lp->adv_router = lsa->data->adv_router;
84 static void
85 ospf_lsdb_delete_entry (struct ospf_lsdb *lsdb, struct route_node *rn)
87 struct ospf_lsa *lsa = rn->info;
89 if (!lsa)
90 return;
92 assert (rn->table == lsdb->type[lsa->data->type].db);
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;
109 /* Add new LSA to lsdb. */
110 void
111 ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
113 struct route_table *table;
114 struct prefix_ls lp;
115 struct route_node *rn;
117 table = lsdb->type[lsa->data->type].db;
118 lsdb_prefix_set (&lp, lsa);
119 rn = route_node_get (table, (struct prefix *)&lp);
121 /* nothing to do? */
122 if (rn->info && rn->info == lsa)
123 return;
125 /* purge old entry? */
126 if (rn->info)
127 ospf_lsdb_delete_entry (lsdb, rn);
129 if (IS_LSA_SELF (lsa))
130 lsdb->type[lsa->data->type].count_self++;
131 lsdb->type[lsa->data->type].count++;
132 lsdb->total++;
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 */
142 void
143 ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
145 struct route_table *table;
146 struct prefix_ls lp;
147 struct route_node *rn;
149 if (!lsdb)
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;
159 if (!lsa)
161 zlog_warn ("%s: Called with NULL LSA", __func__);
162 return;
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))
170 ospf_lsdb_delete_entry (lsdb, rn);
171 route_unlock_node (rn); /* route_node_lookup */
175 void
176 ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
178 struct route_table *table;
179 struct route_node *rn;
180 int i;
182 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
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);
191 void
192 ospf_lsdb_clean_stat (struct ospf_lsdb *lsdb)
194 struct route_table *table;
195 struct route_node *rn;
196 struct ospf_lsa *lsa;
197 int i;
199 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
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;
208 struct ospf_lsa *
209 ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
211 struct route_table *table;
212 struct prefix_ls lp;
213 struct route_node *rn;
214 struct ospf_lsa *find;
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)
221 find = rn->info;
222 route_unlock_node (rn);
223 return find;
225 return NULL;
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)
232 struct route_table *table;
233 struct prefix_ls lp;
234 struct route_node *rn;
235 struct ospf_lsa *find;
237 table = lsdb->type[type].db;
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;
245 rn = route_node_lookup (table, (struct prefix *) &lp);
246 if (rn)
248 find = rn->info;
249 route_unlock_node (rn);
250 return find;
252 return NULL;
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)
260 struct route_table *table;
261 struct prefix_ls lp;
262 struct route_node *rn;
263 struct ospf_lsa *find;
265 table = lsdb->type[type].db;
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;
273 if (first)
274 rn = route_top (table);
275 else
277 rn = route_node_get (table, (struct prefix *) &lp);
278 rn = route_next (rn);
281 for (; rn; rn = route_next (rn))
282 if (rn->info)
283 break;
285 if (rn && rn->info)
287 find = rn->info;
288 route_unlock_node (rn);
289 return find;
291 return NULL;
294 unsigned long
295 ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
297 return lsdb->total;
300 unsigned long
301 ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
303 return lsdb->type[type].count;
306 unsigned long
307 ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
309 return lsdb->type[type].count_self;
312 unsigned int
313 ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
315 return lsdb->type[type].checksum;
318 unsigned long
319 ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
321 return (lsdb->total == 0);