/* *------------------------------------------------------------------ * l2_api.c - layer 2 forwarding api * * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define vl_typedefs /* define message structures */ #include #undef vl_typedefs #define vl_endianfun /* define message structures */ #include #undef vl_endianfun #define vl_api_bridge_domain_details_t_endian vl_noop_handler #define vl_api_bridge_domain_details_t_print vl_noop_handler /* instantiate all the print functions we know about */ #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) #define vl_printfun #include #undef vl_printfun #include #define foreach_vpe_api_msg \ _(L2_XCONNECT_DUMP, l2_xconnect_dump) \ _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \ _(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \ _(L2FIB_FLUSH_ALL, l2fib_flush_all) \ _(L2FIB_FLUSH_INT, l2fib_flush_int) \ _(L2FIB_FLUSH_BD, l2fib_flush_bd) \ _(L2FIB_ADD_DEL, l2fib_add_del) \ _(WANT_L2_MACS_EVENTS, want_l2_macs_events) \ _(L2_FLAGS, l2_flags) \ _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \ _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \ _(L2_PATCH_ADD_DEL, l2_patch_add_del) \ _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \ _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \ _(BD_IP_MAC_FLUSH, bd_ip_mac_flush) \ _(BD_IP_MAC_DUMP, bd_ip_mac_dump) \ _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \ _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \ _(BRIDGE_FLAGS, bridge_flags) \ _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \ _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \ _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \ _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \ _(BVI_CREATE, bvi_create) \ _(BVI_DELETE, bvi_delete) static void send_l2_xconnect_details (vl_api_registration_t * reg, u32 context, u32 rx_sw_if_index, u32 tx_sw_if_index) { vl_api_l2_xconnect_details_t *mp; mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS); mp->context = context; mp->rx_sw_if_index = htonl (rx_sw_if_index); mp->tx_sw_if_index = htonl (tx_sw_if_index); vl_api_send_msg (reg, (u8 *) mp); } static void vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp) { vl_api_registration_t *reg; vnet_main_t *vnm = vnet_get_main (); vnet_interface_main_t *im = &vnm->interface_main; l2input_main_t *l2im = &l2input_main; vnet_sw_interface_t *swif; l2_input_config_t *config; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* *INDENT-OFF* */ pool_foreach (swif, im->sw_interfaces, ({ config = vec_elt_at_index (l2im->configs, swif->sw_if_index); if (config->xconnect) send_l2_xconnect_details (reg, mp->context, swif->sw_if_index, config->output_sw_if_index); })); /* *INDENT-ON* */ } static void vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp) { int rv = 0; vl_api_l2_fib_clear_table_reply_t *rmp; /* Clear all MACs including static MACs */ l2fib_clear_table (); REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY); } static void send_l2fib_table_entry (vpe_api_main_t * am, vl_api_registration_t * reg, l2fib_entry_key_t * l2fe_key, l2fib_entry_result_t * l2fe_res, u32 context) { vl_api_l2_fib_table_details_t *mp; mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS); mp->bd_id = ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id); clib_memcpy (mp->mac, l2fe_key->fields.mac, 6); mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index); mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0); mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe
#   BSD LICENSE
#
#   Copyright 2016 NXP.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#     * Neither the name of NXP nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

include $(RTE_SDK)/mk/rte.vars.mk

core-libs := librte_eal librte_mbuf librte_mempool librte_ring librte_ether

DIRS-$(CONFIG_RTE_LIBRTE_FSLMC_BUS) += fslmc
DEPDIRS-fslmc = $(core-libs)

include $(RTE_SDK)/mk/rte.subdir.mk
>rx_sw_if_index); u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index); vlib_main_t *vm = vlib_get_main (); vnet_main_t *vnm = vnet_get_main (); VALIDATE_RX_SW_IF_INDEX (mp); if (mp->enable) { VALIDATE_TX_SW_IF_INDEX (mp); rv = set_int_l2_mode (vm, vnm, MODE_L2_XC, rx_sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index); } else { rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL, 0, 0); } switch (rv) { case MODE_ERROR_ETH: rv = VNET_API_ERROR_NON_ETHERNET; break; case MODE_ERROR_BVI_DEF: rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI; break; } BAD_RX_SW_IF_INDEX_LABEL; BAD_TX_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY); } static int l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l) { v = clib_net_to_host_u32 (v); switch (v) { case L2_API_PORT_TYPE_NORMAL: *l = L2_BD_PORT_TYPE_NORMAL; return 0; case L2_API_PORT_TYPE_BVI: *l = L2_BD_PORT_TYPE_BVI; return 0; case L2_API_PORT_TYPE_UU_FWD: *l = L2_BD_PORT_TYPE_UU_FWD; return 0; } return (VNET_API_ERROR_INVALID_VALUE); } static void vl_api_sw_interface_set_l2_bridge_t_handler (vl_api_sw_interface_set_l2_bridge_t * mp) { bd_main_t *bdm = &bd_main; vl_api_sw_interface_set_l2_bridge_reply_t *rmp; int rv = 0; vlib_main_t *vm = vlib_get_main (); vnet_main_t *vnm = vnet_get_main (); l2_bd_port_type_t pt; VALIDATE_RX_SW_IF_INDEX (mp); u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index); rv = l2_bd_port_type_decode (mp->port_type, &pt); if (0 != rv) goto out; if (mp->enable) { VALIDATE_BD_ID (mp); u32 bd_id = ntohl (mp->bd_id); u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id); rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE, rx_sw_if_index, bd_index, pt, mp->shg, 0); } else { rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0); } switch (rv) { case MODE_ERROR_ETH: rv = VNET_API_ERROR_NON_ETHERNET; break; case MODE_ERROR_BVI_DEF: rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI; break; } BAD_RX_SW_IF_INDEX_LABEL; BAD_BD_ID_LABEL; out: REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY); } static void send_bd_ip_mac_entry (vpe_api_main_t * am, vl_api_registration_t * reg, u32 bd_id, u8 is_ipv6, u8 * ip_address, u8 * mac_address, u32 context) { vl_api_bd_ip_mac_details_t *mp; mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS); mp->bd_id = ntohl (bd_id); clib_memcpy (mp->mac_address, mac_address, 6); mp->is_ipv6 = is_ipv6; clib_memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4); mp->context = context; vl_api_send_msg (reg, (u8 *) mp); } static void vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp) { vpe_api_main_t *am = &vpe_api_main; bd_main_t *bdm = &bd_main; l2_bridge_domain_t *bd_config; u32 bd_id = ntohl (mp->bd_id); u32 bd_index, start, end; vl_api_registration_t *reg; uword *p; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* see bd_id: ~0 means "any" */ if (bd_id == ~0) { start = 1; end = vec_len (l2input_main.bd_configs); } else { p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) return; bd_index = p[0]; vec_validate (l2input_main.bd_configs, bd_index); start = bd_index; end = start + 1; } for (bd_index = start; bd_index < end; bd_index++) { bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index); if (bd_is_valid (bd_config)) { ip4_address_t ip4_addr; ip6_address_t *ip6_addr; u64 mac_addr; bd_id = bd_config->bd_id; /* *INDENT-OFF* */ hash_foreach (ip4_addr.as_u32, mac_addr, bd_config->mac_by_ip4, ({ send_bd_ip_mac_entry (am, reg, bd_id, 0, (u8 *) &(ip4_addr.as_u8), (u8 *) &mac_addr, mp->context); })); hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6, ({ send_bd_ip_mac_entry (am, reg, bd_id, 1, (u8 *) &(ip6_addr->as_u8), (u8 *) &mac_addr, mp->context); })); /* *INDENT-ON* */ } } } static void vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp) { ip46_address_t ip_addr = ip46_address_initializer; vl_api_bd_ip_mac_add_del_reply_t *rmp; bd_main_t *bdm = &bd_main; u32 bd_index, bd_id; mac_address_t mac; ip46_type_t type; int rv = 0; uword *p; bd_id = ntohl (mp->bd_id); if (bd_id == 0) { rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; goto out; } p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto out; } bd_index = p[0]; type = ip_address_decode (&mp->ip, &ip_addr); mac_address_decode (mp->mac, &mac); if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add)) rv = VNET_API_ERROR_UNSPECIFIED; out: REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY); } static void vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp) { vl_api_bd_ip_mac_flush_reply_t *rmp; bd_main_t *bdm = &bd_main; u32 bd_index, bd_id; int rv = 0; uword *p; bd_id = ntohl (mp->bd_id); if (bd_id == 0) { rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; goto out; } p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto out; } bd_index = p[0]; bd_flush_ip_mac (bd_index); out: REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY); } extern void l2_efp_filter_configure (vnet_main_t * vnet_main, u32 sw_if_index, u8 enable); static void vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t * mp) { int rv; vl_api_l2_interface_efp_filter_reply_t *rmp; vnet_main_t *vnm = vnet_get_main (); VALIDATE_SW_IF_INDEX (mp); // enable/disable the feature l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable); rv = vnm->api_errno; BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY); } static void vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp) { extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add); vl_api_l2_patch_add_del_reply_t *rmp; int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add); int rv = 0; VALIDATE_RX_SW_IF_INDEX (mp); VALIDATE_TX_SW_IF_INDEX (mp); rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index), ntohl (mp->tx_sw_if_index), (int) (mp->is_add != 0)); BAD_RX_SW_IF_INDEX_LABEL; BAD_TX_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY); } static void vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp) { vl_api_sw_interface_set_vpath_reply_t *rmp; int rv = 0; u32 sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable); vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6", sw_if_index, mp->enable, 0, 0); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY); } static void vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp) { vl_api_bvi_create_reply_t *rmp; mac_address_t mac; u32 sw_if_index; int rv; mac_address_decode (mp->mac, &mac); rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index); /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_BVI_CREATE_REPLY, ({ rmp->sw_if_index = ntohl (sw_if_index); })); /* *INDENT-ON* */ } static void vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp) { vl_api_bvi_delete_reply_t *rmp; int rv; rv = l2_bvi_delete (ntohl (mp->sw_if_index)); REPLY_MACRO (VL_API_BVI_DELETE_REPLY); } /* * l2_api_hookup * Add vpe's API message handlers to the table. * vlib has already mapped shared memory and * added the client registration handlers. * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() */ #define vl_msg_name_crc_list #include #undef vl_msg_name_crc_list static void setup_message_id_table (api_main_t * am) { #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); foreach_vl_msg_name_crc_l2; #undef _ } static clib_error_t * l2_api_hookup (vlib_main_t * vm) { api_main_t *am = &api_main; #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_vpe_api_msg; #undef _ /* * Set up the (msg_name, crc, message-id) table */ setup_message_id_table (am); return 0; } VLIB_API_INIT_FUNCTION (l2_api_hookup); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */