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/vnet_msg_enum.h>
37 #define vl_typedefs /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 #define vl_endianfun /* define message structures */
42 #include <vnet/vnet_all_api_h.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 #include <vnet/vnet_all_api_h.h>
51 #include <vlibapi/api_helper_macros.h>
53 #define foreach_vpe_api_msg \
54 _(L2_XCONNECT_DUMP, l2_xconnect_dump) \
55 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \
56 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
57 _(L2FIB_FLUSH_ALL, l2fib_flush_all) \
58 _(L2FIB_FLUSH_INT, l2fib_flush_int) \
59 _(L2FIB_FLUSH_BD, l2fib_flush_bd) \
60 _(L2FIB_ADD_DEL, l2fib_add_del) \
61 _(WANT_L2_MACS_EVENTS, want_l2_macs_events) \
62 _(L2_FLAGS, l2_flags) \
63 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
64 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
65 _(L2_PATCH_ADD_DEL, l2_patch_add_del) \
66 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
67 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
68 _(BD_IP_MAC_FLUSH, bd_ip_mac_flush) \
69 _(BD_IP_MAC_DUMP, bd_ip_mac_dump) \
70 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
71 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
72 _(BRIDGE_FLAGS, bridge_flags) \
73 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
74 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
75 _(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
76 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
77 _(BVI_CREATE, bvi_create) \
78 _(BVI_DELETE, bvi_delete) \
79 _(WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events)
82 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
83 u32 rx_sw_if_index, u32 tx_sw_if_index)
85 vl_api_l2_xconnect_details_t *mp;
87 mp = vl_msg_api_alloc (sizeof (*mp));
88 clib_memset (mp, 0, sizeof (*mp));
89 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
90 mp->context = context;
91 mp->rx_sw_if_index = htonl (rx_sw_if_index);
92 mp->tx_sw_if_index = htonl (tx_sw_if_index);
94 vl_api_send_msg (reg, (u8 *) mp);
98 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
100 vl_api_registration_t *reg;
101 vnet_main_t *vnm = vnet_get_main ();
102 vnet_interface_main_t *im = &vnm->interface_main;
103 l2input_main_t *l2im = &l2input_main;
104 vnet_sw_interface_t *swif;
105 l2_input_config_t *config;
107 reg = vl_api_client_index_to_registration (mp->client_index);
112 pool_foreach (swif, im->sw_interfaces,
114 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
115 if (l2_input_is_xconnect(config))
116 send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
117 config->output_sw_if_index);
123 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
126 vl_api_l2_fib_clear_table_reply_t *rmp;
128 /* Clear all MACs including static MACs */
129 l2fib_clear_table ();
131 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
135 send_l2fib_table_entry (vpe_api_main_t * am,
136 vl_api_registration_t * reg,
137 l2fib_entry_key_t * l2fe_key,
138 l2fib_entry_result_t * l2fe_res, u32 context)
140 vl_api_l2_fib_table_details_t *mp;
142 mp = vl_msg_api_alloc (sizeof (*mp));
143 clib_memset (mp, 0, sizeof (*mp));
144 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
147 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
149 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
150 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
151 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
152 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
153 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
154 mp->context = context;
156 vl_api_send_msg (reg, (u8 *) mp);
160 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
162 vpe_api_main_t *am = &vpe_api_main;
163 bd_main_t *bdm = &bd_main;
164 l2fib_entry_key_t *l2fe_key = NULL;
165 l2fib_entry_result_t *l2fe_res = NULL;
166 u32 ni, bd_id = ntohl (mp->bd_id);
168 vl_api_registration_t *reg;
171 reg = vl_api_client_index_to_registration (mp->client_index);
175 /* see l2fib_table_dump: ~0 means "any" */
180 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
187 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
189 vec_foreach_index (ni, l2fe_key)
191 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
192 vec_elt_at_index (l2fe_res, ni), mp->context);
199 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
201 bd_main_t *bdm = &bd_main;
202 l2input_main_t *l2im = &l2input_main;
203 vl_api_l2fib_add_del_reply_t *rmp;
205 u32 bd_id = ntohl (mp->bd_id);
206 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
210 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
211 goto bad_sw_if_index;
217 mac_address_decode (mp->mac, &mac);
221 l2fib_add_filter_entry (mac.bytes, bd_index);
224 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
225 u32 sw_if_index = ntohl (mp->sw_if_index);
226 VALIDATE_SW_IF_INDEX (mp);
227 if (vec_len (l2im->configs) <= sw_if_index)
229 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
230 goto bad_sw_if_index;
234 l2_input_config_t *config;
235 config = vec_elt_at_index (l2im->configs, sw_if_index);
236 if (!l2_input_is_bridge (config))
238 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
239 goto bad_sw_if_index;
243 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
245 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
246 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
251 u32 sw_if_index = ntohl (mp->sw_if_index);
252 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
253 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
256 BAD_SW_IF_INDEX_LABEL;
258 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
262 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
265 vl_api_want_l2_macs_events_reply_t *rmp;
266 l2learn_main_t *lm = &l2learn_main;
267 l2fib_main_t *fm = &l2fib_main;
268 u32 pid = ntohl (mp->pid);
269 u32 learn_limit = ntohl (mp->learn_limit);
271 if (mp->enable_disable)
273 if (lm->client_pid == 0)
275 lm->client_pid = pid;
276 lm->client_index = mp->client_index;
278 if (mp->max_macs_in_event)
279 fm->max_macs_in_event = mp->max_macs_in_event * 10;
282 rv = VNET_API_ERROR_INVALID_VALUE;
287 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
290 rv = VNET_API_ERROR_INVALID_VALUE;
294 /* change learn limit and flush all learned MACs */
295 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
296 lm->global_learn_limit = learn_limit;
299 rv = VNET_API_ERROR_INVALID_VALUE;
303 l2fib_flush_all_mac (vlib_get_main ());
305 else if (lm->client_pid != pid)
307 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
311 else if (lm->client_pid)
314 lm->client_index = 0;
315 if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
316 lm->global_learn_limit = learn_limit;
318 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
322 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
326 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
329 vlib_main_t *vm = vlib_get_main ();
330 vl_api_l2fib_flush_int_reply_t *rmp;
332 VALIDATE_SW_IF_INDEX (mp);
334 u32 sw_if_index = ntohl (mp->sw_if_index);
335 l2fib_flush_int_mac (vm, sw_if_index);
337 BAD_SW_IF_INDEX_LABEL;
338 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
342 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
345 vl_api_l2fib_flush_all_reply_t *rmp;
347 l2fib_flush_all_mac (vlib_get_main ());
348 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
352 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
355 vlib_main_t *vm = vlib_get_main ();
356 bd_main_t *bdm = &bd_main;
357 vl_api_l2fib_flush_bd_reply_t *rmp;
359 u32 bd_id = ntohl (mp->bd_id);
360 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
363 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
366 l2fib_flush_bd_mac (vm, *p);
368 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
372 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
374 vl_api_l2_flags_reply_t *rmp;
378 VALIDATE_SW_IF_INDEX (mp);
380 u32 sw_if_index = ntohl (mp->sw_if_index);
381 u32 flags = ntohl (mp->feature_bitmap);
384 if (flags & L2_LEARN)
385 bitmap |= L2INPUT_FEAT_LEARN;
388 bitmap |= L2INPUT_FEAT_FWD;
390 if (flags & L2_FLOOD)
391 bitmap |= L2INPUT_FEAT_FLOOD;
393 if (flags & L2_UU_FLOOD)
394 bitmap |= L2INPUT_FEAT_UU_FLOOD;
396 if (flags & L2_ARP_TERM)
397 bitmap |= L2INPUT_FEAT_ARP_TERM;
399 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
401 BAD_SW_IF_INDEX_LABEL;
404 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
406 rmp->resulting_feature_bitmap = ntohl(rbm);
412 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
415 vlib_main_t *vm = vlib_get_main ();
416 bd_main_t *bdm = &bd_main;
417 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
419 u32 bd_id = ntohl (mp->bd_id);
424 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
428 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
431 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
434 bd_set_mac_age (vm, *p, mp->mac_age);
436 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
440 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
442 l2_bridge_domain_add_del_args_t a = {
443 .is_add = mp->is_add,
445 .uu_flood = mp->uu_flood,
446 .forward = mp->forward,
448 .arp_term = mp->arp_term,
449 .arp_ufwd = mp->arp_ufwd,
450 .mac_age = mp->mac_age,
451 .bd_id = ntohl (mp->bd_id),
455 int rv = bd_add_del (&a);
457 vl_api_bridge_domain_add_del_reply_t *rmp;
458 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
462 send_bridge_domain_details (l2input_main_t * l2im,
463 vl_api_registration_t * reg,
464 l2_bridge_domain_t * bd_config,
465 u32 n_sw_ifs, u32 context)
467 vl_api_bridge_domain_details_t *mp;
468 l2_flood_member_t *m;
469 vl_api_bridge_domain_sw_if_t *sw_ifs;
470 l2_input_config_t *input_cfg;
472 mp = vl_msg_api_alloc (sizeof (*mp) +
473 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
474 clib_memset (mp, 0, sizeof (*mp));
475 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
476 mp->bd_id = ntohl (bd_config->bd_id);
477 mp->flood = bd_feature_flood (bd_config);
478 mp->uu_flood = bd_feature_uu_flood (bd_config);
479 mp->forward = bd_feature_forward (bd_config);
480 mp->learn = bd_feature_learn (bd_config);
481 mp->arp_term = bd_feature_arp_term (bd_config);
482 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
483 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
484 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
485 mp->mac_age = bd_config->mac_age;
486 if (bd_config->bd_tag)
488 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
489 ARRAY_LEN (mp->bd_tag) - 1);
490 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
493 mp->context = context;
495 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
496 vec_foreach (m, bd_config->members)
498 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
499 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
500 sw_ifs->shg = input_cfg->shg;
504 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
506 vl_api_send_msg (reg, (u8 *) mp);
510 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
512 bd_main_t *bdm = &bd_main;
513 l2input_main_t *l2im = &l2input_main;
514 vl_api_registration_t *reg;
515 u32 bd_id, bd_index, end, filter_sw_if_index;
517 reg = vl_api_client_index_to_registration (mp->client_index);
521 filter_sw_if_index = ntohl (mp->sw_if_index);
522 if (filter_sw_if_index != ~0)
523 return; /* UNIMPLEMENTED */
525 bd_id = ntohl (mp->bd_id);
530 bd_index = 0, end = vec_len (l2im->bd_configs);
533 bd_index = bd_find_index (bdm, bd_id);
540 for (; bd_index < end; bd_index++)
542 l2_bridge_domain_t *bd_config =
543 l2input_bd_config_from_index (l2im, bd_index);
544 /* skip placeholder bd_id 0 */
545 if (bd_config && (bd_config->bd_id > 0))
546 send_bridge_domain_details (l2im, reg, bd_config,
547 vec_len (bd_config->members),
553 bd_flags_decode (vl_api_bd_flags_t v)
555 bd_flags_t f = L2_NONE;
559 if (v & BRIDGE_API_FLAG_LEARN)
561 if (v & BRIDGE_API_FLAG_FWD)
563 if (v & BRIDGE_API_FLAG_FLOOD)
565 if (v & BRIDGE_API_FLAG_UU_FLOOD)
567 if (v & BRIDGE_API_FLAG_ARP_TERM)
569 if (v & BRIDGE_API_FLAG_ARP_UFWD)
576 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
578 vlib_main_t *vm = vlib_get_main ();
579 bd_main_t *bdm = &bd_main;
580 vl_api_bridge_flags_reply_t *rmp;
584 bd_flags_t flags = bd_flags_decode (mp->flags);
585 u32 bd_id = ntohl (mp->bd_id);
588 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
592 u32 bd_index = bd_find_index (bdm, bd_id);
595 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
599 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
603 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
605 rmp->resulting_feature_bitmap = ntohl(bitmap);
611 vl_api_l2_interface_vlan_tag_rewrite_t_handler
612 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
615 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
616 vnet_main_t *vnm = vnet_get_main ();
617 vlib_main_t *vm = vlib_get_main ();
620 VALIDATE_SW_IF_INDEX (mp);
622 vtr_op = ntohl (mp->vtr_op);
624 /* The L2 code is unsuspicious */
627 case L2_VTR_DISABLED:
632 case L2_VTR_TRANSLATE_1_1:
633 case L2_VTR_TRANSLATE_1_2:
634 case L2_VTR_TRANSLATE_2_1:
635 case L2_VTR_TRANSLATE_2_2:
639 rv = VNET_API_ERROR_INVALID_VALUE;
640 goto bad_sw_if_index;
643 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
644 ntohl (mp->push_dot1q), ntohl (mp->tag1),
647 BAD_SW_IF_INDEX_LABEL;
649 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
653 vl_api_l2_interface_pbb_tag_rewrite_t_handler
654 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
656 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
657 vnet_main_t *vnm = vnet_get_main ();
658 vlib_main_t *vm = vlib_get_main ();
661 mac_address_t b_dmac, b_smac;
663 VALIDATE_SW_IF_INDEX (mp);
665 vtr_op = ntohl (mp->vtr_op);
669 case L2_VTR_DISABLED:
672 case L2_VTR_TRANSLATE_2_1:
676 rv = VNET_API_ERROR_INVALID_VALUE;
677 goto bad_sw_if_index;
680 mac_address_decode (mp->b_dmac, &b_dmac);
681 mac_address_decode (mp->b_smac, &b_smac);
683 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
684 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
685 ntohl (mp->i_sid), ntohs (mp->outer_tag));
687 BAD_SW_IF_INDEX_LABEL;
689 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
693 vl_api_sw_interface_set_l2_xconnect_t_handler
694 (vl_api_sw_interface_set_l2_xconnect_t * mp)
696 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
698 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
699 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
700 vlib_main_t *vm = vlib_get_main ();
701 vnet_main_t *vnm = vnet_get_main ();
703 VALIDATE_RX_SW_IF_INDEX (mp);
707 VALIDATE_TX_SW_IF_INDEX (mp);
708 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
710 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
714 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
715 L2_BD_PORT_TYPE_NORMAL, 0, 0);
721 rv = VNET_API_ERROR_NON_ETHERNET;
723 case MODE_ERROR_BVI_DEF:
724 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
728 BAD_RX_SW_IF_INDEX_LABEL;
729 BAD_TX_SW_IF_INDEX_LABEL;
731 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
735 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
737 v = clib_net_to_host_u32 (v);
741 case L2_API_PORT_TYPE_NORMAL:
742 *l = L2_BD_PORT_TYPE_NORMAL;
744 case L2_API_PORT_TYPE_BVI:
745 *l = L2_BD_PORT_TYPE_BVI;
747 case L2_API_PORT_TYPE_UU_FWD:
748 *l = L2_BD_PORT_TYPE_UU_FWD;
752 return (VNET_API_ERROR_INVALID_VALUE);
756 vl_api_sw_interface_set_l2_bridge_t_handler
757 (vl_api_sw_interface_set_l2_bridge_t * mp)
759 bd_main_t *bdm = &bd_main;
760 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
762 vlib_main_t *vm = vlib_get_main ();
763 vnet_main_t *vnm = vnet_get_main ();
764 l2_bd_port_type_t pt;
766 VALIDATE_RX_SW_IF_INDEX (mp);
767 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
768 rv = l2_bd_port_type_decode (mp->port_type, &pt);
775 u32 bd_id = ntohl (mp->bd_id);
776 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
778 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
779 rx_sw_if_index, bd_index, pt, mp->shg, 0);
783 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
789 rv = VNET_API_ERROR_NON_ETHERNET;
791 case MODE_ERROR_BVI_DEF:
792 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
796 BAD_RX_SW_IF_INDEX_LABEL;
799 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
803 send_bd_ip_mac_entry (vpe_api_main_t * am,
804 vl_api_registration_t * reg,
806 const ip46_address_t * ip,
808 const mac_address_t * mac, u32 context)
810 vl_api_bd_ip_mac_details_t *mp;
812 mp = vl_msg_api_alloc (sizeof (*mp));
813 clib_memset (mp, 0, sizeof (*mp));
814 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
816 mp->context = context;
817 mp->entry.bd_id = ntohl (bd_id);
819 ip_address_encode (ip, itype, &mp->entry.ip);
820 mac_address_encode (mac, mp->entry.mac);
822 vl_api_send_msg (reg, (u8 *) mp);
826 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
828 vpe_api_main_t *am = &vpe_api_main;
829 bd_main_t *bdm = &bd_main;
830 l2_bridge_domain_t *bd_config;
831 u32 bd_id = ntohl (mp->bd_id);
832 u32 bd_index, start, end;
833 vl_api_registration_t *reg;
836 reg = vl_api_client_index_to_registration (mp->client_index);
840 /* see bd_id: ~0 means "any" */
844 end = vec_len (l2input_main.bd_configs);
848 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
853 vec_validate (l2input_main.bd_configs, bd_index);
858 for (bd_index = start; bd_index < end; bd_index++)
860 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
861 if (bd_is_valid (bd_config))
863 ip4_address_t ip4_addr;
864 ip6_address_t *ip6_addr;
867 bd_id = bd_config->bd_id;
870 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
872 ip46_address_t ip = {
875 mac_address_from_u64(&mac, mac64);
877 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
881 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
883 ip46_address_t ip = {
886 mac_address_from_u64(&mac, mac64);
888 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
897 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
899 ip46_address_t ip_addr = ip46_address_initializer;
900 vl_api_bd_ip_mac_add_del_reply_t *rmp;
901 bd_main_t *bdm = &bd_main;
908 bd_id = ntohl (mp->entry.bd_id);
912 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
916 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
919 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
924 type = ip_address_decode (&mp->entry.ip, &ip_addr);
925 mac_address_decode (mp->entry.mac, &mac);
927 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
928 rv = VNET_API_ERROR_UNSPECIFIED;
931 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
935 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
937 vl_api_bd_ip_mac_flush_reply_t *rmp;
938 bd_main_t *bdm = &bd_main;
943 bd_id = ntohl (mp->bd_id);
947 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
951 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
954 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
959 bd_flush_ip_mac (bd_index);
962 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
965 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
966 u32 sw_if_index, u8 enable);
969 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
973 vl_api_l2_interface_efp_filter_reply_t *rmp;
974 vnet_main_t *vnm = vnet_get_main ();
976 VALIDATE_SW_IF_INDEX (mp);
978 // enable/disable the feature
979 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
982 BAD_SW_IF_INDEX_LABEL;
983 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
987 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
989 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
991 vl_api_l2_patch_add_del_reply_t *rmp;
992 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
996 VALIDATE_RX_SW_IF_INDEX (mp);
997 VALIDATE_TX_SW_IF_INDEX (mp);
999 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1000 ntohl (mp->tx_sw_if_index),
1001 (int) (mp->is_add != 0));
1003 BAD_RX_SW_IF_INDEX_LABEL;
1004 BAD_TX_SW_IF_INDEX_LABEL;
1006 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1010 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1012 vl_api_sw_interface_set_vpath_reply_t *rmp;
1014 u32 sw_if_index = ntohl (mp->sw_if_index);
1016 VALIDATE_SW_IF_INDEX (mp);
1018 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1019 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1020 sw_if_index, mp->enable, 0, 0);
1021 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1022 sw_if_index, mp->enable, 0, 0);
1023 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1024 sw_if_index, mp->enable, 0, 0);
1025 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1026 sw_if_index, mp->enable, 0, 0);
1028 BAD_SW_IF_INDEX_LABEL;
1030 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1034 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1036 vl_api_bvi_create_reply_t *rmp;
1041 mac_address_decode (mp->mac, &mac);
1043 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1046 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1048 rmp->sw_if_index = ntohl (sw_if_index);
1054 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1056 vl_api_bvi_delete_reply_t *rmp;
1059 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1061 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1065 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1066 const l2_arp_term_publish_event_t * e2)
1068 if (e1 == NULL || e2 == NULL)
1070 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1071 (e1->sw_if_index == e2->sw_if_index) &&
1072 (mac_address_equal (&e1->mac, &e2->mac)));
1076 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1079 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1080 * and need to be volatile - to prevent them from being optimized into
1081 * a register - which could change during suspension */
1082 volatile f64 last = vlib_time_now (vm);
1083 volatile l2_arp_term_publish_event_t last_event = { };
1085 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1089 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1090 vpe_client_registration_t *reg;
1093 vlib_process_wait_for_event (vm);
1095 vlib_process_get_event_data (vm, &event_type);
1096 now = vlib_time_now (vm);
1098 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1100 l2_arp_term_publish_event_t *event;
1102 vec_foreach (event, l2am->publish_events)
1104 /* dampen duplicate events - cast away volatile */
1105 if (l2_arp_term_publish_event_is_equal
1106 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1107 (now - last) < 10.0)
1111 last_event = *event;
1115 pool_foreach(reg, vpe_api_main.l2_arp_term_events_registrations,
1117 vl_api_registration_t *vl_reg;
1118 vl_reg = vl_api_client_index_to_registration (reg->client_index);
1119 ALWAYS_ASSERT (vl_reg != NULL);
1121 if (reg && vl_api_can_send_msg (vl_reg))
1123 vl_api_l2_arp_term_event_t * vevent;
1124 vevent = vl_msg_api_alloc (sizeof *vevent);
1125 clib_memset (vevent, 0, sizeof *vevent);
1126 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1127 vevent->client_index = reg->client_index;
1128 vevent->pid = reg->client_pid;
1129 ip_address_encode(&event->ip,
1132 vevent->sw_if_index = htonl(event->sw_if_index);
1133 mac_address_encode(&event->mac, vevent->mac);
1134 vl_api_send_msg (vl_reg, (u8 *) vevent);
1139 vec_reset_length (l2am->publish_events);
1147 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1148 .function = l2_arp_term_process,
1149 .type = VLIB_NODE_TYPE_PROCESS,
1150 .name = "l2-arp-term-publisher",
1155 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1158 vl_api_want_l2_arp_term_events_reply_t *rmp;
1159 vpe_api_main_t *am = &vpe_api_main;
1160 vpe_client_registration_t *rp;
1164 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1170 clib_warning ("pid %d: already enabled...", mp->pid);
1171 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1176 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1177 pool_put (am->l2_arp_term_events_registrations, rp);
1178 hash_unset (am->l2_arp_term_events_registration_hash,
1180 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1181 l2_arp_term_set_publisher_node (false);
1185 if (mp->enable == 0)
1187 clib_warning ("pid %d: already disabled...", mp->pid);
1188 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1191 pool_get (am->l2_arp_term_events_registrations, rp);
1192 rp->client_index = mp->client_index;
1193 rp->client_pid = mp->pid;
1194 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1195 rp - am->l2_arp_term_events_registrations);
1196 l2_arp_term_set_publisher_node (true);
1199 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1202 static clib_error_t *
1203 want_l2_arp_term_events_reaper (u32 client_index)
1205 vpe_client_registration_t *rp;
1211 /* remove from the registration hash */
1212 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1216 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1217 pool_put (am->l2_arp_term_events_registrations, rp);
1218 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1219 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1220 l2_arp_term_set_publisher_node (false);
1225 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1229 * Add vpe's API message handlers to the table.
1230 * vlib has already mapped shared memory and
1231 * added the client registration handlers.
1232 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1234 #define vl_msg_name_crc_list
1235 #include <vnet/vnet_all_api_h.h>
1236 #undef vl_msg_name_crc_list
1239 setup_message_id_table (api_main_t * am)
1241 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1242 foreach_vl_msg_name_crc_l2;
1246 static clib_error_t *
1247 l2_api_hookup (vlib_main_t * vm)
1249 api_main_t *am = vlibapi_get_main ();
1252 vl_msg_api_set_handlers(VL_API_##N, #n, \
1253 vl_api_##n##_t_handler, \
1255 vl_api_##n##_t_endian, \
1256 vl_api_##n##_t_print, \
1257 sizeof(vl_api_##n##_t), 1);
1258 foreach_vpe_api_msg;
1261 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1262 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1265 * Set up the (msg_name, crc, message-id) table
1267 setup_message_id_table (am);
1272 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1275 * fd.io coding-style-patch-verification: ON
1278 * eval: (c-set-style "gnu")