]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_lsdb.c
2004-10-10 Paul Jakma <paul@dishone.st>
[mirror_frr.git] / ospf6d / ospf6_lsdb.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
3 *
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"
27 #include "prefix.h"
28 #include "table.h"
29 #include "vty.h"
30
31 #include "ospf6_proto.h"
32 #include "ospf6_lsa.h"
33 #include "ospf6_lsdb.h"
34 #include "ospf6d.h"
35
36 struct ospf6_lsdb *
37 ospf6_lsdb_create (void *data)
38 {
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
49 lsdb->data = data;
50 lsdb->table = route_table_init ();
51 return lsdb;
52 }
53
54 void
55 ospf6_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
62 static void
63 ospf6_lsdb_set_key (struct prefix_ipv6 *key, void *value, int len)
64 {
65 assert (key->prefixlen % 8 == 0);
66
67 memcpy ((caddr_t) &key->prefix + key->prefixlen / 8,
68 (caddr_t) value, len);
69 key->family = AF_INET6;
70 key->prefixlen += len * 8;
71 }
72
73 #ifndef NDEBUG
74 static void
75 _lsdb_count_assert (struct ospf6_lsdb *lsdb)
76 {
77 struct ospf6_lsa *debug;
78 unsigned 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
86 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
94 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
101 void
102 ospf6_lsdb_add (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
103 {
104 struct prefix_ipv6 key;
105 struct route_node *current, *nextnode, *prevnode;
106 struct ospf6_lsa *next, *prev, *old = NULL;
107
108 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));
113
114 current = route_node_get (lsdb->table, (struct prefix *) &key);
115 old = current->info;
116 current->info = lsa;
117 ospf6_lsa_lock (lsa);
118
119 if (old)
120 {
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 }
128 else
129 {
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 if (OSPF6_LSA_IS_MAXAGE (old))
178 {
179 if (lsdb->hook_add)
180 (*lsdb->hook_add) (lsa);
181 }
182 else
183 {
184 if (lsdb->hook_remove)
185 (*lsdb->hook_remove) (old);
186 if (lsdb->hook_add)
187 (*lsdb->hook_add) (lsa);
188 }
189 }
190 }
191 else if (OSPF6_LSA_IS_MAXAGE (lsa))
192 {
193 if (lsdb->hook_remove)
194 (*lsdb->hook_remove) (lsa);
195 }
196 else
197 {
198 if (lsdb->hook_add)
199 (*lsdb->hook_add) (lsa);
200 }
201
202 if (old)
203 ospf6_lsa_unlock (old);
204
205 ospf6_lsdb_count_assert (lsdb);
206 }
207
208 void
209 ospf6_lsdb_remove (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
210 {
211 struct route_node *node;
212 struct prefix_ipv6 key;
213
214 memset (&key, 0, sizeof (key));
215 ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type));
216 ospf6_lsdb_set_key (&key, &lsa->header->adv_router,
217 sizeof (lsa->header->adv_router));
218 ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id));
219
220 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
221 assert (node && node->info == lsa);
222
223 if (lsa->prev)
224 lsa->prev->next = lsa->next;
225 if (lsa->next)
226 lsa->next->prev = lsa->prev;
227
228 node->info = NULL;
229 lsdb->count--;
230
231 if (lsdb->hook_remove)
232 (*lsdb->hook_remove) (lsa);
233
234 ospf6_lsa_unlock (lsa);
235 route_unlock_node (node);
236
237 ospf6_lsdb_count_assert (lsdb);
238 }
239
240 struct ospf6_lsa *
241 ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router,
242 struct ospf6_lsdb *lsdb)
243 {
244 struct route_node *node;
245 struct prefix_ipv6 key;
246
247 if (lsdb == NULL)
248 return NULL;
249
250 memset (&key, 0, sizeof (key));
251 ospf6_lsdb_set_key (&key, &type, sizeof (type));
252 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
253 ospf6_lsdb_set_key (&key, &id, sizeof (id));
254
255 node = route_node_lookup (lsdb->table, (struct prefix *) &key);
256 if (node == NULL || node->info == NULL)
257 return NULL;
258 return (struct ospf6_lsa *) node->info;
259 }
260
261 /* Iteration function */
262 struct ospf6_lsa *
263 ospf6_lsdb_head (struct ospf6_lsdb *lsdb)
264 {
265 struct route_node *node;
266
267 node = route_top (lsdb->table);
268 if (node == NULL)
269 return NULL;
270
271 /* skip to the existing lsdb entry */
272 while (node && node->info == NULL)
273 node = route_next (node);
274 if (node == NULL)
275 return NULL;
276
277 route_unlock_node (node);
278 if (node->info)
279 ospf6_lsa_lock ((struct ospf6_lsa *) node->info);
280 return (struct ospf6_lsa *) node->info;
281 }
282
283 struct ospf6_lsa *
284 ospf6_lsdb_next (struct ospf6_lsa *lsa)
285 {
286 struct ospf6_lsa *next = lsa->next;
287
288 ospf6_lsa_unlock (lsa);
289 if (next)
290 ospf6_lsa_lock (next);
291
292 return next;
293 }
294
295 /* Macro version of check_bit (). */
296 #define CHECK_BIT(X,P) ((((u_char *)(X))[(P) / 8]) >> (7 - ((P) % 8)) & 1)
297
298 struct ospf6_lsa *
299 ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router,
300 struct ospf6_lsdb *lsdb)
301 {
302 struct route_node *node;
303 struct prefix_ipv6 key;
304 struct ospf6_lsa *lsa;
305
306 memset (&key, 0, sizeof (key));
307 ospf6_lsdb_set_key (&key, &type, sizeof (type));
308 ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router));
309
310 node = lsdb->table->top;
311
312 /* Walk down tree. */
313 while (node && node->p.prefixlen <= key.prefixlen &&
314 prefix_match (&node->p, (struct prefix *) &key))
315 node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
316
317 if (node)
318 route_lock_node (node);
319 while (node && node->info == NULL)
320 node = route_next (node);
321
322 if (node == NULL)
323 return NULL;
324 else
325 route_unlock_node (node);
326
327 if (! prefix_match ((struct prefix *) &key, &node->p))
328 return NULL;
329
330 lsa = node->info;
331 ospf6_lsa_lock (lsa);
332
333 return lsa;
334 }
335
336 struct ospf6_lsa *
337 ospf6_lsdb_type_router_next (u_int16_t type, u_int32_t adv_router,
338 struct ospf6_lsa *lsa)
339 {
340 struct ospf6_lsa *next = lsa->next;
341
342 if (next)
343 {
344 if (next->header->type != type ||
345 next->header->adv_router != adv_router)
346 next = NULL;
347 }
348
349 if (next)
350 ospf6_lsa_lock (next);
351 ospf6_lsa_unlock (lsa);
352 return next;
353 }
354
355 struct ospf6_lsa *
356 ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb)
357 {
358 struct route_node *node;
359 struct prefix_ipv6 key;
360 struct ospf6_lsa *lsa;
361
362 memset (&key, 0, sizeof (key));
363 ospf6_lsdb_set_key (&key, &type, sizeof (type));
364
365 /* Walk down tree. */
366 node = lsdb->table->top;
367 while (node && node->p.prefixlen <= key.prefixlen &&
368 prefix_match (&node->p, (struct prefix *) &key))
369 node = node->link[CHECK_BIT(&key.prefix, node->p.prefixlen)];
370
371 if (node)
372 route_lock_node (node);
373 while (node && node->info == NULL)
374 node = route_next (node);
375
376 if (node == NULL)
377 return NULL;
378 else
379 route_unlock_node (node);
380
381 if (! prefix_match ((struct prefix *) &key, &node->p))
382 return NULL;
383
384 lsa = node->info;
385 ospf6_lsa_lock (lsa);
386
387 return lsa;
388 }
389
390 struct ospf6_lsa *
391 ospf6_lsdb_type_next (u_int16_t type, struct ospf6_lsa *lsa)
392 {
393 struct ospf6_lsa *next = lsa->next;
394
395 if (next)
396 {
397 if (next->header->type != type)
398 next = NULL;
399 }
400
401 if (next)
402 ospf6_lsa_lock (next);
403 ospf6_lsa_unlock (lsa);
404 return next;
405 }
406
407 void
408 ospf6_lsdb_remove_all (struct ospf6_lsdb *lsdb)
409 {
410 struct ospf6_lsa *lsa;
411 for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa))
412 ospf6_lsdb_remove (lsa, lsdb);
413 }
414
415 void
416 ospf6_lsdb_show (struct vty *vty, int level,
417 u_int16_t *type, u_int32_t *id, u_int32_t *adv_router,
418 struct ospf6_lsdb *lsdb)
419 {
420 struct ospf6_lsa *lsa;
421 void (*showfunc) (struct vty *, struct ospf6_lsa *) = NULL;
422
423 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
424 showfunc = ospf6_lsa_show_summary;
425 else if (level == OSPF6_LSDB_SHOW_LEVEL_DETAIL)
426 showfunc = ospf6_lsa_show;
427 else if (level == OSPF6_LSDB_SHOW_LEVEL_INTERNAL)
428 showfunc = ospf6_lsa_show_internal;
429 else if (level == OSPF6_LSDB_SHOW_LEVEL_DUMP)
430 showfunc = ospf6_lsa_show_dump;
431
432 if (type && id && adv_router)
433 {
434 lsa = ospf6_lsdb_lookup (*type, *id, *adv_router, lsdb);
435 if (lsa)
436 {
437 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
438 ospf6_lsa_show (vty, lsa);
439 else
440 (*showfunc) (vty, lsa);
441 }
442 return;
443 }
444
445 if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL)
446 ospf6_lsa_show_summary_header (vty);
447
448 if (type && adv_router)
449 lsa = ospf6_lsdb_type_router_head (*type, *adv_router, lsdb);
450 else if (type)
451 lsa = ospf6_lsdb_type_head (*type, lsdb);
452 else
453 lsa = ospf6_lsdb_head (lsdb);
454 while (lsa)
455 {
456 if ((! adv_router || lsa->header->adv_router == *adv_router) &&
457 (! id || lsa->header->id == *id))
458 (*showfunc) (vty, lsa);
459
460 if (type && adv_router)
461 lsa = ospf6_lsdb_type_router_next (*type, *adv_router, lsa);
462 else if (type)
463 lsa = ospf6_lsdb_type_next (*type, lsa);
464 else
465 lsa = ospf6_lsdb_next (lsa);
466 }
467 }
468
469 /* Decide new Link State ID to originate.
470 note return value is network byte order */
471 u_int32_t
472 ospf6_new_ls_id (u_int16_t type, u_int32_t adv_router,
473 struct ospf6_lsdb *lsdb)
474 {
475 struct ospf6_lsa *lsa;
476 u_int32_t id = 1;
477
478 for (lsa = ospf6_lsdb_type_router_head (type, adv_router, lsdb); lsa;
479 lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa))
480 {
481 if (ntohl (lsa->header->id) < id)
482 continue;
483 if (ntohl (lsa->header->id) > id)
484 break;
485 id++;
486 }
487
488 return ((u_int32_t) htonl (id));
489 }
490
491 /* Decide new LS sequence number to originate.
492 note return value is network byte order */
493 u_int32_t
494 ospf6_new_ls_seqnum (u_int16_t type, u_int32_t id, u_int32_t adv_router,
495 struct ospf6_lsdb *lsdb)
496 {
497 struct ospf6_lsa *lsa;
498 signed long seqnum = 0;
499
500 /* if current database copy not found, return InitialSequenceNumber */
501 lsa = ospf6_lsdb_lookup (type, id, adv_router, lsdb);
502 if (lsa == NULL)
503 seqnum = INITIAL_SEQUENCE_NUMBER;
504 else
505 seqnum = (signed long) ntohl (lsa->header->seqnum) + 1;
506
507 return ((u_int32_t) htonl (seqnum));
508 }
509
510