8fc7e730e471de7f8a9bba623d5ec9bff9ceb230
[vpp.git] / src / vnet / srmpls / sr_mpls_policy.c
1 /*
2  * sr_mpls_policy.c: SR-MPLS policies
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates. Licensed under the Apache
5  * License, Version 2.0 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16
17 /**
18  * @file
19  * @brief SR MPLS policy creation and application
20  *
21  * Create an SR policy.
22  * An SR policy can be either of 'default' type or 'spray' type
23  * An SR policy has attached a list of SID lists.
24  * In case the SR policy is a default one it will load balance among them.
25  * An SR policy has associated a BindingSID.
26  * In case any packet arrives with MPLS_label == BindingSID then the SR policy
27  * associated to such bindingSID will be applied to such packet.
28  * Also, a BSID can be associated with a (Next-Hop, Color)
29  *
30  */
31
32 #include <vlib/vlib.h>
33 #include <vnet/vnet.h>
34 #include <vnet/srmpls/sr_mpls.h>
35 #include <vnet/fib/mpls_fib.h>
36 #include <vnet/dpo/dpo.h>
37 #include <vnet/ip/ip.h>
38
39 #include <vppinfra/error.h>
40 #include <vppinfra/elog.h>
41
42 mpls_sr_main_t sr_mpls_main;
43
44 /***************************  SR LB helper functions **************************/
45 /**
46  * @brief Creates a Segment List and adds it to an SR policy
47  *
48  * Creates a Segment List and adds it to the SR policy. Notice that the SL are
49  * not necessarily unique. Hence there might be two Segment List within the
50  * same SR Policy with exactly the same segments and same weight.
51  *
52  * @param sr_policy is the SR policy where the SL will be added
53  * @param sl is a vector of IPv6 addresses composing the Segment List
54  * @param weight is the weight of the SegmentList (for load-balancing purposes)
55  * @param is_encap represents the mode (SRH insertion vs Encapsulation)
56  *
57  * @return pointer to the just created segment list
58  */
59 static inline mpls_sr_sl_t *
60 create_sl (mpls_sr_policy_t * sr_policy, mpls_label_t * sl, u32 weight)
61 {
62   mpls_sr_main_t *sm = &sr_mpls_main;
63   mpls_sr_sl_t *segment_list;
64   u32 ii;
65
66   pool_get (sm->sid_lists, segment_list);
67   memset (segment_list, 0, sizeof (*segment_list));
68
69   vec_add1 (sr_policy->segments_lists, segment_list - sm->sid_lists);
70
71   /* Fill in segment list */
72   segment_list->weight =
73     (weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT);
74   segment_list->segments = vec_dup (sl);
75
76   mpls_eos_bit_t eos;
77   FOR_EACH_MPLS_EOS_BIT (eos)
78   {
79     fib_route_path_t path = {
80       .frp_proto = DPO_PROTO_MPLS,
81       .frp_sw_if_index = ~0,
82       .frp_fib_index = 0,
83       .frp_weight = segment_list->weight,
84       .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
85       .frp_label_stack = NULL,
86       .frp_local_label = sl[0],
87     };
88
89     if (vec_len (sl) > 1)
90       {
91         vec_validate (path.frp_label_stack, vec_len (sl) - 2);
92         for (ii = 1; ii < vec_len (sl); ii++)
93           {
94             path.frp_label_stack[ii - 1].fml_value = sl[ii];
95           }
96       }
97     else
98       {
99         /*
100          * add an impliciet NULL label to allow non-eos recursion
101          */
102         fib_mpls_label_t lbl = {
103           .fml_value = MPLS_IETF_IMPLICIT_NULL_LABEL,
104         };
105         vec_add1 (path.frp_label_stack, lbl);
106       }
107
108     fib_route_path_t *paths = NULL;
109     vec_add1 (paths, path);
110
111     /* *INDENT-OFF* */
112     fib_prefix_t pfx = {
113         .fp_len = 21,
114         .fp_proto = FIB_PROTOCOL_MPLS,
115         .fp_label = sr_policy->bsid,
116         .fp_eos = eos,
117         .fp_payload_proto = DPO_PROTO_MPLS,
118     };
119     /* *INDENT-ON* */
120
121     fib_table_entry_path_add2 (0,
122                                &pfx,
123                                FIB_SOURCE_SR,
124                                (sr_policy->type == SR_POLICY_TYPE_DEFAULT ?
125                                 FIB_ENTRY_FLAG_NONE :
126                                 FIB_ENTRY_FLAG_MULTICAST), paths);
127     vec_free (paths);
128   }
129
130   return segment_list;
131 }
132
133 /******************************* SR rewrite API *******************************/
134 /*
135  * Three functions for handling sr policies: -> sr_mpls_policy_add ->
136  * sr_mpls_policy_del -> sr_mpls_policy_mod All of them are API. CLI function
137  * on sr_policy_command_fn
138  */
139
140 /**
141  * @brief Create a new SR policy
142  *
143  * @param bsid is the bindingSID of the SR Policy
144  * @param segments is a vector of MPLS labels composing the segment list
145  * @param behavior is the behavior of the SR policy. (default//spray)
146  * @param fib_table is the VRF where to install the FIB entry for the BSID
147  * @param weight is the weight of this specific SID list
148  *
149  * @return 0 if correct, else error
150  */
151 int
152 sr_mpls_policy_add (mpls_label_t bsid, mpls_label_t * segments,
153                     u8 behavior, u32 weight)
154 {
155   mpls_sr_main_t *sm = &sr_mpls_main;
156   mpls_sr_policy_t *sr_policy = 0;
157   uword *p;
158
159   if (!sm->sr_policies_index_hash)
160     sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
161
162   /* Search for existing keys (BSID) */
163   p = hash_get (sm->sr_policies_index_hash, bsid);
164   if (p)
165     {
166       /* Add SR policy that already exists; complain */
167       return -12;
168     }
169   /* Add an SR policy object */
170   pool_get (sm->sr_policies, sr_policy);
171   memset (sr_policy, 0, sizeof (*sr_policy));
172   sr_policy->bsid = bsid;
173   sr_policy->type = behavior;
174   sr_policy->endpoint_type = 0;
175   ip6_address_set_zero (&sr_policy->endpoint.ip6);
176   sr_policy->color = (u32) ~ 0;
177
178   /* Copy the key */
179   hash_set (sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies);
180
181   /* Create a segment list and add the index to the SR policy */
182   create_sl (sr_policy, segments, weight);
183
184   return 0;
185 }
186
187 /**
188  * @brief Delete a SR policy
189  *
190  * @param bsid is the bindingSID of the SR Policy
191  * @param index is the index of the SR policy
192  *
193  * @return 0 if correct, else error
194  */
195 int
196 sr_mpls_policy_del (mpls_label_t bsid)
197 {
198   mpls_sr_main_t *sm = &sr_mpls_main;
199   mpls_sr_policy_t *sr_policy = 0;
200   mpls_sr_sl_t *segment_list;
201   mpls_eos_bit_t eos;
202   u32 *sl_index;
203   uword *p;
204
205   if (!sm->sr_policies_index_hash)
206     sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
207
208   p = hash_get (sm->sr_policies_index_hash, bsid);
209   if (p)
210     sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
211   else
212     return -1;
213
214   /* Clean SID Lists */
215   vec_foreach (sl_index, sr_policy->segments_lists)
216   {
217     segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
218
219     fib_route_path_t path = {
220       .frp_proto = DPO_PROTO_MPLS,
221       .frp_sw_if_index = ~0,
222       .frp_fib_index = 0,
223       .frp_weight = segment_list->weight,
224       .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
225       .frp_local_label = segment_list->segments[0],
226     };
227
228     vec_add (path.frp_label_stack, segment_list + 1,
229              vec_len (segment_list) - 1);
230
231     fib_route_path_t *paths = NULL;
232     vec_add1 (paths, path);
233
234     /* remove each of the MPLS routes */
235     FOR_EACH_MPLS_EOS_BIT (eos)
236     {
237                         /* *INDENT-OFF* */
238                         fib_prefix_t    pfx = {
239                                 .fp_len = 21,
240                                 .fp_proto = FIB_PROTOCOL_MPLS,
241                                 .fp_label = sr_policy->bsid,
242                                 .fp_eos = eos,
243                                 .fp_payload_proto = DPO_PROTO_MPLS,
244                         };
245                         /* *INDENT-ON* */
246
247       fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
248     }
249     vec_free (paths);
250     vec_free (segment_list->segments);
251     pool_put_index (sm->sid_lists, *sl_index);
252   }
253
254   /* If there is still traces of TE, make sure locks are released */
255   if (sr_policy->endpoint_type != 0 && sr_policy->color != (u32) ~ 0)
256     {
257       sr_mpls_policy_assign_endpoint_color (bsid, NULL, 0, (u32) ~ 0);
258     }
259
260   /* Remove SR policy entry */
261   hash_unset (sm->sr_policies_index_hash, sr_policy->bsid);
262   pool_put (sm->sr_policies, sr_policy);
263
264   return 0;
265 }
266
267 /**
268  * @brief Modify an existing SR policy
269  *
270  * The possible modifications are adding a new Segment List, modifying an
271  * existing Segment List (modify the weight only) and delete a given
272  * Segment List from the SR Policy.
273  *
274  * @param bsid is the bindingSID of the SR Policy
275  * @param fib_table is the VRF where to install the FIB entry for the BSID
276  * @param operation is the operation to perform (among the top ones)
277  * @param segments is a vector of IPv6 address composing the segment list
278  * @param sl_index is the index of the Segment List to modify/delete
279  * @param weight is the weight of the sid list. optional.
280  *
281  * @return 0 ok, >0 index of SL, <0 error
282  */
283 int
284 sr_mpls_policy_mod (mpls_label_t bsid, u8 operation,
285                     mpls_label_t * segments, u32 sl_index, u32 weight)
286 {
287   mpls_sr_main_t *sm = &sr_mpls_main;
288   mpls_sr_policy_t *sr_policy = 0;
289   mpls_sr_sl_t *segment_list;
290   u32 *sl_index_iterate;
291   uword *p;
292
293   if (!sm->sr_policies_index_hash)
294     sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
295
296   p = hash_get (sm->sr_policies_index_hash, bsid);
297   if (p)
298     sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
299   else
300     return -1;
301
302   if (operation == 1)
303     {                           /* Add SR List to an existing SR policy */
304       /* Create the new SL */
305       segment_list = create_sl (sr_policy, segments, weight);
306       return segment_list - sm->sid_lists;
307     }
308   else if (operation == 2)
309     {                           /* Delete SR List from an existing SR
310                                  * policy */
311       /* Check that currently there are more than one SID list */
312       if (vec_len (sr_policy->segments_lists) == 1)
313         return -21;
314
315       /*
316        * Check that the SR list does exist and is assigned to the
317        * sr policy
318        */
319       vec_foreach (sl_index_iterate, sr_policy->segments_lists)
320         if (*sl_index_iterate == sl_index)
321         break;
322
323       if (*sl_index_iterate != sl_index)
324         return -22;
325
326       /* Remove the lucky SR list that is being kicked out */
327       segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
328
329       mpls_eos_bit_t eos;
330       fib_route_path_t path = {
331         .frp_proto = DPO_PROTO_MPLS,
332         .frp_sw_if_index = ~0,
333         .frp_fib_index = 0,
334         .frp_weight = segment_list->weight,
335         .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
336         .frp_local_label = segment_list->segments[0],
337       };
338
339       vec_add (path.frp_label_stack, segment_list + 1,
340                vec_len (segment_list) - 1);
341
342       fib_route_path_t *paths = NULL;
343       vec_add1 (paths, path);
344
345       FOR_EACH_MPLS_EOS_BIT (eos)
346       {
347                         /* *INDENT-OFF* */
348                         fib_prefix_t    pfx = {
349                                 .fp_len = 21,
350                                 .fp_proto = FIB_PROTOCOL_MPLS,
351                                 .fp_label = sr_policy->bsid,
352                                 .fp_eos = eos,
353                                 .fp_payload_proto = DPO_PROTO_MPLS,
354                         };
355                         /* *INDENT-ON* */
356
357         fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
358       }
359
360       vec_free (paths);
361       vec_free (segment_list->segments);
362       pool_put_index (sm->sid_lists, sl_index);
363       vec_del1 (sr_policy->segments_lists,
364                 sl_index_iterate - sr_policy->segments_lists);
365     }
366   else if (operation == 3)
367     {                           /* Modify the weight of an existing
368                                  * SR List */
369       /* Find the corresponding SL */
370       vec_foreach (sl_index_iterate, sr_policy->segments_lists)
371         if (*sl_index_iterate == sl_index)
372         break;
373
374       if (*sl_index_iterate != sl_index)
375         return -32;
376
377       /* Change the weight */
378       segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
379
380       /* Update LB */
381       mpls_eos_bit_t eos;
382       fib_route_path_t path = {
383         .frp_proto = DPO_PROTO_MPLS,
384         .frp_sw_if_index = ~0,
385         .frp_fib_index = 0,
386         .frp_weight = segment_list->weight,
387         .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
388         .frp_local_label = segment_list->segments[0],
389       };
390
391       vec_add (path.frp_label_stack, segment_list + 1,
392                vec_len (segment_list) - 1);
393
394       fib_route_path_t *paths = NULL;
395       vec_add1 (paths, path);
396
397       FOR_EACH_MPLS_EOS_BIT (eos)
398       {
399                         /* *INDENT-OFF* */
400                         fib_prefix_t    pfx = {
401                                 .fp_len = 21,
402                                 .fp_proto = FIB_PROTOCOL_MPLS,
403                                 .fp_label = sr_policy->bsid,
404                                 .fp_eos = eos,
405                                 .fp_payload_proto = DPO_PROTO_MPLS,
406                         };
407                         /* *INDENT-ON* */
408
409         fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
410       }
411
412       segment_list->weight = weight;
413
414       path.frp_weight = segment_list->weight;
415
416       vec_free (paths);
417       paths = NULL;
418       vec_add1 (paths, path);
419
420       FOR_EACH_MPLS_EOS_BIT (eos)
421       {
422                         /* *INDENT-OFF* */
423                         fib_prefix_t    pfx = {
424                                 .fp_len = 21,
425                                 .fp_proto = FIB_PROTOCOL_MPLS,
426                                 .fp_label = sr_policy->bsid,
427                                 .fp_eos = eos,
428                                 .fp_payload_proto = DPO_PROTO_MPLS,
429                         };
430                         /* *INDENT-ON* */
431
432         fib_table_entry_path_add2 (0,
433                                    &pfx,
434                                    FIB_SOURCE_SR,
435                                    (sr_policy->type ==
436                                     SR_POLICY_TYPE_DEFAULT ?
437                                     FIB_ENTRY_FLAG_NONE :
438                                     FIB_ENTRY_FLAG_MULTICAST), paths);
439       }
440     }
441   return 0;
442 }
443
444 /**
445  * @brief CLI for 'sr mpls policies' command family
446  */
447 static clib_error_t *
448 sr_mpls_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
449                            vlib_cli_command_t * cmd)
450 {
451   int rv = -1;
452   char is_del = 0, is_add = 0, is_mod = 0;
453   char policy_set = 0;
454   mpls_label_t bsid, next_label;
455   u32 sl_index = (u32) ~ 0;
456   u32 weight = (u32) ~ 0;
457   mpls_label_t *segments = 0;
458   u8 operation = 0;
459   u8 is_spray = 0;
460
461   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
462     {
463       if (!is_add && !is_mod && !is_del && unformat (input, "add"))
464         is_add = 1;
465       else if (!is_add && !is_mod && !is_del && unformat (input, "del"))
466         is_del = 1;
467       else if (!is_add && !is_mod && !is_del && unformat (input, "mod"))
468         is_mod = 1;
469       else if (!policy_set
470                && unformat (input, "bsid %U", unformat_mpls_unicast_label,
471                             &bsid))
472         policy_set = 1;
473       else if (unformat (input, "weight %d", &weight));
474       else if (unformat
475                (input, "next %U", unformat_mpls_unicast_label, &next_label))
476         {
477           vec_add (segments, &next_label, 1);
478         }
479       else if (unformat (input, "add sl"))
480         operation = 1;
481       else if (unformat (input, "del sl index %d", &sl_index))
482         operation = 2;
483       else if (unformat (input, "mod sl index %d", &sl_index))
484         operation = 3;
485       else if (unformat (input, "spray"))
486         is_spray = 1;
487       else
488         break;
489     }
490
491   if (!is_add && !is_mod && !is_del)
492     return clib_error_return (0, "Incorrect CLI");
493
494   if (!policy_set)
495     return clib_error_return (0, "No SR policy BSID or index specified");
496
497   if (is_add)
498     {
499       if (vec_len (segments) == 0)
500         return clib_error_return (0, "No Segment List specified");
501
502       rv = sr_mpls_policy_add (bsid, segments,
503                                (is_spray ? SR_POLICY_TYPE_SPRAY :
504                                 SR_POLICY_TYPE_DEFAULT), weight);
505       vec_free (segments);
506     }
507   else if (is_del)
508     rv = sr_mpls_policy_del (bsid);
509   else if (is_mod)
510     {
511       if (!operation)
512         return clib_error_return (0, "No SL modification specified");
513       if (operation != 1 && sl_index == (u32) ~ 0)
514         return clib_error_return (0, "No Segment List index specified");
515       if (operation == 1 && vec_len (segments) == 0)
516         return clib_error_return (0, "No Segment List specified");
517       if (operation == 3 && weight == (u32) ~ 0)
518         return clib_error_return (0, "No new weight for the SL specified");
519       rv = sr_mpls_policy_mod (bsid, operation, segments, sl_index, weight);
520       vec_free (segments);
521     }
522   switch (rv)
523     {
524     case 0:
525       break;
526     case 1:
527       return 0;
528     case -12:
529       return clib_error_return (0,
530                                 "There is already a FIB entry for the BindingSID address.\n"
531                                 "The SR policy could not be created.");
532     case -21:
533       return clib_error_return (0,
534                                 "The selected SR policy only contains ONE segment list. "
535                                 "Please remove the SR policy instead");
536     case -22:
537       return clib_error_return (0,
538                                 "Could not delete the segment list. "
539                                 "It is not associated with that SR policy.");
540     case -23:
541       return clib_error_return (0,
542                                 "Could not delete the segment list. "
543                                 "It is not associated with that SR policy.");
544     case -32:
545       return clib_error_return (0,
546                                 "Could not modify the segment list. "
547                                 "The given SL is not associated with such SR policy.");
548     default:
549       return clib_error_return (0, "BUG: sr policy returns %d", rv);
550     }
551   return 0;
552 }
553
554 /* *INDENT-OFF* */
555 VLIB_CLI_COMMAND(sr_mpls_policy_command, static)=
556 {
557         .path = "sr mpls policy",
558                 .short_help = "sr mpls policy [add||del||mod] bsid 2999 "
559                 "next 10 next 20 next 30 (weight 1) (spray)",
560                 .long_help = "TBD.\n",
561                 .function = sr_mpls_policy_command_fn,
562 };
563 /* *INDENT-ON* */
564
565 /**
566  * @brief CLI to display onscreen all the SR MPLS policies
567  */
568 static clib_error_t *
569 show_sr_mpls_policies_command_fn (vlib_main_t * vm, unformat_input_t * input,
570                                   vlib_cli_command_t * cmd)
571 {
572   mpls_sr_main_t *sm = &sr_mpls_main;
573   mpls_sr_sl_t *segment_list = 0;
574   mpls_sr_policy_t *sr_policy = 0;
575   mpls_sr_policy_t **vec_policies = 0;
576   mpls_label_t *label;
577   u32 *sl_index;
578   u8 *s;
579   int i = 0;
580
581   vlib_cli_output (vm, "SR MPLS policies:");
582
583         /* *INDENT-OFF* */
584         pool_foreach(sr_policy, sm->sr_policies, {
585                 vec_add1(vec_policies, sr_policy);
586         });
587         /* *INDENT-ON* */
588
589   vec_foreach_index (i, vec_policies)
590   {
591     sr_policy = vec_policies[i];
592     vlib_cli_output (vm, "[%u].-\tBSID: %U",
593                      (u32) (sr_policy - sm->sr_policies),
594                      format_mpls_unicast_label, sr_policy->bsid);
595     switch (sr_policy->endpoint_type)
596       {
597       case SR_STEER_IPV6:
598         vlib_cli_output (vm, "\tEndpoint: %U", format_ip6_address,
599                          &sr_policy->endpoint.ip6);
600         vlib_cli_output (vm, "\tColor: %u", sr_policy->color);
601         break;
602       case SR_STEER_IPV4:
603         vlib_cli_output (vm, "\tEndpoint: %U", format_ip4_address,
604                          &sr_policy->endpoint.ip4);
605         vlib_cli_output (vm, "\tColor: %u", sr_policy->color);
606         break;
607       default:
608         vlib_cli_output (vm, "\tTE disabled");
609       }
610     vlib_cli_output (vm, "\tType: %s",
611                      (sr_policy->type ==
612                       SR_POLICY_TYPE_DEFAULT ? "Default" : "Spray"));
613     vlib_cli_output (vm, "\tSegment Lists:");
614     vec_foreach (sl_index, sr_policy->segments_lists)
615     {
616       s = NULL;
617       segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
618       s = format (s, "\t[%u].- ", *sl_index);
619       s = format (s, "< ");
620       vec_foreach (label, segment_list->segments)
621       {
622         s = format (s, "%U, ", format_mpls_unicast_label, *label);
623       }
624       s = format (s, "\b\b > ");
625       vlib_cli_output (vm, "  %s", s);
626     }
627     vlib_cli_output (vm, "-----------");
628   }
629   vec_free (vec_policies);
630   return 0;
631 }
632
633 /* *INDENT-OFF* */
634 VLIB_CLI_COMMAND(show_sr_mpls_policies_command, static)=
635 {
636         .path = "show sr mpls policies",
637                 .short_help = "show sr mpls policies",
638                 .function = show_sr_mpls_policies_command_fn,
639 };
640 /* *INDENT-ON* */
641
642 /**
643  * @brief Update the Endpoint,Color tuple of an SR policy
644  *
645  * @param bsid is the bindingSID of the SR Policy
646  * @param endpoint represents the IP46 of the endpoint
647  * @param color represents the color (u32)
648  *
649  * To reset to NULL use ~0 as parameters.
650  *
651  * @return 0 if correct, else error
652  */
653 int
654 sr_mpls_policy_assign_endpoint_color (mpls_label_t bsid,
655                                       ip46_address_t * endpoint,
656                                       u8 endpoint_type, u32 color)
657 {
658   mpls_sr_main_t *sm = &sr_mpls_main;
659   mpls_sr_policy_t *sr_policy = 0;
660   uword *endpoint_table, *p, *old_value;
661
662   ip46_address_t any;
663   any.as_u64[0] = any.as_u64[1] = (u64) ~ 0;
664
665   if (!sm->sr_policies_index_hash)
666     sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
667
668   p = hash_get (sm->sr_policies_index_hash, bsid);
669   if (p)
670     sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
671   else
672     return -1;
673
674   /* If previous Endpoint, color existed, remove (NH,C) and (ANY,C) */
675   if (sr_policy->endpoint_type)
676     {
677       endpoint_table =
678         mhash_get (&sm->sr_policies_c2e2eclabel_hash, &sr_policy->color);
679       if (!endpoint_table)
680         return -2;
681       old_value =
682         mhash_get ((mhash_t *) endpoint_table, &sr_policy->endpoint);
683
684       /* CID 180995 This should never be NULL unless the two hash tables
685        * get out of sync */
686       ASSERT (old_value != NULL);
687
688       fib_prefix_t pfx = { 0 };
689       pfx.fp_proto = FIB_PROTOCOL_MPLS;
690       pfx.fp_len = 21;
691       pfx.fp_label = (u32) * old_value;
692
693       mpls_eos_bit_t eos;
694       FOR_EACH_MPLS_EOS_BIT (eos)
695       {
696         pfx.fp_eos = eos;
697         fib_table_entry_path_remove (sm->fib_table_EC,
698                                      &pfx,
699                                      FIB_SOURCE_SR,
700                                      DPO_PROTO_MPLS,
701                                      NULL,
702                                      ~0, 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
703       }
704
705       old_value = mhash_get ((mhash_t *) endpoint_table, &any);
706       pfx.fp_label = (u32) * old_value;
707
708       FOR_EACH_MPLS_EOS_BIT (eos)
709       {
710         pfx.fp_eos = eos;
711         fib_table_entry_path_remove (sm->fib_table_EC,
712                                      &pfx,
713                                      FIB_SOURCE_SR,
714                                      DPO_PROTO_MPLS,
715                                      NULL,
716                                      ~0, 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
717       }
718
719       /* Release the lock on (NH, Color) and (ANY, Color) */
720       internal_label_unlock (sr_policy->endpoint, sr_policy->color);
721       internal_label_unlock (any, sr_policy->color);
722
723       /* Reset the values on the SR policy */
724       sr_policy->endpoint_type = 0;
725       sr_policy->endpoint.as_u64[0] = sr_policy->endpoint.as_u64[1] =
726         (u64) ~ 0;
727       sr_policy->color = (u32) ~ 0;
728     }
729
730   if (endpoint_type)
731     {
732       sr_policy->endpoint_type = endpoint_type;
733       sr_policy->endpoint.as_u64[0] = endpoint->as_u64[0];
734       sr_policy->endpoint.as_u64[1] = endpoint->as_u64[1];
735       sr_policy->color = color;
736
737       u32 label = find_or_create_internal_label (*endpoint, color);
738       internal_label_lock (*endpoint, sr_policy->color);
739
740       /* If FIB doesnt exist, create them */
741       if (sm->fib_table_EC == (u32) ~ 0)
742         {
743           sm->fib_table_EC = fib_table_create_and_lock (FIB_PROTOCOL_MPLS,
744                                                         FIB_SOURCE_SR,
745                                                         "SR-MPLS Traffic Engineering (NextHop,Color)");
746
747           fib_table_flush (sm->fib_table_EC, FIB_PROTOCOL_MPLS,
748                            FIB_SOURCE_SPECIAL);
749         }
750
751       fib_prefix_t pfx = { 0 };
752       pfx.fp_proto = FIB_PROTOCOL_MPLS;
753       pfx.fp_len = 21;
754
755       fib_route_path_t path = {
756         .frp_proto = DPO_PROTO_MPLS,
757         .frp_sw_if_index = ~0,
758         .frp_fib_index = 0,
759         .frp_weight = 1,
760         .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
761         .frp_label_stack = 0
762       };
763       path.frp_local_label = sr_policy->bsid;
764
765       //Add the entry to ANY,Color
766       u32 any_label = find_or_create_internal_label (any, color);
767       internal_label_lock (any, sr_policy->color);
768
769       pfx.fp_eos = MPLS_EOS;
770       path.frp_eos = MPLS_EOS;
771
772       fib_route_path_t *paths = NULL;
773       vec_add1 (paths, path);
774
775       pfx.fp_label = label;
776       fib_table_entry_update (sm->fib_table_EC,
777                               &pfx,
778                               FIB_SOURCE_SR,
779                               FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
780
781       pfx.fp_label = any_label;
782       fib_table_entry_update (sm->fib_table_EC,
783                               &pfx,
784                               FIB_SOURCE_SR,
785                               FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
786
787       fib_mpls_label_t fml = {
788         .fml_value = MPLS_IETF_IMPLICIT_NULL_LABEL,
789       };
790
791       vec_add1 (path.frp_label_stack, fml);
792       pfx.fp_eos = MPLS_NON_EOS;
793       path.frp_eos = MPLS_NON_EOS;
794
795       paths = NULL;
796       vec_add1 (paths, path);
797
798       pfx.fp_label = label;
799       fib_table_entry_update (sm->fib_table_EC,
800                               &pfx,
801                               FIB_SOURCE_SR,
802                               FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
803
804       pfx.fp_label = any_label;
805       fib_table_entry_update (sm->fib_table_EC,
806                               &pfx,
807                               FIB_SOURCE_SR,
808                               FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
809     }
810   return 0;
811 }
812
813 /**
814  * @brief CLI to modify the Endpoint,Color of an SR policy
815  */
816 static clib_error_t *
817 cli_sr_mpls_policy_ec_command_fn (vlib_main_t * vm, unformat_input_t * input,
818                                   vlib_cli_command_t * cmd)
819 {
820   ip46_address_t endpoint;
821   u32 color = (u32) ~ 0;
822   mpls_label_t bsid;
823   u8 endpoint_type = 0;
824   char clear = 0, color_set = 0, bsid_set = 0;
825
826   memset (&endpoint, 0, sizeof (ip46_address_t));
827
828   int rv;
829   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
830     {
831       if (!endpoint_type
832           && unformat (input, "endpoint %U", unformat_ip6_address,
833                        &endpoint.ip6))
834         endpoint_type = SR_STEER_IPV6;
835       else if (!endpoint_type
836                && unformat (input, "endpoint %U", unformat_ip4_address,
837                             &endpoint.ip4))
838         endpoint_type = SR_STEER_IPV4;
839       else if (!color_set && unformat (input, "color %u", &color))
840         color_set = 1;
841       else if (!bsid_set
842                && unformat (input, "bsid %U", unformat_mpls_unicast_label,
843                             &bsid))
844         bsid_set = 1;
845       else if (!clear && unformat (input, "clear"))
846         clear = 1;
847       else
848         break;
849     }
850
851   if (!bsid_set)
852     return clib_error_return (0, "No BSID specified");
853   if (!endpoint_type && !clear)
854     return clib_error_return (0, "No Endpoint specified");
855   if (!color_set && !clear)
856     return clib_error_return (0, "No Color set");
857
858   /* In case its a cleanup */
859   if (clear)
860     {
861       ip6_address_set_zero (&endpoint.ip6);
862       color = (u32) ~ 0;
863     }
864   rv =
865     sr_mpls_policy_assign_endpoint_color (bsid, &endpoint, endpoint_type,
866                                           color);
867
868   if (rv)
869     clib_error_return (0, "Error on Endpoint,Color");
870
871   return 0;
872 }
873
874 /* *INDENT-OFF* */
875 VLIB_CLI_COMMAND(cli_sr_mpls_policy_ec_command, static)=
876 {
877         .path = "sr mpls policy te",
878                 .short_help = "sr mpls policy te bsid xxxxx endpoint x.x.x.x color 12341234",
879                 .function = cli_sr_mpls_policy_ec_command_fn,
880 };
881 /* *INDENT-ON* */
882
883 /********************* SR MPLS Policy initialization ***********************/
884 /**
885  * @brief SR MPLS Policy  initialization
886  */
887 clib_error_t *
888 sr_mpls_policy_rewrite_init (vlib_main_t * vm)
889 {
890   mpls_sr_main_t *sm = &sr_mpls_main;
891
892   /* Init memory for sr policy keys (bsid <-> ip6_address_t) */
893   sm->sr_policies_index_hash = NULL;
894   sm->sr_policies_c2e2eclabel_hash.hash = NULL;
895   return 0;
896 }
897
898 VLIB_INIT_FUNCTION (sr_mpls_policy_rewrite_init);
899
900 /*
901  * fd.io coding-style-patch-verification: ON
902  *
903  * Local Variables: eval: (c-set-style "gnu") End:
904  */