/* *------------------------------------------------------------------ * 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 #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) \ _(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) static void send_l2_xconnect_details (unix_shared_memory_queue_t * q, 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)); 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_msg_api_send_shmem (q, (u8 *) & mp); } static void vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp) { unix_shared_memory_queue_t *q; 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; q = vl_api_client_index_to_input_queue (mp->client_index); if (q == 0) 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 (q, 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, unix_shared_memory_queue_t * q, 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)); 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); mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0); mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index); mp->static_mac = l2fe_res->fields.static_mac; mp->filter_mac = l2fe_res->fields.filter; mp->bvi_mac = l2fe_res->fields.bvi; mp->context = context; vl_msg_api_send_shmem (q, (u8 *) & mp); } static void vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp) { vpe_api_main_t *am = &vpe_api_main; bd_main_t *bdm = &bd_main; l2fib_entry_key_t *l2fe_key = NULL; l2fib_entry_result_t *l2fe_res = NULL; u32 ni, bd_id = ntohl (mp->bd_id); u32 bd_index; unix_shared_memory_queue_t *q; uword *p; q = vl_api_client_index_to_input_queue (mp->client_index); if (q == 0) return; /* see l2fib_table_dump: ~0 means "any" */ if (bd_id == ~0) bd_index = ~0; else { p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) return; bd_index = p[0]; } l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res); vec_foreach_index (ni, l2fe_key) { send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni), vec_elt_at_index (l2fe_res, ni), mp->context); } vec_free (l2fe_key); vec_free (l2fe_res); } static void vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp) { bd_main_t *bdm = &bd_main; l2input_main_t *l2im = &l2input_main; vl_api_l2fib_add_del_reply_t *rmp; int rv = 0; u32 bd_id = ntohl (mp->bd_id); uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (!p) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto bad_sw_if_index; } u32 bd_index = p[0]; u64 mac = mp->mac; if (mp->is_add) { if (mp->filter_mac) l2fib_add_filter_entry (mac, bd_index); else { u32 sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); if (vec_len (l2im->configs) <= sw_if_index) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; goto bad_sw_if_index; } else { l2_input_config_t *config; config = vec_elt_at_index (l2im->configs, sw_if_index); if (config->bridge == 0) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; goto bad_sw_if_index; } } u8 static_mac = mp->static_mac ? 1 : 0; u8 bvi_mac = mp->bvi_mac ? 1 : 0; l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, bvi_mac); } } else { l2fib_del_entry (mac, bd_index); } BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY); } static void vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp) { int rv = 0; vl_api_want_l2_macs_events_reply_t *rmp; l2learn_main_t *lm = &l2learn_main; l2fib_main_t *fm = &l2fib_main; u32 pid = ntohl (mp->pid); u32 learn_limit = ntohl (mp->learn_limit); if (mp->enable_disable) { if (lm->client_pid == 0) { lm->client_pid = pid; lm->client_index = mp->client_index; if (mp->max_macs_in_event) fm->max_macs_in_event = mp->max_macs_in_event * 10; else fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT; if (mp->scan_delay) fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3; else fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT; /* change learn limit and flush all learned MACs */ if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT)) lm->global_learn_limit = learn_limit; else lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT; l2fib_flush_all_mac (vlib_get_main ()); } else if (lm->client_pid != pid) { rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT; goto exit; } } else if (lm->client_pid) { lm->client_pid = 0; lm->client_index = 0; if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT)) lm->global_learn_limit = learn_limit; else lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT; } exit: REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY); } static void vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp) { int rv = 0; vlib_main_t *vm = vlib_get_main (); vl_api_l2fib_flush_int_reply_t *rmp; VALIDATE_SW_IF_INDEX (mp); u32 sw_if_index = ntohl (mp->sw_if_index); l2fib_flush_int_mac (vm, sw_if_index); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY); } static void