2 *------------------------------------------------------------------
3 * l2_api.c - layer 2 forwarding api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/l2/l2_input.h>
26 #include <vnet/l2/l2_fib.h>
27 #include <vnet/l2/l2_vtr.h>
29 #include <vnet/vnet_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <vnet/vnet_all_api_h.h>
45 #include <vlibapi/api_helper_macros.h>
47 #define foreach_vpe_api_msg \
48 _(L2_XCONNECT_DUMP, l2_xconnect_dump) \
49 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
50 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
51 _(L2FIB_FLUSH_INT, l2fib_flush_int) \
52 _(L2FIB_FLUSH_BD, l2fib_flush_bd) \
53 _(L2FIB_ADD_DEL, l2fib_add_del) \
54 _(L2_FLAGS, l2_flags) \
55 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
56 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
57 _(BRIDGE_FLAGS, bridge_flags) \
58 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
59 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
60 _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)
63 send_l2_xconnect_details (unix_shared_memory_queue_t * q, u32 context,
64 u32 rx_sw_if_index, u32 tx_sw_if_index)
66 vl_api_l2_xconnect_details_t *mp;
68 mp = vl_msg_api_alloc (sizeof (*mp));
69 memset (mp, 0, sizeof (*mp));
70 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
71 mp->context = context;
72 mp->rx_sw_if_index = htonl (rx_sw_if_index);
73 mp->tx_sw_if_index = htonl (tx_sw_if_index);
75 vl_msg_api_send_shmem (q, (u8 *) & mp);
79 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
81 unix_shared_memory_queue_t *q;
82 vnet_main_t *vnm = vnet_get_main ();
83 vnet_interface_main_t *im = &vnm->interface_main;
84 l2input_main_t *l2im = &l2input_main;
85 vnet_sw_interface_t *swif;
86 l2_input_config_t *config;
88 q = vl_api_client_index_to_input_queue (mp->client_index);
93 pool_foreach (swif, im->sw_interfaces,
95 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
97 send_l2_xconnect_details (q, mp->context, swif->sw_if_index,
98 config->output_sw_if_index);
104 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
107 vl_api_l2_fib_clear_table_reply_t *rmp;
109 /* DAW-FIXME: This API should only clear non-static l2fib entries, but
110 * that is not currently implemented. When that TODO is fixed
111 * this call should be changed to pass 1 instead of 0.
113 l2fib_clear_table (0);
115 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
119 send_l2fib_table_entry (vpe_api_main_t * am,
120 unix_shared_memory_queue_t * q,
121 l2fib_entry_key_t * l2fe_key,
122 l2fib_entry_result_t * l2fe_res, u32 context)
124 vl_api_l2_fib_table_entry_t *mp;
126 mp = vl_msg_api_alloc (sizeof (*mp));
127 memset (mp, 0, sizeof (*mp));
128 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
131 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
133 mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
134 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
135 mp->static_mac = l2fe_res->fields.static_mac;
136 mp->filter_mac = l2fe_res->fields.filter;
137 mp->bvi_mac = l2fe_res->fields.bvi;
138 mp->context = context;
140 vl_msg_api_send_shmem (q, (u8 *) & mp);
144 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
146 vpe_api_main_t *am = &vpe_api_main;
147 bd_main_t *bdm = &bd_main;
148 l2fib_entry_key_t *l2fe_key = NULL;
149 l2fib_entry_result_t *l2fe_res = NULL;
150 u32 ni, bd_id = ntohl (mp->bd_id);
152 unix_shared_memory_queue_t *q;
155 q = vl_api_client_index_to_input_queue (mp->client_index);
159 /* see l2fib_table_dump: ~0 means "any" */
164 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
171 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
173 vec_foreach_index (ni, l2fe_key)
175 send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
176 vec_elt_at_index (l2fe_res, ni), mp->context);
183 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
185 bd_main_t *bdm = &bd_main;
186 l2input_main_t *l2im = &l2input_main;
187 vl_api_l2fib_add_del_reply_t *rmp;
190 u32 sw_if_index = ntohl (mp->sw_if_index);
191 u32 bd_id = ntohl (mp->bd_id);
200 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
203 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
204 goto bad_sw_if_index;
210 filter_mac = mp->filter_mac ? 1 : 0;
213 VALIDATE_SW_IF_INDEX (mp);
214 if (vec_len (l2im->configs) <= sw_if_index)
216 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
217 goto bad_sw_if_index;
221 l2_input_config_t *config;
222 config = vec_elt_at_index (l2im->configs, sw_if_index);
223 if (config->bridge == 0)
225 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
226 goto bad_sw_if_index;
230 static_mac = mp->static_mac ? 1 : 0;
231 bvi_mac = mp->bvi_mac ? 1 : 0;
232 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
237 l2fib_del_entry (mac, bd_index);
240 BAD_SW_IF_INDEX_LABEL;
242 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
246 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
249 vlib_main_t *vm = vlib_get_main ();
250 vl_api_l2fib_flush_int_reply_t *rmp;
252 VALIDATE_SW_IF_INDEX (mp);
254 u32 sw_if_index = ntohl (mp->sw_if_index);
255 l2fib_flush_int_mac (vm, sw_if_index);
257 BAD_SW_IF_INDEX_LABEL;
258 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
262 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
265 vlib_main_t *vm = vlib_get_main ();
266 bd_main_t *bdm = &bd_main;
267 vl_api_l2fib_flush_bd_reply_t *rmp;
269 u32 bd_id = ntohl (mp->bd_id);
270 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
273 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
276 l2fib_flush_bd_mac (vm, *p);
278 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
282 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
284 vl_api_l2_flags_reply_t *rmp;
288 VALIDATE_SW_IF_INDEX (mp);
290 u32 sw_if_index = ntohl (mp->sw_if_index);
291 u32 flags = ntohl (mp->feature_bitmap) & L2INPUT_VALID_MASK;
292 rbm = l2input_intf_bitmap_enable (sw_if_index, flags, mp->is_set);
294 BAD_SW_IF_INDEX_LABEL;
297 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
299 rmp->resulting_feature_bitmap = ntohl(rbm);
305 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
308 vlib_main_t *vm = vlib_get_main ();
309 bd_main_t *bdm = &bd_main;
310 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
312 u32 bd_id = ntohl (mp->bd_id);
313 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
316 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
319 bd_set_mac_age (vm, *p, mp->mac_age);
321 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
325 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
327 l2_bridge_domain_add_del_args_t a = {
328 .is_add = mp->is_add,
330 .uu_flood = mp->uu_flood,
331 .forward = mp->forward,
333 .arp_term = mp->arp_term,
334 .mac_age = mp->mac_age,
335 .bd_id = ntohl (mp->bd_id),
338 int rv = bd_add_del (&a);
340 vl_api_bridge_domain_add_del_reply_t *rmp;
341 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
345 send_bridge_domain_details (unix_shared_memory_queue_t * q,
346 l2_bridge_domain_t * bd_config,
347 u32 n_sw_ifs, u32 context)
349 vl_api_bridge_domain_details_t *mp;
351 mp = vl_msg_api_alloc (sizeof (*mp));
352 memset (mp, 0, sizeof (*mp));
353 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
354 mp->bd_id = ntohl (bd_config->bd_id);
355 mp->flood = bd_feature_flood (bd_config);
356 mp->uu_flood = bd_feature_uu_flood (bd_config);
357 mp->forward = bd_feature_forward (bd_config);
358 mp->learn = bd_feature_learn (bd_config);
359 mp->arp_term = bd_feature_arp_term (bd_config);
360 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
361 mp->mac_age = bd_config->mac_age;
362 mp->n_sw_ifs = ntohl (n_sw_ifs);
363 mp->context = context;
365 vl_msg_api_send_shmem (q, (u8 *) & mp);
369 send_bd_sw_if_details (l2input_main_t * l2im,
370 unix_shared_memory_queue_t * q,
371 l2_flood_member_t * member, u32 bd_id, u32 context)
373 vl_api_bridge_domain_sw_if_details_t *mp;
374 l2_input_config_t *input_cfg;
376 mp = vl_msg_api_alloc (sizeof (*mp));
377 memset (mp, 0, sizeof (*mp));
378 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
379 mp->bd_id = ntohl (bd_id);
380 mp->sw_if_index = ntohl (member->sw_if_index);
381 input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
382 mp->shg = input_cfg->shg;
383 mp->context = context;
385 vl_msg_api_send_shmem (q, (u8 *) & mp);
389 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
391 bd_main_t *bdm = &bd_main;
392 l2input_main_t *l2im = &l2input_main;
393 unix_shared_memory_queue_t *q;
394 l2_bridge_domain_t *bd_config;
398 q = vl_api_client_index_to_input_queue (mp->client_index);
403 bd_id = ntohl (mp->bd_id);
405 bd_index = (bd_id == ~0) ? 0 : bd_find_index (bdm, bd_id);
406 ASSERT (bd_index != ~0);
407 end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
408 for (; bd_index < end; bd_index++)
410 bd_config = l2input_bd_config_from_index (l2im, bd_index);
411 /* skip dummy bd_id 0 */
412 if (bd_config && (bd_config->bd_id > 0))
415 l2_flood_member_t *m;
417 n_sw_ifs = vec_len (bd_config->members);
418 send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
420 vec_foreach (m, bd_config->members)
422 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
429 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
431 vlib_main_t *vm = vlib_get_main ();
432 bd_main_t *bdm = &bd_main;
433 vl_api_bridge_flags_reply_t *rmp;
435 u32 bd_id = ntohl (mp->bd_id);
437 u32 flags = ntohl (mp->feature_bitmap);
440 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
443 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
449 bd_set_flags (vm, bd_index, flags, mp->is_set);
453 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
455 rmp->resulting_feature_bitmap = ntohl(flags);
461 vl_api_l2_interface_vlan_tag_rewrite_t_handler
462 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
465 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
466 vnet_main_t *vnm = vnet_get_main ();
467 vlib_main_t *vm = vlib_get_main ();
470 VALIDATE_SW_IF_INDEX (mp);
472 vtr_op = ntohl (mp->vtr_op);
474 /* The L2 code is unsuspicious */
477 case L2_VTR_DISABLED:
482 case L2_VTR_TRANSLATE_1_1:
483 case L2_VTR_TRANSLATE_1_2:
484 case L2_VTR_TRANSLATE_2_1:
485 case L2_VTR_TRANSLATE_2_2:
489 rv = VNET_API_ERROR_INVALID_VALUE;
490 goto bad_sw_if_index;
493 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
494 ntohl (mp->push_dot1q), ntohl (mp->tag1),
497 BAD_SW_IF_INDEX_LABEL;
499 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
503 vl_api_l2_interface_pbb_tag_rewrite_t_handler
504 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
506 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
507 vnet_main_t *vnm = vnet_get_main ();
508 vlib_main_t *vm = vlib_get_main ();
512 VALIDATE_SW_IF_INDEX (mp);
514 vtr_op = ntohl (mp->vtr_op);
518 case L2_VTR_DISABLED:
521 case L2_VTR_TRANSLATE_2_1:
525 rv = VNET_API_ERROR_INVALID_VALUE;
526 goto bad_sw_if_index;
529 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
530 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
531 ntohl (mp->i_sid), ntohs (mp->outer_tag));
533 BAD_SW_IF_INDEX_LABEL;
535 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
540 * Add vpe's API message handlers to the table.
541 * vlib has alread mapped shared memory and
542 * added the client registration handlers.
543 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
545 #define vl_msg_name_crc_list
546 #include <vnet/vnet_all_api_h.h>
547 #undef vl_msg_name_crc_list
550 setup_message_id_table (api_main_t * am)
552 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
553 foreach_vl_msg_name_crc_l2;
557 static clib_error_t *
558 l2_api_hookup (vlib_main_t * vm)
560 api_main_t *am = &api_main;
563 vl_msg_api_set_handlers(VL_API_##N, #n, \
564 vl_api_##n##_t_handler, \
566 vl_api_##n##_t_endian, \
567 vl_api_##n##_t_print, \
568 sizeof(vl_api_##n##_t), 1);
573 * Set up the (msg_name, crc, message-id) table
575 setup_message_id_table (am);
580 VLIB_API_INIT_FUNCTION (l2_api_hookup);
583 * fd.io coding-style-patch-verification: ON
586 * eval: (c-set-style "gnu")