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 l2input_main_t *l2im = &l2input_main;
108 l2_input_config_t *config;
110 reg = vl_api_client_index_to_registration (mp->client_index);
115 vec_foreach_index (sw_if_index, l2im->configs)
117 config = vec_elt_at_index (l2im->configs, sw_if_index);
118 if (l2_input_is_xconnect (config))
119 send_l2_xconnect_details (reg, mp->context, sw_if_index,
120 config->output_sw_if_index);
126 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
129 vl_api_l2_fib_clear_table_reply_t *rmp;
131 /* Clear all MACs including static MACs */
132 l2fib_clear_table ();
134 REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
138 send_l2fib_table_entry (vpe_api_main_t * am,
139 vl_api_registration_t * reg,
140 l2fib_entry_key_t * l2fe_key,
141 l2fib_entry_result_t * l2fe_res, u32 context)
143 vl_api_l2_fib_table_details_t *mp;
145 mp = vl_msg_api_alloc (sizeof (*mp));
146 clib_memset (mp, 0, sizeof (*mp));
147 mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
150 ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
152 mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac);
153 mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
154 mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0);
155 mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0);
156 mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0);
157 mp->context = context;
159 vl_api_send_msg (reg, (u8 *) mp);
163 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
165 vpe_api_main_t *am = &vpe_api_main;
166 bd_main_t *bdm = &bd_main;
167 l2fib_entry_key_t *l2fe_key = NULL;
168 l2fib_entry_result_t *l2fe_res = NULL;
169 u32 ni, bd_id = ntohl (mp->bd_id);
171 vl_api_registration_t *reg;
174 reg = vl_api_client_index_to_registration (mp->client_index);
178 /* see l2fib_table_dump: ~0 means "any" */
183 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
190 l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
192 vec_foreach_index (ni, l2fe_key)
194 send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni),
195 vec_elt_at_index (l2fe_res, ni), mp->context);
202 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
204 bd_main_t *bdm = &bd_main;
205 l2input_main_t *l2im = &l2input_main;
206 vl_api_l2fib_add_del_reply_t *rmp;
208 u32 bd_id = ntohl (mp->bd_id);
209 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
213 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
214 goto bad_sw_if_index;
220 mac_address_decode (mp->mac, &mac);
224 l2fib_add_filter_entry (mac.bytes, bd_index);
227 l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE;
228 u32 sw_if_index = ntohl (mp->sw_if_index);
229 VALIDATE_SW_IF_INDEX (mp);
230 if (vec_len (l2im->configs) <= sw_if_index)
232 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
233 goto bad_sw_if_index;
237 l2_input_config_t *config;
238 config = vec_elt_at_index (l2im->configs, sw_if_index);
239 if (!l2_input_is_bridge (config))
241 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
242 goto bad_sw_if_index;
246 flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC;
248 flags |= L2FIB_ENTRY_RESULT_FLAG_BVI;
249 l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags);
254 u32 sw_if_index = ntohl (mp->sw_if_index);
255 if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index))
256 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
259 BAD_SW_IF_INDEX_LABEL;
261 REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
265 vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
268 vl_api_want_l2_macs_events2_reply_t *rmp;
269 l2learn_main_t *lm = &l2learn_main;
270 l2fib_main_t *fm = &l2fib_main;
271 u32 pid = ntohl (mp->pid);
273 if (mp->enable_disable)
275 if ((lm->client_pid == 0) || (lm->client_pid == pid))
277 if (mp->max_macs_in_event)
278 fm->max_macs_in_event = mp->max_macs_in_event * 10;
281 rv = VNET_API_ERROR_INVALID_VALUE;
285 /* if scan_delay was not set before */
286 if (fm->event_scan_delay == 0.0)
287 fm->event_scan_delay = (f64) (10) * 10e-3;
289 lm->client_pid = pid;
290 lm->client_index = mp->client_index;
291 l2fib_flush_all_mac (vlib_get_main ());
293 else if (lm->client_pid != pid)
295 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
299 else if (lm->client_pid)
302 lm->client_index = 0;
306 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
310 vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
313 vl_api_want_l2_macs_events_reply_t *rmp;
314 l2learn_main_t *lm = &l2learn_main;
315 l2fib_main_t *fm = &l2fib_main;
316 u32 pid = ntohl (mp->pid);
317 u32 learn_limit = ntohl (mp->learn_limit);
319 if (mp->enable_disable)
321 if ((lm->client_pid == 0) || (lm->client_pid == pid))
323 if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
324 (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
326 rv = VNET_API_ERROR_INVALID_VALUE;
329 lm->client_pid = pid;
330 lm->client_index = mp->client_index;
332 fm->max_macs_in_event = mp->max_macs_in_event * 10;
333 fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
335 /* change learn limit and flush all learned MACs */
336 lm->global_learn_limit = learn_limit;
337 l2fib_flush_all_mac (vlib_get_main ());
339 else if (lm->client_pid != pid)
341 rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
345 else if (lm->client_pid)
348 lm->client_index = 0;
349 if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
350 lm->global_learn_limit = learn_limit;
352 lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
356 REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS_REPLY);
360 vl_api_l2fib_flush_int_t_handler (vl_api_l2fib_flush_int_t * mp)
363 vlib_main_t *vm = vlib_get_main ();
364 vl_api_l2fib_flush_int_reply_t *rmp;
366 VALIDATE_SW_IF_INDEX (mp);
368 u32 sw_if_index = ntohl (mp->sw_if_index);
369 l2fib_flush_int_mac (vm, sw_if_index);
371 BAD_SW_IF_INDEX_LABEL;
372 REPLY_MACRO (VL_API_L2FIB_FLUSH_INT_REPLY);
376 vl_api_l2fib_flush_all_t_handler (vl_api_l2fib_flush_all_t * mp)
379 vl_api_l2fib_flush_all_reply_t *rmp;
381 l2fib_flush_all_mac (vlib_get_main ());
382 REPLY_MACRO (VL_API_L2FIB_FLUSH_ALL_REPLY);
386 vl_api_l2fib_flush_bd_t_handler (vl_api_l2fib_flush_bd_t * mp)
389 vlib_main_t *vm = vlib_get_main ();
390 bd_main_t *bdm = &bd_main;
391 vl_api_l2fib_flush_bd_reply_t *rmp;
393 u32 bd_id = ntohl (mp->bd_id);
394 uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
397 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
400 l2fib_flush_bd_mac (vm, *p);
402 REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
406 vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
409 l2fib_main_t *fm = &l2fib_main;
410 vl_api_l2fib_set_scan_delay_reply_t *rmp;
411 u16 scan_delay = ntohs (mp->scan_delay);
415 fm->event_scan_delay = (f64) (scan_delay) *10e-3;
416 l2fib_flush_all_mac (vlib_get_main ());
420 rv = VNET_API_ERROR_INVALID_VALUE;
425 REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
429 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
431 vl_api_l2_flags_reply_t *rmp;
435 VALIDATE_SW_IF_INDEX (mp);
437 u32 sw_if_index = ntohl (mp->sw_if_index);
438 u32 flags = ntohl (mp->feature_bitmap);
441 if (flags & L2_LEARN)
442 bitmap |= L2INPUT_FEAT_LEARN;
445 bitmap |= L2INPUT_FEAT_FWD;
447 if (flags & L2_FLOOD)
448 bitmap |= L2INPUT_FEAT_FLOOD;
450 if (flags & L2_UU_FLOOD)
451 bitmap |= L2INPUT_FEAT_UU_FLOOD;
453 if (flags & L2_ARP_TERM)
454 bitmap |= L2INPUT_FEAT_ARP_TERM;
456 rbm = l2input_intf_bitmap_enable (sw_if_index, bitmap, mp->is_set);
458 BAD_SW_IF_INDEX_LABEL;
461 REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
463 rmp->resulting_feature_bitmap = ntohl(rbm);
469 vl_api_bridge_domain_set_default_learn_limit_t_handler (
470 vl_api_bridge_domain_set_default_learn_limit_t *mp)
472 vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
475 l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
476 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
480 vl_api_bridge_domain_set_learn_limit_t_handler (
481 vl_api_bridge_domain_set_learn_limit_t *mp)
483 vlib_main_t *vm = vlib_get_main ();
484 bd_main_t *bdm = &bd_main;
485 vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
487 u32 bd_id = ntohl (mp->bd_id);
492 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
496 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
499 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
502 bd_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
504 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
508 vl_api_bridge_domain_set_mac_age_t_handler (vl_api_bridge_domain_set_mac_age_t
511 vlib_main_t *vm = vlib_get_main ();
512 bd_main_t *bdm = &bd_main;
513 vl_api_bridge_domain_set_mac_age_reply_t *rmp;
515 u32 bd_id = ntohl (mp->bd_id);
520 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
524 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
527 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
530 bd_set_mac_age (vm, *p, mp->mac_age);
532 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_MAC_AGE_REPLY);
536 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
538 l2_bridge_domain_add_del_args_t a = {
539 .is_add = mp->is_add,
541 .uu_flood = mp->uu_flood,
542 .forward = mp->forward,
544 .arp_term = mp->arp_term,
545 .arp_ufwd = mp->arp_ufwd,
546 .mac_age = mp->mac_age,
547 .bd_id = ntohl (mp->bd_id),
551 int rv = bd_add_del (&a);
553 vl_api_bridge_domain_add_del_reply_t *rmp;
554 REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
558 send_bridge_domain_details (l2input_main_t * l2im,
559 vl_api_registration_t * reg,
560 l2_bridge_domain_t * bd_config,
561 u32 n_sw_ifs, u32 context)
563 vl_api_bridge_domain_details_t *mp;
564 l2_flood_member_t *m;
565 vl_api_bridge_domain_sw_if_t *sw_ifs;
566 l2_input_config_t *input_cfg;
568 mp = vl_msg_api_alloc (sizeof (*mp) +
569 (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
570 clib_memset (mp, 0, sizeof (*mp));
571 mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
572 mp->bd_id = ntohl (bd_config->bd_id);
573 mp->flood = bd_feature_flood (bd_config);
574 mp->uu_flood = bd_feature_uu_flood (bd_config);
575 mp->forward = bd_feature_forward (bd_config);
576 mp->learn = bd_feature_learn (bd_config);
577 mp->arp_term = bd_feature_arp_term (bd_config);
578 mp->arp_ufwd = bd_feature_arp_ufwd (bd_config);
579 mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
580 mp->uu_fwd_sw_if_index = ntohl (bd_config->uu_fwd_sw_if_index);
581 mp->mac_age = bd_config->mac_age;
582 if (bd_config->bd_tag)
584 strncpy ((char *) mp->bd_tag, (char *) bd_config->bd_tag,
585 ARRAY_LEN (mp->bd_tag) - 1);
586 mp->bd_tag[ARRAY_LEN (mp->bd_tag) - 1] = 0;
589 mp->context = context;
591 sw_ifs = (vl_api_bridge_domain_sw_if_t *) mp->sw_if_details;
592 vec_foreach (m, bd_config->members)
594 sw_ifs->sw_if_index = ntohl (m->sw_if_index);
595 input_cfg = vec_elt_at_index (l2im->configs, m->sw_if_index);
596 sw_ifs->shg = input_cfg->shg;
600 mp->n_sw_ifs = htonl (mp->n_sw_ifs);
602 vl_api_send_msg (reg, (u8 *) mp);
606 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
608 bd_main_t *bdm = &bd_main;
609 l2input_main_t *l2im = &l2input_main;
610 vl_api_registration_t *reg;
611 u32 bd_id, bd_index, end, filter_sw_if_index;
613 reg = vl_api_client_index_to_registration (mp->client_index);
617 filter_sw_if_index = ntohl (mp->sw_if_index);
618 if (filter_sw_if_index != ~0)
619 return; /* UNIMPLEMENTED */
621 bd_id = ntohl (mp->bd_id);
626 bd_index = 0, end = vec_len (l2im->bd_configs);
629 bd_index = bd_find_index (bdm, bd_id);
636 for (; bd_index < end; bd_index++)
638 l2_bridge_domain_t *bd_config =
639 l2input_bd_config_from_index (l2im, bd_index);
640 /* skip placeholder bd_id 0 */
641 if (bd_config && (bd_config->bd_id > 0))
642 send_bridge_domain_details (l2im, reg, bd_config,
643 vec_len (bd_config->members),
649 bd_flags_decode (vl_api_bd_flags_t v)
651 bd_flags_t f = L2_NONE;
655 if (v & BRIDGE_API_FLAG_LEARN)
657 if (v & BRIDGE_API_FLAG_FWD)
659 if (v & BRIDGE_API_FLAG_FLOOD)
661 if (v & BRIDGE_API_FLAG_UU_FLOOD)
663 if (v & BRIDGE_API_FLAG_ARP_TERM)
665 if (v & BRIDGE_API_FLAG_ARP_UFWD)
672 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
674 vlib_main_t *vm = vlib_get_main ();
675 bd_main_t *bdm = &bd_main;
676 vl_api_bridge_flags_reply_t *rmp;
680 bd_flags_t flags = bd_flags_decode (mp->flags);
681 u32 bd_id = ntohl (mp->bd_id);
684 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
688 u32 bd_index = bd_find_index (bdm, bd_id);
691 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
695 bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
699 REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
701 rmp->resulting_feature_bitmap = ntohl(bitmap);
707 vl_api_l2_interface_vlan_tag_rewrite_t_handler
708 (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
711 vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
712 vnet_main_t *vnm = vnet_get_main ();
713 vlib_main_t *vm = vlib_get_main ();
716 VALIDATE_SW_IF_INDEX (mp);
718 vtr_op = ntohl (mp->vtr_op);
720 /* The L2 code is unsuspicious */
723 case L2_VTR_DISABLED:
728 case L2_VTR_TRANSLATE_1_1:
729 case L2_VTR_TRANSLATE_1_2:
730 case L2_VTR_TRANSLATE_2_1:
731 case L2_VTR_TRANSLATE_2_2:
735 rv = VNET_API_ERROR_INVALID_VALUE;
736 goto bad_sw_if_index;
739 rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
740 ntohl (mp->push_dot1q), ntohl (mp->tag1),
743 BAD_SW_IF_INDEX_LABEL;
745 REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
749 vl_api_l2_interface_pbb_tag_rewrite_t_handler
750 (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
752 vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
753 vnet_main_t *vnm = vnet_get_main ();
754 vlib_main_t *vm = vlib_get_main ();
757 mac_address_t b_dmac, b_smac;
759 VALIDATE_SW_IF_INDEX (mp);
761 vtr_op = ntohl (mp->vtr_op);
765 case L2_VTR_DISABLED:
768 case L2_VTR_TRANSLATE_2_1:
772 rv = VNET_API_ERROR_INVALID_VALUE;
773 goto bad_sw_if_index;
776 mac_address_decode (mp->b_dmac, &b_dmac);
777 mac_address_decode (mp->b_smac, &b_smac);
779 rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
780 b_dmac.bytes, b_smac.bytes, ntohs (mp->b_vlanid),
781 ntohl (mp->i_sid), ntohs (mp->outer_tag));
783 BAD_SW_IF_INDEX_LABEL;
785 REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
789 vl_api_sw_interface_set_l2_xconnect_t_handler
790 (vl_api_sw_interface_set_l2_xconnect_t * mp)
792 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
794 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
795 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
796 vlib_main_t *vm = vlib_get_main ();
797 vnet_main_t *vnm = vnet_get_main ();
799 VALIDATE_RX_SW_IF_INDEX (mp);
803 VALIDATE_TX_SW_IF_INDEX (mp);
804 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
806 L2_BD_PORT_TYPE_NORMAL, 0, tx_sw_if_index);
810 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0,
811 L2_BD_PORT_TYPE_NORMAL, 0, 0);
817 rv = VNET_API_ERROR_NON_ETHERNET;
819 case MODE_ERROR_BVI_DEF:
820 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
824 BAD_RX_SW_IF_INDEX_LABEL;
825 BAD_TX_SW_IF_INDEX_LABEL;
827 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
831 l2_bd_port_type_decode (vl_api_l2_port_type_t v, l2_bd_port_type_t * l)
833 v = clib_net_to_host_u32 (v);
837 case L2_API_PORT_TYPE_NORMAL:
838 *l = L2_BD_PORT_TYPE_NORMAL;
840 case L2_API_PORT_TYPE_BVI:
841 *l = L2_BD_PORT_TYPE_BVI;
843 case L2_API_PORT_TYPE_UU_FWD:
844 *l = L2_BD_PORT_TYPE_UU_FWD;
848 return (VNET_API_ERROR_INVALID_VALUE);
852 vl_api_sw_interface_set_l2_bridge_t_handler
853 (vl_api_sw_interface_set_l2_bridge_t * mp)
855 bd_main_t *bdm = &bd_main;
856 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
858 vlib_main_t *vm = vlib_get_main ();
859 vnet_main_t *vnm = vnet_get_main ();
860 l2_bd_port_type_t pt;
862 VALIDATE_RX_SW_IF_INDEX (mp);
863 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
864 rv = l2_bd_port_type_decode (mp->port_type, &pt);
871 u32 bd_id = ntohl (mp->bd_id);
872 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
874 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
875 rx_sw_if_index, bd_index, pt, mp->shg, 0);
879 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, pt, 0, 0);
885 rv = VNET_API_ERROR_NON_ETHERNET;
887 case MODE_ERROR_BVI_DEF:
888 rv = VNET_API_ERROR_BD_ALREADY_HAS_BVI;
892 BAD_RX_SW_IF_INDEX_LABEL;
895 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
899 send_bd_ip_mac_entry (vpe_api_main_t * am,
900 vl_api_registration_t * reg,
902 const ip46_address_t * ip,
904 const mac_address_t * mac, u32 context)
906 vl_api_bd_ip_mac_details_t *mp;
908 mp = vl_msg_api_alloc (sizeof (*mp));
909 clib_memset (mp, 0, sizeof (*mp));
910 mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
912 mp->context = context;
913 mp->entry.bd_id = ntohl (bd_id);
915 ip_address_encode (ip, itype, &mp->entry.ip);
916 mac_address_encode (mac, mp->entry.mac);
918 vl_api_send_msg (reg, (u8 *) mp);
922 vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
924 vpe_api_main_t *am = &vpe_api_main;
925 bd_main_t *bdm = &bd_main;
926 l2_bridge_domain_t *bd_config;
927 u32 bd_id = ntohl (mp->bd_id);
928 u32 bd_index, start, end;
929 vl_api_registration_t *reg;
932 reg = vl_api_client_index_to_registration (mp->client_index);
936 /* see bd_id: ~0 means "any" */
940 end = vec_len (l2input_main.bd_configs);
944 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
949 vec_validate (l2input_main.bd_configs, bd_index);
954 for (bd_index = start; bd_index < end; bd_index++)
956 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
957 if (bd_is_valid (bd_config))
959 ip4_address_t ip4_addr;
960 ip6_address_t *ip6_addr;
963 bd_id = bd_config->bd_id;
966 hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4,
968 ip46_address_t ip = {
971 mac_address_from_u64(&mac, mac64);
973 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4,
977 hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6,
979 ip46_address_t ip = {
982 mac_address_from_u64(&mac, mac64);
984 send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
993 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
995 ip46_address_t ip_addr = ip46_address_initializer;
996 vl_api_bd_ip_mac_add_del_reply_t *rmp;
997 bd_main_t *bdm = &bd_main;
1004 bd_id = ntohl (mp->entry.bd_id);
1008 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1012 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1015 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1020 type = ip_address_decode (&mp->entry.ip, &ip_addr);
1021 mac_address_decode (mp->entry.mac, &mac);
1023 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add))
1024 rv = VNET_API_ERROR_UNSPECIFIED;
1027 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1031 vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp)
1033 vl_api_bd_ip_mac_flush_reply_t *rmp;
1034 bd_main_t *bdm = &bd_main;
1035 u32 bd_index, bd_id;
1039 bd_id = ntohl (mp->bd_id);
1043 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
1047 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1050 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1055 bd_flush_ip_mac (bd_index);
1058 REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY);
1061 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1062 u32 sw_if_index, u8 enable);
1065 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1069 vl_api_l2_interface_efp_filter_reply_t *rmp;
1070 vnet_main_t *vnm = vnet_get_main ();
1072 VALIDATE_SW_IF_INDEX (mp);
1074 // enable/disable the feature
1075 l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
1076 rv = vnm->api_errno;
1078 BAD_SW_IF_INDEX_LABEL;
1079 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1083 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1085 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1087 vl_api_l2_patch_add_del_reply_t *rmp;
1088 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1092 VALIDATE_RX_SW_IF_INDEX (mp);
1093 VALIDATE_TX_SW_IF_INDEX (mp);
1095 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1096 ntohl (mp->tx_sw_if_index),
1097 (int) (mp->is_add != 0));
1099 BAD_RX_SW_IF_INDEX_LABEL;
1100 BAD_TX_SW_IF_INDEX_LABEL;
1102 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1106 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1108 vl_api_sw_interface_set_vpath_reply_t *rmp;
1110 u32 sw_if_index = ntohl (mp->sw_if_index);
1112 VALIDATE_SW_IF_INDEX (mp);
1114 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1115 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1116 sw_if_index, mp->enable, 0, 0);
1117 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1118 sw_if_index, mp->enable, 0, 0);
1119 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1120 sw_if_index, mp->enable, 0, 0);
1121 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1122 sw_if_index, mp->enable, 0, 0);
1124 BAD_SW_IF_INDEX_LABEL;
1126 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1130 vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
1132 vl_api_bvi_create_reply_t *rmp;
1137 mac_address_decode (mp->mac, &mac);
1139 rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index);
1142 REPLY_MACRO2(VL_API_BVI_CREATE_REPLY,
1144 rmp->sw_if_index = ntohl (sw_if_index);
1150 vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp)
1152 vl_api_bvi_delete_reply_t *rmp;
1155 rv = l2_bvi_delete (ntohl (mp->sw_if_index));
1157 REPLY_MACRO (VL_API_BVI_DELETE_REPLY);
1161 l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1,
1162 const l2_arp_term_publish_event_t * e2)
1164 if (e1 == NULL || e2 == NULL)
1166 return (ip46_address_is_equal (&e1->ip, &e2->ip) &&
1167 (e1->sw_if_index == e2->sw_if_index) &&
1168 (mac_address_equal (&e1->mac, &e2->mac)));
1172 l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1175 /* These cross the longjmp boundary (vlib_process_wait_for_event)
1176 * and need to be volatile - to prevent them from being optimized into
1177 * a register - which could change during suspension */
1178 volatile f64 last = vlib_time_now (vm);
1179 volatile l2_arp_term_publish_event_t last_event = { };
1181 l2_arp_term_main_t *l2am = &l2_arp_term_main;
1185 uword event_type = L2_ARP_TERM_EVENT_PUBLISH;
1186 vpe_client_registration_t *reg;
1189 vlib_process_wait_for_event (vm);
1191 vlib_process_get_event_data (vm, &event_type);
1192 now = vlib_time_now (vm);
1194 if (event_type == L2_ARP_TERM_EVENT_PUBLISH)
1196 l2_arp_term_publish_event_t *event;
1198 vec_foreach (event, l2am->publish_events)
1200 /* dampen duplicate events - cast away volatile */
1201 if (l2_arp_term_publish_event_is_equal
1202 (event, (l2_arp_term_publish_event_t *) & last_event) &&
1203 (now - last) < 10.0)
1207 last_event = *event;
1211 pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations)
1213 vl_api_registration_t *vl_reg;
1214 vl_reg = vl_api_client_index_to_registration (reg->client_index);
1215 ALWAYS_ASSERT (vl_reg != NULL);
1217 if (reg && vl_api_can_send_msg (vl_reg))
1219 vl_api_l2_arp_term_event_t * vevent;
1220 vevent = vl_msg_api_alloc (sizeof *vevent);
1221 clib_memset (vevent, 0, sizeof *vevent);
1222 vevent->_vl_msg_id = htons (VL_API_L2_ARP_TERM_EVENT);
1223 vevent->client_index = reg->client_index;
1224 vevent->pid = reg->client_pid;
1225 ip_address_encode(&event->ip,
1228 vevent->sw_if_index = htonl(event->sw_if_index);
1229 mac_address_encode(&event->mac, vevent->mac);
1230 vl_api_send_msg (vl_reg, (u8 *) vevent);
1235 vec_reset_length (l2am->publish_events);
1243 VLIB_REGISTER_NODE (l2_arp_term_process_node) = {
1244 .function = l2_arp_term_process,
1245 .type = VLIB_NODE_TYPE_PROCESS,
1246 .name = "l2-arp-term-publisher",
1251 vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t *
1254 vl_api_want_l2_arp_term_events_reply_t *rmp;
1255 vpe_api_main_t *am = &vpe_api_main;
1256 vpe_client_registration_t *rp;
1260 p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index);
1266 clib_warning ("pid %d: already enabled...", mp->pid);
1267 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1272 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1273 pool_put (am->l2_arp_term_events_registrations, rp);
1274 hash_unset (am->l2_arp_term_events_registration_hash,
1276 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1277 l2_arp_term_set_publisher_node (false);
1281 if (mp->enable == 0)
1283 clib_warning ("pid %d: already disabled...", mp->pid);
1284 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1287 pool_get (am->l2_arp_term_events_registrations, rp);
1288 rp->client_index = mp->client_index;
1289 rp->client_pid = mp->pid;
1290 hash_set (am->l2_arp_term_events_registration_hash, rp->client_index,
1291 rp - am->l2_arp_term_events_registrations);
1292 l2_arp_term_set_publisher_node (true);
1295 REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY);
1298 static clib_error_t *
1299 want_l2_arp_term_events_reaper (u32 client_index)
1301 vpe_client_registration_t *rp;
1307 /* remove from the registration hash */
1308 p = hash_get (am->l2_arp_term_events_registration_hash, client_index);
1312 rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]);
1313 pool_put (am->l2_arp_term_events_registrations, rp);
1314 hash_unset (am->l2_arp_term_events_registration_hash, client_index);
1315 if (pool_elts (am->l2_arp_term_events_registrations) == 0)
1316 l2_arp_term_set_publisher_node (false);
1321 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
1325 * Add vpe's API message handlers to the table.
1326 * vlib has already mapped shared memory and
1327 * added the client registration handlers.
1328 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1330 #define vl_msg_name_crc_list
1331 #include <vnet/vnet_all_api_h.h>
1332 #undef vl_msg_name_crc_list
1335 setup_message_id_table (api_main_t * am)
1337 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1338 foreach_vl_msg_name_crc_l2;
1342 static clib_error_t *
1343 l2_api_hookup (vlib_main_t * vm)
1345 api_main_t *am = vlibapi_get_main ();
1348 vl_msg_api_set_handlers(VL_API_##N, #n, \
1349 vl_api_##n##_t_handler, \
1351 vl_api_##n##_t_endian, \
1352 vl_api_##n##_t_print, \
1353 sizeof(vl_api_##n##_t), 1);
1354 foreach_vpe_api_msg;
1357 /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
1358 am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
1361 * Set up the (msg_name, crc, message-id) table
1363 setup_message_id_table (am);
1368 VLIB_API_INIT_FUNCTION (l2_api_hookup);
1371 * fd.io coding-style-patch-verification: ON
1374 * eval: (c-set-style "gnu")