/* * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include const static char * mfib_table_flags_strings[] = MFIB_TABLE_ATTRIBUTES; mfib_table_t * mfib_table_get (fib_node_index_t index, fib_protocol_t proto) { switch (proto) { case FIB_PROTOCOL_IP4: return (pool_elt_at_index(ip4_main.mfibs, index)); case FIB_PROTOCOL_IP6: return (pool_elt_at_index(ip6_main.mfibs, index)); case FIB_PROTOCOL_MPLS: break; } ASSERT(0); return (NULL); } static inline fib_node_index_t mfib_table_lookup_i (const mfib_table_t *mfib_table, const mfib_prefix_t *prefix) { switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: return (ip4_mfib_table_lookup(&mfib_table->v4, &prefix->fp_src_addr.ip4, &prefix->fp_grp_addr.ip4, prefix->fp_len)); case FIB_PROTOCOL_IP6: return (ip6_mfib_table_lookup(&mfib_table->v6, &prefix->fp_src_addr.ip6, &prefix->fp_grp_addr.ip6, prefix->fp_len)); case FIB_PROTOCOL_MPLS: break; } return (FIB_NODE_INDEX_INVALID); } fib_node_index_t mfib_table_lookup (u32 fib_index, const mfib_prefix_t *prefix) { return (mfib_table_lookup_i(mfib_table_get(fib_index, prefix->fp_proto), prefix)); } static inline fib_node_index_t mfib_table_lookup_exact_match_i (const mfib_table_t *mfib_table, const mfib_prefix_t *prefix) { switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: return (ip4_mfib_table_lookup_exact_match(&mfib_table->v4, &prefix->fp_grp_addr.ip4, &prefix->fp_src_addr.ip4, prefix->fp_len)); case FIB_PROTOCOL_IP6: return (ip6_mfib_table_lookup_exact_match(&mfib_table->v6, &prefix->fp_grp_addr.ip6, &prefix->fp_src_addr.ip6, prefix->fp_len)); case FIB_PROTOCOL_MPLS: break; } return (FIB_NODE_INDEX_INVALID); } fib_node_index_t mfib_table_lookup_exact_match (u32 fib_index, const mfib_prefix_t *prefix) { return (mfib_table_lookup_exact_match_i(mfib_table_get(fib_index, prefix->fp_proto), prefix)); } static fib_node_index_t mfib_table_get_less_specific_i (const mfib_table_t *mfib_table, const mfib_prefix_t *prefix) { switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: return (ip4_mfib_table_get_less_specific(&mfib_table->v4, &prefix->fp_src_addr.ip4, &prefix->fp_grp_addr.ip4, prefix->fp_len)); case FIB_PROTOCOL_IP6: return (ip6_mfib_table_get_less_specific(&mfib_table->v6, &prefix->fp_src_addr.ip6, &prefix->fp_grp_addr.ip6, prefix->fp_len)); case FIB_PROTOCOL_MPLS: break; } return (FIB_NODE_INDEX_INVALID); } fib_node_index_t mfib_table_get_less_specific (u32 fib_index, const mfib_prefix_t *prefix) { return (mfib_table_get_less_specific_i(mfib_table_get(fib_index, prefix->fp_proto), prefix)); } static void mfib_table_entry_remove (mfib_table_t *mfib_table, const mfib_prefix_t *prefix, fib_node_index_t mfib_entry_index) { vlib_smp_unsafe_warning(); mfib_table->mft_total_route_counts--; switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: ip4_mfib_table_entry_remove(&mfib_table->v4, &prefix->fp_grp_addr.ip4, &prefix->fp_src_addr.ip4, prefix->fp_len); break; case FIB_PROTOCOL_IP6: ip6_mfib_table_entry_remove(&mfib_table->v6, &prefix->fp_grp_addr.ip6, &prefix->fp_src_addr.ip6, prefix->fp_len); break; case FIB_PROTOCOL_MPLS: ASSERT(0); break; } mfib_entry_cover_change_notify(mfib_entry_index, FIB_NODE_INDEX_INVALID); mfib_entry_unlock(mfib_entry_index); } static void mfib_table_post_insert_actions (mfib_table_t *mfib_table, const mfib_prefix_t *prefix, fib_node_index_t mfib_entry_index) { fib_node_index_t mfib_entry_cover_index; /* * find the covering entry */ mfib_entry_cover_index = mfib_table_get_less_specific_i(mfib_table, prefix); /* * the indicies are the same when the default route is first added */ if (mfib_entry_cover_index != mfib_entry_index) { /* * inform the covering entry that a new more specific * has been inserted beneath it. * If the prefix that has been inserted is a host route * then it is not possible that it will be the cover for any * other entry, so we can elide the walk. */ if (!mfib_entry_is_host(mfib_entry_index)) { mfib_entry_cover_change_notify(mfib_entry_cover_index, mfib_entry_index); } } } static void mfib_table_entry_insert (mfib_table_t *mfib_table, const mfib_prefix_t *prefix, fib_node_index_t mfib_entry_index) { vlib_smp_unsafe_warning(); mfib_entry_lock(mfib_entry_index); mfib_table->mft_total_route_counts++; switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: ip4_mfib_table_entry_insert(&mfib_table->v4, &prefix->fp_grp_addr.ip4, &prefix->fp_src_addr.ip4, prefix->fp_len, mfib_entry_index); break; case FIB_PROTOCOL_IP6: ip6_mfib_table_entry_insert(&mfib_table->v6, &prefix->fp_grp_addr.ip6, &prefix->fp_src_addr.ip6, prefix->fp_len, mfib_entry_index); break; case FIB_PROTOCOL_MPLS: break; } mfib_table_post_insert_actions(mfib_table, prefix, mfib_entry_index); } fib_node_index_t mfib_table_entry_update (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, fib_rpf_id_t rpf_id, mfib_entry_flags_t entry_flags) { fib_node_index_t mfib_entry_index; mfib_table_t *mfib_table; mfib_table = mfib_table_get(fib_index, prefix->fp_proto); mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix); if (FIB_NODE_INDEX_INVALID == mfib_entry_index) { if (MFIB_ENTRY_FLAG_NONE != entry_flags) { /* * update to a non-existing entry with non-zero flags */ mfib_entry_index = mfib_entry_create(fib_index, source, prefix, rpf_id, entry_flags, INDEX_INVALID); mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index); } /* * else * the entry doesn't exist and the request is to set no flags * the result would be an entry that doesn't exist - so do nothing */ } else { mfib_entry_lock(mfib_entry_index); if (mfib_entry_update(mfib_entry_index, source, entry_flags, rpf_id, INDEX_INVALID)) { /* * this update means we can now remove the entry. */ mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index); } mfib_entry_unlock(mfib_entry_index); } return (mfib_entry_index); } static fib_node_index_t mfib_table_entry_paths_update_i (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpaths) { fib_node_index_t mfib_entry_index; mfib_table_t *mfib_table; mfib_table = mfib_table_get(fib_index, prefix->fp_proto); mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix); if (FIB_NODE_INDEX_INVALID == mfib_entry_index) { mfib_entry_index = mfib_entry_create(fib_index, source, prefix, MFIB_RPF_ID_NONE, MFIB_ENTRY_FLAG_NONE, INDEX_INVALID); mfib_entry_path_update(mfib_entry_index, source, rpaths); mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index); } else { mfib_entry_path_update(mfib_entry_index, source, rpaths); } return (mfib_entry_index); } fib_node_index_t mfib_table_entry_path_update (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpath) { fib_node_index_t mfib_entry_index; fib_route_path_t *rpaths = NULL; vec_add1(rpaths, *rpath); mfib_entry_index = mfib_table_entry_paths_update_i(fib_index, prefix, source, rpaths); vec_free(rpaths); return (mfib_entry_index); } fib_node_index_t mfib_table_entry_paths_update (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpaths) { return (mfib_table_entry_paths_update_i(fib_index, prefix, source, rpaths)); } static void mfib_table_entry_paths_remove_i (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpaths) { fib_node_index_t mfib_entry_index; mfib_table_t *mfib_table; mfib_table = mfib_table_get(fib_index, prefix->fp_proto); mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix); if (FIB_NODE_INDEX_INVALID == mfib_entry_index) { /* * removing an entry that does not exist. i'll allow it. */ } else { int no_more_sources; /* * don't nobody go nowhere */ mfib_entry_lock(mfib_entry_index); no_more_sources = mfib_entry_path_remove(mfib_entry_index, source, rpaths); if (no_more_sources) { /* * last source gone. remove from the table */ mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index); } mfib_entry_unlock(mfib_entry_index); } } void mfib_table_entry_paths_remove (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpaths) { mfib_table_entry_paths_remove_i(fib_index, prefix, source, rpaths); } void mfib_table_entry_path_remove (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, const fib_route_path_t *rpath) { fib_route_path_t *rpaths = NULL; vec_add1(rpaths, *rpath); mfib_table_entry_paths_remove_i(fib_index, prefix, source, rpaths); vec_free(rpaths); } fib_node_index_t mfib_table_entry_special_add (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source, mfib_entry_flags_t entry_flags, index_t repi) { fib_node_index_t mfib_entry_index; mfib_table_t *mfib_table; mfib_table = mfib_table_get(fib_index, prefix->fp_proto); mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix); if (INDEX_INVALID != repi) { entry_flags |= MFIB_ENTRY_FLAG_EXCLUSIVE; } if (FIB_NODE_INDEX_INVALID == mfib_entry_index) { mfib_entry_index = mfib_entry_create(fib_index, source, prefix, MFIB_RPF_ID_NONE, entry_flags, repi); mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index); } else { mfib_entry_special_add(mfib_entry_index, source, entry_flags, MFIB_RPF_ID_NONE, repi); } return (mfib_entry_index); } static void mfib_table_entry_delete_i (u32 fib_index, fib_node_index_t mfib_entry_index, const mfib_prefix_t *prefix, mfib_source_t source) { mfib_table_t *mfib_table; mfib_table = mfib_table_get(fib_index, prefix->fp_proto); /* * don't nobody go nowhere */ mfib_entry_lock(mfib_entry_index); if (mfib_entry_delete(mfib_entry_index, source)) { /* * last source gone. remove from the table */ mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index); } /* * else * still has sources, leave it be. */ mfib_entry_unlock(mfib_entry_index); } void mfib_table_entry_delete (u32 fib_index, const mfib_prefix_t *prefix, mfib_source_t source) { fib_node_index_t mfib_entry_index; mfib_entry_index = mfib_table_lookup_exact_match(fib_index, prefix); if (FIB_NODE_INDEX_INVALID == mfib_entry_index) { /* * removing an etry that does not exist. * i'll allow it, but i wo
# Copyright (c) 2018 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the Lice