]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_route.c
ospf6d: remove unused bitfield
[mirror_frr.git] / ospf6d / ospf6_route.c
CommitLineData
718e3744 1/*
508e53e2 2 * Copyright (C) 2003 Yasuhiro Ohara
718e3744 3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
896014f4
DL
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
718e3744 19 */
20
508e53e2 21#include <zebra.h>
22
23#include "log.h"
24#include "memory.h"
25#include "prefix.h"
26#include "table.h"
27#include "vty.h"
28#include "command.h"
cf1ce250 29#include "linklist.h"
508e53e2 30
508e53e2 31#include "ospf6_proto.h"
32#include "ospf6_lsa.h"
049207c3 33#include "ospf6_lsdb.h"
508e53e2 34#include "ospf6_route.h"
cf1ce250
PJ
35#include "ospf6_top.h"
36#include "ospf6_area.h"
37#include "ospf6_interface.h"
049207c3 38#include "ospf6d.h"
c3c0ac83 39#include "ospf6_zebra.h"
d5cb3508
YR
40#ifndef VTYSH_EXTRACT_PL
41#include "ospf6d/ospf6_route_clippy.c"
42#endif
718e3744 43
30043e4c 44DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE, "OSPF6 route");
de4a0bda 45DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE_TABLE, "OSPF6 route table");
30043e4c
DL
46DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_NEXTHOP, "OSPF6 nexthop");
47DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_PATH, "OSPF6 Path");
48
508e53e2 49unsigned char conf_debug_ospf6_route = 0;
718e3744 50
d62a17ae 51static char *ospf6_route_table_name(struct ospf6_route_table *table)
cf1ce250 52{
d62a17ae 53 static char name[64];
54 switch (table->scope_type) {
55 case OSPF6_SCOPE_TYPE_GLOBAL: {
56 switch (table->table_type) {
57 case OSPF6_TABLE_TYPE_ROUTES:
58 snprintf(name, sizeof(name), "global route table");
59 break;
60 case OSPF6_TABLE_TYPE_BORDER_ROUTERS:
61 snprintf(name, sizeof(name), "global brouter table");
62 break;
63 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES:
64 snprintf(name, sizeof(name), "global external table");
65 break;
66 default:
67 snprintf(name, sizeof(name), "global unknown table");
68 break;
69 }
70 } break;
71
72 case OSPF6_SCOPE_TYPE_AREA: {
73 struct ospf6_area *oa = (struct ospf6_area *)table->scope;
74 switch (table->table_type) {
75 case OSPF6_TABLE_TYPE_SPF_RESULTS:
76 snprintf(name, sizeof(name), "area %s spf table",
77 oa->name);
78 break;
79 case OSPF6_TABLE_TYPE_ROUTES:
80 snprintf(name, sizeof(name), "area %s route table",
81 oa->name);
82 break;
83 case OSPF6_TABLE_TYPE_PREFIX_RANGES:
84 snprintf(name, sizeof(name), "area %s range table",
85 oa->name);
86 break;
87 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES:
88 snprintf(name, sizeof(name),
89 "area %s summary prefix table", oa->name);
90 break;
91 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS:
92 snprintf(name, sizeof(name),
93 "area %s summary router table", oa->name);
94 break;
95 default:
96 snprintf(name, sizeof(name), "area %s unknown table",
97 oa->name);
98 break;
99 }
100 } break;
101
102 case OSPF6_SCOPE_TYPE_INTERFACE: {
103 struct ospf6_interface *oi =
104 (struct ospf6_interface *)table->scope;
105 switch (table->table_type) {
106 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES:
107 snprintf(name, sizeof(name),
108 "interface %s connected table",
109 oi->interface->name);
110 break;
111 default:
112 snprintf(name, sizeof(name),
113 "interface %s unknown table",
114 oi->interface->name);
115 break;
116 }
117 } break;
118
119 default: {
120 switch (table->table_type) {
121 case OSPF6_TABLE_TYPE_SPF_RESULTS:
122 snprintf(name, sizeof(name), "temporary spf table");
123 break;
124 default:
125 snprintf(name, sizeof(name), "temporary unknown table");
126 break;
127 }
128 } break;
129 }
130 return name;
cf1ce250
PJ
131}
132
d7c0a89a 133void ospf6_linkstate_prefix(uint32_t adv_router, uint32_t id,
d62a17ae 134 struct prefix *prefix)
508e53e2 135{
d62a17ae 136 memset(prefix, 0, sizeof(struct prefix));
137 prefix->family = AF_INET6;
138 prefix->prefixlen = 64;
139 memcpy(&prefix->u.prefix6.s6_addr[0], &adv_router, 4);
140 memcpy(&prefix->u.prefix6.s6_addr[4], &id, 4);
508e53e2 141}
718e3744 142
d62a17ae 143void ospf6_linkstate_prefix2str(struct prefix *prefix, char *buf, int size)
718e3744 144{
d7c0a89a 145 uint32_t adv_router, id;
d62a17ae 146 char adv_router_str[16], id_str[16];
147 memcpy(&adv_router, &prefix->u.prefix6.s6_addr[0], 4);
148 memcpy(&id, &prefix->u.prefix6.s6_addr[4], 4);
149 inet_ntop(AF_INET, &adv_router, adv_router_str, sizeof(adv_router_str));
150 inet_ntop(AF_INET, &id, id_str, sizeof(id_str));
151 if (ntohl(id))
152 snprintf(buf, size, "%s Net-ID: %s", adv_router_str, id_str);
153 else
154 snprintf(buf, size, "%s", adv_router_str);
508e53e2 155}
718e3744 156
508e53e2 157/* Global strings for logging */
2b64873d 158const char *const ospf6_dest_type_str[OSPF6_DEST_TYPE_MAX] = {
d62a17ae 159 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
160};
718e3744 161
2b64873d 162const char *const ospf6_dest_type_substr[OSPF6_DEST_TYPE_MAX] = {
d62a17ae 163 "?", "R", "N", "D", "L", "A",
164};
718e3744 165
2b64873d 166const char *const ospf6_path_type_str[OSPF6_PATH_TYPE_MAX] = {
d62a17ae 167 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
168};
718e3744 169
2b64873d 170const char *const ospf6_path_type_substr[OSPF6_PATH_TYPE_MAX] = {
d62a17ae 171 "??", "IA", "IE", "E1", "E2",
172};
718e3744 173
eacd0828
YR
174const char *ospf6_path_type_json[OSPF6_PATH_TYPE_MAX] = {
175 "UnknownRoute", "IntraArea", "InterArea", "External1", "External2",
176};
177
718e3744 178
d62a17ae 179struct ospf6_nexthop *ospf6_nexthop_create(void)
c3c0ac83 180{
d62a17ae 181 struct ospf6_nexthop *nh;
c3c0ac83 182
d62a17ae 183 nh = XCALLOC(MTYPE_OSPF6_NEXTHOP, sizeof(struct ospf6_nexthop));
184 return nh;
c3c0ac83
DS
185}
186
d62a17ae 187void ospf6_nexthop_delete(struct ospf6_nexthop *nh)
c3c0ac83 188{
0a22ddfb 189 XFREE(MTYPE_OSPF6_NEXTHOP, nh);
c3c0ac83
DS
190}
191
d62a17ae 192void ospf6_clear_nexthops(struct list *nh_list)
c3c0ac83 193{
d62a17ae 194 struct listnode *node;
195 struct ospf6_nexthop *nh;
196
197 if (nh_list) {
198 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
199 ospf6_nexthop_clear(nh);
200 }
c3c0ac83
DS
201}
202
203static struct ospf6_nexthop *
d62a17ae 204ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
c3c0ac83 205{
d62a17ae 206 struct listnode *node;
207 struct ospf6_nexthop *nh;
208
209 if (nh_list && nh_match) {
210 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
211 if (ospf6_nexthop_is_same(nh, nh_match))
212 return (nh);
213 }
c3c0ac83 214 }
c3c0ac83 215
d62a17ae 216 return (NULL);
c3c0ac83
DS
217}
218
d62a17ae 219void ospf6_copy_nexthops(struct list *dst, struct list *src)
c3c0ac83 220{
d62a17ae 221 struct ospf6_nexthop *nh_new, *nh;
222 struct listnode *node;
223
224 if (dst && src) {
225 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
226 if (ospf6_nexthop_is_set(nh)) {
227 nh_new = ospf6_nexthop_create();
228 ospf6_nexthop_copy(nh_new, nh);
064d4355 229 listnode_add_sort(dst, nh_new);
d62a17ae 230 }
231 }
c3c0ac83 232 }
c3c0ac83
DS
233}
234
d62a17ae 235void ospf6_merge_nexthops(struct list *dst, struct list *src)
c3c0ac83 236{
d62a17ae 237 struct listnode *node;
238 struct ospf6_nexthop *nh, *nh_new;
239
240 if (src && dst) {
241 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
242 if (!ospf6_route_find_nexthop(dst, nh)) {
243 nh_new = ospf6_nexthop_create();
244 ospf6_nexthop_copy(nh_new, nh);
064d4355 245 listnode_add_sort(dst, nh_new);
d62a17ae 246 }
247 }
c3c0ac83 248 }
c3c0ac83
DS
249}
250
d62a17ae 251int ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
c3c0ac83 252{
d62a17ae 253 struct listnode *anode, *bnode;
254 struct ospf6_nexthop *anh, *bnh;
f867df18 255 bool identical = false;
d62a17ae 256
257 if (a && b) {
258 if (listcount(a->nh_list) == listcount(b->nh_list)) {
259 for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
f867df18 260 identical = false;
d62a17ae 261 for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
f867df18
CS
262 bnh)) {
263 if (ospf6_nexthop_is_same(anh, bnh))
264 identical = true;
265 }
266 /* Currnet List A element not found List B
267 * Non-Identical lists return */
268 if (identical == false)
269 return 1;
d62a17ae 270 }
f867df18 271 return 0;
d62a17ae 272 } else
f867df18 273 return 1;
c3c0ac83 274 }
d62a17ae 275 /* One of the routes doesn't exist ? */
c3c0ac83 276 return (1);
c3c0ac83
DS
277}
278
d62a17ae 279int ospf6_num_nexthops(struct list *nh_list)
c3c0ac83 280{
d62a17ae 281 return (listcount(nh_list));
c3c0ac83
DS
282}
283
d62a17ae 284void ospf6_add_nexthop(struct list *nh_list, int ifindex, struct in6_addr *addr)
c3c0ac83 285{
d62a17ae 286 struct ospf6_nexthop *nh;
287 struct ospf6_nexthop nh_match;
288
289 if (nh_list) {
d2e5d5d4
MR
290 if (addr) {
291 if (ifindex)
292 nh_match.type = NEXTHOP_TYPE_IPV6_IFINDEX;
293 else
294 nh_match.type = NEXTHOP_TYPE_IPV6;
295
d62a17ae 296 memcpy(&nh_match.address, addr,
297 sizeof(struct in6_addr));
d2e5d5d4
MR
298 } else {
299 nh_match.type = NEXTHOP_TYPE_IFINDEX;
300
d62a17ae 301 memset(&nh_match.address, 0, sizeof(struct in6_addr));
d2e5d5d4
MR
302 }
303
304 nh_match.ifindex = ifindex;
d62a17ae 305
306 if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
307 nh = ospf6_nexthop_create();
308 ospf6_nexthop_copy(nh, &nh_match);
309 listnode_add(nh_list, nh);
310 }
c3c0ac83 311 }
c3c0ac83
DS
312}
313
d2e5d5d4
MR
314void ospf6_add_route_nexthop_blackhole(struct ospf6_route *route)
315{
316 struct ospf6_nexthop *nh;
317 struct ospf6_nexthop nh_match = {};
318
319 /* List not allocated. */
320 if (route->nh_list == NULL)
321 return;
322
323 /* Entry already exists. */
324 nh_match.type = NEXTHOP_TYPE_BLACKHOLE;
325 if (ospf6_route_find_nexthop(route->nh_list, &nh_match))
326 return;
327
328 nh = ospf6_nexthop_create();
329 ospf6_nexthop_copy(nh, &nh_match);
330 listnode_add(route->nh_list, nh);
331}
332
d62a17ae 333void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
5afa1c6b 334 struct zapi_nexthop nexthops[],
1fa58587 335 int entries, vrf_id_t vrf_id)
c3c0ac83 336{
d62a17ae 337 struct ospf6_nexthop *nh;
338 struct listnode *node;
d62a17ae 339 int i;
340
341 if (route) {
342 i = 0;
343 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
344 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
d2e5d5d4
MR
345 zlog_debug(" nexthop: %s %pI6%%%.*s(%d)",
346 nexthop_type_to_str(nh->type),
347 &nh->address, IFNAMSIZ,
348 ifindex2ifname(nh->ifindex, vrf_id),
349 nh->ifindex);
d62a17ae 350 }
d2e5d5d4 351
b30f3b91
RW
352 if (i >= entries)
353 return;
354
1fa58587 355 nexthops[i].vrf_id = vrf_id;
d2e5d5d4
MR
356 nexthops[i].type = nh->type;
357
358 switch (nh->type) {
359 case NEXTHOP_TYPE_BLACKHOLE:
360 /* NOTHING */
361 break;
362
363 case NEXTHOP_TYPE_IFINDEX:
364 nexthops[i].ifindex = nh->ifindex;
365 break;
366
367 case NEXTHOP_TYPE_IPV4_IFINDEX:
368 case NEXTHOP_TYPE_IPV4:
369 /*
370 * OSPFv3 with IPv4 routes is not supported
371 * yet. Skip this next hop.
372 */
373 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
374 zlog_debug(" Skipping IPv4 next hop");
375 continue;
376
377 case NEXTHOP_TYPE_IPV6_IFINDEX:
378 nexthops[i].ifindex = nh->ifindex;
379 /* FALLTHROUGH */
380 case NEXTHOP_TYPE_IPV6:
5afa1c6b 381 nexthops[i].gate.ipv6 = nh->address;
d2e5d5d4
MR
382 break;
383 }
b30f3b91 384 i++;
d62a17ae 385 }
c3c0ac83 386 }
c3c0ac83
DS
387}
388
d62a17ae 389int ospf6_route_get_first_nh_index(struct ospf6_route *route)
c3c0ac83 390{
d62a17ae 391 struct ospf6_nexthop *nh;
c3c0ac83 392
d62a17ae 393 if (route) {
10197318
DS
394 nh = listnode_head(route->nh_list);
395 if (nh)
396 return nh->ifindex;
d62a17ae 397 }
c3c0ac83 398
95f7965d 399 return -1;
c3c0ac83
DS
400}
401
064d4355 402int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
d107621d 403{
f867df18
CS
404 if (a->ifindex < b->ifindex)
405 return -1;
406 else if (a->ifindex > b->ifindex)
d107621d 407 return 1;
f867df18
CS
408 else
409 return memcmp(&a->address, &b->address,
410 sizeof(struct in6_addr));
411
d107621d
CS
412 return 0;
413}
414
064d4355
CS
415static int ospf6_path_cmp(struct ospf6_path *a, struct ospf6_path *b)
416{
417 if (a->origin.adv_router < b->origin.adv_router)
418 return -1;
419 else if (a->origin.adv_router > b->origin.adv_router)
420 return 1;
421 else
422 return 0;
423}
424
425void ospf6_path_free(struct ospf6_path *op)
426{
427 if (op->nh_list)
6a154c88 428 list_delete(&op->nh_list);
064d4355
CS
429 XFREE(MTYPE_OSPF6_PATH, op);
430}
431
432struct ospf6_path *ospf6_path_dup(struct ospf6_path *path)
433{
434 struct ospf6_path *new;
435
436 new = XCALLOC(MTYPE_OSPF6_PATH, sizeof(struct ospf6_path));
437 memcpy(new, path, sizeof(struct ospf6_path));
438 new->nh_list = list_new();
439 new->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
996c9314 440 new->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
064d4355
CS
441
442 return new;
443}
444
03f3c1c1
CS
445void ospf6_copy_paths(struct list *dst, struct list *src)
446{
447 struct ospf6_path *path_new, *path;
448 struct listnode *node;
449
450 if (dst && src) {
451 for (ALL_LIST_ELEMENTS_RO(src, node, path)) {
452 path_new = ospf6_path_dup(path);
453 ospf6_copy_nexthops(path_new->nh_list, path->nh_list);
454 listnode_add_sort(dst, path_new);
455 }
456 }
457}
458
22813fdb 459struct ospf6_route *ospf6_route_create(struct ospf6 *ospf6)
718e3744 460{
d62a17ae 461 struct ospf6_route *route;
e285b70d 462
d62a17ae 463 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
464 route->nh_list = list_new();
d107621d 465 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
996c9314 466 route->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
064d4355
CS
467 route->paths = list_new();
468 route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
996c9314 469 route->paths->del = (void (*)(void *))ospf6_path_free;
22813fdb
MR
470 route->ospf6 = ospf6;
471
d62a17ae 472 return route;
508e53e2 473}
718e3744 474
d62a17ae 475void ospf6_route_delete(struct ospf6_route *route)
508e53e2 476{
d62a17ae 477 if (route) {
d107621d 478 if (route->nh_list)
6a154c88 479 list_delete(&route->nh_list);
064d4355 480 if (route->paths)
6a154c88 481 list_delete(&route->paths);
d62a17ae 482 XFREE(MTYPE_OSPF6_ROUTE, route);
483 }
718e3744 484}
485
d62a17ae 486struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
718e3744 487{
d62a17ae 488 struct ospf6_route *new;
489
22813fdb 490 new = ospf6_route_create(route->ospf6);
d62a17ae 491 new->type = route->type;
492 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
4699ad72 493 new->prefix_options = route->prefix_options;
d62a17ae 494 new->installed = route->installed;
495 new->changed = route->changed;
496 new->flag = route->flag;
497 new->route_option = route->route_option;
498 new->linkstate_id = route->linkstate_id;
499 new->path = route->path;
500 ospf6_copy_nexthops(new->nh_list, route->nh_list);
03f3c1c1 501 ospf6_copy_paths(new->paths, route->paths);
d62a17ae 502 new->rnode = NULL;
503 new->prev = NULL;
504 new->next = NULL;
505 new->table = NULL;
506 new->lock = 0;
507 return new;
508e53e2 508}
718e3744 509
d62a17ae 510void ospf6_route_lock(struct ospf6_route *route)
508e53e2 511{
d62a17ae 512 route->lock++;
508e53e2 513}
718e3744 514
d62a17ae 515void ospf6_route_unlock(struct ospf6_route *route)
508e53e2 516{
d62a17ae 517 assert(route->lock > 0);
518 route->lock--;
519 if (route->lock == 0) {
520 /* Can't detach from the table until here
521 because ospf6_route_next () will use
522 the 'route->table' pointer for logging */
523 route->table = NULL;
524 ospf6_route_delete(route);
525 }
508e53e2 526}
718e3744 527
508e53e2 528/* Route compare function. If ra is more preferred, it returns
529 less than 0. If rb is more preferred returns greater than 0.
530 Otherwise (neither one is preferred), returns 0 */
d62a17ae 531int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
508e53e2 532{
d62a17ae 533 assert(ospf6_route_is_same(ra, rb));
534 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
535 && ra->path.type < OSPF6_PATH_TYPE_MAX);
536 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
537 && rb->path.type < OSPF6_PATH_TYPE_MAX);
538
539 if (ra->type != rb->type)
540 return (ra->type - rb->type);
541
d62a17ae 542 if (ra->path.type != rb->path.type)
543 return (ra->path.type - rb->path.type);
544
545 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
546 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
547 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
07b37f93
CS
548 else
549 return (ra->path.cost - rb->path.cost);
d62a17ae 550 } else {
551 if (ra->path.cost != rb->path.cost)
552 return (ra->path.cost - rb->path.cost);
553 }
554
10efbdc2
CS
555 if (ra->path.area_id != rb->path.area_id)
556 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
557
d62a17ae 558 return 0;
718e3744 559}
560
d62a17ae 561struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
562 struct ospf6_route_table *table)
718e3744 563{
d62a17ae 564 struct route_node *node;
565 struct ospf6_route *route;
718e3744 566
d62a17ae 567 node = route_node_lookup(table->table, prefix);
568 if (node == NULL)
569 return NULL;
508e53e2 570
d62a17ae 571 route = (struct ospf6_route *)node->info;
d107621d 572 route_unlock_node(node); /* to free the lookup lock */
d62a17ae 573 return route;
508e53e2 574}
718e3744 575
508e53e2 576struct ospf6_route *
d62a17ae 577ospf6_route_lookup_identical(struct ospf6_route *route,
578 struct ospf6_route_table *table)
508e53e2 579{
d62a17ae 580 struct ospf6_route *target;
581
582 for (target = ospf6_route_lookup(&route->prefix, table); target;
583 target = target->next) {
996c9314
LB
584 if (target->type == route->type
585 && (memcmp(&target->prefix, &route->prefix,
586 sizeof(struct prefix))
587 == 0)
588 && target->path.type == route->path.type
589 && target->path.cost == route->path.cost
590 && target->path.u.cost_e2 == route->path.u.cost_e2
591 && ospf6_route_cmp_nexthops(target, route) == 0)
d62a17ae 592 return target;
593 }
594 return NULL;
718e3744 595}
596
508e53e2 597struct ospf6_route *
d62a17ae 598ospf6_route_lookup_bestmatch(struct prefix *prefix,
599 struct ospf6_route_table *table)
718e3744 600{
d62a17ae 601 struct route_node *node;
602 struct ospf6_route *route;
718e3744 603
d62a17ae 604 node = route_node_match(table->table, prefix);
605 if (node == NULL)
606 return NULL;
607 route_unlock_node(node);
718e3744 608
d62a17ae 609 route = (struct ospf6_route *)node->info;
610 return route;
718e3744 611}
612
e39d0538 613#ifdef DEBUG
d62a17ae 614static void route_table_assert(struct ospf6_route_table *table)
718e3744 615{
d62a17ae 616 struct ospf6_route *prev, *r, *next;
d62a17ae 617 unsigned int link_error = 0, num = 0;
618
619 r = ospf6_route_head(table);
620 prev = NULL;
621 while (r) {
622 if (r->prev != prev)
623 link_error++;
624
625 next = ospf6_route_next(r);
626
627 if (r->next != next)
628 link_error++;
629
630 prev = r;
631 r = next;
632 }
633
634 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
635 num++;
636
637 if (link_error == 0 && num == table->count)
638 return;
639
450971aa 640 flog_err(EC_LIB_DEVELOPMENT, "PANIC !!");
1c50c1c0
QY
641 flog_err(EC_LIB_DEVELOPMENT,
642 "Something has gone wrong with ospf6_route_table[%p]", table);
d62a17ae 643 zlog_debug("table count = %d, real number = %d", table->count, num);
644 zlog_debug("DUMP START");
2dbe669b
DA
645 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
646 zlog_info("%p<-[%p]->%p : %pFX", r->prev, r, r->next,
647 &r->prefix);
d62a17ae 648 zlog_debug("DUMP END");
649
650 assert(link_error == 0 && num == table->count);
718e3744 651}
cf1ce250 652#define ospf6_route_table_assert(t) (route_table_assert (t))
508e53e2 653#else
cf1ce250 654#define ospf6_route_table_assert(t) ((void) 0)
e39d0538 655#endif /*DEBUG*/
718e3744 656
d62a17ae 657struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
e285b70d 658 struct ospf6_route_table *table)
718e3744 659{
d62a17ae 660 struct route_node *node, *nextnode, *prevnode;
661 struct ospf6_route *current = NULL;
662 struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
663 char buf[PREFIX2STR_BUFFER];
664 struct timeval now;
665
666 assert(route->rnode == NULL);
667 assert(route->lock == 0);
668 assert(route->next == NULL);
669 assert(route->prev == NULL);
670
671 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
672 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
9142948e
RW
673 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
674 inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&route->prefix), buf,
675 sizeof(buf));
d62a17ae 676 else
677 prefix2str(&route->prefix, buf, sizeof(buf));
678
679 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
6942698d 680 zlog_debug("%s %p: route add %p: %s paths %u nh %u",
d62a17ae 681 ospf6_route_table_name(table), (void *)table,
6942698d
CS
682 (void *)route, buf, listcount(route->paths),
683 listcount(route->nh_list));
d62a17ae 684 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
685 zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
686 buf);
687
688 monotime(&now);
689
690 node = route_node_get(table->table, &route->prefix);
691 route->rnode = node;
692
693 /* find place to insert */
694 for (current = node->info; current; current = current->next) {
695 if (!ospf6_route_is_same(current, route))
696 next = current;
697 else if (current->type != route->type)
698 prev = current;
699 else if (ospf6_route_is_same_origin(current, route))
700 old = current;
701 else if (ospf6_route_cmp(current, route) > 0)
702 next = current;
703 else
704 prev = current;
705
706 if (old || next)
707 break;
708 }
718e3744 709
d62a17ae 710 if (old) {
711 /* if route does not actually change, return unchanged */
712 if (ospf6_route_is_identical(old, route)) {
713 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
714 zlog_debug(
07b37f93 715 "%s %p: route add %p: needless update of %p old cost %u",
d62a17ae 716 ospf6_route_table_name(table),
717 (void *)table, (void *)route,
07b37f93 718 (void *)old, old->path.cost);
d62a17ae 719 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
720 zlog_debug("%s: route add: needless update",
721 ospf6_route_table_name(table));
722
723 ospf6_route_delete(route);
724 SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
725 ospf6_route_table_assert(table);
726
d107621d
CS
727 /* to free the lookup lock */
728 route_unlock_node(node);
d62a17ae 729 return old;
730 }
731
732 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
996c9314 733 zlog_debug(
6942698d 734 "%s %p: route add %p cost %u paths %u nh %u: update of %p cost %u paths %u nh %u",
996c9314 735 ospf6_route_table_name(table), (void *)table,
03f3c1c1 736 (void *)route, route->path.cost,
6942698d 737 listcount(route->paths),
03f3c1c1 738 listcount(route->nh_list), (void *)old,
6942698d
CS
739 old->path.cost, listcount(old->paths),
740 listcount(old->nh_list));
d62a17ae 741 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
742 zlog_debug("%s: route add: update",
743 ospf6_route_table_name(table));
744
745 /* replace old one if exists */
746 if (node->info == old) {
747 node->info = route;
748 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
35769de4
K
749 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
750 zlog_debug("%s: replace old route %s",
751 __func__, buf);
d62a17ae 752 }
753
754 if (old->prev)
755 old->prev->next = route;
756 route->prev = old->prev;
757 if (old->next)
758 old->next->prev = route;
759 route->next = old->next;
760
761 route->installed = old->installed;
762 route->changed = now;
763 assert(route->table == NULL);
764 route->table = table;
765
766 ospf6_route_unlock(old); /* will be deleted later */
767 ospf6_route_lock(route);
768
769 SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
770 ospf6_route_table_assert(table);
771
772 if (table->hook_add)
e285b70d 773 (*table->hook_add)(route);
d62a17ae 774
775 return route;
776 }
777
778 /* insert if previous or next node found */
779 if (prev || next) {
780 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
781 zlog_debug(
07b37f93 782 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
d62a17ae 783 ospf6_route_table_name(table), (void *)table,
07b37f93 784 (void *)route, route->path.cost, (void *)prev,
c10e14e9 785 (void *)next, route_node_get_lock_count(node));
d62a17ae 786 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
07b37f93
CS
787 zlog_debug("%s: route add cost %u: another path found",
788 ospf6_route_table_name(table),
789 route->path.cost);
d62a17ae 790
791 if (prev == NULL)
792 prev = next->prev;
793 if (next == NULL)
794 next = prev->next;
795
796 if (prev)
797 prev->next = route;
798 route->prev = prev;
799 if (next)
800 next->prev = route;
801 route->next = next;
802
803 if (node->info == next) {
4f4060f6 804 assert(next && next->rnode == node);
d62a17ae 805 node->info = route;
806 UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
807 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
808 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
35769de4 809 zlog_debug(
c601fa3f 810 "%s %p: route add %p cost %u: replacing previous best: %p cost %u",
d62a17ae 811 ospf6_route_table_name(table),
812 (void *)table, (void *)route,
35769de4
K
813 route->path.cost, (void *)next,
814 next->path.cost);
d62a17ae 815 }
816
817 route->installed = now;
818 route->changed = now;
819 assert(route->table == NULL);
820 route->table = table;
821
822 ospf6_route_lock(route);
823 table->count++;
824 ospf6_route_table_assert(table);
825
826 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
827 if (table->hook_add)
e285b70d 828 (*table->hook_add)(route);
d62a17ae 829
830 return route;
831 }
832
833 /* Else, this is the brand new route regarding to the prefix */
834 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
c601fa3f 835 zlog_debug("%s %p: route add %p %s cost %u: brand new route",
d62a17ae 836 ospf6_route_table_name(table), (void *)table,
c601fa3f 837 (void *)route, buf, route->path.cost);
d62a17ae 838 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
839 zlog_debug("%s: route add: brand new route",
840 ospf6_route_table_name(table));
841
842 assert(node->info == NULL);
843 node->info = route;
844 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
845 ospf6_route_lock(route);
846 route->installed = now;
847 route->changed = now;
848 assert(route->table == NULL);
849 route->table = table;
850
851 /* lookup real existing next route */
852 nextnode = node;
853 route_lock_node(nextnode);
854 do {
855 nextnode = route_next(nextnode);
856 } while (nextnode && nextnode->info == NULL);
857
858 /* set next link */
859 if (nextnode == NULL)
860 route->next = NULL;
861 else {
862 route_unlock_node(nextnode);
863
864 next = nextnode->info;
865 route->next = next;
866 next->prev = route;
867 }
868
869 /* lookup real existing prev route */
870 prevnode = node;
871 route_lock_node(prevnode);
872 do {
873 prevnode = route_prev(prevnode);
874 } while (prevnode && prevnode->info == NULL);
875
876 /* set prev link */
877 if (prevnode == NULL)
878 route->prev = NULL;
879 else {
880 route_unlock_node(prevnode);
881
882 prev = prevnode->info;
883 while (prev->next && ospf6_route_is_same(prev, prev->next))
884 prev = prev->next;
885 route->prev = prev;
886 prev->next = route;
887 }
888
889 table->count++;
890 ospf6_route_table_assert(table);
891
892 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
893 if (table->hook_add)
e285b70d 894 (*table->hook_add)(route);
d62a17ae 895
896 return route;
508e53e2 897}
718e3744 898
d62a17ae 899void ospf6_route_remove(struct ospf6_route *route,
e285b70d 900 struct ospf6_route_table *table)
508e53e2 901{
d62a17ae 902 struct route_node *node;
903 struct ospf6_route *current;
904 char buf[PREFIX2STR_BUFFER];
905
906 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
907 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
9142948e
RW
908 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
909 inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&route->prefix), buf,
910 sizeof(buf));
d62a17ae 911 else
912 prefix2str(&route->prefix, buf, sizeof(buf));
913
914 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
07b37f93 915 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
d62a17ae 916 ospf6_route_table_name(table), (void *)table,
07b37f93 917 (void *)route, buf, route->path.cost, route->lock);
d62a17ae 918 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
919 zlog_debug("%s: route remove: %s",
920 ospf6_route_table_name(table), buf);
921
922 node = route_node_lookup(table->table, &route->prefix);
923 assert(node);
924
925 /* find the route to remove, making sure that the route pointer
926 is from the route table. */
927 current = node->info;
d107621d 928 while (current && current != route)
d62a17ae 929 current = current->next;
d107621d 930
d62a17ae 931 assert(current == route);
932
933 /* adjust doubly linked list */
934 if (route->prev)
935 route->prev->next = route->next;
936 if (route->next)
937 route->next->prev = route->prev;
938
939 if (node->info == route) {
940 if (route->next && route->next->rnode == node) {
941 node->info = route->next;
942 SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
35769de4
K
943 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
944 zlog_debug("%s: remove route %s", __func__,
945 buf);
d107621d
CS
946 } else {
947 node->info = NULL;
948 route->rnode = NULL;
949 route_unlock_node(node); /* to free the original lock */
950 }
d62a17ae 951 }
508e53e2 952
d107621d 953 route_unlock_node(node); /* to free the lookup lock */
d62a17ae 954 table->count--;
955 ospf6_route_table_assert(table);
718e3744 956
d62a17ae 957 SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
508e53e2 958
dd2395e1 959 /* Note hook_remove may call ospf6_route_remove */
d62a17ae 960 if (table->hook_remove)
e285b70d 961 (*table->hook_remove)(route);
cf1ce250 962
d62a17ae 963 ospf6_route_unlock(route);
964}
cf1ce250 965
d62a17ae 966struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
967{
968 struct route_node *node;
969 struct ospf6_route *route;
970
971 node = route_top(table->table);
972 if (node == NULL)
973 return NULL;
974
975 /* skip to the real existing entry */
976 while (node && node->info == NULL)
977 node = route_next(node);
978 if (node == NULL)
979 return NULL;
980
981 route_unlock_node(node);
982 assert(node->info);
983
984 route = (struct ospf6_route *)node->info;
985 assert(route->prev == NULL);
986 assert(route->table == table);
987 ospf6_route_lock(route);
988
989 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
990 zlog_info("%s %p: route head: %p<-[%p]->%p",
991 ospf6_route_table_name(table), (void *)table,
992 (void *)route->prev, (void *)route,
993 (void *)route->next);
994
995 return route;
718e3744 996}
997
d62a17ae 998struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
718e3744 999{
d62a17ae 1000 struct ospf6_route *next = route->next;
718e3744 1001
d62a17ae 1002 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
a2d0055a 1003 zlog_info("%s %p: route next: %p<-[%p]->%p , route ref count %u",
d62a17ae 1004 ospf6_route_table_name(route->table),
1005 (void *)route->table, (void *)route->prev,
a2d0055a
CS
1006 (void *)route, (void *)route->next,
1007 route->lock);
cf1ce250 1008
d62a17ae 1009 ospf6_route_unlock(route);
1010 if (next)
1011 ospf6_route_lock(next);
718e3744 1012
d62a17ae 1013 return next;
508e53e2 1014}
718e3744 1015
d62a17ae 1016struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
508e53e2 1017{
d62a17ae 1018 struct route_node *rnode;
1019 struct ospf6_route *next;
1020
1021 ospf6_route_unlock(route);
1022
1023 rnode = route->rnode;
1024 route_lock_node(rnode);
1025 rnode = route_next(rnode);
1026 while (rnode && rnode->info == NULL)
1027 rnode = route_next(rnode);
1028 if (rnode == NULL)
1029 return NULL;
1030 route_unlock_node(rnode);
1031
1032 assert(rnode->info);
1033 next = (struct ospf6_route *)rnode->info;
1034 ospf6_route_lock(next);
1035 return next;
508e53e2 1036}
718e3744 1037
d62a17ae 1038struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
1039 struct ospf6_route_table *table)
508e53e2 1040{
d62a17ae 1041 struct route_node *node;
1042 struct ospf6_route *route;
1043
1044 /* Walk down tree. */
1045 node = table->table->top;
1046 while (node && node->p.prefixlen < prefix->prefixlen
1047 && prefix_match(&node->p, prefix))
1048 node = node->link[prefix_bit(&prefix->u.prefix,
1049 node->p.prefixlen)];
1050
1051 if (node)
1052 route_lock_node(node);
1053 while (node && node->info == NULL)
1054 node = route_next(node);
1055 if (node == NULL)
1056 return NULL;
1057 route_unlock_node(node);
1058
1059 if (!prefix_match(prefix, &node->p))
1060 return NULL;
1061
1062 route = node->info;
1063 ospf6_route_lock(route);
1064 return route;
508e53e2 1065}
718e3744 1066
d62a17ae 1067struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
1068 struct ospf6_route *route)
508e53e2 1069{
d62a17ae 1070 struct ospf6_route *next;
508e53e2 1071
d62a17ae 1072 next = ospf6_route_next(route);
1073 if (next && !prefix_match(prefix, &next->prefix)) {
1074 ospf6_route_unlock(next);
1075 next = NULL;
1076 }
508e53e2 1077
d62a17ae 1078 return next;
718e3744 1079}
1080
e285b70d 1081void ospf6_route_remove_all(struct ospf6_route_table *table)
718e3744 1082{
d62a17ae 1083 struct ospf6_route *route;
1084 for (route = ospf6_route_head(table); route;
1085 route = ospf6_route_next(route))
e285b70d 1086 ospf6_route_remove(route, table);
718e3744 1087}
1088
d62a17ae 1089struct ospf6_route_table *ospf6_route_table_create(int s, int t)
718e3744 1090{
d62a17ae 1091 struct ospf6_route_table *new;
de4a0bda
PR
1092 new = XCALLOC(MTYPE_OSPF6_ROUTE_TABLE,
1093 sizeof(struct ospf6_route_table));
d62a17ae 1094 new->table = route_table_init();
1095 new->scope_type = s;
1096 new->table_type = t;
1097 return new;
718e3744 1098}
1099
e285b70d 1100void ospf6_route_table_delete(struct ospf6_route_table *table)
718e3744 1101{
e285b70d 1102 ospf6_route_remove_all(table);
d62a17ae 1103 route_table_finish(table->table);
de4a0bda 1104 XFREE(MTYPE_OSPF6_ROUTE_TABLE, table);
718e3744 1105}
1106
6b0655a2 1107
508e53e2 1108/* VTY commands */
eacd0828 1109void ospf6_route_show(struct vty *vty, struct ospf6_route *route,
a49ef569 1110 json_object *json_routes, bool use_json)
718e3744 1111{
d62a17ae 1112 int i;
1113 char destination[PREFIX2STR_BUFFER], nexthop[64];
68bfcc05 1114 char duration[64];
d62a17ae 1115 struct timeval now, res;
1116 struct listnode *node;
1117 struct ospf6_nexthop *nh;
eacd0828
YR
1118 json_object *json_route = NULL;
1119 json_object *json_array_next_hops = NULL;
1120 json_object *json_next_hop;
d62a17ae 1121
1122 monotime(&now);
1123 timersub(&now, &route->changed, &res);
1124 timerstring(&res, duration, sizeof(duration));
1125
1126 /* destination */
1127 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1128 ospf6_linkstate_prefix2str(&route->prefix, destination,
1129 sizeof(destination));
1130 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1131 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1132 destination, sizeof(destination));
1133 else
1134 prefix2str(&route->prefix, destination, sizeof(destination));
1135
eacd0828
YR
1136 if (use_json) {
1137 json_route = json_object_new_object();
eacd0828
YR
1138 json_object_boolean_add(json_route, "isBestRoute",
1139 ospf6_route_is_best(route));
1140 json_object_string_add(json_route, "destinationType",
1141 OSPF6_DEST_TYPE_SUBSTR(route->type));
1142 json_object_string_add(
1143 json_route, "pathType",
1144 OSPF6_PATH_TYPE_SUBSTR(route->path.type));
1145 json_object_string_add(json_route, "duration", duration);
1146 }
1147
1148 /* Nexthops */
1149 if (use_json)
1150 json_array_next_hops = json_object_new_array();
1151 else
1152 i = 0;
d62a17ae 1153 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
beadc736 1154 struct interface *ifp;
d62a17ae 1155 /* nexthop */
1156 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
beadc736 1157 ifp = if_lookup_by_index_all_vrf(nh->ifindex);
eacd0828
YR
1158 if (use_json) {
1159 json_next_hop = json_object_new_object();
1160 json_object_string_add(json_next_hop, "nextHop",
1161 nexthop);
1162 json_object_string_add(json_next_hop, "interfaceName",
1163 ifp->name);
1164 json_object_array_add(json_array_next_hops,
1165 json_next_hop);
1166 } else {
1167 if (!i) {
1168 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1169 (ospf6_route_is_best(route) ? '*'
1170 : ' '),
1171 OSPF6_DEST_TYPE_SUBSTR(route->type),
1172 OSPF6_PATH_TYPE_SUBSTR(
1173 route->path.type),
1174 destination, nexthop, IFNAMSIZ,
1175 ifp->name, duration);
1176 i++;
1177 } else
1178 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1179 ' ', "", "", "", nexthop, IFNAMSIZ,
1180 ifp->name, "");
1181 }
1182 }
1183 if (use_json) {
1184 json_object_object_add(json_route, "nextHops",
1185 json_array_next_hops);
a49ef569 1186 json_object_object_add(json_routes, destination, json_route);
c3c0ac83 1187 }
718e3744 1188}
1189
eacd0828 1190void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route,
a49ef569 1191 json_object *json_routes, bool use_json)
718e3744 1192{
eacd0828 1193 char destination[PREFIX2STR_BUFFER], nexthop[64];
d62a17ae 1194 char area_id[16], id[16], adv_router[16], capa[16], options[16];
b0822151 1195 char pfx_options[16];
d62a17ae 1196 struct timeval now, res;
68bfcc05 1197 char duration[64];
d62a17ae 1198 struct listnode *node;
1199 struct ospf6_nexthop *nh;
eacd0828
YR
1200 char flag[6];
1201 json_object *json_route = NULL;
1202 json_object *json_array_next_hops = NULL;
1203 json_object *json_next_hop;
d62a17ae 1204
1205 monotime(&now);
1206
1207 /* destination */
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));
1214 else
1215 prefix2str(&route->prefix, destination, sizeof(destination));
d62a17ae 1216
eacd0828
YR
1217 if (use_json) {
1218 json_route = json_object_new_object();
eacd0828
YR
1219 json_object_string_add(json_route, "destinationType",
1220 OSPF6_DEST_TYPE_NAME(route->type));
1221 } else {
1222 vty_out(vty, "Destination: %s\n", destination);
1223 vty_out(vty, "Destination type: %s\n",
1224 OSPF6_DEST_TYPE_NAME(route->type));
1225 }
d62a17ae 1226
1227 /* Time */
1228 timersub(&now, &route->installed, &res);
1229 timerstring(&res, duration, sizeof(duration));
eacd0828
YR
1230 if (use_json)
1231 json_object_string_add(json_route, "installedTimeSince",
1232 duration);
1233 else
1234 vty_out(vty, "Installed Time: %s ago\n", duration);
d62a17ae 1235
1236 timersub(&now, &route->changed, &res);
1237 timerstring(&res, duration, sizeof(duration));
eacd0828
YR
1238 if (use_json)
1239 json_object_string_add(json_route, "changedTimeSince",
1240 duration);
1241 else
1242 vty_out(vty, "Changed Time: %s ago\n", duration);
d62a17ae 1243
1244 /* Debugging info */
eacd0828
YR
1245 if (use_json) {
1246 json_object_int_add(json_route, "numberOfLock", route->lock);
1247 snprintf(
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"
1252 : "-"),
1253 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C"
1254 : "-"));
1255 json_object_string_add(json_route, "flags", flag);
1256 } else {
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"
1261 : "-"),
1262 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C"
1263 : "-"));
1264 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1265 (void *)route->prev, (void *)route,
1266 (void *)route->next);
1267 }
d62a17ae 1268
1269 /* Path section */
1270
1271 /* Area-ID */
1272 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
eacd0828
YR
1273 if (use_json)
1274 json_object_string_add(json_route, "associatedArea", area_id);
1275 else
1276 vty_out(vty, "Associated Area: %s\n", area_id);
d62a17ae 1277
1278 /* Path type */
eacd0828
YR
1279 if (use_json)
1280 json_object_string_add(json_route, "pathType",
1281 OSPF6_PATH_TYPE_NAME(route->path.type));
1282 else
1283 vty_out(vty, "Path Type: %s\n",
1284 OSPF6_PATH_TYPE_NAME(route->path.type));
d62a17ae 1285
1286 /* LS Origin */
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));
eacd0828
YR
1290 if (use_json) {
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",
1296 adv_router);
1297 } else {
1298 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1299 ospf6_lstype_name(route->path.origin.type), id,
1300 adv_router);
1301 }
d62a17ae 1302
1303 /* Options */
1304 ospf6_options_printbuf(route->path.options, options, sizeof(options));
eacd0828
YR
1305 if (use_json)
1306 json_object_string_add(json_route, "options", options);
1307 else
1308 vty_out(vty, "Options: %s\n", options);
d62a17ae 1309
1310 /* Router Bits */
1311 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
eacd0828
YR
1312 if (use_json)
1313 json_object_string_add(json_route, "routerBits", capa);
1314 else
1315 vty_out(vty, "Router Bits: %s\n", capa);
d62a17ae 1316
1317 /* Prefix Options */
b0822151
DL
1318 ospf6_prefix_options_printbuf(route->prefix_options, pfx_options,
1319 sizeof(pfx_options));
eacd0828 1320 if (use_json)
b0822151
DL
1321 json_object_string_add(json_route, "prefixOptions",
1322 pfx_options);
eacd0828 1323 else
b0822151 1324 vty_out(vty, "Prefix Options: %s\n", pfx_options);
d62a17ae 1325
1326 /* Metrics */
eacd0828
YR
1327 if (use_json) {
1328 json_object_int_add(json_route, "metricType",
1329 route->path.metric_type);
1330 json_object_int_add(json_route, "metricCost", route->path.cost);
1331 json_object_int_add(json_route, "metricCostE2",
1332 route->path.u.cost_e2);
1333
1334 json_object_int_add(json_route, "pathsCount",
1335 route->paths->count);
1336 json_object_int_add(json_route, "nextHopCount",
1337 route->nh_list->count);
1338 } else {
1339 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1340 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1341 route->path.u.cost_e2);
1342
1343 vty_out(vty, "Paths count: %u\n", route->paths->count);
1344 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
1345 }
d62a17ae 1346
1347 /* Nexthops */
eacd0828
YR
1348 if (use_json)
1349 json_array_next_hops = json_object_new_array();
1350 else
1351 vty_out(vty, "Nexthop:\n");
1352
d62a17ae 1353 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
beadc736 1354 struct interface *ifp;
beadc736 1355 ifp = if_lookup_by_index_all_vrf(nh->ifindex);
eacd0828
YR
1356 /* nexthop */
1357 if (use_json) {
1358 inet_ntop(AF_INET6, &nh->address, nexthop,
1359 sizeof(nexthop));
1360 json_next_hop = json_object_new_object();
1361 json_object_string_add(json_next_hop, "nextHop",
1362 nexthop);
1363 json_object_string_add(json_next_hop, "interfaceName",
1364 ifp->name);
1365 json_object_array_add(json_array_next_hops,
1366 json_next_hop);
1367 } else
1368 vty_out(vty, " %pI6 %.*s\n", &nh->address, IFNAMSIZ,
1369 ifp->name);
d62a17ae 1370 }
eacd0828
YR
1371 if (use_json) {
1372 json_object_object_add(json_route, "nextHops",
1373 json_array_next_hops);
a49ef569 1374 json_object_object_add(json_routes, destination, json_route);
eacd0828
YR
1375 } else
1376 vty_out(vty, "\n");
508e53e2 1377}
718e3744 1378
d62a17ae 1379static void ospf6_route_show_table_summary(struct vty *vty,
eacd0828
YR
1380 struct ospf6_route_table *table,
1381 json_object *json, bool use_json)
508e53e2 1382{
d62a17ae 1383 struct ospf6_route *route, *prev = NULL;
1384 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1385 unsigned int number = 0;
1386 int nh_count = 0, nhinval = 0, ecmp = 0;
1387 int alternative = 0, destination = 0;
eacd0828 1388 char path_str[30];
d62a17ae 1389
1390 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1391 pathtype[i] = 0;
1392
1393 for (route = ospf6_route_head(table); route;
1394 route = ospf6_route_next(route)) {
1395 if (prev == NULL || !ospf6_route_is_same(prev, route))
1396 destination++;
1397 else
1398 alternative++;
1399 nh_count = ospf6_num_nexthops(route->nh_list);
1400 if (!nh_count)
1401 nhinval++;
1402 else if (nh_count > 1)
1403 ecmp++;
1404 pathtype[route->path.type]++;
1405 number++;
1406
1407 prev = route;
1408 }
1409
1410 assert(number == table->count);
eacd0828
YR
1411 if (use_json) {
1412 json_object_int_add(json, "numberOfOspfv3Routes", number);
1413 json_object_int_add(json, "numberOfDestination", destination);
1414 json_object_int_add(json, "numberOfAlternativeRoutes",
1415 alternative);
1416 json_object_int_add(json, "numberOfEcmp", ecmp);
1417 } else {
1418 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1419 vty_out(vty, "Number of Destination: %d\n", destination);
1420 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1421 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1422 }
d62a17ae 1423 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
eacd0828
YR
1424 if (use_json) {
1425 snprintf(path_str, sizeof(path_str), "numberOf%sRoutes",
1426 OSPF6_PATH_TYPE_JSON(i));
1427 json_object_int_add(json, path_str, pathtype[i]);
1428 } else
1429 vty_out(vty, "Number of %s routes: %d\n",
1430 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
d62a17ae 1431 }
4846ef64 1432}
1433
d62a17ae 1434static void ospf6_route_show_table_prefix(struct vty *vty,
1435 struct prefix *prefix,
eacd0828
YR
1436 struct ospf6_route_table *table,
1437 json_object *json, bool use_json)
4846ef64 1438{
d62a17ae 1439 struct ospf6_route *route;
a49ef569 1440 json_object *json_routes = NULL;
d62a17ae 1441
1442 route = ospf6_route_lookup(prefix, table);
1443 if (route == NULL)
1444 return;
1445
eacd0828 1446 if (use_json)
a49ef569 1447 json_routes = json_object_new_object();
d62a17ae 1448 ospf6_route_lock(route);
1449 while (route && ospf6_route_is_prefix(prefix, route)) {
1450 /* Specifying a prefix will always display details */
a49ef569 1451 ospf6_route_show_detail(vty, route, json_routes, use_json);
d62a17ae 1452 route = ospf6_route_next(route);
1453 }
eacd0828
YR
1454
1455 if (use_json)
a49ef569 1456 json_object_object_add(json, "routes", json_routes);
d62a17ae 1457 if (route)
1458 ospf6_route_unlock(route);
4846ef64 1459}
1460
d62a17ae 1461static void ospf6_route_show_table_address(struct vty *vty,
1462 struct prefix *prefix,
eacd0828
YR
1463 struct ospf6_route_table *table,
1464 json_object *json, bool use_json)
4846ef64 1465{
d62a17ae 1466 struct ospf6_route *route;
a49ef569 1467 json_object *json_routes = NULL;
d62a17ae 1468
1469 route = ospf6_route_lookup_bestmatch(prefix, table);
1470 if (route == NULL)
1471 return;
1472
eacd0828 1473 if (use_json)
a49ef569 1474 json_routes = json_object_new_object();
d62a17ae 1475 prefix = &route->prefix;
1476 ospf6_route_lock(route);
1477 while (route && ospf6_route_is_prefix(prefix, route)) {
1478 /* Specifying a prefix will always display details */
a49ef569 1479 ospf6_route_show_detail(vty, route, json_routes, use_json);
d62a17ae 1480 route = ospf6_route_next(route);
1481 }
eacd0828 1482 if (use_json)
a49ef569 1483 json_object_object_add(json, "routes", json_routes);
d62a17ae 1484 if (route)
1485 ospf6_route_unlock(route);
4846ef64 1486}
1487
d62a17ae 1488static void ospf6_route_show_table_match(struct vty *vty, int detail,
1489 struct prefix *prefix,
eacd0828
YR
1490 struct ospf6_route_table *table,
1491 json_object *json, bool use_json)
4846ef64 1492{
d62a17ae 1493 struct ospf6_route *route;
a49ef569 1494 json_object *json_routes = NULL;
eacd0828 1495
d62a17ae 1496 assert(prefix->family);
1497
1498 route = ospf6_route_match_head(prefix, table);
eacd0828 1499 if (use_json)
a49ef569 1500 json_routes = json_object_new_object();
d62a17ae 1501 while (route) {
1502 if (detail)
a49ef569 1503 ospf6_route_show_detail(vty, route, json_routes,
eacd0828 1504 use_json);
d62a17ae 1505 else
a49ef569 1506 ospf6_route_show(vty, route, json_routes, use_json);
d62a17ae 1507 route = ospf6_route_match_next(prefix, route);
1508 }
eacd0828 1509 if (use_json)
a49ef569 1510 json_object_object_add(json, "routes", json_routes);
4846ef64 1511}
1512
d62a17ae 1513static void ospf6_route_show_table_type(struct vty *vty, int detail,
d7c0a89a 1514 uint8_t type,
eacd0828
YR
1515 struct ospf6_route_table *table,
1516 json_object *json, bool use_json)
4846ef64 1517{
d62a17ae 1518 struct ospf6_route *route;
a49ef569 1519 json_object *json_routes = NULL;
d62a17ae 1520
1521 route = ospf6_route_head(table);
eacd0828 1522 if (use_json)
a49ef569 1523 json_routes = json_object_new_object();
d62a17ae 1524 while (route) {
1525 if (route->path.type == type) {
1526 if (detail)
a49ef569 1527 ospf6_route_show_detail(vty, route, json_routes,
eacd0828 1528 use_json);
d62a17ae 1529 else
a49ef569 1530 ospf6_route_show(vty, route, json_routes,
eacd0828 1531 use_json);
d62a17ae 1532 }
1533 route = ospf6_route_next(route);
1534 }
eacd0828 1535 if (use_json)
a49ef569 1536 json_object_object_add(json, "routes", json_routes);
4846ef64 1537}
1538
d62a17ae 1539static void ospf6_route_show_table(struct vty *vty, int detail,
eacd0828
YR
1540 struct ospf6_route_table *table,
1541 json_object *json, bool use_json)
4846ef64 1542{
d62a17ae 1543 struct ospf6_route *route;
a49ef569 1544 json_object *json_routes = NULL;
d62a17ae 1545
1546 route = ospf6_route_head(table);
eacd0828 1547 if (use_json)
a49ef569 1548 json_routes = json_object_new_object();
d62a17ae 1549 while (route) {
1550 if (detail)
a49ef569 1551 ospf6_route_show_detail(vty, route, json_routes,
eacd0828 1552 use_json);
d62a17ae 1553 else
a49ef569 1554 ospf6_route_show(vty, route, json_routes, use_json);
d62a17ae 1555 route = ospf6_route_next(route);
1556 }
eacd0828 1557 if (use_json)
a49ef569 1558 json_object_object_add(json, "routes", json_routes);
718e3744 1559}
1560
d62a17ae 1561int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1562 struct cmd_token **argv,
eacd0828 1563 struct ospf6_route_table *table, bool use_json)
718e3744 1564{
d62a17ae 1565 int summary = 0;
1566 int match = 0;
1567 int detail = 0;
1568 int slash = 0;
1569 int isprefix = 0;
1570 int i, ret;
1571 struct prefix prefix;
d7c0a89a 1572 uint8_t type = 0;
eacd0828
YR
1573 int arg_end = use_json ? (argc - 1) : argc;
1574 json_object *json = NULL;
d62a17ae 1575
1576 memset(&prefix, 0, sizeof(struct prefix));
1577
eacd0828
YR
1578 if (use_json)
1579 json = json_object_new_object();
1580
1581 for (i = argc_start; i < arg_end; i++) {
d62a17ae 1582 if (strmatch(argv[i]->text, "summary")) {
1583 summary++;
1584 continue;
1585 }
1586
1587 if (strmatch(argv[i]->text, "intra-area")) {
1588 type = OSPF6_PATH_TYPE_INTRA;
1589 continue;
1590 }
1591
1592 if (strmatch(argv[i]->text, "inter-area")) {
1593 type = OSPF6_PATH_TYPE_INTER;
1594 continue;
1595 }
1596
1597 if (strmatch(argv[i]->text, "external-1")) {
1598 type = OSPF6_PATH_TYPE_EXTERNAL1;
1599 continue;
1600 }
1601
1602 if (strmatch(argv[i]->text, "external-2")) {
1603 type = OSPF6_PATH_TYPE_EXTERNAL2;
1604 continue;
1605 }
1606
1607 if (strmatch(argv[i]->text, "detail")) {
1608 detail++;
1609 continue;
1610 }
1611
1612 if (strmatch(argv[i]->text, "match")) {
1613 match++;
1614 continue;
1615 }
1616
1617 ret = str2prefix(argv[i]->arg, &prefix);
1618 if (ret == 1 && prefix.family == AF_INET6) {
1619 isprefix++;
1620 if (strchr(argv[i]->arg, '/'))
1621 slash++;
1622 continue;
1623 }
eacd0828
YR
1624 if (use_json)
1625 json_object_string_add(json, "malformedArgument",
1626 argv[i]->arg);
1627 else
1628 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
d62a17ae 1629
d62a17ae 1630 return CMD_SUCCESS;
1631 }
1632
1633 /* Give summary of this route table */
1634 if (summary) {
eacd0828
YR
1635 ospf6_route_show_table_summary(vty, table, json, use_json);
1636 if (use_json) {
1637 vty_out(vty, "%s\n",
1638 json_object_to_json_string_ext(
1639 json, JSON_C_TO_STRING_PRETTY));
1640 json_object_free(json);
1641 }
d62a17ae 1642 return CMD_SUCCESS;
1643 }
1644
1645 /* Give exact prefix-match route */
1646 if (isprefix && !match) {
1647 /* If exact address, give best matching route */
1648 if (!slash)
eacd0828
YR
1649 ospf6_route_show_table_address(vty, &prefix, table,
1650 json, use_json);
d62a17ae 1651 else
eacd0828
YR
1652 ospf6_route_show_table_prefix(vty, &prefix, table, json,
1653 use_json);
1654
1655 if (use_json) {
1656 vty_out(vty, "%s\n",
1657 json_object_to_json_string_ext(
1658 json, JSON_C_TO_STRING_PRETTY));
1659 json_object_free(json);
1660 }
d62a17ae 1661 return CMD_SUCCESS;
1662 }
1663
1664 if (match)
eacd0828
YR
1665 ospf6_route_show_table_match(vty, detail, &prefix, table, json,
1666 use_json);
d62a17ae 1667 else if (type)
eacd0828
YR
1668 ospf6_route_show_table_type(vty, detail, type, table, json,
1669 use_json);
d62a17ae 1670 else
eacd0828 1671 ospf6_route_show_table(vty, detail, table, json, use_json);
d62a17ae 1672
eacd0828
YR
1673 if (use_json) {
1674 vty_out(vty, "%s\n",
1675 json_object_to_json_string_ext(
1676 json, JSON_C_TO_STRING_PRETTY));
1677 json_object_free(json);
1678 }
d62a17ae 1679 return CMD_SUCCESS;
4846ef64 1680}
508e53e2 1681
d62a17ae 1682static void ospf6_linkstate_show_header(struct vty *vty)
4846ef64 1683{
d62a17ae 1684 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1685 "Net-ID", "Rtr-Bits", "Options", "Cost");
4846ef64 1686}
1687
d62a17ae 1688static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
4846ef64 1689{
d7c0a89a 1690 uint32_t router, id;
d62a17ae 1691 char routername[16], idname[16], rbits[16], options[16];
1692
1693 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1694 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1695 id = ospf6_linkstate_prefix_id(&route->prefix);
1696 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1697
1698 ospf6_capability_printbuf(route->path.router_bits, rbits,
1699 sizeof(rbits));
1700 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1701
1702 if (ntohl(id))
1703 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1704 routername, idname, rbits, options,
1705 (unsigned long)route->path.cost);
1706 else
1707 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1708 routername, idname, rbits, options,
1709 (unsigned long)route->path.cost);
4846ef64 1710}
1711
1712
d62a17ae 1713static void ospf6_linkstate_show_table_exact(struct vty *vty,
1714 struct prefix *prefix,
1715 struct ospf6_route_table *table)
4846ef64 1716{
d62a17ae 1717 struct ospf6_route *route;
1718
1719 route = ospf6_route_lookup(prefix, table);
1720 if (route == NULL)
1721 return;
1722
1723 ospf6_route_lock(route);
1724 while (route && ospf6_route_is_prefix(prefix, route)) {
1725 /* Specifying a prefix will always display details */
eacd0828 1726 ospf6_route_show_detail(vty, route, NULL, false);
d62a17ae 1727 route = ospf6_route_next(route);
1728 }
1729 if (route)
1730 ospf6_route_unlock(route);
4846ef64 1731}
1732
d62a17ae 1733static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1734 struct ospf6_route_table *table)
4846ef64 1735{
d62a17ae 1736 struct ospf6_route *route;
1737
1738 if (!detail)
1739 ospf6_linkstate_show_header(vty);
1740
1741 route = ospf6_route_head(table);
1742 while (route) {
1743 if (detail)
eacd0828 1744 ospf6_route_show_detail(vty, route, NULL, false);
d62a17ae 1745 else
1746 ospf6_linkstate_show(vty, route);
1747 route = ospf6_route_next(route);
1748 }
718e3744 1749}
1750
d62a17ae 1751int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1752 struct cmd_token **argv,
1753 struct ospf6_route_table *table)
718e3744 1754{
d62a17ae 1755 int detail = 0;
1756 int is_id = 0;
1757 int is_router = 0;
1758 int i, ret;
1759 struct prefix router, id, prefix;
1760
1761 memset(&router, 0, sizeof(struct prefix));
1762 memset(&id, 0, sizeof(struct prefix));
1763 memset(&prefix, 0, sizeof(struct prefix));
1764
1765 for (i = idx_ipv4; i < argc; i++) {
1766 if (strmatch(argv[i]->text, "detail")) {
1767 detail++;
1768 continue;
1769 }
1770
1771 if (!is_router) {
1772 ret = str2prefix(argv[i]->arg, &router);
1773 if (ret == 1 && router.family == AF_INET) {
1774 is_router++;
1775 continue;
1776 }
1777 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1778 return CMD_SUCCESS;
1779 }
1780
1781 if (!is_id) {
1782 ret = str2prefix(argv[i]->arg, &id);
1783 if (ret == 1 && id.family == AF_INET) {
1784 is_id++;
1785 continue;
1786 }
1787 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1788 return CMD_SUCCESS;
1789 }
1790
1791 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1792 return CMD_SUCCESS;
1793 }
1794
1795 if (is_router)
1796 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1797 id.u.prefix4.s_addr, &prefix);
1798
1799 if (prefix.family)
1800 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1801 else
1802 ospf6_linkstate_show_table(vty, detail, table);
1803
1804 return CMD_SUCCESS;
508e53e2 1805}
718e3744 1806
4846ef64 1807
d62a17ae 1808void ospf6_brouter_show_header(struct vty *vty)
6452df09 1809{
d62a17ae 1810 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1811 "Options", "Path-Type", "Area");
6452df09 1812}
1813
d62a17ae 1814void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
6452df09 1815{
d7c0a89a 1816 uint32_t adv_router;
d62a17ae 1817 char adv[16], rbits[16], options[16], area[16];
1818
1819 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1820 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1821 ospf6_capability_printbuf(route->path.router_bits, rbits,
1822 sizeof(rbits));
1823 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1824 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1825
1826 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1827 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1828 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1829 OSPF6_PATH_TYPE_NAME(route->path.type), area);
6452df09 1830}
1831
d5cb3508
YR
1832DEFPY(debug_ospf6_route,
1833 debug_ospf6_route_cmd,
1834 "[no$no] debug ospf6 route <all|table|intra-area|inter-area|memory>",
1835 NO_STR
1836 DEBUG_STR
1837 OSPF6_STR
1838 "Debug routes\n"
1839 "Debug for all types of route calculation\n"
1840 "Debug route table calculation\n"
1841 "Debug intra-area route calculation\n"
1842 "Debug inter-area route calculation\n"
1843 "Debug route memory use\n")
508e53e2 1844{
d5cb3508 1845 int idx_type;
d62a17ae 1846 unsigned char level = 0;
1847
d5cb3508 1848 idx_type = ((no) ? 4 : 3);
d62a17ae 1849
d5cb3508
YR
1850 if (!strcmp(argv[idx_type]->text, "all"))
1851 level = OSPF6_DEBUG_ROUTE_ALL;
1852 else if (!strcmp(argv[idx_type]->text, "table"))
d62a17ae 1853 level = OSPF6_DEBUG_ROUTE_TABLE;
8034beff 1854 else if (!strcmp(argv[idx_type]->text, "intra-area"))
d62a17ae 1855 level = OSPF6_DEBUG_ROUTE_INTRA;
8034beff 1856 else if (!strcmp(argv[idx_type]->text, "inter-area"))
d62a17ae 1857 level = OSPF6_DEBUG_ROUTE_INTER;
8034beff 1858 else if (!strcmp(argv[idx_type]->text, "memory"))
d62a17ae 1859 level = OSPF6_DEBUG_ROUTE_MEMORY;
d5cb3508
YR
1860
1861 if (no)
1862 OSPF6_DEBUG_ROUTE_OFF(level);
1863 else
1864 OSPF6_DEBUG_ROUTE_ON(level);
d62a17ae 1865 return CMD_SUCCESS;
718e3744 1866}
1867
d62a17ae 1868int config_write_ospf6_debug_route(struct vty *vty)
508e53e2 1869{
d5cb3508
YR
1870 if (IS_OSPF6_DEBUG_ROUTE(ALL) == OSPF6_DEBUG_ROUTE_ALL) {
1871 vty_out(vty, "debug ospf6 route all\n");
1872 return 0;
1873 }
d62a17ae 1874 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1875 vty_out(vty, "debug ospf6 route table\n");
1876 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1877 vty_out(vty, "debug ospf6 route intra-area\n");
1878 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1879 vty_out(vty, "debug ospf6 route inter-area\n");
6f7af48c
CS
1880 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1881 vty_out(vty, "debug ospf6 route memory\n");
1882
d62a17ae 1883 return 0;
508e53e2 1884}
1885
d62a17ae 1886void install_element_ospf6_debug_route(void)
508e53e2 1887{
d62a17ae 1888 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
d62a17ae 1889 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
508e53e2 1890}