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 convergnece 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;
67 * Hash table of paths. valid only with INDEXED flag
73 * Array of strings/names for the FIB sources
75 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
78 * The memory pool from which we allocate all the path-lists
80 static fib_path_list_t * fib_path_list_pool;
83 * The data-base of shared path-lists
85 static uword *fib_path_list_db;
90 vlib_log_class_t fib_path_list_logger;
95 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \
97 vlib_log_debug(fib_path_list_logger, \
99 format_fib_path_list, \
100 fib_path_list_get_index(_pl), 0, \
104 static fib_path_list_t *
105 fib_path_list_get (fib_node_index_t index)
107 return (pool_elt_at_index(fib_path_list_pool, index));
111 fib_path_list_get_node (fib_node_index_t index)
113 return ((fib_node_t*)fib_path_list_get(index));
116 static fib_path_list_t*
117 fib_path_list_from_fib_node (fib_node_t *node)
119 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
120 return ((fib_path_list_t*)node);
123 static fib_node_index_t
124 fib_path_list_get_index (fib_path_list_t *path_list)
126 return (path_list - fib_path_list_pool);
130 format_fib_path_list (u8 * s, va_list * args)
132 fib_node_index_t *path_index, path_list_index;
133 fib_path_list_attribute_t attr;
134 fib_path_list_t *path_list;
137 path_list_index = va_arg (*args, fib_node_index_t);
138 indent = va_arg (*args, u32);
139 path_list = fib_path_list_get(path_list_index);
141 s = format (s, "%Upath-list:[%d]",
142 format_white_space, indent,
143 fib_path_list_get_index(path_list));
144 s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
146 if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
148 s = format (s, " flags:");
149 FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
151 if ((1<<attr) & path_list->fpl_flags)
153 s = format (s, "%s,", fib_path_list_attr_names[attr]);
157 s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);
159 vec_foreach (path_index, path_list->fpl_paths)
161 s = format(s, "%U", format_fib_path, *path_index, indent+2,
162 FIB_PATH_FORMAT_FLAGS_NONE);
170 fib_path_list_format (fib_node_index_t path_list_index,
173 return (format(s, "%U", format_fib_path_list, path_list_index, 4));
177 fib_path_list_hash (fib_path_list_t *path_list)
179 uword old_path_list_hash, new_path_list_hash, path_hash;
180 fib_node_index_t *path_index;
184 new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths);
186 vec_foreach (path_index, path_list->fpl_paths)
188 path_hash = fib_path_hash(*path_index);
190 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
192 hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
196 return (new_path_list_hash);
200 fib_path_list_db_hash_key_from_index (uword index)
206 fib_path_list_db_hash_key_is_index (uword key)
212 fib_path_list_db_hash_key_2_index (uword key)
214 ASSERT (fib_path_list_db_hash_key_is_index (key));
218 static fib_path_list_t*
219 fib_path_list_db_get_from_hash_key (uword key)
221 fib_path_list_t *path_list;
223 if (fib_path_list_db_hash_key_is_index (key))
225 fib_node_index_t path_list_index;
227 path_list_index = fib_path_list_db_hash_key_2_index(key);
228 path_list = fib_path_list_get(path_list_index);
232 path_list = uword_to_pointer (key, fib_path_list_t *);
239 fib_path_list_db_hash_key_sum (hash_t * h,
242 fib_path_list_t *path_list;
244 path_list = fib_path_list_db_get_from_hash_key(key);
246 return (fib_path_list_hash(path_list));
250 fib_path_list_db_hash_key_equal (hash_t * h,
254 fib_path_list_t *path_list1, *path_list2;
256 path_list1 = fib_path_list_db_get_from_hash_key(key1);
257 path_list2 = fib_path_list_db_get_from_hash_key(key2);
259 return (fib_path_list_hash(path_list1) ==
260 fib_path_list_hash(path_list2));
263 static fib_node_index_t
264 fib_path_list_db_find (fib_path_list_t *path_list)
268 p = hash_get(fib_path_list_db, path_list);
275 return (FIB_NODE_INDEX_INVALID);
279 fib_path_list_db_insert (fib_node_index_t path_list_index)
281 fib_path_list_t *path_list;
283 path_list = fib_path_list_get(path_list_index);
285 ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
287 hash_set (fib_path_list_db,
288 fib_path_list_db_hash_key_from_index(path_list_index),
291 FIB_PATH_LIST_DBG(path_list, "DB-inserted");
295 fib_path_list_db_remove (fib_node_index_t path_list_index)
297 fib_path_list_t *path_list;
299 path_list = fib_path_list_get(path_list_index);
301 ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
303 hash_unset(fib_path_list_db,
304 fib_path_list_db_hash_key_from_index(path_list_index));
306 FIB_PATH_LIST_DBG(path_list, "DB-removed");
310 fib_path_list_destroy (fib_path_list_t *path_list)
312 fib_node_index_t *path_index;
314 FIB_PATH_LIST_DBG(path_list, "destroy");
316 vec_foreach (path_index, path_list->fpl_paths)
318 fib_path_destroy(*path_index);
321 vec_free(path_list->fpl_paths);
322 fib_urpf_list_unlock(path_list->fpl_urpf);
324 fib_node_deinit(&path_list->fpl_node);
325 pool_put(fib_path_list_pool, path_list);
329 fib_path_list_last_lock_gone (fib_node_t *node)
331 fib_path_list_t *path_list;
333 path_list = fib_path_list_from_fib_node(node);
335 FIB_PATH_LIST_DBG(path_list, "last-lock");
337 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
339 fib_path_list_db_remove(fib_path_list_get_index(path_list));
341 fib_path_list_destroy(path_list);
344 static load_balance_flags_t
345 fib_path_list_fwd_flags_2_load_balance (fib_path_list_fwd_flags_t pl_flags)
347 load_balance_flags_t lb_flags = LOAD_BALANCE_FLAG_NONE;
349 if (pl_flags & FIB_PATH_LIST_FWD_FLAG_STICKY)
351 lb_flags |= LOAD_BALANCE_ATTR_STICKY;
359 * update the multipath adj this path-list will contribute to its
360 * children's forwarding.
363 fib_path_list_mk_lb (fib_path_list_t *path_list,
364 fib_forward_chain_type_t fct,
366 fib_path_list_fwd_flags_t flags)
368 fib_node_index_t *path_index;
369 load_balance_path_t *nhs;
373 dproto = fib_forw_chain_type_to_dpo_proto(fct);
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.
395 load_balance_create(vec_len(nhs),
397 fib_table_get_default_flow_hash_config(
398 dpo_proto_to_fib(dproto))));
399 load_balance_multipath_update(dpo, nhs,
400 fib_path_list_fwd_flags_2_load_balance(flags));
402 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
408 * @brief [re]build the path list's uRPF list
411 fib_path_list_mk_urpf (fib_path_list_t *path_list)
413 fib_node_index_t *path_index;
416 * ditch the old one. by iterating through all paths we are going
417 * to re-find all the adjs that were in the old one anyway. If we
418 * keep the old one, then the |sort|uniq requires more work.
419 * All users of the RPF list have their own lock, so we can release
422 fib_urpf_list_unlock(path_list->fpl_urpf);
423 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
425 vec_foreach (path_index, path_list->fpl_paths)
427 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
430 fib_urpf_list_bake(path_list->fpl_urpf);
434 * @brief Contribute (add) this path list's uRPF list. This allows the child
435 * to construct an aggregate list.
438 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
441 fib_path_list_t *path_list;
443 path_list = fib_path_list_get(path_list_index);
445 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
449 * @brief Return the the child the RPF list pre-built for this path list
452 fib_path_list_get_urpf (fib_node_index_t path_list_index)
454 fib_path_list_t *path_list;
456 path_list = fib_path_list_get(path_list_index);
458 return (path_list->fpl_urpf);
462 * fib_path_list_back_walk
464 * Called from one of this path-list's paths to progate
468 fib_path_list_back_walk (fib_node_index_t path_list_index,
469 fib_node_back_walk_ctx_t *ctx)
471 fib_path_list_t *path_list;
473 path_list = fib_path_list_get(path_list_index);
475 fib_path_list_mk_urpf(path_list);
477 FIB_PATH_LIST_DBG(path_list, "bw:%U",
478 format_fib_node_bw_reason, ctx->fnbw_reason);
481 * propagate the backwalk further
483 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR)
486 * many children. schedule a async walk
488 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
490 FIB_WALK_PRIORITY_LOW,
496 * only a few children. continue the walk synchronously
498 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
503 * fib_path_list_back_walk_notify
505 * A back walk has reach this path-list.
507 static fib_node_back_walk_rc_t
508 fib_path_list_back_walk_notify (fib_node_t *node,
509 fib_node_back_walk_ctx_t *ctx)
512 * the path-list is not a direct child of any other node type
513 * paths, which do not change thier to-list-mapping, save the
514 * list they are a member of, and invoke the BW function directly.
518 return (FIB_NODE_BACK_WALK_CONTINUE);
522 * Display the path-list memory usage
525 fib_path_list_memory_show (void)
527 fib_show_memory_usage("Path-list",
528 pool_elts(fib_path_list_pool),
529 pool_len(fib_path_list_pool),
530 sizeof(fib_path_list_t));
531 fib_urpf_list_show_mem();
535 * The FIB path-list's graph node virtual function table
537 static const fib_node_vft_t fib_path_list_vft = {
538 .fnv_get = fib_path_list_get_node,
539 .fnv_last_lock = fib_path_list_last_lock_gone,
540 .fnv_back_walk = fib_path_list_back_walk_notify,
541 .fnv_mem_show = fib_path_list_memory_show,
544 static inline fib_path_list_t *
545 fib_path_list_alloc (fib_node_index_t *path_list_index)
547 fib_path_list_t *path_list;
549 pool_get(fib_path_list_pool, path_list);
550 clib_memset(path_list, 0, sizeof(*path_list));
552 fib_node_init(&path_list->fpl_node,
553 FIB_NODE_TYPE_PATH_LIST);
554 path_list->fpl_urpf = INDEX_INVALID;
555 path_list->fpl_paths = NULL;
557 *path_list_index = fib_path_list_get_index(path_list);
559 FIB_PATH_LIST_DBG(path_list, "alloc");
564 static fib_path_list_t *
565 fib_path_list_resolve (fib_path_list_t *path_list)
567 fib_node_index_t *path_index, *paths, path_list_index;
569 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
572 * resolving a path-list is a recursive action. this means more path
573 * lists can be created during this call, and hence this path-list
574 * can be realloc'd. so we work with copies.
575 * this function is called only once per-path list, so its no great overhead.
577 path_list_index = fib_path_list_get_index(path_list);
578 paths = vec_dup(path_list->fpl_paths);
580 vec_foreach (path_index, paths)
582 fib_path_resolve(*path_index);
586 path_list = fib_path_list_get(path_list_index);
588 FIB_PATH_LIST_DBG(path_list, "resovled");
590 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
592 fib_path_list_mk_urpf(path_list);
598 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
600 fib_path_list_t *path_list;
602 if (FIB_NODE_INDEX_INVALID == path_list_index)
607 path_list = fib_path_list_get(path_list_index);
609 return (vec_len(path_list->fpl_paths));
614 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
616 fib_node_index_t *path_index;
617 fib_path_list_t *path_list;
620 path_list = fib_path_list_get(path_list_index);
623 vec_foreach (path_index, path_list->fpl_paths)
625 sw_if_index = fib_path_get_resolving_interface(*path_index);
626 if (~0 != sw_if_index)
628 return (sw_if_index);
632 return (sw_if_index);
636 fib_path_list_get_proto (fib_node_index_t path_list_index)
638 fib_path_list_t *path_list;
640 path_list = fib_path_list_get(path_list_index);
643 * we don't support a mix of path protocols, so we can return the proto
646 return (fib_path_get_proto(path_list->fpl_paths[0]));
650 fib_path_list_is_looped (fib_node_index_t path_list_index)
652 fib_path_list_t *path_list;
654 path_list = fib_path_list_get(path_list_index);
656 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
660 fib_path_list_is_popular (fib_node_index_t path_list_index)
662 fib_path_list_t *path_list;
664 path_list = fib_path_list_get(path_list_index);
666 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR);
669 static fib_path_list_flags_t
670 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
673 * we do no share drop nor exclusive path-lists
675 if (flags & FIB_PATH_LIST_FLAG_DROP ||
676 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
678 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
685 fib_path_list_create (fib_path_list_flags_t flags,
686 const fib_route_path_t *rpaths)
688 fib_node_index_t path_list_index, old_path_list_index;
689 fib_path_list_t *path_list;
692 flags = fib_path_list_flags_fixup(flags);
693 path_list = fib_path_list_alloc(&path_list_index);
694 path_list->fpl_flags = flags;
698 vec_foreach_index(i, rpaths)
700 vec_add1(path_list->fpl_paths,
701 fib_path_create(path_list_index,
705 * we sort the paths since the key for the path-list is
706 * the description of the paths it contains. The paths need to
707 * be sorted else this description will differ.
709 if (vec_len(path_list->fpl_paths) > 1)
711 vec_sort_with_function(path_list->fpl_paths,
712 fib_path_cmp_for_sort);
717 * If a shared path list is requested, consult the DB for a match
719 if (flags & FIB_PATH_LIST_FLAG_SHARED)
722 * check for a matching path-list in the DB.
723 * If we find one then we can return the existing one and destroy the
724 * new one just created.
726 old_path_list_index = fib_path_list_db_find(path_list);
727 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
729 fib_path_list_destroy(path_list);
731 path_list_index = old_path_list_index;
736 * if there was not a matching path-list, then this
737 * new one will need inserting into the DB and resolving.
739 fib_path_list_db_insert(path_list_index);
740 path_list = fib_path_list_resolve(path_list);
746 * no shared path list requested. resolve and use the one
749 path_list = fib_path_list_resolve(path_list);
752 return (path_list_index);
755 static fib_path_cfg_flags_t
756 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
758 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
760 if (plf & FIB_PATH_LIST_FLAG_DROP)
762 pf |= FIB_PATH_CFG_FLAG_DROP;
764 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
766 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
768 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
770 pf |= FIB_PATH_CFG_FLAG_LOCAL;
777 fib_path_list_create_special (dpo_proto_t nh_proto,
778 fib_path_list_flags_t flags,
781 fib_node_index_t path_index, path_list_index;
782 fib_path_list_t *path_list;
784 path_list = fib_path_list_alloc(&path_list_index);
785 path_list->fpl_flags = flags;
788 fib_path_create_special(path_list_index,
790 fib_path_list_flags_2_path_flags(flags),
792 vec_add1(path_list->fpl_paths, path_index);
795 * we don't share path-lists. we can do PIC on them so why bother.
797 path_list = fib_path_list_resolve(path_list);
799 return (path_list_index);
803 * return the index info the path-lists's vector of paths, of the matching path.
807 fib_path_list_find_rpath (fib_node_index_t path_list_index,
808 const fib_route_path_t *rpath)
810 fib_path_list_t *path_list;
813 path_list = fib_path_list_get(path_list_index);
815 vec_foreach_index (ii, path_list->fpl_paths)
817 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
827 * fib_path_list_copy_and_path_add
829 * Create a copy of a path-list and append one more path to it.
830 * The path-list returned could either have been newly created, or
831 * can be a shared path-list from the data-base.
834 fib_path_list_path_add (fib_node_index_t path_list_index,
835 const fib_route_path_t *rpaths)
837 fib_node_index_t new_path_index, *orig_path_index;
838 fib_path_list_t *path_list;
841 * alloc the new list before we retrieve the old one, lest
842 * the alloc result in a realloc
844 path_list = fib_path_list_get(path_list_index);
846 ASSERT(1 == vec_len(rpaths));
847 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
849 FIB_PATH_LIST_DBG(path_list, "path-add");
851 new_path_index = fib_path_create(path_list_index,
854 vec_foreach (orig_path_index, path_list->fpl_paths)
857 * don't add duplicate paths
859 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
861 fib_path_destroy(new_path_index);
862 return (*orig_path_index);
867 * Add the new path - no sort, no sharing, no key..
869 vec_add1(path_list->fpl_paths, new_path_index);
871 FIB_PATH_LIST_DBG(path_list, "path-added");
874 * no shared path list requested. resolve and use the one
877 fib_path_resolve(new_path_index);
879 return (new_path_index);
883 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
884 fib_path_list_flags_t flags,
885 const fib_route_path_t *rpaths)
887 fib_node_index_t path_index, new_path_index, *orig_path_index;
888 fib_path_list_t *path_list, *orig_path_list;
889 fib_node_index_t exist_path_list_index;
890 fib_node_index_t path_list_index;
893 ASSERT(1 == vec_len(rpaths));
896 * alloc the new list before we retrieve the old one, lest
897 * the alloc result in a realloc
899 path_list = fib_path_list_alloc(&path_list_index);
901 orig_path_list = fib_path_list_get(orig_path_list_index);
903 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
905 flags = fib_path_list_flags_fixup(flags);
906 path_list->fpl_flags = flags;
908 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
911 new_path_index = fib_path_create(path_list_index,
914 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
917 * don't add duplicate paths
918 * In the unlikely event the path is a duplicate, then we'll
919 * find a matching path-list later and this one will be toast.
921 if (0 != fib_path_cmp(new_path_index, *orig_path_index))
923 path_index = fib_path_copy(*orig_path_index, path_list_index);
924 path_list->fpl_paths[pi++] = path_index;
928 _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
932 path_list->fpl_paths[pi] = new_path_index;
935 * we sort the paths since the key for the path-list is
936 * the description of the paths it contains. The paths need to
937 * be sorted else this description will differ.
939 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
941 FIB_PATH_LIST_DBG(path_list, "path-added");
944 * check for a matching path-list in the DB.
945 * If we find one then we can return the existing one and destroy the
946 * new one just created.
948 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
950 exist_path_list_index = fib_path_list_db_find(path_list);
951 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
953 fib_path_list_destroy(path_list);
955 path_list_index = exist_path_list_index;
960 * if there was not a matching path-list, then this
961 * new one will need inserting into the DB and resolving.
963 fib_path_list_db_insert(path_list_index);
965 path_list = fib_path_list_resolve(path_list);
971 * no shared path list requested. resolve and use the one
974 path_list = fib_path_list_resolve(path_list);
977 return (path_list_index);
981 * fib_path_list_path_remove
984 fib_path_list_path_remove (fib_node_index_t path_list_index,
985 const fib_route_path_t *rpaths)
987 fib_node_index_t match_path_index, tmp_path_index;
988 fib_path_list_t *path_list;
991 path_list = fib_path_list_get(path_list_index);
993 ASSERT(1 == vec_len(rpaths));
994 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
996 FIB_PATH_LIST_DBG(path_list, "path-remove");
999 * create a representation of the path to be removed, so it
1000 * can be used as a comparison object during the copy.
1002 tmp_path_index = fib_path_create(path_list_index,
1004 match_path_index = FIB_NODE_INDEX_INVALID;
1006 vec_foreach_index (pi, path_list->fpl_paths)
1008 if (0 == fib_path_cmp(tmp_path_index,
1009 path_list->fpl_paths[pi]))
1014 match_path_index = path_list->fpl_paths[pi];
1015 fib_path_destroy(match_path_index);
1016 vec_del1(path_list->fpl_paths, pi);
1021 * done with the temporary now
1023 fib_path_destroy(tmp_path_index);
1025 return (match_path_index);
1029 * fib_path_list_copy_and_path_remove
1031 * Copy the path-list excluding the path passed.
1032 * If the path is the last one, then the index reurned will be invalid.
1033 * i.e. the path-list is toast.
1036 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
1037 fib_path_list_flags_t flags,
1038 const fib_route_path_t *rpath)
1040 fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
1041 fib_path_list_t *path_list, *orig_path_list;
1042 fib_node_index_t pi;
1044 path_list = fib_path_list_alloc(&path_list_index);
1046 flags = fib_path_list_flags_fixup(flags);
1047 orig_path_list = fib_path_list_get(orig_path_list_index);
1049 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
1051 path_list->fpl_flags = flags;
1053 * allocate as many paths as we might need in one go, rather than
1054 * using vec_add to do a few at a time.
1056 if (vec_len(orig_path_list->fpl_paths) > 1)
1058 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
1063 * create a representation of the path to be removed, so it
1064 * can be used as a comparison object during the copy.
1066 tmp_path_index = fib_path_create(path_list_index, rpath);
1068 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
1070 if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
1071 path_index = fib_path_copy(*orig_path_index, path_list_index);
1072 if (pi < vec_len(path_list->fpl_paths))
1074 path_list->fpl_paths[pi++] = path_index;
1079 * this is the unlikely case that the path being
1080 * removed does not match one in the path-list, so
1081 * we end up with as many paths as we started with.
1082 * the paths vector was sized above with the expectation
1083 * that we would have 1 less.
1085 vec_add1(path_list->fpl_paths, path_index);
1091 * done with the temporary now
1093 fib_path_destroy(tmp_path_index);
1096 * if there are no paths, then the new path-list is aborted
1098 if (0 == vec_len(path_list->fpl_paths)) {
1099 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1101 fib_path_list_destroy(path_list);
1103 path_list_index = FIB_NODE_INDEX_INVALID;
1106 * we sort the paths since the key for the path-list is
1107 * the description of the paths it contains. The paths need to
1108 * be sorted else this description will differ.
1110 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1113 * If a shared path list is requested, consult the DB for a match
1115 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1117 fib_node_index_t exist_path_list_index;
1120 * check for a matching path-list in the DB.
1121 * If we find one then we can return the existing one and destroy the
1122 * new one just created.
1124 exist_path_list_index = fib_path_list_db_find(path_list);
1125 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1127 fib_path_list_destroy(path_list);
1129 path_list_index = exist_path_list_index;
1134 * if there was not a matching path-list, then this
1135 * new one will need inserting into the DB and resolving.
1137 fib_path_list_db_insert(path_list_index);
1139 path_list = fib_path_list_resolve(path_list);
1145 * no shared path list requested. resolve and use the one
1148 path_list = fib_path_list_resolve(path_list);
1152 return (path_list_index);
1156 * fib_path_list_contribute_forwarding
1158 * Return the index of a load-balance that user of this path-list should
1159 * use for forwarding
1162 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1163 fib_forward_chain_type_t fct,
1164 fib_path_list_fwd_flags_t flags,
1167 fib_path_list_t *path_list;
1169 path_list = fib_path_list_get(path_list_index);
1171 fib_path_list_mk_lb(path_list, fct, dpo, flags);
1173 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
1176 * If there's only one bucket in the load-balance then we can
1179 if ((1 == load_balance_n_buckets(dpo->dpoi_index)) &&
1180 (FIB_PATH_LIST_FWD_FLAG_COLLAPSE & flags))
1182 dpo_copy(dpo, load_balance_get_bucket(dpo->dpoi_index, 0));
1187 * fib_path_list_get_adj
1189 * Return the index of a adjacency for the first path that user of this
1190 * path-list should use for forwarding
1193 fib_path_list_get_adj (fib_node_index_t path_list_index,
1194 fib_forward_chain_type_t type)
1196 fib_path_list_t *path_list;
1198 path_list = fib_path_list_get(path_list_index);
1199 return (fib_path_get_adj(path_list->fpl_paths[0]));
1203 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1204 fib_node_index_t **entry_indicies)
1206 fib_node_index_t *path_index;
1207 int is_looped, list_looped;
1208 fib_path_list_t *path_list;
1211 path_list = fib_path_list_get(path_list_index);
1213 vec_foreach (path_index, path_list->fpl_paths)
1215 fib_node_index_t *copy, **copy_ptr;
1218 * we need a copy of the nodes visited so that when we add entries
1219 * we explore on the nth path and a looped is detected, those entries
1220 * are not again searched for n+1 path and so finding a loop that does
1223 copy = vec_dup(*entry_indicies);
1226 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1227 list_looped += is_looped;
1232 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", list_looped);
1236 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1240 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1243 return (list_looped);
1247 fib_path_list_child_add (fib_node_index_t path_list_index,
1248 fib_node_type_t child_type,
1249 fib_node_index_t child_index)
1253 sibling = fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1258 if (FIB_PATH_LIST_POPULAR == fib_node_get_n_children(FIB_NODE_TYPE_PATH_LIST,
1262 * Set the popular flag on the path-list once we pass the magic
1263 * threshold. then walk children to update.
1264 * We don't undo this action. The rational being that the number
1265 * of entries using this prefix is large enough such that it is a
1266 * non-trival amount of effort to converge them. If we get into the
1267 * situation where we are adding and removing entries such that we
1268 * flip-flop over the threshold, then this non-trivial work is added
1269 * to each of those routes adds/deletes - not a situation we want.
1271 fib_node_back_walk_ctx_t ctx = {
1272 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
1274 fib_path_list_t *path_list;
1276 path_list = fib_path_list_get(path_list_index);
1277 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_POPULAR;
1279 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, &ctx);
1286 fib_path_list_child_remove (fib_node_index_t path_list_index,
1289 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1295 fib_path_list_lock(fib_node_index_t path_list_index)
1297 fib_path_list_t *path_list;
1299 if (FIB_NODE_INDEX_INVALID != path_list_index)
1301 path_list = fib_path_list_get(path_list_index);
1303 fib_node_lock(&path_list->fpl_node);
1308 fib_path_list_unlock (fib_node_index_t path_list_index)
1310 fib_path_list_t *path_list;
1312 if (FIB_NODE_INDEX_INVALID != path_list_index)
1314 path_list = fib_path_list_get(path_list_index);
1316 fib_node_unlock(&path_list->fpl_node);
1321 fib_path_list_pool_size (void)
1323 return (pool_elts(fib_path_list_pool));
1327 fib_path_list_db_size (void)
1329 return (hash_elts(fib_path_list_db));
1333 fib_path_list_walk (fib_node_index_t path_list_index,
1334 fib_path_list_walk_fn_t func,
1337 fib_node_index_t *path_index;
1338 fib_path_list_t *path_list;
1340 path_list = fib_path_list_get(path_list_index);
1342 vec_foreach(path_index, path_list->fpl_paths)
1344 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1352 fib_path_list_walk_w_ext (fib_node_index_t path_list_index,
1353 const fib_path_ext_list_t *ext_list,
1354 fib_path_list_walk_w_ext_fn_t func,
1357 fib_node_index_t *path_index;
1358 fib_path_list_t *path_list;
1359 fib_path_ext_t *path_ext;
1361 path_list = fib_path_list_get(path_list_index);
1363 vec_foreach(path_index, path_list->fpl_paths)
1365 path_ext = fib_path_ext_list_find_by_path_index(ext_list, *path_index);
1367 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1376 fib_path_list_module_init (void)
1378 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1380 fib_path_list_db = hash_create2 (/* elts */ 0,
1382 /* value_bytes */ sizeof (fib_node_index_t),
1383 fib_path_list_db_hash_key_sum,
1384 fib_path_list_db_hash_key_equal,
1385 /* format pair/arg */
1387 fib_path_list_logger = vlib_log_register_class("fib", "path-list");
1390 static clib_error_t *
1391 show_fib_path_list_command (vlib_main_t * vm,
1392 unformat_input_t * input,
1393 vlib_cli_command_t * cmd)
1395 fib_path_list_t *path_list;
1396 fib_node_index_t pli;
1398 if (unformat (input, "%d", &pli))
1401 * show one in detail
1403 if (!pool_is_free_index(fib_path_list_pool, pli))
1405 path_list = fib_path_list_get(pli);
1406 u8 *s = fib_path_list_format(pli, NULL);
1407 s = format(s, "children:");
1408 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1409 vlib_cli_output (vm, "%s", s);
1414 vlib_cli_output (vm, "path list %d invalid", pli);
1422 vlib_cli_output (vm, "FIB Path Lists");
1423 pool_foreach_index (pli, fib_path_list_pool,
1425 vlib_cli_output (vm, "%U", format_fib_path_list, pli, 0);
1431 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1432 .path = "show fib path-lists",
1433 .function = show_fib_path_list_command,
1434 .short_help = "show fib path-lists",