+/*
+ * fib_entry_src_action_copy
+ *
+ * copy a source data from another entry to this one
+ */
+static fib_entry_t *
+fib_entry_src_action_copy (fib_entry_t *fib_entry,
+ const fib_entry_src_t *orig_src)
+{
+ fib_entry_src_t *esrc;
+
+ esrc = fib_entry_src_find_or_create(fib_entry,
+ orig_src->fes_src,
+ orig_src->fes_entry_flags);
+
+ FIB_ENTRY_SRC_VFT_INVOKE(esrc, fesv_copy,
+ (orig_src, fib_entry, esrc));
+
+ fib_path_list_unlock(esrc->fes_pl);
+
+ /*
+ * copy over all the data ...
+ */
+ esrc->fes_flags = orig_src->fes_flags;
+ esrc->fes_pl = orig_src->fes_pl;
+
+ /*
+ * ... then update
+ */
+ esrc->fes_ref_count = 1;
+ esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_INHERITED;
+ esrc->fes_flags &= ~(FIB_ENTRY_SRC_FLAG_ACTIVE |
+ FIB_ENTRY_SRC_FLAG_CONTRIBUTING);
+ esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_COVERED_INHERIT;
+
+ /*
+ * the source owns a lock on the entry
+ */
+ fib_path_list_lock(esrc->fes_pl);
+ fib_entry_lock(fib_entry_get_index(fib_entry));
+
+ return (fib_entry);
+}
+
+/*
+ * fib_entry_src_action_update
+ *
+ * copy a source data from another entry to this one
+ */
+static fib_entry_src_t *
+fib_entry_src_action_update_from_cover (fib_entry_t *fib_entry,
+ const fib_entry_src_t *orig_src)
+{
+ fib_entry_src_t *esrc;
+
+ esrc = fib_entry_src_find_or_create(fib_entry,
+ orig_src->fes_src,
+ orig_src->fes_entry_flags);
+
+ /*
+ * the source owns a lock on the entry
+ */
+ fib_path_list_unlock(esrc->fes_pl);
+ esrc->fes_pl = orig_src->fes_pl;
+ fib_path_list_lock(esrc->fes_pl);
+
+ return (esrc);
+}
+
+static fib_table_walk_rc_t
+fib_entry_src_covered_inherit_add_i (fib_entry_t *fib_entry,
+ const fib_entry_src_t *cover_src)
+{
+ fib_entry_src_t *esrc;
+
+ esrc = fib_entry_src_find(fib_entry, cover_src->fes_src);
+
+ if (cover_src == esrc)
+ {
+ return (FIB_TABLE_WALK_CONTINUE);
+ }
+
+ if (NULL != esrc)
+ {
+ /*
+ * the covered entry already has this source.
+ */
+ if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
+ {
+ /*
+ * the covered source is itself a COVERED_INHERIT, i.e.
+ * it also pushes this source down the sub-tree.
+ * We consider this more specfic covered to be the owner
+ * of the sub-tree from this point down.
+ */
+ return (FIB_TABLE_WALK_SUB_TREE_STOP);
+ }
+ if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED)
+ {
+ /*
+ * The covered's source data has been inherited, presumably
+ * from this cover, i.e. this is a modify.
+ */
+ esrc = fib_entry_src_action_update_from_cover(fib_entry, cover_src);
+ fib_entry_source_change(fib_entry, esrc->fes_src, esrc->fes_src);
+ }
+ else
+ {
+ /*
+ * The covered's source was not inherited and it is also
+ * not inherting. Nevertheless, it still owns the sub-tree from
+ * this point down.
+ */
+ return (FIB_TABLE_WALK_SUB_TREE_STOP);
+ }
+ }
+ else
+ {
+ /*
+ * The covered does not have this source - add it.
+ */
+ fib_source_t best_source;
+
+ best_source = fib_entry_get_best_source(
+ fib_entry_get_index(fib_entry));
+
+ fib_entry_src_action_copy(fib_entry, cover_src);
+ fib_entry_source_change(fib_entry, best_source, cover_src->fes_src);
+
+ }
+ return (FIB_TABLE_WALK_CONTINUE);
+}
+
+static fib_table_walk_rc_t
+fib_entry_src_covered_inherit_walk_add (fib_node_index_t fei,
+ void *ctx)
+{
+ return (fib_entry_src_covered_inherit_add_i(fib_entry_get(fei), ctx));
+}
+
+static fib_table_walk_rc_t
+fib_entry_src_covered_inherit_walk_remove (fib_node_index_t fei,
+ void *ctx)
+{
+ fib_entry_src_t *cover_src, *esrc;
+ fib_entry_t *fib_entry;
+
+ fib_entry = fib_entry_get(fei);
+
+ cover_src = ctx;
+ esrc = fib_entry_src_find(fib_entry, cover_src->fes_src);
+
+ if (cover_src == esrc)
+ {
+ return (FIB_TABLE_WALK_CONTINUE);
+ }
+
+ if (NULL != esrc)
+ {
+ /*
+ * the covered entry already has this source.
+ */
+ if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
+ {
+ /*
+ * the covered source is itself a COVERED_INHERIT, i.e.
+ * it also pushes this source down the sub-tree.
+ * We consider this more specfic covered to be the owner
+ * of the sub-tree from this point down.
+ */
+ return (FIB_TABLE_WALK_SUB_TREE_STOP);
+ }
+ if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED)
+ {
+ /*
+ * The covered's source data has been inherited, presumably
+ * from this cover
+ */
+ fib_entry_src_flag_t remaining;
+
+ remaining = fib_entry_special_remove(fei, cover_src->fes_src);
+
+ ASSERT(FIB_ENTRY_SRC_FLAG_ADDED == remaining);
+ }
+ else
+ {
+ /*
+ * The covered's source was not inherited and it is also
+ * not inherting. Nevertheless, it still owns the sub-tree from
+ * this point down.
+ */
+ return (FIB_TABLE_WALK_SUB_TREE_STOP);
+ }
+ }
+ else
+ {
+ /*
+ * The covered does not have this source - that's an error,
+ * since it should have inherited, but there is nothing we can do
+ * about it now.
+ */
+ }
+ return (FIB_TABLE_WALK_CONTINUE);
+}
+
+void
+fib_entry_src_inherit (const fib_entry_t *cover,
+ fib_entry_t *covered)
+{
+ CLIB_UNUSED(fib_source_t source);
+ const fib_entry_src_t *src;
+
+ FOR_EACH_SRC_ADDED(cover, src, source,
+ ({
+ if ((src->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT) ||
+ (src->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
+ {
+ fib_entry_src_covered_inherit_add_i(covered, src);
+ }
+ }))
+}
+
+static void
+fib_entry_src_covered_inherit_add (fib_entry_t *fib_entry,
+ fib_source_t source)
+
+{
+ fib_entry_src_t *esrc;
+
+ esrc = fib_entry_src_find(fib_entry, source);
+
+ ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
+
+ if ((esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT) ||
+ (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
+ {
+ fib_table_sub_tree_walk(fib_entry->fe_fib_index,
+ fib_entry->fe_prefix.fp_proto,
+ &fib_entry->fe_prefix,
+ fib_entry_src_covered_inherit_walk_add,
+ esrc);
+ }
+}
+
+static void
+fib_entry_src_covered_inherit_remove (fib_entry_t *fib_entry,
+ fib_entry_src_t *esrc)
+
+{
+ ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
+
+ if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
+ {
+ fib_table_sub_tree_walk(fib_entry->fe_fib_index,
+ fib_entry->fe_prefix.fp_proto,
+ &fib_entry->fe_prefix,
+ fib_entry_src_covered_inherit_walk_remove,
+ esrc);
+ }
+}
+