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