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_ADD_DEL, l2fib_add_del) \
52 _(L2_FLAGS, l2_flags) \
53 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
54 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
55 _(BRIDGE_FLAGS, bridge_flags) \
56 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
57 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)
60 send_l2_xconnect_details (unix_shared_memory_queue_t * q, u32 context,
61 u32 rx_sw_if_index, u32 tx_sw_if_index)
63 vl_api_l2_xconnect_details_t *mp;
65 mp = vl_msg_api_alloc (sizeof (*mp));
66 memset (mp, 0, sizeof (*mp));
67 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
68 mp->context = context;
69 mp->rx_sw_if_index = htonl (rx_sw_if_index);
70 mp->tx_sw_if_index = htonl (tx_sw_if_index);
72 vl_msg_api_send_shmem (q, (u8 *) & mp);
76 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
78 unix_shared_memory_queue_t *q;
79 vnet_main_t *vnm = vnet_get_main ();
80 vnet_interface_main_t *im = &vnm->interface_main;
81 l2input_main_t *l2im = &l2input_main;
82 vnet_sw_interface_t *swif;
83 l2_input_config_t *config;
85 q = vl_api_client_index_to_input_queue (mp->client_index);
90 pool_foreach (swif, im->sw_interfaces,
92 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
94 send_l2_xconnect_details (q, mp->context, swif->sw_if_index,
95 config->output_sw_if_index);
101 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
104 vl_api_l2_fib_clear_table_reply_t *rmp;
106 /* DAW-FIXME: This API should only clear non-static l2fib entries, but
107 * that is not currently implemented. When that TODO is fixed
108 * this call should be changed to pass 1 instead of 0.
110 l2fib_clear_table (0);
112 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
116 send_l2fib_table_entry (vpe_api_main_t * am,
117 unix_shared_memory_queue_t * q,
118 l2fib_entry_key_t * l2fe_key,
119 l2fib_entry_result_t * l2fe_res, u32 context)
121 vl_api_l2_fib_table_entry_t *mp;
123 mp = vl_msg_api_alloc (sizeof (*mp));
124 memset (mp, 0, sizeof (*mp));
125 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
128 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
130 mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
131 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
132 mp->static_mac = l2fe_res->fields.static_mac;
133 mp->filter_mac = l2fe_res->fields.filter;
134 mp->bvi_mac = l2fe_res->fields.bvi;
135 mp->context = context;
137 vl_msg_api_send_shmem (q, (u8 *) & mp);
141 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
143 vpe_api_main_t *am = &vpe_api_main;
144 bd_main_t *bdm = &bd_main;
145 l2fib_entry_key_t *l2fe_key = NULL;
146 l2fib_entry_result_t *l2fe_res = NULL;
147 u32 ni, bd_id = ntohl (mp->bd_id);
149 unix_shared_memory_queue_t *q;
152 q = vl_api_client_index_to_input_queue (mp->client_index);
156 /* see l2fib_table_dump: ~0 means "any" */
161 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
168 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
170 vec_foreach_index (ni, l2fe_key)
172 send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
173 vec_elt_at_index (l2fe_res, ni), mp->context);
180 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
182 bd_main_t *bdm = &bd_main;
183 l2input_main_t *l2im = &l2input_main;
184 vl_api_l2fib_add_del_reply_t *rmp;
187 u32 sw_if_index = ntohl (mp->sw_if_index);
188 u32 bd_id = ntohl (mp->bd_id);
197 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
200 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
201 goto bad_sw_if_index;
207 filter_mac = mp->filter_mac ? 1 : 0;
210 VALIDATE_SW_IF_INDEX (mp);
211 if (vec_len (l2im->configs) <= sw_if_index)
213 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
214 goto bad_sw_if_index;
218 l2_input_config_t *config;
219 config = vec_elt_at_index (l2im->configs, sw_if_index);
220 if (config->bridge == 0)
222 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
223 goto bad_sw_if_index;
227 static_mac = mp->static_mac ? 1 : 0;
228 bvi_mac = mp->bvi_mac ? 1 : 0;
229 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
234 l2fib_del_entry (mac, bd_index);
237 BAD_SW_IF_INDEX_LABEL;
239 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
243 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
245 vl_api_l2_flags_reply_t *rmp;
247 u32 sw_if_index = ntohl (mp->sw_if_index);
248 u32 flags = ntohl (mp->feature_bitmap);
251 VALIDATE_SW_IF_INDEX (mp);
254 if (flags & L2INPUT_FEAT_ ## a) \
255 rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
256 foreach_l2input_feat;
259 BAD_SW_IF_INDEX_LABEL;
262 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
264 rmp->resulting_feature_bitmap = ntohl(rbm);
270 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
272 vlib_main_t *vm = vlib_get_main ();
273 bd_main_t *bdm = &bd_main;
274 vl_api_bridge_domain_add_del_reply_t *rmp;
276 u32 enable_flags = 0, disable_flags = 0;
277 u32 bd_id = ntohl (mp->bd_id);
282 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
285 enable_flags |= L2_FLOOD;
287 disable_flags |= L2_FLOOD;
290 enable_flags |= L2_UU_FLOOD;
292 disable_flags |= L2_UU_FLOOD;
295 enable_flags |= L2_FWD;
297 disable_flags |= L2_FWD;
300 enable_flags |= L2_ARP_TERM;
302 disable_flags |= L2_ARP_TERM;
305 enable_flags |= L2_LEARN;
307 disable_flags |= L2_LEARN;
310 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
313 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
315 bd_set_mac_age (vm, bd_index, mp->mac_age);
318 rv = bd_delete_bd_index (bdm, bd_id);
320 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
324 send_bridge_domain_details (unix_shared_memory_queue_t * q,
325 l2_bridge_domain_t * bd_config,
326 u32 n_sw_ifs, u32 context)
328 vl_api_bridge_domain_details_t *mp;
330 mp = vl_msg_api_alloc (sizeof (*mp));
331 memset (mp, 0, sizeof (*mp));
332 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
333 mp->bd_id = ntohl (bd_config->bd_id);
334 mp->flood = bd_feature_flood (bd_config);
335 mp->uu_flood = bd_feature_uu_flood (bd_config);
336 mp->forward = bd_feature_forward (bd_config);
337 mp->learn = bd_feature_learn (bd_config);
338 mp->arp_term = bd_feature_arp_term (bd_config);
339 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
340 mp->mac_age = bd_config->mac_age;
341 mp->n_sw_ifs = ntohl (n_sw_ifs);
342 mp->context = context;
344 vl_msg_api_send_shmem (q, (u8 *) & mp);
348 send_bd_sw_if_details (l2input_main_t * l2im,
349 unix_shared_memory_queue_t * q,
350 l2_flood_member_t * member, u32 bd_id, u32 context)
352 vl_api_bridge_domain_sw_if_details_t *mp;
353 l2_input_config_t *input_cfg;
355 mp = vl_msg_api_alloc (sizeof (*mp));
356 memset (mp, 0, sizeof (*mp));
357 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
358 mp->bd_id = ntohl (bd_id);
359 mp->sw_if_index = ntohl (member->sw_if_index);
360 input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
361 mp->shg = input_cfg->shg;
362 mp->context = context;
364 vl_msg_api_send_shmem (q, (u8 *) & mp);
368 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
370 bd_main_t *bdm = &bd_main;
371 l2input_main_t *l2im = &l2input_main;
372 unix_shared_memory_queue_t *q;
373 l2_bridge_domain_t *bd_config;
377 q = vl_api_client_index_to_input_queue (mp->client_index);
382 bd_id = ntohl (mp->bd_id);
384 bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
385 end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
386 for (; bd_index < end; bd_index++)
388 bd_config = l2input_bd_config_from_index (l2im, bd_index);
389 /* skip dummy bd_id 0 */
390 if (bd_config && (bd_config->bd_id > 0))
393 l2_flood_member_t *m;
395 n_sw_ifs = vec_len (bd_config->members);
396 send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
398 vec_foreach (m, bd_config->members)
400 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
407 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
409 vlib_main_t *vm = vlib_get_main ();
410 bd_main_t *bdm = &bd_main;
411 vl_api_bridge_flags_reply_t *rmp;
413 u32 bd_id = ntohl (mp->bd_id);
415 u32 flags = ntohl (mp->feature_bitmap);
418 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
421 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
427 bd_set_flags (vm, bd_index, flags, mp->is_set);
431 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
433 rmp->resulting_feature_bitmap = ntohl(flags);
439 vl_api_l2_interface_vlan_tag_rewrite_t_handler
440 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
443 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
444 vnet_main_t *vnm = vnet_get_main ();
445 vlib_main_t *vm = vlib_get_main ();
448 VALIDATE_SW_IF_INDEX (mp);
450 vtr_op = ntohl (mp->vtr_op);
452 /* The L2 code is unsuspicious */
455 case L2_VTR_DISABLED:
460 case L2_VTR_TRANSLATE_1_1:
461 case L2_VTR_TRANSLATE_1_2:
462 case L2_VTR_TRANSLATE_2_1:
463 case L2_VTR_TRANSLATE_2_2:
467 rv = VNET_API_ERROR_INVALID_VALUE;
468 goto bad_sw_if_index;
471 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
472 ntohl (mp->push_dot1q), ntohl (mp->tag1),
475 BAD_SW_IF_INDEX_LABEL;
477 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
481 vl_api_l2_interface_pbb_tag_rewrite_t_handler
482 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
484 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
485 vnet_main_t *vnm = vnet_get_main ();
486 vlib_main_t *vm = vlib_get_main ();
490 VALIDATE_SW_IF_INDEX (mp);
492 vtr_op = ntohl (mp->vtr_op);
496 case L2_VTR_DISABLED:
499 case L2_VTR_TRANSLATE_2_1:
503 rv = VNET_API_ERROR_INVALID_VALUE;
504 goto bad_sw_if_index;
507 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
508 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
509 ntohl (mp->i_sid), ntohs (mp->outer_tag));
511 BAD_SW_IF_INDEX_LABEL;
513 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
518 * Add vpe's API message handlers to the table.
519 * vlib has alread mapped shared memory and
520 * added the client registration handlers.
521 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
523 #define vl_msg_name_crc_list
524 #include <vnet/vnet_all_api_h.h>
525 #undef vl_msg_name_crc_list
528 setup_message_id_table (api_main_t * am)
530 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
531 foreach_vl_msg_name_crc_l2;
535 static clib_error_t *
536 l2_api_hookup (vlib_main_t * vm)
538 api_main_t *am = &api_main;
541 vl_msg_api_set_handlers(VL_API_##N, #n, \
542 vl_api_##n##_t_handler, \
544 vl_api_##n##_t_endian, \
545 vl_api_##n##_t_print, \
546 sizeof(vl_api_##n##_t), 1);
551 * Set up the (msg_name, crc, message-id) table
553 setup_message_id_table (am);
558 VLIB_API_INIT_FUNCTION (l2_api_hookup);
561 * fd.io coding-style-patch-verification: ON
564 * eval: (c-set-style "gnu")