]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_lsdb.c
Merge pull request #9238 from leonshaw/fix/netns-delete
[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 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
19 */
20
21 #include <zebra.h>
22
23 #include "memory.h"
24 #include "log.h"
25 #include "command.h"
26 #include "prefix.h"
27 #include "table.h"
28 #include "vty.h"
29
30 #include "ospf6_proto.h"
31 #include "ospf6_lsa.h"
32 #include "ospf6_lsdb.h"
33 #include "ospf6_asbr.h"
34 #include "ospf6_route.h"
35 #include "ospf6d.h"
36 #include "bitfield.h"
37
38 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_LSDB, "OSPF6 LSA database");
39
40 struct ospf6_lsdb *ospf6_lsdb_create(void *data)
41 {
42 struct ospf6_lsdb *lsdb;
43
44 lsdb = XCALLOC(MTYPE_OSPF6_LSDB, sizeof(struct ospf6_lsdb));
45 memset(lsdb, 0, sizeof(struct ospf6_lsdb));
46
47 lsdb->data = data;
48 lsdb->table = route_table_init();
49 return lsdb;
50 }
51
52 void ospf6_lsdb_delete(struct ospf6_lsdb *lsdb)
53 {
54 if (lsdb != NULL) {
55 ospf6_lsdb_remove_all(lsdb);
56 route_table_finish(lsdb->table);
57 XFREE(MTYPE_OSPF6_LSDB, lsdb);
58 }
59 }
60
61 static void ospf6_lsdb_set_key(struct prefix_ipv6 *key, const void *value,
62 int len)
63 {
64 assert(key->prefixlen % 8 == 0);
65
66 memcpy((caddr_t)&key->prefix + key->prefixlen / 8, (caddr_t)value, len);
67 key->family = AF_INET6;
68 key->prefixlen += len * 8;
69 }
70
71 #ifdef DEBUG
72 static void _lsdb_count_assert(struct ospf6_lsdb *lsdb)
73 {
74 struct ospf6_lsa *debug, *debugnext;
75 unsigned int num = 0;
76 for (ALL_LSDB(lsdb, debug, debugnext))
77 num++;
78
79 if (num == lsdb->count)
80 return;
81
82 zlog_debug("PANIC !! lsdb[%p]->count = %d, real = %d", lsdb,
83 lsdb->count, num);
84 for (ALL_LSDB(lsdb, debug, debugnext))
85 zlog_debug("%s lsdb[%p]", debug->name, debug->lsdb);
86 zlog_debug("DUMP END");
87
88 assert(num == lsdb->count);
89 }
90 #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t))
91 #else /*DEBUG*/
92 #define ospf6_lsdb_count_assert(t) ((void) 0)
93 #endif /*DEBUG*/
94
95 void ospf6_lsdb_add(struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
96 {
97 struct prefix_ipv6 key;
98 struct route_node *current;
99 struct ospf6_lsa *old = NULL;
100
101 memset(&key, 0, sizeof(key));
102 ospf6_lsdb_set_key(&key, &lsa->header->type, sizeof(lsa->header->type));
103 ospf6_lsdb_set_key(&key, &lsa->header->adv_router,
104 sizeof(lsa->header->adv_router));
105 ospf6_lsdb_set_key(&key, &lsa->header->id, sizeof(lsa->header->id));
106
107 current = route_node_get(lsdb->table, (struct prefix *)&key);
108 old = current->info;
109 current->info = lsa;
110 lsa->rn = current;
111 ospf6_lsa_lock(lsa);
112
113 if (!old) {
114 lsdb->count++;
115
116 if (OSPF6_LSA_IS_MAXAGE(lsa)) {
117 if (lsdb->hook_remove)
118 (*lsdb->hook_remove)(lsa);
119 } else {
120 if (lsdb->hook_add)
121 (*lsdb->hook_add)(lsa);
122 }
123 } else {
124 if (OSPF6_LSA_IS_CHANGED(old, lsa)) {
125 if (OSPF6_LSA_IS_MAXAGE(lsa)) {
126 if (lsdb->hook_remove) {
127 (*lsdb->hook_remove)(old);
128 (*lsdb->hook_remove)(lsa);
129 }
130 } else if (OSPF6_LSA_IS_MAXAGE(old)) {
131 if (lsdb->hook_add)
132 (*lsdb->hook_add)(lsa);
133 } else {
134 if (lsdb->hook_remove)
135 (*lsdb->hook_remove)(old);
136 if (lsdb->hook_add)
137 (*lsdb->hook_add)(lsa);
138 }
139 }
140 /* to free the lookup lock in node get*/
141 route_unlock_node(current);
142 ospf6_lsa_unlock(old);
143 }
144
145 ospf6_lsdb_count_assert(lsdb);
146 }
147
148 void ospf6_lsdb_remove(struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb)
149 {
150 struct route_node *node;
151 struct prefix_ipv6 key;
152
153 memset(&key, 0, sizeof(key));
154 ospf6_lsdb_set_key(&key, &lsa->header->type, sizeof(lsa->header->type));
155 ospf6_lsdb_set_key(&key, &lsa->header->adv_router,
156 sizeof(lsa->header->adv_router));
157 ospf6_lsdb_set_key(&key, &lsa->header->id, sizeof(lsa->header->id));
158
159 node = route_node_lookup(lsdb->table, (struct prefix *)&key);
160 assert(node && node->info == lsa);
161
162 node->info = NULL;
163 lsdb->count--;
164
165 if (lsdb->hook_remove)
166 (*lsdb->hook_remove)(lsa);
167
168 route_unlock_node(node); /* to free the lookup lock */
169 route_unlock_node(node); /* to free the original lock */
170 ospf6_lsa_unlock(lsa);
171
172 ospf6_lsdb_count_assert(lsdb);
173 }
174
175 struct ospf6_lsa *ospf6_lsdb_lookup(uint16_t type, uint32_t id,
176 uint32_t adv_router,
177 struct ospf6_lsdb *lsdb)
178 {
179 struct route_node *node;
180 struct prefix_ipv6 key;
181
182 if (lsdb == NULL)
183 return NULL;
184
185 memset(&key, 0, sizeof(key));
186 ospf6_lsdb_set_key(&key, &type, sizeof(type));
187 ospf6_lsdb_set_key(&key, &adv_router, sizeof(adv_router));
188 ospf6_lsdb_set_key(&key, &id, sizeof(id));
189
190 node = route_node_lookup(lsdb->table, (struct prefix *)&key);
191 if (node == NULL || node->info == NULL)
192 return NULL;
193
194 route_unlock_node(node);
195 return (struct ospf6_lsa *)node->info;
196 }
197
198 struct ospf6_lsa *ospf6_find_external_lsa(struct ospf6 *ospf6, struct prefix *p)
199 {
200 struct ospf6_route *match;
201 struct ospf6_lsa *lsa;
202 struct ospf6_external_info *info;
203
204 match = ospf6_route_lookup(p, ospf6->external_table);
205 if (match == NULL) {
206 if (IS_OSPF6_DEBUG_ASBR)
207 zlog_debug("No such route %pFX to withdraw", p);
208
209 return NULL;
210 }
211
212 info = match->route_option;
213 assert(info);
214
215 lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
216 htonl(info->id), ospf6->router_id, ospf6->lsdb);
217 return lsa;
218 }
219
220 struct ospf6_lsa *ospf6_lsdb_lookup_next(uint16_t type, uint32_t id,
221 uint32_t adv_router,
222 struct ospf6_lsdb *lsdb)
223 {
224 struct route_node *node;
225 struct prefix_ipv6 key;
226
227 if (lsdb == NULL)
228 return NULL;
229
230 memset(&key, 0, sizeof(key));
231 ospf6_lsdb_set_key(&key, &type, sizeof(type));
232 ospf6_lsdb_set_key(&key, &adv_router, sizeof(adv_router));
233 ospf6_lsdb_set_key(&key, &id, sizeof(id));
234
235 zlog_debug("lsdb_lookup_next: key: %pFX", &key);
236
237 node = route_table_get_next(lsdb->table, &key);
238
239 /* skip to real existing entry */
240 while (node && node->info == NULL)
241 node = route_next(node);
242
243 if (!node)
244 return NULL;
245
246 route_unlock_node(node);
247 if (!node->info)
248 return NULL;
249
250 return (struct ospf6_lsa *)node->info;
251 }
252
253 const struct route_node *ospf6_lsdb_head(struct ospf6_lsdb *lsdb, int argmode,
254 uint16_t type, uint32_t adv_router,
255 struct ospf6_lsa **lsa)
256 {
257 struct route_node *node, *end;
258
259 *lsa = NULL;
260
261 if (argmode > 0) {
262 struct prefix_ipv6 key = {.family = AF_INET6, .prefixlen = 0};
263
264 ospf6_lsdb_set_key(&key, &type, sizeof(type));
265 if (argmode > 1)
266 ospf6_lsdb_set_key(&key, &adv_router,
267 sizeof(adv_router));
268
269 node = route_table_get_next(lsdb->table, &key);
270 if (!node || !prefix_match((struct prefix *)&key, &node->p))
271 return NULL;
272
273 for (end = node; end && end->parent
274 && end->parent->p.prefixlen >= key.prefixlen;
275 end = end->parent)
276 ;
277 } else {
278 node = route_top(lsdb->table);
279 end = NULL;
280 }
281
282 while (node && !node->info)
283 node = route_next_until(node, end);
284
285 if (!node)
286 return NULL;
287 if (!node->info) {
288 route_unlock_node(node);
289 return NULL;
290 }
291
292 *lsa = node->info;
293 ospf6_lsa_lock(*lsa);
294
295 return end;
296 }
297
298 struct ospf6_lsa *ospf6_lsdb_next(const struct route_node *iterend,
299 struct ospf6_lsa *lsa)
300 {
301 struct route_node *node = lsa->rn;
302
303 ospf6_lsa_unlock(lsa);
304
305 do
306 node = route_next_until(node, iterend);
307 while (node && !node->info);
308
309 if (node && node->info) {
310 struct ospf6_lsa *next = node->info;
311 ospf6_lsa_lock(next);
312 return next;
313 }
314
315 if (node)
316 route_unlock_node(node);
317 return NULL;
318 }
319
320 void ospf6_lsdb_remove_all(struct ospf6_lsdb *lsdb)
321 {
322 struct ospf6_lsa *lsa, *lsanext;
323
324 if (lsdb == NULL)
325 return;
326
327 for (ALL_LSDB(lsdb, lsa, lsanext))
328 ospf6_lsdb_remove(lsa, lsdb);
329 }
330
331 void ospf6_lsdb_lsa_unlock(struct ospf6_lsa *lsa)
332 {
333 if (lsa != NULL) {
334 if (lsa->rn != NULL)
335 route_unlock_node(lsa->rn);
336 ospf6_lsa_unlock(lsa);
337 }
338 }
339
340 int ospf6_lsdb_maxage_remover(struct ospf6_lsdb *lsdb)
341 {
342 int reschedule = 0;
343 struct ospf6_lsa *lsa, *lsanext;
344
345 for (ALL_LSDB(lsdb, lsa, lsanext)) {
346 if (!OSPF6_LSA_IS_MAXAGE(lsa)) {
347 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa->header->type))
348 zlog_debug("Not MaxAge %s", lsa->name);
349 continue;
350 }
351
352 if (lsa->retrans_count != 0) {
353 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa->header->type))
354 zlog_debug("Remove MaxAge %s retrans_count %d",
355 lsa->name, lsa->retrans_count);
356
357 reschedule = 1;
358 continue;
359 }
360 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa->header->type))
361 zlog_debug("Remove MaxAge %s", lsa->name);
362
363 if (CHECK_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED)) {
364 UNSET_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED);
365 /*
366 * lsa->header->age = 0;
367 */
368 lsa->header->seqnum =
369 htonl(OSPF_MAX_SEQUENCE_NUMBER + 1);
370 ospf6_lsa_checksum(lsa->header);
371
372 THREAD_OFF(lsa->refresh);
373 thread_execute(master, ospf6_lsa_refresh, lsa, 0);
374 } else {
375 zlog_debug("calling ospf6_lsdb_remove %s", lsa->name);
376 ospf6_lsdb_remove(lsa, lsdb);
377 }
378 }
379
380 return (reschedule);
381 }
382
383 uint32_t ospf6_new_ls_id(uint16_t type, uint32_t adv_router,
384 struct ospf6_lsdb *lsdb)
385 {
386 struct ospf6_lsa *lsa;
387 uint32_t id = 1, tmp_id;
388
389 /* This routine is curently invoked only for Inter-Prefix LSAs for
390 * non-summarized routes (no area/range).
391 */
392 for (ALL_LSDB_TYPED_ADVRTR(lsdb, type, adv_router, lsa)) {
393 tmp_id = ntohl(lsa->header->id);
394 if (tmp_id < id)
395 continue;
396
397 if (tmp_id > id) {
398 ospf6_lsdb_lsa_unlock(lsa);
399 break;
400 }
401 id++;
402 }
403
404 return ((uint32_t)htonl(id));
405 }
406
407 /* Decide new LS sequence number to originate.
408 note return value is network byte order */
409 uint32_t ospf6_new_ls_seqnum(uint16_t type, uint32_t id, uint32_t adv_router,
410 struct ospf6_lsdb *lsdb)
411 {
412 struct ospf6_lsa *lsa;
413 signed long seqnum = 0;
414
415 /* if current database copy not found, return InitialSequenceNumber */
416 lsa = ospf6_lsdb_lookup(type, id, adv_router, lsdb);
417 if (lsa == NULL)
418 seqnum = OSPF_INITIAL_SEQUENCE_NUMBER;
419 else
420 seqnum = (signed long)ntohl(lsa->header->seqnum) + 1;
421
422 return ((uint32_t)htonl(seqnum));
423 }