-
Notifications
You must be signed in to change notification settings - Fork 626
/
sr_policy_rewrite.c
3679 lines (3197 loc) · 111 KB
/
sr_policy_rewrite.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
/*
* sr_policy_rewrite.c: ipv6 sr policy creation
*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* @brief SR policy creation and application
*
* Create an SR policy.
* An SR policy can be either of 'default' type or 'spray' type
* An SR policy has attached a list of SID lists.
* In case the SR policy is a default one it will load balance among them.
* An SR policy has associated a BindingSID.
* In case any packet arrives with IPv6 DA == BindingSID then the SR policy
* associated to such bindingSID will be applied to such packet.
*
* SR policies can be applied either by using IPv6 encapsulation or
* SRH insertion. Both methods can be found on this file.
*
* Traffic input usually is IPv6 packets. However it is possible to have
* IPv4 packets or L2 frames. (that are encapsulated into IPv6 with SRH)
*
* This file provides the appropriate VPP graph nodes to do any of these
* methods.
*
*/
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/srv6/sr.h>
#include <vnet/ip/ip4_inlines.h>
#include <vnet/ip/ip6_inlines.h>
#include <vnet/srv6/sr_packet.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/dpo/dpo.h>
#include <vnet/dpo/replicate_dpo.h>
#include <vnet/srv6/sr_pt.h>
#include <vppinfra/error.h>
#include <vppinfra/elog.h>
/**
* @brief SR policy rewrite trace
*/
typedef struct
{
ip6_address_t src, dst;
} sr_policy_rewrite_trace_t;
/* Graph arcs */
#define foreach_sr_policy_rewrite_next \
_(IP6_LOOKUP, "ip6-lookup") \
_(ERROR, "error-drop")
typedef enum
{
#define _(s,n) SR_POLICY_REWRITE_NEXT_##s,
foreach_sr_policy_rewrite_next
#undef _
SR_POLICY_REWRITE_N_NEXT,
} sr_policy_rewrite_next_t;
/* SR rewrite errors */
#define foreach_sr_policy_rewrite_error \
_(INTERNAL_ERROR, "Segment Routing undefined error") \
_(BSID_ZERO, "BSID with SL = 0") \
_(COUNTER_TOTAL, "SR steered IPv6 packets") \
_(COUNTER_ENCAP, "SR: Encaps packets") \
_(COUNTER_INSERT, "SR: SRH inserted packets") \
_(COUNTER_BSID, "SR: BindingSID steered packets")
typedef enum
{
#define _(sym,str) SR_POLICY_REWRITE_ERROR_##sym,
foreach_sr_policy_rewrite_error
#undef _
SR_POLICY_REWRITE_N_ERROR,
} sr_policy_rewrite_error_t;
static char *sr_policy_rewrite_error_strings[] = {
#define _(sym,string) string,
foreach_sr_policy_rewrite_error
#undef _
};
/**
* @brief Dynamically added SR SL DPO type
*/
static dpo_type_t sr_pr_encaps_dpo_type;
static dpo_type_t sr_pr_insert_dpo_type;
static dpo_type_t sr_pr_bsid_encaps_dpo_type;
static dpo_type_t sr_pr_bsid_insert_dpo_type;
/**
* @brief IPv6 SA for encapsulated packets
*/
static ip6_address_t sr_pr_encaps_src;
static u8 sr_pr_encaps_hop_limit = IPv6_DEFAULT_HOP_LIMIT;
/******************* SR rewrite set encaps IPv6 source addr *******************/
/* Note: This is temporal. We don't know whether to follow this path or
take the ip address of a loopback interface or even the OIF */
void
sr_set_source (ip6_address_t * address)
{
clib_memcpy_fast (&sr_pr_encaps_src, address, sizeof (sr_pr_encaps_src));
}
ip6_address_t *
sr_get_encaps_source ()
{
return &sr_pr_encaps_src;
}
static clib_error_t *
set_sr_src_command_fn (vlib_main_t * vm, unformat_input_t * input,
vlib_cli_command_t * cmd)
{
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
if (unformat
(input, "addr %U", unformat_ip6_address, &sr_pr_encaps_src))
return 0;
else
return clib_error_return (0, "No address specified");
}
return clib_error_return (0, "No address specified");
}
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_sr_src_command, static) = {
.path = "set sr encaps source",
.short_help = "set sr encaps source addr <ip6_addr>",
.function = set_sr_src_command_fn,
};
/* *INDENT-ON* */
/******************** SR rewrite set encaps IPv6 hop-limit ********************/
void
sr_set_hop_limit (u8 hop_limit)
{
sr_pr_encaps_hop_limit = hop_limit;
}
u8
sr_get_hop_limit (void)
{
return sr_pr_encaps_hop_limit;
}
static clib_error_t *
set_sr_hop_limit_command_fn (vlib_main_t * vm, unformat_input_t * input,
vlib_cli_command_t * cmd)
{
int hop_limit = sr_get_hop_limit ();
if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
return clib_error_return (0, "No value specified");
if (!unformat (input, "%d", &hop_limit))
return clib_error_return (0, "Invalid value");
if (hop_limit <= 0 || hop_limit > 255)
return clib_error_return (0, "Value out of range [1-255]");
sr_pr_encaps_hop_limit = (u8) hop_limit;
return 0;
}
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_sr_hop_limit_command, static) = {
.path = "set sr encaps hop-limit",
.short_help = "set sr encaps hop-limit <value>",
.function = set_sr_hop_limit_command_fn,
};
/* *INDENT-ON* */
/*********************** SR rewrite string computation ************************/
/**
* @brief SR rewrite string computation for IPv6 encapsulation (inline)
*
* @param sl is a vector of IPv6 addresses composing the Segment List
*
* @return precomputed rewrite string for encapsulation
*/
static inline u8 *
compute_rewrite_encaps (ip6_address_t *sl, u8 type)
{
ip6_header_t *iph;
ip6_sr_header_t *srh;
ip6_sr_pt_tlv_t *srh_pt_tlv;
ip6_address_t *addrp, *this_address;
u32 header_length = 0;
u8 *rs = NULL;
header_length = 0;
header_length += IPv6_DEFAULT_HEADER_LENGTH;
if (type == SR_POLICY_TYPE_TEF)
{
header_length += sizeof (ip6_sr_header_t);
header_length += vec_len (sl) * sizeof (ip6_address_t);
header_length += sizeof (ip6_sr_pt_tlv_t);
}
else if (vec_len (sl) > 1)
{
header_length += sizeof (ip6_sr_header_t);
header_length += vec_len (sl) * sizeof (ip6_address_t);
}
vec_validate (rs, header_length - 1);
iph = (ip6_header_t *) rs;
iph->ip_version_traffic_class_and_flow_label =
clib_host_to_net_u32 (0 | ((6 & 0xF) << 28));
iph->src_address.as_u64[0] = sr_pr_encaps_src.as_u64[0];
iph->src_address.as_u64[1] = sr_pr_encaps_src.as_u64[1];
iph->payload_length = header_length - IPv6_DEFAULT_HEADER_LENGTH;
iph->protocol = IP_PROTOCOL_IPV6;
iph->hop_limit = sr_pr_encaps_hop_limit;
if (type == SR_POLICY_TYPE_TEF)
{
srh = (ip6_sr_header_t *) (iph + 1);
iph->protocol = IP_PROTOCOL_IPV6_ROUTE;
srh->protocol = IP_PROTOCOL_IPV6;
srh->type = ROUTING_HEADER_TYPE_SR;
srh->flags = 0x00;
srh->tag = 0x0000;
srh->segments_left = vec_len (sl) - 1;
srh->last_entry = vec_len (sl) - 1;
srh->length =
((sizeof (ip6_sr_header_t) + (vec_len (sl) * sizeof (ip6_address_t)) +
sizeof (ip6_sr_pt_tlv_t)) /
8) -
1;
addrp = srh->segments + vec_len (sl) - 1;
vec_foreach (this_address, sl)
{
clib_memcpy_fast (addrp->as_u8, this_address->as_u8,
sizeof (ip6_address_t));
addrp--;
}
srh_pt_tlv = (ip6_sr_pt_tlv_t *) (srh->segments + vec_len (sl));
srh_pt_tlv->type = IP6_SRH_PT_TLV_TYPE;
srh_pt_tlv->length = IP6_SRH_PT_TLV_LEN;
}
else if (vec_len (sl) > 1)
{
srh = (ip6_sr_header_t *) (iph + 1);
iph->protocol = IP_PROTOCOL_IPV6_ROUTE;
srh->protocol = IP_PROTOCOL_IPV6;
srh->type = ROUTING_HEADER_TYPE_SR;
srh->segments_left = vec_len (sl) - 1;
srh->last_entry = vec_len (sl) - 1;
srh->length = ((sizeof (ip6_sr_header_t) +
(vec_len (sl) * sizeof (ip6_address_t))) / 8) - 1;
srh->flags = 0x00;
srh->tag = 0x0000;
addrp = srh->segments + vec_len (sl) - 1;
vec_foreach (this_address, sl)
{
clib_memcpy_fast (addrp->as_u8, this_address->as_u8,
sizeof (ip6_address_t));
addrp--;
}
}
iph->dst_address.as_u64[0] = sl->as_u64[0];
iph->dst_address.as_u64[1] = sl->as_u64[1];
return rs;
}
/**
* @brief SR rewrite string computation for SRH insertion (inline)
*
* @param sl is a vector of IPv6 addresses composing the Segment List
*
* @return precomputed rewrite string for SRH insertion
*/
static inline u8 *
compute_rewrite_insert (ip6_address_t *sl, u8 type)
{
ip6_sr_header_t *srh;
ip6_address_t *addrp, *this_address;
u32 header_length = 0;
u8 *rs = NULL;
header_length = 0;
header_length += sizeof (ip6_sr_header_t);
header_length += (vec_len (sl) + 1) * sizeof (ip6_address_t);
vec_validate (rs, header_length - 1);
srh = (ip6_sr_header_t *) rs;
srh->type = ROUTING_HEADER_TYPE_SR;
srh->segments_left = vec_len (sl);
srh->last_entry = vec_len (sl);
srh->length = ((sizeof (ip6_sr_header_t) +
((vec_len (sl) + 1) * sizeof (ip6_address_t))) / 8) - 1;
srh->flags = 0x00;
srh->tag = 0x0000;
addrp = srh->segments + vec_len (sl);
vec_foreach (this_address, sl)
{
clib_memcpy_fast (addrp->as_u8, this_address->as_u8,
sizeof (ip6_address_t));
addrp--;
}
return rs;
}
/**
* @brief SR rewrite string computation for SRH insertion with BSID (inline)
*
* @param sl is a vector of IPv6 addresses composing the Segment List
*
* @return precomputed rewrite string for SRH insertion with BSID
*/
static inline u8 *
compute_rewrite_bsid (ip6_address_t * sl)
{
ip6_sr_header_t *srh;
ip6_address_t *addrp, *this_address;
u32 header_length = 0;
u8 *rs = NULL;
header_length = 0;
header_length += sizeof (ip6_sr_header_t);
header_length += vec_len (sl) * sizeof (ip6_address_t);
vec_validate (rs, header_length - 1);
srh = (ip6_sr_header_t *) rs;
srh->type = ROUTING_HEADER_TYPE_SR;
srh->segments_left = vec_len (sl) - 1;
srh->last_entry = vec_len (sl) - 1;
srh->length = ((sizeof (ip6_sr_header_t) +
(vec_len (sl) * sizeof (ip6_address_t))) / 8) - 1;
srh->flags = 0x00;
srh->tag = 0x0000;
addrp = srh->segments + vec_len (sl) - 1;
vec_foreach (this_address, sl)
{
clib_memcpy_fast (addrp->as_u8, this_address->as_u8,
sizeof (ip6_address_t));
addrp--;
}
return rs;
}
/*************************** SR LB helper functions **************************/
/**
* @brief Creates a Segment List and adds it to an SR policy
*
* Creates a Segment List and adds it to the SR policy. Notice that the SL are
* not necessarily unique. Hence there might be two Segment List within the
* same SR Policy with exactly the same segments and same weight.
*
* @param sr_policy is the SR policy where the SL will be added
* @param sl is a vector of IPv6 addresses composing the Segment List
* @param weight is the weight of the SegmentList (for load-balancing purposes)
* @param is_encap represents the mode (SRH insertion vs Encapsulation)
*
* @return pointer to the just created segment list
*/
static inline ip6_sr_sl_t *
create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight,
u8 is_encap)
{
ip6_sr_main_t *sm = &sr_main;
ip6_sr_sl_t *segment_list;
sr_policy_fn_registration_t *plugin = 0;
pool_get (sm->sid_lists, segment_list);
clib_memset (segment_list, 0, sizeof (*segment_list));
vec_add1 (sr_policy->segments_lists, segment_list - sm->sid_lists);
/* Fill in segment list */
segment_list->weight =
(weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT);
segment_list->segments = vec_dup (sl);
segment_list->policy_type = sr_policy->type;
segment_list->egress_fib_table =
ip6_fib_index_from_table_id (sr_policy->fib_table);
if (is_encap)
{
segment_list->rewrite = compute_rewrite_encaps (sl, sr_policy->type);
segment_list->rewrite_bsid = segment_list->rewrite;
}
else
{
segment_list->rewrite = compute_rewrite_insert (sl, sr_policy->type);
segment_list->rewrite_bsid = compute_rewrite_bsid (sl);
}
if (sr_policy->plugin)
{
plugin =
pool_elt_at_index (sm->policy_plugin_functions,
sr_policy->plugin - SR_BEHAVIOR_LAST);
segment_list->plugin = sr_policy->plugin;
segment_list->plugin_mem = sr_policy->plugin_mem;
plugin->creation (sr_policy);
}
/* Create DPO */
dpo_reset (&segment_list->bsid_dpo);
dpo_reset (&segment_list->ip6_dpo);
dpo_reset (&segment_list->ip4_dpo);
if (is_encap)
{
if (!sr_policy->plugin)
{
dpo_set (&segment_list->ip6_dpo, sr_pr_encaps_dpo_type,
DPO_PROTO_IP6, segment_list - sm->sid_lists);
dpo_set (&segment_list->ip4_dpo, sr_pr_encaps_dpo_type,
DPO_PROTO_IP4, segment_list - sm->sid_lists);
dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_encaps_dpo_type,
DPO_PROTO_IP6, segment_list - sm->sid_lists);
}
else
{
dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6,
segment_list - sm->sid_lists);
dpo_set (&segment_list->ip4_dpo, plugin->dpo, DPO_PROTO_IP4,
segment_list - sm->sid_lists);
dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6,
segment_list - sm->sid_lists);
}
}
else
{
if (!sr_policy->plugin)
{
dpo_set (&segment_list->ip6_dpo, sr_pr_insert_dpo_type,
DPO_PROTO_IP6, segment_list - sm->sid_lists);
dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_insert_dpo_type,
DPO_PROTO_IP6, segment_list - sm->sid_lists);
}
else
{
dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6,
segment_list - sm->sid_lists);
dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6,
segment_list - sm->sid_lists);
}
}
return segment_list;
}
/**
* @brief Updates the Load-Balancer after an SR Policy change
*
* @param sr_policy is the modified SR Policy
*/
static inline void
update_lb (ip6_sr_policy_t * sr_policy)
{
flow_hash_config_t fhc;
u32 *sl_index;
ip6_sr_sl_t *segment_list;
ip6_sr_main_t *sm = &sr_main;
load_balance_path_t path;
path.path_index = FIB_NODE_INDEX_INVALID;
load_balance_path_t *ip4_path_vector = 0;
load_balance_path_t *ip6_path_vector = 0;
load_balance_path_t *b_path_vector = 0;
/* In case LB does not exist, create it */
if (!dpo_id_is_valid (&sr_policy->bsid_dpo))
{
fib_prefix_t pfx = {
.fp_proto = FIB_PROTOCOL_IP6,
.fp_len = 128,
.fp_addr = {
.ip6 = sr_policy->bsid,
}
};
/* Add FIB entry for BSID */
fhc = fib_table_get_flow_hash_config (sr_policy->fib_table,
FIB_PROTOCOL_IP6);
dpo_set (&sr_policy->bsid_dpo, DPO_LOAD_BALANCE, DPO_PROTO_IP6,
load_balance_create (0, DPO_PROTO_IP6, fhc));
dpo_set (&sr_policy->ip6_dpo, DPO_LOAD_BALANCE, DPO_PROTO_IP6,
load_balance_create (0, DPO_PROTO_IP6, fhc));
/* Update FIB entry's to point to the LB DPO in the main FIB and hidden one */
fib_table_entry_special_dpo_update (fib_table_find (FIB_PROTOCOL_IP6,
sr_policy->fib_table),
&pfx, FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->bsid_dpo);
fib_table_entry_special_dpo_update (sm->fib_table_ip6,
&pfx,
FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->ip6_dpo);
if (sr_policy->is_encap)
{
dpo_set (&sr_policy->ip4_dpo, DPO_LOAD_BALANCE, DPO_PROTO_IP4,
load_balance_create (0, DPO_PROTO_IP4, fhc));
fib_table_entry_special_dpo_update (sm->fib_table_ip4,
&pfx,
FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->ip4_dpo);
}
}
/* Create the LB path vector */
vec_foreach (sl_index, sr_policy->segments_lists)
{
segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
path.path_dpo = segment_list->bsid_dpo;
path.path_weight = segment_list->weight;
vec_add1 (b_path_vector, path);
path.path_dpo = segment_list->ip6_dpo;
vec_add1 (ip6_path_vector, path);
if (sr_policy->is_encap)
{
path.path_dpo = segment_list->ip4_dpo;
vec_add1 (ip4_path_vector, path);
}
}
/* Update LB multipath */
load_balance_multipath_update (&sr_policy->bsid_dpo, b_path_vector,
LOAD_BALANCE_FLAG_NONE);
load_balance_multipath_update (&sr_policy->ip6_dpo, ip6_path_vector,
LOAD_BALANCE_FLAG_NONE);
if (sr_policy->is_encap)
load_balance_multipath_update (&sr_policy->ip4_dpo, ip4_path_vector,
LOAD_BALANCE_FLAG_NONE);
/* Cleanup */
vec_free (b_path_vector);
vec_free (ip6_path_vector);
vec_free (ip4_path_vector);
}
/**
* @brief Updates the Replicate DPO after an SR Policy change
*
* @param sr_policy is the modified SR Policy (type spray)
*/
static inline void
update_replicate (ip6_sr_policy_t * sr_policy)
{
u32 *sl_index;
ip6_sr_sl_t *segment_list;
ip6_sr_main_t *sm = &sr_main;
load_balance_path_t path;
path.path_index = FIB_NODE_INDEX_INVALID;
load_balance_path_t *b_path_vector = 0;
load_balance_path_t *ip6_path_vector = 0;
load_balance_path_t *ip4_path_vector = 0;
/* In case LB does not exist, create it */
if (!dpo_id_is_valid (&sr_policy->bsid_dpo))
{
dpo_set (&sr_policy->bsid_dpo, DPO_REPLICATE,
DPO_PROTO_IP6, replicate_create (0, DPO_PROTO_IP6));
dpo_set (&sr_policy->ip6_dpo, DPO_REPLICATE,
DPO_PROTO_IP6, replicate_create (0, DPO_PROTO_IP6));
/* Update FIB entry's DPO to point to SR without LB */
fib_prefix_t pfx = {
.fp_proto = FIB_PROTOCOL_IP6,
.fp_len = 128,
.fp_addr = {
.ip6 = sr_policy->bsid,
}
};
fib_table_entry_special_dpo_update (fib_table_find (FIB_PROTOCOL_IP6,
sr_policy->fib_table),
&pfx, FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->bsid_dpo);
fib_table_entry_special_dpo_update (sm->fib_table_ip6,
&pfx,
FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->ip6_dpo);
if (sr_policy->is_encap)
{
dpo_set (&sr_policy->ip4_dpo, DPO_REPLICATE, DPO_PROTO_IP4,
replicate_create (0, DPO_PROTO_IP4));
fib_table_entry_special_dpo_update (sm->fib_table_ip4,
&pfx,
FIB_SOURCE_SR,
FIB_ENTRY_FLAG_EXCLUSIVE,
&sr_policy->ip4_dpo);
}
}
/* Create the replicate path vector */
path.path_weight = 1;
vec_foreach (sl_index, sr_policy->segments_lists)
{
segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
path.path_dpo = segment_list->bsid_dpo;
vec_add1 (b_path_vector, path);
path.path_dpo = segment_list->ip6_dpo;
vec_add1 (ip6_path_vector, path);
if (sr_policy->is_encap)
{
path.path_dpo = segment_list->ip4_dpo;
vec_add1 (ip4_path_vector, path);
}
}
/* Update replicate multipath */
replicate_multipath_update (&sr_policy->bsid_dpo, b_path_vector);
replicate_multipath_update (&sr_policy->ip6_dpo, ip6_path_vector);
if (sr_policy->is_encap)
replicate_multipath_update (&sr_policy->ip4_dpo, ip4_path_vector);
}
/******************************* SR rewrite API *******************************/
/* Three functions for handling sr policies:
* -> sr_policy_add
* -> sr_policy_del
* -> sr_policy_mod
* All of them are API. CLI function on sr_policy_command_fn */
/**
* @brief Create a new SR policy
*
* @param bsid is the bindingSID of the SR Policy
* @param segments is a vector of IPv6 address composing the segment list
* @param weight is the weight of the sid list. optional.
* @param behavior is the behavior of the SR policy. (default//spray)
* @param fib_table is the VRF where to install the FIB entry for the BSID
* @param is_encap (bool) whether SR policy should behave as Encap/SRH Insertion
*
* @return 0 if correct, else error
*/
int
sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments, u32 weight,
u8 type, u32 fib_table, u8 is_encap, u16 plugin,
void *ls_plugin_mem)
{
ip6_sr_main_t *sm = &sr_main;
ip6_sr_policy_t *sr_policy = 0;
uword *p;
/* Search for existing keys (BSID) */
p = mhash_get (&sm->sr_policies_index_hash, bsid);
if (p)
{
/* Add SR policy that already exists; complain */
return -12;
}
/* Search collision in FIB entries */
/* Explanation: It might be possible that some other entity has already
* created a route for the BSID. This in theory is impossible, but in
* practise we could see it. Assert it and scream if needed */
fib_prefix_t pfx = {
.fp_proto = FIB_PROTOCOL_IP6,
.fp_len = 128,
.fp_addr = {
.ip6 = *bsid,
}
};
/* Lookup the FIB index associated to the table selected */
u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6,
(fib_table != (u32) ~ 0 ? fib_table : 0));
if (fib_index == ~0)
return -13;
/* Lookup whether there exists an entry for the BSID */
fib_node_index_t fei = fib_table_lookup_exact_match (fib_index, &pfx);
if (FIB_NODE_INDEX_INVALID != fei)
return -12; //There is an entry for such lookup
/* Add an SR policy object */
pool_get (sm->sr_policies, sr_policy);
clib_memset (sr_policy, 0, sizeof (*sr_policy));
clib_memcpy_fast (&sr_policy->bsid, bsid, sizeof (ip6_address_t));
sr_policy->type = type;
sr_policy->fib_table = (fib_table != (u32) ~ 0 ? fib_table : 0); //Is default FIB 0 ?
sr_policy->is_encap = is_encap;
if (plugin)
{
sr_policy->plugin = plugin;
sr_policy->plugin_mem = ls_plugin_mem;
}
/* Copy the key */
mhash_set (&sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies,
NULL);
/* Create a segment list and add the index to the SR policy */
create_sl (sr_policy, segments, weight, is_encap);
/* If FIB doesnt exist, create them */
if (sm->fib_table_ip6 == (u32) ~ 0)
{
sm->fib_table_ip6 = fib_table_create_and_lock (FIB_PROTOCOL_IP6,
FIB_SOURCE_SR,
"SRv6 steering of IP6 prefixes through BSIDs");
sm->fib_table_ip4 = fib_table_create_and_lock (FIB_PROTOCOL_IP6,
FIB_SOURCE_SR,
"SRv6 steering of IP4 prefixes through BSIDs");
}
/* Create IPv6 FIB for the BindingSID attached to the DPO of the only SL */
if (sr_policy->type == SR_POLICY_TYPE_DEFAULT ||
sr_policy->type == SR_POLICY_TYPE_TEF)
update_lb (sr_policy);
else if (sr_policy->type == SR_POLICY_TYPE_SPRAY)
update_replicate (sr_policy);
return 0;
}
/**
* @brief Delete a SR policy
*
* @param bsid is the bindingSID of the SR Policy
* @param index is the index of the SR policy
*
* @return 0 if correct, else error
*/
int
sr_policy_del (ip6_address_t * bsid, u32 index)
{
ip6_sr_main_t *sm = &sr_main;
ip6_sr_policy_t *sr_policy = 0;
ip6_sr_sl_t *segment_list;
u32 *sl_index;
uword *p;
if (bsid)
{
p = mhash_get (&sm->sr_policies_index_hash, bsid);
if (p)
sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
else
return -1;
}
else
{
sr_policy = pool_elt_at_index (sm->sr_policies, index);
}
/* Remove BindingSID FIB entry */
fib_prefix_t pfx = {
.fp_proto = FIB_PROTOCOL_IP6,
.fp_len = 128,
.fp_addr = {
.ip6 = sr_policy->bsid,
}
,
};
fib_table_entry_special_remove (fib_table_find (FIB_PROTOCOL_IP6,
sr_policy->fib_table),
&pfx, FIB_SOURCE_SR);
fib_table_entry_special_remove (sm->fib_table_ip6, &pfx, FIB_SOURCE_SR);
if (sr_policy->is_encap)
fib_table_entry_special_remove (sm->fib_table_ip4, &pfx, FIB_SOURCE_SR);
if (dpo_id_is_valid (&sr_policy->bsid_dpo))
{
dpo_reset (&sr_policy->bsid_dpo);
dpo_reset (&sr_policy->ip4_dpo);
dpo_reset (&sr_policy->ip6_dpo);
}
/* Clean SID Lists */
vec_foreach (sl_index, sr_policy->segments_lists)
{
segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
vec_free (segment_list->segments);
vec_free (segment_list->rewrite);
if (!sr_policy->is_encap)
vec_free (segment_list->rewrite_bsid);
pool_put_index (sm->sid_lists, *sl_index);
}
if (sr_policy->plugin)
{
sr_policy_fn_registration_t *plugin = 0;
plugin =
pool_elt_at_index (sm->policy_plugin_functions,
sr_policy->plugin - SR_BEHAVIOR_LAST);
plugin->removal (sr_policy);
sr_policy->plugin = 0;
sr_policy->plugin_mem = NULL;
}
/* Remove SR policy entry */
mhash_unset (&sm->sr_policies_index_hash, &sr_policy->bsid, NULL);
pool_put (sm->sr_policies, sr_policy);
/* If FIB empty unlock it */
if (!pool_elts (sm->sr_policies) && !pool_elts (sm->steer_policies))
{
fib_table_unlock (sm->fib_table_ip6, FIB_PROTOCOL_IP6, FIB_SOURCE_SR);
fib_table_unlock (sm->fib_table_ip4, FIB_PROTOCOL_IP6, FIB_SOURCE_SR);
sm->fib_table_ip6 = (u32) ~ 0;
sm->fib_table_ip4 = (u32) ~ 0;
}
return 0;
}
/**
* @brief Modify an existing SR policy
*
* The possible modifications are adding a new Segment List, modifying an
* existing Segment List (modify the weight only) and delete a given
* Segment List from the SR Policy.
*
* @param bsid is the bindingSID of the SR Policy
* @param index is the index of the SR policy
* @param fib_table is the VRF where to install the FIB entry for the BSID
* @param operation is the operation to perform (among the top ones)
* @param segments is a vector of IPv6 address composing the segment list
* @param sl_index is the index of the Segment List to modify/delete
* @param weight is the weight of the sid list. optional.
* @param is_encap Mode. Encapsulation or SRH insertion.
*
* @return 0 if correct, else error
*/
int
sr_policy_mod (ip6_address_t * bsid, u32 index, u32 fib_table,
u8 operation, ip6_address_t * segments, u32 sl_index,
u32 weight)
{
ip6_sr_main_t *sm = &sr_main;
ip6_sr_policy_t *sr_policy = 0;
ip6_sr_sl_t *segment_list;
u32 *sl_index_iterate;
uword *p;
if (bsid)
{
p = mhash_get (&sm->sr_policies_index_hash, bsid);
if (p)
sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
else
return -1;
}
else
{
sr_policy = pool_elt_at_index (sm->sr_policies, index);
}
if (operation == 1) /* Add SR List to an existing SR policy */
{
/* Create the new SL */
segment_list =
create_sl (sr_policy, segments, weight, sr_policy->is_encap);
/* Create a new LB DPO */
if (sr_policy->type == SR_POLICY_TYPE_DEFAULT)
update_lb (sr_policy);
else if (sr_policy->type == SR_POLICY_TYPE_SPRAY)
update_replicate (sr_policy);
}
else if (operation == 2) /* Delete SR List from an existing SR policy */
{
/* Check that currently there are more than one SID list */
if (vec_len (sr_policy->segments_lists) == 1)
return -21;
/* Check that the SR list does exist and is assigned to the sr policy */
vec_foreach (sl_index_iterate, sr_policy->segments_lists)
if (*sl_index_iterate == sl_index)
break;
if (*sl_index_iterate != sl_index)
return -22;
/* Remove the lucky SR list that is being kicked out */
segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
vec_free (segment_list->segments);
vec_free (segment_list->rewrite);
if (!sr_policy->is_encap)
vec_free (segment_list->rewrite_bsid);
pool_put_index (sm->sid_lists, sl_index);
vec_del1 (sr_policy->segments_lists,
sl_index_iterate - sr_policy->segments_lists);
/* Create a new LB DPO */
if (sr_policy->type == SR_POLICY_TYPE_DEFAULT)
update_lb (sr_policy);
else if (sr_policy->type == SR_POLICY_TYPE_SPRAY)
update_replicate (sr_policy);
}
else if (operation == 3) /* Modify the weight of an existing SR List */
{
/* Find the corresponding SL */
vec_foreach (sl_index_iterate, sr_policy->segments_lists)
if (*sl_index_iterate == sl_index)
break;
if (*sl_index_iterate != sl_index)
return -32;
/* Change the weight */
segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
segment_list->weight = weight;
/* Update LB */
if (sr_policy->type == SR_POLICY_TYPE_DEFAULT)
update_lb (sr_policy);
}
else /* Incorrect op. */
return -1;
return 0;
}
/**
* @brief CLI for 'sr policies' command family
*/
static clib_error_t *
sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
vlib_cli_command_t * cmd)
{
ip6_sr_main_t *sm = &sr_main;
int rv = -1;
char is_del = 0, is_add = 0, is_mod = 0;
char policy_set = 0;
ip6_address_t bsid, next_address;
u32 sr_policy_index = (u32) ~ 0, sl_index = (u32) ~ 0;
u32 weight = (u32) ~ 0, fib_table = (u32) ~ 0;
ip6_address_t *segments = 0, *this_seg;
u8 operation = 0;
char is_encap = 1;
u8 type = SR_POLICY_TYPE_DEFAULT;
u16 behavior = 0;
void *ls_plugin_mem = 0;
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
if (!is_add && !is_mod && !is_del && unformat (input, "add"))
is_add = 1;
else if (!is_add && !is_mod && !is_del && unformat (input, "del"))
is_del = 1;
else if (!is_add && !is_mod && !is_del && unformat (input, "mod"))
is_mod = 1;
else if (!policy_set
&& unformat (input, "bsid %U", unformat_ip6_address, &bsid))
policy_set = 1;
else if (!is_add && !policy_set
&& unformat (input, "index %d", &sr_policy_index))
policy_set = 1;
else if (unformat (input, "weight %d", &weight));
else
if (unformat (input, "next %U", unformat_ip6_address, &next_address))
{
vec_add2 (segments, this_seg, 1);
clib_memcpy_fast (this_seg->as_u8, next_address.as_u8,
sizeof (*this_seg));
}
else if (unformat (input, "add sl"))
operation = 1;
else if (unformat (input, "del sl index %d", &sl_index))
operation = 2;