2 * sr_mpls_policy.c: SR-MPLS policies
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * @brief SR MPLS policy creation and application
22 * Create an SR policy.
23 * An SR policy can be either of 'default' type or 'spray' type
24 * An SR policy has attached a list of SID lists.
25 * In case the SR policy is a default one it will load balance among them.
26 * An SR policy has associated a BindingSID.
27 * In case any packet arrives with MPLS_label == BindingSID then the SR policy
28 * associated to such bindingSID will be applied to such packet.
32 #include <vlib/vlib.h>
33 #include <vnet/vnet.h>
34 #include <vnet/srmpls/sr.h>
35 #include <vnet/fib/mpls_fib.h>
36 #include <vnet/dpo/dpo.h>
37 #include <vnet/dpo/replicate_dpo.h>
38 #include <vnet/dpo/mpls_label_dpo.h>
39 #include <vnet/dpo/lookup_dpo.h>
41 #include <vppinfra/error.h>
42 #include <vppinfra/elog.h>
44 mpls_sr_main_t sr_mpls_main;
46 /*************************** SR LB helper functions **************************/
48 * @brief Creates a Segment List and adds it to an SR policy
50 * Creates a Segment List and adds it to the SR policy. Notice that the SL are
51 * not necessarily unique. Hence there might be two Segment List within the
52 * same SR Policy with exactly the same segments and same weight.
54 * @param sr_policy is the SR policy where the SL will be added
55 * @param sl is a vector of IPv6 addresses composing the Segment List
56 * @param weight is the weight of the SegmentList (for load-balancing purposes)
57 * @param is_encap represents the mode (SRH insertion vs Encapsulation)
59 * @return pointer to the just created segment list
61 static inline mpls_sr_sl_t *
62 create_sl (mpls_sr_policy_t * sr_policy, mpls_label_t * sl, u32 weight)
64 mpls_sr_main_t *sm = &sr_mpls_main;
65 mpls_sr_sl_t *segment_list;
67 pool_get (sm->sid_lists, segment_list);
68 memset (segment_list, 0, sizeof (*segment_list));
70 vec_add1 (sr_policy->segments_lists, segment_list - sm->sid_lists);
72 /* Fill in segment list */
73 segment_list->weight =
74 (weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT);
75 segment_list->segments = vec_dup (sl);
77 fib_route_path_t path = {
78 .frp_proto = DPO_PROTO_MPLS,
79 .frp_sw_if_index = ~0,
81 .frp_weight = segment_list->weight,
82 .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
83 .frp_label_stack = NULL,
84 .frp_local_label = sl[0],
87 vec_add (path.frp_label_stack, sl + 1, vec_len (sl) - 1);
89 fib_route_path_t *paths = NULL;
90 vec_add1 (paths, path);
93 FOR_EACH_MPLS_EOS_BIT (eos)
98 .fp_proto = FIB_PROTOCOL_MPLS,
99 .fp_label = sr_policy->bsid,
101 .fp_payload_proto = DPO_PROTO_MPLS,
105 fib_table_entry_path_add2 (0,
108 (sr_policy->type == SR_POLICY_TYPE_DEFAULT ?
109 FIB_ENTRY_FLAG_NONE :
110 FIB_ENTRY_FLAG_MULTICAST), paths);
118 /******************************* SR rewrite API *******************************/
119 /* Three functions for handling sr policies:
120 * -> sr_mpls_policy_add
121 * -> sr_mpls_policy_del
122 * -> sr_mpls_policy_mod
123 * All of them are API. CLI function on sr_policy_command_fn */
126 * @brief Create a new SR policy
128 * @param bsid is the bindingSID of the SR Policy
129 * @param segments is a vector of MPLS labels composing the segment list
130 * @param behavior is the behavior of the SR policy. (default//spray)
131 * @param fib_table is the VRF where to install the FIB entry for the BSID
132 * @param weight is the weight of this specific SID list
134 * @return 0 if correct, else error
137 sr_mpls_policy_add (mpls_label_t bsid, mpls_label_t * segments,
138 u8 behavior, u32 weight)
140 mpls_sr_main_t *sm = &sr_mpls_main;
141 mpls_sr_policy_t *sr_policy = 0;
144 /* Search for existing keys (BSID) */
145 p = hash_get (sm->sr_policies_index_hash, bsid);
148 /* Add SR policy that already exists; complain */
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;
159 hash_set (sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies);
161 /* Create a segment list and add the index to the SR policy */
162 create_sl (sr_policy, segments, weight);
168 * @brief Delete a SR policy
170 * @param bsid is the bindingSID of the SR Policy
171 * @param index is the index of the SR policy
173 * @return 0 if correct, else error
176 sr_mpls_policy_del (mpls_label_t bsid, u32 index)
178 mpls_sr_main_t *sm = &sr_mpls_main;
179 mpls_sr_policy_t *sr_policy = 0;
180 mpls_sr_sl_t *segment_list;
187 p = hash_get (sm->sr_policies_index_hash, bsid);
189 sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
195 sr_policy = pool_elt_at_index (sm->sr_policies, index);
200 /* Clean SID Lists */
201 vec_foreach (sl_index, sr_policy->segments_lists)
203 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
205 fib_route_path_t path = {
206 .frp_proto = DPO_PROTO_MPLS,
207 .frp_sw_if_index = ~0,
209 .frp_weight = segment_list->weight,
210 .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
211 .frp_local_label = segment_list->segments[0],
214 fib_route_path_t *paths = NULL;
215 vec_add1 (paths, path);
217 /* remove each of the MPLS routes */
218 FOR_EACH_MPLS_EOS_BIT (eos)
223 .fp_proto = FIB_PROTOCOL_MPLS,
224 .fp_label = sr_policy->bsid,
226 .fp_payload_proto = DPO_PROTO_MPLS,
230 fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
233 vec_free (segment_list->segments);
234 pool_put_index (sm->sid_lists, *sl_index);
237 /* Remove SR policy entry */
238 hash_unset (sm->sr_policies_index_hash, sr_policy->bsid);
239 pool_put (sm->sr_policies, sr_policy);
245 * @brief Modify an existing SR policy
247 * The possible modifications are adding a new Segment List, modifying an
248 * existing Segment List (modify the weight only) and delete a given
249 * Segment List from the SR Policy.
251 * @param bsid is the bindingSID of the SR Policy
252 * @param index is the index of the SR policy
253 * @param fib_table is the VRF where to install the FIB entry for the BSID
254 * @param operation is the operation to perform (among the top ones)
255 * @param segments is a vector of IPv6 address composing the segment list
256 * @param sl_index is the index of the Segment List to modify/delete
257 * @param weight is the weight of the sid list. optional.
259 * @return 0 if correct, else error
262 sr_mpls_policy_mod (mpls_label_t bsid, u32 index, u8 operation,
263 mpls_label_t * segments, u32 sl_index, u32 weight)
265 mpls_sr_main_t *sm = &sr_mpls_main;
266 mpls_sr_policy_t *sr_policy = 0;
267 mpls_sr_sl_t *segment_list;
268 u32 *sl_index_iterate;
273 p = hash_get (sm->sr_policies_index_hash, bsid);
275 sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
281 sr_policy = pool_elt_at_index (sm->sr_policies, index);
286 if (operation == 1) /* Add SR List to an existing SR policy */
288 /* Create the new SL */
289 segment_list = create_sl (sr_policy, segments, weight);
292 else if (operation == 2) /* Delete SR List from an existing SR policy */
294 /* Check that currently there are more than one SID list */
295 if (vec_len (sr_policy->segments_lists) == 1)
298 /* Check that the SR list does exist and is assigned to the sr policy */
299 vec_foreach (sl_index_iterate, sr_policy->segments_lists)
300 if (*sl_index_iterate == sl_index)
303 if (*sl_index_iterate != sl_index)
306 /* Remove the lucky SR list that is being kicked out */
307 segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
310 fib_route_path_t path = {
311 .frp_proto = DPO_PROTO_MPLS,
312 .frp_sw_if_index = ~0,
314 .frp_weight = segment_list->weight,
315 .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
316 .frp_local_label = segment_list->segments[0],
319 fib_route_path_t *paths = NULL;
320 vec_add1 (paths, path);
322 FOR_EACH_MPLS_EOS_BIT (eos)
327 .fp_proto = FIB_PROTOCOL_MPLS,
328 .fp_label = sr_policy->bsid,
330 .fp_payload_proto = DPO_PROTO_MPLS,
334 fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
338 vec_free (segment_list->segments);
339 pool_put_index (sm->sid_lists, sl_index);
340 vec_del1 (sr_policy->segments_lists,
341 sl_index_iterate - sr_policy->segments_lists);
343 else if (operation == 3) /* Modify the weight of an existing SR List */
345 /* Find the corresponding SL */
346 vec_foreach (sl_index_iterate, sr_policy->segments_lists)
347 if (*sl_index_iterate == sl_index)
350 if (*sl_index_iterate != sl_index)
353 /* Change the weight */
354 segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
355 segment_list->weight = weight;
364 * @brief CLI for 'sr mpls policies' command family
366 static clib_error_t *
367 sr_mpls_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
368 vlib_cli_command_t * cmd)
371 char is_del = 0, is_add = 0, is_mod = 0;
373 mpls_label_t bsid, next_label;
374 u32 sr_policy_index = (u32) ~ 0, sl_index = (u32) ~ 0;
375 u32 weight = (u32) ~ 0;
376 mpls_label_t *segments = 0;
380 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
382 if (!is_add && !is_mod && !is_del && unformat (input, "add"))
384 else if (!is_add && !is_mod && !is_del && unformat (input, "del"))
386 else if (!is_add && !is_mod && !is_del && unformat (input, "mod"))
389 && unformat (input, "bsid %U", unformat_mpls_unicast_label,
392 else if (!is_add && !policy_set
393 && unformat (input, "index %d", &sr_policy_index))
395 else if (unformat (input, "weight %d", &weight));
398 (input, "next %U", unformat_mpls_unicast_label, &next_label))
400 vec_add (segments, &next_label, 1);
402 else if (unformat (input, "add sl"))
404 else if (unformat (input, "del sl index %d", &sl_index))
406 else if (unformat (input, "mod sl index %d", &sl_index))
408 else if (unformat (input, "spray"))
414 if (!is_add && !is_mod && !is_del)
415 return clib_error_return (0, "Incorrect CLI");
418 return clib_error_return (0, "No SR policy BSID or index specified");
422 if (vec_len (segments) == 0)
423 return clib_error_return (0, "No Segment List specified");
425 rv = sr_mpls_policy_add (bsid, segments,
426 (is_spray ? SR_POLICY_TYPE_SPRAY :
427 SR_POLICY_TYPE_DEFAULT), weight);
431 sr_mpls_policy_del ((sr_policy_index != (u32) ~ 0 ? (u32) ~ 0 : bsid),
436 return clib_error_return (0, "No SL modification specified");
437 if (operation != 1 && sl_index == (u32) ~ 0)
438 return clib_error_return (0, "No Segment List index specified");
439 if (operation == 1 && vec_len (segments) == 0)
440 return clib_error_return (0, "No Segment List specified");
441 if (operation == 3 && weight == (u32) ~ 0)
442 return clib_error_return (0, "No new weight for the SL specified");
444 sr_mpls_policy_mod ((sr_policy_index != (u32) ~ 0 ? (u32) ~ 0 : bsid),
445 sr_policy_index, operation, segments,
456 return clib_error_return (0,
457 "There is already a FIB entry for the BindingSID address.\n"
458 "The SR policy could not be created.");
460 return clib_error_return (0,
461 "The selected SR policy only contains ONE segment list. "
462 "Please remove the SR policy instead");
464 return clib_error_return (0,
465 "Could not delete the segment list. "
466 "It is not associated with that SR policy.");
468 return clib_error_return (0,
469 "Could not modify the segment list. "
470 "The given SL is not associated with such SR policy.");
472 return clib_error_return (0, "BUG: sr policy returns %d", rv);
478 VLIB_CLI_COMMAND (sr_mpls_policy_command, static) = {
479 .path = "sr mpls policy",
480 .short_help = "sr mpls policy [add||del||mod] bsid 2999 "
481 "next 10 next 20 next 30 (weight 1) (spray)",
482 .long_help = "TBD.\n",
483 .function = sr_mpls_policy_command_fn,
488 * @brief CLI to display onscreen all the SR MPLS policies
490 static clib_error_t *
491 show_sr_mpls_policies_command_fn (vlib_main_t * vm, unformat_input_t * input,
492 vlib_cli_command_t * cmd)
494 mpls_sr_main_t *sm = &sr_mpls_main;
495 mpls_sr_sl_t *segment_list = 0;
496 mpls_sr_policy_t *sr_policy = 0;
497 mpls_sr_policy_t **vec_policies = 0;
503 vlib_cli_output (vm, "SR MPLS policies:");
506 pool_foreach (sr_policy, sm->sr_policies, {vec_add1 (vec_policies, sr_policy); } );
509 vec_foreach_index (i, vec_policies)
511 sr_policy = vec_policies[i];
512 vlib_cli_output (vm, "[%u].-\tBSID: %U",
513 (u32) (sr_policy - sm->sr_policies),
514 format_mpls_unicast_label, sr_policy->bsid);
515 vlib_cli_output (vm, "\tType: %s",
517 SR_POLICY_TYPE_DEFAULT ? "Default" : "Spray"));
518 vlib_cli_output (vm, "\tSegment Lists:");
519 vec_foreach (sl_index, sr_policy->segments_lists)
522 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
523 s = format (s, "\t[%u].- ", *sl_index);
524 s = format (s, "< ");
525 vec_foreach (label, segment_list->segments)
527 s = format (s, "%U, ", format_mpls_unicast_label, *label);
529 s = format (s, "\b\b > ");
530 vlib_cli_output (vm, " %s", s);
532 vlib_cli_output (vm, "-----------");
534 vec_free (vec_policies);
539 VLIB_CLI_COMMAND (show_sr_mpls_policies_command, static) = {
540 .path = "show sr mpls policies",
541 .short_help = "show sr mpls policies",
542 .function = show_sr_mpls_policies_command_fn,
546 /********************* SR MPLS Policy initialization ***********************/
548 * @brief SR MPLS Policy initialization
551 sr_mpls_policy_rewrite_init (vlib_main_t * vm)
553 mpls_sr_main_t *sm = &sr_mpls_main;
555 /* Init memory for sr policy keys (bsid <-> ip6_address_t) */
556 sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
561 VLIB_INIT_FUNCTION (sr_mpls_policy_rewrite_init);
564 * fd.io coding-style-patch-verification: ON
567 * eval: (c-set-style "gnu")