2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "ospf6_proto.h"
31 #include "ospf6_lsa.h"
32 #include "ospf6_lsdb.h"
33 #include "ospf6_route.h"
37 struct ospf6_lsdb
*ospf6_lsdb_create(void *data
)
39 struct ospf6_lsdb
*lsdb
;
41 lsdb
= XCALLOC(MTYPE_OSPF6_LSDB
, sizeof(struct ospf6_lsdb
));
42 memset(lsdb
, 0, sizeof(struct ospf6_lsdb
));
45 lsdb
->table
= route_table_init();
49 void ospf6_lsdb_delete(struct ospf6_lsdb
*lsdb
)
52 ospf6_lsdb_remove_all(lsdb
);
53 route_table_finish(lsdb
->table
);
54 XFREE(MTYPE_OSPF6_LSDB
, lsdb
);
58 static void ospf6_lsdb_set_key(struct prefix_ipv6
*key
, const void *value
,
61 assert(key
->prefixlen
% 8 == 0);
63 memcpy((caddr_t
)&key
->prefix
+ key
->prefixlen
/ 8, (caddr_t
)value
, len
);
64 key
->family
= AF_INET6
;
65 key
->prefixlen
+= len
* 8;
69 static void _lsdb_count_assert(struct ospf6_lsdb
*lsdb
)
71 struct ospf6_lsa
*debug
, *debugnext
;
73 for (ALL_LSDB(lsdb
, debug
, debugnext
))
76 if (num
== lsdb
->count
)
79 zlog_debug("PANIC !! lsdb[%p]->count = %d, real = %d", lsdb
,
81 for (ALL_LSDB(lsdb
, debug
, debugnext
))
82 zlog_debug("%s lsdb[%p]", debug
->name
, debug
->lsdb
);
83 zlog_debug("DUMP END");
85 assert(num
== lsdb
->count
);
87 #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t))
89 #define ospf6_lsdb_count_assert(t) ((void) 0)
92 void ospf6_lsdb_add(struct ospf6_lsa
*lsa
, struct ospf6_lsdb
*lsdb
)
94 struct prefix_ipv6 key
;
95 struct route_node
*current
;
96 struct ospf6_lsa
*old
= NULL
;
98 memset(&key
, 0, sizeof(key
));
99 ospf6_lsdb_set_key(&key
, &lsa
->header
->type
, sizeof(lsa
->header
->type
));
100 ospf6_lsdb_set_key(&key
, &lsa
->header
->adv_router
,
101 sizeof(lsa
->header
->adv_router
));
102 ospf6_lsdb_set_key(&key
, &lsa
->header
->id
, sizeof(lsa
->header
->id
));
104 current
= route_node_get(lsdb
->table
, (struct prefix
*)&key
);
113 if (OSPF6_LSA_IS_MAXAGE(lsa
)) {
114 if (lsdb
->hook_remove
)
115 (*lsdb
->hook_remove
)(lsa
);
118 (*lsdb
->hook_add
)(lsa
);
121 if (OSPF6_LSA_IS_CHANGED(old
, lsa
)) {
122 if (OSPF6_LSA_IS_MAXAGE(lsa
)) {
123 if (lsdb
->hook_remove
) {
124 (*lsdb
->hook_remove
)(old
);
125 (*lsdb
->hook_remove
)(lsa
);
127 } else if (OSPF6_LSA_IS_MAXAGE(old
)) {
129 (*lsdb
->hook_add
)(lsa
);
131 if (lsdb
->hook_remove
)
132 (*lsdb
->hook_remove
)(old
);
134 (*lsdb
->hook_add
)(lsa
);
137 /* to free the lookup lock in node get*/
138 route_unlock_node(current
);
139 ospf6_lsa_unlock(old
);
142 ospf6_lsdb_count_assert(lsdb
);
145 void ospf6_lsdb_remove(struct ospf6_lsa
*lsa
, struct ospf6_lsdb
*lsdb
)
147 struct route_node
*node
;
148 struct prefix_ipv6 key
;
150 memset(&key
, 0, sizeof(key
));
151 ospf6_lsdb_set_key(&key
, &lsa
->header
->type
, sizeof(lsa
->header
->type
));
152 ospf6_lsdb_set_key(&key
, &lsa
->header
->adv_router
,
153 sizeof(lsa
->header
->adv_router
));
154 ospf6_lsdb_set_key(&key
, &lsa
->header
->id
, sizeof(lsa
->header
->id
));
156 node
= route_node_lookup(lsdb
->table
, (struct prefix
*)&key
);
157 assert(node
&& node
->info
== lsa
);
162 if (lsdb
->hook_remove
)
163 (*lsdb
->hook_remove
)(lsa
);
165 route_unlock_node(node
); /* to free the lookup lock */
166 route_unlock_node(node
); /* to free the original lock */
167 ospf6_lsa_unlock(lsa
);
169 ospf6_lsdb_count_assert(lsdb
);
172 struct ospf6_lsa
*ospf6_lsdb_lookup(uint16_t type
, uint32_t id
,
174 struct ospf6_lsdb
*lsdb
)
176 struct route_node
*node
;
177 struct prefix_ipv6 key
;
182 memset(&key
, 0, sizeof(key
));
183 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
184 ospf6_lsdb_set_key(&key
, &adv_router
, sizeof(adv_router
));
185 ospf6_lsdb_set_key(&key
, &id
, sizeof(id
));
187 node
= route_node_lookup(lsdb
->table
, (struct prefix
*)&key
);
188 if (node
== NULL
|| node
->info
== NULL
)
191 route_unlock_node(node
);
192 return (struct ospf6_lsa
*)node
->info
;
195 struct ospf6_lsa
*ospf6_lsdb_lookup_next(uint16_t type
, uint32_t id
,
197 struct ospf6_lsdb
*lsdb
)
199 struct route_node
*node
;
200 struct prefix_ipv6 key
;
205 memset(&key
, 0, sizeof(key
));
206 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
207 ospf6_lsdb_set_key(&key
, &adv_router
, sizeof(adv_router
));
208 ospf6_lsdb_set_key(&key
, &id
, sizeof(id
));
210 zlog_debug("lsdb_lookup_next: key: %pFX", &key
);
212 node
= route_table_get_next(lsdb
->table
, &key
);
214 /* skip to real existing entry */
215 while (node
&& node
->info
== NULL
)
216 node
= route_next(node
);
221 route_unlock_node(node
);
225 return (struct ospf6_lsa
*)node
->info
;
228 const struct route_node
*ospf6_lsdb_head(struct ospf6_lsdb
*lsdb
, int argmode
,
229 uint16_t type
, uint32_t adv_router
,
230 struct ospf6_lsa
**lsa
)
232 struct route_node
*node
, *end
;
237 struct prefix_ipv6 key
= {.family
= AF_INET6
, .prefixlen
= 0};
239 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
241 ospf6_lsdb_set_key(&key
, &adv_router
,
244 node
= route_table_get_next(lsdb
->table
, &key
);
245 if (!node
|| !prefix_match((struct prefix
*)&key
, &node
->p
))
248 for (end
= node
; end
&& end
->parent
249 && end
->parent
->p
.prefixlen
>= key
.prefixlen
;
253 node
= route_top(lsdb
->table
);
257 while (node
&& !node
->info
)
258 node
= route_next_until(node
, end
);
263 route_unlock_node(node
);
268 ospf6_lsa_lock(*lsa
);
273 struct ospf6_lsa
*ospf6_lsdb_next(const struct route_node
*iterend
,
274 struct ospf6_lsa
*lsa
)
276 struct route_node
*node
= lsa
->rn
;
278 ospf6_lsa_unlock(lsa
);
281 node
= route_next_until(node
, iterend
);
282 while (node
&& !node
->info
);
284 if (node
&& node
->info
) {
285 struct ospf6_lsa
*next
= node
->info
;
286 ospf6_lsa_lock(next
);
291 route_unlock_node(node
);
295 void ospf6_lsdb_remove_all(struct ospf6_lsdb
*lsdb
)
297 struct ospf6_lsa
*lsa
, *lsanext
;
302 for (ALL_LSDB(lsdb
, lsa
, lsanext
))
303 ospf6_lsdb_remove(lsa
, lsdb
);
306 void ospf6_lsdb_lsa_unlock(struct ospf6_lsa
*lsa
)
310 route_unlock_node(lsa
->rn
);
311 ospf6_lsa_unlock(lsa
);
315 int ospf6_lsdb_maxage_remover(struct ospf6_lsdb
*lsdb
)
318 struct ospf6_lsa
*lsa
, *lsanext
;
320 for (ALL_LSDB(lsdb
, lsa
, lsanext
)) {
321 if (!OSPF6_LSA_IS_MAXAGE(lsa
))
323 if (lsa
->retrans_count
!= 0) {
327 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
))
328 zlog_debug("Remove MaxAge %s", lsa
->name
);
330 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_SEQWRAPPED
)) {
331 UNSET_FLAG(lsa
->flag
, OSPF6_LSA_SEQWRAPPED
);
333 * lsa->header->age = 0;
335 lsa
->header
->seqnum
=
336 htonl(OSPF_MAX_SEQUENCE_NUMBER
+ 1);
337 ospf6_lsa_checksum(lsa
->header
);
339 THREAD_OFF(lsa
->refresh
);
340 thread_execute(master
, ospf6_lsa_refresh
, lsa
, 0);
342 ospf6_lsdb_remove(lsa
, lsdb
);
349 void ospf6_lsdb_show(struct vty
*vty
, enum ospf_lsdb_show_level level
,
350 uint16_t *type
, uint32_t *id
, uint32_t *adv_router
,
351 struct ospf6_lsdb
*lsdb
)
353 struct ospf6_lsa
*lsa
;
354 const struct route_node
*end
= NULL
;
355 void (*showfunc
)(struct vty
*, struct ospf6_lsa
*) = NULL
;
358 case OSPF6_LSDB_SHOW_LEVEL_DETAIL
:
359 showfunc
= ospf6_lsa_show
;
361 case OSPF6_LSDB_SHOW_LEVEL_INTERNAL
:
362 showfunc
= ospf6_lsa_show_internal
;
364 case OSPF6_LSDB_SHOW_LEVEL_DUMP
:
365 showfunc
= ospf6_lsa_show_dump
;
367 case OSPF6_LSDB_SHOW_LEVEL_NORMAL
:
369 showfunc
= ospf6_lsa_show_summary
;
372 if (type
&& id
&& adv_router
) {
373 lsa
= ospf6_lsdb_lookup(*type
, *id
, *adv_router
, lsdb
);
375 if (level
== OSPF6_LSDB_SHOW_LEVEL_NORMAL
)
376 ospf6_lsa_show(vty
, lsa
);
378 (*showfunc
)(vty
, lsa
);
383 if (level
== OSPF6_LSDB_SHOW_LEVEL_NORMAL
)
384 ospf6_lsa_show_summary_header(vty
);
386 end
= ospf6_lsdb_head(lsdb
, !!type
+ !!(type
&& adv_router
),
387 type
? *type
: 0, adv_router
? *adv_router
: 0,
390 if ((!adv_router
|| lsa
->header
->adv_router
== *adv_router
)
391 && (!id
|| lsa
->header
->id
== *id
))
392 (*showfunc
)(vty
, lsa
);
394 lsa
= ospf6_lsdb_next(end
, lsa
);
398 uint32_t ospf6_new_ls_id(uint16_t type
, uint32_t adv_router
,
399 struct ospf6_lsdb
*lsdb
)
401 struct ospf6_lsa
*lsa
;
402 uint32_t id
= 1, tmp_id
;
404 /* This routine is curently invoked only for Inter-Prefix LSAs for
405 * non-summarized routes (no area/range).
407 for (ALL_LSDB_TYPED_ADVRTR(lsdb
, type
, adv_router
, lsa
)) {
408 tmp_id
= ntohl(lsa
->header
->id
);
413 ospf6_lsdb_lsa_unlock(lsa
);
419 return ((uint32_t)htonl(id
));
422 /* Decide new LS sequence number to originate.
423 note return value is network byte order */
424 uint32_t ospf6_new_ls_seqnum(uint16_t type
, uint32_t id
, uint32_t adv_router
,
425 struct ospf6_lsdb
*lsdb
)
427 struct ospf6_lsa
*lsa
;
428 signed long seqnum
= 0;
430 /* if current database copy not found, return InitialSequenceNumber */
431 lsa
= ospf6_lsdb_lookup(type
, id
, adv_router
, lsdb
);
433 seqnum
= OSPF_INITIAL_SEQUENCE_NUMBER
;
435 seqnum
= (signed long)ntohl(lsa
->header
->seqnum
) + 1;
437 return ((uint32_t)htonl(seqnum
));