-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathisis_sr.c
2329 lines (2017 loc) · 62.1 KB
/
isis_sr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is an implementation of Segment Routing for IS-IS as per RFC 8667
*
* Copyright (C) 2019 Orange http://www.orange.com
*
* Author: Olivier Dugeon <olivier.dugeon@orange.com>
* Contributor: Renato Westphal <renato@opensourcerouting.org> for NetDEF
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "if.h"
#include "linklist.h"
#include "log.h"
#include "command.h"
#include "termtable.h"
#include "memory.h"
#include "prefix.h"
#include "table.h"
#include "vty.h"
#include "zclient.h"
#include "lib/lib_errors.h"
#include "isisd/isisd.h"
#include "isisd/isis_spf.h"
#include "isisd/isis_spf_private.h"
#include "isisd/isis_adjacency.h"
#include "isisd/isis_route.h"
#include "isisd/isis_mt.h"
#include "isisd/isis_sr.h"
#include "isisd/isis_tlvs.h"
#include "isisd/isis_misc.h"
#include "isisd/isis_zebra.h"
#include "isisd/isis_errors.h"
/* Local variables and functions */
DEFINE_MTYPE_STATIC(ISISD, ISIS_SR_INFO, "ISIS segment routing information")
static void sr_prefix_uninstall(struct sr_prefix *srp);
static void sr_prefix_reinstall(struct sr_prefix *srp, bool make_before_break);
static void sr_local_block_delete(struct isis_area *area);
static int sr_local_block_init(struct isis_area *area);
static void sr_adj_sid_update(struct sr_adjacency *sra,
struct sr_local_block *srlb);
/* --- RB-Tree Management functions ----------------------------------------- */
/**
* SR Prefix comparison for RB-Tree.
*
* @param a First SR prefix
* @param b Second SR prefix
*
* @return -1 (a < b), 0 (a == b) or +1 (a > b)
*/
static inline int sr_prefix_sid_compare(const struct sr_prefix *a,
const struct sr_prefix *b)
{
return prefix_cmp(&a->prefix, &b->prefix);
}
DECLARE_RBTREE_UNIQ(srdb_node_prefix, struct sr_prefix, node_entry,
sr_prefix_sid_compare)
DECLARE_RBTREE_UNIQ(srdb_area_prefix, struct sr_prefix, area_entry,
sr_prefix_sid_compare)
/**
* Configured SR Prefix comparison for RB-Tree.
*
* @param a First SR prefix
* @param b Second SR prefix
*
* @return -1 (a < b), 0 (a == b) or +1 (a > b)
*/
static inline int sr_prefix_sid_cfg_compare(const struct sr_prefix_cfg *a,
const struct sr_prefix_cfg *b)
{
return prefix_cmp(&a->prefix, &b->prefix);
}
DECLARE_RBTREE_UNIQ(srdb_prefix_cfg, struct sr_prefix_cfg, entry,
sr_prefix_sid_cfg_compare)
/**
* SR Node comparison for RB-Tree.
*
* @param a First SR node
* @param b Second SR node
*
* @return -1 (a < b), 0 (a == b) or +1 (a > b)
*/
static inline int sr_node_compare(const struct sr_node *a,
const struct sr_node *b)
{
return memcmp(a->sysid, b->sysid, ISIS_SYS_ID_LEN);
}
DECLARE_RBTREE_UNIQ(srdb_node, struct sr_node, entry, sr_node_compare)
/* --- Functions used for Yang model and CLI to configure Segment Routing --- */
/**
* Check if prefix correspond to a Node SID.
*
* @param ifp Interface
* @param prefix Prefix to be checked
*
* @return True if the interface/address pair corresponds to a Node-SID
*/
static bool sr_prefix_is_node_sid(const struct interface *ifp,
const struct prefix *prefix)
{
return (if_is_loopback(ifp) && is_host_route(prefix));
}
/**
* Update local SRGB configuration. SRGB is reserved though Label Manager.
* This function trigger the update of local Prefix-SID installation.
*
* @param area IS-IS area
* @param lower_bound Lower bound of SRGB
* @param upper_bound Upper bound of SRGB
*
* @return 0 on success, -1 otherwise
*/
int isis_sr_cfg_srgb_update(struct isis_area *area, uint32_t lower_bound,
uint32_t upper_bound)
{
struct isis_sr_db *srdb = &area->srdb;
sr_debug("ISIS-Sr (%s): Update SRGB with new range [%u/%u]",
area->area_tag, lower_bound, upper_bound);
/* Just store new SRGB values if Label Manager is not available.
* SRGB will be configured later when SR start */
if (!isis_zebra_label_manager_ready()) {
srdb->config.srgb_lower_bound = lower_bound;
srdb->config.srgb_upper_bound = upper_bound;
return 0;
}
/* Label Manager is ready, start by releasing the old SRGB. */
if (srdb->srgb_active) {
isis_zebra_release_label_range(srdb->config.srgb_lower_bound,
srdb->config.srgb_upper_bound);
srdb->srgb_active = false;
}
srdb->config.srgb_lower_bound = lower_bound;
srdb->config.srgb_upper_bound = upper_bound;
if (srdb->enabled) {
struct sr_prefix *srp;
/* then request new SRGB if SR is enabled. */
if (isis_zebra_request_label_range(
srdb->config.srgb_lower_bound,
srdb->config.srgb_upper_bound
- srdb->config.srgb_lower_bound + 1) < 0) {
srdb->srgb_active = false;
return -1;
} else
srdb->srgb_active = true;
sr_debug(" |- Got new SRGB [%u/%u]",
srdb->config.srgb_lower_bound,
srdb->config.srgb_upper_bound);
/* Reinstall local Prefix-SIDs to update their input labels. */
for (int level = ISIS_LEVEL1; level <= ISIS_LEVELS; level++) {
frr_each (srdb_area_prefix,
&area->srdb.prefix_sids[level - 1], srp) {
sr_prefix_reinstall(srp, false);
}
}
lsp_regenerate_schedule(area, area->is_type, 0);
} else if (srdb->config.enabled) {
/* Try to enable SR again using the new SRGB. */
isis_sr_start(area);
}
return 0;
}
/**
* Update Segment Routing Local Block range which is reserved though the
* Label Manager. This function trigger the update of local Adjacency-SID
* installation.
*
* @param area IS-IS area
* @param lower_bound Lower bound of SRLB
* @param upper_bound Upper bound of SRLB
*
* @return 0 on success, -1 otherwise
*/
int isis_sr_cfg_srlb_update(struct isis_area *area, uint32_t lower_bound,
uint32_t upper_bound)
{
struct isis_sr_db *srdb = &area->srdb;
struct listnode *node, *nnode;
struct sr_adjacency *sra;
sr_debug("ISIS-Sr (%s): Update SRLB with new range [%u/%u]",
area->area_tag, lower_bound, upper_bound);
/* Just store new SRLB values if Label Manager is not available.
* SRLB will be configured later when SR start */
if (!isis_zebra_label_manager_ready()) {
srdb->config.srlb_lower_bound = lower_bound;
srdb->config.srlb_upper_bound = upper_bound;
return 0;
}
/* LM is ready, start by deleting the old SRLB */
sr_local_block_delete(area);
srdb->config.srlb_lower_bound = lower_bound;
srdb->config.srlb_upper_bound = upper_bound;
if (srdb->enabled) {
/* Initialize new SRLB */
if (sr_local_block_init(area) != 0)
return -1;
/* Reinstall local Adjacency-SIDs with new labels. */
for (ALL_LIST_ELEMENTS(area->srdb.adj_sids, node, nnode, sra))
sr_adj_sid_update(sra, &srdb->srlb);
/* Update and Flood LSP */
lsp_regenerate_schedule(area, area->is_type, 0);
} else if (srdb->config.enabled) {
/* Try to enable SR again using the new SRLB. */
isis_sr_start(area);
}
return 0;
}
/**
* Add new Prefix-SID configuration to the SRDB.
*
* @param area IS-IS area
* @param prefix Prefix to be added
*
* @return Newly added Prefix-SID configuration structure
*/
struct sr_prefix_cfg *isis_sr_cfg_prefix_add(struct isis_area *area,
const struct prefix *prefix)
{
struct sr_prefix_cfg *pcfg;
struct interface *ifp;
sr_debug("ISIS-Sr (%s): Add local prefix %pFX", area->area_tag, prefix);
pcfg = XCALLOC(MTYPE_ISIS_SR_INFO, sizeof(*pcfg));
pcfg->prefix = *prefix;
pcfg->area = area;
/* Pull defaults from the YANG module. */
pcfg->sid_type = yang_get_default_enum(
"%s/prefix-sid-map/prefix-sid/sid-value-type", ISIS_SR);
pcfg->last_hop_behavior = yang_get_default_enum(
"%s/prefix-sid-map/prefix-sid/last-hop-behavior", ISIS_SR);
/* Set the N-flag when appropriate. */
ifp = if_lookup_prefix(prefix, VRF_DEFAULT);
if (ifp && sr_prefix_is_node_sid(ifp, prefix))
pcfg->node_sid = true;
/* Save prefix-sid configuration. */
srdb_prefix_cfg_add(&area->srdb.config.prefix_sids, pcfg);
return pcfg;
}
/**
* Removal of locally configured Prefix-SID.
*
* @param pcfg Configured Prefix-SID
*/
void isis_sr_cfg_prefix_del(struct sr_prefix_cfg *pcfg)
{
struct isis_area *area = pcfg->area;
sr_debug("ISIS-Sr (%s): Delete local Prefix-SID %pFX %s %u",
area->area_tag, &pcfg->prefix,
pcfg->sid_type == SR_SID_VALUE_TYPE_INDEX ? "index" : "label",
pcfg->sid);
srdb_prefix_cfg_del(&area->srdb.config.prefix_sids, pcfg);
XFREE(MTYPE_ISIS_SR_INFO, pcfg);
}
/**
* Lookup for Prefix-SID in the local configuration.
*
* @param area IS-IS area
* @param prefix Prefix to lookup
*
* @return Configured Prefix-SID structure if found, NULL otherwise
*/
struct sr_prefix_cfg *isis_sr_cfg_prefix_find(struct isis_area *area,
union prefixconstptr prefix)
{
struct sr_prefix_cfg pcfg = {};
prefix_copy(&pcfg.prefix, prefix.p);
return srdb_prefix_cfg_find(&area->srdb.config.prefix_sids, &pcfg);
}
/**
* Fill in Prefix-SID Sub-TLV according to the corresponding configuration.
*
* @param pcfg Prefix-SID configuration
* @param external False if prefix is locally configured, true otherwise
* @param psid Prefix-SID sub-TLV to be updated
*/
void isis_sr_prefix_cfg2subtlv(const struct sr_prefix_cfg *pcfg, bool external,
struct isis_prefix_sid *psid)
{
/* Set SID algorithm. */
psid->algorithm = SR_ALGORITHM_SPF;
/* Set SID flags. */
psid->flags = 0;
switch (pcfg->last_hop_behavior) {
case SR_LAST_HOP_BEHAVIOR_EXP_NULL:
SET_FLAG(psid->flags, ISIS_PREFIX_SID_NO_PHP);
SET_FLAG(psid->flags, ISIS_PREFIX_SID_EXPLICIT_NULL);
break;
case SR_LAST_HOP_BEHAVIOR_NO_PHP:
SET_FLAG(psid->flags, ISIS_PREFIX_SID_NO_PHP);
UNSET_FLAG(psid->flags, ISIS_PREFIX_SID_EXPLICIT_NULL);
break;
case SR_LAST_HOP_BEHAVIOR_PHP:
UNSET_FLAG(psid->flags, ISIS_PREFIX_SID_NO_PHP);
UNSET_FLAG(psid->flags, ISIS_PREFIX_SID_EXPLICIT_NULL);
break;
}
if (external)
SET_FLAG(psid->flags, ISIS_PREFIX_SID_READVERTISED);
if (pcfg->node_sid)
SET_FLAG(psid->flags, ISIS_PREFIX_SID_NODE);
/* Set SID value. */
psid->value = pcfg->sid;
if (pcfg->sid_type == SR_SID_VALUE_TYPE_ABSOLUTE) {
SET_FLAG(psid->flags, ISIS_PREFIX_SID_VALUE);
SET_FLAG(psid->flags, ISIS_PREFIX_SID_LOCAL);
}
}
/* --- Segment Routing Prefix Management functions -------------------------- */
/**
* Add Segment Routing Prefix to a given Segment Routing Node.
*
* @param area IS-IS area
* @param srn Segment Routing Node
* @param prefix Prefix to be added
* @param local True if prefix is locally configured, false otherwise
* @param psid Prefix-SID sub-TLVs
*
* @return New Segment Routing Prefix structure
*/
static struct sr_prefix *sr_prefix_add(struct isis_area *area,
struct sr_node *srn,
union prefixconstptr prefix, bool local,
const struct isis_prefix_sid *psid)
{
struct sr_prefix *srp;
srp = XCALLOC(MTYPE_ISIS_SR_INFO, sizeof(*srp));
prefix_copy(&srp->prefix, prefix.p);
srp->sid = *psid;
srp->input_label = MPLS_INVALID_LABEL;
if (local) {
srp->type = ISIS_SR_PREFIX_LOCAL;
isis_sr_nexthop_reset(&srp->u.local.info);
} else {
srp->type = ISIS_SR_PREFIX_REMOTE;
srp->u.remote.rinfo = NULL;
}
srp->srn = srn;
srdb_node_prefix_add(&srn->prefix_sids, srp);
/* TODO: this might fail if we have Anycast SIDs in the IS-IS area. */
srdb_area_prefix_add(&area->srdb.prefix_sids[srn->level - 1], srp);
sr_debug(" |- Added new SR Prefix-SID %pFX %s %u to SR Node %s",
&srp->prefix, IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value, sysid_print(srn->sysid));
return srp;
}
/**
* Remove given Segment Prefix from given Segment Routing Node.
* Prefix-SID is un-installed first.
*
* @param area IS-IS area
* @param srn Segment Routing Node
* @param srp Segment Routing Prefix
*/
static void sr_prefix_del(struct isis_area *area, struct sr_node *srn,
struct sr_prefix *srp)
{
sr_debug(" |- Delete SR Prefix-SID %pFX %s %u to SR Node %s",
&srp->prefix, IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value, sysid_print(srn->sysid));
sr_prefix_uninstall(srp);
srdb_node_prefix_del(&srn->prefix_sids, srp);
srdb_area_prefix_del(&area->srdb.prefix_sids[srn->level - 1], srp);
XFREE(MTYPE_ISIS_SR_INFO, srp);
}
/**
* Find Segment Routing Prefix by Area.
*
* @param area IS-IS area
* @param level IS-IS level
* @param prefix Prefix to lookup
*
* @return Segment Routing Prefix structure if found, NULL otherwise
*/
static struct sr_prefix *sr_prefix_find_by_area(struct isis_area *area,
int level,
union prefixconstptr prefix)
{
struct sr_prefix srp = {};
prefix_copy(&srp.prefix, prefix.p);
return srdb_area_prefix_find(&area->srdb.prefix_sids[level - 1], &srp);
}
/**
* Find Segment Routing Prefix by Segment Routing Node.
*
* @param srn Segment Routing Node
* @param prefix Prefix to lookup
*
* @return Segment Routing Prefix structure if found, NULL otherwise
*/
static struct sr_prefix *sr_prefix_find_by_node(struct sr_node *srn,
union prefixconstptr prefix)
{
struct sr_prefix srp = {};
prefix_copy(&srp.prefix, prefix.p);
return srdb_node_prefix_find(&srn->prefix_sids, &srp);
}
/* --- Segment Routing Node Management functions ---------------------------- */
/**
* Add Segment Routing Node to the Segment Routing Data Base.
*
* @param area IS-IS area
* @param level IS-IS level
* @param sysid Node System ID
* @param cap Segment Routing Capability sub-TLVs
*
* @return New Segment Routing Node structure
*/
static struct sr_node *sr_node_add(struct isis_area *area, int level,
const uint8_t *sysid)
{
struct sr_node *srn;
srn = XCALLOC(MTYPE_ISIS_SR_INFO, sizeof(*srn));
srn->level = level;
memcpy(srn->sysid, sysid, ISIS_SYS_ID_LEN);
srn->area = area;
srdb_node_prefix_init(&srn->prefix_sids);
srdb_node_add(&area->srdb.sr_nodes[level - 1], srn);
sr_debug(" |- Added new SR Node %s", sysid_print(srn->sysid));
return srn;
}
static void sr_node_del(struct isis_area *area, int level, struct sr_node *srn)
/**
* Remove Segment Routing Node from the Segment Routing Data Base.
* All Prefix-SID attached to this Segment Routing Node are removed first.
*
* @param area IS-IS area
* @param level IS-IS level
* @param srn Segment Routing Node to be deleted
*/
{
sr_debug(" |- Delete SR Node %s", sysid_print(srn->sysid));
/* Remove and uninstall Prefix-SIDs. */
while (srdb_node_prefix_count(&srn->prefix_sids) > 0) {
struct sr_prefix *srp;
srp = srdb_node_prefix_first(&srn->prefix_sids);
sr_prefix_del(area, srn, srp);
}
srdb_node_del(&area->srdb.sr_nodes[level - 1], srn);
XFREE(MTYPE_ISIS_SR_INFO, srn);
}
/**
* Find Segment Routing Node in the Segment Routing Data Base per system ID.
*
* @param area IS-IS area
* @param level IS-IS level
* @param sysid Node System ID to lookup
*
* @return Segment Routing Node structure if found, NULL otherwise
*/
static struct sr_node *sr_node_find(struct isis_area *area, int level,
const uint8_t *sysid)
{
struct sr_node srn = {};
memcpy(srn.sysid, sysid, ISIS_SYS_ID_LEN);
return srdb_node_find(&area->srdb.sr_nodes[level - 1], &srn);
}
/**
* Update Segment Routing Node following an SRGB update. This function
* is called when a neighbor SR Node has updated its SRGB.
*
* @param area IS-IS area
* @param level IS-IS level
* @param sysid Segment Routing Node system ID
*/
static void sr_node_srgb_update(struct isis_area *area, int level,
uint8_t *sysid)
{
struct sr_prefix *srp;
sr_debug("ISIS-Sr (%s): Update neighbors SR Node with new SRGB",
area->area_tag);
frr_each (srdb_area_prefix, &area->srdb.prefix_sids[level - 1], srp) {
struct listnode *node;
struct isis_nexthop *nh;
if (srp->type == ISIS_SR_PREFIX_LOCAL)
continue;
if (srp->u.remote.rinfo == NULL)
continue;
for (ALL_LIST_ELEMENTS_RO(srp->u.remote.rinfo->nexthops, node,
nh)) {
if (memcmp(nh->sysid, sysid, ISIS_SYS_ID_LEN) != 0)
continue;
/*
* The Prefix-SID input label hasn't changed. We could
* re-install all Prefix-SID with "Make Before Break"
* option. Zebra layer will update output label(s) by
* adding new entry before removing the old one(s).
*/
sr_prefix_reinstall(srp, true);
break;
}
}
}
/* --- Segment Routing Nexthop information Management functions ------------- */
/**
* Update Segment Routing Nexthop.
*
* @param srnh Segment Routing next hop
* @param label Output MPLS label
*/
void isis_sr_nexthop_update(struct sr_nexthop_info *srnh, mpls_label_t label)
{
srnh->label = label;
if (srnh->uptime == 0)
srnh->uptime = time(NULL);
}
/**
* Reset Segment Routing Nexthop.
*
* @param srnh Segment Routing Nexthop
*/
void isis_sr_nexthop_reset(struct sr_nexthop_info *srnh)
{
srnh->label = MPLS_INVALID_LABEL;
srnh->uptime = 0;
}
/* --- Segment Routing Prefix-SID Management functions to configure LFIB ---- */
/**
* Lookup IS-IS route in the Shortest Path Tree.
*
* @param area IS-IS area
* @param tree_id Shortest Path Tree identifier
* @param srp Segment Routing Prefix to lookup
*
* @return Route Information for this prefix if found, NULL otherwise
*/
static struct isis_route_info *sr_prefix_lookup_route(struct isis_area *area,
enum spf_tree_id tree_id,
struct sr_prefix *srp)
{
struct route_node *rn;
int level = srp->srn->level;
rn = route_node_lookup(area->spftree[tree_id][level - 1]->route_table,
&srp->prefix);
if (rn) {
route_unlock_node(rn);
if (rn->info)
return rn->info;
}
return NULL;
}
/**
* Compute input label for the given Prefix-SID.
*
* @param srp Segment Routing Prefix
*
* @return MPLS label or MPLS_INVALID_LABEL in case of SRGB overflow
*/
static mpls_label_t sr_prefix_in_label(const struct sr_prefix *srp)
{
const struct sr_node *srn = srp->srn;
struct isis_area *area = srn->area;
/* Return SID value as MPLS label if it is an Absolute SID */
if (CHECK_FLAG(srp->sid.flags,
ISIS_PREFIX_SID_VALUE | ISIS_PREFIX_SID_LOCAL))
return srp->sid.value;
/* Check that SID index falls inside the SRGB */
if (srp->sid.value >= (area->srdb.config.srgb_upper_bound
- area->srdb.config.srgb_lower_bound + 1)) {
flog_warn(EC_ISIS_SID_OVERFLOW,
"%s: SID index %u falls outside local SRGB range",
__func__, srp->sid.value);
return MPLS_INVALID_LABEL;
}
/* Return MPLS label as SID index + SRGB_lower_bound as per RFC 8667 */
return (area->srdb.config.srgb_lower_bound + srp->sid.value);
}
/**
* Compute output label for the given Prefix-SID.
*
* @param srp Segment Routing Prefix
* @param srn_nexthop Segment Routing nexthop node
* @param sysid System ID of the SR node which advertised the Prefix-SID
*
* @return MPLS label or MPLS_INVALID_LABEL in case of error
*/
static mpls_label_t sr_prefix_out_label(const struct sr_prefix *srp,
const struct sr_node *srn_nexthop,
const uint8_t *sysid)
{
const struct sr_node *srn = srp->srn;
/* Check if the nexthop SR Node is the last hop? */
if (memcmp(sysid, srn->sysid, ISIS_SYS_ID_LEN) == 0) {
/* SR-Node doesn't request NO-PHP. Return Implicit NULL label */
if (!CHECK_FLAG(srp->sid.flags, ISIS_PREFIX_SID_NO_PHP))
return MPLS_LABEL_IMPLICIT_NULL;
/* SR-Node requests Implicit NULL Label */
if (CHECK_FLAG(srp->sid.flags, ISIS_PREFIX_SID_EXPLICIT_NULL)) {
if (srp->prefix.family == AF_INET)
return MPLS_LABEL_IPV4_EXPLICIT_NULL;
else
return MPLS_LABEL_IPV6_EXPLICIT_NULL;
}
/* Fallthrough */
}
/* Return SID value as MPLS label if it is an Absolute SID */
if (CHECK_FLAG(srp->sid.flags,
ISIS_PREFIX_SID_VALUE | ISIS_PREFIX_SID_LOCAL)) {
/*
* V/L SIDs have local significance, so only adjacent routers
* can use them (RFC8667 section #2.1.1.1)
*/
if (srp->srn != srn_nexthop)
return MPLS_INVALID_LABEL;
return srp->sid.value;
}
/* Check that SID index falls inside the SRGB */
if (srp->sid.value >= srn_nexthop->cap.srgb.range_size) {
flog_warn(EC_ISIS_SID_OVERFLOW,
"%s: SID index %u falls outside remote SRGB range",
__func__, srp->sid.value);
return MPLS_INVALID_LABEL;
}
/* Return MPLS label as SID index + SRGB_lower_bound as per RFC 8667 */
return (srn_nexthop->cap.srgb.lower_bound + srp->sid.value);
}
/**
* Process local Prefix-SID and install it if possible. Input label is
* computed before installing it in LFIB.
*
* @param srp Segment Routing Prefix
*
* @return 0 on success, -1 otherwise
*/
static int sr_prefix_install_local(struct sr_prefix *srp)
{
mpls_label_t input_label;
const struct sr_node *srn = srp->srn;
/*
* No need to install Label for local Prefix-SID unless the
* no-PHP option is configured.
*/
if (!CHECK_FLAG(srp->sid.flags, ISIS_PREFIX_SID_NO_PHP)
|| CHECK_FLAG(srp->sid.flags, ISIS_PREFIX_SID_EXPLICIT_NULL))
return -1;
sr_debug(" |- Installing Prefix-SID %pFX %s %u (%s) with nexthop self",
&srp->prefix, IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value, circuit_t2string(srn->level));
/* Compute input label and check that is valid. */
input_label = sr_prefix_in_label(srp);
if (input_label == MPLS_INVALID_LABEL)
return -1;
/* Update internal state. */
srp->input_label = input_label;
isis_sr_nexthop_update(&srp->u.local.info, MPLS_LABEL_IMPLICIT_NULL);
/* Install Prefix-SID in the forwarding plane. */
isis_zebra_send_prefix_sid(ZEBRA_MPLS_LABELS_REPLACE, srp);
return 0;
}
/**
* Process remote Prefix-SID and install it if possible. Input and Output
* labels are computed before installing them in LFIB.
*
* @param srp Segment Routing Prefix
*
* @return 0 on success, -1 otherwise
*/
static int sr_prefix_install_remote(struct sr_prefix *srp)
{
const struct sr_node *srn = srp->srn;
struct isis_area *area = srn->area;
enum spf_tree_id tree_id;
struct listnode *node;
struct isis_nexthop *nexthop;
mpls_label_t input_label;
size_t nexthop_num = 0;
/* Lookup to associated IS-IS route. */
tree_id = (srp->prefix.family == AF_INET) ? SPFTREE_IPV4 : SPFTREE_IPV6;
srp->u.remote.rinfo = sr_prefix_lookup_route(area, tree_id, srp);
if (!srp->u.remote.rinfo)
/* SPF hasn't converged for this route yet. */
return -1;
/* Compute input label and check that is valid. */
input_label = sr_prefix_in_label(srp);
if (input_label == MPLS_INVALID_LABEL)
return -1;
sr_debug(" |- Installing Prefix-SID %pFX %s %u (%s)", &srp->prefix,
IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value, circuit_t2string(srn->level));
/* Process all SPF nexthops */
for (ALL_LIST_ELEMENTS_RO(srp->u.remote.rinfo->nexthops, node,
nexthop)) {
struct sr_node *srn_nexthop;
mpls_label_t output_label;
/* Check if the nexthop advertised a SRGB. */
srn_nexthop = sr_node_find(area, srn->level, nexthop->sysid);
if (!srn_nexthop)
goto next;
/*
* Check if the nexthop can handle SR-MPLS encapsulated IPv4 or
* IPv6 packets.
*/
if ((nexthop->family == AF_INET
&& !IS_SR_IPV4(srn_nexthop->cap.srgb))
|| (nexthop->family == AF_INET6
&& !IS_SR_IPV6(srn_nexthop->cap.srgb)))
goto next;
/* Compute output label and check if it is valid */
output_label =
sr_prefix_out_label(srp, srn_nexthop, nexthop->sysid);
if (output_label == MPLS_INVALID_LABEL)
goto next;
if (IS_DEBUG_SR) {
static char buf[INET6_ADDRSTRLEN];
inet_ntop(nexthop->family, &nexthop->ip, buf,
sizeof(buf));
zlog_debug(" |- nexthop %s label %u", buf,
output_label);
}
isis_sr_nexthop_update(&nexthop->sr, output_label);
nexthop_num++;
continue;
next:
isis_sr_nexthop_reset(&nexthop->sr);
}
/* Check that we found at least one valid nexthop */
if (nexthop_num == 0) {
sr_debug(" |- no valid nexthops");
return -1;
}
/* Update internal state. */
srp->input_label = input_label;
/* Install Prefix-SID in the forwarding plane. */
isis_zebra_send_prefix_sid(ZEBRA_MPLS_LABELS_REPLACE, srp);
return 0;
}
/**
* Process local or remote Prefix-SID and install it if possible.
*
* @param srp Segment Routing Prefix
*/
static void sr_prefix_install(struct sr_prefix *srp)
{
const struct sr_node *srn = srp->srn;
struct isis_area *area = srn->area;
int ret;
sr_debug("ISIS-Sr (%s): Install Prefix-SID %pFX %s %u", area->area_tag,
&srp->prefix, IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value);
/* L1 routes are preferred over the L2 ones. */
if (area->is_type == IS_LEVEL_1_AND_2) {
struct sr_prefix *srp_l1, *srp_l2;
switch (srn->level) {
case ISIS_LEVEL1:
srp_l2 = sr_prefix_find_by_area(area, ISIS_LEVEL2,
&srp->prefix);
if (srp_l2)
sr_prefix_uninstall(srp_l2);
break;
case ISIS_LEVEL2:
srp_l1 = sr_prefix_find_by_area(area, ISIS_LEVEL1,
&srp->prefix);
if (srp_l1)
return;
break;
default:
break;
}
}
/* Install corresponding LFIB entry */
if (srp->type == ISIS_SR_PREFIX_LOCAL)
ret = sr_prefix_install_local(srp);
else
ret = sr_prefix_install_remote(srp);
if (ret != 0)
sr_prefix_uninstall(srp);
}
/**
* Uninstall local or remote Prefix-SID.
*
* @param srp Segment Routing Prefix
*/
static void sr_prefix_uninstall(struct sr_prefix *srp)
{
struct listnode *node;
struct isis_nexthop *nexthop;
/* Check that Input Label is valid */
if (srp->input_label == MPLS_INVALID_LABEL)
return;
sr_debug("ISIS-Sr: Un-install Prefix-SID %pFX %s %u", &srp->prefix,
IS_SID_VALUE(srp->sid.flags) ? "label" : "index",
srp->sid.value);
/* Uninstall Prefix-SID from the forwarding plane. */
isis_zebra_send_prefix_sid(ZEBRA_MPLS_LABELS_DELETE, srp);
/* Reset internal state. */
srp->input_label = MPLS_INVALID_LABEL;
switch (srp->type) {
case ISIS_SR_PREFIX_LOCAL:
isis_sr_nexthop_reset(&srp->u.local.info);
break;
case ISIS_SR_PREFIX_REMOTE:
if (srp->u.remote.rinfo) {
for (ALL_LIST_ELEMENTS_RO(srp->u.remote.rinfo->nexthops,
node, nexthop))
isis_sr_nexthop_reset(&nexthop->sr);
}
break;
}
}
/**
* Reinstall local or remote Prefix-SID.
*
* @param srp Segment Routing Prefix
*/
static inline void sr_prefix_reinstall(struct sr_prefix *srp,
bool make_before_break)
{
/*
* Make Before Break can be used only when we know for sure that
* the Prefix-SID input label hasn't changed. Otherwise we need to
* uninstall the Prefix-SID first using the old input label before
* reinstalling it.
*/
if (!make_before_break)
sr_prefix_uninstall(srp);
/* New input label is computed in sr_prefix_install() function */
sr_prefix_install(srp);
}
/* --- IS-IS LSP Parse functions -------------------------------------------- */
/**
* Compare Router Capabilities. Only Flags, SRGB and Algorithm are used for the
* comparison. MSD and SRLB modification must not trigger and SR-Prefix update.
*
* @param r1 First Router Capabilities to compare
* @param r2 Second Router Capabilities to compare
* @return 0 if r1 and r2 are equal or -1 otherwise
*/
static int router_cap_cmp(const struct isis_router_cap *r1,
const struct isis_router_cap *r2)
{
if (r1->flags == r2->flags
&& r1->srgb.lower_bound == r2->srgb.lower_bound
&& r1->srgb.range_size == r2->srgb.range_size
&& r1->algo[0] == r2->algo[0])
return 0;
else
return -1;
}
/**
* Parse all SR-related information from the given Router Capabilities TLV.
*
* @param area IS-IS area
* @param level IS-IS level
* @param sysid System ID of the LSP
* @param router_cap Router Capability subTLVs
*
* @return Segment Routing Node structure for this System ID
*/
static struct sr_node *
parse_router_cap_tlv(struct isis_area *area, int level, const uint8_t *sysid,
const struct isis_router_cap *router_cap)
{
struct sr_node *srn;
if (!router_cap || router_cap->srgb.range_size == 0)
return NULL;
sr_debug("ISIS-Sr (%s): Parse Router Capability TLV", area->area_tag);
srn = sr_node_find(area, level, sysid);