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 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
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"
39 #include "ospf6_zebra.h"
41 unsigned char conf_debug_ospf6_route
= 0;
43 static char *ospf6_route_table_name(struct ospf6_route_table
*table
)
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");
52 case OSPF6_TABLE_TYPE_BORDER_ROUTERS
:
53 snprintf(name
, sizeof(name
), "global brouter table");
55 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES
:
56 snprintf(name
, sizeof(name
), "global external table");
59 snprintf(name
, sizeof(name
), "global unknown table");
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",
71 case OSPF6_TABLE_TYPE_ROUTES
:
72 snprintf(name
, sizeof(name
), "area %s route table",
75 case OSPF6_TABLE_TYPE_PREFIX_RANGES
:
76 snprintf(name
, sizeof(name
), "area %s range table",
79 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES
:
80 snprintf(name
, sizeof(name
),
81 "area %s summary prefix table", oa
->name
);
83 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS
:
84 snprintf(name
, sizeof(name
),
85 "area %s summary router table", oa
->name
);
88 snprintf(name
, sizeof(name
), "area %s unknown table",
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
);
104 snprintf(name
, sizeof(name
),
105 "interface %s unknown table",
106 oi
->interface
->name
);
112 switch (table
->table_type
) {
113 case OSPF6_TABLE_TYPE_SPF_RESULTS
:
114 snprintf(name
, sizeof(name
), "temporary spf table");
117 snprintf(name
, sizeof(name
), "temporary unknown table");
125 void ospf6_linkstate_prefix(u_int32_t adv_router
, u_int32_t id
,
126 struct prefix
*prefix
)
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);
135 void ospf6_linkstate_prefix2str(struct prefix
*prefix
, char *buf
, int size
)
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
));
144 snprintf(buf
, size
, "%s Net-ID: %s", adv_router_str
, id_str
);
146 snprintf(buf
, size
, "%s", adv_router_str
);
149 /* Global strings for logging */
150 const char *ospf6_dest_type_str
[OSPF6_DEST_TYPE_MAX
] = {
151 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
154 const char *ospf6_dest_type_substr
[OSPF6_DEST_TYPE_MAX
] = {
155 "?", "R", "N", "D", "L", "A",
158 const char *ospf6_path_type_str
[OSPF6_PATH_TYPE_MAX
] = {
159 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
162 const char *ospf6_path_type_substr
[OSPF6_PATH_TYPE_MAX
] = {
163 "??", "IA", "IE", "E1", "E2",
167 struct ospf6_nexthop
*ospf6_nexthop_create(void)
169 struct ospf6_nexthop
*nh
;
171 nh
= XCALLOC(MTYPE_OSPF6_NEXTHOP
, sizeof(struct ospf6_nexthop
));
175 void ospf6_nexthop_delete(struct ospf6_nexthop
*nh
)
178 XFREE(MTYPE_OSPF6_NEXTHOP
, nh
);
181 void ospf6_clear_nexthops(struct list
*nh_list
)
183 struct listnode
*node
;
184 struct ospf6_nexthop
*nh
;
187 for (ALL_LIST_ELEMENTS_RO(nh_list
, node
, nh
))
188 ospf6_nexthop_clear(nh
);
192 static struct ospf6_nexthop
*
193 ospf6_route_find_nexthop(struct list
*nh_list
, struct ospf6_nexthop
*nh_match
)
195 struct listnode
*node
;
196 struct ospf6_nexthop
*nh
;
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
))
208 void ospf6_copy_nexthops(struct list
*dst
, struct list
*src
)
210 struct ospf6_nexthop
*nh_new
, *nh
;
211 struct listnode
*node
;
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(dst
, nh_new
);
224 void ospf6_merge_nexthops(struct list
*dst
, struct list
*src
)
226 struct listnode
*node
;
227 struct ospf6_nexthop
*nh
, *nh_new
;
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(dst
, nh_new
);
240 int ospf6_route_cmp_nexthops(struct ospf6_route
*a
, struct ospf6_route
*b
)
242 struct listnode
*anode
, *bnode
;
243 struct ospf6_nexthop
*anh
, *bnh
;
246 if (listcount(a
->nh_list
) == listcount(b
->nh_list
)) {
247 for (ALL_LIST_ELEMENTS_RO(a
->nh_list
, anode
, anh
)) {
248 for (ALL_LIST_ELEMENTS_RO(b
->nh_list
, bnode
,
250 if (!ospf6_nexthop_is_same(anh
, bnh
))
257 /* One of the routes doesn't exist ? */
261 int ospf6_num_nexthops(struct list
*nh_list
)
263 return (listcount(nh_list
));
266 void ospf6_add_nexthop(struct list
*nh_list
, int ifindex
, struct in6_addr
*addr
)
268 struct ospf6_nexthop
*nh
;
269 struct ospf6_nexthop nh_match
;
272 nh_match
.ifindex
= ifindex
;
274 memcpy(&nh_match
.address
, addr
,
275 sizeof(struct in6_addr
));
277 memset(&nh_match
.address
, 0, sizeof(struct in6_addr
));
279 if (!ospf6_route_find_nexthop(nh_list
, &nh_match
)) {
280 nh
= ospf6_nexthop_create();
281 ospf6_nexthop_copy(nh
, &nh_match
);
282 listnode_add(nh_list
, nh
);
287 void ospf6_route_zebra_copy_nexthops(struct ospf6_route
*route
,
288 struct zapi_nexthop nexthops
[],
291 struct ospf6_nexthop
*nh
;
292 struct listnode
*node
;
298 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
299 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
301 inet_ntop(AF_INET6
, &nh
->address
, buf
,
303 ifname
= ifindex2ifname(nh
->ifindex
,
305 zlog_debug(" nexthop: %s%%%.*s(%d)", buf
,
306 IFNAMSIZ
, ifname
, nh
->ifindex
);
311 nexthops
[i
].ifindex
= nh
->ifindex
;
312 if (!IN6_IS_ADDR_UNSPECIFIED(&nh
->address
)) {
313 nexthops
[i
].gate
.ipv6
= nh
->address
;
314 nexthops
[i
].type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
316 nexthops
[i
].type
= NEXTHOP_TYPE_IFINDEX
;
322 int ospf6_route_get_first_nh_index(struct ospf6_route
*route
)
324 struct ospf6_nexthop
*nh
;
327 if ((nh
= (struct ospf6_nexthop
*)listhead(route
->nh_list
)))
328 return (nh
->ifindex
);
334 static int ospf6_nexthop_cmp(struct ospf6_nexthop
*a
, struct ospf6_nexthop
*b
)
336 if ((a
)->ifindex
== (b
)->ifindex
&&
337 IN6_ARE_ADDR_EQUAL(&(a
)->address
, &(b
)->address
))
342 struct ospf6_route
*ospf6_route_create(void)
344 struct ospf6_route
*route
;
345 route
= XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route
));
346 route
->nh_list
= list_new();
347 route
->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
348 route
->nh_list
->del
= (void (*) (void *))ospf6_nexthop_delete
;
352 void ospf6_route_delete(struct ospf6_route
*route
)
356 list_delete(route
->nh_list
);
357 XFREE(MTYPE_OSPF6_ROUTE
, route
);
361 struct ospf6_route
*ospf6_route_copy(struct ospf6_route
*route
)
363 struct ospf6_route
*new;
365 new = ospf6_route_create();
366 new->type
= route
->type
;
367 memcpy(&new->prefix
, &route
->prefix
, sizeof(struct prefix
));
368 new->installed
= route
->installed
;
369 new->changed
= route
->changed
;
370 new->flag
= route
->flag
;
371 new->route_option
= route
->route_option
;
372 new->linkstate_id
= route
->linkstate_id
;
373 new->path
= route
->path
;
374 ospf6_copy_nexthops(new->nh_list
, route
->nh_list
);
383 void ospf6_route_lock(struct ospf6_route
*route
)
388 void ospf6_route_unlock(struct ospf6_route
*route
)
390 assert(route
->lock
> 0);
392 if (route
->lock
== 0) {
393 /* Can't detach from the table until here
394 because ospf6_route_next () will use
395 the 'route->table' pointer for logging */
397 ospf6_route_delete(route
);
401 /* Route compare function. If ra is more preferred, it returns
402 less than 0. If rb is more preferred returns greater than 0.
403 Otherwise (neither one is preferred), returns 0 */
404 int ospf6_route_cmp(struct ospf6_route
*ra
, struct ospf6_route
*rb
)
406 assert(ospf6_route_is_same(ra
, rb
));
407 assert(OSPF6_PATH_TYPE_NONE
< ra
->path
.type
408 && ra
->path
.type
< OSPF6_PATH_TYPE_MAX
);
409 assert(OSPF6_PATH_TYPE_NONE
< rb
->path
.type
410 && rb
->path
.type
< OSPF6_PATH_TYPE_MAX
);
412 if (ra
->type
!= rb
->type
)
413 return (ra
->type
- rb
->type
);
415 if (ra
->path
.area_id
!= rb
->path
.area_id
)
416 return (ntohl(ra
->path
.area_id
) - ntohl(rb
->path
.area_id
));
418 if (ra
->path
.type
!= rb
->path
.type
)
419 return (ra
->path
.type
- rb
->path
.type
);
421 if (ra
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
) {
422 if (ra
->path
.u
.cost_e2
!= rb
->path
.u
.cost_e2
)
423 return (ra
->path
.u
.cost_e2
- rb
->path
.u
.cost_e2
);
425 if (ra
->path
.cost
!= rb
->path
.cost
)
426 return (ra
->path
.cost
- rb
->path
.cost
);
432 struct ospf6_route
*ospf6_route_lookup(struct prefix
*prefix
,
433 struct ospf6_route_table
*table
)
435 struct route_node
*node
;
436 struct ospf6_route
*route
;
438 node
= route_node_lookup(table
->table
, prefix
);
442 route
= (struct ospf6_route
*)node
->info
;
443 route_unlock_node(node
); /* to free the lookup lock */
448 ospf6_route_lookup_identical(struct ospf6_route
*route
,
449 struct ospf6_route_table
*table
)
451 struct ospf6_route
*target
;
453 for (target
= ospf6_route_lookup(&route
->prefix
, table
); target
;
454 target
= target
->next
) {
455 if (ospf6_route_is_identical(target
, route
))
462 ospf6_route_lookup_bestmatch(struct prefix
*prefix
,
463 struct ospf6_route_table
*table
)
465 struct route_node
*node
;
466 struct ospf6_route
*route
;
468 node
= route_node_match(table
->table
, prefix
);
471 route_unlock_node(node
);
473 route
= (struct ospf6_route
*)node
->info
;
478 static void route_table_assert(struct ospf6_route_table
*table
)
480 struct ospf6_route
*prev
, *r
, *next
;
481 char buf
[PREFIX2STR_BUFFER
];
482 unsigned int link_error
= 0, num
= 0;
484 r
= ospf6_route_head(table
);
490 next
= ospf6_route_next(r
);
499 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
502 if (link_error
== 0 && num
== table
->count
)
505 zlog_err("PANIC !!");
506 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table
);
507 zlog_debug("table count = %d, real number = %d", table
->count
, num
);
508 zlog_debug("DUMP START");
509 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
)) {
510 prefix2str(&r
->prefix
, buf
, sizeof(buf
));
511 zlog_info("%p<-[%p]->%p : %s", r
->prev
, r
, r
->next
, buf
);
513 zlog_debug("DUMP END");
515 assert(link_error
== 0 && num
== table
->count
);
517 #define ospf6_route_table_assert(t) (route_table_assert (t))
519 #define ospf6_route_table_assert(t) ((void) 0)
522 struct ospf6_route
*ospf6_route_add(struct ospf6_route
*route
,
523 struct ospf6_route_table
*table
)
525 struct route_node
*node
, *nextnode
, *prevnode
;
526 struct ospf6_route
*current
= NULL
;
527 struct ospf6_route
*prev
= NULL
, *old
= NULL
, *next
= NULL
;
528 char buf
[PREFIX2STR_BUFFER
];
531 assert(route
->rnode
== NULL
);
532 assert(route
->lock
== 0);
533 assert(route
->next
== NULL
);
534 assert(route
->prev
== NULL
);
536 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
537 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
539 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
541 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
542 zlog_debug("%s %p: route add %p: %s",
543 ospf6_route_table_name(table
), (void *)table
,
545 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
546 zlog_debug("%s: route add: %s", ospf6_route_table_name(table
),
551 node
= route_node_get(table
->table
, &route
->prefix
);
554 /* find place to insert */
555 for (current
= node
->info
; current
; current
= current
->next
) {
556 if (!ospf6_route_is_same(current
, route
))
558 else if (current
->type
!= route
->type
)
560 else if (ospf6_route_is_same_origin(current
, route
))
562 else if (ospf6_route_cmp(current
, route
) > 0)
572 /* if route does not actually change, return unchanged */
573 if (ospf6_route_is_identical(old
, route
)) {
574 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
576 "%s %p: route add %p: needless update of %p",
577 ospf6_route_table_name(table
),
578 (void *)table
, (void *)route
,
580 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
581 zlog_debug("%s: route add: needless update",
582 ospf6_route_table_name(table
));
584 ospf6_route_delete(route
);
585 SET_FLAG(old
->flag
, OSPF6_ROUTE_ADD
);
586 ospf6_route_table_assert(table
);
588 /* to free the lookup lock */
589 route_unlock_node(node
);
593 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
594 zlog_debug("%s %p: route add %p: update of %p",
595 ospf6_route_table_name(table
), (void *)table
,
596 (void *)route
, (void *)old
);
597 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
598 zlog_debug("%s: route add: update",
599 ospf6_route_table_name(table
));
601 /* replace old one if exists */
602 if (node
->info
== old
) {
604 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
608 old
->prev
->next
= route
;
609 route
->prev
= old
->prev
;
611 old
->next
->prev
= route
;
612 route
->next
= old
->next
;
614 route
->installed
= old
->installed
;
615 route
->changed
= now
;
616 assert(route
->table
== NULL
);
617 route
->table
= table
;
619 ospf6_route_unlock(old
); /* will be deleted later */
620 ospf6_route_lock(route
);
622 SET_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
);
623 ospf6_route_table_assert(table
);
626 (*table
->hook_add
)(route
);
631 /* insert if previous or next node found */
633 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
635 "%s %p: route add %p: another path: prev %p, next %p node refcount %u",
636 ospf6_route_table_name(table
), (void *)table
,
637 (void *)route
, (void *)prev
, (void *)next
,
639 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
640 zlog_debug("%s: route add: another path found",
641 ospf6_route_table_name(table
));
655 if (node
->info
== next
) {
656 assert(next
->rnode
== node
);
658 UNSET_FLAG(next
->flag
, OSPF6_ROUTE_BEST
);
659 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
660 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
662 "%s %p: route add %p: replacing previous best: %p",
663 ospf6_route_table_name(table
),
664 (void *)table
, (void *)route
,
668 route
->installed
= now
;
669 route
->changed
= now
;
670 assert(route
->table
== NULL
);
671 route
->table
= table
;
673 ospf6_route_lock(route
);
675 ospf6_route_table_assert(table
);
677 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
679 (*table
->hook_add
)(route
);
684 /* Else, this is the brand new route regarding to the prefix */
685 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
686 zlog_debug("%s %p: route add %p: brand new route",
687 ospf6_route_table_name(table
), (void *)table
,
689 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
690 zlog_debug("%s: route add: brand new route",
691 ospf6_route_table_name(table
));
693 assert(node
->info
== NULL
);
695 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
696 ospf6_route_lock(route
);
697 route
->installed
= now
;
698 route
->changed
= now
;
699 assert(route
->table
== NULL
);
700 route
->table
= table
;
702 /* lookup real existing next route */
704 route_lock_node(nextnode
);
706 nextnode
= route_next(nextnode
);
707 } while (nextnode
&& nextnode
->info
== NULL
);
710 if (nextnode
== NULL
)
713 route_unlock_node(nextnode
);
715 next
= nextnode
->info
;
720 /* lookup real existing prev route */
722 route_lock_node(prevnode
);
724 prevnode
= route_prev(prevnode
);
725 } while (prevnode
&& prevnode
->info
== NULL
);
728 if (prevnode
== NULL
)
731 route_unlock_node(prevnode
);
733 prev
= prevnode
->info
;
734 while (prev
->next
&& ospf6_route_is_same(prev
, prev
->next
))
741 ospf6_route_table_assert(table
);
743 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
745 (*table
->hook_add
)(route
);
750 void ospf6_route_remove(struct ospf6_route
*route
,
751 struct ospf6_route_table
*table
)
753 struct route_node
*node
;
754 struct ospf6_route
*current
;
755 char buf
[PREFIX2STR_BUFFER
];
757 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
758 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
760 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
762 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
763 zlog_debug("%s %p: route remove %p: %s rnode refcount %u",
764 ospf6_route_table_name(table
), (void *)table
,
765 (void *)route
, buf
, route
->rnode
->lock
);
766 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
767 zlog_debug("%s: route remove: %s",
768 ospf6_route_table_name(table
), buf
);
770 node
= route_node_lookup(table
->table
, &route
->prefix
);
773 /* find the route to remove, making sure that the route pointer
774 is from the route table. */
775 current
= node
->info
;
776 while (current
&& current
!= route
)
777 current
= current
->next
;
779 assert(current
== route
);
781 /* adjust doubly linked list */
783 route
->prev
->next
= route
->next
;
785 route
->next
->prev
= route
->prev
;
787 if (node
->info
== route
) {
788 if (route
->next
&& route
->next
->rnode
== node
) {
789 node
->info
= route
->next
;
790 SET_FLAG(route
->next
->flag
, OSPF6_ROUTE_BEST
);
794 route_unlock_node(node
); /* to free the original lock */
798 route_unlock_node(node
); /* to free the lookup lock */
800 ospf6_route_table_assert(table
);
802 SET_FLAG(route
->flag
, OSPF6_ROUTE_WAS_REMOVED
);
804 if (table
->hook_remove
)
805 (*table
->hook_remove
)(route
);
807 ospf6_route_unlock(route
);
810 struct ospf6_route
*ospf6_route_head(struct ospf6_route_table
*table
)
812 struct route_node
*node
;
813 struct ospf6_route
*route
;
815 node
= route_top(table
->table
);
819 /* skip to the real existing entry */
820 while (node
&& node
->info
== NULL
)
821 node
= route_next(node
);
825 route_unlock_node(node
);
828 route
= (struct ospf6_route
*)node
->info
;
829 assert(route
->prev
== NULL
);
830 assert(route
->table
== table
);
831 ospf6_route_lock(route
);
833 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
834 zlog_info("%s %p: route head: %p<-[%p]->%p",
835 ospf6_route_table_name(table
), (void *)table
,
836 (void *)route
->prev
, (void *)route
,
837 (void *)route
->next
);
842 struct ospf6_route
*ospf6_route_next(struct ospf6_route
*route
)
844 struct ospf6_route
*next
= route
->next
;
846 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
847 zlog_info("%s %p: route next: %p<-[%p]->%p",
848 ospf6_route_table_name(route
->table
),
849 (void *)route
->table
, (void *)route
->prev
,
850 (void *)route
, (void *)route
->next
);
852 ospf6_route_unlock(route
);
854 ospf6_route_lock(next
);
859 struct ospf6_route
*ospf6_route_best_next(struct ospf6_route
*route
)
861 struct route_node
*rnode
;
862 struct ospf6_route
*next
;
864 ospf6_route_unlock(route
);
866 rnode
= route
->rnode
;
867 route_lock_node(rnode
);
868 rnode
= route_next(rnode
);
869 while (rnode
&& rnode
->info
== NULL
)
870 rnode
= route_next(rnode
);
873 route_unlock_node(rnode
);
876 next
= (struct ospf6_route
*)rnode
->info
;
877 ospf6_route_lock(next
);
881 struct ospf6_route
*ospf6_route_match_head(struct prefix
*prefix
,
882 struct ospf6_route_table
*table
)
884 struct route_node
*node
;
885 struct ospf6_route
*route
;
887 /* Walk down tree. */
888 node
= table
->table
->top
;
889 while (node
&& node
->p
.prefixlen
< prefix
->prefixlen
890 && prefix_match(&node
->p
, prefix
))
891 node
= node
->link
[prefix_bit(&prefix
->u
.prefix
,
895 route_lock_node(node
);
896 while (node
&& node
->info
== NULL
)
897 node
= route_next(node
);
900 route_unlock_node(node
);
902 if (!prefix_match(prefix
, &node
->p
))
906 ospf6_route_lock(route
);
910 struct ospf6_route
*ospf6_route_match_next(struct prefix
*prefix
,
911 struct ospf6_route
*route
)
913 struct ospf6_route
*next
;
915 next
= ospf6_route_next(route
);
916 if (next
&& !prefix_match(prefix
, &next
->prefix
)) {
917 ospf6_route_unlock(next
);
924 void ospf6_route_remove_all(struct ospf6_route_table
*table
)
926 struct ospf6_route
*route
;
927 for (route
= ospf6_route_head(table
); route
;
928 route
= ospf6_route_next(route
))
929 ospf6_route_remove(route
, table
);
932 struct ospf6_route_table
*ospf6_route_table_create(int s
, int t
)
934 struct ospf6_route_table
*new;
935 new = XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route_table
));
936 new->table
= route_table_init();
942 void ospf6_route_table_delete(struct ospf6_route_table
*table
)
944 ospf6_route_remove_all(table
);
945 bf_free(table
->idspace
);
946 route_table_finish(table
->table
);
947 XFREE(MTYPE_OSPF6_ROUTE
, table
);
952 void ospf6_route_show(struct vty
*vty
, struct ospf6_route
*route
)
955 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
958 struct timeval now
, res
;
959 struct listnode
*node
;
960 struct ospf6_nexthop
*nh
;
963 timersub(&now
, &route
->changed
, &res
);
964 timerstring(&res
, duration
, sizeof(duration
));
967 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
968 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
969 sizeof(destination
));
970 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
971 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
972 destination
, sizeof(destination
));
974 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
977 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
979 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
980 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
983 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
984 (ospf6_route_is_best(route
) ? '*' : ' '),
985 OSPF6_DEST_TYPE_SUBSTR(route
->type
),
986 OSPF6_PATH_TYPE_SUBSTR(route
->path
.type
),
987 destination
, nexthop
, IFNAMSIZ
, ifname
,
991 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
992 "", "", "", nexthop
, IFNAMSIZ
, ifname
, "");
996 void ospf6_route_show_detail(struct vty
*vty
, struct ospf6_route
*route
)
999 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1000 char area_id
[16], id
[16], adv_router
[16], capa
[16], options
[16];
1001 struct timeval now
, res
;
1003 struct listnode
*node
;
1004 struct ospf6_nexthop
*nh
;
1009 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1010 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1011 sizeof(destination
));
1012 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1013 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1014 destination
, sizeof(destination
));
1016 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1017 vty_out(vty
, "Destination: %s\n", destination
);
1019 /* destination type */
1020 vty_out(vty
, "Destination type: %s\n",
1021 OSPF6_DEST_TYPE_NAME(route
->type
));
1024 timersub(&now
, &route
->installed
, &res
);
1025 timerstring(&res
, duration
, sizeof(duration
));
1026 vty_out(vty
, "Installed Time: %s ago\n", duration
);
1028 timersub(&now
, &route
->changed
, &res
);
1029 timerstring(&res
, duration
, sizeof(duration
));
1030 vty_out(vty
, " Changed Time: %s ago\n", duration
);
1032 /* Debugging info */
1033 vty_out(vty
, "Lock: %d Flags: %s%s%s%s\n", route
->lock
,
1034 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1035 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1036 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R" : "-"),
1037 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C" : "-"));
1038 vty_out(vty
, "Memory: prev: %p this: %p next: %p\n",
1039 (void *)route
->prev
, (void *)route
, (void *)route
->next
);
1044 inet_ntop(AF_INET
, &route
->path
.area_id
, area_id
, sizeof(area_id
));
1045 vty_out(vty
, "Associated Area: %s\n", area_id
);
1048 vty_out(vty
, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1051 inet_ntop(AF_INET
, &route
->path
.origin
.id
, id
, sizeof(id
));
1052 inet_ntop(AF_INET
, &route
->path
.origin
.adv_router
, adv_router
,
1053 sizeof(adv_router
));
1054 vty_out(vty
, "LS Origin: %s Id: %s Adv: %s\n",
1055 ospf6_lstype_name(route
->path
.origin
.type
), id
, adv_router
);
1058 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1059 vty_out(vty
, "Options: %s\n", options
);
1062 ospf6_capability_printbuf(route
->path
.router_bits
, capa
, sizeof(capa
));
1063 vty_out(vty
, "Router Bits: %s\n", capa
);
1065 /* Prefix Options */
1066 vty_out(vty
, "Prefix Options: xxx\n");
1069 vty_out(vty
, "Metric Type: %d\n", route
->path
.metric_type
);
1070 vty_out(vty
, "Metric: %d (%d)\n", route
->path
.cost
,
1071 route
->path
.u
.cost_e2
);
1073 vty_out(vty
, "Nexthop count: %u\n", route
->nh_list
->count
);
1075 vty_out(vty
, "Nexthop:\n");
1076 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1078 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
1079 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
1080 vty_out(vty
, " %s %.*s\n", nexthop
, IFNAMSIZ
, ifname
);
1085 static void ospf6_route_show_table_summary(struct vty
*vty
,
1086 struct ospf6_route_table
*table
)
1088 struct ospf6_route
*route
, *prev
= NULL
;
1089 int i
, pathtype
[OSPF6_PATH_TYPE_MAX
];
1090 unsigned int number
= 0;
1091 int nh_count
= 0, nhinval
= 0, ecmp
= 0;
1092 int alternative
= 0, destination
= 0;
1094 for (i
= 0; i
< OSPF6_PATH_TYPE_MAX
; i
++)
1097 for (route
= ospf6_route_head(table
); route
;
1098 route
= ospf6_route_next(route
)) {
1099 if (prev
== NULL
|| !ospf6_route_is_same(prev
, route
))
1103 nh_count
= ospf6_num_nexthops(route
->nh_list
);
1106 else if (nh_count
> 1)
1108 pathtype
[route
->path
.type
]++;
1114 assert(number
== table
->count
);
1116 vty_out(vty
, "Number of OSPFv3 routes: %d\n", number
);
1117 vty_out(vty
, "Number of Destination: %d\n", destination
);
1118 vty_out(vty
, "Number of Alternative routes: %d\n", alternative
);
1119 vty_out(vty
, "Number of Equal Cost Multi Path: %d\n", ecmp
);
1120 for (i
= OSPF6_PATH_TYPE_INTRA
; i
<= OSPF6_PATH_TYPE_EXTERNAL2
; i
++) {
1121 vty_out(vty
, "Number of %s routes: %d\n",
1122 OSPF6_PATH_TYPE_NAME(i
), pathtype
[i
]);
1126 static void ospf6_route_show_table_prefix(struct vty
*vty
,
1127 struct prefix
*prefix
,
1128 struct ospf6_route_table
*table
)
1130 struct ospf6_route
*route
;
1132 route
= ospf6_route_lookup(prefix
, table
);
1136 ospf6_route_lock(route
);
1137 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1138 /* Specifying a prefix will always display details */
1139 ospf6_route_show_detail(vty
, route
);
1140 route
= ospf6_route_next(route
);
1143 ospf6_route_unlock(route
);
1146 static void ospf6_route_show_table_address(struct vty
*vty
,
1147 struct prefix
*prefix
,
1148 struct ospf6_route_table
*table
)
1150 struct ospf6_route
*route
;
1152 route
= ospf6_route_lookup_bestmatch(prefix
, table
);
1156 prefix
= &route
->prefix
;
1157 ospf6_route_lock(route
);
1158 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1159 /* Specifying a prefix will always display details */
1160 ospf6_route_show_detail(vty
, route
);
1161 route
= ospf6_route_next(route
);
1164 ospf6_route_unlock(route
);
1167 static void ospf6_route_show_table_match(struct vty
*vty
, int detail
,
1168 struct prefix
*prefix
,
1169 struct ospf6_route_table
*table
)
1171 struct ospf6_route
*route
;
1172 assert(prefix
->family
);
1174 route
= ospf6_route_match_head(prefix
, table
);
1177 ospf6_route_show_detail(vty
, route
);
1179 ospf6_route_show(vty
, route
);
1180 route
= ospf6_route_match_next(prefix
, route
);
1184 static void ospf6_route_show_table_type(struct vty
*vty
, int detail
,
1186 struct ospf6_route_table
*table
)
1188 struct ospf6_route
*route
;
1190 route
= ospf6_route_head(table
);
1192 if (route
->path
.type
== type
) {
1194 ospf6_route_show_detail(vty
, route
);
1196 ospf6_route_show(vty
, route
);
1198 route
= ospf6_route_next(route
);
1202 static void ospf6_route_show_table(struct vty
*vty
, int detail
,
1203 struct ospf6_route_table
*table
)
1205 struct ospf6_route
*route
;
1207 route
= ospf6_route_head(table
);
1210 ospf6_route_show_detail(vty
, route
);
1212 ospf6_route_show(vty
, route
);
1213 route
= ospf6_route_next(route
);
1217 int ospf6_route_table_show(struct vty
*vty
, int argc_start
, int argc
,
1218 struct cmd_token
**argv
,
1219 struct ospf6_route_table
*table
)
1227 struct prefix prefix
;
1230 memset(&prefix
, 0, sizeof(struct prefix
));
1232 for (i
= argc_start
; i
< argc
; i
++) {
1233 if (strmatch(argv
[i
]->text
, "summary")) {
1238 if (strmatch(argv
[i
]->text
, "intra-area")) {
1239 type
= OSPF6_PATH_TYPE_INTRA
;
1243 if (strmatch(argv
[i
]->text
, "inter-area")) {
1244 type
= OSPF6_PATH_TYPE_INTER
;
1248 if (strmatch(argv
[i
]->text
, "external-1")) {
1249 type
= OSPF6_PATH_TYPE_EXTERNAL1
;
1253 if (strmatch(argv
[i
]->text
, "external-2")) {
1254 type
= OSPF6_PATH_TYPE_EXTERNAL2
;
1258 if (strmatch(argv
[i
]->text
, "detail")) {
1263 if (strmatch(argv
[i
]->text
, "match")) {
1268 ret
= str2prefix(argv
[i
]->arg
, &prefix
);
1269 if (ret
== 1 && prefix
.family
== AF_INET6
) {
1271 if (strchr(argv
[i
]->arg
, '/'))
1276 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1280 /* Give summary of this route table */
1282 ospf6_route_show_table_summary(vty
, table
);
1286 /* Give exact prefix-match route */
1287 if (isprefix
&& !match
) {
1288 /* If exact address, give best matching route */
1290 ospf6_route_show_table_address(vty
, &prefix
, table
);
1292 ospf6_route_show_table_prefix(vty
, &prefix
, table
);
1298 ospf6_route_show_table_match(vty
, detail
, &prefix
, table
);
1300 ospf6_route_show_table_type(vty
, detail
, type
, table
);
1302 ospf6_route_show_table(vty
, detail
, table
);
1307 static void ospf6_linkstate_show_header(struct vty
*vty
)
1309 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1310 "Net-ID", "Rtr-Bits", "Options", "Cost");
1313 static void ospf6_linkstate_show(struct vty
*vty
, struct ospf6_route
*route
)
1315 u_int32_t router
, id
;
1316 char routername
[16], idname
[16], rbits
[16], options
[16];
1318 router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1319 inet_ntop(AF_INET
, &router
, routername
, sizeof(routername
));
1320 id
= ospf6_linkstate_prefix_id(&route
->prefix
);
1321 inet_ntop(AF_INET
, &id
, idname
, sizeof(idname
));
1323 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1325 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1328 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1329 routername
, idname
, rbits
, options
,
1330 (unsigned long)route
->path
.cost
);
1332 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1333 routername
, idname
, rbits
, options
,
1334 (unsigned long)route
->path
.cost
);
1338 static void ospf6_linkstate_show_table_exact(struct vty
*vty
,
1339 struct prefix
*prefix
,
1340 struct ospf6_route_table
*table
)
1342 struct ospf6_route
*route
;
1344 route
= ospf6_route_lookup(prefix
, table
);
1348 ospf6_route_lock(route
);
1349 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1350 /* Specifying a prefix will always display details */
1351 ospf6_route_show_detail(vty
, route
);
1352 route
= ospf6_route_next(route
);
1355 ospf6_route_unlock(route
);
1358 static void ospf6_linkstate_show_table(struct vty
*vty
, int detail
,
1359 struct ospf6_route_table
*table
)
1361 struct ospf6_route
*route
;
1364 ospf6_linkstate_show_header(vty
);
1366 route
= ospf6_route_head(table
);
1369 ospf6_route_show_detail(vty
, route
);
1371 ospf6_linkstate_show(vty
, route
);
1372 route
= ospf6_route_next(route
);
1376 int ospf6_linkstate_table_show(struct vty
*vty
, int idx_ipv4
, int argc
,
1377 struct cmd_token
**argv
,
1378 struct ospf6_route_table
*table
)
1384 struct prefix router
, id
, prefix
;
1386 memset(&router
, 0, sizeof(struct prefix
));
1387 memset(&id
, 0, sizeof(struct prefix
));
1388 memset(&prefix
, 0, sizeof(struct prefix
));
1390 for (i
= idx_ipv4
; i
< argc
; i
++) {
1391 if (strmatch(argv
[i
]->text
, "detail")) {
1397 ret
= str2prefix(argv
[i
]->arg
, &router
);
1398 if (ret
== 1 && router
.family
== AF_INET
) {
1402 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1407 ret
= str2prefix(argv
[i
]->arg
, &id
);
1408 if (ret
== 1 && id
.family
== AF_INET
) {
1412 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1416 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1421 ospf6_linkstate_prefix(router
.u
.prefix4
.s_addr
,
1422 id
.u
.prefix4
.s_addr
, &prefix
);
1425 ospf6_linkstate_show_table_exact(vty
, &prefix
, table
);
1427 ospf6_linkstate_show_table(vty
, detail
, table
);
1433 void ospf6_brouter_show_header(struct vty
*vty
)
1435 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1436 "Options", "Path-Type", "Area");
1439 void ospf6_brouter_show(struct vty
*vty
, struct ospf6_route
*route
)
1441 u_int32_t adv_router
;
1442 char adv
[16], rbits
[16], options
[16], area
[16];
1444 adv_router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1445 inet_ntop(AF_INET
, &adv_router
, adv
, sizeof(adv
));
1446 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1448 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1449 inet_ntop(AF_INET
, &route
->path
.area_id
, area
, sizeof(area
));
1451 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1452 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1453 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", adv
, rbits
, options
,
1454 OSPF6_PATH_TYPE_NAME(route
->path
.type
), area
);
1457 DEFUN (debug_ospf6_route
,
1458 debug_ospf6_route_cmd
,
1459 "debug ospf6 route <table|intra-area|inter-area|memory>",
1463 "Debug route table calculation\n"
1464 "Debug intra-area route calculation\n"
1465 "Debug inter-area route calculation\n"
1466 "Debug route memory use\n"
1470 unsigned char level
= 0;
1472 if (!strncmp(argv
[idx_type
]->arg
, "table", 5))
1473 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1474 else if (!strncmp(argv
[idx_type
]->arg
, "intra", 5))
1475 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1476 else if (!strncmp(argv
[idx_type
]->arg
, "inter", 5))
1477 level
= OSPF6_DEBUG_ROUTE_INTER
;
1478 else if (!strncmp(argv
[idx_type
]->arg
, "memor", 5))
1479 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1480 OSPF6_DEBUG_ROUTE_ON(level
);
1484 DEFUN (no_debug_ospf6_route
,
1485 no_debug_ospf6_route_cmd
,
1486 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1491 "Debug route table calculation\n"
1492 "Debug intra-area route calculation\n"
1493 "Debug inter-area route calculation\n"
1494 "Debug route memory use\n")
1497 unsigned char level
= 0;
1499 if (!strncmp(argv
[idx_type
]->arg
, "table", 5))
1500 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1501 else if (!strncmp(argv
[idx_type
]->arg
, "intra", 5))
1502 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1503 else if (!strncmp(argv
[idx_type
]->arg
, "inter", 5))
1504 level
= OSPF6_DEBUG_ROUTE_INTER
;
1505 else if (!strncmp(argv
[idx_type
]->arg
, "memor", 5))
1506 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1507 OSPF6_DEBUG_ROUTE_OFF(level
);
1511 int config_write_ospf6_debug_route(struct vty
*vty
)
1513 if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
1514 vty_out(vty
, "debug ospf6 route table\n");
1515 if (IS_OSPF6_DEBUG_ROUTE(INTRA
))
1516 vty_out(vty
, "debug ospf6 route intra-area\n");
1517 if (IS_OSPF6_DEBUG_ROUTE(INTER
))
1518 vty_out(vty
, "debug ospf6 route inter-area\n");
1522 void install_element_ospf6_debug_route(void)
1524 install_element(ENABLE_NODE
, &debug_ospf6_route_cmd
);
1525 install_element(ENABLE_NODE
, &no_debug_ospf6_route_cmd
);
1526 install_element(CONFIG_NODE
, &debug_ospf6_route_cmd
);
1527 install_element(CONFIG_NODE
, &no_debug_ospf6_route_cmd
);