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
));
43 zlog_warn("Can't malloc lsdb");
46 memset(lsdb
, 0, sizeof(struct ospf6_lsdb
));
49 lsdb
->table
= route_table_init();
53 void ospf6_lsdb_delete(struct ospf6_lsdb
*lsdb
)
56 ospf6_lsdb_remove_all(lsdb
);
57 route_table_finish(lsdb
->table
);
58 XFREE(MTYPE_OSPF6_LSDB
, lsdb
);
62 static void ospf6_lsdb_set_key(struct prefix_ipv6
*key
, const void *value
,
65 assert(key
->prefixlen
% 8 == 0);
67 memcpy((caddr_t
)&key
->prefix
+ key
->prefixlen
/ 8, (caddr_t
)value
, len
);
68 key
->family
= AF_INET6
;
69 key
->prefixlen
+= len
* 8;
73 static void _lsdb_count_assert(struct ospf6_lsdb
*lsdb
)
75 struct ospf6_lsa
*debug
;
77 for (ALL_LSDB(lsdb
, debug
))
80 if (num
== lsdb
->count
)
83 zlog_debug("PANIC !! lsdb[%p]->count = %d, real = %d", lsdb
,
85 for (ALL_LSDB(lsdb
, debug
))
86 zlog_debug("%s lsdb[%p]", debug
->name
, debug
->lsdb
);
87 zlog_debug("DUMP END");
89 assert(num
== lsdb
->count
);
91 #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t))
93 #define ospf6_lsdb_count_assert(t) ((void) 0)
96 void ospf6_lsdb_add(struct ospf6_lsa
*lsa
, struct ospf6_lsdb
*lsdb
)
98 struct prefix_ipv6 key
;
99 struct route_node
*current
;
100 struct ospf6_lsa
*old
= NULL
;
102 memset(&key
, 0, sizeof(key
));
103 ospf6_lsdb_set_key(&key
, &lsa
->header
->type
, sizeof(lsa
->header
->type
));
104 ospf6_lsdb_set_key(&key
, &lsa
->header
->adv_router
,
105 sizeof(lsa
->header
->adv_router
));
106 ospf6_lsdb_set_key(&key
, &lsa
->header
->id
, sizeof(lsa
->header
->id
));
108 current
= route_node_get(lsdb
->table
, (struct prefix
*)&key
);
117 if (OSPF6_LSA_IS_MAXAGE(lsa
)) {
118 if (lsdb
->hook_remove
)
119 (*lsdb
->hook_remove
)(lsa
);
122 (*lsdb
->hook_add
)(lsa
);
125 if (OSPF6_LSA_IS_CHANGED(old
, lsa
)) {
126 if (OSPF6_LSA_IS_MAXAGE(lsa
)) {
127 if (lsdb
->hook_remove
) {
128 (*lsdb
->hook_remove
)(old
);
129 (*lsdb
->hook_remove
)(lsa
);
131 } else if (OSPF6_LSA_IS_MAXAGE(old
)) {
133 (*lsdb
->hook_add
)(lsa
);
135 if (lsdb
->hook_remove
)
136 (*lsdb
->hook_remove
)(old
);
138 (*lsdb
->hook_add
)(lsa
);
141 /* to free the lookup lock in node get*/
142 route_unlock_node(current
);
143 ospf6_lsa_unlock(old
);
146 ospf6_lsdb_count_assert(lsdb
);
149 void ospf6_lsdb_remove(struct ospf6_lsa
*lsa
, struct ospf6_lsdb
*lsdb
)
151 struct route_node
*node
;
152 struct prefix_ipv6 key
;
154 memset(&key
, 0, sizeof(key
));
155 ospf6_lsdb_set_key(&key
, &lsa
->header
->type
, sizeof(lsa
->header
->type
));
156 ospf6_lsdb_set_key(&key
, &lsa
->header
->adv_router
,
157 sizeof(lsa
->header
->adv_router
));
158 ospf6_lsdb_set_key(&key
, &lsa
->header
->id
, sizeof(lsa
->header
->id
));
160 node
= route_node_lookup(lsdb
->table
, (struct prefix
*)&key
);
161 assert(node
&& node
->info
== lsa
);
166 if (lsdb
->hook_remove
)
167 (*lsdb
->hook_remove
)(lsa
);
169 route_unlock_node(node
); /* to free the lookup lock */
170 route_unlock_node(node
); /* to free the original lock */
171 ospf6_lsa_unlock(lsa
);
173 ospf6_lsdb_count_assert(lsdb
);
176 struct ospf6_lsa
*ospf6_lsdb_lookup(uint16_t type
, uint32_t id
,
178 struct ospf6_lsdb
*lsdb
)
180 struct route_node
*node
;
181 struct prefix_ipv6 key
;
186 memset(&key
, 0, sizeof(key
));
187 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
188 ospf6_lsdb_set_key(&key
, &adv_router
, sizeof(adv_router
));
189 ospf6_lsdb_set_key(&key
, &id
, sizeof(id
));
191 node
= route_node_lookup(lsdb
->table
, (struct prefix
*)&key
);
192 if (node
== NULL
|| node
->info
== NULL
)
195 route_unlock_node(node
);
196 return (struct ospf6_lsa
*)node
->info
;
199 struct ospf6_lsa
*ospf6_lsdb_lookup_next(uint16_t type
, uint32_t id
,
201 struct ospf6_lsdb
*lsdb
)
203 struct route_node
*node
;
204 struct prefix_ipv6 key
;
209 memset(&key
, 0, sizeof(key
));
210 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
211 ospf6_lsdb_set_key(&key
, &adv_router
, sizeof(adv_router
));
212 ospf6_lsdb_set_key(&key
, &id
, sizeof(id
));
215 char buf
[PREFIX2STR_BUFFER
];
216 prefix2str(&key
, buf
, sizeof(buf
));
217 zlog_debug("lsdb_lookup_next: key: %s", buf
);
220 node
= route_table_get_next(lsdb
->table
, &key
);
222 /* skip to real existing entry */
223 while (node
&& node
->info
== NULL
)
224 node
= route_next(node
);
229 route_unlock_node(node
);
233 return (struct ospf6_lsa
*)node
->info
;
236 const struct route_node
*ospf6_lsdb_head(struct ospf6_lsdb
*lsdb
, int argmode
,
237 uint16_t type
, uint32_t adv_router
,
238 struct ospf6_lsa
**lsa
)
240 struct route_node
*node
, *end
;
245 struct prefix_ipv6 key
= {.family
= AF_INET6
, .prefixlen
= 0};
247 ospf6_lsdb_set_key(&key
, &type
, sizeof(type
));
249 ospf6_lsdb_set_key(&key
, &adv_router
,
252 node
= route_table_get_next(lsdb
->table
, &key
);
253 if (!node
|| !prefix_match((struct prefix
*)&key
, &node
->p
))
256 for (end
= node
; end
&& end
->parent
257 && end
->parent
->p
.prefixlen
>= key
.prefixlen
;
261 node
= route_top(lsdb
->table
);
265 while (node
&& !node
->info
)
266 node
= route_next_until(node
, end
);
271 route_unlock_node(node
);
276 ospf6_lsa_lock(*lsa
);
281 struct ospf6_lsa
*ospf6_lsdb_next(const struct route_node
*iterend
,
282 struct ospf6_lsa
*lsa
)
284 struct route_node
*node
= lsa
->rn
;
286 ospf6_lsa_unlock(lsa
);
289 node
= route_next_until(node
, iterend
);
290 while (node
&& !node
->info
);
292 if (node
&& node
->info
) {
293 struct ospf6_lsa
*next
= node
->info
;
294 ospf6_lsa_lock(next
);
299 route_unlock_node(node
);
303 void ospf6_lsdb_remove_all(struct ospf6_lsdb
*lsdb
)
305 struct ospf6_lsa
*lsa
;
310 for (ALL_LSDB(lsdb
, lsa
))
311 ospf6_lsdb_remove(lsa
, lsdb
);
314 void ospf6_lsdb_lsa_unlock(struct ospf6_lsa
*lsa
)
318 route_unlock_node(lsa
->rn
);
319 ospf6_lsa_unlock(lsa
);
323 int ospf6_lsdb_maxage_remover(struct ospf6_lsdb
*lsdb
)
326 struct ospf6_lsa
*lsa
;
328 for (ALL_LSDB(lsdb
, lsa
)) {
329 if (!OSPF6_LSA_IS_MAXAGE(lsa
))
331 if (lsa
->retrans_count
!= 0) {
335 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
))
336 zlog_debug("Remove MaxAge %s", lsa
->name
);
338 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_SEQWRAPPED
)) {
339 UNSET_FLAG(lsa
->flag
, OSPF6_LSA_SEQWRAPPED
);
341 * lsa->header->age = 0;
343 lsa
->header
->seqnum
=
344 htonl(OSPF_MAX_SEQUENCE_NUMBER
+ 1);
345 ospf6_lsa_checksum(lsa
->header
);
347 THREAD_OFF(lsa
->refresh
);
348 thread_execute(master
, ospf6_lsa_refresh
, lsa
, 0);
350 ospf6_lsdb_remove(lsa
, lsdb
);
357 void ospf6_lsdb_show(struct vty
*vty
, enum ospf_lsdb_show_level level
,
358 uint16_t *type
, uint32_t *id
, uint32_t *adv_router
,
359 struct ospf6_lsdb
*lsdb
)
361 struct ospf6_lsa
*lsa
;
362 const struct route_node
*end
= NULL
;
363 void (*showfunc
)(struct vty
*, struct ospf6_lsa
*) = NULL
;
366 case OSPF6_LSDB_SHOW_LEVEL_DETAIL
:
367 showfunc
= ospf6_lsa_show
;
369 case OSPF6_LSDB_SHOW_LEVEL_INTERNAL
:
370 showfunc
= ospf6_lsa_show_internal
;
372 case OSPF6_LSDB_SHOW_LEVEL_DUMP
:
373 showfunc
= ospf6_lsa_show_dump
;
375 case OSPF6_LSDB_SHOW_LEVEL_NORMAL
:
377 showfunc
= ospf6_lsa_show_summary
;
380 if (type
&& id
&& adv_router
) {
381 lsa
= ospf6_lsdb_lookup(*type
, *id
, *adv_router
, lsdb
);
383 if (level
== OSPF6_LSDB_SHOW_LEVEL_NORMAL
)
384 ospf6_lsa_show(vty
, lsa
);
386 (*showfunc
)(vty
, lsa
);
391 if (level
== OSPF6_LSDB_SHOW_LEVEL_NORMAL
)
392 ospf6_lsa_show_summary_header(vty
);
394 end
= ospf6_lsdb_head(lsdb
, !!type
+ !!(type
&& adv_router
),
395 type
? *type
: 0, adv_router
? *adv_router
: 0,
398 if ((!adv_router
|| lsa
->header
->adv_router
== *adv_router
)
399 && (!id
|| lsa
->header
->id
== *id
))
400 (*showfunc
)(vty
, lsa
);
402 lsa
= ospf6_lsdb_next(end
, lsa
);
406 uint32_t ospf6_new_ls_id(uint16_t type
, uint32_t adv_router
,
407 struct ospf6_lsdb
*lsdb
)
409 struct ospf6_lsa
*lsa
;
410 uint32_t id
= 1, tmp_id
;
412 /* This routine is curently invoked only for Inter-Prefix LSAs for
413 * non-summarized routes (no area/range).
415 for (ALL_LSDB_TYPED_ADVRTR(lsdb
, type
, adv_router
, lsa
)) {
416 tmp_id
= ntohl(lsa
->header
->id
);
421 ospf6_lsdb_lsa_unlock(lsa
);
427 return ((uint32_t)htonl(id
));
430 /* Decide new LS sequence number to originate.
431 note return value is network byte order */
432 uint32_t ospf6_new_ls_seqnum(uint16_t type
, uint32_t id
, uint32_t adv_router
,
433 struct ospf6_lsdb
*lsdb
)
435 struct ospf6_lsa
*lsa
;
436 signed long seqnum
= 0;
438 /* if current database copy not found, return InitialSequenceNumber */
439 lsa
= ospf6_lsdb_lookup(type
, id
, adv_router
, lsdb
);
441 seqnum
= OSPF_INITIAL_SEQUENCE_NUMBER
;
443 seqnum
= (signed long)ntohl(lsa
->header
->seqnum
) + 1;
445 return ((uint32_t)htonl(seqnum
));