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