]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_route.c
Merge pull request #1502 from chiragshah6/ospf_vrf_dev
[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"
718e3744 40
508e53e2 41unsigned char conf_debug_ospf6_route = 0;
718e3744 42
d62a17ae 43static char *ospf6_route_table_name(struct ospf6_route_table *table)
cf1ce250 44{
d62a17ae 45 static char name[64];
46 switch (table->scope_type) {
47 case OSPF6_SCOPE_TYPE_GLOBAL: {
48 switch (table->table_type) {
49 case OSPF6_TABLE_TYPE_ROUTES:
50 snprintf(name, sizeof(name), "global route table");
51 break;
52 case OSPF6_TABLE_TYPE_BORDER_ROUTERS:
53 snprintf(name, sizeof(name), "global brouter table");
54 break;
55 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES:
56 snprintf(name, sizeof(name), "global external table");
57 break;
58 default:
59 snprintf(name, sizeof(name), "global unknown table");
60 break;
61 }
62 } break;
63
64 case OSPF6_SCOPE_TYPE_AREA: {
65 struct ospf6_area *oa = (struct ospf6_area *)table->scope;
66 switch (table->table_type) {
67 case OSPF6_TABLE_TYPE_SPF_RESULTS:
68 snprintf(name, sizeof(name), "area %s spf table",
69 oa->name);
70 break;
71 case OSPF6_TABLE_TYPE_ROUTES:
72 snprintf(name, sizeof(name), "area %s route table",
73 oa->name);
74 break;
75 case OSPF6_TABLE_TYPE_PREFIX_RANGES:
76 snprintf(name, sizeof(name), "area %s range table",
77 oa->name);
78 break;
79 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES:
80 snprintf(name, sizeof(name),
81 "area %s summary prefix table", oa->name);
82 break;
83 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS:
84 snprintf(name, sizeof(name),
85 "area %s summary router table", oa->name);
86 break;
87 default:
88 snprintf(name, sizeof(name), "area %s unknown table",
89 oa->name);
90 break;
91 }
92 } break;
93
94 case OSPF6_SCOPE_TYPE_INTERFACE: {
95 struct ospf6_interface *oi =
96 (struct ospf6_interface *)table->scope;
97 switch (table->table_type) {
98 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES:
99 snprintf(name, sizeof(name),
100 "interface %s connected table",
101 oi->interface->name);
102 break;
103 default:
104 snprintf(name, sizeof(name),
105 "interface %s unknown table",
106 oi->interface->name);
107 break;
108 }
109 } break;
110
111 default: {
112 switch (table->table_type) {
113 case OSPF6_TABLE_TYPE_SPF_RESULTS:
114 snprintf(name, sizeof(name), "temporary spf table");
115 break;
116 default:
117 snprintf(name, sizeof(name), "temporary unknown table");
118 break;
119 }
120 } break;
121 }
122 return name;
cf1ce250
PJ
123}
124
d62a17ae 125void ospf6_linkstate_prefix(u_int32_t adv_router, u_int32_t id,
126 struct prefix *prefix)
508e53e2 127{
d62a17ae 128 memset(prefix, 0, sizeof(struct prefix));
129 prefix->family = AF_INET6;
130 prefix->prefixlen = 64;
131 memcpy(&prefix->u.prefix6.s6_addr[0], &adv_router, 4);
132 memcpy(&prefix->u.prefix6.s6_addr[4], &id, 4);
508e53e2 133}
718e3744 134
d62a17ae 135void ospf6_linkstate_prefix2str(struct prefix *prefix, char *buf, int size)
718e3744 136{
d62a17ae 137 u_int32_t adv_router, id;
138 char adv_router_str[16], id_str[16];
139 memcpy(&adv_router, &prefix->u.prefix6.s6_addr[0], 4);
140 memcpy(&id, &prefix->u.prefix6.s6_addr[4], 4);
141 inet_ntop(AF_INET, &adv_router, adv_router_str, sizeof(adv_router_str));
142 inet_ntop(AF_INET, &id, id_str, sizeof(id_str));
143 if (ntohl(id))
144 snprintf(buf, size, "%s Net-ID: %s", adv_router_str, id_str);
145 else
146 snprintf(buf, size, "%s", adv_router_str);
508e53e2 147}
718e3744 148
508e53e2 149/* Global strings for logging */
d62a17ae 150const char *ospf6_dest_type_str[OSPF6_DEST_TYPE_MAX] = {
151 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
152};
718e3744 153
d62a17ae 154const char *ospf6_dest_type_substr[OSPF6_DEST_TYPE_MAX] = {
155 "?", "R", "N", "D", "L", "A",
156};
718e3744 157
d62a17ae 158const char *ospf6_path_type_str[OSPF6_PATH_TYPE_MAX] = {
159 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
160};
718e3744 161
d62a17ae 162const char *ospf6_path_type_substr[OSPF6_PATH_TYPE_MAX] = {
163 "??", "IA", "IE", "E1", "E2",
164};
718e3744 165
718e3744 166
d62a17ae 167struct ospf6_nexthop *ospf6_nexthop_create(void)
c3c0ac83 168{
d62a17ae 169 struct ospf6_nexthop *nh;
c3c0ac83 170
d62a17ae 171 nh = XCALLOC(MTYPE_OSPF6_NEXTHOP, sizeof(struct ospf6_nexthop));
172 return nh;
c3c0ac83
DS
173}
174
d62a17ae 175void ospf6_nexthop_delete(struct ospf6_nexthop *nh)
c3c0ac83 176{
d62a17ae 177 if (nh)
178 XFREE(MTYPE_OSPF6_NEXTHOP, nh);
c3c0ac83
DS
179}
180
d62a17ae 181void ospf6_clear_nexthops(struct list *nh_list)
c3c0ac83 182{
d62a17ae 183 struct listnode *node;
184 struct ospf6_nexthop *nh;
185
186 if (nh_list) {
187 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
188 ospf6_nexthop_clear(nh);
189 }
c3c0ac83
DS
190}
191
192static struct ospf6_nexthop *
d62a17ae 193ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
c3c0ac83 194{
d62a17ae 195 struct listnode *node;
196 struct ospf6_nexthop *nh;
197
198 if (nh_list && nh_match) {
199 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
200 if (ospf6_nexthop_is_same(nh, nh_match))
201 return (nh);
202 }
c3c0ac83 203 }
c3c0ac83 204
d62a17ae 205 return (NULL);
c3c0ac83
DS
206}
207
d62a17ae 208void ospf6_copy_nexthops(struct list *dst, struct list *src)
c3c0ac83 209{
d62a17ae 210 struct ospf6_nexthop *nh_new, *nh;
211 struct listnode *node;
212
213 if (dst && src) {
214 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
215 if (ospf6_nexthop_is_set(nh)) {
216 nh_new = ospf6_nexthop_create();
217 ospf6_nexthop_copy(nh_new, nh);
218 listnode_add(dst, nh_new);
219 }
220 }
c3c0ac83 221 }
c3c0ac83
DS
222}
223
d62a17ae 224void ospf6_merge_nexthops(struct list *dst, struct list *src)
c3c0ac83 225{
d62a17ae 226 struct listnode *node;
227 struct ospf6_nexthop *nh, *nh_new;
228
229 if (src && dst) {
230 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
231 if (!ospf6_route_find_nexthop(dst, nh)) {
232 nh_new = ospf6_nexthop_create();
233 ospf6_nexthop_copy(nh_new, nh);
234 listnode_add(dst, nh_new);
235 }
236 }
c3c0ac83 237 }
c3c0ac83
DS
238}
239
d62a17ae 240int ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
c3c0ac83 241{
d62a17ae 242 struct listnode *anode, *bnode;
243 struct ospf6_nexthop *anh, *bnh;
244
245 if (a && b) {
246 if (listcount(a->nh_list) == listcount(b->nh_list)) {
247 for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
248 for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
249 bnh))
250 if (!ospf6_nexthop_is_same(anh, bnh))
251 return (1);
252 }
253 return (0);
254 } else
255 return (1);
c3c0ac83 256 }
d62a17ae 257 /* One of the routes doesn't exist ? */
c3c0ac83 258 return (1);
c3c0ac83
DS
259}
260
d62a17ae 261int ospf6_num_nexthops(struct list *nh_list)
c3c0ac83 262{
d62a17ae 263 return (listcount(nh_list));
c3c0ac83
DS
264}
265
d62a17ae 266void ospf6_add_nexthop(struct list *nh_list, int ifindex, struct in6_addr *addr)
c3c0ac83 267{
d62a17ae 268 struct ospf6_nexthop *nh;
269 struct ospf6_nexthop nh_match;
270
271 if (nh_list) {
272 nh_match.ifindex = ifindex;
273 if (addr != NULL)
274 memcpy(&nh_match.address, addr,
275 sizeof(struct in6_addr));
276 else
277 memset(&nh_match.address, 0, sizeof(struct in6_addr));
278
279 if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
280 nh = ospf6_nexthop_create();
281 ospf6_nexthop_copy(nh, &nh_match);
282 listnode_add(nh_list, nh);
283 }
c3c0ac83 284 }
c3c0ac83
DS
285}
286
d62a17ae 287void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
5afa1c6b 288 struct zapi_nexthop nexthops[],
d62a17ae 289 int entries)
c3c0ac83 290{
d62a17ae 291 struct ospf6_nexthop *nh;
292 struct listnode *node;
293 char buf[64];
294 int i;
295
296 if (route) {
297 i = 0;
298 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
299 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
300 const char *ifname;
301 inet_ntop(AF_INET6, &nh->address, buf,
302 sizeof(buf));
303 ifname = ifindex2ifname(nh->ifindex,
304 VRF_DEFAULT);
305 zlog_debug(" nexthop: %s%%%.*s(%d)", buf,
306 IFNAMSIZ, ifname, nh->ifindex);
307 }
b30f3b91
RW
308 if (i >= entries)
309 return;
310
311 nexthops[i].ifindex = nh->ifindex;
312 if (!IN6_IS_ADDR_UNSPECIFIED(&nh->address)) {
5afa1c6b 313 nexthops[i].gate.ipv6 = nh->address;
5afa1c6b 314 nexthops[i].type = NEXTHOP_TYPE_IPV6_IFINDEX;
b30f3b91
RW
315 } else
316 nexthops[i].type = NEXTHOP_TYPE_IFINDEX;
317 i++;
d62a17ae 318 }
c3c0ac83 319 }
c3c0ac83
DS
320}
321
d62a17ae 322int ospf6_route_get_first_nh_index(struct ospf6_route *route)
c3c0ac83 323{
d62a17ae 324 struct ospf6_nexthop *nh;
c3c0ac83 325
d62a17ae 326 if (route) {
327 if ((nh = (struct ospf6_nexthop *)listhead(route->nh_list)))
328 return (nh->ifindex);
329 }
c3c0ac83 330
d62a17ae 331 return (-1);
c3c0ac83
DS
332}
333
d107621d
CS
334static int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
335{
336 if ((a)->ifindex == (b)->ifindex &&
337 IN6_ARE_ADDR_EQUAL(&(a)->address, &(b)->address))
338 return 1;
339 return 0;
340}
341
d62a17ae 342struct ospf6_route *ospf6_route_create(void)
718e3744 343{
d62a17ae 344 struct ospf6_route *route;
345 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
346 route->nh_list = list_new();
d107621d
CS
347 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
348 route->nh_list->del = (void (*) (void *))ospf6_nexthop_delete;
d62a17ae 349 return route;
508e53e2 350}
718e3744 351
d62a17ae 352void ospf6_route_delete(struct ospf6_route *route)
508e53e2 353{
d62a17ae 354 if (route) {
d107621d 355 if (route->nh_list)
affe9e99 356 list_delete_and_null(&route->nh_list);
d62a17ae 357 XFREE(MTYPE_OSPF6_ROUTE, route);
358 }
718e3744 359}
360
d62a17ae 361struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
718e3744 362{
d62a17ae 363 struct ospf6_route *new;
364
365 new = ospf6_route_create();
366 new->type = route->type;
367 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
368 new->installed = route->installed;
369 new->changed = route->changed;
370 new->flag = route->flag;
371 new->route_option = route->route_option;
372 new->linkstate_id = route->linkstate_id;
373 new->path = route->path;
374 ospf6_copy_nexthops(new->nh_list, route->nh_list);
375 new->rnode = NULL;
376 new->prev = NULL;
377 new->next = NULL;
378 new->table = NULL;
379 new->lock = 0;
380 return new;
508e53e2 381}
718e3744 382
d62a17ae 383void ospf6_route_lock(struct ospf6_route *route)
508e53e2 384{
d62a17ae 385 route->lock++;
508e53e2 386}
718e3744 387
d62a17ae 388void ospf6_route_unlock(struct ospf6_route *route)
508e53e2 389{
d62a17ae 390 assert(route->lock > 0);
391 route->lock--;
392 if (route->lock == 0) {
393 /* Can't detach from the table until here
394 because ospf6_route_next () will use
395 the 'route->table' pointer for logging */
396 route->table = NULL;
397 ospf6_route_delete(route);
398 }
508e53e2 399}
718e3744 400
508e53e2 401/* Route compare function. If ra is more preferred, it returns
402 less than 0. If rb is more preferred returns greater than 0.
403 Otherwise (neither one is preferred), returns 0 */
d62a17ae 404int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
508e53e2 405{
d62a17ae 406 assert(ospf6_route_is_same(ra, rb));
407 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
408 && ra->path.type < OSPF6_PATH_TYPE_MAX);
409 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
410 && rb->path.type < OSPF6_PATH_TYPE_MAX);
411
412 if (ra->type != rb->type)
413 return (ra->type - rb->type);
414
415 if (ra->path.area_id != rb->path.area_id)
416 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
417
418 if (ra->path.type != rb->path.type)
419 return (ra->path.type - rb->path.type);
420
421 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
422 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
423 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
424 } else {
425 if (ra->path.cost != rb->path.cost)
426 return (ra->path.cost - rb->path.cost);
427 }
428
429 return 0;
718e3744 430}
431
d62a17ae 432struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
433 struct ospf6_route_table *table)
718e3744 434{
d62a17ae 435 struct route_node *node;
436 struct ospf6_route *route;
718e3744 437
d62a17ae 438 node = route_node_lookup(table->table, prefix);
439 if (node == NULL)
440 return NULL;
508e53e2 441
d62a17ae 442 route = (struct ospf6_route *)node->info;
d107621d 443 route_unlock_node(node); /* to free the lookup lock */
d62a17ae 444 return route;
508e53e2 445}
718e3744 446
508e53e2 447struct ospf6_route *
d62a17ae 448ospf6_route_lookup_identical(struct ospf6_route *route,
449 struct ospf6_route_table *table)
508e53e2 450{
d62a17ae 451 struct ospf6_route *target;
452
453 for (target = ospf6_route_lookup(&route->prefix, table); target;
454 target = target->next) {
455 if (ospf6_route_is_identical(target, route))
456 return target;
457 }
458 return NULL;
718e3744 459}
460
508e53e2 461struct ospf6_route *
d62a17ae 462ospf6_route_lookup_bestmatch(struct prefix *prefix,
463 struct ospf6_route_table *table)
718e3744 464{
d62a17ae 465 struct route_node *node;
466 struct ospf6_route *route;
718e3744 467
d62a17ae 468 node = route_node_match(table->table, prefix);
469 if (node == NULL)
470 return NULL;
471 route_unlock_node(node);
718e3744 472
d62a17ae 473 route = (struct ospf6_route *)node->info;
474 return route;
718e3744 475}
476
e39d0538 477#ifdef DEBUG
d62a17ae 478static void route_table_assert(struct ospf6_route_table *table)
718e3744 479{
d62a17ae 480 struct ospf6_route *prev, *r, *next;
481 char buf[PREFIX2STR_BUFFER];
482 unsigned int link_error = 0, num = 0;
483
484 r = ospf6_route_head(table);
485 prev = NULL;
486 while (r) {
487 if (r->prev != prev)
488 link_error++;
489
490 next = ospf6_route_next(r);
491
492 if (r->next != next)
493 link_error++;
494
495 prev = r;
496 r = next;
497 }
498
499 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
500 num++;
501
502 if (link_error == 0 && num == table->count)
503 return;
504
505 zlog_err("PANIC !!");
506 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table);
507 zlog_debug("table count = %d, real number = %d", table->count, num);
508 zlog_debug("DUMP START");
509 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r)) {
510 prefix2str(&r->prefix, buf, sizeof(buf));
511 zlog_info("%p<-[%p]->%p : %s", r->prev, r, r->next, buf);
512 }
513 zlog_debug("DUMP END");
514
515 assert(link_error == 0 && num == table->count);
718e3744 516}
cf1ce250 517#define ospf6_route_table_assert(t) (route_table_assert (t))
508e53e2 518#else
cf1ce250 519#define ospf6_route_table_assert(t) ((void) 0)
e39d0538 520#endif /*DEBUG*/
718e3744 521
d62a17ae 522struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
523 struct ospf6_route_table *table)
718e3744 524{
d62a17ae 525 struct route_node *node, *nextnode, *prevnode;
526 struct ospf6_route *current = NULL;
527 struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
528 char buf[PREFIX2STR_BUFFER];
529 struct timeval now;
530
531 assert(route->rnode == NULL);
532 assert(route->lock == 0);
533 assert(route->next == NULL);
534 assert(route->prev == NULL);
535
536 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
537 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
538 else
539 prefix2str(&route->prefix, buf, sizeof(buf));
540
541 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
542 zlog_debug("%s %p: route add %p: %s",
543 ospf6_route_table_name(table), (void *)table,
544 (void *)route, buf);
545 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
546 zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
547 buf);
548
549 monotime(&now);
550
551 node = route_node_get(table->table, &route->prefix);
552 route->rnode = node;
553
554 /* find place to insert */
555 for (current = node->info; current; current = current->next) {
556 if (!ospf6_route_is_same(current, route))
557 next = current;
558 else if (current->type != route->type)
559 prev = current;
560 else if (ospf6_route_is_same_origin(current, route))
561 old = current;
562 else if (ospf6_route_cmp(current, route) > 0)
563 next = current;
564 else
565 prev = current;
566
567 if (old || next)
568 break;
569 }
718e3744 570
d62a17ae 571 if (old) {
572 /* if route does not actually change, return unchanged */
573 if (ospf6_route_is_identical(old, route)) {
574 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
575 zlog_debug(
576 "%s %p: route add %p: needless update of %p",
577 ospf6_route_table_name(table),
578 (void *)table, (void *)route,
579 (void *)old);
580 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
581 zlog_debug("%s: route add: needless update",
582 ospf6_route_table_name(table));
583
584 ospf6_route_delete(route);
585 SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
586 ospf6_route_table_assert(table);
587
d107621d
CS
588 /* to free the lookup lock */
589 route_unlock_node(node);
d62a17ae 590 return old;
591 }
592
593 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
594 zlog_debug("%s %p: route add %p: update of %p",
595 ospf6_route_table_name(table), (void *)table,
596 (void *)route, (void *)old);
597 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
598 zlog_debug("%s: route add: update",
599 ospf6_route_table_name(table));
600
601 /* replace old one if exists */
602 if (node->info == old) {
603 node->info = route;
604 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
605 }
606
607 if (old->prev)
608 old->prev->next = route;
609 route->prev = old->prev;
610 if (old->next)
611 old->next->prev = route;
612 route->next = old->next;
613
614 route->installed = old->installed;
615 route->changed = now;
616 assert(route->table == NULL);
617 route->table = table;
618
619 ospf6_route_unlock(old); /* will be deleted later */
620 ospf6_route_lock(route);
621
622 SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
623 ospf6_route_table_assert(table);
624
625 if (table->hook_add)
626 (*table->hook_add)(route);
627
628 return route;
629 }
630
631 /* insert if previous or next node found */
632 if (prev || next) {
633 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
634 zlog_debug(
dfac5d39 635 "%s %p: route add %p: another path: prev %p, next %p node refcount %u",
d62a17ae 636 ospf6_route_table_name(table), (void *)table,
d107621d
CS
637 (void *)route, (void *)prev, (void *)next,
638 node->lock);
d62a17ae 639 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
640 zlog_debug("%s: route add: another path found",
641 ospf6_route_table_name(table));
642
643 if (prev == NULL)
644 prev = next->prev;
645 if (next == NULL)
646 next = prev->next;
647
648 if (prev)
649 prev->next = route;
650 route->prev = prev;
651 if (next)
652 next->prev = route;
653 route->next = next;
654
655 if (node->info == next) {
656 assert(next->rnode == node);
657 node->info = route;
658 UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
659 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
660 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
661 zlog_info(
662 "%s %p: route add %p: replacing previous best: %p",
663 ospf6_route_table_name(table),
664 (void *)table, (void *)route,
665 (void *)next);
666 }
667
668 route->installed = now;
669 route->changed = now;
670 assert(route->table == NULL);
671 route->table = table;
672
673 ospf6_route_lock(route);
674 table->count++;
675 ospf6_route_table_assert(table);
676
677 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
678 if (table->hook_add)
679 (*table->hook_add)(route);
680
681 return route;
682 }
683
684 /* Else, this is the brand new route regarding to the prefix */
685 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
dd2395e1 686 zlog_debug("%s %p: route add %p %s : brand new route",
d62a17ae 687 ospf6_route_table_name(table), (void *)table,
dd2395e1 688 (void *)route, buf);
d62a17ae 689 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
690 zlog_debug("%s: route add: brand new route",
691 ospf6_route_table_name(table));
692
693 assert(node->info == NULL);
694 node->info = route;
695 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
696 ospf6_route_lock(route);
697 route->installed = now;
698 route->changed = now;
699 assert(route->table == NULL);
700 route->table = table;
701
702 /* lookup real existing next route */
703 nextnode = node;
704 route_lock_node(nextnode);
705 do {
706 nextnode = route_next(nextnode);
707 } while (nextnode && nextnode->info == NULL);
708
709 /* set next link */
710 if (nextnode == NULL)
711 route->next = NULL;
712 else {
713 route_unlock_node(nextnode);
714
715 next = nextnode->info;
716 route->next = next;
717 next->prev = route;
718 }
719
720 /* lookup real existing prev route */
721 prevnode = node;
722 route_lock_node(prevnode);
723 do {
724 prevnode = route_prev(prevnode);
725 } while (prevnode && prevnode->info == NULL);
726
727 /* set prev link */
728 if (prevnode == NULL)
729 route->prev = NULL;
730 else {
731 route_unlock_node(prevnode);
732
733 prev = prevnode->info;
734 while (prev->next && ospf6_route_is_same(prev, prev->next))
735 prev = prev->next;
736 route->prev = prev;
737 prev->next = route;
738 }
739
740 table->count++;
741 ospf6_route_table_assert(table);
742
743 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
744 if (table->hook_add)
745 (*table->hook_add)(route);
746
747 return route;
508e53e2 748}
718e3744 749
d62a17ae 750void ospf6_route_remove(struct ospf6_route *route,
751 struct ospf6_route_table *table)
508e53e2 752{
d62a17ae 753 struct route_node *node;
754 struct ospf6_route *current;
755 char buf[PREFIX2STR_BUFFER];
756
757 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
758 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
759 else
760 prefix2str(&route->prefix, buf, sizeof(buf));
761
762 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
dd2395e1 763 zlog_debug("%s %p: route remove %p: %s refcount %u",
d62a17ae 764 ospf6_route_table_name(table), (void *)table,
dd2395e1 765 (void *)route, buf, route->lock);
d62a17ae 766 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
767 zlog_debug("%s: route remove: %s",
768 ospf6_route_table_name(table), buf);
769
770 node = route_node_lookup(table->table, &route->prefix);
771 assert(node);
772
773 /* find the route to remove, making sure that the route pointer
774 is from the route table. */
775 current = node->info;
d107621d 776 while (current && current != route)
d62a17ae 777 current = current->next;
d107621d 778
d62a17ae 779 assert(current == route);
780
781 /* adjust doubly linked list */
782 if (route->prev)
783 route->prev->next = route->next;
784 if (route->next)
785 route->next->prev = route->prev;
786
787 if (node->info == route) {
788 if (route->next && route->next->rnode == node) {
789 node->info = route->next;
790 SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
d107621d
CS
791 } else {
792 node->info = NULL;
793 route->rnode = NULL;
794 route_unlock_node(node); /* to free the original lock */
795 }
d62a17ae 796 }
508e53e2 797
d107621d 798 route_unlock_node(node); /* to free the lookup lock */
d62a17ae 799 table->count--;
800 ospf6_route_table_assert(table);
718e3744 801
d62a17ae 802 SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
508e53e2 803
dd2395e1 804 /* Note hook_remove may call ospf6_route_remove */
d62a17ae 805 if (table->hook_remove)
806 (*table->hook_remove)(route);
cf1ce250 807
d62a17ae 808 ospf6_route_unlock(route);
809}
cf1ce250 810
d62a17ae 811struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
812{
813 struct route_node *node;
814 struct ospf6_route *route;
815
816 node = route_top(table->table);
817 if (node == NULL)
818 return NULL;
819
820 /* skip to the real existing entry */
821 while (node && node->info == NULL)
822 node = route_next(node);
823 if (node == NULL)
824 return NULL;
825
826 route_unlock_node(node);
827 assert(node->info);
828
829 route = (struct ospf6_route *)node->info;
830 assert(route->prev == NULL);
831 assert(route->table == table);
832 ospf6_route_lock(route);
833
834 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
835 zlog_info("%s %p: route head: %p<-[%p]->%p",
836 ospf6_route_table_name(table), (void *)table,
837 (void *)route->prev, (void *)route,
838 (void *)route->next);
839
840 return route;
718e3744 841}
842
d62a17ae 843struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
718e3744 844{
d62a17ae 845 struct ospf6_route *next = route->next;
718e3744 846
d62a17ae 847 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
848 zlog_info("%s %p: route next: %p<-[%p]->%p",
849 ospf6_route_table_name(route->table),
850 (void *)route->table, (void *)route->prev,
851 (void *)route, (void *)route->next);
cf1ce250 852
d62a17ae 853 ospf6_route_unlock(route);
854 if (next)
855 ospf6_route_lock(next);
718e3744 856
d62a17ae 857 return next;
508e53e2 858}
718e3744 859
d62a17ae 860struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
508e53e2 861{
d62a17ae 862 struct route_node *rnode;
863 struct ospf6_route *next;
864
865 ospf6_route_unlock(route);
866
867 rnode = route->rnode;
868 route_lock_node(rnode);
869 rnode = route_next(rnode);
870 while (rnode && rnode->info == NULL)
871 rnode = route_next(rnode);
872 if (rnode == NULL)
873 return NULL;
874 route_unlock_node(rnode);
875
876 assert(rnode->info);
877 next = (struct ospf6_route *)rnode->info;
878 ospf6_route_lock(next);
879 return next;
508e53e2 880}
718e3744 881
d62a17ae 882struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
883 struct ospf6_route_table *table)
508e53e2 884{
d62a17ae 885 struct route_node *node;
886 struct ospf6_route *route;
887
888 /* Walk down tree. */
889 node = table->table->top;
890 while (node && node->p.prefixlen < prefix->prefixlen
891 && prefix_match(&node->p, prefix))
892 node = node->link[prefix_bit(&prefix->u.prefix,
893 node->p.prefixlen)];
894
895 if (node)
896 route_lock_node(node);
897 while (node && node->info == NULL)
898 node = route_next(node);
899 if (node == NULL)
900 return NULL;
901 route_unlock_node(node);
902
903 if (!prefix_match(prefix, &node->p))
904 return NULL;
905
906 route = node->info;
907 ospf6_route_lock(route);
908 return route;
508e53e2 909}
718e3744 910
d62a17ae 911struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
912 struct ospf6_route *route)
508e53e2 913{
d62a17ae 914 struct ospf6_route *next;
508e53e2 915
d62a17ae 916 next = ospf6_route_next(route);
917 if (next && !prefix_match(prefix, &next->prefix)) {
918 ospf6_route_unlock(next);
919 next = NULL;
920 }
508e53e2 921
d62a17ae 922 return next;
718e3744 923}
924
d62a17ae 925void ospf6_route_remove_all(struct ospf6_route_table *table)
718e3744 926{
d62a17ae 927 struct ospf6_route *route;
928 for (route = ospf6_route_head(table); route;
929 route = ospf6_route_next(route))
930 ospf6_route_remove(route, table);
718e3744 931}
932
d62a17ae 933struct ospf6_route_table *ospf6_route_table_create(int s, int t)
718e3744 934{
d62a17ae 935 struct ospf6_route_table *new;
936 new = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route_table));
937 new->table = route_table_init();
938 new->scope_type = s;
939 new->table_type = t;
940 return new;
718e3744 941}
942
d62a17ae 943void ospf6_route_table_delete(struct ospf6_route_table *table)
718e3744 944{
d62a17ae 945 ospf6_route_remove_all(table);
d107621d 946 bf_free(table->idspace);
d62a17ae 947 route_table_finish(table->table);
948 XFREE(MTYPE_OSPF6_ROUTE, table);
718e3744 949}
950
6b0655a2 951
508e53e2 952/* VTY commands */
d62a17ae 953void ospf6_route_show(struct vty *vty, struct ospf6_route *route)
718e3744 954{
d62a17ae 955 int i;
956 char destination[PREFIX2STR_BUFFER], nexthop[64];
68bfcc05 957 char duration[64];
d62a17ae 958 const char *ifname;
959 struct timeval now, res;
960 struct listnode *node;
961 struct ospf6_nexthop *nh;
962
963 monotime(&now);
964 timersub(&now, &route->changed, &res);
965 timerstring(&res, duration, sizeof(duration));
966
967 /* destination */
968 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
969 ospf6_linkstate_prefix2str(&route->prefix, destination,
970 sizeof(destination));
971 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
972 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
973 destination, sizeof(destination));
974 else
975 prefix2str(&route->prefix, destination, sizeof(destination));
976
977 i = 0;
978 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
979 /* nexthop */
980 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
981 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
982
983 if (!i) {
984 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
985 (ospf6_route_is_best(route) ? '*' : ' '),
986 OSPF6_DEST_TYPE_SUBSTR(route->type),
987 OSPF6_PATH_TYPE_SUBSTR(route->path.type),
988 destination, nexthop, IFNAMSIZ, ifname,
989 duration);
990 i++;
991 } else
992 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
993 "", "", "", nexthop, IFNAMSIZ, ifname, "");
c3c0ac83 994 }
718e3744 995}
996
d62a17ae 997void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route)
718e3744 998{
d62a17ae 999 const char *ifname;
1000 char destination[PREFIX2STR_BUFFER], nexthop[64];
1001 char area_id[16], id[16], adv_router[16], capa[16], options[16];
1002 struct timeval now, res;
68bfcc05 1003 char duration[64];
d62a17ae 1004 struct listnode *node;
1005 struct ospf6_nexthop *nh;
1006
1007 monotime(&now);
1008
1009 /* destination */
1010 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1011 ospf6_linkstate_prefix2str(&route->prefix, destination,
1012 sizeof(destination));
1013 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1014 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1015 destination, sizeof(destination));
1016 else
1017 prefix2str(&route->prefix, destination, sizeof(destination));
1018 vty_out(vty, "Destination: %s\n", destination);
1019
1020 /* destination type */
1021 vty_out(vty, "Destination type: %s\n",
1022 OSPF6_DEST_TYPE_NAME(route->type));
1023
1024 /* Time */
1025 timersub(&now, &route->installed, &res);
1026 timerstring(&res, duration, sizeof(duration));
1027 vty_out(vty, "Installed Time: %s ago\n", duration);
1028
1029 timersub(&now, &route->changed, &res);
1030 timerstring(&res, duration, sizeof(duration));
1031 vty_out(vty, " Changed Time: %s ago\n", duration);
1032
1033 /* Debugging info */
1034 vty_out(vty, "Lock: %d Flags: %s%s%s%s\n", route->lock,
1035 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1036 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1037 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R" : "-"),
1038 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C" : "-"));
1039 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1040 (void *)route->prev, (void *)route, (void *)route->next);
1041
1042 /* Path section */
1043
1044 /* Area-ID */
1045 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
1046 vty_out(vty, "Associated Area: %s\n", area_id);
1047
1048 /* Path type */
1049 vty_out(vty, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route->path.type));
1050
1051 /* LS Origin */
1052 inet_ntop(AF_INET, &route->path.origin.id, id, sizeof(id));
1053 inet_ntop(AF_INET, &route->path.origin.adv_router, adv_router,
1054 sizeof(adv_router));
1055 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1056 ospf6_lstype_name(route->path.origin.type), id, adv_router);
1057
1058 /* Options */
1059 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1060 vty_out(vty, "Options: %s\n", options);
1061
1062 /* Router Bits */
1063 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
1064 vty_out(vty, "Router Bits: %s\n", capa);
1065
1066 /* Prefix Options */
1067 vty_out(vty, "Prefix Options: xxx\n");
1068
1069 /* Metrics */
1070 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1071 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1072 route->path.u.cost_e2);
1073
d107621d 1074 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
d62a17ae 1075 /* Nexthops */
1076 vty_out(vty, "Nexthop:\n");
1077 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1078 /* nexthop */
1079 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1080 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1081 vty_out(vty, " %s %.*s\n", nexthop, IFNAMSIZ, ifname);
1082 }
1083 vty_out(vty, "\n");
508e53e2 1084}
718e3744 1085
d62a17ae 1086static void ospf6_route_show_table_summary(struct vty *vty,
1087 struct ospf6_route_table *table)
508e53e2 1088{
d62a17ae 1089 struct ospf6_route *route, *prev = NULL;
1090 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1091 unsigned int number = 0;
1092 int nh_count = 0, nhinval = 0, ecmp = 0;
1093 int alternative = 0, destination = 0;
1094
1095 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1096 pathtype[i] = 0;
1097
1098 for (route = ospf6_route_head(table); route;
1099 route = ospf6_route_next(route)) {
1100 if (prev == NULL || !ospf6_route_is_same(prev, route))
1101 destination++;
1102 else
1103 alternative++;
1104 nh_count = ospf6_num_nexthops(route->nh_list);
1105 if (!nh_count)
1106 nhinval++;
1107 else if (nh_count > 1)
1108 ecmp++;
1109 pathtype[route->path.type]++;
1110 number++;
1111
1112 prev = route;
1113 }
1114
1115 assert(number == table->count);
1116
1117 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1118 vty_out(vty, "Number of Destination: %d\n", destination);
1119 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1120 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1121 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
1122 vty_out(vty, "Number of %s routes: %d\n",
1123 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
1124 }
4846ef64 1125}
1126
d62a17ae 1127static void ospf6_route_show_table_prefix(struct vty *vty,
1128 struct prefix *prefix,
1129 struct ospf6_route_table *table)
4846ef64 1130{
d62a17ae 1131 struct ospf6_route *route;
1132
1133 route = ospf6_route_lookup(prefix, table);
1134 if (route == NULL)
1135 return;
1136
1137 ospf6_route_lock(route);
1138 while (route && ospf6_route_is_prefix(prefix, route)) {
1139 /* Specifying a prefix will always display details */
1140 ospf6_route_show_detail(vty, route);
1141 route = ospf6_route_next(route);
1142 }
1143 if (route)
1144 ospf6_route_unlock(route);
4846ef64 1145}
1146
d62a17ae 1147static void ospf6_route_show_table_address(struct vty *vty,
1148 struct prefix *prefix,
1149 struct ospf6_route_table *table)
4846ef64 1150{
d62a17ae 1151 struct ospf6_route *route;
1152
1153 route = ospf6_route_lookup_bestmatch(prefix, table);
1154 if (route == NULL)
1155 return;
1156
1157 prefix = &route->prefix;
1158 ospf6_route_lock(route);
1159 while (route && ospf6_route_is_prefix(prefix, route)) {
1160 /* Specifying a prefix will always display details */
1161 ospf6_route_show_detail(vty, route);
1162 route = ospf6_route_next(route);
1163 }
1164 if (route)
1165 ospf6_route_unlock(route);
4846ef64 1166}
1167
d62a17ae 1168static void ospf6_route_show_table_match(struct vty *vty, int detail,
1169 struct prefix *prefix,
1170 struct ospf6_route_table *table)
4846ef64 1171{
d62a17ae 1172 struct ospf6_route *route;
1173 assert(prefix->family);
1174
1175 route = ospf6_route_match_head(prefix, table);
1176 while (route) {
1177 if (detail)
1178 ospf6_route_show_detail(vty, route);
1179 else
1180 ospf6_route_show(vty, route);
1181 route = ospf6_route_match_next(prefix, route);
1182 }
4846ef64 1183}
1184
d62a17ae 1185static void ospf6_route_show_table_type(struct vty *vty, int detail,
1186 u_char type,
1187 struct ospf6_route_table *table)
4846ef64 1188{
d62a17ae 1189 struct ospf6_route *route;
1190
1191 route = ospf6_route_head(table);
1192 while (route) {
1193 if (route->path.type == type) {
1194 if (detail)
1195 ospf6_route_show_detail(vty, route);
1196 else
1197 ospf6_route_show(vty, route);
1198 }
1199 route = ospf6_route_next(route);
1200 }
4846ef64 1201}
1202
d62a17ae 1203static void ospf6_route_show_table(struct vty *vty, int detail,
1204 struct ospf6_route_table *table)
4846ef64 1205{
d62a17ae 1206 struct ospf6_route *route;
1207
1208 route = ospf6_route_head(table);
1209 while (route) {
1210 if (detail)
1211 ospf6_route_show_detail(vty, route);
1212 else
1213 ospf6_route_show(vty, route);
1214 route = ospf6_route_next(route);
1215 }
718e3744 1216}
1217
d62a17ae 1218int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1219 struct cmd_token **argv,
1220 struct ospf6_route_table *table)
718e3744 1221{
d62a17ae 1222 int summary = 0;
1223 int match = 0;
1224 int detail = 0;
1225 int slash = 0;
1226 int isprefix = 0;
1227 int i, ret;
1228 struct prefix prefix;
1229 u_char type = 0;
1230
1231 memset(&prefix, 0, sizeof(struct prefix));
1232
1233 for (i = argc_start; i < argc; i++) {
1234 if (strmatch(argv[i]->text, "summary")) {
1235 summary++;
1236 continue;
1237 }
1238
1239 if (strmatch(argv[i]->text, "intra-area")) {
1240 type = OSPF6_PATH_TYPE_INTRA;
1241 continue;
1242 }
1243
1244 if (strmatch(argv[i]->text, "inter-area")) {
1245 type = OSPF6_PATH_TYPE_INTER;
1246 continue;
1247 }
1248
1249 if (strmatch(argv[i]->text, "external-1")) {
1250 type = OSPF6_PATH_TYPE_EXTERNAL1;
1251 continue;
1252 }
1253
1254 if (strmatch(argv[i]->text, "external-2")) {
1255 type = OSPF6_PATH_TYPE_EXTERNAL2;
1256 continue;
1257 }
1258
1259 if (strmatch(argv[i]->text, "detail")) {
1260 detail++;
1261 continue;
1262 }
1263
1264 if (strmatch(argv[i]->text, "match")) {
1265 match++;
1266 continue;
1267 }
1268
1269 ret = str2prefix(argv[i]->arg, &prefix);
1270 if (ret == 1 && prefix.family == AF_INET6) {
1271 isprefix++;
1272 if (strchr(argv[i]->arg, '/'))
1273 slash++;
1274 continue;
1275 }
1276
1277 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1278 return CMD_SUCCESS;
1279 }
1280
1281 /* Give summary of this route table */
1282 if (summary) {
1283 ospf6_route_show_table_summary(vty, table);
1284 return CMD_SUCCESS;
1285 }
1286
1287 /* Give exact prefix-match route */
1288 if (isprefix && !match) {
1289 /* If exact address, give best matching route */
1290 if (!slash)
1291 ospf6_route_show_table_address(vty, &prefix, table);
1292 else
1293 ospf6_route_show_table_prefix(vty, &prefix, table);
1294
1295 return CMD_SUCCESS;
1296 }
1297
1298 if (match)
1299 ospf6_route_show_table_match(vty, detail, &prefix, table);
1300 else if (type)
1301 ospf6_route_show_table_type(vty, detail, type, table);
1302 else
1303 ospf6_route_show_table(vty, detail, table);
1304
1305 return CMD_SUCCESS;
4846ef64 1306}
508e53e2 1307
d62a17ae 1308static void ospf6_linkstate_show_header(struct vty *vty)
4846ef64 1309{
d62a17ae 1310 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1311 "Net-ID", "Rtr-Bits", "Options", "Cost");
4846ef64 1312}
1313
d62a17ae 1314static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
4846ef64 1315{
d62a17ae 1316 u_int32_t router, id;
1317 char routername[16], idname[16], rbits[16], options[16];
1318
1319 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1320 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1321 id = ospf6_linkstate_prefix_id(&route->prefix);
1322 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1323
1324 ospf6_capability_printbuf(route->path.router_bits, rbits,
1325 sizeof(rbits));
1326 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1327
1328 if (ntohl(id))
1329 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1330 routername, idname, rbits, options,
1331 (unsigned long)route->path.cost);
1332 else
1333 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1334 routername, idname, rbits, options,
1335 (unsigned long)route->path.cost);
4846ef64 1336}
1337
1338
d62a17ae 1339static void ospf6_linkstate_show_table_exact(struct vty *vty,
1340 struct prefix *prefix,
1341 struct ospf6_route_table *table)
4846ef64 1342{
d62a17ae 1343 struct ospf6_route *route;
1344
1345 route = ospf6_route_lookup(prefix, table);
1346 if (route == NULL)
1347 return;
1348
1349 ospf6_route_lock(route);
1350 while (route && ospf6_route_is_prefix(prefix, route)) {
1351 /* Specifying a prefix will always display details */
1352 ospf6_route_show_detail(vty, route);
1353 route = ospf6_route_next(route);
1354 }
1355 if (route)
1356 ospf6_route_unlock(route);
4846ef64 1357}
1358
d62a17ae 1359static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1360 struct ospf6_route_table *table)
4846ef64 1361{
d62a17ae 1362 struct ospf6_route *route;
1363
1364 if (!detail)
1365 ospf6_linkstate_show_header(vty);
1366
1367 route = ospf6_route_head(table);
1368 while (route) {
1369 if (detail)
1370 ospf6_route_show_detail(vty, route);
1371 else
1372 ospf6_linkstate_show(vty, route);
1373 route = ospf6_route_next(route);
1374 }
718e3744 1375}
1376
d62a17ae 1377int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1378 struct cmd_token **argv,
1379 struct ospf6_route_table *table)
718e3744 1380{
d62a17ae 1381 int detail = 0;
1382 int is_id = 0;
1383 int is_router = 0;
1384 int i, ret;
1385 struct prefix router, id, prefix;
1386
1387 memset(&router, 0, sizeof(struct prefix));
1388 memset(&id, 0, sizeof(struct prefix));
1389 memset(&prefix, 0, sizeof(struct prefix));
1390
1391 for (i = idx_ipv4; i < argc; i++) {
1392 if (strmatch(argv[i]->text, "detail")) {
1393 detail++;
1394 continue;
1395 }
1396
1397 if (!is_router) {
1398 ret = str2prefix(argv[i]->arg, &router);
1399 if (ret == 1 && router.family == AF_INET) {
1400 is_router++;
1401 continue;
1402 }
1403 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1404 return CMD_SUCCESS;
1405 }
1406
1407 if (!is_id) {
1408 ret = str2prefix(argv[i]->arg, &id);
1409 if (ret == 1 && id.family == AF_INET) {
1410 is_id++;
1411 continue;
1412 }
1413 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1414 return CMD_SUCCESS;
1415 }
1416
1417 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1418 return CMD_SUCCESS;
1419 }
1420
1421 if (is_router)
1422 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1423 id.u.prefix4.s_addr, &prefix);
1424
1425 if (prefix.family)
1426 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1427 else
1428 ospf6_linkstate_show_table(vty, detail, table);
1429
1430 return CMD_SUCCESS;
508e53e2 1431}
718e3744 1432
4846ef64 1433
d62a17ae 1434void ospf6_brouter_show_header(struct vty *vty)
6452df09 1435{
d62a17ae 1436 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1437 "Options", "Path-Type", "Area");
6452df09 1438}
1439
d62a17ae 1440void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
6452df09 1441{
d62a17ae 1442 u_int32_t adv_router;
1443 char adv[16], rbits[16], options[16], area[16];
1444
1445 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1446 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1447 ospf6_capability_printbuf(route->path.router_bits, rbits,
1448 sizeof(rbits));
1449 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1450 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1451
1452 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1453 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1454 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1455 OSPF6_PATH_TYPE_NAME(route->path.type), area);
6452df09 1456}
1457
508e53e2 1458DEFUN (debug_ospf6_route,
1459 debug_ospf6_route_cmd,
6147e2c6 1460 "debug ospf6 route <table|intra-area|inter-area|memory>",
508e53e2 1461 DEBUG_STR
1462 OSPF6_STR
16cedbb0 1463 "Debug routes\n"
508e53e2 1464 "Debug route table calculation\n"
508e53e2 1465 "Debug intra-area route calculation\n"
1466 "Debug inter-area route calculation\n"
8f228de7 1467 "Debug route memory use\n"
508e53e2 1468 )
1469{
d62a17ae 1470 int idx_type = 3;
1471 unsigned char level = 0;
1472
8034beff 1473 if (!strcmp(argv[idx_type]->text, "table"))
d62a17ae 1474 level = OSPF6_DEBUG_ROUTE_TABLE;
8034beff 1475 else if (!strcmp(argv[idx_type]->text, "intra-area"))
d62a17ae 1476 level = OSPF6_DEBUG_ROUTE_INTRA;
8034beff 1477 else if (!strcmp(argv[idx_type]->text, "inter-area"))
d62a17ae 1478 level = OSPF6_DEBUG_ROUTE_INTER;
8034beff 1479 else if (!strcmp(argv[idx_type]->text, "memory"))
d62a17ae 1480 level = OSPF6_DEBUG_ROUTE_MEMORY;
1481 OSPF6_DEBUG_ROUTE_ON(level);
1482 return CMD_SUCCESS;
508e53e2 1483}
1484
1485DEFUN (no_debug_ospf6_route,
1486 no_debug_ospf6_route_cmd,
6147e2c6 1487 "no debug ospf6 route <table|intra-area|inter-area|memory>",
508e53e2 1488 NO_STR
1489 DEBUG_STR
1490 OSPF6_STR
16cedbb0 1491 "Debug routes\n"
508e53e2 1492 "Debug route table calculation\n"
8f228de7 1493 "Debug intra-area route calculation\n"
16cedbb0 1494 "Debug inter-area route calculation\n"
8f228de7 1495 "Debug route memory use\n")
508e53e2 1496{
d62a17ae 1497 int idx_type = 4;
1498 unsigned char level = 0;
1499
8034beff 1500 if (!strcmp(argv[idx_type]->text, "table"))
d62a17ae 1501 level = OSPF6_DEBUG_ROUTE_TABLE;
8034beff 1502 else if (!strcmp(argv[idx_type]->text, "intra-area"))
d62a17ae 1503 level = OSPF6_DEBUG_ROUTE_INTRA;
8034beff 1504 else if (!strcmp(argv[idx_type]->text, "inter-area"))
d62a17ae 1505 level = OSPF6_DEBUG_ROUTE_INTER;
8034beff 1506 else if (!strcmp(argv[idx_type]->text, "memory"))
d62a17ae 1507 level = OSPF6_DEBUG_ROUTE_MEMORY;
1508 OSPF6_DEBUG_ROUTE_OFF(level);
1509 return CMD_SUCCESS;
718e3744 1510}
1511
d62a17ae 1512int config_write_ospf6_debug_route(struct vty *vty)
508e53e2 1513{
d62a17ae 1514 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1515 vty_out(vty, "debug ospf6 route table\n");
1516 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1517 vty_out(vty, "debug ospf6 route intra-area\n");
1518 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1519 vty_out(vty, "debug ospf6 route inter-area\n");
6f7af48c
CS
1520 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1521 vty_out(vty, "debug ospf6 route memory\n");
1522
d62a17ae 1523 return 0;
508e53e2 1524}
1525
d62a17ae 1526void install_element_ospf6_debug_route(void)
508e53e2 1527{
d62a17ae 1528 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
1529 install_element(ENABLE_NODE, &no_debug_ospf6_route_cmd);
1530 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
1531 install_element(CONFIG_NODE, &no_debug_ospf6_route_cmd);
508e53e2 1532}