Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * l2_api.c - layer 2 forwarding api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/l2/l2_input.h> |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 26 | #include <vnet/l2/l2_fib.h> |
Pavel Kotucek | adec587 | 2017-01-25 08:50:53 +0100 | [diff] [blame] | 27 | #include <vnet/l2/l2_vtr.h> |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 28 | #include <vnet/l2/l2_learn.h> |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 29 | |
| 30 | #include <vnet/vnet_msg_enum.h> |
| 31 | |
| 32 | #define vl_typedefs /* define message structures */ |
| 33 | #include <vnet/vnet_all_api_h.h> |
| 34 | #undef vl_typedefs |
| 35 | |
| 36 | #define vl_endianfun /* define message structures */ |
| 37 | #include <vnet/vnet_all_api_h.h> |
| 38 | #undef vl_endianfun |
| 39 | |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 40 | #define vl_api_bridge_domain_details_t_endian vl_noop_handler |
| 41 | #define vl_api_bridge_domain_details_t_print vl_noop_handler |
| 42 | |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 43 | /* instantiate all the print functions we know about */ |
| 44 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 45 | #define vl_printfun |
| 46 | #include <vnet/vnet_all_api_h.h> |
| 47 | #undef vl_printfun |
| 48 | |
| 49 | #include <vlibapi/api_helper_macros.h> |
| 50 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 51 | #define foreach_vpe_api_msg \ |
| 52 | _(L2_XCONNECT_DUMP, l2_xconnect_dump) \ |
| 53 | _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \ |
| 54 | _(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \ |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 55 | _(L2FIB_FLUSH_ALL, l2fib_flush_all) \ |
Eyal Bari | f24991c | 2017-04-05 05:33:21 +0300 | [diff] [blame] | 56 | _(L2FIB_FLUSH_INT, l2fib_flush_int) \ |
| 57 | _(L2FIB_FLUSH_BD, l2fib_flush_bd) \ |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 58 | _(L2FIB_ADD_DEL, l2fib_add_del) \ |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 59 | _(WANT_L2_MACS_EVENTS, want_l2_macs_events) \ |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 60 | _(L2_FLAGS, l2_flags) \ |
| 61 | _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \ |
| 62 | _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \ |
Pavel Kotucek | adec587 | 2017-01-25 08:50:53 +0100 | [diff] [blame] | 63 | _(BRIDGE_FLAGS, bridge_flags) \ |
| 64 | _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \ |
Eyal Bari | fead670 | 2017-04-04 04:46:32 +0300 | [diff] [blame] | 65 | _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \ |
| 66 | _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 67 | |
| 68 | static void |
| 69 | send_l2_xconnect_details (unix_shared_memory_queue_t * q, u32 context, |
| 70 | u32 rx_sw_if_index, u32 tx_sw_if_index) |
| 71 | { |
| 72 | vl_api_l2_xconnect_details_t *mp; |
| 73 | |
| 74 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 75 | memset (mp, 0, sizeof (*mp)); |
| 76 | mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS); |
| 77 | mp->context = context; |
| 78 | mp->rx_sw_if_index = htonl (rx_sw_if_index); |
| 79 | mp->tx_sw_if_index = htonl (tx_sw_if_index); |
| 80 | |
| 81 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp) |
| 86 | { |
| 87 | unix_shared_memory_queue_t *q; |
| 88 | vnet_main_t *vnm = vnet_get_main (); |
| 89 | vnet_interface_main_t *im = &vnm->interface_main; |
| 90 | l2input_main_t *l2im = &l2input_main; |
| 91 | vnet_sw_interface_t *swif; |
| 92 | l2_input_config_t *config; |
| 93 | |
| 94 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 95 | if (q == 0) |
| 96 | return; |
| 97 | |
| 98 | /* *INDENT-OFF* */ |
| 99 | pool_foreach (swif, im->sw_interfaces, |
| 100 | ({ |
| 101 | config = vec_elt_at_index (l2im->configs, swif->sw_if_index); |
| 102 | if (config->xconnect) |
| 103 | send_l2_xconnect_details (q, mp->context, swif->sw_if_index, |
| 104 | config->output_sw_if_index); |
| 105 | })); |
| 106 | /* *INDENT-ON* */ |
| 107 | } |
| 108 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 109 | static void |
| 110 | vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp) |
| 111 | { |
| 112 | int rv = 0; |
| 113 | vl_api_l2_fib_clear_table_reply_t *rmp; |
| 114 | |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 115 | /* Clear all MACs including static MACs */ |
| 116 | l2fib_clear_table (); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 117 | |
| 118 | REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY); |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | send_l2fib_table_entry (vpe_api_main_t * am, |
| 123 | unix_shared_memory_queue_t * q, |
| 124 | l2fib_entry_key_t * l2fe_key, |
| 125 | l2fib_entry_result_t * l2fe_res, u32 context) |
| 126 | { |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 127 | vl_api_l2_fib_table_details_t *mp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 128 | |
| 129 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 130 | memset (mp, 0, sizeof (*mp)); |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 131 | mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 132 | |
| 133 | mp->bd_id = |
| 134 | ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id); |
| 135 | |
| 136 | mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0); |
| 137 | mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index); |
| 138 | mp->static_mac = l2fe_res->fields.static_mac; |
| 139 | mp->filter_mac = l2fe_res->fields.filter; |
| 140 | mp->bvi_mac = l2fe_res->fields.bvi; |
| 141 | mp->context = context; |
| 142 | |
| 143 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 144 | } |
| 145 | |
| 146 | static void |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 147 | vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp) |
| 148 | { |
| 149 | vpe_api_main_t *am = &vpe_api_main; |
| 150 | bd_main_t *bdm = &bd_main; |
| 151 | l2fib_entry_key_t *l2fe_key = NULL; |
| 152 | l2fib_entry_result_t *l2fe_res = NULL; |
| 153 | u32 ni, bd_id = ntohl (mp->bd_id); |
| 154 | u32 bd_index; |
| 155 | unix_shared_memory_queue_t *q; |
| 156 | uword *p; |
| 157 | |
| 158 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 159 | if (q == 0) |
| 160 | return; |
| 161 | |
| 162 | /* see l2fib_table_dump: ~0 means "any" */ |
| 163 | if (bd_id == ~0) |
| 164 | bd_index = ~0; |
| 165 | else |
| 166 | { |
| 167 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
| 168 | if (p == 0) |
| 169 | return; |
| 170 | |
| 171 | bd_index = p[0]; |
| 172 | } |
| 173 | |
| 174 | l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res); |
| 175 | |
| 176 | vec_foreach_index (ni, l2fe_key) |
| 177 | { |
| 178 | send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni), |
| 179 | vec_elt_at_index (l2fe_res, ni), mp->context); |
| 180 | } |
| 181 | vec_free (l2fe_key); |
| 182 | vec_free (l2fe_res); |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp) |
| 187 | { |
| 188 | bd_main_t *bdm = &bd_main; |
| 189 | l2input_main_t *l2im = &l2input_main; |
| 190 | vl_api_l2fib_add_del_reply_t *rmp; |
| 191 | int rv = 0; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 192 | u32 bd_id = ntohl (mp->bd_id); |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 193 | uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 194 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 195 | if (!p) |
| 196 | { |
| 197 | rv = VNET_API_ERROR_NO_SUCH_ENTRY; |
| 198 | goto bad_sw_if_index; |
| 199 | } |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 200 | u32 bd_index = p[0]; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 201 | |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 202 | u64 mac = mp->mac; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 203 | if (mp->is_add) |
| 204 | { |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 205 | if (mp->filter_mac) |
| 206 | l2fib_add_filter_entry (mac, bd_index); |
| 207 | else |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 208 | { |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 209 | u32 sw_if_index = ntohl (mp->sw_if_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 210 | VALIDATE_SW_IF_INDEX (mp); |
| 211 | if (vec_len (l2im->configs) <= sw_if_index) |
| 212 | { |
| 213 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 214 | goto bad_sw_if_index; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | l2_input_config_t *config; |
| 219 | config = vec_elt_at_index (l2im->configs, sw_if_index); |
| 220 | if (config->bridge == 0) |
| 221 | { |
| 222 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 223 | goto bad_sw_if_index; |
| 224 | } |
| 225 | } |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 226 | u8 static_mac = mp->static_mac ? 1 : 0; |
| 227 | u8 bvi_mac = mp->bvi_mac ? 1 : 0; |
Eyal Bari | 31a71ab | 2017-06-25 14:42:33 +0300 | [diff] [blame] | 228 | l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_mac, |
| 229 | bvi_mac); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 230 | } |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
| 234 | l2fib_del_entry (mac, bd_index); |
| 235 | } |
| 236 | |
| 237 | BAD_SW_IF_INDEX_LABEL; |
| 238 | |
| 239 | REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY); |
| 240 | } |
| 241 | |
| 242 | static void |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 243 | vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp) |
| 244 | { |
| 245 | int rv = 0; |
| 246 | vl_api_want_l2_macs_events_reply_t *rmp; |
| 247 | l2learn_main_t *lm = &l2learn_main; |
| 248 | l2fib_main_t *fm = &l2fib_main; |
| 249 | u32 pid = ntohl (mp->pid); |
| 250 | u32 learn_limit = ntohl (mp->learn_limit); |
| 251 | |
| 252 | if (mp->enable_disable) |
| 253 | { |
| 254 | if (lm->client_pid == 0) |
| 255 | { |
| 256 | lm->client_pid = pid; |
| 257 | lm->client_index = mp->client_index; |
| 258 | |
| 259 | if (mp->max_macs_in_event) |
| 260 | fm->max_macs_in_event = mp->max_macs_in_event * 10; |
| 261 | else |
| 262 | fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT; |
| 263 | |
| 264 | if (mp->scan_delay) |
| 265 | fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3; |
| 266 | else |
| 267 | fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT; |
| 268 | |
| 269 | /* change learn limit and flush all learned MACs */ |
| 270 | if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT)) |
| 271 | lm->global_learn_limit = learn_limit; |
| 272 | else |
| 273 | lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT; |
| 274 | |
| 275 | l2fib_flush_all_mac (vlib_get_main ()); |
| 276 | } |
| 277 | else if (lm->client_pid != pid) |
| 278 | { |
| 279 | rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT; |
| 280 | goto exit; |
| 281 | } |
| 282 | } |
| 283 | else if (lm->client_pid) |
| 284 | { |
| 285 | lm->client_pid = 0; |
| 286 | lm->client_index = 0; |
John Lo | e531f4c | 2017-08-22 09:16:50 -0400 | [diff] [blame] | 287 | if (learn_limit) |
| 288 | lm->global_learn_limit = learn_limit; |
| 289 | else |
| 290 | lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | exit: |
| 294 | REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY); |
| 295 | } |
| 296 | |
| 297 | static void |
Eyal Bari | f24991c | 2017-04-05 05:33:21 +0300 | [diff] [blame] | 298 | vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp) |
| 299 | { |
| 300 | int rv = 0; |
| 301 | vlib_main_t *vm = vlib_get_main (); |
| 302 | vl_api_l2fib_flush_int_reply_t *rmp; |
| 303 | |
| 304 | VALIDATE_SW_IF_INDEX (mp); |
| 305 | |
| 306 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 307 | l2fib_flush_int_mac (vm, sw_if_index); |
| 308 | |
| 309 | BAD_SW_IF_INDEX_LABEL; |
| 310 | REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY); |
| 311 | } |
| 312 | |
| 313 | static void |
Eyal Bari | 7537e71 | 2017-04-27 14:07:55 +0300 | [diff] [blame] | 314 | vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp) |
| 315 | { |
| 316 | int rv = 0; |
| 317 | vl_api_l2fib_flush_all_reply_t *rmp; |
| 318 | |
| 319 | l2fib_flush_all_mac (vlib_get_main ()); |
| 320 | REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY); |
| 321 | } |
| 322 | |
| 323 | static void |
Eyal Bari | f24991c | 2017-04-05 05:33:21 +0300 | [diff] [blame] | 324 | vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp) |
| 325 | { |
| 326 | int rv = 0; |
| 327 | vlib_main_t *vm = vlib_get_main (); |
| 328 | bd_main_t *bdm = &bd_main; |
| 329 | vl_api_l2fib_flush_bd_reply_t *rmp; |
| 330 | |
| 331 | u32 bd_id = ntohl (mp->bd_id); |
| 332 | uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
| 333 | if (p == 0) |
| 334 | { |
| 335 | rv = VNET_API_ERROR_NO_SUCH_ENTRY; |
| 336 | goto out; |
| 337 | } |
| 338 | l2fib_flush_bd_mac (vm, *p); |
| 339 | out: |
| 340 | REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY); |
| 341 | } |
| 342 | |
| 343 | static void |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 344 | vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp) |
| 345 | { |
| 346 | vl_api_l2_flags_reply_t *rmp; |
| 347 | int rv = 0; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 348 | u32 rbm = 0; |
| 349 | |
| 350 | VALIDATE_SW_IF_INDEX (mp); |
| 351 | |
Eyal Bari | fead670 | 2017-04-04 04:46:32 +0300 | [diff] [blame] | 352 | u32 sw_if_index = ntohl (mp->sw_if_index); |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 353 | u32 flags = ntohl (mp->feature_bitmap); |
| 354 | u32 bitmap = 0; |
| 355 | |
| 356 | if (flags & L2_LEARN) |
| 357 | bitmap |= L2INPUT_FEAT_LEARN; |
| 358 | |
| 359 | if (flags & L2_FWD) |
| 360 | bitmap |= L2INPUT_FEAT_FWD; |
| 361 | |
| 362 | if (flags & L2_FLOOD) |
| 363 | bitmap |= L2INPUT_FEAT_FLOOD; |
| 364 | |
| 365 | if (flags & L2_UU_FLOOD) |
| 366 | bitmap |= L2INPUT_FEAT_UU_FLOOD; |
| 367 | |
| 368 | if (flags & L2_ARP_TERM) |
| 369 | bitmap |= L2INPUT_FEAT_ARP_TERM; |
| 370 | |
| 371 | rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 372 | |
| 373 | BAD_SW_IF_INDEX_LABEL; |
| 374 | |
| 375 | /* *INDENT-OFF* */ |
| 376 | REPLY_MACRO2(VL_API_L2_FLAGS_REPLY, |
| 377 | ({ |
| 378 | rmp->resulting_feature_bitmap = ntohl(rbm); |
| 379 | })); |
| 380 | /* *INDENT-ON* */ |
| 381 | } |
| 382 | |
| 383 | static void |
Eyal Bari | fead670 | 2017-04-04 04:46:32 +0300 | [diff] [blame] | 384 | vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t |
| 385 | * mp) |
| 386 | { |
| 387 | vlib_main_t *vm = vlib_get_main (); |
| 388 | bd_main_t *bdm = &bd_main; |
| 389 | vl_api_bridge_domain_set_mac_age_reply_t *rmp; |
| 390 | int rv = 0; |
| 391 | u32 bd_id = ntohl (mp->bd_id); |
Jon Loeliger | 1c7d485 | 2017-05-02 11:06:23 -0500 | [diff] [blame] | 392 | uword *p; |
| 393 | |
| 394 | if (bd_id == 0) |
| 395 | { |
| 396 | rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | p = hash_get (bdm->bd_index_by_bd_id, bd_id); |
Eyal Bari | fead670 | 2017-04-04 04:46:32 +0300 | [diff] [blame] | 401 | if (p == 0) |
| 402 | { |
| 403 | rv = VNET_API_ERROR_NO_SUCH_ENTRY; |
| 404 | goto out; |
| 405 | } |
| 406 | bd_set_mac_age (vm, *p, mp->mac_age); |
| 407 | out: |
| 408 | REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY); |
| 409 | } |
| 410 | |
| 411 | static void |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 412 | vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp) |
| 413 | { |
Eyal Bari | b1352ed | 2017-04-07 23:14:17 +0300 | [diff] [blame] | 414 | l2_bridge_domain_add_del_args_t a = { |
| 415 | .is_add = mp->is_add, |
| 416 | .flood = mp->flood, |
| 417 | .uu_flood = mp->uu_flood, |
| 418 | .forward = mp->forward, |
| 419 | .learn = mp->learn, |
| 420 | .arp_term = mp->arp_term, |
| 421 | .mac_age = mp->mac_age, |
| 422 | .bd_id = ntohl (mp->bd_id), |
| 423 | }; |
| 424 | |
| 425 | int rv = bd_add_del (&a); |
| 426 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 427 | vl_api_bridge_domain_add_del_reply_t *rmp; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 428 | REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY); |
| 429 | } |
| 430 | |
| 431 | static void |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 432 | send_bridge_domain_details (l2input_main_t * l2im, |
| 433 | unix_shared_memory_queue_t * q, |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 434 | l2_bridge_domain_t * bd_config, |
| 435 | u32 n_sw_ifs, u32 context) |
| 436 | { |
| 437 | vl_api_bridge_domain_details_t *mp; |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 438 | l2_flood_member_t *m; |
| 439 | vl_api_bridge_domain_sw_if_t *sw_ifs; |
| 440 | l2_input_config_t *input_cfg; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 441 | |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 442 | mp = vl_msg_api_alloc (sizeof (*mp) + |
| 443 | (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t))); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 444 | memset (mp, 0, sizeof (*mp)); |
| 445 | mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS); |
| 446 | mp->bd_id = ntohl (bd_config->bd_id); |
| 447 | mp->flood = bd_feature_flood (bd_config); |
| 448 | mp->uu_flood = bd_feature_uu_flood (bd_config); |
| 449 | mp->forward = bd_feature_forward (bd_config); |
| 450 | mp->learn = bd_feature_learn (bd_config); |
| 451 | mp->arp_term = bd_feature_arp_term (bd_config); |
| 452 | mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index); |
| 453 | mp->mac_age = bd_config->mac_age; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 454 | mp->context = context; |
| 455 | |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 456 | sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details; |
| 457 | vec_foreach (m, bd_config->members) |
| 458 | { |
| 459 | sw_ifs->sw_if_index = ntohl (m->sw_if_index); |
| 460 | input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index); |
| 461 | sw_ifs->shg = input_cfg->shg; |
| 462 | sw_ifs++; |
| 463 | mp->n_sw_ifs++; |
| 464 | } |
| 465 | mp->n_sw_ifs = htonl (mp->n_sw_ifs); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 466 | |
| 467 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp) |
| 472 | { |
| 473 | bd_main_t *bdm = &bd_main; |
| 474 | l2input_main_t *l2im = &l2input_main; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 475 | |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 476 | unix_shared_memory_queue_t *q = |
| 477 | vl_api_client_index_to_input_queue (mp->client_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 478 | if (q == 0) |
| 479 | return; |
| 480 | |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 481 | u32 bd_id = ntohl (mp->bd_id); |
Jon Loeliger | 1c7d485 | 2017-05-02 11:06:23 -0500 | [diff] [blame] | 482 | if (bd_id == 0) |
| 483 | return; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 484 | |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 485 | u32 bd_index, end; |
| 486 | if (bd_id == ~0) |
| 487 | bd_index = 0, end = vec_len (l2im->bd_configs); |
| 488 | else |
| 489 | { |
| 490 | bd_index = bd_find_index (bdm, bd_id); |
| 491 | if (bd_index == ~0) |
| 492 | return; |
| 493 | |
| 494 | end = bd_index + 1; |
| 495 | } |
Jon Loeliger | 1c7d485 | 2017-05-02 11:06:23 -0500 | [diff] [blame] | 496 | |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 497 | for (; bd_index < end; bd_index++) |
| 498 | { |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 499 | l2_bridge_domain_t *bd_config = |
| 500 | l2input_bd_config_from_index (l2im, bd_index); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 501 | /* skip dummy bd_id 0 */ |
| 502 | if (bd_config && (bd_config->bd_id > 0)) |
Ole Troan | 01384fe | 2017-05-12 11:55:35 +0200 | [diff] [blame] | 503 | send_bridge_domain_details (l2im, q, bd_config, |
| 504 | vec_len (bd_config->members), |
| 505 | mp->context); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp) |
| 511 | { |
| 512 | vlib_main_t *vm = vlib_get_main (); |
| 513 | bd_main_t *bdm = &bd_main; |
| 514 | vl_api_bridge_flags_reply_t *rmp; |
| 515 | int rv = 0; |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 516 | |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 517 | u32 flags = ntohl (mp->feature_bitmap); |
| 518 | u32 bd_id = ntohl (mp->bd_id); |
Jon Loeliger | 1c7d485 | 2017-05-02 11:06:23 -0500 | [diff] [blame] | 519 | if (bd_id == 0) |
| 520 | { |
| 521 | rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; |
| 522 | goto out; |
| 523 | } |
| 524 | |
Eyal Bari | 259fca7 | 2017-05-14 10:38:39 +0300 | [diff] [blame] | 525 | u32 bd_index = bd_find_index (bdm, bd_id); |
| 526 | if (bd_index == ~0) |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 527 | { |
| 528 | rv = VNET_API_ERROR_NO_SUCH_ENTRY; |
| 529 | goto out; |
| 530 | } |
| 531 | |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 532 | u32 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 533 | |
| 534 | out: |
| 535 | /* *INDENT-OFF* */ |
| 536 | REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY, |
| 537 | ({ |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 538 | rmp->resulting_feature_bitmap = ntohl(bitmap); |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 539 | })); |
| 540 | /* *INDENT-ON* */ |
| 541 | } |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 542 | |
Pavel Kotucek | adec587 | 2017-01-25 08:50:53 +0100 | [diff] [blame] | 543 | static void |
| 544 | vl_api_l2_interface_vlan_tag_rewrite_t_handler |
| 545 | (vl_api_l2_interface_vlan_tag_rewrite_t * mp) |
| 546 | { |
| 547 | int rv = 0; |
| 548 | vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp; |
| 549 | vnet_main_t *vnm = vnet_get_main (); |
| 550 | vlib_main_t *vm = vlib_get_main (); |
| 551 | u32 vtr_op; |
| 552 | |
| 553 | VALIDATE_SW_IF_INDEX (mp); |
| 554 | |
| 555 | vtr_op = ntohl (mp->vtr_op); |
| 556 | |
| 557 | /* The L2 code is unsuspicious */ |
| 558 | switch (vtr_op) |
| 559 | { |
| 560 | case L2_VTR_DISABLED: |
| 561 | case L2_VTR_PUSH_1: |
| 562 | case L2_VTR_PUSH_2: |
| 563 | case L2_VTR_POP_1: |
| 564 | case L2_VTR_POP_2: |
| 565 | case L2_VTR_TRANSLATE_1_1: |
| 566 | case L2_VTR_TRANSLATE_1_2: |
| 567 | case L2_VTR_TRANSLATE_2_1: |
| 568 | case L2_VTR_TRANSLATE_2_2: |
| 569 | break; |
| 570 | |
| 571 | default: |
| 572 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 573 | goto bad_sw_if_index; |
| 574 | } |
| 575 | |
| 576 | rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op, |
| 577 | ntohl (mp->push_dot1q), ntohl (mp->tag1), |
| 578 | ntohl (mp->tag2)); |
| 579 | |
| 580 | BAD_SW_IF_INDEX_LABEL; |
| 581 | |
| 582 | REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY); |
| 583 | } |
| 584 | |
| 585 | static void |
| 586 | vl_api_l2_interface_pbb_tag_rewrite_t_handler |
| 587 | (vl_api_l2_interface_pbb_tag_rewrite_t * mp) |
| 588 | { |
| 589 | vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp; |
| 590 | vnet_main_t *vnm = vnet_get_main (); |
| 591 | vlib_main_t *vm = vlib_get_main (); |
| 592 | u32 vtr_op; |
| 593 | int rv = 0; |
| 594 | |
| 595 | VALIDATE_SW_IF_INDEX (mp); |
| 596 | |
| 597 | vtr_op = ntohl (mp->vtr_op); |
| 598 | |
| 599 | switch (vtr_op) |
| 600 | { |
| 601 | case L2_VTR_DISABLED: |
| 602 | case L2_VTR_PUSH_2: |
| 603 | case L2_VTR_POP_2: |
| 604 | case L2_VTR_TRANSLATE_2_1: |
| 605 | break; |
| 606 | |
| 607 | default: |
| 608 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 609 | goto bad_sw_if_index; |
| 610 | } |
| 611 | |
| 612 | rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op, |
| 613 | mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid), |
| 614 | ntohl (mp->i_sid), ntohs (mp->outer_tag)); |
| 615 | |
| 616 | BAD_SW_IF_INDEX_LABEL; |
| 617 | |
| 618 | REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY); |
| 619 | } |
| 620 | |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 621 | /* |
Pavel Kotucek | 0f971d8 | 2017-01-03 10:48:54 +0100 | [diff] [blame] | 622 | * l2_api_hookup |
Matus Fabian | f468e23 | 2016-12-02 06:00:53 -0800 | [diff] [blame] | 623 | * Add vpe's API message handlers to the table. |
| 624 | * vlib has alread mapped shared memory and |
| 625 | * added the client registration handlers. |
| 626 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 627 | */ |
| 628 | #define vl_msg_name_crc_list |
| 629 | #include <vnet/vnet_all_api_h.h> |
| 630 | #undef vl_msg_name_crc_list |
| 631 | |
| 632 | static void |
| 633 | setup_message_id_table (api_main_t * am) |
| 634 | { |
| 635 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 636 | foreach_vl_msg_name_crc_l2; |
| 637 | #undef _ |
| 638 | } |
| 639 | |
| 640 | static clib_error_t * |
| 641 | l2_api_hookup (vlib_main_t * vm) |
| 642 | { |
| 643 | api_main_t *am = &api_main; |
| 644 | |
| 645 | #define _(N,n) \ |
| 646 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 647 | vl_api_##n##_t_handler, \ |
| 648 | vl_noop_handler, \ |
| 649 | vl_api_##n##_t_endian, \ |
| 650 | vl_api_##n##_t_print, \ |
| 651 | sizeof(vl_api_##n##_t), 1); |
| 652 | foreach_vpe_api_msg; |
| 653 | #undef _ |
| 654 | |
| 655 | /* |
| 656 | * Set up the (msg_name, crc, message-id) table |
| 657 | */ |
| 658 | setup_message_id_table (am); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | VLIB_API_INIT_FUNCTION (l2_api_hookup); |
| 664 | |
| 665 | /* |
| 666 | * fd.io coding-style-patch-verification: ON |
| 667 | * |
| 668 | * Local Variables: |
| 669 | * eval: (c-set-style "gnu") |
| 670 | * End: |
| 671 | */ |