]>
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "ospfd/ospfd.h"
30 #include "ospfd/ospf_asbr.h"
31 #include "ospfd/ospf_lsa.h"
32 #include "ospfd/ospf_lsdb.h"
34 struct ospf_lsdb
*ospf_lsdb_new(void)
36 struct ospf_lsdb
*new;
38 new = XCALLOC(MTYPE_OSPF_LSDB
, sizeof(struct ospf_lsdb
));
44 void ospf_lsdb_init(struct ospf_lsdb
*lsdb
)
48 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
49 lsdb
->type
[i
].db
= route_table_init();
52 void ospf_lsdb_free(struct ospf_lsdb
*lsdb
)
54 ospf_lsdb_cleanup(lsdb
);
55 XFREE(MTYPE_OSPF_LSDB
, lsdb
);
58 void ospf_lsdb_cleanup(struct ospf_lsdb
*lsdb
)
62 assert(lsdb
->total
== 0);
64 ospf_lsdb_delete_all(lsdb
);
66 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
67 route_table_finish(lsdb
->type
[i
].db
);
70 void ls_prefix_set(struct prefix_ls
*lp
, struct ospf_lsa
*lsa
)
72 if (lp
&& lsa
&& lsa
->data
) {
73 lp
->family
= AF_UNSPEC
;
75 lp
->id
= lsa
->data
->id
;
76 lp
->adv_router
= lsa
->data
->adv_router
;
80 static void ospf_lsdb_delete_entry(struct ospf_lsdb
*lsdb
,
81 struct route_node
*rn
)
83 struct ospf_lsa
*lsa
= rn
->info
;
88 assert(rn
->table
== lsdb
->type
[lsa
->data
->type
].db
);
91 lsdb
->type
[lsa
->data
->type
].count_self
--;
92 lsdb
->type
[lsa
->data
->type
].count
--;
93 lsdb
->type
[lsa
->data
->type
].checksum
-= ntohs(lsa
->data
->checksum
);
96 route_unlock_node(rn
);
97 #ifdef MONITOR_LSDB_CHANGE
98 if (lsdb
->del_lsa_hook
!= NULL
)
99 (*lsdb
->del_lsa_hook
)(lsa
);
100 #endif /* MONITOR_LSDB_CHANGE */
101 ospf_lsa_unlock(&lsa
); /* lsdb */
105 /* Add new LSA to lsdb. */
106 void ospf_lsdb_add(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
108 struct route_table
*table
;
110 struct route_node
*rn
;
112 table
= lsdb
->type
[lsa
->data
->type
].db
;
113 ls_prefix_set(&lp
, lsa
);
114 rn
= route_node_get(table
, (struct prefix
*)&lp
);
117 if (rn
->info
&& rn
->info
== lsa
) {
118 route_unlock_node(rn
);
122 /* purge old entry? */
124 ospf_lsdb_delete_entry(lsdb
, rn
);
126 if (IS_LSA_SELF(lsa
))
127 lsdb
->type
[lsa
->data
->type
].count_self
++;
128 lsdb
->type
[lsa
->data
->type
].count
++;
131 #ifdef MONITOR_LSDB_CHANGE
132 if (lsdb
->new_lsa_hook
!= NULL
)
133 (*lsdb
->new_lsa_hook
)(lsa
);
134 #endif /* MONITOR_LSDB_CHANGE */
135 lsdb
->type
[lsa
->data
->type
].checksum
+= ntohs(lsa
->data
->checksum
);
136 rn
->info
= ospf_lsa_lock(lsa
); /* lsdb */
139 void ospf_lsdb_delete(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
141 struct route_table
*table
;
143 struct route_node
*rn
;
148 assert(lsa
->data
->type
< OSPF_MAX_LSA
);
149 table
= lsdb
->type
[lsa
->data
->type
].db
;
150 ls_prefix_set(&lp
, lsa
);
151 if ((rn
= route_node_lookup(table
, (struct prefix
*)&lp
))) {
153 ospf_lsdb_delete_entry(lsdb
, rn
);
154 route_unlock_node(rn
); /* route_node_lookup */
158 void ospf_lsdb_delete_all(struct ospf_lsdb
*lsdb
)
160 struct route_table
*table
;
161 struct route_node
*rn
;
164 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
165 table
= lsdb
->type
[i
].db
;
166 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
167 if (rn
->info
!= NULL
)
168 ospf_lsdb_delete_entry(lsdb
, rn
);
172 struct ospf_lsa
*ospf_lsdb_lookup(struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
174 struct route_table
*table
;
176 struct route_node
*rn
;
177 struct ospf_lsa
*find
;
179 table
= lsdb
->type
[lsa
->data
->type
].db
;
180 ls_prefix_set(&lp
, lsa
);
181 rn
= route_node_lookup(table
, (struct prefix
*)&lp
);
184 route_unlock_node(rn
);
190 struct ospf_lsa
*ospf_lsdb_lookup_by_id(struct ospf_lsdb
*lsdb
, uint8_t type
,
192 struct in_addr adv_router
)
194 struct route_table
*table
;
196 struct route_node
*rn
;
197 struct ospf_lsa
*find
;
199 table
= lsdb
->type
[type
].db
;
201 memset(&lp
, 0, sizeof(lp
));
202 lp
.family
= AF_UNSPEC
;
205 lp
.adv_router
= adv_router
;
207 rn
= route_node_lookup(table
, (struct prefix
*)&lp
);
210 route_unlock_node(rn
);
216 struct ospf_lsa
*ospf_lsdb_lookup_by_id_next(struct ospf_lsdb
*lsdb
,
217 uint8_t type
, struct in_addr id
,
218 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(lp
));
229 lp
.family
= AF_UNSPEC
;
232 lp
.adv_router
= adv_router
;
235 rn
= route_top(table
);
237 if ((rn
= route_node_lookup(table
, (struct prefix
*)&lp
))
243 for (; rn
; rn
= route_next(rn
))
247 if (rn
&& rn
->info
) {
249 route_unlock_node(rn
);
255 unsigned long ospf_lsdb_count_all(struct ospf_lsdb
*lsdb
)
260 unsigned long ospf_lsdb_count(struct ospf_lsdb
*lsdb
, int type
)
262 return lsdb
->type
[type
].count
;
265 unsigned long ospf_lsdb_count_self(struct ospf_lsdb
*lsdb
, int type
)
267 return lsdb
->type
[type
].count_self
;
270 unsigned int ospf_lsdb_checksum(struct ospf_lsdb
*lsdb
, int type
)
272 return lsdb
->type
[type
].checksum
;
275 unsigned long ospf_lsdb_isempty(struct ospf_lsdb
*lsdb
)
277 return (lsdb
->total
== 0);