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
;
244 bool identical
= false;
247 if (listcount(a
->nh_list
) == listcount(b
->nh_list
)) {
248 for (ALL_LIST_ELEMENTS_RO(a
->nh_list
, anode
, anh
)) {
250 for (ALL_LIST_ELEMENTS_RO(b
->nh_list
, bnode
,
252 if (ospf6_nexthop_is_same(anh
, bnh
))
255 /* Currnet List A element not found List B
256 * Non-Identical lists return */
257 if (identical
== false)
264 /* One of the routes doesn't exist ? */
268 int ospf6_num_nexthops(struct list
*nh_list
)
270 return (listcount(nh_list
));
273 void ospf6_add_nexthop(struct list
*nh_list
, int ifindex
, struct in6_addr
*addr
)
275 struct ospf6_nexthop
*nh
;
276 struct ospf6_nexthop nh_match
;
279 nh_match
.ifindex
= ifindex
;
281 memcpy(&nh_match
.address
, addr
,
282 sizeof(struct in6_addr
));
284 memset(&nh_match
.address
, 0, sizeof(struct in6_addr
));
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
);
294 void ospf6_route_zebra_copy_nexthops(struct ospf6_route
*route
,
295 struct zapi_nexthop nexthops
[],
298 struct ospf6_nexthop
*nh
;
299 struct listnode
*node
;
305 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
306 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
308 inet_ntop(AF_INET6
, &nh
->address
, buf
,
310 ifname
= ifindex2ifname(nh
->ifindex
,
312 zlog_debug(" nexthop: %s%%%.*s(%d)", buf
,
313 IFNAMSIZ
, ifname
, nh
->ifindex
);
318 nexthops
[i
].ifindex
= nh
->ifindex
;
319 if (!IN6_IS_ADDR_UNSPECIFIED(&nh
->address
)) {
320 nexthops
[i
].gate
.ipv6
= nh
->address
;
321 nexthops
[i
].type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
323 nexthops
[i
].type
= NEXTHOP_TYPE_IFINDEX
;
329 int ospf6_route_get_first_nh_index(struct ospf6_route
*route
)
331 struct ospf6_nexthop
*nh
;
334 if ((nh
= (struct ospf6_nexthop
*)listhead(route
->nh_list
)))
335 return (nh
->ifindex
);
341 static int ospf6_nexthop_cmp(struct ospf6_nexthop
*a
, struct ospf6_nexthop
*b
)
343 if (a
->ifindex
< b
->ifindex
)
345 else if (a
->ifindex
> b
->ifindex
)
348 return memcmp(&a
->address
, &b
->address
,
349 sizeof(struct in6_addr
));
354 struct ospf6_route
*ospf6_route_create(void)
356 struct ospf6_route
*route
;
357 route
= XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route
));
358 route
->nh_list
= list_new();
359 route
->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
360 route
->nh_list
->del
= (void (*) (void *))ospf6_nexthop_delete
;
364 void ospf6_route_delete(struct ospf6_route
*route
)
368 list_delete_and_null(&route
->nh_list
);
369 XFREE(MTYPE_OSPF6_ROUTE
, route
);
373 struct ospf6_route
*ospf6_route_copy(struct ospf6_route
*route
)
375 struct ospf6_route
*new;
377 new = ospf6_route_create();
378 new->type
= route
->type
;
379 memcpy(&new->prefix
, &route
->prefix
, sizeof(struct prefix
));
380 new->installed
= route
->installed
;
381 new->changed
= route
->changed
;
382 new->flag
= route
->flag
;
383 new->route_option
= route
->route_option
;
384 new->linkstate_id
= route
->linkstate_id
;
385 new->path
= route
->path
;
386 ospf6_copy_nexthops(new->nh_list
, route
->nh_list
);
395 void ospf6_route_lock(struct ospf6_route
*route
)
400 void ospf6_route_unlock(struct ospf6_route
*route
)
402 assert(route
->lock
> 0);
404 if (route
->lock
== 0) {
405 /* Can't detach from the table until here
406 because ospf6_route_next () will use
407 the 'route->table' pointer for logging */
409 ospf6_route_delete(route
);
413 /* Route compare function. If ra is more preferred, it returns
414 less than 0. If rb is more preferred returns greater than 0.
415 Otherwise (neither one is preferred), returns 0 */
416 int ospf6_route_cmp(struct ospf6_route
*ra
, struct ospf6_route
*rb
)
418 assert(ospf6_route_is_same(ra
, rb
));
419 assert(OSPF6_PATH_TYPE_NONE
< ra
->path
.type
420 && ra
->path
.type
< OSPF6_PATH_TYPE_MAX
);
421 assert(OSPF6_PATH_TYPE_NONE
< rb
->path
.type
422 && rb
->path
.type
< OSPF6_PATH_TYPE_MAX
);
424 if (ra
->type
!= rb
->type
)
425 return (ra
->type
- rb
->type
);
427 if (ra
->path
.area_id
!= rb
->path
.area_id
)
428 return (ntohl(ra
->path
.area_id
) - ntohl(rb
->path
.area_id
));
430 if (ra
->path
.type
!= rb
->path
.type
)
431 return (ra
->path
.type
- rb
->path
.type
);
433 if (ra
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
) {
434 if (ra
->path
.u
.cost_e2
!= rb
->path
.u
.cost_e2
)
435 return (ra
->path
.u
.cost_e2
- rb
->path
.u
.cost_e2
);
437 if (ra
->path
.cost
!= rb
->path
.cost
)
438 return (ra
->path
.cost
- rb
->path
.cost
);
444 struct ospf6_route
*ospf6_route_lookup(struct prefix
*prefix
,
445 struct ospf6_route_table
*table
)
447 struct route_node
*node
;
448 struct ospf6_route
*route
;
450 node
= route_node_lookup(table
->table
, prefix
);
454 route
= (struct ospf6_route
*)node
->info
;
455 route_unlock_node(node
); /* to free the lookup lock */
460 ospf6_route_lookup_identical(struct ospf6_route
*route
,
461 struct ospf6_route_table
*table
)
463 struct ospf6_route
*target
;
465 for (target
= ospf6_route_lookup(&route
->prefix
, table
); target
;
466 target
= target
->next
) {
467 if (ospf6_route_is_identical(target
, route
))
474 ospf6_route_lookup_bestmatch(struct prefix
*prefix
,
475 struct ospf6_route_table
*table
)
477 struct route_node
*node
;
478 struct ospf6_route
*route
;
480 node
= route_node_match(table
->table
, prefix
);
483 route_unlock_node(node
);
485 route
= (struct ospf6_route
*)node
->info
;
490 static void route_table_assert(struct ospf6_route_table
*table
)
492 struct ospf6_route
*prev
, *r
, *next
;
493 char buf
[PREFIX2STR_BUFFER
];
494 unsigned int link_error
= 0, num
= 0;
496 r
= ospf6_route_head(table
);
502 next
= ospf6_route_next(r
);
511 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
514 if (link_error
== 0 && num
== table
->count
)
517 zlog_err("PANIC !!");
518 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table
);
519 zlog_debug("table count = %d, real number = %d", table
->count
, num
);
520 zlog_debug("DUMP START");
521 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
)) {
522 prefix2str(&r
->prefix
, buf
, sizeof(buf
));
523 zlog_info("%p<-[%p]->%p : %s", r
->prev
, r
, r
->next
, buf
);
525 zlog_debug("DUMP END");
527 assert(link_error
== 0 && num
== table
->count
);
529 #define ospf6_route_table_assert(t) (route_table_assert (t))
531 #define ospf6_route_table_assert(t) ((void) 0)
534 struct ospf6_route
*ospf6_route_add(struct ospf6_route
*route
,
535 struct ospf6_route_table
*table
)
537 struct route_node
*node
, *nextnode
, *prevnode
;
538 struct ospf6_route
*current
= NULL
;
539 struct ospf6_route
*prev
= NULL
, *old
= NULL
, *next
= NULL
;
540 char buf
[PREFIX2STR_BUFFER
];
543 assert(route
->rnode
== NULL
);
544 assert(route
->lock
== 0);
545 assert(route
->next
== NULL
);
546 assert(route
->prev
== NULL
);
548 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
549 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
551 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
553 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
554 zlog_debug("%s %p: route add %p: %s",
555 ospf6_route_table_name(table
), (void *)table
,
557 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
558 zlog_debug("%s: route add: %s", ospf6_route_table_name(table
),
563 node
= route_node_get(table
->table
, &route
->prefix
);
566 /* find place to insert */
567 for (current
= node
->info
; current
; current
= current
->next
) {
568 if (!ospf6_route_is_same(current
, route
))
570 else if (current
->type
!= route
->type
)
572 else if (ospf6_route_is_same_origin(current
, route
))
574 else if (ospf6_route_cmp(current
, route
) > 0)
584 /* if route does not actually change, return unchanged */
585 if (ospf6_route_is_identical(old
, route
)) {
586 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
588 "%s %p: route add %p: needless update of %p",
589 ospf6_route_table_name(table
),
590 (void *)table
, (void *)route
,
592 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
593 zlog_debug("%s: route add: needless update",
594 ospf6_route_table_name(table
));
596 ospf6_route_delete(route
);
597 SET_FLAG(old
->flag
, OSPF6_ROUTE_ADD
);
598 ospf6_route_table_assert(table
);
600 /* to free the lookup lock */
601 route_unlock_node(node
);
605 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
606 zlog_debug("%s %p: route add %p: update of %p",
607 ospf6_route_table_name(table
), (void *)table
,
608 (void *)route
, (void *)old
);
609 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
610 zlog_debug("%s: route add: update",
611 ospf6_route_table_name(table
));
613 /* replace old one if exists */
614 if (node
->info
== old
) {
616 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
620 old
->prev
->next
= route
;
621 route
->prev
= old
->prev
;
623 old
->next
->prev
= route
;
624 route
->next
= old
->next
;
626 route
->installed
= old
->installed
;
627 route
->changed
= now
;
628 assert(route
->table
== NULL
);
629 route
->table
= table
;
631 ospf6_route_unlock(old
); /* will be deleted later */
632 ospf6_route_lock(route
);
634 SET_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
);
635 ospf6_route_table_assert(table
);
638 (*table
->hook_add
)(route
);
643 /* insert if previous or next node found */
645 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
647 "%s %p: route add %p: another path: prev %p, next %p node refcount %u",
648 ospf6_route_table_name(table
), (void *)table
,
649 (void *)route
, (void *)prev
, (void *)next
,
651 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
652 zlog_debug("%s: route add: another path found",
653 ospf6_route_table_name(table
));
667 if (node
->info
== next
) {
668 assert(next
->rnode
== node
);
670 UNSET_FLAG(next
->flag
, OSPF6_ROUTE_BEST
);
671 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
672 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
674 "%s %p: route add %p: replacing previous best: %p",
675 ospf6_route_table_name(table
),
676 (void *)table
, (void *)route
,
680 route
->installed
= now
;
681 route
->changed
= now
;
682 assert(route
->table
== NULL
);
683 route
->table
= table
;
685 ospf6_route_lock(route
);
687 ospf6_route_table_assert(table
);
689 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
691 (*table
->hook_add
)(route
);
696 /* Else, this is the brand new route regarding to the prefix */
697 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
698 zlog_debug("%s %p: route add %p %s : brand new route",
699 ospf6_route_table_name(table
), (void *)table
,
701 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
702 zlog_debug("%s: route add: brand new route",
703 ospf6_route_table_name(table
));
705 assert(node
->info
== NULL
);
707 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
708 ospf6_route_lock(route
);
709 route
->installed
= now
;
710 route
->changed
= now
;
711 assert(route
->table
== NULL
);
712 route
->table
= table
;
714 /* lookup real existing next route */
716 route_lock_node(nextnode
);
718 nextnode
= route_next(nextnode
);
719 } while (nextnode
&& nextnode
->info
== NULL
);
722 if (nextnode
== NULL
)
725 route_unlock_node(nextnode
);
727 next
= nextnode
->info
;
732 /* lookup real existing prev route */
734 route_lock_node(prevnode
);
736 prevnode
= route_prev(prevnode
);
737 } while (prevnode
&& prevnode
->info
== NULL
);
740 if (prevnode
== NULL
)
743 route_unlock_node(prevnode
);
745 prev
= prevnode
->info
;
746 while (prev
->next
&& ospf6_route_is_same(prev
, prev
->next
))
753 ospf6_route_table_assert(table
);
755 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
757 (*table
->hook_add
)(route
);
762 void ospf6_route_remove(struct ospf6_route
*route
,
763 struct ospf6_route_table
*table
)
765 struct route_node
*node
;
766 struct ospf6_route
*current
;
767 char buf
[PREFIX2STR_BUFFER
];
769 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
770 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
772 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
774 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
775 zlog_debug("%s %p: route remove %p: %s refcount %u",
776 ospf6_route_table_name(table
), (void *)table
,
777 (void *)route
, buf
, route
->lock
);
778 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
779 zlog_debug("%s: route remove: %s",
780 ospf6_route_table_name(table
), buf
);
782 node
= route_node_lookup(table
->table
, &route
->prefix
);
785 /* find the route to remove, making sure that the route pointer
786 is from the route table. */
787 current
= node
->info
;
788 while (current
&& current
!= route
)
789 current
= current
->next
;
791 assert(current
== route
);
793 /* adjust doubly linked list */
795 route
->prev
->next
= route
->next
;
797 route
->next
->prev
= route
->prev
;
799 if (node
->info
== route
) {
800 if (route
->next
&& route
->next
->rnode
== node
) {
801 node
->info
= route
->next
;
802 SET_FLAG(route
->next
->flag
, OSPF6_ROUTE_BEST
);
806 route_unlock_node(node
); /* to free the original lock */
810 route_unlock_node(node
); /* to free the lookup lock */
812 ospf6_route_table_assert(table
);
814 SET_FLAG(route
->flag
, OSPF6_ROUTE_WAS_REMOVED
);
816 /* Note hook_remove may call ospf6_route_remove */
817 if (table
->hook_remove
)
818 (*table
->hook_remove
)(route
);
820 ospf6_route_unlock(route
);
823 struct ospf6_route
*ospf6_route_head(struct ospf6_route_table
*table
)
825 struct route_node
*node
;
826 struct ospf6_route
*route
;
828 node
= route_top(table
->table
);
832 /* skip to the real existing entry */
833 while (node
&& node
->info
== NULL
)
834 node
= route_next(node
);
838 route_unlock_node(node
);
841 route
= (struct ospf6_route
*)node
->info
;
842 assert(route
->prev
== NULL
);
843 assert(route
->table
== table
);
844 ospf6_route_lock(route
);
846 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
847 zlog_info("%s %p: route head: %p<-[%p]->%p",
848 ospf6_route_table_name(table
), (void *)table
,
849 (void *)route
->prev
, (void *)route
,
850 (void *)route
->next
);
855 struct ospf6_route
*ospf6_route_next(struct ospf6_route
*route
)
857 struct ospf6_route
*next
= route
->next
;
859 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
860 zlog_info("%s %p: route next: %p<-[%p]->%p",
861 ospf6_route_table_name(route
->table
),
862 (void *)route
->table
, (void *)route
->prev
,
863 (void *)route
, (void *)route
->next
);
865 ospf6_route_unlock(route
);
867 ospf6_route_lock(next
);
872 struct ospf6_route
*ospf6_route_best_next(struct ospf6_route
*route
)
874 struct route_node
*rnode
;
875 struct ospf6_route
*next
;
877 ospf6_route_unlock(route
);
879 rnode
= route
->rnode
;
880 route_lock_node(rnode
);
881 rnode
= route_next(rnode
);
882 while (rnode
&& rnode
->info
== NULL
)
883 rnode
= route_next(rnode
);
886 route_unlock_node(rnode
);
889 next
= (struct ospf6_route
*)rnode
->info
;
890 ospf6_route_lock(next
);
894 struct ospf6_route
*ospf6_route_match_head(struct prefix
*prefix
,
895 struct ospf6_route_table
*table
)
897 struct route_node
*node
;
898 struct ospf6_route
*route
;
900 /* Walk down tree. */
901 node
= table
->table
->top
;
902 while (node
&& node
->p
.prefixlen
< prefix
->prefixlen
903 && prefix_match(&node
->p
, prefix
))
904 node
= node
->link
[prefix_bit(&prefix
->u
.prefix
,
908 route_lock_node(node
);
909 while (node
&& node
->info
== NULL
)
910 node
= route_next(node
);
913 route_unlock_node(node
);
915 if (!prefix_match(prefix
, &node
->p
))
919 ospf6_route_lock(route
);
923 struct ospf6_route
*ospf6_route_match_next(struct prefix
*prefix
,
924 struct ospf6_route
*route
)
926 struct ospf6_route
*next
;
928 next
= ospf6_route_next(route
);
929 if (next
&& !prefix_match(prefix
, &next
->prefix
)) {
930 ospf6_route_unlock(next
);
937 void ospf6_route_remove_all(struct ospf6_route_table
*table
)
939 struct ospf6_route
*route
;
940 for (route
= ospf6_route_head(table
); route
;
941 route
= ospf6_route_next(route
))
942 ospf6_route_remove(route
, table
);
945 struct ospf6_route_table
*ospf6_route_table_create(int s
, int t
)
947 struct ospf6_route_table
*new;
948 new = XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route_table
));
949 new->table
= route_table_init();
955 void ospf6_route_table_delete(struct ospf6_route_table
*table
)
957 ospf6_route_remove_all(table
);
958 bf_free(table
->idspace
);
959 route_table_finish(table
->table
);
960 XFREE(MTYPE_OSPF6_ROUTE
, table
);
965 void ospf6_route_show(struct vty
*vty
, struct ospf6_route
*route
)
968 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
971 struct timeval now
, res
;
972 struct listnode
*node
;
973 struct ospf6_nexthop
*nh
;
976 timersub(&now
, &route
->changed
, &res
);
977 timerstring(&res
, duration
, sizeof(duration
));
980 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
981 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
982 sizeof(destination
));
983 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
984 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
985 destination
, sizeof(destination
));
987 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
990 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
992 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
993 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
996 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
997 (ospf6_route_is_best(route
) ? '*' : ' '),
998 OSPF6_DEST_TYPE_SUBSTR(route
->type
),
999 OSPF6_PATH_TYPE_SUBSTR(route
->path
.type
),
1000 destination
, nexthop
, IFNAMSIZ
, ifname
,
1004 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
1005 "", "", "", nexthop
, IFNAMSIZ
, ifname
, "");
1009 void ospf6_route_show_detail(struct vty
*vty
, struct ospf6_route
*route
)
1012 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1013 char area_id
[16], id
[16], adv_router
[16], capa
[16], options
[16];
1014 struct timeval now
, res
;
1016 struct listnode
*node
;
1017 struct ospf6_nexthop
*nh
;
1022 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1023 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1024 sizeof(destination
));
1025 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1026 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1027 destination
, sizeof(destination
));
1029 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1030 vty_out(vty
, "Destination: %s\n", destination
);
1032 /* destination type */
1033 vty_out(vty
, "Destination type: %s\n",
1034 OSPF6_DEST_TYPE_NAME(route
->type
));
1037 timersub(&now
, &route
->installed
, &res
);
1038 timerstring(&res
, duration
, sizeof(duration
));
1039 vty_out(vty
, "Installed Time: %s ago\n", duration
);
1041 timersub(&now
, &route
->changed
, &res
);
1042 timerstring(&res
, duration
, sizeof(duration
));
1043 vty_out(vty
, " Changed Time: %s ago\n", duration
);
1045 /* Debugging info */
1046 vty_out(vty
, "Lock: %d Flags: %s%s%s%s\n", route
->lock
,
1047 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1048 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1049 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R" : "-"),
1050 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C" : "-"));
1051 vty_out(vty
, "Memory: prev: %p this: %p next: %p\n",
1052 (void *)route
->prev
, (void *)route
, (void *)route
->next
);
1057 inet_ntop(AF_INET
, &route
->path
.area_id
, area_id
, sizeof(area_id
));
1058 vty_out(vty
, "Associated Area: %s\n", area_id
);
1061 vty_out(vty
, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1064 inet_ntop(AF_INET
, &route
->path
.origin
.id
, id
, sizeof(id
));
1065 inet_ntop(AF_INET
, &route
->path
.origin
.adv_router
, adv_router
,
1066 sizeof(adv_router
));
1067 vty_out(vty
, "LS Origin: %s Id: %s Adv: %s\n",
1068 ospf6_lstype_name(route
->path
.origin
.type
), id
, adv_router
);
1071 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1072 vty_out(vty
, "Options: %s\n", options
);
1075 ospf6_capability_printbuf(route
->path
.router_bits
, capa
, sizeof(capa
));
1076 vty_out(vty
, "Router Bits: %s\n", capa
);
1078 /* Prefix Options */
1079 vty_out(vty
, "Prefix Options: xxx\n");
1082 vty_out(vty
, "Metric Type: %d\n", route
->path
.metric_type
);
1083 vty_out(vty
, "Metric: %d (%d)\n", route
->path
.cost
,
1084 route
->path
.u
.cost_e2
);
1086 vty_out(vty
, "Nexthop count: %u\n", route
->nh_list
->count
);
1088 vty_out(vty
, "Nexthop:\n");
1089 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1091 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
1092 ifname
= ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
);
1093 vty_out(vty
, " %s %.*s\n", nexthop
, IFNAMSIZ
, ifname
);
1098 static void ospf6_route_show_table_summary(struct vty
*vty
,
1099 struct ospf6_route_table
*table
)
1101 struct ospf6_route
*route
, *prev
= NULL
;
1102 int i
, pathtype
[OSPF6_PATH_TYPE_MAX
];
1103 unsigned int number
= 0;
1104 int nh_count
= 0, nhinval
= 0, ecmp
= 0;
1105 int alternative
= 0, destination
= 0;
1107 for (i
= 0; i
< OSPF6_PATH_TYPE_MAX
; i
++)
1110 for (route
= ospf6_route_head(table
); route
;
1111 route
= ospf6_route_next(route
)) {
1112 if (prev
== NULL
|| !ospf6_route_is_same(prev
, route
))
1116 nh_count
= ospf6_num_nexthops(route
->nh_list
);
1119 else if (nh_count
> 1)
1121 pathtype
[route
->path
.type
]++;
1127 assert(number
== table
->count
);
1129 vty_out(vty
, "Number of OSPFv3 routes: %d\n", number
);
1130 vty_out(vty
, "Number of Destination: %d\n", destination
);
1131 vty_out(vty
, "Number of Alternative routes: %d\n", alternative
);
1132 vty_out(vty
, "Number of Equal Cost Multi Path: %d\n", ecmp
);
1133 for (i
= OSPF6_PATH_TYPE_INTRA
; i
<= OSPF6_PATH_TYPE_EXTERNAL2
; i
++) {
1134 vty_out(vty
, "Number of %s routes: %d\n",
1135 OSPF6_PATH_TYPE_NAME(i
), pathtype
[i
]);
1139 static void ospf6_route_show_table_prefix(struct vty
*vty
,
1140 struct prefix
*prefix
,
1141 struct ospf6_route_table
*table
)
1143 struct ospf6_route
*route
;
1145 route
= ospf6_route_lookup(prefix
, table
);
1149 ospf6_route_lock(route
);
1150 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1151 /* Specifying a prefix will always display details */
1152 ospf6_route_show_detail(vty
, route
);
1153 route
= ospf6_route_next(route
);
1156 ospf6_route_unlock(route
);
1159 static void ospf6_route_show_table_address(struct vty
*vty
,
1160 struct prefix
*prefix
,
1161 struct ospf6_route_table
*table
)
1163 struct ospf6_route
*route
;
1165 route
= ospf6_route_lookup_bestmatch(prefix
, table
);
1169 prefix
= &route
->prefix
;
1170 ospf6_route_lock(route
);
1171 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1172 /* Specifying a prefix will always display details */
1173 ospf6_route_show_detail(vty
, route
);
1174 route
= ospf6_route_next(route
);
1177 ospf6_route_unlock(route
);
1180 static void ospf6_route_show_table_match(struct vty
*vty
, int detail
,
1181 struct prefix
*prefix
,
1182 struct ospf6_route_table
*table
)
1184 struct ospf6_route
*route
;
1185 assert(prefix
->family
);
1187 route
= ospf6_route_match_head(prefix
, table
);
1190 ospf6_route_show_detail(vty
, route
);
1192 ospf6_route_show(vty
, route
);
1193 route
= ospf6_route_match_next(prefix
, route
);
1197 static void ospf6_route_show_table_type(struct vty
*vty
, int detail
,
1199 struct ospf6_route_table
*table
)
1201 struct ospf6_route
*route
;
1203 route
= ospf6_route_head(table
);
1205 if (route
->path
.type
== type
) {
1207 ospf6_route_show_detail(vty
, route
);
1209 ospf6_route_show(vty
, route
);
1211 route
= ospf6_route_next(route
);
1215 static void ospf6_route_show_table(struct vty
*vty
, int detail
,
1216 struct ospf6_route_table
*table
)
1218 struct ospf6_route
*route
;
1220 route
= ospf6_route_head(table
);
1223 ospf6_route_show_detail(vty
, route
);
1225 ospf6_route_show(vty
, route
);
1226 route
= ospf6_route_next(route
);
1230 int ospf6_route_table_show(struct vty
*vty
, int argc_start
, int argc
,
1231 struct cmd_token
**argv
,
1232 struct ospf6_route_table
*table
)
1240 struct prefix prefix
;
1243 memset(&prefix
, 0, sizeof(struct prefix
));
1245 for (i
= argc_start
; i
< argc
; i
++) {
1246 if (strmatch(argv
[i
]->text
, "summary")) {
1251 if (strmatch(argv
[i
]->text
, "intra-area")) {
1252 type
= OSPF6_PATH_TYPE_INTRA
;
1256 if (strmatch(argv
[i
]->text
, "inter-area")) {
1257 type
= OSPF6_PATH_TYPE_INTER
;
1261 if (strmatch(argv
[i
]->text
, "external-1")) {
1262 type
= OSPF6_PATH_TYPE_EXTERNAL1
;
1266 if (strmatch(argv
[i
]->text
, "external-2")) {
1267 type
= OSPF6_PATH_TYPE_EXTERNAL2
;
1271 if (strmatch(argv
[i
]->text
, "detail")) {
1276 if (strmatch(argv
[i
]->text
, "match")) {
1281 ret
= str2prefix(argv
[i
]->arg
, &prefix
);
1282 if (ret
== 1 && prefix
.family
== AF_INET6
) {
1284 if (strchr(argv
[i
]->arg
, '/'))
1289 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1293 /* Give summary of this route table */
1295 ospf6_route_show_table_summary(vty
, table
);
1299 /* Give exact prefix-match route */
1300 if (isprefix
&& !match
) {
1301 /* If exact address, give best matching route */
1303 ospf6_route_show_table_address(vty
, &prefix
, table
);
1305 ospf6_route_show_table_prefix(vty
, &prefix
, table
);
1311 ospf6_route_show_table_match(vty
, detail
, &prefix
, table
);
1313 ospf6_route_show_table_type(vty
, detail
, type
, table
);
1315 ospf6_route_show_table(vty
, detail
, table
);
1320 static void ospf6_linkstate_show_header(struct vty
*vty
)
1322 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1323 "Net-ID", "Rtr-Bits", "Options", "Cost");
1326 static void ospf6_linkstate_show(struct vty
*vty
, struct ospf6_route
*route
)
1328 u_int32_t router
, id
;
1329 char routername
[16], idname
[16], rbits
[16], options
[16];
1331 router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1332 inet_ntop(AF_INET
, &router
, routername
, sizeof(routername
));
1333 id
= ospf6_linkstate_prefix_id(&route
->prefix
);
1334 inet_ntop(AF_INET
, &id
, idname
, sizeof(idname
));
1336 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1338 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1341 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1342 routername
, idname
, rbits
, options
,
1343 (unsigned long)route
->path
.cost
);
1345 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1346 routername
, idname
, rbits
, options
,
1347 (unsigned long)route
->path
.cost
);
1351 static void ospf6_linkstate_show_table_exact(struct vty
*vty
,
1352 struct prefix
*prefix
,
1353 struct ospf6_route_table
*table
)
1355 struct ospf6_route
*route
;
1357 route
= ospf6_route_lookup(prefix
, table
);
1361 ospf6_route_lock(route
);
1362 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1363 /* Specifying a prefix will always display details */
1364 ospf6_route_show_detail(vty
, route
);
1365 route
= ospf6_route_next(route
);
1368 ospf6_route_unlock(route
);
1371 static void ospf6_linkstate_show_table(struct vty
*vty
, int detail
,
1372 struct ospf6_route_table
*table
)
1374 struct ospf6_route
*route
;
1377 ospf6_linkstate_show_header(vty
);
1379 route
= ospf6_route_head(table
);
1382 ospf6_route_show_detail(vty
, route
);
1384 ospf6_linkstate_show(vty
, route
);
1385 route
= ospf6_route_next(route
);
1389 int ospf6_linkstate_table_show(struct vty
*vty
, int idx_ipv4
, int argc
,
1390 struct cmd_token
**argv
,
1391 struct ospf6_route_table
*table
)
1397 struct prefix router
, id
, prefix
;
1399 memset(&router
, 0, sizeof(struct prefix
));
1400 memset(&id
, 0, sizeof(struct prefix
));
1401 memset(&prefix
, 0, sizeof(struct prefix
));
1403 for (i
= idx_ipv4
; i
< argc
; i
++) {
1404 if (strmatch(argv
[i
]->text
, "detail")) {
1410 ret
= str2prefix(argv
[i
]->arg
, &router
);
1411 if (ret
== 1 && router
.family
== AF_INET
) {
1415 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1420 ret
= str2prefix(argv
[i
]->arg
, &id
);
1421 if (ret
== 1 && id
.family
== AF_INET
) {
1425 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1429 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1434 ospf6_linkstate_prefix(router
.u
.prefix4
.s_addr
,
1435 id
.u
.prefix4
.s_addr
, &prefix
);
1438 ospf6_linkstate_show_table_exact(vty
, &prefix
, table
);
1440 ospf6_linkstate_show_table(vty
, detail
, table
);
1446 void ospf6_brouter_show_header(struct vty
*vty
)
1448 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1449 "Options", "Path-Type", "Area");
1452 void ospf6_brouter_show(struct vty
*vty
, struct ospf6_route
*route
)
1454 u_int32_t adv_router
;
1455 char adv
[16], rbits
[16], options
[16], area
[16];
1457 adv_router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1458 inet_ntop(AF_INET
, &adv_router
, adv
, sizeof(adv
));
1459 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1461 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1462 inet_ntop(AF_INET
, &route
->path
.area_id
, area
, sizeof(area
));
1464 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1465 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1466 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", adv
, rbits
, options
,
1467 OSPF6_PATH_TYPE_NAME(route
->path
.type
), area
);
1470 DEFUN (debug_ospf6_route
,
1471 debug_ospf6_route_cmd
,
1472 "debug ospf6 route <table|intra-area|inter-area|memory>",
1476 "Debug route table calculation\n"
1477 "Debug intra-area route calculation\n"
1478 "Debug inter-area route calculation\n"
1479 "Debug route memory use\n"
1483 unsigned char level
= 0;
1485 if (!strcmp(argv
[idx_type
]->text
, "table"))
1486 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1487 else if (!strcmp(argv
[idx_type
]->text
, "intra-area"))
1488 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1489 else if (!strcmp(argv
[idx_type
]->text
, "inter-area"))
1490 level
= OSPF6_DEBUG_ROUTE_INTER
;
1491 else if (!strcmp(argv
[idx_type
]->text
, "memory"))
1492 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1493 OSPF6_DEBUG_ROUTE_ON(level
);
1497 DEFUN (no_debug_ospf6_route
,
1498 no_debug_ospf6_route_cmd
,
1499 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1504 "Debug route table calculation\n"
1505 "Debug intra-area route calculation\n"
1506 "Debug inter-area route calculation\n"
1507 "Debug route memory use\n")
1510 unsigned char level
= 0;
1512 if (!strcmp(argv
[idx_type
]->text
, "table"))
1513 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1514 else if (!strcmp(argv
[idx_type
]->text
, "intra-area"))
1515 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1516 else if (!strcmp(argv
[idx_type
]->text
, "inter-area"))
1517 level
= OSPF6_DEBUG_ROUTE_INTER
;
1518 else if (!strcmp(argv
[idx_type
]->text
, "memory"))
1519 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1520 OSPF6_DEBUG_ROUTE_OFF(level
);
1524 int config_write_ospf6_debug_route(struct vty
*vty
)
1526 if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
1527 vty_out(vty
, "debug ospf6 route table\n");
1528 if (IS_OSPF6_DEBUG_ROUTE(INTRA
))
1529 vty_out(vty
, "debug ospf6 route intra-area\n");
1530 if (IS_OSPF6_DEBUG_ROUTE(INTER
))
1531 vty_out(vty
, "debug ospf6 route inter-area\n");
1532 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
1533 vty_out(vty
, "debug ospf6 route memory\n");
1538 void install_element_ospf6_debug_route(void)
1540 install_element(ENABLE_NODE
, &debug_ospf6_route_cmd
);
1541 install_element(ENABLE_NODE
, &no_debug_ospf6_route_cmd
);
1542 install_element(CONFIG_NODE
, &debug_ospf6_route_cmd
);
1543 install_element(CONFIG_NODE
, &no_debug_ospf6_route_cmd
);