]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_route.c
ospf6d: ECMP for external routes
[mirror_frr.git] / ospf6d / ospf6_route.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 "log.h"
24 #include "memory.h"
25 #include "prefix.h"
26 #include "table.h"
27 #include "vty.h"
28 #include "command.h"
29 #include "linklist.h"
30
31 #include "ospf6_proto.h"
32 #include "ospf6_lsa.h"
33 #include "ospf6_lsdb.h"
34 #include "ospf6_route.h"
35 #include "ospf6_top.h"
36 #include "ospf6_area.h"
37 #include "ospf6_interface.h"
38 #include "ospf6d.h"
39 #include "ospf6_zebra.h"
40
41 unsigned char conf_debug_ospf6_route = 0;
42
43 static char *ospf6_route_table_name(struct ospf6_route_table *table)
44 {
45 static char name[64];
46 switch (table->scope_type) {
47 case OSPF6_SCOPE_TYPE_GLOBAL: {
48 switch (table->table_type) {
49 case OSPF6_TABLE_TYPE_ROUTES:
50 snprintf(name, sizeof(name), "global route table");
51 break;
52 case OSPF6_TABLE_TYPE_BORDER_ROUTERS:
53 snprintf(name, sizeof(name), "global brouter table");
54 break;
55 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES:
56 snprintf(name, sizeof(name), "global external table");
57 break;
58 default:
59 snprintf(name, sizeof(name), "global unknown table");
60 break;
61 }
62 } break;
63
64 case OSPF6_SCOPE_TYPE_AREA: {
65 struct ospf6_area *oa = (struct ospf6_area *)table->scope;
66 switch (table->table_type) {
67 case OSPF6_TABLE_TYPE_SPF_RESULTS:
68 snprintf(name, sizeof(name), "area %s spf table",
69 oa->name);
70 break;
71 case OSPF6_TABLE_TYPE_ROUTES:
72 snprintf(name, sizeof(name), "area %s route table",
73 oa->name);
74 break;
75 case OSPF6_TABLE_TYPE_PREFIX_RANGES:
76 snprintf(name, sizeof(name), "area %s range table",
77 oa->name);
78 break;
79 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES:
80 snprintf(name, sizeof(name),
81 "area %s summary prefix table", oa->name);
82 break;
83 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS:
84 snprintf(name, sizeof(name),
85 "area %s summary router table", oa->name);
86 break;
87 default:
88 snprintf(name, sizeof(name), "area %s unknown table",
89 oa->name);
90 break;
91 }
92 } break;
93
94 case OSPF6_SCOPE_TYPE_INTERFACE: {
95 struct ospf6_interface *oi =
96 (struct ospf6_interface *)table->scope;
97 switch (table->table_type) {
98 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES:
99 snprintf(name, sizeof(name),
100 "interface %s connected table",
101 oi->interface->name);
102 break;
103 default:
104 snprintf(name, sizeof(name),
105 "interface %s unknown table",
106 oi->interface->name);
107 break;
108 }
109 } break;
110
111 default: {
112 switch (table->table_type) {
113 case OSPF6_TABLE_TYPE_SPF_RESULTS:
114 snprintf(name, sizeof(name), "temporary spf table");
115 break;
116 default:
117 snprintf(name, sizeof(name), "temporary unknown table");
118 break;
119 }
120 } break;
121 }
122 return name;
123 }
124
125 void ospf6_linkstate_prefix(u_int32_t adv_router, u_int32_t id,
126 struct prefix *prefix)
127 {
128 memset(prefix, 0, sizeof(struct prefix));
129 prefix->family = AF_INET6;
130 prefix->prefixlen = 64;
131 memcpy(&prefix->u.prefix6.s6_addr[0], &adv_router, 4);
132 memcpy(&prefix->u.prefix6.s6_addr[4], &id, 4);
133 }
134
135 void ospf6_linkstate_prefix2str(struct prefix *prefix, char *buf, int size)
136 {
137 u_int32_t adv_router, id;
138 char adv_router_str[16], id_str[16];
139 memcpy(&adv_router, &prefix->u.prefix6.s6_addr[0], 4);
140 memcpy(&id, &prefix->u.prefix6.s6_addr[4], 4);
141 inet_ntop(AF_INET, &adv_router, adv_router_str, sizeof(adv_router_str));
142 inet_ntop(AF_INET, &id, id_str, sizeof(id_str));
143 if (ntohl(id))
144 snprintf(buf, size, "%s Net-ID: %s", adv_router_str, id_str);
145 else
146 snprintf(buf, size, "%s", adv_router_str);
147 }
148
149 /* Global strings for logging */
150 const char *ospf6_dest_type_str[OSPF6_DEST_TYPE_MAX] = {
151 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
152 };
153
154 const char *ospf6_dest_type_substr[OSPF6_DEST_TYPE_MAX] = {
155 "?", "R", "N", "D", "L", "A",
156 };
157
158 const char *ospf6_path_type_str[OSPF6_PATH_TYPE_MAX] = {
159 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
160 };
161
162 const char *ospf6_path_type_substr[OSPF6_PATH_TYPE_MAX] = {
163 "??", "IA", "IE", "E1", "E2",
164 };
165
166
167 struct ospf6_nexthop *ospf6_nexthop_create(void)
168 {
169 struct ospf6_nexthop *nh;
170
171 nh = XCALLOC(MTYPE_OSPF6_NEXTHOP, sizeof(struct ospf6_nexthop));
172 return nh;
173 }
174
175 void ospf6_nexthop_delete(struct ospf6_nexthop *nh)
176 {
177 if (nh)
178 XFREE(MTYPE_OSPF6_NEXTHOP, nh);
179 }
180
181 void ospf6_clear_nexthops(struct list *nh_list)
182 {
183 struct listnode *node;
184 struct ospf6_nexthop *nh;
185
186 if (nh_list) {
187 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
188 ospf6_nexthop_clear(nh);
189 }
190 }
191
192 static struct ospf6_nexthop *
193 ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
194 {
195 struct listnode *node;
196 struct ospf6_nexthop *nh;
197
198 if (nh_list && nh_match) {
199 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
200 if (ospf6_nexthop_is_same(nh, nh_match))
201 return (nh);
202 }
203 }
204
205 return (NULL);
206 }
207
208 void ospf6_copy_nexthops(struct list *dst, struct list *src)
209 {
210 struct ospf6_nexthop *nh_new, *nh;
211 struct listnode *node;
212
213 if (dst && src) {
214 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
215 if (ospf6_nexthop_is_set(nh)) {
216 nh_new = ospf6_nexthop_create();
217 ospf6_nexthop_copy(nh_new, nh);
218 listnode_add_sort(dst, nh_new);
219 }
220 }
221 }
222 }
223
224 void ospf6_merge_nexthops(struct list *dst, struct list *src)
225 {
226 struct listnode *node;
227 struct ospf6_nexthop *nh, *nh_new;
228
229 if (src && dst) {
230 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
231 if (!ospf6_route_find_nexthop(dst, nh)) {
232 nh_new = ospf6_nexthop_create();
233 ospf6_nexthop_copy(nh_new, nh);
234 listnode_add_sort(dst, nh_new);
235 }
236 }
237 }
238 }
239
240 int ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
241 {
242 struct listnode *anode, *bnode;
243 struct ospf6_nexthop *anh, *bnh;
244 bool identical = false;
245
246 if (a && b) {
247 if (listcount(a->nh_list) == listcount(b->nh_list)) {
248 for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
249 identical = false;
250 for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
251 bnh)) {
252 if (ospf6_nexthop_is_same(anh, bnh))
253 identical = true;
254 }
255 /* Currnet List A element not found List B
256 * Non-Identical lists return */
257 if (identical == false)
258 return 1;
259 }
260 return 0;
261 } else
262 return 1;
263 }
264 /* One of the routes doesn't exist ? */
265 return (1);
266 }
267
268 int ospf6_num_nexthops(struct list *nh_list)
269 {
270 return (listcount(nh_list));
271 }
272
273 void ospf6_add_nexthop(struct list *nh_list, int ifindex, struct in6_addr *addr)
274 {
275 struct ospf6_nexthop *nh;
276 struct ospf6_nexthop nh_match;
277
278 if (nh_list) {
279 nh_match.ifindex = ifindex;
280 if (addr != NULL)
281 memcpy(&nh_match.address, addr,
282 sizeof(struct in6_addr));
283 else
284 memset(&nh_match.address, 0, sizeof(struct in6_addr));
285
286 if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
287 nh = ospf6_nexthop_create();
288 ospf6_nexthop_copy(nh, &nh_match);
289 listnode_add(nh_list, nh);
290 }
291 }
292 }
293
294 void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
295 struct zapi_nexthop nexthops[],
296 int entries)
297 {
298 struct ospf6_nexthop *nh;
299 struct listnode *node;
300 char buf[64];
301 int i;
302
303 if (route) {
304 i = 0;
305 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
306 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
307 const char *ifname;
308 inet_ntop(AF_INET6, &nh->address, buf,
309 sizeof(buf));
310 ifname = ifindex2ifname(nh->ifindex,
311 VRF_DEFAULT);
312 zlog_debug(" nexthop: %s%%%.*s(%d)", buf,
313 IFNAMSIZ, ifname, nh->ifindex);
314 }
315 if (i >= entries)
316 return;
317
318 nexthops[i].vrf_id = VRF_DEFAULT;
319 nexthops[i].ifindex = nh->ifindex;
320 if (!IN6_IS_ADDR_UNSPECIFIED(&nh->address)) {
321 nexthops[i].gate.ipv6 = nh->address;
322 nexthops[i].type = NEXTHOP_TYPE_IPV6_IFINDEX;
323 } else
324 nexthops[i].type = NEXTHOP_TYPE_IFINDEX;
325 i++;
326 }
327 }
328 }
329
330 int ospf6_route_get_first_nh_index(struct ospf6_route *route)
331 {
332 struct ospf6_nexthop *nh;
333
334 if (route) {
335 if ((nh = (struct ospf6_nexthop *)listhead(route->nh_list)))
336 return (nh->ifindex);
337 }
338
339 return (-1);
340 }
341
342 int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
343 {
344 if (a->ifindex < b->ifindex)
345 return -1;
346 else if (a->ifindex > b->ifindex)
347 return 1;
348 else
349 return memcmp(&a->address, &b->address,
350 sizeof(struct in6_addr));
351
352 return 0;
353 }
354
355 static int ospf6_path_cmp(struct ospf6_path *a, struct ospf6_path *b)
356 {
357 if (a->origin.adv_router < b->origin.adv_router)
358 return -1;
359 else if (a->origin.adv_router > b->origin.adv_router)
360 return 1;
361 else
362 return 0;
363 }
364
365 void ospf6_path_free(struct ospf6_path *op)
366 {
367 if (op->nh_list)
368 list_delete_and_null(&op->nh_list);
369 XFREE(MTYPE_OSPF6_PATH, op);
370 }
371
372 struct ospf6_path *ospf6_path_dup(struct ospf6_path *path)
373 {
374 struct ospf6_path *new;
375
376 new = XCALLOC(MTYPE_OSPF6_PATH, sizeof(struct ospf6_path));
377 memcpy(new, path, sizeof(struct ospf6_path));
378 new->nh_list = list_new();
379 new->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
380 new->nh_list->del = (void (*) (void *))ospf6_nexthop_delete;
381
382 return new;
383 }
384
385 struct ospf6_route *ospf6_route_create(void)
386 {
387 struct ospf6_route *route;
388 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
389 route->nh_list = list_new();
390 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
391 route->nh_list->del = (void (*) (void *))ospf6_nexthop_delete;
392 route->paths = list_new();
393 route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
394 route->paths->del = (void (*)(void *))ospf6_path_free;
395 return route;
396 }
397
398 void ospf6_route_delete(struct ospf6_route *route)
399 {
400 if (route) {
401 if (route->nh_list)
402 list_delete_and_null(&route->nh_list);
403 if (route->paths)
404 list_delete_and_null(&route->paths);
405 XFREE(MTYPE_OSPF6_ROUTE, route);
406 }
407 }
408
409 struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
410 {
411 struct ospf6_route *new;
412
413 new = ospf6_route_create();
414 new->type = route->type;
415 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
416 new->installed = route->installed;
417 new->changed = route->changed;
418 new->flag = route->flag;
419 new->route_option = route->route_option;
420 new->linkstate_id = route->linkstate_id;
421 new->path = route->path;
422 ospf6_copy_nexthops(new->nh_list, route->nh_list);
423 new->rnode = NULL;
424 new->prev = NULL;
425 new->next = NULL;
426 new->table = NULL;
427 new->lock = 0;
428 return new;
429 }
430
431 void ospf6_route_lock(struct ospf6_route *route)
432 {
433 route->lock++;
434 }
435
436 void ospf6_route_unlock(struct ospf6_route *route)
437 {
438 assert(route->lock > 0);
439 route->lock--;
440 if (route->lock == 0) {
441 /* Can't detach from the table until here
442 because ospf6_route_next () will use
443 the 'route->table' pointer for logging */
444 route->table = NULL;
445 ospf6_route_delete(route);
446 }
447 }
448
449 /* Route compare function. If ra is more preferred, it returns
450 less than 0. If rb is more preferred returns greater than 0.
451 Otherwise (neither one is preferred), returns 0 */
452 int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
453 {
454 assert(ospf6_route_is_same(ra, rb));
455 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
456 && ra->path.type < OSPF6_PATH_TYPE_MAX);
457 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
458 && rb->path.type < OSPF6_PATH_TYPE_MAX);
459
460 if (ra->type != rb->type)
461 return (ra->type - rb->type);
462
463 if (ra->path.area_id != rb->path.area_id)
464 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
465
466 if (ra->path.type != rb->path.type)
467 return (ra->path.type - rb->path.type);
468
469 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
470 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
471 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
472 else
473 return (ra->path.cost - rb->path.cost);
474 } else {
475 if (ra->path.cost != rb->path.cost)
476 return (ra->path.cost - rb->path.cost);
477 }
478
479 return 0;
480 }
481
482 struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
483 struct ospf6_route_table *table)
484 {
485 struct route_node *node;
486 struct ospf6_route *route;
487
488 node = route_node_lookup(table->table, prefix);
489 if (node == NULL)
490 return NULL;
491
492 route = (struct ospf6_route *)node->info;
493 route_unlock_node(node); /* to free the lookup lock */
494 return route;
495 }
496
497 struct ospf6_route *
498 ospf6_route_lookup_identical(struct ospf6_route *route,
499 struct ospf6_route_table *table)
500 {
501 struct ospf6_route *target;
502
503 for (target = ospf6_route_lookup(&route->prefix, table); target;
504 target = target->next) {
505 if (target->type == route->type &&
506 (memcmp(&target->prefix, &route->prefix,
507 sizeof(struct prefix)) == 0) &&
508 target->path.type == route->path.type &&
509 target->path.cost == route->path.cost &&
510 target->path.u.cost_e2 == route->path.u.cost_e2 &&
511 ospf6_route_cmp_nexthops(target, route) == 0)
512 return target;
513 }
514 return NULL;
515 }
516
517 struct ospf6_route *
518 ospf6_route_lookup_bestmatch(struct prefix *prefix,
519 struct ospf6_route_table *table)
520 {
521 struct route_node *node;
522 struct ospf6_route *route;
523
524 node = route_node_match(table->table, prefix);
525 if (node == NULL)
526 return NULL;
527 route_unlock_node(node);
528
529 route = (struct ospf6_route *)node->info;
530 return route;
531 }
532
533 #ifdef DEBUG
534 static void route_table_assert(struct ospf6_route_table *table)
535 {
536 struct ospf6_route *prev, *r, *next;
537 char buf[PREFIX2STR_BUFFER];
538 unsigned int link_error = 0, num = 0;
539
540 r = ospf6_route_head(table);
541 prev = NULL;
542 while (r) {
543 if (r->prev != prev)
544 link_error++;
545
546 next = ospf6_route_next(r);
547
548 if (r->next != next)
549 link_error++;
550
551 prev = r;
552 r = next;
553 }
554
555 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
556 num++;
557
558 if (link_error == 0 && num == table->count)
559 return;
560
561 zlog_err("PANIC !!");
562 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table);
563 zlog_debug("table count = %d, real number = %d", table->count, num);
564 zlog_debug("DUMP START");
565 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r)) {
566 prefix2str(&r->prefix, buf, sizeof(buf));
567 zlog_info("%p<-[%p]->%p : %s", r->prev, r, r->next, buf);
568 }
569 zlog_debug("DUMP END");
570
571 assert(link_error == 0 && num == table->count);
572 }
573 #define ospf6_route_table_assert(t) (route_table_assert (t))
574 #else
575 #define ospf6_route_table_assert(t) ((void) 0)
576 #endif /*DEBUG*/
577
578 struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
579 struct ospf6_route_table *table)
580 {
581 struct route_node *node, *nextnode, *prevnode;
582 struct ospf6_route *current = NULL;
583 struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
584 char buf[PREFIX2STR_BUFFER];
585 struct timeval now;
586
587 assert(route->rnode == NULL);
588 assert(route->lock == 0);
589 assert(route->next == NULL);
590 assert(route->prev == NULL);
591
592 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
593 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
594 else
595 prefix2str(&route->prefix, buf, sizeof(buf));
596
597 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
598 zlog_debug("%s %p: route add %p: %s",
599 ospf6_route_table_name(table), (void *)table,
600 (void *)route, buf);
601 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
602 zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
603 buf);
604
605 monotime(&now);
606
607 node = route_node_get(table->table, &route->prefix);
608 route->rnode = node;
609
610 /* find place to insert */
611 for (current = node->info; current; current = current->next) {
612 if (!ospf6_route_is_same(current, route))
613 next = current;
614 else if (current->type != route->type)
615 prev = current;
616 else if (ospf6_route_is_same_origin(current, route))
617 old = current;
618 else if (ospf6_route_cmp(current, route) > 0)
619 next = current;
620 else
621 prev = current;
622
623 if (old || next)
624 break;
625 }
626
627 if (old) {
628 /* if route does not actually change, return unchanged */
629 if (ospf6_route_is_identical(old, route)) {
630 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
631 zlog_debug(
632 "%s %p: route add %p: needless update of %p old cost %u",
633 ospf6_route_table_name(table),
634 (void *)table, (void *)route,
635 (void *)old, old->path.cost);
636 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
637 zlog_debug("%s: route add: needless update",
638 ospf6_route_table_name(table));
639
640 ospf6_route_delete(route);
641 SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
642 ospf6_route_table_assert(table);
643
644 /* to free the lookup lock */
645 route_unlock_node(node);
646 return old;
647 }
648
649 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
650 zlog_debug("%s %p: route add %p cost %u: update of %p old cost %u",
651 ospf6_route_table_name(table), (void *)table,
652 (void *)route, route->path.cost, (void *)old,
653 old->path.cost);
654 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
655 zlog_debug("%s: route add: update",
656 ospf6_route_table_name(table));
657
658 /* replace old one if exists */
659 if (node->info == old) {
660 node->info = route;
661 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
662 }
663
664 if (old->prev)
665 old->prev->next = route;
666 route->prev = old->prev;
667 if (old->next)
668 old->next->prev = route;
669 route->next = old->next;
670
671 route->installed = old->installed;
672 route->changed = now;
673 assert(route->table == NULL);
674 route->table = table;
675
676 ospf6_route_unlock(old); /* will be deleted later */
677 ospf6_route_lock(route);
678
679 SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
680 ospf6_route_table_assert(table);
681
682 if (table->hook_add)
683 (*table->hook_add)(route);
684
685 return route;
686 }
687
688 /* insert if previous or next node found */
689 if (prev || next) {
690 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
691 zlog_debug(
692 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
693 ospf6_route_table_name(table), (void *)table,
694 (void *)route, route->path.cost, (void *)prev,
695 (void *)next, node->lock);
696 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
697 zlog_debug("%s: route add cost %u: another path found",
698 ospf6_route_table_name(table),
699 route->path.cost);
700
701 if (prev == NULL)
702 prev = next->prev;
703 if (next == NULL)
704 next = prev->next;
705
706 if (prev)
707 prev->next = route;
708 route->prev = prev;
709 if (next)
710 next->prev = route;
711 route->next = next;
712
713 if (node->info == next) {
714 assert(next->rnode == node);
715 node->info = route;
716 UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
717 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
718 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
719 zlog_info(
720 "%s %p: route add %p: replacing previous best: %p",
721 ospf6_route_table_name(table),
722 (void *)table, (void *)route,
723 (void *)next);
724 }
725
726 route->installed = now;
727 route->changed = now;
728 assert(route->table == NULL);
729 route->table = table;
730
731 ospf6_route_lock(route);
732 table->count++;
733 ospf6_route_table_assert(table);
734
735 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
736 if (table->hook_add)
737 (*table->hook_add)(route);
738
739 return route;
740 }
741
742 /* Else, this is the brand new route regarding to the prefix */
743 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
744 zlog_debug("%s %p: route add %p %s : brand new route",
745 ospf6_route_table_name(table), (void *)table,
746 (void *)route, buf);
747 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
748 zlog_debug("%s: route add: brand new route",
749 ospf6_route_table_name(table));
750
751 assert(node->info == NULL);
752 node->info = route;
753 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
754 ospf6_route_lock(route);
755 route->installed = now;
756 route->changed = now;
757 assert(route->table == NULL);
758 route->table = table;
759
760 /* lookup real existing next route */
761 nextnode = node;
762 route_lock_node(nextnode);
763 do {
764 nextnode = route_next(nextnode);
765 } while (nextnode && nextnode->info == NULL);
766
767 /* set next link */
768 if (nextnode == NULL)
769 route->next = NULL;
770 else {
771 route_unlock_node(nextnode);
772
773 next = nextnode->info;
774 route->next = next;
775 next->prev = route;
776 }
777
778 /* lookup real existing prev route */
779 prevnode = node;
780 route_lock_node(prevnode);
781 do {
782 prevnode = route_prev(prevnode);
783 } while (prevnode && prevnode->info == NULL);
784
785 /* set prev link */
786 if (prevnode == NULL)
787 route->prev = NULL;
788 else {
789 route_unlock_node(prevnode);
790
791 prev = prevnode->info;
792 while (prev->next && ospf6_route_is_same(prev, prev->next))
793 prev = prev->next;
794 route->prev = prev;
795 prev->next = route;
796 }
797
798 table->count++;
799 ospf6_route_table_assert(table);
800
801 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
802 if (table->hook_add)
803 (*table->hook_add)(route);
804
805 return route;
806 }
807
808 void ospf6_route_remove(struct ospf6_route *route,
809 struct ospf6_route_table *table)
810 {
811 struct route_node *node;
812 struct ospf6_route *current;
813 char buf[PREFIX2STR_BUFFER];
814
815 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
816 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
817 else
818 prefix2str(&route->prefix, buf, sizeof(buf));
819
820 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
821 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
822 ospf6_route_table_name(table), (void *)table,
823 (void *)route, buf, route->path.cost, route->lock);
824 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
825 zlog_debug("%s: route remove: %s",
826 ospf6_route_table_name(table), buf);
827
828 node = route_node_lookup(table->table, &route->prefix);
829 assert(node);
830
831 /* find the route to remove, making sure that the route pointer
832 is from the route table. */
833 current = node->info;
834 while (current && current != route)
835 current = current->next;
836
837 assert(current == route);
838
839 /* adjust doubly linked list */
840 if (route->prev)
841 route->prev->next = route->next;
842 if (route->next)
843 route->next->prev = route->prev;
844
845 if (node->info == route) {
846 if (route->next && route->next->rnode == node) {
847 node->info = route->next;
848 SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
849 } else {
850 node->info = NULL;
851 route->rnode = NULL;
852 route_unlock_node(node); /* to free the original lock */
853 }
854 }
855
856 route_unlock_node(node); /* to free the lookup lock */
857 table->count--;
858 ospf6_route_table_assert(table);
859
860 SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
861
862 /* Note hook_remove may call ospf6_route_remove */
863 if (table->hook_remove)
864 (*table->hook_remove)(route);
865
866 ospf6_route_unlock(route);
867 }
868
869 struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
870 {
871 struct route_node *node;
872 struct ospf6_route *route;
873
874 node = route_top(table->table);
875 if (node == NULL)
876 return NULL;
877
878 /* skip to the real existing entry */
879 while (node && node->info == NULL)
880 node = route_next(node);
881 if (node == NULL)
882 return NULL;
883
884 route_unlock_node(node);
885 assert(node->info);
886
887 route = (struct ospf6_route *)node->info;
888 assert(route->prev == NULL);
889 assert(route->table == table);
890 ospf6_route_lock(route);
891
892 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
893 zlog_info("%s %p: route head: %p<-[%p]->%p",
894 ospf6_route_table_name(table), (void *)table,
895 (void *)route->prev, (void *)route,
896 (void *)route->next);
897
898 return route;
899 }
900
901 struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
902 {
903 struct ospf6_route *next = route->next;
904
905 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
906 zlog_info("%s %p: route next: %p<-[%p]->%p",
907 ospf6_route_table_name(route->table),
908 (void *)route->table, (void *)route->prev,
909 (void *)route, (void *)route->next);
910
911 ospf6_route_unlock(route);
912 if (next)
913 ospf6_route_lock(next);
914
915 return next;
916 }
917
918 struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
919 {
920 struct route_node *rnode;
921 struct ospf6_route *next;
922
923 ospf6_route_unlock(route);
924
925 rnode = route->rnode;
926 route_lock_node(rnode);
927 rnode = route_next(rnode);
928 while (rnode && rnode->info == NULL)
929 rnode = route_next(rnode);
930 if (rnode == NULL)
931 return NULL;
932 route_unlock_node(rnode);
933
934 assert(rnode->info);
935 next = (struct ospf6_route *)rnode->info;
936 ospf6_route_lock(next);
937 return next;
938 }
939
940 struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
941 struct ospf6_route_table *table)
942 {
943 struct route_node *node;
944 struct ospf6_route *route;
945
946 /* Walk down tree. */
947 node = table->table->top;
948 while (node && node->p.prefixlen < prefix->prefixlen
949 && prefix_match(&node->p, prefix))
950 node = node->link[prefix_bit(&prefix->u.prefix,
951 node->p.prefixlen)];
952
953 if (node)
954 route_lock_node(node);
955 while (node && node->info == NULL)
956 node = route_next(node);
957 if (node == NULL)
958 return NULL;
959 route_unlock_node(node);
960
961 if (!prefix_match(prefix, &node->p))
962 return NULL;
963
964 route = node->info;
965 ospf6_route_lock(route);
966 return route;
967 }
968
969 struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
970 struct ospf6_route *route)
971 {
972 struct ospf6_route *next;
973
974 next = ospf6_route_next(route);
975 if (next && !prefix_match(prefix, &next->prefix)) {
976 ospf6_route_unlock(next);
977 next = NULL;
978 }
979
980 return next;
981 }
982
983 void ospf6_route_remove_all(struct ospf6_route_table *table)
984 {
985 struct ospf6_route *route;
986 for (route = ospf6_route_head(table); route;
987 route = ospf6_route_next(route))
988 ospf6_route_remove(route, table);
989 }
990
991 struct ospf6_route_table *ospf6_route_table_create(int s, int t)
992 {
993 struct ospf6_route_table *new;
994 new = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route_table));
995 new->table = route_table_init();
996 new->scope_type = s;
997 new->table_type = t;
998 return new;
999 }
1000
1001 void ospf6_route_table_delete(struct ospf6_route_table *table)
1002 {
1003 ospf6_route_remove_all(table);
1004 bf_free(table->idspace);
1005 route_table_finish(table->table);
1006 XFREE(MTYPE_OSPF6_ROUTE, table);
1007 }
1008
1009
1010 /* VTY commands */
1011 void ospf6_route_show(struct vty *vty, struct ospf6_route *route)
1012 {
1013 int i;
1014 char destination[PREFIX2STR_BUFFER], nexthop[64];
1015 char duration[64];
1016 const char *ifname;
1017 struct timeval now, res;
1018 struct listnode *node;
1019 struct ospf6_nexthop *nh;
1020
1021 monotime(&now);
1022 timersub(&now, &route->changed, &res);
1023 timerstring(&res, duration, sizeof(duration));
1024
1025 /* destination */
1026 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1027 ospf6_linkstate_prefix2str(&route->prefix, destination,
1028 sizeof(destination));
1029 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1030 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1031 destination, sizeof(destination));
1032 else
1033 prefix2str(&route->prefix, destination, sizeof(destination));
1034
1035 i = 0;
1036 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1037 /* nexthop */
1038 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1039 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1040
1041 if (!i) {
1042 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1043 (ospf6_route_is_best(route) ? '*' : ' '),
1044 OSPF6_DEST_TYPE_SUBSTR(route->type),
1045 OSPF6_PATH_TYPE_SUBSTR(route->path.type),
1046 destination, nexthop, IFNAMSIZ, ifname,
1047 duration);
1048 i++;
1049 } else
1050 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
1051 "", "", "", nexthop, IFNAMSIZ, ifname, "");
1052 }
1053 }
1054
1055 void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route)
1056 {
1057 const char *ifname;
1058 char destination[PREFIX2STR_BUFFER], nexthop[64];
1059 char area_id[16], id[16], adv_router[16], capa[16], options[16];
1060 struct timeval now, res;
1061 char duration[64];
1062 struct listnode *node;
1063 struct ospf6_nexthop *nh;
1064
1065 monotime(&now);
1066
1067 /* destination */
1068 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1069 ospf6_linkstate_prefix2str(&route->prefix, destination,
1070 sizeof(destination));
1071 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1072 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1073 destination, sizeof(destination));
1074 else
1075 prefix2str(&route->prefix, destination, sizeof(destination));
1076 vty_out(vty, "Destination: %s\n", destination);
1077
1078 /* destination type */
1079 vty_out(vty, "Destination type: %s\n",
1080 OSPF6_DEST_TYPE_NAME(route->type));
1081
1082 /* Time */
1083 timersub(&now, &route->installed, &res);
1084 timerstring(&res, duration, sizeof(duration));
1085 vty_out(vty, "Installed Time: %s ago\n", duration);
1086
1087 timersub(&now, &route->changed, &res);
1088 timerstring(&res, duration, sizeof(duration));
1089 vty_out(vty, " Changed Time: %s ago\n", duration);
1090
1091 /* Debugging info */
1092 vty_out(vty, "Lock: %d Flags: %s%s%s%s\n", route->lock,
1093 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1094 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1095 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R" : "-"),
1096 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C" : "-"));
1097 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1098 (void *)route->prev, (void *)route, (void *)route->next);
1099
1100 /* Path section */
1101
1102 /* Area-ID */
1103 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
1104 vty_out(vty, "Associated Area: %s\n", area_id);
1105
1106 /* Path type */
1107 vty_out(vty, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route->path.type));
1108
1109 /* LS Origin */
1110 inet_ntop(AF_INET, &route->path.origin.id, id, sizeof(id));
1111 inet_ntop(AF_INET, &route->path.origin.adv_router, adv_router,
1112 sizeof(adv_router));
1113 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1114 ospf6_lstype_name(route->path.origin.type), id, adv_router);
1115
1116 /* Options */
1117 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1118 vty_out(vty, "Options: %s\n", options);
1119
1120 /* Router Bits */
1121 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
1122 vty_out(vty, "Router Bits: %s\n", capa);
1123
1124 /* Prefix Options */
1125 vty_out(vty, "Prefix Options: xxx\n");
1126
1127 /* Metrics */
1128 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1129 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1130 route->path.u.cost_e2);
1131
1132 vty_out(vty, "Paths count: %u\n", route->paths->count);
1133 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
1134 /* Nexthops */
1135 vty_out(vty, "Nexthop:\n");
1136 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1137 /* nexthop */
1138 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1139 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1140 vty_out(vty, " %s %.*s\n", nexthop, IFNAMSIZ, ifname);
1141 }
1142 vty_out(vty, "\n");
1143 }
1144
1145 static void ospf6_route_show_table_summary(struct vty *vty,
1146 struct ospf6_route_table *table)
1147 {
1148 struct ospf6_route *route, *prev = NULL;
1149 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1150 unsigned int number = 0;
1151 int nh_count = 0, nhinval = 0, ecmp = 0;
1152 int alternative = 0, destination = 0;
1153
1154 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1155 pathtype[i] = 0;
1156
1157 for (route = ospf6_route_head(table); route;
1158 route = ospf6_route_next(route)) {
1159 if (prev == NULL || !ospf6_route_is_same(prev, route))
1160 destination++;
1161 else
1162 alternative++;
1163 nh_count = ospf6_num_nexthops(route->nh_list);
1164 if (!nh_count)
1165 nhinval++;
1166 else if (nh_count > 1)
1167 ecmp++;
1168 pathtype[route->path.type]++;
1169 number++;
1170
1171 prev = route;
1172 }
1173
1174 assert(number == table->count);
1175
1176 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1177 vty_out(vty, "Number of Destination: %d\n", destination);
1178 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1179 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1180 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
1181 vty_out(vty, "Number of %s routes: %d\n",
1182 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
1183 }
1184 }
1185
1186 static void ospf6_route_show_table_prefix(struct vty *vty,
1187 struct prefix *prefix,
1188 struct ospf6_route_table *table)
1189 {
1190 struct ospf6_route *route;
1191
1192 route = ospf6_route_lookup(prefix, table);
1193 if (route == NULL)
1194 return;
1195
1196 ospf6_route_lock(route);
1197 while (route && ospf6_route_is_prefix(prefix, route)) {
1198 /* Specifying a prefix will always display details */
1199 ospf6_route_show_detail(vty, route);
1200 route = ospf6_route_next(route);
1201 }
1202 if (route)
1203 ospf6_route_unlock(route);
1204 }
1205
1206 static void ospf6_route_show_table_address(struct vty *vty,
1207 struct prefix *prefix,
1208 struct ospf6_route_table *table)
1209 {
1210 struct ospf6_route *route;
1211
1212 route = ospf6_route_lookup_bestmatch(prefix, table);
1213 if (route == NULL)
1214 return;
1215
1216 prefix = &route->prefix;
1217 ospf6_route_lock(route);
1218 while (route && ospf6_route_is_prefix(prefix, route)) {
1219 /* Specifying a prefix will always display details */
1220 ospf6_route_show_detail(vty, route);
1221 route = ospf6_route_next(route);
1222 }
1223 if (route)
1224 ospf6_route_unlock(route);
1225 }
1226
1227 static void ospf6_route_show_table_match(struct vty *vty, int detail,
1228 struct prefix *prefix,
1229 struct ospf6_route_table *table)
1230 {
1231 struct ospf6_route *route;
1232 assert(prefix->family);
1233
1234 route = ospf6_route_match_head(prefix, table);
1235 while (route) {
1236 if (detail)
1237 ospf6_route_show_detail(vty, route);
1238 else
1239 ospf6_route_show(vty, route);
1240 route = ospf6_route_match_next(prefix, route);
1241 }
1242 }
1243
1244 static void ospf6_route_show_table_type(struct vty *vty, int detail,
1245 u_char type,
1246 struct ospf6_route_table *table)
1247 {
1248 struct ospf6_route *route;
1249
1250 route = ospf6_route_head(table);
1251 while (route) {
1252 if (route->path.type == type) {
1253 if (detail)
1254 ospf6_route_show_detail(vty, route);
1255 else
1256 ospf6_route_show(vty, route);
1257 }
1258 route = ospf6_route_next(route);
1259 }
1260 }
1261
1262 static void ospf6_route_show_table(struct vty *vty, int detail,
1263 struct ospf6_route_table *table)
1264 {
1265 struct ospf6_route *route;
1266
1267 route = ospf6_route_head(table);
1268 while (route) {
1269 if (detail)
1270 ospf6_route_show_detail(vty, route);
1271 else
1272 ospf6_route_show(vty, route);
1273 route = ospf6_route_next(route);
1274 }
1275 }
1276
1277 int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1278 struct cmd_token **argv,
1279 struct ospf6_route_table *table)
1280 {
1281 int summary = 0;
1282 int match = 0;
1283 int detail = 0;
1284 int slash = 0;
1285 int isprefix = 0;
1286 int i, ret;
1287 struct prefix prefix;
1288 u_char type = 0;
1289
1290 memset(&prefix, 0, sizeof(struct prefix));
1291
1292 for (i = argc_start; i < argc; i++) {
1293 if (strmatch(argv[i]->text, "summary")) {
1294 summary++;
1295 continue;
1296 }
1297
1298 if (strmatch(argv[i]->text, "intra-area")) {
1299 type = OSPF6_PATH_TYPE_INTRA;
1300 continue;
1301 }
1302
1303 if (strmatch(argv[i]->text, "inter-area")) {
1304 type = OSPF6_PATH_TYPE_INTER;
1305 continue;
1306 }
1307
1308 if (strmatch(argv[i]->text, "external-1")) {
1309 type = OSPF6_PATH_TYPE_EXTERNAL1;
1310 continue;
1311 }
1312
1313 if (strmatch(argv[i]->text, "external-2")) {
1314 type = OSPF6_PATH_TYPE_EXTERNAL2;
1315 continue;
1316 }
1317
1318 if (strmatch(argv[i]->text, "detail")) {
1319 detail++;
1320 continue;
1321 }
1322
1323 if (strmatch(argv[i]->text, "match")) {
1324 match++;
1325 continue;
1326 }
1327
1328 ret = str2prefix(argv[i]->arg, &prefix);
1329 if (ret == 1 && prefix.family == AF_INET6) {
1330 isprefix++;
1331 if (strchr(argv[i]->arg, '/'))
1332 slash++;
1333 continue;
1334 }
1335
1336 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1337 return CMD_SUCCESS;
1338 }
1339
1340 /* Give summary of this route table */
1341 if (summary) {
1342 ospf6_route_show_table_summary(vty, table);
1343 return CMD_SUCCESS;
1344 }
1345
1346 /* Give exact prefix-match route */
1347 if (isprefix && !match) {
1348 /* If exact address, give best matching route */
1349 if (!slash)
1350 ospf6_route_show_table_address(vty, &prefix, table);
1351 else
1352 ospf6_route_show_table_prefix(vty, &prefix, table);
1353
1354 return CMD_SUCCESS;
1355 }
1356
1357 if (match)
1358 ospf6_route_show_table_match(vty, detail, &prefix, table);
1359 else if (type)
1360 ospf6_route_show_table_type(vty, detail, type, table);
1361 else
1362 ospf6_route_show_table(vty, detail, table);
1363
1364 return CMD_SUCCESS;
1365 }
1366
1367 static void ospf6_linkstate_show_header(struct vty *vty)
1368 {
1369 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1370 "Net-ID", "Rtr-Bits", "Options", "Cost");
1371 }
1372
1373 static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
1374 {
1375 u_int32_t router, id;
1376 char routername[16], idname[16], rbits[16], options[16];
1377
1378 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1379 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1380 id = ospf6_linkstate_prefix_id(&route->prefix);
1381 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1382
1383 ospf6_capability_printbuf(route->path.router_bits, rbits,
1384 sizeof(rbits));
1385 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1386
1387 if (ntohl(id))
1388 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1389 routername, idname, rbits, options,
1390 (unsigned long)route->path.cost);
1391 else
1392 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1393 routername, idname, rbits, options,
1394 (unsigned long)route->path.cost);
1395 }
1396
1397
1398 static void ospf6_linkstate_show_table_exact(struct vty *vty,
1399 struct prefix *prefix,
1400 struct ospf6_route_table *table)
1401 {
1402 struct ospf6_route *route;
1403
1404 route = ospf6_route_lookup(prefix, table);
1405 if (route == NULL)
1406 return;
1407
1408 ospf6_route_lock(route);
1409 while (route && ospf6_route_is_prefix(prefix, route)) {
1410 /* Specifying a prefix will always display details */
1411 ospf6_route_show_detail(vty, route);
1412 route = ospf6_route_next(route);
1413 }
1414 if (route)
1415 ospf6_route_unlock(route);
1416 }
1417
1418 static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1419 struct ospf6_route_table *table)
1420 {
1421 struct ospf6_route *route;
1422
1423 if (!detail)
1424 ospf6_linkstate_show_header(vty);
1425
1426 route = ospf6_route_head(table);
1427 while (route) {
1428 if (detail)
1429 ospf6_route_show_detail(vty, route);
1430 else
1431 ospf6_linkstate_show(vty, route);
1432 route = ospf6_route_next(route);
1433 }
1434 }
1435
1436 int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1437 struct cmd_token **argv,
1438 struct ospf6_route_table *table)
1439 {
1440 int detail = 0;
1441 int is_id = 0;
1442 int is_router = 0;
1443 int i, ret;
1444 struct prefix router, id, prefix;
1445
1446 memset(&router, 0, sizeof(struct prefix));
1447 memset(&id, 0, sizeof(struct prefix));
1448 memset(&prefix, 0, sizeof(struct prefix));
1449
1450 for (i = idx_ipv4; i < argc; i++) {
1451 if (strmatch(argv[i]->text, "detail")) {
1452 detail++;
1453 continue;
1454 }
1455
1456 if (!is_router) {
1457 ret = str2prefix(argv[i]->arg, &router);
1458 if (ret == 1 && router.family == AF_INET) {
1459 is_router++;
1460 continue;
1461 }
1462 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1463 return CMD_SUCCESS;
1464 }
1465
1466 if (!is_id) {
1467 ret = str2prefix(argv[i]->arg, &id);
1468 if (ret == 1 && id.family == AF_INET) {
1469 is_id++;
1470 continue;
1471 }
1472 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1473 return CMD_SUCCESS;
1474 }
1475
1476 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1477 return CMD_SUCCESS;
1478 }
1479
1480 if (is_router)
1481 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1482 id.u.prefix4.s_addr, &prefix);
1483
1484 if (prefix.family)
1485 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1486 else
1487 ospf6_linkstate_show_table(vty, detail, table);
1488
1489 return CMD_SUCCESS;
1490 }
1491
1492
1493 void ospf6_brouter_show_header(struct vty *vty)
1494 {
1495 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1496 "Options", "Path-Type", "Area");
1497 }
1498
1499 void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
1500 {
1501 u_int32_t adv_router;
1502 char adv[16], rbits[16], options[16], area[16];
1503
1504 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1505 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1506 ospf6_capability_printbuf(route->path.router_bits, rbits,
1507 sizeof(rbits));
1508 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1509 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1510
1511 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1512 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1513 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1514 OSPF6_PATH_TYPE_NAME(route->path.type), area);
1515 }
1516
1517 DEFUN (debug_ospf6_route,
1518 debug_ospf6_route_cmd,
1519 "debug ospf6 route <table|intra-area|inter-area|memory>",
1520 DEBUG_STR
1521 OSPF6_STR
1522 "Debug routes\n"
1523 "Debug route table calculation\n"
1524 "Debug intra-area route calculation\n"
1525 "Debug inter-area route calculation\n"
1526 "Debug route memory use\n"
1527 )
1528 {
1529 int idx_type = 3;
1530 unsigned char level = 0;
1531
1532 if (!strcmp(argv[idx_type]->text, "table"))
1533 level = OSPF6_DEBUG_ROUTE_TABLE;
1534 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1535 level = OSPF6_DEBUG_ROUTE_INTRA;
1536 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1537 level = OSPF6_DEBUG_ROUTE_INTER;
1538 else if (!strcmp(argv[idx_type]->text, "memory"))
1539 level = OSPF6_DEBUG_ROUTE_MEMORY;
1540 OSPF6_DEBUG_ROUTE_ON(level);
1541 return CMD_SUCCESS;
1542 }
1543
1544 DEFUN (no_debug_ospf6_route,
1545 no_debug_ospf6_route_cmd,
1546 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1547 NO_STR
1548 DEBUG_STR
1549 OSPF6_STR
1550 "Debug routes\n"
1551 "Debug route table calculation\n"
1552 "Debug intra-area route calculation\n"
1553 "Debug inter-area route calculation\n"
1554 "Debug route memory use\n")
1555 {
1556 int idx_type = 4;
1557 unsigned char level = 0;
1558
1559 if (!strcmp(argv[idx_type]->text, "table"))
1560 level = OSPF6_DEBUG_ROUTE_TABLE;
1561 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1562 level = OSPF6_DEBUG_ROUTE_INTRA;
1563 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1564 level = OSPF6_DEBUG_ROUTE_INTER;
1565 else if (!strcmp(argv[idx_type]->text, "memory"))
1566 level = OSPF6_DEBUG_ROUTE_MEMORY;
1567 OSPF6_DEBUG_ROUTE_OFF(level);
1568 return CMD_SUCCESS;
1569 }
1570
1571 int config_write_ospf6_debug_route(struct vty *vty)
1572 {
1573 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1574 vty_out(vty, "debug ospf6 route table\n");
1575 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1576 vty_out(vty, "debug ospf6 route intra-area\n");
1577 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1578 vty_out(vty, "debug ospf6 route inter-area\n");
1579 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1580 vty_out(vty, "debug ospf6 route memory\n");
1581
1582 return 0;
1583 }
1584
1585 void install_element_ospf6_debug_route(void)
1586 {
1587 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
1588 install_element(ENABLE_NODE, &no_debug_ospf6_route_cmd);
1589 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
1590 install_element(CONFIG_NODE, &no_debug_ospf6_route_cmd);
1591 }