]>
git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_lsdb.c
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
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
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
*ospf_lsdb_new()
37 struct ospf_lsdb
*new;
39 new = XCALLOC(MTYPE_OSPF_LSDB
, sizeof(struct ospf_lsdb
));
45 void ospf_lsdb_init(struct ospf_lsdb
*lsdb
)
49 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
50 lsdb
->type
[i
].db
= route_table_init();
53 void ospf_lsdb_free(struct ospf_lsdb
*lsdb
)
55 ospf_lsdb_cleanup(lsdb
);
56 XFREE(MTYPE_OSPF_LSDB
, lsdb
);
59 void ospf_lsdb_cleanup(struct ospf_lsdb
*lsdb
)
63 assert(lsdb
->total
== 0);
65 ospf_lsdb_delete_all(lsdb
);
67 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
68 route_table_finish(lsdb
->type
[i
].db
);
71 void ls_prefix_set(struct prefix_ls
*lp
, struct ospf_lsa
*lsa
)
73 if (lp
&& lsa
&& lsa
->data
) {
76 lp
->id
= lsa
->data
->id
;
77 lp
->adv_router
= lsa
->data
->adv_router
;
81 static void ospf_lsdb_delete_entry(struct ospf_lsdb
*lsdb
,
82 struct route_node
*rn
)
84 struct ospf_lsa
*lsa
= rn
->info
;
89 assert(rn
->table
== lsdb
->type
[lsa
->data
->type
].db
);
92 lsdb
->type
[lsa
->data
->type
].count_self
--;
93 lsdb
->type
[lsa
->data
->type
].count
--;
94 lsdb
->type
[lsa
->data
->type
].checksum
-= ntohs(lsa
->data
->checksum
);
97 route_unlock_node(rn
);
98 #ifdef MONITOR_LSDB_CHANGE
99 if (lsdb
->del_lsa_hook
!= NULL
)
100 (*lsdb
->del_lsa_hook
)(lsa
);
101 #endif /* MONITOR_LSDB_CHANGE */
102 ospf_lsa_unlock(&lsa
); /* lsdb */
106 /* Add new LSA to lsdb. */
107 void ospf_lsdb_add(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
109 struct route_table
*table
;
111 struct route_node
*rn
;
113 table
= lsdb
->type
[lsa
->data
->type
].db
;
114 ls_prefix_set(&lp
, lsa
);
115 rn
= route_node_get(table
, (struct prefix
*)&lp
);
118 if (rn
->info
&& rn
->info
== lsa
) {
119 route_unlock_node(rn
);
123 /* purge old entry? */
125 ospf_lsdb_delete_entry(lsdb
, rn
);
127 if (IS_LSA_SELF(lsa
))
128 lsdb
->type
[lsa
->data
->type
].count_self
++;
129 lsdb
->type
[lsa
->data
->type
].count
++;
132 #ifdef MONITOR_LSDB_CHANGE
133 if (lsdb
->new_lsa_hook
!= NULL
)
134 (*lsdb
->new_lsa_hook
)(lsa
);
135 #endif /* MONITOR_LSDB_CHANGE */
136 lsdb
->type
[lsa
->data
->type
].checksum
+= ntohs(lsa
->data
->checksum
);
137 rn
->info
= ospf_lsa_lock(lsa
); /* lsdb */
140 void ospf_lsdb_delete(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
142 struct route_table
*table
;
144 struct route_node
*rn
;
147 zlog_warn("%s: Called with NULL LSDB", __func__
);
149 zlog_warn("LSA[Type%d:%s]: LSA %p, lsa->lsdb %p",
150 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
151 (void *)lsa
, (void *)lsa
->lsdb
);
156 zlog_warn("%s: Called with NULL LSA", __func__
);
160 assert(lsa
->data
->type
< OSPF_MAX_LSA
);
161 table
= lsdb
->type
[lsa
->data
->type
].db
;
162 ls_prefix_set(&lp
, lsa
);
163 if ((rn
= route_node_lookup(table
, (struct prefix
*)&lp
))) {
165 ospf_lsdb_delete_entry(lsdb
, rn
);
166 route_unlock_node(rn
); /* route_node_lookup */
170 void ospf_lsdb_delete_all(struct ospf_lsdb
*lsdb
)
172 struct route_table
*table
;
173 struct route_node
*rn
;
176 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
177 table
= lsdb
->type
[i
].db
;
178 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
179 if (rn
->info
!= NULL
)
180 ospf_lsdb_delete_entry(lsdb
, rn
);
184 void ospf_lsdb_clean_stat(struct ospf_lsdb
*lsdb
)
186 struct route_table
*table
;
187 struct route_node
*rn
;
188 struct ospf_lsa
*lsa
;
191 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
192 table
= lsdb
->type
[i
].db
;
193 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
194 if ((lsa
= (rn
->info
)) != NULL
)
195 lsa
->stat
= LSA_SPF_NOT_EXPLORED
;
199 struct ospf_lsa
*ospf_lsdb_lookup(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
201 struct route_table
*table
;
203 struct route_node
*rn
;
204 struct ospf_lsa
*find
;
206 table
= lsdb
->type
[lsa
->data
->type
].db
;
207 ls_prefix_set(&lp
, lsa
);
208 rn
= route_node_lookup(table
, (struct prefix
*)&lp
);
211 route_unlock_node(rn
);
217 struct ospf_lsa
*ospf_lsdb_lookup_by_id(struct ospf_lsdb
*lsdb
, u_char type
,
219 struct in_addr adv_router
)
221 struct route_table
*table
;
223 struct route_node
*rn
;
224 struct ospf_lsa
*find
;
226 table
= lsdb
->type
[type
].db
;
228 memset(&lp
, 0, sizeof(struct prefix_ls
));
232 lp
.adv_router
= adv_router
;
234 rn
= route_node_lookup(table
, (struct prefix
*)&lp
);
237 route_unlock_node(rn
);
243 struct ospf_lsa
*ospf_lsdb_lookup_by_id_next(struct ospf_lsdb
*lsdb
,
244 u_char type
, struct in_addr id
,
245 struct in_addr adv_router
,
248 struct route_table
*table
;
250 struct route_node
*rn
;
251 struct ospf_lsa
*find
;
253 table
= lsdb
->type
[type
].db
;
255 memset(&lp
, 0, sizeof(struct prefix_ls
));
259 lp
.adv_router
= adv_router
;
262 rn
= route_top(table
);
264 if ((rn
= route_node_lookup(table
, (struct prefix
*)&lp
))
270 for (; rn
; rn
= route_next(rn
))
274 if (rn
&& rn
->info
) {
276 route_unlock_node(rn
);
282 unsigned long ospf_lsdb_count_all(struct ospf_lsdb
*lsdb
)
287 unsigned long ospf_lsdb_count(struct ospf_lsdb
*lsdb
, int type
)
289 return lsdb
->type
[type
].count
;
292 unsigned long ospf_lsdb_count_self(struct ospf_lsdb
*lsdb
, int type
)
294 return lsdb
->type
[type
].count_self
;
297 unsigned int ospf_lsdb_checksum(struct ospf_lsdb
*lsdb
, int type
)
299 return lsdb
->type
[type
].checksum
;
302 unsigned long ospf_lsdb_isempty(struct ospf_lsdb
*lsdb
)
304 return (lsdb
->total
== 0);