blob: 6b5b841c2ac9d2ef6ebf64a34dfbdacdd32fe715 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/mpls/mpls.h>
17#include <vnet/dpo/mpls_label_dpo.h>
18#include <vnet/dpo/load_balance.h>
19#include <vnet/dpo/drop_dpo.h>
20
Neale Rannsad422ed2016-11-02 14:20:04 +000021#include <vnet/fib/fib_path_ext.h>
22#include <vnet/fib/fib_entry_src.h>
23#include <vnet/fib/fib_path.h>
24#include <vnet/fib/fib_path_list.h>
25#include <vnet/fib/fib_internal.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026
Neale Ranns81424992017-05-18 03:03:22 -070027const char *fib_path_ext_adj_flags_names[] = FIB_PATH_EXT_ADJ_ATTR_NAMES;
Neale Ranns31ed7442018-02-23 05:29:09 -080028const char *fib_path_ext_mpls_flags_names[] = FIB_PATH_EXT_MPLS_ATTR_NAMES;
Neale Ranns81424992017-05-18 03:03:22 -070029
Neale Ranns0bfe5d82016-08-25 15:29:12 +010030u8 *
31format_fib_path_ext (u8 * s, va_list * args)
32{
33 fib_path_ext_t *path_ext;
Neale Rannsad422ed2016-11-02 14:20:04 +000034 u32 ii;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010035
36 path_ext = va_arg (*args, fib_path_ext_t *);
37
Neale Ranns81424992017-05-18 03:03:22 -070038 s = format(s, "path:%d ", path_ext->fpe_path_index);
39
40 switch (path_ext->fpe_type)
Neale Rannsad422ed2016-11-02 14:20:04 +000041 {
Neale Ranns31ed7442018-02-23 05:29:09 -080042 case FIB_PATH_EXT_MPLS: {
43 fib_path_ext_mpls_attr_t attr;
44
45 if (path_ext->fpe_mpls_flags)
46 {
47 s = format(s, "mpls-flags:[");
48
49 FOR_EACH_PATH_EXT_MPLS_ATTR(attr)
50 {
51 if ((1<<attr) & path_ext->fpe_mpls_flags) {
52 s = format(s, "%s", fib_path_ext_mpls_flags_names[attr]);
53 }
54 }
55 s = format(s, "]");
56 }
57 s = format(s, " labels:[",
Neale Ranns81424992017-05-18 03:03:22 -070058 path_ext->fpe_path_index);
59 for (ii = 0; ii < vec_len(path_ext->fpe_path.frp_label_stack); ii++)
60 {
Neale Ranns31ed7442018-02-23 05:29:09 -080061 s = format(s, "[%U]",
62 format_fib_mpls_label,
63 &path_ext->fpe_path.frp_label_stack[ii]);
Neale Ranns81424992017-05-18 03:03:22 -070064 }
Neale Ranns31ed7442018-02-23 05:29:09 -080065 s = format(s, "]");
Neale Ranns81424992017-05-18 03:03:22 -070066 break;
Neale Ranns31ed7442018-02-23 05:29:09 -080067 }
Neale Ranns81424992017-05-18 03:03:22 -070068 case FIB_PATH_EXT_ADJ: {
69 fib_path_ext_adj_attr_t attr;
70
Neale Ranns81424992017-05-18 03:03:22 -070071 if (path_ext->fpe_adj_flags)
72 {
Neale Ranns31ed7442018-02-23 05:29:09 -080073 s = format(s, "adj-flags:[");
Neale Ranns81424992017-05-18 03:03:22 -070074 FOR_EACH_PATH_EXT_ADJ_ATTR(attr)
75 {
Neale Ranns31ed7442018-02-23 05:29:09 -080076 if ((1<<attr) & path_ext->fpe_adj_flags)
77 {
78 s = format(s, "%s", fib_path_ext_adj_flags_names[attr]);
79 }
Neale Ranns81424992017-05-18 03:03:22 -070080 }
Neale Ranns31ed7442018-02-23 05:29:09 -080081 s = format(s, "]");
Neale Ranns81424992017-05-18 03:03:22 -070082 }
83 break;
84 }
Neale Rannsad422ed2016-11-02 14:20:04 +000085 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086 return (s);
87}
88
89int
90fib_path_ext_cmp (fib_path_ext_t *path_ext,
91 const fib_route_path_t *rpath)
92{
93 return (fib_route_path_cmp(&path_ext->fpe_path, rpath));
94}
95
Neale Ranns81424992017-05-18 03:03:22 -070096static fib_path_list_walk_rc_t
Neale Ranns0bfe5d82016-08-25 15:29:12 +010097fib_path_ext_match (fib_node_index_t pl_index,
98 fib_node_index_t path_index,
99 void *ctx)
100{
101 fib_path_ext_t *path_ext = ctx;
102
103 if (!fib_path_cmp_w_route_path(path_index,
104 &path_ext->fpe_path))
105 {
106 path_ext->fpe_path_index = path_index;
Neale Ranns81424992017-05-18 03:03:22 -0700107 return (FIB_PATH_LIST_WALK_STOP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 }
Neale Ranns81424992017-05-18 03:03:22 -0700109 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110}
111
112void
113fib_path_ext_resolve (fib_path_ext_t *path_ext,
114 fib_node_index_t path_list_index)
115{
116 /*
117 * Find the path on the path list that this is an extension for
118 */
119 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
120 fib_path_list_walk(path_list_index,
121 fib_path_ext_match,
122 path_ext);
123}
124
Neale Rannsdcd6d622017-05-26 02:59:16 -0700125static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126fib_path_ext_init (fib_path_ext_t *path_ext,
127 fib_node_index_t path_list_index,
Neale Ranns81424992017-05-18 03:03:22 -0700128 fib_path_ext_type_t ext_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100129 const fib_route_path_t *rpath)
130{
131 path_ext->fpe_path = *rpath;
132 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
Neale Ranns81424992017-05-18 03:03:22 -0700133 path_ext->fpe_adj_flags = FIB_PATH_EXT_ADJ_FLAG_NONE;
134 path_ext->fpe_type = ext_type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100135
136 fib_path_ext_resolve(path_ext, path_list_index);
137}
138
Neale Rannsad422ed2016-11-02 14:20:04 +0000139/**
140 * @brief Return true if the label stack is implicit null
Neale Ranns31ed7442018-02-23 05:29:09 -0800141 * imp-null and pop equate to the same this as this level -
142 * the label is coming off.
Neale Rannsad422ed2016-11-02 14:20:04 +0000143 */
144static int
145fib_path_ext_is_imp_null (fib_path_ext_t *path_ext)
146{
147 return ((1 == vec_len(path_ext->fpe_label_stack)) &&
Neale Ranns31ed7442018-02-23 05:29:09 -0800148 ((MPLS_IETF_IMPLICIT_NULL_LABEL == path_ext->fpe_label_stack[0].fml_value) ||
149 (MPLS_LABEL_POP == path_ext->fpe_label_stack[0].fml_value)));
150}
151
152mpls_label_dpo_flags_t
153fib_path_ext_mpls_flags_to_mpls_label (fib_path_ext_mpls_flags_t fpe_flags)
154{
155 mpls_label_dpo_flags_t ml_flags = MPLS_LABEL_DPO_FLAG_NONE;
156
157 if (fpe_flags &FIB_PATH_EXT_MPLS_FLAG_NO_IP_TTL_DECR)
158 {
159 ml_flags |= MPLS_LABEL_DPO_FLAG_NO_IP_TTL_DECR;
160 }
161
162 return (ml_flags);
Neale Rannsad422ed2016-11-02 14:20:04 +0000163}
164
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165load_balance_path_t *
166fib_path_ext_stack (fib_path_ext_t *path_ext,
Neale Rannsad422ed2016-11-02 14:20:04 +0000167 fib_forward_chain_type_t child_fct,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800168 fib_forward_chain_type_t imp_null_fct,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169 load_balance_path_t *nhs)
170{
Neale Rannsad422ed2016-11-02 14:20:04 +0000171 fib_forward_chain_type_t parent_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100172 load_balance_path_t *nh;
173
174 if (!fib_path_is_resolved(path_ext->fpe_path_index))
175 return (nhs);
176
177 /*
178 * Since we are stacking this path-extension, it must have a valid out
179 * label. From the chain type request by the child, determine what
180 * chain type we will request from the parent.
181 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000182 switch (child_fct)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100183 {
184 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000185 {
186 /*
187 * The EOS chain is a tricky since, when the path has an imp NULL one cannot know
188 * the adjacency to link to without knowing what the packets payload protocol
189 * will be once the label is popped.
190 */
191 if (fib_path_ext_is_imp_null(path_ext))
192 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800193 parent_fct = imp_null_fct;
Neale Rannsad422ed2016-11-02 14:20:04 +0000194 }
195 else
196 {
197 /*
198 * we have a label to stack. packets will thus be labelled when
199 * they encounter the child, ergo, non-eos.
200 */
201 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
202 }
Neale Ranns5899fde2016-10-12 13:51:05 +0100203 break;
Neale Rannsad422ed2016-11-02 14:20:04 +0000204 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100205 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
206 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
Neale Rannsad422ed2016-11-02 14:20:04 +0000207 if (fib_path_ext_is_imp_null(path_ext))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100208 {
209 /*
210 * implicit-null label for the eos or IP chain, need to pick up
211 * the IP adj
212 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000213 parent_fct = child_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100214 }
215 else
216 {
217 /*
218 * we have a label to stack. packets will thus be labelled when
Neale Rannsad422ed2016-11-02 14:20:04 +0000219 * they encounter the child, ergo, non-eos.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000221 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100222 }
223 break;
224 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000225 parent_fct = child_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226 break;
Neale Rannsda78f952017-05-24 09:15:43 -0700227 case FIB_FORW_CHAIN_TYPE_ETHERNET:
228 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
229 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100230 default:
231 return (nhs);
232 break;
233 }
234
Neale Ranns948e00f2016-10-20 13:39:34 +0100235 dpo_id_t via_dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100236
237 /*
238 * The next object in the graph after the imposition of the label
239 * will be the DPO contributed by the path through which the packets
240 * are to be sent. We stack the MPLS Label DPO on this path DPO
241 */
242 fib_path_contribute_forwarding(path_ext->fpe_path_index,
Neale Rannsad422ed2016-11-02 14:20:04 +0000243 parent_fct,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244 &via_dpo);
245
246 if (dpo_is_drop(&via_dpo) ||
247 load_balance_is_drop(&via_dpo))
248 {
249 /*
250 * don't stack a path extension on a drop. doing so will create
251 * a LB bucket entry on drop, and we will lose a percentage of traffic.
252 */
253 }
254 else
255 {
256 vec_add2(nhs, nh, 1);
257 nh->path_weight = fib_path_get_weight(path_ext->fpe_path_index);
258 nh->path_index = path_ext->fpe_path_index;
259 dpo_copy(&nh->path_dpo, &via_dpo);
260
261 /*
262 * The label is stackable for this chain type
263 * construct the mpls header that will be imposed in the data-path
264 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000265 if (!fib_path_ext_is_imp_null(path_ext))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000267 /*
268 * we use the parent protocol for the label so that
269 * we pickup the correct MPLS imposition nodes to do
270 * ip[46] processing.
271 */
Neale Ranns31ed7442018-02-23 05:29:09 -0800272 dpo_id_t parent = DPO_INVALID;
Neale Rannsad422ed2016-11-02 14:20:04 +0000273 dpo_proto_t chain_proto;
274 mpls_eos_bit_t eos;
Neale Rannsad422ed2016-11-02 14:20:04 +0000275
276 eos = (child_fct == FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS ?
277 MPLS_NON_EOS :
278 MPLS_EOS);
279 chain_proto = fib_forw_chain_type_to_dpo_proto(child_fct);
280
Neale Ranns31ed7442018-02-23 05:29:09 -0800281 dpo_copy(&parent, &nh->path_dpo);
282 mpls_label_dpo_create(path_ext->fpe_label_stack,
283 eos,
284 chain_proto,
285 fib_path_ext_mpls_flags_to_mpls_label(
286 path_ext->fpe_mpls_flags),
287 &parent,
288 &nh->path_dpo);
Neale Rannsad422ed2016-11-02 14:20:04 +0000289
Neale Ranns31ed7442018-02-23 05:29:09 -0800290 dpo_reset(&parent);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100291 }
Neale Ranns62fe07c2017-10-31 12:28:22 -0700292 else if (child_fct == FIB_FORW_CHAIN_TYPE_MPLS_EOS)
293 {
294 /*
295 * MPLS EOS packets using an imp-null. Insert the disposition.
296 */
297 fib_path_stack_mpls_disp(nh->path_index,
298 fib_forw_chain_type_to_dpo_proto(parent_fct),
Neale Ranns31ed7442018-02-23 05:29:09 -0800299 path_ext->fpe_label_stack[0].fml_mode,
Neale Ranns62fe07c2017-10-31 12:28:22 -0700300 &nh->path_dpo);
301 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100302 }
303 dpo_reset(&via_dpo);
304
305 return (nhs);
306}
Neale Ranns81424992017-05-18 03:03:22 -0700307
308fib_path_ext_t *
309fib_path_ext_list_find (const fib_path_ext_list_t *list,
310 fib_path_ext_type_t ext_type,
311 const fib_route_path_t *rpath)
312{
313 fib_path_ext_t *path_ext;
314
315 vec_foreach(path_ext, list->fpel_exts)
316 {
317 if ((path_ext->fpe_type == ext_type) &&
318 !fib_path_ext_cmp(path_ext, rpath) )
319 {
320 return (path_ext);
321 }
322 }
323 return (NULL);
324}
325
326fib_path_ext_t *
327fib_path_ext_list_find_by_path_index (const fib_path_ext_list_t *list,
328 fib_node_index_t path_index)
329{
330 fib_path_ext_t *path_ext;
331
332 vec_foreach(path_ext, list->fpel_exts)
333 {
334 if (path_ext->fpe_path_index == path_index)
335 {
336 return (path_ext);
337 }
338 }
339 return (NULL);
340}
341
342
343fib_path_ext_t *
344fib_path_ext_list_push_back (fib_path_ext_list_t *list,
345 fib_node_index_t path_list_index,
346 fib_path_ext_type_t ext_type,
347 const fib_route_path_t *rpath)
348{
349 fib_path_ext_t *path_ext;
350
351 path_ext = fib_path_ext_list_find(list, ext_type, rpath);
352
353 if (NULL == path_ext)
354 {
355 vec_add2(list->fpel_exts, path_ext, 1);
356 fib_path_ext_init(path_ext, path_list_index, ext_type, rpath);
357 }
358
359 return (path_ext);
360}
361
362/*
363 * insert, sorted, a path extension to the entry's list.
364 * It's not strictly necessary to sort the path extensions, since each
365 * extension has the path index to which it resolves. However, by being
366 * sorted the load-balance produced has a deterministic order, not an order
367 * based on the sequence of extension additions. this is a considerable benefit.
368 */
369fib_path_ext_t *
370fib_path_ext_list_insert (fib_path_ext_list_t *list,
371 fib_node_index_t path_list_index,
372 fib_path_ext_type_t ext_type,
373 const fib_route_path_t *rpath)
374{
375 fib_path_ext_t new_path_ext, *path_ext;
376 int i = 0;
377
378 if (0 == fib_path_ext_list_length(list))
379 {
380 return (fib_path_ext_list_push_back(list, path_list_index,
381 ext_type, rpath));
382 }
383
384 fib_path_ext_init(&new_path_ext, path_list_index, ext_type, rpath);
385
386 vec_foreach(path_ext, list->fpel_exts)
387 {
Neale Rannsdcd6d622017-05-26 02:59:16 -0700388 int res = fib_path_ext_cmp(path_ext, rpath);
389
390 if (0 == res)
391 {
392 /*
393 * don't add duplicate extensions. modify instead
394 */
395 vec_free(path_ext->fpe_label_stack);
396 *path_ext = new_path_ext;
397 goto done;
398 }
399 else if (res < 0)
Neale Ranns81424992017-05-18 03:03:22 -0700400 {
401 i++;
402 }
403 else
404 {
405 break;
406 }
407 }
408 vec_insert_elts(list->fpel_exts, &new_path_ext, 1, i);
Neale Rannsdcd6d622017-05-26 02:59:16 -0700409done:
Neale Ranns81424992017-05-18 03:03:22 -0700410 return (&(list->fpel_exts[i]));
411}
412
413void
414fib_path_ext_list_resolve (fib_path_ext_list_t *list,
415 fib_node_index_t path_list_index)
416{
417 fib_path_ext_t *path_ext;
418
419 vec_foreach(path_ext, list->fpel_exts)
420 {
421 fib_path_ext_resolve(path_ext, path_list_index);
422 };
423}
424
425void
426fib_path_ext_list_remove (fib_path_ext_list_t *list,
427 fib_path_ext_type_t ext_type,
428 const fib_route_path_t *rpath)
429{
430 fib_path_ext_t *path_ext;
431
432 path_ext = fib_path_ext_list_find(list, ext_type, rpath);
433
434 if (NULL != path_ext)
435 {
436 /*
437 * delete the element moving the remaining elements down 1 position.
438 * this preserves the sorted order.
439 */
440 vec_free(path_ext->fpe_label_stack);
441 vec_delete(list->fpel_exts, 1, (path_ext - list->fpel_exts));
442 }
443}
444
445void
446fib_path_ext_list_flush (fib_path_ext_list_t *list)
447{
448 fib_path_ext_t *path_ext;
449
450 vec_foreach(path_ext, list->fpel_exts)
451 {
452 vec_free(path_ext->fpe_label_stack);
453 };
454 vec_free(list->fpel_exts);
455 list->fpel_exts = NULL;
456}
457
458u8*
459format_fib_path_ext_list (u8 * s, va_list * args)
460{
461 fib_path_ext_list_t *list;
462 fib_path_ext_t *path_ext;
463
464 list = va_arg (*args, fib_path_ext_list_t *);
465
466 if (fib_path_ext_list_length(list))
467 {
468 s = format(s, " Extensions:");
469 vec_foreach(path_ext, list->fpel_exts)
470 {
471 s = format(s, "\n %U", format_fib_path_ext, path_ext);
472 };
473 }
474
475 return (s);
476}
477
478int
479fib_path_ext_list_length (const fib_path_ext_list_t *list)
480{
481 return (vec_len(list->fpel_exts));
482}