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>
30 * The magic number of child entries that make a path-list popular.
31 * There's a trade-off here between convergnece and forwarding speed.
32 * Popular path-lists generate load-balance maps for the entries that
33 * use them. If the map is present there is a switch path cost to indirect
34 * through the map - this indirection provides the fast convergence - so
35 * without the map convergence is slower.
37 #define FIB_PATH_LIST_POPULAR 64
41 * A representation of the list/set of path trough which a prefix is reachable
43 typedef struct fib_path_list_t_ {
45 * A path-list is a node in the FIB graph.
50 * Flags on the path-list
52 fib_path_list_flags_t fpl_flags;
55 * Vector of paths indicies for all configured paths.
56 * For shareable path-lists this list MUST not change.
58 fib_node_index_t *fpl_paths;
61 * the RPF list calculated for this path list
63 fib_node_index_t fpl_urpf;
66 * Hash table of paths. valid only with INDEXED flag
72 * Array of strings/names for the FIB sources
74 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
77 * The memory pool from which we allocate all the path-lists
79 static fib_path_list_t * fib_path_list_pool;
82 * The data-base of shared path-lists
84 static uword *fib_path_list_db;
89 vlib_log_class_t fib_path_list_logger;
94 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \
96 vlib_log_debug(fib_path_list_logger, \
98 format_fib_path_list, \
99 fib_path_list_get_index(_pl), 0, \
103 static fib_path_list_t *
104 fib_path_list_get (fib_node_index_t index)
106 return (pool_elt_at_index(fib_path_list_pool, index));
110 fib_path_list_get_node (fib_node_index_t index)
112 return ((fib_node_t*)fib_path_list_get(index));
115 static fib_path_list_t*
116 fib_path_list_from_fib_node (fib_node_t *node)
118 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
119 return ((fib_path_list_t*)node);
122 static fib_node_index_t
123 fib_path_list_get_index (fib_path_list_t *path_list)
125 return (path_list - fib_path_list_pool);
129 format_fib_path_list (u8 * s, va_list * args)
131 fib_node_index_t *path_index, path_list_index;
132 fib_path_list_attribute_t attr;
133 fib_path_list_t *path_list;
136 path_list_index = va_arg (*args, fib_node_index_t);
137 indent = va_arg (*args, u32);
138 path_list = fib_path_list_get(path_list_index);
140 s = format (s, "%Upath-list:[%d]",
141 format_white_space, indent,
142 fib_path_list_get_index(path_list));
143 s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
145 if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
147 s = format (s, " flags:");
148 FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
150 if ((1<<attr) & path_list->fpl_flags)
152 s = format (s, "%s,", fib_path_list_attr_names[attr]);
156 s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);
158 vec_foreach (path_index, path_list->fpl_paths)
160 s = format(s, "%U", format_fib_path, *path_index, indent+2,
161 FIB_PATH_FORMAT_FLAGS_NONE);
169 fib_path_list_format (fib_node_index_t path_list_index,
172 return (format(s, "%U", format_fib_path_list, path_list_index, 4));
176 fib_path_list_hash (fib_path_list_t *path_list)
178 uword old_path_list_hash, new_path_list_hash, path_hash;
179 fib_node_index_t *path_index;
185 (vec_len(path_list->fpl_paths) << 16 |
186 (path_list->fpl_flags & FIB_PATH_LIST_KEY_FLAGS));
188 vec_foreach (path_index, path_list->fpl_paths)
190 path_hash = fib_path_hash(*path_index);
192 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
194 hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
198 return (new_path_list_hash);
202 fib_path_list_db_hash_key_from_index (uword index)
208 fib_path_list_db_hash_key_is_index (uword key)
214 fib_path_list_db_hash_key_2_index (uword key)
216 ASSERT (fib_path_list_db_hash_key_is_index (key));
220 static fib_path_list_t*
221 fib_path_list_db_get_from_hash_key (uword key)
223 fib_path_list_t *path_list;
225 if (fib_path_list_db_hash_key_is_index (key))
227 fib_node_index_t path_list_index;
229 path_list_index = fib_path_list_db_hash_key_2_index(key);
230 path_list = fib_path_list_get(path_list_index);
234 path_list = uword_to_pointer (key, fib_path_list_t *);
241 fib_path_list_db_hash_key_sum (hash_t * h,
244 fib_path_list_t *path_list;
246 path_list = fib_path_list_db_get_from_hash_key(key);
248 return (fib_path_list_hash(path_list));
252 fib_path_list_db_hash_key_equal (hash_t * h,
256 fib_path_list_t *path_list1, *path_list2;
258 path_list1 = fib_path_list_db_get_from_hash_key(key1);
259 path_list2 = fib_path_list_db_get_from_hash_key(key2);
261 return (fib_path_list_hash(path_list1) ==
262 fib_path_list_hash(path_list2));
265 static fib_node_index_t
266 fib_path_list_db_find (fib_path_list_t *path_list)
270 p = hash_get(fib_path_list_db, path_list);
277 return (FIB_NODE_INDEX_INVALID);
281 fib_path_list_db_insert (fib_node_index_t path_list_index)
283 fib_path_list_t *path_list;
285 path_list = fib_path_list_get(path_list_index);
287 ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
289 hash_set (fib_path_list_db,
290 fib_path_list_db_hash_key_from_index(path_list_index),
293 FIB_PATH_LIST_DBG(path_list, "DB-inserted");
297 fib_path_list_db_remove (fib_node_index_t path_list_index)
299 fib_path_list_t *path_list;
301 path_list = fib_path_list_get(path_list_index);
303 ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
305 hash_unset(fib_path_list_db,
306 fib_path_list_db_hash_key_from_index(path_list_index));
308 FIB_PATH_LIST_DBG(path_list, "DB-removed");
312 fib_path_list_destroy (fib_path_list_t *path_list)
314 fib_node_index_t *path_index;
316 FIB_PATH_LIST_DBG(path_list, "destroy");
318 vec_foreach (path_index, path_list->fpl_paths)
320 fib_path_destroy(*path_index);
323 vec_free(path_list->fpl_paths);
324 fib_urpf_list_unlock(path_list->fpl_urpf);
326 fib_node_deinit(&path_list->fpl_node);
327 pool_put(fib_path_list_pool, path_list);
331 fib_path_list_last_lock_gone (fib_node_t *node)
333 fib_path_list_t *path_list;
335 path_list = fib_path_list_from_fib_node(node);
337 FIB_PATH_LIST_DBG(path_list, "last-lock");
339 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
341 fib_path_list_db_remove(fib_path_list_get_index(path_list));
343 fib_path_list_destroy(path_list);
346 static load_balance_flags_t
347 fib_path_list_fwd_flags_2_load_balance (fib_path_list_fwd_flags_t pl_flags)
349 load_balance_flags_t lb_flags = LOAD_BALANCE_FLAG_NONE;
351 if (pl_flags & FIB_PATH_LIST_FWD_FLAG_STICKY)
353 lb_flags |= LOAD_BALANCE_ATTR_STICKY;
361 * update the multipath adj this path-list will contribute to its
362 * children's forwarding.
365 fib_path_list_mk_lb (fib_path_list_t *path_list,
366 fib_forward_chain_type_t fct,
368 fib_path_list_fwd_flags_t flags)
370 load_balance_path_t *nhs;
371 fib_node_index_t *path_index;
376 * We gather the DPOs from resolved paths.
378 vec_foreach (path_index, path_list->fpl_paths)
380 if ((flags & FIB_PATH_LIST_FWD_FLAG_STICKY) ||
381 fib_path_is_resolved(*path_index))
383 nhs = fib_path_append_nh_for_multipath_hash(*path_index,
389 * Path-list load-balances, which if used, would be shared and hence
390 * never need a load-balance map.
394 fib_forw_chain_type_to_dpo_proto(fct),
395 load_balance_create(vec_len(nhs),
396 fib_forw_chain_type_to_dpo_proto(fct),
397 0 /* FIXME FLOW HASH */));
398 load_balance_multipath_update(dpo, nhs,
399 fib_path_list_fwd_flags_2_load_balance(flags));
401 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
407 * @brief [re]build the path list's uRPF list
410 fib_path_list_mk_urpf (fib_path_list_t *path_list)
412 fib_node_index_t *path_index;
415 * ditch the old one. by iterating through all paths we are going
416 * to re-find all the adjs that were in the old one anyway. If we
417 * keep the old one, then the |sort|uniq requires more work.
418 * All users of the RPF list have their own lock, so we can release
421 fib_urpf_list_unlock(path_list->fpl_urpf);
422 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
424 vec_foreach (path_index, path_list->fpl_paths)
426 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
429 fib_urpf_list_bake(path_list->fpl_urpf);
433 * @brief Contribute (add) this path list's uRPF list. This allows the child
434 * to construct an aggregate list.
437 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
440 fib_path_list_t *path_list;
442 path_list = fib_path_list_get(path_list_index);
444 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
448 * @brief Return the the child the RPF list pre-built for this path list
451 fib_path_list_get_urpf (fib_node_index_t path_list_index)
453 fib_path_list_t *path_list;
455 path_list = fib_path_list_get(path_list_index);
457 return (path_list->fpl_urpf);
461 * fib_path_list_back_walk
463 * Called from one of this path-list's paths to progate
467 fib_path_list_back_walk (fib_node_index_t path_list_index,
468 fib_node_back_walk_ctx_t *ctx)
470 fib_path_list_t *path_list;
472 path_list = fib_path_list_get(path_list_index);
474 fib_path_list_mk_urpf(path_list);
476 FIB_PATH_LIST_DBG(path_list, "bw:%U",
477 format_fib_node_bw_reason, ctx->fnbw_reason);
480 * propagate the backwalk further
482 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR)
485 * many children. schedule a async walk
487 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
489 FIB_WALK_PRIORITY_LOW,
495 * only a few children. continue the walk synchronously
497 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
502 * fib_path_list_back_walk_notify
504 * A back walk has reach this path-list.
506 static fib_node_back_walk_rc_t
507 fib_path_list_back_walk_notify (fib_node_t *node,
508 fib_node_back_walk_ctx_t *ctx)
511 * the path-list is not a direct child of any other node type
512 * paths, which do not change thier to-list-mapping, save the
513 * list they are a member of, and invoke the BW function directly.
517 return (FIB_NODE_BACK_WALK_CONTINUE);
521 * Display the path-list memory usage
524 fib_path_list_memory_show (void)
526 fib_show_memory_usage("Path-list",
527 pool_elts(fib_path_list_pool),
528 pool_len(fib_path_list_pool),
529 sizeof(fib_path_list_t));
530 fib_urpf_list_show_mem();
534 * The FIB path-list's graph node virtual function table
536 static const fib_node_vft_t fib_path_list_vft = {
537 .fnv_get = fib_path_list_get_node,
538 .fnv_last_lock = fib_path_list_last_lock_gone,
539 .fnv_back_walk = fib_path_list_back_walk_notify,
540 .fnv_mem_show = fib_path_list_memory_show,
543 static inline fib_path_list_t *
544 fib_path_list_alloc (fib_node_index_t *path_list_index)
546 fib_path_list_t *path_list;
548 pool_get(fib_path_list_pool, path_list);
549 clib_memset(path_list, 0, sizeof(*path_list));
551 fib_node_init(&path_list->fpl_node,
552 FIB_NODE_TYPE_PATH_LIST);
553 path_list->fpl_urpf = INDEX_INVALID;
554 path_list->fpl_paths = NULL;
556 *path_list_index = fib_path_list_get_index(path_list);
558 FIB_PATH_LIST_DBG(path_list, "alloc");
563 static fib_path_list_t *
564 fib_path_list_resolve (fib_path_list_t *path_list)
566 fib_node_index_t *path_index, *paths, path_list_index;
568 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
571 * resolving a path-list is a recursive action. this means more path
572 * lists can be created during this call, and hence this path-list
573 * can be realloc'd. so we work with copies.
574 * this function is called only once per-path list, so its no great overhead.
576 path_list_index = fib_path_list_get_index(path_list);
577 paths = vec_dup(path_list->fpl_paths);
579 vec_foreach (path_index, paths)
581 fib_path_resolve(*path_index);
585 path_list = fib_path_list_get(path_list_index);
587 FIB_PATH_LIST_DBG(path_list, "resovled");
589 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
591 fib_path_list_mk_urpf(path_list);
597 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
599 fib_path_list_t *path_list;
601 if (FIB_NODE_INDEX_INVALID == path_list_index)
606 path_list = fib_path_list_get(path_list_index);
608 return (vec_len(path_list->fpl_paths));
613 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
615 fib_node_index_t *path_index;
616 fib_path_list_t *path_list;
619 path_list = fib_path_list_get(path_list_index);
622 vec_foreach (path_index, path_list->fpl_paths)
624 sw_if_index = fib_path_get_resolving_interface(*path_index);
625 if (~0 != sw_if_index)
627 return (sw_if_index);
631 return (sw_if_index);
635 fib_path_list_get_proto (fib_node_index_t path_list_index)
637 fib_path_list_t *path_list;
639 path_list = fib_path_list_get(path_list_index);
642 * we don't support a mix of path protocols, so we can return the proto
645 return (fib_path_get_proto(path_list->fpl_paths[0]));
649 fib_path_list_is_looped (fib_node_index_t path_list_index)
651 fib_path_list_t *path_list;
653 path_list = fib_path_list_get(path_list_index);
655 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
659 fib_path_list_is_popular (fib_node_index_t path_list_index)
661 fib_path_list_t *path_list;
663 path_list = fib_path_list_get(path_list_index);
665 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR);
668 static fib_path_list_flags_t
669 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
672 * we do no share drop nor exclusive path-lists
674 if (flags & FIB_PATH_LIST_FLAG_DROP ||
675 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
677 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
684 fib_path_list_create (fib_path_list_flags_t flags,
685 const fib_route_path_t *rpaths)
687 fib_node_index_t path_list_index, old_path_list_index;
688 fib_path_list_t *path_list;
691 flags = fib_path_list_flags_fixup(flags);
692 path_list = fib_path_list_alloc(&path_list_index);
693 path_list->fpl_flags = flags;
697 vec_foreach_index(i, rpaths)
699 vec_add1(path_list->fpl_paths,
700 fib_path_create(path_list_index,
704 * we sort the paths since the key for the path-list is
705 * the description of the paths it contains. The paths need to
706 * be sorted else this description will differ.
708 if (vec_len(path_list->fpl_paths) > 1)
710 vec_sort_with_function(path_list->fpl_paths,
711 fib_path_cmp_for_sort);
716 * If a shared path list is requested, consult the DB for a match
718 if (flags & FIB_PATH_LIST_FLAG_SHARED)
721 * check for a matching path-list in the DB.
722 * If we find one then we can return the existing one and destroy the
723 * new one just created.
725 old_path_list_index = fib_path_list_db_find(path_list);
726 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
728 fib_path_list_destroy(path_list);
730 path_list_index = old_path_list_index;
735 * if there was not a matching path-list, then this
736 * new one will need inserting into the DB and resolving.
738 fib_path_list_db_insert(path_list_index);
739 path_list = fib_path_list_resolve(path_list);
745 * no shared path list requested. resolve and use the one
748 path_list = fib_path_list_resolve(path_list);
751 return (path_list_index);
754 static fib_path_cfg_flags_t
755 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
757 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
759 if (plf & FIB_PATH_LIST_FLAG_DROP)
761 pf |= FIB_PATH_CFG_FLAG_DROP;
763 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
765 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
767 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
769 pf |= FIB_PATH_CFG_FLAG_LOCAL;
776 fib_path_list_create_special (dpo_proto_t nh_proto,
777 fib_path_list_flags_t flags,
780 fib_node_index_t path_index, path_list_index;
781 fib_path_list_t *path_list;
783 path_list = fib_path_list_alloc(&path_list_index);
784 path_list->fpl_flags = flags;
787 fib_path_create_special(path_list_index,
789 fib_path_list_flags_2_path_flags(flags),
791 vec_add1(path_list->fpl_paths, path_index);
794 * we don't share path-lists. we can do PIC on them so why bother.
796 path_list = fib_path_list_resolve(path_list);
798 return (path_list_index);
802 * return the index info the path-lists's vector of paths, of the matching path.
806 fib_path_list_find_rpath (fib_node_index_t path_list_index,
807 const fib_route_path_t *rpath)
809 fib_path_list_t *path_list;
812 path_list = fib_path_list_get(path_list_index);
814 vec_foreach_index (ii, path_list->fpl_paths)
816 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
826 * fib_path_list_copy_and_path_add
828 * Create a copy of a path-list and append one more path to it.
829 * The path-list returned could either have been newly created, or
830 * can be a shared path-list from the data-base.
833 fib_path_list_path_add (fib_node_index_t path_list_index,
834 const fib_route_path_t *rpaths)
836 fib_node_index_t new_path_index, *orig_path_index;
837 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(1 == vec_len(rpaths));
846 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
848 FIB_PATH_LIST_DBG(path_list, "path-add");
850 new_path_index = fib_path_create(path_list_index,
853 vec_foreach (orig_path_index, path_list->fpl_paths)
856 * don't add duplicate paths
858 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
860 fib_path_destroy(new_path_index);
861 return (*orig_path_index);
866 * Add the new path - no sort, no sharing, no key..
868 vec_add1(path_list->fpl_paths, new_path_index);
870 FIB_PATH_LIST_DBG(path_list, "path-added");
873 * no shared path list requested. resolve and use the one
876 fib_path_resolve(new_path_index);
878 return (new_path_index);
882 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
883 fib_path_list_flags_t flags,
884 const fib_route_path_t *rpaths)
886 fib_node_index_t path_index, new_path_index, *orig_path_index;
887 fib_path_list_t *path_list, *orig_path_list;
888 fib_node_index_t exist_path_list_index;
889 fib_node_index_t path_list_index;
892 ASSERT(1 == vec_len(rpaths));
895 * alloc the new list before we retrieve the old one, lest
896 * the alloc result in a realloc
898 path_list = fib_path_list_alloc(&path_list_index);
900 orig_path_list = fib_path_list_get(orig_path_list_index);
902 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
904 flags = fib_path_list_flags_fixup(flags);
905 path_list->fpl_flags = flags;
907 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
910 new_path_index = fib_path_create(path_list_index,
913 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
916 * don't add duplicate paths
917 * In the unlikely event the path is a duplicate, then we'll
918 * find a matching path-list later and this one will be toast.
920 if (0 != fib_path_cmp(new_path_index, *orig_path_index))
922 path_index = fib_path_copy(*orig_path_index, path_list_index);
923 path_list->fpl_paths[pi++] = path_index;
927 _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
931 path_list->fpl_paths[pi] = new_path_index;
934 * we sort the paths since the key for the path-list is
935 * the description of the paths it contains. The paths need to
936 * be sorted else this description will differ.
938 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
940 FIB_PATH_LIST_DBG(path_list, "path-added");
943 * check for a matching path-list in the DB.
944 * If we find one then we can return the existing one and destroy the
945 * new one just created.
947 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
949 exist_path_list_index = fib_path_list_db_find(path_list);
950 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
952 fib_path_list_destroy(path_list);
954 path_list_index = exist_path_list_index;
959 * if there was not a matching path-list, then this
960 * new one will need inserting into the DB and resolving.
962 fib_path_list_db_insert(path_list_index);
964 path_list = fib_path_list_resolve(path_list);
970 * no shared path list requested. resolve and use the one
973 path_list = fib_path_list_resolve(path_list);
976 return (path_list_index);
980 * fib_path_list_path_remove
983 fib_path_list_path_remove (fib_node_index_t path_list_index,
984 const fib_route_path_t *rpaths)
986 fib_node_index_t match_path_index, tmp_path_index;
987 fib_path_list_t *path_list;
990 path_list = fib_path_list_get(path_list_index);
992 ASSERT(1 == vec_len(rpaths));
993 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
995 FIB_PATH_LIST_DBG(path_list, "path-remove");
998 * create a representation of the path to be removed, so it
999 * can be used as a comparison object during the copy.
1001 tmp_path_index = fib_path_create(path_list_index,
1003 match_path_index = FIB_NODE_INDEX_INVALID;
1005 vec_foreach_index (pi, path_list->fpl_paths)
1007 if (0 == fib_path_cmp(tmp_path_index,
1008 path_list->fpl_paths[pi]))
1013 match_path_index = path_list->fpl_paths[pi];
1014 fib_path_destroy(match_path_index);
1015 vec_del1(path_list->fpl_paths, pi);
1020 * done with the temporary now
1022 fib_path_destroy(tmp_path_index);
1024 return (match_path_index);
1028 * fib_path_list_copy_and_path_remove
1030 * Copy the path-list excluding the path passed.
1031 * If the path is the last one, then the index reurned will be invalid.
1032 * i.e. the path-list is toast.
1035 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
1036 fib_path_list_flags_t flags,
1037 const fib_route_path_t *rpath)
1039 fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
1040 fib_path_list_t *path_list, *orig_path_list;
1041 fib_node_index_t pi;
1043 path_list = fib_path_list_alloc(&path_list_index);
1045 flags = fib_path_list_flags_fixup(flags);
1046 orig_path_list = fib_path_list_get(orig_path_list_index);
1048 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
1050 path_list->fpl_flags = flags;
1052 * allocate as many paths as we might need in one go, rather than
1053 * using vec_add to do a few at a time.
1055 if (vec_len(orig_path_list->fpl_paths) > 1)
1057 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
1062 * create a representation of the path to be removed, so it
1063 * can be used as a comparison object during the copy.
1065 tmp_path_index = fib_path_create(path_list_index, rpath);
1067 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
1069 if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
1070 path_index = fib_path_copy(*orig_path_index, path_list_index);
1071 if (pi < vec_len(path_list->fpl_paths))
1073 path_list->fpl_paths[pi++] = path_index;
1078 * this is the unlikely case that the path being
1079 * removed does not match one in the path-list, so
1080 * we end up with as many paths as we started with.
1081 * the paths vector was sized above with the expectation
1082 * that we would have 1 less.
1084 vec_add1(path_list->fpl_paths, path_index);
1090 * done with the temporary now
1092 fib_path_destroy(tmp_path_index);
1095 * if there are no paths, then the new path-list is aborted
1097 if (0 == vec_len(path_list->fpl_paths)) {
1098 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1100 fib_path_list_destroy(path_list);
1102 path_list_index = FIB_NODE_INDEX_INVALID;
1105 * we sort the paths since the key for the path-list is
1106 * the description of the paths it contains. The paths need to
1107 * be sorted else this description will differ.
1109 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1112 * If a shared path list is requested, consult the DB for a match
1114 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1116 fib_node_index_t exist_path_list_index;
1119 * check for a matching path-list in the DB.
1120 * If we find one then we can return the existing one and destroy the
1121 * new one just created.
1123 exist_path_list_index = fib_path_list_db_find(path_list);
1124 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1126 fib_path_list_destroy(path_list);
1128 path_list_index = exist_path_list_index;
1133 * if there was not a matching path-list, then this
1134 * new one will need inserting into the DB and resolving.
1136 fib_path_list_db_insert(path_list_index);
1138 path_list = fib_path_list_resolve(path_list);
1144 * no shared path list requested. resolve and use the one
1147 path_list = fib_path_list_resolve(path_list);
1151 return (path_list_index);
1155 * fib_path_list_contribute_forwarding
1157 * Return the index of a load-balance that user of this path-list should
1158 * use for forwarding
1161 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1162 fib_forward_chain_type_t fct,
1163 fib_path_list_fwd_flags_t flags,
1166 fib_path_list_t *path_list;
1168 path_list = fib_path_list_get(path_list_index);
1170 fib_path_list_mk_lb(path_list, fct, dpo, flags);
1172 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
1175 * If there's only one bucket in the load-balance then we can
1178 if ((1 == load_balance_n_buckets(dpo->dpoi_index)) &&
1179 (FIB_PATH_LIST_FWD_FLAG_COLLAPSE & flags))
1181 dpo_copy(dpo, load_balance_get_bucket(dpo->dpoi_index, 0));
1186 * fib_path_list_get_adj
1188 * Return the index of a adjacency for the first path that user of this
1189 * path-list should use for forwarding
1192 fib_path_list_get_adj (fib_node_index_t path_list_index,
1193 fib_forward_chain_type_t type)
1195 fib_path_list_t *path_list;
1197 path_list = fib_path_list_get(path_list_index);
1198 return (fib_path_get_adj(path_list->fpl_paths[0]));
1202 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1203 fib_node_index_t **entry_indicies)
1205 fib_node_index_t *path_index;
1206 int is_looped, list_looped;
1207 fib_path_list_t *path_list;
1210 path_list = fib_path_list_get(path_list_index);
1212 vec_foreach (path_index, path_list->fpl_paths)
1214 fib_node_index_t *copy, **copy_ptr;
1217 * we need a copy of the nodes visited so that when we add entries
1218 * we explore on the nth path and a looped is detected, those entries
1219 * are not again searched for n+1 path and so finding a loop that does
1222 copy = vec_dup(*entry_indicies);
1225 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1226 list_looped += is_looped;
1231 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", list_looped);
1235 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1239 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1242 return (list_looped);
1246 fib_path_list_child_add (fib_node_index_t path_list_index,
1247 fib_node_type_t child_type,
1248 fib_node_index_t child_index)
1252 sibling = fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1257 if (FIB_PATH_LIST_POPULAR == fib_node_get_n_children(FIB_NODE_TYPE_PATH_LIST,
1261 * Set the popular flag on the path-list once we pass the magic
1262 * threshold. then walk children to update.
1263 * We don't undo this action. The rational being that the number
1264 * of entries using this prefix is large enough such that it is a
1265 * non-trival amount of effort to converge them. If we get into the
1266 * situation where we are adding and removing entries such that we
1267 * flip-flop over the threshold, then this non-trivial work is added
1268 * to each of those routes adds/deletes - not a situation we want.
1270 fib_node_back_walk_ctx_t ctx = {
1271 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
1273 fib_path_list_t *path_list;
1275 path_list = fib_path_list_get(path_list_index);
1276 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_POPULAR;
1278 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, &ctx);
1285 fib_path_list_child_remove (fib_node_index_t path_list_index,
1288 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1294 fib_path_list_lock(fib_node_index_t path_list_index)
1296 fib_path_list_t *path_list;
1298 if (FIB_NODE_INDEX_INVALID != path_list_index)
1300 path_list = fib_path_list_get(path_list_index);
1302 fib_node_lock(&path_list->fpl_node);
1307 fib_path_list_unlock (fib_node_index_t path_list_index)
1309 fib_path_list_t *path_list;
1311 if (FIB_NODE_INDEX_INVALID != path_list_index)
1313 path_list = fib_path_list_get(path_list_index);
1315 fib_node_unlock(&path_list->fpl_node);
1320 fib_path_list_pool_size (void)
1322 return (pool_elts(fib_path_list_pool));
1326 fib_path_list_db_size (void)
1328 return (hash_elts(fib_path_list_db));
1332 fib_path_list_walk (fib_node_index_t path_list_index,
1333 fib_path_list_walk_fn_t func,
1336 fib_node_index_t *path_index;
1337 fib_path_list_t *path_list;
1339 path_list = fib_path_list_get(path_list_index);
1341 vec_foreach(path_index, path_list->fpl_paths)
1343 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1351 fib_path_list_walk_w_ext (fib_node_index_t path_list_index,
1352 const fib_path_ext_list_t *ext_list,
1353 fib_path_list_walk_w_ext_fn_t func,
1356 fib_node_index_t *path_index;
1357 fib_path_list_t *path_list;
1358 fib_path_ext_t *path_ext;
1360 path_list = fib_path_list_get(path_list_index);
1362 vec_foreach(path_index, path_list->fpl_paths)
1364 path_ext = fib_path_ext_list_find_by_path_index(ext_list, *path_index);
1366 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1375 fib_path_list_module_init (void)
1377 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1379 fib_path_list_db = hash_create2 (/* elts */ 0,
1381 /* value_bytes */ sizeof (fib_node_index_t),
1382 fib_path_list_db_hash_key_sum,
1383 fib_path_list_db_hash_key_equal,
1384 /* format pair/arg */
1386 fib_path_list_logger = vlib_log_register_class("fib", "path-list");
1389 static clib_error_t *
1390 show_fib_path_list_command (vlib_main_t * vm,
1391 unformat_input_t * input,
1392 vlib_cli_command_t * cmd)
1394 fib_path_list_t *path_list;
1395 fib_node_index_t pli;
1397 if (unformat (input, "%d", &pli))
1400 * show one in detail
1402 if (!pool_is_free_index(fib_path_list_pool, pli))
1404 path_list = fib_path_list_get(pli);
1405 u8 *s = fib_path_list_format(pli, NULL);
1406 s = format(s, "children:");
1407 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1408 vlib_cli_output (vm, "%s", s);
1413 vlib_cli_output (vm, "path list %d invalid", pli);
1421 vlib_cli_output (vm, "FIB Path Lists");
1422 pool_foreach_index (pli, fib_path_list_pool,
1424 vlib_cli_output (vm, "%U", format_fib_path_list, pli, 0);
1430 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1431 .path = "show fib path-lists",
1432 .function = show_fib_path_list_command,
1433 .short_help = "show fib path-lists",