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