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 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_ROUTE
, "OSPF6 route");
42 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_ROUTE_TABLE
, "OSPF6 route table");
43 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_NEXTHOP
, "OSPF6 nexthop");
44 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_PATH
, "OSPF6 Path");
46 unsigned char conf_debug_ospf6_route
= 0;
48 static char *ospf6_route_table_name(struct ospf6_route_table
*table
)
51 switch (table
->scope_type
) {
52 case OSPF6_SCOPE_TYPE_GLOBAL
: {
53 switch (table
->table_type
) {
54 case OSPF6_TABLE_TYPE_ROUTES
:
55 snprintf(name
, sizeof(name
), "global route table");
57 case OSPF6_TABLE_TYPE_BORDER_ROUTERS
:
58 snprintf(name
, sizeof(name
), "global brouter table");
60 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES
:
61 snprintf(name
, sizeof(name
), "global external table");
64 snprintf(name
, sizeof(name
), "global unknown table");
69 case OSPF6_SCOPE_TYPE_AREA
: {
70 struct ospf6_area
*oa
= (struct ospf6_area
*)table
->scope
;
71 switch (table
->table_type
) {
72 case OSPF6_TABLE_TYPE_SPF_RESULTS
:
73 snprintf(name
, sizeof(name
), "area %s spf table",
76 case OSPF6_TABLE_TYPE_ROUTES
:
77 snprintf(name
, sizeof(name
), "area %s route table",
80 case OSPF6_TABLE_TYPE_PREFIX_RANGES
:
81 snprintf(name
, sizeof(name
), "area %s range table",
84 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES
:
85 snprintf(name
, sizeof(name
),
86 "area %s summary prefix table", oa
->name
);
88 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS
:
89 snprintf(name
, sizeof(name
),
90 "area %s summary router table", oa
->name
);
93 snprintf(name
, sizeof(name
), "area %s unknown table",
99 case OSPF6_SCOPE_TYPE_INTERFACE
: {
100 struct ospf6_interface
*oi
=
101 (struct ospf6_interface
*)table
->scope
;
102 switch (table
->table_type
) {
103 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES
:
104 snprintf(name
, sizeof(name
),
105 "interface %s connected table",
106 oi
->interface
->name
);
109 snprintf(name
, sizeof(name
),
110 "interface %s unknown table",
111 oi
->interface
->name
);
117 switch (table
->table_type
) {
118 case OSPF6_TABLE_TYPE_SPF_RESULTS
:
119 snprintf(name
, sizeof(name
), "temporary spf table");
122 snprintf(name
, sizeof(name
), "temporary unknown table");
130 void ospf6_linkstate_prefix(uint32_t adv_router
, uint32_t id
,
131 struct prefix
*prefix
)
133 memset(prefix
, 0, sizeof(struct prefix
));
134 prefix
->family
= AF_INET6
;
135 prefix
->prefixlen
= 64;
136 memcpy(&prefix
->u
.prefix6
.s6_addr
[0], &adv_router
, 4);
137 memcpy(&prefix
->u
.prefix6
.s6_addr
[4], &id
, 4);
140 void ospf6_linkstate_prefix2str(struct prefix
*prefix
, char *buf
, int size
)
142 uint32_t adv_router
, id
;
143 char adv_router_str
[16], id_str
[16];
144 memcpy(&adv_router
, &prefix
->u
.prefix6
.s6_addr
[0], 4);
145 memcpy(&id
, &prefix
->u
.prefix6
.s6_addr
[4], 4);
146 inet_ntop(AF_INET
, &adv_router
, adv_router_str
, sizeof(adv_router_str
));
147 inet_ntop(AF_INET
, &id
, id_str
, sizeof(id_str
));
149 snprintf(buf
, size
, "%s Net-ID: %s", adv_router_str
, id_str
);
151 snprintf(buf
, size
, "%s", adv_router_str
);
154 /* Global strings for logging */
155 const char *const ospf6_dest_type_str
[OSPF6_DEST_TYPE_MAX
] = {
156 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
159 const char *const ospf6_dest_type_substr
[OSPF6_DEST_TYPE_MAX
] = {
160 "?", "R", "N", "D", "L", "A",
163 const char *const ospf6_path_type_str
[OSPF6_PATH_TYPE_MAX
] = {
164 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
167 const char *const ospf6_path_type_substr
[OSPF6_PATH_TYPE_MAX
] = {
168 "??", "IA", "IE", "E1", "E2",
171 const char *ospf6_path_type_json
[OSPF6_PATH_TYPE_MAX
] = {
172 "UnknownRoute", "IntraArea", "InterArea", "External1", "External2",
176 struct ospf6_nexthop
*ospf6_nexthop_create(void)
178 struct ospf6_nexthop
*nh
;
180 nh
= XCALLOC(MTYPE_OSPF6_NEXTHOP
, sizeof(struct ospf6_nexthop
));
184 void ospf6_nexthop_delete(struct ospf6_nexthop
*nh
)
186 XFREE(MTYPE_OSPF6_NEXTHOP
, nh
);
189 void ospf6_clear_nexthops(struct list
*nh_list
)
191 struct listnode
*node
;
192 struct ospf6_nexthop
*nh
;
195 for (ALL_LIST_ELEMENTS_RO(nh_list
, node
, nh
))
196 ospf6_nexthop_clear(nh
);
200 static struct ospf6_nexthop
*
201 ospf6_route_find_nexthop(struct list
*nh_list
, struct ospf6_nexthop
*nh_match
)
203 struct listnode
*node
;
204 struct ospf6_nexthop
*nh
;
206 if (nh_list
&& nh_match
) {
207 for (ALL_LIST_ELEMENTS_RO(nh_list
, node
, nh
)) {
208 if (ospf6_nexthop_is_same(nh
, nh_match
))
216 void ospf6_copy_nexthops(struct list
*dst
, struct list
*src
)
218 struct ospf6_nexthop
*nh_new
, *nh
;
219 struct listnode
*node
;
222 for (ALL_LIST_ELEMENTS_RO(src
, node
, nh
)) {
223 if (ospf6_nexthop_is_set(nh
)) {
224 nh_new
= ospf6_nexthop_create();
225 ospf6_nexthop_copy(nh_new
, nh
);
226 listnode_add_sort(dst
, nh_new
);
232 void ospf6_merge_nexthops(struct list
*dst
, struct list
*src
)
234 struct listnode
*node
;
235 struct ospf6_nexthop
*nh
, *nh_new
;
238 for (ALL_LIST_ELEMENTS_RO(src
, node
, nh
)) {
239 if (!ospf6_route_find_nexthop(dst
, nh
)) {
240 nh_new
= ospf6_nexthop_create();
241 ospf6_nexthop_copy(nh_new
, nh
);
242 listnode_add_sort(dst
, nh_new
);
248 int ospf6_route_cmp_nexthops(struct ospf6_route
*a
, struct ospf6_route
*b
)
250 struct listnode
*anode
, *bnode
;
251 struct ospf6_nexthop
*anh
, *bnh
;
252 bool identical
= false;
255 if (listcount(a
->nh_list
) == listcount(b
->nh_list
)) {
256 for (ALL_LIST_ELEMENTS_RO(a
->nh_list
, anode
, anh
)) {
258 for (ALL_LIST_ELEMENTS_RO(b
->nh_list
, bnode
,
260 if (ospf6_nexthop_is_same(anh
, bnh
))
263 /* Currnet List A element not found List B
264 * Non-Identical lists return */
265 if (identical
== false)
272 /* One of the routes doesn't exist ? */
276 int ospf6_num_nexthops(struct list
*nh_list
)
278 return (listcount(nh_list
));
281 void ospf6_add_nexthop(struct list
*nh_list
, int ifindex
, struct in6_addr
*addr
)
283 struct ospf6_nexthop
*nh
;
284 struct ospf6_nexthop nh_match
;
289 nh_match
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
291 nh_match
.type
= NEXTHOP_TYPE_IPV6
;
293 memcpy(&nh_match
.address
, addr
,
294 sizeof(struct in6_addr
));
296 nh_match
.type
= NEXTHOP_TYPE_IFINDEX
;
298 memset(&nh_match
.address
, 0, sizeof(struct in6_addr
));
301 nh_match
.ifindex
= ifindex
;
303 if (!ospf6_route_find_nexthop(nh_list
, &nh_match
)) {
304 nh
= ospf6_nexthop_create();
305 ospf6_nexthop_copy(nh
, &nh_match
);
306 listnode_add(nh_list
, nh
);
311 void ospf6_add_route_nexthop_blackhole(struct ospf6_route
*route
)
313 struct ospf6_nexthop
*nh
;
314 struct ospf6_nexthop nh_match
= {};
316 /* List not allocated. */
317 if (route
->nh_list
== NULL
)
320 /* Entry already exists. */
321 nh_match
.type
= NEXTHOP_TYPE_BLACKHOLE
;
322 if (ospf6_route_find_nexthop(route
->nh_list
, &nh_match
))
325 nh
= ospf6_nexthop_create();
326 ospf6_nexthop_copy(nh
, &nh_match
);
327 listnode_add(route
->nh_list
, nh
);
330 void ospf6_route_zebra_copy_nexthops(struct ospf6_route
*route
,
331 struct zapi_nexthop nexthops
[],
332 int entries
, vrf_id_t vrf_id
)
334 struct ospf6_nexthop
*nh
;
335 struct listnode
*node
;
340 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
341 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
342 zlog_debug(" nexthop: %s %pI6%%%.*s(%d)",
343 nexthop_type_to_str(nh
->type
),
344 &nh
->address
, IFNAMSIZ
,
345 ifindex2ifname(nh
->ifindex
, vrf_id
),
352 nexthops
[i
].vrf_id
= vrf_id
;
353 nexthops
[i
].type
= nh
->type
;
356 case NEXTHOP_TYPE_BLACKHOLE
:
360 case NEXTHOP_TYPE_IFINDEX
:
361 nexthops
[i
].ifindex
= nh
->ifindex
;
364 case NEXTHOP_TYPE_IPV4_IFINDEX
:
365 case NEXTHOP_TYPE_IPV4
:
367 * OSPFv3 with IPv4 routes is not supported
368 * yet. Skip this next hop.
370 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
371 zlog_debug(" Skipping IPv4 next hop");
374 case NEXTHOP_TYPE_IPV6_IFINDEX
:
375 nexthops
[i
].ifindex
= nh
->ifindex
;
377 case NEXTHOP_TYPE_IPV6
:
378 nexthops
[i
].gate
.ipv6
= nh
->address
;
386 int ospf6_route_get_first_nh_index(struct ospf6_route
*route
)
388 struct ospf6_nexthop
*nh
;
391 nh
= listnode_head(route
->nh_list
);
399 int ospf6_nexthop_cmp(struct ospf6_nexthop
*a
, struct ospf6_nexthop
*b
)
401 if (a
->ifindex
< b
->ifindex
)
403 else if (a
->ifindex
> b
->ifindex
)
406 return memcmp(&a
->address
, &b
->address
,
407 sizeof(struct in6_addr
));
412 static int ospf6_path_cmp(struct ospf6_path
*a
, struct ospf6_path
*b
)
414 if (a
->origin
.adv_router
< b
->origin
.adv_router
)
416 else if (a
->origin
.adv_router
> b
->origin
.adv_router
)
422 void ospf6_path_free(struct ospf6_path
*op
)
425 list_delete(&op
->nh_list
);
426 XFREE(MTYPE_OSPF6_PATH
, op
);
429 struct ospf6_path
*ospf6_path_dup(struct ospf6_path
*path
)
431 struct ospf6_path
*new;
433 new = XCALLOC(MTYPE_OSPF6_PATH
, sizeof(struct ospf6_path
));
434 memcpy(new, path
, sizeof(struct ospf6_path
));
435 new->nh_list
= list_new();
436 new->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
437 new->nh_list
->del
= (void (*)(void *))ospf6_nexthop_delete
;
442 void ospf6_copy_paths(struct list
*dst
, struct list
*src
)
444 struct ospf6_path
*path_new
, *path
;
445 struct listnode
*node
;
448 for (ALL_LIST_ELEMENTS_RO(src
, node
, path
)) {
449 path_new
= ospf6_path_dup(path
);
450 ospf6_copy_nexthops(path_new
->nh_list
, path
->nh_list
);
451 listnode_add_sort(dst
, path_new
);
456 struct ospf6_route
*ospf6_route_create(struct ospf6
*ospf6
)
458 struct ospf6_route
*route
;
460 route
= XCALLOC(MTYPE_OSPF6_ROUTE
, sizeof(struct ospf6_route
));
461 route
->nh_list
= list_new();
462 route
->nh_list
->cmp
= (int (*)(void *, void *))ospf6_nexthop_cmp
;
463 route
->nh_list
->del
= (void (*)(void *))ospf6_nexthop_delete
;
464 route
->paths
= list_new();
465 route
->paths
->cmp
= (int (*)(void *, void *))ospf6_path_cmp
;
466 route
->paths
->del
= (void (*)(void *))ospf6_path_free
;
467 route
->ospf6
= ospf6
;
472 void ospf6_route_delete(struct ospf6_route
*route
)
476 list_delete(&route
->nh_list
);
478 list_delete(&route
->paths
);
479 XFREE(MTYPE_OSPF6_ROUTE
, route
);
483 struct ospf6_route
*ospf6_route_copy(struct ospf6_route
*route
)
485 struct ospf6_route
*new;
487 new = ospf6_route_create(route
->ospf6
);
488 new->type
= route
->type
;
489 memcpy(&new->prefix
, &route
->prefix
, sizeof(struct prefix
));
490 new->installed
= route
->installed
;
491 new->changed
= route
->changed
;
492 new->flag
= route
->flag
;
493 new->route_option
= route
->route_option
;
494 new->linkstate_id
= route
->linkstate_id
;
495 new->path
= route
->path
;
496 ospf6_copy_nexthops(new->nh_list
, route
->nh_list
);
497 ospf6_copy_paths(new->paths
, route
->paths
);
506 void ospf6_route_lock(struct ospf6_route
*route
)
511 void ospf6_route_unlock(struct ospf6_route
*route
)
513 assert(route
->lock
> 0);
515 if (route
->lock
== 0) {
516 /* Can't detach from the table until here
517 because ospf6_route_next () will use
518 the 'route->table' pointer for logging */
520 ospf6_route_delete(route
);
524 /* Route compare function. If ra is more preferred, it returns
525 less than 0. If rb is more preferred returns greater than 0.
526 Otherwise (neither one is preferred), returns 0 */
527 int ospf6_route_cmp(struct ospf6_route
*ra
, struct ospf6_route
*rb
)
529 assert(ospf6_route_is_same(ra
, rb
));
530 assert(OSPF6_PATH_TYPE_NONE
< ra
->path
.type
531 && ra
->path
.type
< OSPF6_PATH_TYPE_MAX
);
532 assert(OSPF6_PATH_TYPE_NONE
< rb
->path
.type
533 && rb
->path
.type
< OSPF6_PATH_TYPE_MAX
);
535 if (ra
->type
!= rb
->type
)
536 return (ra
->type
- rb
->type
);
538 if (ra
->path
.type
!= rb
->path
.type
)
539 return (ra
->path
.type
- rb
->path
.type
);
541 if (ra
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
) {
542 if (ra
->path
.u
.cost_e2
!= rb
->path
.u
.cost_e2
)
543 return (ra
->path
.u
.cost_e2
- rb
->path
.u
.cost_e2
);
545 return (ra
->path
.cost
- rb
->path
.cost
);
547 if (ra
->path
.cost
!= rb
->path
.cost
)
548 return (ra
->path
.cost
- rb
->path
.cost
);
551 if (ra
->path
.area_id
!= rb
->path
.area_id
)
552 return (ntohl(ra
->path
.area_id
) - ntohl(rb
->path
.area_id
));
557 struct ospf6_route
*ospf6_route_lookup(struct prefix
*prefix
,
558 struct ospf6_route_table
*table
)
560 struct route_node
*node
;
561 struct ospf6_route
*route
;
563 node
= route_node_lookup(table
->table
, prefix
);
567 route
= (struct ospf6_route
*)node
->info
;
568 route_unlock_node(node
); /* to free the lookup lock */
573 ospf6_route_lookup_identical(struct ospf6_route
*route
,
574 struct ospf6_route_table
*table
)
576 struct ospf6_route
*target
;
578 for (target
= ospf6_route_lookup(&route
->prefix
, table
); target
;
579 target
= target
->next
) {
580 if (target
->type
== route
->type
581 && (memcmp(&target
->prefix
, &route
->prefix
,
582 sizeof(struct prefix
))
584 && target
->path
.type
== route
->path
.type
585 && target
->path
.cost
== route
->path
.cost
586 && target
->path
.u
.cost_e2
== route
->path
.u
.cost_e2
587 && ospf6_route_cmp_nexthops(target
, route
) == 0)
594 ospf6_route_lookup_bestmatch(struct prefix
*prefix
,
595 struct ospf6_route_table
*table
)
597 struct route_node
*node
;
598 struct ospf6_route
*route
;
600 node
= route_node_match(table
->table
, prefix
);
603 route_unlock_node(node
);
605 route
= (struct ospf6_route
*)node
->info
;
610 static void route_table_assert(struct ospf6_route_table
*table
)
612 struct ospf6_route
*prev
, *r
, *next
;
613 unsigned int link_error
= 0, num
= 0;
615 r
= ospf6_route_head(table
);
621 next
= ospf6_route_next(r
);
630 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
633 if (link_error
== 0 && num
== table
->count
)
636 flog_err(EC_LIB_DEVELOPMENT
, "PANIC !!");
637 flog_err(EC_LIB_DEVELOPMENT
,
638 "Something has gone wrong with ospf6_route_table[%p]", table
);
639 zlog_debug("table count = %d, real number = %d", table
->count
, num
);
640 zlog_debug("DUMP START");
641 for (r
= ospf6_route_head(table
); r
; r
= ospf6_route_next(r
))
642 zlog_info("%p<-[%p]->%p : %pFX", r
->prev
, r
, r
->next
,
644 zlog_debug("DUMP END");
646 assert(link_error
== 0 && num
== table
->count
);
648 #define ospf6_route_table_assert(t) (route_table_assert (t))
650 #define ospf6_route_table_assert(t) ((void) 0)
653 struct ospf6_route
*ospf6_route_add(struct ospf6_route
*route
,
654 struct ospf6_route_table
*table
)
656 struct route_node
*node
, *nextnode
, *prevnode
;
657 struct ospf6_route
*current
= NULL
;
658 struct ospf6_route
*prev
= NULL
, *old
= NULL
, *next
= NULL
;
659 char buf
[PREFIX2STR_BUFFER
];
662 assert(route
->rnode
== NULL
);
663 assert(route
->lock
== 0);
664 assert(route
->next
== NULL
);
665 assert(route
->prev
== NULL
);
667 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
668 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
670 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
672 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
673 zlog_debug("%s %p: route add %p: %s paths %u nh %u",
674 ospf6_route_table_name(table
), (void *)table
,
675 (void *)route
, buf
, listcount(route
->paths
),
676 listcount(route
->nh_list
));
677 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
678 zlog_debug("%s: route add: %s", ospf6_route_table_name(table
),
683 node
= route_node_get(table
->table
, &route
->prefix
);
686 /* find place to insert */
687 for (current
= node
->info
; current
; current
= current
->next
) {
688 if (!ospf6_route_is_same(current
, route
))
690 else if (current
->type
!= route
->type
)
692 else if (ospf6_route_is_same_origin(current
, route
))
694 else if (ospf6_route_cmp(current
, route
) > 0)
704 /* if route does not actually change, return unchanged */
705 if (ospf6_route_is_identical(old
, route
)) {
706 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
708 "%s %p: route add %p: needless update of %p old cost %u",
709 ospf6_route_table_name(table
),
710 (void *)table
, (void *)route
,
711 (void *)old
, old
->path
.cost
);
712 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
713 zlog_debug("%s: route add: needless update",
714 ospf6_route_table_name(table
));
716 ospf6_route_delete(route
);
717 SET_FLAG(old
->flag
, OSPF6_ROUTE_ADD
);
718 ospf6_route_table_assert(table
);
720 /* to free the lookup lock */
721 route_unlock_node(node
);
725 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
727 "%s %p: route add %p cost %u paths %u nh %u: update of %p cost %u paths %u nh %u",
728 ospf6_route_table_name(table
), (void *)table
,
729 (void *)route
, route
->path
.cost
,
730 listcount(route
->paths
),
731 listcount(route
->nh_list
), (void *)old
,
732 old
->path
.cost
, listcount(old
->paths
),
733 listcount(old
->nh_list
));
734 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
735 zlog_debug("%s: route add: update",
736 ospf6_route_table_name(table
));
738 /* replace old one if exists */
739 if (node
->info
== old
) {
741 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
742 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
743 zlog_debug("%s: replace old route %s",
748 old
->prev
->next
= route
;
749 route
->prev
= old
->prev
;
751 old
->next
->prev
= route
;
752 route
->next
= old
->next
;
754 route
->installed
= old
->installed
;
755 route
->changed
= now
;
756 assert(route
->table
== NULL
);
757 route
->table
= table
;
759 ospf6_route_unlock(old
); /* will be deleted later */
760 ospf6_route_lock(route
);
762 SET_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
);
763 ospf6_route_table_assert(table
);
766 (*table
->hook_add
)(route
);
771 /* insert if previous or next node found */
773 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
775 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
776 ospf6_route_table_name(table
), (void *)table
,
777 (void *)route
, route
->path
.cost
, (void *)prev
,
778 (void *)next
, route_node_get_lock_count(node
));
779 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
780 zlog_debug("%s: route add cost %u: another path found",
781 ospf6_route_table_name(table
),
796 if (node
->info
== next
) {
797 assert(next
&& next
->rnode
== node
);
799 UNSET_FLAG(next
->flag
, OSPF6_ROUTE_BEST
);
800 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
801 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
803 "%s %p: route add %p cost %u: replacing previous best: %p cost %u",
804 ospf6_route_table_name(table
),
805 (void *)table
, (void *)route
,
806 route
->path
.cost
, (void *)next
,
810 route
->installed
= now
;
811 route
->changed
= now
;
812 assert(route
->table
== NULL
);
813 route
->table
= table
;
815 ospf6_route_lock(route
);
817 ospf6_route_table_assert(table
);
819 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
821 (*table
->hook_add
)(route
);
826 /* Else, this is the brand new route regarding to the prefix */
827 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
828 zlog_debug("%s %p: route add %p %s cost %u: brand new route",
829 ospf6_route_table_name(table
), (void *)table
,
830 (void *)route
, buf
, route
->path
.cost
);
831 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
832 zlog_debug("%s: route add: brand new route",
833 ospf6_route_table_name(table
));
835 assert(node
->info
== NULL
);
837 SET_FLAG(route
->flag
, OSPF6_ROUTE_BEST
);
838 ospf6_route_lock(route
);
839 route
->installed
= now
;
840 route
->changed
= now
;
841 assert(route
->table
== NULL
);
842 route
->table
= table
;
844 /* lookup real existing next route */
846 route_lock_node(nextnode
);
848 nextnode
= route_next(nextnode
);
849 } while (nextnode
&& nextnode
->info
== NULL
);
852 if (nextnode
== NULL
)
855 route_unlock_node(nextnode
);
857 next
= nextnode
->info
;
862 /* lookup real existing prev route */
864 route_lock_node(prevnode
);
866 prevnode
= route_prev(prevnode
);
867 } while (prevnode
&& prevnode
->info
== NULL
);
870 if (prevnode
== NULL
)
873 route_unlock_node(prevnode
);
875 prev
= prevnode
->info
;
876 while (prev
->next
&& ospf6_route_is_same(prev
, prev
->next
))
883 ospf6_route_table_assert(table
);
885 SET_FLAG(route
->flag
, OSPF6_ROUTE_ADD
);
887 (*table
->hook_add
)(route
);
892 void ospf6_route_remove(struct ospf6_route
*route
,
893 struct ospf6_route_table
*table
)
895 struct route_node
*node
;
896 struct ospf6_route
*current
;
897 char buf
[PREFIX2STR_BUFFER
];
899 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
900 ospf6_linkstate_prefix2str(&route
->prefix
, buf
, sizeof(buf
));
902 prefix2str(&route
->prefix
, buf
, sizeof(buf
));
904 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
905 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
906 ospf6_route_table_name(table
), (void *)table
,
907 (void *)route
, buf
, route
->path
.cost
, route
->lock
);
908 else if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
909 zlog_debug("%s: route remove: %s",
910 ospf6_route_table_name(table
), buf
);
912 node
= route_node_lookup(table
->table
, &route
->prefix
);
915 /* find the route to remove, making sure that the route pointer
916 is from the route table. */
917 current
= node
->info
;
918 while (current
&& current
!= route
)
919 current
= current
->next
;
921 assert(current
== route
);
923 /* adjust doubly linked list */
925 route
->prev
->next
= route
->next
;
927 route
->next
->prev
= route
->prev
;
929 if (node
->info
== route
) {
930 if (route
->next
&& route
->next
->rnode
== node
) {
931 node
->info
= route
->next
;
932 SET_FLAG(route
->next
->flag
, OSPF6_ROUTE_BEST
);
933 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
934 zlog_debug("%s: remove route %s", __func__
,
939 route_unlock_node(node
); /* to free the original lock */
943 route_unlock_node(node
); /* to free the lookup lock */
945 ospf6_route_table_assert(table
);
947 SET_FLAG(route
->flag
, OSPF6_ROUTE_WAS_REMOVED
);
949 /* Note hook_remove may call ospf6_route_remove */
950 if (table
->hook_remove
)
951 (*table
->hook_remove
)(route
);
953 ospf6_route_unlock(route
);
956 struct ospf6_route
*ospf6_route_head(struct ospf6_route_table
*table
)
958 struct route_node
*node
;
959 struct ospf6_route
*route
;
961 node
= route_top(table
->table
);
965 /* skip to the real existing entry */
966 while (node
&& node
->info
== NULL
)
967 node
= route_next(node
);
971 route_unlock_node(node
);
974 route
= (struct ospf6_route
*)node
->info
;
975 assert(route
->prev
== NULL
);
976 assert(route
->table
== table
);
977 ospf6_route_lock(route
);
979 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
980 zlog_info("%s %p: route head: %p<-[%p]->%p",
981 ospf6_route_table_name(table
), (void *)table
,
982 (void *)route
->prev
, (void *)route
,
983 (void *)route
->next
);
988 struct ospf6_route
*ospf6_route_next(struct ospf6_route
*route
)
990 struct ospf6_route
*next
= route
->next
;
992 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
993 zlog_info("%s %p: route next: %p<-[%p]->%p , route ref count %u",
994 ospf6_route_table_name(route
->table
),
995 (void *)route
->table
, (void *)route
->prev
,
996 (void *)route
, (void *)route
->next
,
999 ospf6_route_unlock(route
);
1001 ospf6_route_lock(next
);
1006 struct ospf6_route
*ospf6_route_best_next(struct ospf6_route
*route
)
1008 struct route_node
*rnode
;
1009 struct ospf6_route
*next
;
1011 ospf6_route_unlock(route
);
1013 rnode
= route
->rnode
;
1014 route_lock_node(rnode
);
1015 rnode
= route_next(rnode
);
1016 while (rnode
&& rnode
->info
== NULL
)
1017 rnode
= route_next(rnode
);
1020 route_unlock_node(rnode
);
1022 assert(rnode
->info
);
1023 next
= (struct ospf6_route
*)rnode
->info
;
1024 ospf6_route_lock(next
);
1028 struct ospf6_route
*ospf6_route_match_head(struct prefix
*prefix
,
1029 struct ospf6_route_table
*table
)
1031 struct route_node
*node
;
1032 struct ospf6_route
*route
;
1034 /* Walk down tree. */
1035 node
= table
->table
->top
;
1036 while (node
&& node
->p
.prefixlen
< prefix
->prefixlen
1037 && prefix_match(&node
->p
, prefix
))
1038 node
= node
->link
[prefix_bit(&prefix
->u
.prefix
,
1039 node
->p
.prefixlen
)];
1042 route_lock_node(node
);
1043 while (node
&& node
->info
== NULL
)
1044 node
= route_next(node
);
1047 route_unlock_node(node
);
1049 if (!prefix_match(prefix
, &node
->p
))
1053 ospf6_route_lock(route
);
1057 struct ospf6_route
*ospf6_route_match_next(struct prefix
*prefix
,
1058 struct ospf6_route
*route
)
1060 struct ospf6_route
*next
;
1062 next
= ospf6_route_next(route
);
1063 if (next
&& !prefix_match(prefix
, &next
->prefix
)) {
1064 ospf6_route_unlock(next
);
1071 void ospf6_route_remove_all(struct ospf6_route_table
*table
)
1073 struct ospf6_route
*route
;
1074 for (route
= ospf6_route_head(table
); route
;
1075 route
= ospf6_route_next(route
))
1076 ospf6_route_remove(route
, table
);
1079 struct ospf6_route_table
*ospf6_route_table_create(int s
, int t
)
1081 struct ospf6_route_table
*new;
1082 new = XCALLOC(MTYPE_OSPF6_ROUTE_TABLE
,
1083 sizeof(struct ospf6_route_table
));
1084 new->table
= route_table_init();
1085 new->scope_type
= s
;
1086 new->table_type
= t
;
1090 void ospf6_route_table_delete(struct ospf6_route_table
*table
)
1092 ospf6_route_remove_all(table
);
1093 bf_free(table
->idspace
);
1094 route_table_finish(table
->table
);
1095 XFREE(MTYPE_OSPF6_ROUTE_TABLE
, table
);
1100 void ospf6_route_show(struct vty
*vty
, struct ospf6_route
*route
,
1101 json_object
*json_routes
, bool use_json
)
1104 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1106 struct timeval now
, res
;
1107 struct listnode
*node
;
1108 struct ospf6_nexthop
*nh
;
1109 json_object
*json_route
= NULL
;
1110 json_object
*json_array_next_hops
= NULL
;
1111 json_object
*json_next_hop
;
1113 if (om6
->ospf6
== NULL
) {
1114 vty_out(vty
, "OSPFv3 is not running\n");
1119 timersub(&now
, &route
->changed
, &res
);
1120 timerstring(&res
, duration
, sizeof(duration
));
1123 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1124 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1125 sizeof(destination
));
1126 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1127 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1128 destination
, sizeof(destination
));
1130 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1133 json_route
= json_object_new_object();
1134 json_object_boolean_add(json_route
, "isBestRoute",
1135 ospf6_route_is_best(route
));
1136 json_object_string_add(json_route
, "destinationType",
1137 OSPF6_DEST_TYPE_SUBSTR(route
->type
));
1138 json_object_string_add(
1139 json_route
, "pathType",
1140 OSPF6_PATH_TYPE_SUBSTR(route
->path
.type
));
1141 json_object_string_add(json_route
, "duration", duration
);
1146 json_array_next_hops
= json_object_new_array();
1149 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1150 struct interface
*ifp
;
1152 inet_ntop(AF_INET6
, &nh
->address
, nexthop
, sizeof(nexthop
));
1153 ifp
= if_lookup_by_index_all_vrf(nh
->ifindex
);
1155 json_next_hop
= json_object_new_object();
1156 json_object_string_add(json_next_hop
, "nextHop",
1158 json_object_string_add(json_next_hop
, "interfaceName",
1160 json_object_array_add(json_array_next_hops
,
1164 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1165 (ospf6_route_is_best(route
) ? '*'
1167 OSPF6_DEST_TYPE_SUBSTR(route
->type
),
1168 OSPF6_PATH_TYPE_SUBSTR(
1170 destination
, nexthop
, IFNAMSIZ
,
1171 ifp
->name
, duration
);
1174 vty_out(vty
, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1175 ' ', "", "", "", nexthop
, IFNAMSIZ
,
1180 json_object_object_add(json_route
, "nextHops",
1181 json_array_next_hops
);
1182 json_object_object_add(json_routes
, destination
, json_route
);
1186 void ospf6_route_show_detail(struct vty
*vty
, struct ospf6_route
*route
,
1187 json_object
*json_routes
, bool use_json
)
1189 char destination
[PREFIX2STR_BUFFER
], nexthop
[64];
1190 char area_id
[16], id
[16], adv_router
[16], capa
[16], options
[16];
1191 struct timeval now
, res
;
1193 struct listnode
*node
;
1194 struct ospf6_nexthop
*nh
;
1196 json_object
*json_route
= NULL
;
1197 json_object
*json_array_next_hops
= NULL
;
1198 json_object
*json_next_hop
;
1200 if (om6
->ospf6
== NULL
) {
1201 vty_out(vty
, "OSPFv3 is not running\n");
1208 if (route
->type
== OSPF6_DEST_TYPE_LINKSTATE
)
1209 ospf6_linkstate_prefix2str(&route
->prefix
, destination
,
1210 sizeof(destination
));
1211 else if (route
->type
== OSPF6_DEST_TYPE_ROUTER
)
1212 inet_ntop(route
->prefix
.family
, &route
->prefix
.u
.prefix
,
1213 destination
, sizeof(destination
));
1215 prefix2str(&route
->prefix
, destination
, sizeof(destination
));
1218 json_route
= json_object_new_object();
1219 json_object_string_add(json_route
, "destinationType",
1220 OSPF6_DEST_TYPE_NAME(route
->type
));
1222 vty_out(vty
, "Destination: %s\n", destination
);
1223 vty_out(vty
, "Destination type: %s\n",
1224 OSPF6_DEST_TYPE_NAME(route
->type
));
1228 timersub(&now
, &route
->installed
, &res
);
1229 timerstring(&res
, duration
, sizeof(duration
));
1231 json_object_string_add(json_route
, "installedTimeSince",
1234 vty_out(vty
, "Installed Time: %s ago\n", duration
);
1236 timersub(&now
, &route
->changed
, &res
);
1237 timerstring(&res
, duration
, sizeof(duration
));
1239 json_object_string_add(json_route
, "changedTimeSince",
1242 vty_out(vty
, "Changed Time: %s ago\n", duration
);
1244 /* Debugging info */
1246 json_object_int_add(json_route
, "numberOfLock", route
->lock
);
1248 flag
, sizeof(flag
), "%s%s%s%s",
1249 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1250 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1251 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R"
1253 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C"
1255 json_object_string_add(json_route
, "flags", flag
);
1257 vty_out(vty
, "Lock: %d Flags: %s%s%s%s\n", route
->lock
,
1258 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_BEST
) ? "B" : "-"),
1259 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_ADD
) ? "A" : "-"),
1260 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_REMOVE
) ? "R"
1262 (CHECK_FLAG(route
->flag
, OSPF6_ROUTE_CHANGE
) ? "C"
1264 vty_out(vty
, "Memory: prev: %p this: %p next: %p\n",
1265 (void *)route
->prev
, (void *)route
,
1266 (void *)route
->next
);
1272 inet_ntop(AF_INET
, &route
->path
.area_id
, area_id
, sizeof(area_id
));
1274 json_object_string_add(json_route
, "associatedArea", area_id
);
1276 vty_out(vty
, "Associated Area: %s\n", area_id
);
1280 json_object_string_add(json_route
, "pathType",
1281 OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1283 vty_out(vty
, "Path Type: %s\n",
1284 OSPF6_PATH_TYPE_NAME(route
->path
.type
));
1287 inet_ntop(AF_INET
, &route
->path
.origin
.id
, id
, sizeof(id
));
1288 inet_ntop(AF_INET
, &route
->path
.origin
.adv_router
, adv_router
,
1289 sizeof(adv_router
));
1291 json_object_string_add(
1292 json_route
, "lsOriginRoutePathType",
1293 ospf6_lstype_name(route
->path
.origin
.type
));
1294 json_object_string_add(json_route
, "lsId", id
);
1295 json_object_string_add(json_route
, "lsAdvertisingRouter",
1298 vty_out(vty
, "LS Origin: %s Id: %s Adv: %s\n",
1299 ospf6_lstype_name(route
->path
.origin
.type
), id
,
1304 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1306 json_object_string_add(json_route
, "options", options
);
1308 vty_out(vty
, "Options: %s\n", options
);
1311 ospf6_capability_printbuf(route
->path
.router_bits
, capa
, sizeof(capa
));
1313 json_object_string_add(json_route
, "routerBits", capa
);
1315 vty_out(vty
, "Router Bits: %s\n", capa
);
1317 /* Prefix Options */
1319 json_object_string_add(json_route
, "prefixOptions", "xxx");
1321 vty_out(vty
, "Prefix Options: xxx\n");
1325 json_object_int_add(json_route
, "metricType",
1326 route
->path
.metric_type
);
1327 json_object_int_add(json_route
, "metricCost", route
->path
.cost
);
1328 json_object_int_add(json_route
, "metricCostE2",
1329 route
->path
.u
.cost_e2
);
1331 json_object_int_add(json_route
, "pathsCount",
1332 route
->paths
->count
);
1333 json_object_int_add(json_route
, "nextHopCount",
1334 route
->nh_list
->count
);
1336 vty_out(vty
, "Metric Type: %d\n", route
->path
.metric_type
);
1337 vty_out(vty
, "Metric: %d (%d)\n", route
->path
.cost
,
1338 route
->path
.u
.cost_e2
);
1340 vty_out(vty
, "Paths count: %u\n", route
->paths
->count
);
1341 vty_out(vty
, "Nexthop count: %u\n", route
->nh_list
->count
);
1346 json_array_next_hops
= json_object_new_array();
1348 vty_out(vty
, "Nexthop:\n");
1350 for (ALL_LIST_ELEMENTS_RO(route
->nh_list
, node
, nh
)) {
1351 struct interface
*ifp
;
1352 ifp
= if_lookup_by_index_all_vrf(nh
->ifindex
);
1355 inet_ntop(AF_INET6
, &nh
->address
, nexthop
,
1357 json_next_hop
= json_object_new_object();
1358 json_object_string_add(json_next_hop
, "nextHop",
1360 json_object_string_add(json_next_hop
, "interfaceName",
1362 json_object_array_add(json_array_next_hops
,
1365 vty_out(vty
, " %pI6 %.*s\n", &nh
->address
, IFNAMSIZ
,
1369 json_object_object_add(json_route
, "nextHops",
1370 json_array_next_hops
);
1371 json_object_object_add(json_routes
, destination
, json_route
);
1376 static void ospf6_route_show_table_summary(struct vty
*vty
,
1377 struct ospf6_route_table
*table
,
1378 json_object
*json
, bool use_json
)
1380 struct ospf6_route
*route
, *prev
= NULL
;
1381 int i
, pathtype
[OSPF6_PATH_TYPE_MAX
];
1382 unsigned int number
= 0;
1383 int nh_count
= 0, nhinval
= 0, ecmp
= 0;
1384 int alternative
= 0, destination
= 0;
1387 for (i
= 0; i
< OSPF6_PATH_TYPE_MAX
; i
++)
1390 for (route
= ospf6_route_head(table
); route
;
1391 route
= ospf6_route_next(route
)) {
1392 if (prev
== NULL
|| !ospf6_route_is_same(prev
, route
))
1396 nh_count
= ospf6_num_nexthops(route
->nh_list
);
1399 else if (nh_count
> 1)
1401 pathtype
[route
->path
.type
]++;
1407 assert(number
== table
->count
);
1409 json_object_int_add(json
, "numberOfOspfv3Routes", number
);
1410 json_object_int_add(json
, "numberOfDestination", destination
);
1411 json_object_int_add(json
, "numberOfAlternativeRoutes",
1413 json_object_int_add(json
, "numberOfEcmp", ecmp
);
1415 vty_out(vty
, "Number of OSPFv3 routes: %d\n", number
);
1416 vty_out(vty
, "Number of Destination: %d\n", destination
);
1417 vty_out(vty
, "Number of Alternative routes: %d\n", alternative
);
1418 vty_out(vty
, "Number of Equal Cost Multi Path: %d\n", ecmp
);
1420 for (i
= OSPF6_PATH_TYPE_INTRA
; i
<= OSPF6_PATH_TYPE_EXTERNAL2
; i
++) {
1422 snprintf(path_str
, sizeof(path_str
), "numberOf%sRoutes",
1423 OSPF6_PATH_TYPE_JSON(i
));
1424 json_object_int_add(json
, path_str
, pathtype
[i
]);
1426 vty_out(vty
, "Number of %s routes: %d\n",
1427 OSPF6_PATH_TYPE_NAME(i
), pathtype
[i
]);
1431 static void ospf6_route_show_table_prefix(struct vty
*vty
,
1432 struct prefix
*prefix
,
1433 struct ospf6_route_table
*table
,
1434 json_object
*json
, bool use_json
)
1436 struct ospf6_route
*route
;
1437 json_object
*json_routes
= NULL
;
1439 route
= ospf6_route_lookup(prefix
, table
);
1444 json_routes
= json_object_new_object();
1445 ospf6_route_lock(route
);
1446 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1447 /* Specifying a prefix will always display details */
1448 ospf6_route_show_detail(vty
, route
, json_routes
, use_json
);
1449 route
= ospf6_route_next(route
);
1453 json_object_object_add(json
, "routes", json_routes
);
1455 ospf6_route_unlock(route
);
1458 static void ospf6_route_show_table_address(struct vty
*vty
,
1459 struct prefix
*prefix
,
1460 struct ospf6_route_table
*table
,
1461 json_object
*json
, bool use_json
)
1463 struct ospf6_route
*route
;
1464 json_object
*json_routes
= NULL
;
1466 route
= ospf6_route_lookup_bestmatch(prefix
, table
);
1471 json_routes
= json_object_new_object();
1472 prefix
= &route
->prefix
;
1473 ospf6_route_lock(route
);
1474 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1475 /* Specifying a prefix will always display details */
1476 ospf6_route_show_detail(vty
, route
, json_routes
, use_json
);
1477 route
= ospf6_route_next(route
);
1480 json_object_object_add(json
, "routes", json_routes
);
1482 ospf6_route_unlock(route
);
1485 static void ospf6_route_show_table_match(struct vty
*vty
, int detail
,
1486 struct prefix
*prefix
,
1487 struct ospf6_route_table
*table
,
1488 json_object
*json
, bool use_json
)
1490 struct ospf6_route
*route
;
1491 json_object
*json_routes
= NULL
;
1493 assert(prefix
->family
);
1495 route
= ospf6_route_match_head(prefix
, table
);
1497 json_routes
= json_object_new_object();
1500 ospf6_route_show_detail(vty
, route
, json_routes
,
1503 ospf6_route_show(vty
, route
, json_routes
, use_json
);
1504 route
= ospf6_route_match_next(prefix
, route
);
1507 json_object_object_add(json
, "routes", json_routes
);
1510 static void ospf6_route_show_table_type(struct vty
*vty
, int detail
,
1512 struct ospf6_route_table
*table
,
1513 json_object
*json
, bool use_json
)
1515 struct ospf6_route
*route
;
1516 json_object
*json_routes
= NULL
;
1518 route
= ospf6_route_head(table
);
1520 json_routes
= json_object_new_object();
1522 if (route
->path
.type
== type
) {
1524 ospf6_route_show_detail(vty
, route
, json_routes
,
1527 ospf6_route_show(vty
, route
, json_routes
,
1530 route
= ospf6_route_next(route
);
1533 json_object_object_add(json
, "routes", json_routes
);
1536 static void ospf6_route_show_table(struct vty
*vty
, int detail
,
1537 struct ospf6_route_table
*table
,
1538 json_object
*json
, bool use_json
)
1540 struct ospf6_route
*route
;
1541 json_object
*json_routes
= NULL
;
1543 route
= ospf6_route_head(table
);
1545 json_routes
= json_object_new_object();
1548 ospf6_route_show_detail(vty
, route
, json_routes
,
1551 ospf6_route_show(vty
, route
, json_routes
, use_json
);
1552 route
= ospf6_route_next(route
);
1555 json_object_object_add(json
, "routes", json_routes
);
1558 int ospf6_route_table_show(struct vty
*vty
, int argc_start
, int argc
,
1559 struct cmd_token
**argv
,
1560 struct ospf6_route_table
*table
, bool use_json
)
1568 struct prefix prefix
;
1570 int arg_end
= use_json
? (argc
- 1) : argc
;
1571 json_object
*json
= NULL
;
1573 memset(&prefix
, 0, sizeof(struct prefix
));
1576 json
= json_object_new_object();
1578 for (i
= argc_start
; i
< arg_end
; i
++) {
1579 if (strmatch(argv
[i
]->text
, "summary")) {
1584 if (strmatch(argv
[i
]->text
, "intra-area")) {
1585 type
= OSPF6_PATH_TYPE_INTRA
;
1589 if (strmatch(argv
[i
]->text
, "inter-area")) {
1590 type
= OSPF6_PATH_TYPE_INTER
;
1594 if (strmatch(argv
[i
]->text
, "external-1")) {
1595 type
= OSPF6_PATH_TYPE_EXTERNAL1
;
1599 if (strmatch(argv
[i
]->text
, "external-2")) {
1600 type
= OSPF6_PATH_TYPE_EXTERNAL2
;
1604 if (strmatch(argv
[i
]->text
, "detail")) {
1609 if (strmatch(argv
[i
]->text
, "match")) {
1614 ret
= str2prefix(argv
[i
]->arg
, &prefix
);
1615 if (ret
== 1 && prefix
.family
== AF_INET6
) {
1617 if (strchr(argv
[i
]->arg
, '/'))
1622 json_object_string_add(json
, "malformedArgument",
1625 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1630 /* Give summary of this route table */
1632 ospf6_route_show_table_summary(vty
, table
, json
, use_json
);
1634 vty_out(vty
, "%s\n",
1635 json_object_to_json_string_ext(
1636 json
, JSON_C_TO_STRING_PRETTY
));
1637 json_object_free(json
);
1642 /* Give exact prefix-match route */
1643 if (isprefix
&& !match
) {
1644 /* If exact address, give best matching route */
1646 ospf6_route_show_table_address(vty
, &prefix
, table
,
1649 ospf6_route_show_table_prefix(vty
, &prefix
, table
, json
,
1653 vty_out(vty
, "%s\n",
1654 json_object_to_json_string_ext(
1655 json
, JSON_C_TO_STRING_PRETTY
));
1656 json_object_free(json
);
1662 ospf6_route_show_table_match(vty
, detail
, &prefix
, table
, json
,
1665 ospf6_route_show_table_type(vty
, detail
, type
, table
, json
,
1668 ospf6_route_show_table(vty
, detail
, table
, json
, use_json
);
1671 vty_out(vty
, "%s\n",
1672 json_object_to_json_string_ext(
1673 json
, JSON_C_TO_STRING_PRETTY
));
1674 json_object_free(json
);
1679 static void ospf6_linkstate_show_header(struct vty
*vty
)
1681 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1682 "Net-ID", "Rtr-Bits", "Options", "Cost");
1685 static void ospf6_linkstate_show(struct vty
*vty
, struct ospf6_route
*route
)
1687 uint32_t router
, id
;
1688 char routername
[16], idname
[16], rbits
[16], options
[16];
1690 router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1691 inet_ntop(AF_INET
, &router
, routername
, sizeof(routername
));
1692 id
= ospf6_linkstate_prefix_id(&route
->prefix
);
1693 inet_ntop(AF_INET
, &id
, idname
, sizeof(idname
));
1695 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1697 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1700 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1701 routername
, idname
, rbits
, options
,
1702 (unsigned long)route
->path
.cost
);
1704 vty_out(vty
, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1705 routername
, idname
, rbits
, options
,
1706 (unsigned long)route
->path
.cost
);
1710 static void ospf6_linkstate_show_table_exact(struct vty
*vty
,
1711 struct prefix
*prefix
,
1712 struct ospf6_route_table
*table
)
1714 struct ospf6_route
*route
;
1716 route
= ospf6_route_lookup(prefix
, table
);
1720 ospf6_route_lock(route
);
1721 while (route
&& ospf6_route_is_prefix(prefix
, route
)) {
1722 /* Specifying a prefix will always display details */
1723 ospf6_route_show_detail(vty
, route
, NULL
, false);
1724 route
= ospf6_route_next(route
);
1727 ospf6_route_unlock(route
);
1730 static void ospf6_linkstate_show_table(struct vty
*vty
, int detail
,
1731 struct ospf6_route_table
*table
)
1733 struct ospf6_route
*route
;
1736 ospf6_linkstate_show_header(vty
);
1738 route
= ospf6_route_head(table
);
1741 ospf6_route_show_detail(vty
, route
, NULL
, false);
1743 ospf6_linkstate_show(vty
, route
);
1744 route
= ospf6_route_next(route
);
1748 int ospf6_linkstate_table_show(struct vty
*vty
, int idx_ipv4
, int argc
,
1749 struct cmd_token
**argv
,
1750 struct ospf6_route_table
*table
)
1756 struct prefix router
, id
, prefix
;
1758 memset(&router
, 0, sizeof(struct prefix
));
1759 memset(&id
, 0, sizeof(struct prefix
));
1760 memset(&prefix
, 0, sizeof(struct prefix
));
1762 for (i
= idx_ipv4
; i
< argc
; i
++) {
1763 if (strmatch(argv
[i
]->text
, "detail")) {
1769 ret
= str2prefix(argv
[i
]->arg
, &router
);
1770 if (ret
== 1 && router
.family
== AF_INET
) {
1774 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1779 ret
= str2prefix(argv
[i
]->arg
, &id
);
1780 if (ret
== 1 && id
.family
== AF_INET
) {
1784 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1788 vty_out(vty
, "Malformed argument: %s\n", argv
[i
]->arg
);
1793 ospf6_linkstate_prefix(router
.u
.prefix4
.s_addr
,
1794 id
.u
.prefix4
.s_addr
, &prefix
);
1797 ospf6_linkstate_show_table_exact(vty
, &prefix
, table
);
1799 ospf6_linkstate_show_table(vty
, detail
, table
);
1805 void ospf6_brouter_show_header(struct vty
*vty
)
1807 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1808 "Options", "Path-Type", "Area");
1811 void ospf6_brouter_show(struct vty
*vty
, struct ospf6_route
*route
)
1813 uint32_t adv_router
;
1814 char adv
[16], rbits
[16], options
[16], area
[16];
1816 adv_router
= ospf6_linkstate_prefix_adv_router(&route
->prefix
);
1817 inet_ntop(AF_INET
, &adv_router
, adv
, sizeof(adv
));
1818 ospf6_capability_printbuf(route
->path
.router_bits
, rbits
,
1820 ospf6_options_printbuf(route
->path
.options
, options
, sizeof(options
));
1821 inet_ntop(AF_INET
, &route
->path
.area_id
, area
, sizeof(area
));
1823 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1824 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1825 vty_out(vty
, "%-15s %-8s %-14s %-10s %-15s\n", adv
, rbits
, options
,
1826 OSPF6_PATH_TYPE_NAME(route
->path
.type
), area
);
1829 DEFUN (debug_ospf6_route
,
1830 debug_ospf6_route_cmd
,
1831 "debug ospf6 route <table|intra-area|inter-area|memory>",
1835 "Debug route table calculation\n"
1836 "Debug intra-area route calculation\n"
1837 "Debug inter-area route calculation\n"
1838 "Debug route memory use\n"
1842 unsigned char level
= 0;
1844 if (!strcmp(argv
[idx_type
]->text
, "table"))
1845 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1846 else if (!strcmp(argv
[idx_type
]->text
, "intra-area"))
1847 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1848 else if (!strcmp(argv
[idx_type
]->text
, "inter-area"))
1849 level
= OSPF6_DEBUG_ROUTE_INTER
;
1850 else if (!strcmp(argv
[idx_type
]->text
, "memory"))
1851 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1852 OSPF6_DEBUG_ROUTE_ON(level
);
1856 DEFUN (no_debug_ospf6_route
,
1857 no_debug_ospf6_route_cmd
,
1858 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1863 "Debug route table calculation\n"
1864 "Debug intra-area route calculation\n"
1865 "Debug inter-area route calculation\n"
1866 "Debug route memory use\n")
1869 unsigned char level
= 0;
1871 if (!strcmp(argv
[idx_type
]->text
, "table"))
1872 level
= OSPF6_DEBUG_ROUTE_TABLE
;
1873 else if (!strcmp(argv
[idx_type
]->text
, "intra-area"))
1874 level
= OSPF6_DEBUG_ROUTE_INTRA
;
1875 else if (!strcmp(argv
[idx_type
]->text
, "inter-area"))
1876 level
= OSPF6_DEBUG_ROUTE_INTER
;
1877 else if (!strcmp(argv
[idx_type
]->text
, "memory"))
1878 level
= OSPF6_DEBUG_ROUTE_MEMORY
;
1879 OSPF6_DEBUG_ROUTE_OFF(level
);
1883 int config_write_ospf6_debug_route(struct vty
*vty
)
1885 if (IS_OSPF6_DEBUG_ROUTE(TABLE
))
1886 vty_out(vty
, "debug ospf6 route table\n");
1887 if (IS_OSPF6_DEBUG_ROUTE(INTRA
))
1888 vty_out(vty
, "debug ospf6 route intra-area\n");
1889 if (IS_OSPF6_DEBUG_ROUTE(INTER
))
1890 vty_out(vty
, "debug ospf6 route inter-area\n");
1891 if (IS_OSPF6_DEBUG_ROUTE(MEMORY
))
1892 vty_out(vty
, "debug ospf6 route memory\n");
1897 void install_element_ospf6_debug_route(void)
1899 install_element(ENABLE_NODE
, &debug_ospf6_route_cmd
);
1900 install_element(ENABLE_NODE
, &no_debug_ospf6_route_cmd
);
1901 install_element(CONFIG_NODE
, &debug_ospf6_route_cmd
);
1902 install_element(CONFIG_NODE
, &no_debug_ospf6_route_cmd
);