2 *------------------------------------------------------------------
3 * l2_api.c - layer 2 forwarding api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Copyright (c) 2022 Nordix Foundation.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *------------------------------------------------------------------
21 #include <vnet/vnet.h>
22 #include <vlibmemory/api.h>
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/l2/l2_input.h>
27 #include <vnet/l2/l2_fib.h>
28 #include <vnet/l2/l2_vtr.h>
29 #include <vnet/l2/l2_learn.h>
30 #include <vnet/l2/l2_bd.h>
31 #include <vnet/l2/l2_bvi.h>
32 #include <vnet/l2/l2_arp_term.h>
33 #include <vnet/ip/ip_types_api.h>
34 #include <vnet/ethernet/ethernet_types_api.h>
36 #include <vnet/format_fns.h>
37 #include <vnet/l2/l2.api_enum.h>
38 #include <vnet/l2/l2.api_types.h>
40 #define REPLY_MSG_ID_BASE l2input_main.msg_id_base
41 #include <vlibapi/api_helper_macros.h>
44 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
45 u32 rx_sw_if_index, u32 tx_sw_if_index)
47 vl_api_l2_xconnect_details_t *mp;
49 mp = vl_msg_api_alloc (sizeof (*mp));
50 clib_memset (mp, 0, sizeof (*mp));
51 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_XCONNECT_DETAILS);
52 mp->context = context;
53 mp->rx_sw_if_index = htonl (rx_sw_if_index);
54 mp->tx_sw_if_index = htonl (tx_sw_if_index);
56 vl_api_send_msg (reg, (u8 *) mp);
60 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
62 vl_api_registration_t *reg;
63 l2input_main_t *l2im = &l2input_main;
65 l2_input_config_t *config;
67 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);
81 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
84 vl_api_l2_fib_clear_table_reply_t *rmp;
86 /* Clear all MACs including static MACs */
89 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
93 send_l2fib_table_entry (vpe_api_main_t * am,
94 vl_api_registration_t * reg,
95 l2fib_entry_key_t * l2fe_key,
96 l2fib_entry_result_t * l2fe_res, u32 context)
98 vl_api_l2_fib_table_details_t *mp;
100 mp = vl_msg_api_alloc (sizeof (*mp));
101 clib_memset (mp, 0, sizeof (*mp));
102 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_FIB_TABLE_DETAILS);
105 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
107 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
108 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
109 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
110 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
111 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
112 mp->context = context;
114 vl_api_send_msg (reg, (u8 *) mp);
118 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
120 vpe_api_main_t *am = &vpe_api_main;
121 bd_main_t *bdm = &bd_main;
122 l2fib_entry_key_t *l2fe_key = NULL;
123 l2fib_entry_result_t *l2fe_res = NULL;
124 u32 ni, bd_id = ntohl (mp->bd_id);
126 vl_api_registration_t *reg;
129 reg = vl_api_client_index_to_registration (mp->client_index);
133 /* see l2fib_table_dump: ~0 means "any" */
138 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
145 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
147 vec_foreach_index (ni, l2fe_key)
149 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
150 vec_elt_at_index (l2fe_res, ni), mp->context);
157 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
159 bd_main_t *bdm = &bd_main;
160 l2input_main_t *l2im = &l2input_main;
161 vl_api_l2fib_add_del_reply_t *rmp;
163 u32 bd_id = ntohl (mp->bd_id);
164 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
168 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
169 goto bad_sw_if_index;
175 mac_address_decode (mp->mac, &mac);
179 l2fib_add_filter_entry (mac.bytes, bd_index);
182 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
183 u32 sw_if_index = ntohl (mp->sw_if_index);
184 VALIDATE_SW_IF_INDEX (mp);
185 if (vec_len (l2im->configs) <= sw_if_index)
187 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
188 goto bad_sw_if_index;
192 l2_input_config_t *config;
193 config = vec_elt_at_index (l2im->configs, sw_if_index);
194 if (!l2_input_is_bridge (config))
196 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
197 goto bad_sw_if_index;
201 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
203 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
204 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
209 u32 sw_if_index = ntohl (mp->sw_if_index);
210 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
211 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
214 BAD_SW_IF_INDEX_LABEL;
216 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
220 vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
223 vl_api_want_l2_macs_events2_reply_t *rmp;
224 l2learn_main_t *lm = &l2learn_main;
225 l2fib_main_t *fm = &l2fib_main;
226 u32 pid = ntohl (mp->pid);
228 if (mp->enable_disable)
230 if ((lm->client_pid == 0) || (lm->client_pid == pid))
232 if (mp->max_macs_in_event)
233 fm->max_macs_in_event = mp->max_macs_in_event * 10;
236 rv = VNET_API_ERROR_INVALID_VALUE;
240 /* if scan_delay was not set before */
241 if (fm->event_scan_delay == 0.0)
242 fm->event_scan_delay = (f64) (10) * 10e-3;
244 lm->client_pid = pid;
245 lm->client_index = mp->client_index;
246 l2fib_flush_all_mac (vlib_get_main ());
248 else if (lm->client_pid != pid)
250 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
254 else if (lm->client_pid)
257 lm->client_index = 0;
261 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
265 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
268 vl_api_want_l2_macs_events_reply_t *rmp;
269 l2learn_main_t *lm = &l2learn_main;
270 l2fib_main_t *fm = &l2fib_main;
271 u32 pid = ntohl (mp->pid);
272 u32 learn_limit = ntohl (mp->learn_limit);
274 if (mp->enable_disable)
276 if ((lm->client_pid == 0) || (lm->client_pid == pid))
278 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
279 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
281 rv = VNET_API_ERROR_INVALID_VALUE;
284 lm->client_pid = pid;
285 lm->client_index = mp->client_index;
287 fm->max_macs_in_event = mp->max_macs_in_event * 10;
288 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
290 /* change learn limit and flush all learned MACs */
291 lm->global_learn_limit = learn_limit;
292 l2fib_flush_all_mac (vlib_get_main ());
294 else if (lm->client_pid != pid)
296 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
300 else if (lm->client_pid)
303 lm->client_index = 0;
304 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
305 lm->global_learn_limit = learn_limit;
307 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
311 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
315 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
318 vlib_main_t *vm = vlib_get_main ();
319 vl_api_l2fib_flush_int_reply_t *rmp;
321 VALIDATE_SW_IF_INDEX (mp);
323 u32 sw_if_index = ntohl (mp->sw_if_index);
324 l2fib_flush_int_mac (vm, sw_if_index);
326 BAD_SW_IF_INDEX_LABEL;
327 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
331 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
334 vl_api_l2fib_flush_all_reply_t *rmp;
336 l2fib_flush_all_mac (vlib_get_main ());
337 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
341 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
344 vlib_main_t *vm = vlib_get_main ();
345 bd_main_t *bdm = &bd_main;
346 vl_api_l2fib_flush_bd_reply_t *rmp;
348 u32 bd_id = ntohl (mp->bd_id);
349 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
352 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
355 l2fib_flush_bd_mac (vm, *p);
357 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
361 vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
364 l2fib_main_t *fm = &l2fib_main;
365 vl_api_l2fib_set_scan_delay_reply_t *rmp;
366 u16 scan_delay = ntohs (mp->scan_delay);
370 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
371 l2fib_flush_all_mac (vlib_get_main ());
375 rv = VNET_API_ERROR_INVALID_VALUE;
380 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
384 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
386 vl_api_l2_flags_reply_t *rmp;
390 VALIDATE_SW_IF_INDEX (mp);
392 u32 sw_if_index = ntohl (mp->sw_if_index);
393 u32 flags = ntohl (mp->feature_bitmap);
396 if (flags & L2_LEARN)
397 bitmap |= L2INPUT_FEAT_LEARN;
400 bitmap |= L2INPUT_FEAT_FWD;
402 if (flags & L2_FLOOD)
403 bitmap |= L2INPUT_FEAT_FLOOD;
405 if (flags & L2_UU_FLOOD)
406 bitmap |= L2INPUT_FEAT_UU_FLOOD;
408 if (flags & L2_ARP_TERM)
409 bitmap |= L2INPUT_FEAT_ARP_TERM;
411 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
413 BAD_SW_IF_INDEX_LABEL;
415 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
417 rmp->resulting_feature_bitmap = ntohl(rbm);
422 vl_api_bridge_domain_set_default_learn_limit_t_handler (
423 vl_api_bridge_domain_set_default_learn_limit_t *mp)
425 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
428 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
429 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
433 vl_api_bridge_domain_set_learn_limit_t_handler (
434 vl_api_bridge_domain_set_learn_limit_t *mp)
436 vlib_main_t *vm = vlib_get_main ();
437 bd_main_t *bdm = &bd_main;
438 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
440 u32 bd_id = ntohl (mp->bd_id);
445 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
449 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
452 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
455 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
457 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
461 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
464 vlib_main_t *vm = vlib_get_main ();
465 bd_main_t *bdm = &bd_main;
466 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
468 u32 bd_id = ntohl (mp->bd_id);
473 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
477 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
480 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
483 bd_set_mac_age (vm, *p, mp->mac_age);
485 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
489 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
491 l2_bridge_domain_add_del_args_t a = {
492 .is_add = mp->is_add,
494 .uu_flood = mp->uu_flood,
495 .forward = mp->forward,
497 .arp_term = mp->arp_term,
498 .arp_ufwd = mp->arp_ufwd,
499 .mac_age = mp->mac_age,
500 .bd_id = ntohl (mp->bd_id),
504 int rv = bd_add_del (&a);
506 vl_api_bridge_domain_add_del_reply_t *rmp;
507 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
511 vl_api_bridge_domain_add_del_v2_t_handler (
512 vl_api_bridge_domain_add_del_v2_t *mp)
514 vl_api_bridge_domain_add_del_v2_reply_t *rmp;
515 u32 bd_id = ntohl (mp->bd_id);
518 if ((~0 == bd_id) && (mp->is_add))
519 bd_id = bd_get_unused_id ();
521 if ((~0 == bd_id) && (mp->is_add))
522 rv = VNET_API_ERROR_EAGAIN;
525 l2_bridge_domain_add_del_args_t a = { .is_add = mp->is_add,
527 .uu_flood = mp->uu_flood,
528 .forward = mp->forward,
530 .arp_term = mp->arp_term,
531 .arp_ufwd = mp->arp_ufwd,
532 .mac_age = mp->mac_age,
534 .bd_tag = mp->bd_tag };
535 rv = bd_add_del (&a);
537 REPLY_MACRO2 (VL_API_BRIDGE_DOMAIN_ADD_DEL_V2_REPLY,
538 ({ rmp->bd_id = htonl (bd_id); }));
542 send_bridge_domain_details (l2input_main_t * l2im,
543 vl_api_registration_t * reg,
544 l2_bridge_domain_t * bd_config,
545 u32 n_sw_ifs, u32 context)
547 vl_api_bridge_domain_details_t *mp;
548 l2_flood_member_t *m;
549 vl_api_bridge_domain_sw_if_t *sw_ifs;
550 l2_input_config_t *input_cfg;
552 mp = vl_msg_api_alloc (sizeof (*mp) +
553 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
554 clib_memset (mp, 0, sizeof (*mp));
555 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DETAILS);
556 mp->bd_id = ntohl (bd_config->bd_id);
557 mp->flood = bd_feature_flood (bd_config);
558 mp->uu_flood = bd_feature_uu_flood (bd_config);
559 mp->forward = bd_feature_forward (bd_config);
560 mp->learn = bd_feature_learn (bd_config);
561 mp->arp_term = bd_feature_arp_term (bd_config);
562 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
563 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
564 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
565 mp->mac_age = bd_config->mac_age;
566 if (bd_config->bd_tag)
568 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
569 ARRAY_LEN (mp->bd_tag) - 1);
570 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
573 mp->context = context;
575 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
576 vec_foreach (m, bd_config->members)
578 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
579 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
580 sw_ifs->shg = input_cfg->shg;
584 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
586 vl_api_send_msg (reg, (u8 *) mp);
590 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
592 bd_main_t *bdm = &bd_main;
593 l2input_main_t *l2im = &l2input_main;
594 vl_api_registration_t *reg;
595 u32 bd_id, bd_index, end, filter_sw_if_index;
597 reg = vl_api_client_index_to_registration (mp->client_index);
601 filter_sw_if_index = ntohl (mp->sw_if_index);
602 if (filter_sw_if_index != ~0)
603 return; /* UNIMPLEMENTED */
605 bd_id = ntohl (mp->bd_id);
610 bd_index = 0, end = vec_len (l2im->bd_configs);
613 bd_index = bd_find_index (bdm, bd_id);
620 for (; bd_index < end; bd_index++)
622 l2_bridge_domain_t *bd_config =
623 l2input_bd_config_from_index (l2im, bd_index);
624 /* skip placeholder bd_id 0 */
625 if (bd_config && (bd_config->bd_id > 0))
626 send_bridge_domain_details (l2im, reg, bd_config,
627 vec_len (bd_config->members),
633 bd_flags_decode (vl_api_bd_flags_t v)
635 bd_flags_t f = L2_NONE;
639 if (v & BRIDGE_API_FLAG_LEARN)
641 if (v & BRIDGE_API_FLAG_FWD)
643 if (v & BRIDGE_API_FLAG_FLOOD)
645 if (v & BRIDGE_API_FLAG_UU_FLOOD)
647 if (v & BRIDGE_API_FLAG_ARP_TERM)
649 if (v & BRIDGE_API_FLAG_ARP_UFWD)
656 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
658 vlib_main_t *vm = vlib_get_main ();
659 bd_main_t *bdm = &bd_main;
660 vl_api_bridge_flags_reply_t *rmp;
664 bd_flags_t flags = bd_flags_decode (mp->flags);
665 u32 bd_id = ntohl (mp->bd_id);
668 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
672 u32 bd_index = bd_find_index (bdm, bd_id);
675 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
679 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
682 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
684 rmp->resulting_feature_bitmap = ntohl(bitmap);
689 vl_api_l2_interface_vlan_tag_rewrite_t_handler
690 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
693 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
694 vnet_main_t *vnm = vnet_get_main ();
695 vlib_main_t *vm = vlib_get_main ();
698 VALIDATE_SW_IF_INDEX (mp);
700 vtr_op = ntohl (mp->vtr_op);
702 /* The L2 code is unsuspicious */
705 case L2_VTR_DISABLED:
710 case L2_VTR_TRANSLATE_1_1:
711 case L2_VTR_TRANSLATE_1_2:
712 case L2_VTR_TRANSLATE_2_1:
713 case L2_VTR_TRANSLATE_2_2:
717 rv = VNET_API_ERROR_INVALID_VALUE;
718 goto bad_sw_if_index;
721 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
722 ntohl (mp->push_dot1q), ntohl (mp->tag1),
725 BAD_SW_IF_INDEX_LABEL;
727 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
731 vl_api_l2_interface_pbb_tag_rewrite_t_handler
732 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
734 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
735 vnet_main_t *vnm = vnet_get_main ();
736 vlib_main_t *vm = vlib_get_main ();
739 mac_address_t b_dmac, b_smac;
741 VALIDATE_SW_IF_INDEX (mp);
743 vtr_op = ntohl (mp->vtr_op);
747 case L2_VTR_DISABLED:
750 case L2_VTR_TRANSLATE_2_1:
754 rv = VNET_API_ERROR_INVALID_VALUE;
755 goto bad_sw_if_index;
758 mac_address_decode (mp->b_dmac, &b_dmac);
759 mac_address_decode (mp->b_smac, &b_smac);
761 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
762 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
763 ntohl (mp->i_sid), ntohs (mp->outer_tag));
765 BAD_SW_IF_INDEX_LABEL;
767 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
771 vl_api_sw_interface_set_l2_xconnect_t_handler
772 (vl_api_sw_interface_set_l2_xconnect_t * mp)
774 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
776 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
777 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
778 vlib_main_t *vm = vlib_get_main ();
779 vnet_main_t *vnm = vnet_get_main ();
781 VALIDATE_RX_SW_IF_INDEX (mp);
785 VALIDATE_TX_SW_IF_INDEX (mp);
786 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
788 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
792 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
793 L2_BD_PORT_TYPE_NORMAL, 0, 0);
799 rv = VNET_API_ERROR_NON_ETHERNET;
801 case MODE_ERROR_BVI_DEF:
802 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
806 BAD_RX_SW_IF_INDEX_LABEL;
807 BAD_TX_SW_IF_INDEX_LABEL;
809 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
813 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
815 v = clib_net_to_host_u32 (v);
819 case L2_API_PORT_TYPE_NORMAL:
820 *l = L2_BD_PORT_TYPE_NORMAL;
822 case L2_API_PORT_TYPE_BVI:
823 *l = L2_BD_PORT_TYPE_BVI;
825 case L2_API_PORT_TYPE_UU_FWD:
826 *l = L2_BD_PORT_TYPE_UU_FWD;
830 return (VNET_API_ERROR_INVALID_VALUE);
834 vl_api_sw_interface_set_l2_bridge_t_handler
835 (vl_api_sw_interface_set_l2_bridge_t * mp)
837 bd_main_t *bdm = &bd_main;
838 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
840 vlib_main_t *vm = vlib_get_main ();
841 vnet_main_t *vnm = vnet_get_main ();
842 l2_bd_port_type_t pt;
844 VALIDATE_RX_SW_IF_INDEX (mp);
845 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
846 rv = l2_bd_port_type_decode (mp->port_type, &pt);
853 u32 bd_id = ntohl (mp->bd_id);
854 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
856 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
857 rx_sw_if_index, bd_index, pt, mp->shg, 0);
861 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
867 rv = VNET_API_ERROR_NON_ETHERNET;
869 case MODE_ERROR_BVI_DEF:
870 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
874 BAD_RX_SW_IF_INDEX_LABEL;
877 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
881 send_bd_ip_mac_entry (vpe_api_main_t * am,
882 vl_api_registration_t * reg,
884 const ip46_address_t * ip,
886 const mac_address_t * mac, u32 context)
888 vl_api_bd_ip_mac_details_t *mp;
890 mp = vl_msg_api_alloc (sizeof (*mp));
891 clib_memset (mp, 0, sizeof (*mp));
892 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BD_IP_MAC_DETAILS);
894 mp->context = context;
895 mp->entry.bd_id = ntohl (bd_id);
897 ip_address_encode (ip, itype, &mp->entry.ip);
898 mac_address_encode (mac, mp->entry.mac);
900 vl_api_send_msg (reg, (u8 *) mp);
904 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
906 vpe_api_main_t *am = &vpe_api_main;
907 bd_main_t *bdm = &bd_main;
908 l2_bridge_domain_t *bd_config;
909 u32 bd_id = ntohl (mp->bd_id);
910 u32 bd_index, start, end;
911 vl_api_registration_t *reg;
914 reg = vl_api_client_index_to_registration (mp->client_index);
918 /* see bd_id: ~0 means "any" */
922 end = vec_len (l2input_main.bd_configs);
926 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
931 vec_validate (l2input_main.bd_configs, bd_index);
936 for (bd_index = start; bd_index < end; bd_index++)
938 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
939 if (bd_is_valid (bd_config))
941 ip4_address_t ip4_addr;
942 ip6_address_t *ip6_addr;
945 bd_id = bd_config->bd_id;
947 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
949 ip46_address_t ip = {
952 mac_address_from_u64(&mac, mac64);
954 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
958 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
960 ip46_address_t ip = {
963 mac_address_from_u64(&mac, mac64);
965 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
973 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
975 ip46_address_t ip_addr = ip46_address_initializer;
976 vl_api_bd_ip_mac_add_del_reply_t *rmp;
977 bd_main_t *bdm = &bd_main;
984 bd_id = ntohl (mp->entry.bd_id);
988 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
992 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
995 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1000 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1001 mac_address_decode (mp->entry.mac, &mac);
1003 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
1004 rv = VNET_API_ERROR_UNSPECIFIED;
1007 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1011 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1013 vl_api_bd_ip_mac_flush_reply_t *rmp;
1014 bd_main_t *bdm = &bd_main;
1015 u32 bd_index, bd_id;
1019 bd_id = ntohl (mp->bd_id);
1023 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1027 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1030 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1035 bd_flush_ip_mac (bd_index);
1038 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1041 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1042 u32 sw_if_index, u8 enable);
1045 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1049 vl_api_l2_interface_efp_filter_reply_t *rmp;
1050 vnet_main_t *vnm = vnet_get_main ();
1052 VALIDATE_SW_IF_INDEX (mp);
1054 // enable/disable the feature
1055 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
1056 rv = vnm->api_errno;
1058 BAD_SW_IF_INDEX_LABEL;
1059 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1063 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1065 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1067 vl_api_l2_patch_add_del_reply_t *rmp;
1068 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1072 VALIDATE_RX_SW_IF_INDEX (mp);
1073 VALIDATE_TX_SW_IF_INDEX (mp);
1075 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1076 ntohl (mp->tx_sw_if_index),
1077 (int) (mp->is_add != 0));
1079 BAD_RX_SW_IF_INDEX_LABEL;
1080 BAD_TX_SW_IF_INDEX_LABEL;
1082 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1086 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1088 vl_api_sw_interface_set_vpath_reply_t *rmp;
1090 u32 sw_if_index = ntohl (mp->sw_if_index);
1092 VALIDATE_SW_IF_INDEX (mp);
1094 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1095 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1096 sw_if_index, mp->enable, 0, 0);
1097 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1098 sw_if_index, mp->enable, 0, 0);
1099 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1100 sw_if_index, mp->enable, 0, 0);
1101 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1102 sw_if_index, mp->enable, 0, 0);
1104 BAD_SW_IF_INDEX_LABEL;
1106 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1110 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1112 vl_api_bvi_create_reply_t *rmp;
1117 mac_address_decode (mp->mac, &mac);
1119 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1121 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1123 rmp->sw_if_index = ntohl (sw_if_index);
1128 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1130 vl_api_bvi_delete_reply_t *rmp;
1133 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1135 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1139 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1140 const l2_arp_term_publish_event_t * e2)
1142 if (e1 == NULL || e2 == NULL)
1144 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1145 (e1->sw_if_index == e2->sw_if_index) &&
1146 (mac_address_equal (&e1->mac, &e2->mac)));
1150 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1153 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1154 * and need to be volatile - to prevent them from being optimized into
1155 * a register - which could change during suspension */
1156 volatile f64 last = vlib_time_now (vm);
1157 volatile l2_arp_term_publish_event_t last_event = { };
1159 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1163 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1164 vpe_client_registration_t *reg;
1167 vlib_process_wait_for_event (vm);
1169 vlib_process_get_event_data (vm, &event_type);
1170 now = vlib_time_now (vm);
1172 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1174 l2_arp_term_publish_event_t *event;
1176 vec_foreach (event, l2am->publish_events)
1178 /* dampen duplicate events - cast away volatile */
1179 if (l2_arp_term_publish_event_is_equal
1180 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1181 (now - last) < 10.0)
1185 last_event = *event;
1188 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1190 vl_api_registration_t *vl_reg;
1192 vl_api_client_index_to_registration (reg->client_index);
1193 ALWAYS_ASSERT (vl_reg != NULL);
1195 if (vl_reg && vl_api_can_send_msg (vl_reg))
1197 vl_api_l2_arp_term_event_t *vevent;
1198 vevent = vl_msg_api_alloc (sizeof *vevent);
1199 clib_memset (vevent, 0, sizeof *vevent);
1200 vevent->_vl_msg_id =
1201 htons (REPLY_MSG_ID_BASE + VL_API_L2_ARP_TERM_EVENT);
1202 vevent->client_index = reg->client_index;
1203 vevent->pid = reg->client_pid;
1204 ip_address_encode (&event->ip, event->type, &vevent->ip);
1205 vevent->sw_if_index = htonl (event->sw_if_index);
1206 mac_address_encode (&event->mac, vevent->mac);
1207 vl_api_send_msg (vl_reg, (u8 *) vevent);
1211 vec_reset_length (l2am->publish_events);
1218 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1219 .function = l2_arp_term_process,
1220 .type = VLIB_NODE_TYPE_PROCESS,
1221 .name = "l2-arp-term-publisher",
1225 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1228 vl_api_want_l2_arp_term_events_reply_t *rmp;
1229 vpe_api_main_t *am = &vpe_api_main;
1230 vpe_client_registration_t *rp;
1234 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1240 clib_warning ("pid %d: already enabled...", mp->pid);
1241 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1246 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1247 pool_put (am->l2_arp_term_events_registrations, rp);
1248 hash_unset (am->l2_arp_term_events_registration_hash,
1250 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1251 l2_arp_term_set_publisher_node (false);
1255 if (mp->enable == 0)
1257 clib_warning ("pid %d: already disabled...", mp->pid);
1258 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1261 pool_get (am->l2_arp_term_events_registrations, rp);
1262 rp->client_index = mp->client_index;
1263 rp->client_pid = mp->pid;
1264 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1265 rp - am->l2_arp_term_events_registrations);
1266 l2_arp_term_set_publisher_node (true);
1269 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1272 static clib_error_t *
1273 want_l2_arp_term_events_reaper (u32 client_index)
1275 vpe_client_registration_t *rp;
1281 /* remove from the registration hash */
1282 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1286 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1287 pool_put (am->l2_arp_term_events_registrations, rp);
1288 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1289 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1290 l2_arp_term_set_publisher_node (false);
1295 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1297 #include <vnet/l2/l2.api.c>
1298 static clib_error_t *
1299 l2_api_hookup (vlib_main_t * vm)
1301 api_main_t *am = vlibapi_get_main ();
1304 * Set up the (msg_name, crc, message-id) table
1306 REPLY_MSG_ID_BASE = setup_message_id_table ();
1308 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1309 vl_api_set_msg_thread_safe (
1310 am, REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DUMP, 1);
1315 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1318 * fd.io coding-style-patch-verification: ON
1321 * eval: (c-set-style "gnu")