/* * mpls_tunnel.c: MPLS tunnel interfaces (i.e. for RSVP-TE) * * Copyright (c) 2012 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 /** * @brief pool of tunnel instances */ static mpls_tunnel_t *mpls_tunnel_pool; /** * @brief Pool of free tunnel SW indices - i.e. recycled indices */ static u32 * mpls_tunnel_free_hw_if_indices; /** * @brief DB of SW index to tunnel index */ static u32 *mpls_tunnel_db; /** * @brief Get a tunnel object from a SW interface index */ static mpls_tunnel_t* mpls_tunnel_get_from_sw_if_index (u32 sw_if_index) { if ((vec_len(mpls_tunnel_db) < sw_if_index) || (~0 == mpls_tunnel_db[sw_if_index])) return (NULL); return (pool_elt_at_index(mpls_tunnel_pool, mpls_tunnel_db[sw_if_index])); } /** * @brief Return true if the label stack is imp-null only */ static fib_forward_chain_type_t mpls_tunnel_get_fwd_chain_type (const mpls_tunnel_t *mt) { if ((1 == vec_len(mt->mt_label_stack)) && (mt->mt_label_stack[0] == MPLS_IETF_IMPLICIT_NULL_LABEL)) { /* * the only label in the label stack is implicit null * we need to build an IP chain. */ if (FIB_PROTOCOL_IP4 == fib_path_list_get_proto(mt->mt_path_list)) { return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); } else { return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6); } } else { return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS); } } /** * @brief Build a rewrite string for the MPLS tunnel. * * We have choices here; * 1 - have an Adjacency with a zero length string and stack it on * MPLS label objects * 2 - put the label header rewrites in the adjacency string. * * We choose 2 since it results in fewer graph nodes in the egress path */ static u8* mpls_tunnel_build_rewrite (vnet_main_t * vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address) { mpls_unicast_header_t *muh; mpls_tunnel_t *mt; u8 *rewrite; u32 mti, ii; rewrite = NULL; mti = mpls_tunnel_db[sw_if_index]; mt = pool_elt_at_index(mpls_tunnel_pool, mti); /* * The vector must be allocated as u8 so the length is correct */ ASSERT(0 < vec_len(mt->mt_label_stack)); vec_validate(rewrite, (sizeof(*muh) * vec_len(mt->mt_label_stack)) - 1); ASSERT(rewrite); muh = (mpls_unicast_header_t *)rewrite; /* * The last (inner most) label in the stack may be EOS, all the rest Non-EOS */ for (ii = 0; ii < vec_len(mt->mt_label_stack)-1; ii++) { vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]); vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255); vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0); vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS); muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl); } vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]); vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255); vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0); if ((VNET_LINK_MPLS == link_type) && (mt->mt_label_stack[ii] != MPLS_IETF_IMPLICIT_NULL_LABEL)) { vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS); } else { vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_EOS); } muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl); return (rewrite); } /** * mpls_tunnel_stack * * 'stack' (resolve the recursion for) the tunnel's midchain adjacency */ static void mpls_tunnel_stack (adj_index_t ai) { ip_adjacency_t *adj; mpls_tunnel_t *mt; u32 sw_if_index; adj = adj_get(ai); sw_if_index = adj->rewrite_header.sw_if_index; mt = mpls_tunnel_get_from_sw_if_index(sw_if_index); if (NULL == mt) return; /* * find the adjacency that is contributed by the FIB path-list * that this tunnel resovles via, and use it as the next adj * in the midchain */ if (vnet_hw_interface_get_flags(vnet_get_main(), mt->mt_hw_if_index) & VNET_HW_INTERFACE_FLAG_LINK_UP) { dpo_id_t dpo = DPO_INVALID; fib_path_list_contribute_forwarding(mt->mt_path_list, mpls_tunnel_get_fwd_chain_type(mt), &dpo); if (DPO_LOAD_BALANCE == dpo.dpoi_type) { /* * we don't support multiple paths, so no need to load-balance. * pull the first and only choice and stack directly on that. */ load_balance_t *lb; lb = load_balance_get (dpo.dpoi_index); ASSERT(1 == lb->lb_n_buckets); dpo_copy(&dpo, load_balance_get_bucket_i (lb, 0)); } adj_nbr_midchain_stack(ai, &dpo); dpo_reset(&dpo); } else { adj_nbr_midchain_unstack(ai); } } /** * @brief Call back when restacking all adjacencies on a MPLS interface */ static adj_walk_rc_t mpls_adj_walk_cb (adj_index_t ai, void *ctx) { mpls_tunnel_stack(ai); return (ADJ_WALK_RC_CONTINUE); } static void mpls_tunnel_restack (mpls_tunnel_t *mt) { fib_protocol_t proto; /* * walk all the adjacencies on the MPLS interface and restack them */ FOR_EACH_FIB_PROTOCOL(proto) { adj_nbr_walk(mt->mt_sw_if_index, proto, mpls_adj_walk_cb, NULL); } } static clib_error_t * mpls_tunnel_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) { vnet_hw_interface_t * hi; mpls_tunnel_t *mt; hi = vnet_get_hw_interface (vnm, hw_if_index); mt = mpls_tunnel_get_from_sw_if_index(hi->sw_if_index); if (NULL == mt) return (NULL);
VAPICLIENT_17.07 {
	global:
	vapi_msg_alloc;
	vapi_msg_free;
	vapi_ctx_alloc;
	vapi_ctx_free;
	vapi_is_msg_available;
	vapi_connect;
	vapi_disconnect;
	vapi_get_fd;
	vapi_send;
	vapi_send2;
	vapi_recv;
	vapi_wait;
	vapi_dispatch_one;
	vapi_dispatch;
	vapi_set_event_cb;
	vapi_clear_event_cb;
	vapi_set_generic_event_cb;
	vapi_clear_generic_event_cb;
	vapi_get_client_index;
	vapi_register_msg;
	vapi_get_client_index;
	vapi_is_nonblocking;
	vapi_requests_empty;
	vapi_requests_full;
	vapi_gen_req_context;
	vapi_producer_lock;
	vapi_send_with_control_ping;
	vapi_store_request;
	vapi_is_nonblocking;
	vapi_producer_unlock;
	vapi_lookup_vl_msg_id;
	vapi_lookup_vapi_msg_id_t;
	vapi_msg_is_with_context;
	vapi_get_context_offset;
	vapi_msg_id_control_ping;
	vapi_msg_id_control_ping_reply;
	vapi_get_message_count;
	vapi_get_msg_name;

	local: *;
};
PUT) { if (unformat (input, "%d", &mti)) ; else break; } if (~0 == mti) { pool_foreach (mt, mpls_tunnel_pool, ({ vlib_cli_output (vm, "[@%d] %U", mt - mpls_tunnel_pool, format_mpls_tunnel, mt); })); } else { if (pool_is_free_index(mpls_tunnel_pool, mti)) return clib_error_return (0, "Not atunnel index %d", mti); mt = pool_elt_at_index(mpls_tunnel_pool, mti); vlib_cli_output (vm, "[@%d] %U", mt - mpls_tunnel_pool, format_mpls_tunnel, mt); } return 0; } /*? * This command to show MPLS tunnels * * @cliexpar * @cliexstart{sh mpls tunnel 2} * [@2] mpls_tunnel2: sw_if_index:5 hw_if_index:5 * label-stack: * 3, * via: * index:26 locks:1 proto:ipv4 uPRF-list:26 len:1 itfs:[2, ] * index:26 pl-index:26 ipv4 weight=1 attached-nexthop: oper-flags:resolved, * 10.0.0.2 loop0 * [@0]: ipv4 via 10.0.0.2 loop0: IP4: de:ad:00:00:00:00 -> 00:00:11:aa:bb:cc * @cliexend ?*/ VLIB_CLI_COMMAND (show_mpls_tunnel_command, static) = { .path = "show mpls tunnel", .function = show_mpls_tunnel_command_fn, }; static mpls_tunnel_t * mpls_tunnel_from_fib_node (fib_node_t *node) { #if (CLIB_DEBUG > 0) ASSERT(FIB_NODE_TYPE_MPLS_TUNNEL == node->fn_type); #endif return ((mpls_tunnel_t*) (((char*)node) - STRUCT_OFFSET_OF(mpls_tunnel_t, mt_node))); } /** * Function definition to backwalk a FIB node */ static fib_node_back_walk_rc_t mpls_tunnel_back_walk (fib_node_t *node, fib_node_back_walk_ctx_t *ctx) { mpls_tunnel_restack(mpls_tunnel_from_fib_node(node)); return (FIB_NODE_BACK_WALK_CONTINUE); } /** * Function definition to get a FIB node from its index */ static fib_node_t* mpls_tunnel_fib_node_get (fib_node_index_t index) { mpls_tunnel_t * mt; mt = pool_elt_at_index(mpls_tunnel_pool, index); return (&mt->mt_node); } /** * Function definition to inform the FIB node that its last lock has gone. */ static void mpls_tunnel_last_lock_gone (fib_node_t *node) { /* * The MPLS MPLS tunnel is a root of the graph. As such * it never has children and thus is never locked. */ ASSERT(0); } /* * Virtual function table registered by MPLS MPLS tunnels * for participation in the FIB object graph. */ const static fib_node_vft_t mpls_vft = { .fnv_get = mpls_tunnel_fib_node_get, .fnv_last_lock = mpls_tunnel_last_lock_gone, .fnv_back_walk = mpls_tunnel_back_walk, }; static clib_error_t * mpls_tunnel_init (vlib_main_t *vm) { fib_node_register_type(FIB_NODE_TYPE_MPLS_TUNNEL, &mpls_vft); return 0; } VLIB_INIT_FUNCTION(mpls_tunnel_init);