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/bier/bier_entry.h>
17 #include <vnet/bier/bier_update.h>
19 #include <vnet/fib/fib_path_list.h>
21 #include <vnet/bier/bier_fmask_db.h>
22 #include <vnet/bier/bier_fmask.h>
23 #include <vnet/bier/bier_table.h>
25 bier_entry_t *bier_entry_pool;
28 bier_entry_get_index (const bier_entry_t *be)
30 return (be - bier_entry_pool);
33 static fib_path_list_walk_rc_t
34 bier_entry_link_walk (fib_node_index_t pl_index,
35 fib_node_index_t path_index,
38 bier_entry_t *be = arg;
41 bfmi = fib_path_get_resolving_index(path_index);
42 bier_fmask_link(bfmi, be->be_bp);
44 return (FIB_PATH_LIST_WALK_CONTINUE);
47 static fib_path_list_walk_rc_t
48 bier_entry_unlink_walk (fib_node_index_t pl_index,
49 fib_node_index_t path_index,
52 bier_entry_t *be = arg;
55 bfmi = fib_path_get_resolving_index(path_index);
56 bier_fmask_unlink(bfmi, be->be_bp);
58 return (FIB_PATH_LIST_WALK_CONTINUE);
62 bier_entry_create (index_t bti,
67 pool_get(bier_entry_pool, be);
71 be->be_path_list = FIB_NODE_INDEX_INVALID;
73 return (bier_entry_get_index(be));
77 bier_entry_delete (index_t bei)
81 be = bier_entry_get(bei);
84 * if we still ahve a path-list, unlink from it
86 if (FIB_NODE_INDEX_INVALID != be->be_path_list)
88 fib_path_list_walk(be->be_path_list,
89 bier_entry_unlink_walk,
91 fib_path_list_child_remove(be->be_path_list,
92 be->be_sibling_index);
95 pool_put(bier_entry_pool, be);
99 bier_entry_table_ecmp_walk_add_fmask (index_t btei,
102 bier_entry_t *be = arg;;
105 * choose a fmask from the entry's resolved set to add
106 * to ECMP table's lookup table
108 if (FIB_NODE_INDEX_INVALID != be->be_path_list)
110 const bier_table_id_t *btid;
111 dpo_id_t dpo = DPO_INVALID;
112 const dpo_id_t *choice;
115 btid = bier_table_get_id(btei);
117 fib_path_list_contribute_forwarding(be->be_path_list,
118 FIB_FORW_CHAIN_TYPE_BIER,
122 * select the appropriate bucket from the LB
124 ASSERT(dpo.dpoi_type == DPO_LOAD_BALANCE);
126 lb = load_balance_get(dpo.dpoi_index);
128 choice = load_balance_get_bucket_i(lb,
130 (lb->lb_n_buckets_minus_1));
132 if (choice->dpoi_type == DPO_BIER_FMASK)
134 bier_table_ecmp_set_fmask(btei, be->be_bp,
140 * any other type results in a drop, which we represent
141 * with an empty bucket
143 bier_table_ecmp_set_fmask(btei, be->be_bp,
152 * no fmasks left. insert a drop
154 bier_table_ecmp_set_fmask(btei, be->be_bp, INDEX_INVALID);
159 bier_entry_path_add (index_t bei,
160 const fib_route_path_t *rpaths)
162 fib_node_index_t old_pl_index;
165 be = bier_entry_get(bei);
166 old_pl_index = be->be_path_list;
169 * lock the path-list so it does not go away before we unlink
170 * from its resolved fmasks
172 fib_path_list_lock(old_pl_index);
174 if (FIB_NODE_INDEX_INVALID == be->be_path_list)
176 old_pl_index = FIB_NODE_INDEX_INVALID;
177 be->be_path_list = fib_path_list_create((FIB_PATH_LIST_FLAG_SHARED |
178 FIB_PATH_LIST_FLAG_NO_URPF),
180 be->be_sibling_index = fib_path_list_child_add(be->be_path_list,
181 FIB_NODE_TYPE_BIER_ENTRY,
182 bier_entry_get_index(be));
187 old_pl_index = be->be_path_list;
190 fib_path_list_copy_and_path_add(old_pl_index,
191 (FIB_PATH_LIST_FLAG_SHARED |
192 FIB_PATH_LIST_FLAG_NO_URPF),
195 fib_path_list_child_remove(old_pl_index,
196 be->be_sibling_index);
197 be->be_sibling_index = fib_path_list_child_add(be->be_path_list,
198 FIB_NODE_TYPE_BIER_ENTRY,
199 bier_entry_get_index(be));
202 * link the entry's bit-position to each fmask in the new path-list
203 * then unlink from the old.
205 fib_path_list_walk(be->be_path_list,
206 bier_entry_link_walk,
208 if (FIB_NODE_INDEX_INVALID != old_pl_index)
210 fib_path_list_walk(old_pl_index,
211 bier_entry_unlink_walk,
216 * update the ECNP tables with the new choice
218 bier_table_ecmp_walk(be->be_bti,
219 bier_entry_table_ecmp_walk_add_fmask,
223 * symmetric unlock. The old path-list may not exist hereinafter
225 fib_path_list_unlock(old_pl_index);
229 bier_entry_path_remove (index_t bei,
230 const fib_route_path_t *rpaths)
232 fib_node_index_t old_pl_index;
235 be = bier_entry_get(bei);
236 old_pl_index = be->be_path_list;
238 fib_path_list_lock(old_pl_index);
240 ASSERT (FIB_NODE_INDEX_INVALID != be->be_path_list);
243 fib_path_list_copy_and_path_remove(old_pl_index,
244 (FIB_PATH_LIST_FLAG_SHARED |
245 FIB_PATH_LIST_FLAG_NO_URPF),
248 if (be->be_path_list != old_pl_index)
253 fib_path_list_child_remove(old_pl_index,
254 be->be_sibling_index);
256 if (FIB_NODE_INDEX_INVALID != be->be_path_list)
259 * link the entry's bit-position to each fmask in the new path-list
260 * then unlink from the old.
262 fib_path_list_walk(be->be_path_list,
263 bier_entry_link_walk,
265 be->be_sibling_index =
266 fib_path_list_child_add(be->be_path_list,
267 FIB_NODE_TYPE_BIER_ENTRY,
268 bier_entry_get_index(be));
271 fib_path_list_walk(old_pl_index,
272 bier_entry_unlink_walk,
275 fib_path_list_unlock(old_pl_index);
279 * update the ECNP tables with the new choice
281 bier_table_ecmp_walk(be->be_bti,
282 bier_entry_table_ecmp_walk_add_fmask,
285 return (fib_path_list_get_n_paths(be->be_path_list));
289 bier_entry_contribute_forwarding(index_t bei,
292 bier_entry_t *be = bier_entry_get(bei);
294 fib_path_list_contribute_forwarding(be->be_path_list,
295 FIB_FORW_CHAIN_TYPE_BIER,
300 format_bier_entry (u8* s, va_list *ap)
302 index_t bei = va_arg(*ap, index_t);
303 bier_show_flags_t flags = va_arg(*ap, bier_show_flags_t);
305 bier_entry_t *be = bier_entry_get(bei);
307 s = format(s, " bp:%d\n", be->be_bp);
308 s = fib_path_list_format(be->be_path_list, s);
310 if (flags & BIER_SHOW_DETAIL)
312 dpo_id_t dpo = DPO_INVALID;
314 bier_entry_contribute_forwarding(bei, &dpo);
316 s = format(s, " forwarding:\n");
318 format_dpo_id, &dpo, 2);
326 bier_entry_get_node (fib_node_index_t index)
328 bier_entry_t *be = bier_entry_get(index);
329 return (&(be->be_node));
333 bier_entry_get_from_node (fib_node_t *node)
335 return ((bier_entry_t*)(((char*)node) -
336 STRUCT_OFFSET_OF(bier_entry_t,
341 bier_entry_last_lock_gone (fib_node_t *node)
344 * the lifetime of the entry is managed by the table.
350 * A back walk has reached this BIER entry
352 static fib_node_back_walk_rc_t
353 bier_entry_back_walk_notify (fib_node_t *node,
354 fib_node_back_walk_ctx_t *ctx)
357 * re-populate the ECMP tables with new choices
359 bier_entry_t *be = bier_entry_get_from_node(node);
361 bier_table_ecmp_walk(be->be_bti,
362 bier_entry_table_ecmp_walk_add_fmask,
366 * no need to propagate further up the graph.
368 return (FIB_NODE_BACK_WALK_CONTINUE);
372 * The BIER fmask's graph node virtual function table
374 static const fib_node_vft_t bier_entry_vft = {
375 .fnv_get = bier_entry_get_node,
376 .fnv_last_lock = bier_entry_last_lock_gone,
377 .fnv_back_walk = bier_entry_back_walk_notify,
381 bier_entry_module_init (vlib_main_t * vm)
383 fib_node_register_type (FIB_NODE_TYPE_BIER_ENTRY, &bier_entry_vft);
388 VLIB_INIT_FUNCTION (bier_entry_module_init);