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>
29 * The magic number of child entries that make a path-list popular.
30 * There's a trade-off here between convergnece and forwarding speed.
31 * Popular path-lists generate load-balance maps for the entires that
32 * use them. If the map is present there is a switch path cost to indirect
33 * through the map - this indirection provides the fast convergence - so
34 * without the map convergence is slower.
36 #define FIB_PATH_LIST_POPULAR 64
40 * A representation of the list/set of path trough which a prefix is reachable
42 typedef struct fib_path_list_t_ {
44 * A path-list is a node in the FIB graph.
49 * Flags on the path-list
51 fib_path_list_flags_t fpl_flags;
54 * Vector of paths indicies for all configured paths.
55 * For shareable path-lists this list MUST not change.
57 fib_node_index_t *fpl_paths;
60 * the RPF list calculated for this path list
62 fib_node_index_t fpl_urpf;
65 * Hash table of paths. valid only with INDEXED flag
71 * Array of strings/names for the FIB sources
73 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
76 * The memory pool from which we allocate all the path-lists
78 static fib_path_list_t * fib_path_list_pool;
81 * The data-base of shared path-lists
83 static uword *fib_path_list_db;
89 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \
92 _tmp = fib_path_list_format( \
93 fib_path_list_get_index(_pl), _tmp); \
94 clib_warning("pl:[%d:%p:%p:%s]:" _fmt, \
95 fib_path_list_get_index(_pl), \
96 _pl, _pl->fpl_paths, _tmp, \
101 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)
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)
120 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
122 return ((fib_path_list_t*)node);
125 static fib_node_index_t
126 fib_path_list_get_index (fib_path_list_t *path_list)
128 return (path_list - fib_path_list_pool);
132 format_fib_path_list (u8 * s, va_list * args)
134 fib_path_list_attribute_t attr;
135 fib_node_index_t *path_index;
136 fib_path_list_t *path_list;
138 path_list = va_arg (*args, fib_path_list_t *);
140 s = format (s, " index:%u", fib_path_list_get_index(path_list));
141 s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
143 if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
145 s = format (s, " flags:");
146 FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
148 if ((1<<attr) & path_list->fpl_flags)
150 s = format (s, "%s,", fib_path_list_attr_names[attr]);
154 s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);
156 vec_foreach (path_index, path_list->fpl_paths)
158 s = fib_path_format(*path_index, s);
166 fib_path_list_format (fib_node_index_t path_list_index,
169 fib_path_list_t *path_list;
171 path_list = fib_path_list_get(path_list_index);
173 return (format(s, "%U", format_fib_path_list, path_list));
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);
347 * update the multipath adj this path-list will contribute to its
348 * children's forwarding.
351 fib_path_list_mk_lb (fib_path_list_t *path_list,
352 fib_forward_chain_type_t fct,
355 load_balance_path_t *nhs;
356 fib_node_index_t *path_index;
360 if (!dpo_id_is_valid(dpo))
367 fib_forw_chain_type_to_dpo_proto(fct),
368 load_balance_create(0,
369 fib_forw_chain_type_to_dpo_proto(fct),
370 0 /* FIXME FLOW HASH */));
374 * We gather the DPOs from resolved paths.
376 vec_foreach (path_index, path_list->fpl_paths)
378 nhs = fib_path_append_nh_for_multipath_hash(*path_index,
384 * Path-list load-balances, which if used, would be shared and hence
385 * never need a load-balance map.
387 load_balance_multipath_update(dpo, nhs, LOAD_BALANCE_FLAG_NONE);
389 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
395 * @brief [re]build the path list's uRPF list
398 fib_path_list_mk_urpf (fib_path_list_t *path_list)
400 fib_node_index_t *path_index;
403 * ditch the old one. by iterating through all paths we are going
404 * to re-find all the adjs that were in the old one anyway. If we
405 * keep the old one, then the |sort|uniq requires more work.
406 * All users of the RPF list have their own lock, so we can release
409 fib_urpf_list_unlock(path_list->fpl_urpf);
410 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
412 vec_foreach (path_index, path_list->fpl_paths)
414 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
417 fib_urpf_list_bake(path_list->fpl_urpf);
421 * @brief Contribute (add) this path list's uRPF list. This allows the child
422 * to construct an aggregate list.
425 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
428 fib_path_list_t *path_list;
430 path_list = fib_path_list_get(path_list_index);
432 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
436 * @brief Return the the child the RPF list pre-built for this path list
439 fib_path_list_get_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 return (path_list->fpl_urpf);
449 * fib_path_list_back_walk
451 * Called from one of this path-list's paths to progate
455 fib_path_list_back_walk (fib_node_index_t path_list_index,
456 fib_node_back_walk_ctx_t *ctx)
458 fib_path_list_t *path_list;
460 path_list = fib_path_list_get(path_list_index);
462 fib_path_list_mk_urpf(path_list);
465 * propagate the backwalk further
467 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR)
470 * many children. schedule a async walk
472 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
474 FIB_WALK_PRIORITY_LOW,
480 * only a few children. continue the walk synchronously
482 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
487 * fib_path_list_back_walk_notify
489 * A back walk has reach this path-list.
491 static fib_node_back_walk_rc_t
492 fib_path_list_back_walk_notify (fib_node_t *node,
493 fib_node_back_walk_ctx_t *ctx)
496 * the path-list is not a direct child of any other node type
497 * paths, which do not change thier to-list-mapping, save the
498 * list they are a member of, and invoke the BW function directly.
502 return (FIB_NODE_BACK_WALK_CONTINUE);
506 * Display the path-list memory usage
509 fib_path_list_memory_show (void)
511 fib_show_memory_usage("Path-list",
512 pool_elts(fib_path_list_pool),
513 pool_len(fib_path_list_pool),
514 sizeof(fib_path_list_t));
515 fib_urpf_list_show_mem();
519 * The FIB path-list's graph node virtual function table
521 static const fib_node_vft_t fib_path_list_vft = {
522 .fnv_get = fib_path_list_get_node,
523 .fnv_last_lock = fib_path_list_last_lock_gone,
524 .fnv_back_walk = fib_path_list_back_walk_notify,
525 .fnv_mem_show = fib_path_list_memory_show,
528 static inline fib_path_list_t *
529 fib_path_list_alloc (fib_node_index_t *path_list_index)
531 fib_path_list_t *path_list;
533 pool_get(fib_path_list_pool, path_list);
534 memset(path_list, 0, sizeof(*path_list));
536 fib_node_init(&path_list->fpl_node,
537 FIB_NODE_TYPE_PATH_LIST);
538 path_list->fpl_urpf = INDEX_INVALID;
539 path_list->fpl_paths = NULL;
541 *path_list_index = fib_path_list_get_index(path_list);
543 FIB_PATH_LIST_DBG(path_list, "alloc");
548 static fib_path_list_t *
549 fib_path_list_resolve (fib_path_list_t *path_list)
551 fib_node_index_t *path_index, *paths, path_list_index;
553 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
556 * resolving a path-list is a recursive action. this means more path
557 * lists can be created during this call, and hence this path-list
558 * can be realloc'd. so we work with copies.
559 * this function is called only once per-path list, so its no great overhead.
561 path_list_index = fib_path_list_get_index(path_list);
562 paths = vec_dup(path_list->fpl_paths);
564 vec_foreach (path_index, paths)
566 fib_path_resolve(*path_index);
570 path_list = fib_path_list_get(path_list_index);
572 FIB_PATH_LIST_DBG(path_list, "resovled");
574 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
576 fib_path_list_mk_urpf(path_list);
582 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
584 fib_path_list_t *path_list;
586 path_list = fib_path_list_get(path_list_index);
588 return (vec_len(path_list->fpl_paths));
593 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
595 fib_node_index_t *path_index;
596 fib_path_list_t *path_list;
599 path_list = fib_path_list_get(path_list_index);
602 vec_foreach (path_index, path_list->fpl_paths)
604 sw_if_index = fib_path_get_resolving_interface(*path_index);
605 if (~0 != sw_if_index)
607 return (sw_if_index);
611 return (sw_if_index);
615 fib_path_list_get_proto (fib_node_index_t path_list_index)
617 fib_path_list_t *path_list;
619 path_list = fib_path_list_get(path_list_index);
622 * we don't support a mix of path protocols, so we can return the proto
625 return (fib_path_get_proto(path_list->fpl_paths[0]));
629 fib_path_list_is_looped (fib_node_index_t path_list_index)
631 fib_path_list_t *path_list;
633 path_list = fib_path_list_get(path_list_index);
635 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
639 fib_path_list_is_popular (fib_node_index_t path_list_index)
641 fib_path_list_t *path_list;
643 path_list = fib_path_list_get(path_list_index);
645 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_POPULAR);
648 static fib_path_list_flags_t
649 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
652 * we do no share drop nor exclusive path-lists
654 if (flags & FIB_PATH_LIST_FLAG_DROP ||
655 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
657 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
664 fib_path_list_create (fib_path_list_flags_t flags,
665 const fib_route_path_t *rpaths)
667 fib_node_index_t path_list_index, old_path_list_index;
668 fib_path_list_t *path_list;
671 flags = fib_path_list_flags_fixup(flags);
672 path_list = fib_path_list_alloc(&path_list_index);
673 path_list->fpl_flags = flags;
677 vec_foreach_index(i, rpaths)
679 vec_add1(path_list->fpl_paths,
680 fib_path_create(path_list_index,
684 * we sort the paths since the key for the path-list is
685 * the description of the paths it contains. The paths need to
686 * be sorted else this description will differ.
688 if (vec_len(path_list->fpl_paths) > 1)
690 vec_sort_with_function(path_list->fpl_paths,
691 fib_path_cmp_for_sort);
696 * If a shared path list is requested, consult the DB for a match
698 if (flags & FIB_PATH_LIST_FLAG_SHARED)
701 * check for a matching path-list in the DB.
702 * If we find one then we can return the existing one and destroy the
703 * new one just created.
705 old_path_list_index = fib_path_list_db_find(path_list);
706 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
708 fib_path_list_destroy(path_list);
710 path_list_index = old_path_list_index;
715 * if there was not a matching path-list, then this
716 * new one will need inserting into the DB and resolving.
718 fib_path_list_db_insert(path_list_index);
719 path_list = fib_path_list_resolve(path_list);
725 * no shared path list requested. resolve and use the one
728 path_list = fib_path_list_resolve(path_list);
731 return (path_list_index);
734 static fib_path_cfg_flags_t
735 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
737 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
739 if (plf & FIB_PATH_LIST_FLAG_DROP)
741 pf |= FIB_PATH_CFG_FLAG_DROP;
743 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
745 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
747 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
749 pf |= FIB_PATH_CFG_FLAG_LOCAL;
756 fib_path_list_create_special (fib_protocol_t nh_proto,
757 fib_path_list_flags_t flags,
760 fib_node_index_t path_index, path_list_index;
761 fib_path_list_t *path_list;
763 path_list = fib_path_list_alloc(&path_list_index);
764 path_list->fpl_flags = flags;
767 fib_path_create_special(path_list_index,
769 fib_path_list_flags_2_path_flags(flags),
771 vec_add1(path_list->fpl_paths, path_index);
774 * we don't share path-lists. we can do PIC on them so why bother.
776 path_list = fib_path_list_resolve(path_list);
778 return (path_list_index);
782 * return the index info the path-lists's vector of paths, of the matching path.
786 fib_path_list_find_rpath (fib_node_index_t path_list_index,
787 const fib_route_path_t *rpath)
789 fib_path_list_t *path_list;
792 path_list = fib_path_list_get(path_list_index);
794 vec_foreach_index (ii, path_list->fpl_paths)
796 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
806 * fib_path_list_copy_and_path_add
808 * Create a copy of a path-list and append one more path to it.
809 * The path-list returned could either have been newly created, or
810 * can be a shared path-list from the data-base.
813 fib_path_list_path_add (fib_node_index_t path_list_index,
814 const fib_route_path_t *rpaths)
816 fib_node_index_t new_path_index, *orig_path_index;
817 fib_path_list_t *path_list;
820 * alloc the new list before we retrieve the old one, lest
821 * the alloc result in a realloc
823 path_list = fib_path_list_get(path_list_index);
825 ASSERT(1 == vec_len(rpaths));
826 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
828 FIB_PATH_LIST_DBG(orig_path_list, "path-add");
830 new_path_index = fib_path_create(path_list_index,
833 vec_foreach (orig_path_index, path_list->fpl_paths)
836 * don't add duplicate paths
838 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
840 fib_path_destroy(new_path_index);
841 return (*orig_path_index);
846 * Add the new path - no sort, no sharing, no key..
848 vec_add1(path_list->fpl_paths, new_path_index);
850 FIB_PATH_LIST_DBG(path_list, "path-added");
853 * no shared path list requested. resolve and use the one
856 fib_path_resolve(new_path_index);
858 return (new_path_index);
862 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
863 fib_path_list_flags_t flags,
864 const fib_route_path_t *rpaths)
866 fib_node_index_t path_index, new_path_index, *orig_path_index;
867 fib_path_list_t *path_list, *orig_path_list;
868 fib_node_index_t exist_path_list_index;
869 fib_node_index_t path_list_index;
872 ASSERT(1 == vec_len(rpaths));
875 * alloc the new list before we retrieve the old one, lest
876 * the alloc result in a realloc
878 path_list = fib_path_list_alloc(&path_list_index);
880 orig_path_list = fib_path_list_get(orig_path_list_index);
882 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
884 flags = fib_path_list_flags_fixup(flags);
885 path_list->fpl_flags = flags;
887 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
890 new_path_index = fib_path_create(path_list_index,
893 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
896 * don't add duplicate paths
897 * In the unlikely event the path is a duplicate, then we'll
898 * find a matching path-list later and this one will be toast.
900 if (0 != fib_path_cmp(new_path_index, *orig_path_index))
902 path_index = fib_path_copy(*orig_path_index, path_list_index);
903 path_list->fpl_paths[pi++] = path_index;
907 _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
911 path_list->fpl_paths[pi] = new_path_index;
914 * we sort the paths since the key for the path-list is
915 * the description of the paths it contains. The paths need to
916 * be sorted else this description will differ.
918 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
920 FIB_PATH_LIST_DBG(path_list, "path-added");
923 * check for a matching path-list in the DB.
924 * If we find one then we can return the existing one and destroy the
925 * new one just created.
927 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
929 exist_path_list_index = fib_path_list_db_find(path_list);
930 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
932 fib_path_list_destroy(path_list);
934 path_list_index = exist_path_list_index;
939 * if there was not a matching path-list, then this
940 * new one will need inserting into the DB and resolving.
942 fib_path_list_db_insert(path_list_index);
944 path_list = fib_path_list_resolve(path_list);
950 * no shared path list requested. resolve and use the one
953 path_list = fib_path_list_resolve(path_list);
956 return (path_list_index);
960 * fib_path_list_path_remove
963 fib_path_list_path_remove (fib_node_index_t path_list_index,
964 const fib_route_path_t *rpaths)
966 fib_node_index_t match_path_index, tmp_path_index;
967 fib_path_list_t *path_list;
970 path_list = fib_path_list_get(path_list_index);
972 ASSERT(1 == vec_len(rpaths));
973 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
975 FIB_PATH_LIST_DBG(orig_path_list, "path-remove");
978 * create a representation of the path to be removed, so it
979 * can be used as a comparison object during the copy.
981 tmp_path_index = fib_path_create(path_list_index,
983 match_path_index = FIB_NODE_INDEX_INVALID;
985 vec_foreach_index (pi, path_list->fpl_paths)
987 if (0 == fib_path_cmp(tmp_path_index,
988 path_list->fpl_paths[pi]))
993 match_path_index = path_list->fpl_paths[pi];
994 fib_path_destroy(match_path_index);
995 vec_del1(path_list->fpl_paths, pi);
1000 * done with the temporary now
1002 fib_path_destroy(tmp_path_index);
1004 return (match_path_index);
1008 * fib_path_list_copy_and_path_remove
1010 * Copy the path-list excluding the path passed.
1011 * If the path is the last one, then the index reurned will be invalid.
1012 * i.e. the path-list is toast.
1015 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
1016 fib_path_list_flags_t flags,
1017 const fib_route_path_t *rpaths)
1019 fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
1020 fib_path_list_t *path_list, *orig_path_list;
1021 fib_node_index_t pi;
1023 ASSERT(1 == vec_len(rpaths));
1025 path_list = fib_path_list_alloc(&path_list_index);
1027 flags = fib_path_list_flags_fixup(flags);
1028 orig_path_list = fib_path_list_get(orig_path_list_index);
1030 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
1032 path_list->fpl_flags = flags;
1034 * allocate as many paths as we might need in one go, rather than
1035 * using vec_add to do a few at a time.
1037 if (vec_len(orig_path_list->fpl_paths) > 1)
1039 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
1044 * create a representation of the path to be removed, so it
1045 * can be used as a comparison object during the copy.
1047 tmp_path_index = fib_path_create(path_list_index,
1050 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
1052 if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
1053 path_index = fib_path_copy(*orig_path_index, path_list_index);
1054 if (pi < vec_len(path_list->fpl_paths))
1056 path_list->fpl_paths[pi++] = path_index;
1061 * this is the unlikely case that the path being
1062 * removed does not match one in the path-list, so
1063 * we end up with as many paths as we started with.
1064 * the paths vector was sized above with the expectation
1065 * that we would have 1 less.
1067 vec_add1(path_list->fpl_paths, path_index);
1073 * done with the temporary now
1075 fib_path_destroy(tmp_path_index);
1078 * if there are no paths, then the new path-list is aborted
1080 if (0 == vec_len(path_list->fpl_paths)) {
1081 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1083 fib_path_list_destroy(path_list);
1085 path_list_index = FIB_NODE_INDEX_INVALID;
1088 * we sort the paths since the key for the path-list is
1089 * the description of the paths it contains. The paths need to
1090 * be sorted else this description will differ.
1092 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1095 * If a shared path list is requested, consult the DB for a match
1097 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1099 fib_node_index_t exist_path_list_index;
1102 * check for a matching path-list in the DB.
1103 * If we find one then we can return the existing one and destroy the
1104 * new one just created.
1106 exist_path_list_index = fib_path_list_db_find(path_list);
1107 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1109 fib_path_list_destroy(path_list);
1111 path_list_index = exist_path_list_index;
1116 * if there was not a matching path-list, then this
1117 * new one will need inserting into the DB and resolving.
1119 fib_path_list_db_insert(path_list_index);
1121 path_list = fib_path_list_resolve(path_list);
1127 * no shared path list requested. resolve and use the one
1130 path_list = fib_path_list_resolve(path_list);
1134 return (path_list_index);
1138 * fib_path_list_contribute_forwarding
1140 * Return the index of a load-balance that user of this path-list should
1141 * use for forwarding
1144 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1145 fib_forward_chain_type_t fct,
1148 fib_path_list_t *path_list;
1150 path_list = fib_path_list_get(path_list_index);
1152 fib_path_list_mk_lb(path_list, fct, dpo);
1156 * fib_path_list_get_adj
1158 * Return the index of a adjacency for the first path that user of this
1159 * path-list should use for forwarding
1162 fib_path_list_get_adj (fib_node_index_t path_list_index,
1163 fib_forward_chain_type_t type)
1165 fib_path_list_t *path_list;
1167 path_list = fib_path_list_get(path_list_index);
1168 return (fib_path_get_adj(path_list->fpl_paths[0]));
1172 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1173 fib_node_index_t **entry_indicies)
1175 fib_node_index_t *path_index;
1176 int is_looped, list_looped;
1177 fib_path_list_t *path_list;
1180 path_list = fib_path_list_get(path_list_index);
1182 vec_foreach (path_index, path_list->fpl_paths)
1184 fib_node_index_t *copy, **copy_ptr;
1187 * we need a copy of the nodes visited so that when we add entries
1188 * we explore on the nth path and a looped is detected, those entries
1189 * are not again searched for n+1 path and so finding a loop that does
1192 copy = vec_dup(*entry_indicies);
1195 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1196 list_looped += is_looped;
1199 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", eval);
1203 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1207 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1210 return (list_looped);
1214 fib_path_list_child_add (fib_node_index_t path_list_index,
1215 fib_node_type_t child_type,
1216 fib_node_index_t child_index)
1220 sibling = fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1225 if (FIB_PATH_LIST_POPULAR == fib_node_get_n_children(FIB_NODE_TYPE_PATH_LIST,
1229 * Set the popular flag on the path-list once we pass the magic
1230 * threshold. then walk children to update.
1231 * We don't undo this action. The rational being that the number
1232 * of entries using this prefix is large enough such that it is a
1233 * non-trival amount of effort to converge them. If we get into the
1234 * situation where we are adding and removing entries such that we
1235 * flip-flop over the threshold, then this non-trivial work is added
1236 * to each of those routes adds/deletes - not a situation we want.
1238 fib_node_back_walk_ctx_t ctx = {
1239 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
1241 fib_path_list_t *path_list;
1243 path_list = fib_path_list_get(path_list_index);
1244 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_POPULAR;
1246 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, &ctx);
1253 fib_path_list_child_remove (fib_node_index_t path_list_index,
1256 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1262 fib_path_list_lock(fib_node_index_t path_list_index)
1264 fib_path_list_t *path_list;
1266 if (FIB_NODE_INDEX_INVALID != path_list_index)
1268 path_list = fib_path_list_get(path_list_index);
1270 fib_node_lock(&path_list->fpl_node);
1271 FIB_PATH_LIST_DBG(path_list, "lock");
1276 fib_path_list_unlock (fib_node_index_t path_list_index)
1278 fib_path_list_t *path_list;
1280 if (FIB_NODE_INDEX_INVALID != path_list_index)
1282 path_list = fib_path_list_get(path_list_index);
1283 FIB_PATH_LIST_DBG(path_list, "unlock");
1285 fib_node_unlock(&path_list->fpl_node);
1290 fib_path_list_pool_size (void)
1292 return (pool_elts(fib_path_list_pool));
1296 fib_path_list_db_size (void)
1298 return (hash_elts(fib_path_list_db));
1302 fib_path_list_walk (fib_node_index_t path_list_index,
1303 fib_path_list_walk_fn_t func,
1306 fib_node_index_t *path_index;
1307 fib_path_list_t *path_list;
1309 path_list = fib_path_list_get(path_list_index);
1311 vec_foreach(path_index, path_list->fpl_paths)
1313 if (FIB_PATH_LIST_WALK_STOP == func(path_list_index,
1322 fib_path_list_module_init (void)
1324 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1326 fib_path_list_db = hash_create2 (/* elts */ 0,
1328 /* value_bytes */ sizeof (fib_node_index_t),
1329 fib_path_list_db_hash_key_sum,
1330 fib_path_list_db_hash_key_equal,
1331 /* format pair/arg */
1335 static clib_error_t *
1336 show_fib_path_list_command (vlib_main_t * vm,
1337 unformat_input_t * input,
1338 vlib_cli_command_t * cmd)
1340 fib_path_list_t *path_list;
1341 fib_node_index_t pli;
1343 if (unformat (input, "%d", &pli))
1346 * show one in detail
1348 if (!pool_is_free_index(fib_path_list_pool, pli))
1350 path_list = fib_path_list_get(pli);
1351 u8 *s = fib_path_list_format(pli, NULL);
1352 s = format(s, "children:");
1353 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1354 vlib_cli_output (vm, "%s", s);
1359 vlib_cli_output (vm, "path list %d invalid", pli);
1367 vlib_cli_output (vm, "FIB Path Lists");
1368 pool_foreach(path_list, fib_path_list_pool,
1370 vlib_cli_output (vm, "%U", format_fib_path_list, path_list);
1376 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1377 .path = "show fib path-lists",
1378 .function = show_fib_path_list_command,
1379 .short_help = "show fib path-lists",