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>
30 * A representation of the list/set of path trough which a prefix is reachable
32 typedef struct fib_path_list_t_ {
34 * A path-list is a node in the FIB graph.
39 * Flags on the path-list
41 fib_path_list_flags_t fpl_flags;
44 * Vector of paths indicies for all configured paths.
45 * For shareable path-lists this list MUST not change.
47 fib_node_index_t *fpl_paths;
50 * the RPF list calculated for this path list
52 fib_node_index_t fpl_urpf;
55 * Hash table of paths. valid only with INDEXED flag
61 * Array of strings/names for the FIB sources
63 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
66 * The memory pool from which we allocate all the path-lists
68 static fib_path_list_t * fib_path_list_pool;
71 * The data-base of shared path-lists
73 static uword *fib_path_list_db;
79 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \
82 _tmp = fib_path_list_format( \
83 fib_path_list_get_index(_pl), _tmp); \
84 clib_warning("pl:[%d:%p:%p:%s]:" _fmt, \
85 fib_path_list_get_index(_pl), \
86 _pl, _pl->fpl_paths, _tmp, \
91 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)
94 static fib_path_list_t *
95 fib_path_list_get (fib_node_index_t index)
97 return (pool_elt_at_index(fib_path_list_pool, index));
101 fib_path_list_get_node (fib_node_index_t index)
103 return ((fib_node_t*)fib_path_list_get(index));
106 static fib_path_list_t*
107 fib_path_list_from_fib_node (fib_node_t *node)
110 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
112 return ((fib_path_list_t*)node);
115 static fib_node_index_t
116 fib_path_list_get_index (fib_path_list_t *path_list)
118 return (path_list - fib_path_list_pool);
122 format_fib_path_list (u8 * s, va_list * args)
124 fib_path_list_attribute_t attr;
125 fib_node_index_t *path_index;
126 fib_path_list_t *path_list;
128 path_list = va_arg (*args, fib_path_list_t *);
130 s = format (s, " index:%u", fib_path_list_get_index(path_list));
131 s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
133 if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
135 s = format (s, " flags:");
136 FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
138 if ((1<<attr) & path_list->fpl_flags)
140 s = format (s, "%s,", fib_path_list_attr_names[attr]);
144 s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);
146 vec_foreach (path_index, path_list->fpl_paths)
148 s = fib_path_format(*path_index, s);
156 fib_path_list_format (fib_node_index_t path_list_index,
159 fib_path_list_t *path_list;
161 path_list = fib_path_list_get(path_list_index);
163 return (format(s, "%U", format_fib_path_list, path_list));
167 fib_path_list_hash (fib_path_list_t *path_list)
169 uword old_path_list_hash, new_path_list_hash, path_hash;
170 fib_node_index_t *path_index;
174 new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths);
176 vec_foreach (path_index, path_list->fpl_paths)
178 path_hash = fib_path_hash(*path_index);
180 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
182 hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
186 return (new_path_list_hash);
190 fib_path_list_db_hash_key_from_index (uword index)
196 fib_path_list_db_hash_key_is_index (uword key)
202 fib_path_list_db_hash_key_2_index (uword key)
204 ASSERT (fib_path_list_db_hash_key_is_index (key));
208 static fib_path_list_t*
209 fib_path_list_db_get_from_hash_key (uword key)
211 fib_path_list_t *path_list;
213 if (fib_path_list_db_hash_key_is_index (key))
215 fib_node_index_t path_list_index;
217 path_list_index = fib_path_list_db_hash_key_2_index(key);
218 path_list = fib_path_list_get(path_list_index);
222 path_list = uword_to_pointer (key, fib_path_list_t *);
229 fib_path_list_db_hash_key_sum (hash_t * h,
232 fib_path_list_t *path_list;
234 path_list = fib_path_list_db_get_from_hash_key(key);
236 return (fib_path_list_hash(path_list));
240 fib_path_list_db_hash_key_equal (hash_t * h,
244 fib_path_list_t *path_list1, *path_list2;
246 path_list1 = fib_path_list_db_get_from_hash_key(key1);
247 path_list2 = fib_path_list_db_get_from_hash_key(key2);
249 return (fib_path_list_hash(path_list1) ==
250 fib_path_list_hash(path_list2));
253 static fib_node_index_t
254 fib_path_list_db_find (fib_path_list_t *path_list)
258 p = hash_get(fib_path_list_db, path_list);
265 return (FIB_NODE_INDEX_INVALID);
269 fib_path_list_db_insert (fib_node_index_t path_list_index)
271 fib_path_list_t *path_list;
273 path_list = fib_path_list_get(path_list_index);
275 ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
277 hash_set (fib_path_list_db,
278 fib_path_list_db_hash_key_from_index(path_list_index),
281 FIB_PATH_LIST_DBG(path_list, "DB-inserted");
285 fib_path_list_db_remove (fib_node_index_t path_list_index)
287 fib_path_list_t *path_list;
289 path_list = fib_path_list_get(path_list_index);
291 ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
293 hash_unset(fib_path_list_db,
294 fib_path_list_db_hash_key_from_index(path_list_index));
296 FIB_PATH_LIST_DBG(path_list, "DB-removed");
300 fib_path_list_destroy (fib_path_list_t *path_list)
302 fib_node_index_t *path_index;
304 FIB_PATH_LIST_DBG(path_list, "destroy");
306 vec_foreach (path_index, path_list->fpl_paths)
308 fib_path_destroy(*path_index);
311 vec_free(path_list->fpl_paths);
312 fib_urpf_list_unlock(path_list->fpl_urpf);
314 fib_node_deinit(&path_list->fpl_node);
315 pool_put(fib_path_list_pool, path_list);
319 fib_path_list_last_lock_gone (fib_node_t *node)
321 fib_path_list_t *path_list;
323 path_list = fib_path_list_from_fib_node(node);
325 FIB_PATH_LIST_DBG(path_list, "last-lock");
327 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
329 fib_path_list_db_remove(fib_path_list_get_index(path_list));
331 fib_path_list_destroy(path_list);
337 * update the multipath adj this path-list will contribute to its
338 * children's forwarding.
341 fib_path_list_mk_lb (fib_path_list_t *path_list,
342 fib_forward_chain_type_t fct,
345 load_balance_path_t *nhs;
346 fib_node_index_t *path_index;
350 if (!dpo_id_is_valid(dpo))
357 fib_forw_chain_type_to_dpo_proto(fct),
358 load_balance_create(0,
359 fib_forw_chain_type_to_dpo_proto(fct),
360 0 /* FIXME FLOW HASH */));
364 * We gather the DPOs from resolved paths.
366 vec_foreach (path_index, path_list->fpl_paths)
368 nhs = fib_path_append_nh_for_multipath_hash(*path_index,
374 * Path-list load-balances, which if used, would be shared and hence
375 * never need a load-balance map.
377 load_balance_multipath_update(dpo, nhs, LOAD_BALANCE_FLAG_NONE);
379 FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
385 * @brief [re]build the path list's uRPF list
388 fib_path_list_mk_urpf (fib_path_list_t *path_list)
390 fib_node_index_t *path_index;
393 * ditch the old one. by iterating through all paths we are going
394 * to re-find all the adjs that were in the old one anyway. If we
395 * keep the old one, then the |sort|uniq requires more work.
396 * All users of the RPF list have their own lock, so we can release
399 fib_urpf_list_unlock(path_list->fpl_urpf);
400 path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();
402 vec_foreach (path_index, path_list->fpl_paths)
404 fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
407 fib_urpf_list_bake(path_list->fpl_urpf);
411 * @brief Contribute (add) this path list's uRPF list. This allows the child
412 * to construct an aggregate list.
415 fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
418 fib_path_list_t *path_list;
420 path_list = fib_path_list_get(path_list_index);
422 fib_urpf_list_combine(urpf, path_list->fpl_urpf);
426 * @brief Return the the child the RPF list pre-built for this path list
429 fib_path_list_get_urpf (fib_node_index_t path_list_index)
431 fib_path_list_t *path_list;
433 path_list = fib_path_list_get(path_list_index);
435 return (path_list->fpl_urpf);
439 * fib_path_list_back_walk
441 * Called from one of this path-list's paths to progate
445 fib_path_list_back_walk (fib_node_index_t path_list_index,
446 fib_node_back_walk_ctx_t *ctx)
448 fib_path_list_t *path_list;
450 path_list = fib_path_list_get(path_list_index);
452 fib_path_list_mk_urpf(path_list);
455 * propagate the backwalk further
457 if (32 >= fib_node_list_get_size(path_list->fpl_node.fn_children))
460 * only a few children. continue the walk synchronously
462 fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
467 * many children. schedule a async walk
469 fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
471 FIB_WALK_PRIORITY_LOW,
477 * fib_path_list_back_walk_notify
479 * A back walk has reach this path-list.
481 static fib_node_back_walk_rc_t
482 fib_path_list_back_walk_notify (fib_node_t *node,
483 fib_node_back_walk_ctx_t *ctx)
486 * the path-list is not a direct child of any other node type
487 * paths, which do not change thier to-list-mapping, save the
488 * list they are a member of, and invoke the BW function directly.
492 return (FIB_NODE_BACK_WALK_CONTINUE);
496 * Display the path-list memory usage
499 fib_path_list_memory_show (void)
501 fib_show_memory_usage("Path-list",
502 pool_elts(fib_path_list_pool),
503 pool_len(fib_path_list_pool),
504 sizeof(fib_path_list_t));
505 fib_urpf_list_show_mem();
509 * The FIB path-list's graph node virtual function table
511 static const fib_node_vft_t fib_path_list_vft = {
512 .fnv_get = fib_path_list_get_node,
513 .fnv_last_lock = fib_path_list_last_lock_gone,
514 .fnv_back_walk = fib_path_list_back_walk_notify,
515 .fnv_mem_show = fib_path_list_memory_show,
518 static inline fib_path_list_t *
519 fib_path_list_alloc (fib_node_index_t *path_list_index)
521 fib_path_list_t *path_list;
523 pool_get(fib_path_list_pool, path_list);
524 memset(path_list, 0, sizeof(*path_list));
526 fib_node_init(&path_list->fpl_node,
527 FIB_NODE_TYPE_PATH_LIST);
528 path_list->fpl_urpf = INDEX_INVALID;
529 path_list->fpl_paths = NULL;
531 *path_list_index = fib_path_list_get_index(path_list);
533 FIB_PATH_LIST_DBG(path_list, "alloc");
538 static fib_path_list_t *
539 fib_path_list_resolve (fib_path_list_t *path_list)
541 fib_node_index_t *path_index, *paths, path_list_index;
543 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
546 * resolving a path-list is a recursive action. this means more path
547 * lists can be created during this call, and hence this path-list
548 * can be realloc'd. so we work with copies.
549 * this function is called only once per-path list, so its no great overhead.
551 path_list_index = fib_path_list_get_index(path_list);
552 paths = vec_dup(path_list->fpl_paths);
554 vec_foreach (path_index, paths)
556 fib_path_resolve(*path_index);
560 path_list = fib_path_list_get(path_list_index);
562 FIB_PATH_LIST_DBG(path_list, "resovled");
564 if (!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_NO_URPF))
566 fib_path_list_mk_urpf(path_list);
572 fib_path_list_get_n_paths (fib_node_index_t path_list_index)
574 fib_path_list_t *path_list;
576 path_list = fib_path_list_get(path_list_index);
578 return (vec_len(path_list->fpl_paths));
583 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
585 fib_node_index_t *path_index;
586 fib_path_list_t *path_list;
589 path_list = fib_path_list_get(path_list_index);
592 vec_foreach (path_index, path_list->fpl_paths)
594 sw_if_index = fib_path_get_resolving_interface(*path_index);
595 if (~0 != sw_if_index)
597 return (sw_if_index);
601 return (sw_if_index);
605 fib_path_list_get_proto (fib_node_index_t path_list_index)
607 fib_path_list_t *path_list;
609 path_list = fib_path_list_get(path_list_index);
612 * we don't support a mix of path protocols, so we can return the proto
615 return (fib_path_get_proto(path_list->fpl_paths[0]));
619 fib_path_list_is_looped (fib_node_index_t path_list_index)
621 fib_path_list_t *path_list;
623 path_list = fib_path_list_get(path_list_index);
625 return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
628 static fib_path_list_flags_t
629 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
632 * we do no share drop nor exclusive path-lists
634 if (flags & FIB_PATH_LIST_FLAG_DROP ||
635 flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
637 flags &= ~FIB_PATH_LIST_FLAG_SHARED;
644 fib_path_list_create (fib_path_list_flags_t flags,
645 const fib_route_path_t *rpaths)
647 fib_node_index_t path_list_index, old_path_list_index;
648 fib_path_list_t *path_list;
651 flags = fib_path_list_flags_fixup(flags);
652 path_list = fib_path_list_alloc(&path_list_index);
653 path_list->fpl_flags = flags;
657 vec_foreach_index(i, rpaths)
659 vec_add1(path_list->fpl_paths,
660 fib_path_create(path_list_index,
666 * If a shared path list is requested, consult the DB for a match
668 if (flags & FIB_PATH_LIST_FLAG_SHARED)
671 * check for a matching path-list in the DB.
672 * If we find one then we can return the existing one and destroy the
673 * new one just created.
675 old_path_list_index = fib_path_list_db_find(path_list);
676 if (FIB_NODE_INDEX_INVALID != old_path_list_index)
678 fib_path_list_destroy(path_list);
680 path_list_index = old_path_list_index;
685 * if there was not a matching path-list, then this
686 * new one will need inserting into the DB and resolving.
688 fib_path_list_db_insert(path_list_index);
689 path_list = fib_path_list_resolve(path_list);
695 * no shared path list requested. resolve and use the one
698 path_list = fib_path_list_resolve(path_list);
701 return (path_list_index);
704 static fib_path_cfg_flags_t
705 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
707 fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
709 if (plf & FIB_PATH_LIST_FLAG_DROP)
711 pf |= FIB_PATH_CFG_FLAG_DROP;
713 if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
715 pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
717 if (plf & FIB_PATH_LIST_FLAG_LOCAL)
719 pf |= FIB_PATH_CFG_FLAG_LOCAL;
726 fib_path_list_create_special (fib_protocol_t nh_proto,
727 fib_path_list_flags_t flags,
730 fib_node_index_t path_index, path_list_index;
731 fib_path_list_t *path_list;
733 path_list = fib_path_list_alloc(&path_list_index);
734 path_list->fpl_flags = flags;
737 fib_path_create_special(path_list_index,
739 fib_path_list_flags_2_path_flags(flags),
741 vec_add1(path_list->fpl_paths, path_index);
744 * we don't share path-lists. we can do PIC on them so why bother.
746 path_list = fib_path_list_resolve(path_list);
748 return (path_list_index);
752 * return the index info the path-lists's vector of paths, of the matching path.
756 fib_path_list_find_rpath (fib_node_index_t path_list_index,
757 const fib_route_path_t *rpath)
759 fib_path_list_t *path_list;
762 path_list = fib_path_list_get(path_list_index);
764 vec_foreach_index (ii, path_list->fpl_paths)
766 if (!fib_path_cmp_w_route_path(path_list->fpl_paths[ii], rpath))
776 * fib_path_list_copy_and_path_add
778 * Create a copy of a path-list and append one more path to it.
779 * The path-list returned could either have been newly created, or
780 * can be a shared path-list from the data-base.
783 fib_path_list_path_add (fib_node_index_t path_list_index,
784 const fib_route_path_t *rpaths)
786 fib_node_index_t new_path_index, *orig_path_index;
787 fib_path_list_t *path_list;
790 * alloc the new list before we retrieve the old one, lest
791 * the alloc result in a realloc
793 path_list = fib_path_list_get(path_list_index);
795 ASSERT(1 == vec_len(rpaths));
796 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
798 FIB_PATH_LIST_DBG(orig_path_list, "path-add");
800 new_path_index = fib_path_create(path_list_index,
803 vec_foreach (orig_path_index, path_list->fpl_paths)
806 * don't add duplicate paths
808 if (0 == fib_path_cmp(new_path_index, *orig_path_index))
810 return (*orig_path_index);
815 * Add the new path - no sort, no sharing, no key..
817 vec_add1(path_list->fpl_paths, new_path_index);
819 FIB_PATH_LIST_DBG(path_list, "path-added");
822 * no shared path list requested. resolve and use the one
825 fib_path_resolve(new_path_index);
827 return (new_path_index);
831 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
832 fib_path_list_flags_t flags,
833 const fib_route_path_t *rpaths)
835 fib_node_index_t path_index, new_path_index, *orig_path_index;
836 fib_path_list_t *path_list, *orig_path_list;
837 fib_node_index_t exist_path_list_index;
838 fib_node_index_t path_list_index;
841 ASSERT(1 == vec_len(rpaths));
844 * alloc the new list before we retrieve the old one, lest
845 * the alloc result in a realloc
847 path_list = fib_path_list_alloc(&path_list_index);
849 orig_path_list = fib_path_list_get(orig_path_list_index);
851 FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
853 flags = fib_path_list_flags_fixup(flags);
854 path_list->fpl_flags = flags;
856 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
859 new_path_index = fib_path_create(path_list_index,
862 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
865 * don't add duplicate paths
866 * In the unlikely event the path is a duplicate, then we'll
867 * find a matching path-list later and this one will be toast.
869 if (0 != fib_path_cmp(new_path_index, *orig_path_index))
871 path_index = fib_path_copy(*orig_path_index, path_list_index);
872 path_list->fpl_paths[pi++] = path_index;
876 _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
880 path_list->fpl_paths[pi] = new_path_index;
883 * we sort the paths since the key for the path-list is
884 * the description of the paths it contains. The paths need to
885 * be sorted else this description will differ.
887 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
889 FIB_PATH_LIST_DBG(path_list, "path-added");
892 * check for a matching path-list in the DB.
893 * If we find one then we can return the existing one and destroy the
894 * new one just created.
896 exist_path_list_index = fib_path_list_db_find(path_list);
897 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
899 fib_path_list_destroy(path_list);
901 path_list_index = exist_path_list_index;
906 * if there was not a matching path-list, then this
907 * new one will need inserting into the DB and resolving.
909 fib_path_list_db_insert(path_list_index);
911 path_list = fib_path_list_resolve(path_list);
914 return (path_list_index);
918 * fib_path_list_path_remove
921 fib_path_list_path_remove (fib_node_index_t path_list_index,
922 const fib_route_path_t *rpaths)
924 fib_node_index_t match_path_index, tmp_path_index;
925 fib_path_list_t *path_list;
928 path_list = fib_path_list_get(path_list_index);
930 ASSERT(1 == vec_len(rpaths));
931 ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED));
933 FIB_PATH_LIST_DBG(orig_path_list, "path-remove");
936 * create a representation of the path to be removed, so it
937 * can be used as a comparison object during the copy.
939 tmp_path_index = fib_path_create(path_list_index,
941 match_path_index = FIB_NODE_INDEX_INVALID;
943 vec_foreach_index (pi, path_list->fpl_paths)
945 if (0 == fib_path_cmp(tmp_path_index,
946 path_list->fpl_paths[pi]))
951 match_path_index = path_list->fpl_paths[pi];
952 fib_path_destroy(match_path_index);
953 vec_del1(path_list->fpl_paths, pi);
958 * done with the temporary now
960 fib_path_destroy(tmp_path_index);
962 return (match_path_index);
966 * fib_path_list_copy_and_path_remove
968 * Copy the path-list excluding the path passed.
969 * If the path is the last one, then the index reurned will be invalid.
970 * i.e. the path-list is toast.
973 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
974 fib_path_list_flags_t flags,
975 const fib_route_path_t *rpaths)
977 fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
978 fib_path_list_t *path_list, *orig_path_list;
981 ASSERT(1 == vec_len(rpaths));
983 path_list = fib_path_list_alloc(&path_list_index);
985 flags = fib_path_list_flags_fixup(flags);
986 orig_path_list = fib_path_list_get(orig_path_list_index);
988 FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
990 path_list->fpl_flags = flags;
992 * allocate as many paths as we might need in one go, rather than
993 * using vec_add to do a few at a time.
995 if (vec_len(orig_path_list->fpl_paths) > 1)
997 vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
1002 * create a representation of the path to be removed, so it
1003 * can be used as a comparison object during the copy.
1005 tmp_path_index = fib_path_create(path_list_index,
1008 vec_foreach (orig_path_index, orig_path_list->fpl_paths)
1010 if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
1011 path_index = fib_path_copy(*orig_path_index, path_list_index);
1012 if (pi < vec_len(path_list->fpl_paths))
1014 path_list->fpl_paths[pi++] = path_index;
1019 * this is the unlikely case that the path being
1020 * removed does not match one in the path-list, so
1021 * we end up with as many paths as we started with.
1022 * the paths vector was sized above with the expectation
1023 * that we would have 1 less.
1025 vec_add1(path_list->fpl_paths, path_index);
1031 * done with the temporary now
1033 fib_path_destroy(tmp_path_index);
1036 * if there are no paths, then the new path-list is aborted
1038 if (0 == vec_len(path_list->fpl_paths)) {
1039 FIB_PATH_LIST_DBG(path_list, "last-path-removed");
1041 fib_path_list_destroy(path_list);
1043 path_list_index = FIB_NODE_INDEX_INVALID;
1046 * we sort the paths since the key for the path-list is
1047 * the description of the paths it contains. The paths need to
1048 * be sorted else this description will differ.
1050 vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
1053 * If a shared path list is requested, consult the DB for a match
1055 if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
1057 fib_node_index_t exist_path_list_index;
1060 * check for a matching path-list in the DB.
1061 * If we find one then we can return the existing one and destroy the
1062 * new one just created.
1064 exist_path_list_index = fib_path_list_db_find(path_list);
1065 if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
1067 fib_path_list_destroy(path_list);
1069 path_list_index = exist_path_list_index;
1074 * if there was not a matching path-list, then this
1075 * new one will need inserting into the DB and resolving.
1077 fib_path_list_db_insert(path_list_index);
1079 path_list = fib_path_list_resolve(path_list);
1085 * no shared path list requested. resolve and use the one
1088 path_list = fib_path_list_resolve(path_list);
1092 return (path_list_index);
1096 * fib_path_list_contribute_forwarding
1098 * Return the index of a load-balance that user of this path-list should
1099 * use for forwarding
1102 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
1103 fib_forward_chain_type_t fct,
1106 fib_path_list_t *path_list;
1108 path_list = fib_path_list_get(path_list_index);
1110 fib_path_list_mk_lb(path_list, fct, dpo);
1114 * fib_path_list_get_adj
1116 * Return the index of a adjacency for the first path that user of this
1117 * path-list should use for forwarding
1120 fib_path_list_get_adj (fib_node_index_t path_list_index,
1121 fib_forward_chain_type_t type)
1123 fib_path_list_t *path_list;
1125 path_list = fib_path_list_get(path_list_index);
1126 return (fib_path_get_adj(path_list->fpl_paths[0]));
1130 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
1131 fib_node_index_t **entry_indicies)
1133 fib_node_index_t *path_index;
1134 int is_looped, list_looped;
1135 fib_path_list_t *path_list;
1138 path_list = fib_path_list_get(path_list_index);
1140 vec_foreach (path_index, path_list->fpl_paths)
1142 fib_node_index_t *copy, **copy_ptr;
1145 * we need a copy of the nodes visited so that when we add entries
1146 * we explore on the nth path and a looped is detected, those entries
1147 * are not again searched for n+1 path and so finding a loop that does
1150 copy = vec_dup(*entry_indicies);
1153 is_looped = fib_path_recursive_loop_detect(*path_index, copy_ptr);
1154 list_looped += is_looped;
1157 FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", eval);
1161 path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
1165 path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
1168 return (list_looped);
1172 fib_path_list_child_add (fib_node_index_t path_list_index,
1173 fib_node_type_t child_type,
1174 fib_node_index_t child_index)
1176 return (fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
1183 fib_path_list_child_remove (fib_node_index_t path_list_index,
1186 fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
1192 fib_path_list_lock(fib_node_index_t path_list_index)
1194 fib_path_list_t *path_list;
1196 if (FIB_NODE_INDEX_INVALID != path_list_index)
1198 path_list = fib_path_list_get(path_list_index);
1200 fib_node_lock(&path_list->fpl_node);
1201 FIB_PATH_LIST_DBG(path_list, "lock");
1206 fib_path_list_unlock (fib_node_index_t path_list_index)
1208 fib_path_list_t *path_list;
1210 if (FIB_NODE_INDEX_INVALID != path_list_index)
1212 path_list = fib_path_list_get(path_list_index);
1213 FIB_PATH_LIST_DBG(path_list, "unlock");
1215 fib_node_unlock(&path_list->fpl_node);
1220 fib_path_list_pool_size (void)
1222 return (pool_elts(fib_path_list_pool));
1226 fib_path_list_db_size (void)
1228 return (hash_elts(fib_path_list_db));
1232 fib_path_list_walk (fib_node_index_t path_list_index,
1233 fib_path_list_walk_fn_t func,
1236 fib_node_index_t *path_index;
1237 fib_path_list_t *path_list;
1239 path_list = fib_path_list_get(path_list_index);
1241 vec_foreach(path_index, path_list->fpl_paths)
1243 if (!func(path_list_index, *path_index, ctx))
1250 fib_path_list_module_init (void)
1252 fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1254 fib_path_list_db = hash_create2 (/* elts */ 0,
1256 /* value_bytes */ sizeof (fib_node_index_t),
1257 fib_path_list_db_hash_key_sum,
1258 fib_path_list_db_hash_key_equal,
1259 /* format pair/arg */
1263 static clib_error_t *
1264 show_fib_path_list_command (vlib_main_t * vm,
1265 unformat_input_t * input,
1266 vlib_cli_command_t * cmd)
1268 fib_path_list_t *path_list;
1269 fib_node_index_t pli;
1271 if (unformat (input, "%d", &pli))
1274 * show one in detail
1276 if (!pool_is_free_index(fib_path_list_pool, pli))
1278 path_list = fib_path_list_get(pli);
1279 u8 *s = fib_path_list_format(pli, NULL);
1280 s = format(s, "children:");
1281 s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1282 vlib_cli_output (vm, "%s", s);
1287 vlib_cli_output (vm, "path list %d invalid", pli);
1295 vlib_cli_output (vm, "FIB Path Lists");
1296 pool_foreach(path_list, fib_path_list_pool,
1298 vlib_cli_output (vm, "%U", format_fib_path_list, path_list);
1304 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1305 .path = "show fib path-lists",
1306 .function = show_fib_path_list_command,
1307 .short_help = "show fib path-lists",