forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathospf_sr.c
2976 lines (2553 loc) · 80.5 KB
/
ospf_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
* as per RFC 8665 - OSPF Extensions for Segment Routing
* and RFC 8476 - Signaling Maximum SID Depth (MSD) Using OSPF
*
* Module name: Segment Routing
*
* Author: Olivier Dugeon <olivier.dugeon@orange.com>
* Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
*
* Copyright (C) 2016 - 2020 Orange Labs http://www.orange.com
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <zebra.h>
#include "printfrr.h"
#include "command.h"
#include "hash.h"
#include "if.h"
#include "if.h"
#include "jhash.h"
#include "libospf.h" /* for ospf interface types */
#include "linklist.h"
#include "log.h"
#include "memory.h"
#include "monotime.h"
#include "network.h"
#include "prefix.h"
#include "sockunion.h" /* for inet_aton() */
#include "stream.h"
#include "table.h"
#include "thread.h"
#include "vty.h"
#include "zclient.h"
#include "sbuf.h"
#include <lib/json.h>
#include "ospf_errors.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_ase.h"
#include "ospfd/ospf_sr.h"
#include "ospfd/ospf_ri.h"
#include "ospfd/ospf_ext.h"
#include "ospfd/ospf_zebra.h"
/*
* Global variable to manage Segment Routing on this node.
* Note that all parameter values are stored in network byte order.
*/
static struct ospf_sr_db OspfSR;
static void ospf_sr_register_vty(void);
static inline void del_adj_sid(struct sr_nhlfe nhlfe);
static int ospf_sr_start(struct ospf *ospf);
/*
* Segment Routing Data Base functions
*/
/* Hash function for Segment Routing entry */
static unsigned int sr_hash(const void *p)
{
const struct in_addr *rid = p;
return jhash_1word(rid->s_addr, 0);
}
/* Compare 2 Router ID hash entries based on SR Node */
static bool sr_cmp(const void *p1, const void *p2)
{
const struct sr_node *srn = p1;
const struct in_addr *rid = p2;
return IPV4_ADDR_SAME(&srn->adv_router, rid);
}
/* Functions to remove an SR Link */
static void del_sr_link(void *val)
{
struct sr_link *srl = (struct sr_link *)val;
del_adj_sid(srl->nhlfe[0]);
del_adj_sid(srl->nhlfe[1]);
XFREE(MTYPE_OSPF_SR_PARAMS, val);
}
/* Functions to remove an SR Prefix */
static void del_sr_pref(void *val)
{
struct sr_prefix *srp = (struct sr_prefix *)val;
ospf_zebra_delete_prefix_sid(srp);
XFREE(MTYPE_OSPF_SR_PARAMS, val);
}
/* Allocate new Segment Routine node */
static struct sr_node *sr_node_new(struct in_addr *rid)
{
if (rid == NULL)
return NULL;
struct sr_node *new;
/* Allocate Segment Routing node memory */
new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_node));
/* Default Algorithm, SRGB and MSD */
for (int i = 0; i < ALGORITHM_COUNT; i++)
new->algo[i] = SR_ALGORITHM_UNSET;
new->srgb.range_size = 0;
new->srgb.lower_bound = 0;
new->msd = 0;
/* Create Link, Prefix and Range TLVs list */
new->ext_link = list_new();
new->ext_prefix = list_new();
new->ext_link->del = del_sr_link;
new->ext_prefix->del = del_sr_pref;
IPV4_ADDR_COPY(&new->adv_router, rid);
new->neighbor = NULL;
new->instance = 0;
osr_debug(" |- Created new SR node for %pI4", &new->adv_router);
return new;
}
/* Supposed to be used for testing */
struct sr_node *ospf_sr_node_create(struct in_addr *rid)
{
struct sr_node *srn;
srn = hash_get(OspfSR.neighbors, (void *)rid, (void *)sr_node_new);
return srn;
}
/* Delete Segment Routing node */
static void sr_node_del(struct sr_node *srn)
{
/* Sanity Check */
if (srn == NULL)
return;
osr_debug(" |- Delete SR node for %pI4", &srn->adv_router);
/* Clean Extended Link */
list_delete(&srn->ext_link);
/* Clean Prefix List */
list_delete(&srn->ext_prefix);
XFREE(MTYPE_OSPF_SR_PARAMS, srn);
}
/* Get SR Node for a given nexthop */
static struct sr_node *get_sr_node_by_nexthop(struct ospf *ospf,
struct in_addr nexthop)
{
struct ospf_interface *oi = NULL;
struct ospf_neighbor *nbr = NULL;
struct listnode *node;
struct route_node *rn;
struct sr_node *srn;
bool found;
/* Sanity check */
if (OspfSR.neighbors == NULL)
return NULL;
osr_debug(" |- Search SR-Node for nexthop %pI4", &nexthop);
/* First, search neighbor Router ID for this nexthop */
found = false;
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
nbr = rn->info;
if ((nbr) && (IPV4_ADDR_SAME(&nexthop, &nbr->src))) {
found = true;
break;
}
}
if (found)
break;
}
if (!found)
return NULL;
osr_debug(" |- Found nexthop Router ID %pI4", &nbr->router_id);
/* Then, search SR Node */
srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, &nbr->router_id);
return srn;
}
/*
* Segment Routing Local Block management functions
*/
/**
* It is necessary to known which label is already allocated to manage the range
* of SRLB. This is particular useful when an interface flap (goes up / down
* frequently). Here, SR will release and then allocate label for the Adjacency
* for each concerned interface. If we don't care, there is a risk to run out of
* label.
*
* For that purpose, a similar principle as already provided to manage chunk of
* label is proposed. But, here, the label chunk has not a fix range of 64
* labels that could be easily manage with a single variable of 64 bits size.
* So, used_mark is used as a bit wise to mark label reserved (bit set) or not
* (bit unset). Its size is equal to the number of label of the SRLB range round
* up to 64 bits.
*
* - sr__local_block_init() computes the number of 64 bits variables that are
* needed to manage the SRLB range and allocates this number.
* - ospf_sr_local_block_request_label() pick up the first available label and
* set corresponding bit
* - ospf_sr_local_block_release_label() release label by reseting the
* corresponding bit and set the next label to the first free position
*/
/**
* Initialize Segment Routing Local Block from SRDB configuration and reserve
* block of bits to manage label allocation.
*
* @param lower_bound The lower bound of the SRLB range
* @param upper_bound The upper bound of the SRLB range
*
* @return 0 on success, -1 otherwise
*/
static int sr_local_block_init(uint32_t lower_bound, uint32_t upper_bound)
{
struct sr_local_block *srlb = &OspfSR.srlb;
uint32_t size;
/* Check if SRLB is not already configured */
if (srlb->reserved)
return 0;
/*
* Request SRLB to the label manager. If the allocation fails, return
* an error to disable SR until a new SRLB is successfully allocated.
*/
size = upper_bound - lower_bound + 1;
if (ospf_zebra_request_label_range(lower_bound, size)) {
zlog_err("SR: Error reserving SRLB [%u/%u] %u labels",
lower_bound, upper_bound, size);
return -1;
}
osr_debug("SR: Got new SRLB [%u/%u], %u labels", lower_bound,
upper_bound, size);
/* Initialize the SRLB */
srlb->start = lower_bound;
srlb->end = upper_bound;
srlb->current = 0;
/* Compute the needed Used Mark number and allocate them */
srlb->max_block = size / SRLB_BLOCK_SIZE;
if ((size % SRLB_BLOCK_SIZE) != 0)
srlb->max_block++;
srlb->used_mark = XCALLOC(MTYPE_OSPF_SR_PARAMS,
srlb->max_block * SRLB_BLOCK_SIZE);
srlb->reserved = true;
return 0;
}
static int sr_global_block_init(uint32_t start, uint32_t size)
{
struct sr_global_block *srgb = &OspfSR.srgb;
/* Check if already configured */
if (srgb->reserved)
return 0;
/* request chunk */
uint32_t end = start + size - 1;
if (ospf_zebra_request_label_range(start, size) < 0) {
zlog_err("SR: Error reserving SRGB [%u/%u], %u labels", start,
end, size);
return -1;
}
osr_debug("SR: Got new SRGB [%u/%u], %u labels", start, end, size);
/* success */
srgb->start = start;
srgb->size = size;
srgb->reserved = true;
return 0;
}
/**
* Remove Segment Routing Local Block.
*
*/
static void sr_local_block_delete(void)
{
struct sr_local_block *srlb = &OspfSR.srlb;
/* Check if SRLB is not already delete */
if (!srlb->reserved)
return;
osr_debug("SR (%s): Remove SRLB [%u/%u]", __func__, srlb->start,
srlb->end);
/* First release the label block */
ospf_zebra_release_label_range(srlb->start, srlb->end);
/* Then reset SRLB structure */
if (srlb->used_mark != NULL)
XFREE(MTYPE_OSPF_SR_PARAMS, srlb->used_mark);
srlb->reserved = false;
}
/**
* Remove Segment Routing Global block
*/
static void sr_global_block_delete(void)
{
struct sr_global_block *srgb = &OspfSR.srgb;
if (!srgb->reserved)
return;
osr_debug("SR (%s): Remove SRGB [%u/%u]", __func__, srgb->start,
srgb->start + srgb->size - 1);
ospf_zebra_release_label_range(srgb->start,
srgb->start + srgb->size - 1);
srgb->reserved = false;
}
/**
* Request a label from the Segment Routing Local Block.
*
* @return First available label on success or MPLS_INVALID_LABEL if the
* block of labels is full
*/
mpls_label_t ospf_sr_local_block_request_label(void)
{
struct sr_local_block *srlb = &OspfSR.srlb;
mpls_label_t label;
uint32_t index;
uint32_t pos;
uint32_t size = srlb->end - srlb->start + 1;
/* Check if we ran out of available labels */
if (srlb->current >= size)
return MPLS_INVALID_LABEL;
/* Get first available label and mark it used */
label = srlb->current + srlb->start;
index = srlb->current / SRLB_BLOCK_SIZE;
pos = 1ULL << (srlb->current % SRLB_BLOCK_SIZE);
srlb->used_mark[index] |= pos;
/* Jump to the next free position */
srlb->current++;
pos = srlb->current % SRLB_BLOCK_SIZE;
while (srlb->current < size) {
if (pos == 0)
index++;
if (!((1ULL << pos) & srlb->used_mark[index]))
break;
else {
srlb->current++;
pos = srlb->current % SRLB_BLOCK_SIZE;
}
}
if (srlb->current == size)
zlog_warn(
"SR: Warning, SRLB is depleted and next label request will fail");
return label;
}
/**
* Release label in the Segment Routing Local Block.
*
* @param label Label to be release
*
* @return 0 on success or -1 if label falls outside SRLB
*/
int ospf_sr_local_block_release_label(mpls_label_t label)
{
struct sr_local_block *srlb = &OspfSR.srlb;
uint32_t index;
uint32_t pos;
/* Check that label falls inside the SRLB */
if ((label < srlb->start) || (label > srlb->end)) {
flog_warn(EC_OSPF_SR_SID_OVERFLOW,
"%s: Returning label %u is outside SRLB [%u/%u]",
__func__, label, srlb->start, srlb->end);
return -1;
}
index = (label - srlb->start) / SRLB_BLOCK_SIZE;
pos = 1ULL << ((label - srlb->start) % SRLB_BLOCK_SIZE);
srlb->used_mark[index] &= ~pos;
/* Reset current to the first available position */
for (index = 0; index < srlb->max_block; index++) {
if (srlb->used_mark[index] != 0xFFFFFFFFFFFFFFFF) {
for (pos = 0; pos < SRLB_BLOCK_SIZE; pos++)
if (!((1ULL << pos) & srlb->used_mark[index])) {
srlb->current =
index * SRLB_BLOCK_SIZE + pos;
break;
}
break;
}
}
return 0;
}
/*
* Segment Routing Initialization functions
*/
/**
* Thread function to re-attempt connection to the Label Manager and thus be
* able to start Segment Routing.
*
* @param start Thread structure that contains area as argument
*
* @return 1 on success
*/
static void sr_start_label_manager(struct thread *start)
{
struct ospf *ospf;
ospf = THREAD_ARG(start);
/* re-attempt to start SR & Label Manager connection */
ospf_sr_start(ospf);
}
/* Segment Routing starter function */
static int ospf_sr_start(struct ospf *ospf)
{
struct route_node *rn;
struct ospf_lsa *lsa;
struct sr_node *srn;
int rc = 0;
osr_debug("SR (%s): Start Segment Routing", __func__);
/* Initialize self SR Node if not already done */
if (OspfSR.self == NULL) {
srn = hash_get(OspfSR.neighbors, (void *)&(ospf->router_id),
(void *)sr_node_new);
/* Complete & Store self SR Node */
srn->srgb.range_size = OspfSR.srgb.size;
srn->srgb.lower_bound = OspfSR.srgb.start;
srn->srlb.lower_bound = OspfSR.srlb.start;
srn->srlb.range_size = OspfSR.srlb.end - OspfSR.srlb.start + 1;
srn->algo[0] = OspfSR.algo[0];
srn->msd = OspfSR.msd;
OspfSR.self = srn;
}
/* Then, start Label Manager if not ready */
if (!ospf_zebra_label_manager_ready())
if (ospf_zebra_label_manager_connect() < 0) {
/* Re-attempt to connect to Label Manager in 1 sec. */
thread_add_timer(master, sr_start_label_manager, ospf,
1, &OspfSR.t_start_lm);
osr_debug(" |- Failed to start the Label Manager");
return -1;
}
/*
* Request SRLB & SGRB to the label manager if not already reserved.
* If the allocation fails, return an error to disable SR until a new
* SRLB and/or SRGB are successfully allocated.
*/
if (sr_local_block_init(OspfSR.srlb.start, OspfSR.srlb.end) < 0)
return -1;
if (sr_global_block_init(OspfSR.srgb.start, OspfSR.srgb.size) < 0)
return -1;
/* SR is UP and ready to flood LSA */
OspfSR.status = SR_UP;
/* Set Router Information SR parameters */
osr_debug("SR: Activate SR for Router Information LSA");
ospf_router_info_update_sr(true, OspfSR.self);
/* Update Ext LSA */
osr_debug("SR: Activate SR for Extended Link/Prefix LSA");
ospf_ext_update_sr(true);
osr_debug("SR (%s): Update SR-DB from LSDB", __func__);
/* Start by looking to Router Info & Extended LSA in lsdb */
if ((ospf != NULL) && (ospf->backbone != NULL)) {
LSDB_LOOP (OPAQUE_AREA_LSDB(ospf->backbone), rn, lsa) {
if (IS_LSA_MAXAGE(lsa) || IS_LSA_SELF(lsa))
continue;
int lsa_id =
GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
switch (lsa_id) {
case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
ospf_sr_ri_lsa_update(lsa);
break;
case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
ospf_sr_ext_prefix_lsa_update(lsa);
break;
case OPAQUE_TYPE_EXTENDED_LINK_LSA:
ospf_sr_ext_link_lsa_update(lsa);
break;
default:
break;
}
}
}
rc = 1;
return rc;
}
/* Stop Segment Routing */
static void ospf_sr_stop(void)
{
if (OspfSR.status == SR_OFF)
return;
osr_debug("SR (%s): Stop Segment Routing", __func__);
/* Disable any re-attempt to connect to Label Manager */
THREAD_OFF(OspfSR.t_start_lm);
/* Release SRGB if active */
sr_global_block_delete();
/* Release SRLB if active */
sr_local_block_delete();
/*
* Remove all SR Nodes from the Hash table. Prefix and Link SID will
* be remove though list_delete() call. See sr_node_del()
*/
hash_clean(OspfSR.neighbors, (void *)sr_node_del);
OspfSR.self = NULL;
OspfSR.status = SR_OFF;
}
/*
* Segment Routing initialize function
*
* @param - nothing
*
* @return 0 if OK, -1 otherwise
*/
int ospf_sr_init(void)
{
int rc = -1;
osr_debug("SR (%s): Initialize SR Data Base", __func__);
memset(&OspfSR, 0, sizeof(OspfSR));
OspfSR.status = SR_OFF;
/* Only AREA flooding is supported in this release */
OspfSR.scope = OSPF_OPAQUE_AREA_LSA;
/* Initialize Algorithms, SRGB, SRLB and MSD TLVs */
/* Only Algorithm SPF is supported */
OspfSR.algo[0] = SR_ALGORITHM_SPF;
for (int i = 1; i < ALGORITHM_COUNT; i++)
OspfSR.algo[i] = SR_ALGORITHM_UNSET;
OspfSR.srgb.size = DEFAULT_SRGB_SIZE;
OspfSR.srgb.start = DEFAULT_SRGB_LABEL;
OspfSR.srgb.reserved = false;
OspfSR.srlb.start = DEFAULT_SRLB_LABEL;
OspfSR.srlb.end = DEFAULT_SRLB_END;
OspfSR.srlb.reserved = false;
OspfSR.msd = 0;
/* Initialize Hash table for neighbor SR nodes */
OspfSR.neighbors = hash_create(sr_hash, sr_cmp, "OSPF_SR");
if (OspfSR.neighbors == NULL)
return rc;
/* Register Segment Routing VTY command */
ospf_sr_register_vty();
rc = 0;
return rc;
}
/*
* Segment Routing termination function
*
* @param - nothing
* @return - nothing
*/
void ospf_sr_term(void)
{
/* Stop Segment Routing */
ospf_sr_stop();
/* Clear SR Node Table */
if (OspfSR.neighbors)
hash_free(OspfSR.neighbors);
}
/*
* Segment Routing finish function
*
* @param - nothing
* @return - nothing
*/
void ospf_sr_finish(void)
{
/* Stop Segment Routing */
ospf_sr_stop();
}
/*
* Following functions are used to manipulate the
* Next Hop Label Forwarding entry (NHLFE)
*/
/* Compute label from index */
static mpls_label_t index2label(uint32_t index, struct sr_block srgb)
{
mpls_label_t label;
label = srgb.lower_bound + index;
if (label > (srgb.lower_bound + srgb.range_size)) {
flog_warn(EC_OSPF_SR_SID_OVERFLOW,
"%s: SID index %u falls outside SRGB range",
__func__, index);
return MPLS_INVALID_LABEL;
} else
return label;
}
/* Get the prefix sid for a specific router id */
mpls_label_t ospf_sr_get_prefix_sid_by_id(struct in_addr *id)
{
struct sr_node *srn;
struct sr_prefix *srp;
mpls_label_t label;
srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, id);
if (srn) {
/*
* TODO: Here we assume that the SRGBs are the same,
* and that the node's prefix SID is at the head of
* the list, probably needs tweaking.
*/
srp = listnode_head(srn->ext_prefix);
label = index2label(srp->sid, srn->srgb);
} else {
label = MPLS_INVALID_LABEL;
}
return label;
}
/* Get the adjacency sid for a specific 'root' id and 'neighbor' id */
mpls_label_t ospf_sr_get_adj_sid_by_id(struct in_addr *root_id,
struct in_addr *neighbor_id)
{
struct sr_node *srn;
struct sr_link *srl;
mpls_label_t label;
struct listnode *node;
srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, root_id);
label = MPLS_INVALID_LABEL;
if (srn) {
for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) {
if (srl->type == ADJ_SID
&& srl->remote_id.s_addr == neighbor_id->s_addr) {
label = srl->sid[0];
break;
}
}
}
return label;
}
/* Get neighbor full structure from address */
static struct ospf_neighbor *get_neighbor_by_addr(struct ospf *top,
struct in_addr addr)
{
struct ospf_neighbor *nbr;
struct ospf_interface *oi;
struct listnode *node;
struct route_node *rn;
/* Sanity Check */
if (top == NULL)
return NULL;
for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi))
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
nbr = rn->info;
if (nbr)
if (IPV4_ADDR_SAME(&nbr->address.u.prefix4,
&addr)
|| IPV4_ADDR_SAME(&nbr->router_id, &addr)) {
route_unlock_node(rn);
return nbr;
}
}
return NULL;
}
/* Get OSPF Path from address */
static struct ospf_route *get_nexthop_by_addr(struct ospf *top,
struct prefix_ipv4 p)
{
struct route_node *rn;
/* Sanity Check */
if (top == NULL)
return NULL;
osr_debug(" |- Search Nexthop for prefix %pFX",
(struct prefix *)&p);
rn = route_node_lookup(top->new_table, (struct prefix *)&p);
/*
* Check if we found an OSPF route. May be NULL if SPF has not
* yet populate routing table for this prefix.
*/
if (rn == NULL)
return NULL;
route_unlock_node(rn);
return rn->info;
}
/* Compute NHLFE entry for Extended Link */
static int compute_link_nhlfe(struct sr_link *srl)
{
struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
struct ospf_neighbor *nh;
int rc = 0;
osr_debug(" |- Compute NHLFE for link %pI4", &srl->itf_addr);
/* First determine the OSPF Neighbor */
nh = get_neighbor_by_addr(top, srl->nhlfe[0].nexthop);
/* Neighbor could be not found when OSPF Adjacency just fire up
* because SPF don't yet populate routing table. This NHLFE will
* be fixed later when SR SPF schedule will be called.
*/
if (nh == NULL)
return rc;
osr_debug(" |- Found nexthop %pI4", &nh->router_id);
/* Set ifindex for this neighbor */
srl->nhlfe[0].ifindex = nh->oi->ifp->ifindex;
srl->nhlfe[1].ifindex = nh->oi->ifp->ifindex;
/* Update neighbor address for LAN_ADJ_SID */
if (srl->type == LAN_ADJ_SID) {
IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &nh->src);
IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &nh->src);
}
/* Set Input & Output Label */
if (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
srl->nhlfe[0].label_in = srl->sid[0];
else
srl->nhlfe[0].label_in =
index2label(srl->sid[0], srl->srn->srgb);
if (CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
srl->nhlfe[1].label_in = srl->sid[1];
else
srl->nhlfe[1].label_in =
index2label(srl->sid[1], srl->srn->srgb);
srl->nhlfe[0].label_out = MPLS_LABEL_IMPLICIT_NULL;
srl->nhlfe[1].label_out = MPLS_LABEL_IMPLICIT_NULL;
rc = 1;
return rc;
}
/**
* Compute output label for the given Prefix-SID.
*
* @param srp Segment Routing Prefix
* @param srnext Segment Routing nexthop node
*
* @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 *srnext)
{
/* Check if the nexthop SR Node is the last hop? */
if (srnext == srp->srn) {
/* SR-Node doesn't request NO-PHP. Return Implicit NULL label */
if (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
return MPLS_LABEL_IMPLICIT_NULL;
/* SR-Node requests Explicit NULL Label */
if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG))
return MPLS_LABEL_IPV4_EXPLICIT_NULL;
/* Fallthrough */
}
/* Return SID value as MPLS label if it is an Absolute SID */
if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG
| EXT_SUBTLV_PREFIX_SID_LFLG)) {
/*
* V/L SIDs have local significance, so only adjacent routers
* can use them (RFC8665 section #5)
*/
if (srp->srn != srnext)
return MPLS_INVALID_LABEL;
return srp->sid;
}
/* Return MPLS label as SRGB lower bound + SID index as per RFC 8665 */
return (index2label(srp->sid, srnext->srgb));
}
/*
* Compute NHLFE entry for Extended Prefix
*
* @param srp - Segment Routing Prefix
*
* @return -1 if no route is found, 0 if there is no SR route ready
* and 1 if success or update
*/
static int compute_prefix_nhlfe(struct sr_prefix *srp)
{
struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
struct ospf_path *path;
struct listnode *node;
struct sr_node *srnext;
int rc = -1;
osr_debug(" |- Compute NHLFE for prefix %pFX",
(struct prefix *)&srp->prefv4);
/* First determine the nexthop */
srp->route = get_nexthop_by_addr(top, srp->prefv4);
/* Nexthop could be not found when OSPF Adjacency just fire up
* because SPF don't yet populate routing table. This NHLFE will
* be fixed later when SR SPF schedule will be called.
*/
if (srp->route == NULL)
return rc;
/* Compute Input Label with self SRGB */
srp->label_in = index2label(srp->sid, OspfSR.self->srgb);
rc = 0;
for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
osr_debug(" |- Process new route via %pI4 for this prefix",
&path->nexthop);
/*
* Get SR-Node for this nexthop. Could be not yet available
* as Extended Link / Prefix and Router Information are flooded
* after LSA Type 1 & 2 which populate the OSPF Route Table
*/
srnext = get_sr_node_by_nexthop(top, path->nexthop);
if (srnext == NULL)
continue;
/* And store this information for later update */
srnext->neighbor = OspfSR.self;
path->srni.nexthop = srnext;
/*
* SR Node could be known, but SRGB could be not initialize
* This is due to the fact that Extended Link / Prefix could
* be received before corresponding Router Information LSA
*/
if (srnext == NULL || srnext->srgb.lower_bound == 0
|| srnext->srgb.range_size == 0) {
osr_debug(
" |- SR-Node %pI4 not ready. Stop process",
&srnext->adv_router);
path->srni.label_out = MPLS_INVALID_LABEL;
continue;
}
osr_debug(" |- Found SRGB %u/%u for next hop SR-Node %pI4",
srnext->srgb.range_size, srnext->srgb.lower_bound,
&srnext->adv_router);
/* Compute Output Label with Nexthop SR Node SRGB */
path->srni.label_out = sr_prefix_out_label(srp, srnext);
osr_debug(" |- Computed new labels in: %u out: %u",
srp->label_in, path->srni.label_out);
rc = 1;
}
return rc;
}
/* Add new NHLFE entry for Adjacency SID */
static inline void add_adj_sid(struct sr_nhlfe nhlfe)
{
if (nhlfe.label_in != 0)
ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_ADD, nhlfe);
}
/* Remove NHLFE entry for Adjacency SID */
static inline void del_adj_sid(struct sr_nhlfe nhlfe)
{
if (nhlfe.label_in != 0)
ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_DELETE, nhlfe);
}
/* Update NHLFE entry for Adjacency SID */
static inline void update_adj_sid(struct sr_nhlfe n1, struct sr_nhlfe n2)
{
del_adj_sid(n1);
add_adj_sid(n2);
}
/*
* Functions to parse and get Extended Link / Prefix
* TLVs and SubTLVs
*/
/* Extended Link SubTLVs Getter */
static struct sr_link *get_ext_link_sid(struct tlv_header *tlvh, size_t size)
{
struct sr_link *srl;
struct ext_tlv_link *link = (struct ext_tlv_link *)tlvh;
struct ext_subtlv_adj_sid *adj_sid;
struct ext_subtlv_lan_adj_sid *lan_sid;
struct ext_subtlv_rmt_itf_addr *rmt_itf;