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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
21 #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>
27 const char *fib_path_ext_adj_flags_names[] = FIB_PATH_EXT_ADJ_ATTR_NAMES;
30 format_fib_path_ext (u8 * s, va_list * args)
32 fib_path_ext_t *path_ext;
35 path_ext = va_arg (*args, fib_path_ext_t *);
37 s = format(s, "path:%d ", path_ext->fpe_path_index);
39 switch (path_ext->fpe_type)
41 case FIB_PATH_EXT_MPLS:
42 s = format(s, "labels:",
43 path_ext->fpe_path_index);
44 for (ii = 0; ii < vec_len(path_ext->fpe_path.frp_label_stack); ii++)
47 format_mpls_unicast_label,
48 path_ext->fpe_path.frp_label_stack[ii]);
51 case FIB_PATH_EXT_ADJ: {
52 fib_path_ext_adj_attr_t attr;
54 s = format(s, "adj-flags:");
55 if (path_ext->fpe_adj_flags)
57 FOR_EACH_PATH_EXT_ADJ_ATTR(attr)
59 s = format(s, "%s", fib_path_ext_adj_flags_names[attr]);
64 s = format(s, "None");
73 fib_path_ext_cmp (fib_path_ext_t *path_ext,
74 const fib_route_path_t *rpath)
76 return (fib_route_path_cmp(&path_ext->fpe_path, rpath));
79 static fib_path_list_walk_rc_t
80 fib_path_ext_match (fib_node_index_t pl_index,
81 fib_node_index_t path_index,
84 fib_path_ext_t *path_ext = ctx;
86 if (!fib_path_cmp_w_route_path(path_index,
89 path_ext->fpe_path_index = path_index;
90 return (FIB_PATH_LIST_WALK_STOP);
92 return (FIB_PATH_LIST_WALK_CONTINUE);
96 fib_path_ext_resolve (fib_path_ext_t *path_ext,
97 fib_node_index_t path_list_index)
100 * Find the path on the path list that this is an extension for
102 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
103 fib_path_list_walk(path_list_index,
109 fib_path_ext_init (fib_path_ext_t *path_ext,
110 fib_node_index_t path_list_index,
111 fib_path_ext_type_t ext_type,
112 const fib_route_path_t *rpath)
114 path_ext->fpe_path = *rpath;
115 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
116 path_ext->fpe_adj_flags = FIB_PATH_EXT_ADJ_FLAG_NONE;
117 path_ext->fpe_type = ext_type;
119 fib_path_ext_resolve(path_ext, path_list_index);
123 * @brief Return true if the label stack is implicit null
126 fib_path_ext_is_imp_null (fib_path_ext_t *path_ext)
128 return ((1 == vec_len(path_ext->fpe_label_stack)) &&
129 (MPLS_IETF_IMPLICIT_NULL_LABEL == path_ext->fpe_label_stack[0]));
132 load_balance_path_t *
133 fib_path_ext_stack (fib_path_ext_t *path_ext,
134 fib_forward_chain_type_t child_fct,
135 fib_forward_chain_type_t imp_null_fct,
136 load_balance_path_t *nhs)
138 fib_forward_chain_type_t parent_fct;
139 load_balance_path_t *nh;
141 if (!fib_path_is_resolved(path_ext->fpe_path_index))
145 * Since we are stacking this path-extension, it must have a valid out
146 * label. From the chain type request by the child, determine what
147 * chain type we will request from the parent.
151 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
154 * The EOS chain is a tricky since, when the path has an imp NULL one cannot know
155 * the adjacency to link to without knowing what the packets payload protocol
156 * will be once the label is popped.
158 if (fib_path_ext_is_imp_null(path_ext))
160 parent_fct = imp_null_fct;
165 * we have a label to stack. packets will thus be labelled when
166 * they encounter the child, ergo, non-eos.
168 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
172 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
173 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
174 if (fib_path_ext_is_imp_null(path_ext))
177 * implicit-null label for the eos or IP chain, need to pick up
180 parent_fct = child_fct;
185 * we have a label to stack. packets will thus be labelled when
186 * they encounter the child, ergo, non-eos.
188 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
191 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
192 parent_fct = child_fct;
194 case FIB_FORW_CHAIN_TYPE_ETHERNET:
195 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
202 dpo_id_t via_dpo = DPO_INVALID;
205 * The next object in the graph after the imposition of the label
206 * will be the DPO contributed by the path through which the packets
207 * are to be sent. We stack the MPLS Label DPO on this path DPO
209 fib_path_contribute_forwarding(path_ext->fpe_path_index,
213 if (dpo_is_drop(&via_dpo) ||
214 load_balance_is_drop(&via_dpo))
217 * don't stack a path extension on a drop. doing so will create
218 * a LB bucket entry on drop, and we will lose a percentage of traffic.
223 vec_add2(nhs, nh, 1);
224 nh->path_weight = fib_path_get_weight(path_ext->fpe_path_index);
225 nh->path_index = path_ext->fpe_path_index;
226 dpo_copy(&nh->path_dpo, &via_dpo);
229 * The label is stackable for this chain type
230 * construct the mpls header that will be imposed in the data-path
232 if (!fib_path_ext_is_imp_null(path_ext))
235 * we use the parent protocol for the label so that
236 * we pickup the correct MPLS imposition nodes to do
239 dpo_proto_t chain_proto;
243 eos = (child_fct == FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS ?
246 chain_proto = fib_forw_chain_type_to_dpo_proto(child_fct);
248 mldi = mpls_label_dpo_create(path_ext->fpe_label_stack,
253 dpo_set(&nh->path_dpo,
258 else if (child_fct == FIB_FORW_CHAIN_TYPE_MPLS_EOS)
261 * MPLS EOS packets using an imp-null. Insert the disposition.
263 fib_path_stack_mpls_disp(nh->path_index,
264 fib_forw_chain_type_to_dpo_proto(parent_fct),
274 fib_path_ext_list_find (const fib_path_ext_list_t *list,
275 fib_path_ext_type_t ext_type,
276 const fib_route_path_t *rpath)
278 fib_path_ext_t *path_ext;
280 vec_foreach(path_ext, list->fpel_exts)
282 if ((path_ext->fpe_type == ext_type) &&
283 !fib_path_ext_cmp(path_ext, rpath) )
292 fib_path_ext_list_find_by_path_index (const fib_path_ext_list_t *list,
293 fib_node_index_t path_index)
295 fib_path_ext_t *path_ext;
297 vec_foreach(path_ext, list->fpel_exts)
299 if (path_ext->fpe_path_index == path_index)
309 fib_path_ext_list_push_back (fib_path_ext_list_t *list,
310 fib_node_index_t path_list_index,
311 fib_path_ext_type_t ext_type,
312 const fib_route_path_t *rpath)
314 fib_path_ext_t *path_ext;
316 path_ext = fib_path_ext_list_find(list, ext_type, rpath);
318 if (NULL == path_ext)
320 vec_add2(list->fpel_exts, path_ext, 1);
321 fib_path_ext_init(path_ext, path_list_index, ext_type, rpath);
328 * insert, sorted, a path extension to the entry's list.
329 * It's not strictly necessary to sort the path extensions, since each
330 * extension has the path index to which it resolves. However, by being
331 * sorted the load-balance produced has a deterministic order, not an order
332 * based on the sequence of extension additions. this is a considerable benefit.
335 fib_path_ext_list_insert (fib_path_ext_list_t *list,
336 fib_node_index_t path_list_index,
337 fib_path_ext_type_t ext_type,
338 const fib_route_path_t *rpath)
340 fib_path_ext_t new_path_ext, *path_ext;
343 if (0 == fib_path_ext_list_length(list))
345 return (fib_path_ext_list_push_back(list, path_list_index,
349 fib_path_ext_init(&new_path_ext, path_list_index, ext_type, rpath);
351 vec_foreach(path_ext, list->fpel_exts)
353 int res = fib_path_ext_cmp(path_ext, rpath);
358 * don't add duplicate extensions. modify instead
360 vec_free(path_ext->fpe_label_stack);
361 *path_ext = new_path_ext;
373 vec_insert_elts(list->fpel_exts, &new_path_ext, 1, i);
375 return (&(list->fpel_exts[i]));
379 fib_path_ext_list_resolve (fib_path_ext_list_t *list,
380 fib_node_index_t path_list_index)
382 fib_path_ext_t *path_ext;
384 vec_foreach(path_ext, list->fpel_exts)
386 fib_path_ext_resolve(path_ext, path_list_index);
391 fib_path_ext_list_remove (fib_path_ext_list_t *list,
392 fib_path_ext_type_t ext_type,
393 const fib_route_path_t *rpath)
395 fib_path_ext_t *path_ext;
397 path_ext = fib_path_ext_list_find(list, ext_type, rpath);
399 if (NULL != path_ext)
402 * delete the element moving the remaining elements down 1 position.
403 * this preserves the sorted order.
405 vec_free(path_ext->fpe_label_stack);
406 vec_delete(list->fpel_exts, 1, (path_ext - list->fpel_exts));
411 fib_path_ext_list_flush (fib_path_ext_list_t *list)
413 fib_path_ext_t *path_ext;
415 vec_foreach(path_ext, list->fpel_exts)
417 vec_free(path_ext->fpe_label_stack);
419 vec_free(list->fpel_exts);
420 list->fpel_exts = NULL;
424 format_fib_path_ext_list (u8 * s, va_list * args)
426 fib_path_ext_list_t *list;
427 fib_path_ext_t *path_ext;
429 list = va_arg (*args, fib_path_ext_list_t *);
431 if (fib_path_ext_list_length(list))
433 s = format(s, " Extensions:");
434 vec_foreach(path_ext, list->fpel_exts)
436 s = format(s, "\n %U", format_fib_path_ext, path_ext);
444 fib_path_ext_list_length (const fib_path_ext_list_t *list)
446 return (vec_len(list->fpel_exts));