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"
38 struct ospf_lsdb
*new;
40 new = XCALLOC (MTYPE_OSPF_LSDB
, sizeof (struct ospf_lsdb
));
47 ospf_lsdb_init (struct ospf_lsdb
*lsdb
)
51 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
52 lsdb
->type
[i
].db
= route_table_init ();
56 ospf_lsdb_free (struct ospf_lsdb
*lsdb
)
58 ospf_lsdb_cleanup (lsdb
);
59 XFREE (MTYPE_OSPF_LSDB
, lsdb
);
63 ospf_lsdb_cleanup (struct ospf_lsdb
*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
);
76 lsdb_prefix_set (struct prefix_ls
*lp
, struct ospf_lsa
*lsa
)
78 memset (lp
, 0, sizeof (struct prefix_ls
));
81 lp
->id
= lsa
->data
->id
;
82 lp
->adv_router
= lsa
->data
->adv_router
;
85 /* Add new LSA to lsdb. */
87 ospf_lsdb_add (struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
89 struct route_table
*table
;
91 struct route_node
*rn
;
93 table
= lsdb
->type
[lsa
->data
->type
].db
;
94 lsdb_prefix_set (&lp
, lsa
);
95 rn
= route_node_get (table
, (struct prefix
*)&lp
);
98 if (IS_LSA_SELF (lsa
))
99 lsdb
->type
[lsa
->data
->type
].count_self
++;
100 lsdb
->type
[lsa
->data
->type
].count
++;
108 ospf_lsa_unlock (rn
->info
);
109 route_unlock_node (rn
);
112 #ifdef MONITOR_LSDB_CHANGE
113 if (lsdb
->new_lsa_hook
!= NULL
)
114 (* lsdb
->new_lsa_hook
)(lsa
);
115 #endif /* MONITOR_LSDB_CHANGE */
116 rn
->info
= ospf_lsa_lock (lsa
);
120 ospf_lsdb_delete (struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
122 struct route_table
*table
;
124 struct route_node
*rn
;
126 table
= lsdb
->type
[lsa
->data
->type
].db
;
127 lsdb_prefix_set (&lp
, lsa
);
128 rn
= route_node_lookup (table
, (struct prefix
*) &lp
);
132 if (IS_LSA_SELF (lsa
))
133 lsdb
->type
[lsa
->data
->type
].count_self
--;
134 lsdb
->type
[lsa
->data
->type
].count
--;
137 route_unlock_node (rn
);
138 route_unlock_node (rn
);
139 #ifdef MONITOR_LSDB_CHANGE
140 if (lsdb
->del_lsa_hook
!= NULL
)
141 (* lsdb
->del_lsa_hook
)(lsa
);
142 #endif /* MONITOR_LSDB_CHANGE */
143 ospf_lsa_unlock (lsa
);
149 ospf_lsdb_delete_all (struct ospf_lsdb
*lsdb
)
151 struct route_table
*table
;
152 struct route_node
*rn
;
153 struct ospf_lsa
*lsa
;
156 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
158 table
= lsdb
->type
[i
].db
;
159 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
160 if ((lsa
= (rn
->info
)) != NULL
)
162 if (IS_LSA_SELF (lsa
))
163 lsdb
->type
[i
].count_self
--;
164 lsdb
->type
[i
].count
--;
167 route_unlock_node (rn
);
168 #ifdef MONITOR_LSDB_CHANGE
169 if (lsdb
->del_lsa_hook
!= NULL
)
170 (* lsdb
->del_lsa_hook
)(lsa
);
171 #endif /* MONITOR_LSDB_CHANGE */
172 ospf_lsa_unlock (lsa
);
178 ospf_lsdb_lookup (struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
180 struct route_table
*table
;
182 struct route_node
*rn
;
183 struct ospf_lsa
*find
;
185 table
= lsdb
->type
[lsa
->data
->type
].db
;
186 lsdb_prefix_set (&lp
, lsa
);
187 rn
= route_node_lookup (table
, (struct prefix
*) &lp
);
191 route_unlock_node (rn
);
198 ospf_lsdb_lookup_by_id (struct ospf_lsdb
*lsdb
, u_char type
,
199 struct in_addr id
, struct in_addr adv_router
)
201 struct route_table
*table
;
203 struct route_node
*rn
;
204 struct ospf_lsa
*find
;
206 table
= lsdb
->type
[type
].db
;
208 memset (&lp
, 0, sizeof (struct prefix_ls
));
212 lp
.adv_router
= adv_router
;
214 rn
= route_node_lookup (table
, (struct prefix
*) &lp
);
218 route_unlock_node (rn
);
225 ospf_lsdb_lookup_by_id_next (struct ospf_lsdb
*lsdb
, u_char type
,
226 struct in_addr id
, struct in_addr adv_router
,
229 struct route_table
*table
;
231 struct route_node
*rn
;
232 struct ospf_lsa
*find
;
234 table
= lsdb
->type
[type
].db
;
236 memset (&lp
, 0, sizeof (struct prefix_ls
));
240 lp
.adv_router
= adv_router
;
243 rn
= route_top (table
);
246 rn
= route_node_get (table
, (struct prefix
*) &lp
);
247 rn
= route_next (rn
);
250 for (; rn
; rn
= route_next (rn
))
257 route_unlock_node (rn
);
264 ospf_lsdb_count_all (struct ospf_lsdb
*lsdb
)
270 ospf_lsdb_count (struct ospf_lsdb
*lsdb
, int type
)
272 return lsdb
->type
[type
].count
;
276 ospf_lsdb_count_self (struct ospf_lsdb
*lsdb
, int type
)
278 return lsdb
->type
[type
].count_self
;
282 ospf_lsdb_isempty (struct ospf_lsdb
*lsdb
)
284 return (lsdb
->total
== 0);