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"
40 #include "ospf6d/ospf6_route_clippy.c"
42 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_ROUTE
, "OSPF6 route");
43 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_ROUTE_TABLE
, "OSPF6 route table");
44 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_NEXTHOP
, "OSPF6 nexthop");
45 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_PATH
, "OSPF6 Path");
47 unsigned char conf_debug_ospf6_route
= 0;
49 static char *ospf6_route_table_name(struct ospf6_route_table
*table
)
52 switch (table
->scope_type
) {
53 case OSPF6_SCOPE_TYPE_GLOBAL
: {
54 switch (table
->table_type
) {
55 case OSPF6_TABLE_TYPE_ROUTES
:
56 snprintf(name
, sizeof(name
), "global route table");
58 case OSPF6_TABLE_TYPE_BORDER_ROUTERS
:
59 snprintf(name
, sizeof(name
), "global brouter table");
61 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES
:
62 snprintf(name
, sizeof(name
), "global external table");
65 snprintf(name
, sizeof(name
), "global unknown table");
70 case OSPF6_SCOPE_TYPE_AREA
: {
71 struct ospf6_area
*oa
= (struct ospf6_area
*)table
->scope
;
72 switch (table
->table_type
) {
73 case OSPF6_TABLE_TYPE_SPF_RESULTS
:
74 snprintf(name
, sizeof(name
), "area %s spf table",
77 case OSPF6_TABLE_TYPE_ROUTES
:
78 snprintf(name
, sizeof(name
), "area %s route table",
81 case OSPF6_TABLE_TYPE_PREFIX_RANGES
:
82 snprintf(name
, sizeof(name
), "area %s range table",
85 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES
:
86 snprintf(name
, sizeof(name
),
87 "area %s summary prefix table", oa
->name
);
89 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS
:
90 snprintf(name
, sizeof(name
),
91 "area %s summary router table", oa
->name
);
94 snprintf(name
, sizeof(name
), "area %s unknown table",
100 case OSPF6_SCOPE_TYPE_INTERFACE
: {
101 struct ospf6_interface
*oi
=
102 (struct ospf6_interface
*)table
->scope
;
103 switch (table
->table_type
) {
104 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES
:
105 snprintf(name
, sizeof(name
),
106 "interface %s connected table",
107 oi
->interface
->name
);
110 snprintf(name
, sizeof(name
),
111 "interface %s unknown table",
112 oi
->interface
->name
);
118 switch (table
->table_type
) {
119 case OSPF6_TABLE_TYPE_SPF_RESULTS
:
120 snprintf(name
, sizeof(name
), "temporary spf table");
123 snprintf(name
, sizeof(name
), "temporary unknown table");
131 void ospf6_linkstate_prefix(uint32_t adv_router
, uint32_t id
,
132 struct prefix
*prefix
)
134 memset(prefix
, 0, sizeof(struct prefix
));
135 prefix
->family
= AF_INET6
;
136 prefix
->prefixlen
= 64;
137 memcpy(&prefix
->u
.prefix6
.s6_addr
[0], &adv_router
, 4);
138 memcpy(&prefix
->u
.prefix6
.s6_addr
[4], &id
, 4);
141 void ospf6_linkstate_prefix2str(struct prefix
*prefix
, char *buf
, int size
)
143 uint32_t adv_router
, id
;
144 char adv_router_str
[16], id_str
[16];
145 memcpy(&adv_router
, &prefix
->u
.prefix6
.s6_addr
[0], 4);
146 memcpy(&id
, &prefix
->u
.prefix6
.s6_addr
[4], 4);
147 inet_ntop(AF_INET
, &adv_router
, adv_router_str
, sizeof(adv_router_str
));
148 inet_ntop(AF_INET
, &id
, id_str
, sizeof(id_str
));
150 snprintf(buf
, size
, "%s Net-ID: %s", adv_router_str
, id_str
);
152 snprintf(buf
, size
, "%s", adv_router_str
);
155 /* Global strings for logging */
156 const char *const ospf6_dest_type_str
[OSPF6_DEST_TYPE_MAX
] = {
157 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
160 const char *const ospf6_dest_type_substr
[OSPF6_DEST_TYPE_MAX
] = {
161 "?", "R", "N", "D", "L", "A",
164 const char *const ospf6_path_type_str
[OSPF6_PATH_TYPE_MAX
] = {
165 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
168 const char *const ospf6_path_type_substr
[OSPF6_PATH_TYPE_MAX
] = {
169 "??", "IA", "IE", "E1", "E2",
172 const char *ospf6_path_type_json
[OSPF6_PATH_TYPE_MAX
] = {
173 "UnknownRoute", "IntraArea", "InterArea", "External1", "External2",
177 struct ospf6_nexthop
*ospf6_nexthop_create(void)
179 struct ospf6_nexthop
*nh
;
181 nh
= XCALLOC(MTYPE_OSPF6_NEXTHOP
, sizeof(struct ospf6_nexthop
));
185 void ospf6_nexthop_delete(struct ospf6_nexthop
*nh
)
187 XFREE(MTYPE_OSPF6_NEXTHOP
, nh
);
190 void ospf6_clear_nexthops(struct list
*nh_list
)
192 struct listnode
*node
;
193 struct ospf6_nexthop
*nh
;
196 for (ALL_LIST_ELEMENTS_RO(nh_list
, node
, nh
))
197 ospf6_nexthop_clear(nh
);
201 static struct ospf6_nexthop
*
202 ospf6_route_find_nexthop(struct list
*nh_list
, struct ospf6_nexthop
*nh_match
)
204 struct listnode
*node
;
205 struct ospf6_nexthop
*nh
;
207 if (nh_list
&& nh_match
) {
208 for (ALL_LIST_ELEMENTS_RO(nh_list
, node
, nh
)) {
209 if (ospf6_nexthop_is_same(nh
, nh_match
))
217 void ospf6_copy_nexthops(struct list
*dst
, struct list
*src
)
219 struct ospf6_nexthop
*nh_new
, *nh
;
220 struct listnode
*node
;
223 for (ALL_LIST_ELEMENTS_RO(src
, node
, nh
)) {
224 if (ospf6_nexthop_is_set(nh
)) {
225 nh_new
= ospf6_nexthop_create();
226 ospf6_nexthop_copy(nh_new
, nh
);
227 listnode_add_sort(dst
, nh_new
);
233 void ospf6_merge_nexthops(struct list
*dst
, struct list
*src
)
235 struct listnode
*node
;
236 struct ospf6_nexthop
*nh
, *nh_new
;
239 for (ALL_LIST_ELEMENTS_RO(src
, node
, nh
)) {
240 if (!ospf6_route_find_nexthop(dst
, nh
)) {
241 nh_new
= ospf6_nexthop_create();
242 ospf6_nexthop_copy(nh_new
, nh
);
243 listnode_add_sort(dst
, nh_new
);
250 * If the nexthops are the same return true
252 bool ospf6_route_cmp_nexthops(struct ospf6_route
*a
, struct ospf6_route
*b
)
254 struct listnode
*anode
, *bnode
;
255 struct ospf6_nexthop
*anh
, *bnh
;
256 bool identical
= false;
259 if (listcount(a
->nh_list
) == listcount(b
->nh_list
)) {
260 for (ALL_LIST_ELEMENTS_RO(a
->nh_list
, anode
, anh
)) {
262 for (ALL_LIST_ELEMENTS_RO(b
->nh_list
, bnode
,
264 if (ospf6_nexthop_is_same(anh
, bnh
))
267 /* Currnet List A element not found List B
268 * Non-Identical lists return */
269 if (identical
== false)
276 /* One of the routes doesn't exist ? */
280 int ospf6_num_nexthops(struct list
*nh_list
)
282 return (listcount(nh_list
));
285 void ospf6_add_nexthop(struct list
*nh_list
, int ifindex
, struct in6_addr
*addr
)
287 struct ospf6_nexthop
*nh
;
288 struct ospf6_nexthop nh_match
;
293 nh_match
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
295 nh_match
.type
= NEXTHOP_TYPE_IPV6
;
297 memcpy(&nh_match
.address
, addr
,
298 sizeof(struct in6_addr
));
300 nh_match
.type
= NEXTHOP_TYPE_IFINDEX
;
302 memset(&nh_match
.address
, 0, sizeof(struct in6_addr
));
305 nh_match
.ifindex
= ifindex
;
307 if (!ospf6_route_find_nexthop(nh_list
, &nh_match
)) {
308 nh
= ospf6_nexthop_create();
309 ospf6_nexthop_copy(nh
, &nh_match
);
310 listnode_add(nh_list
, nh
);
315 void ospf6_add_route_nexthop_blackhole(struct ospf6_route
*route
)
317 struct ospf6_nexthop
*nh
;
318 struct ospf6_nexthop nh_match
= {};
320 /* List not allocated. */
321 if (route
->nh_list
== NULL
)
324 /* Entry already exists. */
325 nh_match
.type
= NEXTHOP_TYPE_BLACKHOLE
;
326 if (ospf6_route_find_nexthop(route
->nh_list
, &nh_match
))
329 nh
= ospf6_nexthop_create();
330 ospf6_nexthop_copy(nh
, &nh_match
);
331 listnode_add(route
->nh_list
, nh
);
334 void ospf6_route_zebra_copy_nexthops(struct ospf6_route
*route
,
335 struct zapi_nexthop nexthops
[],
336 int entries
, vrf_id_t vrf_id
)
338 struct ospf6_nexthop
*nh
;
339 struct listnode
*node
;
344 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
345 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
346 zlog_debug(" nexthop: %s %pI6%%%.*s(%d)",
347 nexthop_type_to_str(nh
->type
),
348 &nh
->address
, IFNAMSIZ
,
349 ifindex2ifname(nh
->ifindex
, vrf_id
),
356 nexthops
[i
].vrf_id
= vrf_id
;
357 nexthops
[i
].type
= nh
->type
;
360 case NEXTHOP_TYPE_BLACKHOLE
:
364 case NEXTHOP_TYPE_IFINDEX
:
365 nexthops
[i
].ifindex
= nh
->ifindex
;
368 case NEXTHOP_TYPE_IPV4_IFINDEX
:
369 case NEXTHOP_TYPE_IPV4
:
371 * OSPFv3 with IPv4 routes is not supported
372 * yet. Skip this next hop.
374 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
375 zlog_debug(" Skipping IPv4 next hop");
378 case NEXTHOP_TYPE_IPV6_IFINDEX
:
379 nexthops
[i
].ifindex
= nh
->ifindex
;
381 case NEXTHOP_TYPE_IPV6
:
382 nexthops
[i
].gate
.ipv6
= nh
->address
;
390 int ospf6_route_get_first_nh_index(struct ospf6_route
*route
)
392 struct ospf6_nexthop
*nh
;
395 nh
= listnode_head(route
->nh_list
);
403 int ospf6_nexthop_cmp(struct ospf6_nexthop
*a
, struct ospf6_nexthop
*b
)
405 if (a
->ifindex
< b
->ifindex
)
407 else if (a
->ifindex
> b
->ifindex
)
410 return memcmp(&a
->address
, &b
->address
,
411 sizeof(struct in6_addr
));
414 static int ospf6_path_cmp(struct ospf6_path
*a
, struct ospf6_path
*b
)
416 if (a
->origin
.adv_router
< b
->origin
.adv_router
)
418 else if (a
->origin
.adv_router
> b
->origin
.adv_router
)
424 void ospf6_path_free(struct ospf6_path
*op
)
427 list_delete(&op
->nh_list
);
428 XFREE(MTYPE_OSPF6_PATH
, op
);
431 struct ospf6_path
*ospf6_path_dup(struct ospf6_path
*path
)
433 struct ospf6_path
*new;
435 new = XCALLOC(MTYPE_OSPF6_PATH
, sizeof(struct ospf6_path
));
436 memcpy(new, path
, sizeof(struct ospf6_path
));
437 new->nh_list
= list_new();
438 new->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
439 new->nh_list
->del
= (void (*)(void *))ospf6_nexthop_delete
;
444 void ospf6_copy_paths(struct list
*dst
, struct list
*src
)
446 struct ospf6_path
*path_new
, *path
;
447 struct listnode
*node
;
450 for (ALL_LIST_ELEMENTS_RO(src
, node
, path
)) {
451 path_new
= ospf6_path_dup(path
);
452 ospf6_copy_nexthops(path_new
->nh_list
, path
->nh_list
);
453 listnode_add_sort(dst
, path_new
);
458 struct ospf6_route
*ospf6_route_create(struct ospf6
*ospf6
)
460 struct ospf6_route
*route
;
462 route
= XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route
));
463 route
->nh_list
= list_new();
464 route
->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
465 route
->nh_list
->del
= (void (*)(void *))ospf6_nexthop_delete
;
466 route
->paths
= list_new();
467 route
->paths
->cmp
= (int (*)(void *, void *))ospf6_path_cmp
;
468 route
->paths
->del
= (void (*)(void *))ospf6_path_free
;
469 route
->ospf6
= ospf6
;
474 void ospf6_route_delete(struct ospf6_route
*route
)
478 list_delete(&route
->nh_list
);
480 list_delete(&route
->paths
);
481 XFREE(MTYPE_OSPF6_ROUTE
, route
);
485 struct ospf6_route
*ospf6_route_copy(struct ospf6_route
*route
)
487 struct ospf6_route
*new;
489 new = ospf6_route_create(route
->ospf6
);
490 new->type
= route
->type
;
491 memcpy(&new->prefix
, &route
->prefix
, sizeof(struct prefix
));
492 new->prefix_options
= route
->prefix_options
;
493 new->installed
= route
->installed
;
494 new->changed
= route
->changed
;
495 new->flag
= route
->flag
;
496 new->route_option
= route
->route_option
;
497 new->linkstate_id
= route
->linkstate_id
;
498 new->path
= route
->path
;
499 ospf6_copy_nexthops(new->nh_list
, route
->nh_list
);
500 ospf6_copy_paths(new->paths
, route
->paths
);
509 void ospf6_route_lock(struct ospf6_route
*route
)
514 void ospf6_route_unlock(struct ospf6_route
*route
)
516 assert(route
->lock
> 0);
518 if (route
->lock
== 0) {
519 /* Can't detach from the table until here
520 because ospf6_route_next () will use
521 the 'route->table' pointer for logging */
523 ospf6_route_delete(route
);
527 /* Route compare function. If ra is more preferred, it returns
528 less than 0. If rb is more preferred returns greater than 0.
529 Otherwise (neither one is preferred), returns 0 */
530 int ospf6_route_cmp(struct ospf6_route
*ra
, struct ospf6_route
*rb
)
532 assert(ospf6_route_is_same(ra
, rb
));
533 assert(OSPF6_PATH_TYPE_NONE
< ra
->path
.type
534 && ra
->path
.type
< OSPF6_PATH_TYPE_MAX
);
535 assert(OSPF6_PATH_TYPE_NONE
< rb
->path
.type
536 && rb
->path
.type
< OSPF6_PATH_TYPE_MAX
);
538 if (ra
->type
!= rb
->type
)
539 return (ra
->type
- rb
->type
);
541 if (ra
->path
.type
!= rb
->path
.type
)
542 return (ra
->path
.type
- rb
->path
.type
);
544 if (ra
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
) {
545 if (ra
->path
.u
.cost_e2
!= rb
->path
.u
.cost_e2
)
546 return (ra
->path
.u
.cost_e2
- rb
->path
.u
.cost_e2
);
548 return (ra
->path
.cost
- rb
->path
.cost
);
550 if (ra
->path
.cost
!= rb
->path
.cost
)
551 return (ra
->path
.cost
- rb
->path
.cost
);
554 if (ra
->path
.area_id
!= rb
->path
.area_id
)
555 return (ntohl(ra
->path
.area_id
) - ntohl(rb
->path
.area_id
));
560 struct ospf6_route
*ospf6_route_lookup(struct prefix
*prefix
,
561 struct ospf6_route_table
*table
)
563 struct route_node
*node
;
564 struct ospf6_route
*route
;
566 node
= route_node_lookup(table
->table
, prefix
);
570 route
= (struct ospf6_route
*)node
->info
;
571 route_unlock_node(node
); /* to free the lookup lock */
576 ospf6_route_lookup_identical(struct ospf6_route
*route
,
577 struct ospf6_route_table
*table
)
579 struct ospf6_route
*target
;
581 for (target
= ospf6_route_lookup(&route
->prefix
, table
); target
;
582 target
= target
->next
) {
583 if (ospf6_route_is_identical(target
, route
))
590 ospf6_route_lookup_bestmatch(struct prefix
*prefix
,
591 struct ospf6_route_table
*table
)
593 struct route_node
*node
;
594 struct ospf6_route
*route
;
596 node
= route_node_match(table
->table
, prefix
);
599 route_unlock_node(node
);
601 route
= (struct ospf6_route
*)node
->info
;
606 static void route_table_assert(struct ospf6_route_table
*table
)
608 struct ospf6_route
*prev
, *r
, *next
;
609 unsigned int link_error
= 0, num
= 0;
611 r
= ospf6_route_head(table
);
617 next
= ospf6_route_next(r
);
626 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
629 if (link_error
== 0 && num
== table
->count
)
632 flog_err(EC_LIB_DEVELOPMENT
, "PANIC !!");
633 flog_err(EC_LIB_DEVELOPMENT
,
634 "Something has gone wrong with ospf6_route_table[%p]", table
);
635 zlog_debug("table count = %d, real number = %d", table
->count
, num
);
636 zlog_debug("DUMP START");
637 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
638 zlog_info("%p<-[%p]->%p : %pFX", r
->prev
, r
, r
->next
,
640 zlog_debug("DUMP END");
642 assert(link_error
== 0 && num
== table
->count
);
644 #define ospf6_route_table_assert(t) (route_table_assert (t))
646 #define ospf6_route_table_assert(t) ((void) 0)
649 struct ospf6_route
*ospf6_route_add(struct ospf6_route
*route
,
650 struct ospf6_route_table
*table
)
652 struct route_node
*node
, *nextnode
, *prevnode
;
653 struct ospf6_route
*current
= NULL
;
654 struct ospf6_route
*prev
= NULL
, *old
= NULL
, *next
= NULL
;
655 char buf
[PREFIX2STR_BUFFER
];
658 assert(route
->rnode
== NULL
);
659 assert(route
->lock
== 0);
660 assert(route
->next
== NULL
);
661 assert(route
->prev
== NULL
);
663 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
664 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
665 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
666 inet_ntop(AF_INET
, &ADV_ROUTER_IN_PREFIX(&route
->prefix
), buf
,
669 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
671 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
672 zlog_debug("%s %p: route add %p: %s paths %u nh %u",
673 ospf6_route_table_name(table
), (void *)table
,
674 (void *)route
, buf
, listcount(route
->paths
),
675 listcount(route
->nh_list
));
676 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
677 zlog_debug("%s: route add: %s", ospf6_route_table_name(table
),
682 node
= route_node_get(table
->table
, &route
->prefix
);
685 /* find place to insert */
686 for (current
= node
->info
; current
; current
= current
->next
) {
687 if (!ospf6_route_is_same(current
, route
))
689 else if (current
->type
!= route
->type
)
691 else if (ospf6_route_is_same_origin(current
, route
))
693 else if (ospf6_route_cmp(current
, route
) > 0)
703 /* if route does not actually change, return unchanged */
704 if (ospf6_route_is_identical(old
, route
)) {
705 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
707 "%s %p: route add %p: needless update of %p old cost %u",
708 ospf6_route_table_name(table
),
709 (void *)table
, (void *)route
,
710 (void *)old
, old
->path
.cost
);
711 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
712 zlog_debug("%s: route add: needless update",
713 ospf6_route_table_name(table
));
715 ospf6_route_delete(route
);
716 SET_FLAG(old
->flag
, OSPF6_ROUTE_ADD
);
717 ospf6_route_table_assert(table
);
719 /* to free the lookup lock */
720 route_unlock_node(node
);
724 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
726 "%s %p: route add %p cost %u paths %u nh %u: update of %p cost %u paths %u nh %u",
727 ospf6_route_table_name(table
), (void *)table
,
728 (void *)route
, route
->path
.cost
,
729 listcount(route
->paths
),
730 listcount(route
->nh_list
), (void *)old
,
731 old
->path
.cost
, listcount(old
->paths
),
732 listcount(old
->nh_list
));
733 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
734 zlog_debug("%s: route add: update",
735 ospf6_route_table_name(table
));
737 /* replace old one if exists */
738 if (node
->info
== old
) {
740 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
741 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
742 zlog_debug("%s: replace old route %s",
747 old
->prev
->next
= route
;
748 route
->prev
= old
->prev
;
750 old
->next
->prev
= route
;
751 route
->next
= old
->next
;
753 route
->installed
= old
->installed
;
754 route
->changed
= now
;
755 assert(route
->table
== NULL
);
756 route
->table
= table
;
758 ospf6_route_unlock(old
); /* will be deleted later */
759 ospf6_route_lock(route
);
761 SET_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
);
762 ospf6_route_table_assert(table
);
765 (*table
->hook_add
)(route
);
770 /* insert if previous or next node found */
772 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
774 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
775 ospf6_route_table_name(table
), (void *)table
,
776 (void *)route
, route
->path
.cost
, (void *)prev
,
777 (void *)next
, route_node_get_lock_count(node
));
778 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
779 zlog_debug("%s: route add cost %u: another path found",
780 ospf6_route_table_name(table
),
795 if (node
->info
== next
) {
796 assert(next
&& next
->rnode
== node
);
798 UNSET_FLAG(next
->flag
, OSPF6_ROUTE_BEST
);
799 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
800 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
802 "%s %p: route add %p cost %u: replacing previous best: %p cost %u",
803 ospf6_route_table_name(table
),
804 (void *)table
, (void *)route
,
805 route
->path
.cost
, (void *)next
,
809 route
->installed
= now
;
810 route
->changed
= now
;
811 assert(route
->table
== NULL
);
812 route
->table
= table
;
814 ospf6_route_lock(route
);
816 ospf6_route_table_assert(table
);
818 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
820 (*table
->hook_add
)(route
);
825 /* Else, this is the brand new route regarding to the prefix */
826 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
827 zlog_debug("%s %p: route add %p %s cost %u: brand new route",
828 ospf6_route_table_name(table
), (void *)table
,
829 (void *)route
, buf
, route
->path
.cost
);
830 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
831 zlog_debug("%s: route add: brand new route",
832 ospf6_route_table_name(table
));
834 assert(node
->info
== NULL
);
836 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
837 ospf6_route_lock(route
);
838 route
->installed
= now
;
839 route
->changed
= now
;
840 assert(route
->table
== NULL
);
841 route
->table
= table
;
843 /* lookup real existing next route */
845 route_lock_node(nextnode
);
847 nextnode
= route_next(nextnode
);
848 } while (nextnode
&& nextnode
->info
== NULL
);
851 if (nextnode
== NULL
)
854 route_unlock_node(nextnode
);
856 next
= nextnode
->info
;
861 /* lookup real existing prev route */
863 route_lock_node(prevnode
);
865 prevnode
= route_prev(prevnode
);
866 } while (prevnode
&& prevnode
->info
== NULL
);
869 if (prevnode
== NULL
)
872 route_unlock_node(prevnode
);
874 prev
= prevnode
->info
;
875 while (prev
->next
&& ospf6_route_is_same(prev
, prev
->next
))
882 ospf6_route_table_assert(table
);
884 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
886 (*table
->hook_add
)(route
);
891 void ospf6_route_remove(struct ospf6_route
*route
,
892 struct ospf6_route_table
*table
)
894 struct route_node
*node
;
895 struct ospf6_route
*current
;
896 char buf
[PREFIX2STR_BUFFER
];
898 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
899 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
900 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
901 inet_ntop(AF_INET
, &ADV_ROUTER_IN_PREFIX(&route
->prefix
), buf
,
904 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
906 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
907 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
908 ospf6_route_table_name(table
), (void *)table
,
909 (void *)route
, buf
, route
->path
.cost
, route
->lock
);
910 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
911 zlog_debug("%s: route remove: %s",
912 ospf6_route_table_name(table
), buf
);
914 node
= route_node_lookup(table
->table
, &route
->prefix
);
917 /* find the route to remove, making sure that the route pointer
918 is from the route table. */
919 current
= node
->info
;
920 while (current
&& current
!= route
)
921 current
= current
->next
;
923 assert(current
== route
);
925 /* adjust doubly linked list */
927 route
->prev
->next
= route
->next
;
929 route
->next
->prev
= route
->prev
;
931 if (node
->info
== route
) {
932 if (route
->next
&& route
->next
->rnode
== node
) {
933 node
->info
= route
->next
;
934 SET_FLAG(route
->next
->flag
, OSPF6_ROUTE_BEST
);
935 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
936 zlog_debug("%s: remove route %s", __func__
,
941 route_unlock_node(node
); /* to free the original lock */
945 route_unlock_node(node
); /* to free the lookup lock */
947 ospf6_route_table_assert(table
);
949 SET_FLAG(route
->flag
, OSPF6_ROUTE_WAS_REMOVED
);
951 /* Note hook_remove may call ospf6_route_remove */
952 if (table
->hook_remove
)
953 (*table
->hook_remove
)(route
);
955 ospf6_route_unlock(route
);
958 struct ospf6_route
*ospf6_route_head(struct ospf6_route_table
*table
)
960 struct route_node
*node
;
961 struct ospf6_route
*route
;
963 node
= route_top(table
->table
);
967 /* skip to the real existing entry */
968 while (node
&& node
->info
== NULL
)
969 node
= route_next(node
);
973 route_unlock_node(node
);
976 route
= (struct ospf6_route
*)node
->info
;
977 assert(route
->prev
== NULL
);
978 assert(route
->table
== table
);
979 ospf6_route_lock(route
);
981 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
982 zlog_info("%s %p: route head: %p<-[%p]->%p",
983 ospf6_route_table_name(table
), (void *)table
,
984 (void *)route
->prev
, (void *)route
,
985 (void *)route
->next
);
990 struct ospf6_route
*ospf6_route_next(struct ospf6_route
*route
)
992 struct ospf6_route
*next
= route
->next
;
994 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
995 zlog_info("%s %p: route next: %p<-[%p]->%p , route ref count %u",
996 ospf6_route_table_name(route
->table
),
997 (void *)route
->table
, (void *)route
->prev
,
998 (void *)route
, (void *)route
->next
,
1001 ospf6_route_unlock(route
);
1003 ospf6_route_lock(next
);
1008 struct ospf6_route
*ospf6_route_best_next(struct ospf6_route
*route
)
1010 struct route_node
*rnode
;
1011 struct ospf6_route
*next
;
1013 ospf6_route_unlock(route
);
1015 rnode
= route
->rnode
;
1016 route_lock_node(rnode
);
1017 rnode
= route_next(rnode
);
1018 while (rnode
&& rnode
->info
== NULL
)
1019 rnode
= route_next(rnode
);
1022 route_unlock_node(rnode
);
1024 assert(rnode
->info
);
1025 next
= (struct ospf6_route
*)rnode
->info
;
1026 ospf6_route_lock(next
);
1030 struct ospf6_route
*ospf6_route_match_head(struct prefix
*prefix
,
1031 struct ospf6_route_table
*table
)
1033 struct route_node
*node
;
1034 struct ospf6_route
*route
;
1036 /* Walk down tree. */
1037 node
= table
->table
->top
;
1038 while (node
&& node
->p
.prefixlen
< prefix
->prefixlen
1039 && prefix_match(&node
->p
, prefix
))
1040 node
= node
->link
[prefix_bit(&prefix
->u
.prefix
,
1041 node
->p
.prefixlen
)];
1044 route_lock_node(node
);
1045 while (node
&& node
->info
== NULL
)
1046 node
= route_next(node
);
1049 route_unlock_node(node
);
1051 if (!prefix_match(prefix
, &node
->p
))
1055 ospf6_route_lock(route
);
1059 struct ospf6_route
*ospf6_route_match_next(struct prefix
*prefix
,
1060 struct ospf6_route
*route
)
1062 struct ospf6_route
*next
;
1064 next
= ospf6_route_next(route
);
1065 if (next
&& !prefix_match(prefix
, &next
->prefix
)) {
1066 ospf6_route_unlock(next
);
1073 void ospf6_route_remove_all(struct ospf6_route_table
*table
)
1075 struct ospf6_route
*route
;
1076 for (route
= ospf6_route_head(table
); route
;
1077 route
= ospf6_route_next(route
))
1078 ospf6_route_remove(route
, table
);
1081 struct ospf6_route_table
*ospf6_route_table_create(int s
, int t
)
1083 struct ospf6_route_table
*new;
1084 new = XCALLOC(MTYPE_OSPF6_ROUTE_TABLE
,
1085 sizeof(struct ospf6_route_table
));
1086 new->table
= route_table_init();
1087 new->scope_type
= s
;
1088 new->table_type
= t
;
1092 void ospf6_route_table_delete(struct ospf6_route_table
*table
)
1094 ospf6_route_remove_all(table
);
1095 route_table_finish(table
->table
);
1096 XFREE(MTYPE_OSPF6_ROUTE_TABLE
, table
);
1101 void ospf6_route_show(struct vty
*vty
, struct ospf6_route
*route
,
1102 json_object
*json_routes
, bool use_json
)
1105 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1107 struct timeval now
, res
;
1108 struct listnode
*node
;
1109 struct ospf6_nexthop
*nh
;
1110 json_object
*json_route
= NULL
;
1111 json_object
*json_array_next_hops
= NULL
;
1112 json_object
*json_next_hop
;
1113 vrf_id_t vrf_id
= route
->ospf6
->vrf_id
;
1116 timersub(&now
, &route
->changed
, &res
);
1117 timerstring(&res
, duration
, sizeof(duration
));
1120 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1121 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1122 sizeof(destination
));
1123 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1124 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1125 destination
, sizeof(destination
));
1127 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1130 json_route
= json_object_new_object();
1131 json_object_boolean_add(json_route
, "isBestRoute",
1132 ospf6_route_is_best(route
));
1133 json_object_string_add(json_route
, "destinationType",
1134 OSPF6_DEST_TYPE_SUBSTR(route
->type
));
1135 json_object_string_add(
1136 json_route
, "pathType",
1137 OSPF6_PATH_TYPE_SUBSTR(route
->path
.type
));
1138 json_object_string_add(json_route
, "duration", duration
);
1143 json_array_next_hops
= json_object_new_array();
1146 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1148 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
1150 json_next_hop
= json_object_new_object();
1151 json_object_string_add(json_next_hop
, "nextHop",
1153 json_object_string_add(
1154 json_next_hop
, "interfaceName",
1155 ifindex2ifname(nh
->ifindex
, vrf_id
));
1156 json_object_array_add(json_array_next_hops
,
1160 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1161 (ospf6_route_is_best(route
) ? '*'
1163 OSPF6_DEST_TYPE_SUBSTR(route
->type
),
1164 OSPF6_PATH_TYPE_SUBSTR(
1166 destination
, nexthop
, IFNAMSIZ
,
1167 ifindex2ifname(nh
->ifindex
, vrf_id
),
1171 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1172 ' ', "", "", "", nexthop
, IFNAMSIZ
,
1173 ifindex2ifname(nh
->ifindex
, vrf_id
),
1178 json_object_object_add(json_route
, "nextHops",
1179 json_array_next_hops
);
1180 json_object_object_add(json_routes
, destination
, json_route
);
1184 void ospf6_route_show_detail(struct vty
*vty
, struct ospf6_route
*route
,
1185 json_object
*json_routes
, bool use_json
)
1187 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1188 char area_id
[16], id
[16], adv_router
[16], capa
[16], options
[32];
1189 char pfx_options
[16];
1190 struct timeval now
, res
;
1192 struct listnode
*node
;
1193 struct ospf6_nexthop
*nh
;
1195 json_object
*json_route
= NULL
;
1196 json_object
*json_array_next_hops
= NULL
;
1197 json_object
*json_next_hop
;
1198 vrf_id_t vrf_id
= route
->ospf6
->vrf_id
;
1203 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1204 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1205 sizeof(destination
));
1206 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1207 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1208 destination
, sizeof(destination
));
1210 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1213 json_route
= json_object_new_object();
1214 json_object_string_add(json_route
, "destinationType",
1215 OSPF6_DEST_TYPE_NAME(route
->type
));
1217 vty_out(vty
, "Destination: %s\n", destination
);
1218 vty_out(vty
, "Destination type: %s\n",
1219 OSPF6_DEST_TYPE_NAME(route
->type
));
1223 timersub(&now
, &route
->installed
, &res
);
1224 timerstring(&res
, duration
, sizeof(duration
));
1226 json_object_string_add(json_route
, "installedTimeSince",
1229 vty_out(vty
, "Installed Time: %s ago\n", duration
);
1231 timersub(&now
, &route
->changed
, &res
);
1232 timerstring(&res
, duration
, sizeof(duration
));
1234 json_object_string_add(json_route
, "changedTimeSince",
1237 vty_out(vty
, "Changed Time: %s ago\n", duration
);
1239 /* Debugging info */
1241 json_object_int_add(json_route
, "numberOfLock", route
->lock
);
1243 flag
, sizeof(flag
), "%s%s%s%s",
1244 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1245 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1246 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R"
1248 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C"
1250 json_object_string_add(json_route
, "flags", flag
);
1252 vty_out(vty
, "Lock: %d Flags: %s%s%s%s\n", route
->lock
,
1253 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1254 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1255 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R"
1257 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C"
1259 vty_out(vty
, "Memory: prev: %p this: %p next: %p\n",
1260 (void *)route
->prev
, (void *)route
,
1261 (void *)route
->next
);
1267 inet_ntop(AF_INET
, &route
->path
.area_id
, area_id
, sizeof(area_id
));
1269 json_object_string_add(json_route
, "associatedArea", area_id
);
1271 vty_out(vty
, "Associated Area: %s\n", area_id
);
1275 json_object_string_add(json_route
, "pathType",
1276 OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1278 vty_out(vty
, "Path Type: %s\n",
1279 OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1282 inet_ntop(AF_INET
, &route
->path
.origin
.id
, id
, sizeof(id
));
1283 inet_ntop(AF_INET
, &route
->path
.origin
.adv_router
, adv_router
,
1284 sizeof(adv_router
));
1286 json_object_string_add(
1287 json_route
, "lsOriginRoutePathType",
1288 ospf6_lstype_name(route
->path
.origin
.type
));
1289 json_object_string_add(json_route
, "lsId", id
);
1290 json_object_string_add(json_route
, "lsAdvertisingRouter",
1293 vty_out(vty
, "LS Origin: %s Id: %s Adv: %s\n",
1294 ospf6_lstype_name(route
->path
.origin
.type
), id
,
1299 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1301 json_object_string_add(json_route
, "options", options
);
1303 vty_out(vty
, "Options: %s\n", options
);
1306 ospf6_capability_printbuf(route
->path
.router_bits
, capa
, sizeof(capa
));
1308 json_object_string_add(json_route
, "routerBits", capa
);
1310 vty_out(vty
, "Router Bits: %s\n", capa
);
1312 /* Prefix Options */
1313 ospf6_prefix_options_printbuf(route
->prefix_options
, pfx_options
,
1314 sizeof(pfx_options
));
1316 json_object_string_add(json_route
, "prefixOptions",
1319 vty_out(vty
, "Prefix Options: %s\n", pfx_options
);
1323 json_object_int_add(json_route
, "metricType",
1324 route
->path
.metric_type
);
1325 json_object_int_add(json_route
, "metricCost", route
->path
.cost
);
1326 json_object_int_add(json_route
, "metricCostE2",
1327 route
->path
.u
.cost_e2
);
1329 json_object_int_add(json_route
, "pathsCount",
1330 route
->paths
->count
);
1331 json_object_int_add(json_route
, "nextHopCount",
1332 route
->nh_list
->count
);
1334 vty_out(vty
, "Metric Type: %d\n", route
->path
.metric_type
);
1335 vty_out(vty
, "Metric: %d (%d)\n", route
->path
.cost
,
1336 route
->path
.u
.cost_e2
);
1338 vty_out(vty
, "Paths count: %u\n", route
->paths
->count
);
1339 vty_out(vty
, "Nexthop count: %u\n", route
->nh_list
->count
);
1344 json_array_next_hops
= json_object_new_array();
1346 vty_out(vty
, "Nexthop:\n");
1348 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1351 inet_ntop(AF_INET6
, &nh
->address
, nexthop
,
1353 json_next_hop
= json_object_new_object();
1354 json_object_string_add(json_next_hop
, "nextHop",
1356 json_object_string_add(
1357 json_next_hop
, "interfaceName",
1358 ifindex2ifname(nh
->ifindex
, vrf_id
));
1359 json_object_array_add(json_array_next_hops
,
1362 vty_out(vty
, " %pI6 %.*s\n", &nh
->address
, IFNAMSIZ
,
1363 ifindex2ifname(nh
->ifindex
, vrf_id
));
1366 json_object_object_add(json_route
, "nextHops",
1367 json_array_next_hops
);
1368 json_object_object_add(json_routes
, destination
, json_route
);
1373 static void ospf6_route_show_table_summary(struct vty
*vty
,
1374 struct ospf6_route_table
*table
,
1375 json_object
*json
, bool use_json
)
1377 struct ospf6_route
*route
, *prev
= NULL
;
1378 int i
, pathtype
[OSPF6_PATH_TYPE_MAX
];
1379 unsigned int number
= 0;
1380 int nh_count
= 0, ecmp
= 0;
1381 int alternative
= 0, destination
= 0;
1384 for (i
= 0; i
< OSPF6_PATH_TYPE_MAX
; i
++)
1387 for (route
= ospf6_route_head(table
); route
;
1388 route
= ospf6_route_next(route
)) {
1389 if (prev
== NULL
|| !ospf6_route_is_same(prev
, route
))
1393 nh_count
= ospf6_num_nexthops(route
->nh_list
);
1396 pathtype
[route
->path
.type
]++;
1402 assert(number
== table
->count
);
1404 json_object_int_add(json
, "numberOfOspfv3Routes", number
);
1405 json_object_int_add(json
, "numberOfDestination", destination
);
1406 json_object_int_add(json
, "numberOfAlternativeRoutes",
1408 json_object_int_add(json
, "numberOfEcmp", ecmp
);
1410 vty_out(vty
, "Number of OSPFv3 routes: %d\n", number
);
1411 vty_out(vty
, "Number of Destination: %d\n", destination
);
1412 vty_out(vty
, "Number of Alternative routes: %d\n", alternative
);
1413 vty_out(vty
, "Number of Equal Cost Multi Path: %d\n", ecmp
);
1415 for (i
= OSPF6_PATH_TYPE_INTRA
; i
<= OSPF6_PATH_TYPE_EXTERNAL2
; i
++) {
1417 snprintf(path_str
, sizeof(path_str
), "numberOf%sRoutes",
1418 OSPF6_PATH_TYPE_JSON(i
));
1419 json_object_int_add(json
, path_str
, pathtype
[i
]);
1421 vty_out(vty
, "Number of %s routes: %d\n",
1422 OSPF6_PATH_TYPE_NAME(i
), pathtype
[i
]);
1426 static void ospf6_route_show_table_prefix(struct vty
*vty
,
1427 struct prefix
*prefix
,
1428 struct ospf6_route_table
*table
,
1429 json_object
*json
, bool use_json
)
1431 struct ospf6_route
*route
;
1432 json_object
*json_routes
= NULL
;
1434 route
= ospf6_route_lookup(prefix
, table
);
1439 json_routes
= json_object_new_object();
1440 ospf6_route_lock(route
);
1441 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1442 /* Specifying a prefix will always display details */
1443 ospf6_route_show_detail(vty
, route
, json_routes
, use_json
);
1444 route
= ospf6_route_next(route
);
1448 json_object_object_add(json
, "routes", json_routes
);
1450 ospf6_route_unlock(route
);
1453 static void ospf6_route_show_table_address(struct vty
*vty
,
1454 struct prefix
*prefix
,
1455 struct ospf6_route_table
*table
,
1456 json_object
*json
, bool use_json
)
1458 struct ospf6_route
*route
;
1459 json_object
*json_routes
= NULL
;
1461 route
= ospf6_route_lookup_bestmatch(prefix
, table
);
1466 json_routes
= json_object_new_object();
1467 prefix
= &route
->prefix
;
1468 ospf6_route_lock(route
);
1469 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1470 /* Specifying a prefix will always display details */
1471 ospf6_route_show_detail(vty
, route
, json_routes
, use_json
);
1472 route
= ospf6_route_next(route
);
1475 json_object_object_add(json
, "routes", json_routes
);
1477 ospf6_route_unlock(route
);
1480 static void ospf6_route_show_table_match(struct vty
*vty
, int detail
,
1481 struct prefix
*prefix
,
1482 struct ospf6_route_table
*table
,
1483 json_object
*json
, bool use_json
)
1485 struct ospf6_route
*route
;
1486 json_object
*json_routes
= NULL
;
1488 assert(prefix
->family
);
1490 route
= ospf6_route_match_head(prefix
, table
);
1492 json_routes
= json_object_new_object();
1495 ospf6_route_show_detail(vty
, route
, json_routes
,
1498 ospf6_route_show(vty
, route
, json_routes
, use_json
);
1499 route
= ospf6_route_match_next(prefix
, route
);
1502 json_object_object_add(json
, "routes", json_routes
);
1505 static void ospf6_route_show_table_type(struct vty
*vty
, int detail
,
1507 struct ospf6_route_table
*table
,
1508 json_object
*json
, bool use_json
)
1510 struct ospf6_route
*route
;
1511 json_object
*json_routes
= NULL
;
1513 route
= ospf6_route_head(table
);
1515 json_routes
= json_object_new_object();
1517 if (route
->path
.type
== type
) {
1519 ospf6_route_show_detail(vty
, route
, json_routes
,
1522 ospf6_route_show(vty
, route
, json_routes
,
1525 route
= ospf6_route_next(route
);
1528 json_object_object_add(json
, "routes", json_routes
);
1531 static void ospf6_route_show_table(struct vty
*vty
, int detail
,
1532 struct ospf6_route_table
*table
,
1533 json_object
*json
, bool use_json
)
1535 struct ospf6_route
*route
;
1536 json_object
*json_routes
= NULL
;
1538 route
= ospf6_route_head(table
);
1540 json_routes
= json_object_new_object();
1543 ospf6_route_show_detail(vty
, route
, json_routes
,
1546 ospf6_route_show(vty
, route
, json_routes
, use_json
);
1547 route
= ospf6_route_next(route
);
1550 json_object_object_add(json
, "routes", json_routes
);
1553 int ospf6_route_table_show(struct vty
*vty
, int argc_start
, int argc
,
1554 struct cmd_token
**argv
,
1555 struct ospf6_route_table
*table
, bool use_json
)
1563 struct prefix prefix
;
1565 int arg_end
= use_json
? (argc
- 1) : argc
;
1566 json_object
*json
= NULL
;
1568 memset(&prefix
, 0, sizeof(prefix
));
1571 json
= json_object_new_object();
1573 for (i
= argc_start
; i
< arg_end
; i
++) {
1574 if (strmatch(argv
[i
]->text
, "summary")) {
1579 if (strmatch(argv
[i
]->text
, "intra-area")) {
1580 type
= OSPF6_PATH_TYPE_INTRA
;
1584 if (strmatch(argv
[i
]->text
, "inter-area")) {
1585 type
= OSPF6_PATH_TYPE_INTER
;
1589 if (strmatch(argv
[i
]->text
, "external-1")) {
1590 type
= OSPF6_PATH_TYPE_EXTERNAL1
;
1594 if (strmatch(argv
[i
]->text
, "external-2")) {
1595 type
= OSPF6_PATH_TYPE_EXTERNAL2
;
1599 if (strmatch(argv
[i
]->text
, "detail")) {
1604 if (strmatch(argv
[i
]->text
, "match")) {
1609 ret
= str2prefix(argv
[i
]->arg
, &prefix
);
1610 if (ret
== 1 && prefix
.family
== AF_INET6
) {
1612 if (strchr(argv
[i
]->arg
, '/'))
1617 json_object_string_add(json
, "malformedArgument",
1620 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1625 /* Give summary of this route table */
1627 ospf6_route_show_table_summary(vty
, table
, json
, use_json
);
1629 vty_json(vty
, json
);
1633 /* Give exact prefix-match route */
1634 if (isprefix
&& !match
) {
1635 /* If exact address, give best matching route */
1637 ospf6_route_show_table_address(vty
, &prefix
, table
,
1640 ospf6_route_show_table_prefix(vty
, &prefix
, table
, json
,
1644 vty_json(vty
, json
);
1649 ospf6_route_show_table_match(vty
, detail
, &prefix
, table
, json
,
1652 ospf6_route_show_table_type(vty
, detail
, type
, table
, json
,
1655 ospf6_route_show_table(vty
, detail
, table
, json
, use_json
);
1658 vty_json(vty
, json
);
1662 static void ospf6_linkstate_show_header(struct vty
*vty
)
1664 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1665 "Net-ID", "Rtr-Bits", "Options", "Cost");
1668 static void ospf6_linkstate_show(struct vty
*vty
, struct ospf6_route
*route
)
1670 uint32_t router
, id
;
1671 char routername
[16], idname
[16], rbits
[16], options
[32];
1673 router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1674 inet_ntop(AF_INET
, &router
, routername
, sizeof(routername
));
1675 id
= ospf6_linkstate_prefix_id(&route
->prefix
);
1676 inet_ntop(AF_INET
, &id
, idname
, sizeof(idname
));
1678 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1680 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1683 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1684 routername
, idname
, rbits
, options
,
1685 (unsigned long)route
->path
.cost
);
1687 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1688 routername
, idname
, rbits
, options
,
1689 (unsigned long)route
->path
.cost
);
1693 static void ospf6_linkstate_show_table_exact(struct vty
*vty
,
1694 struct prefix
*prefix
,
1695 struct ospf6_route_table
*table
)
1697 struct ospf6_route
*route
;
1699 route
= ospf6_route_lookup(prefix
, table
);
1703 ospf6_route_lock(route
);
1704 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1705 /* Specifying a prefix will always display details */
1706 ospf6_route_show_detail(vty
, route
, NULL
, false);
1707 route
= ospf6_route_next(route
);
1710 ospf6_route_unlock(route
);
1713 static void ospf6_linkstate_show_table(struct vty
*vty
, int detail
,
1714 struct ospf6_route_table
*table
)
1716 struct ospf6_route
*route
;
1719 ospf6_linkstate_show_header(vty
);
1721 route
= ospf6_route_head(table
);
1724 ospf6_route_show_detail(vty
, route
, NULL
, false);
1726 ospf6_linkstate_show(vty
, route
);
1727 route
= ospf6_route_next(route
);
1731 int ospf6_linkstate_table_show(struct vty
*vty
, int idx_ipv4
, int argc
,
1732 struct cmd_token
**argv
,
1733 struct ospf6_route_table
*table
)
1739 struct prefix router
, id
, prefix
;
1741 memset(&router
, 0, sizeof(router
));
1742 memset(&id
, 0, sizeof(id
));
1743 memset(&prefix
, 0, sizeof(prefix
));
1745 for (i
= idx_ipv4
; i
< argc
; i
++) {
1746 if (strmatch(argv
[i
]->text
, "detail")) {
1752 ret
= str2prefix(argv
[i
]->arg
, &router
);
1753 if (ret
== 1 && router
.family
== AF_INET
) {
1757 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1762 ret
= str2prefix(argv
[i
]->arg
, &id
);
1763 if (ret
== 1 && id
.family
== AF_INET
) {
1767 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1771 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1776 ospf6_linkstate_prefix(router
.u
.prefix4
.s_addr
,
1777 id
.u
.prefix4
.s_addr
, &prefix
);
1780 ospf6_linkstate_show_table_exact(vty
, &prefix
, table
);
1782 ospf6_linkstate_show_table(vty
, detail
, table
);
1788 void ospf6_brouter_show_header(struct vty
*vty
)
1790 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1791 "Options", "Path-Type", "Area");
1794 void ospf6_brouter_show(struct vty
*vty
, struct ospf6_route
*route
)
1796 uint32_t adv_router
;
1797 char adv
[16], rbits
[16], options
[32], area
[16];
1799 adv_router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1800 inet_ntop(AF_INET
, &adv_router
, adv
, sizeof(adv
));
1801 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1803 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1804 inet_ntop(AF_INET
, &route
->path
.area_id
, area
, sizeof(area
));
1806 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1807 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1808 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", adv
, rbits
, options
,
1809 OSPF6_PATH_TYPE_NAME(route
->path
.type
), area
);
1812 DEFPY(debug_ospf6_route
,
1813 debug_ospf6_route_cmd
,
1814 "[no$no] debug ospf6 route <all|table|intra-area|inter-area|memory>",
1819 "Debug for all types of route calculation\n"
1820 "Debug route table calculation\n"
1821 "Debug intra-area route calculation\n"
1822 "Debug inter-area route calculation\n"
1823 "Debug route memory use\n")
1826 unsigned char level
= 0;
1828 idx_type
= ((no
) ? 4 : 3);
1830 if (!strcmp(argv
[idx_type
]->text
, "all"))
1831 level
= OSPF6_DEBUG_ROUTE_ALL
;
1832 else if (!strcmp(argv
[idx_type
]->text
, "table"))
1833 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1834 else if (!strcmp(argv
[idx_type
]->text
, "intra-area"))
1835 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1836 else if (!strcmp(argv
[idx_type
]->text
, "inter-area"))
1837 level
= OSPF6_DEBUG_ROUTE_INTER
;
1838 else if (!strcmp(argv
[idx_type
]->text
, "memory"))
1839 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1842 OSPF6_DEBUG_ROUTE_OFF(level
);
1844 OSPF6_DEBUG_ROUTE_ON(level
);
1848 int config_write_ospf6_debug_route(struct vty
*vty
)
1850 if (IS_OSPF6_DEBUG_ROUTE(ALL
) == OSPF6_DEBUG_ROUTE_ALL
) {
1851 vty_out(vty
, "debug ospf6 route all\n");
1854 if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
1855 vty_out(vty
, "debug ospf6 route table\n");
1856 if (IS_OSPF6_DEBUG_ROUTE(INTRA
))
1857 vty_out(vty
, "debug ospf6 route intra-area\n");
1858 if (IS_OSPF6_DEBUG_ROUTE(INTER
))
1859 vty_out(vty
, "debug ospf6 route inter-area\n");
1860 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
1861 vty_out(vty
, "debug ospf6 route memory\n");
1866 void install_element_ospf6_debug_route(void)
1868 install_element(ENABLE_NODE
, &debug_ospf6_route_cmd
);
1869 install_element(CONFIG_NODE
, &debug_ospf6_route_cmd
);