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