2 * l2_bd.c : layer 2 bridge domain
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/ip/format.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_bd.h>
26 #include <vnet/l2/l2_learn.h>
27 #include <vnet/l2/l2_fib.h>
28 #include <vnet/l2/l2_vtr.h>
29 #include <vnet/ip/ip4_packet.h>
30 #include <vnet/ip/ip6_packet.h>
32 #include <vppinfra/error.h>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/vec.h>
38 * @brief Ethernet Bridge Domain.
40 * Code in this file manages Layer 2 bridge domains.
47 Init bridge domain if not done already.
48 For feature bitmap, set all bits except ARP termination
51 bd_validate (l2_bridge_domain_t * bd_config)
53 if (bd_is_valid (bd_config))
55 bd_config->feature_bitmap =
56 ~(L2INPUT_FEAT_ARP_TERM | L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_ARP_UFWD);
57 bd_config->bvi_sw_if_index = ~0;
58 bd_config->uu_fwd_sw_if_index = ~0;
59 bd_config->members = 0;
60 bd_config->flood_count = 0;
61 bd_config->tun_master_count = 0;
62 bd_config->tun_normal_count = 0;
63 bd_config->no_flood_count = 0;
64 bd_config->mac_by_ip4 = 0;
65 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
70 bd_find_index (bd_main_t * bdm, u32 bd_id)
72 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
79 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
81 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
82 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
84 /* mark this index taken */
85 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
87 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
89 vec_validate (l2input_main.bd_configs, rv);
90 l2input_main.bd_configs[rv].bd_id = bd_id;
96 bd_free_ip_mac_tables (l2_bridge_domain_t * bd)
99 ip6_address_t *ip6_addr_key;
101 hash_free (bd->mac_by_ip4);
103 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
105 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
108 hash_free (bd->mac_by_ip6);
112 bd_delete (bd_main_t * bdm, u32 bd_index)
114 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
115 u32 bd_id = bd->bd_id;
117 /* flush non-static MACs in BD and removed bd_id from hash table */
118 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
119 hash_unset (bdm->bd_index_by_bd_id, bd_id);
121 /* mark this index clear */
122 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
124 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
126 bd->feature_bitmap = 0;
129 vec_free (bd->bd_tag);
131 /* free memory used by BD */
132 vec_free (bd->members);
133 bd_free_ip_mac_tables (bd);
139 update_flood_count (l2_bridge_domain_t * bd_config)
141 bd_config->flood_count = (vec_len (bd_config->members) -
142 (bd_config->tun_master_count ?
143 bd_config->tun_normal_count : 0));
144 bd_config->flood_count -= bd_config->no_flood_count;
148 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
151 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
152 (vnet_get_main (), member->sw_if_index);
155 * Add one element to the vector
156 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
157 * When flooding, the bvi interface (if present) must be the last member
158 * processed due to how BVI processing can change the packet. To enable
159 * this order, we make the bvi interface the first in the vector and
160 * flooding walks the vector in reverse. The flood-count determines where
161 * in the member list to start the walk from.
163 switch (sw_if->flood_class)
165 case VNET_FLOOD_CLASS_NO_FLOOD:
166 bd_config->no_flood_count++;
167 ix = vec_len (bd_config->members);
169 case VNET_FLOOD_CLASS_BVI:
172 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
173 bd_config->tun_master_count++;
175 case VNET_FLOOD_CLASS_NORMAL:
176 ix = (vec_len (bd_config->members) -
177 bd_config->tun_normal_count - bd_config->no_flood_count);
179 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
180 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
181 bd_config->tun_normal_count++;
185 vec_insert_elts (bd_config->members, member, 1, ix);
186 update_flood_count (bd_config);
189 #define BD_REMOVE_ERROR_OK 0
190 #define BD_REMOVE_ERROR_NOT_FOUND 1
193 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
197 /* Find and delete the member */
198 vec_foreach_index (ix, bd_config->members)
200 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
201 if (m->sw_if_index == sw_if_index)
203 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
204 (vnet_get_main (), sw_if_index);
206 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
208 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
209 bd_config->tun_master_count--;
210 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
211 bd_config->tun_normal_count--;
212 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
213 bd_config->no_flood_count--;
215 vec_delete (bd_config->members, 1, ix);
216 update_flood_count (bd_config);
218 return BD_REMOVE_ERROR_OK;
222 return BD_REMOVE_ERROR_NOT_FOUND;
227 l2bd_init (vlib_main_t * vm)
229 bd_main_t *bdm = &bd_main;
230 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
232 * create a placeholder bd with bd_id of 0 and bd_index of 0 with feature set
233 * to packet drop only. Thus, packets received from any L2 interface with
234 * uninitialized bd_index of 0 can be dropped safely.
236 u32 bd_index = bd_add_bd_index (bdm, 0);
237 ASSERT (bd_index == 0);
238 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
244 VLIB_INIT_FUNCTION (l2bd_init);
247 bd_get (u32 bd_index)
249 if (bd_index < vec_len (l2input_main.bd_configs))
250 return (vec_elt_at_index (l2input_main.bd_configs, bd_index));
255 bd_input_walk (u32 bd_index, bd_input_walk_fn_t fn, void *data)
257 l2_flood_member_t *member;
258 l2_bridge_domain_t *bd;
262 bd = bd_get (bd_index);
266 vec_foreach (member, bd->members)
268 if (WALK_STOP == fn (bd_index, member->sw_if_index))
270 sw_if_index = member->sw_if_index;
275 return (sw_if_index);
279 b2_input_recache (u32 bd_index)
281 bd_input_walk (bd_index, l2input_recache, NULL);
285 Set the learn/forward/flood flags for the bridge domain.
286 Return 0 if ok, non-zero if for an error.
289 bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
292 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
293 bd_validate (bd_config);
294 u32 feature_bitmap = 0;
296 if (flags & L2_LEARN)
298 feature_bitmap |= L2INPUT_FEAT_LEARN;
302 feature_bitmap |= L2INPUT_FEAT_FWD;
304 if (flags & L2_FLOOD)
306 feature_bitmap |= L2INPUT_FEAT_FLOOD;
308 if (flags & L2_UU_FLOOD)
310 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
312 if (flags & L2_ARP_TERM)
314 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
316 if (flags & L2_ARP_UFWD)
318 feature_bitmap |= L2INPUT_FEAT_ARP_UFWD;
323 bd_config->feature_bitmap |= feature_bitmap;
327 bd_config->feature_bitmap &= ~feature_bitmap;
330 b2_input_recache (bd_index);
332 return bd_config->feature_bitmap;
336 Set the mac age for the bridge domain.
339 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
341 l2_bridge_domain_t *bd_config;
344 vec_validate (l2input_main.bd_configs, bd_index);
345 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
346 bd_config->mac_age = age;
347 b2_input_recache (bd_index);
349 /* check if there is at least one bd with mac aging enabled */
350 vec_foreach (bd_config, l2input_main.bd_configs)
351 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
353 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
354 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
355 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
359 Set the tag for the bridge domain.
363 bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
366 l2_bridge_domain_t *bd_config;
367 vec_validate (l2input_main.bd_configs, bd_index);
368 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
370 old = bd_config->bd_tag;
374 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
378 bd_config->bd_tag = NULL;
385 Set bridge-domain learn enable/disable.
387 set bridge-domain learn <bd_id> [disable]
389 static clib_error_t *
390 bd_learn (vlib_main_t * vm,
391 unformat_input_t * input, vlib_cli_command_t * cmd)
393 bd_main_t *bdm = &bd_main;
394 clib_error_t *error = 0;
399 if (!unformat (input, "%d", &bd_id))
401 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
402 format_unformat_error, input);
407 return clib_error_return (0,
408 "No operations on the default bridge domain are supported");
410 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
413 return clib_error_return (0, "No such bridge domain %d", bd_id);
418 if (unformat (input, "disable"))
423 /* set the bridge domain flag */
424 bd_set_flags (vm, bd_index, L2_LEARN, enable);
431 * Layer 2 learning can be enabled and disabled on each
432 * interface and on each bridge-domain. Use this command to
433 * manage bridge-domains. It is enabled by default.
436 * Example of how to enable learning (where 200 is the bridge-domain-id):
437 * @cliexcmd{set bridge-domain learn 200}
438 * Example of how to disable learning (where 200 is the bridge-domain-id):
439 * @cliexcmd{set bridge-domain learn 200 disable}
442 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
443 .path = "set bridge-domain learn",
444 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
445 .function = bd_learn,
450 Set bridge-domain forward enable/disable.
452 set bridge-domain forward <bd_index> [disable]
454 static clib_error_t *
455 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
457 bd_main_t *bdm = &bd_main;
458 clib_error_t *error = 0;
463 if (!unformat (input, "%d", &bd_id))
465 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
466 format_unformat_error, input);
471 return clib_error_return (0,
472 "No operations on the default bridge domain are supported");
474 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
477 return clib_error_return (0, "No such bridge domain %d", bd_id);
482 if (unformat (input, "disable"))
487 /* set the bridge domain flag */
488 bd_set_flags (vm, bd_index, L2_FWD, enable);
496 * Layer 2 unicast forwarding can be enabled and disabled on each
497 * interface and on each bridge-domain. Use this command to
498 * manage bridge-domains. It is enabled by default.
501 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
502 * @cliexcmd{set bridge-domain forward 200}
503 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
504 * @cliexcmd{set bridge-domain forward 200 disable}
507 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
508 .path = "set bridge-domain forward",
509 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
515 Set bridge-domain flood enable/disable.
517 set bridge-domain flood <bd_index> [disable]
519 static clib_error_t *
520 bd_flood (vlib_main_t * vm,
521 unformat_input_t * input, vlib_cli_command_t * cmd)
523 bd_main_t *bdm = &bd_main;
524 clib_error_t *error = 0;
529 if (!unformat (input, "%d", &bd_id))
531 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
532 format_unformat_error, input);
537 return clib_error_return (0,
538 "No operations on the default bridge domain are supported");
540 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
543 return clib_error_return (0, "No such bridge domain %d", bd_id);
548 if (unformat (input, "disable"))
553 /* set the bridge domain flag */
554 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
561 * Layer 2 flooding can be enabled and disabled on each
562 * interface and on each bridge-domain. Use this command to
563 * manage bridge-domains. It is enabled by default.
566 * Example of how to enable flooding (where 200 is the bridge-domain-id):
567 * @cliexcmd{set bridge-domain flood 200}
568 * Example of how to disable flooding (where 200 is the bridge-domain-id):
569 * @cliexcmd{set bridge-domain flood 200 disable}
572 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
573 .path = "set bridge-domain flood",
574 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
575 .function = bd_flood,
580 Set bridge-domain unknown-unicast flood enable/disable.
582 set bridge-domain uu-flood <bd_index> [disable]
584 static clib_error_t *
585 bd_uu_flood (vlib_main_t * vm,
586 unformat_input_t * input, vlib_cli_command_t * cmd)
588 bd_main_t *bdm = &bd_main;
589 clib_error_t *error = 0;
594 if (!unformat (input, "%d", &bd_id))
596 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
597 format_unformat_error, input);
602 return clib_error_return (0,
603 "No operations on the default bridge domain are supported");
605 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
608 return clib_error_return (0, "No such bridge domain %d", bd_id);
613 if (unformat (input, "disable"))
618 /* set the bridge domain flag */
619 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
626 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
627 * bridge-domain. It is enabled by default.
630 * Example of how to enable unknown-unicast flooding (where 200 is the
632 * @cliexcmd{set bridge-domain uu-flood 200}
633 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
634 * @cliexcmd{set bridge-domain uu-flood 200 disable}
637 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
638 .path = "set bridge-domain uu-flood",
639 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
640 .function = bd_uu_flood,
645 Set bridge-domain arp-unicast forward enable/disable.
647 set bridge-domain arp-ufwd <bd_index> [disable]
649 static clib_error_t *
650 bd_arp_ufwd (vlib_main_t * vm,
651 unformat_input_t * input, vlib_cli_command_t * cmd)
653 bd_main_t *bdm = &bd_main;
654 clib_error_t *error = 0;
659 if (!unformat (input, "%d", &bd_id))
661 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
662 format_unformat_error, input);
667 return clib_error_return (0,
668 "No operations on the default bridge domain are supported");
670 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
673 return clib_error_return (0, "No such bridge domain %d", bd_id);
678 if (unformat (input, "disable"))
683 /* set the bridge domain flag */
684 bd_set_flags (vm, bd_index, L2_ARP_UFWD, enable);
691 * Layer 2 arp-unicast forwarding can be enabled and disabled on each
692 * bridge-domain. It is disabled by default.
695 * Example of how to enable arp-unicast forwarding (where 200 is the
697 * @cliexcmd{set bridge-domain arp-ufwd 200}
698 * Example of how to disable arp-unicast forwarding (where 200 is the bridge-domain-id):
699 * @cliexcmd{set bridge-domain arp-ufwd 200 disable}
702 VLIB_CLI_COMMAND (bd_arp_ufwd_cli, static) = {
703 .path = "set bridge-domain arp-ufwd",
704 .short_help = "set bridge-domain arp-ufwd <bridge-domain-id> [disable]",
705 .function = bd_arp_ufwd,
710 Set bridge-domain arp term enable/disable.
712 set bridge-domain arp term <bridge-domain-id> [disable]
714 static clib_error_t *
715 bd_arp_term (vlib_main_t * vm,
716 unformat_input_t * input, vlib_cli_command_t * cmd)
718 bd_main_t *bdm = &bd_main;
719 clib_error_t *error = 0;
724 if (!unformat (input, "%d", &bd_id))
726 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
727 format_unformat_error, input);
732 return clib_error_return (0,
733 "No operations on the default bridge domain are supported");
735 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
739 return clib_error_return (0, "No such bridge domain %d", bd_id);
742 if (unformat (input, "disable"))
745 /* set the bridge domain flag */
746 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
752 static clib_error_t *
753 bd_mac_age (vlib_main_t * vm,
754 unformat_input_t * input, vlib_cli_command_t * cmd)
756 bd_main_t *bdm = &bd_main;
757 clib_error_t *error = 0;
762 if (!unformat (input, "%d", &bd_id))
764 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
765 format_unformat_error, input);
770 return clib_error_return (0,
771 "No operations on the default bridge domain are supported");
773 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
776 return clib_error_return (0, "No such bridge domain %d", bd_id);
780 if (!unformat (input, "%u", &age))
783 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
784 format_unformat_error, input);
788 /* set the bridge domain flag */
792 clib_error_return (0, "mac aging time cannot be bigger than 255");
795 bd_set_mac_age (vm, bd_index, (u8) age);
802 * Layer 2 mac aging can be enabled and disabled on each
803 * bridge-domain. Use this command to set or disable mac aging
804 * on specific bridge-domains. It is disabled by default.
807 * Example of how to set mac aging (where 200 is the bridge-domain-id and
808 * 5 is aging time in minutes):
809 * @cliexcmd{set bridge-domain mac-age 200 5}
810 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
811 * @cliexcmd{set bridge-domain flood 200 0}
814 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
815 .path = "set bridge-domain mac-age",
816 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
817 .function = bd_mac_age,
822 * Modify whether or not an existing bridge-domain should terminate and respond
823 * to ARP Requests. ARP Termination is disabled by default.
826 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
827 * @cliexcmd{set bridge-domain arp term 200}
828 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
829 * @cliexcmd{set bridge-domain arp term 200 disable}
832 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
833 .path = "set bridge-domain arp term",
834 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
835 .function = bd_arp_term,
841 * Add/delete IP address to MAC address mapping.
843 * The clib hash implementation stores uword entries in the hash table.
844 * The hash table mac_by_ip4 is keyed via IP4 address and store the
845 * 6-byte MAC address directly in the hash table entry uword.
847 * @warning This only works for 64-bit processor with 8-byte uword;
848 * which means this code *WILL NOT WORK* for a 32-bit processor with
852 bd_add_del_ip_mac (u32 bd_index,
854 const ip46_address_t * ip,
855 const mac_address_t * mac, u8 is_add)
857 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
858 u64 new_mac = mac_address_as_u64 (mac);
861 /* make sure uword is 8 bytes */
862 ASSERT (sizeof (uword) == sizeof (u64));
863 ASSERT (bd_is_valid (bd_cfg));
865 if (IP46_TYPE_IP6 == type)
867 ip6_address_t *ip6_addr_key;
869 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, &ip->ip6);
874 /* new entry - allocate and create ip6 address key */
875 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
876 clib_memcpy (ip6_addr_key, &ip->ip6, sizeof (ip6_address_t));
878 else if (*old_mac == new_mac)
880 /* same mac entry already exist for ip6 address */
885 /* update mac for ip6 address */
886 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
887 ip6_addr_key = (ip6_address_t *) hp->key;
889 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
893 if (old_mac && (*old_mac == new_mac))
895 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
896 ip6_addr_key = (ip6_address_t *) hp->key;
897 hash_unset_mem (bd_cfg->mac_by_ip6, &ip->ip6);
898 clib_mem_free (ip6_addr_key);
906 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
909 if (old_mac && (*old_mac == new_mac))
910 /* mac entry already exist */
912 hash_set (bd_cfg->mac_by_ip4, ip->ip4.as_u32, new_mac);
916 if (old_mac && (*old_mac == new_mac))
917 hash_unset (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
926 * Flush IP address to MAC address mapping tables in a BD.
929 bd_flush_ip_mac (u32 bd_index)
931 l2_bridge_domain_t *bd = l2input_bd_config (bd_index);
932 ASSERT (bd_is_valid (bd));
933 bd_free_ip_mac_tables (bd);
936 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
940 Set bridge-domain arp entry add/delete.
942 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
944 static clib_error_t *
945 bd_arp_entry (vlib_main_t * vm,
946 unformat_input_t * input, vlib_cli_command_t * cmd)
948 ip46_address_t ip_addr = ip46_address_initializer;
949 ip46_type_t type = IP46_TYPE_IP4;
950 bd_main_t *bdm = &bd_main;
951 clib_error_t *error = 0;
957 if (!unformat (input, "%d", &bd_id))
959 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
960 format_unformat_error, input);
965 return clib_error_return (0,
966 "No operations on the default bridge domain are supported");
968 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
973 return clib_error_return (0, "No such bridge domain %d", bd_id);
975 if (unformat (input, "%U", unformat_ip4_address, &ip_addr.ip4))
977 type = IP46_TYPE_IP4;
979 else if (unformat (input, "%U", unformat_ip6_address, &ip_addr.ip6))
981 type = IP46_TYPE_IP6;
983 else if (unformat (input, "del-all"))
985 bd_flush_ip_mac (bd_index);
990 error = clib_error_return (0, "expecting IP address but got `%U'",
991 format_unformat_error, input);
995 if (!unformat (input, "%U", unformat_mac_address_t, &mac))
997 error = clib_error_return (0, "expecting MAC address but got `%U'",
998 format_unformat_error, input);
1002 if (unformat (input, "del"))
1007 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
1008 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, is_add))
1010 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
1011 is_add ? "add" : "del",
1012 format_ip46_address, &ip_addr, IP46_TYPE_ANY,
1013 format_mac_address_t, &mac);
1021 * Add an ARP entry to an existing bridge-domain.
1024 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
1025 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
1026 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
1027 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
1030 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
1031 .path = "set bridge-domain arp entry",
1032 .short_help = "set bridge-domain arp entry <bridge-domain-id> [<ip-addr> <mac-addr> [del] | del-all]",
1033 .function = bd_arp_entry,
1038 format_uu_cfg (u8 * s, va_list * args)
1040 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
1042 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
1043 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
1044 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
1045 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
1046 return (format (s, "flood"));
1048 return (format (s, "drop"));
1052 Show bridge-domain state.
1054 show bridge-domain [<bd_index>]
1056 static clib_error_t *
1057 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1059 vnet_main_t *vnm = vnet_get_main ();
1060 bd_main_t *bdm = &bd_main;
1061 clib_error_t *error = 0;
1063 l2_bridge_domain_t *bd_config;
1073 end = vec_len (l2input_main.bd_configs);
1075 if (unformat (input, "%d", &bd_id))
1077 if (unformat (input, "detail"))
1079 else if (unformat (input, "det"))
1081 if (unformat (input, "int"))
1083 if (unformat (input, "arp"))
1085 if (unformat (input, "bd-tag"))
1089 return clib_error_return (0,
1090 "No operations on the default bridge domain are supported");
1092 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1096 return clib_error_return (0, "No such bridge domain %d", bd_id);
1098 vec_validate (l2input_main.bd_configs, bd_index);
1099 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1100 if (bd_is_valid (bd_config))
1107 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1112 /* Show all bridge-domains that have been initialized */
1115 for (bd_index = start; bd_index < end; bd_index++)
1117 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1118 if (bd_is_valid (bd_config))
1123 vlib_cli_output (vm,
1124 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s %=9s %=11s",
1125 "BD-ID", "Index", "BSN", "Age(min)",
1126 "Learning", "U-Forwrd", "UU-Flood",
1127 "Flooding", "ARP-Term", "arp-ufwd",
1131 if (bd_config->mac_age)
1132 as = format (as, "%d", bd_config->mac_age);
1134 as = format (as, "off");
1135 vlib_cli_output (vm,
1136 "%=8d %=7d %=9v %=9s %=9s %=11U %=9s %=9s %=9s %=11U",
1137 bd_config->bd_id, bd_index, as,
1138 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1140 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1142 format_uu_cfg, bd_config,
1143 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1145 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1147 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_UFWD ?
1149 format_vnet_sw_if_index_name_with_NA,
1150 vnm, bd_config->bvi_sw_if_index);
1151 vlib_cli_output (vm, "%U", format_l2_input_feature_bitmap,
1152 bd_config->feature_bitmap);
1153 vec_reset_length (as);
1157 /* Show all member interfaces */
1159 vec_foreach_index (i, bd_config->members)
1161 l2_flood_member_t *member =
1162 vec_elt_at_index (bd_config->members, i);
1163 u8 swif_seq_num = l2_input_seq_num (member->sw_if_index);
1164 u32 vtr_opr, dot1q, tag1, tag2;
1167 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1168 "Interface", "If-idx", "ISN", "SHG",
1169 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1171 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1173 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1174 format_vnet_sw_if_index_name, vnm,
1175 member->sw_if_index, member->sw_if_index,
1176 swif_seq_num, member->shg,
1177 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1178 "-", i < bd_config->flood_count ? "*" : "-",
1179 format_vtr, vtr_opr, dot1q, tag1, tag2);
1181 if (~0 != bd_config->uu_fwd_sw_if_index)
1182 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1183 format_vnet_sw_if_index_name, vnm,
1184 bd_config->uu_fwd_sw_if_index,
1185 bd_config->uu_fwd_sw_if_index,
1186 0, 0, "uu", "-", "None");
1190 if ((detail || arp) &&
1191 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1194 ip6_address_t *ip6_addr;
1196 vlib_cli_output (vm,
1197 "\n IP4/IP6 to MAC table for ARP Termination");
1200 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1202 vlib_cli_output (vm, "%=40U => %=20U",
1203 format_ip4_address, &ip4_addr,
1204 format_ethernet_address, &mac_addr);
1207 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1209 vlib_cli_output (vm, "%=40U => %=20U",
1210 format_ip6_address, ip6_addr,
1211 format_ethernet_address, &mac_addr);
1216 if ((detail || bd_tag) && (bd_config->bd_tag))
1218 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1227 vlib_cli_output (vm, "no bridge-domains in use");
1235 * Show a summary of all the bridge-domain instances or detailed view of a
1236 * single bridge-domain. Bridge-domains are created by adding an interface
1237 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1241 * Example of displaying all bridge-domains:
1242 * @cliexstart{show bridge-domain}
1243 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1244 * 0 0 off off off off off local0
1245 * 200 1 on on on on off N/A
1248 * Example of displaying details of a single bridge-domains:
1249 * @cliexstart{show bridge-domain 200 detail}
1250 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1251 * 200 1 on on on on off N/A
1253 * Interface Index SHG BVI VLAN-Tag-Rewrite
1254 * GigabitEthernet0/8/0.200 3 0 - none
1255 * GigabitEthernet0/9/0.200 4 0 - none
1260 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1261 .path = "show bridge-domain",
1262 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
1263 .function = bd_show,
1268 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1270 bd_main_t *bdm = &bd_main;
1271 l2fib_main_t *fm = &l2fib_main;
1272 vlib_main_t *vm = bdm->vlib_main;
1275 if (fm->mac_table_initialized == 0)
1276 l2fib_table_init ();
1278 u32 bd_index = bd_find_index (bdm, a->bd_id);
1282 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1283 if (a->bd_id > L2_BD_ID_MAX)
1284 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1285 bd_index = bd_add_bd_index (bdm, a->bd_id);
1287 bd_flags_t enable_flags = 0, disable_flags = 0;
1289 enable_flags |= L2_FLOOD;
1291 disable_flags |= L2_FLOOD;
1294 enable_flags |= L2_UU_FLOOD;
1296 disable_flags |= L2_UU_FLOOD;
1299 enable_flags |= L2_FWD;
1301 disable_flags |= L2_FWD;
1304 enable_flags |= L2_LEARN;
1306 disable_flags |= L2_LEARN;
1309 enable_flags |= L2_ARP_TERM;
1311 disable_flags |= L2_ARP_TERM;
1314 enable_flags |= L2_ARP_UFWD;
1316 disable_flags |= L2_ARP_UFWD;
1319 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1322 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1324 bd_set_mac_age (vm, bd_index, a->mac_age);
1327 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1333 return VNET_API_ERROR_NO_SUCH_ENTRY;
1335 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1336 if (vec_len (l2input_main.bd_configs[bd_index].members))
1337 return VNET_API_ERROR_BD_IN_USE;
1338 rv = bd_delete (bdm, bd_index);
1345 Create or delete bridge-domain.
1347 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1348 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
1351 static clib_error_t *
1352 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1353 vlib_cli_command_t * cmd)
1355 unformat_input_t _line_input, *line_input = &_line_input;
1356 clib_error_t *error = 0;
1359 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term =
1363 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1366 /* Get a line of input. */
1367 if (!unformat_user (input, unformat_line_input, line_input))
1370 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1372 if (unformat (line_input, "%d", &bd_id))
1374 else if (unformat (line_input, "flood %d", &flood))
1376 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1378 else if (unformat (line_input, "forward %d", &forward))
1380 else if (unformat (line_input, "learn %d", &learn))
1382 else if (unformat (line_input, "arp-term %d", &arp_term))
1384 else if (unformat (line_input, "arp-ufwd %d", &arp_ufwd))
1386 else if (unformat (line_input, "mac-age %d", &mac_age))
1388 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1390 else if (unformat (line_input, "del"))
1393 flood = uu_flood = forward = learn = 0;
1401 error = clib_error_return (0, "bridge-domain-id not specified");
1407 error = clib_error_return (0, "bridge domain 0 can not be modified");
1413 error = clib_error_return (0, "mac age must be less than 256");
1416 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1418 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1422 clib_memset (a, 0, sizeof (*a));
1425 a->flood = (u8) flood;
1426 a->uu_flood = (u8) uu_flood;
1427 a->forward = (u8) forward;
1428 a->learn = (u8) learn;
1429 a->arp_term = (u8) arp_term;
1430 a->arp_ufwd = (u8) arp_ufwd;
1431 a->mac_age = (u8) mac_age;
1434 rv = bd_add_del (a);
1440 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1442 case VNET_API_ERROR_BD_IN_USE:
1443 error = clib_error_return (0, "bridge domain in use - remove members");
1445 case VNET_API_ERROR_NO_SUCH_ENTRY:
1446 error = clib_error_return (0, "bridge domain ID does not exist");
1448 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1449 error = clib_error_return (0, "bridge domain 0 can not be modified");
1451 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1452 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1455 error = clib_error_return (0, "bd_add_del returned %d", rv);
1461 unformat_free (line_input);
1468 * Create/Delete bridge-domain instance
1472 * Example of creating bridge-domain 1:
1473 * @cliexstart{create bridge-domain 1}
1477 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1478 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1481 * vpp# show bridge-domain
1482 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1483 * 0 0 0 off off off off off off local0
1484 * 1 1 0 off on on off on off N/A
1485 * 2 2 0 60 on on off on on N/A
1489 * Example of delete bridge-domain 1:
1490 * @cliexstart{create bridge-domain 1 del}
1496 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1497 .path = "create bridge-domain",
1498 .short_help = "create bridge-domain <bridge-domain-id>"
1499 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1500 " [arp-ufwd <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]",
1501 .function = bd_add_del_command_fn,
1508 * fd.io coding-style-patch-verification: ON
1511 * eval: (c-set-style "gnu")