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