2 * IS-IS Rout(e)ing protocol - isis_route.c
3 * Copyright (C) 2001,2002 Sampo Saaristo
4 * Tampere University of Technology
5 * Institute of Communications Engineering
7 * based on ../ospf6d/ospf6_route.[ch]
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public Licenseas published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
15 * This program is distributed in the hope that it will be useful,but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "lib_errors.h"
37 #include "srcdest_table.h"
39 #include "isis_constants.h"
40 #include "isis_common.h"
41 #include "isis_flags.h"
43 #include "isis_misc.h"
44 #include "isis_adjacency.h"
45 #include "isis_circuit.h"
49 #include "isis_spf_private.h"
50 #include "isis_route.h"
51 #include "isis_zebra.h"
53 DEFINE_MTYPE_STATIC(ISISD
, ISIS_NEXTHOP
, "ISIS nexthop");
54 DEFINE_MTYPE_STATIC(ISISD
, ISIS_ROUTE_INFO
, "ISIS route info");
56 DEFINE_HOOK(isis_route_update_hook
,
57 (struct isis_area
* area
, struct prefix
*prefix
,
58 struct isis_route_info
*route_info
),
59 (area
, prefix
, route_info
));
61 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
62 union g_addr
*ip
, ifindex_t ifindex
);
63 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
64 struct prefix_ipv6
*src_p
,
65 struct isis_route_info
*route_info
);
67 static struct isis_nexthop
*isis_nexthop_create(int family
, union g_addr
*ip
,
70 struct isis_nexthop
*nexthop
;
72 nexthop
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
74 nexthop
->family
= family
;
75 nexthop
->ifindex
= ifindex
;
81 void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
83 XFREE(MTYPE_ISIS_NEXTHOP_LABELS
, nexthop
->label_stack
);
84 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
87 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
88 union g_addr
*ip
, ifindex_t ifindex
)
90 struct listnode
*node
;
91 struct isis_nexthop
*nh
;
93 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
94 if (nh
->family
!= family
)
96 if (nh
->ifindex
!= ifindex
)
101 if (IPV4_ADDR_CMP(&nh
->ip
.ipv4
, &ip
->ipv4
))
105 if (IPV6_ADDR_CMP(&nh
->ip
.ipv6
, &ip
->ipv6
))
109 flog_err(EC_LIB_DEVELOPMENT
,
110 "%s: unknown address family [%d]", __func__
,
121 void adjinfo2nexthop(int family
, struct list
*nexthops
,
122 struct isis_adjacency
*adj
, struct isis_sr_psid_info
*sr
,
123 struct mpls_label_stack
*label_stack
)
125 struct isis_nexthop
*nh
;
126 union g_addr ip
= {};
130 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
131 ip
.ipv4
= adj
->ipv4_addresses
[i
];
133 if (!nexthoplookup(nexthops
, AF_INET
, &ip
,
134 adj
->circuit
->interface
->ifindex
)) {
135 nh
= isis_nexthop_create(
137 adj
->circuit
->interface
->ifindex
);
138 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
141 nh
->label_stack
= label_stack
;
142 listnode_add(nexthops
, nh
);
148 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
149 ip
.ipv6
= adj
->ipv6_addresses
[i
];
151 if (!nexthoplookup(nexthops
, AF_INET6
, &ip
,
152 adj
->circuit
->interface
->ifindex
)) {
153 nh
= isis_nexthop_create(
155 adj
->circuit
->interface
->ifindex
);
156 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
159 nh
->label_stack
= label_stack
;
160 listnode_add(nexthops
, nh
);
166 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address family [%d]",
172 static void isis_route_add_dummy_nexthops(struct isis_route_info
*rinfo
,
173 const uint8_t *sysid
,
174 struct isis_sr_psid_info
*sr
,
175 struct mpls_label_stack
*label_stack
)
177 struct isis_nexthop
*nh
;
179 nh
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
180 memcpy(nh
->sysid
, sysid
, sizeof(nh
->sysid
));
182 nh
->label_stack
= label_stack
;
183 listnode_add(rinfo
->nexthops
, nh
);
186 static struct isis_route_info
*
187 isis_route_info_new(struct prefix
*prefix
, struct prefix_ipv6
*src_p
,
188 uint32_t cost
, uint32_t depth
, struct isis_sr_psid_info
*sr
,
189 struct list
*adjacencies
, bool allow_ecmp
)
191 struct isis_route_info
*rinfo
;
192 struct isis_vertex_adj
*vadj
;
193 struct listnode
*node
;
195 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
197 rinfo
->nexthops
= list_new();
198 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, vadj
)) {
199 struct isis_spf_adj
*sadj
= vadj
->sadj
;
200 struct isis_adjacency
*adj
= sadj
->adj
;
201 struct isis_sr_psid_info
*sr
= &vadj
->sr
;
202 struct mpls_label_stack
*label_stack
= vadj
->label_stack
;
205 * Create dummy nexthops when running SPF on a testing
208 if (CHECK_FLAG(im
->options
, F_ISIS_UNIT_TEST
)) {
209 isis_route_add_dummy_nexthops(rinfo
, sadj
->id
, sr
,
216 /* check for force resync this route */
217 if (CHECK_FLAG(adj
->circuit
->flags
,
218 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
219 SET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
221 /* update neighbor router address */
222 switch (prefix
->family
) {
224 if (depth
== 2 && prefix
->prefixlen
== IPV4_MAX_BITLEN
)
225 adj
->router_address
= prefix
->u
.prefix4
;
228 if (depth
== 2 && prefix
->prefixlen
== IPV6_MAX_BITLEN
229 && (!src_p
|| !src_p
->prefixlen
)) {
230 adj
->router_address6
= prefix
->u
.prefix6
;
234 flog_err(EC_LIB_DEVELOPMENT
,
235 "%s: unknown address family [%d]", __func__
,
239 adjinfo2nexthop(prefix
->family
, rinfo
->nexthops
, adj
, sr
,
246 rinfo
->depth
= depth
;
252 static void isis_route_info_delete(struct isis_route_info
*route_info
)
254 if (route_info
->nexthops
) {
255 route_info
->nexthops
->del
=
256 (void (*)(void *))isis_nexthop_delete
;
257 list_delete(&route_info
->nexthops
);
260 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
263 void isis_route_node_cleanup(struct route_table
*table
, struct route_node
*node
)
266 isis_route_info_delete(node
->info
);
269 static bool isis_sr_psid_info_same(struct isis_sr_psid_info
*new,
270 struct isis_sr_psid_info
*old
)
272 if (new->present
!= old
->present
)
275 if (new->label
!= old
->label
)
278 if (new->sid
.flags
!= old
->sid
.flags
279 || new->sid
.value
!= old
->sid
.value
)
285 static bool isis_label_stack_same(struct mpls_label_stack
*new,
286 struct mpls_label_stack
*old
)
292 if (new->num_labels
!= old
->num_labels
)
294 if (memcmp(&new->label
, &old
->label
,
295 sizeof(mpls_label_t
) * new->num_labels
))
301 static int isis_route_info_same(struct isis_route_info
*new,
302 struct isis_route_info
*old
, char *buf
,
305 struct listnode
*node
;
306 struct isis_nexthop
*new_nh
, *old_nh
;
308 if (new->cost
!= old
->cost
) {
310 snprintf(buf
, buf_size
, "cost (old: %u, new: %u)",
311 old
->cost
, new->cost
);
315 if (new->depth
!= old
->depth
) {
317 snprintf(buf
, buf_size
, "depth (old: %u, new: %u)",
318 old
->depth
, new->depth
);
322 if (!isis_sr_psid_info_same(&new->sr
, &old
->sr
)) {
324 snprintf(buf
, buf_size
, "SR input label");
328 if (new->nexthops
->count
!= old
->nexthops
->count
) {
330 snprintf(buf
, buf_size
, "nhops num (old: %u, new: %u)",
331 old
->nexthops
->count
, new->nexthops
->count
);
335 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, new_nh
)) {
336 old_nh
= nexthoplookup(old
->nexthops
, new_nh
->family
,
337 &new_nh
->ip
, new_nh
->ifindex
);
340 snprintf(buf
, buf_size
,
341 "new nhop"); /* TODO: print nhop */
344 if (!isis_sr_psid_info_same(&new_nh
->sr
, &old_nh
->sr
)) {
346 snprintf(buf
, buf_size
, "nhop SR label");
349 if (!isis_label_stack_same(new_nh
->label_stack
,
350 old_nh
->label_stack
)) {
352 snprintf(buf
, buf_size
, "nhop label stack");
357 /* only the resync flag needs to be checked */
358 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
359 != CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)) {
361 snprintf(buf
, buf_size
, "resync flag");
368 struct isis_route_info
*
369 isis_route_create(struct prefix
*prefix
, struct prefix_ipv6
*src_p
,
370 uint32_t cost
, uint32_t depth
, struct isis_sr_psid_info
*sr
,
371 struct list
*adjacencies
, bool allow_ecmp
,
372 struct isis_area
*area
, struct route_table
*table
)
374 struct route_node
*route_node
;
375 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
381 rinfo_new
= isis_route_info_new(prefix
, src_p
, cost
, depth
, sr
,
382 adjacencies
, allow_ecmp
);
383 route_node
= srcdest_rnode_get(table
, prefix
, src_p
);
385 rinfo_old
= route_node
->info
;
387 if (IS_DEBUG_RTE_EVENTS
)
388 zlog_debug("ISIS-Rte (%s) route created: %pFX",
389 area
->area_tag
, prefix
);
390 route_info
= rinfo_new
;
391 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
393 route_unlock_node(route_node
);
395 if (IS_DEBUG_RTE_EVENTS
)
396 zlog_debug("ISIS-Rte (%s) route already exists: %pFX",
397 area
->area_tag
, prefix
);
398 #endif /* EXTREME_DEBUG */
399 if (isis_route_info_same(rinfo_new
, rinfo_old
, change_buf
,
400 sizeof(change_buf
))) {
402 if (IS_DEBUG_RTE_EVENTS
)
404 "ISIS-Rte (%s) route unchanged: %pFX",
405 area
->area_tag
, prefix
);
406 #endif /* EXTREME_DEBUG */
407 isis_route_info_delete(rinfo_new
);
408 route_info
= rinfo_old
;
410 if (IS_DEBUG_RTE_EVENTS
)
412 "ISIS-Rte (%s): route changed: %pFX, change: %s",
413 area
->area_tag
, prefix
, change_buf
);
414 rinfo_new
->sr_previous
= rinfo_old
->sr
;
415 isis_route_info_delete(rinfo_old
);
416 route_info
= rinfo_new
;
417 UNSET_FLAG(route_info
->flag
,
418 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
422 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
423 route_node
->info
= route_info
;
428 void isis_route_delete(struct isis_area
*area
, struct route_node
*rode
,
429 struct route_table
*table
)
431 struct isis_route_info
*rinfo
;
432 char buff
[SRCDEST2STR_BUFFER
];
433 struct prefix
*prefix
;
434 struct prefix_ipv6
*src_p
;
437 srcdest_rnode2str(rode
, buff
, sizeof(buff
));
439 srcdest_rnode_prefixes(rode
, (const struct prefix
**)&prefix
,
440 (const struct prefix
**)&src_p
);
444 if (IS_DEBUG_RTE_EVENTS
)
446 "ISIS-Rte: tried to delete non-existant route %s",
451 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
452 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
453 if (IS_DEBUG_RTE_EVENTS
)
454 zlog_debug("ISIS-Rte: route delete %s", buff
);
455 isis_route_update(area
, prefix
, src_p
, rinfo
);
457 isis_route_info_delete(rinfo
);
459 route_unlock_node(rode
);
462 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
463 struct prefix_ipv6
*src_p
,
464 struct isis_route_info
*route_info
)
466 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
467 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
471 * Explicitly uninstall previous Prefix-SID label if it has
472 * changed or was removed.
474 if (route_info
->sr_previous
.present
475 && (!route_info
->sr
.present
476 || route_info
->sr_previous
.label
477 != route_info
->sr
.label
))
478 isis_zebra_prefix_sid_uninstall(
479 area
, prefix
, route_info
,
480 &route_info
->sr_previous
);
483 isis_zebra_route_add_route(area
->isis
, prefix
, src_p
,
485 /* Install/reinstall Prefix-SID label. */
486 if (route_info
->sr
.present
)
487 isis_zebra_prefix_sid_install(area
, prefix
, route_info
,
489 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
491 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
492 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
494 /* Uninstall Prefix-SID label. */
495 if (route_info
->sr
.present
)
496 isis_zebra_prefix_sid_uninstall(
497 area
, prefix
, route_info
, &route_info
->sr
);
498 /* Uninstall route. */
499 isis_zebra_route_del_route(area
->isis
, prefix
, src_p
,
501 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
503 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
507 static void _isis_route_verify_table(struct isis_area
*area
,
508 struct route_table
*table
,
509 struct route_table
*table_backup
,
510 struct route_table
**tables
)
512 struct route_node
*rnode
, *drnode
;
513 struct isis_route_info
*rinfo
;
515 char buff
[SRCDEST2STR_BUFFER
];
516 #endif /* EXTREME_DEBUG */
518 for (rnode
= route_top(table
); rnode
;
519 rnode
= srcdest_route_next(rnode
)) {
520 if (rnode
->info
== NULL
)
524 struct prefix
*dst_p
;
525 struct prefix_ipv6
*src_p
;
527 srcdest_rnode_prefixes(rnode
,
528 (const struct prefix
**)&dst_p
,
529 (const struct prefix
**)&src_p
);
531 /* Link primary route to backup route. */
533 struct route_node
*rnode_bck
;
535 rnode_bck
= srcdest_rnode_lookup(table_backup
, dst_p
,
538 rinfo
->backup
= rnode_bck
->info
;
539 UNSET_FLAG(rinfo
->flag
,
540 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
541 } else if (rinfo
->backup
) {
542 rinfo
->backup
= NULL
;
543 UNSET_FLAG(rinfo
->flag
,
544 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
549 if (IS_DEBUG_RTE_EVENTS
) {
550 srcdest2str(dst_p
, src_p
, buff
, sizeof(buff
));
552 "ISIS-Rte (%s): route validate: %s %s %s %s",
554 (CHECK_FLAG(rinfo
->flag
,
555 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
558 (CHECK_FLAG(rinfo
->flag
,
559 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
562 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
567 #endif /* EXTREME_DEBUG */
569 isis_route_update(area
, dst_p
, src_p
, rinfo
);
571 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
574 /* Area is either L1 or L2 => we use level route tables
576 * validating => no problems with deleting routes. */
578 isis_route_delete(area
, rnode
, table
);
582 /* If area is L1L2, we work with merge table and
584 * delete node from level tables as well before deleting
586 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
587 drnode
= srcdest_rnode_lookup(tables
[level
- 1],
592 route_unlock_node(drnode
);
594 if (drnode
->info
!= rnode
->info
)
598 route_unlock_node(drnode
);
601 isis_route_delete(area
, rnode
, table
);
605 void isis_route_verify_table(struct isis_area
*area
, struct route_table
*table
,
606 struct route_table
*table_backup
)
608 _isis_route_verify_table(area
, table
, table_backup
, NULL
);
611 /* Function to validate route tables for L1L2 areas. In this case we can't use
612 * level route tables directly, we have to merge them at first. L1 routes are
613 * preferred over the L2 ones.
615 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
616 * merge table at first, then L2 paths are added if L1 path for same prefix
617 * doesn't already exists there.
619 * FIXME: Is it right place to do it at all? Maybe we should push both levels
620 * to the RIB with different zebra route types and let RIB handle this? */
621 void isis_route_verify_merge(struct isis_area
*area
,
622 struct route_table
*level1_table
,
623 struct route_table
*level1_table_backup
,
624 struct route_table
*level2_table
,
625 struct route_table
*level2_table_backup
)
627 struct route_table
*tables
[] = {level1_table
, level2_table
};
628 struct route_table
*tables_backup
[] = {level1_table_backup
,
629 level2_table_backup
};
630 struct route_table
*merge
;
631 struct route_node
*rnode
, *mrnode
;
633 merge
= srcdest_table_init();
635 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
636 for (rnode
= route_top(tables
[level
- 1]); rnode
;
637 rnode
= srcdest_route_next(rnode
)) {
638 struct isis_route_info
*rinfo
= rnode
->info
;
639 struct route_node
*rnode_bck
;
644 struct prefix
*prefix
;
645 struct prefix_ipv6
*src_p
;
647 srcdest_rnode_prefixes(rnode
,
648 (const struct prefix
**)&prefix
,
649 (const struct prefix
**)&src_p
);
651 /* Link primary route to backup route. */
652 rnode_bck
= srcdest_rnode_lookup(
653 tables_backup
[level
- 1], prefix
, src_p
);
655 rinfo
->backup
= rnode_bck
->info
;
656 UNSET_FLAG(rinfo
->flag
,
657 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
658 } else if (rinfo
->backup
) {
659 rinfo
->backup
= NULL
;
660 UNSET_FLAG(rinfo
->flag
,
661 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
664 mrnode
= srcdest_rnode_get(merge
, prefix
, src_p
);
665 struct isis_route_info
*mrinfo
= mrnode
->info
;
667 route_unlock_node(mrnode
);
668 if (CHECK_FLAG(mrinfo
->flag
,
669 ISIS_ROUTE_FLAG_ACTIVE
)) {
670 /* Clear the ZEBRA_SYNCED flag on the
671 * L2 route when L1 wins, otherwise L2
672 * won't get reinstalled when L1
677 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
680 } else if (CHECK_FLAG(rinfo
->flag
,
681 ISIS_ROUTE_FLAG_ACTIVE
)) {
682 /* Clear the ZEBRA_SYNCED flag on the L1
683 * route when L2 wins, otherwise L1
684 * won't get reinstalled when it
689 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
694 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
698 mrnode
->info
= rnode
->info
;
702 _isis_route_verify_table(area
, merge
, NULL
, tables
);
703 route_table_finish(merge
);
706 void isis_route_invalidate_table(struct isis_area
*area
,
707 struct route_table
*table
)
709 struct route_node
*rode
;
710 struct isis_route_info
*rinfo
;
711 for (rode
= route_top(table
); rode
; rode
= srcdest_route_next(rode
)) {
712 if (rode
->info
== NULL
)
717 rinfo
->backup
= NULL
;
719 * For now, always force routes that have backup
720 * nexthops to be reinstalled.
722 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
724 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);