ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / ospfd / ospf_lsdb.c
blob2e1bdedffe973896d8930d9fb0d4f5ba34b8404b
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 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;
85 static void
86 ospf_lsdb_delete_entry (struct ospf_lsdb *lsdb, struct route_node *rn)
88 struct ospf_lsa *lsa = rn->info;
90 if (!lsa)
91 return;
93 assert (rn->table == lsdb->type[lsa->data->type].db);
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;
110 /* Add new LSA to lsdb. */
111 void
112 ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
114 struct route_table *table;
115 struct prefix_ls lp;
116 struct route_node *rn;
118 table = lsdb->type[lsa->data->type].db;
119 lsdb_prefix_set (&lp, lsa);
120 rn = route_node_get (table, (struct prefix *)&lp);
122 /* nothing to do? */
123 if (rn->info && rn->info == lsa)
124 return;
126 /* purge old entry? */
127 if (rn->info)
128 ospf_lsdb_delete_entry (lsdb, rn);
130 if (IS_LSA_SELF (lsa))
131 lsdb->type[lsa->data->type].count_self++;
132 lsdb->type[lsa->data->type].count++;
133 lsdb->total++;
135 #ifdef MONITOR_LSDB_CHANGE
136 if (lsdb->new_lsa_hook != NULL)
137 (* lsdb->new_lsa_hook)(lsa);
138 #endif /* MONITOR_LSDB_CHANGE */
139 lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
140 rn->info = ospf_lsa_lock (lsa); /* lsdb */
143 void
144 ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
146 struct route_table *table;
147 struct prefix_ls lp;
148 struct route_node *rn;
150 if (!lsdb)
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;
160 if (!lsa)
162 zlog_warn ("%s: Called with NULL LSA", __func__);
163 return;
166 table = lsdb->type[lsa->data->type].db;
167 lsdb_prefix_set (&lp, lsa);
168 rn = route_node_lookup (table, (struct prefix *) &lp);
169 if (rn && (rn->info == lsa))
171 ospf_lsdb_delete_entry (lsdb, rn);
172 route_unlock_node (rn); /* route_node_lookup */
176 void
177 ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
179 struct route_table *table;
180 struct route_node *rn;
181 int i;
183 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
185 table = lsdb->type[i].db;
186 for (rn = route_top (table); rn; rn = route_next (rn))
187 if (rn->info != NULL)
188 ospf_lsdb_delete_entry (lsdb, rn);
192 void
193 ospf_lsdb_clean_stat (struct ospf_lsdb *lsdb)
195 struct route_table *table;
196 struct route_node *rn;
197 struct ospf_lsa *lsa;
198 int i;
200 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
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;
209 struct ospf_lsa *
210 ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
212 struct route_table *table;
213 struct prefix_ls lp;
214 struct route_node *rn;
215 struct ospf_lsa *find;
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)
222 find = rn->info;
223 route_unlock_node (rn);
224 return find;
226 return NULL;
229 struct ospf_lsa *
230 ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type,
231 struct in_addr id, struct in_addr adv_router)
233 struct route_table *table;
234 struct prefix_ls lp;
235 struct route_node *rn;
236 struct ospf_lsa *find;
238 table = lsdb->type[type].db;
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;
246 rn = route_node_lookup (table, (struct prefix *) &lp);
247 if (rn)
249 find = rn->info;
250 route_unlock_node (rn);
251 return find;
253 return NULL;
256 struct ospf_lsa *
257 ospf_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)
261 struct route_table *table;
262 struct prefix_ls lp;
263 struct route_node *rn;
264 struct ospf_lsa *find;
266 table = lsdb->type[type].db;
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;
274 if (first)
275 rn = route_top (table);
276 else
278 rn = route_node_get (table, (struct prefix *) &lp);
279 rn = route_next (rn);
282 for (; rn; rn = route_next (rn))
283 if (rn->info)
284 break;
286 if (rn && rn->info)
288 find = rn->info;
289 route_unlock_node (rn);
290 return find;
292 return NULL;
295 unsigned long
296 ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
298 return lsdb->total;
301 unsigned long
302 ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
304 return lsdb->type[type].count;
307 unsigned long
308 ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
310 return lsdb->type[type].count_self;
313 unsigned int
314 ospf_lsdb_checksum (struct ospf_lsdb *lsdb, int type)
316 return lsdb->type[type].checksum;
319 unsigned long
320 ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
322 return (lsdb->total == 0);