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>
28 #include <vnet/l2/l2_learn.h>
30 #include <vnet/vnet_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 #define vl_api_bridge_domain_details_t_endian vl_noop_handler
41 #define vl_api_bridge_domain_details_t_print vl_noop_handler
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <vnet/vnet_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
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) \
55 _(L2FIB_FLUSH_ALL, l2fib_flush_all) \
56 _(L2FIB_FLUSH_INT, l2fib_flush_int) \
57 _(L2FIB_FLUSH_BD, l2fib_flush_bd) \
58 _(L2FIB_ADD_DEL, l2fib_add_del) \
59 _(WANT_L2_MACS_EVENTS, want_l2_macs_events) \
60 _(L2_FLAGS, l2_flags) \
61 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
62 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
63 _(BRIDGE_FLAGS, bridge_flags) \
64 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
65 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
66 _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)
69 send_l2_xconnect_details (unix_shared_memory_queue_t * q, u32 context,
70 u32 rx_sw_if_index, u32 tx_sw_if_index)
72 vl_api_l2_xconnect_details_t *mp;
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);
81 vl_msg_api_send_shmem (q, (u8 *) & mp);
85 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
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;
94 q = vl_api_client_index_to_input_queue (mp->client_index);
99 pool_foreach (swif, im->sw_interfaces,
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);
110 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
113 vl_api_l2_fib_clear_table_reply_t *rmp;
115 /* Clear all MACs including static MACs */
116 l2fib_clear_table ();
118 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
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)
127 vl_api_l2_fib_table_details_t *mp;
129 mp = vl_msg_api_alloc (sizeof (*mp));
130 memset (mp, 0, sizeof (*mp));
131 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
134 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
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;
143 vl_msg_api_send_shmem (q, (u8 *) & mp);
147 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
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);
155 unix_shared_memory_queue_t *q;
158 q = vl_api_client_index_to_input_queue (mp->client_index);
162 /* see l2fib_table_dump: ~0 means "any" */
167 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
174 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
176 vec_foreach_index (ni, l2fe_key)
178 send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
179 vec_elt_at_index (l2fe_res, ni), mp->context);
186 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
188 bd_main_t *bdm = &bd_main;
189 l2input_main_t *l2im = &l2input_main;
190 vl_api_l2fib_add_del_reply_t *rmp;
192 u32 bd_id = ntohl (mp->bd_id);
193 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
197 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
198 goto bad_sw_if_index;
206 l2fib_add_filter_entry (mac, bd_index);
209 u32 sw_if_index = ntohl (mp->sw_if_index);
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;
226 u8 static_mac = mp->static_mac ? 1 : 0;
227 u8 bvi_mac = mp->bvi_mac ? 1 : 0;
228 l2fib_add_fwd_entry (mac, bd_index, sw_if_index, static_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_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
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);
252 if (mp->enable_disable)
254 if (lm->client_pid == 0)
256 lm->client_pid = pid;
257 lm->client_index = mp->client_index;
259 if (mp->max_macs_in_event)
260 fm->max_macs_in_event = mp->max_macs_in_event * 10;
262 fm->max_macs_in_event = L2FIB_EVENT_MAX_MACS_DEFAULT;
265 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
267 fm->event_scan_delay = L2FIB_EVENT_SCAN_DELAY_DEFAULT;
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;
273 lm->global_learn_limit = L2FIB_EVENT_LEARN_LIMIT_DEFAULT;
275 l2fib_flush_all_mac (vlib_get_main ());
277 else if (lm->client_pid != pid)
279 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
283 else if (lm->client_pid)
286 lm->client_index = 0;
287 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
291 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
295 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
298 vlib_main_t *vm = vlib_get_main ();
299 vl_api_l2fib_flush_int_reply_t *rmp;
301 VALIDATE_SW_IF_INDEX (mp);
303 u32 sw_if_index = ntohl (mp->sw_if_index);
304 l2fib_flush_int_mac (vm, sw_if_index);
306 BAD_SW_IF_INDEX_LABEL;
307 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
311 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
314 vl_api_l2fib_flush_all_reply_t *rmp;
316 l2fib_flush_all_mac (vlib_get_main ());
317 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
321 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
324 vlib_main_t *vm = vlib_get_main ();
325 bd_main_t *bdm = &bd_main;
326 vl_api_l2fib_flush_bd_reply_t *rmp;
328 u32 bd_id = ntohl (mp->bd_id);
329 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
332 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
335 l2fib_flush_bd_mac (vm, *p);
337 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
341 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
343 vl_api_l2_flags_reply_t *rmp;
347 VALIDATE_SW_IF_INDEX (mp);
349 u32 sw_if_index = ntohl (mp->sw_if_index);
350 u32 flags = ntohl (mp->feature_bitmap);
353 if (flags & L2_LEARN)
354 bitmap |= L2INPUT_FEAT_LEARN;
357 bitmap |= L2INPUT_FEAT_FWD;
359 if (flags & L2_FLOOD)
360 bitmap |= L2INPUT_FEAT_FLOOD;
362 if (flags & L2_UU_FLOOD)
363 bitmap |= L2INPUT_FEAT_UU_FLOOD;
365 if (flags & L2_ARP_TERM)
366 bitmap |= L2INPUT_FEAT_ARP_TERM;
368 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
370 BAD_SW_IF_INDEX_LABEL;
373 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
375 rmp->resulting_feature_bitmap = ntohl(rbm);
381 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
384 vlib_main_t *vm = vlib_get_main ();
385 bd_main_t *bdm = &bd_main;
386 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
388 u32 bd_id = ntohl (mp->bd_id);
393 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
397 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
400 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
403 bd_set_mac_age (vm, *p, mp->mac_age);
405 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
409 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
411 l2_bridge_domain_add_del_args_t a = {
412 .is_add = mp->is_add,
414 .uu_flood = mp->uu_flood,
415 .forward = mp->forward,
417 .arp_term = mp->arp_term,
418 .mac_age = mp->mac_age,
419 .bd_id = ntohl (mp->bd_id),
422 int rv = bd_add_del (&a);
424 vl_api_bridge_domain_add_del_reply_t *rmp;
425 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
429 send_bridge_domain_details (l2input_main_t * l2im,
430 unix_shared_memory_queue_t * q,
431 l2_bridge_domain_t * bd_config,
432 u32 n_sw_ifs, u32 context)
434 vl_api_bridge_domain_details_t *mp;
435 l2_flood_member_t *m;
436 vl_api_bridge_domain_sw_if_t *sw_ifs;
437 l2_input_config_t *input_cfg;
439 mp = vl_msg_api_alloc (sizeof (*mp) +
440 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
441 memset (mp, 0, sizeof (*mp));
442 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
443 mp->bd_id = ntohl (bd_config->bd_id);
444 mp->flood = bd_feature_flood (bd_config);
445 mp->uu_flood = bd_feature_uu_flood (bd_config);
446 mp->forward = bd_feature_forward (bd_config);
447 mp->learn = bd_feature_learn (bd_config);
448 mp->arp_term = bd_feature_arp_term (bd_config);
449 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
450 mp->mac_age = bd_config->mac_age;
451 mp->context = context;
453 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
454 vec_foreach (m, bd_config->members)
456 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
457 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
458 sw_ifs->shg = input_cfg->shg;
462 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
464 vl_msg_api_send_shmem (q, (u8 *) & mp);
468 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
470 bd_main_t *bdm = &bd_main;
471 l2input_main_t *l2im = &l2input_main;
473 unix_shared_memory_queue_t *q =
474 vl_api_client_index_to_input_queue (mp->client_index);
478 u32 bd_id = ntohl (mp->bd_id);
484 bd_index = 0, end = vec_len (l2im->bd_configs);
487 bd_index = bd_find_index (bdm, bd_id);
494 for (; bd_index < end; bd_index++)
496 l2_bridge_domain_t *bd_config =
497 l2input_bd_config_from_index (l2im, bd_index);
498 /* skip dummy bd_id 0 */
499 if (bd_config && (bd_config->bd_id > 0))
500 send_bridge_domain_details (l2im, q, bd_config,
501 vec_len (bd_config->members),
507 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
509 vlib_main_t *vm = vlib_get_main ();
510 bd_main_t *bdm = &bd_main;
511 vl_api_bridge_flags_reply_t *rmp;
514 u32 flags = ntohl (mp->feature_bitmap);
515 u32 bd_id = ntohl (mp->bd_id);
518 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
522 u32 bd_index = bd_find_index (bdm, bd_id);
525 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
529 u32 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
533 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
535 rmp->resulting_feature_bitmap = ntohl(bitmap);
541 vl_api_l2_interface_vlan_tag_rewrite_t_handler
542 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
545 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
546 vnet_main_t *vnm = vnet_get_main ();
547 vlib_main_t *vm = vlib_get_main ();
550 VALIDATE_SW_IF_INDEX (mp);
552 vtr_op = ntohl (mp->vtr_op);
554 /* The L2 code is unsuspicious */
557 case L2_VTR_DISABLED:
562 case L2_VTR_TRANSLATE_1_1:
563 case L2_VTR_TRANSLATE_1_2:
564 case L2_VTR_TRANSLATE_2_1:
565 case L2_VTR_TRANSLATE_2_2:
569 rv = VNET_API_ERROR_INVALID_VALUE;
570 goto bad_sw_if_index;
573 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
574 ntohl (mp->push_dot1q), ntohl (mp->tag1),
577 BAD_SW_IF_INDEX_LABEL;
579 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
583 vl_api_l2_interface_pbb_tag_rewrite_t_handler
584 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
586 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
587 vnet_main_t *vnm = vnet_get_main ();
588 vlib_main_t *vm = vlib_get_main ();
592 VALIDATE_SW_IF_INDEX (mp);
594 vtr_op = ntohl (mp->vtr_op);
598 case L2_VTR_DISABLED:
601 case L2_VTR_TRANSLATE_2_1:
605 rv = VNET_API_ERROR_INVALID_VALUE;
606 goto bad_sw_if_index;
609 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
610 mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
611 ntohl (mp->i_sid), ntohs (mp->outer_tag));
613 BAD_SW_IF_INDEX_LABEL;
615 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
620 * Add vpe's API message handlers to the table.
621 * vlib has alread mapped shared memory and
622 * added the client registration handlers.
623 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
625 #define vl_msg_name_crc_list
626 #include <vnet/vnet_all_api_h.h>
627 #undef vl_msg_name_crc_list
630 setup_message_id_table (api_main_t * am)
632 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
633 foreach_vl_msg_name_crc_l2;
637 static clib_error_t *
638 l2_api_hookup (vlib_main_t * vm)
640 api_main_t *am = &api_main;
643 vl_msg_api_set_handlers(VL_API_##N, #n, \
644 vl_api_##n##_t_handler, \
646 vl_api_##n##_t_endian, \
647 vl_api_##n##_t_print, \
648 sizeof(vl_api_##n##_t), 1);
653 * Set up the (msg_name, crc, message-id) table
655 setup_message_id_table (am);
660 VLIB_API_INIT_FUNCTION (l2_api_hookup);
663 * fd.io coding-style-patch-verification: ON
666 * eval: (c-set-style "gnu")