blob: 247238b9adc289a9ad27e9605d483e04b877ba33 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
hasso508e53e2004-05-18 18:57:06 +00002 * Copyright (C) 2003 Yasuhiro Ohara
paul718e3742002-12-13 20:15:29 +00003 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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.
20 */
21
22#include <zebra.h>
23
24#include "memory.h"
25#include "log.h"
26#include "command.h"
hasso508e53e2004-05-18 18:57:06 +000027#include "prefix.h"
28#include "table.h"
hasso049207c2004-08-04 20:02:13 +000029#include "vty.h"
paul718e3742002-12-13 20:15:29 +000030
hasso508e53e2004-05-18 18:57:06 +000031#include "ospf6_proto.h"
32#include "ospf6_lsa.h"
paul718e3742002-12-13 20:15:29 +000033#include "ospf6_lsdb.h"
hasso049207c2004-08-04 20:02:13 +000034#include "ospf6d.h"
paul718e3742002-12-13 20:15:29 +000035
paul718e3742002-12-13 20:15:29 +000036struct ospf6_lsdb *
hasso6452df02004-08-15 05:52:07 +000037ospf6_lsdb_create (void *data)
paul718e3742002-12-13 20:15:29 +000038{
39 struct ospf6_lsdb *lsdb;
40
41 lsdb = XCALLOC (MTYPE_OSPF6_LSDB, sizeof (struct ospf6_lsdb));
42 if (lsdb == NULL)
43 {
44 zlog_warn ("Can't malloc lsdb");
45 return NULL;
46 }
47 memset (lsdb, 0, sizeof (struct ospf6_lsdb));
48
hasso6452df02004-08-15 05:52:07 +000049 lsdb->data = data;
paul718e3742002-12-13 20:15:29 +000050 lsdb->table = route_table_init ();
51 return lsdb;
52}
53
54void
55ospf6_lsdb_delete (struct ospf6_lsdb *lsdb)
56{
57 ospf6_lsdb_remove_all (lsdb);
58 route_table_finish (lsdb->table);
59 XFREE (MTYPE_OSPF6_LSDB, lsdb);
60}
61
62static void
hasso508e53e2004-05-18 18:57:06 +000063ospf6_lsdb_set_key (struct prefix_ipv6 *key, void *value, int len)
paul718e3742002-12-13 20:15:29 +000064{
hasso508e53e2004-05-18 18:57:06 +000065 assert (key->prefixlen % 8 == 0);
paul718e3742002-12-13 20:15:29 +000066
hasso508e53e2004-05-18 18:57:06 +000067 memcpy ((caddr_t) &key->prefix + key->prefixlen / 8,
68 (caddr_t) value, len);
paul718e3742002-12-13 20:15:29 +000069 key->family = AF_INET6;
hasso508e53e2004-05-18 18:57:06 +000070 key->prefixlen += len * 8;
paul718e3742002-12-13 20:15:29 +000071}
72
hasso508e53e2004-05-18 18:57:06 +000073#ifndef NDEBUG
74static void
75_lsdb_count_assert (struct ospf6_lsdb *lsdb)
76{
77 struct ospf6_lsa *debug;
78 int num = 0;
79 for (debug = ospf6_lsdb_head (lsdb); debug;
80 debug = ospf6_lsdb_next (debug))
81 num++;
82
83 if (num == lsdb->count)
84 return;
85
hasso3b687352004-08-19 06:56:53 +000086 zlog_info ("PANIC !! lsdb[%p]->count = %d, real = %d",
87 lsdb, lsdb->count, num);
88 for (debug = ospf6_lsdb_head (lsdb); debug;
89 debug = ospf6_lsdb_next (debug))
90 zlog_info ("%p %p %s lsdb[%p]", debug->prev, debug->next, debug->name,
91 debug->lsdb);
92 zlog_info ("DUMP END");
93
hasso508e53e2004-05-18 18:57:06 +000094 assert (num == lsdb->count);
95}
96#define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t))
97#else /*NDEBUG*/
98#define ospf6_lsdb_count_assert(t) ((void) 0)
99#endif /*NDEBUG*/
100
paul718e3742002-12-13 20:15:29 +0000101void
102ospf6_lsdb_add (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
103{
paul718e3742002-12-13 20:15:29 +0000104 struct prefix_ipv6 key;
hasso508e53e2004-05-18 18:57:06 +0000105 struct route_node *current, *nextnode, *prevnode;
106 struct ospf6_lsa *next, *prev, *old = NULL;
paul718e3742002-12-13 20:15:29 +0000107
hasso508e53e2004-05-18 18:57:06 +0000108 memset (&key, 0, sizeof (key));
109 ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type));
110 ospf6_lsdb_set_key (&key, &lsa->header->adv_router,
111 sizeof (lsa->header->adv_router));
112 ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id));
paul718e3742002-12-13 20:15:29 +0000113
hasso508e53e2004-05-18 18:57:06 +0000114 current = route_node_get (lsdb->table, (struct prefix *) &key);
115 old = current->info;
116 current->info = lsa;
paul718e3742002-12-13 20:15:29 +0000117 ospf6_lsa_lock (lsa);
118
119 if (old)
hasso508e53e2004-05-18 18:57:06 +0000120 {
121 if (old->prev)
122 old->prev->next = lsa;
123 if (old->next)
124 old->next->prev = lsa;
125 lsa->next = old->next;
126 lsa->prev = old->prev;
127 }
paul718e3742002-12-13 20:15:29 +0000128 else
hasso508e53e2004-05-18 18:57:06 +0000129 {
130 /* next link */
131 nextnode = current;
132 route_lock_node (nextnode);
133 do {
134 nextnode = route_next (nextnode);
135 } while (nextnode && nextnode->info == NULL);
136 if (nextnode == NULL)
137 lsa->next = NULL;
138 else
139 {
140 next = nextnode->info;
141 lsa->next = next;
142 next->prev = lsa;
143 route_unlock_node (nextnode);
144 }
145
146 /* prev link */
147 prevnode = current;
148 route_lock_node (prevnode);
149 do {
150 prevnode = route_prev (prevnode);
151 } while (prevnode && prevnode->info == NULL);
152 if (prevnode == NULL)
153 lsa->prev = NULL;
154 else
155 {
156 prev = prevnode->info;
157 lsa->prev = prev;
158 prev->next = lsa;
159 route_unlock_node (prevnode);
160 }
161
162 lsdb->count++;
163 }
164
165 if (old)
166 {
167 if (OSPF6_LSA_IS_CHANGED (old, lsa))
168 {
169 if (OSPF6_LSA_IS_MAXAGE (lsa))
170 {
171 if (lsdb->hook_remove)
172 {
173 (*lsdb->hook_remove) (old);
174 (*lsdb->hook_remove) (lsa);
175 }
176 }
177 else
178 {
179 if (lsdb->hook_remove)
180 (*lsdb->hook_remove) (old);
181 if (lsdb->hook_add)
182 (*lsdb->hook_add) (lsa);
183 }
184 }
185 }
186 else if (OSPF6_LSA_IS_MAXAGE (lsa))
187 {
188 if (lsdb->hook_remove)
189 (*lsdb->hook_remove) (lsa);
190 }
191 else
192 {
193 if (lsdb->hook_add)
194 (*lsdb->hook_add) (lsa);
195 }
196
197 if (old)
198 ospf6_lsa_unlock (old);
199
200 ospf6_lsdb_count_assert (lsdb);
paul718e3742002-12-13 20:15:29 +0000201}
202
203void
204ospf6_lsdb_remove (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
205{
hasso508e53e2004-05-18 18:57:06 +0000206 struct route_node *node;
paul718e3742002-12-13 20:15:29 +0000207 struct prefix_ipv6 key;
paul718e3742002-12-13 20:15:29 +0000208
hasso508e53e2004-05-18 18:57:06 +0000209 memset (&key, 0, sizeof (key));
210 ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type));
211 ospf6_lsdb_set_key (&key, &lsa->header->adv_router,
212 sizeof (lsa->header->adv_router));
213 ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id));
paul718e3742002-12-13 20:15:29 +0000214
hasso508e53e2004-05-18 18:57:06 +0000215 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
216 assert (node && node->info == lsa);
paul718e3742002-12-13 20:15:29 +0000217
hasso508e53e2004-05-18 18:57:06 +0000218 if (lsa->prev)
219 lsa->prev->next = lsa->next;
220 if (lsa->next)
221 lsa->next->prev = lsa->prev;
paul718e3742002-12-13 20:15:29 +0000222
hasso508e53e2004-05-18 18:57:06 +0000223 node->info = NULL;
paul718e3742002-12-13 20:15:29 +0000224 lsdb->count--;
paul718e3742002-12-13 20:15:29 +0000225
hasso508e53e2004-05-18 18:57:06 +0000226 if (lsdb->hook_remove)
227 (*lsdb->hook_remove) (lsa);
paul718e3742002-12-13 20:15:29 +0000228
hasso508e53e2004-05-18 18:57:06 +0000229 ospf6_lsa_unlock (lsa);
230 route_unlock_node (node);
hasso6452df02004-08-15 05:52:07 +0000231
hasso508e53e2004-05-18 18:57:06 +0000232 ospf6_lsdb_count_assert (lsdb);
paul718e3742002-12-13 20:15:29 +0000233}
234
235struct ospf6_lsa *
236ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router,
hasso508e53e2004-05-18 18:57:06 +0000237 struct ospf6_lsdb *lsdb)
paul718e3742002-12-13 20:15:29 +0000238{
hasso508e53e2004-05-18 18:57:06 +0000239 struct route_node *node;
240 struct prefix_ipv6 key;
paul718e3742002-12-13 20:15:29 +0000241
hasso508e53e2004-05-18 18:57:06 +0000242 if (lsdb == NULL)
243 return NULL;
paul718e3742002-12-13 20:15:29 +0000244
hasso508e53e2004-05-18 18:57:06 +0000245 memset (&key, 0, sizeof (key));
246 ospf6_lsdb_set_key (&key, &type, sizeof (type));
247 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
248 ospf6_lsdb_set_key (&key, &id, sizeof (id));
paul718e3742002-12-13 20:15:29 +0000249
hasso508e53e2004-05-18 18:57:06 +0000250 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
251 if (node == NULL || node->info == NULL)
252 return NULL;
253 return (struct ospf6_lsa *) node->info;
paul718e3742002-12-13 20:15:29 +0000254}
255
hasso508e53e2004-05-18 18:57:06 +0000256/* Iteration function */
257struct ospf6_lsa *
258ospf6_lsdb_head (struct ospf6_lsdb *lsdb)
paul718e3742002-12-13 20:15:29 +0000259{
hasso508e53e2004-05-18 18:57:06 +0000260 struct route_node *node;
paul718e3742002-12-13 20:15:29 +0000261
hasso508e53e2004-05-18 18:57:06 +0000262 node = route_top (lsdb->table);
263 if (node == NULL)
264 return NULL;
paul718e3742002-12-13 20:15:29 +0000265
hasso508e53e2004-05-18 18:57:06 +0000266 /* skip to the existing lsdb entry */
267 while (node && node->info == NULL)
268 node = route_next (node);
269 if (node == NULL)
270 return NULL;
271
272 route_unlock_node (node);
273 if (node->info)
274 ospf6_lsa_lock ((struct ospf6_lsa *) node->info);
275 return (struct ospf6_lsa *) node->info;
276}
277
278struct ospf6_lsa *
279ospf6_lsdb_next (struct ospf6_lsa *lsa)
280{
281 struct ospf6_lsa *next = lsa->next;
282
283 ospf6_lsa_unlock (lsa);
284 if (next)
285 ospf6_lsa_lock (next);
286
287 return next;
288}
289
290/* Macro version of check_bit (). */
291#define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
292
293struct ospf6_lsa *
294ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router,
295 struct ospf6_lsdb *lsdb)
296{
297 struct route_node *node;
298 struct prefix_ipv6 key;
299 struct ospf6_lsa *lsa;
300
301 memset (&key, 0, sizeof (key));
302 ospf6_lsdb_set_key (&key, &type, sizeof (type));
303 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
304
305 node = lsdb->table->top;
306
307 /* Walk down tree. */
308 while (node && node->p.prefixlen <= key.prefixlen &&
309 prefix_match (&node->p, (struct prefix *) &key))
310 node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
311
312 if (node)
313 route_lock_node (node);
314 while (node && node->info == NULL)
315 node = route_next (node);
316
317 if (node == NULL)
318 return NULL;
319 else
320 route_unlock_node (node);
321
322 if (! prefix_match ((struct prefix *) &key, &node->p))
323 return NULL;
324
325 lsa = node->info;
326 ospf6_lsa_lock (lsa);
327
328 return lsa;
329}
330
331struct ospf6_lsa *
332ospf6_lsdb_type_router_next (u_int16_t type, u_int32_t adv_router,
333 struct ospf6_lsa *lsa)
334{
335 struct ospf6_lsa *next = lsa->next;
336
337 if (next)
paul718e3742002-12-13 20:15:29 +0000338 {
hasso508e53e2004-05-18 18:57:06 +0000339 if (next->header->type != type ||
340 next->header->adv_router != adv_router)
341 next = NULL;
paul718e3742002-12-13 20:15:29 +0000342 }
343
hasso508e53e2004-05-18 18:57:06 +0000344 if (next)
345 ospf6_lsa_lock (next);
346 ospf6_lsa_unlock (lsa);
347 return next;
348}
paul718e3742002-12-13 20:15:29 +0000349
hasso508e53e2004-05-18 18:57:06 +0000350struct ospf6_lsa *
351ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb)
352{
353 struct route_node *node;
354 struct prefix_ipv6 key;
355 struct ospf6_lsa *lsa;
paul718e3742002-12-13 20:15:29 +0000356
hasso508e53e2004-05-18 18:57:06 +0000357 memset (&key, 0, sizeof (key));
358 ospf6_lsdb_set_key (&key, &type, sizeof (type));
paul718e3742002-12-13 20:15:29 +0000359
hasso508e53e2004-05-18 18:57:06 +0000360 /* Walk down tree. */
361 node = lsdb->table->top;
362 while (node && node->p.prefixlen <= key.prefixlen &&
363 prefix_match (&node->p, (struct prefix *) &key))
364 node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
paul718e3742002-12-13 20:15:29 +0000365
hasso508e53e2004-05-18 18:57:06 +0000366 if (node)
367 route_lock_node (node);
368 while (node && node->info == NULL)
369 node = route_next (node);
paul718e3742002-12-13 20:15:29 +0000370
hasso508e53e2004-05-18 18:57:06 +0000371 if (node == NULL)
372 return NULL;
373 else
374 route_unlock_node (node);
paul718e3742002-12-13 20:15:29 +0000375
hasso508e53e2004-05-18 18:57:06 +0000376 if (! prefix_match ((struct prefix *) &key, &node->p))
377 return NULL;
378
379 lsa = node->info;
380 ospf6_lsa_lock (lsa);
381
382 return lsa;
383}
384
385struct ospf6_lsa *
386ospf6_lsdb_type_next (u_int16_t type, struct ospf6_lsa *lsa)
387{
388 struct ospf6_lsa *next = lsa->next;
389
390 if (next)
391 {
392 if (next->header->type != type)
393 next = NULL;
394 }
395
396 if (next)
397 ospf6_lsa_lock (next);
398 ospf6_lsa_unlock (lsa);
399 return next;
paul718e3742002-12-13 20:15:29 +0000400}
401
402void
403ospf6_lsdb_remove_all (struct ospf6_lsdb *lsdb)
404{
paul718e3742002-12-13 20:15:29 +0000405 struct ospf6_lsa *lsa;
hasso508e53e2004-05-18 18:57:06 +0000406 for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa))
407 ospf6_lsdb_remove (lsa, lsdb);
paul718e3742002-12-13 20:15:29 +0000408}
409
hasso049207c2004-08-04 20:02:13 +0000410void
411ospf6_lsdb_show (struct vty *vty, int level,
412 u_int16_t *type, u_int32_t *id, u_int32_t *adv_router,
413 struct ospf6_lsdb *lsdb)
414{
415 struct ospf6_lsa *lsa;
416 void (*showfunc) (struct vty *, struct ospf6_lsa *) = NULL;
417
418 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
419 showfunc = ospf6_lsa_show_summary;
420 else if (level == OSPF6_LSDB_SHOW_LEVEL_DETAIL)
421 showfunc = ospf6_lsa_show;
422 else if (level == OSPF6_LSDB_SHOW_LEVEL_INTERNAL)
423 showfunc = ospf6_lsa_show_internal;
424 else if (level == OSPF6_LSDB_SHOW_LEVEL_DUMP)
425 showfunc = ospf6_lsa_show_dump;
426
427 if (type && id && adv_router)
428 {
429 lsa = ospf6_lsdb_lookup (*type, *id, *adv_router, lsdb);
430 if (lsa)
431 {
432 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
433 ospf6_lsa_show (vty, lsa);
434 else
435 (*showfunc) (vty, lsa);
436 }
437 return;
438 }
439
440 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
441 ospf6_lsa_show_summary_header (vty);
442
443 if (type && adv_router)
444 lsa = ospf6_lsdb_type_router_head (*type, *adv_router, lsdb);
445 else if (type)
446 lsa = ospf6_lsdb_type_head (*type, lsdb);
447 else
448 lsa = ospf6_lsdb_head (lsdb);
449 while (lsa)
450 {
451 if ((! adv_router || lsa->header->adv_router == *adv_router) &&
452 (! id || lsa->header->id == *id))
453 (*showfunc) (vty, lsa);
454
455 if (type && adv_router)
456 lsa = ospf6_lsdb_type_router_next (*type, *adv_router, lsa);
457 else if (type)
458 lsa = ospf6_lsdb_type_next (*type, lsa);
459 else
460 lsa = ospf6_lsdb_next (lsa);
461 }
462}
463
464/* Decide new Link State ID to originate.
465 note return value is network byte order */
466u_int32_t
467ospf6_new_ls_id (u_int16_t type, u_int32_t adv_router,
468 struct ospf6_lsdb *lsdb)
469{
470 struct ospf6_lsa *lsa;
471 u_int32_t id = 1;
472
473 for (lsa = ospf6_lsdb_type_router_head (type, adv_router, lsdb); lsa;
474 lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa))
475 {
476 if (ntohl (lsa->header->id) < id)
477 continue;
478 if (ntohl (lsa->header->id) > id)
hasso6452df02004-08-15 05:52:07 +0000479 break;
hasso049207c2004-08-04 20:02:13 +0000480 id++;
481 }
482
483 return ((u_int32_t) htonl (id));
484}
485
486/* Decide new LS sequence number to originate.
487 note return value is network byte order */
488u_int32_t
489ospf6_new_ls_seqnum (u_int16_t type, u_int32_t id, u_int32_t adv_router,
490 struct ospf6_lsdb *lsdb)
491{
492 struct ospf6_lsa *lsa;
493 signed long seqnum = 0;
494
495 /* if current database copy not found, return InitialSequenceNumber */
496 lsa = ospf6_lsdb_lookup (type, id, adv_router, lsdb);
497 if (lsa == NULL)
498 seqnum = INITIAL_SEQUENCE_NUMBER;
499 else
500 seqnum = (signed long) ntohl (lsa->header->seqnum) + 1;
501
502 return ((u_int32_t) htonl (seqnum));
503}
504
paul718e3742002-12-13 20:15:29 +0000505