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
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
31 #include "ospf6_proto.h"
32 #include "ospf6_lsa.h"
33 #include "ospf6_lsdb.h"
37 ospf6_lsdb_create (void *data)
39 struct ospf6_lsdb *lsdb;
41 lsdb = XCALLOC (MTYPE_OSPF6_LSDB, sizeof (struct ospf6_lsdb));
44 zlog_warn ("Can't malloc lsdb");
47 memset (lsdb, 0, sizeof (struct ospf6_lsdb));
50 lsdb->table = route_table_init ();
55 ospf6_lsdb_delete (struct ospf6_lsdb *lsdb)
59 ospf6_lsdb_remove_all (lsdb);
60 route_table_finish (lsdb->table);
61 XFREE (MTYPE_OSPF6_LSDB, lsdb);
66 ospf6_lsdb_set_key (struct prefix_ipv6 *key, void *value, int len)
68 assert (key->prefixlen % 8 == 0);
70 memcpy ((caddr_t) &key->prefix + key->prefixlen / 8,
71 (caddr_t) value, len);
72 key->family = AF_INET6;
73 key->prefixlen += len * 8;
78 _lsdb_count_assert (struct ospf6_lsdb *lsdb)
80 struct ospf6_lsa *debug;
82 for (debug = ospf6_lsdb_head (lsdb); debug;
83 debug = ospf6_lsdb_next (debug))
86 if (num == lsdb->count)
89 zlog_debug ("PANIC !! lsdb[%p]->count = %d, real = %d",
90 lsdb, lsdb->count, num);
91 for (debug = ospf6_lsdb_head (lsdb); debug;
92 debug = ospf6_lsdb_next (debug))
93 zlog_debug ("%p %p %s lsdb[%p]", debug->prev, debug->next, debug->name,
95 zlog_debug ("DUMP END");
97 assert (num == lsdb->count);
99 #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t))
101 #define ospf6_lsdb_count_assert(t) ((void) 0)
105 ospf6_lsdb_add (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
107 struct prefix_ipv6 key;
108 struct route_node *current;
109 struct ospf6_lsa *old = NULL;
111 memset (&key, 0, sizeof (key));
112 ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type));
113 ospf6_lsdb_set_key (&key, &lsa->header->adv_router,
114 sizeof (lsa->header->adv_router));
115 ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id));
117 current = route_node_get (lsdb->table, (struct prefix *) &key);
121 ospf6_lsa_lock (lsa);
127 if (OSPF6_LSA_IS_MAXAGE (lsa))
129 if (lsdb->hook_remove)
130 (*lsdb->hook_remove) (lsa);
135 (*lsdb->hook_add) (lsa);
140 if (OSPF6_LSA_IS_CHANGED (old, lsa))
142 if (OSPF6_LSA_IS_MAXAGE (lsa))
144 if (lsdb->hook_remove)
146 (*lsdb->hook_remove) (old);
147 (*lsdb->hook_remove) (lsa);
150 else if (OSPF6_LSA_IS_MAXAGE (old))
153 (*lsdb->hook_add) (lsa);
157 if (lsdb->hook_remove)
158 (*lsdb->hook_remove) (old);
160 (*lsdb->hook_add) (lsa);
163 ospf6_lsa_unlock (old);
166 ospf6_lsdb_count_assert (lsdb);
170 ospf6_lsdb_remove (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
172 struct route_node *node;
173 struct prefix_ipv6 key;
175 memset (&key, 0, sizeof (key));
176 ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type));
177 ospf6_lsdb_set_key (&key, &lsa->header->adv_router,
178 sizeof (lsa->header->adv_router));
179 ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id));
181 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
182 assert (node && node->info == lsa);
187 if (lsdb->hook_remove)
188 (*lsdb->hook_remove) (lsa);
190 route_unlock_node (node); /* to free the lookup lock */
191 route_unlock_node (node); /* to free the original lock */
192 ospf6_lsa_unlock (lsa);
194 ospf6_lsdb_count_assert (lsdb);
198 ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router,
199 struct ospf6_lsdb *lsdb)
201 struct route_node *node;
202 struct prefix_ipv6 key;
207 memset (&key, 0, sizeof (key));
208 ospf6_lsdb_set_key (&key, &type, sizeof (type));
209 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
210 ospf6_lsdb_set_key (&key, &id, sizeof (id));
212 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
213 if (node == NULL || node->info == NULL)
216 route_unlock_node (node);
217 return (struct ospf6_lsa *) node->info;
221 ospf6_lsdb_lookup_next (u_int16_t type, u_int32_t id, u_int32_t adv_router,
222 struct ospf6_lsdb *lsdb)
224 struct route_node *node;
225 struct route_node *matched = NULL;
226 struct prefix_ipv6 key;
232 memset (&key, 0, sizeof (key));
233 ospf6_lsdb_set_key (&key, &type, sizeof (type));
234 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
235 ospf6_lsdb_set_key (&key, &id, sizeof (id));
236 p = (struct prefix *) &key;
240 prefix2str (p, buf, sizeof (buf));
241 zlog_debug ("lsdb_lookup_next: key: %s", buf);
244 node = lsdb->table->top;
245 /* walk down tree. */
246 while (node && node->p.prefixlen <= p->prefixlen &&
247 prefix_match (&node->p, p))
250 node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
256 node = lsdb->table->top;
257 route_lock_node (node);
259 /* skip to real existing entry */
260 while (node && node->info == NULL)
261 node = route_next (node);
266 if (prefix_same (&node->p, p))
268 node = route_next (node);
269 while (node && node->info == NULL)
270 node = route_next (node);
276 route_unlock_node (node);
277 return (struct ospf6_lsa *) node->info;
280 /* Iteration function */
282 ospf6_lsdb_head (struct ospf6_lsdb *lsdb)
284 struct route_node *node;
286 node = route_top (lsdb->table);
290 /* skip to the existing lsdb entry */
291 while (node && node->info == NULL)
292 node = route_next (node);
297 ospf6_lsa_lock ((struct ospf6_lsa *) node->info);
298 return (struct ospf6_lsa *) node->info;
302 ospf6_lsdb_next (struct ospf6_lsa *lsa)
304 struct route_node *node = lsa->rn;
305 struct ospf6_lsa *next = NULL;
308 node = route_next (node);
309 } while (node && node->info == NULL);
311 if ((node != NULL) && (node->info != NULL))
314 ospf6_lsa_lock (next);
317 ospf6_lsa_unlock (lsa);
322 ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router,
323 struct ospf6_lsdb *lsdb)
325 struct route_node *node;
326 struct prefix_ipv6 key;
327 struct ospf6_lsa *lsa;
329 memset (&key, 0, sizeof (key));
330 ospf6_lsdb_set_key (&key, &type, sizeof (type));
331 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
333 node = lsdb->table->top;
335 /* Walk down tree. */
336 while (node && node->p.prefixlen <= key.prefixlen &&
337 prefix_match (&node->p, (struct prefix *) &key))
338 node = node->link[prefix6_bit(&key.prefix, node->p.prefixlen)];
341 route_lock_node (node);
342 while (node && node->info == NULL)
343 node = route_next (node);
348 if (! prefix_match ((struct prefix *) &key, &node->p))
352 ospf6_lsa_lock (lsa);
358 ospf6_lsdb_type_router_next (u_int16_t type, u_int32_t adv_router,
359 struct ospf6_lsa *lsa)
361 struct ospf6_lsa *next = ospf6_lsdb_next(lsa);
365 if (next->header->type != type ||
366 next->header->adv_router != adv_router)
368 route_unlock_node (next->rn);
369 ospf6_lsa_unlock (next);
378 ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb)
380 struct route_node *node;
381 struct prefix_ipv6 key;
382 struct ospf6_lsa *lsa;
384 memset (&key, 0, sizeof (key));
385 ospf6_lsdb_set_key (&key, &type, sizeof (type));
387 /* Walk down tree. */
388 node = lsdb->table->top;
389 while (node && node->p.prefixlen <= key.prefixlen &&
390 prefix_match (&node->p, (struct prefix *) &key))
391 node = node->link[prefix6_bit(&key.prefix, node->p.prefixlen)];
394 route_lock_node (node);
395 while (node && node->info == NULL)
396 node = route_next (node);
401 if (! prefix_match ((struct prefix *) &key, &node->p))
405 ospf6_lsa_lock (lsa);
411 ospf6_lsdb_type_next (u_int16_t type, struct ospf6_lsa *lsa)
413 struct ospf6_lsa *next = ospf6_lsdb_next (lsa);
417 if (next->header->type != type)
419 route_unlock_node (next->rn);
420 ospf6_lsa_unlock (next);
429 ospf6_lsdb_remove_all (struct ospf6_lsdb *lsdb)
431 struct ospf6_lsa *lsa;
436 for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa))
437 ospf6_lsdb_remove (lsa, lsdb);
441 ospf6_lsdb_lsa_unlock (struct ospf6_lsa *lsa)
446 route_unlock_node (lsa->rn);
447 ospf6_lsa_unlock (lsa);
452 ospf6_lsdb_maxage_remover (struct ospf6_lsdb *lsdb)
455 struct ospf6_lsa *lsa;
457 for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa))
459 if (! OSPF6_LSA_IS_MAXAGE (lsa))
461 if (lsa->retrans_count != 0)
466 if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
467 zlog_debug ("Remove MaxAge %s", lsa->name);
468 if (CHECK_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED))
470 UNSET_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED);
472 * lsa->header->age = 0;
474 lsa->header->seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER + 1);
475 ospf6_lsa_checksum (lsa->header);
477 THREAD_OFF(lsa->refresh);
478 thread_execute (master, ospf6_lsa_refresh, lsa, 0);
480 ospf6_lsdb_remove (lsa, lsdb);
488 ospf6_lsdb_show (struct vty *vty, enum ospf_lsdb_show_level level,
489 u_int16_t *type, u_int32_t *id, u_int32_t *adv_router,
490 struct ospf6_lsdb *lsdb)
492 struct ospf6_lsa *lsa;
493 void (*showfunc) (struct vty *, struct ospf6_lsa *) = NULL;
497 case OSPF6_LSDB_SHOW_LEVEL_DETAIL:
498 showfunc = ospf6_lsa_show;
500 case OSPF6_LSDB_SHOW_LEVEL_INTERNAL:
501 showfunc = ospf6_lsa_show_internal;
503 case OSPF6_LSDB_SHOW_LEVEL_DUMP:
504 showfunc = ospf6_lsa_show_dump;
506 case OSPF6_LSDB_SHOW_LEVEL_NORMAL:
508 showfunc = ospf6_lsa_show_summary;
511 if (type && id && adv_router)
513 lsa = ospf6_lsdb_lookup (*type, *id, *adv_router, lsdb);
516 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
517 ospf6_lsa_show (vty, lsa);
519 (*showfunc) (vty, lsa);
524 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
525 ospf6_lsa_show_summary_header (vty);
527 if (type && adv_router)
528 lsa = ospf6_lsdb_type_router_head (*type, *adv_router, lsdb);
530 lsa = ospf6_lsdb_type_head (*type, lsdb);
532 lsa = ospf6_lsdb_head (lsdb);
535 if ((! adv_router || lsa->header->adv_router == *adv_router) &&
536 (! id || lsa->header->id == *id))
537 (*showfunc) (vty, lsa);
539 if (type && adv_router)
540 lsa = ospf6_lsdb_type_router_next (*type, *adv_router, lsa);
542 lsa = ospf6_lsdb_type_next (*type, lsa);
544 lsa = ospf6_lsdb_next (lsa);
548 /* Decide new Link State ID to originate.
549 note return value is network byte order */
551 ospf6_new_ls_id (u_int16_t type, u_int32_t adv_router,
552 struct ospf6_lsdb *lsdb)
554 struct ospf6_lsa *lsa;
557 for (lsa = ospf6_lsdb_type_router_head (type, adv_router, lsdb); lsa;
558 lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa))
560 if (ntohl (lsa->header->id) < id)
562 if (ntohl (lsa->header->id) > id)
564 ospf6_lsdb_lsa_unlock (lsa);
570 return ((u_int32_t) htonl (id));
573 /* Decide new LS sequence number to originate.
574 note return value is network byte order */
576 ospf6_new_ls_seqnum (u_int16_t type, u_int32_t id, u_int32_t adv_router,
577 struct ospf6_lsdb *lsdb)
579 struct ospf6_lsa *lsa;
580 signed long seqnum = 0;
582 /* if current database copy not found, return InitialSequenceNumber */
583 lsa = ospf6_lsdb_lookup (type, id, adv_router, lsdb);
585 seqnum = OSPF_INITIAL_SEQUENCE_NUMBER;
587 seqnum = (signed long) ntohl (lsa->header->seqnum) + 1;
589 return ((u_int32_t) htonl (seqnum));