]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_lfa.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / isisd / isis_lfa.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 NetDEF, Inc.
4 * Renato Westphal
5 */
6
7 #include <zebra.h>
8
9 #include "linklist.h"
10 #include "log.h"
11 #include "memory.h"
12 #include "vrf.h"
13 #include "table.h"
14 #include "srcdest_table.h"
15 #include "plist.h"
16 #include "zclient.h"
17
18 #include "isis_common.h"
19 #include "isisd.h"
20 #include "isis_misc.h"
21 #include "isis_adjacency.h"
22 #include "isis_circuit.h"
23 #include "isis_lsp.h"
24 #include "isis_spf.h"
25 #include "isis_route.h"
26 #include "isis_mt.h"
27 #include "isis_tlvs.h"
28 #include "isis_spf_private.h"
29 #include "isis_zebra.h"
30 #include "isis_errors.h"
31
32 DEFINE_MTYPE_STATIC(ISISD, ISIS_SPF_NODE, "ISIS SPF Node");
33 DEFINE_MTYPE_STATIC(ISISD, ISIS_LFA_TIEBREAKER, "ISIS LFA Tiebreaker");
34 DEFINE_MTYPE_STATIC(ISISD, ISIS_LFA_EXCL_IFACE, "ISIS LFA Excluded Interface");
35 DEFINE_MTYPE_STATIC(ISISD, ISIS_RLFA, "ISIS Remote LFA");
36 DEFINE_MTYPE(ISISD, ISIS_NEXTHOP_LABELS, "ISIS nexthop MPLS labels");
37
38 static inline int isis_spf_node_compare(const struct isis_spf_node *a,
39 const struct isis_spf_node *b)
40 {
41 return memcmp(a->sysid, b->sysid, sizeof(a->sysid));
42 }
43 RB_GENERATE(isis_spf_nodes, isis_spf_node, entry, isis_spf_node_compare)
44
45 /**
46 * Initialize list of SPF nodes.
47 *
48 * @param nodes List of SPF nodes
49 */
50 void isis_spf_node_list_init(struct isis_spf_nodes *nodes)
51 {
52 RB_INIT(isis_spf_nodes, nodes);
53 }
54
55 /**
56 * Clear list of SPF nodes, releasing all allocated memory.
57 *
58 * @param nodes List of SPF nodes
59 */
60 void isis_spf_node_list_clear(struct isis_spf_nodes *nodes)
61 {
62 while (!RB_EMPTY(isis_spf_nodes, nodes)) {
63 struct isis_spf_node *node = RB_ROOT(isis_spf_nodes, nodes);
64
65 if (node->adjacencies)
66 list_delete(&node->adjacencies);
67 if (node->lfa.spftree)
68 isis_spftree_del(node->lfa.spftree);
69 if (node->lfa.spftree_reverse)
70 isis_spftree_del(node->lfa.spftree_reverse);
71 isis_spf_node_list_clear(&node->lfa.p_space);
72 RB_REMOVE(isis_spf_nodes, nodes, node);
73 XFREE(MTYPE_ISIS_SPF_NODE, node);
74 }
75 }
76
77 /**
78 * Add new node to list of SPF nodes.
79 *
80 * @param nodes List of SPF nodes
81 * @param sysid Node System ID
82 *
83 * @return Pointer to new IS-IS SPF node structure.
84 */
85 struct isis_spf_node *isis_spf_node_new(struct isis_spf_nodes *nodes,
86 const uint8_t *sysid)
87 {
88 struct isis_spf_node *node;
89
90 node = XCALLOC(MTYPE_ISIS_SPF_NODE, sizeof(*node));
91 memcpy(node->sysid, sysid, sizeof(node->sysid));
92 node->adjacencies = list_new();
93 isis_spf_node_list_init(&node->lfa.p_space);
94 RB_INSERT(isis_spf_nodes, nodes, node);
95
96 return node;
97 }
98
99 /**
100 * Lookup SPF node by its System ID on the given list.
101 *
102 * @param nodes List of SPF nodes
103 * @param sysid Node System ID
104 *
105 * @return Pointer to SPF node if found, NULL otherwise
106 */
107 struct isis_spf_node *isis_spf_node_find(const struct isis_spf_nodes *nodes,
108 const uint8_t *sysid)
109 {
110 struct isis_spf_node node = {};
111
112 memcpy(node.sysid, sysid, sizeof(node.sysid));
113 return RB_FIND(isis_spf_nodes, nodes, &node);
114 }
115
116 /**
117 * LFA tiebreaker RB-tree comparison function.
118 *
119 * @param a First LFA tiebreaker
120 * @param b Second LFA tiebreaker
121 *
122 * @return -1 (a < b), 0 (a == b) or +1 (a > b)
123 */
124 int lfa_tiebreaker_cmp(const struct lfa_tiebreaker *a,
125 const struct lfa_tiebreaker *b)
126 {
127 if (a->index < b->index)
128 return -1;
129 if (a->index > b->index)
130 return 1;
131
132 return a->type - b->type;
133 }
134
135 /**
136 * Initialize list of LFA tie-breakers.
137 *
138 * @param area IS-IS area
139 * @param level IS-IS level
140 */
141 void isis_lfa_tiebreakers_init(struct isis_area *area, int level)
142 {
143 lfa_tiebreaker_tree_init(&area->lfa_tiebreakers[level - 1]);
144 }
145
146 /**
147 * Clear list of LFA tie-breakers, releasing all allocated memory.
148 *
149 * @param area IS-IS area
150 * @param level IS-IS level
151 */
152 void isis_lfa_tiebreakers_clear(struct isis_area *area, int level)
153 {
154 while (lfa_tiebreaker_tree_count(&area->lfa_tiebreakers[level - 1])
155 > 0) {
156 struct lfa_tiebreaker *tie_b;
157
158 tie_b = lfa_tiebreaker_tree_first(
159 &area->lfa_tiebreakers[level - 1]);
160 isis_lfa_tiebreaker_delete(area, level, tie_b);
161 }
162 }
163
164 /**
165 * Add new LFA tie-breaker to list of LFA tie-breakers.
166 *
167 * @param area IS-IS area
168 * @param level IS-IS level
169 * @param index LFA tie-breaker index
170 * @param type LFA tie-breaker type
171 *
172 * @return Pointer to new LFA tie-breaker structure.
173 */
174 struct lfa_tiebreaker *isis_lfa_tiebreaker_add(struct isis_area *area,
175 int level, uint8_t index,
176 enum lfa_tiebreaker_type type)
177 {
178 struct lfa_tiebreaker *tie_b;
179
180 tie_b = XCALLOC(MTYPE_ISIS_LFA_TIEBREAKER, sizeof(*tie_b));
181 tie_b->index = index;
182 tie_b->type = type;
183 tie_b->area = area;
184 lfa_tiebreaker_tree_add(&area->lfa_tiebreakers[level - 1], tie_b);
185
186 return tie_b;
187 }
188
189 /**
190 * Remove LFA tie-breaker from list of LFA tie-breakers.
191 *
192 * @param area IS-IS area
193 * @param level IS-IS level
194 * @param tie_b Pointer to LFA tie-breaker structure
195 */
196 void isis_lfa_tiebreaker_delete(struct isis_area *area, int level,
197 struct lfa_tiebreaker *tie_b)
198 {
199 lfa_tiebreaker_tree_del(&area->lfa_tiebreakers[level - 1], tie_b);
200 XFREE(MTYPE_ISIS_LFA_TIEBREAKER, tie_b);
201 }
202
203 static bool lfa_excl_interface_hash_cmp(const void *value1, const void *value2)
204 {
205 return strmatch(value1, value2);
206 }
207
208 static unsigned int lfa_excl_interface_hash_make(const void *value)
209 {
210 return string_hash_make(value);
211 }
212
213 static void *lfa_excl_interface_hash_alloc(void *p)
214 {
215 return XSTRDUP(MTYPE_ISIS_LFA_EXCL_IFACE, p);
216 }
217
218 static void lfa_excl_interface_hash_free(void *arg)
219 {
220 XFREE(MTYPE_ISIS_LFA_EXCL_IFACE, arg);
221 }
222
223 /**
224 * Initialize hash table of LFA excluded interfaces.
225 *
226 * @param circuit IS-IS interface
227 * @param level IS-IS level
228 */
229 void isis_lfa_excluded_ifaces_init(struct isis_circuit *circuit, int level)
230 {
231 circuit->lfa_excluded_ifaces[level - 1] = hash_create(
232 lfa_excl_interface_hash_make, lfa_excl_interface_hash_cmp,
233 "LFA Excluded Interfaces");
234 }
235
236 /**
237 * Clear hash table of LFA excluded interfaces, releasing all allocated memory.
238 *
239 * @param nodes List of SPF nodes
240 */
241 void isis_lfa_excluded_ifaces_clear(struct isis_circuit *circuit, int level)
242 {
243 hash_clean(circuit->lfa_excluded_ifaces[level - 1],
244 lfa_excl_interface_hash_free);
245 }
246
247 /**
248 * Add new interface to hash table of excluded interfaces.
249 *
250 * @param circuit IS-IS interface
251 * @param level IS-IS level
252 * @param ifname Excluded interface name
253 */
254 void isis_lfa_excluded_iface_add(struct isis_circuit *circuit, int level,
255 const char *ifname)
256 {
257 (void)hash_get(circuit->lfa_excluded_ifaces[level - 1], (char *)ifname,
258 lfa_excl_interface_hash_alloc);
259 }
260
261 /**
262 * Remove interface from hash table of excluded interfaces.
263 *
264 * @param circuit IS-IS interface
265 * @param level IS-IS level
266 * @param ifname Excluded interface name
267 */
268 void isis_lfa_excluded_iface_delete(struct isis_circuit *circuit, int level,
269 const char *ifname)
270 {
271 char *found;
272
273 found = hash_lookup(circuit->lfa_excluded_ifaces[level - 1],
274 (char *)ifname);
275 if (found) {
276 hash_release(circuit->lfa_excluded_ifaces[level - 1], found);
277 lfa_excl_interface_hash_free(found);
278 }
279 }
280
281 /**
282 * Lookup excluded interface.
283 *
284 * @param circuit IS-IS interface
285 * @param level IS-IS level
286 * @param ifname Excluded interface name
287 */
288 bool isis_lfa_excluded_iface_check(struct isis_circuit *circuit, int level,
289 const char *ifname)
290 {
291 return hash_lookup(circuit->lfa_excluded_ifaces[level - 1],
292 (char *)ifname);
293 }
294
295 /**
296 * Check if a given IS-IS adjacency needs to be excised when computing the SPF
297 * post-convergence tree.
298 *
299 * @param spftree IS-IS SPF tree
300 * @param id Adjacency System ID (or LAN ID of the designated router
301 * for broadcast interfaces)
302 *
303 * @return true if the adjacency needs to be excised, false
304 * otherwise
305 */
306 bool isis_lfa_excise_adj_check(const struct isis_spftree *spftree,
307 const uint8_t *id)
308 {
309 const struct lfa_protected_resource *resource;
310
311 if (spftree->type != SPF_TYPE_RLFA && spftree->type != SPF_TYPE_TI_LFA)
312 return false;
313
314 /*
315 * Adjacencies formed over the failed interface should be excised both
316 * when using link and node protection.
317 */
318 resource = &spftree->lfa.protected_resource;
319 if (!memcmp(resource->adjacency, id, ISIS_SYS_ID_LEN + 1))
320 return true;
321
322 return false;
323 }
324
325 /**
326 * Check if a given IS-IS node needs to be excised when computing the SPF
327 * post-convergence tree.
328 *
329 * @param spftree IS-IS SPF tree
330 * @param id Node System ID
331 *
332 * @return true if the node needs to be excised, false otherwise
333 */
334 bool isis_lfa_excise_node_check(const struct isis_spftree *spftree,
335 const uint8_t *id)
336 {
337 const struct lfa_protected_resource *resource;
338
339 if (spftree->type != SPF_TYPE_TI_LFA)
340 return false;
341
342 /*
343 * When using node protection, nodes reachable over the failed interface
344 * must be excised.
345 */
346 resource = &spftree->lfa.protected_resource;
347 if (resource->type == LFA_LINK_PROTECTION)
348 return false;
349
350 if (isis_spf_node_find(&resource->nodes, id))
351 return true;
352
353 return false;
354 }
355
356 struct tilfa_find_pnode_prefix_sid_args {
357 uint32_t sid_index;
358 };
359
360 static int tilfa_find_pnode_prefix_sid_cb(const struct prefix *prefix,
361 uint32_t metric, bool external,
362 struct isis_subtlvs *subtlvs,
363 void *arg)
364 {
365 struct tilfa_find_pnode_prefix_sid_args *args = arg;
366 struct isis_prefix_sid *psid;
367
368 if (!subtlvs || subtlvs->prefix_sids.count == 0)
369 return LSP_ITER_CONTINUE;
370
371 psid = (struct isis_prefix_sid *)subtlvs->prefix_sids.head;
372
373 /* Require the node flag to be set. */
374 if (!CHECK_FLAG(psid->flags, ISIS_PREFIX_SID_NODE))
375 return LSP_ITER_CONTINUE;
376
377 args->sid_index = psid->value;
378
379 return LSP_ITER_STOP;
380 }
381
382 /* Find Prefix-SID associated to a System ID. */
383 static uint32_t tilfa_find_pnode_prefix_sid(struct isis_spftree *spftree,
384 const uint8_t *sysid)
385 {
386 struct isis_lsp *lsp;
387 struct tilfa_find_pnode_prefix_sid_args args;
388
389 lsp = isis_root_system_lsp(spftree->lspdb, sysid);
390 if (!lsp)
391 return UINT32_MAX;
392
393 args.sid_index = UINT32_MAX;
394 isis_lsp_iterate_ip_reach(lsp, spftree->family, spftree->mtid,
395 tilfa_find_pnode_prefix_sid_cb, &args);
396
397 return args.sid_index;
398 }
399
400 struct tilfa_find_qnode_adj_sid_args {
401 const uint8_t *qnode_sysid;
402 mpls_label_t label;
403 };
404
405 static int tilfa_find_qnode_adj_sid_cb(const uint8_t *id, uint32_t metric,
406 bool oldmetric,
407 struct isis_ext_subtlvs *subtlvs,
408 void *arg)
409 {
410 struct tilfa_find_qnode_adj_sid_args *args = arg;
411 struct isis_adj_sid *adj_sid;
412
413 if (memcmp(id, args->qnode_sysid, ISIS_SYS_ID_LEN))
414 return LSP_ITER_CONTINUE;
415 if (!subtlvs || subtlvs->adj_sid.count == 0)
416 return LSP_ITER_CONTINUE;
417
418 adj_sid = (struct isis_adj_sid *)subtlvs->adj_sid.head;
419 args->label = adj_sid->sid;
420
421 return LSP_ITER_STOP;
422 }
423
424 /* Find Adj-SID associated to a pair of System IDs. */
425 static mpls_label_t tilfa_find_qnode_adj_sid(struct isis_spftree *spftree,
426 const uint8_t *source_sysid,
427 const uint8_t *qnode_sysid)
428 {
429 struct isis_lsp *lsp;
430 struct tilfa_find_qnode_adj_sid_args args;
431
432 lsp = isis_root_system_lsp(spftree->lspdb, source_sysid);
433 if (!lsp)
434 return MPLS_INVALID_LABEL;
435
436 args.qnode_sysid = qnode_sysid;
437 args.label = MPLS_INVALID_LABEL;
438 isis_lsp_iterate_is_reach(lsp, spftree->mtid,
439 tilfa_find_qnode_adj_sid_cb, &args);
440
441 return args.label;
442 }
443
444 /*
445 * Compute the MPLS label stack associated to a TI-LFA repair list. This
446 * needs to be computed separately for each adjacency since different
447 * neighbors can have different SRGBs.
448 */
449 static struct mpls_label_stack *
450 tilfa_compute_label_stack(struct lspdb_head *lspdb,
451 const struct isis_spf_adj *sadj,
452 const struct list *repair_list)
453 {
454 struct mpls_label_stack *label_stack;
455 struct isis_tilfa_sid *sid;
456 struct listnode *node;
457 size_t i = 0;
458
459 /* Allocate label stack. */
460 label_stack = XCALLOC(MTYPE_ISIS_NEXTHOP_LABELS,
461 sizeof(struct mpls_label_stack)
462 + listcount(repair_list)
463 * sizeof(mpls_label_t));
464 label_stack->num_labels = listcount(repair_list);
465
466 for (ALL_LIST_ELEMENTS_RO(repair_list, node, sid)) {
467 const uint8_t *target_node;
468 struct isis_sr_block *srgb;
469 mpls_label_t label;
470
471 switch (sid->type) {
472 case TILFA_SID_PREFIX:
473 if (sid->value.index.remote)
474 target_node = sid->value.index.remote_sysid;
475 else
476 target_node = sadj->id;
477 srgb = isis_sr_find_srgb(lspdb, target_node);
478 if (!srgb) {
479 zlog_warn("%s: SRGB not found for node %s",
480 __func__,
481 print_sys_hostname(target_node));
482 goto error;
483 }
484
485 /* Check if the SID index falls inside the SRGB. */
486 if (sid->value.index.value >= srgb->range_size) {
487 flog_warn(
488 EC_ISIS_SID_OVERFLOW,
489 "%s: SID index %u falls outside remote SRGB range",
490 __func__, sid->value.index.value);
491 goto error;
492 }
493
494 /*
495 * Prefix-SID: map SID index to label value within the
496 * SRGB.
497 */
498 label = srgb->lower_bound + sid->value.index.value;
499 break;
500 case TILFA_SID_ADJ:
501 /* Adj-SID: absolute label value can be used directly */
502 label = sid->value.label;
503 break;
504 default:
505 flog_err(EC_LIB_DEVELOPMENT,
506 "%s: unknown TI-LFA SID type [%u]", __func__,
507 sid->type);
508 exit(1);
509 }
510 label_stack->label[i++] = label;
511 }
512
513 return label_stack;
514
515 error:
516 XFREE(MTYPE_ISIS_NEXTHOP_LABELS, label_stack);
517 return NULL;
518 }
519
520 static int tilfa_repair_list_apply(struct isis_spftree *spftree,
521 struct isis_vertex *vertex_dest,
522 const struct isis_vertex *vertex_pnode,
523 const struct list *repair_list)
524 {
525 struct isis_vertex_adj *vadj;
526 struct listnode *node;
527
528 for (ALL_LIST_ELEMENTS_RO(vertex_dest->Adj_N, node, vadj)) {
529 struct isis_spf_adj *sadj = vadj->sadj;
530 struct mpls_label_stack *label_stack;
531
532 /*
533 * Don't try to apply the repair list if one was already applied
534 * before (can't have ECMP past the P-node).
535 */
536 if (vadj->label_stack)
537 continue;
538
539 if (!isis_vertex_adj_exists(spftree, vertex_pnode, sadj))
540 continue;
541
542 label_stack = tilfa_compute_label_stack(spftree->lspdb, sadj,
543 repair_list);
544 if (!label_stack) {
545 char buf[VID2STR_BUFFER];
546
547 vid2string(vertex_dest, buf, sizeof(buf));
548 zlog_warn(
549 "%s: %s %s adjacency %s: failed to compute label stack",
550 __func__, vtype2string(vertex_dest->type), buf,
551 print_sys_hostname(sadj->id));
552 return -1;
553 }
554
555 vadj->label_stack = label_stack;
556 }
557
558 return 0;
559 }
560
561 /*
562 * Check if a node belongs to the extended P-space corresponding to a given
563 * destination.
564 */
565 static bool lfa_ext_p_space_check(const struct isis_spftree *spftree_pc,
566 const struct isis_vertex *vertex_dest,
567 const struct isis_vertex *vertex)
568 {
569 struct isis_spftree *spftree_old = spftree_pc->lfa.old.spftree;
570 struct isis_vertex_adj *vadj;
571 struct listnode *node;
572
573 /* Check the local P-space first. */
574 if (isis_spf_node_find(&spftree_pc->lfa.p_space, vertex->N.id))
575 return true;
576
577 /*
578 * Check the P-space of the adjacent routers used to reach the
579 * destination.
580 */
581 for (ALL_LIST_ELEMENTS_RO(vertex_dest->Adj_N, node, vadj)) {
582 struct isis_spf_adj *sadj = vadj->sadj;
583 struct isis_spf_node *adj_node;
584
585 adj_node =
586 isis_spf_node_find(&spftree_old->adj_nodes, sadj->id);
587 if (!adj_node)
588 continue;
589
590 if (isis_spf_node_find(&adj_node->lfa.p_space, vertex->N.id))
591 return true;
592 }
593
594 return false;
595 }
596
597 /* Check if a node belongs to the Q-space. */
598 static bool lfa_q_space_check(const struct isis_spftree *spftree_pc,
599 const struct isis_vertex *vertex)
600 {
601 return isis_spf_node_find(&spftree_pc->lfa.q_space, vertex->N.id);
602 }
603
604 /* This is a recursive function. */
605 static int tilfa_build_repair_list(struct isis_spftree *spftree_pc,
606 struct isis_vertex *vertex_dest,
607 const struct isis_vertex *vertex,
608 const struct isis_vertex *vertex_child,
609 struct isis_spf_nodes *used_pnodes,
610 struct list *repair_list)
611 {
612 struct isis_vertex *pvertex;
613 struct listnode *node;
614 bool is_pnode, is_qnode;
615 char buf[VID2STR_BUFFER];
616 struct isis_tilfa_sid sid_dest = {}, sid_qnode = {}, sid_pnode = {};
617 uint32_t sid_index;
618 mpls_label_t label_qnode;
619
620 if (IS_DEBUG_LFA) {
621 vid2string(vertex, buf, sizeof(buf));
622 zlog_debug("ISIS-LFA: vertex %s %s", vtype2string(vertex->type),
623 buf);
624 }
625
626 /* Push original Prefix-SID label when necessary. */
627 if (VTYPE_IP(vertex->type) && vertex->N.ip.sr.present) {
628 pvertex = listnode_head(vertex->parents);
629 assert(pvertex);
630
631 sid_index = vertex->N.ip.sr.sid.value;
632 if (IS_DEBUG_LFA)
633 zlog_debug(
634 "ISIS-LFA: pushing Prefix-SID to %pFX (index %u)",
635 &vertex->N.ip.p.dest, sid_index);
636 sid_dest.type = TILFA_SID_PREFIX;
637 sid_dest.value.index.value = sid_index;
638 sid_dest.value.index.remote = true;
639 memcpy(sid_dest.value.index.remote_sysid, pvertex->N.id,
640 sizeof(sid_dest.value.index.remote_sysid));
641 listnode_add_head(repair_list, &sid_dest);
642 }
643
644 if (!vertex_child)
645 goto parents;
646 if (vertex->type != VTYPE_NONPSEUDO_IS
647 && vertex->type != VTYPE_NONPSEUDO_TE_IS)
648 goto parents;
649 if (!VTYPE_IS(vertex_child->type))
650 vertex_child = NULL;
651
652 /* Check if node is part of the extended P-space and/or Q-space. */
653 is_pnode = lfa_ext_p_space_check(spftree_pc, vertex_dest, vertex);
654 is_qnode = lfa_q_space_check(spftree_pc, vertex);
655
656 /* Push Adj-SID label when necessary. */
657 if ((!is_qnode
658 || spftree_pc->lfa.protected_resource.type == LFA_NODE_PROTECTION)
659 && vertex_child) {
660 /*
661 * If vertex is the penultimate hop router, then pushing an
662 * Adj-SID towards the final hop means that the No-PHP flag of
663 * the original Prefix-SID must be honored. We do that by
664 * removing the previously added Prefix-SID from the repair list
665 * when those conditions are met.
666 */
667 if (vertex->depth == (vertex_dest->depth - 2)
668 && VTYPE_IP(vertex_dest->type)
669 && vertex_dest->N.ip.sr.present
670 && !CHECK_FLAG(vertex_dest->N.ip.sr.sid.flags,
671 ISIS_PREFIX_SID_NO_PHP)) {
672 list_delete_all_node(repair_list);
673 }
674
675 label_qnode = tilfa_find_qnode_adj_sid(spftree_pc, vertex->N.id,
676 vertex_child->N.id);
677 if (label_qnode == MPLS_INVALID_LABEL) {
678 zlog_warn("ISIS-LFA: failed to find %s->%s Adj-SID",
679 print_sys_hostname(vertex->N.id),
680 print_sys_hostname(vertex_child->N.id));
681 return -1;
682 }
683 if (IS_DEBUG_LFA)
684 zlog_debug(
685 "ISIS-LFA: pushing %s->%s Adj-SID (label %u)",
686 print_sys_hostname(vertex->N.id),
687 print_sys_hostname(vertex_child->N.id),
688 label_qnode);
689 sid_qnode.type = TILFA_SID_ADJ;
690 sid_qnode.value.label = label_qnode;
691 listnode_add_head(repair_list, &sid_qnode);
692 }
693
694 /* Push Prefix-SID label when necessary. */
695 if (is_pnode) {
696 /* The same P-node can't be used more than once. */
697 if (isis_spf_node_find(used_pnodes, vertex->N.id)) {
698 if (IS_DEBUG_LFA)
699 zlog_debug(
700 "ISIS-LFA: skipping already used P-node");
701 return 0;
702 }
703 isis_spf_node_new(used_pnodes, vertex->N.id);
704
705 if (!vertex_child) {
706 if (IS_DEBUG_LFA)
707 zlog_debug(
708 "ISIS-LFA: destination is within Ext-P-Space");
709 return 0;
710 }
711
712 sid_index =
713 tilfa_find_pnode_prefix_sid(spftree_pc, vertex->N.id);
714 if (sid_index == UINT32_MAX) {
715 zlog_warn(
716 "ISIS-LFA: failed to find Prefix-SID corresponding to PQ-node %s",
717 print_sys_hostname(vertex->N.id));
718 return -1;
719 }
720
721 if (IS_DEBUG_LFA)
722 zlog_debug(
723 "ISIS-LFA: pushing Node-SID to %s (index %u)",
724 print_sys_hostname(vertex->N.id), sid_index);
725 sid_pnode.type = TILFA_SID_PREFIX;
726 sid_pnode.value.index.value = sid_index;
727 listnode_add_head(repair_list, &sid_pnode);
728
729 /* Apply repair list. */
730 if (spftree_pc->area->srdb.config.msd
731 && listcount(repair_list)
732 > spftree_pc->area->srdb.config.msd) {
733 zlog_warn(
734 "ISIS-LFA: list of repair segments exceeds locally configured MSD (%u > %u)",
735 listcount(repair_list),
736 spftree_pc->area->srdb.config.msd);
737 return -1;
738 }
739 if (tilfa_repair_list_apply(spftree_pc, vertex_dest, vertex,
740 repair_list)
741 != 0)
742 return -1;
743 return 0;
744 }
745
746 parents:
747 for (ALL_LIST_ELEMENTS_RO(vertex->parents, node, pvertex)) {
748 struct list *repair_list_parent;
749 bool ecmp;
750 int ret;
751
752 ecmp = (listcount(vertex->parents) > 1) ? true : false;
753 repair_list_parent = ecmp ? list_dup(repair_list) : repair_list;
754 ret = tilfa_build_repair_list(spftree_pc, vertex_dest, pvertex,
755 vertex, used_pnodes,
756 repair_list_parent);
757 if (ecmp)
758 list_delete(&repair_list_parent);
759 if (ret != 0)
760 return ret;
761 }
762
763 return 0;
764 }
765
766 static const char *lfa_protection_type2str(enum lfa_protection_type type)
767 {
768 switch (type) {
769 case LFA_LINK_PROTECTION:
770 return "link protection";
771 case LFA_NODE_PROTECTION:
772 return "node protection";
773 default:
774 return "unknown protection type";
775 }
776 }
777
778 static const char *
779 lfa_protected_resource2str(const struct lfa_protected_resource *resource)
780 {
781 const uint8_t *fail_id;
782 static char buffer[128];
783
784 fail_id = resource->adjacency;
785 snprintf(buffer, sizeof(buffer), "%s.%u's failure (%s)",
786 print_sys_hostname(fail_id), LSP_PSEUDO_ID(fail_id),
787 lfa_protection_type2str(resource->type));
788
789 return buffer;
790 }
791
792 static bool
793 spf_adj_check_is_affected(const struct isis_spf_adj *sadj,
794 const struct lfa_protected_resource *resource,
795 const uint8_t *root_sysid, bool reverse)
796 {
797 if (!!CHECK_FLAG(sadj->flags, F_ISIS_SPF_ADJ_BROADCAST)
798 != !!LSP_PSEUDO_ID(resource->adjacency))
799 return false;
800
801 if (CHECK_FLAG(sadj->flags, F_ISIS_SPF_ADJ_BROADCAST)) {
802 if (!memcmp(sadj->lan.desig_is_id, resource->adjacency,
803 ISIS_SYS_ID_LEN + 1))
804 return true;
805 } else {
806 if (!reverse
807 && !memcmp(sadj->id, resource->adjacency, ISIS_SYS_ID_LEN))
808 return true;
809 if (reverse && !memcmp(sadj->id, root_sysid, ISIS_SYS_ID_LEN))
810 return true;
811 }
812
813 return false;
814 }
815
816 /* Check if the given vertex is affected by a given local failure. */
817 static bool
818 spf_vertex_check_is_affected(const struct isis_vertex *vertex,
819 const uint8_t *root_sysid,
820 const struct lfa_protected_resource *resource)
821 {
822 struct isis_vertex_adj *vadj;
823 struct listnode *node;
824 size_t affected_nhs = 0;
825
826 /* Local routes don't need protection. */
827 if (VTYPE_IP(vertex->type) && vertex->depth == 1)
828 return false;
829
830 for (ALL_LIST_ELEMENTS_RO(vertex->Adj_N, node, vadj)) {
831 struct isis_spf_adj *sadj = vadj->sadj;
832
833 if (spf_adj_check_is_affected(sadj, resource, root_sysid,
834 false))
835 affected_nhs++;
836 }
837
838 /*
839 * No need to compute backup paths for ECMP routes, except if all
840 * primary nexthops share the same broadcast interface.
841 */
842 if (listcount(vertex->Adj_N) == affected_nhs)
843 return true;
844
845 return false;
846 }
847
848 /* Check if a given RLFA/TI-LFA post-convergence SPF vertex needs protection. */
849 static bool lfa_check_needs_protection(const struct isis_spftree *spftree_pc,
850 const struct isis_vertex *vertex)
851 {
852 struct isis_vertex *vertex_old;
853
854 /* Only local adjacencies need TI-LFA Adj-SID protection. */
855 if (spftree_pc->type == SPF_TYPE_TI_LFA && VTYPE_IS(vertex->type)
856 && !isis_adj_find(spftree_pc->area, spftree_pc->level,
857 vertex->N.id))
858 return false;
859
860 vertex_old = isis_find_vertex(&spftree_pc->lfa.old.spftree->paths,
861 &vertex->N, vertex->type);
862 if (!vertex_old)
863 return false;
864
865 /* Skip vertex if it's already protected by local LFA. */
866 if (CHECK_FLAG(vertex_old->flags, F_ISIS_VERTEX_LFA_PROTECTED))
867 return false;
868
869 return spf_vertex_check_is_affected(
870 vertex_old, spftree_pc->sysid,
871 &spftree_pc->lfa.protected_resource);
872 }
873
874 /**
875 * Check if the given SPF vertex needs protection and, if so, compute and
876 * install the corresponding repair paths.
877 *
878 * @param spftree_pc The post-convergence SPF tree
879 * @param vertex IS-IS SPF vertex to check
880 *
881 * @return 0 if the vertex needs to be protected, -1 otherwise
882 */
883 int isis_tilfa_check(struct isis_spftree *spftree_pc,
884 struct isis_vertex *vertex)
885 {
886 struct isis_spf_nodes used_pnodes;
887 char buf[VID2STR_BUFFER];
888 struct list *repair_list;
889 int ret;
890
891 if (!spftree_pc->area->srdb.enabled)
892 return -1;
893
894 if (!lfa_check_needs_protection(spftree_pc, vertex)) {
895 if (IS_DEBUG_LFA)
896 zlog_debug(
897 "ISIS-LFA: %s %s unaffected by %s",
898 vtype2string(vertex->type),
899 vid2string(vertex, buf, sizeof(buf)),
900 lfa_protected_resource2str(
901 &spftree_pc->lfa.protected_resource));
902
903 return -1;
904 }
905
906 /*
907 * Check if the route/adjacency was already covered by node protection.
908 */
909 if (VTYPE_IS(vertex->type)) {
910 struct isis_adjacency *adj;
911
912 adj = isis_adj_find(spftree_pc->area, spftree_pc->level,
913 vertex->N.id);
914 if (adj
915 && isis_sr_adj_sid_find(adj, spftree_pc->family,
916 ISIS_SR_LAN_BACKUP)) {
917 if (IS_DEBUG_LFA)
918 zlog_debug(
919 "ISIS-LFA: %s %s already covered by node protection",
920 vtype2string(vertex->type),
921 vid2string(vertex, buf, sizeof(buf)));
922
923 return -1;
924 }
925 }
926 if (VTYPE_IP(vertex->type)) {
927 struct route_table *route_table;
928
929 route_table = spftree_pc->lfa.old.spftree->route_table_backup;
930 if (route_node_lookup(route_table, &vertex->N.ip.p.dest)) {
931 if (IS_DEBUG_LFA)
932 zlog_debug(
933 "ISIS-LFA: %s %s already covered by node protection",
934 vtype2string(vertex->type),
935 vid2string(vertex, buf, sizeof(buf)));
936
937 return -1;
938 }
939 }
940
941 if (IS_DEBUG_LFA)
942 zlog_debug(
943 "ISIS-LFA: computing repair path(s) of %s %s w.r.t %s",
944 vtype2string(vertex->type),
945 vid2string(vertex, buf, sizeof(buf)),
946 lfa_protected_resource2str(
947 &spftree_pc->lfa.protected_resource));
948
949 /* Create base repair list. */
950 repair_list = list_new();
951
952 isis_spf_node_list_init(&used_pnodes);
953 ret = tilfa_build_repair_list(spftree_pc, vertex, vertex, NULL,
954 &used_pnodes, repair_list);
955 isis_spf_node_list_clear(&used_pnodes);
956 list_delete(&repair_list);
957 if (ret != 0)
958 zlog_warn(
959 "ISIS-LFA: failed to compute repair path(s) of %s %s w.r.t %s",
960 vtype2string(vertex->type),
961 vid2string(vertex, buf, sizeof(buf)),
962 lfa_protected_resource2str(
963 &spftree_pc->lfa.protected_resource));
964
965 return ret;
966 }
967
968 static bool
969 spf_adj_node_is_affected(struct isis_spf_node *adj_node,
970 const struct lfa_protected_resource *resource,
971 const uint8_t *root_sysid)
972 {
973 struct isis_spf_adj *sadj;
974 struct listnode *node;
975
976 for (ALL_LIST_ELEMENTS_RO(adj_node->adjacencies, node, sadj)) {
977 if (sadj->metric != adj_node->best_metric)
978 continue;
979 if (spf_adj_check_is_affected(sadj, resource, root_sysid,
980 false))
981 return true;
982 }
983
984 return false;
985 }
986
987 static bool vertex_is_affected(struct isis_spftree *spftree_root,
988 const struct isis_spf_nodes *adj_nodes,
989 bool p_space, const struct isis_vertex *vertex,
990 const struct lfa_protected_resource *resource)
991 {
992 struct isis_vertex *pvertex;
993 struct listnode *node, *vnode;
994
995 for (ALL_LIST_ELEMENTS_RO(vertex->parents, node, pvertex)) {
996 struct isis_spftree *spftree_parent;
997 struct isis_vertex *vertex_child;
998 struct isis_vertex_adj *vadj;
999 bool reverse = false;
1000
1001 if (p_space && resource->type == LFA_NODE_PROTECTION) {
1002 if (isis_spf_node_find(&resource->nodes, vertex->N.id))
1003 return true;
1004 goto parents;
1005 }
1006
1007 /* Check if either the vertex or its parent is the root node. */
1008 if (memcmp(vertex->N.id, spftree_root->sysid, ISIS_SYS_ID_LEN)
1009 && memcmp(pvertex->N.id, spftree_root->sysid,
1010 ISIS_SYS_ID_LEN))
1011 goto parents;
1012
1013 /* Get SPT of the parent vertex. */
1014 if (!memcmp(pvertex->N.id, spftree_root->sysid,
1015 ISIS_SYS_ID_LEN))
1016 spftree_parent = spftree_root;
1017 else {
1018 struct isis_spf_node *adj_node;
1019
1020 adj_node = isis_spf_node_find(adj_nodes, pvertex->N.id);
1021 assert(adj_node);
1022 spftree_parent = adj_node->lfa.spftree;
1023 assert(spftree_parent);
1024 reverse = true;
1025 }
1026
1027 /* Get paths pvertex uses to reach vertex. */
1028 vertex_child = isis_find_vertex(&spftree_parent->paths,
1029 &vertex->N, vertex->type);
1030 if (!vertex_child)
1031 goto parents;
1032
1033 /* Check if any of these paths use the protected resource. */
1034 for (ALL_LIST_ELEMENTS_RO(vertex_child->Adj_N, vnode, vadj))
1035 if (spf_adj_check_is_affected(vadj->sadj, resource,
1036 spftree_root->sysid,
1037 reverse))
1038 return true;
1039
1040 parents:
1041 if (vertex_is_affected(spftree_root, adj_nodes, p_space,
1042 pvertex, resource))
1043 return true;
1044 }
1045
1046 return false;
1047 }
1048
1049 /* Calculate set of nodes reachable without using the protected interface. */
1050 static void lfa_calc_reach_nodes(struct isis_spftree *spftree,
1051 struct isis_spftree *spftree_root,
1052 const struct isis_spf_nodes *adj_nodes,
1053 bool p_space,
1054 const struct lfa_protected_resource *resource,
1055 struct isis_spf_nodes *nodes)
1056 {
1057 struct isis_vertex *vertex;
1058 struct listnode *node;
1059
1060 for (ALL_QUEUE_ELEMENTS_RO(&spftree->paths, node, vertex)) {
1061 char buf[VID2STR_BUFFER];
1062
1063 if (!VTYPE_IS(vertex->type))
1064 continue;
1065
1066 /* Skip root node. */
1067 if (!memcmp(vertex->N.id, spftree_root->sysid, ISIS_SYS_ID_LEN))
1068 continue;
1069
1070 /* Don't add the same node twice. */
1071 if (isis_spf_node_find(nodes, vertex->N.id))
1072 continue;
1073
1074 if (!vertex_is_affected(spftree_root, adj_nodes, p_space,
1075 vertex, resource)) {
1076 if (IS_DEBUG_LFA)
1077 zlog_debug(
1078 "ISIS-LFA: adding %s",
1079 vid2string(vertex, buf, sizeof(buf)));
1080
1081 isis_spf_node_new(nodes, vertex->N.id);
1082 }
1083 }
1084 }
1085
1086 /**
1087 * Helper function used to create an SPF tree structure and run reverse SPF on
1088 * it.
1089 *
1090 * @param spftree IS-IS SPF tree
1091 *
1092 * @return Pointer to new SPF tree structure.
1093 */
1094 struct isis_spftree *isis_spf_reverse_run(const struct isis_spftree *spftree)
1095 {
1096 struct isis_spftree *spftree_reverse;
1097
1098 spftree_reverse = isis_spftree_new(
1099 spftree->area, spftree->lspdb, spftree->sysid, spftree->level,
1100 spftree->tree_id, SPF_TYPE_REVERSE,
1101 F_SPFTREE_NO_ADJACENCIES | F_SPFTREE_NO_ROUTES);
1102 isis_run_spf(spftree_reverse);
1103
1104 return spftree_reverse;
1105 }
1106
1107 /*
1108 * Calculate the Extended P-space and Q-space associated to a given link
1109 * failure.
1110 */
1111 static void lfa_calc_pq_spaces(struct isis_spftree *spftree_pc,
1112 const struct lfa_protected_resource *resource)
1113 {
1114 struct isis_spftree *spftree;
1115 struct isis_spftree *spftree_reverse;
1116 struct isis_spf_nodes *adj_nodes;
1117 struct isis_spf_node *adj_node;
1118
1119 /* Obtain pre-failure SPTs and list of adjacent nodes. */
1120 spftree = spftree_pc->lfa.old.spftree;
1121 spftree_reverse = spftree_pc->lfa.old.spftree_reverse;
1122 adj_nodes = &spftree->adj_nodes;
1123
1124 if (IS_DEBUG_LFA)
1125 zlog_debug("ISIS-LFA: computing P-space (self)");
1126 lfa_calc_reach_nodes(spftree, spftree, adj_nodes, true, resource,
1127 &spftree_pc->lfa.p_space);
1128
1129 RB_FOREACH (adj_node, isis_spf_nodes, adj_nodes) {
1130 if (spf_adj_node_is_affected(adj_node, resource,
1131 spftree->sysid)) {
1132 if (IS_DEBUG_LFA)
1133 zlog_debug("ISIS-LFA: computing Q-space (%s)",
1134 print_sys_hostname(adj_node->sysid));
1135
1136 /*
1137 * Compute the reverse SPF in the behalf of the node
1138 * adjacent to the failure, if we haven't done that
1139 * before
1140 */
1141 if (!adj_node->lfa.spftree_reverse)
1142 adj_node->lfa.spftree_reverse =
1143 isis_spf_reverse_run(
1144 adj_node->lfa.spftree);
1145
1146 lfa_calc_reach_nodes(adj_node->lfa.spftree_reverse,
1147 spftree_reverse, adj_nodes, false,
1148 resource,
1149 &spftree_pc->lfa.q_space);
1150 } else {
1151 if (IS_DEBUG_LFA)
1152 zlog_debug("ISIS-LFA: computing P-space (%s)",
1153 print_sys_hostname(adj_node->sysid));
1154 lfa_calc_reach_nodes(adj_node->lfa.spftree, spftree,
1155 adj_nodes, true, resource,
1156 &adj_node->lfa.p_space);
1157 }
1158 }
1159 }
1160
1161 /**
1162 * Compute the TI-LFA backup paths for a given protected interface.
1163 *
1164 * @param area IS-IS area
1165 * @param spftree IS-IS SPF tree
1166 * @param spftree_reverse IS-IS Reverse SPF tree
1167 * @param resource Protected resource
1168 *
1169 * @return Pointer to the post-convergence SPF tree
1170 */
1171 struct isis_spftree *isis_tilfa_compute(struct isis_area *area,
1172 struct isis_spftree *spftree,
1173 struct isis_spftree *spftree_reverse,
1174 struct lfa_protected_resource *resource)
1175 {
1176 struct isis_spftree *spftree_pc;
1177 struct isis_spf_node *adj_node;
1178
1179 if (IS_DEBUG_LFA)
1180 zlog_debug("ISIS-LFA: computing TI-LFAs for %s",
1181 lfa_protected_resource2str(resource));
1182
1183 /* Populate list of nodes affected by link failure. */
1184 if (resource->type == LFA_NODE_PROTECTION) {
1185 isis_spf_node_list_init(&resource->nodes);
1186 RB_FOREACH (adj_node, isis_spf_nodes, &spftree->adj_nodes) {
1187 if (spf_adj_node_is_affected(adj_node, resource,
1188 spftree->sysid))
1189 isis_spf_node_new(&resource->nodes,
1190 adj_node->sysid);
1191 }
1192 }
1193
1194 /* Create post-convergence SPF tree. */
1195 spftree_pc = isis_spftree_new(area, spftree->lspdb, spftree->sysid,
1196 spftree->level, spftree->tree_id,
1197 SPF_TYPE_TI_LFA, spftree->flags);
1198 spftree_pc->lfa.old.spftree = spftree;
1199 spftree_pc->lfa.old.spftree_reverse = spftree_reverse;
1200 spftree_pc->lfa.protected_resource = *resource;
1201
1202 /* Compute the extended P-space and Q-space. */
1203 lfa_calc_pq_spaces(spftree_pc, resource);
1204
1205 if (IS_DEBUG_LFA)
1206 zlog_debug(
1207 "ISIS-LFA: computing the post convergence SPT w.r.t. %s",
1208 lfa_protected_resource2str(resource));
1209
1210 /* Re-run SPF in the local node to find the post-convergence paths. */
1211 isis_run_spf(spftree_pc);
1212
1213 /* Clear list of nodes affeted by link failure. */
1214 if (resource->type == LFA_NODE_PROTECTION)
1215 isis_spf_node_list_clear(&resource->nodes);
1216
1217 return spftree_pc;
1218 }
1219
1220 /**
1221 * Run forward SPF on all adjacent routers.
1222 *
1223 * @param spftree IS-IS SPF tree
1224 *
1225 * @return 0 on success, -1 otherwise
1226 */
1227 int isis_spf_run_neighbors(struct isis_spftree *spftree)
1228 {
1229 struct isis_lsp *lsp;
1230 struct isis_spf_node *adj_node;
1231
1232 lsp = isis_root_system_lsp(spftree->lspdb, spftree->sysid);
1233 if (!lsp)
1234 return -1;
1235
1236 RB_FOREACH (adj_node, isis_spf_nodes, &spftree->adj_nodes) {
1237 if (IS_DEBUG_LFA)
1238 zlog_debug("ISIS-LFA: running SPF on neighbor %s",
1239 print_sys_hostname(adj_node->sysid));
1240
1241 /* Compute the SPT on behalf of the neighbor. */
1242 adj_node->lfa.spftree = isis_spftree_new(
1243 spftree->area, spftree->lspdb, adj_node->sysid,
1244 spftree->level, spftree->tree_id, SPF_TYPE_FORWARD,
1245 F_SPFTREE_NO_ADJACENCIES | F_SPFTREE_NO_ROUTES);
1246 isis_run_spf(adj_node->lfa.spftree);
1247 }
1248
1249 return 0;
1250 }
1251
1252 /* Find Router ID of PQ node. */
1253 static struct in_addr *rlfa_pq_node_rtr_id(struct isis_spftree *spftree,
1254 const struct isis_vertex *vertex_pq)
1255 {
1256 struct isis_lsp *lsp;
1257
1258 lsp = isis_root_system_lsp(spftree->lspdb, vertex_pq->N.id);
1259 if (!lsp)
1260 return NULL;
1261
1262 if (lsp->tlvs->router_cap->router_id.s_addr == INADDR_ANY)
1263 return NULL;
1264
1265 return &lsp->tlvs->router_cap->router_id;
1266 }
1267
1268 /* Find PQ node by intersecting the P/Q spaces. This is a recursive function. */
1269 static const struct in_addr *
1270 rlfa_find_pq_node(struct isis_spftree *spftree_pc,
1271 struct isis_vertex *vertex_dest,
1272 const struct isis_vertex *vertex,
1273 const struct isis_vertex *vertex_child)
1274 {
1275 struct isis_area *area = spftree_pc->area;
1276 int level = spftree_pc->level;
1277 struct isis_vertex *pvertex;
1278 struct listnode *node;
1279 bool is_pnode, is_qnode;
1280
1281 if (!vertex_child)
1282 goto parents;
1283 if (vertex->type != VTYPE_NONPSEUDO_IS
1284 && vertex->type != VTYPE_NONPSEUDO_TE_IS)
1285 goto parents;
1286 if (!VTYPE_IS(vertex_child->type))
1287 vertex_child = NULL;
1288
1289 /* Check if node is part of the extended P-space and/or Q-space. */
1290 is_pnode = lfa_ext_p_space_check(spftree_pc, vertex_dest, vertex);
1291 is_qnode = lfa_q_space_check(spftree_pc, vertex);
1292
1293 if (is_pnode && is_qnode) {
1294 const struct in_addr *rtr_id_pq;
1295 uint32_t max_metric;
1296 struct prefix_list *plist = NULL;
1297
1298 rtr_id_pq = rlfa_pq_node_rtr_id(spftree_pc, vertex);
1299 if (!rtr_id_pq) {
1300 if (IS_DEBUG_LFA) {
1301 char buf[VID2STR_BUFFER];
1302
1303 vid2string(vertex, buf, sizeof(buf));
1304 zlog_debug(
1305 "ISIS-LFA: tentative PQ node (%s %s) doesn't have a router-ID",
1306 vtype2string(vertex->type), buf);
1307 }
1308 goto parents;
1309 }
1310
1311 max_metric = spftree_pc->lfa.remote.max_metric;
1312 if (max_metric && vertex->d_N > max_metric) {
1313 if (IS_DEBUG_LFA)
1314 zlog_debug(
1315 "ISIS-LFA: skipping PQ node %pI4 (maximum metric)",
1316 rtr_id_pq);
1317 goto parents;
1318 }
1319
1320 plist = area->rlfa_plist[level - 1];
1321 if (plist) {
1322 struct prefix p;
1323
1324 p.family = AF_INET;
1325 p.prefixlen = IPV4_MAX_BITLEN;
1326 p.u.prefix4 = *rtr_id_pq;
1327 if (prefix_list_apply(plist, &p) == PREFIX_DENY) {
1328 if (IS_DEBUG_LFA)
1329 zlog_debug(
1330 "ISIS-LFA: PQ node %pI4 filtered by prefix-list",
1331 rtr_id_pq);
1332 goto parents;
1333 }
1334 }
1335
1336 if (IS_DEBUG_LFA)
1337 zlog_debug("ISIS-LFA: found PQ node: %pI4", rtr_id_pq);
1338
1339 return rtr_id_pq;
1340 }
1341
1342 parents:
1343 for (ALL_LIST_ELEMENTS_RO(vertex->parents, node, pvertex)) {
1344 const struct in_addr *rtr_id_pq;
1345
1346 rtr_id_pq = rlfa_find_pq_node(spftree_pc, vertex_dest, pvertex,
1347 vertex);
1348 if (rtr_id_pq)
1349 return rtr_id_pq;
1350 }
1351
1352 return NULL;
1353 }
1354
1355 int rlfa_cmp(const struct rlfa *a, const struct rlfa *b)
1356 {
1357 return prefix_cmp(&a->prefix, &b->prefix);
1358 }
1359
1360 static struct rlfa *rlfa_add(struct isis_spftree *spftree,
1361 struct isis_vertex *vertex,
1362 struct in_addr pq_address)
1363 {
1364 struct rlfa *rlfa;
1365
1366 assert(VTYPE_IP(vertex->type));
1367 rlfa = XCALLOC(MTYPE_ISIS_RLFA, sizeof(*rlfa));
1368 rlfa->prefix = vertex->N.ip.p.dest;
1369 rlfa->vertex = vertex;
1370 rlfa->pq_address = pq_address;
1371 rlfa_tree_add(&spftree->lfa.remote.rlfas, rlfa);
1372
1373 return rlfa;
1374 }
1375
1376 static void rlfa_delete(struct isis_spftree *spftree, struct rlfa *rlfa)
1377 {
1378 rlfa_tree_del(&spftree->lfa.remote.rlfas, rlfa);
1379 XFREE(MTYPE_ISIS_RLFA, rlfa);
1380 }
1381
1382 static struct rlfa *rlfa_lookup(struct isis_spftree *spftree,
1383 union prefixconstptr pu)
1384 {
1385 struct rlfa s = {};
1386
1387 s.prefix = *pu.p;
1388 return rlfa_tree_find(&spftree->lfa.remote.rlfas, &s);
1389 }
1390
1391 static void isis_area_verify_routes_cb(struct thread *thread)
1392 {
1393 struct isis_area *area = THREAD_ARG(thread);
1394
1395 if (IS_DEBUG_LFA)
1396 zlog_debug("ISIS-LFA: updating RLFAs in the RIB");
1397
1398 isis_area_verify_routes(area);
1399 }
1400
1401 static mpls_label_t rlfa_nexthop_label(struct isis_spftree *spftree,
1402 struct isis_vertex_adj *vadj,
1403 struct zapi_rlfa_response *response)
1404 {
1405 struct isis_spf_adj *sadj = vadj->sadj;
1406 struct isis_adjacency *adj = sadj->adj;
1407
1408 /*
1409 * Special case to make unit tests work (use implicit-null labels
1410 * instead of artifical ones).
1411 */
1412 if (CHECK_FLAG(spftree->flags, F_SPFTREE_NO_ADJACENCIES))
1413 return MPLS_LABEL_IMPLICIT_NULL;
1414
1415 for (unsigned int i = 0; i < response->nexthop_num; i++) {
1416 switch (response->nexthops[i].family) {
1417 case AF_INET:
1418 for (unsigned int j = 0; j < adj->ipv4_address_count;
1419 j++) {
1420 struct in_addr addr = adj->ipv4_addresses[j];
1421
1422 if (!IPV4_ADDR_SAME(
1423 &addr,
1424 &response->nexthops[i].gate.ipv4))
1425 continue;
1426
1427 return response->nexthops[i].label;
1428 }
1429 break;
1430 case AF_INET6:
1431 for (unsigned int j = 0; j < adj->ll_ipv6_count; j++) {
1432 struct in6_addr addr = adj->ll_ipv6_addrs[j];
1433
1434 if (!IPV6_ADDR_SAME(
1435 &addr,
1436 &response->nexthops[i].gate.ipv6))
1437 continue;
1438
1439 return response->nexthops[i].label;
1440 }
1441 break;
1442
1443 default:
1444 break;
1445 }
1446 }
1447
1448 return MPLS_INVALID_LABEL;
1449 }
1450
1451 int isis_rlfa_activate(struct isis_spftree *spftree, struct rlfa *rlfa,
1452 struct zapi_rlfa_response *response)
1453 {
1454 struct isis_area *area = spftree->area;
1455 struct isis_vertex *vertex = rlfa->vertex;
1456 struct isis_vertex_adj *vadj;
1457 struct listnode *node;
1458
1459 for (ALL_LIST_ELEMENTS_RO(vertex->Adj_N, node, vadj)) {
1460 mpls_label_t ldp_label;
1461 struct mpls_label_stack *label_stack;
1462 size_t num_labels = 0;
1463 size_t i = 0;
1464
1465 ldp_label = rlfa_nexthop_label(spftree, vadj, response);
1466 if (ldp_label == MPLS_INVALID_LABEL) {
1467 if (IS_DEBUG_LFA)
1468 zlog_debug(
1469 "ISIS-LFA: failed to activate RLFA: missing LDP label to reach PQ node through %s",
1470 sysid_print(vadj->sadj->id));
1471 return -1;
1472 }
1473
1474 if (ldp_label != MPLS_LABEL_IMPLICIT_NULL)
1475 num_labels++;
1476 if (response->pq_label != MPLS_LABEL_IMPLICIT_NULL)
1477 num_labels++;
1478 if (vadj->sr.present
1479 && vadj->sr.label != MPLS_LABEL_IMPLICIT_NULL)
1480 num_labels++;
1481
1482 /* Allocate label stack. */
1483 label_stack =
1484 XCALLOC(MTYPE_ISIS_NEXTHOP_LABELS,
1485 sizeof(struct mpls_label_stack)
1486 + num_labels * sizeof(mpls_label_t));
1487 label_stack->num_labels = num_labels;
1488
1489 /* Push label allocated by the nexthop (outer label). */
1490 if (ldp_label != MPLS_LABEL_IMPLICIT_NULL)
1491 label_stack->label[i++] = ldp_label;
1492 /* Push label allocated by the PQ node (inner label). */
1493 if (response->pq_label != MPLS_LABEL_IMPLICIT_NULL)
1494 label_stack->label[i++] = response->pq_label;
1495 /* Preserve the original Prefix-SID label when it's present. */
1496 if (vadj->sr.present
1497 && vadj->sr.label != MPLS_LABEL_IMPLICIT_NULL)
1498 label_stack->label[i++] = vadj->sr.label;
1499
1500 vadj->label_stack = label_stack;
1501 }
1502
1503 isis_route_create(&vertex->N.ip.p.dest, &vertex->N.ip.p.src,
1504 vertex->d_N, vertex->depth, &vertex->N.ip.sr,
1505 vertex->Adj_N, true, area,
1506 spftree->route_table_backup);
1507 spftree->lfa.protection_counters.rlfa[vertex->N.ip.priority] += 1;
1508
1509 THREAD_OFF(area->t_rlfa_rib_update);
1510 thread_add_timer(master, isis_area_verify_routes_cb, area, 2,
1511 &area->t_rlfa_rib_update);
1512
1513 return 0;
1514 }
1515
1516 void isis_rlfa_deactivate(struct isis_spftree *spftree, struct rlfa *rlfa)
1517 {
1518 struct isis_area *area = spftree->area;
1519 struct isis_vertex *vertex = rlfa->vertex;
1520 struct route_node *rn;
1521
1522 rn = route_node_lookup(spftree->route_table_backup, &rlfa->prefix);
1523 if (!rn)
1524 return;
1525 isis_route_delete(area, rn, spftree->route_table_backup);
1526 spftree->lfa.protection_counters.rlfa[vertex->N.ip.priority] -= 1;
1527
1528 THREAD_OFF(area->t_rlfa_rib_update);
1529 thread_add_timer(master, isis_area_verify_routes_cb, area, 2,
1530 &area->t_rlfa_rib_update);
1531 }
1532
1533 void isis_rlfa_list_init(struct isis_spftree *spftree)
1534 {
1535 rlfa_tree_init(&spftree->lfa.remote.rlfas);
1536 }
1537
1538 void isis_rlfa_list_clear(struct isis_spftree *spftree)
1539 {
1540 while (rlfa_tree_count(&spftree->lfa.remote.rlfas) > 0) {
1541 struct rlfa *rlfa;
1542
1543 rlfa = rlfa_tree_first(&spftree->lfa.remote.rlfas);
1544 isis_rlfa_deactivate(spftree, rlfa);
1545 rlfa_delete(spftree, rlfa);
1546 }
1547 }
1548
1549 void isis_rlfa_process_ldp_response(struct zapi_rlfa_response *response)
1550 {
1551 struct isis *isis;
1552 struct isis_area *area;
1553 struct isis_spftree *spftree;
1554 struct rlfa *rlfa;
1555 enum spf_tree_id tree_id;
1556 uint32_t spf_run_id;
1557 int level;
1558
1559 if (response->igp.protocol != ZEBRA_ROUTE_ISIS)
1560 return;
1561
1562 isis = isis_lookup_by_vrfid(response->igp.vrf_id);
1563 if (!isis)
1564 return;
1565
1566 area = isis_area_lookup(response->igp.isis.area_tag,
1567 response->igp.vrf_id);
1568 if (!area)
1569 return;
1570
1571 tree_id = response->igp.isis.spf.tree_id;
1572 if (tree_id != SPFTREE_IPV4 && tree_id != SPFTREE_IPV6) {
1573 zlog_warn("ISIS-LFA: invalid SPF tree ID received from LDP");
1574 return;
1575 }
1576
1577 level = response->igp.isis.spf.level;
1578 if (level != ISIS_LEVEL1 && level != ISIS_LEVEL2) {
1579 zlog_warn("ISIS-LFA: invalid IS-IS level received from LDP");
1580 return;
1581 }
1582
1583 spf_run_id = response->igp.isis.spf.run_id;
1584 spftree = area->spftree[tree_id][level - 1];
1585 if (spftree->runcount != spf_run_id)
1586 /* Outdated RLFA, ignore... */
1587 return;
1588
1589 rlfa = rlfa_lookup(spftree, &response->destination);
1590 if (!rlfa) {
1591 zlog_warn(
1592 "ISIS-LFA: couldn't find Remote-LFA %pFX received from LDP",
1593 &response->destination);
1594 return;
1595 }
1596
1597 if (response->pq_label != MPLS_INVALID_LABEL) {
1598 if (IS_DEBUG_LFA)
1599 zlog_debug(
1600 "ISIS-LFA: activating/updating RLFA for %pFX",
1601 &rlfa->prefix);
1602
1603 if (isis_rlfa_activate(spftree, rlfa, response) != 0)
1604 isis_rlfa_deactivate(spftree, rlfa);
1605 } else {
1606 if (IS_DEBUG_LFA)
1607 zlog_debug("ISIS-LFA: deactivating RLFA for %pFX",
1608 &rlfa->prefix);
1609
1610 isis_rlfa_deactivate(spftree, rlfa);
1611 }
1612 }
1613
1614 void isis_ldp_rlfa_handle_client_close(struct zapi_client_close_info *info)
1615 {
1616 struct isis *isis = isis_lookup_by_vrfid(VRF_DEFAULT);
1617 struct isis_area *area;
1618 struct listnode *node;
1619
1620 if (!isis)
1621 return;
1622
1623 /* Check if the LDP main client session closed */
1624 if (info->proto != ZEBRA_ROUTE_LDP || info->session_id == 0)
1625 return;
1626
1627 if (IS_DEBUG_LFA)
1628 zlog_debug("ISIS-LFA: LDP is down, deactivating all RLFAs");
1629
1630 for (ALL_LIST_ELEMENTS_RO(isis->area_list, node, area)) {
1631 for (int tree = SPFTREE_IPV4; tree < SPFTREE_COUNT; tree++) {
1632 for (int level = ISIS_LEVEL1; level <= ISIS_LEVELS;
1633 level++) {
1634 struct isis_spftree *spftree;
1635
1636 if (!(area->is_type & level))
1637 continue;
1638 if (!area->spftree[tree][level - 1])
1639 continue;
1640
1641 spftree = area->spftree[tree][level - 1];
1642 isis_rlfa_list_clear(spftree);
1643 }
1644 }
1645 }
1646 }
1647
1648 /**
1649 * Check if the given SPF vertex needs protection and, if so, attempt to
1650 * compute a Remote LFA for it.
1651 *
1652 * @param spftree_pc The post-convergence SPF tree
1653 * @param vertex IS-IS SPF vertex to check
1654 */
1655 void isis_rlfa_check(struct isis_spftree *spftree_pc,
1656 struct isis_vertex *vertex)
1657 {
1658 struct isis_spftree *spftree_old = spftree_pc->lfa.old.spftree;
1659 struct rlfa *rlfa;
1660 const struct in_addr *rtr_id_pq;
1661 char buf[VID2STR_BUFFER];
1662
1663 if (!lfa_check_needs_protection(spftree_pc, vertex)) {
1664 if (IS_DEBUG_LFA)
1665 zlog_debug(
1666 "ISIS-LFA: %s %s unaffected by %s",
1667 vtype2string(vertex->type),
1668 vid2string(vertex, buf, sizeof(buf)),
1669 lfa_protected_resource2str(
1670 &spftree_pc->lfa.protected_resource));
1671
1672 return;
1673 }
1674
1675 if (IS_DEBUG_LFA)
1676 zlog_debug(
1677 "ISIS-LFA: computing repair path(s) of %s %s w.r.t %s",
1678 vtype2string(vertex->type),
1679 vid2string(vertex, buf, sizeof(buf)),
1680 lfa_protected_resource2str(
1681 &spftree_pc->lfa.protected_resource));
1682
1683 /* Find PQ node. */
1684 rtr_id_pq = rlfa_find_pq_node(spftree_pc, vertex, vertex, NULL);
1685 if (!rtr_id_pq) {
1686 if (IS_DEBUG_LFA)
1687 zlog_debug("ISIS-LFA: no acceptable PQ node found");
1688 return;
1689 }
1690
1691 /* Store valid RLFA and store LDP label for the PQ node. */
1692 rlfa = rlfa_add(spftree_old, vertex, *rtr_id_pq);
1693
1694 /* Register RLFA with LDP. */
1695 if (isis_zebra_rlfa_register(spftree_old, rlfa) != 0)
1696 rlfa_delete(spftree_old, rlfa);
1697 }
1698
1699 /**
1700 * Compute the Remote LFA backup paths for a given protected interface.
1701 *
1702 * @param area IS-IS area
1703 * @param spftree IS-IS SPF tree
1704 * @param spftree_reverse IS-IS Reverse SPF tree
1705 * @param max_metric Remote LFA maximum metric
1706 * @param resource Protected resource
1707 *
1708 * @return Pointer to the post-convergence SPF tree
1709 */
1710 struct isis_spftree *isis_rlfa_compute(struct isis_area *area,
1711 struct isis_spftree *spftree,
1712 struct isis_spftree *spftree_reverse,
1713 uint32_t max_metric,
1714 struct lfa_protected_resource *resource)
1715 {
1716 struct isis_spftree *spftree_pc;
1717
1718 if (IS_DEBUG_LFA)
1719 zlog_debug("ISIS-LFA: computing remote LFAs for %s",
1720 lfa_protected_resource2str(resource));
1721
1722 /* Create post-convergence SPF tree. */
1723 spftree_pc = isis_spftree_new(area, spftree->lspdb, spftree->sysid,
1724 spftree->level, spftree->tree_id,
1725 SPF_TYPE_RLFA, spftree->flags);
1726 spftree_pc->lfa.old.spftree = spftree;
1727 spftree_pc->lfa.old.spftree_reverse = spftree_reverse;
1728 spftree_pc->lfa.remote.max_metric = max_metric;
1729 spftree_pc->lfa.protected_resource = *resource;
1730
1731 /* Compute the extended P-space and Q-space. */
1732 lfa_calc_pq_spaces(spftree_pc, resource);
1733
1734 if (IS_DEBUG_LFA)
1735 zlog_debug(
1736 "ISIS-LFA: computing the post convergence SPT w.r.t. %s",
1737 lfa_protected_resource2str(resource));
1738
1739 /* Re-run SPF in the local node to find the post-convergence paths. */
1740 isis_run_spf(spftree_pc);
1741
1742 return spftree_pc;
1743 }
1744
1745 /* Calculate the distance from the root node to the given IP destination. */
1746 static int lfa_calc_dist_destination(struct isis_spftree *spftree,
1747 const struct isis_vertex *vertex_N,
1748 uint32_t *distance)
1749 {
1750 struct isis_vertex *vertex, *vertex_best = NULL;
1751
1752 switch (spftree->family) {
1753 case AF_INET:
1754 for (int vtype = VTYPE_IPREACH_INTERNAL;
1755 vtype <= VTYPE_IPREACH_TE; vtype++) {
1756 vertex = isis_find_vertex(
1757 &spftree->paths, &vertex_N->N.ip.p.dest, vtype);
1758 if (!vertex)
1759 continue;
1760
1761 /* Pick vertex with the best metric. */
1762 if (!vertex_best || vertex_best->d_N > vertex->d_N)
1763 vertex_best = vertex;
1764 }
1765 break;
1766 case AF_INET6:
1767 for (int vtype = VTYPE_IP6REACH_INTERNAL;
1768 vtype <= VTYPE_IP6REACH_EXTERNAL; vtype++) {
1769 vertex = isis_find_vertex(
1770 &spftree->paths, &vertex_N->N.ip.p.dest, vtype);
1771 if (!vertex)
1772 continue;
1773
1774 /* Pick vertex with the best metric. */
1775 if (!vertex_best || vertex_best->d_N > vertex->d_N)
1776 vertex_best = vertex;
1777 }
1778 break;
1779 default:
1780 break;
1781 }
1782
1783 if (!vertex_best)
1784 return -1;
1785
1786 assert(VTYPE_IP(vertex_best->type));
1787 vertex_best = listnode_head(vertex_best->parents);
1788 *distance = vertex_best->d_N;
1789
1790 return 0;
1791 }
1792
1793 /* Calculate the distance from the root node to the given node. */
1794 static int lfa_calc_dist_node(struct isis_spftree *spftree,
1795 const uint8_t *sysid, uint32_t *distance)
1796 {
1797 struct isis_vertex *vertex, *vertex_best = NULL;
1798
1799 for (int vtype = VTYPE_PSEUDO_IS; vtype <= VTYPE_NONPSEUDO_TE_IS;
1800 vtype++) {
1801 vertex = isis_find_vertex(&spftree->paths, sysid, vtype);
1802 if (!vertex)
1803 continue;
1804
1805 /* Pick vertex with the best metric. */
1806 if (!vertex_best || vertex_best->d_N > vertex->d_N)
1807 vertex_best = vertex;
1808 }
1809
1810 if (!vertex_best)
1811 return -1;
1812
1813 *distance = vertex_best->d_N;
1814
1815 return 0;
1816 }
1817
1818 /*
1819 * Check loop-free criterion (RFC 5286's inequality 1):
1820 * - Dist_opt(N, D) < Dist_opt(N, S) + Dist_opt(S, D)
1821 */
1822 static bool clfa_loop_free_check(struct isis_spftree *spftree,
1823 struct isis_vertex *vertex_S_D,
1824 struct isis_spf_adj *sadj_primary,
1825 struct isis_spf_adj *sadj_N,
1826 uint32_t *path_metric)
1827 {
1828 struct isis_spf_node *node_N;
1829 uint32_t dist_N_D;
1830 uint32_t dist_N_S;
1831 uint32_t dist_S_D;
1832
1833 node_N = isis_spf_node_find(&spftree->adj_nodes, sadj_N->id);
1834 assert(node_N);
1835
1836 /* Distance from N to D. */
1837 if (lfa_calc_dist_destination(node_N->lfa.spftree, vertex_S_D,
1838 &dist_N_D)
1839 != 0)
1840 return false;
1841
1842 /* Distance from N to S (or PN). */
1843 if (CHECK_FLAG(sadj_primary->flags, F_ISIS_SPF_ADJ_BROADCAST)) {
1844 static uint8_t pn_sysid[ISIS_SYS_ID_LEN + 1];
1845
1846 memcpy(pn_sysid, sadj_primary->id, ISIS_SYS_ID_LEN + 1);
1847 if (lfa_calc_dist_node(node_N->lfa.spftree, pn_sysid, &dist_N_S)
1848 != 0)
1849 return false;
1850 } else {
1851 static uint8_t root_sysid[ISIS_SYS_ID_LEN + 1];
1852
1853 memcpy(root_sysid, spftree->sysid, ISIS_SYS_ID_LEN);
1854 LSP_PSEUDO_ID(root_sysid) = 0;
1855 if (lfa_calc_dist_node(node_N->lfa.spftree, root_sysid,
1856 &dist_N_S)
1857 != 0)
1858 return false;
1859 }
1860
1861 /* Distance from S (or PN) to D. */
1862 vertex_S_D = listnode_head(vertex_S_D->parents);
1863 dist_S_D = vertex_S_D->d_N;
1864 if (CHECK_FLAG(sadj_primary->flags, F_ISIS_SPF_ADJ_BROADCAST))
1865 dist_S_D -= sadj_primary->metric;
1866
1867 if (IS_DEBUG_LFA)
1868 zlog_debug("ISIS-LFA: loop-free check: %u < %u + %u", dist_N_D,
1869 dist_N_S, dist_S_D);
1870
1871 if (dist_N_D < (dist_N_S + dist_S_D)) {
1872 *path_metric = sadj_N->metric + dist_N_D;
1873 return true;
1874 }
1875
1876 return false;
1877 }
1878
1879 /*
1880 * Check loop-free criterion (RFC 5286's inequality 2):
1881 * - Distance_opt(N, D) < Distance_opt(S, D)
1882 */
1883 static bool clfa_downstream_check(struct isis_spftree *spftree,
1884 struct isis_vertex *vertex_S_D,
1885 struct isis_spf_adj *sadj_N)
1886 {
1887 struct isis_spf_node *node_N;
1888 uint32_t dist_N_D;
1889 uint32_t dist_S_D;
1890
1891 node_N = isis_spf_node_find(&spftree->adj_nodes, sadj_N->id);
1892 assert(node_N);
1893
1894 /* Distance from N to D. */
1895 if (lfa_calc_dist_destination(node_N->lfa.spftree, vertex_S_D,
1896 &dist_N_D)
1897 != 0)
1898 return false;
1899
1900 /* Distance from S (or PN) to D. */
1901 vertex_S_D = listnode_head(vertex_S_D->parents);
1902 dist_S_D = vertex_S_D->d_N;
1903
1904 if (IS_DEBUG_LFA)
1905 zlog_debug("ISIS-LFA: downstream check: %u < %u", dist_N_D,
1906 dist_S_D);
1907
1908 if (dist_N_D < dist_S_D)
1909 return true;
1910
1911 return false;
1912 }
1913
1914 /*
1915 * Check loop-free criterion (RFC 5286's inequality 3):
1916 * - Dist_opt(N, D) < Dist_opt(N, E) + Dist_opt(E, D)
1917 */
1918 static bool clfa_node_protecting_check(struct isis_spftree *spftree,
1919 struct isis_vertex *vertex_S_D,
1920 struct isis_spf_adj *sadj_N,
1921 struct isis_spf_adj *sadj_E)
1922 {
1923 struct isis_spf_node *node_N, *node_E;
1924 uint32_t dist_N_D;
1925 uint32_t dist_N_E;
1926 uint32_t dist_E_D;
1927
1928 node_N = isis_spf_node_find(&spftree->adj_nodes, sadj_N->id);
1929 assert(node_N);
1930 node_E = isis_spf_node_find(&spftree->adj_nodes, sadj_E->id);
1931 assert(node_E);
1932
1933 /* Distance from N to D. */
1934 if (lfa_calc_dist_destination(node_N->lfa.spftree, vertex_S_D,
1935 &dist_N_D)
1936 != 0)
1937 return false;
1938
1939 /* Distance from N to E. */
1940 if (lfa_calc_dist_node(node_N->lfa.spftree, node_E->sysid, &dist_N_E)
1941 != 0)
1942 return false;
1943
1944 /* Distance from E to D. */
1945 if (lfa_calc_dist_destination(node_E->lfa.spftree, vertex_S_D,
1946 &dist_E_D)
1947 != 0)
1948 return false;
1949
1950 if (IS_DEBUG_LFA)
1951 zlog_debug("ISIS-LFA: node protecting check: %u < %u + %u",
1952 dist_N_D, dist_N_E, dist_E_D);
1953
1954 return (dist_N_D < (dist_N_E + dist_E_D));
1955 }
1956
1957 static struct list *
1958 isis_lfa_tiebreakers(struct isis_area *area, struct isis_spftree *spftree,
1959 struct lfa_protected_resource *resource,
1960 struct isis_vertex *vertex,
1961 struct isis_spf_adj *sadj_primary, struct list *lfa_list)
1962 {
1963 struct lfa_tiebreaker *tie_b;
1964 int level = spftree->level;
1965 struct list *filtered_lfa_list;
1966 struct list *tent_lfa_list;
1967
1968 filtered_lfa_list = list_dup(lfa_list);
1969 filtered_lfa_list->del = NULL;
1970
1971 if (listcount(filtered_lfa_list) == 1)
1972 return filtered_lfa_list;
1973
1974 /* Check tiebreakers in ascending order by index. */
1975 frr_each (lfa_tiebreaker_tree, &area->lfa_tiebreakers[level - 1],
1976 tie_b) {
1977 struct isis_vertex_adj *lfa;
1978 struct listnode *node, *nnode;
1979 uint32_t best_metric = UINT32_MAX;
1980
1981 tent_lfa_list = list_dup(filtered_lfa_list);
1982
1983 switch (tie_b->type) {
1984 case LFA_TIEBREAKER_DOWNSTREAM:
1985 for (ALL_LIST_ELEMENTS(tent_lfa_list, node, nnode,
1986 lfa)) {
1987 if (clfa_downstream_check(spftree, vertex,
1988 lfa->sadj))
1989 continue;
1990
1991 if (IS_DEBUG_LFA)
1992 zlog_debug(
1993 "ISIS-LFA: LFA %s doesn't satisfy the downstream condition",
1994 print_sys_hostname(
1995 lfa->sadj->id));
1996 listnode_delete(tent_lfa_list, lfa);
1997 }
1998 break;
1999 case LFA_TIEBREAKER_LOWEST_METRIC:
2000 /* Find the best metric first. */
2001 for (ALL_LIST_ELEMENTS_RO(tent_lfa_list, node, lfa)) {
2002 if (lfa->lfa_metric < best_metric)
2003 best_metric = lfa->lfa_metric;
2004 }
2005
2006 /* Remove LFAs that don't have the best metric. */
2007 for (ALL_LIST_ELEMENTS(tent_lfa_list, node, nnode,
2008 lfa)) {
2009 if (lfa->lfa_metric == best_metric)
2010 continue;
2011
2012 if (IS_DEBUG_LFA)
2013 zlog_debug(
2014 "ISIS-LFA: LFA %s doesn't have the lowest cost metric",
2015 print_sys_hostname(
2016 lfa->sadj->id));
2017 listnode_delete(tent_lfa_list, lfa);
2018 }
2019 break;
2020 case LFA_TIEBREAKER_NODE_PROTECTING:
2021 for (ALL_LIST_ELEMENTS(tent_lfa_list, node, nnode,
2022 lfa)) {
2023 if (clfa_node_protecting_check(spftree, vertex,
2024 lfa->sadj,
2025 sadj_primary))
2026 continue;
2027
2028 if (IS_DEBUG_LFA)
2029 zlog_debug(
2030 "ISIS-LFA: LFA %s doesn't provide node protection",
2031 print_sys_hostname(
2032 lfa->sadj->id));
2033 listnode_delete(tent_lfa_list, lfa);
2034 }
2035 break;
2036 }
2037
2038 /*
2039 * Decide what to do next based on the number of remaining LFAs.
2040 */
2041 switch (listcount(tent_lfa_list)) {
2042 case 0:
2043 /*
2044 * Ignore this tie-breaker since it excluded all LFAs.
2045 * Move on to the next one (if any).
2046 */
2047 list_delete(&tent_lfa_list);
2048 break;
2049 case 1:
2050 /* Finish tie-breaking once we get a single LFA. */
2051 list_delete(&filtered_lfa_list);
2052 filtered_lfa_list = tent_lfa_list;
2053 return filtered_lfa_list;
2054 default:
2055 /*
2056 * We still have two or more LFAs. Move on to the next
2057 * tie-breaker (if any).
2058 */
2059 list_delete(&filtered_lfa_list);
2060 filtered_lfa_list = tent_lfa_list;
2061 break;
2062 }
2063 }
2064
2065 return filtered_lfa_list;
2066 }
2067
2068 void isis_lfa_compute(struct isis_area *area, struct isis_circuit *circuit,
2069 struct isis_spftree *spftree,
2070 struct lfa_protected_resource *resource)
2071 {
2072 struct isis_vertex *vertex, *parent_vertex;
2073 struct listnode *vnode, *snode;
2074 int level = spftree->level;
2075
2076 resource->type = LFA_LINK_PROTECTION;
2077
2078 if (IS_DEBUG_LFA)
2079 zlog_debug("ISIS-LFA: computing local LFAs for %s",
2080 lfa_protected_resource2str(resource));
2081
2082 for (ALL_QUEUE_ELEMENTS_RO(&spftree->paths, vnode, vertex)) {
2083 struct list *lfa_list;
2084 struct list *filtered_lfa_list;
2085 struct isis_spf_adj *sadj_N;
2086 struct isis_vertex_adj *vadj_primary;
2087 struct isis_spf_adj *sadj_primary;
2088 bool allow_ecmp;
2089 uint32_t prefix_metric, best_metric = UINT32_MAX;
2090 char buf[VID2STR_BUFFER];
2091
2092 if (!VTYPE_IP(vertex->type))
2093 continue;
2094
2095 vid2string(vertex, buf, sizeof(buf));
2096
2097 if (!spf_vertex_check_is_affected(vertex, spftree->sysid,
2098 resource)) {
2099 if (IS_DEBUG_LFA)
2100 zlog_debug(
2101 "ISIS-LFA: %s %s unaffected by %s",
2102 vtype2string(vertex->type), buf,
2103 lfa_protected_resource2str(resource));
2104 continue;
2105 }
2106
2107 if (IS_DEBUG_LFA)
2108 zlog_debug("ISIS-LFA: checking %s %s w.r.t %s",
2109 vtype2string(vertex->type), buf,
2110 lfa_protected_resource2str(resource));
2111
2112 if (vertex->N.ip.priority
2113 > area->lfa_priority_limit[level - 1]) {
2114 if (IS_DEBUG_LFA)
2115 zlog_debug(
2116 "ISIS-LFA: skipping computing LFAs due to low prefix priority");
2117 continue;
2118 }
2119
2120 vadj_primary = listnode_head(vertex->Adj_N);
2121 sadj_primary = vadj_primary->sadj;
2122
2123 parent_vertex = listnode_head(vertex->parents);
2124 prefix_metric = vertex->d_N - parent_vertex->d_N;
2125
2126 /*
2127 * Loop over list of SPF adjacencies and compute a list of
2128 * preliminary LFAs.
2129 */
2130 lfa_list = list_new();
2131 lfa_list->del = isis_vertex_adj_free;
2132 for (ALL_LIST_ELEMENTS_RO(spftree->sadj_list, snode, sadj_N)) {
2133 uint32_t lfa_metric, path_metric;
2134 struct isis_vertex_adj *lfa;
2135 struct isis_prefix_sid *psid = NULL;
2136 bool last_hop = false;
2137
2138 /* Skip pseudonodes. */
2139 if (LSP_PSEUDO_ID(sadj_N->id))
2140 continue;
2141
2142 /*
2143 * Skip nexthops that are along a link whose cost is
2144 * infinite.
2145 */
2146 if (CHECK_FLAG(sadj_N->flags,
2147 F_ISIS_SPF_ADJ_METRIC_INFINITY))
2148 continue;
2149
2150 /* Skip nexthops that have the overload bit set. */
2151 if (spftree->mtid != ISIS_MT_IPV4_UNICAST) {
2152 struct isis_mt_router_info *mt_router_info;
2153
2154 mt_router_info =
2155 isis_tlvs_lookup_mt_router_info(
2156 sadj_N->lsp->tlvs,
2157 spftree->mtid);
2158 if (mt_router_info && mt_router_info->overload)
2159 continue;
2160 } else if (ISIS_MASK_LSP_OL_BIT(
2161 sadj_N->lsp->hdr.lsp_bits))
2162 continue;
2163
2164 /* Skip primary nexthop. */
2165 if (spf_adj_check_is_affected(sadj_N, resource, NULL,
2166 false))
2167 continue;
2168
2169 /* Skip excluded interfaces as per the configuration. */
2170 if (circuit
2171 && isis_lfa_excluded_iface_check(
2172 circuit, level,
2173 sadj_N->adj->circuit->interface->name))
2174 continue;
2175
2176 if (IS_DEBUG_LFA)
2177 zlog_debug(
2178 "ISIS-LFA: checking candidate LFA %s",
2179 print_sys_hostname(sadj_N->id));
2180
2181 /* Check loop-free criterion. */
2182 if (!clfa_loop_free_check(spftree, vertex, sadj_primary,
2183 sadj_N, &path_metric)) {
2184 if (IS_DEBUG_LFA)
2185 zlog_debug(
2186 "ISIS-LFA: LFA condition not met for %s",
2187 print_sys_hostname(sadj_N->id));
2188 continue;
2189 }
2190
2191 lfa_metric = path_metric + prefix_metric;
2192 if (lfa_metric < best_metric)
2193 best_metric = lfa_metric;
2194
2195 if (IS_DEBUG_LFA)
2196 zlog_debug(
2197 "ISIS-LFA: %s is a valid loop-free alternate",
2198 print_sys_hostname(sadj_N->id));
2199
2200 if (vertex->N.ip.sr.present) {
2201 psid = &vertex->N.ip.sr.sid;
2202 if (path_metric == sadj_N->metric)
2203 last_hop = true;
2204 }
2205 lfa = isis_vertex_adj_add(spftree, vertex, lfa_list,
2206 sadj_N, psid, last_hop);
2207 lfa->lfa_metric = lfa_metric;
2208 }
2209
2210 if (list_isempty(lfa_list)) {
2211 if (IS_DEBUG_LFA)
2212 zlog_debug(
2213 "ISIS-LFA: no valid local LFAs found");
2214 list_delete(&lfa_list);
2215 continue;
2216 }
2217
2218 SET_FLAG(vertex->flags, F_ISIS_VERTEX_LFA_PROTECTED);
2219
2220 /* Check tie-breakers. */
2221 filtered_lfa_list =
2222 isis_lfa_tiebreakers(area, spftree, resource, vertex,
2223 sadj_primary, lfa_list);
2224
2225 /* Create backup route using the best LFAs. */
2226 allow_ecmp = area->lfa_load_sharing[level - 1];
2227 isis_route_create(&vertex->N.ip.p.dest, &vertex->N.ip.p.src,
2228 best_metric, vertex->depth, &vertex->N.ip.sr,
2229 filtered_lfa_list, allow_ecmp, area,
2230 spftree->route_table_backup);
2231 spftree->lfa.protection_counters.lfa[vertex->N.ip.priority] +=
2232 1;
2233
2234 list_delete(&filtered_lfa_list);
2235 list_delete(&lfa_list);
2236 }
2237 }
2238
2239 static void isis_spf_run_tilfa(struct isis_area *area,
2240 struct isis_circuit *circuit,
2241 struct isis_spftree *spftree,
2242 struct isis_spftree *spftree_reverse,
2243 struct lfa_protected_resource *resource)
2244 {
2245 struct isis_spftree *spftree_pc_link;
2246 struct isis_spftree *spftree_pc_node;
2247
2248 /* Compute node protecting repair paths first (if necessary). */
2249 if (circuit->tilfa_node_protection[spftree->level - 1]) {
2250 resource->type = LFA_NODE_PROTECTION;
2251 spftree_pc_node = isis_tilfa_compute(area, spftree,
2252 spftree_reverse, resource);
2253 isis_spftree_del(spftree_pc_node);
2254
2255 /* don't do link protection unless link-fallback is configured
2256 */
2257 if (!circuit->tilfa_link_fallback[spftree->level - 1])
2258 return;
2259 }
2260
2261 /* Compute link protecting repair paths. */
2262 resource->type = LFA_LINK_PROTECTION;
2263 spftree_pc_link =
2264 isis_tilfa_compute(area, spftree, spftree_reverse, resource);
2265 isis_spftree_del(spftree_pc_link);
2266 }
2267
2268 /**
2269 * Run the LFA/RLFA/TI-LFA algorithms for all protected interfaces.
2270 *
2271 * @param area IS-IS area
2272 * @param spftree IS-IS SPF tree
2273 */
2274 void isis_spf_run_lfa(struct isis_area *area, struct isis_spftree *spftree)
2275 {
2276 struct isis_spftree *spftree_reverse = NULL;
2277 struct isis_circuit *circuit;
2278 struct listnode *node;
2279 int level = spftree->level;
2280
2281 /* Run reverse SPF locally. */
2282 if (area->rlfa_protected_links[level - 1] > 0
2283 || area->tilfa_protected_links[level - 1] > 0)
2284 spftree_reverse = isis_spf_reverse_run(spftree);
2285
2286 /* Run forward SPF on all adjacent routers. */
2287 isis_spf_run_neighbors(spftree);
2288
2289 /* Check which interfaces are protected. */
2290 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, node, circuit)) {
2291 struct lfa_protected_resource resource = {};
2292 struct isis_adjacency *adj;
2293 static uint8_t null_sysid[ISIS_SYS_ID_LEN + 1];
2294
2295 if (!(circuit->is_type & level))
2296 continue;
2297
2298 if (!circuit->lfa_protection[level - 1]
2299 && !circuit->tilfa_protection[level - 1])
2300 continue;
2301
2302 /* Fill in the protected resource. */
2303 switch (circuit->circ_type) {
2304 case CIRCUIT_T_BROADCAST:
2305 if (level == ISIS_LEVEL1)
2306 memcpy(resource.adjacency,
2307 circuit->u.bc.l1_desig_is,
2308 ISIS_SYS_ID_LEN + 1);
2309 else
2310 memcpy(resource.adjacency,
2311 circuit->u.bc.l2_desig_is,
2312 ISIS_SYS_ID_LEN + 1);
2313 /* Do nothing if no DR was elected yet. */
2314 if (!memcmp(resource.adjacency, null_sysid,
2315 ISIS_SYS_ID_LEN + 1))
2316 continue;
2317 break;
2318 case CIRCUIT_T_P2P:
2319 adj = circuit->u.p2p.neighbor;
2320 if (!adj)
2321 continue;
2322 memcpy(resource.adjacency, adj->sysid, ISIS_SYS_ID_LEN);
2323 LSP_PSEUDO_ID(resource.adjacency) = 0;
2324 break;
2325 default:
2326 continue;
2327 }
2328
2329 if (circuit->lfa_protection[level - 1]) {
2330 /* Run local LFA. */
2331 isis_lfa_compute(area, circuit, spftree, &resource);
2332
2333 if (circuit->rlfa_protection[level - 1]) {
2334 struct isis_spftree *spftree_pc;
2335 uint32_t max_metric;
2336
2337 /* Run remote LFA. */
2338 assert(spftree_reverse);
2339 max_metric =
2340 circuit->rlfa_max_metric[level - 1];
2341 spftree_pc = isis_rlfa_compute(
2342 area, spftree, spftree_reverse,
2343 max_metric, &resource);
2344 listnode_add(spftree->lfa.remote.pc_spftrees,
2345 spftree_pc);
2346 }
2347 } else if (circuit->tilfa_protection[level - 1]) {
2348 /* Run TI-LFA. */
2349 assert(spftree_reverse);
2350 isis_spf_run_tilfa(area, circuit, spftree,
2351 spftree_reverse, &resource);
2352 }
2353 }
2354
2355 if (spftree_reverse)
2356 isis_spftree_del(spftree_reverse);
2357 }