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