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