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 entires 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;
183 new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths);
185 vec_foreach (path_index, path_list->fpl_paths)
187 path_hash = fib_path_hash(*path_index);
189 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
191 hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
195 return (new_path_list_hash);
199 fib_path_list_db_hash_key_from_index (uword index)
205 fib_path_list_db_hash_key_is_index (uword key)
211 fib_path_list_db_hash_key_2_index (uword key)
213 ASSERT (fib_path_list_db_hash_key_is_index (key));
217 static fib_path_list_t*
218 fib_path_list_db_get_from_hash_key (uword key)
220 fib_path_list_t *path_list;
222 if (fib_path_list_db_hash_key_is_index (key))
224 fib_node_index_t path_list_index;
226 path_list_index = fib_path_list_db_hash_key_2_index(key);
227 path_list = fib_path_list_get(path_list_index);
231 path_list = uword_to_pointer (key, fib_path_list_t *);
238 fib_path_list_db_hash_key_sum (hash_t * h,
241 fib_path_list_t *path_list;
243 path_list = fib_path_list_db_get_from_hash_key(key);
245 return (fib_path_list_hash(path_list));
249 fib_path_list_db_hash_key_equal (hash_t * h,
253 fib_path_list_t *path_list1, *path_list2;
255 path_list1 = fib_path_list_db_get_from_hash_key(key1);
256 path_list2 = fib_path_list_db_get_from_hash_key(key2);
258 return (fib_path_list_hash(path_list1) ==
259 fib_path_list_hash(path_list2));
262 static fib_node_index_t
263 fib_path_list_db_find (fib_path_list_t *path_list)
267 p = hash_get(fib_path_list_db, path_list);
274 return (FIB_NODE_INDEX_INVALID);
278 fib_path_list_db_insert (fib_node_index_t path_list_index)
280 fib_path_list_t *path_list;
282 path_list = fib_path_list_get(path_list_index);
284 ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
286 hash_set (fib_path_list_db,
287 fib_path_list_db_hash_key_from_index(path_list_index),
290 FIB_PATH_LIST_DBG(path_list, "DB-inserted");
294 fib_path_list_db_remove (fib_node_index_t path_list_index)
296 fib_path_list_t *path_list;
298 path_list = fib_path_list_get(path_list_index);
300 ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
302 hash_unset(fib_path_list_db,
303 fib_path_list_db_hash_key_from_index(path_list_index));
305 FIB_PATH_LIST_DBG(path_list, "DB-removed");
309 fib_path_list_destroy (fib_path_list_t *path_list)
311 fib_node_index_t *path_index;
313 FIB_PATH_LIST_DBG(path_list, "destroy");
315 vec_foreach (path_index, path_list->fpl_paths)
317 fib_path_destroy(*path_index);
320 vec_free(path_list->fpl_paths);
321 fib_urpf_list_unlock(path_list->fpl_urpf);
323 fib_node_deinit(&path_list->fpl_node);
324 pool_put(fib_path_list_pool, path_list);
328 fib_path_list_last_lock_gone (fib_node_t *node)
330 fib_path_list_t *path_list;
332 path_list = fib_path_list_from_fib_node(node);
334 FIB_PATH_LIST_DBG(path_list, "last-lock");
336 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
338 fib_path_list_db_remove(fib_path_list_get_index(path_list));
340 fib_path_list_destroy(path_list);
343 static load_balance_flags_t
344 fib_path_list_fwd_flags_2_load_balance (fib_path_list_fwd_flags_t pl_flags)
346 load_balance_flags_t lb_flags = LOAD_BALANCE_FLAG_NONE;
348 if (pl_flags & FIB_PATH_LIST_FWD_FLAG_STICKY)
350 lb_flags |= LOAD_BALANCE_ATTR_STICKY;
358 * update the multipath adj this path-list will contribute to its
359 * children's forwarding.
362 fib_path_list_mk_lb (fib_path_list_t *path_list,
363 fib_forward_chain_type_t fct,
365 fib_path_list_fwd_flags_t flags)
367 load_balance_path_t *nhs;
368 fib_node_index_t *path_index;
373 * We gather the DPOs from resolved paths.
375 vec_foreach (path_index, path_list->fpl_paths)
377 if ((flags & FIB_PATH_LIST_FWD_FLAG_STICKY) ||
378 fib_path_is_resolved(*path_index))
380 nhs = fib_path_append_nh_for_multipath_hash(*path_index,
386 * Path-list load-balances, which if used, would be shared and hence
387 * never need a load-balance map.
391 fib_forw_chain_type_to_dpo_proto(fct),
392 load_balance_create(vec_len(nhs),
393 fib_forw_chain_type_to_dpo_proto(fct),
394 0 /* FIXME FLOW HASH */));
395 load_balance_multipath_update(dpo, nhs,
396 fib_path_list_fwd_flags_2_load_balance(flags));
398 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
404 * @brief [re]build the path list's uRPF list
407 fib_path_list_mk_urpf (fib_path_list_t *path_list)
409 fib_node_index_t *path_index;
412 * ditch the old one. by iterating through all paths we are going
413 * to re-find all the adjs that were in the old one anyway. If we
414 * keep the old one, then the |sort|uniq requires more work.
415 * All users of the RPF list have their own lock, so we can release
418 fib_urpf_list_unlock(path_list->fpl_urpf);
419 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
421 vec_foreach (path_index, path_list->fpl_paths)
423 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
426 fib_urpf_list_bake(path_list->fpl_urpf);
430 * @brief Contribute (add) this path list's uRPF list. This allows the child
431 * to construct an aggregate list.
434 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
437 fib_path_list_t *path_list;
439 path_list = fib_path_list_get(path_list_index);
441 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
445 * @brief Return the the child the RPF list pre-built for this path list
448 fib_path_list_get_urpf (fib_node_index_t path_list_index)
450 fib_path_list_t *path_list;
452 path_list = fib_path_list_get(path_list_index);
454 return (path_list->fpl_urpf);
458 * fib_path_list_back_walk
460 * Called from one of this path-list's paths to progate
464 fib_path_list_back_walk (fib_node_index_t path_list_index,
465 fib_node_back_walk_ctx_t *ctx)
467 fib_path_list_t *path_list;
469 path_list = fib_path_list_get(path_list_index);
471 fib_path_list_mk_urpf(path_list);
473 FIB_PATH_LIST_DBG(path_list, "bw:%U",
474 format_fib_node_bw_reason, ctx->fnbw_reason);
477 * propagate the backwalk further
479 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR)
482 * many children. schedule a async walk
484 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
486 FIB_WALK_PRIORITY_LOW,
492 * only a few children. continue the walk synchronously
494 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
499 * fib_path_list_back_walk_notify
501 * A back walk has reach this path-list.
503 static fib_node_back_walk_rc_t
504 fib_path_list_back_walk_notify (fib_node_t *node,
505 fib_node_back_walk_ctx_t *ctx)
508 * the path-list is not a direct child of any other node type
509 * paths, which do not change thier to-list-mapping, save the
510 * list they are a member of, and invoke the BW function directly.
514 return (FIB_NODE_BACK_WALK_CONTINUE);
518 * Display the path-list memory usage
521 fib_path_list_memory_show (void)
523 fib_show_memory_usage("Path-list",
524 pool_elts(fib_path_list_pool),
525 pool_len(fib_path_list_pool),
526 sizeof(fib_path_list_t));
527 fib_urpf_list_show_mem();
531 * The FIB path-list's graph node virtual function table
533 static const fib_node_vft_t fib_path_list_vft = {
534 .fnv_get = fib_path_list_get_node,
535 .fnv_last_lock = fib_path_list_last_lock_gone,
536 .fnv_back_walk = fib_path_list_back_walk_notify,
537 .fnv_mem_show = fib_path_list_memory_show,
540 static inline fib_path_list_t *
541 fib_path_list_alloc (fib_node_index_t *path_list_index)
543 fib_path_list_t *path_list;
545 pool_get(fib_path_list_pool, path_list);
546 clib_memset(path_list, 0, sizeof(*path_list));
548 fib_node_init(&path_list->fpl_node,
549 FIB_NODE_TYPE_PATH_LIST);
550 path_list->fpl_urpf = INDEX_INVALID;
551 path_list->fpl_paths = NULL;
553 *path_list_index = fib_path_list_get_index(path_list);
555 FIB_PATH_LIST_DBG(path_list, "alloc");
560 static fib_path_list_t *
561 fib_path_list_resolve (fib_path_list_t *path_list)
563 fib_node_index_t *path_index, *paths, path_list_index;
565 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
568 * resolving a path-list is a recursive action. this means more path
569 * lists can be created during this call, and hence this path-list
570 * can be realloc'd. so we work with copies.
571 * this function is called only once per-path list, so its no great overhead.
573 path_list_index = fib_path_list_get_index(path_list);
574 paths = vec_dup(path_list->fpl_paths);
576 vec_foreach (path_index, paths)
578 fib_path_resolve(*path_index);
582 path_list = fib_path_list_get(path_list_index);
584 FIB_PATH_LIST_DBG(path_list, "resovled");
586 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
588 fib_path_list_mk_urpf(path_list);
594 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
596 fib_path_list_t *path_list;
598 if (FIB_NODE_INDEX_INVALID == path_list_index)
603 path_list = fib_path_list_get(path_list_index);
605 return (vec_len(path_list->fpl_paths));
610 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
612 fib_node_index_t *path_index;
613 fib_path_list_t *path_list;
616 path_list = fib_path_list_get(path_list_index);
619 vec_foreach (path_index, path_list->fpl_paths)
621 sw_if_index = fib_path_get_resolving_interface(*path_index);
622 if (~0 != sw_if_index)
624 return (sw_if_index);
628 return (sw_if_index);
632 fib_path_list_get_proto (fib_node_index_t path_list_index)
634 fib_path_list_t *path_list;
636 path_list = fib_path_list_get(path_list_index);
639 * we don't support a mix of path protocols, so we can return the proto
642 return (fib_path_get_proto(path_list->fpl_paths[0]));
646 fib_path_list_is_looped (fib_node_index_t path_list_index)
648 fib_path_list_t *path_list;
650 path_list = fib_path_list_get(path_list_index);
652 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
656 fib_path_list_is_popular (fib_node_index_t path_list_index)
658 fib_path_list_t *path_list;
660 path_list = fib_path_list_get(path_list_index);
662 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR);
665 static fib_path_list_flags_t
666 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
669 * we do no share drop nor exclusive path-lists
671 if (flags & FIB_PATH_LIST_FLAG_DROP ||
672 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
674 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
681 fib_path_list_create (fib_path_list_flags_t flags,
682 const fib_route_path_t *rpaths)
684 fib_node_index_t path_list_index, old_path_list_index;
685 fib_path_list_t *path_list;
688 flags = fib_path_list_flags_fixup(flags);
689 path_list = fib_path_list_alloc(&path_list_index);
690 path_list->fpl_flags = flags;
694 vec_foreach_index(i, rpaths)
696 vec_add1(path_list->fpl_paths,
697 fib_path_create(path_list_index,
701 * we sort the paths since the key for the path-list is
702 * the description of the paths it contains. The paths need to
703 * be sorted else this description will differ.
705 if (vec_len(path_list->fpl_paths) > 1)
707 vec_sort_with_function(path_list->fpl_paths,
708 fib_path_cmp_for_sort);
713 * If a shared path list is requested, consult the DB for a match
715 if (flags & FIB_PATH_LIST_FLAG_SHARED)
718 * check for a matching path-list in the DB.
719 * If we find one then we can return the existing one and destroy the
720 * new one just created.
722 old_path_list_index = fib_path_list_db_find(path_list);
723 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
725 fib_path_list_destroy(path_list);
727 path_list_index = old_path_list_index;
732 * if there was not a matching path-list, then this
733 * new one will need inserting into the DB and resolving.
735 fib_path_list_db_insert(path_list_index);
736 path_list = fib_path_list_resolve(path_list);
742 * no shared path list requested. resolve and use the one
745 path_list = fib_path_list_resolve(path_list);
748 return (path_list_index);
751 static fib_path_cfg_flags_t
752 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
754 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
756 if (plf & FIB_PATH_LIST_FLAG_DROP)
758 pf |= FIB_PATH_CFG_FLAG_DROP;
760 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
762 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
764 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
766 pf |= FIB_PATH_CFG_FLAG_LOCAL;
773 fib_path_list_create_special (dpo_proto_t nh_proto,
774 fib_path_list_flags_t flags,
777 fib_node_index_t path_index, path_list_index;
778 fib_path_list_t *path_list;
780 path_list = fib_path_list_alloc(&path_list_index);
781 path_list->fpl_flags = flags;
784 fib_path_create_special(path_list_index,
786 fib_path_list_flags_2_path_flags(flags),
788 vec_add1(path_list->fpl_paths, path_index);
791 * we don't share path-lists. we can do PIC on them so why bother.
793 path_list = fib_path_list_resolve(path_list);
795 return (path_list_index);
799 * return the index info the path-lists's vector of paths, of the matching path.
803 fib_path_list_find_rpath (fib_node_index_t path_list_index,
804 const fib_route_path_t *rpath)
806 fib_path_list_t *path_list;
809 path_list = fib_path_list_get(path_list_index);
811 vec_foreach_index (ii, path_list->fpl_paths)
813 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
823 * fib_path_list_copy_and_path_add
825 * Create a copy of a path-list and append one more path to it.
826 * The path-list returned could either have been newly created, or
827 * can be a shared path-list from the data-base.
830 fib_path_list_path_add (fib_node_index_t path_list_index,
831 const fib_route_path_t *rpaths)
833 fib_node_index_t new_path_index, *orig_path_index;
834 fib_path_list_t *path_list;
837 * alloc the new list before we retrieve the old one, lest
838 * the alloc result in a realloc
840 path_list = fib_path_list_get(path_list_index);
842 ASSERT(1 == vec_len(rpaths));
843 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
845 FIB_PATH_LIST_DBG(path_list, "path-add");
847 new_path_index = fib_path_create(path_list_index,
850 vec_foreach (orig_path_index, path_list->fpl_paths)
853 * don't add duplicate paths
855 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
857 fib_path_destroy(new_path_index);
858 return (*orig_path_index);
863 * Add the new path - no sort, no sharing, no key..
865 vec_add1(path_list->fpl_paths, new_path_index);
867 FIB_PATH_LIST_DBG(path_list, "path-added");
870 * no shared path list requested. resolve and use the one
873 fib_path_resolve(new_path_index);
875 return (new_path_index);
879 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
880 fib_path_list_flags_t flags,
881 const fib_route_path_t *rpaths)
883 fib_node_index_t path_index, new_path_index, *orig_path_index;
884 fib_path_list_t *path_list, *orig_path_list;
885 fib_node_index_t exist_path_list_index;
886 fib_node_index_t path_list_index;
889 ASSERT(1 == vec_len(rpaths));
892 * alloc the new list before we retrieve the old one, lest
893 * the alloc result in a realloc
895 path_list = fib_path_list_alloc(&path_list_index);
897 orig_path_list = fib_path_list_get(orig_path_list_index);
899 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
901 flags = fib_path_list_flags_fixup(flags);
902 path_list->fpl_flags = flags;
904 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
907 new_path_index = fib_path_create(path_list_index,
910 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
913 * don't add duplicate paths
914 * In the unlikely event the path is a duplicate, then we'll
915 * find a matching path-list later and this one will be toast.
917 if (0 != fib_path_cmp(new_path_index, *orig_path_index))
919 path_index = fib_path_copy(*orig_path_index, path_list_index);
920 path_list->fpl_paths[pi++] = path_index;
924 _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
928 path_list->fpl_paths[pi] = new_path_index;
931 * we sort the paths since the key for the path-list is
932 * the description of the paths it contains. The paths need to
933 * be sorted else this description will differ.
935 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
937 FIB_PATH_LIST_DBG(path_list, "path-added");
940 * check for a matching path-list in the DB.
941 * If we find one then we can return the existing one and destroy the
942 * new one just created.
944 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
946 exist_path_list_index = fib_path_list_db_find(path_list);
947 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
949 fib_path_list_destroy(path_list);
951 path_list_index = exist_path_list_index;
956 * if there was not a matching path-list, then this
957 * new one will need inserting into the DB and resolving.
959 fib_path_list_db_insert(path_list_index);
961 path_list = fib_path_list_resolve(path_list);
967 * no shared path list requested. resolve and use the one
970 path_list = fib_path_list_resolve(path_list);
973 return (path_list_index);
977 * fib_path_list_path_remove
980 fib_path_list_path_remove (fib_node_index_t path_list_index,
981 const fib_route_path_t *rpaths)
983 fib_node_index_t match_path_index, tmp_path_index;
984 fib_path_list_t *path_list;
987 path_list = fib_path_list_get(path_list_index);
989 ASSERT(1 == vec_len(rpaths));
990 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
992 FIB_PATH_LIST_DBG(path_list, "path-remove");
995 * create a representation of the path to be removed, so it
996 * can be used as a comparison object during the copy.
998 tmp_path_index = fib_path_create(path_list_index,
1000 match_path_index = FIB_NODE_INDEX_INVALID;
1002 vec_foreach_index (pi, path_list->fpl_paths)
1004 if (0 == fib_path_cmp(tmp_path_index,
1005 path_list->fpl_paths[pi]))
1010 match_path_index = path_list->fpl_paths[pi];
1011 fib_path_destroy(match_path_index);
1012 vec_del1(path_list->fpl_paths, pi);
1017 * done with the temporary now
1019 fib_path_destroy(tmp_path_index);
1021 return (match_path_index);
1025 * fib_path_list_copy_and_path_remove
1027 * Copy the path-list excluding the path passed.
1028 * If the path is the last one, then the index reurned will be invalid.
1029 * i.e. the path-list is toast.
1032 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
1033 fib_path_list_flags_t flags,
1034 const fib_route_path_t *rpath)
1036 fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
1037 fib_path_list_t *path_list, *orig_path_list;
1038 fib_node_index_t pi;
1040 path_list = fib_path_list_alloc(&path_list_index);
1042 flags = fib_path_list_flags_fixup(flags);
1043 orig_path_list = fib_path_list_get(orig_path_list_index);
1045 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
1047 path_list->fpl_flags = flags;
1049 * allocate as many paths as we might need in one go, rather than
1050 * using vec_add to do a few at a time.
1052 if (vec_len(orig_path_list->fpl_paths) > 1)
1054 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
1059 * create a representation of the path to be removed, so it
1060 * can be used as a comparison object during the copy.
1062 tmp_path_index = fib_path_create(path_list_index, rpath);
1064 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
1066 if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
1067 path_index = fib_path_copy(*orig_path_index, path_list_index);
1068 if (pi < vec_len(path_list->fpl_paths))
1070 path_list->fpl_paths[pi++] = path_index;
1075 * this is the unlikely case that the path being
1076 * removed does not match one in the path-list, so
1077 * we end up with as many paths as we started with.
1078 * the paths vector was sized above with the expectation
1079 * that we would have 1 less.
1081 vec_add1(path_list->fpl_paths, path_index);
1087 * done with the temporary now
1089 fib_path_destroy(tmp_path_index);
1092 * if there are no paths, then the new path-list is aborted
1094 if (0 == vec_len(path_list->fpl_paths)) {
1095 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1097 fib_path_list_destroy(path_list);
1099 path_list_index = FIB_NODE_INDEX_INVALID;
1102 * we sort the paths since the key for the path-list is
1103 * the description of the paths it contains. The paths need to
1104 * be sorted else this description will differ.
1106 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1109 * If a shared path list is requested, consult the DB for a match
1111 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1113 fib_node_index_t exist_path_list_index;
1116 * check for a matching path-list in the DB.
1117 * If we find one then we can return the existing one and destroy the
1118 * new one just created.
1120 exist_path_list_index = fib_path_list_db_find(path_list);
1121 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1123 fib_path_list_destroy(path_list);
1125 path_list_index = exist_path_list_index;
1130 * if there was not a matching path-list, then this
1131 * new one will need inserting into the DB and resolving.
1133 fib_path_list_db_insert(path_list_index);
1135 path_list = fib_path_list_resolve(path_list);
1141 * no shared path list requested. resolve and use the one
1144 path_list = fib_path_list_resolve(path_list);
1148 return (path_list_index);
1152 * fib_path_list_contribute_forwarding
1154 * Return the index of a load-balance that user of this path-list should
1155 * use for forwarding
1158 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1159 fib_forward_chain_type_t fct,
1160 fib_path_list_fwd_flags_t flags,
1163 fib_path_list_t *path_list;
1165 path_list = fib_path_list_get(path_list_index);
1167 fib_path_list_mk_lb(path_list, fct, dpo, flags);
1169 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
1172 * If there's only one bucket in the load-balance then we can
1175 if ((1 == load_balance_n_buckets(dpo->dpoi_index)) &&
1176 (FIB_PATH_LIST_FWD_FLAG_COLLAPSE & flags))
1178 dpo_copy(dpo, load_balance_get_bucket(dpo->dpoi_index, 0));
1183 * fib_path_list_get_adj
1185 * Return the index of a adjacency for the first path that user of this
1186 * path-list should use for forwarding
1189 fib_path_list_get_adj (fib_node_index_t path_list_index,
1190 fib_forward_chain_type_t type)
1192 fib_path_list_t *path_list;
1194 path_list = fib_path_list_get(path_list_index);
1195 return (fib_path_get_adj(path_list->fpl_paths[0]));
1199 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1200 fib_node_index_t **entry_indicies)
1202 fib_node_index_t *path_index;
1203 int is_looped, list_looped;
1204 fib_path_list_t *path_list;
1207 path_list = fib_path_list_get(path_list_index);
1209 vec_foreach (path_index, path_list->fpl_paths)
1211 fib_node_index_t *copy, **copy_ptr;
1214 * we need a copy of the nodes visited so that when we add entries
1215 * we explore on the nth path and a looped is detected, those entries
1216 * are not again searched for n+1 path and so finding a loop that does
1219 copy = vec_dup(*entry_indicies);
1222 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1223 list_looped += is_looped;
1228 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", list_looped);
1232 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1236 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1239 return (list_looped);
1243 fib_path_list_child_add (fib_node_index_t path_list_index,
1244 fib_node_type_t child_type,
1245 fib_node_index_t child_index)
1249 sibling = fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1254 if (FIB_PATH_LIST_POPULAR == fib_node_get_n_children(FIB_NODE_TYPE_PATH_LIST,
1258 * Set the popular flag on the path-list once we pass the magic
1259 * threshold. then walk children to update.
1260 * We don't undo this action. The rational being that the number
1261 * of entries using this prefix is large enough such that it is a
1262 * non-trival amount of effort to converge them. If we get into the
1263 * situation where we are adding and removing entries such that we
1264 * flip-flop over the threshold, then this non-trivial work is added
1265 * to each of those routes adds/deletes - not a situation we want.
1267 fib_node_back_walk_ctx_t ctx = {
1268 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
1270 fib_path_list_t *path_list;
1272 path_list = fib_path_list_get(path_list_index);
1273 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_POPULAR;
1275 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, &ctx);
1282 fib_path_list_child_remove (fib_node_index_t path_list_index,
1285 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1291 fib_path_list_lock(fib_node_index_t path_list_index)
1293 fib_path_list_t *path_list;
1295 if (FIB_NODE_INDEX_INVALID != path_list_index)
1297 path_list = fib_path_list_get(path_list_index);
1299 fib_node_lock(&path_list->fpl_node);
1304 fib_path_list_unlock (fib_node_index_t path_list_index)
1306 fib_path_list_t *path_list;
1308 if (FIB_NODE_INDEX_INVALID != path_list_index)
1310 path_list = fib_path_list_get(path_list_index);
1312 fib_node_unlock(&path_list->fpl_node);
1317 fib_path_list_pool_size (void)
1319 return (pool_elts(fib_path_list_pool));
1323 fib_path_list_db_size (void)
1325 return (hash_elts(fib_path_list_db));
1329 fib_path_list_walk (fib_node_index_t path_list_index,
1330 fib_path_list_walk_fn_t func,
1333 fib_node_index_t *path_index;
1334 fib_path_list_t *path_list;
1336 path_list = fib_path_list_get(path_list_index);
1338 vec_foreach(path_index, path_list->fpl_paths)
1340 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1348 fib_path_list_walk_w_ext (fib_node_index_t path_list_index,
1349 const fib_path_ext_list_t *ext_list,
1350 fib_path_list_walk_w_ext_fn_t func,
1353 fib_node_index_t *path_index;
1354 fib_path_list_t *path_list;
1355 fib_path_ext_t *path_ext;
1357 path_list = fib_path_list_get(path_list_index);
1359 vec_foreach(path_index, path_list->fpl_paths)
1361 path_ext = fib_path_ext_list_find_by_path_index(ext_list, *path_index);
1363 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1372 fib_path_list_module_init (void)
1374 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1376 fib_path_list_db = hash_create2 (/* elts */ 0,
1378 /* value_bytes */ sizeof (fib_node_index_t),
1379 fib_path_list_db_hash_key_sum,
1380 fib_path_list_db_hash_key_equal,
1381 /* format pair/arg */
1383 fib_path_list_logger = vlib_log_register_class("fib", "path-list");
1386 static clib_error_t *
1387 show_fib_path_list_command (vlib_main_t * vm,
1388 unformat_input_t * input,
1389 vlib_cli_command_t * cmd)
1391 fib_path_list_t *path_list;
1392 fib_node_index_t pli;
1394 if (unformat (input, "%d", &pli))
1397 * show one in detail
1399 if (!pool_is_free_index(fib_path_list_pool, pli))
1401 path_list = fib_path_list_get(pli);
1402 u8 *s = fib_path_list_format(pli, NULL);
1403 s = format(s, "children:");
1404 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1405 vlib_cli_output (vm, "%s", s);
1410 vlib_cli_output (vm, "path list %d invalid", pli);
1418 vlib_cli_output (vm, "FIB Path Lists");
1419 pool_foreach_index (pli, fib_path_list_pool,
1421 vlib_cli_output (vm, "%U", format_fib_path_list, pli, 0);
1427 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1428 .path = "show fib path-lists",
1429 .function = show_fib_path_list_command,
1430 .short_help = "show fib path-lists",