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 _ (L2FIB_SET_SCAN_DELAY, l2fib_set_scan_delay) \
62 _ (WANT_L2_MACS_EVENTS, want_l2_macs_events) \
63 _ (WANT_L2_MACS_EVENTS2, want_l2_macs_events2) \
64 _ (L2_FLAGS, l2_flags) \
65 _ (SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
66 _ (SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
67 _ (L2_PATCH_ADD_DEL, l2_patch_add_del) \
68 _ (L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
69 _ (BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
70 _ (BD_IP_MAC_FLUSH, bd_ip_mac_flush) \
71 _ (BD_IP_MAC_DUMP, bd_ip_mac_dump) \
72 _ (BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del) \
73 _ (BRIDGE_DOMAIN_DUMP, bridge_domain_dump) \
74 _ (BRIDGE_FLAGS, bridge_flags) \
75 _ (L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
76 _ (L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite) \
77 _ (BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age) \
78 _ (SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
79 _ (BVI_CREATE, bvi_create) \
80 _ (BVI_DELETE, bvi_delete) \
81 _ (WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events) \
82 _ (BRIDGE_DOMAIN_SET_LEARN_LIMIT, bridge_domain_set_learn_limit) \
83 _ (BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT, \
84 bridge_domain_set_default_learn_limit)
87 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
88 u32 rx_sw_if_index, u32 tx_sw_if_index)
90 vl_api_l2_xconnect_details_t *mp;
92 mp = vl_msg_api_alloc (sizeof (*mp));
93 clib_memset (mp, 0, sizeof (*mp));
94 mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
95 mp->context = context;
96 mp->rx_sw_if_index = htonl (rx_sw_if_index);
97 mp->tx_sw_if_index = htonl (tx_sw_if_index);
99 vl_api_send_msg (reg, (u8 *) mp);
103 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
105 vl_api_registration_t *reg;
106 vnet_main_t *vnm = vnet_get_main ();
107 vnet_interface_main_t *im = &vnm->interface_main;
108 l2input_main_t *l2im = &l2input_main;
109 vnet_sw_interface_t *swif;
110 l2_input_config_t *config;
112 reg = vl_api_client_index_to_registration (mp->client_index);
117 pool_foreach (swif, im->sw_interfaces)
119 config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
120 if (l2_input_is_xconnect(config))
121 send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
122 config->output_sw_if_index);
128 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
131 vl_api_l2_fib_clear_table_reply_t *rmp;
133 /* Clear all MACs including static MACs */
134 l2fib_clear_table ();
136 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
140 send_l2fib_table_entry (vpe_api_main_t * am,
141 vl_api_registration_t * reg,
142 l2fib_entry_key_t * l2fe_key,
143 l2fib_entry_result_t * l2fe_res, u32 context)
145 vl_api_l2_fib_table_details_t *mp;
147 mp = vl_msg_api_alloc (sizeof (*mp));
148 clib_memset (mp, 0, sizeof (*mp));
149 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
152 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
154 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
155 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
156 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
157 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
158 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
159 mp->context = context;
161 vl_api_send_msg (reg, (u8 *) mp);
165 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
167 vpe_api_main_t *am = &vpe_api_main;
168 bd_main_t *bdm = &bd_main;
169 l2fib_entry_key_t *l2fe_key = NULL;
170 l2fib_entry_result_t *l2fe_res = NULL;
171 u32 ni, bd_id = ntohl (mp->bd_id);
173 vl_api_registration_t *reg;
176 reg = vl_api_client_index_to_registration (mp->client_index);
180 /* see l2fib_table_dump: ~0 means "any" */
185 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
192 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
194 vec_foreach_index (ni, l2fe_key)
196 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
197 vec_elt_at_index (l2fe_res, ni), mp->context);
204 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
206 bd_main_t *bdm = &bd_main;
207 l2input_main_t *l2im = &l2input_main;
208 vl_api_l2fib_add_del_reply_t *rmp;
210 u32 bd_id = ntohl (mp->bd_id);
211 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
215 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
216 goto bad_sw_if_index;
222 mac_address_decode (mp->mac, &mac);
226 l2fib_add_filter_entry (mac.bytes, bd_index);
229 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
230 u32 sw_if_index = ntohl (mp->sw_if_index);
231 VALIDATE_SW_IF_INDEX (mp);
232 if (vec_len (l2im->configs) <= sw_if_index)
234 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
235 goto bad_sw_if_index;
239 l2_input_config_t *config;
240 config = vec_elt_at_index (l2im->configs, sw_if_index);
241 if (!l2_input_is_bridge (config))
243 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
244 goto bad_sw_if_index;
248 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
250 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
251 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
256 u32 sw_if_index = ntohl (mp->sw_if_index);
257 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
258 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
261 BAD_SW_IF_INDEX_LABEL;
263 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
267 vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
270 vl_api_want_l2_macs_events2_reply_t *rmp;
271 l2learn_main_t *lm = &l2learn_main;
272 l2fib_main_t *fm = &l2fib_main;
273 u32 pid = ntohl (mp->pid);
275 if (mp->enable_disable)
277 if ((lm->client_pid == 0) || (lm->client_pid == pid))
279 if (mp->max_macs_in_event)
280 fm->max_macs_in_event = mp->max_macs_in_event * 10;
283 rv = VNET_API_ERROR_INVALID_VALUE;
287 /* if scan_delay was not set before */
288 if (fm->event_scan_delay == 0.0)
289 fm->event_scan_delay = (f64) (10) * 10e-3;
291 lm->client_pid = pid;
292 lm->client_index = mp->client_index;
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;
308 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
312 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
315 vl_api_want_l2_macs_events_reply_t *rmp;
316 l2learn_main_t *lm = &l2learn_main;
317 l2fib_main_t *fm = &l2fib_main;
318 u32 pid = ntohl (mp->pid);
319 u32 learn_limit = ntohl (mp->learn_limit);
321 if (mp->enable_disable)
323 if ((lm->client_pid == 0) || (lm->client_pid == pid))
325 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
326 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
328 rv = VNET_API_ERROR_INVALID_VALUE;
331 lm->client_pid = pid;
332 lm->client_index = mp->client_index;
334 fm->max_macs_in_event = mp->max_macs_in_event * 10;
335 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
337 /* change learn limit and flush all learned MACs */
338 lm->global_learn_limit = learn_limit;
339 l2fib_flush_all_mac (vlib_get_main ());
341 else if (lm->client_pid != pid)
343 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
347 else if (lm->client_pid)
350 lm->client_index = 0;
351 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
352 lm->global_learn_limit = learn_limit;
354 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
358 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
362 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
365 vlib_main_t *vm = vlib_get_main ();
366 vl_api_l2fib_flush_int_reply_t *rmp;
368 VALIDATE_SW_IF_INDEX (mp);
370 u32 sw_if_index = ntohl (mp->sw_if_index);
371 l2fib_flush_int_mac (vm, sw_if_index);
373 BAD_SW_IF_INDEX_LABEL;
374 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
378 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
381 vl_api_l2fib_flush_all_reply_t *rmp;
383 l2fib_flush_all_mac (vlib_get_main ());
384 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
388 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
391 vlib_main_t *vm = vlib_get_main ();
392 bd_main_t *bdm = &bd_main;
393 vl_api_l2fib_flush_bd_reply_t *rmp;
395 u32 bd_id = ntohl (mp->bd_id);
396 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
399 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
402 l2fib_flush_bd_mac (vm, *p);
404 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
408 vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
411 l2fib_main_t *fm = &l2fib_main;
412 vl_api_l2fib_set_scan_delay_reply_t *rmp;
413 u16 scan_delay = ntohs (mp->scan_delay);
417 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
418 l2fib_flush_all_mac (vlib_get_main ());
422 rv = VNET_API_ERROR_INVALID_VALUE;
427 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
431 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
433 vl_api_l2_flags_reply_t *rmp;
437 VALIDATE_SW_IF_INDEX (mp);
439 u32 sw_if_index = ntohl (mp->sw_if_index);
440 u32 flags = ntohl (mp->feature_bitmap);
443 if (flags & L2_LEARN)
444 bitmap |= L2INPUT_FEAT_LEARN;
447 bitmap |= L2INPUT_FEAT_FWD;
449 if (flags & L2_FLOOD)
450 bitmap |= L2INPUT_FEAT_FLOOD;
452 if (flags & L2_UU_FLOOD)
453 bitmap |= L2INPUT_FEAT_UU_FLOOD;
455 if (flags & L2_ARP_TERM)
456 bitmap |= L2INPUT_FEAT_ARP_TERM;
458 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
460 BAD_SW_IF_INDEX_LABEL;
463 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
465 rmp->resulting_feature_bitmap = ntohl(rbm);
471 vl_api_bridge_domain_set_default_learn_limit_t_handler (
472 vl_api_bridge_domain_set_default_learn_limit_t *mp)
474 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
477 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
478 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
482 vl_api_bridge_domain_set_learn_limit_t_handler (
483 vl_api_bridge_domain_set_learn_limit_t *mp)
485 vlib_main_t *vm = vlib_get_main ();
486 bd_main_t *bdm = &bd_main;
487 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
489 u32 bd_id = ntohl (mp->bd_id);
494 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
498 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
501 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
504 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
506 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
510 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
513 vlib_main_t *vm = vlib_get_main ();
514 bd_main_t *bdm = &bd_main;
515 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
517 u32 bd_id = ntohl (mp->bd_id);
522 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
526 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
529 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
532 bd_set_mac_age (vm, *p, mp->mac_age);
534 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
538 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
540 l2_bridge_domain_add_del_args_t a = {
541 .is_add = mp->is_add,
543 .uu_flood = mp->uu_flood,
544 .forward = mp->forward,
546 .arp_term = mp->arp_term,
547 .arp_ufwd = mp->arp_ufwd,
548 .mac_age = mp->mac_age,
549 .bd_id = ntohl (mp->bd_id),
553 int rv = bd_add_del (&a);
555 vl_api_bridge_domain_add_del_reply_t *rmp;
556 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
560 send_bridge_domain_details (l2input_main_t * l2im,
561 vl_api_registration_t * reg,
562 l2_bridge_domain_t * bd_config,
563 u32 n_sw_ifs, u32 context)
565 vl_api_bridge_domain_details_t *mp;
566 l2_flood_member_t *m;
567 vl_api_bridge_domain_sw_if_t *sw_ifs;
568 l2_input_config_t *input_cfg;
570 mp = vl_msg_api_alloc (sizeof (*mp) +
571 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
572 clib_memset (mp, 0, sizeof (*mp));
573 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
574 mp->bd_id = ntohl (bd_config->bd_id);
575 mp->flood = bd_feature_flood (bd_config);
576 mp->uu_flood = bd_feature_uu_flood (bd_config);
577 mp->forward = bd_feature_forward (bd_config);
578 mp->learn = bd_feature_learn (bd_config);
579 mp->arp_term = bd_feature_arp_term (bd_config);
580 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
581 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
582 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
583 mp->mac_age = bd_config->mac_age;
584 if (bd_config->bd_tag)
586 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
587 ARRAY_LEN (mp->bd_tag) - 1);
588 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
591 mp->context = context;
593 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
594 vec_foreach (m, bd_config->members)
596 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
597 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
598 sw_ifs->shg = input_cfg->shg;
602 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
604 vl_api_send_msg (reg, (u8 *) mp);
608 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
610 bd_main_t *bdm = &bd_main;
611 l2input_main_t *l2im = &l2input_main;
612 vl_api_registration_t *reg;
613 u32 bd_id, bd_index, end, filter_sw_if_index;
615 reg = vl_api_client_index_to_registration (mp->client_index);
619 filter_sw_if_index = ntohl (mp->sw_if_index);
620 if (filter_sw_if_index != ~0)
621 return; /* UNIMPLEMENTED */
623 bd_id = ntohl (mp->bd_id);
628 bd_index = 0, end = vec_len (l2im->bd_configs);
631 bd_index = bd_find_index (bdm, bd_id);
638 for (; bd_index < end; bd_index++)
640 l2_bridge_domain_t *bd_config =
641 l2input_bd_config_from_index (l2im, bd_index);
642 /* skip placeholder bd_id 0 */
643 if (bd_config && (bd_config->bd_id > 0))
644 send_bridge_domain_details (l2im, reg, bd_config,
645 vec_len (bd_config->members),
651 bd_flags_decode (vl_api_bd_flags_t v)
653 bd_flags_t f = L2_NONE;
657 if (v & BRIDGE_API_FLAG_LEARN)
659 if (v & BRIDGE_API_FLAG_FWD)
661 if (v & BRIDGE_API_FLAG_FLOOD)
663 if (v & BRIDGE_API_FLAG_UU_FLOOD)
665 if (v & BRIDGE_API_FLAG_ARP_TERM)
667 if (v & BRIDGE_API_FLAG_ARP_UFWD)
674 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
676 vlib_main_t *vm = vlib_get_main ();
677 bd_main_t *bdm = &bd_main;
678 vl_api_bridge_flags_reply_t *rmp;
682 bd_flags_t flags = bd_flags_decode (mp->flags);
683 u32 bd_id = ntohl (mp->bd_id);
686 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
690 u32 bd_index = bd_find_index (bdm, bd_id);
693 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
697 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
701 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
703 rmp->resulting_feature_bitmap = ntohl(bitmap);
709 vl_api_l2_interface_vlan_tag_rewrite_t_handler
710 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
713 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
714 vnet_main_t *vnm = vnet_get_main ();
715 vlib_main_t *vm = vlib_get_main ();
718 VALIDATE_SW_IF_INDEX (mp);
720 vtr_op = ntohl (mp->vtr_op);
722 /* The L2 code is unsuspicious */
725 case L2_VTR_DISABLED:
730 case L2_VTR_TRANSLATE_1_1:
731 case L2_VTR_TRANSLATE_1_2:
732 case L2_VTR_TRANSLATE_2_1:
733 case L2_VTR_TRANSLATE_2_2:
737 rv = VNET_API_ERROR_INVALID_VALUE;
738 goto bad_sw_if_index;
741 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
742 ntohl (mp->push_dot1q), ntohl (mp->tag1),
745 BAD_SW_IF_INDEX_LABEL;
747 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
751 vl_api_l2_interface_pbb_tag_rewrite_t_handler
752 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
754 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
755 vnet_main_t *vnm = vnet_get_main ();
756 vlib_main_t *vm = vlib_get_main ();
759 mac_address_t b_dmac, b_smac;
761 VALIDATE_SW_IF_INDEX (mp);
763 vtr_op = ntohl (mp->vtr_op);
767 case L2_VTR_DISABLED:
770 case L2_VTR_TRANSLATE_2_1:
774 rv = VNET_API_ERROR_INVALID_VALUE;
775 goto bad_sw_if_index;
778 mac_address_decode (mp->b_dmac, &b_dmac);
779 mac_address_decode (mp->b_smac, &b_smac);
781 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
782 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
783 ntohl (mp->i_sid), ntohs (mp->outer_tag));
785 BAD_SW_IF_INDEX_LABEL;
787 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
791 vl_api_sw_interface_set_l2_xconnect_t_handler
792 (vl_api_sw_interface_set_l2_xconnect_t * mp)
794 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
796 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
797 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
798 vlib_main_t *vm = vlib_get_main ();
799 vnet_main_t *vnm = vnet_get_main ();
801 VALIDATE_RX_SW_IF_INDEX (mp);
805 VALIDATE_TX_SW_IF_INDEX (mp);
806 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
808 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
812 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
813 L2_BD_PORT_TYPE_NORMAL, 0, 0);
819 rv = VNET_API_ERROR_NON_ETHERNET;
821 case MODE_ERROR_BVI_DEF:
822 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
826 BAD_RX_SW_IF_INDEX_LABEL;
827 BAD_TX_SW_IF_INDEX_LABEL;
829 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
833 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
835 v = clib_net_to_host_u32 (v);
839 case L2_API_PORT_TYPE_NORMAL:
840 *l = L2_BD_PORT_TYPE_NORMAL;
842 case L2_API_PORT_TYPE_BVI:
843 *l = L2_BD_PORT_TYPE_BVI;
845 case L2_API_PORT_TYPE_UU_FWD:
846 *l = L2_BD_PORT_TYPE_UU_FWD;
850 return (VNET_API_ERROR_INVALID_VALUE);
854 vl_api_sw_interface_set_l2_bridge_t_handler
855 (vl_api_sw_interface_set_l2_bridge_t * mp)
857 bd_main_t *bdm = &bd_main;
858 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
860 vlib_main_t *vm = vlib_get_main ();
861 vnet_main_t *vnm = vnet_get_main ();
862 l2_bd_port_type_t pt;
864 VALIDATE_RX_SW_IF_INDEX (mp);
865 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
866 rv = l2_bd_port_type_decode (mp->port_type, &pt);
873 u32 bd_id = ntohl (mp->bd_id);
874 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
876 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
877 rx_sw_if_index, bd_index, pt, mp->shg, 0);
881 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
887 rv = VNET_API_ERROR_NON_ETHERNET;
889 case MODE_ERROR_BVI_DEF:
890 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
894 BAD_RX_SW_IF_INDEX_LABEL;
897 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
901 send_bd_ip_mac_entry (vpe_api_main_t * am,
902 vl_api_registration_t * reg,
904 const ip46_address_t * ip,
906 const mac_address_t * mac, u32 context)
908 vl_api_bd_ip_mac_details_t *mp;
910 mp = vl_msg_api_alloc (sizeof (*mp));
911 clib_memset (mp, 0, sizeof (*mp));
912 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
914 mp->context = context;
915 mp->entry.bd_id = ntohl (bd_id);
917 ip_address_encode (ip, itype, &mp->entry.ip);
918 mac_address_encode (mac, mp->entry.mac);
920 vl_api_send_msg (reg, (u8 *) mp);
924 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
926 vpe_api_main_t *am = &vpe_api_main;
927 bd_main_t *bdm = &bd_main;
928 l2_bridge_domain_t *bd_config;
929 u32 bd_id = ntohl (mp->bd_id);
930 u32 bd_index, start, end;
931 vl_api_registration_t *reg;
934 reg = vl_api_client_index_to_registration (mp->client_index);
938 /* see bd_id: ~0 means "any" */
942 end = vec_len (l2input_main.bd_configs);
946 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
951 vec_validate (l2input_main.bd_configs, bd_index);
956 for (bd_index = start; bd_index < end; bd_index++)
958 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
959 if (bd_is_valid (bd_config))
961 ip4_address_t ip4_addr;
962 ip6_address_t *ip6_addr;
965 bd_id = bd_config->bd_id;
968 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
970 ip46_address_t ip = {
973 mac_address_from_u64(&mac, mac64);
975 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
979 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
981 ip46_address_t ip = {
984 mac_address_from_u64(&mac, mac64);
986 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
995 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
997 ip46_address_t ip_addr = ip46_address_initializer;
998 vl_api_bd_ip_mac_add_del_reply_t *rmp;
999 bd_main_t *bdm = &bd_main;
1000 u32 bd_index, bd_id;
1006 bd_id = ntohl (mp->entry.bd_id);
1010 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1014 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1017 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1022 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1023 mac_address_decode (mp->entry.mac, &mac);
1025 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
1026 rv = VNET_API_ERROR_UNSPECIFIED;
1029 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1033 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1035 vl_api_bd_ip_mac_flush_reply_t *rmp;
1036 bd_main_t *bdm = &bd_main;
1037 u32 bd_index, bd_id;
1041 bd_id = ntohl (mp->bd_id);
1045 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1049 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1052 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1057 bd_flush_ip_mac (bd_index);
1060 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1063 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1064 u32 sw_if_index, u8 enable);
1067 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1071 vl_api_l2_interface_efp_filter_reply_t *rmp;
1072 vnet_main_t *vnm = vnet_get_main ();
1074 VALIDATE_SW_IF_INDEX (mp);
1076 // enable/disable the feature
1077 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
1078 rv = vnm->api_errno;
1080 BAD_SW_IF_INDEX_LABEL;
1081 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1085 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1087 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1089 vl_api_l2_patch_add_del_reply_t *rmp;
1090 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1094 VALIDATE_RX_SW_IF_INDEX (mp);
1095 VALIDATE_TX_SW_IF_INDEX (mp);
1097 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1098 ntohl (mp->tx_sw_if_index),
1099 (int) (mp->is_add != 0));
1101 BAD_RX_SW_IF_INDEX_LABEL;
1102 BAD_TX_SW_IF_INDEX_LABEL;
1104 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1108 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1110 vl_api_sw_interface_set_vpath_reply_t *rmp;
1112 u32 sw_if_index = ntohl (mp->sw_if_index);
1114 VALIDATE_SW_IF_INDEX (mp);
1116 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1117 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1118 sw_if_index, mp->enable, 0, 0);
1119 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1120 sw_if_index, mp->enable, 0, 0);
1121 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1122 sw_if_index, mp->enable, 0, 0);
1123 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1124 sw_if_index, mp->enable, 0, 0);
1126 BAD_SW_IF_INDEX_LABEL;
1128 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1132 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1134 vl_api_bvi_create_reply_t *rmp;
1139 mac_address_decode (mp->mac, &mac);
1141 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1144 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1146 rmp->sw_if_index = ntohl (sw_if_index);
1152 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1154 vl_api_bvi_delete_reply_t *rmp;
1157 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1159 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1163 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1164 const l2_arp_term_publish_event_t * e2)
1166 if (e1 == NULL || e2 == NULL)
1168 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1169 (e1->sw_if_index == e2->sw_if_index) &&
1170 (mac_address_equal (&e1->mac, &e2->mac)));
1174 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1177 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1178 * and need to be volatile - to prevent them from being optimized into
1179 * a register - which could change during suspension */
1180 volatile f64 last = vlib_time_now (vm);
1181 volatile l2_arp_term_publish_event_t last_event = { };
1183 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1187 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1188 vpe_client_registration_t *reg;
1191 vlib_process_wait_for_event (vm);
1193 vlib_process_get_event_data (vm, &event_type);
1194 now = vlib_time_now (vm);
1196 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1198 l2_arp_term_publish_event_t *event;
1200 vec_foreach (event, l2am->publish_events)
1202 /* dampen duplicate events - cast away volatile */
1203 if (l2_arp_term_publish_event_is_equal
1204 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1205 (now - last) < 10.0)
1209 last_event = *event;
1213 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1215 vl_api_registration_t *vl_reg;
1216 vl_reg = vl_api_client_index_to_registration (reg->client_index);
1217 ALWAYS_ASSERT (vl_reg != NULL);
1219 if (reg && vl_api_can_send_msg (vl_reg))
1221 vl_api_l2_arp_term_event_t * vevent;
1222 vevent = vl_msg_api_alloc (sizeof *vevent);
1223 clib_memset (vevent, 0, sizeof *vevent);
1224 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1225 vevent->client_index = reg->client_index;
1226 vevent->pid = reg->client_pid;
1227 ip_address_encode(&event->ip,
1230 vevent->sw_if_index = htonl(event->sw_if_index);
1231 mac_address_encode(&event->mac, vevent->mac);
1232 vl_api_send_msg (vl_reg, (u8 *) vevent);
1237 vec_reset_length (l2am->publish_events);
1245 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1246 .function = l2_arp_term_process,
1247 .type = VLIB_NODE_TYPE_PROCESS,
1248 .name = "l2-arp-term-publisher",
1253 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1256 vl_api_want_l2_arp_term_events_reply_t *rmp;
1257 vpe_api_main_t *am = &vpe_api_main;
1258 vpe_client_registration_t *rp;
1262 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1268 clib_warning ("pid %d: already enabled...", mp->pid);
1269 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1274 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1275 pool_put (am->l2_arp_term_events_registrations, rp);
1276 hash_unset (am->l2_arp_term_events_registration_hash,
1278 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1279 l2_arp_term_set_publisher_node (false);
1283 if (mp->enable == 0)
1285 clib_warning ("pid %d: already disabled...", mp->pid);
1286 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1289 pool_get (am->l2_arp_term_events_registrations, rp);
1290 rp->client_index = mp->client_index;
1291 rp->client_pid = mp->pid;
1292 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1293 rp - am->l2_arp_term_events_registrations);
1294 l2_arp_term_set_publisher_node (true);
1297 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1300 static clib_error_t *
1301 want_l2_arp_term_events_reaper (u32 client_index)
1303 vpe_client_registration_t *rp;
1309 /* remove from the registration hash */
1310 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1314 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1315 pool_put (am->l2_arp_term_events_registrations, rp);
1316 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1317 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1318 l2_arp_term_set_publisher_node (false);
1323 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1327 * Add vpe's API message handlers to the table.
1328 * vlib has already mapped shared memory and
1329 * added the client registration handlers.
1330 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1332 #define vl_msg_name_crc_list
1333 #include <vnet/vnet_all_api_h.h>
1334 #undef vl_msg_name_crc_list
1337 setup_message_id_table (api_main_t * am)
1339 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1340 foreach_vl_msg_name_crc_l2;
1344 static clib_error_t *
1345 l2_api_hookup (vlib_main_t * vm)
1347 api_main_t *am = vlibapi_get_main ();
1350 vl_msg_api_set_handlers(VL_API_##N, #n, \
1351 vl_api_##n##_t_handler, \
1353 vl_api_##n##_t_endian, \
1354 vl_api_##n##_t_print, \
1355 sizeof(vl_api_##n##_t), 1);
1356 foreach_vpe_api_msg;
1359 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1360 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1363 * Set up the (msg_name, crc, message-id) table
1365 setup_message_id_table (am);
1370 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1373 * fd.io coding-style-patch-verification: ON
1376 * eval: (c-set-style "gnu")