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 = ~L2INPUT_FEAT_ARP_TERM;
56 bd_config->bvi_sw_if_index = ~0;
57 bd_config->members = 0;
58 bd_config->flood_count = 0;
59 bd_config->tun_master_count = 0;
60 bd_config->tun_normal_count = 0;
61 bd_config->mac_by_ip4 = 0;
62 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
67 bd_find_index (bd_main_t * bdm, u32 bd_id)
69 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
76 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
78 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
79 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
81 /* mark this index taken */
82 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
84 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
86 vec_validate (l2input_main.bd_configs, rv);
87 l2input_main.bd_configs[rv].bd_id = bd_id;
93 bd_delete (bd_main_t * bdm, u32 bd_index)
95 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
96 u32 bd_id = bd->bd_id;
97 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
99 hash_unset (bdm->bd_index_by_bd_id, bd_id);
101 /* mark this index clear */
102 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
104 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
106 bd->feature_bitmap = 0;
108 /* free memory used by BD and flush non-static MACs in BD */
109 vec_free (bd->members);
110 hash_free (bd->mac_by_ip4);
111 hash_free (bd->mac_by_ip6);
117 update_flood_count (l2_bridge_domain_t * bd_config)
119 bd_config->flood_count = vec_len (bd_config->members) -
120 (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
124 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
127 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
128 (vnet_get_main (), member->sw_if_index);
131 * Add one element to the vector
132 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
133 * When flooding, the bvi interface (if present) must be the last member
134 * processed due to how BVI processing can change the packet. To enable
135 * this order, we make the bvi interface the first in the vector and
136 * flooding walks the vector in reverse.
138 switch (sw_if->flood_class)
140 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
141 bd_config->tun_master_count++;
145 case VNET_FLOOD_CLASS_NORMAL:
146 ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
147 vec_len (bd_config->members) - bd_config->tun_normal_count;
149 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
150 ix = vec_len (bd_config->members);
151 bd_config->tun_normal_count++;
155 vec_insert_elts (bd_config->members, member, 1, ix);
156 update_flood_count (bd_config);
159 #define BD_REMOVE_ERROR_OK 0
160 #define BD_REMOVE_ERROR_NOT_FOUND 1
163 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
167 /* Find and delete the member */
168 vec_foreach_index (ix, bd_config->members)
170 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
171 if (m->sw_if_index == sw_if_index)
173 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
174 (vnet_get_main (), sw_if_index);
176 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
178 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
179 bd_config->tun_master_count--;
180 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
181 bd_config->tun_normal_count--;
183 vec_delete (bd_config->members, 1, ix);
184 update_flood_count (bd_config);
186 return BD_REMOVE_ERROR_OK;
190 return BD_REMOVE_ERROR_NOT_FOUND;
195 l2bd_init (vlib_main_t * vm)
197 bd_main_t *bdm = &bd_main;
198 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
200 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
201 * to packet drop only. Thus, packets received from any L2 interface with
202 * uninitialized bd_index of 0 can be dropped safely.
204 u32 bd_index = bd_add_bd_index (bdm, 0);
205 ASSERT (bd_index == 0);
206 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
212 VLIB_INIT_FUNCTION (l2bd_init);
216 Set the learn/forward/flood flags for the bridge domain.
217 Return 0 if ok, non-zero if for an error.
220 bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
223 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
224 bd_validate (bd_config);
225 u32 feature_bitmap = 0;
227 if (flags & L2_LEARN)
229 feature_bitmap |= L2INPUT_FEAT_LEARN;
233 feature_bitmap |= L2INPUT_FEAT_FWD;
235 if (flags & L2_FLOOD)
237 feature_bitmap |= L2INPUT_FEAT_FLOOD;
239 if (flags & L2_UU_FLOOD)
241 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
243 if (flags & L2_ARP_TERM)
245 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
250 bd_config->feature_bitmap |= feature_bitmap;
254 bd_config->feature_bitmap &= ~feature_bitmap;
261 Set the mac age for the bridge domain.
264 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
266 l2_bridge_domain_t *bd_config;
269 vec_validate (l2input_main.bd_configs, bd_index);
270 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
271 bd_config->mac_age = age;
273 /* check if there is at least one bd with mac aging enabled */
274 vec_foreach (bd_config, l2input_main.bd_configs)
275 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
277 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
278 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
279 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
283 Set bridge-domain learn enable/disable.
285 set bridge-domain learn <bd_id> [disable]
287 static clib_error_t *
288 bd_learn (vlib_main_t * vm,
289 unformat_input_t * input, vlib_cli_command_t * cmd)
291 bd_main_t *bdm = &bd_main;
292 clib_error_t *error = 0;
297 if (!unformat (input, "%d", &bd_id))
299 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
300 format_unformat_error, input);
305 return clib_error_return (0,
306 "No operations on the default bridge domain are supported");
308 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
311 return clib_error_return (0, "No such bridge domain %d", bd_id);
316 if (unformat (input, "disable"))
321 /* set the bridge domain flag */
322 if (bd_set_flags (vm, bd_index, L2_LEARN, enable))
325 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
334 * Layer 2 learning can be enabled and disabled on each
335 * interface and on each bridge-domain. Use this command to
336 * manage bridge-domains. It is enabled by default.
339 * Example of how to enable learning (where 200 is the bridge-domain-id):
340 * @cliexcmd{set bridge-domain learn 200}
341 * Example of how to disable learning (where 200 is the bridge-domain-id):
342 * @cliexcmd{set bridge-domain learn 200 disable}
345 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
346 .path = "set bridge-domain learn",
347 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
348 .function = bd_learn,
353 Set bridge-domain forward enable/disable.
355 set bridge-domain forward <bd_index> [disable]
357 static clib_error_t *
358 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
360 bd_main_t *bdm = &bd_main;
361 clib_error_t *error = 0;
366 if (!unformat (input, "%d", &bd_id))
368 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
369 format_unformat_error, input);
374 return clib_error_return (0,
375 "No operations on the default bridge domain are supported");
377 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
380 return clib_error_return (0, "No such bridge domain %d", bd_id);
385 if (unformat (input, "disable"))
390 /* set the bridge domain flag */
391 if (bd_set_flags (vm, bd_index, L2_FWD, enable))
394 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
404 * Layer 2 unicast forwarding can be enabled and disabled on each
405 * interface and on each bridge-domain. Use this command to
406 * manage bridge-domains. It is enabled by default.
409 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
410 * @cliexcmd{set bridge-domain forward 200}
411 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
412 * @cliexcmd{set bridge-domain forward 200 disable}
415 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
416 .path = "set bridge-domain forward",
417 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
423 Set bridge-domain flood enable/disable.
425 set bridge-domain flood <bd_index> [disable]
427 static clib_error_t *
428 bd_flood (vlib_main_t * vm,
429 unformat_input_t * input, vlib_cli_command_t * cmd)
431 bd_main_t *bdm = &bd_main;
432 clib_error_t *error = 0;
437 if (!unformat (input, "%d", &bd_id))
439 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
440 format_unformat_error, input);
445 return clib_error_return (0,
446 "No operations on the default bridge domain are supported");
448 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
451 return clib_error_return (0, "No such bridge domain %d", bd_id);
456 if (unformat (input, "disable"))
461 /* set the bridge domain flag */
462 if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
465 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
474 * Layer 2 flooding can be enabled and disabled on each
475 * interface and on each bridge-domain. Use this command to
476 * manage bridge-domains. It is enabled by default.
479 * Example of how to enable flooding (where 200 is the bridge-domain-id):
480 * @cliexcmd{set bridge-domain flood 200}
481 * Example of how to disable flooding (where 200 is the bridge-domain-id):
482 * @cliexcmd{set bridge-domain flood 200 disable}
485 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
486 .path = "set bridge-domain flood",
487 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
488 .function = bd_flood,
493 Set bridge-domain unkown-unicast flood enable/disable.
495 set bridge-domain uu-flood <bd_index> [disable]
497 static clib_error_t *
498 bd_uu_flood (vlib_main_t * vm,
499 unformat_input_t * input, vlib_cli_command_t * cmd)
501 bd_main_t *bdm = &bd_main;
502 clib_error_t *error = 0;
507 if (!unformat (input, "%d", &bd_id))
509 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
510 format_unformat_error, input);
515 return clib_error_return (0,
516 "No operations on the default bridge domain are supported");
518 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
521 return clib_error_return (0, "No such bridge domain %d", bd_id);
526 if (unformat (input, "disable"))
531 /* set the bridge domain flag */
532 if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
535 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
544 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
545 * bridge-domain. It is enabled by default.
548 * Example of how to enable unknown-unicast flooding (where 200 is the
550 * @cliexcmd{set bridge-domain uu-flood 200}
551 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
552 * @cliexcmd{set bridge-domain uu-flood 200 disable}
555 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
556 .path = "set bridge-domain uu-flood",
557 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
558 .function = bd_uu_flood,
563 Set bridge-domain arp term enable/disable.
565 set bridge-domain arp term <bridge-domain-id> [disable]
567 static clib_error_t *
568 bd_arp_term (vlib_main_t * vm,
569 unformat_input_t * input, vlib_cli_command_t * cmd)
571 bd_main_t *bdm = &bd_main;
572 clib_error_t *error = 0;
577 if (!unformat (input, "%d", &bd_id))
579 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
580 format_unformat_error, input);
585 return clib_error_return (0,
586 "No operations on the default bridge domain are supported");
588 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
592 return clib_error_return (0, "No such bridge domain %d", bd_id);
595 if (unformat (input, "disable"))
598 /* set the bridge domain flag */
599 if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
602 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
610 static clib_error_t *
611 bd_mac_age (vlib_main_t * vm,
612 unformat_input_t * input, vlib_cli_command_t * cmd)
614 bd_main_t *bdm = &bd_main;
615 clib_error_t *error = 0;
620 if (!unformat (input, "%d", &bd_id))
622 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
623 format_unformat_error, input);
628 return clib_error_return (0,
629 "No operations on the default bridge domain are supported");
631 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
634 return clib_error_return (0, "No such bridge domain %d", bd_id);
638 if (!unformat (input, "%u", &age))
641 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
642 format_unformat_error, input);
646 /* set the bridge domain flag */
650 clib_error_return (0, "mac aging time cannot be bigger than 255");
653 bd_set_mac_age (vm, bd_index, (u8) age);
660 * Layer 2 mac aging can be enabled and disabled on each
661 * bridge-domain. Use this command to set or disable mac aging
662 * on specific bridge-domains. It is disabled by default.
665 * Example of how to set mac aging (where 200 is the bridge-domain-id and
666 * 5 is aging time in minutes):
667 * @cliexcmd{set bridge-domain mac-age 200 5}
668 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
669 * @cliexcmd{set bridge-domain flood 200 0}
672 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
673 .path = "set bridge-domain mac-age",
674 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
675 .function = bd_mac_age,
680 * Modify whether or not an existing bridge-domain should terminate and respond
681 * to ARP Requests. ARP Termination is disabled by default.
684 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
685 * @cliexcmd{set bridge-domain arp term 200}
686 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
687 * @cliexcmd{set bridge-domain arp term 200 disable}
690 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
691 .path = "set bridge-domain arp term",
692 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
693 .function = bd_arp_term,
699 * Add/delete IP address to MAC address mapping.
701 * The clib hash implementation stores uword entries in the hash table.
702 * The hash table mac_by_ip4 is keyed via IP4 address and store the
703 * 6-byte MAC address directly in the hash table entry uword.
705 * @warning This only works for 64-bit processor with 8-byte uword;
706 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
710 bd_add_del_ip_mac (u32 bd_index,
711 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
713 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
714 u64 new_mac = *(u64 *) mac_addr;
716 u16 *mac16 = (u16 *) & new_mac;
718 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
719 ASSERT (bd_is_valid (bd_cfg));
721 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
724 ip6_address_t *ip6_addr_key;
726 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
730 { /* new entry - allocate and craete ip6 address key */
731 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
732 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
734 else if (*old_mac == new_mac)
735 { /* same mac entry already exist for ip6 address */
739 { /* updat mac for ip6 address */
740 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
741 ip6_addr_key = (ip6_address_t *) hp->key;
743 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
747 if (old_mac && (*old_mac == new_mac))
749 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
750 ip6_addr_key = (ip6_address_t *) hp->key;
751 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
752 clib_mem_free (ip6_addr_key);
760 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
761 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
764 if (old_mac && (*old_mac == new_mac))
765 return 0; /* mac entry already exist */
766 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
770 if (old_mac && (*old_mac == new_mac))
771 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
780 Set bridge-domain arp entry add/delete.
782 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
784 static clib_error_t *
785 bd_arp_entry (vlib_main_t * vm,
786 unformat_input_t * input, vlib_cli_command_t * cmd)
788 bd_main_t *bdm = &bd_main;
789 clib_error_t *error = 0;
797 if (!unformat (input, "%d", &bd_id))
799 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
800 format_unformat_error, input);
805 return clib_error_return (0,
806 "No operations on the default bridge domain are supported");
808 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
813 return clib_error_return (0, "No such bridge domain %d", bd_id);
815 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
819 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
825 error = clib_error_return (0, "expecting IP address but got `%U'",
826 format_unformat_error, input);
830 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
832 error = clib_error_return (0, "expecting MAC address but got `%U'",
833 format_unformat_error, input);
837 if (unformat (input, "del"))
842 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
843 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
845 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
846 is_add ? "add" : "del",
848 format_ip4_address : format_ip6_address,
849 ip_addr, format_ethernet_address, mac_addr);
857 * Add an ARP entry to an existing bridge-domain.
860 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
861 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
862 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
863 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
866 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
867 .path = "set bridge-domain arp entry",
868 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
869 .function = bd_arp_entry,
874 format_vtr (u8 * s, va_list * args)
876 u32 vtr_op = va_arg (*args, u32);
877 u32 dot1q = va_arg (*args, u32);
878 u32 tag1 = va_arg (*args, u32);
879 u32 tag2 = va_arg (*args, u32);
882 case L2_VTR_DISABLED:
883 return format (s, "none");
885 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
887 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
890 return format (s, "pop-1");
892 return format (s, "pop-2");
893 case L2_VTR_TRANSLATE_1_1:
894 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
895 case L2_VTR_TRANSLATE_1_2:
896 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
898 case L2_VTR_TRANSLATE_2_1:
899 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
900 case L2_VTR_TRANSLATE_2_2:
901 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
904 return format (s, "none");
909 Show bridge-domain state.
911 show bridge-domain [<bd_index>]
913 static clib_error_t *
914 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
916 vnet_main_t *vnm = vnet_get_main ();
917 bd_main_t *bdm = &bd_main;
918 clib_error_t *error = 0;
920 l2_bridge_domain_t *bd_config;
929 end = vec_len (l2input_main.bd_configs);
931 if (unformat (input, "%d", &bd_id))
933 if (unformat (input, "detail"))
935 else if (unformat (input, "det"))
937 if (unformat (input, "int"))
939 if (unformat (input, "arp"))
943 return clib_error_return (0,
944 "No operations on the default bridge domain are supported");
946 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
950 return clib_error_return (0, "No such bridge domain %d", bd_id);
952 vec_validate (l2input_main.bd_configs, bd_index);
953 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
954 if (bd_is_valid (bd_config))
961 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
966 /* Show all bridge-domains that have been initialized */
969 for (bd_index = start; bd_index < end; bd_index++)
971 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
972 if (bd_is_valid (bd_config))
978 "%=5s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
979 "ID", "Index", "BSN", "Age(min)", "Learning",
980 "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
984 if (bd_config->mac_age)
985 as = format (as, "%d", bd_config->mac_age);
987 as = format (as, "off");
989 "%=5d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
990 bd_config->bd_id, bd_index, bd_config->seq_num, as,
991 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
993 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
995 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
997 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
999 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1001 format_vnet_sw_if_index_name_with_NA,
1002 vnm, bd_config->bvi_sw_if_index);
1003 vec_reset_length (as);
1007 /* Show all member interfaces */
1009 vec_foreach_index (i, bd_config->members)
1011 l2_flood_member_t *member =
1012 vec_elt_at_index (bd_config->members, i);
1013 l2_input_config_t *int_config =
1014 l2input_intf_config (member->sw_if_index);
1015 u32 vtr_opr, dot1q, tag1, tag2;
1018 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1019 "Interface", "If-idx", "ISN", "SHG",
1020 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1022 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1024 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1025 format_vnet_sw_if_index_name, vnm,
1026 member->sw_if_index, member->sw_if_index,
1027 int_config->seq_num, member->shg,
1028 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1029 "-", i < bd_config->flood_count ? "*" : "-",
1030 format_vtr, vtr_opr, dot1q, tag1, tag2);
1034 if ((detail || arp) &&
1035 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1038 ip6_address_t *ip6_addr;
1040 vlib_cli_output (vm,
1041 "\n IP4/IP6 to MAC table for ARP Termination");
1044 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1046 vlib_cli_output (vm, "%=40U => %=20U",
1047 format_ip4_address, &ip4_addr,
1048 format_ethernet_address, &mac_addr);
1051 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1053 vlib_cli_output (vm, "%=40U => %=20U",
1054 format_ip6_address, ip6_addr,
1055 format_ethernet_address, &mac_addr);
1065 vlib_cli_output (vm, "no bridge-domains in use");
1073 * Show a summary of all the bridge-domain instances or detailed view of a
1074 * single bridge-domain. Bridge-domains are created by adding an interface
1075 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1079 * Example of displaying all bridge-domains:
1080 * @cliexstart{show bridge-domain}
1081 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1082 * 0 0 off off off off off local0
1083 * 200 1 on on on on off N/A
1086 * Example of displaying details of a single bridge-domains:
1087 * @cliexstart{show bridge-domain 200 detail}
1088 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1089 * 200 1 on on on on off N/A
1091 * Interface Index SHG BVI VLAN-Tag-Rewrite
1092 * GigabitEthernet0/8/0.200 3 0 - none
1093 * GigabitEthernet0/9/0.200 4 0 - none
1098 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1099 .path = "show bridge-domain",
1100 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1101 .function = bd_show,
1106 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1108 bd_main_t *bdm = &bd_main;
1109 vlib_main_t *vm = bdm->vlib_main;
1112 u32 bd_index = bd_find_index (bdm, a->bd_id);
1116 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1117 bd_index = bd_add_bd_index (bdm, a->bd_id);
1119 u32 enable_flags = 0, disable_flags = 0;
1121 enable_flags |= L2_FLOOD;
1123 disable_flags |= L2_FLOOD;
1126 enable_flags |= L2_UU_FLOOD;
1128 disable_flags |= L2_UU_FLOOD;
1131 enable_flags |= L2_FWD;
1133 disable_flags |= L2_FWD;
1136 enable_flags |= L2_LEARN;
1138 disable_flags |= L2_LEARN;
1141 enable_flags |= L2_ARP_TERM;
1143 disable_flags |= L2_ARP_TERM;
1146 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1149 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1151 bd_set_mac_age (vm, bd_index, a->mac_age);
1156 return VNET_API_ERROR_NO_SUCH_ENTRY;
1158 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1159 if (vec_len (l2input_main.bd_configs[bd_index].members))
1160 return VNET_API_ERROR_BD_IN_USE;
1161 rv = bd_delete (bdm, bd_index);
1168 Create or delete bridge-domain.
1170 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>]
1171 [flood <0|1>] [arp-term <0|1>] [mac-age <nn>] [del]
1174 static clib_error_t *
1175 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1176 vlib_cli_command_t * cmd)
1178 unformat_input_t _line_input, *line_input = &_line_input;
1179 clib_error_t *error = 0;
1182 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
1184 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1187 /* Get a line of input. */
1188 if (!unformat_user (input, unformat_line_input, line_input))
1191 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1193 if (unformat (line_input, "%d", &bd_id))
1195 else if (unformat (line_input, "flood %d", &flood))
1197 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1199 else if (unformat (line_input, "forward %d", &forward))
1201 else if (unformat (line_input, "learn %d", &learn))
1203 else if (unformat (line_input, "arp-term %d", &arp_term))
1205 else if (unformat (line_input, "mac-age %d", &mac_age))
1207 else if (unformat (line_input, "del"))
1210 flood = uu_flood = forward = learn = 0;
1218 error = clib_error_return (0, "bridge-domain-id not specified");
1224 error = clib_error_return (0, "bridge domain 0 can not be modified");
1230 error = clib_error_return (0, "mac age must be less than 256");
1234 memset (a, 0, sizeof (*a));
1237 a->flood = (u8) flood;
1238 a->uu_flood = (u8) uu_flood;
1239 a->forward = (u8) forward;
1240 a->learn = (u8) learn;
1241 a->arp_term = (u8) arp_term;
1242 a->mac_age = (u8) mac_age;
1244 rv = bd_add_del (a);
1250 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1252 case VNET_API_ERROR_BD_IN_USE:
1253 error = clib_error_return (0, "bridge domain in use - remove members");
1255 case VNET_API_ERROR_NO_SUCH_ENTRY:
1256 error = clib_error_return (0, "bridge domain id does not exist");
1258 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1259 error = clib_error_return (0, "bridge domain 0 can not be modified");
1262 error = clib_error_return (0, "bd_add_del returned %d", rv);
1267 unformat_free (line_input);
1274 * Create/Delete bridge-domain instance
1278 * Example of creating bridge-domain 1:
1279 * @cliexstart{create bridge-domain 1}
1283 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1284 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1287 * vpp# show bridge-domain
1288 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1289 * 0 0 0 off off off off off off local0
1290 * 1 1 0 off on on off on off N/A
1291 * 2 2 0 60 on on off on on N/A
1295 * Example of delete bridge-domain 1:
1296 * @cliexstart{create bridge-domain 1 del}
1302 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1303 .path = "create bridge-domain",
1304 .short_help = "create bridge-domain <bridge-domain-id>"
1305 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1306 " [mac-age <nn>] [del]",
1307 .function = bd_add_del_command_fn,
1314 * fd.io coding-style-patch-verification: ON
1317 * eval: (c-set-style "gnu")