Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * sr_mpls_policy.c: SR-MPLS policies |
| 3 | * |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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: |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 7 | * |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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. |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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. |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 28 | * Also, a BSID can be associated with a (Next-Hop, Color) |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <vlib/vlib.h> |
| 33 | #include <vnet/vnet.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 34 | #include <vnet/srmpls/sr_mpls.h> |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 35 | #include <vnet/fib/mpls_fib.h> |
| 36 | #include <vnet/dpo/dpo.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 37 | #include <vnet/ip/ip.h> |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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; |
Neale Ranns | 7c922dc | 2018-08-30 06:12:27 -0700 | [diff] [blame] | 64 | u32 ii; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 65 | |
| 66 | pool_get (sm->sid_lists, segment_list); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 67 | clib_memset (segment_list, 0, sizeof (*segment_list)); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 76 | mpls_eos_bit_t eos; |
| 77 | FOR_EACH_MPLS_EOS_BIT (eos) |
| 78 | { |
Pablo Camarillo | 618c94a | 2018-01-08 15:53:20 +0100 | [diff] [blame] | 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 | |
Neale Ranns | 7c922dc | 2018-08-30 06:12:27 -0700 | [diff] [blame] | 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 | } |
Pablo Camarillo | 618c94a | 2018-01-08 15:53:20 +0100 | [diff] [blame] | 107 | |
| 108 | fib_route_path_t *paths = NULL; |
| 109 | vec_add1 (paths, path); |
| 110 | |
Neale Ranns | 3b23e9d | 2018-08-29 03:05:17 -0700 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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); |
Pablo Camarillo | 618c94a | 2018-01-08 15:53:20 +0100 | [diff] [blame] | 125 | vec_free (paths); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 128 | return segment_list; |
| 129 | } |
| 130 | |
| 131 | /******************************* SR rewrite API *******************************/ |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | */ |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 157 | if (!sm->sr_policies_index_hash) |
| 158 | sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t)); |
| 159 | |
Neale Ranns | da8e180 | 2018-09-20 02:49:25 -0700 | [diff] [blame] | 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 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | } |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 171 | /* Add an SR policy object */ |
| 172 | pool_get (sm->sr_policies, sr_policy); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 173 | clib_memset (sr_policy, 0, sizeof (*sr_policy)); |
Neale Ranns | da8e180 | 2018-09-20 02:49:25 -0700 | [diff] [blame] | 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); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 181 | sr_policy->bsid = bsid; |
| 182 | sr_policy->type = behavior; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 183 | sr_policy->endpoint_type = 0; |
| 184 | ip6_address_set_zero (&sr_policy->endpoint.ip6); |
| 185 | sr_policy->color = (u32) ~ 0; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 205 | sr_mpls_policy_del (mpls_label_t bsid) |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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]); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 220 | else |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 221 | return -1; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 = { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 229 | .frp_proto = DPO_PROTO_MPLS, |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 237 | vec_add (path.frp_label_stack, segment_list + 1, |
| 238 | vec_len (segment_list) - 1); |
| 239 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | { |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Neale Ranns | da8e180 | 2018-09-20 02:49:25 -0700 | [diff] [blame] | 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 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | * |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 292 | * @return 0 ok, >0 index of SL, <0 error |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 293 | */ |
| 294 | int |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 295 | sr_mpls_policy_mod (mpls_label_t bsid, u8 operation, |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 304 | if (!sm->sr_policies_index_hash) |
| 305 | sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t)); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 306 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 */ |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 315 | /* Create the new SL */ |
| 316 | segment_list = create_sl (sr_policy, segments, weight); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 317 | return segment_list - sm->sid_lists; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 318 | } |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 319 | else if (operation == 2) |
| 320 | { /* Delete SR List from an existing SR |
| 321 | * policy */ |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 322 | /* Check that currently there are more than one SID list */ |
| 323 | if (vec_len (sr_policy->segments_lists) == 1) |
| 324 | return -21; |
| 325 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 326 | /* |
| 327 | * Check that the SR list does exist and is assigned to the |
| 328 | * sr policy |
| 329 | */ |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 = { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 342 | .frp_proto = DPO_PROTO_MPLS, |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 350 | vec_add (path.frp_label_stack, segment_list + 1, |
| 351 | vec_len (segment_list) - 1); |
| 352 | |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 353 | fib_route_path_t *paths = NULL; |
| 354 | vec_add1 (paths, path); |
| 355 | |
| 356 | FOR_EACH_MPLS_EOS_BIT (eos) |
| 357 | { |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | } |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 375 | else if (operation == 3) |
| 376 | { /* Modify the weight of an existing |
| 377 | * SR List */ |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 388 | |
| 389 | /* Update LB */ |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | { |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | { |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | } |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 460 | u32 sl_index = (u32) ~ 0; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 478 | else if (unformat (input, "weight %d", &weight)); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 479 | else if (unformat |
| 480 | (input, "next %U", unformat_mpls_unicast_label, &next_label)) |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 510 | vec_free (segments); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 511 | } |
| 512 | else if (is_del) |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 513 | rv = sr_mpls_policy_del (bsid); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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"); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 524 | rv = sr_mpls_policy_mod (bsid, operation, segments, sl_index, weight); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 525 | vec_free (segments); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 526 | } |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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."); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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."); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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."); |
Neale Ranns | da8e180 | 2018-09-20 02:49:25 -0700 | [diff] [blame] | 553 | case VNET_API_ERROR_NO_SUCH_TABLE: |
| 554 | return clib_error_return (0, "the Default MPLS table is not present"); |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 555 | default: |
| 556 | return clib_error_return (0, "BUG: sr policy returns %d", rv); |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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, |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 568 | }; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 588 | pool_foreach (sr_policy, sm->sr_policies) { |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 589 | vec_add1(vec_policies, sr_policy); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 590 | } |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | } |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | }; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 685 | /* CID 180995 This should never be NULL unless the two hash tables |
| 686 | * get out of sync */ |
Dave Barach | ffd1546 | 2020-02-18 10:12:23 -0500 | [diff] [blame] | 687 | ALWAYS_ASSERT (old_value != NULL); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 688 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 788 | fib_mpls_label_t fml = { |
| 789 | .fml_value = MPLS_IETF_IMPLICIT_NULL_LABEL, |
| 790 | }; |
| 791 | |
| 792 | vec_add1 (path.frp_label_stack, fml); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 827 | clib_memset (&endpoint, 0, sizeof (ip46_address_t)); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 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, |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 880 | }; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 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) */ |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 892 | sm->sr_policies_index_hash = NULL; |
| 893 | sm->sr_policies_c2e2eclabel_hash.hash = NULL; |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | VLIB_INIT_FUNCTION (sr_mpls_policy_rewrite_init); |
| 898 | |
| 899 | /* |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 900 | * fd.io coding-style-patch-verification: ON |
| 901 | * |
| 902 | * Local Variables: eval: (c-set-style "gnu") End: |
| 903 | */ |