From 7cd468a3d7dee7d6c92f69a0bb7061ae208ec727 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Mon, 19 Dec 2016 23:05:39 +0100 Subject: Reorganize source tree to use single autotools instance Change-Id: I7b51f88292e057c6443b12224486f2d0c9f8ae23 Signed-off-by: Damjan Marion --- src/vnet/adj/adj.c | 454 ++++++++++++++++++ src/vnet/adj/adj.h | 122 +++++ src/vnet/adj/adj_glean.c | 285 ++++++++++++ src/vnet/adj/adj_glean.h | 61 +++ src/vnet/adj/adj_internal.h | 104 +++++ src/vnet/adj/adj_l2.c | 194 ++++++++ src/vnet/adj/adj_l2.h | 24 + src/vnet/adj/adj_midchain.c | 559 ++++++++++++++++++++++ src/vnet/adj/adj_midchain.h | 102 ++++ src/vnet/adj/adj_nbr.c | 1087 +++++++++++++++++++++++++++++++++++++++++++ src/vnet/adj/adj_nbr.h | 176 +++++++ src/vnet/adj/adj_rewrite.c | 53 +++ src/vnet/adj/adj_rewrite.h | 49 ++ src/vnet/adj/adj_types.h | 53 +++ 14 files changed, 3323 insertions(+) create mode 100644 src/vnet/adj/adj.c create mode 100644 src/vnet/adj/adj.h create mode 100644 src/vnet/adj/adj_glean.c create mode 100644 src/vnet/adj/adj_glean.h create mode 100644 src/vnet/adj/adj_internal.h create mode 100644 src/vnet/adj/adj_l2.c create mode 100644 src/vnet/adj/adj_l2.h create mode 100644 src/vnet/adj/adj_midchain.c create mode 100644 src/vnet/adj/adj_midchain.h create mode 100644 src/vnet/adj/adj_nbr.c create mode 100644 src/vnet/adj/adj_nbr.h create mode 100644 src/vnet/adj/adj_rewrite.c create mode 100644 src/vnet/adj/adj_rewrite.h create mode 100644 src/vnet/adj/adj_types.h (limited to 'src/vnet/adj') diff --git a/src/vnet/adj/adj.c b/src/vnet/adj/adj.c new file mode 100644 index 00000000..e740c4cb --- /dev/null +++ b/src/vnet/adj/adj.c @@ -0,0 +1,454 @@ +/* + * 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 + +/* + * Special Adj with index zero. we need to define this since the v4 mtrie + * assumes an index of 0 implies the ply is empty. therefore all 'real' + * adjs need a non-zero index. + */ +static ip_adjacency_t *special_v4_miss_adj_with_index_zero; + +/* Adjacency packet/byte counters indexed by adjacency index. */ +vlib_combined_counter_main_t adjacency_counters; + +/* + * the single adj pool + */ +ip_adjacency_t *adj_pool; + +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(adj_pool, adj); + + adj_poison(adj); + + /* Make sure certain fields are always initialized. */ + /* Validate adjacency counters. */ + vlib_validate_combined_counter(&adjacency_counters, + adj_get_index(adj)); + + adj->rewrite_header.sw_if_index = ~0; + adj->mcast_group_index = ~0; + adj->saved_lookup_next_index = 0; + adj->n_adj = 1; + adj->lookup_next_index = 0; + + fib_node_init(&adj->ia_node, + FIB_NODE_TYPE_ADJ); + adj->ia_nh_proto = proto; + adj->ia_flags = 0; + + ip4_main.lookup_main.adjacency_heap = adj_pool; + ip6_main.lookup_main.adjacency_heap = adj_pool; + + 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; + default: + break; + } + + if (fiaf & FORMAT_IP_ADJACENCY_DETAIL) + { + s = format (s, "\n locks:%d", adj->ia_node.fn_locks); + s = format (s, " node:[%d]:%U", + adj->rewrite_header.node_index, + format_vlib_node_name, vlib_get_main(), + adj->rewrite_header.node_index); + s = format (s, " next:[%d]:%U", + adj->rewrite_header.next_index, + format_vlib_next_node_name, + vlib_get_main(), + adj->rewrite_header.node_index, + adj->rewrite_header.next_index); + 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; + default: + /* + * 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); + 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); +} + +/** + * @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 the sw interface index of the adjacency. + */ +u32 +adj_get_sw_if_index (adj_index_t ai) +{ + const ip_adjacency_t *adj; + + adj = adj_get(ai); + + return (adj->rewrite_header.sw_if_index); +} + +/** + * @brief Return the link type of the adjacency + */ +const u8* +adj_get_rewrite (adj_index_t ai) +{ + vnet_rewrite_header_t *rw; + ip_adjacency_t *adj; + + adj = adj_get(ai); + rw = &adj->rewrite_header; + + ASSERT (rw->data_bytes != 0xfefe); + + return (rw->data - rw->data_bytes); +} + +static fib_node_t * +adj_get_node (fib_node_index_t index) +{ + ip_adjacency_t *adj; + + adj = adj_get(index); + + return (&adj->ia_node); +} + +#define ADJ_FROM_NODE(_node) \ + ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node))) + +static void +adj_node_last_lock_gone (fib_node_t *node) +{ + adj_last_lock_gone(ADJ_FROM_NODE(node)); +} + +static fib_node_back_walk_rc_t +adj_back_walk_notify (fib_node_t *node, + fib_node_back_walk_ctx_t *ctx) +{ + /* + * Que pasa. yo soj en el final! + */ + ASSERT(0); + + return (FIB_NODE_BACK_WALK_CONTINUE); +} + +/* + * Adjacency's graph node virtual function table + */ +static const fib_node_vft_t adj_vft = { + .fnv_get = adj_get_node, + .fnv_last_lock = adj_node_last_lock_gone, + .fnv_back_walk = adj_back_walk_notify, +}; + +static clib_error_t * +adj_module_init (vlib_main_t * vm) +{ + fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft); + + adj_nbr_module_init(); + adj_glean_module_init(); + adj_midchain_module_init(); + + /* + * one special adj to reserve index 0 + */ + special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4); + + return (NULL); +} + +VLIB_INIT_FUNCTION (adj_module_init); + +static clib_error_t * +adj_show (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + adj_index_t ai = ADJ_INDEX_INVALID; + u32 sw_if_index = ~0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "%d", &ai)) + ; + else if (unformat (input, "%U", + unformat_vnet_sw_interface, vnet_get_main(), + &sw_if_index)) + ; + else + break; + } + + if (ADJ_INDEX_INVALID != ai) + { + if (pool_is_free_index(adj_pool, ai)) + { + vlib_cli_output (vm, "adjacency %d invalid", ai); + return 0; + } + + vlib_cli_output (vm, "[@%d] %U", + ai, + format_ip_adjacency, ai, + FORMAT_IP_ADJACENCY_DETAIL); + } + else + { + /* *INDENT-OFF* */ + pool_foreach_index(ai, adj_pool, + ({ + if (~0 != sw_if_index && + sw_if_index != adj_get_sw_if_index(ai)) + { + } + else + { + vlib_cli_output (vm, "[@%d] %U", + ai, + format_ip_adjacency, ai, + FORMAT_IP_ADJACENCY_NONE); + } + })); + /* *INDENT-ON* */ + } + + return 0; +} + +/*? + * Show all adjacencies. + * @cliexpar + * @cliexstart{sh adj} + * [@0] + * [@1] glean: loop0 + * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * @cliexend + ?*/ +VLIB_CLI_COMMAND (adj_show_command, static) = { + .path = "show adj", + .short_help = "show adj [] [interface]", + .function = adj_show, +}; + +/* + * DEPRECATED: DO NOT USE + */ +ip_adjacency_t * +ip_add_adjacency (ip_lookup_main_t * lm, + ip_adjacency_t * copy_adj, + u32 n_adj, + u32 * adj_index_return) +{ + ip_adjacency_t * adj; + + ASSERT(1==n_adj); + + adj = adj_alloc(FIB_PROTOCOL_IP4); + + if (copy_adj) + *adj = *copy_adj; + + *adj_index_return = adj_get_index(adj); + return adj; +} diff --git a/src/vnet/adj/adj.h b/src/vnet/adj/adj.h new file mode 100644 index 00000000..e85625db --- /dev/null +++ b/src/vnet/adj/adj.h @@ -0,0 +1,122 @@ +/* + * 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. + */ +/** + * An adjacency is a representation of an attached L3 peer. + * + * Adjacency Sub-types: + * - neighbour: a representation of an attached L3 peer. + * Key:{addr,interface,link/ether-type} + * SHARED + * - glean: used to drive ARP/ND for packets destined to a local sub-net. + * 'glean' mean use the packet's destination address as the target + * address in the ARP packet. + * UNSHARED. Only one per-interface. + * - midchain: a nighbour adj on a virtual/tunnel interface. + * - rewrite: an adj with no key, but with a rewrite string. + * + * The API to create and update the adjacency is very sub-type specific. This + * is intentional as it encourages the user to carefully consider which adjacency + * sub-type they are really using, and hence assign it data in the appropriate + * sub-type space in the union of sub-types. This prevents the adj becoming a + * disorganised dumping group for 'my features needs a u16 somewhere' data. It + * is important to enforce this approach as space in the adjacency is a premium, + * as we need it to fit in 1 cache line. + * + * the API is also based around an index to an ajdacency not a raw pointer. This + * is so the user doesn't suffer the same limp inducing firearm injuries that + * the author suffered as the adjacenices can realloc. + */ + +#ifndef __ADJ_H__ +#define __ADJ_H__ + +#include +#include +#include +#include +#include + +/** + * @brief + * Take a reference counting lock on the adjacency + */ +extern void adj_lock(adj_index_t adj_index); +/** + * @brief + * Release a reference counting lock on the adjacency + */ +extern void adj_unlock(adj_index_t adj_index); + +/** + * @brief + * Add a child dependent to an adjacency. The child will + * thus be informed via its registerd back-walk function + * when the adjacency state changes. + */ +extern u32 adj_child_add(adj_index_t adj_index, + fib_node_type_t type, + fib_node_index_t child_index); +/** + * @brief + * Remove a child dependent + */ +extern void adj_child_remove(adj_index_t adj_index, + u32 sibling_index); + +/** + * @brief Walk the Adjacencies on a given interface + */ +extern void adj_walk (u32 sw_if_index, + adj_walk_cb_t cb, + void *ctx); + +/** + * @brief Return the link type of the adjacency + */ +extern vnet_link_t adj_get_link_type (adj_index_t ai); + +/** + * @brief Return the sw interface index of the adjacency. + */ +extern u32 adj_get_sw_if_index (adj_index_t ai); + +/** + * @brief Return the link type of the adjacency + */ +extern const u8* adj_get_rewrite (adj_index_t ai); + +/** + * @brief + * The global adjacnecy pool. Exposed for fast/inline data-plane access + */ +extern ip_adjacency_t *adj_pool; + +/** + * @brief + * Adjacency packet counters + */ +extern vlib_combined_counter_main_t adjacency_counters; + +/** + * @brief + * Get a pointer to an adjacency object from its index + */ +static inline ip_adjacency_t * +adj_get (adj_index_t adj_index) +{ + return (vec_elt_at_index(adj_pool, adj_index)); +} + +#endif diff --git a/src/vnet/adj/adj_glean.c b/src/vnet/adj/adj_glean.c new file mode 100644 index 00000000..8d86e2a9 --- /dev/null +++ b/src/vnet/adj/adj_glean.c @@ -0,0 +1,285 @@ +/* + * 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 + +/* + * The 'DB' of all glean adjs. + * There is only one glean per-interface per-protocol, so this is a per-interface + * vector + */ +static adj_index_t *adj_gleans[FIB_PROTOCOL_MAX]; + +static inline vlib_node_registration_t* +adj_get_glean_node (fib_protocol_t proto) +{ + switch (proto) { + case FIB_PROTOCOL_IP4: + return (&ip4_glean_node); + case FIB_PROTOCOL_IP6: + return (&ip6_glean_node); + case FIB_PROTOCOL_MPLS: + break; + } + ASSERT(0); + return (NULL); +} + +/* + * adj_glean_add_or_lock + * + * The next_hop address here is used for source address selection in the DP. + * The glean adj is added to an interface's connected prefix, the next-hop + * passed here is the local prefix on the same interface. + */ +adj_index_t +adj_glean_add_or_lock (fib_protocol_t proto, + u32 sw_if_index, + const ip46_address_t *nh_addr) +{ + ip_adjacency_t * adj; + + vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID); + + if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) + { + adj = adj_alloc(proto); + + adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN; + adj->ia_nh_proto = proto; + adj_gleans[proto][sw_if_index] = adj_get_index(adj); + + if (NULL != nh_addr) + { + adj->sub_type.glean.receive_addr = *nh_addr; + } + + adj->rewrite_header.data_bytes = 0; + + vnet_rewrite_for_sw_interface(vnet_get_main(), + adj_fib_proto_2_nd(proto), + sw_if_index, + adj_get_glean_node(proto)->index, + VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST, + &adj->rewrite_header, + sizeof (adj->rewrite_data)); + } + else + { + adj = adj_get(adj_gleans[proto][sw_if_index]); + } + + adj_lock(adj_get_index(adj)); + + return (adj_get_index(adj)); +} + +void +adj_glean_remove (fib_protocol_t proto, + u32 sw_if_index) +{ + ASSERT(sw_if_index < vec_len(adj_gleans[proto])); + + adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID; +} + +static clib_error_t * +adj_glean_interface_state_change (vnet_main_t * vnm, + u32 sw_if_index, + u32 flags) +{ + /* + * for each glean on the interface trigger a walk back to the children + */ + fib_protocol_t proto; + ip_adjacency_t *adj; + + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + if (sw_if_index >= vec_len(adj_gleans[proto]) || + ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) + continue; + + adj = adj_get(adj_gleans[proto][sw_if_index]); + + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? + FIB_NODE_BW_REASON_FLAG_INTERFACE_UP : + FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN), + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx); + } + + return (NULL); +} + +VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change); + +/** + * @brief Invoked on each SW interface of a HW interface when the + * HW interface state changes + */ +static void +adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm, + u32 sw_if_index, + void *arg) +{ + adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg); +} + +/** + * @brief Registered callback for HW interface state changes + */ +static clib_error_t * +adj_glean_hw_interface_state_change (vnet_main_t * vnm, + u32 hw_if_index, + u32 flags) +{ + /* + * walk SW interfaces on the HW + */ + uword sw_flags; + + sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? + VNET_SW_INTERFACE_FLAG_ADMIN_UP : + 0); + + vnet_hw_interface_walk_sw(vnm, hw_if_index, + adj_nbr_hw_sw_interface_state_change, + (void*) sw_flags); + + return (NULL); +} + +VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION( + adj_glean_hw_interface_state_change); + +static clib_error_t * +adj_glean_interface_delete (vnet_main_t * vnm, + u32 sw_if_index, + u32 is_add) +{ + /* + * for each glean on the interface trigger a walk back to the children + */ + fib_protocol_t proto; + ip_adjacency_t *adj; + + if (is_add) + { + /* + * not interested in interface additions. we will not back walk + * to resolve paths through newly added interfaces. Why? The control + * plane should have the brains to add interfaces first, then routes. + * So the case where there are paths with a interface that matches + * one just created is the case where the path resolved through an + * interface that was deleted, and still has not been removed. The + * new interface added, is NO GUARANTEE that the interface being + * added now, even though it may have the same sw_if_index, is the + * same interface that the path needs. So tough! + * If the control plane wants these routes to resolve it needs to + * remove and add them again. + */ + return (NULL); + } + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + if (sw_if_index >= vec_len(adj_gleans[proto]) || + ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) + continue; + + adj = adj_get(adj_gleans[proto][sw_if_index]); + + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE, + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx); + } + + return (NULL); +} + +VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete); + +u8* +format_adj_glean (u8* s, va_list *ap) +{ + index_t index = va_arg(*ap, index_t); + CLIB_UNUSED(u32 indent) = va_arg(*ap, u32); + vnet_main_t * vnm = vnet_get_main(); + ip_adjacency_t * adj = adj_get(index); + + return (format(s, "%U-glean: %U", + format_fib_protocol, adj->ia_nh_proto, + format_vnet_sw_interface_name, + vnm, + vnet_get_sw_interface(vnm, + adj->rewrite_header.sw_if_index))); +} + + +static void +adj_dpo_lock (dpo_id_t *dpo) +{ + adj_lock(dpo->dpoi_index); +} +static void +adj_dpo_unlock (dpo_id_t *dpo) +{ + adj_unlock(dpo->dpoi_index); +} + +const static dpo_vft_t adj_glean_dpo_vft = { + .dv_lock = adj_dpo_lock, + .dv_unlock = adj_dpo_unlock, + .dv_format = format_adj_glean, +}; + +/** + * @brief The per-protocol VLIB graph nodes that are assigned to a glean + * object. + * + * this means that these graph nodes are ones from which a glean is the + * parent object in the DPO-graph. + */ +const static char* const glean_ip4_nodes[] = +{ + "ip4-glean", + NULL, +}; +const static char* const glean_ip6_nodes[] = +{ + "ip6-glean", + NULL, +}; + +const static char* const * const glean_nodes[DPO_PROTO_NUM] = +{ + [DPO_PROTO_IP4] = glean_ip4_nodes, + [DPO_PROTO_IP6] = glean_ip6_nodes, + [DPO_PROTO_MPLS] = NULL, +}; + +void +adj_glean_module_init (void) +{ + dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes); +} diff --git a/src/vnet/adj/adj_glean.h b/src/vnet/adj/adj_glean.h new file mode 100644 index 00000000..640bd2f9 --- /dev/null +++ b/src/vnet/adj/adj_glean.h @@ -0,0 +1,61 @@ +/* + * 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. + */ + +/** + * @brief Glean Adjacency + * + * A gleean adjacency represent the need to discover new peers on an + * attached link. Packets that hit a glean adjacency will generate an + * ARP/ND packet addessesed to the packet's destination address. + * Note this is different to an incomplete neighbour adjacency, which + * does not send ARP/ND requests to the packet's destination address, + * but instead to the next-hop address of the adjacency itself. + */ + +#ifndef __ADJ_GLEAN_H__ +#define __ADJ_GLEAN_H__ + +#include + +/** + * @brief + * Add (and lock) a new or lock an existing glean adjacency + * + * @param proto + * The protocol for the neighbours that we wish to glean + * + * @param sw_if_index + * The interface on which to glean + * + * @param nh_addr + * the address applied to the interface on which to glean. This + * as the source address in packets when the ARP/ND packet is sent + */ +extern adj_index_t adj_glean_add_or_lock(fib_protocol_t proto, + u32 sw_if_index, + const ip46_address_t *nh_addr); + +/** + * @brief Format/display a glean adjacency. + */ +extern u8* format_adj_glean(u8* s, va_list *ap); + +/** + * @brief + * Module initialisation + */ +extern void adj_glean_module_init(void); + +#endif diff --git a/src/vnet/adj/adj_internal.h b/src/vnet/adj/adj_internal.h new file mode 100644 index 00000000..833bc7c9 --- /dev/null +++ b/src/vnet/adj/adj_internal.h @@ -0,0 +1,104 @@ +/* + * 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. + */ + +#ifndef __ADJ_INTERNAL_H__ +#define __ADJ_INTERNAL_H__ + +#include +#include +#include +#include + + +/** + * big switch to turn on Adjacency debugging + */ +#undef ADJ_DEBUG + +/* + * Debug macro + */ +#ifdef ADJ_DEBUG +#define ADJ_DBG(_adj, _fmt, _args...) \ +{ \ + clib_warning("adj:[%d:%p]:" _fmt, \ + _adj - adj_pool, _adj, \ + ##_args); \ +} +#else +#define ADJ_DBG(_e, _fmt, _args...) +#endif + +static inline u32 +adj_get_rewrite_node (vnet_link_t linkt) +{ + switch (linkt) { + case VNET_LINK_IP4: + return (ip4_rewrite_node.index); + case VNET_LINK_IP6: + return (ip6_rewrite_node.index); + case VNET_LINK_MPLS: + return (mpls_output_node.index); + case VNET_LINK_ETHERNET: + return (adj_l2_rewrite_node.index); + case VNET_LINK_ARP: + break; + } + ASSERT(0); + return (0); +} + +static inline vnet_link_t +adj_fib_proto_2_nd (fib_protocol_t fp) +{ + switch (fp) + { + case FIB_PROTOCOL_IP4: + return (VNET_LINK_ARP); + case FIB_PROTOCOL_IP6: + return (VNET_LINK_IP6); + case FIB_PROTOCOL_MPLS: + return (VNET_LINK_MPLS); + } + return (0); +} + +/** + * @brief + * Get a pointer to an adjacency object from its index + */ +static inline adj_index_t +adj_get_index (ip_adjacency_t *adj) +{ + return (adj - adj_pool); +} + +extern void adj_nbr_update_rewrite_internal (ip_adjacency_t *adj, + ip_lookup_next_t adj_next_index, + u32 complete_next_index, + u32 next_index, + u8 *rewrite); + +extern ip_adjacency_t * adj_alloc(fib_protocol_t proto); + +extern void adj_nbr_remove(adj_index_t ai, + fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index); +extern void adj_glean_remove(fib_protocol_t proto, + u32 sw_if_index); + +#endif diff --git a/src/vnet/adj/adj_l2.c b/src/vnet/adj/adj_l2.c new file mode 100644 index 00000000..4d2dd708 --- /dev/null +++ b/src/vnet/adj/adj_l2.c @@ -0,0 +1,194 @@ +/* + * 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 + +/** + * @brief Trace data for a L2 Midchain + */ +typedef struct adj_l2_trace_t_ { + /** Adjacency index taken. */ + u32 adj_index; +} adj_l2_trace_t; + +static u8 * +format_adj_l2_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + adj_l2_trace_t * t = va_arg (*args, adj_l2_trace_t *); + + s = format (s, "adj-idx %d : %U", + t->adj_index, + format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE); + return s; +} + +typedef enum adj_l2_rewrite_next_t_ +{ + ADJ_L2_REWRITE_NEXT_DROP, +} adj_l2_rewrite_next_t; + +always_inline uword +adj_l2_rewrite_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame, + int is_midchain) +{ + u32 * from = vlib_frame_vector_args (frame); + u32 n_left_from, n_left_to_next, * to_next, next_index; + u32 cpu_index = os_get_cpu_number(); + ethernet_main_t * em = ðernet_main; + + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + ip_adjacency_t * adj0; + vlib_buffer_t * p0; + char *h0; + u32 pi0, rw_len0, adj_index0, next0 = 0; + u32 tx_sw_if_index0; + + pi0 = to_next[0] = from[0]; + from += 1; + n_left_from -= 1; + to_next += 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, pi0); + h0 = vlib_buffer_get_current (p0); + + adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; + + /* We should never rewrite a pkt using the MISS adjacency */ + ASSERT(adj_index0); + + adj0 = adj_get (adj_index0); + + /* Guess we are only writing on simple Ethernet header. */ + vnet_rewrite_one_header (adj0[0], h0, + sizeof (ethernet_header_t)); + + /* Update packet buffer attributes/set output interface. */ + rw_len0 = adj0[0].rewrite_header.data_bytes; + vnet_buffer(p0)->ip.save_rewrite_length = rw_len0; + + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, adj_index0, + /* packet increment */ 0, + /* byte increment */ rw_len0-sizeof(ethernet_header_t)); + + /* Check MTU of outgoing interface. */ + if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0) <= + adj0[0].rewrite_header.max_l3_packet_bytes))) + { + /* Don't adjust the buffer for ttl issue; icmp-error node wants + * to see the IP headerr */ + p0->current_data -= rw_len0; + p0->current_length += rw_len0; + tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index; + + if (is_midchain) + { + adj0->sub_type.midchain.fixup_func(vm, adj0, p0); + } + + vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0; + + /* + * Follow the feature ARC. this will result eventually in + * the midchain-tx node + */ + vnet_feature_arc_start(em->output_feature_arc_index, tx_sw_if_index0, &next0, p0); + } + else + { + /* can't fragment L2 */ + next0 = ADJ_L2_REWRITE_NEXT_DROP; + } + + if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) + { + adj_l2_trace_t *tr = vlib_add_trace (vm, node, + p0, sizeof (*tr)); + tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX]; + } + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + pi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return frame->n_vectors; +} + +static uword +adj_l2_rewrite (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return adj_l2_rewrite_inline (vm, node, frame, 0); +} + +static uword +adj_l2_midchain (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return adj_l2_rewrite_inline (vm, node, frame, 1); +} + +VLIB_REGISTER_NODE (adj_l2_rewrite_node) = { + .function = adj_l2_rewrite, + .name = "adj-l2-rewrite", + .vector_size = sizeof (u32), + + .format_trace = format_adj_l2_trace, + + .n_next_nodes = 1, + .next_nodes = { + [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_rewrite_node, adj_l2_rewrite) + +VLIB_REGISTER_NODE (adj_l2_midchain_node) = { + .function = adj_l2_midchain, + .name = "adj-l2-midchain", + .vector_size = sizeof (u32), + + .format_trace = format_adj_l2_trace, + + .n_next_nodes = 1, + .next_nodes = { + [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_midchain_node, adj_l2_midchain) diff --git a/src/vnet/adj/adj_l2.h b/src/vnet/adj/adj_l2.h new file mode 100644 index 00000000..3aa1c74b --- /dev/null +++ b/src/vnet/adj/adj_l2.h @@ -0,0 +1,24 @@ +/* + * 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. + */ + +#ifndef __ADJ_L2_H__ +#define __ADJ_L2_H__ + +#include + +extern vlib_node_registration_t adj_l2_midchain_node; +extern vlib_node_registration_t adj_l2_rewrite_node; + +#endif diff --git a/src/vnet/adj/adj_midchain.c b/src/vnet/adj/adj_midchain.c new file mode 100644 index 00000000..8c6ab5aa --- /dev/null +++ b/src/vnet/adj/adj_midchain.c @@ -0,0 +1,559 @@ +/* + * 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 + +/** + * The two midchain tx feature node indices + */ +static u32 adj_midchain_tx_feature_node[VNET_LINK_NUM]; +static u32 adj_midchain_tx_no_count_feature_node[VNET_LINK_NUM]; + +/** + * @brief Trace data for packets traversing the midchain tx node + */ +typedef struct adj_midchain_tx_trace_t_ +{ + /** + * @brief the midchain adj we are traversing + */ + adj_index_t ai; +} adj_midchain_tx_trace_t; + +always_inline uword +adj_midchain_tx_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame, + int interface_count) +{ + u32 * from, * to_next, n_left_from, n_left_to_next; + u32 next_index; + vnet_main_t *vnm = vnet_get_main (); + vnet_interface_main_t *im = &vnm->interface_main; + u32 cpu_index = vm->cpu_index; + + /* Vector of buffer / pkt indices we're supposed to process */ + from = vlib_frame_vector_args (frame); + + /* Number of buffers / pkts */ + n_left_from = frame->n_vectors; + + /* Speculatively send the first buffer to the last disposition we used */ + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + /* set up to enqueue to our disposition with index = next_index */ + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + + while (n_left_from >= 4 && n_left_to_next > 2) + { + u32 bi0, adj_index0, next0; + const ip_adjacency_t * adj0; + const dpo_id_t *dpo0; + vlib_buffer_t * b0; + u32 bi1, adj_index1, next1; + const ip_adjacency_t * adj1; + const dpo_id_t *dpo1; + vlib_buffer_t * b1; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + bi0 = from[0]; + to_next[0] = bi0; + bi1 = from[1]; + to_next[1] = bi1; + + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer(vm, bi0); + b1 = vlib_get_buffer(vm, bi1); + + /* Follow the DPO on which the midchain is stacked */ + adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX]; + adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX]; + + adj0 = adj_get(adj_index0); + adj1 = adj_get(adj_index1); + + dpo0 = &adj0->sub_type.midchain.next_dpo; + dpo1 = &adj1->sub_type.midchain.next_dpo; + + next0 = dpo0->dpoi_next_node; + next1 = dpo1->dpoi_next_node; + + vnet_buffer(b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index; + vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; + + if (interface_count) + { + vlib_increment_combined_counter (im->combined_sw_if_counters + + VNET_INTERFACE_COUNTER_TX, + cpu_index, + adj0->rewrite_header.sw_if_index, + 1, + vlib_buffer_length_in_chain (vm, b0)); + vlib_increment_combined_counter (im->combined_sw_if_counters + + VNET_INTERFACE_COUNTER_TX, + cpu_index, + adj1->rewrite_header.sw_if_index, + 1, + vlib_buffer_length_in_chain (vm, b1)); + } + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node, + b0, sizeof (*tr)); + tr->ai = adj_index0; + } + if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) + { + adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node, + b1, sizeof (*tr)); + tr->ai = adj_index1; + } + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, + next0, next1); + } + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0, adj_index0, next0; + const ip_adjacency_t * adj0; + const dpo_id_t *dpo0; + vlib_buffer_t * b0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer(vm, bi0); + + /* Follow the DPO on which the midchain is stacked */ + adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX]; + adj0 = adj_get(adj_index0); + dpo0 = &adj0->sub_type.midchain.next_dpo; + next0 = dpo0->dpoi_next_node; + vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; + + if (interface_count) + { + vlib_increment_combined_counter (im->combined_sw_if_counters + + VNET_INTERFACE_COUNTER_TX, + cpu_index, + adj0->rewrite_header.sw_if_index, + 1, + vlib_buffer_length_in_chain (vm, b0)); + } + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node, + b0, sizeof (*tr)); + tr->ai = adj_index0; + } + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return frame->n_vectors; +} + +static u8 * +format_adj_midchain_tx_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*); + + s = format(s, "adj-midchain:[%d]:%U", tr->ai, + format_ip_adjacency, tr->ai, + FORMAT_IP_ADJACENCY_NONE); + + return (s); +} + +static uword +adj_midchain_tx (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return (adj_midchain_tx_inline(vm, node, frame, 1)); +} + +VLIB_REGISTER_NODE (adj_midchain_tx_node, static) = { + .function = adj_midchain_tx, + .name = "adj-midchain-tx", + .vector_size = sizeof (u32), + + .format_trace = format_adj_midchain_tx_trace, + + .n_next_nodes = 1, + .next_nodes = { + [0] = "error-drop", + }, +}; + +static uword +adj_midchain_tx_no_count (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return (adj_midchain_tx_inline(vm, node, frame, 0)); +} + +VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node, static) = { + .function = adj_midchain_tx_no_count, + .name = "adj-midchain-tx-no-count", + .vector_size = sizeof (u32), + + .format_trace = format_adj_midchain_tx_trace, + + .n_next_nodes = 1, + .next_nodes = { + [0] = "error-drop", + }, +}; + +VNET_FEATURE_INIT (adj_midchain_tx_ip4, static) = { + .arc_name = "ip4-output", + .node_name = "adj-midchain-tx", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP4], +}; +VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip4, static) = { + .arc_name = "ip4-output", + .node_name = "adj-midchain-tx-no-count", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP4], +}; +VNET_FEATURE_INIT (adj_midchain_tx_ip6, static) = { + .arc_name = "ip6-output", + .node_name = "adj-midchain-tx", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP6], +}; +VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip6, static) = { + .arc_name = "ip6-output", + .node_name = "adj-midchain-tx-no-count", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP6], +}; +VNET_FEATURE_INIT (adj_midchain_tx_mpls, static) = { + .arc_name = "mpls-output", + .node_name = "adj-midchain-tx", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_MPLS], +}; +VNET_FEATURE_INIT (adj_midchain_tx_no_count_mpls, static) = { + .arc_name = "mpls-output", + .node_name = "adj-midchain-tx-no-count", + .runs_before = VNET_FEATURES ("interface-output"), + .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_MPLS], +}; +VNET_FEATURE_INIT (adj_midchain_tx_ethernet, static) = { + .arc_name = "ethernet-output", + .node_name = "adj-midchain-tx", + .runs_before = VNET_FEATURES ("error-drop"), + .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_ETHERNET], +}; +VNET_FEATURE_INIT (adj_midchain_tx_no_count_ethernet, static) = { + .arc_name = "ethernet-output", + .node_name = "adj-midchain-tx-no-count", + .runs_before = VNET_FEATURES ("error-drop"), + .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_ETHERNET], +}; + +static inline u32 +adj_get_midchain_node (vnet_link_t link) +{ + switch (link) { + case VNET_LINK_IP4: + return (ip4_midchain_node.index); + case VNET_LINK_IP6: + return (ip6_midchain_node.index); + case VNET_LINK_MPLS: + return (mpls_midchain_node.index); + case VNET_LINK_ETHERNET: + return (adj_l2_midchain_node.index); + case VNET_LINK_ARP: + break; + } + ASSERT(0); + return (0); +} + +static u8 +adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj) +{ + u8 arc = (u8) ~0; + switch (adj->ia_link) + { + case VNET_LINK_IP4: + { + arc = ip4_main.lookup_main.output_feature_arc_index; + break; + } + case VNET_LINK_IP6: + { + arc = ip6_main.lookup_main.output_feature_arc_index; + break; + } + case VNET_LINK_MPLS: + { + arc = mpls_main.output_feature_arc_index; + break; + } + case VNET_LINK_ETHERNET: + { + arc = ethernet_main.output_feature_arc_index; + break; + } + case VNET_LINK_ARP: + ASSERT(0); + break; + } + + ASSERT (arc != (u8) ~0); + + return (arc); +} + +/** + * adj_nbr_midchain_update_rewrite + * + * Update the adjacency's rewrite string. A NULL string implies the + * rewrite is reset (i.e. when ARP/ND etnry is gone). + * NB: the adj being updated may be handling traffic in the DP. + */ +void +adj_nbr_midchain_update_rewrite (adj_index_t adj_index, + adj_midchain_fixup_t fixup, + adj_midchain_flag_t flags, + u8 *rewrite) +{ + ip_adjacency_t *adj; + u8 arc_index; + u32 feature_index; + + ASSERT(ADJ_INDEX_INVALID != adj_index); + + adj = adj_get(adj_index); + + /* + * one time only update. since we don't support chainging the tunnel + * src,dst, this is all we need. + */ + ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_ARP); + /* + * tunnels can always provide a rewrite. + */ + ASSERT(NULL != rewrite); + + adj->sub_type.midchain.fixup_func = fixup; + + arc_index = adj_midchain_get_feature_arc_index_for_link_type (adj); + feature_index = (flags & ADJ_MIDCHAIN_FLAG_NO_COUNT) ? + adj_midchain_tx_no_count_feature_node[adj->ia_link] : + adj_midchain_tx_feature_node[adj->ia_link]; + + adj->sub_type.midchain.tx_function_node = (flags & ADJ_MIDCHAIN_FLAG_NO_COUNT) ? + adj_midchain_tx_no_count_node.index : + adj_midchain_tx_node.index; + + vnet_feature_enable_disable_with_index (arc_index, feature_index, + adj->rewrite_header.sw_if_index, + 1 /* enable */, 0, 0); + + /* + * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx. + * The graph arc used/created here is from the midchain-tx node to the + * child's registered node. This is because post adj processing the next + * node are any output features, then the midchain-tx. from there we + * need to get to the stacked child's node. + */ + dpo_stack_from_node(adj->sub_type.midchain.tx_function_node, + &adj->sub_type.midchain.next_dpo, + drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link))); + + /* + * update the rewirte with the workers paused. + */ + adj_nbr_update_rewrite_internal(adj, + IP_LOOKUP_NEXT_MIDCHAIN, + adj_get_midchain_node(adj->ia_link), + adj->sub_type.midchain.tx_function_node, + rewrite); +} + +/** + * adj_nbr_midchain_unstack + * + * Unstack the adj. stack it on drop + */ +void +adj_nbr_midchain_unstack (adj_index_t adj_index) +{ + ip_adjacency_t *adj; + + ASSERT(ADJ_INDEX_INVALID != adj_index); + + adj = adj_get(adj_index); + + /* + * stack on the drop + */ + dpo_stack(DPO_ADJACENCY_MIDCHAIN, + vnet_link_to_dpo_proto(adj->ia_link), + &adj->sub_type.midchain.next_dpo, + drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link))); + + CLIB_MEMORY_BARRIER(); +} + +/** + * adj_nbr_midchain_stack + */ +void +adj_nbr_midchain_stack (adj_index_t adj_index, + const dpo_id_t *next) +{ + ip_adjacency_t *adj; + + ASSERT(ADJ_INDEX_INVALID != adj_index); + + adj = adj_get(adj_index); + + ASSERT(IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index); + + dpo_stack_from_node(adj->sub_type.midchain.tx_function_node, + &adj->sub_type.midchain.next_dpo, + next); +} + +u8* +format_adj_midchain (u8* s, va_list *ap) +{ + index_t index = va_arg(*ap, index_t); + u32 indent = va_arg(*ap, u32); + vnet_main_t * vnm = vnet_get_main(); + ip_adjacency_t * adj = adj_get(index); + + s = format (s, "%U", format_vnet_link, adj->ia_link); + s = format (s, " via %U ", + format_ip46_address, &adj->sub_type.nbr.next_hop); + s = format (s, " %U", + format_vnet_rewrite, + vnm->vlib_main, &adj->rewrite_header, + sizeof (adj->rewrite_data), indent); + s = format (s, "\n%Ustacked-on:\n%U%U", + format_white_space, indent, + format_white_space, indent+2, + format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2); + + return (s); +} + +static void +adj_dpo_lock (dpo_id_t *dpo) +{ + adj_lock(dpo->dpoi_index); +} +static void +adj_dpo_unlock (dpo_id_t *dpo) +{ + adj_unlock(dpo->dpoi_index); +} + +const static dpo_vft_t adj_midchain_dpo_vft = { + .dv_lock = adj_dpo_lock, + .dv_unlock = adj_dpo_unlock, + .dv_format = format_adj_midchain, +}; + +/** + * @brief The per-protocol VLIB graph nodes that are assigned to a midchain + * object. + * + * this means that these graph nodes are ones from which a midchain is the + * parent object in the DPO-graph. + */ +const static char* const midchain_ip4_nodes[] = +{ + "ip4-midchain", + NULL, +}; +const static char* const midchain_ip6_nodes[] = +{ + "ip6-midchain", + NULL, +}; +const static char* const midchain_mpls_nodes[] = +{ + "mpls-midchain", + NULL, +}; +const static char* const midchain_ethernet_nodes[] = +{ + "adj-l2-midchain", + NULL, +}; + +const static char* const * const midchain_nodes[DPO_PROTO_NUM] = +{ + [DPO_PROTO_IP4] = midchain_ip4_nodes, + [DPO_PROTO_IP6] = midchain_ip6_nodes, + [DPO_PROTO_MPLS] = midchain_mpls_nodes, + [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes, +}; + +void +adj_midchain_module_init (void) +{ + dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes); +} diff --git a/src/vnet/adj/adj_midchain.h b/src/vnet/adj/adj_midchain.h new file mode 100644 index 00000000..ae414aea --- /dev/null +++ b/src/vnet/adj/adj_midchain.h @@ -0,0 +1,102 @@ +/* + * 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. + */ +/** + * Midchain Adjacency sub-type. These adjs represent an L3 peer on a + * tunnel interface. The tunnel's adjacency is thus not the end of the chain, + * and needs to stack on/link to another chain (or portion of the graph) to + * reach the tunnel's destination. + */ + +#ifndef __ADJ_MIDCHAIN_H__ +#define __ADJ_MIDCHAIN_H__ + +#include + +/** + * @brief Flags controlling the midchain adjacency + */ +typedef enum adj_midchain_flag_t_ +{ + /** + * No flags + */ + ADJ_MIDCHAIN_FLAG_NONE = 0, + + /** + * Packets TX through the midchain do not increment the interface + * counters. This should be used when the adj is associated with an L2 + * interface and that L2 interface is in a bridege domain. In that case + * the packet will have traversed the interface's TX node, and hence have + * been counted, before it traverses ths midchain + */ + ADJ_MIDCHAIN_FLAG_NO_COUNT = (1 << 0), +} adj_midchain_flag_t; + +/** + * @brief + * Convert an existing neighbour adjacency into a midchain + * + * @param adj_index + * The index of the neighbour adjacency. + * + * @param post_rewrite_node + * The VLIB graph node that provides the post-encap fixup. + * where 'fixup' is e.g., correcting chksum, length, etc. + * + * @param rewrite + * The rewrite. + */ +extern void adj_nbr_midchain_update_rewrite(adj_index_t adj_index, + adj_midchain_fixup_t fixup, + adj_midchain_flag_t flags, + u8 *rewrite); + +/** + * @brief + * [re]stack a midchain. 'Stacking' is the act of forming parent-child + * relationships in the data-plane graph. + * + * @param adj_index + * The index of the midchain to stack + * + * @param dpo + * The parent DPO to stack onto (i.e. become a child of). + */ +extern void adj_nbr_midchain_stack(adj_index_t adj_index, + const dpo_id_t *dpo); + +/** + * @brief + * unstack a midchain. This will break the chain between the midchain and + * the next graph section. This is a implemented as stack-on-drop + * + * @param adj_index + * The index of the midchain to stack + */ +extern void adj_nbr_midchain_unstack(adj_index_t adj_index); + +/** + * @brief + * Module initialisation + */ +extern void adj_midchain_module_init(void); + +/** + * @brief + * Format a midchain adjacency + */ +extern u8* format_adj_midchain(u8* s, va_list *ap); + +#endif diff --git a/src/vnet/adj/adj_nbr.c b/src/vnet/adj/adj_nbr.c new file mode 100644 index 00000000..1344bb67 --- /dev/null +++ b/src/vnet/adj/adj_nbr.c @@ -0,0 +1,1087 @@ +/* + * 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 + +/* + * Vector Hash tables of neighbour (traditional) adjacencies + * Key: interface(for the vector index), address (and its proto), + * link-type/ether-type. + */ +static BVT(clib_bihash) **adj_nbr_tables[FIB_PROTOCOL_MAX]; + +// FIXME SIZE APPROPRIATELY. ASK DAVEB. +#define ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (64 * 64) +#define ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (32<<20) + + +#define ADJ_NBR_SET_KEY(_key, _lt, _nh) \ +{ \ + _key.key[0] = (_nh)->as_u64[0]; \ + _key.key[1] = (_nh)->as_u64[1]; \ + _key.key[2] = (_lt); \ +} + +#define ADJ_NBR_ITF_OK(_proto, _itf) \ + (((_itf) < vec_len(adj_nbr_tables[_proto])) && \ + (NULL != adj_nbr_tables[_proto][sw_if_index])) + +static void +adj_nbr_insert (fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index, + adj_index_t adj_index) +{ + BVT(clib_bihash_kv) kv; + + if (sw_if_index >= vec_len(adj_nbr_tables[nh_proto])) + { + vec_validate(adj_nbr_tables[nh_proto], sw_if_index); + } + if (NULL == adj_nbr_tables[nh_proto][sw_if_index]) + { + adj_nbr_tables[nh_proto][sw_if_index] = + clib_mem_alloc_aligned(sizeof(BVT(clib_bihash)), + CLIB_CACHE_LINE_BYTES); + memset(adj_nbr_tables[nh_proto][sw_if_index], + 0, + sizeof(BVT(clib_bihash))); + + BV(clib_bihash_init) (adj_nbr_tables[nh_proto][sw_if_index], + "Adjacency Neighbour table", + ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS, + ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE); + } + + ADJ_NBR_SET_KEY(kv, link_type, nh_addr); + kv.value = adj_index; + + BV(clib_bihash_add_del) (adj_nbr_tables[nh_proto][sw_if_index], &kv, 1); +} + +void +adj_nbr_remove (adj_index_t ai, + fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index) +{ + BVT(clib_bihash_kv) kv; + + if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index)) + return; + + ADJ_NBR_SET_KEY(kv, link_type, nh_addr); + kv.value = ai; + + BV(clib_bihash_add_del) (adj_nbr_tables[nh_proto][sw_if_index], &kv, 0); +} + +static adj_index_t +adj_nbr_find (fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index) +{ + BVT(clib_bihash_kv) kv; + + ADJ_NBR_SET_KEY(kv, link_type, nh_addr); + + if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index)) + return (ADJ_INDEX_INVALID); + + if (BV(clib_bihash_search)(adj_nbr_tables[nh_proto][sw_if_index], + &kv, &kv) < 0) + { + return (ADJ_INDEX_INVALID); + } + else + { + return (kv.value); + } +} + +static inline u32 +adj_get_nd_node (fib_protocol_t proto) +{ + switch (proto) { + case FIB_PROTOCOL_IP4: + return (ip4_arp_node.index); + case FIB_PROTOCOL_IP6: + return (ip6_discover_neighbor_node.index); + case FIB_PROTOCOL_MPLS: + break; + } + ASSERT(0); + return (ip4_arp_node.index); +} + +static ip_adjacency_t* +adj_nbr_alloc (fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index) +{ + ip_adjacency_t *adj; + + adj = adj_alloc(nh_proto); + + adj_nbr_insert(nh_proto, link_type, nh_addr, + sw_if_index, + adj_get_index(adj)); + + /* + * since we just added the ADJ we have no rewrite string for it, + * so its for ARP + */ + adj->lookup_next_index = IP_LOOKUP_NEXT_ARP; + adj->sub_type.nbr.next_hop = *nh_addr; + adj->ia_link = link_type; + adj->ia_nh_proto = nh_proto; + adj->rewrite_header.sw_if_index = sw_if_index; + memset(&adj->sub_type.midchain.next_dpo, 0, + sizeof(adj->sub_type.midchain.next_dpo)); + + return (adj); +} + +/* + * adj_add_for_nbr + * + * Add an adjacency for the neighbour requested. + * + * The key for an adj is: + * - the Next-hops protocol (i.e. v4 or v6) + * - the address of the next-hop + * - the interface the next-hop is reachable through + */ +adj_index_t +adj_nbr_add_or_lock (fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index) +{ + adj_index_t adj_index; + ip_adjacency_t *adj; + + adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index); + + if (ADJ_INDEX_INVALID == adj_index) + { + vnet_main_t *vnm; + + vnm = vnet_get_main(); + adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index); + adj_index = adj_get_index(adj); + adj_lock(adj_index); + + vnet_rewrite_init(vnm, sw_if_index, + adj_get_nd_node(nh_proto), + vnet_tx_node_index_for_sw_interface(vnm, sw_if_index), + &adj->rewrite_header); + + /* + * we need a rewrite where the destination IP address is converted + * to the appropriate link-layer address. This is interface specific. + * So ask the interface to do it. + */ + vnet_update_adjacency_for_sw_interface(vnm, sw_if_index, adj_index); + } + else + { + adj_lock(adj_index); + } + + return (adj_index); +} + +adj_index_t +adj_nbr_add_or_lock_w_rewrite (fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index, + u8 *rewrite) +{ + adj_index_t adj_index; + ip_adjacency_t *adj; + + adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index); + + if (ADJ_INDEX_INVALID == adj_index) + { + adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index); + adj->rewrite_header.sw_if_index = sw_if_index; + } + else + { + adj = adj_get(adj_index); + } + + adj_lock(adj_get_index(adj)); + adj_nbr_update_rewrite(adj_get_index(adj), + ADJ_NBR_REWRITE_FLAG_COMPLETE, + rewrite); + + return (adj_get_index(adj)); +} + +/** + * adj_nbr_update_rewrite + * + * Update the adjacency's rewrite string. A NULL string implies the + * rewirte is reset (i.e. when ARP/ND etnry is gone). + * NB: the adj being updated may be handling traffic in the DP. + */ +void +adj_nbr_update_rewrite (adj_index_t adj_index, + adj_nbr_rewrite_flag_t flags, + u8 *rewrite) +{ + ip_adjacency_t *adj; + + ASSERT(ADJ_INDEX_INVALID != adj_index); + + adj = adj_get(adj_index); + + if (flags & ADJ_NBR_REWRITE_FLAG_COMPLETE) + { + /* + * update the adj's rewrite string and build the arc + * from the rewrite node to the interface's TX node + */ + adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_REWRITE, + adj_get_rewrite_node(adj->ia_link), + vnet_tx_node_index_for_sw_interface( + vnet_get_main(), + adj->rewrite_header.sw_if_index), + rewrite); + } + else + { + adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_ARP, + adj_get_nd_node(adj->ia_nh_proto), + vnet_tx_node_index_for_sw_interface( + vnet_get_main(), + adj->rewrite_header.sw_if_index), + rewrite); + } +} + +/** + * adj_nbr_update_rewrite_internal + * + * Update the adjacency's rewrite string. A NULL string implies the + * rewirte is reset (i.e. when ARP/ND etnry is gone). + * NB: the adj being updated may be handling traffic in the DP. + */ +void +adj_nbr_update_rewrite_internal (ip_adjacency_t *adj, + u32 adj_next_index, + u32 this_node, + u32 next_node, + u8 *rewrite) +{ + ip_adjacency_t *walk_adj; + adj_index_t walk_ai; + vlib_main_t * vm; + u32 old_next; + int do_walk; + + vm = vlib_get_main(); + old_next = adj->lookup_next_index; + + walk_ai = adj_get_index(adj); + if (VNET_LINK_MPLS == adj->ia_link) + { + /* + * The link type MPLS has no children in the control plane graph, it only + * has children in the data-palne graph. The backwalk is up the former. + * So we need to walk from its IP cousin. + */ + walk_ai = adj_nbr_find(adj->ia_nh_proto, + fib_proto_to_link(adj->ia_nh_proto), + &adj->sub_type.nbr.next_hop, + adj->rewrite_header.sw_if_index); + } + + /* + * Don't call the walk re-entrantly + */ + if (ADJ_INDEX_INVALID != walk_ai) + { + walk_adj = adj_get(walk_ai); + if (IP_ADJ_SYNC_WALK_ACTIVE & walk_adj->ia_flags) + { + do_walk = 0; + } + else + { + /* + * Prevent re-entrant walk of the same adj + */ + walk_adj->ia_flags |= IP_ADJ_SYNC_WALK_ACTIVE; + do_walk = 1; + } + } + else + { + do_walk = 0; + } + + /* + * lock the adjacencies that are affected by updates this walk will provoke. + * Since the aim of the walk is to update children to link to a different + * DPO, this adj will no longer be in use and its lock count will drop to 0. + * We don't want it to be deleted as part of this endevour. + */ + adj_lock(adj_get_index(adj)); + adj_lock(walk_ai); + + /* + * Updating a rewrite string is not atomic; + * - the rewrite string is too long to write in one instruction + * - when swapping from incomplete to complete, we also need to update + * the VLIB graph next-index of the adj. + * ideally we would only want to suspend forwarding via this adj whilst we + * do this, but we do not have that level of granularity - it's suspend all + * worker threads or nothing. + * The other chioces are: + * - to mark the adj down and back walk so child load-balances drop this adj + * from the set. + * - update the next_node index of this adj to point to error-drop + * both of which will mean for MAC change we will drop for this adj + * which is not acceptable. However, when the adj changes type (from + * complete to incomplete and vice-versa) the child DPOs, which have the + * VLIB graph next node index, will be sending packets to the wrong graph + * node. So from the options above, updating the next_node of the adj to + * be drop will work, but it relies on each graph node v4/v6/mpls, rewrite/ + * arp/midchain always be valid w.r.t. a mis-match of adj type and node type + * (i.e. a rewrite adj in the arp node). This is not enforcable. Getting it + * wrong will lead to hard to find bugs since its a race condition. So we + * choose the more reliable method of updating the children to use the drop, + * then switching adj's type, then updating the children again. Did I mention + * that this doesn't happen often... + * So we need to distinguish between the two cases: + * 1 - mac change + * 2 - adj type change + */ + if (do_walk && + old_next != adj_next_index && + ADJ_INDEX_INVALID != walk_ai) + { + /* + * the adj is changing type. we need to fix all children so that they + * stack momentarily on a drop, while the adj changes. If we don't do + * this the children will send packets to a VLIB graph node that does + * not correspond to the adj's type - and it goes downhill from there. + */ + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_DOWN, + /* + * force this walk to be synchrous. if we don't and a node in the graph + * (a heavily shared path-list) chooses to back-ground the walk (make it + * async) then it will pause and we will do the adj update below, before + * all the children are updated. not good. + */ + .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC, + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx); + } + + /* + * If we are just updating the MAC string of the adj (which we also can't + * do atomically), then we need to stop packets switching through the adj. + * We can't do that on a per-adj basis, so it's all the packets. + * If we are updating the type, and we walked back to the children above, + * then this barrier serves to flush the queues/frames. + */ + vlib_worker_thread_barrier_sync(vm); + + adj->lookup_next_index = adj_next_index; + + if (NULL != rewrite) + { + /* + * new rewrite provided. + * fill in the adj's rewrite string, and build the VLIB graph arc. + */ + vnet_rewrite_set_data_internal(&adj->rewrite_header, + sizeof(adj->rewrite_data), + rewrite, + vec_len(rewrite)); + vec_free(rewrite); + } + else + { + vnet_rewrite_clear_data_internal(&adj->rewrite_header, + sizeof(adj->rewrite_data)); + } + adj->rewrite_header.node_index = this_node; + adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(), + this_node, + next_node); + + /* + * done with the rewirte update - let the workers loose. + */ + vlib_worker_thread_barrier_release(vm); + + if (do_walk && + (old_next != adj->lookup_next_index) && + (ADJ_INDEX_INVALID != walk_ai)) + { + /* + * backwalk to the children so they can stack on the now updated + * adjacency + */ + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE, + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx); + } + /* + * Prevent re-entrant walk of the same adj + */ + if (do_walk) + { + walk_adj->ia_flags &= ~IP_ADJ_SYNC_WALK_ACTIVE; + } + + adj_unlock(adj_get_index(adj)); + adj_unlock(walk_ai); +} + +typedef struct adj_db_count_ctx_t_ { + u64 count; +} adj_db_count_ctx_t; + +static void +adj_db_count (BVT(clib_bihash_kv) * kvp, + void *arg) +{ + adj_db_count_ctx_t * ctx = arg; + ctx->count++; +} + +u32 +adj_nbr_db_size (void) +{ + adj_db_count_ctx_t ctx = { + .count = 0, + }; + fib_protocol_t proto; + u32 sw_if_index = 0; + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + vec_foreach_index(sw_if_index, adj_nbr_tables[proto]) + { + if (NULL != adj_nbr_tables[proto][sw_if_index]) + { + BV(clib_bihash_foreach_key_value_pair) ( + adj_nbr_tables[proto][sw_if_index], + adj_db_count, + &ctx); + } + } + } + return (ctx.count); +} + +/** + * @brief Context for a walk of the adjacency neighbour DB + */ +typedef struct adj_walk_ctx_t_ +{ + adj_walk_cb_t awc_cb; + void *awc_ctx; +} adj_walk_ctx_t; + +static void +adj_nbr_walk_cb (BVT(clib_bihash_kv) * kvp, + void *arg) +{ + adj_walk_ctx_t *ctx = arg; + + // FIXME: can't stop early... + ctx->awc_cb(kvp->value, ctx->awc_ctx); +} + +void +adj_nbr_walk (u32 sw_if_index, + fib_protocol_t adj_nh_proto, + adj_walk_cb_t cb, + void *ctx) +{ + if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index)) + return; + + adj_walk_ctx_t awc = { + .awc_ctx = ctx, + .awc_cb = cb, + }; + + BV(clib_bihash_foreach_key_value_pair) ( + adj_nbr_tables[adj_nh_proto][sw_if_index], + adj_nbr_walk_cb, + &awc); +} + +/** + * @brief Context for a walk of the adjacency neighbour DB + */ +typedef struct adj_walk_nh_ctx_t_ +{ + adj_walk_cb_t awc_cb; + void *awc_ctx; + const ip46_address_t *awc_nh; +} adj_walk_nh_ctx_t; + +static void +adj_nbr_walk_nh_cb (BVT(clib_bihash_kv) * kvp, + void *arg) +{ + ip_adjacency_t *adj; + adj_walk_nh_ctx_t *ctx = arg; + + adj = adj_get(kvp->value); + + if (!ip46_address_cmp(&adj->sub_type.nbr.next_hop, ctx->awc_nh)) + ctx->awc_cb(kvp->value, ctx->awc_ctx); +} + +/** + * @brief Walk adjacencies on a link with a given v4 next-hop. + * that is visit the adjacencies with different link types. + */ +void +adj_nbr_walk_nh4 (u32 sw_if_index, + const ip4_address_t *addr, + adj_walk_cb_t cb, + void *ctx) +{ + if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP4, sw_if_index)) + return; + + ip46_address_t nh = { + .ip4 = *addr, + }; + + adj_walk_nh_ctx_t awc = { + .awc_ctx = ctx, + .awc_cb = cb, + .awc_nh = &nh, + }; + + BV(clib_bihash_foreach_key_value_pair) ( + adj_nbr_tables[FIB_PROTOCOL_IP4][sw_if_index], + adj_nbr_walk_nh_cb, + &awc); +} + +/** + * @brief Walk adjacencies on a link with a given v6 next-hop. + * that is visit the adjacencies with different link types. + */ +void +adj_nbr_walk_nh6 (u32 sw_if_index, + const ip6_address_t *addr, + adj_walk_cb_t cb, + void *ctx) +{ + if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP6, sw_if_index)) + return; + + ip46_address_t nh = { + .ip6 = *addr, + }; + + adj_walk_nh_ctx_t awc = { + .awc_ctx = ctx, + .awc_cb = cb, + .awc_nh = &nh, + }; + + BV(clib_bihash_foreach_key_value_pair) ( + adj_nbr_tables[FIB_PROTOCOL_IP6][sw_if_index], + adj_nbr_walk_nh_cb, + &awc); +} + +/** + * @brief Walk adjacencies on a link with a given next-hop. + * that is visit the adjacencies with different link types. + */ +void +adj_nbr_walk_nh (u32 sw_if_index, + fib_protocol_t adj_nh_proto, + const ip46_address_t *nh, + adj_walk_cb_t cb, + void *ctx) +{ + if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index)) + return; + + adj_walk_nh_ctx_t awc = { + .awc_ctx = ctx, + .awc_cb = cb, + .awc_nh = nh, + }; + + BV(clib_bihash_foreach_key_value_pair) ( + adj_nbr_tables[adj_nh_proto][sw_if_index], + adj_nbr_walk_nh_cb, + &awc); +} + +/** + * Flags associated with the interface state walks + */ +typedef enum adj_nbr_interface_flags_t_ +{ + ADJ_NBR_INTERFACE_UP = (1 << 0), +} adj_nbr_interface_flags_t; + +/** + * Context for the state change walk of the DB + */ +typedef struct adj_nbr_interface_state_change_ctx_t_ +{ + /** + * Flags on the interface + */ + adj_nbr_interface_flags_t flags; +} adj_nbr_interface_state_change_ctx_t; + +static adj_walk_rc_t +adj_nbr_interface_state_change_one (adj_index_t ai, + void *arg) +{ + /* + * Back walk the graph to inform the forwarding entries + * that this interface state has changed. Do this synchronously + * since this is the walk that provides convergence + */ + adj_nbr_interface_state_change_ctx_t *ctx = arg; + + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = ((ctx->flags & ADJ_NBR_INTERFACE_UP) ? + FIB_NODE_BW_REASON_FLAG_INTERFACE_UP : + FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN), + /* + * the force sync applies only as far as the first fib_entry. + * And it's the fib_entry's we need to converge away from + * the adjacencies on the now down link + */ + .fnbw_flags = (!(ctx->flags & ADJ_NBR_INTERFACE_UP) ? + FIB_NODE_BW_FLAG_FORCE_SYNC : + 0), + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx); + + return (ADJ_WALK_RC_CONTINUE); +} + +/** + * @brief Registered function for SW interface state changes + */ +static clib_error_t * +adj_nbr_sw_interface_state_change (vnet_main_t * vnm, + u32 sw_if_index, + u32 flags) +{ + fib_protocol_t proto; + + /* + * walk each adj on the interface and trigger a walk from that adj + */ + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + adj_nbr_interface_state_change_ctx_t ctx = { + .flags = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? + ADJ_NBR_INTERFACE_UP : + 0), + }; + + adj_nbr_walk(sw_if_index, proto, + adj_nbr_interface_state_change_one, + &ctx); + } + + return (NULL); +} + +VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION_PRIO( + adj_nbr_sw_interface_state_change, + VNET_ITF_FUNC_PRIORITY_HIGH); + +/** + * @brief Invoked on each SW interface of a HW interface when the + * HW interface state changes + */ +static void +adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm, + u32 sw_if_index, + void *arg) +{ + adj_nbr_interface_state_change_ctx_t *ctx = arg; + fib_protocol_t proto; + + /* + * walk each adj on the interface and trigger a walk from that adj + */ + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + adj_nbr_walk(sw_if_index, proto, + adj_nbr_interface_state_change_one, + ctx); + } +} + +/** + * @brief Registered callback for HW interface state changes + */ +static clib_error_t * +adj_nbr_hw_interface_state_change (vnet_main_t * vnm, + u32 hw_if_index, + u32 flags) +{ + /* + * walk SW interface on the HW + */ + adj_nbr_interface_state_change_ctx_t ctx = { + .flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? + ADJ_NBR_INTERFACE_UP : + 0), + }; + + vnet_hw_interface_walk_sw(vnm, hw_if_index, + adj_nbr_hw_sw_interface_state_change, + &ctx); + + return (NULL); +} + +VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION_PRIO( + adj_nbr_hw_interface_state_change, + VNET_ITF_FUNC_PRIORITY_HIGH); + +static adj_walk_rc_t +adj_nbr_interface_delete_one (adj_index_t ai, + void *arg) +{ + /* + * Back walk the graph to inform the forwarding entries + * that this interface has been deleted. + */ + fib_node_back_walk_ctx_t bw_ctx = { + .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE, + }; + + fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx); + + return (ADJ_WALK_RC_CONTINUE); +} + +/** + * adj_nbr_interface_add_del + * + * Registered to receive interface Add and delete notifications + */ +static clib_error_t * +adj_nbr_interface_add_del (vnet_main_t * vnm, + u32 sw_if_index, + u32 is_add) +{ + fib_protocol_t proto; + + if (is_add) + { + /* + * not interested in interface additions. we will not back walk + * to resolve paths through newly added interfaces. Why? The control + * plane should have the brains to add interfaces first, then routes. + * So the case where there are paths with a interface that matches + * one just created is the case where the path resolved through an + * interface that was deleted, and still has not been removed. The + * new interface added, is NO GUARANTEE that the interface being + * added now, even though it may have the same sw_if_index, is the + * same interface that the path needs. So tough! + * If the control plane wants these routes to resolve it needs to + * remove and add them again. + */ + return (NULL); + } + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + adj_nbr_walk(sw_if_index, proto, + adj_nbr_interface_delete_one, + NULL); + } + + return (NULL); + +} + +VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_nbr_interface_add_del); + + +static adj_walk_rc_t +adj_nbr_show_one (adj_index_t ai, + void *arg) +{ + vlib_cli_output (arg, "[@%d] %U", + ai, + format_ip_adjacency, ai, + FORMAT_IP_ADJACENCY_NONE); + + return (ADJ_WALK_RC_CONTINUE); +} + +static clib_error_t * +adj_nbr_show (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + adj_index_t ai = ADJ_INDEX_INVALID; + u32 sw_if_index = ~0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "%d", &ai)) + ; + else if (unformat (input, "%U", + unformat_vnet_sw_interface, vnet_get_main(), + &sw_if_index)) + ; + else + break; + } + + if (ADJ_INDEX_INVALID != ai) + { + vlib_cli_output (vm, "[@%d] %U", + ai, + format_ip_adjacency, ai, + FORMAT_IP_ADJACENCY_DETAIL); + } + else if (~0 != sw_if_index) + { + fib_protocol_t proto; + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + adj_nbr_walk(sw_if_index, proto, + adj_nbr_show_one, + vm); + } + } + else + { + fib_protocol_t proto; + + for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++) + { + vec_foreach_index(sw_if_index, adj_nbr_tables[proto]) + { + adj_nbr_walk(sw_if_index, proto, + adj_nbr_show_one, + vm); + } + } + } + + return 0; +} + +/*? + * Show all neighbour adjacencies. + * @cliexpar + * @cliexstart{sh adj nbr} + * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc + * @cliexend + ?*/ +VLIB_CLI_COMMAND (ip4_show_fib_command, static) = { + .path = "show adj nbr", + .short_help = "show adj nbr [] [interface]", + .function = adj_nbr_show, +}; + +static ip46_type_t +adj_proto_to_46 (fib_protocol_t proto) +{ + switch (proto) + { + case FIB_PROTOCOL_IP4: + return (IP46_TYPE_IP4); + case FIB_PROTOCOL_IP6: + return (IP46_TYPE_IP6); + default: + return (IP46_TYPE_IP4); + } + return (IP46_TYPE_IP4); +} + +u8* +format_adj_nbr_incomplete (u8* s, va_list *ap) +{ + index_t index = va_arg(*ap, index_t); + CLIB_UNUSED(u32 indent) = va_arg(*ap, u32); + vnet_main_t * vnm = vnet_get_main(); + ip_adjacency_t * adj = adj_get(index); + + s = format (s, "arp-%U", format_vnet_link, adj->ia_link); + s = format (s, ": via %U", + format_ip46_address, &adj->sub_type.nbr.next_hop, + adj_proto_to_46(adj->ia_nh_proto)); + s = format (s, " %U", + format_vnet_sw_interface_name, + vnm, + vnet_get_sw_interface(vnm, + adj->rewrite_header.sw_if_index)); + + return (s); +} + +u8* +format_adj_nbr (u8* s, va_list *ap) +{ + index_t index = va_arg(*ap, index_t); + CLIB_UNUSED(u32 indent) = va_arg(*ap, u32); + vnet_main_t * vnm = vnet_get_main(); + ip_adjacency_t * adj = adj_get(index); + + s = format (s, "%U", format_vnet_link, adj->ia_link); + s = format (s, " via %U ", + format_ip46_address, &adj->sub_type.nbr.next_hop, + adj_proto_to_46(adj->ia_nh_proto)); + s = format (s, "%U", + format_vnet_rewrite, + vnm->vlib_main, &adj->rewrite_header, sizeof (adj->rewrite_data), 0); + + return (s); +} + +static void +adj_dpo_lock (dpo_id_t *dpo) +{ + adj_lock(dpo->dpoi_index); +} +static void +adj_dpo_unlock (dpo_id_t *dpo) +{ + adj_unlock(dpo->dpoi_index); +} + +static void +adj_mem_show (void) +{ + fib_show_memory_usage("Adjacency", + pool_elts(adj_pool), + pool_len(adj_pool), + sizeof(ip_adjacency_t)); +} + +const static dpo_vft_t adj_nbr_dpo_vft = { + .dv_lock = adj_dpo_lock, + .dv_unlock = adj_dpo_unlock, + .dv_format = format_adj_nbr, + .dv_mem_show = adj_mem_show, +}; +const static dpo_vft_t adj_nbr_incompl_dpo_vft = { + .dv_lock = adj_dpo_lock, + .dv_unlock = adj_dpo_unlock, + .dv_format = format_adj_nbr_incomplete, +}; + +/** + * @brief The per-protocol VLIB graph nodes that are assigned to an adjacency + * object. + * + * this means that these graph nodes are ones from which a nbr is the + * parent object in the DPO-graph. + */ +const static char* const nbr_ip4_nodes[] = +{ + "ip4-rewrite", + NULL, +}; +const static char* const nbr_ip6_nodes[] = +{ + "ip6-rewrite", + NULL, +}; +const static char* const nbr_mpls_nodes[] = +{ + "mpls-output", + NULL, +}; +const static char* const nbr_ethernet_nodes[] = +{ + "adj-l2-rewrite", + NULL, +}; +const static char* const * const nbr_nodes[DPO_PROTO_NUM] = +{ + [DPO_PROTO_IP4] = nbr_ip4_nodes, + [DPO_PROTO_IP6] = nbr_ip6_nodes, + [DPO_PROTO_MPLS] = nbr_mpls_nodes, + [DPO_PROTO_ETHERNET] = nbr_ethernet_nodes, +}; + +const static char* const nbr_incomplete_ip4_nodes[] = +{ + "ip4-arp", + NULL, +}; +const static char* const nbr_incomplete_ip6_nodes[] = +{ + "ip6-discover-neighbor", + NULL, +}; +const static char* const nbr_incomplete_mpls_nodes[] = +{ + "mpls-adj-incomplete", + NULL, +}; + +const static char* const * const nbr_incomplete_nodes[DPO_PROTO_NUM] = +{ + [DPO_PROTO_IP4] = nbr_incomplete_ip4_nodes, + [DPO_PROTO_IP6] = nbr_incomplete_ip6_nodes, + [DPO_PROTO_MPLS] = nbr_incomplete_mpls_nodes, +}; + +void +adj_nbr_module_init (void) +{ + dpo_register(DPO_ADJACENCY, + &adj_nbr_dpo_vft, + nbr_nodes); + dpo_register(DPO_ADJACENCY_INCOMPLETE, + &adj_nbr_incompl_dpo_vft, + nbr_incomplete_nodes); +} diff --git a/src/vnet/adj/adj_nbr.h b/src/vnet/adj/adj_nbr.h new file mode 100644 index 00000000..293766b8 --- /dev/null +++ b/src/vnet/adj/adj_nbr.h @@ -0,0 +1,176 @@ +/* + * 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. + */ +/** + * @brief + * Neighbour Adjacency sub-type. These adjs represent an L3 peer on a + * connected link. + */ + +#ifndef __ADJ_NBR_H__ +#define __ADJ_NBR_H__ + +#include +#include +#include +#include + +/** + * @brief + * Add (and lock) a new or lock an existing neighbour adjacency + * + * @param nh_proto + * The protocol for the next-hop address (v4 or v6) + * + * @param link_type + * A description of the protocol of the packets that will forward + * through this adj. On an ethernet interface this is the MAC header's + * ether-type + * + * @param nh_addr + * The address of the next-hop/peer to send the packet to + * + * @param sw_if_index + * The interface on which the peer resides + */ +extern adj_index_t adj_nbr_add_or_lock(fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index); + +/** + * @brief + * Add (and lock) a new or lock an existing neighbour adjacency + * + * @param nh_proto + * The protocol for the next-hop address (v4 or v6) + * + * @param link_type + * A description of the protocol of the packets that will forward + * through this adj. On an ethernet interface this is the MAC header's + * ether-type + * + * @param nh_addr + * The address of the next-hop/peer to send the packet to + * + * @param sw_if_index + * The interface on which the peer resides + * + * @param rewrite + * The rewrite to prepend to packets + */ +extern adj_index_t adj_nbr_add_or_lock_w_rewrite(fib_protocol_t nh_proto, + vnet_link_t link_type, + const ip46_address_t *nh_addr, + u32 sw_if_index, + u8 *rewrite); +/** + * @brief When adding a rewrite to an adjacency these are flags that + * apply to that rewrite + */ +typedef enum adj_nbr_rewrite_flag_t_ +{ + ADJ_NBR_REWRITE_FLAG_NONE, + + /** + * An indication that the rewrite is incomplete, i.e. that it describes the + * ARP/ND rewrite when probing. + */ + ADJ_NBR_REWRITE_FLAG_INCOMPLETE = ADJ_NBR_REWRITE_FLAG_NONE, + + /** + * An indication that the rewrite is complete, i.e. that it fully describes + * the link-layer addressing for the desintation. + * The opposite of this is an incomplete rewrite that describes the ARP/ND + * rewrite when probing. + */ + ADJ_NBR_REWRITE_FLAG_COMPLETE = (1 << 0), +} adj_nbr_rewrite_flag_t; + +/** + * @brief + * Update the rewrite string for an existing adjacecny. + * + * @param + * The index of the adj to update + * + * @param + * The new rewrite + */ +extern void adj_nbr_update_rewrite(adj_index_t adj_index, + adj_nbr_rewrite_flag_t flags, + u8 *rewrite); + +/** + * @brief + * Format aa incomplete neigbour (ARP) adjacency + */ +extern u8* format_adj_nbr_incomplete(u8* s, va_list *ap); + +/** + * @brief + * Format a neigbour (REWRITE) adjacency + */ +extern u8* format_adj_nbr(u8* s, va_list *ap); + +/** + * @brief Walk the neighbour Adjacencies on a given interface + */ +extern void adj_nbr_walk (u32 sw_if_index, + fib_protocol_t adj_nh_proto, + adj_walk_cb_t cb, + void *ctx); +/** + * @brief Walk the neighbour Adjacencies on a given interface with a given next-hop + */ +void +adj_nbr_walk_nh (u32 sw_if_index, + fib_protocol_t adj_nh_proto, + const ip46_address_t *nh, + adj_walk_cb_t cb, + void *ctx); + +/** + * @brief Walk adjacencies on a link with a given v4 next-hop. + * that is visit the adjacencies with different link types. + */ +void +adj_nbr_walk_nh4 (u32 sw_if_index, + const ip4_address_t *addr, + adj_walk_cb_t cb, + void *ctx); + +/** + * @brief Walk adjacencies on a link with a given v6 next-hop. + * that is visit the adjacencies with different link types. + */ +void +adj_nbr_walk_nh6 (u32 sw_if_index, + const ip6_address_t *addr, + adj_walk_cb_t cb, + void *ctx); + +/** + * @brief + * Module initialisation + */ +extern void adj_nbr_module_init(void); + +/** + * @brief + * Return the size of the adjacency database. for testing purposes + */ +extern u32 adj_nbr_db_size(void); + +#endif diff --git a/src/vnet/adj/adj_rewrite.c b/src/vnet/adj/adj_rewrite.c new file mode 100644 index 00000000..7d792557 --- /dev/null +++ b/src/vnet/adj/adj_rewrite.c @@ -0,0 +1,53 @@ +/* + * 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 + +/** + * adj_rewrite_add_and_lock + * + * A rewrite sub-type has the rewrite string provided, but no key + */ +adj_index_t +adj_rewrite_add_and_lock (fib_protocol_t nh_proto, + vnet_link_t link_type, + u32 sw_if_index, + u8 *rewrite) +{ + ip_adjacency_t *adj; + + adj = adj_alloc(nh_proto); + + adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE; + memset(&adj->sub_type.nbr.next_hop, 0, sizeof(adj->sub_type.nbr.next_hop)); + adj->ia_link = link_type; + adj->ia_nh_proto = nh_proto; + adj->rewrite_header.sw_if_index = sw_if_index; + + ASSERT(NULL != rewrite); + + vnet_rewrite_for_sw_interface(vnet_get_main(), + link_type, + adj->rewrite_header.sw_if_index, + adj_get_rewrite_node(link_type), + rewrite, + &adj->rewrite_header, + sizeof (adj->rewrite_data)); + + adj_lock(adj_get_index(adj)); + + return (adj_get_index(adj)); +} diff --git a/src/vnet/adj/adj_rewrite.h b/src/vnet/adj/adj_rewrite.h new file mode 100644 index 00000000..25e6bba8 --- /dev/null +++ b/src/vnet/adj/adj_rewrite.h @@ -0,0 +1,49 @@ +/* + * 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. + */ +/** + * @brief + * A rewrite adjacency has no key, and thus cannot be 'found' from the + * FIB resolution code. the client therefore needs to maange these adjacencies + */ + +#ifndef __ADJ_REWRITE_H__ +#define __ADJ_REWRITE_H__ + +#include + +/** + * @brief + * Add (and lock) a new or lock an existing neighbour adjacency + * + * @param nh_proto + * The protocol for the next-hop address (v4 or v6) + * + * @param link_type + * A description of the protocol of the packets that will forward + * through this adj. On an ethernet interface this is the MAC header's + * ether-type + * + * @param sw_if_index + * The interface on which the peer resides + * + * @param rewrite + * The rewrite to prepend to packets + */ +extern adj_index_t adj_rewrite_add_and_lock(fib_protocol_t nh_proto, + vnet_link_t link_type, + u32 sw_if_index, + u8 *rewrite); + +#endif diff --git a/src/vnet/adj/adj_types.h b/src/vnet/adj/adj_types.h new file mode 100644 index 00000000..cf90c084 --- /dev/null +++ b/src/vnet/adj/adj_types.h @@ -0,0 +1,53 @@ +/* + * 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. + */ + +#ifndef __ADJ_TYPES_H__ +#define __ADJ_TYPES_H__ + +#include + +/** + * @brief An index for adjacencies. + * Alas 'C' is not typesafe enough to b0rk when a u32 is used instead of + * an adi_index_t. However, for us humans, we can glean much more intent + * from the declaration + * foo bar(adj_index_t t); + * than we can from + * foo bar(u32 t); + */ +typedef u32 adj_index_t; + +/** + * @brief Invalid ADJ index - used when no adj is known + * likewise blazoned capitals INVALID speak volumes where ~0 does not. + */ +#define ADJ_INDEX_INVALID ((u32)~0) + +/** + * @brief return codes from a adjacency walker callback function + */ +typedef enum adj_walk_rc_t_ +{ + ADJ_WALK_RC_STOP, + ADJ_WALK_RC_CONTINUE, +} adj_walk_rc_t; + +/** + * @brief Call back function when walking adjacencies + */ +typedef adj_walk_rc_t (*adj_walk_cb_t)(adj_index_t ai, + void *ctx); + +#endif -- cgit 1.2.3-korg From 044183faacc5eb8a055658f9deebefb56b254adc Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Tue, 24 Jan 2017 01:34:25 -0800 Subject: [re]Enable per-Adjacency/neighbour counters Change-Id: I953b3888bbc6d8a5f53f684a5edc8742b382f323 Signed-off-by: Neale Ranns --- src/scripts/vnet/ip6 | 18 +- src/vat/api_format.c | 124 ++++++++++++++ src/vat/vat.h | 18 ++ src/vlib/counter.h | 14 ++ src/vnet/adj/adj.c | 4 + src/vnet/adj/adj_l2.c | 10 +- src/vnet/ip/ip4_forward.c | 42 +++-- src/vnet/ip/ip6_forward.c | 24 +-- src/vnet/mpls/mpls_output.c | 37 ++-- src/vpp/api/vpe.api | 41 +++++ src/vpp/stats/stats.c | 405 +++++++++++++++++++++++++++++++++++++++++++- 11 files changed, 675 insertions(+), 62 deletions(-) (limited to 'src/vnet/adj') diff --git a/src/scripts/vnet/ip6 b/src/scripts/vnet/ip6 index 4f9f3ee5..adb27225 100644 --- a/src/scripts/vnet/ip6 +++ b/src/scripts/vnet/ip6 @@ -6,10 +6,24 @@ packet-generator new { no-recycle data { IP6: 1.2.3 -> 4.5.6 - ICMP: ::1 -> ::2 + ICMP: 3002::2 -> 3001::2 ICMP echo_request incrementing 100 } } -tr add pg-input 100 + +loop create +loop create +set int state loop0 up +set int state loop1 up + +set int ip address loop0 2001:1::1/64 +set int ip address loop1 2001:2::1/64 + +set ip6 neighbor loop0 2001:1::2 00:00:DD:EE:AA:DD +set ip6 neighbor loop1 2001:2::2 00:00:DD:EE:AA:EE + +ip route add 3001::/64 via 2001:2::2 loop1 + +trace add pg-input 100 diff --git a/src/vat/api_format.c b/src/vat/api_format.c index 653cf79f..839bcdaa 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -2042,6 +2042,42 @@ static void vl_api_vnet_ip4_fib_counters_t_handler_json } } +static void vl_api_vnet_ip4_nbr_counters_t_handler + (vl_api_vnet_ip4_nbr_counters_t * mp) +{ + /* not supported */ +} + +static void vl_api_vnet_ip4_nbr_counters_t_handler_json + (vl_api_vnet_ip4_nbr_counters_t * mp) +{ + vat_main_t *vam = &vat_main; + vl_api_ip4_nbr_counter_t *v; + ip4_nbr_counter_t *counter; + u32 sw_if_index; + u32 count; + int i; + + sw_if_index = ntohl (mp->sw_if_index); + count = ntohl (mp->count); + vec_validate (vam->ip4_nbr_counters, sw_if_index); + + if (mp->begin) + vec_free (vam->ip4_nbr_counters[sw_if_index]); + + v = (vl_api_ip4_nbr_counter_t *) & mp->c; + for (i = 0; i < count; i++) + { + vec_validate (vam->ip4_nbr_counters[sw_if_index], i); + counter = &vam->ip4_nbr_counters[sw_if_index][i]; + counter->address.s_addr = v->address; + counter->packets = clib_net_to_host_u64 (v->packets); + counter->bytes = clib_net_to_host_u64 (v->bytes); + counter->linkt = v->link_type; + v++; + } +} + static void vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp) { @@ -2087,6 +2123,43 @@ static void vl_api_vnet_ip6_fib_counters_t_handler_json } } +static void vl_api_vnet_ip6_nbr_counters_t_handler + (vl_api_vnet_ip6_nbr_counters_t * mp) +{ + /* not supported */ +} + +static void vl_api_vnet_ip6_nbr_counters_t_handler_json + (vl_api_vnet_ip6_nbr_counters_t * mp) +{ + vat_main_t *vam = &vat_main; + vl_api_ip6_nbr_counter_t *v; + ip6_nbr_counter_t *counter; + struct in6_addr ip6; + u32 sw_if_index; + u32 count; + int i; + + sw_if_index = ntohl (mp->sw_if_index); + count = ntohl (mp->count); + vec_validate (vam->ip6_nbr_counters, sw_if_index); + + if (mp->begin) + vec_free (vam->ip6_nbr_counters[sw_if_index]); + + v = (vl_api_ip6_nbr_counter_t *) & mp->c; + for (i = 0; i < count; i++) + { + vec_validate (vam->ip6_nbr_counters[sw_if_index], i); + counter = &vam->ip6_nbr_counters[sw_if_index][i]; + clib_memcpy (&ip6, &v->address, sizeof (ip6)); + counter->address = ip6; + counter->packets = clib_net_to_host_u64 (v->packets); + counter->bytes = clib_net_to_host_u64 (v->bytes); + v++; + } +} + static void vl_api_get_first_msg_id_reply_t_handler (vl_api_get_first_msg_id_reply_t * mp) { @@ -3490,6 +3563,10 @@ static void vl_api_flow_classify_details_t_handler_json #define vl_api_vnet_ip4_fib_counters_t_print vl_noop_handler #define vl_api_vnet_ip6_fib_counters_t_endian vl_noop_handler #define vl_api_vnet_ip6_fib_counters_t_print vl_noop_handler +#define vl_api_vnet_ip4_nbr_counters_t_endian vl_noop_handler +#define vl_api_vnet_ip4_nbr_counters_t_print vl_noop_handler +#define vl_api_vnet_ip6_nbr_counters_t_endian vl_noop_handler +#define vl_api_vnet_ip6_nbr_counters_t_print vl_noop_handler #define vl_api_lisp_adjacencies_get_reply_t_endian vl_noop_handler #define vl_api_lisp_adjacencies_get_reply_t_print vl_noop_handler @@ -3799,6 +3876,8 @@ _(DHCP_COMPL_EVENT, dhcp_compl_event) \ _(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \ _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters) \ _(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters) \ +_(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters) \ +_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters) \ _(MAP_ADD_DOMAIN_REPLY, map_add_domain_reply) \ _(MAP_DEL_DOMAIN_REPLY, map_del_domain_reply) \ _(MAP_ADD_DEL_RULE_REPLY, map_add_del_rule_reply) \ @@ -4131,6 +4210,8 @@ dump_stats_table (vat_main_t * vam) u64 packets; ip4_fib_counter_t *c4; ip6_fib_counter_t *c6; + ip4_nbr_counter_t *n4; + ip6_nbr_counter_t *n6; int i, j; if (!vam->json_output) @@ -4226,6 +4307,49 @@ dump_stats_table (vat_main_t * vam) } } + /* ip4 nbr counters */ + msg_array = vat_json_object_add (&node, "ip4_nbr_counters"); + vat_json_init_array (msg_array); + for (i = 0; i < vec_len (vam->ip4_nbr_counters); i++) + { + msg = vat_json_array_add (msg_array); + vat_json_init_object (msg); + vat_json_object_add_uint (msg, "sw_if_index", i); + counter_array = vat_json_object_add (msg, "c"); + vat_json_init_array (counter_array); + for (j = 0; j < vec_len (vam->ip4_nbr_counters[i]); j++) + { + counter = vat_json_array_add (counter_array); + vat_json_init_object (counter); + n4 = &vam->ip4_nbr_counters[i][j]; + vat_json_object_add_ip4 (counter, "address", n4->address); + vat_json_object_add_uint (counter, "link-type", n4->linkt); + vat_json_object_add_uint (counter, "packets", n4->packets); + vat_json_object_add_uint (counter, "bytes", n4->bytes); + } + } + + /* ip6 nbr counters */ + msg_array = vat_json_object_add (&node, "ip6_nbr_counters"); + vat_json_init_array (msg_array); + for (i = 0; i < vec_len (vam->ip6_nbr_counters); i++) + { + msg = vat_json_array_add (msg_array); + vat_json_init_object (msg); + vat_json_object_add_uint (msg, "sw_if_index", i); + counter_array = vat_json_object_add (msg, "c"); + vat_json_init_array (counter_array); + for (j = 0; j < vec_len (vam->ip6_nbr_counters[i]); j++) + { + counter = vat_json_array_add (counter_array); + vat_json_init_object (counter); + n6 = &vam->ip6_nbr_counters[i][j]; + vat_json_object_add_ip6 (counter, "address", n6->address); + vat_json_object_add_uint (counter, "packets", n6->packets); + vat_json_object_add_uint (counter, "bytes", n6->bytes); + } + } + vat_json_print (vam->ofp, &node); vat_json_free (&node); diff --git a/src/vat/vat.h b/src/vat/vat.h index 3d7d96ae..831bdf50 100644 --- a/src/vat/vat.h +++ b/src/vat/vat.h @@ -95,6 +95,22 @@ typedef struct u64 bytes; } ip6_fib_counter_t; +typedef struct +{ + struct in_addr address; + vnet_link_t linkt; + u64 packets; + u64 bytes; +} ip4_nbr_counter_t; + +typedef struct +{ + struct in6_addr address; + vnet_link_t linkt; + u64 packets; + u64 bytes; +} ip6_nbr_counter_t; + typedef struct { /* vpe input queue */ @@ -185,6 +201,8 @@ typedef struct u32 *ip4_fib_counters_vrf_id_by_index; ip6_fib_counter_t **ip6_fib_counters; u32 *ip6_fib_counters_vrf_id_by_index; + ip4_nbr_counter_t **ip4_nbr_counters; + ip6_nbr_counter_t **ip6_nbr_counters; /* Convenience */ vlib_main_t *vlib_main; diff --git a/src/vlib/counter.h b/src/vlib/counter.h index a7903206..abfa89ee 100644 --- a/src/vlib/counter.h +++ b/src/vlib/counter.h @@ -273,6 +273,20 @@ vlib_increment_combined_counter (vlib_combined_counter_main_t * cm, } } +#define vlib_prefetch_combined_counter(_cm, _cpu_index, _index) \ +{ \ + vlib_mini_counter_t *_cpu_minis; \ + \ + /* \ + * This CPU's mini index is assumed to already be in cache \ + */ \ + _cpu_minis = (_cm)->minis[(_cpu_index)]; \ + CLIB_PREFETCH(_cpu_minis + (_index), \ + sizeof(*_cpu_minis), \ + STORE); \ +} + + /** Get the value of a combined counter, never called in the speed path Scrapes the entire set of mini counters. Innacurate unless worker threads which might increment the counter are diff --git a/src/vnet/adj/adj.c b/src/vnet/adj/adj.c index e740c4cb..d0be0f0e 100644 --- a/src/vnet/adj/adj.c +++ b/src/vnet/adj/adj.c @@ -122,6 +122,10 @@ format_ip_adjacency (u8 * s, va_list * args) if (fiaf & FORMAT_IP_ADJACENCY_DETAIL) { + 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, " node:[%d]:%U", adj->rewrite_header.node_index, diff --git a/src/vnet/adj/adj_l2.c b/src/vnet/adj/adj_l2.c index 4d2dd708..5a083643 100644 --- a/src/vnet/adj/adj_l2.c +++ b/src/vnet/adj/adj_l2.c @@ -94,11 +94,11 @@ adj_l2_rewrite_inline (vlib_main_t * vm, rw_len0 = adj0[0].rewrite_header.data_bytes; vnet_buffer(p0)->ip.save_rewrite_length = rw_len0; - vlib_increment_combined_counter - (&adjacency_counters, - cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0-sizeof(ethernet_header_t)); + vlib_increment_combined_counter(&adjacency_counters, + cpu_index, + adj_index0, + /* packet increment */ 0, + /* byte increment */ rw_len0); /* Check MTU of outgoing interface. */ if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0) <= diff --git a/src/vnet/ip/ip4_forward.c b/src/vnet/ip/ip4_forward.c index 6e91b9e9..87b345bd 100644 --- a/src/vnet/ip/ip4_forward.c +++ b/src/vnet/ip/ip4_forward.c @@ -2402,19 +2402,12 @@ ip4_rewrite_inline (vlib_main_t * vm, error1); /* - * We've already accounted for an ethernet_header_t elsewhere + * pre-fetch the per-adjacency counters */ - if (PREDICT_FALSE (rw_len0 > sizeof (ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0 - sizeof (ethernet_header_t)); - - if (PREDICT_FALSE (rw_len1 > sizeof (ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, cpu_index, adj_index1, - /* packet increment */ 0, - /* byte increment */ rw_len1 - sizeof (ethernet_header_t)); + vlib_prefetch_combined_counter (&adjacency_counters, + cpu_index, adj_index0); + vlib_prefetch_combined_counter (&adjacency_counters, + cpu_index, adj_index1); /* Don't adjust the buffer for ttl issue; icmp-error node wants * to see the IP headerr */ @@ -2446,6 +2439,19 @@ ip4_rewrite_inline (vlib_main_t * vm, vnet_rewrite_two_headers (adj0[0], adj1[0], ip0, ip1, sizeof (ethernet_header_t)); + /* + * Bump the per-adjacency counters + */ + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0); + + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index1, 1, vlib_buffer_length_in_chain (vm, p1) + rw_len1); + if (is_midchain) { adj0->sub_type.midchain.fixup_func (vm, adj0, p0); @@ -2519,6 +2525,9 @@ ip4_rewrite_inline (vlib_main_t * vm, p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED; } + vlib_prefetch_combined_counter (&adjacency_counters, + cpu_index, adj_index0); + /* Guess we are only writing on simple Ethernet header. */ vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t)); @@ -2526,11 +2535,10 @@ ip4_rewrite_inline (vlib_main_t * vm, rw_len0 = adj0[0].rewrite_header.data_bytes; vnet_buffer (p0)->ip.save_rewrite_length = rw_len0; - if (PREDICT_FALSE (rw_len0 > sizeof (ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0 - sizeof (ethernet_header_t)); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0); /* Check MTU of outgoing interface. */ error0 = (vlib_buffer_length_in_chain (vm, p0) diff --git a/src/vnet/ip/ip6_forward.c b/src/vnet/ip/ip6_forward.c index 197a9b79..232f7283 100644 --- a/src/vnet/ip/ip6_forward.c +++ b/src/vnet/ip/ip6_forward.c @@ -2108,14 +2108,14 @@ ip6_rewrite_inline (vlib_main_t * vm, vnet_buffer (p0)->ip.save_rewrite_length = rw_len0; vnet_buffer (p1)->ip.save_rewrite_length = rw_len1; - vlib_increment_combined_counter (&adjacency_counters, - cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0); - vlib_increment_combined_counter (&adjacency_counters, - cpu_index, adj_index1, - /* packet increment */ 0, - /* byte increment */ rw_len1); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, adj_index1, + 1, vlib_buffer_length_in_chain (vm, p1) + rw_len1); /* Check MTU of outgoing interface. */ error0 = @@ -2233,10 +2233,10 @@ ip6_rewrite_inline (vlib_main_t * vm, rw_len0 = adj0[0].rewrite_header.data_bytes; vnet_buffer (p0)->ip.save_rewrite_length = rw_len0; - vlib_increment_combined_counter (&adjacency_counters, - cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0); /* Check MTU of outgoing interface. */ error0 = diff --git a/src/vnet/mpls/mpls_output.c b/src/vnet/mpls/mpls_output.c index 8292a0cb..c06cf917 100644 --- a/src/vnet/mpls/mpls_output.c +++ b/src/vnet/mpls/mpls_output.c @@ -128,18 +128,19 @@ mpls_output_inline (vlib_main_t * vm, rw_len0 = adj0[0].rewrite_header.data_bytes; rw_len1 = adj1[0].rewrite_header.data_bytes; - if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, - cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0-sizeof(ethernet_header_t)); - if (PREDICT_FALSE (rw_len1 > sizeof(ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, - cpu_index, adj_index1, - /* packet increment */ 0, - /* byte increment */ rw_len1-sizeof(ethernet_header_t)); + /* Bump the adj counters for packet and bytes */ + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, + 1, + vlib_buffer_length_in_chain (vm, p0) + rw_len0); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index1, + 1, + vlib_buffer_length_in_chain (vm, p1) + rw_len1); /* Check MTU of outgoing interface. */ if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <= @@ -234,12 +235,12 @@ mpls_output_inline (vlib_main_t * vm, /* Update packet buffer attributes/set output interface. */ rw_len0 = adj0[0].rewrite_header.data_bytes; - if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t))) - vlib_increment_combined_counter - (&adjacency_counters, - cpu_index, adj_index0, - /* packet increment */ 0, - /* byte increment */ rw_len0-sizeof(ethernet_header_t)); + vlib_increment_combined_counter + (&adjacency_counters, + cpu_index, + adj_index0, + 1, + vlib_buffer_length_in_chain (vm, p0) + rw_len0); /* Check MTU of outgoing interface. */ if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <= diff --git a/src/vpp/api/vpe.api b/src/vpp/api/vpe.api index f32ba670..a00033c5 100644 --- a/src/vpp/api/vpe.api +++ b/src/vpp/api/vpe.api @@ -230,6 +230,31 @@ manual_print manual_endian define vnet_ip4_fib_counters vl_api_ip4_fib_counter_t c[count]; }; +typeonly manual_print manual_endian define ip4_nbr_counter +{ + u32 address; + u8 link_type; + u64 packets; + u64 bytes; +}; + +/** + * @brief Per-neighbour (i.e. per-adjacency) coutners + * @param count The size of the array of counters + * @param sw_if_index The interface the adjacency is on + * @param begin Flag to indicate this is the first set of stats for this + * interface. If this flag is not set the it is a continuation of + * stats for this interface + * @param c counters + */ +manual_print manual_endian define vnet_ip4_nbr_counters +{ + u32 count; + u32 sw_if_index; + u8 begin; + vl_api_ip4_nbr_counter_t c[count]; +}; + typeonly manual_print manual_endian define ip6_fib_counter { u64 address[2]; @@ -245,6 +270,22 @@ manual_print manual_endian define vnet_ip6_fib_counters vl_api_ip6_fib_counter_t c[count]; }; +typeonly manual_print manual_endian define ip6_nbr_counter +{ + u64 address[2]; + u8 link_type; + u64 packets; + u64 bytes; +}; + +manual_print manual_endian define vnet_ip6_nbr_counters +{ + u32 count; + u32 sw_if_index; + u8 begin; + vl_api_ip6_nbr_counter_t c[count]; +}; + /** \brief Request for a single block of summary stats @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request diff --git a/src/vpp/stats/stats.c b/src/vpp/stats/stats.c index 391e02f6..5e9b0d69 100644 --- a/src/vpp/stats/stats.c +++ b/src/vpp/stats/stats.c @@ -49,7 +49,9 @@ _(WANT_STATS, want_stats) \ _(WANT_STATS_REPLY, want_stats_reply) \ _(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \ _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters) \ -_(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters) +_(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters) \ +_(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters) \ +_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters) /* These constants ensure msg sizes <= 1024, aka ring allocation */ #define SIMPLE_COUNTER_BATCH_SIZE 126 @@ -258,6 +260,313 @@ ip46_fib_stats_delay (stats_main_t * sm, u32 sec, u32 nsec) } } +/** + * @brief The context passed when collecting adjacency counters + */ +typedef struct ip4_nbr_stats_ctx_t_ +{ + /** + * The SW IF index all these adjs belong to + */ + u32 sw_if_index; + + /** + * A vector of ip4 nbr counters + */ + vl_api_ip4_nbr_counter_t *counters; +} ip4_nbr_stats_ctx_t; + +static adj_walk_rc_t +ip4_nbr_stats_cb (adj_index_t ai, void *arg) +{ + vl_api_ip4_nbr_counter_t *vl_counter; + vlib_counter_t adj_counter; + ip4_nbr_stats_ctx_t *ctx; + ip_adjacency_t *adj; + + ctx = arg; + vlib_get_combined_counter (&adjacency_counters, ai, &adj_counter); + + if (0 != adj_counter.packets) + { + vec_add2 (ctx->counters, vl_counter, 1); + adj = adj_get (ai); + + vl_counter->packets = clib_host_to_net_u64 (adj_counter.packets); + vl_counter->bytes = clib_host_to_net_u64 (adj_counter.bytes); + vl_counter->address = adj->sub_type.nbr.next_hop.ip4.as_u32; + vl_counter->link_type = adj->ia_link; + } + return (ADJ_WALK_RC_CONTINUE); +} + +#define MIN(x,y) (((x)<(y))?(x):(y)) + +static void +ip4_nbr_ship (stats_main_t * sm, ip4_nbr_stats_ctx_t * ctx) +{ + api_main_t *am = sm->api_main; + vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; + unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue; + vl_api_vnet_ip4_nbr_counters_t *mp = 0; + int first = 0; + + /* + * If the walk context has counters, which may be left over from the last + * suspend, then we continue from there. + */ + while (0 != vec_len (ctx->counters)) + { + u32 n_items = MIN (vec_len (ctx->counters), + IP4_FIB_COUNTER_BATCH_SIZE); + u8 pause = 0; + + dslock (sm, 0 /* release hint */ , 1 /* tag */ ); + + mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + + (n_items * + sizeof + (vl_api_ip4_nbr_counter_t))); + mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_NBR_COUNTERS); + mp->count = ntohl (n_items); + mp->sw_if_index = ntohl (ctx->sw_if_index); + mp->begin = first; + first = 0; + + /* + * copy the counters from the back of the context, then we can easily + * 'erase' them by resetting the vector length. + * The order we push the stats to the caller is not important. + */ + clib_memcpy (mp->c, + &ctx->counters[vec_len (ctx->counters) - n_items], + n_items * sizeof (*ctx->counters)); + + _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items; + + /* + * send to the shm q + */ + unix_shared_memory_queue_lock (q); + pause = unix_shared_memory_queue_is_full (q); + + vl_msg_api_send_shmem_nolock (q, (u8 *) & mp); + unix_shared_memory_queue_unlock (q); + dsunlock (sm); + + if (pause) + ip46_fib_stats_delay (sm, 0 /* sec */ , + STATS_RELEASE_DELAY_NS); + } +} + +static void +do_ip4_nbrs (stats_main_t * sm) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_interface_main_t *im = &vnm->interface_main; + vnet_sw_interface_t *si; + + ip4_nbr_stats_ctx_t ctx = { + .sw_if_index = 0, + .counters = NULL, + }; + + /* *INDENT-OFF* */ + pool_foreach (si, im->sw_interfaces, + ({ + /* + * update the interface we are now concerned with + */ + ctx.sw_if_index = si->sw_if_index; + + /* + * we are about to walk another interface, so we shouldn't have any pending + * stats to export. + */ + ASSERT(ctx.counters == NULL); + + /* + * visit each neighbour adjacency on the interface and collect + * its current stats. + * Because we hold the lock the walk is synchronous, so safe to routing + * updates. It's limited in work by the number of adjacenies on an + * interface, which is typically not huge. + */ + dslock (sm, 0 /* release hint */ , 1 /* tag */ ); + adj_nbr_walk (si->sw_if_index, + FIB_PROTOCOL_IP4, + ip4_nbr_stats_cb, + &ctx); + dsunlock (sm); + + /* + * if this interface has some adjacencies with counters then ship them, + * else continue to the next interface. + */ + if (NULL != ctx.counters) + { + ip4_nbr_ship(sm, &ctx); + } + })); + /* *INDENT-OFF* */ +} + +/** + * @brief The context passed when collecting adjacency counters + */ +typedef struct ip6_nbr_stats_ctx_t_ +{ + /** + * The SW IF index all these adjs belong to + */ + u32 sw_if_index; + + /** + * A vector of ip6 nbr counters + */ + vl_api_ip6_nbr_counter_t *counters; +} ip6_nbr_stats_ctx_t; + +static adj_walk_rc_t +ip6_nbr_stats_cb (adj_index_t ai, + void *arg) +{ + vl_api_ip6_nbr_counter_t *vl_counter; + vlib_counter_t adj_counter; + ip6_nbr_stats_ctx_t *ctx; + ip_adjacency_t *adj; + + ctx = arg; + vlib_get_combined_counter(&adjacency_counters, ai, &adj_counter); + + if (0 != adj_counter.packets) + { + vec_add2(ctx->counters, vl_counter, 1); + adj = adj_get(ai); + + vl_counter->packets = clib_host_to_net_u64(adj_counter.packets); + vl_counter->bytes = clib_host_to_net_u64(adj_counter.bytes); + vl_counter->address[0] = adj->sub_type.nbr.next_hop.ip6.as_u64[0]; + vl_counter->address[1] = adj->sub_type.nbr.next_hop.ip6.as_u64[1]; + vl_counter->link_type = adj->ia_link; + } + return (ADJ_WALK_RC_CONTINUE); +} + +#define MIN(x,y) (((x)<(y))?(x):(y)) + +static void +ip6_nbr_ship (stats_main_t * sm, + ip6_nbr_stats_ctx_t *ctx) +{ + api_main_t *am = sm->api_main; + vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; + unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue; + vl_api_vnet_ip6_nbr_counters_t *mp = 0; + int first = 0; + + /* + * If the walk context has counters, which may be left over from the last + * suspend, then we continue from there. + */ + while (0 != vec_len(ctx->counters)) + { + u32 n_items = MIN (vec_len (ctx->counters), + IP6_FIB_COUNTER_BATCH_SIZE); + u8 pause = 0; + + dslock (sm, 0 /* release hint */ , 1 /* tag */ ); + + mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + + (n_items * + sizeof + (vl_api_ip6_nbr_counter_t))); + mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_NBR_COUNTERS); + mp->count = ntohl (n_items); + mp->sw_if_index = ntohl (ctx->sw_if_index); + mp->begin = first; + first = 0; + + /* + * copy the counters from the back of the context, then we can easily + * 'erase' them by resetting the vector length. + * The order we push the stats to the caller is not important. + */ + clib_memcpy (mp->c, + &ctx->counters[vec_len (ctx->counters) - n_items], + n_items * sizeof (*ctx->counters)); + + _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items; + + /* + * send to the shm q + */ + unix_shared_memory_queue_lock (q); + pause = unix_shared_memory_queue_is_full (q); + + vl_msg_api_send_shmem_nolock (q, (u8 *) & mp); + unix_shared_memory_queue_unlock (q); + dsunlock (sm); + + if (pause) + ip46_fib_stats_delay (sm, 0 /* sec */ , + STATS_RELEASE_DELAY_NS); + } +} + +static void +do_ip6_nbrs (stats_main_t * sm) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_interface_main_t *im = &vnm->interface_main; + vnet_sw_interface_t *si; + + ip6_nbr_stats_ctx_t ctx = { + .sw_if_index = 0, + .counters = NULL, + }; + + /* *INDENT-OFF* */ + pool_foreach (si, im->sw_interfaces, + ({ + /* + * update the interface we are now concerned with + */ + ctx.sw_if_index = si->sw_if_index; + + /* + * we are about to walk another interface, so we shouldn't have any pending + * stats to export. + */ + ASSERT(ctx.counters == NULL); + + /* + * visit each neighbour adjacency on the interface and collect + * its current stats. + * Because we hold the lock the walk is synchronous, so safe to routing + * updates. It's limited in work by the number of adjacenies on an + * interface, which is typically not huge. + */ + dslock (sm, 0 /* release hint */ , 1 /* tag */ ); + adj_nbr_walk (si->sw_if_index, + FIB_PROTOCOL_IP6, + ip6_nbr_stats_cb, + &ctx); + dsunlock (sm); + + /* + * if this interface has some adjacencies with counters then ship them, + * else continue to the next interface. + */ + if (NULL != ctx.counters) + { + ip6_nbr_ship(sm, &ctx); + } + })); + /* *INDENT-OFF* */ +} + static void do_ip4_fibs (stats_main_t * sm) { @@ -318,13 +627,7 @@ again: hash_foreach_pair (p, hash, ({ x.address.data_u32 = p->key; - if (lm->fib_result_n_words > 1) - { - x.index = vec_len (results); - vec_add (results, p->value, lm->fib_result_n_words); - } - else - x.index = p->value[0]; + x.index = p->value[0]; vec_add1 (routes, x); if (sm->data_structure_lock->release_hint) @@ -631,6 +934,8 @@ stats_thread_fn (void *arg) do_combined_interface_counters (sm); do_ip4_fibs (sm); do_ip6_fibs (sm); + do_ip4_nbrs (sm); + do_ip6_nbrs (sm); } } @@ -804,6 +1109,45 @@ vl_api_vnet_ip4_fib_counters_t_handler (vl_api_vnet_ip4_fib_counters_t * mp) } } +static void +vl_api_vnet_ip4_nbr_counters_t_handler (vl_api_vnet_ip4_nbr_counters_t * mp) +{ + vpe_client_registration_t *reg; + stats_main_t *sm = &stats_main; + unix_shared_memory_queue_t *q, *q_prev = NULL; + vl_api_vnet_ip4_nbr_counters_t *mp_copy = NULL; + u32 mp_size; + + mp_size = sizeof (*mp_copy) + + ntohl (mp->count) * sizeof (vl_api_ip4_nbr_counter_t); + + /* *INDENT-OFF* */ + pool_foreach(reg, sm->stats_registrations, + ({ + q = vl_api_client_index_to_input_queue (reg->client_index); + if (q) + { + if (q_prev && (q_prev->cursize < q_prev->maxsize)) + { + mp_copy = vl_msg_api_alloc_as_if_client(mp_size); + clib_memcpy(mp_copy, mp, mp_size); + vl_msg_api_send_shmem (q_prev, (u8 *)&mp); + mp = mp_copy; + } + q_prev = q; + } + })); + /* *INDENT-ON* */ + if (q_prev && (q_prev->cursize < q_prev->maxsize)) + { + vl_msg_api_send_shmem (q_prev, (u8 *) & mp); + } + else + { + vl_msg_api_free (mp); + } +} + static void vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp) { @@ -843,6 +1187,45 @@ vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp) } } +static void +vl_api_vnet_ip6_nbr_counters_t_handler (vl_api_vnet_ip6_nbr_counters_t * mp) +{ + vpe_client_registration_t *reg; + stats_main_t *sm = &stats_main; + unix_shared_memory_queue_t *q, *q_prev = NULL; + vl_api_vnet_ip6_nbr_counters_t *mp_copy = NULL; + u32 mp_size; + + mp_size = sizeof (*mp_copy) + + ntohl (mp->count) * sizeof (vl_api_ip6_nbr_counter_t); + + /* *INDENT-OFF* */ + pool_foreach(reg, sm->stats_registrations, + ({ + q = vl_api_client_index_to_input_queue (reg->client_index); + if (q) + { + if (q_prev && (q_prev->cursize < q_prev->maxsize)) + { + mp_copy = vl_msg_api_alloc_as_if_client(mp_size); + clib_memcpy(mp_copy, mp, mp_size); + vl_msg_api_send_shmem (q_prev, (u8 *)&mp); + mp = mp_copy; + } + q_prev = q; + } + })); + /* *INDENT-ON* */ + if (q_prev && (q_prev->cursize < q_prev->maxsize)) + { + vl_msg_api_send_shmem (q_prev, (u8 *) & mp); + } + else + { + vl_msg_api_free (mp); + } +} + static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp) { @@ -929,6 +1312,10 @@ stats_memclnt_delete_callback (u32 client_index) #define vl_api_vnet_ip4_fib_counters_t_print vl_noop_handler #define vl_api_vnet_ip6_fib_counters_t_endian vl_noop_handler #define vl_api_vnet_ip6_fib_counters_t_print vl_noop_handler +#define vl_api_vnet_ip4_nbr_counters_t_endian vl_noop_handler +#define vl_api_vnet_ip4_nbr_counters_t_print vl_noop_handler +#define vl_api_vnet_ip6_nbr_counters_t_endian vl_noop_handler +#define vl_api_vnet_ip6_nbr_counters_t_print vl_noop_handler static clib_error_t * stats_init (vlib_main_t * vm) @@ -961,6 +1348,8 @@ stats_init (vlib_main_t * vm) am->message_bounce[VL_API_VNET_INTERFACE_COUNTERS] = 1; am->message_bounce[VL_API_VNET_IP4_FIB_COUNTERS] = 1; am->message_bounce[VL_API_VNET_IP6_FIB_COUNTERS] = 1; + am->message_bounce[VL_API_VNET_IP4_NBR_COUNTERS] = 1; + am->message_bounce[VL_API_VNET_IP6_NBR_COUNTERS] = 1; return 0; } -- cgit 1.2.3-korg From 32e1c010b0c34fd0984f7fc45fae648a182025c5 Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Tue, 22 Nov 2016 17:07:28 +0000 Subject: IP Multicast FIB (mfib) - IPv[46] mfib tables with support for (*,G/m), (*,G) and (S,G) exact and longest prefix match - Replication represented via a new replicate DPO. - RPF configuration and data-plane checking - data-plane signals sent to listening control planes. The functions of multicast forwarding entries differ from their unicast conterparts, so we introduce a new mfib_table_t and mfib_entry_t objects. However, we re-use the fib_path_list to resolve and build the entry's output list. the fib_path_list provides the service to construct a replicate DPO for multicast. 'make tests' is added to with two new suites; TEST=mfib, this is invocation of the CLI command 'test mfib' which deals with many path add/remove, flag set/unset scenarios, TEST=ip-mcast, data-plane forwarding tests. Updated applications to use the new MIFB functions; - IPv6 NS/RA. - DHCPv6 unit tests for these are undated accordingly. Change-Id: I49ec37b01f1b170335a5697541c8fd30e6d3a961 Signed-off-by: Neale Ranns --- src/scripts/vnet/mcast/ip4 | 22 + src/vat/api_format.c | 173 +++++ src/vnet.am | 35 +- src/vnet/adj/adj.c | 11 +- src/vnet/adj/adj.h | 1 - src/vnet/adj/adj_internal.h | 2 + src/vnet/adj/adj_mcast.c | 346 ++++++++++ src/vnet/adj/adj_mcast.h | 78 +++ src/vnet/adj/adj_nbr.c | 2 +- src/vnet/adj/adj_rewrite.c | 53 -- src/vnet/adj/adj_rewrite.h | 49 -- src/vnet/dhcpv6/proxy_node.c | 46 +- src/vnet/dpo/dpo.c | 2 + src/vnet/dpo/dpo.h | 8 +- src/vnet/dpo/load_balance.c | 13 +- src/vnet/dpo/load_balance.h | 8 + src/vnet/dpo/replicate_dpo.c | 759 ++++++++++++++++++++++ src/vnet/dpo/replicate_dpo.h | 143 +++++ src/vnet/ethernet/arp.c | 84 ++- src/vnet/ethernet/ethernet.h | 2 + src/vnet/ethernet/interface.c | 20 + src/vnet/fib/fib_attached_export.c | 4 +- src/vnet/fib/fib_entry.h | 2 +- src/vnet/fib/fib_entry_delegate.c | 3 + src/vnet/fib/fib_entry_src.c | 4 + src/vnet/fib/fib_node.h | 2 + src/vnet/fib/fib_path.c | 91 ++- src/vnet/fib/fib_path_list.c | 9 +- src/vnet/fib/fib_path_list.h | 6 + src/vnet/fib/fib_table.c | 69 +- src/vnet/fib/fib_table.h | 16 + src/vnet/fib/fib_test.c | 207 +++--- src/vnet/fib/fib_types.c | 4 + src/vnet/fib/fib_types.h | 14 + src/vnet/fib/fib_urpf_list.c | 20 +- src/vnet/fib/ip4_fib.c | 50 +- src/vnet/fib/ip4_fib.h | 9 + src/vnet/fib/ip6_fib.c | 117 ++-- src/vnet/fib/ip6_fib.h | 11 +- src/vnet/fib/mpls_fib.c | 17 +- src/vnet/fib/mpls_fib.h | 9 + src/vnet/ip/ip.api | 53 ++ src/vnet/ip/ip4.h | 24 + src/vnet/ip/ip4_forward.c | 498 +++++---------- src/vnet/ip/ip4_input.c | 4 +- src/vnet/ip/ip6.h | 27 + src/vnet/ip/ip6_forward.c | 83 ++- src/vnet/ip/ip6_input.c | 43 +- src/vnet/ip/ip6_neighbor.c | 134 ++-- src/vnet/ip/ip_api.c | 210 +++++++ src/vnet/ip/lookup.c | 171 +++++ src/vnet/ip/lookup.h | 82 +-- src/vnet/mcast/mcast.c | 565 ----------------- src/vnet/mcast/mcast.h | 50 -- src/vnet/mcast/mcast_test.c | 149 ----- src/vnet/mfib/ip4_mfib.c | 465 ++++++++++++++ src/vnet/mfib/ip4_mfib.h | 95 +++ src/vnet/mfib/ip6_mfib.c | 663 +++++++++++++++++++ src/vnet/mfib/ip6_mfib.h | 109 ++++ src/vnet/mfib/mfib_entry.c | 1096 ++++++++++++++++++++++++++++++++ src/vnet/mfib/mfib_entry.h | 172 +++++ src/vnet/mfib/mfib_forward.c | 512 +++++++++++++++ src/vnet/mfib/mfib_itf.c | 119 ++++ src/vnet/mfib/mfib_itf.h | 63 ++ src/vnet/mfib/mfib_signal.c | 201 ++++++ src/vnet/mfib/mfib_signal.h | 59 ++ src/vnet/mfib/mfib_table.c | 489 ++++++++++++++ src/vnet/mfib/mfib_table.h | 331 ++++++++++ src/vnet/mfib/mfib_test.c | 1225 ++++++++++++++++++++++++++++++++++++ src/vnet/mfib/mfib_types.c | 213 +++++++ src/vnet/mfib/mfib_types.h | 185 ++++++ src/vnet/misc.c | 3 + src/vnet/rewrite.h | 31 + src/vnet/sr/sr.c | 4 +- src/vnet/util/radix.c | 1104 ++++++++++++++++++++++++++++++++ src/vnet/util/radix.h | 147 +++++ src/vnet/vxlan/vxlan.c | 112 +++- src/vpp/api/api.c | 14 +- src/vppinfra.am | 2 +- src/vppinfra/dlist.h | 2 +- src/vppinfra/format.c | 8 +- src/vppinfra/format.h | 4 +- src/vppinfra/unformat.c | 16 +- test/test_dhcp.py | 16 - test/test_ip6.py | 131 ++-- test/test_ip_mcast.py | 612 ++++++++++++++++++ test/test_mfib.py | 23 + test/vpp_interface.py | 3 +- test/vpp_ip_route.py | 101 ++- test/vpp_papi_provider.py | 34 +- 90 files changed, 11211 insertions(+), 1767 deletions(-) create mode 100644 src/scripts/vnet/mcast/ip4 create mode 100644 src/vnet/adj/adj_mcast.c create mode 100644 src/vnet/adj/adj_mcast.h delete mode 100644 src/vnet/adj/adj_rewrite.c delete mode 100644 src/vnet/adj/adj_rewrite.h create mode 100644 src/vnet/dpo/replicate_dpo.c create mode 100644 src/vnet/dpo/replicate_dpo.h delete mode 100644 src/vnet/mcast/mcast.c delete mode 100644 src/vnet/mcast/mcast.h delete mode 100644 src/vnet/mcast/mcast_test.c create mode 100644 src/vnet/mfib/ip4_mfib.c create mode 100644 src/vnet/mfib/ip4_mfib.h create mode 100644 src/vnet/mfib/ip6_mfib.c create mode 100644 src/vnet/mfib/ip6_mfib.h create mode 100644 src/vnet/mfib/mfib_entry.c create mode 100644 src/vnet/mfib/mfib_entry.h create mode 100644 src/vnet/mfib/mfib_forward.c create mode 100644 src/vnet/mfib/mfib_itf.c create mode 100644 src/vnet/mfib/mfib_itf.h create mode 100644 src/vnet/mfib/mfib_signal.c create mode 100644 src/vnet/mfib/mfib_signal.h create mode 100644 src/vnet/mfib/mfib_table.c create mode 100644 src/vnet/mfib/mfib_table.h create mode 100644 src/vnet/mfib/mfib_test.c create mode 100644 src/vnet/mfib/mfib_types.c create mode 100644 src/vnet/mfib/mfib_types.h create mode 100644 src/vnet/util/radix.c create mode 100644 src/vnet/util/radix.h create mode 100644 test/test_ip_mcast.py create mode 100644 test/test_mfib.py (limited to 'src/vnet/adj') diff --git a/src/scripts/vnet/mcast/ip4 b/src/scripts/vnet/mcast/ip4 new file mode 100644 index 00000000..69f1ee00 --- /dev/null +++ b/src/scripts/vnet/mcast/ip4 @@ -0,0 +1,22 @@ +packet-generator new { + name x + limit 1 + node ip4-input + size 64-64 + no-recycle + data { + ICMP: 1.0.0.2 -> 232.1.1.1 + ICMP echo_request + incrementing 100 + } +} + +trace add pg-input 100 +loop create +loop create +set int state loop0 up +set int state loop1 up + +ip mroute add 232.1.1.1 via pg0 Accept +ip mroute add 232.1.1.1 via loop0 Forward +ip mroute add 232.1.1.1 via loop1 Forward diff --git a/src/vat/api_format.c b/src/vat/api_format.c index b83313de..4cfe4a58 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -48,6 +48,7 @@ #include #include #include +#include #include "vat/json_format.h" @@ -505,6 +506,53 @@ unformat_flow_classify_table_type (unformat_input_t * input, va_list * va) return 1; } +static const char *mfib_flag_names[] = MFIB_ENTRY_NAMES_SHORT; +static const char *mfib_flag_long_names[] = MFIB_ENTRY_NAMES_LONG; +static const char *mfib_itf_flag_long_names[] = MFIB_ITF_NAMES_LONG; +static const char *mfib_itf_flag_names[] = MFIB_ITF_NAMES_SHORT; + +uword +unformat_mfib_itf_flags (unformat_input_t * input, va_list * args) +{ + mfib_itf_flags_t old, *iflags = va_arg (*args, mfib_itf_flags_t *); + mfib_itf_attribute_t attr; + + old = *iflags; + FOR_EACH_MFIB_ITF_ATTRIBUTE (attr) + { + if (unformat (input, mfib_itf_flag_long_names[attr])) + *iflags |= (1 << attr); + } + FOR_EACH_MFIB_ITF_ATTRIBUTE (attr) + { + if (unformat (input, mfib_itf_flag_names[attr])) + *iflags |= (1 << attr); + } + + return (old == *iflags ? 0 : 1); +} + +uword +unformat_mfib_entry_flags (unformat_input_t * input, va_list * args) +{ + mfib_entry_flags_t old, *eflags = va_arg (*args, mfib_entry_flags_t *); + mfib_entry_attribute_t attr; + + old = *eflags; + FOR_EACH_MFIB_ATTRIBUTE (attr) + { + if (unformat (input, mfib_flag_long_names[attr])) + *eflags |= (1 << attr); + } + FOR_EACH_MFIB_ATTRIBUTE (attr) + { + if (unformat (input, mfib_flag_names[attr])) + *eflags |= (1 << attr); + } + + return (old == *eflags ? 0 : 1); +} + #if (VPP_API_TEST_BUILTIN==0) u8 * format_ip4_address (u8 * s, va_list * args) @@ -3592,6 +3640,7 @@ _(bridge_domain_add_del_reply) \ _(sw_interface_set_l2_xconnect_reply) \ _(l2fib_add_del_reply) \ _(ip_add_del_route_reply) \ +_(ip_mroute_add_del_reply) \ _(mpls_route_add_del_reply) \ _(mpls_ip_bind_unbind_reply) \ _(proxy_arp_add_del_reply) \ @@ -3792,6 +3841,7 @@ _(TAP_MODIFY_REPLY, tap_modify_reply) \ _(TAP_DELETE_REPLY, tap_delete_reply) \ _(SW_INTERFACE_TAP_DETAILS, sw_interface_tap_details) \ _(IP_ADD_DEL_ROUTE_REPLY, ip_add_del_route_reply) \ +_(IP_MROUTE_ADD_DEL_REPLY, ip_mroute_add_del_reply) \ _(MPLS_ROUTE_ADD_DEL_REPLY, mpls_route_add_del_reply) \ _(MPLS_IP_BIND_UNBIND_REPLY, mpls_ip_bind_unbind_reply) \ _(PROXY_ARP_ADD_DEL_REPLY, proxy_arp_add_del_reply) \ @@ -6383,6 +6433,126 @@ api_ip_add_del_route (vat_main_t * vam) return (vam->retval); } +static int +api_ip_mroute_add_del (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_ip_mroute_add_del_t *mp; + f64 timeout; + u32 sw_if_index = ~0, vrf_id = 0; + u8 is_ipv6 = 0; + u8 is_local = 0; + u8 create_vrf_if_needed = 0; + u8 is_add = 1; + u8 address_set = 0; + u32 grp_address_length = 0; + ip4_address_t v4_grp_address, v4_src_address; + ip6_address_t v6_grp_address, v6_src_address; + mfib_itf_flags_t iflags = 0; + mfib_entry_flags_t eflags = 0; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "%U %U", + unformat_ip4_address, &v4_src_address, + unformat_ip4_address, &v4_grp_address)) + { + grp_address_length = 64; + address_set = 1; + is_ipv6 = 0; + } + else if (unformat (i, "%U %U", + unformat_ip6_address, &v6_src_address, + unformat_ip6_address, &v6_grp_address)) + { + grp_address_length = 256; + address_set = 1; + is_ipv6 = 1; + } + else if (unformat (i, "%U", unformat_ip4_address, &v4_grp_address)) + { + memset (&v4_src_address, 0, sizeof (v4_src_address)); + grp_address_length = 32; + address_set = 1; + is_ipv6 = 0; + } + else if (unformat (i, "%U", unformat_ip6_address, &v6_grp_address)) + { + memset (&v6_src_address, 0, sizeof (v6_src_address)); + grp_address_length = 128; + address_set = 1; + is_ipv6 = 1; + } + else if (unformat (i, "/%d", &grp_address_length)) + ; + else if (unformat (i, "local")) + { + is_local = 1; + } + else if (unformat (i, "del")) + is_add = 0; + else if (unformat (i, "add")) + is_add = 1; + else if (unformat (i, "vrf %d", &vrf_id)) + ; + else if (unformat (i, "create-vrf")) + create_vrf_if_needed = 1; + else if (unformat (i, "%U", unformat_mfib_itf_flags, &iflags)) + ; + else if (unformat (i, "%U", unformat_mfib_entry_flags, &eflags)) + ; + else + { + clib_warning ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (address_set == 0) + { + errmsg ("missing addresses\n"); + return -99; + } + + /* Construct the API message */ + M (IP_MROUTE_ADD_DEL, ip_mroute_add_del); + + mp->next_hop_sw_if_index = ntohl (sw_if_index); + mp->table_id = ntohl (vrf_id); + mp->create_vrf_if_needed = create_vrf_if_needed; + + mp->is_add = is_add; + mp->is_ipv6 = is_ipv6; + mp->is_local = is_local; + mp->itf_flags = ntohl (iflags); + mp->entry_flags = ntohl (eflags); + mp->grp_address_length = grp_address_length; + mp->grp_address_length = ntohs (mp->grp_address_length); + + if (is_ipv6) + { + clib_memcpy (mp->grp_address, &v6_grp_address, sizeof (v6_grp_address)); + clib_memcpy (mp->src_address, &v6_src_address, sizeof (v6_src_address)); + } + else + { + clib_memcpy (mp->grp_address, &v4_grp_address, sizeof (v4_grp_address)); + clib_memcpy (mp->src_address, &v4_src_address, sizeof (v4_src_address)); + + } + + /* send it... */ + S; + /* Wait for a reply... */ + W; + + /* Return the good/bad news */ + return (vam->retval); +} + static int api_mpls_route_add_del (vat_main_t * vam) { @@ -17512,6 +17682,9 @@ _(ip_add_del_route, \ "[ | sw_if_index ] [resolve-attempts ]\n" \ "[weight ] [drop] [local] [classify ] [del]\n" \ "[multipath] [count ]") \ +_(ip_mroute_add_del, \ + " / [table-id ]\n" \ + "[ | sw_if_index ] [local] [del]") \ _(mpls_route_add_del, \ "