/* * 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 fib_table_t * fib_table_get (fib_node_index_t index, fib_protocol_t proto) { switch (proto) { case FIB_PROTOCOL_IP4: return (pool_elt_at_index(ip4_main.fibs, index)); case FIB_PROTOCOL_IP6: return (pool_elt_at_index(ip6_main.fibs, index)); case FIB_PROTOCOL_MPLS: return (pool_elt_at_index(mpls_main.fibs, index)); } ASSERT(0); return (NULL); } static inline fib_node_index_t fib_table_lookup_i (fib_table_t *fib_table, const fib_prefix_t *prefix) { switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: return (ip4_fib_table_lookup(ip4_fib_get(fib_table->ft_index), &prefix->fp_addr.ip4, prefix->fp_len)); case FIB_PROTOCOL_IP6: return (ip6_fib_table_lookup(fib_table->ft_index, &prefix->fp_addr.ip6, prefix->fp_len)); case FIB_PROTOCOL_MPLS: return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index), prefix->fp_label, prefix->fp_eos)); } return (FIB_NODE_INDEX_INVALID); } fib_node_index_t fib_table_lookup (u32 fib_index, const fib_prefix_t *prefix) { return (fib_table_lookup_i(fib_table_get(fib_index, prefix->fp_proto), prefix)); } static inline fib_node_index_t fib_table_lookup_exact_match_i (const fib_table_t *fib_table, const fib_prefix_t *prefix) { switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: return (ip4_fib_table_lookup_exact_match(ip4_fib_get(fib_table->ft_index), &prefix->fp_addr.ip4, prefix->fp_len)); case FIB_PROTOCOL_IP6: return (ip6_fib_table_lookup_exact_match(fib_table->ft_index, &prefix->fp_addr.ip6, prefix->fp_len)); case FIB_PROTOCOL_MPLS: return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index), prefix->fp_label, prefix->fp_eos)); } return (FIB_NODE_INDEX_INVALID); } fib_node_index_t fib_table_lookup_exact_match (u32 fib_index, const fib_prefix_t *prefix) { return (fib_table_lookup_exact_match_i(fib_table_get(fib_index, prefix->fp_proto), prefix)); } static fib_node_index_t fib_table_get_less_specific_i (fib_table_t *fib_table, const fib_prefix_t *prefix) { fib_prefix_t pfx; pfx = *prefix; if (FIB_PROTOCOL_MPLS == pfx.fp_proto) { return (FIB_NODE_INDEX_INVALID); } /* * in the absence of a tree structure for the table that allows for an O(1) * parent get, a cheeky way to find the cover is to LPM for the prefix with * mask-1. * there should always be a cover, though it may be the default route. the * default route's cover is the default route. */ if (pfx.fp_len != 0) { pfx.fp_len -= 1; } return (fib_table_lookup_i(fib_table, &pfx)); } fib_node_index_t fib_table_get_less_specific (u32 fib_index, const fib_prefix_t *prefix) { return (fib_table_get_less_specific_i(fib_table_get(fib_index, prefix->fp_proto), prefix)); } static void fib_table_entry_remove (fib_table_t *fib_table, const fib_prefix_t *prefix, fib_node_index_t fib_entry_index) { vlib_smp_unsafe_warning(); fib_table->ft_total_route_counts--; switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: ip4_fib_table_entry_remove(ip4_fib_get(fib_table->ft_index), &prefix->fp_addr.ip4, prefix->fp_len); break; case FIB_PROTOCOL_IP6: ip6_fib_table_entry_remove(fib_table->ft_index, &prefix->fp_addr.ip6, prefix->fp_len); break; case FIB_PROTOCOL_MPLS: mpls_fib_table_entry_remove(mpls_fib_get(fib_table->ft_index), prefix->fp_label, prefix->fp_eos); break; } fib_entry_unlock(fib_entry_index); } static void fib_table_post_insert_actions (fib_table_t *fib_table, const fib_prefix_t *prefix, fib_node_index_t fib_entry_index) { fib_node_index_t fib_entry_cover_index; /* * no cover relationships in the MPLS FIB */ if (FIB_PROTOCOL_MPLS == prefix->fp_proto) return; /* * find and inform the covering entry that a new more specific * has been inserted beneath it */ fib_entry_cover_index = fib_table_get_less_specific_i(fib_table, prefix); /* * the indicies are the same when the default route is first added */ if (fib_entry_cover_index != fib_entry_index) { fib_entry_cover_change_notify(fib_entry_cover_index, fib_entry_index); } } static void fib_table_entry_insert (fib_table_t *fib_table, const fib_prefix_t *prefix, fib_node_index_t fib_entry_index) { vlib_smp_unsafe_warning(); fib_entry_lock(fib_entry_index); fib_table->ft_total_route_counts++; switch (prefix->fp_proto) { case FIB_PROTOCOL_IP4: ip4_fib_table_entry_insert(ip4_fib_get(fib_table->ft_index), &prefix->fp_addr.ip4, prefix->fp_len, fib_entry_index); break; case FIB_PROTOCOL_IP6: ip6_fib_table_entry_insert(fib_table->ft_index, &prefix->fp_addr.ip6, prefix->fp_len, fib_entry_index); break; case FIB_PROTOCOL_MPLS: mpls_fib_table_entry_insert(mpls_fib_get(fib_table->ft_index),
# 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 License for the specific language governing permissions and
# limitations under the License.

get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)

find_path(VPP_INCLUDE_DIR PATH_SUFFIXES NAMES vppinfra/clib.h)
find_program(VPP_APIGEN vppapigen)
find_program(VPP_VAPI_C_GEN vapi_c_gen.py)
find_program(VPP_VAPI_CPP_GEN vapi_cpp_gen.py)

if(VPP_INCLUDE_DIR AND VPP_APIGEN)
  include_directories (${VPP_INCLUDE_DIR})
else()
  message(FATAL_ERROR "VPP headers, libraries and/or tools not found")
endif()

set(VPP_EXTERNAL_PROJECT 1)

include(CheckCCompilerFlag)

check_c_compiler_flag("-Wno-address-of-packed-member" compiler_flag_no_address_of_packed_member)
if (compiler_flag_no_address_of_packed_member)
  add_definitions(-Wno-address-of-packed-member)
endif()

set(VPP_RUNTIME_DIR "bin" CACHE STRING "Relative runtime directory path")
set(VPP_LIBRARY_DIR "lib" CACHE STRING "Relative library directory path")
set(VPP_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles)

include(${CMAKE_CURRENT_LIST_DIR}/cpu.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/api.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/library.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/plugin.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/pack.cmake)
_node_index_t fib_entry_index, const fib_prefix_t *prefix, fib_source_t source) { fib_entry_src_flag_t src_flag; fib_table_t *fib_table; int was_sourced; fib_table = fib_table_get(fib_index, prefix->fp_proto); was_sourced = fib_entry_is_sourced(fib_entry_index, source); /* * don't nobody go nowhere */ fib_entry_lock(fib_entry_index); src_flag = fib_entry_delete(fib_entry_index, source); if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag)) { /* * last source gone. remove from the table */ fib_table_entry_remove(fib_table, prefix, fib_entry_index); /* * now the entry is no longer in the table, we can * inform the entries that it covers to re-calculate their cover */ fib_entry_cover_change_notify(fib_entry_index, FIB_NODE_INDEX_INVALID); } /* * else * still has sources, leave it be. */ if (was_sourced != fib_entry_is_sourced(fib_entry_index, source)) { fib_table->ft_src_route_counts[source]--; } fib_entry_unlock(fib_entry_index); } void fib_table_entry_delete (u32 fib_index, const fib_prefix_t *prefix, fib_source_t source) { fib_node_index_t fib_entry_index; fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix); if (FIB_NODE_INDEX_INVALID == fib_entry_index) { /* * removing an etry that does not exist. * i'll allow it, but i won't like it. */ if (0) clib_warning("%U not in FIB", format_fib_prefix, prefix); } else { fib_table_entry_delete_i(fib_index, fib_entry_index, prefix, source); } } void fib_table_entry_delete_index (fib_node_index_t fib_entry_index, fib_source_t source) { fib_prefix_t prefix; fib_entry_get_prefix(fib_entry_index, &prefix); fib_table_entry_delete_i(fib_entry_get_fib_index(fib_entry_index), fib_entry_index, &prefix, source); } fib_node_index_t fib_table_entry_local_label_add (u32 fib_index, const fib_prefix_t *prefix, mpls_label_t label) { fib_node_index_t fib_entry_index; fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix); if (FIB_NODE_INDEX_INVALID == fib_entry_index || !fib_entry_is_sourced(fib_entry_index, FIB_SOURCE_MPLS)) { /* * only source the prefix once. this allows the label change * operation to work */ fib_entry_index = fib_table_entry_special_dpo_add(fib_index, prefix, FIB_SOURCE_MPLS, FIB_ENTRY_FLAG_NONE, NULL); } fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &label); return (fib_entry_index); } void fib_table_entry_local_label_remove (u32 fib_index, const fib_prefix_t *prefix, mpls_label_t label) { fib_node_index_t fib_entry_index; const void *data; mpls_label_t pl; fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix); if (FIB_NODE_INDEX_INVALID == fib_entry_index) return; data = fib_entry_get_source_data(fib_entry_index, FIB_SOURCE_MPLS); if (NULL == data) return; pl = *(mpls_label_t*)data; if (pl != label) return; pl = MPLS_LABEL_INVALID; fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &pl); fib_table_entry_special_remove(fib_index, prefix, FIB_SOURCE_MPLS); } u32 fib_table_get_index_for_sw_if_index (fib_protocol_t proto, u32 sw_if_index) { switch (proto) { case FIB_PROTOCOL_IP4: return (ip4_fib_table_get_index_for_sw_if_index(sw_if_index)); case FIB_PROTOCOL_IP6: return (ip6_fib_table_get_index_for_sw_if_index(sw_if_index)); case FIB_PROTOCOL_MPLS: return (mpls_fib_table_get_index_for_sw_if_index(sw_if_index)); } return (~0); } flow_hash_config_t fib_table_get_flow_hash_config (u32 fib_index, fib_protocol_t proto) { fib_table_t *fib; fib = fib_table_get(fib_index, proto); return (fib->ft_flow_hash_config); } flow_hash_config_t fib_table_get_default_flow_hash_config (fib_protocol_t proto) { switch (proto) { case FIB_PROTOCOL_IP4: case FIB_PROTOCOL_IP6: return (IP_FLOW_HASH_DEFAULT); case FIB_PROTOCOL_MPLS: return (MPLS_FLOW_HASH_DEFAULT); } ASSERT(0); return (IP_FLOW_HASH_DEFAULT); } /** * @brief Table set flow hash config context. */ typedef struct fib_table_set_flow_hash_config_ctx_t_ { /** * the flow hash config to set */ flow_hash_config_t hash_config; } fib_table_set_flow_hash_config_ctx_t; static int fib_table_set_flow_hash_config_cb (fib_node_index_t fib_entry_index, void *arg) { fib_table_set_flow_hash_config_ctx_t *ctx = arg; fib_entry_set_flow_hash_config(fib_entry_index, ctx->hash_config); return (1); } void fib_table_set_flow_hash_config (u32 fib_index, fib_protocol_t proto, flow_hash_config_t hash_config) { fib_table_set_flow_hash_config_ctx_t ctx = { .hash_config = hash_config, }; fib_table_t *fib; fib = fib_table_get(fib_index, proto); fib->ft_flow_hash_config = hash_config; fib_table_walk(fib_index, proto, fib_table_set_flow_hash_config_cb, &ctx); } u32 fib_table_get_table_id_for_sw_if_index (fib_protocol_t proto, u32 sw_if_index) { fib_table_t *fib_table; fib_table = fib_table_get(fib_table_get_index_for_sw_if_index( proto, sw_if_index), proto); return ((NULL != fib_table ? fib_table->ft_table_id : ~0)); } u32 fib_table_find (fib_protocol_t proto, u32 table_id) { switch (proto) { case FIB_PROTOCOL_IP4: return (ip4_fib_index_from_table_id(table_id)); case FIB_PROTOCOL_IP6: return (ip6_fib_index_from_table_id(table_id)); case FIB_PROTOCOL_MPLS: return (mpls_fib_index_from_table_id(table_id)); } return (~0); } static u32 fib_table_find_or_create_and_lock_i (fib_protocol_t proto, u32 table_id, fib_source_t src, const u8 *name) { fib_table_t *fib_table; fib_node_index_t fi; switch (proto) { case FIB_PROTOCOL_IP4: fi = ip4_fib_table_find_or_create_and_lock(table_id, src); break; case FIB_PROTOCOL_IP6: fi = ip6_fib_table_find_or_create_and_lock(table_id, src); break; case FIB_PROTOCOL_MPLS: fi = mpls_fib_table_find_or_create_and_lock(table_id, src); break; default: return (~0); } fib_table = fib_table_get(fi, proto); if (NULL == fib_table->ft_desc) { if (name && name[0]) { fib_table->ft_desc = format(NULL, "%s", name); } else { fib_table->ft_desc = format(NULL, "%U-VRF:%d", format_fib_protocol, proto, table_id); } } return (fi); } u32 fib_table_find_or_create_and_lock (fib_protocol_t proto, u32 table_id, fib_source_t src) { return (fib_table_find_or_create_and_lock_i(proto, table_id, src, NULL)); } u32 fib_table_find_or_create_and_lock_w_name (fib_protocol_t proto, u32 table_id, fib_source_t src, const u8 *name) { return (fib_table_find_or_create_and_lock_i(proto, table_id, src, name)); } u32 fib_table_create_and_lock (fib_protocol_t proto, fib_source_t src, const char *const fmt, ...) { fib_table_t *fib_table; fib_node_index_t fi; va_list ap; va_start(ap, fmt); switch (proto) { case FIB_PROTOCOL_IP4: fi = ip4_fib_table_create_and_lock(src); break; case FIB_PROTOCOL_IP6: fi = ip6_fib_table_create_and_lock(src); break; case FIB_PROTOCOL_MPLS: fi = mpls_fib_table_create_and_lock(src); break; default: return (~0); } fib_table = fib_table_get(fi, proto); fib_table->ft_desc = va_format(fib_table->ft_desc, fmt, &ap); va_end(ap); return (fi); } static void fib_table_destroy (fib_table_t *fib_table) { vec_free(fib_table->ft_desc); switch (fib_table->ft_proto) { case FIB_PROTOCOL_IP4: ip4_fib_table_destroy(fib_table->ft_index); break; case FIB_PROTOCOL_IP6: ip6_fib_table_destroy(fib_table->ft_index); break; case FIB_PROTOCOL_MPLS: mpls_fib_table_destroy(fib_table->ft_index); break; } } void fib_table_walk (u32 fib_index, fib_protocol_t proto, fib_table_walk_fn_t fn, void *ctx) { switch (proto) { case FIB_PROTOCOL_IP4: ip4_fib_table_walk(ip4_fib_get(fib_index), fn, ctx); break; case FIB_PROTOCOL_IP6: ip6_fib_table_walk(fib_index, fn, ctx); break; case FIB_PROTOCOL_MPLS: mpls_fib_table_walk(mpls_fib_get(fib_index), fn, ctx); break; } } void fib_table_unlock (u32 fib_index, fib_protocol_t proto, fib_source_t source) { fib_table_t *fib_table; fib_table = fib_table_get(fib_index, proto); fib_table->ft_locks[source]--; fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS]--; if (0 == fib_table->ft_locks[source]) { /* * The source no longer needs the table. flush any routes * from it just in case */ fib_table_flush(fib_index, proto, source); } if (0 == fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS]) { /* * no more locak from any source - kill it */ fib_table_destroy(fib_table); } } void fib_table_lock (u32 fib_index, fib_protocol_t proto, fib_source_t source) { fib_table_t *fib_table; fib_table = fib_table_get(fib_index, proto); fib_table->ft_locks[source]++; fib_table->ft_locks[FIB_TABLE_TOTAL_LOCKS]++; } u32 fib_table_get_num_entries (u32 fib_index, fib_protocol_t proto, fib_source_t source) { fib_table_t *fib_table; fib_table = fib_table_get(fib_index, proto); return (fib_table->ft_src_route_counts[source]); } u8* format_fib_table_name (u8* s, va_list* ap) { fib_node_index_t fib_index = va_arg(*ap, fib_node_index_t); fib_protocol_t proto = va_arg(*ap, int); // int promotion fib_table_t *fib_table; fib_table = fib_table_get(fib_index, proto); s = format(s, "%v", fib_table->ft_desc); return (s); } /** * @brief Table flush context. Store the indicies of matching FIB entries * that need to be removed. */ typedef struct fib_table_flush_ctx_t_ { /** * The list of entries to flush */ fib_node_index_t *ftf_entries; /** * The source we are flushing */ fib_source_t ftf_source; } fib_table_flush_ctx_t; static int fib_table_flush_cb (fib_node_index_t fib_entry_index, void *arg) { fib_table_flush_ctx_t *ctx = arg; if (fib_entry_is_sourced(fib_entry_index, ctx->ftf_source)) { vec_add1(ctx->ftf_entries, fib_entry_index); } return (1); } void fib_table_flush (u32 fib_index, fib_protocol_t proto, fib_source_t source) { fib_node_index_t *fib_entry_index; fib_table_flush_ctx_t ctx = { .ftf_entries = NULL, .ftf_source = source, }; fib_table_walk(fib_index, proto, fib_table_flush_cb, &ctx); vec_foreach(fib_entry_index, ctx.ftf_entries) { fib_table_entry_delete_index(*fib_entry_index, source); } vec_free(ctx.ftf_entries); }