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 <vppinfra/mhash.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/adj/adj.h>
19 #include <vnet/dpo/load_balance.h>
20 #include <vnet/dpo/load_balance_map.h>
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/fib/fib_internal.h>
24 #include <vnet/fib/fib_node_list.h>
25 #include <vnet/fib/fib_walk.h>
26 #include <vnet/fib/fib_urpf_list.h>
27 #include <vnet/fib/fib_path_ext.h>
28 #include <vnet/fib/fib_table.h>
31 * The magic number of child entries that make a path-list popular.
32 * There's a trade-off here between convergence and forwarding speed.
33 * Popular path-lists generate load-balance maps for the entries that
34 * use them. If the map is present there is a switch path cost to indirect
35 * through the map - this indirection provides the fast convergence - so
36 * without the map convergence is slower.
38 #define FIB_PATH_LIST_POPULAR 64
42 * A representation of the list/set of path trough which a prefix is reachable
44 typedef struct fib_path_list_t_ {
46 * A path-list is a node in the FIB graph.
51 * Flags on the path-list
53 fib_path_list_flags_t fpl_flags;
56 * Vector of paths indicies for all configured paths.
57 * For shareable path-lists this list MUST not change.
59 fib_node_index_t *fpl_paths;
62 * the RPF list calculated for this path list
64 fib_node_index_t fpl_urpf;
68 * Array of strings/names for the FIB sources
70 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
73 * The memory pool from which we allocate all the path-lists
75 static fib_path_list_t * fib_path_list_pool;
78 * The data-base of shared path-lists
80 static uword *fib_path_list_db;
85 vlib_log_class_t fib_path_list_logger;
90 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \
92 vlib_log_debug(fib_path_list_logger, \
94 format_fib_path_list, \
95 fib_path_list_get_index(_pl), 0, \
99 static fib_path_list_t *
100 fib_path_list_get (fib_node_index_t index)
102 return (pool_elt_at_index(fib_path_list_pool, index));
106 fib_path_list_get_node (fib_node_index_t index)
108 return ((fib_node_t*)fib_path_list_get(index));
111 static fib_path_list_t*
112 fib_path_list_from_fib_node (fib_node_t *node)
114 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
115 return ((fib_path_list_t*)node);
118 static fib_node_index_t
119 fib_path_list_get_index (fib_path_list_t *path_list)
121 return (path_list - fib_path_list_pool);
125 format_fib_path_list (u8 * s, va_list * args)
127 fib_node_index_t *path_index, path_list_index;
128 fib_path_list_attribute_t attr;
129 fib_path_list_t *path_list;
132 path_list_index = va_arg (*args, fib_node_index_t);
133 indent = va_arg (*args, u32);
134 path_list = fib_path_list_get(path_list_index);
136 s = format (s, "%Upath-list:[%d]",
137 format_white_space, indent,
138 fib_path_list_get_index(path_list));
139 s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
141 if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
143 s = format (s, " flags:");
144 FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
146 if ((1<<attr) & path_list->fpl_flags)
148 s = format (s, "%s,", fib_path_list_attr_names[attr]);
152 s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);
154 vec_foreach (path_index, path_list->fpl_paths)
156 s = format(s, "%U", format_fib_path, *path_index, indent+2,
157 FIB_PATH_FORMAT_FLAGS_NONE);
165 fib_path_list_format (fib_node_index_t path_list_index,
168 return (format(s, "%U", format_fib_path_list, path_list_index, 4));
172 fib_path_list_hash (fib_path_list_t *path_list)
174 uword old_path_list_hash, new_path_list_hash, path_hash;
175 fib_node_index_t *path_index;
181 (vec_len(path_list->fpl_paths) << 16 |
182 (path_list->fpl_flags & FIB_PATH_LIST_KEY_FLAGS));
184 vec_foreach (path_index, path_list->fpl_paths)
186 path_hash = fib_path_hash(*path_index);
188 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
190 hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
194 return (new_path_list_hash);
198 fib_path_list_db_hash_key_from_index (uword index)
204 fib_path_list_db_hash_key_is_index (uword key)
210 fib_path_list_db_hash_key_2_index (uword key)
212 ASSERT (fib_path_list_db_hash_key_is_index (key));
216 static fib_path_list_t*
217 fib_path_list_db_get_from_hash_key (uword key)
219 fib_path_list_t *path_list;
221 if (fib_path_list_db_hash_key_is_index (key))
223 fib_node_index_t path_list_index;
225 path_list_index = fib_path_list_db_hash_key_2_index(key);
226 path_list = fib_path_list_get(path_list_index);
230 path_list = uword_to_pointer (key, fib_path_list_t *);
237 fib_path_list_db_hash_key_sum (hash_t * h,
240 fib_path_list_t *path_list;
242 path_list = fib_path_list_db_get_from_hash_key(key);
244 return (fib_path_list_hash(path_list));
248 fib_path_list_db_hash_key_equal (hash_t * h,
252 fib_path_list_t *path_list1, *path_list2;
254 path_list1 = fib_path_list_db_get_from_hash_key(key1);
255 path_list2 = fib_path_list_db_get_from_hash_key(key2);
257 return (fib_path_list_hash(path_list1) ==
258 fib_path_list_hash(path_list2));
261 static fib_node_index_t
262 fib_path_list_db_find (fib_path_list_t *path_list)
266 p = hash_get(fib_path_list_db, path_list);
273 return (FIB_NODE_INDEX_INVALID);
277 fib_path_list_db_insert (fib_node_index_t path_list_index)
279 fib_path_list_t *path_list;
281 path_list = fib_path_list_get(path_list_index);
283 ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
285 hash_set (fib_path_list_db,
286 fib_path_list_db_hash_key_from_index(path_list_index),
289 FIB_PATH_LIST_DBG(path_list, "DB-inserted");
293 fib_path_list_db_remove (fib_node_index_t path_list_index)
295 fib_path_list_t *path_list;
297 path_list = fib_path_list_get(path_list_index);
299 ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
301 hash_unset(fib_path_list_db,
302 fib_path_list_db_hash_key_from_index(path_list_index));
304 FIB_PATH_LIST_DBG(path_list, "DB-removed");
308 fib_path_list_destroy (fib_path_list_t *path_list)
310 fib_node_index_t *path_index;
312 FIB_PATH_LIST_DBG(path_list, "destroy");
314 vec_foreach (path_index, path_list->fpl_paths)
316 fib_path_destroy(*path_index);
319 vec_free(path_list->fpl_paths);
320 fib_urpf_list_unlock(path_list->fpl_urpf);
322 fib_node_deinit(&path_list->fpl_node);
323 pool_put(fib_path_list_pool, path_list);
327 fib_path_list_last_lock_gone (fib_node_t *node)
329 fib_path_list_t *path_list;
331 path_list = fib_path_list_from_fib_node(node);
333 FIB_PATH_LIST_DBG(path_list, "last-lock");
335 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
337 fib_path_list_db_remove(fib_path_list_get_index(path_list));
339 fib_path_list_destroy(path_list);
342 static load_balance_flags_t
343 fib_path_list_fwd_flags_2_load_balance (fib_path_list_fwd_flags_t pl_flags)
345 load_balance_flags_t lb_flags = LOAD_BALANCE_FLAG_NONE;
347 if (pl_flags & FIB_PATH_LIST_FWD_FLAG_STICKY)
349 lb_flags |= LOAD_BALANCE_ATTR_STICKY;
357 * update the multipath adj this path-list will contribute to its
358 * children's forwarding.
361 fib_path_list_mk_lb (fib_path_list_t *path_list,
362 fib_forward_chain_type_t fct,
364 fib_path_list_fwd_flags_t flags)
366 fib_node_index_t *path_index;
367 load_balance_path_t *nhs;
371 dproto = fib_forw_chain_type_to_dpo_proto(fct);
374 * We gather the DPOs from resolved paths.
376 vec_foreach (path_index, path_list->fpl_paths)
378 if ((flags & FIB_PATH_LIST_FWD_FLAG_STICKY) ||
379 fib_path_is_resolved(*path_index))
381 nhs = fib_path_append_nh_for_multipath_hash(*path_index,
387 * Path-list load-balances, which if used, would be shared and hence
388 * never need a load-balance map.
393 load_balance_create(vec_len(nhs),
395 load_balance_get_default_flow_hash(dproto)));
396 load_balance_multipath_update(dpo, nhs,
397 fib_path_list_fwd_flags_2_load_balance(flags));
399 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
405 * @brief [re]build the path list's uRPF list
408 fib_path_list_mk_urpf (fib_path_list_t *path_list)
410 fib_node_index_t *path_index;
413 * ditch the old one. by iterating through all paths we are going
414 * to re-find all the adjs that were in the old one anyway. If we
415 * keep the old one, then the |sort|uniq requires more work.
416 * All users of the RPF list have their own lock, so we can release
419 fib_urpf_list_unlock(path_list->fpl_urpf);
420 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
422 vec_foreach (path_index, path_list->fpl_paths)
424 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
427 fib_urpf_list_bake(path_list->fpl_urpf);
431 * @brief Contribute (add) this path list's uRPF list. This allows the child
432 * to construct an aggregate list.
435 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
438 fib_path_list_t *path_list;
440 path_list = fib_path_list_get(path_list_index);
442 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
446 * @brief Return the the child the RPF list pre-built for this path list
449 fib_path_list_get_urpf (fib_node_index_t path_list_index)
451 fib_path_list_t *path_list;
453 path_list = fib_path_list_get(path_list_index);
455 return (path_list->fpl_urpf);
459 * fib_path_list_back_walk
461 * Called from one of this path-list's paths to progate
465 fib_path_list_back_walk (fib_node_index_t path_list_index,
466 fib_node_back_walk_ctx_t *ctx)
468 fib_path_list_t *path_list;
470 path_list = fib_path_list_get(path_list_index);
472 fib_path_list_mk_urpf(path_list);
474 FIB_PATH_LIST_DBG(path_list, "bw:%U",
475 format_fib_node_bw_reason, ctx->fnbw_reason);
478 * propagate the backwalk further
480 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR)
483 * many children. schedule a async walk
485 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
487 FIB_WALK_PRIORITY_LOW,
493 * only a few children. continue the walk synchronously
495 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
500 * fib_path_list_back_walk_notify
502 * A back walk has reach this path-list.
504 static fib_node_back_walk_rc_t
505 fib_path_list_back_walk_notify (fib_node_t *node,
506 fib_node_back_walk_ctx_t *ctx)
509 * the path-list is not a direct child of any other node type
510 * paths, which do not change their to-list-mapping, save the
511 * list they are a member of, and invoke the BW function directly.
515 return (FIB_NODE_BACK_WALK_CONTINUE);
519 * Display the path-list memory usage
522 fib_path_list_memory_show (void)
524 fib_show_memory_usage("Path-list",
525 pool_elts(fib_path_list_pool),
526 pool_len(fib_path_list_pool),
527 sizeof(fib_path_list_t));
528 fib_urpf_list_show_mem();
532 * The FIB path-list's graph node virtual function table
534 static const fib_node_vft_t fib_path_list_vft = {
535 .fnv_get = fib_path_list_get_node,
536 .fnv_last_lock = fib_path_list_last_lock_gone,
537 .fnv_back_walk = fib_path_list_back_walk_notify,
538 .fnv_mem_show = fib_path_list_memory_show,
541 static inline fib_path_list_t *
542 fib_path_list_alloc (fib_node_index_t *path_list_index)
544 fib_path_list_t *path_list;
546 pool_get(fib_path_list_pool, path_list);
547 clib_memset(path_list, 0, sizeof(*path_list));
549 fib_node_init(&path_list->fpl_node,
550 FIB_NODE_TYPE_PATH_LIST);
551 path_list->fpl_urpf = INDEX_INVALID;
552 path_list->fpl_paths = NULL;
554 *path_list_index = fib_path_list_get_index(path_list);
556 FIB_PATH_LIST_DBG(path_list, "alloc");
561 static fib_path_list_t *
562 fib_path_list_resolve (fib_path_list_t *path_list)
564 fib_node_index_t *path_index, *paths, path_list_index;
566 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
569 * resolving a path-list is a recursive action. this means more path
570 * lists can be created during this call, and hence this path-list
571 * can be realloc'd. so we work with copies.
572 * this function is called only once per-path list, so its no great overhead.
574 path_list_index = fib_path_list_get_index(path_list);
575 paths = vec_dup(path_list->fpl_paths);
577 vec_foreach (path_index, paths)
579 fib_path_resolve(*path_index);
583 path_list = fib_path_list_get(path_list_index);
585 FIB_PATH_LIST_DBG(path_list, "resovled");
587 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
589 fib_path_list_mk_urpf(path_list);
595 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
597 fib_path_list_t *path_list;
599 if (FIB_NODE_INDEX_INVALID == path_list_index)
604 path_list = fib_path_list_get(path_list_index);
606 return (vec_len(path_list->fpl_paths));
611 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
613 fib_node_index_t *path_index;
614 fib_path_list_t *path_list;
617 path_list = fib_path_list_get(path_list_index);
620 vec_foreach (path_index, path_list->fpl_paths)
622 sw_if_index = fib_path_get_resolving_interface(*path_index);
623 if (~0 != sw_if_index)
625 return (sw_if_index);
629 return (sw_if_index);
633 fib_path_list_get_proto (fib_node_index_t path_list_index)
635 fib_path_list_t *path_list;
637 path_list = fib_path_list_get(path_list_index);
640 * we don't support a mix of path protocols, so we can return the proto
643 return (fib_path_get_proto(path_list->fpl_paths[0]));
647 fib_path_list_is_looped (fib_node_index_t path_list_index)
649 fib_path_list_t *path_list;
651 path_list = fib_path_list_get(path_list_index);
653 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
657 fib_path_list_is_popular (fib_node_index_t path_list_index)
659 fib_path_list_t *path_list;
661 path_list = fib_path_list_get(path_list_index);
663 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR);
666 static fib_path_list_flags_t
667 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
670 * we do no share drop nor exclusive path-lists
672 if (flags & FIB_PATH_LIST_FLAG_DROP ||
673 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
675 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
682 fib_path_list_create (fib_path_list_flags_t flags,
683 const fib_route_path_t *rpaths)
685 fib_node_index_t path_list_index, old_path_list_index;
686 fib_path_list_t *path_list;
689 flags = fib_path_list_flags_fixup(flags);
690 path_list = fib_path_list_alloc(&path_list_index);
691 path_list->fpl_flags = flags;
695 vec_foreach_index(i, rpaths)
697 vec_add1(path_list->fpl_paths,
698 fib_path_create(path_list_index,
702 * we sort the paths since the key for the path-list is
703 * the description of the paths it contains. The paths need to
704 * be sorted else this description will differ.
706 if (vec_len(path_list->fpl_paths) > 1)
708 vec_sort_with_function(path_list->fpl_paths,
709 fib_path_cmp_for_sort);
714 * If a shared path list is requested, consult the DB for a match
716 if (flags & FIB_PATH_LIST_FLAG_SHARED)
719 * check for a matching path-list in the DB.
720 * If we find one then we can return the existing one and destroy the
721 * new one just created.
723 old_path_list_index = fib_path_list_db_find(path_list);
724 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
726 fib_path_list_destroy(path_list);
728 path_list_index = old_path_list_index;
733 * if there was not a matching path-list, then this
734 * new one will need inserting into the DB and resolving.
736 fib_path_list_db_insert(path_list_index);
737 path_list = fib_path_list_resolve(path_list);
743 * no shared path list requested. resolve and use the one
746 path_list = fib_path_list_resolve(path_list);
749 return (path_list_index);
752 static fib_path_cfg_flags_t
753 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
755 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
757 if (plf & FIB_PATH_LIST_FLAG_DROP)
759 pf |= FIB_PATH_CFG_FLAG_DROP;
761 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
763 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
765 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
767 pf |= FIB_PATH_CFG_FLAG_LOCAL;
774 fib_path_list_create_special (dpo_proto_t nh_proto,
775 fib_path_list_flags_t flags,
778 fib_node_index_t path_index, path_list_index;
779 fib_path_list_t *path_list;
781 path_list = fib_path_list_alloc(&path_list_index);
782 path_list->fpl_flags = flags;
785 fib_path_create_special(path_list_index,
787 fib_path_list_flags_2_path_flags(flags),
789 vec_add1(path_list->fpl_paths, path_index);
792 * we don't share path-lists. we can do PIC on them so why bother.
794 path_list = fib_path_list_resolve(path_list);
796 return (path_list_index);
800 * return the index info the path-lists's vector of paths, of the matching path.
804 fib_path_list_find_rpath (fib_node_index_t path_list_index,
805 const fib_route_path_t *rpath)
807 fib_path_list_t *path_list;
810 path_list = fib_path_list_get(path_list_index);
812 vec_foreach_index (ii, path_list->fpl_paths)
814 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
824 * fib_path_list_copy_and_path_add
826 * Create a copy of a path-list and append one more path to it.
827 * The path-list returned could either have been newly created, or
828 * can be a shared path-list from the data-base.
831 fib_path_list_paths_add (fib_node_index_t path_list_index,
832 const fib_route_path_t *rpaths)
834 fib_node_index_t *new_path_indices, *path_index;
835 const fib_route_path_t *rpath;
836 fib_path_list_t *path_list;
840 * alloc the new list before we retrieve the old one, lest
841 * the alloc result in a realloc
843 path_list = fib_path_list_get(path_list_index);
845 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
847 FIB_PATH_LIST_DBG(path_list, "paths-add");
849 new_path_indices = NULL;
850 vec_validate_init_empty(new_path_indices,
852 FIB_NODE_INDEX_INVALID);
854 vec_foreach (path_index, path_list->fpl_paths)
857 * don't add duplicate paths
861 vec_foreach_index(ii, rpaths)
864 if (0 == fib_path_cmp_w_route_path(*path_index, rpath))
872 new_path_indices[ii] = *path_index;
877 * new_path_indices array contains INVALID for each path not found
878 * and something valid for matches
880 vec_foreach_index (ii, new_path_indices)
882 path_index = &new_path_indices[ii];
885 if (FIB_NODE_INDEX_INVALID == *path_index)
887 *path_index = fib_path_create(path_list_index, rpath);
889 * Add the new path - no sort, no sharing, no key..
891 vec_add1(path_list->fpl_paths, *path_index);
894 * no shared path list requested. resolve and use the one
897 fib_path_resolve(*path_index);
901 FIB_PATH_LIST_DBG(path_list, "paths-added");
903 return (new_path_indices);
907 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
908 fib_path_list_flags_t flags,
909 const fib_route_path_t *rpaths)
911 fib_node_index_t new_path_index, *orig_path_index;
912 fib_path_list_t *path_list, *orig_path_list;
913 fib_node_index_t exist_path_list_index;
914 fib_node_index_t path_list_index;
915 const fib_route_path_t *rpath;
919 * alloc the new list before we retrieve the old one, lest
920 * the alloc result in a realloc
922 path_list = fib_path_list_alloc(&path_list_index);
924 orig_path_list = fib_path_list_get(orig_path_list_index);
926 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
928 flags = fib_path_list_flags_fixup(flags);
929 path_list->fpl_flags = flags;
931 vec_validate(path_list->fpl_paths,
932 (vec_len(orig_path_list->fpl_paths) +
933 vec_len(rpaths) - 1));
936 vec_foreach(orig_path_index, orig_path_list->fpl_paths)
939 * copy the original paths over to the new list
941 path_list->fpl_paths[pi++] = fib_path_copy(*orig_path_index,
944 vec_foreach(rpath, rpaths)
948 new_path_index = fib_path_create(path_list_index, rpath);
950 vec_foreach(orig_path_index, orig_path_list->fpl_paths)
953 * don't add duplicate paths
954 * In the unlikely event the path is a duplicate, then we'll
955 * find a matching path-list later and this one will be toast.
957 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
965 _vec_len(path_list->fpl_paths) =
966 vec_len(path_list->fpl_paths) - 1;
967 fib_path_destroy(new_path_index);
971 path_list->fpl_paths[pi++] = new_path_index;
976 * we sort the paths since the key for the path-list is
977 * the description of the paths it contains. The paths need to
978 * be sorted else this description will differ.
980 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
982 FIB_PATH_LIST_DBG(path_list, "path-added");
985 * check for a matching path-list in the DB.
986 * If we find one then we can return the existing one and destroy the
987 * new one just created.
989 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
991 exist_path_list_index = fib_path_list_db_find(path_list);
992 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
994 fib_path_list_destroy(path_list);
996 path_list_index = exist_path_list_index;
1001 * if there was not a matching path-list, then this
1002 * new one will need inserting into the DB and resolving.
1004 fib_path_list_db_insert(path_list_index);
1006 path_list = fib_path_list_resolve(path_list);
1012 * no shared path list requested. resolve and use the one
1015 path_list = fib_path_list_resolve(path_list);
1018 return (path_list_index);
1022 * fib_path_list_paths_remove
1025 fib_path_list_paths_remove (fib_node_index_t path_list_index,
1026 const fib_route_path_t *rpaths)
1028 fib_node_index_t *match_path_indices;
1029 fib_path_list_t *path_list;
1032 path_list = fib_path_list_get(path_list_index);
1033 match_path_indices = NULL;
1034 vec_validate_init_empty(match_path_indices,
1035 vec_len(rpaths) - 1,
1036 FIB_NODE_INDEX_INVALID);
1038 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
1040 FIB_PATH_LIST_DBG(path_list, "path-remove");
1043 * the number of existing paths is likely to be larger than the
1044 * number of paths being added.
1045 * walk in reverse so the vec_del is ok
1047 vec_foreach_index_backwards(ii, path_list->fpl_paths)
1051 vec_foreach_index(jj, rpaths)
1053 if (0 == fib_path_cmp_w_route_path(path_list->fpl_paths[ii],
1062 fib_node_index_t match_path_index;
1066 match_path_index = path_list->fpl_paths[ii];
1067 vec_del1(path_list->fpl_paths, ii);
1068 fib_path_destroy(match_path_index);
1069 match_path_indices[jj] = match_path_index;
1073 FIB_PATH_LIST_DBG(path_list, "paths-removed");
1075 return (match_path_indices);
1079 * fib_path_list_copy_and_path_remove
1081 * Copy the path-list excluding the path passed.
1082 * If the path is the last one, then the index reurned will be invalid.
1083 * i.e. the path-list is toast.
1086 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
1087 fib_path_list_flags_t flags,
1088 const fib_route_path_t *rpaths)
1090 fib_node_index_t *orig_path_index, path_list_index, tmp_path_index;
1091 fib_path_list_t *path_list, *orig_path_list;
1092 const fib_route_path_t *rpath;
1093 fib_node_index_t pi;
1095 path_list = fib_path_list_alloc(&path_list_index);
1097 flags = fib_path_list_flags_fixup(flags);
1098 orig_path_list = fib_path_list_get(orig_path_list_index);
1100 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
1102 path_list->fpl_flags = flags;
1104 * allocate as many paths as we might need in one go, rather than
1105 * using vec_add to do a few at a time.
1107 vec_validate(path_list->fpl_paths,
1108 vec_len(orig_path_list->fpl_paths) - 1);
1112 * create a representation of the path to be removed, so it
1113 * can be used as a comparison object during the copy.
1115 vec_foreach(orig_path_index, orig_path_list->fpl_paths)
1118 * copy the original paths over to the new list
1120 path_list->fpl_paths[pi++] = fib_path_copy(*orig_path_index,
1123 vec_foreach(rpath, rpaths)
1126 tmp_path_index = fib_path_create(path_list_index, rpath);
1128 vec_foreach_index(pi, path_list->fpl_paths)
1130 if (0 == fib_path_cmp(tmp_path_index, path_list->fpl_paths[pi]))
1138 fib_path_destroy(path_list->fpl_paths[pi]);
1139 vec_del1(path_list->fpl_paths, pi);
1141 fib_path_destroy(tmp_path_index);
1145 * if there are no paths, then the new path-list is aborted
1147 if (0 == vec_len(path_list->fpl_paths)) {
1148 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1150 fib_path_list_destroy(path_list);
1152 path_list_index = FIB_NODE_INDEX_INVALID;
1155 * we sort the paths since the key for the path-list is
1156 * the description of the paths it contains. The paths need to
1157 * be sorted else this description will differ.
1159 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1162 * If a shared path list is requested, consult the DB for a match
1164 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1166 fib_node_index_t exist_path_list_index;
1169 * check for a matching path-list in the DB.
1170 * If we find one then we can return the existing one and destroy the
1171 * new one just created.
1173 exist_path_list_index = fib_path_list_db_find(path_list);
1174 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1176 fib_path_list_destroy(path_list);
1178 path_list_index = exist_path_list_index;
1183 * if there was not a matching path-list, then this
1184 * new one will need inserting into the DB and resolving.
1186 fib_path_list_db_insert(path_list_index);
1188 path_list = fib_path_list_resolve(path_list);
1194 * no shared path list requested. resolve and use the one
1197 path_list = fib_path_list_resolve(path_list);
1201 return (path_list_index);
1205 * fib_path_list_contribute_forwarding
1207 * Return the index of a load-balance that user of this path-list should
1208 * use for forwarding
1211 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1212 fib_forward_chain_type_t fct,
1213 fib_path_list_fwd_flags_t flags,
1216 fib_path_list_t *path_list;
1218 path_list = fib_path_list_get(path_list_index);
1220 fib_path_list_mk_lb(path_list, fct, dpo, flags);
1222 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
1225 * If there's only one bucket in the load-balance then we can
1228 if ((1 == load_balance_n_buckets(dpo->dpoi_index)) &&
1229 (FIB_PATH_LIST_FWD_FLAG_COLLAPSE & flags))
1231 dpo_copy(dpo, load_balance_get_bucket(dpo->dpoi_index, 0));
1236 * fib_path_list_get_adj
1238 * Return the index of a adjacency for the first path that user of this
1239 * path-list should use for forwarding
1242 fib_path_list_get_adj (fib_node_index_t path_list_index,
1243 fib_forward_chain_type_t type)
1245 fib_path_list_t *path_list;
1247 path_list = fib_path_list_get(path_list_index);
1248 return (fib_path_get_adj(path_list->fpl_paths[0]));
1252 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1253 fib_node_index_t **entry_indicies)
1255 fib_node_index_t *path_index;
1256 int is_looped, list_looped;
1257 fib_path_list_t *path_list;
1260 path_list = fib_path_list_get(path_list_index);
1262 vec_foreach (path_index, path_list->fpl_paths)
1264 fib_node_index_t *copy, **copy_ptr;
1267 * we need a copy of the nodes visited so that when we add entries
1268 * we explore on the nth path and a looped is detected, those entries
1269 * are not again searched for n+1 path and so finding a loop that does
1272 copy = vec_dup(*entry_indicies);
1275 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1276 list_looped += is_looped;
1281 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", list_looped);
1285 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1289 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1292 return (list_looped);
1296 fib_path_list_child_add (fib_node_index_t path_list_index,
1297 fib_node_type_t child_type,
1298 fib_node_index_t child_index)
1302 sibling = fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1307 if (FIB_PATH_LIST_POPULAR == fib_node_get_n_children(FIB_NODE_TYPE_PATH_LIST,
1311 * Set the popular flag on the path-list once we pass the magic
1312 * threshold. then walk children to update.
1313 * We don't undo this action. The rational being that the number
1314 * of entries using this prefix is large enough such that it is a
1315 * non-trival amount of effort to converge them. If we get into the
1316 * situation where we are adding and removing entries such that we
1317 * flip-flop over the threshold, then this non-trivial work is added
1318 * to each of those routes adds/deletes - not a situation we want.
1320 fib_node_back_walk_ctx_t ctx = {
1321 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
1323 fib_path_list_t *path_list;
1325 path_list = fib_path_list_get(path_list_index);
1326 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_POPULAR;
1328 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, &ctx);
1335 fib_path_list_child_remove (fib_node_index_t path_list_index,
1338 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1344 fib_path_list_lock(fib_node_index_t path_list_index)
1346 fib_path_list_t *path_list;
1348 if (FIB_NODE_INDEX_INVALID != path_list_index)
1350 path_list = fib_path_list_get(path_list_index);
1352 fib_node_lock(&path_list->fpl_node);
1357 fib_path_list_unlock (fib_node_index_t path_list_index)
1359 fib_path_list_t *path_list;
1361 if (FIB_NODE_INDEX_INVALID != path_list_index)
1363 path_list = fib_path_list_get(path_list_index);
1365 fib_node_unlock(&path_list->fpl_node);
1370 fib_path_list_pool_size (void)
1372 return (pool_elts(fib_path_list_pool));
1376 fib_path_list_db_size (void)
1378 return (hash_elts(fib_path_list_db));
1382 fib_path_list_walk (fib_node_index_t path_list_index,
1383 fib_path_list_walk_fn_t func,
1386 fib_node_index_t *path_index;
1387 fib_path_list_t *path_list;
1389 path_list = fib_path_list_get(path_list_index);
1391 vec_foreach(path_index, path_list->fpl_paths)
1393 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1401 fib_path_list_walk_w_ext (fib_node_index_t path_list_index,
1402 const fib_path_ext_list_t *ext_list,
1403 fib_path_list_walk_w_ext_fn_t func,
1406 fib_node_index_t *path_index;
1407 fib_path_list_t *path_list;
1408 fib_path_ext_t *path_ext;
1410 path_list = fib_path_list_get(path_list_index);
1412 vec_foreach(path_index, path_list->fpl_paths)
1414 path_ext = fib_path_ext_list_find_by_path_index(ext_list, *path_index);
1416 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1425 fib_path_list_module_init (void)
1427 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1429 fib_path_list_db = hash_create2 (/* elts */ 0,
1431 /* value_bytes */ sizeof (fib_node_index_t),
1432 fib_path_list_db_hash_key_sum,
1433 fib_path_list_db_hash_key_equal,
1434 /* format pair/arg */
1436 fib_path_list_logger = vlib_log_register_class("fib", "path-list");
1439 static clib_error_t *
1440 show_fib_path_list_command (vlib_main_t * vm,
1441 unformat_input_t * input,
1442 vlib_cli_command_t * cmd)
1444 fib_path_list_t *path_list;
1445 fib_node_index_t pli;
1447 if (unformat (input, "%d", &pli))
1450 * show one in detail
1452 if (!pool_is_free_index(fib_path_list_pool, pli))
1454 path_list = fib_path_list_get(pli);
1455 u8 *s = fib_path_list_format(pli, NULL);
1456 s = format(s, "children:");
1457 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1458 vlib_cli_output (vm, "%v", s);
1463 vlib_cli_output (vm, "path list %d invalid", pli);
1471 vlib_cli_output (vm, "FIB Path Lists");
1472 pool_foreach_index (pli, fib_path_list_pool)
1474 vlib_cli_output (vm, "%U", format_fib_path_list, pli, 0);
1480 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1481 .path = "show fib path-lists",
1482 .function = show_fib_path_list_command,
1483 .short_help = "show fib path-lists",