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 ifindex_t
*ifindexes
,
289 struct in6_addr
**nexthop_addr
,
292 struct ospf6_nexthop
*nh
;
293 struct listnode
*node
;
299 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
300 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
302 inet_ntop(AF_INET6
, &nh
->address
, buf
,
304 ifname
= ifindex2ifname(nh
->ifindex
,
306 zlog_debug(" nexthop: %s%%%.*s(%d)", buf
,
307 IFNAMSIZ
, ifname
, nh
->ifindex
);
310 nexthop_addr
[i
] = &nh
->address
;
311 ifindexes
[i
] = nh
->ifindex
;
320 int ospf6_route_get_first_nh_index(struct ospf6_route
*route
)
322 struct ospf6_nexthop
*nh
;
325 if ((nh
= (struct ospf6_nexthop
*)listhead(route
->nh_list
)))
326 return (nh
->ifindex
);
332 static int ospf6_nexthop_cmp(struct ospf6_nexthop
*a
, struct ospf6_nexthop
*b
)
334 if ((a
)->ifindex
== (b
)->ifindex
&&
335 IN6_ARE_ADDR_EQUAL(&(a
)->address
, &(b
)->address
))
340 struct ospf6_route
*ospf6_route_create(void)
342 struct ospf6_route
*route
;
343 route
= XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route
));
344 route
->nh_list
= list_new();
345 route
->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
346 route
->nh_list
->del
= (void (*) (void *))ospf6_nexthop_delete
;
350 void ospf6_route_delete(struct ospf6_route
*route
)
354 list_delete(route
->nh_list
);
355 XFREE(MTYPE_OSPF6_ROUTE
, route
);
359 struct ospf6_route
*ospf6_route_copy(struct ospf6_route
*route
)
361 struct ospf6_route
*new;
363 new = ospf6_route_create();
364 new->type
= route
->type
;
365 memcpy(&new->prefix
, &route
->prefix
, sizeof(struct prefix
));
366 new->installed
= route
->installed
;
367 new->changed
= route
->changed
;
368 new->flag
= route
->flag
;
369 new->route_option
= route
->route_option
;
370 new->linkstate_id
= route
->linkstate_id
;
371 new->path
= route
->path
;
372 ospf6_copy_nexthops(new->nh_list
, route
->nh_list
);
381 void ospf6_route_lock(struct ospf6_route
*route
)
386 void ospf6_route_unlock(struct ospf6_route
*route
)
388 assert(route
->lock
> 0);
390 if (route
->lock
== 0) {
391 /* Can't detach from the table until here
392 because ospf6_route_next () will use
393 the 'route->table' pointer for logging */
395 ospf6_route_delete(route
);
399 /* Route compare function. If ra is more preferred, it returns
400 less than 0. If rb is more preferred returns greater than 0.
401 Otherwise (neither one is preferred), returns 0 */
402 int ospf6_route_cmp(struct ospf6_route
*ra
, struct ospf6_route
*rb
)
404 assert(ospf6_route_is_same(ra
, rb
));
405 assert(OSPF6_PATH_TYPE_NONE
< ra
->path
.type
406 && ra
->path
.type
< OSPF6_PATH_TYPE_MAX
);
407 assert(OSPF6_PATH_TYPE_NONE
< rb
->path
.type
408 && rb
->path
.type
< OSPF6_PATH_TYPE_MAX
);
410 if (ra
->type
!= rb
->type
)
411 return (ra
->type
- rb
->type
);
413 if (ra
->path
.area_id
!= rb
->path
.area_id
)
414 return (ntohl(ra
->path
.area_id
) - ntohl(rb
->path
.area_id
));
416 if (ra
->path
.type
!= rb
->path
.type
)
417 return (ra
->path
.type
- rb
->path
.type
);
419 if (ra
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
) {
420 if (ra
->path
.u
.cost_e2
!= rb
->path
.u
.cost_e2
)
421 return (ra
->path
.u
.cost_e2
- rb
->path
.u
.cost_e2
);
423 if (ra
->path
.cost
!= rb
->path
.cost
)
424 return (ra
->path
.cost
- rb
->path
.cost
);
430 struct ospf6_route
*ospf6_route_lookup(struct prefix
*prefix
,
431 struct ospf6_route_table
*table
)
433 struct route_node
*node
;
434 struct ospf6_route
*route
;
436 node
= route_node_lookup(table
->table
, prefix
);
440 route
= (struct ospf6_route
*)node
->info
;
441 route_unlock_node(node
); /* to free the lookup lock */
446 ospf6_route_lookup_identical(struct ospf6_route
*route
,
447 struct ospf6_route_table
*table
)
449 struct ospf6_route
*target
;
451 for (target
= ospf6_route_lookup(&route
->prefix
, table
); target
;
452 target
= target
->next
) {
453 if (ospf6_route_is_identical(target
, route
))
460 ospf6_route_lookup_bestmatch(struct prefix
*prefix
,
461 struct ospf6_route_table
*table
)
463 struct route_node
*node
;
464 struct ospf6_route
*route
;
466 node
= route_node_match(table
->table
, prefix
);
469 route_unlock_node(node
);
471 route
= (struct ospf6_route
*)node
->info
;
476 static void route_table_assert(struct ospf6_route_table
*table
)
478 struct ospf6_route
*prev
, *r
, *next
;
479 char buf
[PREFIX2STR_BUFFER
];
480 unsigned int link_error
= 0, num
= 0;
482 r
= ospf6_route_head(table
);
488 next
= ospf6_route_next(r
);
497 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
500 if (link_error
== 0 && num
== table
->count
)
503 zlog_err("PANIC !!");
504 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table
);
505 zlog_debug("table count = %d, real number = %d", table
->count
, num
);
506 zlog_debug("DUMP START");
507 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
)) {
508 prefix2str(&r
->prefix
, buf
, sizeof(buf
));
509 zlog_info("%p<-[%p]->%p : %s", r
->prev
, r
, r
->next
, buf
);
511 zlog_debug("DUMP END");
513 assert(link_error
== 0 && num
== table
->count
);
515 #define ospf6_route_table_assert(t) (route_table_assert (t))
517 #define ospf6_route_table_assert(t) ((void) 0)
520 struct ospf6_route
*ospf6_route_add(struct ospf6_route
*route
,
521 struct ospf6_route_table
*table
)
523 struct route_node
*node
, *nextnode
, *prevnode
;
524 struct ospf6_route
*current
= NULL
;
525 struct ospf6_route
*prev
= NULL
, *old
= NULL
, *next
= NULL
;
526 char buf
[PREFIX2STR_BUFFER
];
529 assert(route
->rnode
== NULL
);
530 assert(route
->lock
== 0);
531 assert(route
->next
== NULL
);
532 assert(route
->prev
== NULL
);
534 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
535 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
537 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
539 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
540 zlog_debug("%s %p: route add %p: %s",
541 ospf6_route_table_name(table
), (void *)table
,
543 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
544 zlog_debug("%s: route add: %s", ospf6_route_table_name(table
),
549 node
= route_node_get(table
->table
, &route
->prefix
);
552 /* find place to insert */
553 for (current
= node
->info
; current
; current
= current
->next
) {
554 if (!ospf6_route_is_same(current
, route
))
556 else if (current
->type
!= route
->type
)
558 else if (ospf6_route_is_same_origin(current
, route
))
560 else if (ospf6_route_cmp(current
, route
) > 0)
570 /* if route does not actually change, return unchanged */
571 if (ospf6_route_is_identical(old
, route
)) {
572 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
574 "%s %p: route add %p: needless update of %p",
575 ospf6_route_table_name(table
),
576 (void *)table
, (void *)route
,
578 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
579 zlog_debug("%s: route add: needless update",
580 ospf6_route_table_name(table
));
582 ospf6_route_delete(route
);
583 SET_FLAG(old
->flag
, OSPF6_ROUTE_ADD
);
584 ospf6_route_table_assert(table
);
586 /* to free the lookup lock */
587 route_unlock_node(node
);
591 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
592 zlog_debug("%s %p: route add %p: update of %p",
593 ospf6_route_table_name(table
), (void *)table
,
594 (void *)route
, (void *)old
);
595 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
596 zlog_debug("%s: route add: update",
597 ospf6_route_table_name(table
));
599 /* replace old one if exists */
600 if (node
->info
== old
) {
602 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
606 old
->prev
->next
= route
;
607 route
->prev
= old
->prev
;
609 old
->next
->prev
= route
;
610 route
->next
= old
->next
;
612 route
->installed
= old
->installed
;
613 route
->changed
= now
;
614 assert(route
->table
== NULL
);
615 route
->table
= table
;
617 ospf6_route_unlock(old
); /* will be deleted later */
618 ospf6_route_lock(route
);
620 SET_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
);
621 ospf6_route_table_assert(table
);
624 (*table
->hook_add
)(route
);
629 /* insert if previous or next node found */
631 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
633 "%s %p: route add %p: another path: prev %p, next %p node refcount %u",
634 ospf6_route_table_name(table
), (void *)table
,
635 (void *)route
, (void *)prev
, (void *)next
,
637 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
638 zlog_debug("%s: route add: another path found",
639 ospf6_route_table_name(table
));
653 if (node
->info
== next
) {
654 assert(next
->rnode
== node
);
656 UNSET_FLAG(next
->flag
, OSPF6_ROUTE_BEST
);
657 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
658 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
660 "%s %p: route add %p: replacing previous best: %p",
661 ospf6_route_table_name(table
),
662 (void *)table
, (void *)route
,
666 route
->installed
= now
;
667 route
->changed
= now
;
668 assert(route
->table
== NULL
);
669 route
->table
= table
;
671 ospf6_route_lock(route
);
673 ospf6_route_table_assert(table
);
675 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
677 (*table
->hook_add
)(route
);
682 /* Else, this is the brand new route regarding to the prefix */
683 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
684 zlog_debug("%s %p: route add %p: brand new route",
685 ospf6_route_table_name(table
), (void *)table
,
687 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
688 zlog_debug("%s: route add: brand new route",
689 ospf6_route_table_name(table
));
691 assert(node
->info
== NULL
);
693 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
694 ospf6_route_lock(route
);
695 route
->installed
= now
;
696 route
->changed
= now
;
697 assert(route
->table
== NULL
);
698 route
->table
= table
;
700 /* lookup real existing next route */
702 route_lock_node(nextnode
);
704 nextnode
= route_next(nextnode
);
705 } while (nextnode
&& nextnode
->info
== NULL
);
708 if (nextnode
== NULL
)
711 route_unlock_node(nextnode
);
713 next
= nextnode
->info
;
718 /* lookup real existing prev route */
720 route_lock_node(prevnode
);
722 prevnode
= route_prev(prevnode
);
723 } while (prevnode
&& prevnode
->info
== NULL
);
726 if (prevnode
== NULL
)
729 route_unlock_node(prevnode
);
731 prev
= prevnode
->info
;
732 while (prev
->next
&& ospf6_route_is_same(prev
, prev
->next
))
739 ospf6_route_table_assert(table
);
741 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
743 (*table
->hook_add
)(route
);
748 void ospf6_route_remove(struct ospf6_route
*route
,
749 struct ospf6_route_table
*table
)
751 struct route_node
*node
;
752 struct ospf6_route
*current
;
753 char buf
[PREFIX2STR_BUFFER
];
755 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
756 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
758 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
760 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
761 zlog_debug("%s %p: route remove %p: %s rnode refcount %u",
762 ospf6_route_table_name(table
), (void *)table
,
763 (void *)route
, buf
, route
->rnode
->lock
);
764 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
765 zlog_debug("%s: route remove: %s",
766 ospf6_route_table_name(table
), buf
);
768 node
= route_node_lookup(table
->table
, &route
->prefix
);
771 /* find the route to remove, making sure that the route pointer
772 is from the route table. */
773 current
= node
->info
;
774 while (current
&& current
!= route
)
775 current
= current
->next
;
777 assert(current
== route
);
779 /* adjust doubly linked list */
781 route
->prev
->next
= route
->next
;
783 route
->next
->prev
= route
->prev
;
785 if (node
->info
== route
) {
786 if (route
->next
&& route
->next
->rnode
== node
) {
787 node
->info
= route
->next
;
788 SET_FLAG(route
->next
->flag
, OSPF6_ROUTE_BEST
);
792 route_unlock_node(node
); /* to free the original lock */
796 route_unlock_node(node
); /* to free the lookup lock */
798 ospf6_route_table_assert(table
);
800 SET_FLAG(route
->flag
, OSPF6_ROUTE_WAS_REMOVED
);
802 if (table
->hook_remove
)
803 (*table
->hook_remove
)(route
);
805 ospf6_route_unlock(route
);
808 struct ospf6_route
*ospf6_route_head(struct ospf6_route_table
*table
)
810 struct route_node
*node
;
811 struct ospf6_route
*route
;
813 node
= route_top(table
->table
);
817 /* skip to the real existing entry */
818 while (node
&& node
->info
== NULL
)
819 node
= route_next(node
);
823 route_unlock_node(node
);
826 route
= (struct ospf6_route
*)node
->info
;
827 assert(route
->prev
== NULL
);
828 assert(route
->table
== table
);
829 ospf6_route_lock(route
);
831 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
832 zlog_info("%s %p: route head: %p<-[%p]->%p",
833 ospf6_route_table_name(table
), (void *)table
,
834 (void *)route
->prev
, (void *)route
,
835 (void *)route
->next
);
840 struct ospf6_route
*ospf6_route_next(struct ospf6_route
*route
)
842 struct ospf6_route
*next
= route
->next
;
844 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
845 zlog_info("%s %p: route next: %p<-[%p]->%p",
846 ospf6_route_table_name(route
->table
),
847 (void *)route
->table
, (void *)route
->prev
,
848 (void *)route
, (void *)route
->next
);
850 ospf6_route_unlock(route
);
852 ospf6_route_lock(next
);
857 struct ospf6_route
*ospf6_route_best_next(struct ospf6_route
*route
)
859 struct route_node
*rnode
;
860 struct ospf6_route
*next
;
862 ospf6_route_unlock(route
);
864 rnode
= route
->rnode
;
865 route_lock_node(rnode
);
866 rnode
= route_next(rnode
);
867 while (rnode
&& rnode
->info
== NULL
)
868 rnode
= route_next(rnode
);
871 route_unlock_node(rnode
);
874 next
= (struct ospf6_route
*)rnode
->info
;
875 ospf6_route_lock(next
);
879 struct ospf6_route
*ospf6_route_match_head(struct prefix
*prefix
,
880 struct ospf6_route_table
*table
)
882 struct route_node
*node
;
883 struct ospf6_route
*route
;
885 /* Walk down tree. */
886 node
= table
->table
->top
;
887 while (node
&& node
->p
.prefixlen
< prefix
->prefixlen
888 && prefix_match(&node
->p
, prefix
))
889 node
= node
->link
[prefix_bit(&prefix
->u
.prefix
,
893 route_lock_node(node
);
894 while (node
&& node
->info
== NULL
)
895 node
= route_next(node
);
898 route_unlock_node(node
);
900 if (!prefix_match(prefix
, &node
->p
))
904 ospf6_route_lock(route
);
908 struct ospf6_route
*ospf6_route_match_next(struct prefix
*prefix
,
909 struct ospf6_route
*route
)
911 struct ospf6_route
*next
;
913 next
= ospf6_route_next(route
);
914 if (next
&& !prefix_match(prefix
, &next
->prefix
)) {
915 ospf6_route_unlock(next
);
922 void ospf6_route_remove_all(struct ospf6_route_table
*table
)
924 struct ospf6_route
*route
;
925 for (route
= ospf6_route_head(table
); route
;
926 route
= ospf6_route_next(route
))
927 ospf6_route_remove(route
, table
);
930 struct ospf6_route_table
*ospf6_route_table_create(int s
, int t
)
932 struct ospf6_route_table
*new;
933 new = XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route_table
));
934 new->table
= route_table_init();
940 void ospf6_route_table_delete(struct ospf6_route_table
*table
)
942 ospf6_route_remove_all(table
);
943 bf_free(table
->idspace
);
944 route_table_finish(table
->table
);
945 XFREE(MTYPE_OSPF6_ROUTE
, table
);
950 void ospf6_route_show(struct vty
*vty
, struct ospf6_route
*route
)
953 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
956 struct timeval now
, res
;
957 struct listnode
*node
;
958 struct ospf6_nexthop
*nh
;
961 timersub(&now
, &route
->changed
, &res
);
962 timerstring(&res
, duration
, sizeof(duration
));
965 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
966 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
967 sizeof(destination
));
968 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
969 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
970 destination
, sizeof(destination
));
972 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
975 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
977 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
978 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
981 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
982 (ospf6_route_is_best(route
) ? '*' : ' '),
983 OSPF6_DEST_TYPE_SUBSTR(route
->type
),
984 OSPF6_PATH_TYPE_SUBSTR(route
->path
.type
),
985 destination
, nexthop
, IFNAMSIZ
, ifname
,
989 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
990 "", "", "", nexthop
, IFNAMSIZ
, ifname
, "");
994 void ospf6_route_show_detail(struct vty
*vty
, struct ospf6_route
*route
)
997 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
998 char area_id
[16], id
[16], adv_router
[16], capa
[16], options
[16];
999 struct timeval now
, res
;
1001 struct listnode
*node
;
1002 struct ospf6_nexthop
*nh
;
1007 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1008 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1009 sizeof(destination
));
1010 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1011 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1012 destination
, sizeof(destination
));
1014 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1015 vty_out(vty
, "Destination: %s\n", destination
);
1017 /* destination type */
1018 vty_out(vty
, "Destination type: %s\n",
1019 OSPF6_DEST_TYPE_NAME(route
->type
));
1022 timersub(&now
, &route
->installed
, &res
);
1023 timerstring(&res
, duration
, sizeof(duration
));
1024 vty_out(vty
, "Installed Time: %s ago\n", duration
);
1026 timersub(&now
, &route
->changed
, &res
);
1027 timerstring(&res
, duration
, sizeof(duration
));
1028 vty_out(vty
, " Changed Time: %s ago\n", duration
);
1030 /* Debugging info */
1031 vty_out(vty
, "Lock: %d Flags: %s%s%s%s\n", route
->lock
,
1032 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1033 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1034 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R" : "-"),
1035 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C" : "-"));
1036 vty_out(vty
, "Memory: prev: %p this: %p next: %p\n",
1037 (void *)route
->prev
, (void *)route
, (void *)route
->next
);
1042 inet_ntop(AF_INET
, &route
->path
.area_id
, area_id
, sizeof(area_id
));
1043 vty_out(vty
, "Associated Area: %s\n", area_id
);
1046 vty_out(vty
, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1049 inet_ntop(AF_INET
, &route
->path
.origin
.id
, id
, sizeof(id
));
1050 inet_ntop(AF_INET
, &route
->path
.origin
.adv_router
, adv_router
,
1051 sizeof(adv_router
));
1052 vty_out(vty
, "LS Origin: %s Id: %s Adv: %s\n",
1053 ospf6_lstype_name(route
->path
.origin
.type
), id
, adv_router
);
1056 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1057 vty_out(vty
, "Options: %s\n", options
);
1060 ospf6_capability_printbuf(route
->path
.router_bits
, capa
, sizeof(capa
));
1061 vty_out(vty
, "Router Bits: %s\n", capa
);
1063 /* Prefix Options */
1064 vty_out(vty
, "Prefix Options: xxx\n");
1067 vty_out(vty
, "Metric Type: %d\n", route
->path
.metric_type
);
1068 vty_out(vty
, "Metric: %d (%d)\n", route
->path
.cost
,
1069 route
->path
.u
.cost_e2
);
1071 vty_out(vty
, "Nexthop count: %u\n", route
->nh_list
->count
);
1073 vty_out(vty
, "Nexthop:\n");
1074 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1076 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
1077 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
1078 vty_out(vty
, " %s %.*s\n", nexthop
, IFNAMSIZ
, ifname
);
1083 static void ospf6_route_show_table_summary(struct vty
*vty
,
1084 struct ospf6_route_table
*table
)
1086 struct ospf6_route
*route
, *prev
= NULL
;
1087 int i
, pathtype
[OSPF6_PATH_TYPE_MAX
];
1088 unsigned int number
= 0;
1089 int nh_count
= 0, nhinval
= 0, ecmp
= 0;
1090 int alternative
= 0, destination
= 0;
1092 for (i
= 0; i
< OSPF6_PATH_TYPE_MAX
; i
++)
1095 for (route
= ospf6_route_head(table
); route
;
1096 route
= ospf6_route_next(route
)) {
1097 if (prev
== NULL
|| !ospf6_route_is_same(prev
, route
))
1101 nh_count
= ospf6_num_nexthops(route
->nh_list
);
1104 else if (nh_count
> 1)
1106 pathtype
[route
->path
.type
]++;
1112 assert(number
== table
->count
);
1114 vty_out(vty
, "Number of OSPFv3 routes: %d\n", number
);
1115 vty_out(vty
, "Number of Destination: %d\n", destination
);
1116 vty_out(vty
, "Number of Alternative routes: %d\n", alternative
);
1117 vty_out(vty
, "Number of Equal Cost Multi Path: %d\n", ecmp
);
1118 for (i
= OSPF6_PATH_TYPE_INTRA
; i
<= OSPF6_PATH_TYPE_EXTERNAL2
; i
++) {
1119 vty_out(vty
, "Number of %s routes: %d\n",
1120 OSPF6_PATH_TYPE_NAME(i
), pathtype
[i
]);
1124 static void ospf6_route_show_table_prefix(struct vty
*vty
,
1125 struct prefix
*prefix
,
1126 struct ospf6_route_table
*table
)
1128 struct ospf6_route
*route
;
1130 route
= ospf6_route_lookup(prefix
, table
);
1134 ospf6_route_lock(route
);
1135 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1136 /* Specifying a prefix will always display details */
1137 ospf6_route_show_detail(vty
, route
);
1138 route
= ospf6_route_next(route
);
1141 ospf6_route_unlock(route
);
1144 static void ospf6_route_show_table_address(struct vty
*vty
,
1145 struct prefix
*prefix
,
1146 struct ospf6_route_table
*table
)
1148 struct ospf6_route
*route
;
1150 route
= ospf6_route_lookup_bestmatch(prefix
, table
);
1154 prefix
= &route
->prefix
;
1155 ospf6_route_lock(route
);
1156 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1157 /* Specifying a prefix will always display details */
1158 ospf6_route_show_detail(vty
, route
);
1159 route
= ospf6_route_next(route
);
1162 ospf6_route_unlock(route
);
1165 static void ospf6_route_show_table_match(struct vty
*vty
, int detail
,
1166 struct prefix
*prefix
,
1167 struct ospf6_route_table
*table
)
1169 struct ospf6_route
*route
;
1170 assert(prefix
->family
);
1172 route
= ospf6_route_match_head(prefix
, table
);
1175 ospf6_route_show_detail(vty
, route
);
1177 ospf6_route_show(vty
, route
);
1178 route
= ospf6_route_match_next(prefix
, route
);
1182 static void ospf6_route_show_table_type(struct vty
*vty
, int detail
,
1184 struct ospf6_route_table
*table
)
1186 struct ospf6_route
*route
;
1188 route
= ospf6_route_head(table
);
1190 if (route
->path
.type
== type
) {
1192 ospf6_route_show_detail(vty
, route
);
1194 ospf6_route_show(vty
, route
);
1196 route
= ospf6_route_next(route
);
1200 static void ospf6_route_show_table(struct vty
*vty
, int detail
,
1201 struct ospf6_route_table
*table
)
1203 struct ospf6_route
*route
;
1205 route
= ospf6_route_head(table
);
1208 ospf6_route_show_detail(vty
, route
);
1210 ospf6_route_show(vty
, route
);
1211 route
= ospf6_route_next(route
);
1215 int ospf6_route_table_show(struct vty
*vty
, int argc_start
, int argc
,
1216 struct cmd_token
**argv
,
1217 struct ospf6_route_table
*table
)
1225 struct prefix prefix
;
1228 memset(&prefix
, 0, sizeof(struct prefix
));
1230 for (i
= argc_start
; i
< argc
; i
++) {
1231 if (strmatch(argv
[i
]->text
, "summary")) {
1236 if (strmatch(argv
[i
]->text
, "intra-area")) {
1237 type
= OSPF6_PATH_TYPE_INTRA
;
1241 if (strmatch(argv
[i
]->text
, "inter-area")) {
1242 type
= OSPF6_PATH_TYPE_INTER
;
1246 if (strmatch(argv
[i
]->text
, "external-1")) {
1247 type
= OSPF6_PATH_TYPE_EXTERNAL1
;
1251 if (strmatch(argv
[i
]->text
, "external-2")) {
1252 type
= OSPF6_PATH_TYPE_EXTERNAL2
;
1256 if (strmatch(argv
[i
]->text
, "detail")) {
1261 if (strmatch(argv
[i
]->text
, "match")) {
1266 ret
= str2prefix(argv
[i
]->arg
, &prefix
);
1267 if (ret
== 1 && prefix
.family
== AF_INET6
) {
1269 if (strchr(argv
[i
]->arg
, '/'))
1274 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1278 /* Give summary of this route table */
1280 ospf6_route_show_table_summary(vty
, table
);
1284 /* Give exact prefix-match route */
1285 if (isprefix
&& !match
) {
1286 /* If exact address, give best matching route */
1288 ospf6_route_show_table_address(vty
, &prefix
, table
);
1290 ospf6_route_show_table_prefix(vty
, &prefix
, table
);
1296 ospf6_route_show_table_match(vty
, detail
, &prefix
, table
);
1298 ospf6_route_show_table_type(vty
, detail
, type
, table
);
1300 ospf6_route_show_table(vty
, detail
, table
);
1305 static void ospf6_linkstate_show_header(struct vty
*vty
)
1307 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1308 "Net-ID", "Rtr-Bits", "Options", "Cost");
1311 static void ospf6_linkstate_show(struct vty
*vty
, struct ospf6_route
*route
)
1313 u_int32_t router
, id
;
1314 char routername
[16], idname
[16], rbits
[16], options
[16];
1316 router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1317 inet_ntop(AF_INET
, &router
, routername
, sizeof(routername
));
1318 id
= ospf6_linkstate_prefix_id(&route
->prefix
);
1319 inet_ntop(AF_INET
, &id
, idname
, sizeof(idname
));
1321 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1323 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1326 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1327 routername
, idname
, rbits
, options
,
1328 (unsigned long)route
->path
.cost
);
1330 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1331 routername
, idname
, rbits
, options
,
1332 (unsigned long)route
->path
.cost
);
1336 static void ospf6_linkstate_show_table_exact(struct vty
*vty
,
1337 struct prefix
*prefix
,
1338 struct ospf6_route_table
*table
)
1340 struct ospf6_route
*route
;
1342 route
= ospf6_route_lookup(prefix
, table
);
1346 ospf6_route_lock(route
);
1347 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1348 /* Specifying a prefix will always display details */
1349 ospf6_route_show_detail(vty
, route
);
1350 route
= ospf6_route_next(route
);
1353 ospf6_route_unlock(route
);
1356 static void ospf6_linkstate_show_table(struct vty
*vty
, int detail
,
1357 struct ospf6_route_table
*table
)
1359 struct ospf6_route
*route
;
1362 ospf6_linkstate_show_header(vty
);
1364 route
= ospf6_route_head(table
);
1367 ospf6_route_show_detail(vty
, route
);
1369 ospf6_linkstate_show(vty
, route
);
1370 route
= ospf6_route_next(route
);
1374 int ospf6_linkstate_table_show(struct vty
*vty
, int idx_ipv4
, int argc
,
1375 struct cmd_token
**argv
,
1376 struct ospf6_route_table
*table
)
1382 struct prefix router
, id
, prefix
;
1384 memset(&router
, 0, sizeof(struct prefix
));
1385 memset(&id
, 0, sizeof(struct prefix
));
1386 memset(&prefix
, 0, sizeof(struct prefix
));
1388 for (i
= idx_ipv4
; i
< argc
; i
++) {
1389 if (strmatch(argv
[i
]->text
, "detail")) {
1395 ret
= str2prefix(argv
[i
]->arg
, &router
);
1396 if (ret
== 1 && router
.family
== AF_INET
) {
1400 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1405 ret
= str2prefix(argv
[i
]->arg
, &id
);
1406 if (ret
== 1 && id
.family
== AF_INET
) {
1410 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1414 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1419 ospf6_linkstate_prefix(router
.u
.prefix4
.s_addr
,
1420 id
.u
.prefix4
.s_addr
, &prefix
);
1423 ospf6_linkstate_show_table_exact(vty
, &prefix
, table
);
1425 ospf6_linkstate_show_table(vty
, detail
, table
);
1431 void ospf6_brouter_show_header(struct vty
*vty
)
1433 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1434 "Options", "Path-Type", "Area");
1437 void ospf6_brouter_show(struct vty
*vty
, struct ospf6_route
*route
)
1439 u_int32_t adv_router
;
1440 char adv
[16], rbits
[16], options
[16], area
[16];
1442 adv_router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1443 inet_ntop(AF_INET
, &adv_router
, adv
, sizeof(adv
));
1444 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1446 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1447 inet_ntop(AF_INET
, &route
->path
.area_id
, area
, sizeof(area
));
1449 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1450 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1451 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", adv
, rbits
, options
,
1452 OSPF6_PATH_TYPE_NAME(route
->path
.type
), area
);
1455 DEFUN (debug_ospf6_route
,
1456 debug_ospf6_route_cmd
,
1457 "debug ospf6 route <table|intra-area|inter-area|memory>",
1461 "Debug route table calculation\n"
1462 "Debug intra-area route calculation\n"
1463 "Debug inter-area route calculation\n"
1464 "Debug route memory use\n"
1468 unsigned char level
= 0;
1470 if (!strncmp(argv
[idx_type
]->arg
, "table", 5))
1471 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1472 else if (!strncmp(argv
[idx_type
]->arg
, "intra", 5))
1473 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1474 else if (!strncmp(argv
[idx_type
]->arg
, "inter", 5))
1475 level
= OSPF6_DEBUG_ROUTE_INTER
;
1476 else if (!strncmp(argv
[idx_type
]->arg
, "memor", 5))
1477 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1478 OSPF6_DEBUG_ROUTE_ON(level
);
1482 DEFUN (no_debug_ospf6_route
,
1483 no_debug_ospf6_route_cmd
,
1484 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1489 "Debug route table calculation\n"
1490 "Debug intra-area route calculation\n"
1491 "Debug inter-area route calculation\n"
1492 "Debug route memory use\n")
1495 unsigned char level
= 0;
1497 if (!strncmp(argv
[idx_type
]->arg
, "table", 5))
1498 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1499 else if (!strncmp(argv
[idx_type
]->arg
, "intra", 5))
1500 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1501 else if (!strncmp(argv
[idx_type
]->arg
, "inter", 5))
1502 level
= OSPF6_DEBUG_ROUTE_INTER
;
1503 else if (!strncmp(argv
[idx_type
]->arg
, "memor", 5))
1504 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1505 OSPF6_DEBUG_ROUTE_OFF(level
);
1509 int config_write_ospf6_debug_route(struct vty
*vty
)
1511 if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
1512 vty_out(vty
, "debug ospf6 route table\n");
1513 if (IS_OSPF6_DEBUG_ROUTE(INTRA
))
1514 vty_out(vty
, "debug ospf6 route intra-area\n");
1515 if (IS_OSPF6_DEBUG_ROUTE(INTER
))
1516 vty_out(vty
, "debug ospf6 route inter-area\n");
1520 void install_element_ospf6_debug_route(void)
1522 install_element(ENABLE_NODE
, &debug_ospf6_route_cmd
);
1523 install_element(ENABLE_NODE
, &no_debug_ospf6_route_cmd
);
1524 install_element(CONFIG_NODE
, &debug_ospf6_route_cmd
);
1525 install_element(CONFIG_NODE
, &no_debug_ospf6_route_cmd
);