/* * 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 /* Adjacency packet/byte counters indexed by adjacency index. */ vlib_combined_counter_main_t adjacency_counters; /* * the single adj pool */ ip_adjacency_t *adj_pool; /** * @brief Global Config for enabling per-adjacency counters. * By default these are disabled. */ int adj_per_adj_counters; always_inline void adj_poison (ip_adjacency_t * adj) { if (CLIB_DEBUG > 0) { memset (adj, 0xfe, sizeof (adj[0])); } } ip_adjacency_t * adj_alloc (fib_protocol_t proto) { ip_adjacency_t *adj; pool_get_aligned(adj_pool, adj, CLIB_CACHE_LINE_BYTES); adj_poison(adj); /* Make sure certain fields are always initialized. */ /* Validate adjacency counters. */ vlib_validate_combined_counter(&adjacency_counters, adj_get_index(adj)); fib_node_init(&adj->ia_node, FIB_NODE_TYPE_ADJ); adj->ia_nh_proto = proto; adj->ia_flags = 0; adj->rewrite_header.sw_if_index = ~0; adj->rewrite_header.flags = 0; adj->lookup_next_index = 0; adj->ia_delegates = NULL; /* lest it become a midchain in the future */ memset(&adj->sub_type.midchain.next_dpo, 0, sizeof(adj->sub_type.midchain.next_dpo)); return (adj); } static int adj_index_is_special (adj_index_t adj_index) { if (ADJ_INDEX_INVALID == adj_index) return (!0); return (0); } /** * @brief Pretty print helper function for formatting specific adjacencies. * @param s - input string to format * @param args - other args passed to format function such as: * - vnet_main_t * - ip_lookup_main_t * - adj_index */ u8 * format_ip_adjacency (u8 * s, va_list * args) { format_ip_adjacency_flags_t fiaf; ip_adjacency_t * adj; u32 adj_index; adj_index = va_arg (*args, u32); fiaf = va_arg (*args, format_ip_adjacency_flags_t); adj = adj_get(adj_index); switch (adj->lookup_next_index) { case IP_LOOKUP_NEXT_REWRITE: s = format (s, "%U", format_adj_nbr, adj_index, 0); break; case IP_LOOKUP_NEXT_ARP: s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0); break; case IP_LOOKUP_NEXT_GLEAN: s = format (s, "%U", format_adj_glean, adj_index, 0); break; case IP_LOOKUP_NEXT_MIDCHAIN: s = format (s, "%U", format_adj_midchain, adj_index, 2); break; case IP_LOOKUP_NEXT_MCAST: s = format (s, "%U", format_adj_mcast, adj_index, 0); break; case IP_LOOKUP_NEXT_MCAST_MIDCHAIN: s = format (s, "%U", format_adj_mcast_midchain, adj_index, 0); break; default: break; } if (fiaf & FORMAT_IP_ADJACENCY_DETAIL) { adj_delegate_type_t adt; adj_delegate_t *aed; vlib_counter_t counts; vlib_get_combined_counter(&adjacency_counters, adj_index, &counts); s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes); s = format (s, "\n locks:%d", adj->ia_node.fn_locks); s = format(s, "\n delegates:\n "); FOR_EACH_ADJ_DELEGATE(adj, adt, aed, { s = format(s, " %U\n", format_adj_deletegate, aed); }); s = format(s, "\n children:\n "); s = fib_node_children_format(adj->ia_node.fn_children, s); } return s; } /* * adj_last_lock_gone * * last lock/reference to the adj has gone, we no longer need it. */ static void adj_last_lock_gone (ip_adjacency_t *adj) { vlib_main_t * vm = vlib_get_main(); ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children)); ADJ_DBG(adj, "last-lock-gone"); vlib_worker_thread_barrier_sync (vm); switch (adj->lookup_next_index) { case IP_LOOKUP_NEXT_MIDCHAIN: dpo_reset(&adj->sub_type.midchain.next_dpo); /* FALL THROUGH */ case IP_LOOKUP_NEXT_ARP: case IP_LOOKUP_NEXT_REWRITE: /* * complete and incomplete nbr adjs */ adj_nbr_remove(adj_get_index(adj), adj->ia_nh_proto, adj->ia_link, &adj->sub_type.nbr.next_hop, adj->rewrite_header.sw_if_index); break; case IP_LOOKUP_NEXT_GLEAN: adj_glean_remove(adj->ia_nh_proto, adj->rewrite_header.sw_if_index); break; case IP_LOOKUP_NEXT_MCAST: case IP_LOOKUP_NEXT_MCAST_MIDCHAIN: adj_mcast_remove(adj->ia_nh_proto, adj->rewrite_header.sw_if_index); break; case IP_LOOKUP_NEXT_DROP: case IP_LOOKUP_NEXT_PUNT: case IP_LOOKUP_NEXT_LOCAL: case IP_LOOKUP_NEXT_ICMP_ERROR: case IP_LOOKUP_N_NEXT: /* * type not stored in any DB from which we need to remove it */ break; } vlib_worker_thread_barrier_release(vm); fib_node_deinit(&adj->ia_node); ASSERT(0 == vec_len(adj->ia_delegates)); vec_free(adj->ia_delegates); pool_put(adj_pool, adj); } void adj_lock (adj_index_t adj_index) { ip_adjacency_t *adj; if (adj_index_is_special(adj_index)) { return; } adj = adj_get(adj_index); ASSERT(adj); ADJ_DBG(adj, "lock"); fib_node_lock(&adj->ia_node); } void adj_unlock (adj_index_t adj_index) { ip_adjacency_t *adj; if (adj_index_is_special(adj_index)) { return; } adj = adj_get(adj_index); ASSERT(adj); ADJ_DBG(adj, "unlock"); ASSERT(adj); fib_node_unlock(&adj->ia_node); } u32 adj_child_add (adj_index_t adj_index, fib_node_type_t child_type, fib_node_index_t child_index) { ASSERT(ADJ_INDEX_INVALID != adj_index); if (adj_index_is_special(adj_index)) { return (~0); } return (fib_node_child_add(FIB_NODE_TYPE_ADJ, adj_index, child_type, child_index)); } void adj_child_remove (adj_index_t adj_index, u32 sibling_index) { if (adj_index_is_special(adj_index)) { return; } fib_node_child_remove(FIB_NODE_TYPE_ADJ, adj_index, sibling_index); } /* * Context for the walk to update the cached feture flags. */ typedef struct adj_feature_update_t_ { u8 arc; u8 enable; } adj_feature_update_ctx_t; static adj_walk_rc_t adj_feature_update_walk_cb (adj_index_t ai, void *arg) { adj_feature_update_ctx_t *ctx = arg; ip_adjacency_t *adj; adj = adj_get(ai); /* * this ugly mess matches the feature arc that is changing with affected * adjacencies */ if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) && (VNET_LINK_IP6 == adj->ia_link)) || ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) && (VNET_LINK_IP4 == adj->ia_link)) || ((ctx->arc == mpls_main.output_feature_arc_index) && (VNET_LINK_MPLS == adj->ia_link))) { if (ctx->enable) adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES; else adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES; } return (ADJ_WALK_RC_CONTINUE); } void adj_feature_update (u32 sw_if_index, u8 arc_index, u8 is_enable) { /* * Walk all the adjacencies on the interface to update the cached * 'has-features' flag */ adj_feature_update_ctx_t ctx = { .arc = arc_index, .enable = is_enable, }; adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx); } /** * @brief Walk the Adjacencies on a given interface */ void adj_walk (u32 sw_if_index, adj_walk_cb_t cb, void *ctx) { /* * walk all the neighbor adjacencies */ fib_protocol_t proto; FOR_EACH_FIB_IP_PROTOCOL(proto) { adj_nbr_walk(sw_if_index, proto, cb, ctx); adj_mcast_walk(sw_if_index, proto, cb, ctx); } } /** * @brief Return the link type of the adjacency */ vnet_link_t adj_get_link_type (adj_index_t ai) { const ip_adjacency_t *adj; adj = adj_get(ai); return (adj->ia_link); } /** * @brief Return