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>
29 #include <vnet/l2/l2_bd.h>
30 #include <vnet/l2/l2_bvi.h>
31 #include <vnet/l2/l2_arp_term.h>
32 #include <vnet/ip/ip_types_api.h>
33 #include <vnet/ethernet/ethernet_types_api.h>
35 #include <vnet/format_fns.h>
36 #include <vnet/l2/l2.api_enum.h>
37 #include <vnet/l2/l2.api_types.h>
39 #define REPLY_MSG_ID_BASE l2input_main.msg_id_base
40 #include <vlibapi/api_helper_macros.h>
43 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
44 u32 rx_sw_if_index, u32 tx_sw_if_index)
46 vl_api_l2_xconnect_details_t *mp;
48 mp = vl_msg_api_alloc (sizeof (*mp));
49 clib_memset (mp, 0, sizeof (*mp));
50 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_XCONNECT_DETAILS);
51 mp->context = context;
52 mp->rx_sw_if_index = htonl (rx_sw_if_index);
53 mp->tx_sw_if_index = htonl (tx_sw_if_index);
55 vl_api_send_msg (reg, (u8 *) mp);
59 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
61 vl_api_registration_t *reg;
62 l2input_main_t *l2im = &l2input_main;
64 l2_input_config_t *config;
66 reg = vl_api_client_index_to_registration (mp->client_index);
71 vec_foreach_index (sw_if_index, l2im->configs)
73 config = vec_elt_at_index (l2im->configs, sw_if_index);
74 if (l2_input_is_xconnect (config))
75 send_l2_xconnect_details (reg, mp->context, sw_if_index,
76 config->output_sw_if_index);
82 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
85 vl_api_l2_fib_clear_table_reply_t *rmp;
87 /* Clear all MACs including static MACs */
90 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
94 send_l2fib_table_entry (vpe_api_main_t * am,
95 vl_api_registration_t * reg,
96 l2fib_entry_key_t * l2fe_key,
97 l2fib_entry_result_t * l2fe_res, u32 context)
99 vl_api_l2_fib_table_details_t *mp;
101 mp = vl_msg_api_alloc (sizeof (*mp));
102 clib_memset (mp, 0, sizeof (*mp));
103 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_FIB_TABLE_DETAILS);
106 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
108 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
109 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
110 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
111 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
112 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
113 mp->context = context;
115 vl_api_send_msg (reg, (u8 *) mp);
119 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
121 vpe_api_main_t *am = &vpe_api_main;
122 bd_main_t *bdm = &bd_main;
123 l2fib_entry_key_t *l2fe_key = NULL;
124 l2fib_entry_result_t *l2fe_res = NULL;
125 u32 ni, bd_id = ntohl (mp->bd_id);
127 vl_api_registration_t *reg;
130 reg = vl_api_client_index_to_registration (mp->client_index);
134 /* see l2fib_table_dump: ~0 means "any" */
139 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
146 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
148 vec_foreach_index (ni, l2fe_key)
150 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
151 vec_elt_at_index (l2fe_res, ni), mp->context);
158 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
160 bd_main_t *bdm = &bd_main;
161 l2input_main_t *l2im = &l2input_main;
162 vl_api_l2fib_add_del_reply_t *rmp;
164 u32 bd_id = ntohl (mp->bd_id);
165 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
169 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
170 goto bad_sw_if_index;
176 mac_address_decode (mp->mac, &mac);
180 l2fib_add_filter_entry (mac.bytes, bd_index);
183 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
184 u32 sw_if_index = ntohl (mp->sw_if_index);
185 VALIDATE_SW_IF_INDEX (mp);
186 if (vec_len (l2im->configs) <= sw_if_index)
188 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
189 goto bad_sw_if_index;
193 l2_input_config_t *config;
194 config = vec_elt_at_index (l2im->configs, sw_if_index);
195 if (!l2_input_is_bridge (config))
197 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
198 goto bad_sw_if_index;
202 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
204 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
205 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
210 u32 sw_if_index = ntohl (mp->sw_if_index);
211 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
212 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
215 BAD_SW_IF_INDEX_LABEL;
217 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
221 vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
224 vl_api_want_l2_macs_events2_reply_t *rmp;
225 l2learn_main_t *lm = &l2learn_main;
226 l2fib_main_t *fm = &l2fib_main;
227 u32 pid = ntohl (mp->pid);
229 if (mp->enable_disable)
231 if ((lm->client_pid == 0) || (lm->client_pid == pid))
233 if (mp->max_macs_in_event)
234 fm->max_macs_in_event = mp->max_macs_in_event * 10;
237 rv = VNET_API_ERROR_INVALID_VALUE;
241 /* if scan_delay was not set before */
242 if (fm->event_scan_delay == 0.0)
243 fm->event_scan_delay = (f64) (10) * 10e-3;
245 lm->client_pid = pid;
246 lm->client_index = mp->client_index;
247 l2fib_flush_all_mac (vlib_get_main ());
249 else if (lm->client_pid != pid)
251 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
255 else if (lm->client_pid)
258 lm->client_index = 0;
262 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
266 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
269 vl_api_want_l2_macs_events_reply_t *rmp;
270 l2learn_main_t *lm = &l2learn_main;
271 l2fib_main_t *fm = &l2fib_main;
272 u32 pid = ntohl (mp->pid);
273 u32 learn_limit = ntohl (mp->learn_limit);
275 if (mp->enable_disable)
277 if ((lm->client_pid == 0) || (lm->client_pid == pid))
279 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
280 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
282 rv = VNET_API_ERROR_INVALID_VALUE;
285 lm->client_pid = pid;
286 lm->client_index = mp->client_index;
288 fm->max_macs_in_event = mp->max_macs_in_event * 10;
289 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
291 /* change learn limit and flush all learned MACs */
292 lm->global_learn_limit = learn_limit;
293 l2fib_flush_all_mac (vlib_get_main ());
295 else if (lm->client_pid != pid)
297 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
301 else if (lm->client_pid)
304 lm->client_index = 0;
305 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
306 lm->global_learn_limit = learn_limit;
308 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
312 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
316 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
319 vlib_main_t *vm = vlib_get_main ();
320 vl_api_l2fib_flush_int_reply_t *rmp;
322 VALIDATE_SW_IF_INDEX (mp);
324 u32 sw_if_index = ntohl (mp->sw_if_index);
325 l2fib_flush_int_mac (vm, sw_if_index);
327 BAD_SW_IF_INDEX_LABEL;
328 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
332 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
335 vl_api_l2fib_flush_all_reply_t *rmp;
337 l2fib_flush_all_mac (vlib_get_main ());
338 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
342 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
345 vlib_main_t *vm = vlib_get_main ();
346 bd_main_t *bdm = &bd_main;
347 vl_api_l2fib_flush_bd_reply_t *rmp;
349 u32 bd_id = ntohl (mp->bd_id);
350 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
353 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
356 l2fib_flush_bd_mac (vm, *p);
358 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
362 vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
365 l2fib_main_t *fm = &l2fib_main;
366 vl_api_l2fib_set_scan_delay_reply_t *rmp;
367 u16 scan_delay = ntohs (mp->scan_delay);
371 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
372 l2fib_flush_all_mac (vlib_get_main ());
376 rv = VNET_API_ERROR_INVALID_VALUE;
381 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
385 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
387 vl_api_l2_flags_reply_t *rmp;
391 VALIDATE_SW_IF_INDEX (mp);
393 u32 sw_if_index = ntohl (mp->sw_if_index);
394 u32 flags = ntohl (mp->feature_bitmap);
397 if (flags & L2_LEARN)
398 bitmap |= L2INPUT_FEAT_LEARN;
401 bitmap |= L2INPUT_FEAT_FWD;
403 if (flags & L2_FLOOD)
404 bitmap |= L2INPUT_FEAT_FLOOD;
406 if (flags & L2_UU_FLOOD)
407 bitmap |= L2INPUT_FEAT_UU_FLOOD;
409 if (flags & L2_ARP_TERM)
410 bitmap |= L2INPUT_FEAT_ARP_TERM;
412 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
414 BAD_SW_IF_INDEX_LABEL;
417 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
419 rmp->resulting_feature_bitmap = ntohl(rbm);
425 vl_api_bridge_domain_set_default_learn_limit_t_handler (
426 vl_api_bridge_domain_set_default_learn_limit_t *mp)
428 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
431 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
432 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
436 vl_api_bridge_domain_set_learn_limit_t_handler (
437 vl_api_bridge_domain_set_learn_limit_t *mp)
439 vlib_main_t *vm = vlib_get_main ();
440 bd_main_t *bdm = &bd_main;
441 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
443 u32 bd_id = ntohl (mp->bd_id);
448 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
452 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
455 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
458 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
460 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
464 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
467 vlib_main_t *vm = vlib_get_main ();
468 bd_main_t *bdm = &bd_main;
469 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
471 u32 bd_id = ntohl (mp->bd_id);
476 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
480 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
483 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
486 bd_set_mac_age (vm, *p, mp->mac_age);
488 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
492 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
494 l2_bridge_domain_add_del_args_t a = {
495 .is_add = mp->is_add,
497 .uu_flood = mp->uu_flood,
498 .forward = mp->forward,
500 .arp_term = mp->arp_term,
501 .arp_ufwd = mp->arp_ufwd,
502 .mac_age = mp->mac_age,
503 .bd_id = ntohl (mp->bd_id),
507 int rv = bd_add_del (&a);
509 vl_api_bridge_domain_add_del_reply_t *rmp;
510 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
514 send_bridge_domain_details (l2input_main_t * l2im,
515 vl_api_registration_t * reg,
516 l2_bridge_domain_t * bd_config,
517 u32 n_sw_ifs, u32 context)
519 vl_api_bridge_domain_details_t *mp;
520 l2_flood_member_t *m;
521 vl_api_bridge_domain_sw_if_t *sw_ifs;
522 l2_input_config_t *input_cfg;
524 mp = vl_msg_api_alloc (sizeof (*mp) +
525 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
526 clib_memset (mp, 0, sizeof (*mp));
527 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DETAILS);
528 mp->bd_id = ntohl (bd_config->bd_id);
529 mp->flood = bd_feature_flood (bd_config);
530 mp->uu_flood = bd_feature_uu_flood (bd_config);
531 mp->forward = bd_feature_forward (bd_config);
532 mp->learn = bd_feature_learn (bd_config);
533 mp->arp_term = bd_feature_arp_term (bd_config);
534 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
535 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
536 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
537 mp->mac_age = bd_config->mac_age;
538 if (bd_config->bd_tag)
540 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
541 ARRAY_LEN (mp->bd_tag) - 1);
542 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
545 mp->context = context;
547 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
548 vec_foreach (m, bd_config->members)
550 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
551 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
552 sw_ifs->shg = input_cfg->shg;
556 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
558 vl_api_send_msg (reg, (u8 *) mp);
562 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
564 bd_main_t *bdm = &bd_main;
565 l2input_main_t *l2im = &l2input_main;
566 vl_api_registration_t *reg;
567 u32 bd_id, bd_index, end, filter_sw_if_index;
569 reg = vl_api_client_index_to_registration (mp->client_index);
573 filter_sw_if_index = ntohl (mp->sw_if_index);
574 if (filter_sw_if_index != ~0)
575 return; /* UNIMPLEMENTED */
577 bd_id = ntohl (mp->bd_id);
582 bd_index = 0, end = vec_len (l2im->bd_configs);
585 bd_index = bd_find_index (bdm, bd_id);
592 for (; bd_index < end; bd_index++)
594 l2_bridge_domain_t *bd_config =
595 l2input_bd_config_from_index (l2im, bd_index);
596 /* skip placeholder bd_id 0 */
597 if (bd_config && (bd_config->bd_id > 0))
598 send_bridge_domain_details (l2im, reg, bd_config,
599 vec_len (bd_config->members),
605 bd_flags_decode (vl_api_bd_flags_t v)
607 bd_flags_t f = L2_NONE;
611 if (v & BRIDGE_API_FLAG_LEARN)
613 if (v & BRIDGE_API_FLAG_FWD)
615 if (v & BRIDGE_API_FLAG_FLOOD)
617 if (v & BRIDGE_API_FLAG_UU_FLOOD)
619 if (v & BRIDGE_API_FLAG_ARP_TERM)
621 if (v & BRIDGE_API_FLAG_ARP_UFWD)
628 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
630 vlib_main_t *vm = vlib_get_main ();
631 bd_main_t *bdm = &bd_main;
632 vl_api_bridge_flags_reply_t *rmp;
636 bd_flags_t flags = bd_flags_decode (mp->flags);
637 u32 bd_id = ntohl (mp->bd_id);
640 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
644 u32 bd_index = bd_find_index (bdm, bd_id);
647 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
651 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
655 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
657 rmp->resulting_feature_bitmap = ntohl(bitmap);
663 vl_api_l2_interface_vlan_tag_rewrite_t_handler
664 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
667 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
668 vnet_main_t *vnm = vnet_get_main ();
669 vlib_main_t *vm = vlib_get_main ();
672 VALIDATE_SW_IF_INDEX (mp);
674 vtr_op = ntohl (mp->vtr_op);
676 /* The L2 code is unsuspicious */
679 case L2_VTR_DISABLED:
684 case L2_VTR_TRANSLATE_1_1:
685 case L2_VTR_TRANSLATE_1_2:
686 case L2_VTR_TRANSLATE_2_1:
687 case L2_VTR_TRANSLATE_2_2:
691 rv = VNET_API_ERROR_INVALID_VALUE;
692 goto bad_sw_if_index;
695 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
696 ntohl (mp->push_dot1q), ntohl (mp->tag1),
699 BAD_SW_IF_INDEX_LABEL;
701 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
705 vl_api_l2_interface_pbb_tag_rewrite_t_handler
706 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
708 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
709 vnet_main_t *vnm = vnet_get_main ();
710 vlib_main_t *vm = vlib_get_main ();
713 mac_address_t b_dmac, b_smac;
715 VALIDATE_SW_IF_INDEX (mp);
717 vtr_op = ntohl (mp->vtr_op);
721 case L2_VTR_DISABLED:
724 case L2_VTR_TRANSLATE_2_1:
728 rv = VNET_API_ERROR_INVALID_VALUE;
729 goto bad_sw_if_index;
732 mac_address_decode (mp->b_dmac, &b_dmac);
733 mac_address_decode (mp->b_smac, &b_smac);
735 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
736 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
737 ntohl (mp->i_sid), ntohs (mp->outer_tag));
739 BAD_SW_IF_INDEX_LABEL;
741 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
745 vl_api_sw_interface_set_l2_xconnect_t_handler
746 (vl_api_sw_interface_set_l2_xconnect_t * mp)
748 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
750 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
751 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
752 vlib_main_t *vm = vlib_get_main ();
753 vnet_main_t *vnm = vnet_get_main ();
755 VALIDATE_RX_SW_IF_INDEX (mp);
759 VALIDATE_TX_SW_IF_INDEX (mp);
760 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
762 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
766 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
767 L2_BD_PORT_TYPE_NORMAL, 0, 0);
773 rv = VNET_API_ERROR_NON_ETHERNET;
775 case MODE_ERROR_BVI_DEF:
776 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
780 BAD_RX_SW_IF_INDEX_LABEL;
781 BAD_TX_SW_IF_INDEX_LABEL;
783 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
787 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
789 v = clib_net_to_host_u32 (v);
793 case L2_API_PORT_TYPE_NORMAL:
794 *l = L2_BD_PORT_TYPE_NORMAL;
796 case L2_API_PORT_TYPE_BVI:
797 *l = L2_BD_PORT_TYPE_BVI;
799 case L2_API_PORT_TYPE_UU_FWD:
800 *l = L2_BD_PORT_TYPE_UU_FWD;
804 return (VNET_API_ERROR_INVALID_VALUE);
808 vl_api_sw_interface_set_l2_bridge_t_handler
809 (vl_api_sw_interface_set_l2_bridge_t * mp)
811 bd_main_t *bdm = &bd_main;
812 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
814 vlib_main_t *vm = vlib_get_main ();
815 vnet_main_t *vnm = vnet_get_main ();
816 l2_bd_port_type_t pt;
818 VALIDATE_RX_SW_IF_INDEX (mp);
819 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
820 rv = l2_bd_port_type_decode (mp->port_type, &pt);
827 u32 bd_id = ntohl (mp->bd_id);
828 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
830 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
831 rx_sw_if_index, bd_index, pt, mp->shg, 0);
835 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
841 rv = VNET_API_ERROR_NON_ETHERNET;
843 case MODE_ERROR_BVI_DEF:
844 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
848 BAD_RX_SW_IF_INDEX_LABEL;
851 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
855 send_bd_ip_mac_entry (vpe_api_main_t * am,
856 vl_api_registration_t * reg,
858 const ip46_address_t * ip,
860 const mac_address_t * mac, u32 context)
862 vl_api_bd_ip_mac_details_t *mp;
864 mp = vl_msg_api_alloc (sizeof (*mp));
865 clib_memset (mp, 0, sizeof (*mp));
866 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BD_IP_MAC_DETAILS);
868 mp->context = context;
869 mp->entry.bd_id = ntohl (bd_id);
871 ip_address_encode (ip, itype, &mp->entry.ip);
872 mac_address_encode (mac, mp->entry.mac);
874 vl_api_send_msg (reg, (u8 *) mp);
878 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
880 vpe_api_main_t *am = &vpe_api_main;
881 bd_main_t *bdm = &bd_main;
882 l2_bridge_domain_t *bd_config;
883 u32 bd_id = ntohl (mp->bd_id);
884 u32 bd_index, start, end;
885 vl_api_registration_t *reg;
888 reg = vl_api_client_index_to_registration (mp->client_index);
892 /* see bd_id: ~0 means "any" */
896 end = vec_len (l2input_main.bd_configs);
900 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
905 vec_validate (l2input_main.bd_configs, bd_index);
910 for (bd_index = start; bd_index < end; bd_index++)
912 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
913 if (bd_is_valid (bd_config))
915 ip4_address_t ip4_addr;
916 ip6_address_t *ip6_addr;
919 bd_id = bd_config->bd_id;
922 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
924 ip46_address_t ip = {
927 mac_address_from_u64(&mac, mac64);
929 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
933 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
935 ip46_address_t ip = {
938 mac_address_from_u64(&mac, mac64);
940 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
949 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
951 ip46_address_t ip_addr = ip46_address_initializer;
952 vl_api_bd_ip_mac_add_del_reply_t *rmp;
953 bd_main_t *bdm = &bd_main;
960 bd_id = ntohl (mp->entry.bd_id);
964 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
968 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
971 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
976 type = ip_address_decode (&mp->entry.ip, &ip_addr);
977 mac_address_decode (mp->entry.mac, &mac);
979 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
980 rv = VNET_API_ERROR_UNSPECIFIED;
983 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
987 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
989 vl_api_bd_ip_mac_flush_reply_t *rmp;
990 bd_main_t *bdm = &bd_main;
995 bd_id = ntohl (mp->bd_id);
999 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1003 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1006 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1011 bd_flush_ip_mac (bd_index);
1014 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1017 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1018 u32 sw_if_index, u8 enable);
1021 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1025 vl_api_l2_interface_efp_filter_reply_t *rmp;
1026 vnet_main_t *vnm = vnet_get_main ();
1028 VALIDATE_SW_IF_INDEX (mp);
1030 // enable/disable the feature
1031 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
1032 rv = vnm->api_errno;
1034 BAD_SW_IF_INDEX_LABEL;
1035 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1039 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1041 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1043 vl_api_l2_patch_add_del_reply_t *rmp;
1044 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1048 VALIDATE_RX_SW_IF_INDEX (mp);
1049 VALIDATE_TX_SW_IF_INDEX (mp);
1051 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1052 ntohl (mp->tx_sw_if_index),
1053 (int) (mp->is_add != 0));
1055 BAD_RX_SW_IF_INDEX_LABEL;
1056 BAD_TX_SW_IF_INDEX_LABEL;
1058 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1062 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1064 vl_api_sw_interface_set_vpath_reply_t *rmp;
1066 u32 sw_if_index = ntohl (mp->sw_if_index);
1068 VALIDATE_SW_IF_INDEX (mp);
1070 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1071 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1072 sw_if_index, mp->enable, 0, 0);
1073 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1074 sw_if_index, mp->enable, 0, 0);
1075 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1076 sw_if_index, mp->enable, 0, 0);
1077 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1078 sw_if_index, mp->enable, 0, 0);
1080 BAD_SW_IF_INDEX_LABEL;
1082 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1086 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1088 vl_api_bvi_create_reply_t *rmp;
1093 mac_address_decode (mp->mac, &mac);
1095 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1098 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1100 rmp->sw_if_index = ntohl (sw_if_index);
1106 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1108 vl_api_bvi_delete_reply_t *rmp;
1111 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1113 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1117 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1118 const l2_arp_term_publish_event_t * e2)
1120 if (e1 == NULL || e2 == NULL)
1122 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1123 (e1->sw_if_index == e2->sw_if_index) &&
1124 (mac_address_equal (&e1->mac, &e2->mac)));
1128 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1131 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1132 * and need to be volatile - to prevent them from being optimized into
1133 * a register - which could change during suspension */
1134 volatile f64 last = vlib_time_now (vm);
1135 volatile l2_arp_term_publish_event_t last_event = { };
1137 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1141 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1142 vpe_client_registration_t *reg;
1145 vlib_process_wait_for_event (vm);
1147 vlib_process_get_event_data (vm, &event_type);
1148 now = vlib_time_now (vm);
1150 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1152 l2_arp_term_publish_event_t *event;
1154 vec_foreach (event, l2am->publish_events)
1156 /* dampen duplicate events - cast away volatile */
1157 if (l2_arp_term_publish_event_is_equal
1158 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1159 (now - last) < 10.0)
1163 last_event = *event;
1166 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1168 vl_api_registration_t *vl_reg;
1170 vl_api_client_index_to_registration (reg->client_index);
1171 ALWAYS_ASSERT (vl_reg != NULL);
1173 if (vl_reg && vl_api_can_send_msg (vl_reg))
1175 vl_api_l2_arp_term_event_t *vevent;
1176 vevent = vl_msg_api_alloc (sizeof *vevent);
1177 clib_memset (vevent, 0, sizeof *vevent);
1178 vevent->_vl_msg_id =
1179 htons (REPLY_MSG_ID_BASE + VL_API_L2_ARP_TERM_EVENT);
1180 vevent->client_index = reg->client_index;
1181 vevent->pid = reg->client_pid;
1182 ip_address_encode (&event->ip, event->type, &vevent->ip);
1183 vevent->sw_if_index = htonl (event->sw_if_index);
1184 mac_address_encode (&event->mac, vevent->mac);
1185 vl_api_send_msg (vl_reg, (u8 *) vevent);
1189 vec_reset_length (l2am->publish_events);
1197 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1198 .function = l2_arp_term_process,
1199 .type = VLIB_NODE_TYPE_PROCESS,
1200 .name = "l2-arp-term-publisher",
1205 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1208 vl_api_want_l2_arp_term_events_reply_t *rmp;
1209 vpe_api_main_t *am = &vpe_api_main;
1210 vpe_client_registration_t *rp;
1214 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1220 clib_warning ("pid %d: already enabled...", mp->pid);
1221 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1226 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1227 pool_put (am->l2_arp_term_events_registrations, rp);
1228 hash_unset (am->l2_arp_term_events_registration_hash,
1230 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1231 l2_arp_term_set_publisher_node (false);
1235 if (mp->enable == 0)
1237 clib_warning ("pid %d: already disabled...", mp->pid);
1238 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1241 pool_get (am->l2_arp_term_events_registrations, rp);
1242 rp->client_index = mp->client_index;
1243 rp->client_pid = mp->pid;
1244 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1245 rp - am->l2_arp_term_events_registrations);
1246 l2_arp_term_set_publisher_node (true);
1249 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1252 static clib_error_t *
1253 want_l2_arp_term_events_reaper (u32 client_index)
1255 vpe_client_registration_t *rp;
1261 /* remove from the registration hash */
1262 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1266 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1267 pool_put (am->l2_arp_term_events_registrations, rp);
1268 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1269 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1270 l2_arp_term_set_publisher_node (false);
1275 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1277 #include <vnet/l2/l2.api.c>
1278 static clib_error_t *
1279 l2_api_hookup (vlib_main_t * vm)
1281 api_main_t *am = vlibapi_get_main ();
1283 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1284 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1287 * Set up the (msg_name, crc, message-id) table
1289 REPLY_MSG_ID_BASE = setup_message_id_table ();
1294 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1297 * fd.io coding-style-patch-verification: ON
1300 * eval: (c-set-style "gnu")